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