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