xref: /dpdk/examples/l2fwd/main.c (revision dc348f2e81a94dd3b8a32c2f882483227796905d)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdint.h>
9 #include <inttypes.h>
10 #include <sys/types.h>
11 #include <sys/queue.h>
12 #include <setjmp.h>
13 #include <stdarg.h>
14 #include <ctype.h>
15 #include <errno.h>
16 #include <getopt.h>
17 #include <signal.h>
18 #include <stdbool.h>
19 
20 #include <rte_common.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_ethdev.h>
37 #include <rte_mempool.h>
38 #include <rte_mbuf.h>
39 #include <rte_string_fns.h>
40 
41 static volatile bool force_quit;
42 
43 /* MAC updating enabled by default */
44 static int mac_updating = 1;
45 
46 /* Ports set in promiscuous mode off by default. */
47 static int promiscuous_on;
48 
49 #define RTE_LOGTYPE_L2FWD RTE_LOGTYPE_USER1
50 
51 #define MAX_PKT_BURST 32
52 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
53 #define MEMPOOL_CACHE_SIZE 256
54 
55 /*
56  * Configurable number of RX/TX ring descriptors
57  */
58 #define RX_DESC_DEFAULT 1024
59 #define TX_DESC_DEFAULT 1024
60 static uint16_t nb_rxd = RX_DESC_DEFAULT;
61 static uint16_t nb_txd = TX_DESC_DEFAULT;
62 
63 /* ethernet addresses of ports */
64 static struct rte_ether_addr l2fwd_ports_eth_addr[RTE_MAX_ETHPORTS];
65 
66 /* mask of enabled ports */
67 static uint32_t l2fwd_enabled_port_mask = 0;
68 
69 /* list of enabled ports */
70 static uint32_t l2fwd_dst_ports[RTE_MAX_ETHPORTS];
71 
72 struct port_pair_params {
73 #define NUM_PORTS	2
74 	uint16_t port[NUM_PORTS];
75 } __rte_cache_aligned;
76 
77 static struct port_pair_params port_pair_params_array[RTE_MAX_ETHPORTS / 2];
78 static struct port_pair_params *port_pair_params;
79 static uint16_t nb_port_pair_params;
80 
81 static unsigned int l2fwd_rx_queue_per_lcore = 1;
82 
83 #define MAX_RX_QUEUE_PER_LCORE 16
84 #define MAX_TX_QUEUE_PER_PORT 16
85 /* List of queues to be polled for a given lcore. 8< */
86 struct lcore_queue_conf {
87 	unsigned n_rx_port;
88 	unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
89 } __rte_cache_aligned;
90 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
91 /* >8 End of list of queues to be polled for a given lcore. */
92 
93 static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
94 
95 static struct rte_eth_conf port_conf = {
96 	.txmode = {
97 		.mq_mode = RTE_ETH_MQ_TX_NONE,
98 	},
99 };
100 
101 struct rte_mempool * l2fwd_pktmbuf_pool = NULL;
102 
103 /* Per-port statistics struct */
104 struct l2fwd_port_statistics {
105 	uint64_t tx;
106 	uint64_t rx;
107 	uint64_t dropped;
108 } __rte_cache_aligned;
109 struct l2fwd_port_statistics port_statistics[RTE_MAX_ETHPORTS];
110 
111 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
112 /* A tsc-based timer responsible for triggering statistics printout */
113 static uint64_t timer_period = 10; /* default period is 10 seconds */
114 
115 /* Print out statistics on packets dropped */
116 static void
117 print_stats(void)
118 {
119 	uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
120 	unsigned portid;
121 
122 	total_packets_dropped = 0;
123 	total_packets_tx = 0;
124 	total_packets_rx = 0;
125 
126 	const char clr[] = { 27, '[', '2', 'J', '\0' };
127 	const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' };
128 
129 		/* Clear screen and move to top left */
130 	printf("%s%s", clr, topLeft);
131 
132 	printf("\nPort statistics ====================================");
133 
134 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
135 		/* skip disabled ports */
136 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
137 			continue;
138 		printf("\nStatistics for port %u ------------------------------"
139 			   "\nPackets sent: %24"PRIu64
140 			   "\nPackets received: %20"PRIu64
141 			   "\nPackets dropped: %21"PRIu64,
142 			   portid,
143 			   port_statistics[portid].tx,
144 			   port_statistics[portid].rx,
145 			   port_statistics[portid].dropped);
146 
147 		total_packets_dropped += port_statistics[portid].dropped;
148 		total_packets_tx += port_statistics[portid].tx;
149 		total_packets_rx += port_statistics[portid].rx;
150 	}
151 	printf("\nAggregate statistics ==============================="
152 		   "\nTotal packets sent: %18"PRIu64
153 		   "\nTotal packets received: %14"PRIu64
154 		   "\nTotal packets dropped: %15"PRIu64,
155 		   total_packets_tx,
156 		   total_packets_rx,
157 		   total_packets_dropped);
158 	printf("\n====================================================\n");
159 
160 	fflush(stdout);
161 }
162 
163 static void
164 l2fwd_mac_updating(struct rte_mbuf *m, unsigned dest_portid)
165 {
166 	struct rte_ether_hdr *eth;
167 	void *tmp;
168 
169 	eth = rte_pktmbuf_mtod(m, struct rte_ether_hdr *);
170 
171 	/* 02:00:00:00:00:xx */
172 	tmp = &eth->dst_addr.addr_bytes[0];
173 	*((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
174 
175 	/* src addr */
176 	rte_ether_addr_copy(&l2fwd_ports_eth_addr[dest_portid], &eth->src_addr);
177 }
178 
179 /* Simple forward. 8< */
180 static void
181 l2fwd_simple_forward(struct rte_mbuf *m, unsigned portid)
182 {
183 	unsigned dst_port;
184 	int sent;
185 	struct rte_eth_dev_tx_buffer *buffer;
186 
187 	dst_port = l2fwd_dst_ports[portid];
188 
189 	if (mac_updating)
190 		l2fwd_mac_updating(m, dst_port);
191 
192 	buffer = tx_buffer[dst_port];
193 	sent = rte_eth_tx_buffer(dst_port, 0, buffer, m);
194 	if (sent)
195 		port_statistics[dst_port].tx += sent;
196 }
197 /* >8 End of simple forward. */
198 
199 /* main processing loop */
200 static void
201 l2fwd_main_loop(void)
202 {
203 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
204 	struct rte_mbuf *m;
205 	int sent;
206 	unsigned lcore_id;
207 	uint64_t prev_tsc, diff_tsc, cur_tsc, timer_tsc;
208 	unsigned i, j, portid, nb_rx;
209 	struct lcore_queue_conf *qconf;
210 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S *
211 			BURST_TX_DRAIN_US;
212 	struct rte_eth_dev_tx_buffer *buffer;
213 
214 	prev_tsc = 0;
215 	timer_tsc = 0;
216 
217 	lcore_id = rte_lcore_id();
218 	qconf = &lcore_queue_conf[lcore_id];
219 
220 	if (qconf->n_rx_port == 0) {
221 		RTE_LOG(INFO, L2FWD, "lcore %u has nothing to do\n", lcore_id);
222 		return;
223 	}
224 
225 	RTE_LOG(INFO, L2FWD, "entering main loop on lcore %u\n", lcore_id);
226 
227 	for (i = 0; i < qconf->n_rx_port; i++) {
228 
229 		portid = qconf->rx_port_list[i];
230 		RTE_LOG(INFO, L2FWD, " -- lcoreid=%u portid=%u\n", lcore_id,
231 			portid);
232 
233 	}
234 
235 	while (!force_quit) {
236 
237 		/* Drains TX queue in its main loop. 8< */
238 		cur_tsc = rte_rdtsc();
239 
240 		/*
241 		 * TX burst queue drain
242 		 */
243 		diff_tsc = cur_tsc - prev_tsc;
244 		if (unlikely(diff_tsc > drain_tsc)) {
245 
246 			for (i = 0; i < qconf->n_rx_port; i++) {
247 
248 				portid = l2fwd_dst_ports[qconf->rx_port_list[i]];
249 				buffer = tx_buffer[portid];
250 
251 				sent = rte_eth_tx_buffer_flush(portid, 0, buffer);
252 				if (sent)
253 					port_statistics[portid].tx += sent;
254 
255 			}
256 
257 			/* if timer is enabled */
258 			if (timer_period > 0) {
259 
260 				/* advance the timer */
261 				timer_tsc += diff_tsc;
262 
263 				/* if timer has reached its timeout */
264 				if (unlikely(timer_tsc >= timer_period)) {
265 
266 					/* do this only on main core */
267 					if (lcore_id == rte_get_main_lcore()) {
268 						print_stats();
269 						/* reset the timer */
270 						timer_tsc = 0;
271 					}
272 				}
273 			}
274 
275 			prev_tsc = cur_tsc;
276 		}
277 		/* >8 End of draining TX queue. */
278 
279 		/* Read packet from RX queues. 8< */
280 		for (i = 0; i < qconf->n_rx_port; i++) {
281 
282 			portid = qconf->rx_port_list[i];
283 			nb_rx = rte_eth_rx_burst(portid, 0,
284 						 pkts_burst, MAX_PKT_BURST);
285 
286 			port_statistics[portid].rx += nb_rx;
287 
288 			for (j = 0; j < nb_rx; j++) {
289 				m = pkts_burst[j];
290 				rte_prefetch0(rte_pktmbuf_mtod(m, void *));
291 				l2fwd_simple_forward(m, portid);
292 			}
293 		}
294 		/* >8 End of read packet from RX queues. */
295 	}
296 }
297 
298 static int
299 l2fwd_launch_one_lcore(__rte_unused void *dummy)
300 {
301 	l2fwd_main_loop();
302 	return 0;
303 }
304 
305 /* display usage */
306 static void
307 l2fwd_usage(const char *prgname)
308 {
309 	printf("%s [EAL options] -- -p PORTMASK [-P] [-q NQ]\n"
310 	       "  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
311 	       "  -P : Enable promiscuous mode\n"
312 	       "  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
313 	       "  -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n"
314 	       "  --no-mac-updating: Disable MAC addresses updating (enabled by default)\n"
315 	       "      When enabled:\n"
316 	       "       - The source MAC address is replaced by the TX port MAC address\n"
317 	       "       - The destination MAC address is replaced by 02:00:00:00:00:TX_PORT_ID\n"
318 	       "  --portmap: Configure forwarding port pair mapping\n"
319 	       "	      Default: alternate port pairs\n\n",
320 	       prgname);
321 }
322 
323 static int
324 l2fwd_parse_portmask(const char *portmask)
325 {
326 	char *end = NULL;
327 	unsigned long pm;
328 
329 	/* parse hexadecimal string */
330 	pm = strtoul(portmask, &end, 16);
331 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
332 		return 0;
333 
334 	return pm;
335 }
336 
337 static int
338 l2fwd_parse_port_pair_config(const char *q_arg)
339 {
340 	enum fieldnames {
341 		FLD_PORT1 = 0,
342 		FLD_PORT2,
343 		_NUM_FLD
344 	};
345 	unsigned long int_fld[_NUM_FLD];
346 	const char *p, *p0 = q_arg;
347 	char *str_fld[_NUM_FLD];
348 	unsigned int size;
349 	char s[256];
350 	char *end;
351 	int i;
352 
353 	nb_port_pair_params = 0;
354 
355 	while ((p = strchr(p0, '(')) != NULL) {
356 		++p;
357 		p0 = strchr(p, ')');
358 		if (p0 == NULL)
359 			return -1;
360 
361 		size = p0 - p;
362 		if (size >= sizeof(s))
363 			return -1;
364 
365 		memcpy(s, p, size);
366 		s[size] = '\0';
367 		if (rte_strsplit(s, sizeof(s), str_fld,
368 				 _NUM_FLD, ',') != _NUM_FLD)
369 			return -1;
370 		for (i = 0; i < _NUM_FLD; i++) {
371 			errno = 0;
372 			int_fld[i] = strtoul(str_fld[i], &end, 0);
373 			if (errno != 0 || end == str_fld[i] ||
374 			    int_fld[i] >= RTE_MAX_ETHPORTS)
375 				return -1;
376 		}
377 		if (nb_port_pair_params >= RTE_MAX_ETHPORTS/2) {
378 			printf("exceeded max number of port pair params: %hu\n",
379 				nb_port_pair_params);
380 			return -1;
381 		}
382 		port_pair_params_array[nb_port_pair_params].port[0] =
383 				(uint16_t)int_fld[FLD_PORT1];
384 		port_pair_params_array[nb_port_pair_params].port[1] =
385 				(uint16_t)int_fld[FLD_PORT2];
386 		++nb_port_pair_params;
387 	}
388 	port_pair_params = port_pair_params_array;
389 	return 0;
390 }
391 
392 static unsigned int
393 l2fwd_parse_nqueue(const char *q_arg)
394 {
395 	char *end = NULL;
396 	unsigned long n;
397 
398 	/* parse hexadecimal string */
399 	n = strtoul(q_arg, &end, 10);
400 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
401 		return 0;
402 	if (n == 0)
403 		return 0;
404 	if (n >= MAX_RX_QUEUE_PER_LCORE)
405 		return 0;
406 
407 	return n;
408 }
409 
410 static int
411 l2fwd_parse_timer_period(const char *q_arg)
412 {
413 	char *end = NULL;
414 	int n;
415 
416 	/* parse number string */
417 	n = strtol(q_arg, &end, 10);
418 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
419 		return -1;
420 	if (n >= MAX_TIMER_PERIOD)
421 		return -1;
422 
423 	return n;
424 }
425 
426 static const char short_options[] =
427 	"p:"  /* portmask */
428 	"P"   /* promiscuous */
429 	"q:"  /* number of queues */
430 	"T:"  /* timer period */
431 	;
432 
433 #define CMD_LINE_OPT_NO_MAC_UPDATING "no-mac-updating"
434 #define CMD_LINE_OPT_PORTMAP_CONFIG "portmap"
435 
436 enum {
437 	/* long options mapped to a short option */
438 
439 	/* first long only option value must be >= 256, so that we won't
440 	 * conflict with short options */
441 	CMD_LINE_OPT_NO_MAC_UPDATING_NUM = 256,
442 	CMD_LINE_OPT_PORTMAP_NUM,
443 };
444 
445 static const struct option lgopts[] = {
446 	{ CMD_LINE_OPT_NO_MAC_UPDATING, no_argument, 0,
447 		CMD_LINE_OPT_NO_MAC_UPDATING_NUM},
448 	{ CMD_LINE_OPT_PORTMAP_CONFIG, 1, 0, CMD_LINE_OPT_PORTMAP_NUM},
449 	{NULL, 0, 0, 0}
450 };
451 
452 /* Parse the argument given in the command line of the application */
453 static int
454 l2fwd_parse_args(int argc, char **argv)
455 {
456 	int opt, ret, timer_secs;
457 	char **argvopt;
458 	int option_index;
459 	char *prgname = argv[0];
460 
461 	argvopt = argv;
462 	port_pair_params = NULL;
463 
464 	while ((opt = getopt_long(argc, argvopt, short_options,
465 				  lgopts, &option_index)) != EOF) {
466 
467 		switch (opt) {
468 		/* portmask */
469 		case 'p':
470 			l2fwd_enabled_port_mask = l2fwd_parse_portmask(optarg);
471 			if (l2fwd_enabled_port_mask == 0) {
472 				printf("invalid portmask\n");
473 				l2fwd_usage(prgname);
474 				return -1;
475 			}
476 			break;
477 		case 'P':
478 			promiscuous_on = 1;
479 			break;
480 
481 		/* nqueue */
482 		case 'q':
483 			l2fwd_rx_queue_per_lcore = l2fwd_parse_nqueue(optarg);
484 			if (l2fwd_rx_queue_per_lcore == 0) {
485 				printf("invalid queue number\n");
486 				l2fwd_usage(prgname);
487 				return -1;
488 			}
489 			break;
490 
491 		/* timer period */
492 		case 'T':
493 			timer_secs = l2fwd_parse_timer_period(optarg);
494 			if (timer_secs < 0) {
495 				printf("invalid timer period\n");
496 				l2fwd_usage(prgname);
497 				return -1;
498 			}
499 			timer_period = timer_secs;
500 			break;
501 
502 		/* long options */
503 		case CMD_LINE_OPT_PORTMAP_NUM:
504 			ret = l2fwd_parse_port_pair_config(optarg);
505 			if (ret) {
506 				fprintf(stderr, "Invalid config\n");
507 				l2fwd_usage(prgname);
508 				return -1;
509 			}
510 			break;
511 
512 		case CMD_LINE_OPT_NO_MAC_UPDATING_NUM:
513 			mac_updating = 0;
514 			break;
515 
516 		default:
517 			l2fwd_usage(prgname);
518 			return -1;
519 		}
520 	}
521 
522 	if (optind >= 0)
523 		argv[optind-1] = prgname;
524 
525 	ret = optind-1;
526 	optind = 1; /* reset getopt lib */
527 	return ret;
528 }
529 
530 /*
531  * Check port pair config with enabled port mask,
532  * and for valid port pair combinations.
533  */
534 static int
535 check_port_pair_config(void)
536 {
537 	uint32_t port_pair_config_mask = 0;
538 	uint32_t port_pair_mask = 0;
539 	uint16_t index, i, portid;
540 
541 	for (index = 0; index < nb_port_pair_params; index++) {
542 		port_pair_mask = 0;
543 
544 		for (i = 0; i < NUM_PORTS; i++)  {
545 			portid = port_pair_params[index].port[i];
546 			if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) {
547 				printf("port %u is not enabled in port mask\n",
548 				       portid);
549 				return -1;
550 			}
551 			if (!rte_eth_dev_is_valid_port(portid)) {
552 				printf("port %u is not present on the board\n",
553 				       portid);
554 				return -1;
555 			}
556 
557 			port_pair_mask |= 1 << portid;
558 		}
559 
560 		if (port_pair_config_mask & port_pair_mask) {
561 			printf("port %u is used in other port pairs\n", portid);
562 			return -1;
563 		}
564 		port_pair_config_mask |= port_pair_mask;
565 	}
566 
567 	l2fwd_enabled_port_mask &= port_pair_config_mask;
568 
569 	return 0;
570 }
571 
572 /* Check the link status of all ports in up to 9s, and print them finally */
573 static void
574 check_all_ports_link_status(uint32_t port_mask)
575 {
576 #define CHECK_INTERVAL 100 /* 100ms */
577 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
578 	uint16_t portid;
579 	uint8_t count, all_ports_up, print_flag = 0;
580 	struct rte_eth_link link;
581 	int ret;
582 	char link_status_text[RTE_ETH_LINK_MAX_STR_LEN];
583 
584 	printf("\nChecking link status");
585 	fflush(stdout);
586 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
587 		if (force_quit)
588 			return;
589 		all_ports_up = 1;
590 		RTE_ETH_FOREACH_DEV(portid) {
591 			if (force_quit)
592 				return;
593 			if ((port_mask & (1 << portid)) == 0)
594 				continue;
595 			memset(&link, 0, sizeof(link));
596 			ret = rte_eth_link_get_nowait(portid, &link);
597 			if (ret < 0) {
598 				all_ports_up = 0;
599 				if (print_flag == 1)
600 					printf("Port %u link get failed: %s\n",
601 						portid, rte_strerror(-ret));
602 				continue;
603 			}
604 			/* print link status if flag set */
605 			if (print_flag == 1) {
606 				rte_eth_link_to_str(link_status_text,
607 					sizeof(link_status_text), &link);
608 				printf("Port %d %s\n", portid,
609 				       link_status_text);
610 				continue;
611 			}
612 			/* clear all_ports_up flag if any link down */
613 			if (link.link_status == RTE_ETH_LINK_DOWN) {
614 				all_ports_up = 0;
615 				break;
616 			}
617 		}
618 		/* after finally printing all link status, get out */
619 		if (print_flag == 1)
620 			break;
621 
622 		if (all_ports_up == 0) {
623 			printf(".");
624 			fflush(stdout);
625 			rte_delay_ms(CHECK_INTERVAL);
626 		}
627 
628 		/* set the print_flag if all ports up or timeout */
629 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
630 			print_flag = 1;
631 			printf("done\n");
632 		}
633 	}
634 }
635 
636 static void
637 signal_handler(int signum)
638 {
639 	if (signum == SIGINT || signum == SIGTERM) {
640 		printf("\n\nSignal %d received, preparing to exit...\n",
641 				signum);
642 		force_quit = true;
643 	}
644 }
645 
646 int
647 main(int argc, char **argv)
648 {
649 	struct lcore_queue_conf *qconf;
650 	int ret;
651 	uint16_t nb_ports;
652 	uint16_t nb_ports_available = 0;
653 	uint16_t portid, last_port;
654 	unsigned lcore_id, rx_lcore_id;
655 	unsigned nb_ports_in_mask = 0;
656 	unsigned int nb_lcores = 0;
657 	unsigned int nb_mbufs;
658 
659 	/* Init EAL. 8< */
660 	ret = rte_eal_init(argc, argv);
661 	if (ret < 0)
662 		rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
663 	argc -= ret;
664 	argv += ret;
665 
666 	force_quit = false;
667 	signal(SIGINT, signal_handler);
668 	signal(SIGTERM, signal_handler);
669 
670 	/* parse application arguments (after the EAL ones) */
671 	ret = l2fwd_parse_args(argc, argv);
672 	if (ret < 0)
673 		rte_exit(EXIT_FAILURE, "Invalid L2FWD arguments\n");
674 	/* >8 End of init EAL. */
675 
676 	printf("MAC updating %s\n", mac_updating ? "enabled" : "disabled");
677 
678 	/* convert to number of cycles */
679 	timer_period *= rte_get_timer_hz();
680 
681 	nb_ports = rte_eth_dev_count_avail();
682 	if (nb_ports == 0)
683 		rte_exit(EXIT_FAILURE, "No Ethernet ports - bye\n");
684 
685 	if (port_pair_params != NULL) {
686 		if (check_port_pair_config() < 0)
687 			rte_exit(EXIT_FAILURE, "Invalid port pair config\n");
688 	}
689 
690 	/* check port mask to possible port mask */
691 	if (l2fwd_enabled_port_mask & ~((1 << nb_ports) - 1))
692 		rte_exit(EXIT_FAILURE, "Invalid portmask; possible (0x%x)\n",
693 			(1 << nb_ports) - 1);
694 
695 	/* Initialization of the driver. 8< */
696 
697 	/* reset l2fwd_dst_ports */
698 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++)
699 		l2fwd_dst_ports[portid] = 0;
700 	last_port = 0;
701 
702 	/* populate destination port details */
703 	if (port_pair_params != NULL) {
704 		uint16_t idx, p;
705 
706 		for (idx = 0; idx < (nb_port_pair_params << 1); idx++) {
707 			p = idx & 1;
708 			portid = port_pair_params[idx >> 1].port[p];
709 			l2fwd_dst_ports[portid] =
710 				port_pair_params[idx >> 1].port[p ^ 1];
711 		}
712 	} else {
713 		RTE_ETH_FOREACH_DEV(portid) {
714 			/* skip ports that are not enabled */
715 			if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
716 				continue;
717 
718 			if (nb_ports_in_mask % 2) {
719 				l2fwd_dst_ports[portid] = last_port;
720 				l2fwd_dst_ports[last_port] = portid;
721 			} else {
722 				last_port = portid;
723 			}
724 
725 			nb_ports_in_mask++;
726 		}
727 		if (nb_ports_in_mask % 2) {
728 			printf("Notice: odd number of ports in portmask.\n");
729 			l2fwd_dst_ports[last_port] = last_port;
730 		}
731 	}
732 	/* >8 End of initialization of the driver. */
733 
734 	rx_lcore_id = 0;
735 	qconf = NULL;
736 
737 	/* Initialize the port/queue configuration of each logical core */
738 	RTE_ETH_FOREACH_DEV(portid) {
739 		/* skip ports that are not enabled */
740 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
741 			continue;
742 
743 		/* get the lcore_id for this port */
744 		while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
745 		       lcore_queue_conf[rx_lcore_id].n_rx_port ==
746 		       l2fwd_rx_queue_per_lcore) {
747 			rx_lcore_id++;
748 			if (rx_lcore_id >= RTE_MAX_LCORE)
749 				rte_exit(EXIT_FAILURE, "Not enough cores\n");
750 		}
751 
752 		if (qconf != &lcore_queue_conf[rx_lcore_id]) {
753 			/* Assigned a new logical core in the loop above. */
754 			qconf = &lcore_queue_conf[rx_lcore_id];
755 			nb_lcores++;
756 		}
757 
758 		qconf->rx_port_list[qconf->n_rx_port] = portid;
759 		qconf->n_rx_port++;
760 		printf("Lcore %u: RX port %u TX port %u\n", rx_lcore_id,
761 		       portid, l2fwd_dst_ports[portid]);
762 	}
763 
764 	nb_mbufs = RTE_MAX(nb_ports * (nb_rxd + nb_txd + MAX_PKT_BURST +
765 		nb_lcores * MEMPOOL_CACHE_SIZE), 8192U);
766 
767 	/* Create the mbuf pool. 8< */
768 	l2fwd_pktmbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", nb_mbufs,
769 		MEMPOOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
770 		rte_socket_id());
771 	if (l2fwd_pktmbuf_pool == NULL)
772 		rte_exit(EXIT_FAILURE, "Cannot init mbuf pool\n");
773 	/* >8 End of create the mbuf pool. */
774 
775 	/* Initialise each port */
776 	RTE_ETH_FOREACH_DEV(portid) {
777 		struct rte_eth_rxconf rxq_conf;
778 		struct rte_eth_txconf txq_conf;
779 		struct rte_eth_conf local_port_conf = port_conf;
780 		struct rte_eth_dev_info dev_info;
781 
782 		/* skip ports that are not enabled */
783 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0) {
784 			printf("Skipping disabled port %u\n", portid);
785 			continue;
786 		}
787 		nb_ports_available++;
788 
789 		/* init port */
790 		printf("Initializing port %u... ", portid);
791 		fflush(stdout);
792 
793 		ret = rte_eth_dev_info_get(portid, &dev_info);
794 		if (ret != 0)
795 			rte_exit(EXIT_FAILURE,
796 				"Error during getting device (port %u) info: %s\n",
797 				portid, strerror(-ret));
798 
799 		if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
800 			local_port_conf.txmode.offloads |=
801 				RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
802 		/* Configure the number of queues for a port. */
803 		ret = rte_eth_dev_configure(portid, 1, 1, &local_port_conf);
804 		if (ret < 0)
805 			rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n",
806 				  ret, portid);
807 		/* >8 End of configuration of the number of queues for a port. */
808 
809 		ret = rte_eth_dev_adjust_nb_rx_tx_desc(portid, &nb_rxd,
810 						       &nb_txd);
811 		if (ret < 0)
812 			rte_exit(EXIT_FAILURE,
813 				 "Cannot adjust number of descriptors: err=%d, port=%u\n",
814 				 ret, portid);
815 
816 		ret = rte_eth_macaddr_get(portid,
817 					  &l2fwd_ports_eth_addr[portid]);
818 		if (ret < 0)
819 			rte_exit(EXIT_FAILURE,
820 				 "Cannot get MAC address: err=%d, port=%u\n",
821 				 ret, portid);
822 
823 		/* init one RX queue */
824 		fflush(stdout);
825 		rxq_conf = dev_info.default_rxconf;
826 		rxq_conf.offloads = local_port_conf.rxmode.offloads;
827 		/* RX queue setup. 8< */
828 		ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
829 					     rte_eth_dev_socket_id(portid),
830 					     &rxq_conf,
831 					     l2fwd_pktmbuf_pool);
832 		if (ret < 0)
833 			rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup:err=%d, port=%u\n",
834 				  ret, portid);
835 		/* >8 End of RX queue setup. */
836 
837 		/* Init one TX queue on each port. 8< */
838 		fflush(stdout);
839 		txq_conf = dev_info.default_txconf;
840 		txq_conf.offloads = local_port_conf.txmode.offloads;
841 		ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
842 				rte_eth_dev_socket_id(portid),
843 				&txq_conf);
844 		if (ret < 0)
845 			rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup:err=%d, port=%u\n",
846 				ret, portid);
847 		/* >8 End of init one TX queue on each port. */
848 
849 		/* Initialize TX buffers */
850 		tx_buffer[portid] = rte_zmalloc_socket("tx_buffer",
851 				RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST), 0,
852 				rte_eth_dev_socket_id(portid));
853 		if (tx_buffer[portid] == NULL)
854 			rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
855 					portid);
856 
857 		rte_eth_tx_buffer_init(tx_buffer[portid], MAX_PKT_BURST);
858 
859 		ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[portid],
860 				rte_eth_tx_buffer_count_callback,
861 				&port_statistics[portid].dropped);
862 		if (ret < 0)
863 			rte_exit(EXIT_FAILURE,
864 			"Cannot set error callback for tx buffer on port %u\n",
865 				 portid);
866 
867 		ret = rte_eth_dev_set_ptypes(portid, RTE_PTYPE_UNKNOWN, NULL,
868 					     0);
869 		if (ret < 0)
870 			printf("Port %u, Failed to disable Ptype parsing\n",
871 					portid);
872 		/* Start device */
873 		ret = rte_eth_dev_start(portid);
874 		if (ret < 0)
875 			rte_exit(EXIT_FAILURE, "rte_eth_dev_start:err=%d, port=%u\n",
876 				  ret, portid);
877 
878 		printf("done: \n");
879 		if (promiscuous_on) {
880 			ret = rte_eth_promiscuous_enable(portid);
881 			if (ret != 0)
882 				rte_exit(EXIT_FAILURE,
883 					"rte_eth_promiscuous_enable:err=%s, port=%u\n",
884 					rte_strerror(-ret), portid);
885 		}
886 
887 		printf("Port %u, MAC address: " RTE_ETHER_ADDR_PRT_FMT "\n\n",
888 			portid,
889 			RTE_ETHER_ADDR_BYTES(&l2fwd_ports_eth_addr[portid]));
890 
891 		/* initialize port stats */
892 		memset(&port_statistics, 0, sizeof(port_statistics));
893 	}
894 
895 	if (!nb_ports_available) {
896 		rte_exit(EXIT_FAILURE,
897 			"All available ports are disabled. Please set portmask.\n");
898 	}
899 
900 	check_all_ports_link_status(l2fwd_enabled_port_mask);
901 
902 	ret = 0;
903 	/* launch per-lcore init on every lcore */
904 	rte_eal_mp_remote_launch(l2fwd_launch_one_lcore, NULL, CALL_MAIN);
905 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
906 		if (rte_eal_wait_lcore(lcore_id) < 0) {
907 			ret = -1;
908 			break;
909 		}
910 	}
911 
912 	RTE_ETH_FOREACH_DEV(portid) {
913 		if ((l2fwd_enabled_port_mask & (1 << portid)) == 0)
914 			continue;
915 		printf("Closing port %d...", portid);
916 		ret = rte_eth_dev_stop(portid);
917 		if (ret != 0)
918 			printf("rte_eth_dev_stop: err=%d, port=%d\n",
919 			       ret, portid);
920 		rte_eth_dev_close(portid);
921 		printf(" Done\n");
922 	}
923 
924 	/* clean up the EAL */
925 	rte_eal_cleanup();
926 	printf("Bye...\n");
927 
928 	return ret;
929 }
930