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