e7111fa18c404a57596566478de8150754d2e368
[dpdk.git] / examples / l3fwd / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 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 #include <stdbool.h>
17
18 #include <rte_common.h>
19 #include <rte_vect.h>
20 #include <rte_byteorder.h>
21 #include <rte_log.h>
22 #include <rte_memory.h>
23 #include <rte_memcpy.h>
24 #include <rte_eal.h>
25 #include <rte_launch.h>
26 #include <rte_atomic.h>
27 #include <rte_cycles.h>
28 #include <rte_prefetch.h>
29 #include <rte_lcore.h>
30 #include <rte_per_lcore.h>
31 #include <rte_branch_prediction.h>
32 #include <rte_interrupts.h>
33 #include <rte_random.h>
34 #include <rte_debug.h>
35 #include <rte_ether.h>
36 #include <rte_ethdev.h>
37 #include <rte_mempool.h>
38 #include <rte_mbuf.h>
39 #include <rte_ip.h>
40 #include <rte_tcp.h>
41 #include <rte_udp.h>
42 #include <rte_string_fns.h>
43 #include <rte_cpuflags.h>
44
45 #include <cmdline_parse.h>
46 #include <cmdline_parse_etheraddr.h>
47
48 #include "l3fwd.h"
49
50 /*
51  * Configurable number of RX/TX ring descriptors
52  */
53 #define RTE_TEST_RX_DESC_DEFAULT 1024
54 #define RTE_TEST_TX_DESC_DEFAULT 1024
55
56 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
57 #define MAX_RX_QUEUE_PER_PORT 128
58
59 #define MAX_LCORE_PARAMS 1024
60
61 /* Static global variables used within this file. */
62 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
63 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
64
65 /**< Ports set in promiscuous mode off by default. */
66 static int promiscuous_on;
67
68 /* Select Longest-Prefix or Exact match. */
69 static int l3fwd_lpm_on;
70 static int l3fwd_em_on;
71
72 static int numa_on = 1; /**< NUMA is enabled by default. */
73 static int parse_ptype; /**< Parse packet type using rx callback, and */
74                         /**< disabled by default */
75
76 /* Global variables. */
77
78 volatile bool force_quit;
79
80 /* ethernet addresses of ports */
81 uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
82 struct ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
83
84 xmm_t val_eth[RTE_MAX_ETHPORTS];
85
86 /* mask of enabled ports */
87 uint32_t enabled_port_mask;
88
89 /* Used only in exact match mode. */
90 int ipv6; /**< ipv6 is false by default. */
91 uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
92
93 struct lcore_conf lcore_conf[RTE_MAX_LCORE];
94
95 struct lcore_params {
96         uint16_t port_id;
97         uint8_t queue_id;
98         uint8_t lcore_id;
99 } __rte_cache_aligned;
100
101 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
102 static struct lcore_params lcore_params_array_default[] = {
103         {0, 0, 2},
104         {0, 1, 2},
105         {0, 2, 2},
106         {1, 0, 2},
107         {1, 1, 2},
108         {1, 2, 2},
109         {2, 0, 2},
110         {3, 0, 3},
111         {3, 1, 3},
112 };
113
114 static struct lcore_params * lcore_params = lcore_params_array_default;
115 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
116                                 sizeof(lcore_params_array_default[0]);
117
118 static struct rte_eth_conf port_conf = {
119         .rxmode = {
120                 .mq_mode = ETH_MQ_RX_RSS,
121                 .max_rx_pkt_len = ETHER_MAX_LEN,
122                 .split_hdr_size = 0,
123                 .ignore_offload_bitfield = 1,
124                 .offloads = (DEV_RX_OFFLOAD_CRC_STRIP |
125                              DEV_RX_OFFLOAD_CHECKSUM),
126         },
127         .rx_adv_conf = {
128                 .rss_conf = {
129                         .rss_key = NULL,
130                         .rss_hf = ETH_RSS_IP,
131                 },
132         },
133         .txmode = {
134                 .mq_mode = ETH_MQ_TX_NONE,
135         },
136 };
137
138 static struct rte_mempool * pktmbuf_pool[NB_SOCKETS];
139
140 struct l3fwd_lkp_mode {
141         void  (*setup)(int);
142         int   (*check_ptype)(int);
143         rte_rx_callback_fn cb_parse_ptype;
144         int   (*main_loop)(void *);
145         void* (*get_ipv4_lookup_struct)(int);
146         void* (*get_ipv6_lookup_struct)(int);
147 };
148
149 static struct l3fwd_lkp_mode l3fwd_lkp;
150
151 static struct l3fwd_lkp_mode l3fwd_em_lkp = {
152         .setup                  = setup_hash,
153         .check_ptype            = em_check_ptype,
154         .cb_parse_ptype         = em_cb_parse_ptype,
155         .main_loop              = em_main_loop,
156         .get_ipv4_lookup_struct = em_get_ipv4_l3fwd_lookup_struct,
157         .get_ipv6_lookup_struct = em_get_ipv6_l3fwd_lookup_struct,
158 };
159
160 static struct l3fwd_lkp_mode l3fwd_lpm_lkp = {
161         .setup                  = setup_lpm,
162         .check_ptype            = lpm_check_ptype,
163         .cb_parse_ptype         = lpm_cb_parse_ptype,
164         .main_loop              = lpm_main_loop,
165         .get_ipv4_lookup_struct = lpm_get_ipv4_l3fwd_lookup_struct,
166         .get_ipv6_lookup_struct = lpm_get_ipv6_l3fwd_lookup_struct,
167 };
168
169 /*
170  * Setup lookup methods for forwarding.
171  * Currently exact-match and longest-prefix-match
172  * are supported ones.
173  */
174 static void
175 setup_l3fwd_lookup_tables(void)
176 {
177         /* Setup HASH lookup functions. */
178         if (l3fwd_em_on)
179                 l3fwd_lkp = l3fwd_em_lkp;
180         /* Setup LPM lookup functions. */
181         else
182                 l3fwd_lkp = l3fwd_lpm_lkp;
183 }
184
185 static int
186 check_lcore_params(void)
187 {
188         uint8_t queue, lcore;
189         uint16_t i;
190         int socketid;
191
192         for (i = 0; i < nb_lcore_params; ++i) {
193                 queue = lcore_params[i].queue_id;
194                 if (queue >= MAX_RX_QUEUE_PER_PORT) {
195                         printf("invalid queue number: %hhu\n", queue);
196                         return -1;
197                 }
198                 lcore = lcore_params[i].lcore_id;
199                 if (!rte_lcore_is_enabled(lcore)) {
200                         printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
201                         return -1;
202                 }
203                 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
204                         (numa_on == 0)) {
205                         printf("warning: lcore %hhu is on socket %d with numa off \n",
206                                 lcore, socketid);
207                 }
208         }
209         return 0;
210 }
211
212 static int
213 check_port_config(const unsigned nb_ports)
214 {
215         uint16_t portid;
216         uint16_t i;
217
218         for (i = 0; i < nb_lcore_params; ++i) {
219                 portid = lcore_params[i].port_id;
220                 if ((enabled_port_mask & (1 << portid)) == 0) {
221                         printf("port %u is not enabled in port mask\n", portid);
222                         return -1;
223                 }
224                 if (portid >= nb_ports) {
225                         printf("port %u is not present on the board\n", portid);
226                         return -1;
227                 }
228         }
229         return 0;
230 }
231
232 static uint8_t
233 get_port_n_rx_queues(const uint16_t port)
234 {
235         int queue = -1;
236         uint16_t i;
237
238         for (i = 0; i < nb_lcore_params; ++i) {
239                 if (lcore_params[i].port_id == port) {
240                         if (lcore_params[i].queue_id == queue+1)
241                                 queue = lcore_params[i].queue_id;
242                         else
243                                 rte_exit(EXIT_FAILURE, "queue ids of the port %d must be"
244                                                 " in sequence and must start with 0\n",
245                                                 lcore_params[i].port_id);
246                 }
247         }
248         return (uint8_t)(++queue);
249 }
250
251 static int
252 init_lcore_rx_queues(void)
253 {
254         uint16_t i, nb_rx_queue;
255         uint8_t lcore;
256
257         for (i = 0; i < nb_lcore_params; ++i) {
258                 lcore = lcore_params[i].lcore_id;
259                 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
260                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
261                         printf("error: too many queues (%u) for lcore: %u\n",
262                                 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
263                         return -1;
264                 } else {
265                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
266                                 lcore_params[i].port_id;
267                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
268                                 lcore_params[i].queue_id;
269                         lcore_conf[lcore].n_rx_queue++;
270                 }
271         }
272         return 0;
273 }
274
275 /* display usage */
276 static void
277 print_usage(const char *prgname)
278 {
279         fprintf(stderr, "%s [EAL options] --"
280                 " -p PORTMASK"
281                 " [-P]"
282                 " [-E]"
283                 " [-L]"
284                 " --config (port,queue,lcore)[,(port,queue,lcore)]"
285                 " [--eth-dest=X,MM:MM:MM:MM:MM:MM]"
286                 " [--enable-jumbo [--max-pkt-len PKTLEN]]"
287                 " [--no-numa]"
288                 " [--hash-entry-num]"
289                 " [--ipv6]"
290                 " [--parse-ptype]\n\n"
291
292                 "  -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
293                 "  -P : Enable promiscuous mode\n"
294                 "  -E : Enable exact match\n"
295                 "  -L : Enable longest prefix match (default)\n"
296                 "  --config (port,queue,lcore): Rx queue configuration\n"
297                 "  --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for port X\n"
298                 "  --enable-jumbo: Enable jumbo frames\n"
299                 "  --max-pkt-len: Under the premise of enabling jumbo,\n"
300                 "                 maximum packet length in decimal (64-9600)\n"
301                 "  --no-numa: Disable numa awareness\n"
302                 "  --hash-entry-num: Specify the hash entry number in hexadecimal to be setup\n"
303                 "  --ipv6: Set if running ipv6 packets\n"
304                 "  --parse-ptype: Set to use software to analyze packet type\n\n",
305                 prgname);
306 }
307
308 static int
309 parse_max_pkt_len(const char *pktlen)
310 {
311         char *end = NULL;
312         unsigned long len;
313
314         /* parse decimal string */
315         len = strtoul(pktlen, &end, 10);
316         if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
317                 return -1;
318
319         if (len == 0)
320                 return -1;
321
322         return len;
323 }
324
325 static int
326 parse_portmask(const char *portmask)
327 {
328         char *end = NULL;
329         unsigned long pm;
330
331         /* parse hexadecimal string */
332         pm = strtoul(portmask, &end, 16);
333         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
334                 return -1;
335
336         if (pm == 0)
337                 return -1;
338
339         return pm;
340 }
341
342 static int
343 parse_hash_entry_number(const char *hash_entry_num)
344 {
345         char *end = NULL;
346         unsigned long hash_en;
347         /* parse hexadecimal string */
348         hash_en = strtoul(hash_entry_num, &end, 16);
349         if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
350                 return -1;
351
352         if (hash_en == 0)
353                 return -1;
354
355         return hash_en;
356 }
357
358 static int
359 parse_config(const char *q_arg)
360 {
361         char s[256];
362         const char *p, *p0 = q_arg;
363         char *end;
364         enum fieldnames {
365                 FLD_PORT = 0,
366                 FLD_QUEUE,
367                 FLD_LCORE,
368                 _NUM_FLD
369         };
370         unsigned long int_fld[_NUM_FLD];
371         char *str_fld[_NUM_FLD];
372         int i;
373         unsigned size;
374
375         nb_lcore_params = 0;
376
377         while ((p = strchr(p0,'(')) != NULL) {
378                 ++p;
379                 if((p0 = strchr(p,')')) == NULL)
380                         return -1;
381
382                 size = p0 - p;
383                 if(size >= sizeof(s))
384                         return -1;
385
386                 snprintf(s, sizeof(s), "%.*s", size, p);
387                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
388                         return -1;
389                 for (i = 0; i < _NUM_FLD; i++){
390                         errno = 0;
391                         int_fld[i] = strtoul(str_fld[i], &end, 0);
392                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
393                                 return -1;
394                 }
395                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
396                         printf("exceeded max number of lcore params: %hu\n",
397                                 nb_lcore_params);
398                         return -1;
399                 }
400                 lcore_params_array[nb_lcore_params].port_id =
401                         (uint8_t)int_fld[FLD_PORT];
402                 lcore_params_array[nb_lcore_params].queue_id =
403                         (uint8_t)int_fld[FLD_QUEUE];
404                 lcore_params_array[nb_lcore_params].lcore_id =
405                         (uint8_t)int_fld[FLD_LCORE];
406                 ++nb_lcore_params;
407         }
408         lcore_params = lcore_params_array;
409         return 0;
410 }
411
412 static void
413 parse_eth_dest(const char *optarg)
414 {
415         uint16_t portid;
416         char *port_end;
417         uint8_t c, *dest, peer_addr[6];
418
419         errno = 0;
420         portid = strtoul(optarg, &port_end, 10);
421         if (errno != 0 || port_end == optarg || *port_end++ != ',')
422                 rte_exit(EXIT_FAILURE,
423                 "Invalid eth-dest: %s", optarg);
424         if (portid >= RTE_MAX_ETHPORTS)
425                 rte_exit(EXIT_FAILURE,
426                 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
427                 portid, RTE_MAX_ETHPORTS);
428
429         if (cmdline_parse_etheraddr(NULL, port_end,
430                 &peer_addr, sizeof(peer_addr)) < 0)
431                 rte_exit(EXIT_FAILURE,
432                 "Invalid ethernet address: %s\n",
433                 port_end);
434         dest = (uint8_t *)&dest_eth_addr[portid];
435         for (c = 0; c < 6; c++)
436                 dest[c] = peer_addr[c];
437         *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
438 }
439
440 #define MAX_JUMBO_PKT_LEN  9600
441 #define MEMPOOL_CACHE_SIZE 256
442
443 static const char short_options[] =
444         "p:"  /* portmask */
445         "P"   /* promiscuous */
446         "L"   /* enable long prefix match */
447         "E"   /* enable exact match */
448         ;
449
450 #define CMD_LINE_OPT_CONFIG "config"
451 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
452 #define CMD_LINE_OPT_NO_NUMA "no-numa"
453 #define CMD_LINE_OPT_IPV6 "ipv6"
454 #define CMD_LINE_OPT_ENABLE_JUMBO "enable-jumbo"
455 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
456 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
457 enum {
458         /* long options mapped to a short option */
459
460         /* first long only option value must be >= 256, so that we won't
461          * conflict with short options */
462         CMD_LINE_OPT_MIN_NUM = 256,
463         CMD_LINE_OPT_CONFIG_NUM,
464         CMD_LINE_OPT_ETH_DEST_NUM,
465         CMD_LINE_OPT_NO_NUMA_NUM,
466         CMD_LINE_OPT_IPV6_NUM,
467         CMD_LINE_OPT_ENABLE_JUMBO_NUM,
468         CMD_LINE_OPT_HASH_ENTRY_NUM_NUM,
469         CMD_LINE_OPT_PARSE_PTYPE_NUM,
470 };
471
472 static const struct option lgopts[] = {
473         {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
474         {CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM},
475         {CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM},
476         {CMD_LINE_OPT_IPV6, 0, 0, CMD_LINE_OPT_IPV6_NUM},
477         {CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, CMD_LINE_OPT_ENABLE_JUMBO_NUM},
478         {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, CMD_LINE_OPT_HASH_ENTRY_NUM_NUM},
479         {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, CMD_LINE_OPT_PARSE_PTYPE_NUM},
480         {NULL, 0, 0, 0}
481 };
482
483 /*
484  * This expression is used to calculate the number of mbufs needed
485  * depending on user input, taking  into account memory for rx and
486  * tx hardware rings, cache per lcore and mtable per port per lcore.
487  * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum
488  * value of 8192
489  */
490 #define NB_MBUF RTE_MAX(        \
491         (nb_ports*nb_rx_queue*nb_rxd +          \
492         nb_ports*nb_lcores*MAX_PKT_BURST +      \
493         nb_ports*n_tx_queue*nb_txd +            \
494         nb_lcores*MEMPOOL_CACHE_SIZE),          \
495         (unsigned)8192)
496
497 /* Parse the argument given in the command line of the application */
498 static int
499 parse_args(int argc, char **argv)
500 {
501         int opt, ret;
502         char **argvopt;
503         int option_index;
504         char *prgname = argv[0];
505
506         argvopt = argv;
507
508         /* Error or normal output strings. */
509         while ((opt = getopt_long(argc, argvopt, short_options,
510                                 lgopts, &option_index)) != EOF) {
511
512                 switch (opt) {
513                 /* portmask */
514                 case 'p':
515                         enabled_port_mask = parse_portmask(optarg);
516                         if (enabled_port_mask == 0) {
517                                 fprintf(stderr, "Invalid portmask\n");
518                                 print_usage(prgname);
519                                 return -1;
520                         }
521                         break;
522
523                 case 'P':
524                         promiscuous_on = 1;
525                         break;
526
527                 case 'E':
528                         l3fwd_em_on = 1;
529                         break;
530
531                 case 'L':
532                         l3fwd_lpm_on = 1;
533                         break;
534
535                 /* long options */
536                 case CMD_LINE_OPT_CONFIG_NUM:
537                         ret = parse_config(optarg);
538                         if (ret) {
539                                 fprintf(stderr, "Invalid config\n");
540                                 print_usage(prgname);
541                                 return -1;
542                         }
543                         break;
544
545                 case CMD_LINE_OPT_ETH_DEST_NUM:
546                         parse_eth_dest(optarg);
547                         break;
548
549                 case CMD_LINE_OPT_NO_NUMA_NUM:
550                         numa_on = 0;
551                         break;
552
553                 case CMD_LINE_OPT_IPV6_NUM:
554                         ipv6 = 1;
555                         break;
556
557                 case CMD_LINE_OPT_ENABLE_JUMBO_NUM: {
558                         const struct option lenopts = {
559                                 "max-pkt-len", required_argument, 0, 0
560                         };
561
562                         port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
563                         port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
564
565                         /*
566                          * if no max-pkt-len set, use the default
567                          * value ETHER_MAX_LEN.
568                          */
569                         if (getopt_long(argc, argvopt, "",
570                                         &lenopts, &option_index) == 0) {
571                                 ret = parse_max_pkt_len(optarg);
572                                 if (ret < 64 || ret > MAX_JUMBO_PKT_LEN) {
573                                         fprintf(stderr,
574                                                 "invalid maximum packet length\n");
575                                         print_usage(prgname);
576                                         return -1;
577                                 }
578                                 port_conf.rxmode.max_rx_pkt_len = ret;
579                         }
580                         break;
581                 }
582
583                 case CMD_LINE_OPT_HASH_ENTRY_NUM_NUM:
584                         ret = parse_hash_entry_number(optarg);
585                         if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
586                                 hash_entry_number = ret;
587                         } else {
588                                 fprintf(stderr, "invalid hash entry number\n");
589                                 print_usage(prgname);
590                                 return -1;
591                         }
592                         break;
593
594                 case CMD_LINE_OPT_PARSE_PTYPE_NUM:
595                         printf("soft parse-ptype is enabled\n");
596                         parse_ptype = 1;
597                         break;
598
599                 default:
600                         print_usage(prgname);
601                         return -1;
602                 }
603         }
604
605         /* If both LPM and EM are selected, return error. */
606         if (l3fwd_lpm_on && l3fwd_em_on) {
607                 fprintf(stderr, "LPM and EM are mutually exclusive, select only one\n");
608                 return -1;
609         }
610
611         /*
612          * Nothing is selected, pick longest-prefix match
613          * as default match.
614          */
615         if (!l3fwd_lpm_on && !l3fwd_em_on) {
616                 fprintf(stderr, "LPM or EM none selected, default LPM on\n");
617                 l3fwd_lpm_on = 1;
618         }
619
620         /*
621          * ipv6 and hash flags are valid only for
622          * exact macth, reset them to default for
623          * longest-prefix match.
624          */
625         if (l3fwd_lpm_on) {
626                 ipv6 = 0;
627                 hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
628         }
629
630         if (optind >= 0)
631                 argv[optind-1] = prgname;
632
633         ret = optind-1;
634         optind = 1; /* reset getopt lib */
635         return ret;
636 }
637
638 static void
639 print_ethaddr(const char *name, const struct ether_addr *eth_addr)
640 {
641         char buf[ETHER_ADDR_FMT_SIZE];
642         ether_format_addr(buf, ETHER_ADDR_FMT_SIZE, eth_addr);
643         printf("%s%s", name, buf);
644 }
645
646 static int
647 init_mem(unsigned nb_mbuf)
648 {
649         struct lcore_conf *qconf;
650         int socketid;
651         unsigned lcore_id;
652         char s[64];
653
654         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
655                 if (rte_lcore_is_enabled(lcore_id) == 0)
656                         continue;
657
658                 if (numa_on)
659                         socketid = rte_lcore_to_socket_id(lcore_id);
660                 else
661                         socketid = 0;
662
663                 if (socketid >= NB_SOCKETS) {
664                         rte_exit(EXIT_FAILURE,
665                                 "Socket %d of lcore %u is out of range %d\n",
666                                 socketid, lcore_id, NB_SOCKETS);
667                 }
668
669                 if (pktmbuf_pool[socketid] == NULL) {
670                         snprintf(s, sizeof(s), "mbuf_pool_%d", socketid);
671                         pktmbuf_pool[socketid] =
672                                 rte_pktmbuf_pool_create(s, nb_mbuf,
673                                         MEMPOOL_CACHE_SIZE, 0,
674                                         RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
675                         if (pktmbuf_pool[socketid] == NULL)
676                                 rte_exit(EXIT_FAILURE,
677                                         "Cannot init mbuf pool on socket %d\n",
678                                         socketid);
679                         else
680                                 printf("Allocated mbuf pool on socket %d\n",
681                                         socketid);
682
683                         /* Setup either LPM or EM(f.e Hash).  */
684                         l3fwd_lkp.setup(socketid);
685                 }
686                 qconf = &lcore_conf[lcore_id];
687                 qconf->ipv4_lookup_struct =
688                         l3fwd_lkp.get_ipv4_lookup_struct(socketid);
689                 qconf->ipv6_lookup_struct =
690                         l3fwd_lkp.get_ipv6_lookup_struct(socketid);
691         }
692         return 0;
693 }
694
695 /* Check the link status of all ports in up to 9s, and print them finally */
696 static void
697 check_all_ports_link_status(uint16_t port_num, uint32_t port_mask)
698 {
699 #define CHECK_INTERVAL 100 /* 100ms */
700 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
701         uint16_t portid;
702         uint8_t count, all_ports_up, print_flag = 0;
703         struct rte_eth_link link;
704
705         printf("\nChecking link status");
706         fflush(stdout);
707         for (count = 0; count <= MAX_CHECK_TIME; count++) {
708                 if (force_quit)
709                         return;
710                 all_ports_up = 1;
711                 for (portid = 0; portid < port_num; portid++) {
712                         if (force_quit)
713                                 return;
714                         if ((port_mask & (1 << portid)) == 0)
715                                 continue;
716                         memset(&link, 0, sizeof(link));
717                         rte_eth_link_get_nowait(portid, &link);
718                         /* print link status if flag set */
719                         if (print_flag == 1) {
720                                 if (link.link_status)
721                                         printf(
722                                         "Port%d Link Up. Speed %u Mbps -%s\n",
723                                                 portid, link.link_speed,
724                                 (link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
725                                         ("full-duplex") : ("half-duplex\n"));
726                                 else
727                                         printf("Port %d Link Down\n", portid);
728                                 continue;
729                         }
730                         /* clear all_ports_up flag if any link down */
731                         if (link.link_status == ETH_LINK_DOWN) {
732                                 all_ports_up = 0;
733                                 break;
734                         }
735                 }
736                 /* after finally printing all link status, get out */
737                 if (print_flag == 1)
738                         break;
739
740                 if (all_ports_up == 0) {
741                         printf(".");
742                         fflush(stdout);
743                         rte_delay_ms(CHECK_INTERVAL);
744                 }
745
746                 /* set the print_flag if all ports up or timeout */
747                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
748                         print_flag = 1;
749                         printf("done\n");
750                 }
751         }
752 }
753
754 static void
755 signal_handler(int signum)
756 {
757         if (signum == SIGINT || signum == SIGTERM) {
758                 printf("\n\nSignal %d received, preparing to exit...\n",
759                                 signum);
760                 force_quit = true;
761         }
762 }
763
764 static int
765 prepare_ptype_parser(uint16_t portid, uint16_t queueid)
766 {
767         if (parse_ptype) {
768                 printf("Port %d: softly parse packet type info\n", portid);
769                 if (rte_eth_add_rx_callback(portid, queueid,
770                                             l3fwd_lkp.cb_parse_ptype,
771                                             NULL))
772                         return 1;
773
774                 printf("Failed to add rx callback: port=%d\n", portid);
775                 return 0;
776         }
777
778         if (l3fwd_lkp.check_ptype(portid))
779                 return 1;
780
781         printf("port %d cannot parse packet type, please add --%s\n",
782                portid, CMD_LINE_OPT_PARSE_PTYPE);
783         return 0;
784 }
785
786 int
787 main(int argc, char **argv)
788 {
789         struct lcore_conf *qconf;
790         struct rte_eth_dev_info dev_info;
791         struct rte_eth_txconf *txconf;
792         int ret;
793         unsigned nb_ports;
794         uint16_t queueid, portid;
795         unsigned lcore_id;
796         uint32_t n_tx_queue, nb_lcores;
797         uint8_t nb_rx_queue, queue, socketid;
798
799         /* init EAL */
800         ret = rte_eal_init(argc, argv);
801         if (ret < 0)
802                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
803         argc -= ret;
804         argv += ret;
805
806         force_quit = false;
807         signal(SIGINT, signal_handler);
808         signal(SIGTERM, signal_handler);
809
810         /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
811         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
812                 dest_eth_addr[portid] =
813                         ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
814                 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
815         }
816
817         /* parse application arguments (after the EAL ones) */
818         ret = parse_args(argc, argv);
819         if (ret < 0)
820                 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
821
822         if (check_lcore_params() < 0)
823                 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
824
825         ret = init_lcore_rx_queues();
826         if (ret < 0)
827                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
828
829         nb_ports = rte_eth_dev_count();
830
831         if (check_port_config(nb_ports) < 0)
832                 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
833
834         nb_lcores = rte_lcore_count();
835
836         /* Setup function pointers for lookup method. */
837         setup_l3fwd_lookup_tables();
838
839         /* initialize all ports */
840         for (portid = 0; portid < nb_ports; portid++) {
841                 struct rte_eth_conf local_port_conf = port_conf;
842
843                 /* skip ports that are not enabled */
844                 if ((enabled_port_mask & (1 << portid)) == 0) {
845                         printf("\nSkipping disabled port %d\n", portid);
846                         continue;
847                 }
848
849                 /* init port */
850                 printf("Initializing port %d ... ", portid );
851                 fflush(stdout);
852
853                 nb_rx_queue = get_port_n_rx_queues(portid);
854                 n_tx_queue = nb_lcores;
855                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
856                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
857                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
858                         nb_rx_queue, (unsigned)n_tx_queue );
859
860                 rte_eth_dev_info_get(portid, &dev_info);
861                 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
862                         local_port_conf.txmode.offloads |=
863                                 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
864                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
865                                         (uint16_t)n_tx_queue, &local_port_conf);
866                 if (ret < 0)
867                         rte_exit(EXIT_FAILURE,
868                                 "Cannot configure device: err=%d, port=%d\n",
869                                 ret, portid);
870
871                 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
872                                                        &nb_txd);
873                 if (ret < 0)
874                         rte_exit(EXIT_FAILURE,
875                                  "Cannot adjust number of descriptors: err=%d, "
876                                  "port=%d\n", ret, portid);
877
878                 rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
879                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
880                 printf(", ");
881                 print_ethaddr("Destination:",
882                         (const struct ether_addr *)&dest_eth_addr[portid]);
883                 printf(", ");
884
885                 /*
886                  * prepare src MACs for each port.
887                  */
888                 ether_addr_copy(&ports_eth_addr[portid],
889                         (struct ether_addr *)(val_eth + portid) + 1);
890
891                 /* init memory */
892                 ret = init_mem(NB_MBUF);
893                 if (ret < 0)
894                         rte_exit(EXIT_FAILURE, "init_mem failed\n");
895
896                 /* init one TX queue per couple (lcore,port) */
897                 queueid = 0;
898                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
899                         if (rte_lcore_is_enabled(lcore_id) == 0)
900                                 continue;
901
902                         if (numa_on)
903                                 socketid =
904                                 (uint8_t)rte_lcore_to_socket_id(lcore_id);
905                         else
906                                 socketid = 0;
907
908                         printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
909                         fflush(stdout);
910
911                         txconf = &dev_info.default_txconf;
912                         txconf->txq_flags = ETH_TXQ_FLAGS_IGNORE;
913                         txconf->offloads = local_port_conf.txmode.offloads;
914                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
915                                                      socketid, txconf);
916                         if (ret < 0)
917                                 rte_exit(EXIT_FAILURE,
918                                         "rte_eth_tx_queue_setup: err=%d, "
919                                         "port=%d\n", ret, portid);
920
921                         qconf = &lcore_conf[lcore_id];
922                         qconf->tx_queue_id[portid] = queueid;
923                         queueid++;
924
925                         qconf->tx_port_id[qconf->n_tx_port] = portid;
926                         qconf->n_tx_port++;
927                 }
928                 printf("\n");
929         }
930
931         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
932                 if (rte_lcore_is_enabled(lcore_id) == 0)
933                         continue;
934                 qconf = &lcore_conf[lcore_id];
935                 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
936                 fflush(stdout);
937                 /* init RX queues */
938                 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
939                         struct rte_eth_dev *dev;
940                         struct rte_eth_conf *conf;
941                         struct rte_eth_rxconf rxq_conf;
942
943                         portid = qconf->rx_queue_list[queue].port_id;
944                         queueid = qconf->rx_queue_list[queue].queue_id;
945                         dev = &rte_eth_devices[portid];
946                         conf = &dev->data->dev_conf;
947
948                         if (numa_on)
949                                 socketid =
950                                 (uint8_t)rte_lcore_to_socket_id(lcore_id);
951                         else
952                                 socketid = 0;
953
954                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
955                         fflush(stdout);
956
957                         rte_eth_dev_info_get(portid, &dev_info);
958                         rxq_conf = dev_info.default_rxconf;
959                         rxq_conf.offloads = conf->rxmode.offloads;
960                         ret = rte_eth_rx_queue_setup(portid, queueid, nb_rxd,
961                                         socketid,
962                                         &rxq_conf,
963                                         pktmbuf_pool[socketid]);
964                         if (ret < 0)
965                                 rte_exit(EXIT_FAILURE,
966                                 "rte_eth_rx_queue_setup: err=%d, port=%d\n",
967                                 ret, portid);
968                 }
969         }
970
971         printf("\n");
972
973         /* start ports */
974         for (portid = 0; portid < nb_ports; portid++) {
975                 if ((enabled_port_mask & (1 << portid)) == 0) {
976                         continue;
977                 }
978                 /* Start device */
979                 ret = rte_eth_dev_start(portid);
980                 if (ret < 0)
981                         rte_exit(EXIT_FAILURE,
982                                 "rte_eth_dev_start: err=%d, port=%d\n",
983                                 ret, portid);
984
985                 /*
986                  * If enabled, put device in promiscuous mode.
987                  * This allows IO forwarding mode to forward packets
988                  * to itself through 2 cross-connected  ports of the
989                  * target machine.
990                  */
991                 if (promiscuous_on)
992                         rte_eth_promiscuous_enable(portid);
993         }
994
995         printf("\n");
996
997         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
998                 if (rte_lcore_is_enabled(lcore_id) == 0)
999                         continue;
1000                 qconf = &lcore_conf[lcore_id];
1001                 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
1002                         portid = qconf->rx_queue_list[queue].port_id;
1003                         queueid = qconf->rx_queue_list[queue].queue_id;
1004                         if (prepare_ptype_parser(portid, queueid) == 0)
1005                                 rte_exit(EXIT_FAILURE, "ptype check fails\n");
1006                 }
1007         }
1008
1009
1010         check_all_ports_link_status(nb_ports, enabled_port_mask);
1011
1012         ret = 0;
1013         /* launch per-lcore init on every lcore */
1014         rte_eal_mp_remote_launch(l3fwd_lkp.main_loop, NULL, CALL_MASTER);
1015         RTE_LCORE_FOREACH_SLAVE(lcore_id) {
1016                 if (rte_eal_wait_lcore(lcore_id) < 0) {
1017                         ret = -1;
1018                         break;
1019                 }
1020         }
1021
1022         /* stop ports */
1023         for (portid = 0; portid < nb_ports; portid++) {
1024                 if ((enabled_port_mask & (1 << portid)) == 0)
1025                         continue;
1026                 printf("Closing port %d...", portid);
1027                 rte_eth_dev_stop(portid);
1028                 rte_eth_dev_close(portid);
1029                 printf(" Done\n");
1030         }
1031         printf("Bye...\n");
1032
1033         return ret;
1034 }