New upstream version 18.08
[deb_dpdk.git] / examples / l3fwd-vf / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <stdint.h>
8 #include <inttypes.h>
9 #include <sys/types.h>
10 #include <string.h>
11 #include <sys/queue.h>
12 #include <stdarg.h>
13 #include <errno.h>
14 #include <getopt.h>
15 #include <signal.h>
16
17 #include <rte_common.h>
18 #include <rte_byteorder.h>
19 #include <rte_log.h>
20 #include <rte_memory.h>
21 #include <rte_memcpy.h>
22 #include <rte_eal.h>
23 #include <rte_launch.h>
24 #include <rte_atomic.h>
25 #include <rte_spinlock.h>
26 #include <rte_cycles.h>
27 #include <rte_prefetch.h>
28 #include <rte_lcore.h>
29 #include <rte_per_lcore.h>
30 #include <rte_branch_prediction.h>
31 #include <rte_interrupts.h>
32 #include <rte_random.h>
33 #include <rte_debug.h>
34 #include <rte_ether.h>
35 #include <rte_ethdev.h>
36 #include <rte_mempool.h>
37 #include <rte_mbuf.h>
38 #include <rte_ip.h>
39 #include <rte_tcp.h>
40 #include <rte_udp.h>
41 #include <rte_string_fns.h>
42
43 #define APP_LOOKUP_EXACT_MATCH          0
44 #define APP_LOOKUP_LPM                  1
45 #define DO_RFC_1812_CHECKS
46
47 //#define APP_LOOKUP_METHOD             APP_LOOKUP_EXACT_MATCH
48 #ifndef APP_LOOKUP_METHOD
49 #define APP_LOOKUP_METHOD             APP_LOOKUP_LPM
50 #endif
51
52 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
53 #include <rte_hash.h>
54 #elif (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
55 #include <rte_lpm.h>
56 #else
57 #error "APP_LOOKUP_METHOD set to incorrect value"
58 #endif
59
60 #define RTE_LOGTYPE_L3FWD RTE_LOGTYPE_USER1
61
62 #define MEMPOOL_CACHE_SIZE 256
63
64 /*
65  * This expression is used to calculate the number of mbufs needed depending on user input, taking
66  *  into account memory for rx and tx hardware rings, cache per lcore and mtable per port per lcore.
67  *  RTE_MAX is used to ensure that NB_MBUF never goes below a minimum value of 8192
68  */
69
70 #define NB_MBUF RTE_MAX (                                               \
71                                 (nb_ports*nb_rx_queue*nb_rxd +          \
72                                 nb_ports*nb_lcores*MAX_PKT_BURST +      \
73                                 nb_ports*n_tx_queue*nb_txd +            \
74                                 nb_lcores*MEMPOOL_CACHE_SIZE),          \
75                                 (unsigned)8192)
76
77 /*
78  * RX and TX Prefetch, Host, and Write-back threshold values should be
79  * carefully set for optimal performance. Consult the network
80  * controller's datasheet and supporting DPDK documentation for guidance
81  * on how these parameters should be set.
82  */
83 #define RX_PTHRESH 8 /**< Default values of RX prefetch threshold reg. */
84 #define RX_HTHRESH 8 /**< Default values of RX host threshold reg. */
85 #define RX_WTHRESH 4 /**< Default values of RX write-back threshold reg. */
86
87 /*
88  * These default values are optimized for use with the Intel(R) 82599 10 GbE
89  * Controller and the DPDK ixgbe PMD. Consider using other values for other
90  * network controllers and/or network drivers.
91  */
92 #define TX_PTHRESH 36 /**< Default values of TX prefetch threshold reg. */
93 #define TX_HTHRESH 0  /**< Default values of TX host threshold reg. */
94 #define TX_WTHRESH 0  /**< Default values of TX write-back threshold reg. */
95
96 #define MAX_PKT_BURST 32
97 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
98
99 #define NB_SOCKETS 8
100
101 #define SOCKET0 0
102
103 /* Configure how many packets ahead to prefetch, when reading packets */
104 #define PREFETCH_OFFSET 3
105
106 /*
107  * Configurable number of RX/TX ring descriptors
108  */
109 #define RTE_TEST_RX_DESC_DEFAULT 1024
110 #define RTE_TEST_TX_DESC_DEFAULT 1024
111 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
112 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
113
114 /* ethernet addresses of ports */
115 static struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
116
117 /* mask of enabled ports */
118 static uint32_t enabled_port_mask = 0;
119 static int numa_on = 1; /**< NUMA is enabled by default. */
120
121 struct mbuf_table {
122         uint16_t len;
123         struct rte_mbuf *m_table[MAX_PKT_BURST];
124 };
125
126 struct lcore_rx_queue {
127         uint16_t port_id;
128         uint8_t queue_id;
129 } __rte_cache_aligned;
130
131 #define MAX_RX_QUEUE_PER_LCORE 16
132 #define MAX_TX_QUEUE_PER_PORT 1
133 #define MAX_RX_QUEUE_PER_PORT 1
134
135 #define MAX_LCORE_PARAMS 1024
136 struct lcore_params {
137         uint16_t port_id;
138         uint8_t queue_id;
139         uint8_t lcore_id;
140 } __rte_cache_aligned;
141
142 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
143 static struct lcore_params lcore_params_array_default[] = {
144         {0, 0, 2},
145         {0, 1, 2},
146         {0, 2, 2},
147         {1, 0, 2},
148         {1, 1, 2},
149         {1, 2, 2},
150         {2, 0, 2},
151         {3, 0, 3},
152         {3, 1, 3},
153 };
154
155 static struct lcore_params * lcore_params = lcore_params_array_default;
156 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
157                                 sizeof(lcore_params_array_default[0]);
158
159 static struct rte_eth_conf port_conf = {
160         .rxmode = {
161                 .mq_mode        = ETH_MQ_RX_RSS,
162                 .max_rx_pkt_len = ETHER_MAX_LEN,
163                 .split_hdr_size = 0,
164                 .offloads = (DEV_RX_OFFLOAD_CRC_STRIP |
165                              DEV_RX_OFFLOAD_CHECKSUM),
166         },
167         .rx_adv_conf = {
168                 .rss_conf = {
169                         .rss_key = NULL,
170                         .rss_hf = ETH_RSS_IP,
171                 },
172         },
173         .txmode = {
174                 .mq_mode = ETH_MQ_TX_NONE,
175         },
176 };
177
178 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
179
180
181 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
182
183 #ifdef RTE_ARCH_X86
184 #include <rte_hash_crc.h>
185 #define DEFAULT_HASH_FUNC       rte_hash_crc
186 #else
187 #include <rte_jhash.h>
188 #define DEFAULT_HASH_FUNC       rte_jhash
189 #endif
190
191 struct ipv4_5tuple {
192         uint32_t ip_dst;
193         uint32_t ip_src;
194         uint16_t port_dst;
195         uint16_t port_src;
196         uint8_t proto;
197 } __attribute__((__packed__));
198
199 struct l3fwd_route {
200         struct ipv4_5tuple key;
201         uint8_t if_out;
202 };
203
204 static struct l3fwd_route l3fwd_route_array[] = {
205         {{IPv4(100,10,0,1), IPv4(200,10,0,1), 101, 11, IPPROTO_TCP}, 0},
206         {{IPv4(100,20,0,2), IPv4(200,20,0,2), 102, 12, IPPROTO_TCP}, 1},
207         {{IPv4(100,30,0,3), IPv4(200,30,0,3), 103, 13, IPPROTO_TCP}, 2},
208         {{IPv4(100,40,0,4), IPv4(200,40,0,4), 104, 14, IPPROTO_TCP}, 3},
209 };
210
211 typedef struct rte_hash lookup_struct_t;
212 static lookup_struct_t *l3fwd_lookup_struct[NB_SOCKETS];
213
214 #define L3FWD_HASH_ENTRIES      1024
215 struct rte_hash_parameters l3fwd_hash_params = {
216         .name = "l3fwd_hash_0",
217         .entries = L3FWD_HASH_ENTRIES,
218         .key_len = sizeof(struct ipv4_5tuple),
219         .hash_func = DEFAULT_HASH_FUNC,
220         .hash_func_init_val = 0,
221         .socket_id = SOCKET0,
222 };
223
224 #define L3FWD_NUM_ROUTES \
225         (sizeof(l3fwd_route_array) / sizeof(l3fwd_route_array[0]))
226
227 static uint8_t l3fwd_out_if[L3FWD_HASH_ENTRIES] __rte_cache_aligned;
228 #endif
229
230 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
231 struct l3fwd_route {
232         uint32_t ip;
233         uint8_t  depth;
234         uint8_t  if_out;
235 };
236
237 static struct l3fwd_route l3fwd_route_array[] = {
238         {IPv4(1,1,1,0), 24, 0},
239         {IPv4(2,1,1,0), 24, 1},
240         {IPv4(3,1,1,0), 24, 2},
241         {IPv4(4,1,1,0), 24, 3},
242         {IPv4(5,1,1,0), 24, 4},
243         {IPv4(6,1,1,0), 24, 5},
244         {IPv4(7,1,1,0), 24, 6},
245         {IPv4(8,1,1,0), 24, 7},
246 };
247
248 #define L3FWD_NUM_ROUTES \
249         (sizeof(l3fwd_route_array) / sizeof(l3fwd_route_array[0]))
250
251 #define L3FWD_LPM_MAX_RULES     1024
252
253 typedef struct rte_lpm lookup_struct_t;
254 static lookup_struct_t *l3fwd_lookup_struct[NB_SOCKETS];
255 #endif
256
257 struct lcore_conf {
258         uint16_t n_rx_queue;
259         struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
260         uint16_t tx_queue_id;
261         struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
262         lookup_struct_t * lookup_struct;
263 } __rte_cache_aligned;
264
265 static struct lcore_conf lcore_conf[RTE_MAX_LCORE];
266 static rte_spinlock_t spinlock_conf[RTE_MAX_ETHPORTS] = {RTE_SPINLOCK_INITIALIZER};
267 /* Send burst of packets on an output interface */
268 static inline int
269 send_burst(struct lcore_conf *qconf, uint16_t n, uint16_t port)
270 {
271         struct rte_mbuf **m_table;
272         int ret;
273         uint16_t queueid;
274
275         queueid = qconf->tx_queue_id;
276         m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
277
278         rte_spinlock_lock(&spinlock_conf[port]);
279         ret = rte_eth_tx_burst(port, queueid, m_table, n);
280         rte_spinlock_unlock(&spinlock_conf[port]);
281
282         if (unlikely(ret < n)) {
283                 do {
284                         rte_pktmbuf_free(m_table[ret]);
285                 } while (++ret < n);
286         }
287
288         return 0;
289 }
290
291 /* Enqueue a single packet, and send burst if queue is filled */
292 static inline int
293 send_single_packet(struct rte_mbuf *m, uint16_t port)
294 {
295         uint32_t lcore_id;
296         uint16_t len;
297         struct lcore_conf *qconf;
298
299         lcore_id = rte_lcore_id();
300
301         qconf = &lcore_conf[lcore_id];
302         len = qconf->tx_mbufs[port].len;
303         qconf->tx_mbufs[port].m_table[len] = m;
304         len++;
305
306         /* enough pkts to be sent */
307         if (unlikely(len == MAX_PKT_BURST)) {
308                 send_burst(qconf, MAX_PKT_BURST, port);
309                 len = 0;
310         }
311
312         qconf->tx_mbufs[port].len = len;
313         return 0;
314 }
315
316 #ifdef DO_RFC_1812_CHECKS
317 static inline int
318 is_valid_ipv4_pkt(struct ipv4_hdr *pkt, uint32_t link_len)
319 {
320         /* From http://www.rfc-editor.org/rfc/rfc1812.txt section 5.2.2 */
321         /*
322          * 1. The packet length reported by the Link Layer must be large
323          * enough to hold the minimum length legal IP datagram (20 bytes).
324          */
325         if (link_len < sizeof(struct ipv4_hdr))
326                 return -1;
327
328         /* 2. The IP checksum must be correct. */
329         /* this is checked in H/W */
330
331         /*
332          * 3. The IP version number must be 4. If the version number is not 4
333          * then the packet may be another version of IP, such as IPng or
334          * ST-II.
335          */
336         if (((pkt->version_ihl) >> 4) != 4)
337                 return -3;
338         /*
339          * 4. The IP header length field must be large enough to hold the
340          * minimum length legal IP datagram (20 bytes = 5 words).
341          */
342         if ((pkt->version_ihl & 0xf) < 5)
343                 return -4;
344
345         /*
346          * 5. The IP total length field must be large enough to hold the IP
347          * datagram header, whose length is specified in the IP header length
348          * field.
349          */
350         if (rte_cpu_to_be_16(pkt->total_length) < sizeof(struct ipv4_hdr))
351                 return -5;
352
353         return 0;
354 }
355 #endif
356
357 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
358 static void
359 print_key(struct ipv4_5tuple key)
360 {
361         printf("IP dst = %08x, IP src = %08x, port dst = %d, port src = %d, proto = %d\n",
362                (unsigned)key.ip_dst, (unsigned)key.ip_src, key.port_dst, key.port_src, key.proto);
363 }
364
365 static inline uint16_t
366 get_dst_port(struct ipv4_hdr *ipv4_hdr, uint16_t portid,
367               lookup_struct_t *l3fwd_lookup_struct)
368 {
369         struct ipv4_5tuple key;
370         struct tcp_hdr *tcp;
371         struct udp_hdr *udp;
372         int ret = 0;
373
374         key.ip_dst = rte_be_to_cpu_32(ipv4_hdr->dst_addr);
375         key.ip_src = rte_be_to_cpu_32(ipv4_hdr->src_addr);
376         key.proto = ipv4_hdr->next_proto_id;
377
378         switch (ipv4_hdr->next_proto_id) {
379         case IPPROTO_TCP:
380                 tcp = (struct tcp_hdr *)((unsigned char *) ipv4_hdr +
381                                         sizeof(struct ipv4_hdr));
382                 key.port_dst = rte_be_to_cpu_16(tcp->dst_port);
383                 key.port_src = rte_be_to_cpu_16(tcp->src_port);
384                 break;
385
386         case IPPROTO_UDP:
387                 udp = (struct udp_hdr *)((unsigned char *) ipv4_hdr +
388                                         sizeof(struct ipv4_hdr));
389                 key.port_dst = rte_be_to_cpu_16(udp->dst_port);
390                 key.port_src = rte_be_to_cpu_16(udp->src_port);
391                 break;
392
393         default:
394                 key.port_dst = 0;
395                 key.port_src = 0;
396         }
397
398         /* Find destination port */
399         ret = rte_hash_lookup(l3fwd_lookup_struct, (const void *)&key);
400         return ((ret < 0) ? portid : l3fwd_out_if[ret]);
401 }
402 #endif
403
404 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
405 static inline uint32_t
406 get_dst_port(struct ipv4_hdr *ipv4_hdr, uint16_t portid,
407               lookup_struct_t *l3fwd_lookup_struct)
408 {
409         uint32_t next_hop;
410
411         return ((rte_lpm_lookup(l3fwd_lookup_struct,
412                 rte_be_to_cpu_32(ipv4_hdr->dst_addr), &next_hop) == 0) ?
413                 next_hop : portid);
414 }
415 #endif
416
417 static inline void
418 l3fwd_simple_forward(struct rte_mbuf *m, uint16_t portid,
419                       lookup_struct_t *l3fwd_lookup_struct)
420 {
421         struct ether_hdr *eth_hdr;
422         struct ipv4_hdr *ipv4_hdr;
423         void *tmp;
424         uint16_t dst_port;
425
426         eth_hdr = rte_pktmbuf_mtod(m, struct ether_hdr *);
427
428         ipv4_hdr = rte_pktmbuf_mtod_offset(m, struct ipv4_hdr *,
429                                            sizeof(struct ether_hdr));
430
431 #ifdef DO_RFC_1812_CHECKS
432         /* Check to make sure the packet is valid (RFC1812) */
433         if (is_valid_ipv4_pkt(ipv4_hdr, m->pkt_len) < 0) {
434                 rte_pktmbuf_free(m);
435                 return;
436         }
437 #endif
438
439         dst_port = get_dst_port(ipv4_hdr, portid, l3fwd_lookup_struct);
440         if (dst_port >= RTE_MAX_ETHPORTS || (enabled_port_mask & 1 << dst_port) == 0)
441                 dst_port = portid;
442
443         /* 02:00:00:00:00:xx */
444         tmp = &eth_hdr->d_addr.addr_bytes[0];
445         *((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
446
447 #ifdef DO_RFC_1812_CHECKS
448         /* Update time to live and header checksum */
449         --(ipv4_hdr->time_to_live);
450         ++(ipv4_hdr->hdr_checksum);
451 #endif
452
453         /* src addr */
454         ether_addr_copy(&ports_eth_addr[dst_port], &eth_hdr->s_addr);
455
456         send_single_packet(m, dst_port);
457
458 }
459
460 /* main processing loop */
461 static int
462 main_loop(__attribute__((unused)) void *dummy)
463 {
464         struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
465         unsigned lcore_id;
466         uint64_t prev_tsc, diff_tsc, cur_tsc;
467         int i, j, nb_rx;
468         uint8_t queueid;
469         uint16_t portid;
470         struct lcore_conf *qconf;
471         const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
472
473         prev_tsc = 0;
474
475         lcore_id = rte_lcore_id();
476         qconf = &lcore_conf[lcore_id];
477
478         if (qconf->n_rx_queue == 0) {
479                 RTE_LOG(INFO, L3FWD, "lcore %u has nothing to do\n", lcore_id);
480                 return 0;
481         }
482
483         RTE_LOG(INFO, L3FWD, "entering main loop on lcore %u\n", lcore_id);
484
485         for (i = 0; i < qconf->n_rx_queue; i++) {
486
487                 portid = qconf->rx_queue_list[i].port_id;
488                 queueid = qconf->rx_queue_list[i].queue_id;
489                 RTE_LOG(INFO, L3FWD, " --lcoreid=%u portid=%u rxqueueid=%hhu\n",
490                 lcore_id, portid, queueid);
491         }
492
493         while (1) {
494
495                 cur_tsc = rte_rdtsc();
496
497                 /*
498                  * TX burst queue drain
499                  */
500                 diff_tsc = cur_tsc - prev_tsc;
501                 if (unlikely(diff_tsc > drain_tsc)) {
502
503                         /*
504                          * This could be optimized (use queueid instead of
505                          * portid), but it is not called so often
506                          */
507                         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
508                                 if (qconf->tx_mbufs[portid].len == 0)
509                                         continue;
510                                 send_burst(&lcore_conf[lcore_id],
511                                         qconf->tx_mbufs[portid].len,
512                                         portid);
513                                 qconf->tx_mbufs[portid].len = 0;
514                         }
515
516                         prev_tsc = cur_tsc;
517                 }
518
519                 /*
520                  * Read packet from RX queues
521                  */
522                 for (i = 0; i < qconf->n_rx_queue; ++i) {
523
524                         portid = qconf->rx_queue_list[i].port_id;
525                         queueid = qconf->rx_queue_list[i].queue_id;
526                         nb_rx = rte_eth_rx_burst(portid, queueid, pkts_burst, MAX_PKT_BURST);
527
528                         /* Prefetch first packets */
529                         for (j = 0; j < PREFETCH_OFFSET && j < nb_rx; j++) {
530                                 rte_prefetch0(rte_pktmbuf_mtod(
531                                                 pkts_burst[j], void *));
532                         }
533
534                         /* Prefetch and forward already prefetched packets */
535                         for (j = 0; j < (nb_rx - PREFETCH_OFFSET); j++) {
536                                 rte_prefetch0(rte_pktmbuf_mtod(pkts_burst[
537                                                 j + PREFETCH_OFFSET], void *));
538                                 l3fwd_simple_forward(pkts_burst[j], portid, qconf->lookup_struct);
539                         }
540
541                         /* Forward remaining prefetched packets */
542                         for (; j < nb_rx; j++) {
543                                 l3fwd_simple_forward(pkts_burst[j], portid, qconf->lookup_struct);
544                         }
545                 }
546         }
547 }
548
549 static int
550 check_lcore_params(void)
551 {
552         uint8_t queue, lcore;
553         uint16_t i;
554         int socketid;
555
556         for (i = 0; i < nb_lcore_params; ++i) {
557                 queue = lcore_params[i].queue_id;
558                 if (queue >= MAX_RX_QUEUE_PER_PORT) {
559                         printf("invalid queue number: %hhu\n", queue);
560                         return -1;
561                 }
562                 lcore = lcore_params[i].lcore_id;
563                 if (!rte_lcore_is_enabled(lcore)) {
564                         printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
565                         return -1;
566                 }
567                 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
568                         (numa_on == 0)) {
569                         printf("warning: lcore %hhu is on socket %d with numa off \n",
570                                 lcore, socketid);
571                 }
572         }
573         return 0;
574 }
575
576 static int
577 check_port_config(void)
578 {
579         unsigned portid;
580         uint16_t i;
581
582         for (i = 0; i < nb_lcore_params; ++i) {
583                 portid = lcore_params[i].port_id;
584                 if ((enabled_port_mask & (1 << portid)) == 0) {
585                         printf("port %u is not enabled in port mask\n", portid);
586                         return -1;
587                 }
588                 if (!rte_eth_dev_is_valid_port(portid)) {
589                         printf("port %u is not present on the board\n", portid);
590                         return -1;
591                 }
592         }
593         return 0;
594 }
595
596 static uint8_t
597 get_port_n_rx_queues(const uint16_t port)
598 {
599         int queue = -1;
600         uint16_t i;
601
602         for (i = 0; i < nb_lcore_params; ++i) {
603                 if (lcore_params[i].port_id == port && lcore_params[i].queue_id > queue)
604                         queue = lcore_params[i].queue_id;
605         }
606         return (uint8_t)(++queue);
607 }
608
609 static int
610 init_lcore_rx_queues(void)
611 {
612         uint16_t i, nb_rx_queue;
613         uint8_t lcore;
614
615         for (i = 0; i < nb_lcore_params; ++i) {
616                 lcore = lcore_params[i].lcore_id;
617                 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
618                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
619                         printf("error: too many queues (%u) for lcore: %u\n",
620                                 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
621                         return -1;
622                 } else {
623                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
624                                 lcore_params[i].port_id;
625                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
626                                 lcore_params[i].queue_id;
627                         lcore_conf[lcore].n_rx_queue++;
628                 }
629         }
630         return 0;
631 }
632
633 /* display usage */
634 static void
635 print_usage(const char *prgname)
636 {
637         printf ("%s [EAL options] -- -p PORTMASK"
638                 "  [--config (port,queue,lcore)[,(port,queue,lcore]]\n"
639                 "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
640                 "  --config (port,queue,lcore): rx queues configuration\n"
641                 "  --no-numa: optional, disable numa awareness\n",
642                 prgname);
643 }
644
645 /* Custom handling of signals to handle process terminal */
646 static void
647 signal_handler(int signum)
648 {
649         uint16_t portid;
650
651         /* When we receive a SIGINT signal */
652         if (signum == SIGINT) {
653                 RTE_ETH_FOREACH_DEV(portid) {
654                         /* skip ports that are not enabled */
655                         if ((enabled_port_mask & (1 << portid)) == 0)
656                                 continue;
657                         rte_eth_dev_close(portid);
658                 }
659         }
660         rte_exit(EXIT_SUCCESS, "\n User forced exit\n");
661 }
662 static int
663 parse_portmask(const char *portmask)
664 {
665         char *end = NULL;
666         unsigned long pm;
667
668         /* parse hexadecimal string */
669         pm = strtoul(portmask, &end, 16);
670         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
671                 return -1;
672
673         if (pm == 0)
674                 return -1;
675
676         return pm;
677 }
678
679 static int
680 parse_config(const char *q_arg)
681 {
682         char s[256];
683         const char *p, *p0 = q_arg;
684         char *end;
685         enum fieldnames {
686                 FLD_PORT = 0,
687                 FLD_QUEUE,
688                 FLD_LCORE,
689                 _NUM_FLD
690         };
691         unsigned long int_fld[_NUM_FLD];
692         char *str_fld[_NUM_FLD];
693         int i;
694         unsigned size;
695
696         nb_lcore_params = 0;
697
698         while ((p = strchr(p0,'(')) != NULL) {
699                 ++p;
700                 if((p0 = strchr(p,')')) == NULL)
701                         return -1;
702
703                 size = p0 - p;
704                 if(size >= sizeof(s))
705                         return -1;
706
707                 snprintf(s, sizeof(s), "%.*s", size, p);
708                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
709                         return -1;
710                 for (i = 0; i < _NUM_FLD; i++){
711                         errno = 0;
712                         int_fld[i] = strtoul(str_fld[i], &end, 0);
713                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
714                                 return -1;
715                 }
716                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
717                         printf("exceeded max number of lcore params: %hu\n",
718                                 nb_lcore_params);
719                         return -1;
720                 }
721                 lcore_params_array[nb_lcore_params].port_id = int_fld[FLD_PORT];
722                 lcore_params_array[nb_lcore_params].queue_id = (uint8_t)int_fld[FLD_QUEUE];
723                 lcore_params_array[nb_lcore_params].lcore_id = (uint8_t)int_fld[FLD_LCORE];
724                 ++nb_lcore_params;
725         }
726         lcore_params = lcore_params_array;
727         return 0;
728 }
729
730 /* Parse the argument given in the command line of the application */
731 static int
732 parse_args(int argc, char **argv)
733 {
734         int opt, ret;
735         char **argvopt;
736         int option_index;
737         char *prgname = argv[0];
738         static struct option lgopts[] = {
739                 {"config", 1, 0, 0},
740                 {"no-numa", 0, 0, 0},
741                 {NULL, 0, 0, 0}
742         };
743
744         argvopt = argv;
745
746         while ((opt = getopt_long(argc, argvopt, "p:",
747                                 lgopts, &option_index)) != EOF) {
748
749                 switch (opt) {
750                 /* portmask */
751                 case 'p':
752                         enabled_port_mask = parse_portmask(optarg);
753                         if (enabled_port_mask == 0) {
754                                 printf("invalid portmask\n");
755                                 print_usage(prgname);
756                                 return -1;
757                         }
758                         break;
759
760                 /* long options */
761                 case 0:
762                         if (!strcmp(lgopts[option_index].name, "config")) {
763                                 ret = parse_config(optarg);
764                                 if (ret) {
765                                         printf("invalid config\n");
766                                         print_usage(prgname);
767                                         return -1;
768                                 }
769                         }
770
771                         if (!strcmp(lgopts[option_index].name, "no-numa")) {
772                                 printf("numa is disabled \n");
773                                 numa_on = 0;
774                         }
775                         break;
776
777                 default:
778                         print_usage(prgname);
779                         return -1;
780                 }
781         }
782
783         if (optind >= 0)
784                 argv[optind-1] = prgname;
785
786         ret = optind-1;
787         optind = 1; /* reset getopt lib */
788         return ret;
789 }
790
791 static void
792 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
793 {
794         char buf[ETHER_ADDR_FMT_SIZE];
795         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
796         printf("%s%s", name, buf);
797 }
798
799 #if (APP_LOOKUP_METHOD == APP_LOOKUP_EXACT_MATCH)
800 static void
801 setup_hash(int socketid)
802 {
803         unsigned i;
804         int ret;
805         char s[64];
806
807         /* create  hashes */
808         snprintf(s, sizeof(s), "l3fwd_hash_%d", socketid);
809         l3fwd_hash_params.name = s;
810         l3fwd_hash_params.socket_id = socketid;
811         l3fwd_lookup_struct[socketid] = rte_hash_create(&l3fwd_hash_params);
812         if (l3fwd_lookup_struct[socketid] == NULL)
813                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd hash on "
814                                 "socket %d\n", socketid);
815
816         /* populate the hash */
817         for (i = 0; i < L3FWD_NUM_ROUTES; i++) {
818                 ret = rte_hash_add_key (l3fwd_lookup_struct[socketid],
819                                 (void *) &l3fwd_route_array[i].key);
820                 if (ret < 0) {
821                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the"
822                                 "l3fwd hash on socket %d\n", i, socketid);
823                 }
824                 l3fwd_out_if[ret] = l3fwd_route_array[i].if_out;
825                 printf("Hash: Adding key\n");
826                 print_key(l3fwd_route_array[i].key);
827         }
828 }
829 #endif
830
831 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
832 static void
833 setup_lpm(int socketid)
834 {
835         unsigned i;
836         int ret;
837         char s[64];
838
839         struct rte_lpm_config lpm_ipv4_config;
840
841         lpm_ipv4_config.max_rules = L3FWD_LPM_MAX_RULES;
842         lpm_ipv4_config.number_tbl8s = 256;
843         lpm_ipv4_config.flags = 0;
844
845         /* create the LPM table */
846         snprintf(s, sizeof(s), "L3FWD_LPM_%d", socketid);
847         l3fwd_lookup_struct[socketid] =
848                         rte_lpm_create(s, socketid, &lpm_ipv4_config);
849         if (l3fwd_lookup_struct[socketid] == NULL)
850                 rte_exit(EXIT_FAILURE, "Unable to create the l3fwd LPM table"
851                                 " on socket %d\n", socketid);
852
853         /* populate the LPM table */
854         for (i = 0; i < L3FWD_NUM_ROUTES; i++) {
855                 ret = rte_lpm_add(l3fwd_lookup_struct[socketid],
856                         l3fwd_route_array[i].ip,
857                         l3fwd_route_array[i].depth,
858                         l3fwd_route_array[i].if_out);
859
860                 if (ret < 0) {
861                         rte_exit(EXIT_FAILURE, "Unable to add entry %u to the "
862                                 "l3fwd LPM table on socket %d\n",
863                                 i, socketid);
864                 }
865
866                 printf("LPM: Adding route 0x%08x / %d (%d)\n",
867                         (unsigned)l3fwd_route_array[i].ip,
868                         l3fwd_route_array[i].depth,
869                         l3fwd_route_array[i].if_out);
870         }
871 }
872 #endif
873
874 static int
875 init_mem(unsigned nb_mbuf)
876 {
877         struct lcore_conf *qconf;
878         int socketid;
879         unsigned lcore_id;
880         char s[64];
881
882         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
883                 if (rte_lcore_is_enabled(lcore_id) == 0)
884                         continue;
885
886                 if (numa_on)
887                         socketid = rte_lcore_to_socket_id(lcore_id);
888                 else
889                         socketid = 0;
890
891                 if (socketid >= NB_SOCKETS) {
892                         rte_exit(EXIT_FAILURE, "Socket %d of lcore %u is out of range %d\n",
893                                 socketid, lcore_id, NB_SOCKETS);
894                 }
895                 if (pktmbuf_pool[socketid] == NULL) {
896                         snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
897                         pktmbuf_pool[socketid] = rte_pktmbuf_pool_create(s,
898                                 nb_mbuf, MEMPOOL_CACHE_SIZE, 0,
899                                 RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
900                         if (pktmbuf_pool[socketid] == NULL)
901                                 rte_exit(EXIT_FAILURE, "Cannot init mbuf pool on socket %d\n", socketid);
902                         else
903                                 printf("Allocated mbuf pool on socket %d\n", socketid);
904
905 #if (APP_LOOKUP_METHOD == APP_LOOKUP_LPM)
906                         setup_lpm(socketid);
907 #else
908                         setup_hash(socketid);
909 #endif
910                 }
911                 qconf = &lcore_conf[lcore_id];
912                 qconf->lookup_struct = l3fwd_lookup_struct[socketid];
913         }
914         return 0;
915 }
916
917 int
918 main(int argc, char **argv)
919 {
920         struct lcore_conf *qconf;
921         struct rte_eth_dev_info dev_info;
922         struct rte_eth_txconf *txconf;
923         int ret;
924         unsigned nb_ports;
925         uint16_t queueid, portid;
926         unsigned lcore_id;
927         uint32_t nb_lcores;
928         uint16_t n_tx_queue;
929         uint8_t nb_rx_queue, queue, socketid;
930
931         signal(SIGINT, signal_handler);
932         /* init EAL */
933         ret = rte_eal_init(argc, argv);
934         if (ret < 0)
935                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
936         argc -= ret;
937         argv += ret;
938
939         /* parse application arguments (after the EAL ones) */
940         ret = parse_args(argc, argv);
941         if (ret < 0)
942                 rte_exit(EXIT_FAILURE, "Invalid L3FWD-VF parameters\n");
943
944         if (check_lcore_params() < 0)
945                 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
946
947         ret = init_lcore_rx_queues();
948         if (ret < 0)
949                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
950
951         nb_ports = rte_eth_dev_count_avail();
952
953         if (check_port_config() < 0)
954                 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
955
956         nb_lcores = rte_lcore_count();
957
958         /* initialize all ports */
959         RTE_ETH_FOREACH_DEV(portid) {
960                 struct rte_eth_conf local_port_conf = port_conf;
961
962                 /* skip ports that are not enabled */
963                 if ((enabled_port_mask & (1 << portid)) == 0) {
964                         printf("\nSkipping disabled port %d\n", portid);
965                         continue;
966                 }
967
968                 /* init port */
969                 printf("Initializing port %d ... ", portid );
970                 fflush(stdout);
971
972                 /* must always equal(=1) */
973                 nb_rx_queue = get_port_n_rx_queues(portid);
974                 n_tx_queue = MAX_TX_QUEUE_PER_PORT;
975
976                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
977                         nb_rx_queue, (unsigned)1 );
978
979                 rte_eth_dev_info_get(portid, &dev_info);
980                 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
981                         local_port_conf.txmode.offloads |=
982                                 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
983
984                 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
985                         dev_info.flow_type_rss_offloads;
986                 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
987                                 port_conf.rx_adv_conf.rss_conf.rss_hf) {
988                         printf("Port %u modified RSS hash function based on hardware support,"
989                                 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
990                                 portid,
991                                 port_conf.rx_adv_conf.rss_conf.rss_hf,
992                                 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
993                 }
994
995                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
996                                             n_tx_queue, &local_port_conf);
997                 if (ret < 0)
998                         rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%d\n",
999                                 ret, portid);
1000
1001                 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
1002                                                        &nb_txd);
1003                 if (ret < 0)
1004                         rte_exit(EXIT_FAILURE,
1005                                  "Cannot adjust number of descriptors: err=%d, port=%d\n",
1006                                  ret, portid);
1007
1008                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1009                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1010                 printf(", ");
1011
1012                 ret = init_mem(NB_MBUF);
1013                 if (ret < 0)
1014                         rte_exit(EXIT_FAILURE, "init_mem failed\n");
1015
1016                 /* init one TX queue */
1017                 socketid = (uint8_t)rte_lcore_to_socket_id(rte_get_master_lcore());
1018
1019                 printf("txq=%d,%d,%d ", portid, 0, socketid);
1020                 fflush(stdout);
1021
1022                 txconf = &dev_info.default_txconf;
1023                 txconf->offloads = local_port_conf.txmode.offloads;
1024                 ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
1025                                                  socketid, txconf);
1026                 if (ret < 0)
1027                         rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d, "
1028                                 "port=%d\n", ret, portid);
1029
1030                 printf("\n");
1031         }
1032
1033         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1034                 struct rte_eth_rxconf rxq_conf;
1035
1036                 if (rte_lcore_is_enabled(lcore_id) == 0)
1037                         continue;
1038                 qconf = &lcore_conf[lcore_id];
1039                 qconf->tx_queue_id = 0;
1040
1041                 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
1042                 fflush(stdout);
1043                 /* init RX queues */
1044                 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
1045                         struct rte_eth_dev *dev;
1046                         struct rte_eth_conf *conf;
1047
1048                         portid = qconf->rx_queue_list[queue].port_id;
1049                         queueid = qconf->rx_queue_list[queue].queue_id;
1050                         dev = &rte_eth_devices[portid];
1051                         conf = &dev->data->dev_conf;
1052
1053                         if (numa_on)
1054                                 socketid = (uint8_t)rte_lcore_to_socket_id(lcore_id);
1055                         else
1056                                 socketid = 0;
1057
1058                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
1059                         fflush(stdout);
1060
1061                         rte_eth_dev_info_get(portid, &dev_info);
1062                         rxq_conf = dev_info.default_rxconf;
1063                         rxq_conf.offloads = conf->rxmode.offloads;
1064                         ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
1065                                                 socketid, &rxq_conf,
1066                                                 pktmbuf_pool[socketid]);
1067                         if (ret < 0)
1068                                 rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d,"
1069                                                 "port=%d\n", ret, portid);
1070                 }
1071         }
1072         printf("\n");
1073
1074         /* start ports */
1075         RTE_ETH_FOREACH_DEV(portid) {
1076                 if ((enabled_port_mask & (1 << portid)) == 0) {
1077                         continue;
1078                 }
1079                 /* Start device */
1080                 ret = rte_eth_dev_start(portid);
1081                 if (ret < 0)
1082                         rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%d\n",
1083                                 ret, portid);
1084
1085                 printf("done: Port %d\n", portid);
1086
1087         }
1088
1089         /* launch per-lcore init on every lcore */
1090         rte_eal_mp_remote_launch(main_loop, NULL, CALL_MASTER);
1091         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1092                 if (rte_eal_wait_lcore(lcore_id) < 0)
1093                         return -1;
1094         }
1095
1096         return 0;
1097 }