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