xref: /dpdk/examples/link_status_interrupt/main.c (revision ac718398f477cea795e6e6f8695b3e110a158b92)
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <stdint.h>
38 #include <inttypes.h>
39 #include <sys/types.h>
40 #include <string.h>
41 #include <sys/queue.h>
42 #include <netinet/in.h>
43 #include <setjmp.h>
44 #include <stdarg.h>
45 #include <ctype.h>
46 #include <errno.h>
47 #include <getopt.h>
48 
49 #include <rte_common.h>
50 #include <rte_log.h>
51 #include <rte_memory.h>
52 #include <rte_memcpy.h>
53 #include <rte_memzone.h>
54 #include <rte_tailq.h>
55 #include <rte_eal.h>
56 #include <rte_per_lcore.h>
57 #include <rte_launch.h>
58 #include <rte_atomic.h>
59 #include <rte_cycles.h>
60 #include <rte_prefetch.h>
61 #include <rte_lcore.h>
62 #include <rte_per_lcore.h>
63 #include <rte_branch_prediction.h>
64 #include <rte_interrupts.h>
65 #include <rte_pci.h>
66 #include <rte_random.h>
67 #include <rte_debug.h>
68 #include <rte_ether.h>
69 #include <rte_ethdev.h>
70 #include <rte_ring.h>
71 #include <rte_mempool.h>
72 #include <rte_mbuf.h>
73 
74 #include "main.h"
75 
76 #define RTE_LOGTYPE_LSI RTE_LOGTYPE_USER1
77 
78 #define MBUF_SIZE (2048 + sizeof(struct rte_mbuf) + RTE_PKTMBUF_HEADROOM)
79 #define NB_MBUF   8192
80 
81 #define MAX_PKT_BURST 32
82 #define BURST_TX_DRAIN_US 100 /* TX drain every ~100us */
83 
84 /*
85  * Configurable number of RX/TX ring descriptors
86  */
87 #define RTE_TEST_RX_DESC_DEFAULT 128
88 #define RTE_TEST_TX_DESC_DEFAULT 512
89 static uint16_t nb_rxd = RTE_TEST_RX_DESC_DEFAULT;
90 static uint16_t nb_txd = RTE_TEST_TX_DESC_DEFAULT;
91 
92 /* ethernet addresses of ports */
93 static struct ether_addr lsi_ports_eth_addr[RTE_MAX_ETHPORTS];
94 
95 /* mask of enabled ports */
96 static uint32_t lsi_enabled_port_mask = 0;
97 
98 static unsigned int lsi_rx_queue_per_lcore = 1;
99 
100 /* destination port for L2 forwarding */
101 static unsigned lsi_dst_ports[RTE_MAX_ETHPORTS] = {0};
102 
103 #define MAX_PKT_BURST 32
104 struct mbuf_table {
105 	unsigned len;
106 	struct rte_mbuf *m_table[MAX_PKT_BURST];
107 };
108 
109 #define MAX_RX_QUEUE_PER_LCORE 16
110 #define MAX_TX_QUEUE_PER_PORT 16
111 struct lcore_queue_conf {
112 	unsigned n_rx_port;
113 	unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
114 	unsigned tx_queue_id;
115 	struct mbuf_table tx_mbufs[RTE_MAX_ETHPORTS];
116 
117 } __rte_cache_aligned;
118 struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
119 
120 static const struct rte_eth_conf port_conf = {
121 	.rxmode = {
122 		.split_hdr_size = 0,
123 		.header_split   = 0, /**< Header Split disabled */
124 		.hw_ip_checksum = 0, /**< IP checksum offload disabled */
125 		.hw_vlan_filter = 0, /**< VLAN filtering disabled */
126 		.jumbo_frame    = 0, /**< Jumbo Frame Support disabled */
127 		.hw_strip_crc   = 0, /**< CRC stripped by hardware */
128 	},
129 	.txmode = {
130 		.mq_mode = ETH_MQ_TX_NONE,
131 	},
132 	.intr_conf = {
133 		.lsc = 1, /**< lsc interrupt feature enabled */
134 	},
135 };
136 
137 struct rte_mempool * lsi_pktmbuf_pool = NULL;
138 
139 /* Per-port statistics struct */
140 struct lsi_port_statistics {
141 	uint64_t tx;
142 	uint64_t rx;
143 	uint64_t dropped;
144 } __rte_cache_aligned;
145 struct lsi_port_statistics port_statistics[RTE_MAX_ETHPORTS];
146 
147 /* A tsc-based timer responsible for triggering statistics printout */
148 #define TIMER_MILLISECOND 2000000ULL /* around 1ms at 2 Ghz */
149 #define MAX_TIMER_PERIOD 86400 /* 1 day max */
150 static int64_t timer_period = 10 * TIMER_MILLISECOND * 1000; /* default period is 10 seconds */
151 
152 /* Print out statistics on packets dropped */
153 static void
154 print_stats(void)
155 {
156 	struct rte_eth_link link;
157 	uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
158 	unsigned portid;
159 
160 	total_packets_dropped = 0;
161 	total_packets_tx = 0;
162 	total_packets_rx = 0;
163 
164 	const char clr[] = { 27, '[', '2', 'J', '\0' };
165 	const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' };
166 
167 		/* Clear screen and move to top left */
168 	printf("%s%s", clr, topLeft);
169 
170 	printf("\nPort statistics ====================================");
171 
172 	for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
173 		/* skip ports that are not enabled */
174 		if ((lsi_enabled_port_mask & (1 << portid)) == 0)
175 			continue;
176 
177 		memset(&link, 0, sizeof(link));
178 		rte_eth_link_get_nowait((uint8_t)portid, &link);
179 		printf("\nStatistics for port %u ------------------------------"
180 			   "\nLink status: %25s"
181 			   "\nLink speed: %26u"
182 			   "\nLink duplex: %25s"
183 			   "\nPackets sent: %24"PRIu64
184 			   "\nPackets received: %20"PRIu64
185 			   "\nPackets dropped: %21"PRIu64,
186 			   portid,
187 			   (link.link_status ? "Link up" : "Link down"),
188 			   (unsigned)link.link_speed,
189 			   (link.link_duplex == ETH_LINK_FULL_DUPLEX ? \
190 					"full-duplex" : "half-duplex"),
191 			   port_statistics[portid].tx,
192 			   port_statistics[portid].rx,
193 			   port_statistics[portid].dropped);
194 
195 		total_packets_dropped += port_statistics[portid].dropped;
196 		total_packets_tx += port_statistics[portid].tx;
197 		total_packets_rx += port_statistics[portid].rx;
198 	}
199 	printf("\nAggregate statistics ==============================="
200 		   "\nTotal packets sent: %18"PRIu64
201 		   "\nTotal packets received: %14"PRIu64
202 		   "\nTotal packets dropped: %15"PRIu64,
203 		   total_packets_tx,
204 		   total_packets_rx,
205 		   total_packets_dropped);
206 	printf("\n====================================================\n");
207 }
208 
209 /* Send the packet on an output interface */
210 static int
211 lsi_send_burst(struct lcore_queue_conf *qconf, unsigned n, uint8_t port)
212 {
213 	struct rte_mbuf **m_table;
214 	unsigned ret;
215 	unsigned queueid;
216 
217 	queueid = (uint16_t) qconf->tx_queue_id;
218 	m_table = (struct rte_mbuf **)qconf->tx_mbufs[port].m_table;
219 
220 	ret = rte_eth_tx_burst(port, (uint16_t) queueid, m_table, (uint16_t) n);
221 	port_statistics[port].tx += ret;
222 	if (unlikely(ret < n)) {
223 		port_statistics[port].dropped += (n - ret);
224 		do {
225 			rte_pktmbuf_free(m_table[ret]);
226 		} while (++ret < n);
227 	}
228 
229 	return 0;
230 }
231 
232 /* Send the packet on an output interface */
233 static int
234 lsi_send_packet(struct rte_mbuf *m, uint8_t port)
235 {
236 	unsigned lcore_id, len;
237 	struct lcore_queue_conf *qconf;
238 
239 	lcore_id = rte_lcore_id();
240 
241 	qconf = &lcore_queue_conf[lcore_id];
242 	len = qconf->tx_mbufs[port].len;
243 	qconf->tx_mbufs[port].m_table[len] = m;
244 	len++;
245 
246 	/* enough pkts to be sent */
247 	if (unlikely(len == MAX_PKT_BURST)) {
248 		lsi_send_burst(qconf, MAX_PKT_BURST, port);
249 		len = 0;
250 	}
251 
252 	qconf->tx_mbufs[port].len = len;
253 	return 0;
254 }
255 
256 static void
257 lsi_simple_forward(struct rte_mbuf *m, unsigned portid)
258 {
259 	struct ether_hdr *eth;
260 	void *tmp;
261 	unsigned dst_port = lsi_dst_ports[portid];
262 
263 	eth = rte_pktmbuf_mtod(m, struct ether_hdr *);
264 
265 	/* 02:00:00:00:00:xx */
266 	tmp = &eth->d_addr.addr_bytes[0];
267 	*((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dst_port << 40);
268 
269 	/* src addr */
270 	ether_addr_copy(&lsi_ports_eth_addr[dst_port], &eth->s_addr);
271 
272 	lsi_send_packet(m, (uint8_t) dst_port);
273 }
274 
275 /* main processing loop */
276 static void
277 lsi_main_loop(void)
278 {
279 	struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
280 	struct rte_mbuf *m;
281 	unsigned lcore_id;
282 	uint64_t prev_tsc, diff_tsc, cur_tsc, timer_tsc;
283 	unsigned i, j, portid, nb_rx;
284 	struct lcore_queue_conf *qconf;
285 	const uint64_t drain_tsc = (rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
286 
287 	prev_tsc = 0;
288 	timer_tsc = 0;
289 
290 	lcore_id = rte_lcore_id();
291 	qconf = &lcore_queue_conf[lcore_id];
292 
293 	if (qconf->n_rx_port == 0) {
294 		RTE_LOG(INFO, LSI, "lcore %u has nothing to do\n", lcore_id);
295 		return;
296 	}
297 
298 	RTE_LOG(INFO, LSI, "entering main loop on lcore %u\n", lcore_id);
299 
300 	for (i = 0; i < qconf->n_rx_port; i++) {
301 
302 		portid = qconf->rx_port_list[i];
303 		RTE_LOG(INFO, LSI, " -- lcoreid=%u portid=%u\n", lcore_id,
304 			portid);
305 	}
306 
307 	while (1) {
308 
309 		cur_tsc = rte_rdtsc();
310 
311 		/*
312 		 * TX burst queue drain
313 		 */
314 		diff_tsc = cur_tsc - prev_tsc;
315 		if (unlikely(diff_tsc > drain_tsc)) {
316 
317 			/* this could be optimized (use queueid instead of
318 			 * portid), but it is not called so often */
319 			for (portid = 0; portid < RTE_MAX_ETHPORTS; portid++) {
320 				if (qconf->tx_mbufs[portid].len == 0)
321 					continue;
322 				lsi_send_burst(&lcore_queue_conf[lcore_id],
323 						 qconf->tx_mbufs[portid].len,
324 						 (uint8_t) portid);
325 				qconf->tx_mbufs[portid].len = 0;
326 			}
327 
328 			/* if timer is enabled */
329 			if (timer_period > 0) {
330 
331 				/* advance the timer */
332 				timer_tsc += diff_tsc;
333 
334 				/* if timer has reached its timeout */
335 				if (unlikely(timer_tsc >= (uint64_t) timer_period)) {
336 
337 					/* do this only on master core */
338 					if (lcore_id == rte_get_master_lcore()) {
339 						print_stats();
340 						/* reset the timer */
341 						timer_tsc = 0;
342 					}
343 				}
344 			}
345 
346 			prev_tsc = cur_tsc;
347 		}
348 
349 		/*
350 		 * Read packet from RX queues
351 		 */
352 		for (i = 0; i < qconf->n_rx_port; i++) {
353 
354 			portid = qconf->rx_port_list[i];
355 			nb_rx = rte_eth_rx_burst((uint8_t) portid, 0,
356 						 pkts_burst, MAX_PKT_BURST);
357 
358 			port_statistics[portid].rx += nb_rx;
359 
360 			for (j = 0; j < nb_rx; j++) {
361 				m = pkts_burst[j];
362 				rte_prefetch0(rte_pktmbuf_mtod(m, void *));
363 				lsi_simple_forward(m, portid);
364 			}
365 		}
366 	}
367 }
368 
369 static int
370 lsi_launch_one_lcore(__attribute__((unused)) void *dummy)
371 {
372 	lsi_main_loop();
373 	return 0;
374 }
375 
376 /* display usage */
377 static void
378 lsi_usage(const char *prgname)
379 {
380 	printf("%s [EAL options] -- -p PORTMASK [-q NQ]\n"
381 		"  -p PORTMASK: hexadecimal bitmask of ports to configure\n"
382 		"  -q NQ: number of queue (=ports) per lcore (default is 1)\n"
383 		"  -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n",
384 			prgname);
385 }
386 
387 static int
388 lsi_parse_portmask(const char *portmask)
389 {
390 	char *end = NULL;
391 	unsigned long pm;
392 
393 	/* parse hexadecimal string */
394 	pm = strtoul(portmask, &end, 16);
395 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
396 		return -1;
397 
398 	if (pm == 0)
399 		return -1;
400 
401 	return pm;
402 }
403 
404 static unsigned int
405 lsi_parse_nqueue(const char *q_arg)
406 {
407 	char *end = NULL;
408 	unsigned long n;
409 
410 	/* parse hexadecimal string */
411 	n = strtoul(q_arg, &end, 10);
412 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
413 		return 0;
414 	if (n == 0)
415 		return 0;
416 	if (n >= MAX_RX_QUEUE_PER_LCORE)
417 		return 0;
418 
419 	return n;
420 }
421 
422 static int
423 lsi_parse_timer_period(const char *q_arg)
424 {
425 	char *end = NULL;
426 	int n;
427 
428 	/* parse number string */
429 	n = strtol(q_arg, &end, 10);
430 	if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
431 		return -1;
432 	if (n >= MAX_TIMER_PERIOD)
433 		return -1;
434 
435 	return n;
436 }
437 
438 /* Parse the argument given in the command line of the application */
439 static int
440 lsi_parse_args(int argc, char **argv)
441 {
442 	int opt, ret;
443 	char **argvopt;
444 	int option_index;
445 	char *prgname = argv[0];
446 	static struct option lgopts[] = {
447 		{NULL, 0, 0, 0}
448 	};
449 
450 	argvopt = argv;
451 
452 	while ((opt = getopt_long(argc, argvopt, "p:q:T:",
453 				  lgopts, &option_index)) != EOF) {
454 
455 		switch (opt) {
456 		/* portmask */
457 		case 'p':
458 			lsi_enabled_port_mask = lsi_parse_portmask(optarg);
459 			if (lsi_enabled_port_mask == 0) {
460 				printf("invalid portmask\n");
461 				lsi_usage(prgname);
462 				return -1;
463 			}
464 			break;
465 
466 		/* nqueue */
467 		case 'q':
468 			lsi_rx_queue_per_lcore = lsi_parse_nqueue(optarg);
469 			if (lsi_rx_queue_per_lcore == 0) {
470 				printf("invalid queue number\n");
471 				lsi_usage(prgname);
472 				return -1;
473 			}
474 			break;
475 
476 		/* timer period */
477 		case 'T':
478 			timer_period = lsi_parse_timer_period(optarg) * 1000 * TIMER_MILLISECOND;
479 			if (timer_period < 0) {
480 				printf("invalid timer period\n");
481 				lsi_usage(prgname);
482 				return -1;
483 			}
484 			break;
485 
486 		/* long options */
487 		case 0:
488 			lsi_usage(prgname);
489 			return -1;
490 
491 		default:
492 			lsi_usage(prgname);
493 			return -1;
494 		}
495 	}
496 
497 	if (optind >= 0)
498 		argv[optind-1] = prgname;
499 
500 	ret = optind-1;
501 	optind = 0; /* reset getopt lib */
502 	return ret;
503 }
504 
505 /**
506  * It will be called as the callback for specified port after a LSI interrupt
507  * has been fully handled. This callback needs to be implemented carefully as
508  * it will be called in the interrupt host thread which is different from the
509  * application main thread.
510  *
511  * @param port_id
512  *  Port id.
513  * @param type
514  *  event type.
515  * @param param
516  *  Pointer to(address of) the parameters.
517  *
518  * @return
519  *  void.
520  */
521 static void
522 lsi_event_callback(uint8_t port_id, enum rte_eth_event_type type, void *param)
523 {
524 	struct rte_eth_link link;
525 
526 	RTE_SET_USED(param);
527 
528 	printf("\n\nIn registered callback...\n");
529 	printf("Event type: %s\n", type == RTE_ETH_EVENT_INTR_LSC ? "LSC interrupt" : "unknown event");
530 	rte_eth_link_get_nowait(port_id, &link);
531 	if (link.link_status) {
532 		printf("Port %d Link Up - speed %u Mbps - %s\n\n",
533 				port_id, (unsigned)link.link_speed,
534 			(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
535 				("full-duplex") : ("half-duplex"));
536 	} else
537 		printf("Port %d Link Down\n\n", port_id);
538 }
539 
540 /* Check the link status of all ports in up to 9s, and print them finally */
541 static void
542 check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
543 {
544 #define CHECK_INTERVAL 100 /* 100ms */
545 #define MAX_CHECK_TIME 90 /* 9s (90 * 100ms) in total */
546 	uint8_t portid, count, all_ports_up, print_flag = 0;
547 	struct rte_eth_link link;
548 
549 	printf("\nChecking link status");
550 	fflush(stdout);
551 	for (count = 0; count <= MAX_CHECK_TIME; count++) {
552 		all_ports_up = 1;
553 		for (portid = 0; portid < port_num; portid++) {
554 			if ((port_mask & (1 << portid)) == 0)
555 				continue;
556 			memset(&link, 0, sizeof(link));
557 			rte_eth_link_get_nowait(portid, &link);
558 			/* print link status if flag set */
559 			if (print_flag == 1) {
560 				if (link.link_status)
561 					printf("Port %d Link Up - speed %u "
562 						"Mbps - %s\n", (uint8_t)portid,
563 						(unsigned)link.link_speed,
564 				(link.link_duplex == ETH_LINK_FULL_DUPLEX) ?
565 					("full-duplex") : ("half-duplex\n"));
566 				else
567 					printf("Port %d Link Down\n",
568 							(uint8_t)portid);
569 				continue;
570 			}
571 			/* clear all_ports_up flag if any link down */
572 			if (link.link_status == 0) {
573 				all_ports_up = 0;
574 				break;
575 			}
576 		}
577 		/* after finally printing all link status, get out */
578 		if (print_flag == 1)
579 			break;
580 
581 		if (all_ports_up == 0) {
582 			printf(".");
583 			fflush(stdout);
584 			rte_delay_ms(CHECK_INTERVAL);
585 		}
586 
587 		/* set the print_flag if all ports up or timeout */
588 		if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
589 			print_flag = 1;
590 			printf("done\n");
591 		}
592 	}
593 }
594 
595 int
596 MAIN(int argc, char **argv)
597 {
598 	struct lcore_queue_conf *qconf;
599 	struct rte_eth_dev_info dev_info;
600 	int ret;
601 	uint8_t nb_ports;
602 	uint8_t portid, portid_last = 0;
603 	unsigned lcore_id, rx_lcore_id;
604 	unsigned nb_ports_in_mask = 0;
605 
606 	/* init EAL */
607 	ret = rte_eal_init(argc, argv);
608 	if (ret < 0)
609 		rte_exit(EXIT_FAILURE, "rte_eal_init failed");
610 	argc -= ret;
611 	argv += ret;
612 
613 	/* parse application arguments (after the EAL ones) */
614 	ret = lsi_parse_args(argc, argv);
615 	if (ret < 0)
616 		rte_exit(EXIT_FAILURE, "Invalid arguments");
617 
618 	/* create the mbuf pool */
619 	lsi_pktmbuf_pool =
620 		rte_mempool_create("mbuf_pool", NB_MBUF,
621 				   MBUF_SIZE, 32,
622 				   sizeof(struct rte_pktmbuf_pool_private),
623 				   rte_pktmbuf_pool_init, NULL,
624 				   rte_pktmbuf_init, NULL,
625 				   rte_socket_id(), 0);
626 	if (lsi_pktmbuf_pool == NULL)
627 		rte_panic("Cannot init mbuf pool\n");
628 
629 	nb_ports = rte_eth_dev_count();
630 	if (nb_ports == 0)
631 		rte_panic("No Ethernet port - bye\n");
632 
633 	if (nb_ports > RTE_MAX_ETHPORTS)
634 		nb_ports = RTE_MAX_ETHPORTS;
635 
636 	/*
637 	 * Each logical core is assigned a dedicated TX queue on each port.
638 	 */
639 	for (portid = 0; portid < nb_ports; portid++) {
640 		/* skip ports that are not enabled */
641 		if ((lsi_enabled_port_mask & (1 << portid)) == 0)
642 			continue;
643 
644 		/* save the destination port id */
645 		if (nb_ports_in_mask % 2) {
646 			lsi_dst_ports[portid] = portid_last;
647 			lsi_dst_ports[portid_last] = portid;
648 		}
649 		else
650 			portid_last = portid;
651 
652 		nb_ports_in_mask++;
653 
654 		rte_eth_dev_info_get(portid, &dev_info);
655 	}
656 	if (nb_ports_in_mask < 2 || nb_ports_in_mask % 2)
657 		rte_exit(EXIT_FAILURE, "Current enabled port number is %u, "
658 				"but it should be even and at least 2\n",
659 				nb_ports_in_mask);
660 
661 	rx_lcore_id = 0;
662 	qconf = &lcore_queue_conf[rx_lcore_id];
663 
664 	/* Initialize the port/queue configuration of each logical core */
665 	for (portid = 0; portid < nb_ports; portid++) {
666 		/* skip ports that are not enabled */
667 		if ((lsi_enabled_port_mask & (1 << portid)) == 0)
668 			continue;
669 
670 		/* get the lcore_id for this port */
671 		while (rte_lcore_is_enabled(rx_lcore_id) == 0 ||
672 		       lcore_queue_conf[rx_lcore_id].n_rx_port ==
673 		       lsi_rx_queue_per_lcore) {
674 
675 			rx_lcore_id++;
676 			if (rx_lcore_id >= RTE_MAX_LCORE)
677 				rte_exit(EXIT_FAILURE, "Not enough cores\n");
678 		}
679 		if (qconf != &lcore_queue_conf[rx_lcore_id])
680 			/* Assigned a new logical core in the loop above. */
681 			qconf = &lcore_queue_conf[rx_lcore_id];
682 
683 		qconf->rx_port_list[qconf->n_rx_port] = portid;
684 		qconf->n_rx_port++;
685 		printf("Lcore %u: RX port %u\n",rx_lcore_id, (unsigned) portid);
686 	}
687 
688 	/* Initialise each port */
689 	for (portid = 0; portid < nb_ports; portid++) {
690 		/* skip ports that are not enabled */
691 		if ((lsi_enabled_port_mask & (1 << portid)) == 0) {
692 			printf("Skipping disabled port %u\n", (unsigned) portid);
693 			continue;
694 		}
695 		/* init port */
696 		printf("Initializing port %u... ", (unsigned) portid);
697 		fflush(stdout);
698 		ret = rte_eth_dev_configure(portid, 1, 1, &port_conf);
699 		if (ret < 0)
700 			rte_exit(EXIT_FAILURE, "Cannot configure device: err=%d, port=%u\n",
701 				  ret, (unsigned) portid);
702 
703 		/* register lsi interrupt callback, need to be after
704 		 * rte_eth_dev_configure(). if (intr_conf.lsc == 0), no
705 		 * lsc interrupt will be present, and below callback to
706 		 * be registered will never be called.
707 		 */
708 		rte_eth_dev_callback_register(portid,
709 			RTE_ETH_EVENT_INTR_LSC, lsi_event_callback, NULL);
710 
711 		rte_eth_macaddr_get(portid,
712 				    &lsi_ports_eth_addr[portid]);
713 
714 		/* init one RX queue */
715 		fflush(stdout);
716 		ret = rte_eth_rx_queue_setup(portid, 0, nb_rxd,
717 					     rte_eth_dev_socket_id(portid),
718 					     NULL,
719 					     lsi_pktmbuf_pool);
720 		if (ret < 0)
721 			rte_exit(EXIT_FAILURE, "rte_eth_rx_queue_setup: err=%d, port=%u\n",
722 				  ret, (unsigned) portid);
723 
724 		/* init one TX queue logical core on each port */
725 		fflush(stdout);
726 		ret = rte_eth_tx_queue_setup(portid, 0, nb_txd,
727 				rte_eth_dev_socket_id(portid),
728 				NULL);
729 		if (ret < 0)
730 			rte_exit(EXIT_FAILURE, "rte_eth_tx_queue_setup: err=%d,port=%u\n",
731 				  ret, (unsigned) portid);
732 
733 		/* Start device */
734 		ret = rte_eth_dev_start(portid);
735 		if (ret < 0)
736 			rte_exit(EXIT_FAILURE, "rte_eth_dev_start: err=%d, port=%u\n",
737 				  ret, (unsigned) portid);
738 		printf("done:\n");
739 
740 		printf("Port %u, MAC address: %02X:%02X:%02X:%02X:%02X:%02X\n\n",
741 				(unsigned) portid,
742 				lsi_ports_eth_addr[portid].addr_bytes[0],
743 				lsi_ports_eth_addr[portid].addr_bytes[1],
744 				lsi_ports_eth_addr[portid].addr_bytes[2],
745 				lsi_ports_eth_addr[portid].addr_bytes[3],
746 				lsi_ports_eth_addr[portid].addr_bytes[4],
747 				lsi_ports_eth_addr[portid].addr_bytes[5]);
748 
749 		/* initialize port stats */
750 		memset(&port_statistics, 0, sizeof(port_statistics));
751 	}
752 
753 	check_all_ports_link_status(nb_ports, lsi_enabled_port_mask);
754 
755 	/* launch per-lcore init on every lcore */
756 	rte_eal_mp_remote_launch(lsi_launch_one_lcore, NULL, CALL_MASTER);
757 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
758 		if (rte_eal_wait_lcore(lcore_id) < 0)
759 			return -1;
760 	}
761 
762 	return 0;
763 }
764