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