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