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