xref: /dpdk/examples/l3fwd-graph/main.c (revision 8809f78c7dd9f33a44a4f89c58fc91ded34296ed)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(C) 2020 Marvell International Ltd.
3  */
4 
5 #include <arpa/inet.h>
6 #include <errno.h>
7 #include <getopt.h>
8 #include <inttypes.h>
9 #include <signal.h>
10 #include <stdarg.h>
11 #include <stdbool.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <sys/socket.h>
17 #include <sys/types.h>
18 #include <sys/queue.h>
19 #include <unistd.h>
20 
21 #include <rte_branch_prediction.h>
22 #include <rte_common.h>
23 #include <rte_cycles.h>
24 #include <rte_eal.h>
25 #include <rte_ethdev.h>
26 #include <rte_graph_worker.h>
27 #include <rte_launch.h>
28 #include <rte_lcore.h>
29 #include <rte_log.h>
30 #include <rte_mempool.h>
31 #include <rte_node_eth_api.h>
32 #include <rte_node_ip4_api.h>
33 #include <rte_per_lcore.h>
34 #include <rte_string_fns.h>
35 #include <rte_vect.h>
36 
37 #include <cmdline_parse.h>
38 #include <cmdline_parse_etheraddr.h>
39 
40 /* Log type */
41 #define RTE_LOGTYPE_L3FWD_GRAPH RTE_LOGTYPE_USER1
42 
43 /*
44  * Configurable number of RX/TX ring descriptors
45  */
46 #define RTE_TEST_RX_DESC_DEFAULT 1024
47 #define RTE_TEST_TX_DESC_DEFAULT 1024
48 
49 #define MAX_TX_QUEUE_PER_PORT RTE_MAX_ETHPORTS
50 #define MAX_RX_QUEUE_PER_PORT 128
51 
52 #define MAX_RX_QUEUE_PER_LCORE 16
53 
54 #define MAX_LCORE_PARAMS 1024
55 
56 #define NB_SOCKETS 8
57 
58 /* Static global variables used within this file. */
59 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
60 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
61 
62 /**< Ports set in promiscuous mode off by default. */
63 static int promiscuous_on;
64 
65 static int numa_on = 1;	  /**< NUMA is enabled by default. */
66 static int per_port_pool; /**< Use separate buffer pools per port; disabled */
67 			  /**< by default */
68 
69 static volatile bool force_quit;
70 
71 /* Ethernet addresses of ports */
72 static uint64_t dest_eth_addr[RTE_MAX_ETHPORTS];
73 static struct rte_ether_addr ports_eth_addr[RTE_MAX_ETHPORTS];
74 xmm_t val_eth[RTE_MAX_ETHPORTS];
75 
76 /* Mask of enabled ports */
77 static uint32_t enabled_port_mask;
78 
79 struct lcore_rx_queue {
80 	uint16_t port_id;
81 	uint8_t queue_id;
82 	char node_name[RTE_NODE_NAMESIZE];
83 };
84 
85 /* Lcore conf */
86 struct lcore_conf {
87 	uint16_t n_rx_queue;
88 	struct lcore_rx_queue rx_queue_list[MAX_RX_QUEUE_PER_LCORE];
89 
90 	struct rte_graph *graph;
91 	char name[RTE_GRAPH_NAMESIZE];
92 	rte_graph_t graph_id;
93 } __rte_cache_aligned;
94 
95 static 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}, {0, 1, 2}, {0, 2, 2}, {1, 0, 2}, {1, 1, 2},
106 	{1, 2, 2}, {2, 0, 2}, {3, 0, 3}, {3, 1, 3},
107 };
108 
109 static struct lcore_params *lcore_params = lcore_params_array_default;
110 static uint16_t nb_lcore_params = RTE_DIM(lcore_params_array_default);
111 
112 static struct rte_eth_conf port_conf = {
113 	.rxmode = {
114 		.mq_mode = ETH_MQ_RX_RSS,
115 		.max_rx_pkt_len = RTE_ETHER_MAX_LEN,
116 		.split_hdr_size = 0,
117 	},
118 	.rx_adv_conf = {
119 		.rss_conf = {
120 				.rss_key = NULL,
121 				.rss_hf = ETH_RSS_IP,
122 		},
123 	},
124 	.txmode = {
125 		.mq_mode = ETH_MQ_TX_NONE,
126 	},
127 };
128 
129 static struct rte_mempool *pktmbuf_pool[RTE_MAX_ETHPORTS][NB_SOCKETS];
130 
131 static struct rte_node_ethdev_config ethdev_conf[RTE_MAX_ETHPORTS];
132 
133 struct ipv4_l3fwd_lpm_route {
134 	uint32_t ip;
135 	uint8_t depth;
136 	uint8_t if_out;
137 };
138 
139 #define IPV4_L3FWD_LPM_NUM_ROUTES                                              \
140 	(sizeof(ipv4_l3fwd_lpm_route_array) /                                  \
141 	 sizeof(ipv4_l3fwd_lpm_route_array[0]))
142 /* 198.18.0.0/16 are set aside for RFC2544 benchmarking. */
143 static struct ipv4_l3fwd_lpm_route ipv4_l3fwd_lpm_route_array[] = {
144 	{RTE_IPV4(198, 18, 0, 0), 24, 0}, {RTE_IPV4(198, 18, 1, 0), 24, 1},
145 	{RTE_IPV4(198, 18, 2, 0), 24, 2}, {RTE_IPV4(198, 18, 3, 0), 24, 3},
146 	{RTE_IPV4(198, 18, 4, 0), 24, 4}, {RTE_IPV4(198, 18, 5, 0), 24, 5},
147 	{RTE_IPV4(198, 18, 6, 0), 24, 6}, {RTE_IPV4(198, 18, 7, 0), 24, 7},
148 };
149 
150 static int
151 check_lcore_params(void)
152 {
153 	uint8_t queue, lcore;
154 	int socketid;
155 	uint16_t i;
156 
157 	for (i = 0; i < nb_lcore_params; ++i) {
158 		queue = lcore_params[i].queue_id;
159 		if (queue >= MAX_RX_QUEUE_PER_PORT) {
160 			printf("Invalid queue number: %hhu\n", queue);
161 			return -1;
162 		}
163 		lcore = lcore_params[i].lcore_id;
164 		if (!rte_lcore_is_enabled(lcore)) {
165 			printf("Error: lcore %hhu is not enabled in lcore mask\n",
166 			       lcore);
167 			return -1;
168 		}
169 
170 		if (lcore == rte_get_main_lcore()) {
171 			printf("Error: lcore %u is main lcore\n", lcore);
172 			return -1;
173 		}
174 		socketid = rte_lcore_to_socket_id(lcore);
175 		if ((socketid != 0) && (numa_on == 0)) {
176 			printf("Warning: lcore %hhu is on socket %d with numa off\n",
177 			       lcore, socketid);
178 		}
179 	}
180 
181 	return 0;
182 }
183 
184 static int
185 check_port_config(void)
186 {
187 	uint16_t portid;
188 	uint16_t i;
189 
190 	for (i = 0; i < nb_lcore_params; ++i) {
191 		portid = lcore_params[i].port_id;
192 		if ((enabled_port_mask & (1 << portid)) == 0) {
193 			printf("Port %u is not enabled in port mask\n", portid);
194 			return -1;
195 		}
196 		if (!rte_eth_dev_is_valid_port(portid)) {
197 			printf("Port %u is not present on the board\n", portid);
198 			return -1;
199 		}
200 	}
201 
202 	return 0;
203 }
204 
205 static uint8_t
206 get_port_n_rx_queues(const uint16_t port)
207 {
208 	int queue = -1;
209 	uint16_t i;
210 
211 	for (i = 0; i < nb_lcore_params; ++i) {
212 		if (lcore_params[i].port_id == port) {
213 			if (lcore_params[i].queue_id == queue + 1)
214 				queue = lcore_params[i].queue_id;
215 			else
216 				rte_exit(EXIT_FAILURE,
217 					 "Queue ids of the port %d must be"
218 					 " in sequence and must start with 0\n",
219 					 lcore_params[i].port_id);
220 		}
221 	}
222 
223 	return (uint8_t)(++queue);
224 }
225 
226 static int
227 init_lcore_rx_queues(void)
228 {
229 	uint16_t i, nb_rx_queue;
230 	uint8_t lcore;
231 
232 	for (i = 0; i < nb_lcore_params; ++i) {
233 		lcore = lcore_params[i].lcore_id;
234 		nb_rx_queue = lcore_conf[lcore].n_rx_queue;
235 		if (nb_rx_queue >= MAX_RX_QUEUE_PER_LCORE) {
236 			printf("Error: too many queues (%u) for lcore: %u\n",
237 			       (unsigned int)nb_rx_queue + 1,
238 			       (unsigned int)lcore);
239 			return -1;
240 		}
241 
242 		lcore_conf[lcore].rx_queue_list[nb_rx_queue].port_id =
243 			lcore_params[i].port_id;
244 		lcore_conf[lcore].rx_queue_list[nb_rx_queue].queue_id =
245 			lcore_params[i].queue_id;
246 		lcore_conf[lcore].n_rx_queue++;
247 	}
248 
249 	return 0;
250 }
251 
252 /* Display usage */
253 static void
254 print_usage(const char *prgname)
255 {
256 	fprintf(stderr,
257 		"%s [EAL options] --"
258 		" -p PORTMASK"
259 		" [-P]"
260 		" --config (port,queue,lcore)[,(port,queue,lcore)]"
261 		" [--eth-dest=X,MM:MM:MM:MM:MM:MM]"
262 		" [--enable-jumbo [--max-pkt-len PKTLEN]]"
263 		" [--no-numa]"
264 		" [--per-port-pool]\n\n"
265 
266 		"  -p PORTMASK: Hexadecimal bitmask of ports to configure\n"
267 		"  -P : Enable promiscuous mode\n"
268 		"  --config (port,queue,lcore): Rx queue configuration\n"
269 		"  --eth-dest=X,MM:MM:MM:MM:MM:MM: Ethernet destination for "
270 		"port X\n"
271 		"  --enable-jumbo: Enable jumbo frames\n"
272 		"  --max-pkt-len: Under the premise of enabling jumbo,\n"
273 		"                 maximum packet length in decimal (64-9600)\n"
274 		"  --no-numa: Disable numa awareness\n"
275 		"  --per-port-pool: Use separate buffer pool per port\n\n",
276 		prgname);
277 }
278 
279 static int
280 parse_max_pkt_len(const char *pktlen)
281 {
282 	unsigned long len;
283 	char *end = NULL;
284 
285 	/* Parse decimal string */
286 	len = strtoul(pktlen, &end, 10);
287 	if ((pktlen[0] == '\0') || (end == NULL) || (*end != '\0'))
288 		return -1;
289 
290 	if (len == 0)
291 		return -1;
292 
293 	return len;
294 }
295 
296 static int
297 parse_portmask(const char *portmask)
298 {
299 	char *end = NULL;
300 	unsigned long pm;
301 
302 	/* Parse hexadecimal string */
303 	pm = strtoul(portmask, &end, 16);
304 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
305 		return 0;
306 
307 	return pm;
308 }
309 
310 static int
311 parse_config(const char *q_arg)
312 {
313 	enum fieldnames { FLD_PORT = 0, FLD_QUEUE, FLD_LCORE, _NUM_FLD };
314 	unsigned long int_fld[_NUM_FLD];
315 	const char *p, *p0 = q_arg;
316 	char *str_fld[_NUM_FLD];
317 	uint32_t size;
318 	char s[256];
319 	char *end;
320 	int i;
321 
322 	nb_lcore_params = 0;
323 
324 	while ((p = strchr(p0, '(')) != NULL) {
325 		++p;
326 		p0 = strchr(p, ')');
327 		if (p0 == NULL)
328 			return -1;
329 
330 		size = p0 - p;
331 		if (size >= sizeof(s))
332 			return -1;
333 
334 		memcpy(s, p, size);
335 		s[size] = '\0';
336 		if (rte_strsplit(s, sizeof(s), str_fld, _NUM_FLD, ',') !=
337 		    _NUM_FLD)
338 			return -1;
339 		for (i = 0; i < _NUM_FLD; i++) {
340 			errno = 0;
341 			int_fld[i] = strtoul(str_fld[i], &end, 0);
342 			if (errno != 0 || end == str_fld[i])
343 				return -1;
344 		}
345 
346 		if (nb_lcore_params >= MAX_LCORE_PARAMS) {
347 			printf("Exceeded max number of lcore params: %hu\n",
348 			       nb_lcore_params);
349 			return -1;
350 		}
351 
352 		if (int_fld[FLD_PORT] >= RTE_MAX_ETHPORTS ||
353 		    int_fld[FLD_LCORE] >= RTE_MAX_LCORE) {
354 			printf("Invalid port/lcore id\n");
355 			return -1;
356 		}
357 
358 		lcore_params_array[nb_lcore_params].port_id =
359 			(uint8_t)int_fld[FLD_PORT];
360 		lcore_params_array[nb_lcore_params].queue_id =
361 			(uint8_t)int_fld[FLD_QUEUE];
362 		lcore_params_array[nb_lcore_params].lcore_id =
363 			(uint8_t)int_fld[FLD_LCORE];
364 		++nb_lcore_params;
365 	}
366 	lcore_params = lcore_params_array;
367 
368 	return 0;
369 }
370 
371 static void
372 parse_eth_dest(const char *optarg)
373 {
374 	uint8_t c, *dest, peer_addr[6];
375 	uint16_t portid;
376 	char *port_end;
377 
378 	errno = 0;
379 	portid = strtoul(optarg, &port_end, 10);
380 	if (errno != 0 || port_end == optarg || *port_end++ != ',')
381 		rte_exit(EXIT_FAILURE, "Invalid eth-dest: %s", optarg);
382 	if (portid >= RTE_MAX_ETHPORTS)
383 		rte_exit(EXIT_FAILURE,
384 			 "eth-dest: port %d >= RTE_MAX_ETHPORTS(%d)\n", portid,
385 			 RTE_MAX_ETHPORTS);
386 
387 	if (cmdline_parse_etheraddr(NULL, port_end, &peer_addr,
388 				    sizeof(peer_addr)) < 0)
389 		rte_exit(EXIT_FAILURE, "Invalid ethernet address: %s\n",
390 			 port_end);
391 	dest = (uint8_t *)&dest_eth_addr[portid];
392 	for (c = 0; c < 6; c++)
393 		dest[c] = peer_addr[c];
394 	*(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
395 }
396 
397 #define MAX_JUMBO_PKT_LEN  9600
398 #define MEMPOOL_CACHE_SIZE 256
399 
400 static const char short_options[] = "p:" /* portmask */
401 				    "P"	 /* promiscuous */
402 	;
403 
404 #define CMD_LINE_OPT_CONFIG	   "config"
405 #define CMD_LINE_OPT_ETH_DEST	   "eth-dest"
406 #define CMD_LINE_OPT_NO_NUMA	   "no-numa"
407 #define CMD_LINE_OPT_ENABLE_JUMBO  "enable-jumbo"
408 #define CMD_LINE_OPT_PER_PORT_POOL "per-port-pool"
409 enum {
410 	/* Long options mapped to a short option */
411 
412 	/* First long only option value must be >= 256, so that we won't
413 	 * conflict with short options
414 	 */
415 	CMD_LINE_OPT_MIN_NUM = 256,
416 	CMD_LINE_OPT_CONFIG_NUM,
417 	CMD_LINE_OPT_ETH_DEST_NUM,
418 	CMD_LINE_OPT_NO_NUMA_NUM,
419 	CMD_LINE_OPT_ENABLE_JUMBO_NUM,
420 	CMD_LINE_OPT_PARSE_PER_PORT_POOL,
421 };
422 
423 static const struct option lgopts[] = {
424 	{CMD_LINE_OPT_CONFIG, 1, 0, CMD_LINE_OPT_CONFIG_NUM},
425 	{CMD_LINE_OPT_ETH_DEST, 1, 0, CMD_LINE_OPT_ETH_DEST_NUM},
426 	{CMD_LINE_OPT_NO_NUMA, 0, 0, CMD_LINE_OPT_NO_NUMA_NUM},
427 	{CMD_LINE_OPT_ENABLE_JUMBO, 0, 0, CMD_LINE_OPT_ENABLE_JUMBO_NUM},
428 	{CMD_LINE_OPT_PER_PORT_POOL, 0, 0, CMD_LINE_OPT_PARSE_PER_PORT_POOL},
429 	{NULL, 0, 0, 0},
430 };
431 
432 /*
433  * This expression is used to calculate the number of mbufs needed
434  * depending on user input, taking  into account memory for rx and
435  * tx hardware rings, cache per lcore and mtable per port per lcore.
436  * RTE_MAX is used to ensure that NB_MBUF never goes below a minimum
437  * value of 8192
438  */
439 #define NB_MBUF(nports)                                                        \
440 	RTE_MAX((nports * nb_rx_queue * nb_rxd +                               \
441 		 nports * nb_lcores * RTE_GRAPH_BURST_SIZE +                   \
442 		 nports * n_tx_queue * nb_txd +                                \
443 		 nb_lcores * MEMPOOL_CACHE_SIZE), 8192u)
444 
445 /* Parse the argument given in the command line of the application */
446 static int
447 parse_args(int argc, char **argv)
448 {
449 	char *prgname = argv[0];
450 	int option_index;
451 	char **argvopt;
452 	int opt, ret;
453 
454 	argvopt = argv;
455 
456 	/* Error or normal output strings. */
457 	while ((opt = getopt_long(argc, argvopt, short_options, lgopts,
458 				  &option_index)) != EOF) {
459 
460 		switch (opt) {
461 		/* Portmask */
462 		case 'p':
463 			enabled_port_mask = parse_portmask(optarg);
464 			if (enabled_port_mask == 0) {
465 				fprintf(stderr, "Invalid portmask\n");
466 				print_usage(prgname);
467 				return -1;
468 			}
469 			break;
470 
471 		case 'P':
472 			promiscuous_on = 1;
473 			break;
474 
475 		/* Long options */
476 		case CMD_LINE_OPT_CONFIG_NUM:
477 			ret = parse_config(optarg);
478 			if (ret) {
479 				fprintf(stderr, "Invalid config\n");
480 				print_usage(prgname);
481 				return -1;
482 			}
483 			break;
484 
485 		case CMD_LINE_OPT_ETH_DEST_NUM:
486 			parse_eth_dest(optarg);
487 			break;
488 
489 		case CMD_LINE_OPT_NO_NUMA_NUM:
490 			numa_on = 0;
491 			break;
492 
493 		case CMD_LINE_OPT_ENABLE_JUMBO_NUM: {
494 			const struct option lenopts = {"max-pkt-len",
495 						       required_argument, 0, 0};
496 
497 			port_conf.rxmode.offloads |= DEV_RX_OFFLOAD_JUMBO_FRAME;
498 			port_conf.txmode.offloads |= DEV_TX_OFFLOAD_MULTI_SEGS;
499 
500 			/*
501 			 * if no max-pkt-len set, use the default
502 			 * value RTE_ETHER_MAX_LEN.
503 			 */
504 			if (getopt_long(argc, argvopt, "", &lenopts,
505 					&option_index) == 0) {
506 				ret = parse_max_pkt_len(optarg);
507 				if (ret < 64 || ret > MAX_JUMBO_PKT_LEN) {
508 					fprintf(stderr, "Invalid maximum "
509 							"packet length\n");
510 					print_usage(prgname);
511 					return -1;
512 				}
513 				port_conf.rxmode.max_rx_pkt_len = ret;
514 			}
515 			break;
516 		}
517 
518 		case CMD_LINE_OPT_PARSE_PER_PORT_POOL:
519 			printf("Per port buffer pool is enabled\n");
520 			per_port_pool = 1;
521 			break;
522 
523 		default:
524 			print_usage(prgname);
525 			return -1;
526 		}
527 	}
528 
529 	if (optind >= 0)
530 		argv[optind - 1] = prgname;
531 	ret = optind - 1;
532 	optind = 1; /* Reset getopt lib */
533 
534 	return ret;
535 }
536 
537 static void
538 print_ethaddr(const char *name, const struct rte_ether_addr *eth_addr)
539 {
540 	char buf[RTE_ETHER_ADDR_FMT_SIZE];
541 	rte_ether_format_addr(buf, RTE_ETHER_ADDR_FMT_SIZE, eth_addr);
542 	printf("%s%s", name, buf);
543 }
544 
545 static int
546 init_mem(uint16_t portid, uint32_t nb_mbuf)
547 {
548 	uint32_t lcore_id;
549 	int socketid;
550 	char s[64];
551 
552 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
553 		if (rte_lcore_is_enabled(lcore_id) == 0)
554 			continue;
555 
556 		if (numa_on)
557 			socketid = rte_lcore_to_socket_id(lcore_id);
558 		else
559 			socketid = 0;
560 
561 		if (socketid >= NB_SOCKETS) {
562 			rte_exit(EXIT_FAILURE,
563 				 "Socket %d of lcore %u is out of range %d\n",
564 				 socketid, lcore_id, NB_SOCKETS);
565 		}
566 
567 		if (pktmbuf_pool[portid][socketid] == NULL) {
568 			snprintf(s, sizeof(s), "mbuf_pool_%d:%d", portid,
569 				 socketid);
570 			/* Create a pool with priv size of a cacheline */
571 			pktmbuf_pool[portid][socketid] =
572 				rte_pktmbuf_pool_create(
573 					s, nb_mbuf, MEMPOOL_CACHE_SIZE,
574 					RTE_CACHE_LINE_SIZE,
575 					RTE_MBUF_DEFAULT_BUF_SIZE, socketid);
576 			if (pktmbuf_pool[portid][socketid] == NULL)
577 				rte_exit(EXIT_FAILURE,
578 					 "Cannot init mbuf pool on socket %d\n",
579 					 socketid);
580 			else
581 				printf("Allocated mbuf pool on socket %d\n",
582 				       socketid);
583 		}
584 	}
585 
586 	return 0;
587 }
588 
589 /* Check the link status of all ports in up to 9s, and print them finally */
590 static void
591 check_all_ports_link_status(uint32_t port_mask)
592 {
593 #define CHECK_INTERVAL 100 /* 100ms */
594 #define MAX_CHECK_TIME 90  /* 9s (90 * 100ms) in total */
595 	uint8_t count, all_ports_up, print_flag = 0;
596 	struct rte_eth_link link;
597 	uint16_t portid;
598 	int ret;
599 	char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
600 
601 	printf("\nChecking link status");
602 	fflush(stdout);
603 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
604 		if (force_quit)
605 			return;
606 		all_ports_up = 1;
607 		RTE_ETH_FOREACH_DEV(portid)
608 		{
609 			if (force_quit)
610 				return;
611 			if ((port_mask & (1 << portid)) == 0)
612 				continue;
613 			memset(&link, 0, sizeof(link));
614 			ret = rte_eth_link_get_nowait(portid, &link);
615 			if (ret < 0) {
616 				all_ports_up = 0;
617 				if (print_flag == 1)
618 					printf("Port %u link get failed: %s\n",
619 						portid, rte_strerror(-ret));
620 				continue;
621 			}
622 			/* Print link status if flag set */
623 			if (print_flag == 1) {
624 				rte_eth_link_to_str(link_status_text,
625 					sizeof(link_status_text), &link);
626 				printf("Port %d %s\n", portid,
627 				       link_status_text);
628 				continue;
629 			}
630 			/* Clear all_ports_up flag if any link down */
631 			if (link.link_status == ETH_LINK_DOWN) {
632 				all_ports_up = 0;
633 				break;
634 			}
635 		}
636 		/* After finally printing all link status, get out */
637 		if (print_flag == 1)
638 			break;
639 
640 		if (all_ports_up == 0) {
641 			printf(".");
642 			fflush(stdout);
643 			rte_delay_ms(CHECK_INTERVAL);
644 		}
645 
646 		/* Set the print_flag if all ports up or timeout */
647 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
648 			print_flag = 1;
649 			printf("Done\n");
650 		}
651 	}
652 }
653 
654 static void
655 signal_handler(int signum)
656 {
657 	if (signum == SIGINT || signum == SIGTERM) {
658 		printf("\n\nSignal %d received, preparing to exit...\n",
659 		       signum);
660 		force_quit = true;
661 	}
662 }
663 
664 static void
665 print_stats(void)
666 {
667 	const char topLeft[] = {27, '[', '1', ';', '1', 'H', '\0'};
668 	const char clr[] = {27, '[', '2', 'J', '\0'};
669 	struct rte_graph_cluster_stats_param s_param;
670 	struct rte_graph_cluster_stats *stats;
671 	const char *pattern = "worker_*";
672 
673 	/* Prepare stats object */
674 	memset(&s_param, 0, sizeof(s_param));
675 	s_param.f = stdout;
676 	s_param.socket_id = SOCKET_ID_ANY;
677 	s_param.graph_patterns = &pattern;
678 	s_param.nb_graph_patterns = 1;
679 
680 	stats = rte_graph_cluster_stats_create(&s_param);
681 	if (stats == NULL)
682 		rte_exit(EXIT_FAILURE, "Unable to create stats object\n");
683 
684 	while (!force_quit) {
685 		/* Clear screen and move to top left */
686 		printf("%s%s", clr, topLeft);
687 		rte_graph_cluster_stats_get(stats, 0);
688 		rte_delay_ms(1E3);
689 	}
690 
691 	rte_graph_cluster_stats_destroy(stats);
692 }
693 
694 /* Main processing loop */
695 static int
696 graph_main_loop(void *conf)
697 {
698 	struct lcore_conf *qconf;
699 	struct rte_graph *graph;
700 	uint32_t lcore_id;
701 
702 	RTE_SET_USED(conf);
703 
704 	lcore_id = rte_lcore_id();
705 	qconf = &lcore_conf[lcore_id];
706 	graph = qconf->graph;
707 
708 	if (!graph) {
709 		RTE_LOG(INFO, L3FWD_GRAPH, "Lcore %u has nothing to do\n",
710 			lcore_id);
711 		return 0;
712 	}
713 
714 	RTE_LOG(INFO, L3FWD_GRAPH,
715 		"Entering main loop on lcore %u, graph %s(%p)\n", lcore_id,
716 		qconf->name, graph);
717 
718 	while (likely(!force_quit))
719 		rte_graph_walk(graph);
720 
721 	return 0;
722 }
723 
724 int
725 main(int argc, char **argv)
726 {
727 	/* Rewrite data of src and dst ether addr */
728 	uint8_t rewrite_data[2 * sizeof(struct rte_ether_addr)];
729 	static const char * const default_patterns[] = {
730 		"ip4*",
731 		"ethdev_tx-*",
732 		"pkt_drop",
733 	};
734 	uint8_t nb_rx_queue, queue, socketid;
735 	struct rte_graph_param graph_conf;
736 	struct rte_eth_dev_info dev_info;
737 	uint32_t nb_ports, nb_conf = 0;
738 	uint32_t n_tx_queue, nb_lcores;
739 	struct rte_eth_txconf *txconf;
740 	uint16_t queueid, portid, i;
741 	const char **node_patterns;
742 	struct lcore_conf *qconf;
743 	uint16_t nb_graphs = 0;
744 	uint16_t nb_patterns;
745 	uint8_t rewrite_len;
746 	uint32_t lcore_id;
747 	int ret;
748 
749 	/* Init EAL */
750 	ret = rte_eal_init(argc, argv);
751 	if (ret < 0)
752 		rte_exit(EXIT_FAILURE, "Invalid EAL parameters\n");
753 	argc -= ret;
754 	argv += ret;
755 
756 	force_quit = false;
757 	signal(SIGINT, signal_handler);
758 	signal(SIGTERM, signal_handler);
759 
760 	/* Pre-init dst MACs for all ports to 02:00:00:00:00:xx */
761 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
762 		dest_eth_addr[portid] =
763 			RTE_ETHER_LOCAL_ADMIN_ADDR + ((uint64_t)portid << 40);
764 		*(uint64_t *)(val_eth + portid) = dest_eth_addr[portid];
765 	}
766 
767 	/* Parse application arguments (after the EAL ones) */
768 	ret = parse_args(argc, argv);
769 	if (ret < 0)
770 		rte_exit(EXIT_FAILURE, "Invalid L3FWD_GRAPH parameters\n");
771 
772 	if (check_lcore_params() < 0)
773 		rte_exit(EXIT_FAILURE, "check_lcore_params() failed\n");
774 
775 	ret = init_lcore_rx_queues();
776 	if (ret < 0)
777 		rte_exit(EXIT_FAILURE, "init_lcore_rx_queues() failed\n");
778 
779 	if (check_port_config() < 0)
780 		rte_exit(EXIT_FAILURE, "check_port_config() failed\n");
781 
782 	nb_ports = rte_eth_dev_count_avail();
783 	nb_lcores = rte_lcore_count();
784 
785 	/* Initialize all ports */
786 	RTE_ETH_FOREACH_DEV(portid)
787 	{
788 		struct rte_eth_conf local_port_conf = port_conf;
789 
790 		/* Skip ports that are not enabled */
791 		if ((enabled_port_mask & (1 << portid)) == 0) {
792 			printf("\nSkipping disabled port %d\n", portid);
793 			continue;
794 		}
795 
796 		/* Init port */
797 		printf("Initializing port %d ... ", portid);
798 		fflush(stdout);
799 
800 		nb_rx_queue = get_port_n_rx_queues(portid);
801 		n_tx_queue = nb_lcores;
802 		if (n_tx_queue > MAX_TX_QUEUE_PER_PORT)
803 			n_tx_queue = MAX_TX_QUEUE_PER_PORT;
804 		printf("Creating queues: nb_rxq=%d nb_txq=%u... ",
805 		       nb_rx_queue, n_tx_queue);
806 
807 		rte_eth_dev_info_get(portid, &dev_info);
808 		if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
809 			local_port_conf.txmode.offloads |=
810 				DEV_TX_OFFLOAD_MBUF_FAST_FREE;
811 
812 		local_port_conf.rx_adv_conf.rss_conf.rss_hf &=
813 			dev_info.flow_type_rss_offloads;
814 		if (local_port_conf.rx_adv_conf.rss_conf.rss_hf !=
815 		    port_conf.rx_adv_conf.rss_conf.rss_hf) {
816 			printf("Port %u modified RSS hash function based on "
817 			       "hardware support,"
818 			       "requested:%#" PRIx64 " configured:%#" PRIx64
819 			       "\n",
820 			       portid, port_conf.rx_adv_conf.rss_conf.rss_hf,
821 			       local_port_conf.rx_adv_conf.rss_conf.rss_hf);
822 		}
823 
824 		ret = rte_eth_dev_configure(portid, nb_rx_queue,
825 					    n_tx_queue, &local_port_conf);
826 		if (ret < 0)
827 			rte_exit(EXIT_FAILURE,
828 				 "Cannot configure device: err=%d, port=%d\n",
829 				 ret, portid);
830 
831 		ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
832 						       &nb_txd);
833 		if (ret < 0)
834 			rte_exit(EXIT_FAILURE,
835 				 "Cannot adjust number of descriptors: err=%d, "
836 				 "port=%d\n",
837 				 ret, portid);
838 
839 		rte_eth_macaddr_get(portid, &ports_eth_addr[portid]);
840 		print_ethaddr(" Address:", &ports_eth_addr[portid]);
841 		printf(", ");
842 		print_ethaddr(
843 			"Destination:",
844 			(const struct rte_ether_addr *)&dest_eth_addr[portid]);
845 		printf(", ");
846 
847 		/*
848 		 * prepare src MACs for each port.
849 		 */
850 		rte_ether_addr_copy(
851 			&ports_eth_addr[portid],
852 			(struct rte_ether_addr *)(val_eth + portid) + 1);
853 
854 		/* Init memory */
855 		if (!per_port_pool) {
856 			/* portid = 0; this is *not* signifying the first port,
857 			 * rather, it signifies that portid is ignored.
858 			 */
859 			ret = init_mem(0, NB_MBUF(nb_ports));
860 		} else {
861 			ret = init_mem(portid, NB_MBUF(1));
862 		}
863 		if (ret < 0)
864 			rte_exit(EXIT_FAILURE, "init_mem() failed\n");
865 
866 		/* Init one TX queue per couple (lcore,port) */
867 		queueid = 0;
868 		for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
869 			if (rte_lcore_is_enabled(lcore_id) == 0)
870 				continue;
871 
872 			qconf = &lcore_conf[lcore_id];
873 
874 			if (numa_on)
875 				socketid = (uint8_t)rte_lcore_to_socket_id(
876 					lcore_id);
877 			else
878 				socketid = 0;
879 
880 			printf("txq=%u,%d,%d ", lcore_id, queueid, socketid);
881 			fflush(stdout);
882 
883 			txconf = &dev_info.default_txconf;
884 			txconf->offloads = local_port_conf.txmode.offloads;
885 			ret = rte_eth_tx_queue_setup(portid, queueid, nb_txd,
886 						     socketid, txconf);
887 			if (ret < 0)
888 				rte_exit(EXIT_FAILURE,
889 					 "rte_eth_tx_queue_setup: err=%d, "
890 					 "port=%d\n",
891 					 ret, portid);
892 			queueid++;
893 		}
894 
895 		/* Setup ethdev node config */
896 		ethdev_conf[nb_conf].port_id = portid;
897 		ethdev_conf[nb_conf].num_rx_queues = nb_rx_queue;
898 		ethdev_conf[nb_conf].num_tx_queues = n_tx_queue;
899 		if (!per_port_pool)
900 			ethdev_conf[nb_conf].mp = pktmbuf_pool[0];
901 
902 		else
903 			ethdev_conf[nb_conf].mp = pktmbuf_pool[portid];
904 		ethdev_conf[nb_conf].mp_count = NB_SOCKETS;
905 
906 		nb_conf++;
907 		printf("\n");
908 	}
909 
910 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
911 		if (rte_lcore_is_enabled(lcore_id) == 0)
912 			continue;
913 		qconf = &lcore_conf[lcore_id];
914 		printf("\nInitializing rx queues on lcore %u ... ", lcore_id);
915 		fflush(stdout);
916 		/* Init RX queues */
917 		for (queue = 0; queue < qconf->n_rx_queue; ++queue) {
918 			struct rte_eth_rxconf rxq_conf;
919 
920 			portid = qconf->rx_queue_list[queue].port_id;
921 			queueid = qconf->rx_queue_list[queue].queue_id;
922 
923 			if (numa_on)
924 				socketid = (uint8_t)rte_lcore_to_socket_id(
925 					lcore_id);
926 			else
927 				socketid = 0;
928 
929 			printf("rxq=%d,%d,%d ", portid, queueid, socketid);
930 			fflush(stdout);
931 
932 			rte_eth_dev_info_get(portid, &dev_info);
933 			rxq_conf = dev_info.default_rxconf;
934 			rxq_conf.offloads = port_conf.rxmode.offloads;
935 			if (!per_port_pool)
936 				ret = rte_eth_rx_queue_setup(
937 					portid, queueid, nb_rxd, socketid,
938 					&rxq_conf, pktmbuf_pool[0][socketid]);
939 			else
940 				ret = rte_eth_rx_queue_setup(
941 					portid, queueid, nb_rxd, socketid,
942 					&rxq_conf,
943 					pktmbuf_pool[portid][socketid]);
944 			if (ret < 0)
945 				rte_exit(EXIT_FAILURE,
946 					 "rte_eth_rx_queue_setup: err=%d, "
947 					 "port=%d\n",
948 					 ret, portid);
949 
950 			/* Add this queue node to its graph */
951 			snprintf(qconf->rx_queue_list[queue].node_name,
952 				 RTE_NODE_NAMESIZE, "ethdev_rx-%u-%u", portid,
953 				 queueid);
954 		}
955 
956 		/* Alloc a graph to this lcore only if source exists  */
957 		if (qconf->n_rx_queue)
958 			nb_graphs++;
959 	}
960 
961 	printf("\n");
962 
963 	/* Ethdev node config, skip rx queue mapping */
964 	ret = rte_node_eth_config(ethdev_conf, nb_conf, nb_graphs);
965 	if (ret)
966 		rte_exit(EXIT_FAILURE, "rte_node_eth_config: err=%d\n", ret);
967 
968 	/* Start ports */
969 	RTE_ETH_FOREACH_DEV(portid)
970 	{
971 		if ((enabled_port_mask & (1 << portid)) == 0)
972 			continue;
973 
974 		/* Start device */
975 		ret = rte_eth_dev_start(portid);
976 		if (ret < 0)
977 			rte_exit(EXIT_FAILURE,
978 				 "rte_eth_dev_start: err=%d, port=%d\n", ret,
979 				 portid);
980 
981 		/*
982 		 * If enabled, put device in promiscuous mode.
983 		 * This allows IO forwarding mode to forward packets
984 		 * to itself through 2 cross-connected  ports of the
985 		 * target machine.
986 		 */
987 		if (promiscuous_on)
988 			rte_eth_promiscuous_enable(portid);
989 	}
990 
991 	printf("\n");
992 
993 	check_all_ports_link_status(enabled_port_mask);
994 
995 	/* Graph Initialization */
996 	nb_patterns = RTE_DIM(default_patterns);
997 	node_patterns = malloc((MAX_RX_QUEUE_PER_LCORE + nb_patterns) *
998 			       sizeof(*node_patterns));
999 	if (!node_patterns)
1000 		return -ENOMEM;
1001 	memcpy(node_patterns, default_patterns,
1002 	       nb_patterns * sizeof(*node_patterns));
1003 
1004 	memset(&graph_conf, 0, sizeof(graph_conf));
1005 	graph_conf.node_patterns = node_patterns;
1006 
1007 	for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id++) {
1008 		rte_graph_t graph_id;
1009 		rte_edge_t i;
1010 
1011 		if (rte_lcore_is_enabled(lcore_id) == 0)
1012 			continue;
1013 
1014 		qconf = &lcore_conf[lcore_id];
1015 
1016 		/* Skip graph creation if no source exists */
1017 		if (!qconf->n_rx_queue)
1018 			continue;
1019 
1020 		/* Add rx node patterns of this lcore */
1021 		for (i = 0; i < qconf->n_rx_queue; i++) {
1022 			graph_conf.node_patterns[nb_patterns + i] =
1023 				qconf->rx_queue_list[i].node_name;
1024 		}
1025 
1026 		graph_conf.nb_node_patterns = nb_patterns + i;
1027 		graph_conf.socket_id = rte_lcore_to_socket_id(lcore_id);
1028 
1029 		snprintf(qconf->name, sizeof(qconf->name), "worker_%u",
1030 			 lcore_id);
1031 
1032 		graph_id = rte_graph_create(qconf->name, &graph_conf);
1033 		if (graph_id == RTE_GRAPH_ID_INVALID)
1034 			rte_exit(EXIT_FAILURE,
1035 				 "rte_graph_create(): graph_id invalid"
1036 				 " for lcore %u\n", lcore_id);
1037 
1038 		qconf->graph_id = graph_id;
1039 		qconf->graph = rte_graph_lookup(qconf->name);
1040 		if (!qconf->graph)
1041 			rte_exit(EXIT_FAILURE,
1042 				 "rte_graph_lookup(): graph %s not found\n",
1043 				 qconf->name);
1044 	}
1045 
1046 	memset(&rewrite_data, 0, sizeof(rewrite_data));
1047 	rewrite_len = sizeof(rewrite_data);
1048 
1049 	/* Add route to ip4 graph infra */
1050 	for (i = 0; i < IPV4_L3FWD_LPM_NUM_ROUTES; i++) {
1051 		char route_str[INET6_ADDRSTRLEN * 4];
1052 		char abuf[INET6_ADDRSTRLEN];
1053 		struct in_addr in;
1054 		uint32_t dst_port;
1055 
1056 		/* Skip unused ports */
1057 		if ((1 << ipv4_l3fwd_lpm_route_array[i].if_out &
1058 		     enabled_port_mask) == 0)
1059 			continue;
1060 
1061 		dst_port = ipv4_l3fwd_lpm_route_array[i].if_out;
1062 
1063 		in.s_addr = htonl(ipv4_l3fwd_lpm_route_array[i].ip);
1064 		snprintf(route_str, sizeof(route_str), "%s / %d (%d)",
1065 			 inet_ntop(AF_INET, &in, abuf, sizeof(abuf)),
1066 			 ipv4_l3fwd_lpm_route_array[i].depth,
1067 			 ipv4_l3fwd_lpm_route_array[i].if_out);
1068 
1069 		/* Use route index 'i' as next hop id */
1070 		ret = rte_node_ip4_route_add(
1071 			ipv4_l3fwd_lpm_route_array[i].ip,
1072 			ipv4_l3fwd_lpm_route_array[i].depth, i,
1073 			RTE_NODE_IP4_LOOKUP_NEXT_REWRITE);
1074 
1075 		if (ret < 0)
1076 			rte_exit(EXIT_FAILURE,
1077 				 "Unable to add ip4 route %s to graph\n",
1078 				 route_str);
1079 
1080 		memcpy(rewrite_data, val_eth + dst_port, rewrite_len);
1081 
1082 		/* Add next hop rewrite data for id 'i' */
1083 		ret = rte_node_ip4_rewrite_add(i, rewrite_data,
1084 					       rewrite_len, dst_port);
1085 		if (ret < 0)
1086 			rte_exit(EXIT_FAILURE,
1087 				 "Unable to add next hop %u for "
1088 				 "route %s\n", i, route_str);
1089 
1090 		RTE_LOG(INFO, L3FWD_GRAPH, "Added route %s, next_hop %u\n",
1091 			route_str, i);
1092 	}
1093 
1094 	/* Launch per-lcore init on every worker lcore */
1095 	rte_eal_mp_remote_launch(graph_main_loop, NULL, SKIP_MAIN);
1096 
1097 	/* Accumulate and print stats on main until exit */
1098 	if (rte_graph_has_stats_feature())
1099 		print_stats();
1100 
1101 	/* Wait for worker cores to exit */
1102 	ret = 0;
1103 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
1104 		ret = rte_eal_wait_lcore(lcore_id);
1105 		/* Destroy graph */
1106 		if (ret < 0 || rte_graph_destroy(
1107 			rte_graph_from_name(lcore_conf[lcore_id].name))) {
1108 			ret = -1;
1109 			break;
1110 		}
1111 	}
1112 	free(node_patterns);
1113 
1114 	/* Stop ports */
1115 	RTE_ETH_FOREACH_DEV(portid) {
1116 		if ((enabled_port_mask & (1 << portid)) == 0)
1117 			continue;
1118 		printf("Closing port %d...", portid);
1119 		ret = rte_eth_dev_stop(portid);
1120 		if (ret != 0)
1121 			printf("Failed to stop port %u: %s\n",
1122 			       portid, rte_strerror(-ret));
1123 		rte_eth_dev_close(portid);
1124 		printf(" Done\n");
1125 	}
1126 	printf("Bye...\n");
1127 
1128 	return ret;
1129 }
1130