38091aac3966da0f6a2ac7db235a9151dd58987d
[dpdk.git] / examples / l3fwd / main.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2021 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_malloc.h>
23 #include <rte_memory.h>
24 #include <rte_memcpy.h>
25 #include <rte_eal.h>
26 #include <rte_launch.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_mempool.h>
37 #include <rte_mbuf.h>
38 #include <rte_ip.h>
39 #include <rte_tcp.h>
40 #include <rte_udp.h>
41 #include <rte_string_fns.h>
42 #include <rte_cpuflags.h>
43
44 #include <cmdline_parse.h>
45 #include <cmdline_parse_etheraddr.h>
46
47 #include "l3fwd.h"
48 #include "l3fwd_event.h"
49 #include "l3fwd_route.h"
50
51 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_LCORE
52 #define MAX_RX_QUEUE_PER_PORT 128
53
54 #define MAX_LCORE_PARAMS 1024
55
56 uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
57 uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
58
59 /**< Ports set in promiscuous mode off by default. */
60 static int promiscuous_on;
61
62 /* Select Longest-Prefix, Exact match, Forwarding Information Base or Access Control. */
63 enum L3FWD_LOOKUP_MODE {
64         L3FWD_LOOKUP_DEFAULT,
65         L3FWD_LOOKUP_LPM,
66         L3FWD_LOOKUP_EM,
67         L3FWD_LOOKUP_FIB,
68         L3FWD_LOOKUP_ACL
69 };
70 static enum L3FWD_LOOKUP_MODE lookup_mode;
71
72 /* Global variables. */
73 static int numa_on = 1; /**< NUMA is enabled by default. */
74 static int parse_ptype; /**< Parse packet type using rx callback, and */
75                         /**< disabled by default */
76 static int per_port_pool; /**< Use separate buffer pools per port; disabled */
77                           /**< by default */
78
79 volatile bool force_quit;
80
81 /* ethernet addresses of ports */
82 uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
83 struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
84
85 xmm_t val_eth[RTE_MAX_ETHPORTS];
86
87 /* mask of enabled ports */
88 uint32_t enabled_port_mask;
89
90 /* Used only in exact match mode. */
91 int ipv6; /**< ipv6 is false by default. */
92 uint32_t hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
93
94 struct lcore_conf lcore_conf[RTE_MAX_LCORE];
95
96 struct parm_cfg parm_config;
97
98 struct lcore_params {
99         uint16_t port_id;
100         uint8_t queue_id;
101         uint8_t lcore_id;
102 } __rte_cache_aligned;
103
104 static struct lcore_params lcore_params_array[MAX_LCORE_PARAMS];
105 static struct lcore_params lcore_params_array_default[] = {
106         {0, 0, 2},
107         {0, 1, 2},
108         {0, 2, 2},
109         {1, 0, 2},
110         {1, 1, 2},
111         {1, 2, 2},
112         {2, 0, 2},
113         {3, 0, 3},
114         {3, 1, 3},
115 };
116
117 static struct lcore_params * lcore_params = lcore_params_array_default;
118 static uint16_t nb_lcore_params = sizeof(lcore_params_array_default) /
119                                 sizeof(lcore_params_array_default[0]);
120
121 static struct rte_eth_conf port_conf = {
122         .rxmode = {
123                 .mq_mode = RTE_ETH_MQ_RX_RSS,
124                 .split_hdr_size = 0,
125                 .offloads = RTE_ETH_RX_OFFLOAD_CHECKSUM,
126         },
127         .rx_adv_conf = {
128                 .rss_conf = {
129                         .rss_key = NULL,
130                         .rss_hf = RTE_ETH_RSS_IP,
131                 },
132         },
133         .txmode = {
134                 .mq_mode = RTE_ETH_MQ_TX_NONE,
135         },
136 };
137
138 static uint32_t max_pkt_len;
139
140 static struct rte_mempool *pktmbuf_pool[RTE_MAX_ETHPORTS][NB_SOCKETS];
141 static struct rte_mempool *vector_pool[RTE_MAX_ETHPORTS];
142 static uint8_t lkp_per_socket[NB_SOCKETS];
143
144 struct l3fwd_lkp_mode {
145         void  (*read_config_files)(void);
146         void  (*setup)(int);
147         int   (*check_ptype)(int);
148         rte_rx_callback_fn cb_parse_ptype;
149         int   (*main_loop)(void *);
150         void* (*get_ipv4_lookup_struct)(int);
151         void* (*get_ipv6_lookup_struct)(int);
152         void  (*free_routes)(void);
153 };
154
155 static struct l3fwd_lkp_mode l3fwd_lkp;
156
157 static struct l3fwd_lkp_mode l3fwd_em_lkp = {
158         .read_config_files              = read_config_files_em,
159         .setup                  = setup_hash,
160         .check_ptype            = em_check_ptype,
161         .cb_parse_ptype         = em_cb_parse_ptype,
162         .main_loop              = em_main_loop,
163         .get_ipv4_lookup_struct = em_get_ipv4_l3fwd_lookup_struct,
164         .get_ipv6_lookup_struct = em_get_ipv6_l3fwd_lookup_struct,
165         .free_routes                    = em_free_routes,
166 };
167
168 static struct l3fwd_lkp_mode l3fwd_lpm_lkp = {
169         .read_config_files              = read_config_files_lpm,
170         .setup                  = setup_lpm,
171         .check_ptype            = lpm_check_ptype,
172         .cb_parse_ptype         = lpm_cb_parse_ptype,
173         .main_loop              = lpm_main_loop,
174         .get_ipv4_lookup_struct = lpm_get_ipv4_l3fwd_lookup_struct,
175         .get_ipv6_lookup_struct = lpm_get_ipv6_l3fwd_lookup_struct,
176         .free_routes                    = lpm_free_routes,
177 };
178
179 static struct l3fwd_lkp_mode l3fwd_fib_lkp = {
180         .read_config_files              = read_config_files_lpm,
181         .setup                  = setup_fib,
182         .check_ptype            = lpm_check_ptype,
183         .cb_parse_ptype         = lpm_cb_parse_ptype,
184         .main_loop              = fib_main_loop,
185         .get_ipv4_lookup_struct = fib_get_ipv4_l3fwd_lookup_struct,
186         .get_ipv6_lookup_struct = fib_get_ipv6_l3fwd_lookup_struct,
187         .free_routes                    = lpm_free_routes,
188 };
189
190 static struct l3fwd_lkp_mode l3fwd_acl_lkp = {
191         .read_config_files              = read_config_files_acl,
192         .setup                  = setup_acl,
193         .check_ptype            = em_check_ptype,
194         .cb_parse_ptype         = em_cb_parse_ptype,
195         .main_loop              = acl_main_loop,
196         .get_ipv4_lookup_struct = acl_get_ipv4_l3fwd_lookup_struct,
197         .get_ipv6_lookup_struct = acl_get_ipv6_l3fwd_lookup_struct,
198         .free_routes                    = acl_free_routes,
199 };
200
201 /*
202  * 198.18.0.0/16 are set aside for RFC2544 benchmarking (RFC5735).
203  * 198.18.{0-15}.0/24 = Port {0-15}
204  */
205 const struct ipv4_l3fwd_route ipv4_l3fwd_route_array[] = {
206         {RTE_IPV4(198, 18, 0, 0), 24, 0},
207         {RTE_IPV4(198, 18, 1, 0), 24, 1},
208         {RTE_IPV4(198, 18, 2, 0), 24, 2},
209         {RTE_IPV4(198, 18, 3, 0), 24, 3},
210         {RTE_IPV4(198, 18, 4, 0), 24, 4},
211         {RTE_IPV4(198, 18, 5, 0), 24, 5},
212         {RTE_IPV4(198, 18, 6, 0), 24, 6},
213         {RTE_IPV4(198, 18, 7, 0), 24, 7},
214         {RTE_IPV4(198, 18, 8, 0), 24, 8},
215         {RTE_IPV4(198, 18, 9, 0), 24, 9},
216         {RTE_IPV4(198, 18, 10, 0), 24, 10},
217         {RTE_IPV4(198, 18, 11, 0), 24, 11},
218         {RTE_IPV4(198, 18, 12, 0), 24, 12},
219         {RTE_IPV4(198, 18, 13, 0), 24, 13},
220         {RTE_IPV4(198, 18, 14, 0), 24, 14},
221         {RTE_IPV4(198, 18, 15, 0), 24, 15},
222 };
223
224 /*
225  * 2001:200::/48 is IANA reserved range for IPv6 benchmarking (RFC5180).
226  * 2001:200:0:{0-f}::/64 = Port {0-15}
227  */
228 const struct ipv6_l3fwd_route ipv6_l3fwd_route_array[] = {
229         {{32, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 0},
230         {{32, 1, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 1},
231         {{32, 1, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 2},
232         {{32, 1, 2, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 3},
233         {{32, 1, 2, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 4},
234         {{32, 1, 2, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 5},
235         {{32, 1, 2, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 6},
236         {{32, 1, 2, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 7},
237         {{32, 1, 2, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 8},
238         {{32, 1, 2, 0, 0, 0, 0, 9, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 9},
239         {{32, 1, 2, 0, 0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 10},
240         {{32, 1, 2, 0, 0, 0, 0, 11, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 11},
241         {{32, 1, 2, 0, 0, 0, 0, 12, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 12},
242         {{32, 1, 2, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 13},
243         {{32, 1, 2, 0, 0, 0, 0, 14, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 14},
244         {{32, 1, 2, 0, 0, 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0}, 64, 15},
245 };
246
247 /*
248  * API's called during initialization to setup ACL/EM/LPM rules.
249  */
250 void
251 l3fwd_set_rule_ipv4_name(const char *optarg)
252 {
253         parm_config.rule_ipv4_name = optarg;
254 }
255
256 void
257 l3fwd_set_rule_ipv6_name(const char *optarg)
258 {
259         parm_config.rule_ipv6_name = optarg;
260 }
261
262 void
263 l3fwd_set_alg(const char *optarg)
264 {
265         parm_config.alg = parse_acl_alg(optarg);
266 }
267
268 /*
269  * Setup lookup methods for forwarding.
270  * Currently exact-match, longest-prefix-match and forwarding information
271  * base are the supported ones.
272  */
273 static void
274 setup_l3fwd_lookup_tables(void)
275 {
276         /* Setup HASH lookup functions. */
277         if (lookup_mode == L3FWD_LOOKUP_EM)
278                 l3fwd_lkp = l3fwd_em_lkp;
279         /* Setup FIB lookup functions. */
280         else if (lookup_mode == L3FWD_LOOKUP_FIB)
281                 l3fwd_lkp = l3fwd_fib_lkp;
282         /* Setup ACL lookup functions. */
283         else if (lookup_mode == L3FWD_LOOKUP_ACL)
284                 l3fwd_lkp = l3fwd_acl_lkp;
285         /* Setup LPM lookup functions. */
286         else
287                 l3fwd_lkp = l3fwd_lpm_lkp;
288 }
289
290 static int
291 check_lcore_params(void)
292 {
293         uint8_t queue, lcore;
294         uint16_t i;
295         int socketid;
296
297         for (i = 0; i < nb_lcore_params; ++i) {
298                 queue = lcore_params[i].queue_id;
299                 if (queue >= MAX_RX_QUEUE_PER_PORT) {
300                         printf("invalid queue number: %hhu\n", queue);
301                         return -1;
302                 }
303                 lcore = lcore_params[i].lcore_id;
304                 if (!rte_lcore_is_enabled(lcore)) {
305                         printf("error: lcore %hhu is not enabled in lcore mask\n", lcore);
306                         return -1;
307                 }
308                 if ((socketid = rte_lcore_to_socket_id(lcore) != 0) &&
309                         (numa_on == 0)) {
310                         printf("warning: lcore %hhu is on socket %d with numa off \n",
311                                 lcore, socketid);
312                 }
313         }
314         return 0;
315 }
316
317 static int
318 check_port_config(void)
319 {
320         uint16_t portid;
321         uint16_t i;
322
323         for (i = 0; i < nb_lcore_params; ++i) {
324                 portid = lcore_params[i].port_id;
325                 if ((enabled_port_mask & (1 << portid)) == 0) {
326                         printf("port %u is not enabled in port mask\n", portid);
327                         return -1;
328                 }
329                 if (!rte_eth_dev_is_valid_port(portid)) {
330                         printf("port %u is not present on the board\n", portid);
331                         return -1;
332                 }
333         }
334         return 0;
335 }
336
337 static uint8_t
338 get_port_n_rx_queues(const uint16_t port)
339 {
340         int queue = -1;
341         uint16_t i;
342
343         for (i = 0; i < nb_lcore_params; ++i) {
344                 if (lcore_params[i].port_id == port) {
345                         if (lcore_params[i].queue_id == queue+1)
346                                 queue = lcore_params[i].queue_id;
347                         else
348                                 rte_exit(EXIT_FAILURE, "queue ids of the port %d must be"
349                                                 " in sequence and must start with 0\n",
350                                                 lcore_params[i].port_id);
351                 }
352         }
353         return (uint8_t)(++queue);
354 }
355
356 static int
357 init_lcore_rx_queues(void)
358 {
359         uint16_t i, nb_rx_queue;
360         uint8_t lcore;
361
362         for (i = 0; i < nb_lcore_params; ++i) {
363                 lcore = lcore_params[i].lcore_id;
364                 nb_rx_queue = lcore_conf[lcore].n_rx_queue;
365                 if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
366                         printf("error: too many queues (%u) for lcore: %u\n",
367                                 (unsigned)nb_rx_queue + 1, (unsigned)lcore);
368                         return -1;
369                 } else {
370                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
371                                 lcore_params[i].port_id;
372                         lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
373                                 lcore_params[i].queue_id;
374                         lcore_conf[lcore].n_rx_queue++;
375                 }
376         }
377         return 0;
378 }
379
380 /* display usage */
381 static void
382 print_usage(const char *prgname)
383 {
384         char alg[PATH_MAX];
385
386         usage_acl_alg(alg, sizeof(alg));
387         fprintf(stderr, "%s [EAL options] --"
388                 " -p PORTMASK"
389                 "  --rule_ipv4=FILE"
390                 "  --rule_ipv6=FILE"
391                 " [-P]"
392                 " [--lookup]"
393                 " --config (port,queue,lcore)[,(port,queue,lcore)]"
394                 " [--rx-queue-size NPKTS]"
395                 " [--tx-queue-size NPKTS]"
396                 " [--eth-dest=X,MM:MM:MM:MM:MM:MM]"
397                 " [--max-pkt-len PKTLEN]"
398                 " [--no-numa]"
399                 " [--hash-entry-num]"
400                 " [--ipv6]"
401                 " [--parse-ptype]"
402                 " [--per-port-pool]"
403                 " [--mode]"
404                 " [--eventq-sched]"
405                 " [--event-vector [--event-vector-size SIZE] [--event-vector-tmo NS]]"
406                 " [-E]"
407                 " [-L]\n\n"
408
409                 "  -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
410                 "  -P : Enable promiscuous mode\n"
411                 "  --lookup: Select the lookup method\n"
412                 "            Default: lpm\n"
413                 "            Accepted: em (Exact Match), lpm (Longest Prefix Match), fib (Forwarding Information Base),\n"
414                 "                      acl (Access Control List)\n"
415                 "  --config (port,queue,lcore): Rx queue configuration\n"
416                 "  --rx-queue-size NPKTS: Rx queue size in decimal\n"
417                 "            Default: %d\n"
418                 "  --tx-queue-size NPKTS: Tx queue size in decimal\n"
419                 "            Default: %d\n"
420                 "  --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for port X\n"
421                 "  --max-pkt-len PKTLEN: maximum packet length in decimal (64-9600)\n"
422                 "  --no-numa: Disable numa awareness\n"
423                 "  --hash-entry-num: Specify the hash entry number in hexadecimal to be setup\n"
424                 "  --ipv6: Set if running ipv6 packets\n"
425                 "  --parse-ptype: Set to use software to analyze packet type\n"
426                 "  --per-port-pool: Use separate buffer pool per port\n"
427                 "  --mode: Packet transfer mode for I/O, poll or eventdev\n"
428                 "          Default mode = poll\n"
429                 "  --eventq-sched: Event queue synchronization method\n"
430                 "                  ordered, atomic or parallel.\n"
431                 "                  Default: atomic\n"
432                 "                  Valid only if --mode=eventdev\n"
433                 "  --event-eth-rxqs: Number of ethernet RX queues per device.\n"
434                 "                    Default: 1\n"
435                 "                    Valid only if --mode=eventdev\n"
436                 "  --event-vector:  Enable event vectorization.\n"
437                 "  --event-vector-size: Max vector size if event vectorization is enabled.\n"
438                 "  --event-vector-tmo: Max timeout to form vector in nanoseconds if event vectorization is enabled\n"
439                 "  -E : Enable exact match, legacy flag please use --lookup=em instead\n"
440                 "  -L : Enable longest prefix match, legacy flag please use --lookup=lpm instead\n"
441                 "  --rule_ipv4=FILE: Specify the ipv4 rules entries file.\n"
442                 "                    Each rule occupies one line.\n"
443                 "                    2 kinds of rules are supported.\n"
444                 "                    One is ACL entry at while line leads with character '%c',\n"
445                 "                    another is route entry at while line leads with character '%c'.\n"
446                 "  --rule_ipv6=FILE: Specify the ipv6 rules entries file.\n"
447                 "  --alg: ACL classify method to use, one of: %s.\n\n",
448                 prgname, RTE_TEST_RX_DESC_DEFAULT, RTE_TEST_TX_DESC_DEFAULT,
449                 ACL_LEAD_CHAR, ROUTE_LEAD_CHAR, alg);
450 }
451
452 static int
453 parse_max_pkt_len(const char *pktlen)
454 {
455         char *end = NULL;
456         unsigned long len;
457
458         /* parse decimal string */
459         len = strtoul(pktlen, &end, 10);
460         if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
461                 return -1;
462
463         if (len == 0)
464                 return -1;
465
466         return len;
467 }
468
469 static int
470 parse_portmask(const char *portmask)
471 {
472         char *end = NULL;
473         unsigned long pm;
474
475         /* parse hexadecimal string */
476         pm = strtoul(portmask, &end, 16);
477         if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
478                 return 0;
479
480         return pm;
481 }
482
483 static int
484 parse_hash_entry_number(const char *hash_entry_num)
485 {
486         char *end = NULL;
487         unsigned long hash_en;
488         /* parse hexadecimal string */
489         hash_en = strtoul(hash_entry_num, &end, 16);
490         if ((hash_entry_num[0] == '\0') || (end == NULL) || (*end != '\0'))
491                 return -1;
492
493         if (hash_en == 0)
494                 return -1;
495
496         return hash_en;
497 }
498
499 static int
500 parse_config(const char *q_arg)
501 {
502         char s[256];
503         const char *p, *p0 = q_arg;
504         char *end;
505         enum fieldnames {
506                 FLD_PORT = 0,
507                 FLD_QUEUE,
508                 FLD_LCORE,
509                 _NUM_FLD
510         };
511         unsigned long int_fld[_NUM_FLD];
512         char *str_fld[_NUM_FLD];
513         int i;
514         unsigned size;
515
516         nb_lcore_params = 0;
517
518         while ((p = strchr(p0,'(')) != NULL) {
519                 ++p;
520                 if((p0 = strchr(p,')')) == NULL)
521                         return -1;
522
523                 size = p0 - p;
524                 if(size >= sizeof(s))
525                         return -1;
526
527                 snprintf(s, sizeof(s), "%.*s", size, p);
528                 if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') != _NUM_FLD)
529                         return -1;
530                 for (i = 0; i < _NUM_FLD; i++){
531                         errno = 0;
532                         int_fld[i] = strtoul(str_fld[i], &end, 0);
533                         if (errno != 0 || end == str_fld[i] || int_fld[i] > 255)
534                                 return -1;
535                 }
536                 if (nb_lcore_params >= MAX_LCORE_PARAMS) {
537                         printf("exceeded max number of lcore params: %hu\n",
538                                 nb_lcore_params);
539                         return -1;
540                 }
541                 lcore_params_array[nb_lcore_params].port_id =
542                         (uint8_t)int_fld[FLD_PORT];
543                 lcore_params_array[nb_lcore_params].queue_id =
544                         (uint8_t)int_fld[FLD_QUEUE];
545                 lcore_params_array[nb_lcore_params].lcore_id =
546                         (uint8_t)int_fld[FLD_LCORE];
547                 ++nb_lcore_params;
548         }
549         lcore_params = lcore_params_array;
550         return 0;
551 }
552
553 static void
554 parse_eth_dest(const char *optarg)
555 {
556         uint16_t portid;
557         char *port_end;
558         uint8_t c, *dest, peer_addr[6];
559
560         errno = 0;
561         portid = strtoul(optarg, &port_end, 10);
562         if (errno != 0 || port_end == optarg || *port_end++ != ',')
563                 rte_exit(EXIT_FAILURE,
564                 "Invalid eth-dest: %s", optarg);
565         if (portid >= RTE_MAX_ETHPORTS)
566                 rte_exit(EXIT_FAILURE,
567                 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n",
568                 portid, RTE_MAX_ETHPORTS);
569
570         if (cmdline_parse_etheraddr(NULL, port_end,
571                 &peer_addr, sizeof(peer_addr)) < 0)
572                 rte_exit(EXIT_FAILURE,
573                 "Invalid ethernet address: %s\n",
574                 port_end);
575         dest = (uint8_t *)&dest_eth_addr[portid];
576         for (c = 0; c < 6; c++)
577                 dest[c] = peer_addr[c];
578         *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
579 }
580
581 static void
582 parse_mode(const char *optarg)
583 {
584         struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
585
586         if (!strcmp(optarg, "poll"))
587                 evt_rsrc->enabled = false;
588         else if (!strcmp(optarg, "eventdev"))
589                 evt_rsrc->enabled = true;
590 }
591
592 static void
593 parse_queue_size(const char *queue_size_arg, uint16_t *queue_size, int rx)
594 {
595         char *end = NULL;
596         unsigned long value;
597
598         /* parse decimal string */
599         value = strtoul(queue_size_arg, &end, 10);
600         if ((queue_size_arg[0] == '\0') || (end == NULL) ||
601                 (*end != '\0') || (value == 0)) {
602                 if (rx == 1)
603                         rte_exit(EXIT_FAILURE, "Invalid rx-queue-size\n");
604                 else
605                         rte_exit(EXIT_FAILURE, "Invalid tx-queue-size\n");
606
607                 return;
608         }
609
610         if (value > UINT16_MAX) {
611                 if (rx == 1)
612                         rte_exit(EXIT_FAILURE, "rx-queue-size %lu > %d\n",
613                                 value, UINT16_MAX);
614                 else
615                         rte_exit(EXIT_FAILURE, "tx-queue-size %lu > %d\n",
616                                 value, UINT16_MAX);
617
618                 return;
619         }
620
621         *queue_size = value;
622 }
623
624 static void
625 parse_eventq_sched(const char *optarg)
626 {
627         struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
628
629         if (!strcmp(optarg, "ordered"))
630                 evt_rsrc->sched_type = RTE_SCHED_TYPE_ORDERED;
631         if (!strcmp(optarg, "atomic"))
632                 evt_rsrc->sched_type = RTE_SCHED_TYPE_ATOMIC;
633         if (!strcmp(optarg, "parallel"))
634                 evt_rsrc->sched_type = RTE_SCHED_TYPE_PARALLEL;
635 }
636
637 static void
638 parse_event_eth_rx_queues(const char *eth_rx_queues)
639 {
640         struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
641         char *end = NULL;
642         uint8_t num_eth_rx_queues;
643
644         /* parse decimal string */
645         num_eth_rx_queues = strtoul(eth_rx_queues, &end, 10);
646         if ((eth_rx_queues[0] == '\0') || (end == NULL) || (*end != '\0'))
647                 return;
648
649         if (num_eth_rx_queues == 0)
650                 return;
651
652         evt_rsrc->eth_rx_queues = num_eth_rx_queues;
653 }
654
655 static int
656 parse_lookup(const char *optarg)
657 {
658         if (!strcmp(optarg, "em"))
659                 lookup_mode = L3FWD_LOOKUP_EM;
660         else if (!strcmp(optarg, "lpm"))
661                 lookup_mode = L3FWD_LOOKUP_LPM;
662         else if (!strcmp(optarg, "fib"))
663                 lookup_mode = L3FWD_LOOKUP_FIB;
664         else if (!strcmp(optarg, "acl"))
665                 lookup_mode = L3FWD_LOOKUP_ACL;
666         else {
667                 fprintf(stderr, "Invalid lookup option! Accepted options: acl, em, lpm, fib\n");
668                 return -1;
669         }
670         return 0;
671 }
672
673 #define MAX_JUMBO_PKT_LEN  9600
674
675 static const char short_options[] =
676         "p:"  /* portmask */
677         "P"   /* promiscuous */
678         "L"   /* legacy enable long prefix match */
679         "E"   /* legacy enable exact match */
680         ;
681
682 #define CMD_LINE_OPT_CONFIG "config"
683 #define CMD_LINE_OPT_RX_QUEUE_SIZE "rx-queue-size"
684 #define CMD_LINE_OPT_TX_QUEUE_SIZE "tx-queue-size"
685 #define CMD_LINE_OPT_ETH_DEST "eth-dest"
686 #define CMD_LINE_OPT_NO_NUMA "no-numa"
687 #define CMD_LINE_OPT_IPV6 "ipv6"
688 #define CMD_LINE_OPT_MAX_PKT_LEN "max-pkt-len"
689 #define CMD_LINE_OPT_HASH_ENTRY_NUM "hash-entry-num"
690 #define CMD_LINE_OPT_PARSE_PTYPE "parse-ptype"
691 #define CMD_LINE_OPT_PER_PORT_POOL "per-port-pool"
692 #define CMD_LINE_OPT_MODE "mode"
693 #define CMD_LINE_OPT_EVENTQ_SYNC "eventq-sched"
694 #define CMD_LINE_OPT_EVENT_ETH_RX_QUEUES "event-eth-rxqs"
695 #define CMD_LINE_OPT_LOOKUP "lookup"
696 #define CMD_LINE_OPT_ENABLE_VECTOR "event-vector"
697 #define CMD_LINE_OPT_VECTOR_SIZE "event-vector-size"
698 #define CMD_LINE_OPT_VECTOR_TMO_NS "event-vector-tmo"
699 #define CMD_LINE_OPT_RULE_IPV4 "rule_ipv4"
700 #define CMD_LINE_OPT_RULE_IPV6 "rule_ipv6"
701 #define CMD_LINE_OPT_ALG "alg"
702
703 enum {
704         /* long options mapped to a short option */
705
706         /* first long only option value must be >= 256, so that we won't
707          * conflict with short options */
708         CMD_LINE_OPT_MIN_NUM = 256,
709         CMD_LINE_OPT_CONFIG_NUM,
710         CMD_LINE_OPT_RX_QUEUE_SIZE_NUM,
711         CMD_LINE_OPT_TX_QUEUE_SIZE_NUM,
712         CMD_LINE_OPT_ETH_DEST_NUM,
713         CMD_LINE_OPT_NO_NUMA_NUM,
714         CMD_LINE_OPT_IPV6_NUM,
715         CMD_LINE_OPT_MAX_PKT_LEN_NUM,
716         CMD_LINE_OPT_HASH_ENTRY_NUM_NUM,
717         CMD_LINE_OPT_PARSE_PTYPE_NUM,
718         CMD_LINE_OPT_RULE_IPV4_NUM,
719         CMD_LINE_OPT_RULE_IPV6_NUM,
720         CMD_LINE_OPT_ALG_NUM,
721         CMD_LINE_OPT_PARSE_PER_PORT_POOL,
722         CMD_LINE_OPT_MODE_NUM,
723         CMD_LINE_OPT_EVENTQ_SYNC_NUM,
724         CMD_LINE_OPT_EVENT_ETH_RX_QUEUES_NUM,
725         CMD_LINE_OPT_LOOKUP_NUM,
726         CMD_LINE_OPT_ENABLE_VECTOR_NUM,
727         CMD_LINE_OPT_VECTOR_SIZE_NUM,
728         CMD_LINE_OPT_VECTOR_TMO_NS_NUM
729 };
730
731 static const struct option lgopts[] = {
732         {CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
733         {CMD_LINE_OPT_RX_QUEUE_SIZE, 1, 0, CMD_LINE_OPT_RX_QUEUE_SIZE_NUM},
734         {CMD_LINE_OPT_TX_QUEUE_SIZE, 1, 0, CMD_LINE_OPT_TX_QUEUE_SIZE_NUM},
735         {CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM},
736         {CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM},
737         {CMD_LINE_OPT_IPV6, 0, 0, CMD_LINE_OPT_IPV6_NUM},
738         {CMD_LINE_OPT_MAX_PKT_LEN, 1, 0, CMD_LINE_OPT_MAX_PKT_LEN_NUM},
739         {CMD_LINE_OPT_HASH_ENTRY_NUM, 1, 0, CMD_LINE_OPT_HASH_ENTRY_NUM_NUM},
740         {CMD_LINE_OPT_PARSE_PTYPE, 0, 0, CMD_LINE_OPT_PARSE_PTYPE_NUM},
741         {CMD_LINE_OPT_PER_PORT_POOL, 0, 0, CMD_LINE_OPT_PARSE_PER_PORT_POOL},
742         {CMD_LINE_OPT_MODE, 1, 0, CMD_LINE_OPT_MODE_NUM},
743         {CMD_LINE_OPT_EVENTQ_SYNC, 1, 0, CMD_LINE_OPT_EVENTQ_SYNC_NUM},
744         {CMD_LINE_OPT_EVENT_ETH_RX_QUEUES, 1, 0,
745                                         CMD_LINE_OPT_EVENT_ETH_RX_QUEUES_NUM},
746         {CMD_LINE_OPT_LOOKUP, 1, 0, CMD_LINE_OPT_LOOKUP_NUM},
747         {CMD_LINE_OPT_ENABLE_VECTOR, 0, 0, CMD_LINE_OPT_ENABLE_VECTOR_NUM},
748         {CMD_LINE_OPT_VECTOR_SIZE, 1, 0, CMD_LINE_OPT_VECTOR_SIZE_NUM},
749         {CMD_LINE_OPT_VECTOR_TMO_NS, 1, 0, CMD_LINE_OPT_VECTOR_TMO_NS_NUM},
750         {CMD_LINE_OPT_RULE_IPV4,   1, 0, CMD_LINE_OPT_RULE_IPV4_NUM},
751         {CMD_LINE_OPT_RULE_IPV6,   1, 0, CMD_LINE_OPT_RULE_IPV6_NUM},
752         {CMD_LINE_OPT_ALG,   1, 0, CMD_LINE_OPT_ALG_NUM},
753         {NULL, 0, 0, 0}
754 };
755
756 /*
757  * This expression is used to calculate the number of mbufs needed
758  * depending on user input, taking  into account memory for rx and
759  * tx hardware rings, cache per lcore and mtable per port per lcore.
760  * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum
761  * value of 8192
762  */
763 #define NB_MBUF(nports) RTE_MAX(        \
764         (nports*nb_rx_queue*nb_rxd +            \
765         nports*nb_lcores*MAX_PKT_BURST +        \
766         nports*n_tx_queue*nb_txd +              \
767         nb_lcores*MEMPOOL_CACHE_SIZE),          \
768         (unsigned)8192)
769
770 /* Parse the argument given in the command line of the application */
771 static int
772 parse_args(int argc, char **argv)
773 {
774         int opt, ret;
775         char **argvopt;
776         int option_index;
777         char *prgname = argv[0];
778         uint8_t lcore_params = 0;
779         uint8_t eventq_sched = 0;
780         uint8_t eth_rx_q = 0;
781         struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
782
783         argvopt = argv;
784
785         /* Error or normal output strings. */
786         while ((opt = getopt_long(argc, argvopt, short_options,
787                                 lgopts, &option_index)) != EOF) {
788
789                 switch (opt) {
790                 /* portmask */
791                 case 'p':
792                         enabled_port_mask = parse_portmask(optarg);
793                         if (enabled_port_mask == 0) {
794                                 fprintf(stderr, "Invalid portmask\n");
795                                 print_usage(prgname);
796                                 return -1;
797                         }
798                         break;
799
800                 case 'P':
801                         promiscuous_on = 1;
802                         break;
803
804                 case 'E':
805                         if (lookup_mode != L3FWD_LOOKUP_DEFAULT) {
806                                 fprintf(stderr, "Only one lookup mode is allowed at a time!\n");
807                                 return -1;
808                         }
809                         lookup_mode = L3FWD_LOOKUP_EM;
810                         break;
811
812                 case 'L':
813                         if (lookup_mode != L3FWD_LOOKUP_DEFAULT) {
814                                 fprintf(stderr, "Only one lookup mode is allowed at a time!\n");
815                                 return -1;
816                         }
817                         lookup_mode = L3FWD_LOOKUP_LPM;
818                         break;
819
820                 /* long options */
821                 case CMD_LINE_OPT_CONFIG_NUM:
822                         ret = parse_config(optarg);
823                         if (ret) {
824                                 fprintf(stderr, "Invalid config\n");
825                                 print_usage(prgname);
826                                 return -1;
827                         }
828                         lcore_params = 1;
829                         break;
830
831                 case CMD_LINE_OPT_RX_QUEUE_SIZE_NUM:
832                         parse_queue_size(optarg, &nb_rxd, 1);
833                         break;
834
835                 case CMD_LINE_OPT_TX_QUEUE_SIZE_NUM:
836                         parse_queue_size(optarg, &nb_txd, 0);
837                         break;
838
839                 case CMD_LINE_OPT_ETH_DEST_NUM:
840                         parse_eth_dest(optarg);
841                         break;
842
843                 case CMD_LINE_OPT_NO_NUMA_NUM:
844                         numa_on = 0;
845                         break;
846
847                 case CMD_LINE_OPT_IPV6_NUM:
848                         ipv6 = 1;
849                         break;
850
851                 case CMD_LINE_OPT_MAX_PKT_LEN_NUM:
852                         max_pkt_len = parse_max_pkt_len(optarg);
853                         break;
854
855                 case CMD_LINE_OPT_HASH_ENTRY_NUM_NUM:
856                         ret = parse_hash_entry_number(optarg);
857                         if ((ret > 0) && (ret <= L3FWD_HASH_ENTRIES)) {
858                                 hash_entry_number = ret;
859                         } else {
860                                 fprintf(stderr, "invalid hash entry number\n");
861                                 print_usage(prgname);
862                                 return -1;
863                         }
864                         break;
865
866                 case CMD_LINE_OPT_PARSE_PTYPE_NUM:
867                         printf("soft parse-ptype is enabled\n");
868                         parse_ptype = 1;
869                         break;
870
871                 case CMD_LINE_OPT_PARSE_PER_PORT_POOL:
872                         printf("per port buffer pool is enabled\n");
873                         per_port_pool = 1;
874                         break;
875
876                 case CMD_LINE_OPT_MODE_NUM:
877                         parse_mode(optarg);
878                         break;
879
880                 case CMD_LINE_OPT_EVENTQ_SYNC_NUM:
881                         parse_eventq_sched(optarg);
882                         eventq_sched = 1;
883                         break;
884
885                 case CMD_LINE_OPT_EVENT_ETH_RX_QUEUES_NUM:
886                         parse_event_eth_rx_queues(optarg);
887                         eth_rx_q = 1;
888                         break;
889
890                 case CMD_LINE_OPT_LOOKUP_NUM:
891                         if (lookup_mode != L3FWD_LOOKUP_DEFAULT) {
892                                 fprintf(stderr, "Only one lookup mode is allowed at a time!\n");
893                                 return -1;
894                         }
895                         ret = parse_lookup(optarg);
896                         /*
897                          * If parse_lookup was passed an invalid lookup type
898                          * then return -1. Error log included within
899                          * parse_lookup for simplicity.
900                          */
901                         if (ret)
902                                 return -1;
903                         break;
904
905                 case CMD_LINE_OPT_ENABLE_VECTOR_NUM:
906                         printf("event vectorization is enabled\n");
907                         evt_rsrc->vector_enabled = 1;
908                         break;
909                 case CMD_LINE_OPT_VECTOR_SIZE_NUM:
910                         evt_rsrc->vector_size = strtol(optarg, NULL, 10);
911                         break;
912                 case CMD_LINE_OPT_VECTOR_TMO_NS_NUM:
913                         evt_rsrc->vector_tmo_ns = strtoull(optarg, NULL, 10);
914                         break;
915                 case CMD_LINE_OPT_RULE_IPV4_NUM:
916                         l3fwd_set_rule_ipv4_name(optarg);
917                         break;
918                 case CMD_LINE_OPT_RULE_IPV6_NUM:
919                         l3fwd_set_rule_ipv6_name(optarg);
920                         break;
921                 case CMD_LINE_OPT_ALG_NUM:
922                         l3fwd_set_alg(optarg);
923                         break;
924                 default:
925                         print_usage(prgname);
926                         return -1;
927                 }
928         }
929
930         if (evt_rsrc->enabled && lcore_params) {
931                 fprintf(stderr, "lcore config is not valid when event mode is selected\n");
932                 return -1;
933         }
934
935         if (!evt_rsrc->enabled && eth_rx_q) {
936                 fprintf(stderr, "eth_rx_queues is valid only when event mode is selected\n");
937                 return -1;
938         }
939
940         if (!evt_rsrc->enabled && eventq_sched) {
941                 fprintf(stderr, "eventq_sched is valid only when event mode is selected\n");
942                 return -1;
943         }
944
945         if (evt_rsrc->vector_enabled && !evt_rsrc->vector_size) {
946                 evt_rsrc->vector_size = VECTOR_SIZE_DEFAULT;
947                 fprintf(stderr, "vector size set to default (%" PRIu16 ")\n",
948                         evt_rsrc->vector_size);
949         }
950
951         if (evt_rsrc->vector_enabled && !evt_rsrc->vector_tmo_ns) {
952                 evt_rsrc->vector_tmo_ns = VECTOR_TMO_NS_DEFAULT;
953                 fprintf(stderr,
954                         "vector timeout set to default (%" PRIu64 " ns)\n",
955                         evt_rsrc->vector_tmo_ns);
956         }
957
958         /*
959          * Nothing is selected, pick longest-prefix match
960          * as default match.
961          */
962         if (lookup_mode == L3FWD_LOOKUP_DEFAULT) {
963                 fprintf(stderr, "Neither ACL, LPM, EM, or FIB selected, defaulting to LPM\n");
964                 lookup_mode = L3FWD_LOOKUP_LPM;
965         }
966
967         /*
968          * ipv6 and hash flags are valid only for
969          * exact match, reset them to default for
970          * longest-prefix match.
971          */
972         if (lookup_mode == L3FWD_LOOKUP_LPM) {
973                 ipv6 = 0;
974                 hash_entry_number = HASH_ENTRY_NUMBER_DEFAULT;
975         }
976
977         /* For ACL, update port config rss hash filter */
978         if (lookup_mode == L3FWD_LOOKUP_ACL) {
979                 port_conf.rx_adv_conf.rss_conf.rss_hf |=
980                                 RTE_ETH_RSS_UDP | RTE_ETH_RSS_TCP | RTE_ETH_RSS_SCTP;
981         }
982
983         if (optind >= 0)
984                 argv[optind-1] = prgname;
985
986         ret = optind-1;
987         optind = 1; /* reset getopt lib */
988         return ret;
989 }
990
991 static void
992 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
993 {
994         char buf[RTE_ETHER_ADDR_FMT_SIZE];
995         rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
996         printf("%s%s", name, buf);
997 }
998
999 int
1000 init_mem(uint16_t portid, unsigned int nb_mbuf)
1001 {
1002         struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
1003         struct lcore_conf *qconf;
1004         int socketid;
1005         unsigned lcore_id;
1006         char s[64];
1007
1008         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1009                 if (rte_lcore_is_enabled(lcore_id) == 0)
1010                         continue;
1011
1012                 if (numa_on)
1013                         socketid = rte_lcore_to_socket_id(lcore_id);
1014                 else
1015                         socketid = 0;
1016
1017                 if (socketid >= NB_SOCKETS) {
1018                         rte_exit(EXIT_FAILURE,
1019                                 "Socket %d of lcore %u is out of range %d\n",
1020                                 socketid, lcore_id, NB_SOCKETS);
1021                 }
1022
1023                 if (pktmbuf_pool[portid][socketid] == NULL) {
1024                         snprintf(s, sizeof(s), "mbuf_pool_%d:%d",
1025                                  portid, socketid);
1026                         pktmbuf_pool[portid][socketid] =
1027                                 rte_pktmbuf_pool_create(s, nb_mbuf,
1028                                         MEMPOOL_CACHE_SIZE, 0,
1029                                         RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
1030                         if (pktmbuf_pool[portid][socketid] == NULL)
1031                                 rte_exit(EXIT_FAILURE,
1032                                         "Cannot init mbuf pool on socket %d\n",
1033                                         socketid);
1034                         else
1035                                 printf("Allocated mbuf pool on socket %d\n",
1036                                         socketid);
1037
1038                         /* Setup ACL, LPM, EM(f.e Hash) or FIB. But, only once per
1039                          * available socket.
1040                          */
1041                         if (!lkp_per_socket[socketid]) {
1042                                 l3fwd_lkp.setup(socketid);
1043                                 lkp_per_socket[socketid] = 1;
1044                         }
1045                 }
1046
1047                 if (evt_rsrc->vector_enabled && vector_pool[portid] == NULL) {
1048                         unsigned int nb_vec;
1049
1050                         nb_vec = (nb_mbuf + evt_rsrc->vector_size - 1) /
1051                                  evt_rsrc->vector_size;
1052                         snprintf(s, sizeof(s), "vector_pool_%d", portid);
1053                         vector_pool[portid] = rte_event_vector_pool_create(
1054                                 s, nb_vec, 0, evt_rsrc->vector_size, socketid);
1055                         if (vector_pool[portid] == NULL)
1056                                 rte_exit(EXIT_FAILURE,
1057                                          "Failed to create vector pool for port %d\n",
1058                                          portid);
1059                         else
1060                                 printf("Allocated vector pool for port %d\n",
1061                                        portid);
1062                 }
1063
1064                 qconf = &lcore_conf[lcore_id];
1065                 qconf->ipv4_lookup_struct =
1066                         l3fwd_lkp.get_ipv4_lookup_struct(socketid);
1067                 qconf->ipv6_lookup_struct =
1068                         l3fwd_lkp.get_ipv6_lookup_struct(socketid);
1069         }
1070         return 0;
1071 }
1072
1073 /* Check the link status of all ports in up to 9s, and print them finally */
1074 static void
1075 check_all_ports_link_status(uint32_t port_mask)
1076 {
1077 #define CHECK_INTERVAL 100 /* 100ms */
1078 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
1079         uint16_t portid;
1080         uint8_t count, all_ports_up, print_flag = 0;
1081         struct rte_eth_link link;
1082         int ret;
1083         char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
1084
1085         printf("\nChecking link status");
1086         fflush(stdout);
1087         for (count = 0; count <= MAX_CHECK_TIME; count++) {
1088                 if (force_quit)
1089                         return;
1090                 all_ports_up = 1;
1091                 RTE_ETH_FOREACH_DEV(portid) {
1092                         if (force_quit)
1093                                 return;
1094                         if ((port_mask & (1 << portid)) == 0)
1095                                 continue;
1096                         memset(&link, 0, sizeof(link));
1097                         ret = rte_eth_link_get_nowait(portid, &link);
1098                         if (ret < 0) {
1099                                 all_ports_up = 0;
1100                                 if (print_flag == 1)
1101                                         printf("Port %u link get failed: %s\n",
1102                                                 portid, rte_strerror(-ret));
1103                                 continue;
1104                         }
1105                         /* print link status if flag set */
1106                         if (print_flag == 1) {
1107                                 rte_eth_link_to_str(link_status_text,
1108                                         sizeof(link_status_text), &link);
1109                                 printf("Port %d %s\n", portid,
1110                                        link_status_text);
1111                                 continue;
1112                         }
1113                         /* clear all_ports_up flag if any link down */
1114                         if (link.link_status == RTE_ETH_LINK_DOWN) {
1115                                 all_ports_up = 0;
1116                                 break;
1117                         }
1118                 }
1119                 /* after finally printing all link status, get out */
1120                 if (print_flag == 1)
1121                         break;
1122
1123                 if (all_ports_up == 0) {
1124                         printf(".");
1125                         fflush(stdout);
1126                         rte_delay_ms(CHECK_INTERVAL);
1127                 }
1128
1129                 /* set the print_flag if all ports up or timeout */
1130                 if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
1131                         print_flag = 1;
1132                         printf("done\n");
1133                 }
1134         }
1135 }
1136
1137 static void
1138 signal_handler(int signum)
1139 {
1140         if (signum == SIGINT || signum == SIGTERM) {
1141                 printf("\n\nSignal %d received, preparing to exit...\n",
1142                                 signum);
1143                 force_quit = true;
1144         }
1145 }
1146
1147 static int
1148 prepare_ptype_parser(uint16_t portid, uint16_t queueid)
1149 {
1150         if (parse_ptype) {
1151                 printf("Port %d: softly parse packet type info\n", portid);
1152                 if (rte_eth_add_rx_callback(portid, queueid,
1153                                             l3fwd_lkp.cb_parse_ptype,
1154                                             NULL))
1155                         return 1;
1156
1157                 printf("Failed to add rx callback: port=%d\n", portid);
1158                 return 0;
1159         }
1160
1161         if (l3fwd_lkp.check_ptype(portid))
1162                 return 1;
1163
1164         printf("port %d cannot parse packet type, please add --%s\n",
1165                portid, CMD_LINE_OPT_PARSE_PTYPE);
1166         return 0;
1167 }
1168
1169 static uint32_t
1170 eth_dev_get_overhead_len(uint32_t max_rx_pktlen, uint16_t max_mtu)
1171 {
1172         uint32_t overhead_len;
1173
1174         if (max_mtu != UINT16_MAX && max_rx_pktlen > max_mtu)
1175                 overhead_len = max_rx_pktlen - max_mtu;
1176         else
1177                 overhead_len = RTE_ETHER_HDR_LEN + RTE_ETHER_CRC_LEN;
1178
1179         return overhead_len;
1180 }
1181
1182 static int
1183 config_port_max_pkt_len(struct rte_eth_conf *conf,
1184                 struct rte_eth_dev_info *dev_info)
1185 {
1186         uint32_t overhead_len;
1187
1188         if (max_pkt_len == 0)
1189                 return 0;
1190
1191         if (max_pkt_len < RTE_ETHER_MIN_LEN || max_pkt_len > MAX_JUMBO_PKT_LEN)
1192                 return -1;
1193
1194         overhead_len = eth_dev_get_overhead_len(dev_info->max_rx_pktlen,
1195                         dev_info->max_mtu);
1196         conf->rxmode.mtu = max_pkt_len - overhead_len;
1197
1198         if (conf->rxmode.mtu > RTE_ETHER_MTU)
1199                 conf->txmode.offloads |= RTE_ETH_TX_OFFLOAD_MULTI_SEGS;
1200
1201         return 0;
1202 }
1203
1204 static void
1205 l3fwd_poll_resource_setup(void)
1206 {
1207         uint8_t nb_rx_queue, queue, socketid;
1208         struct rte_eth_dev_info dev_info;
1209         uint32_t n_tx_queue, nb_lcores;
1210         struct rte_eth_txconf *txconf;
1211         struct lcore_conf *qconf;
1212         uint16_t queueid, portid;
1213         unsigned int nb_ports;
1214         unsigned int lcore_id;
1215         int ret;
1216
1217         if (check_lcore_params() < 0)
1218                 rte_exit(EXIT_FAILURE, "check_lcore_params failed\n");
1219
1220         ret = init_lcore_rx_queues();
1221         if (ret < 0)
1222                 rte_exit(EXIT_FAILURE, "init_lcore_rx_queues failed\n");
1223
1224         nb_ports = rte_eth_dev_count_avail();
1225
1226         if (check_port_config() < 0)
1227                 rte_exit(EXIT_FAILURE, "check_port_config failed\n");
1228
1229         nb_lcores = rte_lcore_count();
1230
1231         /* initialize all ports */
1232         RTE_ETH_FOREACH_DEV(portid) {
1233                 struct rte_eth_conf local_port_conf = port_conf;
1234
1235                 /* skip ports that are not enabled */
1236                 if ((enabled_port_mask & (1 << portid)) == 0) {
1237                         printf("\nSkipping disabled port %d\n", portid);
1238                         continue;
1239                 }
1240
1241                 /* init port */
1242                 printf("Initializing port %d ... ", portid );
1243                 fflush(stdout);
1244
1245                 nb_rx_queue = get_port_n_rx_queues(portid);
1246                 n_tx_queue = nb_lcores;
1247                 if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
1248                         n_tx_queue = MAX_TX_QUEUE_PER_PORT;
1249                 printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
1250                         nb_rx_queue, (unsigned)n_tx_queue );
1251
1252                 ret = rte_eth_dev_info_get(portid, &dev_info);
1253                 if (ret != 0)
1254                         rte_exit(EXIT_FAILURE,
1255                                 "Error during getting device (port %u) info: %s\n",
1256                                 portid, strerror(-ret));
1257
1258                 ret = config_port_max_pkt_len(&local_port_conf, &dev_info);
1259                 if (ret != 0)
1260                         rte_exit(EXIT_FAILURE,
1261                                 "Invalid max packet length: %u (port %u)\n",
1262                                 max_pkt_len, portid);
1263
1264                 if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
1265                         local_port_conf.txmode.offloads |=
1266                                 RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
1267
1268                 local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
1269                         dev_info.flow_type_rss_offloads;
1270
1271                 if (dev_info.max_rx_queues == 1)
1272                         local_port_conf.rxmode.mq_mode = RTE_ETH_MQ_RX_NONE;
1273
1274                 if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
1275                                 port_conf.rx_adv_conf.rss_conf.rss_hf) {
1276                         printf("Port %u modified RSS hash function based on hardware support,"
1277                                 "requested:%#"PRIx64" configured:%#"PRIx64"\n",
1278                                 portid,
1279                                 port_conf.rx_adv_conf.rss_conf.rss_hf,
1280                                 local_port_conf.rx_adv_conf.rss_conf.rss_hf);
1281                 }
1282
1283                 ret = rte_eth_dev_configure(portid, nb_rx_queue,
1284                                         (uint16_t)n_tx_queue, &local_port_conf);
1285                 if (ret < 0)
1286                         rte_exit(EXIT_FAILURE,
1287                                 "Cannot configure device: err=%d, port=%d\n",
1288                                 ret, portid);
1289
1290                 ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
1291                                                        &nb_txd);
1292                 if (ret < 0)
1293                         rte_exit(EXIT_FAILURE,
1294                                  "Cannot adjust number of descriptors: err=%d, "
1295                                  "port=%d\n", ret, portid);
1296
1297                 ret = rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
1298                 if (ret < 0)
1299                         rte_exit(EXIT_FAILURE,
1300                                  "Cannot get MAC address: err=%d, port=%d\n",
1301                                  ret, portid);
1302
1303                 print_ethaddr(" Address:", &ports_eth_addr[portid]);
1304                 printf(", ");
1305                 print_ethaddr("Destination:",
1306                         (const struct rte_ether_addr *)&dest_eth_addr[portid]);
1307                 printf(", ");
1308
1309                 /*
1310                  * prepare src MACs for each port.
1311                  */
1312                 rte_ether_addr_copy(&ports_eth_addr[portid],
1313                         (struct rte_ether_addr *)(val_eth + portid) + 1);
1314
1315                 /* init memory */
1316                 if (!per_port_pool) {
1317                         /* portid = 0; this is *not* signifying the first port,
1318                          * rather, it signifies that portid is ignored.
1319                          */
1320                         ret = init_mem(0, NB_MBUF(nb_ports));
1321                 } else {
1322                         ret = init_mem(portid, NB_MBUF(1));
1323                 }
1324                 if (ret < 0)
1325                         rte_exit(EXIT_FAILURE, "init_mem failed\n");
1326
1327                 /* init one TX queue per couple (lcore,port) */
1328                 queueid = 0;
1329                 for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1330                         if (rte_lcore_is_enabled(lcore_id) == 0)
1331                                 continue;
1332
1333                         if (numa_on)
1334                                 socketid =
1335                                 (uint8_t)rte_lcore_to_socket_id(lcore_id);
1336                         else
1337                                 socketid = 0;
1338
1339                         printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
1340                         fflush(stdout);
1341
1342                         txconf = &dev_info.default_txconf;
1343                         txconf->offloads = local_port_conf.txmode.offloads;
1344                         ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
1345                                                      socketid, txconf);
1346                         if (ret < 0)
1347                                 rte_exit(EXIT_FAILURE,
1348                                         "rte_eth_tx_queue_setup: err=%d, "
1349                                         "port=%d\n", ret, portid);
1350
1351                         qconf = &lcore_conf[lcore_id];
1352                         qconf->tx_queue_id[portid] = queueid;
1353                         queueid++;
1354
1355                         qconf->tx_port_id[qconf->n_tx_port] = portid;
1356                         qconf->n_tx_port++;
1357                 }
1358                 printf("\n");
1359         }
1360
1361         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1362                 if (rte_lcore_is_enabled(lcore_id) == 0)
1363                         continue;
1364                 qconf = &lcore_conf[lcore_id];
1365                 printf("\nInitializing rx queues on lcore %u ... ", lcore_id );
1366                 fflush(stdout);
1367                 /* init RX queues */
1368                 for(queue = 0; queue < qconf->n_rx_queue; ++queue) {
1369                         struct rte_eth_rxconf rxq_conf;
1370
1371                         portid = qconf->rx_queue_list[queue].port_id;
1372                         queueid = qconf->rx_queue_list[queue].queue_id;
1373
1374                         if (numa_on)
1375                                 socketid =
1376                                 (uint8_t)rte_lcore_to_socket_id(lcore_id);
1377                         else
1378                                 socketid = 0;
1379
1380                         printf("rxq=%d,%d,%d ", portid, queueid, socketid);
1381                         fflush(stdout);
1382
1383                         ret = rte_eth_dev_info_get(portid, &dev_info);
1384                         if (ret != 0)
1385                                 rte_exit(EXIT_FAILURE,
1386                                         "Error during getting device (port %u) info: %s\n",
1387                                         portid, strerror(-ret));
1388
1389                         rxq_conf = dev_info.default_rxconf;
1390                         rxq_conf.offloads = port_conf.rxmode.offloads;
1391                         if (!per_port_pool)
1392                                 ret = rte_eth_rx_queue_setup(portid, queueid,
1393                                                 nb_rxd, socketid,
1394                                                 &rxq_conf,
1395                                                 pktmbuf_pool[0][socketid]);
1396                         else
1397                                 ret = rte_eth_rx_queue_setup(portid, queueid,
1398                                                 nb_rxd, socketid,
1399                                                 &rxq_conf,
1400                                                 pktmbuf_pool[portid][socketid]);
1401                         if (ret < 0)
1402                                 rte_exit(EXIT_FAILURE,
1403                                 "rte_eth_rx_queue_setup: err=%d, port=%d\n",
1404                                 ret, portid);
1405                 }
1406         }
1407 }
1408
1409 static inline int
1410 l3fwd_service_enable(uint32_t service_id)
1411 {
1412         uint8_t min_service_count = UINT8_MAX;
1413         uint32_t slcore_array[RTE_MAX_LCORE];
1414         unsigned int slcore = 0;
1415         uint8_t service_count;
1416         int32_t slcore_count;
1417
1418         if (!rte_service_lcore_count())
1419                 return -ENOENT;
1420
1421         slcore_count = rte_service_lcore_list(slcore_array, RTE_MAX_LCORE);
1422         if (slcore_count < 0)
1423                 return -ENOENT;
1424         /* Get the core which has least number of services running. */
1425         while (slcore_count--) {
1426                 /* Reset default mapping */
1427                 if (rte_service_map_lcore_set(service_id,
1428                                 slcore_array[slcore_count], 0) != 0)
1429                         return -ENOENT;
1430                 service_count = rte_service_lcore_count_services(
1431                                 slcore_array[slcore_count]);
1432                 if (service_count < min_service_count) {
1433                         slcore = slcore_array[slcore_count];
1434                         min_service_count = service_count;
1435                 }
1436         }
1437         if (rte_service_map_lcore_set(service_id, slcore, 1))
1438                 return -ENOENT;
1439         rte_service_lcore_start(slcore);
1440
1441         return 0;
1442 }
1443
1444 static void
1445 l3fwd_event_service_setup(void)
1446 {
1447         struct l3fwd_event_resources *evt_rsrc = l3fwd_get_eventdev_rsrc();
1448         struct rte_event_dev_info evdev_info;
1449         uint32_t service_id, caps;
1450         int ret, i;
1451
1452         rte_event_dev_info_get(evt_rsrc->event_d_id, &evdev_info);
1453         if (!(evdev_info.event_dev_cap & RTE_EVENT_DEV_CAP_DISTRIBUTED_SCHED)) {
1454                 ret = rte_event_dev_service_id_get(evt_rsrc->event_d_id,
1455                                 &service_id);
1456                 if (ret != -ESRCH && ret != 0)
1457                         rte_exit(EXIT_FAILURE,
1458                                  "Error in starting eventdev service\n");
1459                 l3fwd_service_enable(service_id);
1460         }
1461
1462         for (i = 0; i < evt_rsrc->rx_adptr.nb_rx_adptr; i++) {
1463                 ret = rte_event_eth_rx_adapter_caps_get(evt_rsrc->event_d_id,
1464                                 evt_rsrc->rx_adptr.rx_adptr[i], &caps);
1465                 if (ret < 0)
1466                         rte_exit(EXIT_FAILURE,
1467                                  "Failed to get Rx adapter[%d] caps\n",
1468                                  evt_rsrc->rx_adptr.rx_adptr[i]);
1469                 ret = rte_event_eth_rx_adapter_service_id_get(
1470                                 evt_rsrc->event_d_id,
1471                                 &service_id);
1472                 if (ret != -ESRCH && ret != 0)
1473                         rte_exit(EXIT_FAILURE,
1474                                  "Error in starting Rx adapter[%d] service\n",
1475                                  evt_rsrc->rx_adptr.rx_adptr[i]);
1476                 l3fwd_service_enable(service_id);
1477         }
1478
1479         for (i = 0; i < evt_rsrc->tx_adptr.nb_tx_adptr; i++) {
1480                 ret = rte_event_eth_tx_adapter_caps_get(evt_rsrc->event_d_id,
1481                                 evt_rsrc->tx_adptr.tx_adptr[i], &caps);
1482                 if (ret < 0)
1483                         rte_exit(EXIT_FAILURE,
1484                                  "Failed to get Rx adapter[%d] caps\n",
1485                                  evt_rsrc->tx_adptr.tx_adptr[i]);
1486                 ret = rte_event_eth_tx_adapter_service_id_get(
1487                                 evt_rsrc->event_d_id,
1488                                 &service_id);
1489                 if (ret != -ESRCH && ret != 0)
1490                         rte_exit(EXIT_FAILURE,
1491                                  "Error in starting Rx adapter[%d] service\n",
1492                                  evt_rsrc->tx_adptr.tx_adptr[i]);
1493                 l3fwd_service_enable(service_id);
1494         }
1495 }
1496
1497 int
1498 main(int argc, char **argv)
1499 {
1500         struct l3fwd_event_resources *evt_rsrc;
1501         struct lcore_conf *qconf;
1502         uint16_t queueid, portid;
1503         unsigned int lcore_id;
1504         uint8_t queue;
1505         int i, ret;
1506
1507         /* init EAL */
1508         ret = rte_eal_init(argc, argv);
1509         if (ret < 0)
1510                 rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
1511         argc -= ret;
1512         argv += ret;
1513
1514         force_quit = false;
1515         signal(SIGINT, signal_handler);
1516         signal(SIGTERM, signal_handler);
1517
1518         /* pre-init dst MACs for all ports to 02:00:00:00:00:xx */
1519         for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
1520                 dest_eth_addr[portid] =
1521                         RTE_ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
1522                 *(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
1523         }
1524
1525         evt_rsrc = l3fwd_get_eventdev_rsrc();
1526         /* parse application arguments (after the EAL ones) */
1527         ret = parse_args(argc, argv);
1528         if (ret < 0)
1529                 rte_exit(EXIT_FAILURE, "Invalid L3FWD parameters\n");
1530
1531         /* Setup function pointers for lookup method. */
1532         setup_l3fwd_lookup_tables();
1533
1534         /* Add the config file rules */
1535         l3fwd_lkp.read_config_files();
1536
1537         evt_rsrc->per_port_pool = per_port_pool;
1538         evt_rsrc->pkt_pool = pktmbuf_pool;
1539         evt_rsrc->vec_pool = vector_pool;
1540         evt_rsrc->port_mask = enabled_port_mask;
1541         /* Configure eventdev parameters if user has requested */
1542         if (evt_rsrc->enabled) {
1543                 l3fwd_event_resource_setup(&port_conf);
1544                 if (lookup_mode == L3FWD_LOOKUP_EM)
1545                         l3fwd_lkp.main_loop = evt_rsrc->ops.em_event_loop;
1546                 else if (lookup_mode == L3FWD_LOOKUP_FIB)
1547                         l3fwd_lkp.main_loop = evt_rsrc->ops.fib_event_loop;
1548                 else
1549                         l3fwd_lkp.main_loop = evt_rsrc->ops.lpm_event_loop;
1550                 l3fwd_event_service_setup();
1551         } else
1552                 l3fwd_poll_resource_setup();
1553
1554         /* start ports */
1555         RTE_ETH_FOREACH_DEV(portid) {
1556                 if ((enabled_port_mask & (1 << portid)) == 0) {
1557                         continue;
1558                 }
1559                 /* Start device */
1560                 ret = rte_eth_dev_start(portid);
1561                 if (ret < 0)
1562                         rte_exit(EXIT_FAILURE,
1563                                 "rte_eth_dev_start: err=%d, port=%d\n",
1564                                 ret, portid);
1565
1566                 /*
1567                  * If enabled, put device in promiscuous mode.
1568                  * This allows IO forwarding mode to forward packets
1569                  * to itself through 2 cross-connected  ports of the
1570                  * target machine.
1571                  */
1572                 if (promiscuous_on) {
1573                         ret = rte_eth_promiscuous_enable(portid);
1574                         if (ret != 0)
1575                                 rte_exit(EXIT_FAILURE,
1576                                         "rte_eth_promiscuous_enable: err=%s, port=%u\n",
1577                                         rte_strerror(-ret), portid);
1578                 }
1579         }
1580
1581         printf("\n");
1582
1583         for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1584                 if (rte_lcore_is_enabled(lcore_id) == 0)
1585                         continue;
1586                 qconf = &lcore_conf[lcore_id];
1587                 for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
1588                         portid = qconf->rx_queue_list[queue].port_id;
1589                         queueid = qconf->rx_queue_list[queue].queue_id;
1590                         if (prepare_ptype_parser(portid, queueid) == 0)
1591                                 rte_exit(EXIT_FAILURE, "ptype check fails\n");
1592                 }
1593         }
1594
1595         check_all_ports_link_status(enabled_port_mask);
1596
1597         ret = 0;
1598         /* launch per-lcore init on every lcore */
1599         rte_eal_mp_remote_launch(l3fwd_lkp.main_loop, NULL, CALL_MAIN);
1600         if (evt_rsrc->enabled) {
1601                 for (i = 0; i < evt_rsrc->rx_adptr.nb_rx_adptr; i++)
1602                         rte_event_eth_rx_adapter_stop(
1603                                         evt_rsrc->rx_adptr.rx_adptr[i]);
1604                 for (i = 0; i < evt_rsrc->tx_adptr.nb_tx_adptr; i++)
1605                         rte_event_eth_tx_adapter_stop(
1606                                         evt_rsrc->tx_adptr.tx_adptr[i]);
1607
1608                 RTE_ETH_FOREACH_DEV(portid) {
1609                         if ((enabled_port_mask & (1 << portid)) == 0)
1610                                 continue;
1611                         ret = rte_eth_dev_stop(portid);
1612                         if (ret != 0)
1613                                 printf("rte_eth_dev_stop: err=%d, port=%u\n",
1614                                        ret, portid);
1615                 }
1616
1617                 rte_eal_mp_wait_lcore();
1618                 RTE_ETH_FOREACH_DEV(portid) {
1619                         if ((enabled_port_mask & (1 << portid)) == 0)
1620                                 continue;
1621                         rte_eth_dev_close(portid);
1622                 }
1623
1624                 rte_event_dev_stop(evt_rsrc->event_d_id);
1625                 rte_event_dev_close(evt_rsrc->event_d_id);
1626
1627         } else {
1628                 rte_eal_mp_wait_lcore();
1629
1630                 RTE_ETH_FOREACH_DEV(portid) {
1631                         if ((enabled_port_mask & (1 << portid)) == 0)
1632                                 continue;
1633                         printf("Closing port %d...", portid);
1634                         ret = rte_eth_dev_stop(portid);
1635                         if (ret != 0)
1636                                 printf("rte_eth_dev_stop: err=%d, port=%u\n",
1637                                        ret, portid);
1638                         rte_eth_dev_close(portid);
1639                         printf(" Done\n");
1640                 }
1641         }
1642
1643         /* clean up config file routes */
1644         l3fwd_lkp.free_routes();
1645
1646         /* clean up the EAL */
1647         rte_eal_cleanup();
1648
1649         printf("Bye...\n");
1650
1651         return ret;
1652 }