xref: /dpdk/examples/packet_ordering/main.c (revision 3bb3ebb51b789d4ecb417cbdb1dce5c7211f6f18)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2016 Intel Corporation
3  */
4 
5 #include <signal.h>
6 #include <getopt.h>
7 
8 #include <rte_eal.h>
9 #include <rte_common.h>
10 #include <rte_errno.h>
11 #include <rte_ethdev.h>
12 #include <rte_lcore.h>
13 #include <rte_malloc.h>
14 #include <rte_mbuf.h>
15 #include <rte_mempool.h>
16 #include <rte_ring.h>
17 #include <rte_reorder.h>
18 
19 #define RX_DESC_PER_QUEUE 1024
20 #define TX_DESC_PER_QUEUE 1024
21 
22 #define MAX_PKTS_BURST 32
23 #define REORDER_BUFFER_SIZE 8192
24 #define MBUF_PER_POOL 65535
25 #define MBUF_POOL_CACHE_SIZE 250
26 
27 #define RING_SIZE 16384
28 
29 /* Macros for printing using RTE_LOG */
30 #define RTE_LOGTYPE_REORDERAPP          RTE_LOGTYPE_USER1
31 
32 enum {
33 #define OPT_DISABLE_REORDER "disable-reorder"
34 	OPT_DISABLE_REORDER_NUM = 256,
35 #define OPT_INSIGHT_WORKER  "insight-worker"
36 	OPT_INSIGHT_WORKER_NUM,
37 };
38 
39 unsigned int portmask;
40 unsigned int disable_reorder;
41 unsigned int insight_worker;
42 volatile uint8_t quit_signal;
43 
44 static struct rte_mempool *mbuf_pool;
45 
46 static struct rte_eth_conf port_conf_default;
47 
48 struct worker_thread_args {
49 	struct rte_ring *ring_in;
50 	struct rte_ring *ring_out;
51 };
52 
53 struct send_thread_args {
54 	struct rte_ring *ring_in;
55 	struct rte_reorder_buffer *buffer;
56 };
57 
58 volatile struct app_stats {
59 	struct {
60 		uint64_t rx_pkts;
61 		uint64_t enqueue_pkts;
62 		uint64_t enqueue_failed_pkts;
63 	} rx __rte_cache_aligned;
64 
65 	struct {
66 		uint64_t dequeue_pkts;
67 		uint64_t enqueue_pkts;
68 		uint64_t enqueue_failed_pkts;
69 	} wkr __rte_cache_aligned;
70 
71 	struct {
72 		uint64_t dequeue_pkts;
73 		/* Too early pkts transmitted directly w/o reordering */
74 		uint64_t early_pkts_txtd_woro;
75 		/* Too early pkts failed from direct transmit */
76 		uint64_t early_pkts_tx_failed_woro;
77 		uint64_t ro_tx_pkts;
78 		uint64_t ro_tx_failed_pkts;
79 	} tx __rte_cache_aligned;
80 } app_stats;
81 
82 /* per worker lcore stats */
83 struct wkr_stats_per {
84 		uint64_t deq_pkts;
85 		uint64_t enq_pkts;
86 		uint64_t enq_failed_pkts;
87 } __rte_cache_aligned;
88 
89 static struct wkr_stats_per wkr_stats[RTE_MAX_LCORE] = { {0} };
90 /**
91  * Get the last enabled lcore ID
92  *
93  * @return
94  *   The last enabled lcore ID.
95  */
96 static unsigned int
97 get_last_lcore_id(void)
98 {
99 	int i;
100 
101 	for (i = RTE_MAX_LCORE - 1; i >= 0; i--)
102 		if (rte_lcore_is_enabled(i))
103 			return i;
104 	return 0;
105 }
106 
107 /**
108  * Get the previous enabled lcore ID
109  * @param id
110  *  The current lcore ID
111  * @return
112  *   The previous enabled lcore ID or the current lcore
113  *   ID if it is the first available core.
114  */
115 static unsigned int
116 get_previous_lcore_id(unsigned int id)
117 {
118 	int i;
119 
120 	for (i = id - 1; i >= 0; i--)
121 		if (rte_lcore_is_enabled(i))
122 			return i;
123 	return id;
124 }
125 
126 static inline void
127 pktmbuf_free_bulk(struct rte_mbuf *mbuf_table[], unsigned n)
128 {
129 	unsigned int i;
130 
131 	for (i = 0; i < n; i++)
132 		rte_pktmbuf_free(mbuf_table[i]);
133 }
134 
135 /* display usage */
136 static void
137 print_usage(const char *prgname)
138 {
139 	printf("%s [EAL options] -- -p PORTMASK\n"
140 			"  -p PORTMASK: hexadecimal bitmask of ports to configure\n",
141 			prgname);
142 }
143 
144 static int
145 parse_portmask(const char *portmask)
146 {
147 	unsigned long pm;
148 	char *end = NULL;
149 
150 	/* parse hexadecimal string */
151 	pm = strtoul(portmask, &end, 16);
152 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
153 		return 0;
154 
155 	return pm;
156 }
157 
158 /* Parse the argument given in the command line of the application */
159 static int
160 parse_args(int argc, char **argv)
161 {
162 	int opt;
163 	int option_index;
164 	char **argvopt;
165 	char *prgname = argv[0];
166 	static struct option lgopts[] = {
167 		{OPT_DISABLE_REORDER, 0, NULL, OPT_DISABLE_REORDER_NUM},
168 		{OPT_INSIGHT_WORKER,  0, NULL, OPT_INSIGHT_WORKER_NUM },
169 		{NULL,                0, 0,    0                      }
170 	};
171 
172 	argvopt = argv;
173 
174 	while ((opt = getopt_long(argc, argvopt, "p:",
175 					lgopts, &option_index)) != EOF) {
176 		switch (opt) {
177 		/* portmask */
178 		case 'p':
179 			portmask = parse_portmask(optarg);
180 			if (portmask == 0) {
181 				printf("invalid portmask\n");
182 				print_usage(prgname);
183 				return -1;
184 			}
185 			break;
186 
187 		/* long options */
188 		case OPT_DISABLE_REORDER_NUM:
189 			printf("reorder disabled\n");
190 			disable_reorder = 1;
191 			break;
192 
193 		case OPT_INSIGHT_WORKER_NUM:
194 			printf("print all worker statistics\n");
195 			insight_worker = 1;
196 			break;
197 
198 		default:
199 			print_usage(prgname);
200 			return -1;
201 		}
202 	}
203 	if (optind <= 1) {
204 		print_usage(prgname);
205 		return -1;
206 	}
207 
208 	argv[optind-1] = prgname;
209 	optind = 1; /* reset getopt lib */
210 	return 0;
211 }
212 
213 /*
214  * Tx buffer error callback
215  */
216 static void
217 flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
218 		void *userdata __rte_unused) {
219 
220 	/* free the mbufs which failed from transmit */
221 	app_stats.tx.ro_tx_failed_pkts += count;
222 	RTE_LOG_DP(DEBUG, REORDERAPP, "%s:Packet loss with tx_burst\n", __func__);
223 	pktmbuf_free_bulk(unsent, count);
224 
225 }
226 
227 static inline int
228 free_tx_buffers(struct rte_eth_dev_tx_buffer *tx_buffer[]) {
229 	uint16_t port_id;
230 
231 	/* initialize buffers for all ports */
232 	RTE_ETH_FOREACH_DEV(port_id) {
233 		/* skip ports that are not enabled */
234 		if ((portmask & (1 << port_id)) == 0)
235 			continue;
236 
237 		rte_free(tx_buffer[port_id]);
238 	}
239 	return 0;
240 }
241 
242 static inline int
243 configure_tx_buffers(struct rte_eth_dev_tx_buffer *tx_buffer[])
244 {
245 	uint16_t port_id;
246 	int ret;
247 
248 	/* initialize buffers for all ports */
249 	RTE_ETH_FOREACH_DEV(port_id) {
250 		/* skip ports that are not enabled */
251 		if ((portmask & (1 << port_id)) == 0)
252 			continue;
253 
254 		/* Initialize TX buffers */
255 		tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
256 				RTE_ETH_TX_BUFFER_SIZE(MAX_PKTS_BURST), 0,
257 				rte_eth_dev_socket_id(port_id));
258 		if (tx_buffer[port_id] == NULL)
259 			rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
260 				 port_id);
261 
262 		rte_eth_tx_buffer_init(tx_buffer[port_id], MAX_PKTS_BURST);
263 
264 		ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
265 				flush_tx_error_callback, NULL);
266 		if (ret < 0)
267 			rte_exit(EXIT_FAILURE,
268 			"Cannot set error callback for tx buffer on port %u\n",
269 				 port_id);
270 	}
271 	return 0;
272 }
273 
274 static inline int
275 configure_eth_port(uint16_t port_id)
276 {
277 	struct rte_ether_addr addr;
278 	const uint16_t rxRings = 1, txRings = 1;
279 	int ret;
280 	uint16_t q;
281 	uint16_t nb_rxd = RX_DESC_PER_QUEUE;
282 	uint16_t nb_txd = TX_DESC_PER_QUEUE;
283 	struct rte_eth_dev_info dev_info;
284 	struct rte_eth_txconf txconf;
285 	struct rte_eth_conf port_conf = port_conf_default;
286 
287 	if (!rte_eth_dev_is_valid_port(port_id))
288 		return -1;
289 
290 	ret = rte_eth_dev_info_get(port_id, &dev_info);
291 	if (ret != 0) {
292 		printf("Error during getting device (port %u) info: %s\n",
293 				port_id, strerror(-ret));
294 		return ret;
295 	}
296 
297 	if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
298 		port_conf.txmode.offloads |=
299 			DEV_TX_OFFLOAD_MBUF_FAST_FREE;
300 	ret = rte_eth_dev_configure(port_id, rxRings, txRings, &port_conf_default);
301 	if (ret != 0)
302 		return ret;
303 
304 	ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_id, &nb_rxd, &nb_txd);
305 	if (ret != 0)
306 		return ret;
307 
308 	for (q = 0; q < rxRings; q++) {
309 		ret = rte_eth_rx_queue_setup(port_id, q, nb_rxd,
310 				rte_eth_dev_socket_id(port_id), NULL,
311 				mbuf_pool);
312 		if (ret < 0)
313 			return ret;
314 	}
315 
316 	txconf = dev_info.default_txconf;
317 	txconf.offloads = port_conf.txmode.offloads;
318 	for (q = 0; q < txRings; q++) {
319 		ret = rte_eth_tx_queue_setup(port_id, q, nb_txd,
320 				rte_eth_dev_socket_id(port_id), &txconf);
321 		if (ret < 0)
322 			return ret;
323 	}
324 
325 	ret = rte_eth_dev_start(port_id);
326 	if (ret < 0)
327 		return ret;
328 
329 	ret = rte_eth_macaddr_get(port_id, &addr);
330 	if (ret != 0) {
331 		printf("Failed to get MAC address (port %u): %s\n",
332 				port_id, rte_strerror(-ret));
333 		return ret;
334 	}
335 
336 	printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
337 			" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
338 			port_id,
339 			addr.addr_bytes[0], addr.addr_bytes[1],
340 			addr.addr_bytes[2], addr.addr_bytes[3],
341 			addr.addr_bytes[4], addr.addr_bytes[5]);
342 
343 	ret = rte_eth_promiscuous_enable(port_id);
344 	if (ret != 0)
345 		return ret;
346 
347 	return 0;
348 }
349 
350 static void
351 print_stats(void)
352 {
353 	uint16_t i;
354 	struct rte_eth_stats eth_stats;
355 	unsigned int lcore_id, last_lcore_id, main_lcore_id, end_w_lcore_id;
356 
357 	last_lcore_id   = get_last_lcore_id();
358 	main_lcore_id = rte_get_main_lcore();
359 	end_w_lcore_id  = get_previous_lcore_id(last_lcore_id);
360 
361 	printf("\nRX thread stats:\n");
362 	printf(" - Pkts rxd:				%"PRIu64"\n",
363 						app_stats.rx.rx_pkts);
364 	printf(" - Pkts enqd to workers ring:		%"PRIu64"\n",
365 						app_stats.rx.enqueue_pkts);
366 
367 	for (lcore_id = 0; lcore_id <= end_w_lcore_id; lcore_id++) {
368 		if (insight_worker
369 			&& rte_lcore_is_enabled(lcore_id)
370 			&& lcore_id != main_lcore_id) {
371 			printf("\nWorker thread stats on core [%u]:\n",
372 					lcore_id);
373 			printf(" - Pkts deqd from workers ring:		%"PRIu64"\n",
374 					wkr_stats[lcore_id].deq_pkts);
375 			printf(" - Pkts enqd to tx ring:		%"PRIu64"\n",
376 					wkr_stats[lcore_id].enq_pkts);
377 			printf(" - Pkts enq to tx failed:		%"PRIu64"\n",
378 					wkr_stats[lcore_id].enq_failed_pkts);
379 		}
380 
381 		app_stats.wkr.dequeue_pkts += wkr_stats[lcore_id].deq_pkts;
382 		app_stats.wkr.enqueue_pkts += wkr_stats[lcore_id].enq_pkts;
383 		app_stats.wkr.enqueue_failed_pkts +=
384 			wkr_stats[lcore_id].enq_failed_pkts;
385 	}
386 
387 	printf("\nWorker thread stats:\n");
388 	printf(" - Pkts deqd from workers ring:		%"PRIu64"\n",
389 						app_stats.wkr.dequeue_pkts);
390 	printf(" - Pkts enqd to tx ring:		%"PRIu64"\n",
391 						app_stats.wkr.enqueue_pkts);
392 	printf(" - Pkts enq to tx failed:		%"PRIu64"\n",
393 						app_stats.wkr.enqueue_failed_pkts);
394 
395 	printf("\nTX stats:\n");
396 	printf(" - Pkts deqd from tx ring:		%"PRIu64"\n",
397 						app_stats.tx.dequeue_pkts);
398 	printf(" - Ro Pkts transmitted:			%"PRIu64"\n",
399 						app_stats.tx.ro_tx_pkts);
400 	printf(" - Ro Pkts tx failed:			%"PRIu64"\n",
401 						app_stats.tx.ro_tx_failed_pkts);
402 	printf(" - Pkts transmitted w/o reorder:	%"PRIu64"\n",
403 						app_stats.tx.early_pkts_txtd_woro);
404 	printf(" - Pkts tx failed w/o reorder:		%"PRIu64"\n",
405 						app_stats.tx.early_pkts_tx_failed_woro);
406 
407 	RTE_ETH_FOREACH_DEV(i) {
408 		rte_eth_stats_get(i, &eth_stats);
409 		printf("\nPort %u stats:\n", i);
410 		printf(" - Pkts in:   %"PRIu64"\n", eth_stats.ipackets);
411 		printf(" - Pkts out:  %"PRIu64"\n", eth_stats.opackets);
412 		printf(" - In Errs:   %"PRIu64"\n", eth_stats.ierrors);
413 		printf(" - Out Errs:  %"PRIu64"\n", eth_stats.oerrors);
414 		printf(" - Mbuf Errs: %"PRIu64"\n", eth_stats.rx_nombuf);
415 	}
416 }
417 
418 static void
419 int_handler(int sig_num)
420 {
421 	printf("Exiting on signal %d\n", sig_num);
422 	quit_signal = 1;
423 }
424 
425 /**
426  * This thread receives mbufs from the port and affects them an internal
427  * sequence number to keep track of their order of arrival through an
428  * mbuf structure.
429  * The mbufs are then passed to the worker threads via the rx_to_workers
430  * ring.
431  */
432 static int
433 rx_thread(struct rte_ring *ring_out)
434 {
435 	uint32_t seqn = 0;
436 	uint16_t i, ret = 0;
437 	uint16_t nb_rx_pkts;
438 	uint16_t port_id;
439 	struct rte_mbuf *pkts[MAX_PKTS_BURST];
440 
441 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
442 							rte_lcore_id());
443 
444 	while (!quit_signal) {
445 
446 		RTE_ETH_FOREACH_DEV(port_id) {
447 			if ((portmask & (1 << port_id)) != 0) {
448 
449 				/* receive packets */
450 				nb_rx_pkts = rte_eth_rx_burst(port_id, 0,
451 								pkts, MAX_PKTS_BURST);
452 				if (nb_rx_pkts == 0) {
453 					RTE_LOG_DP(DEBUG, REORDERAPP,
454 					"%s():Received zero packets\n",	__func__);
455 					continue;
456 				}
457 				app_stats.rx.rx_pkts += nb_rx_pkts;
458 
459 				/* mark sequence number */
460 				for (i = 0; i < nb_rx_pkts; )
461 					*rte_reorder_seqn(pkts[i++]) = seqn++;
462 
463 				/* enqueue to rx_to_workers ring */
464 				ret = rte_ring_enqueue_burst(ring_out,
465 						(void *)pkts, nb_rx_pkts, NULL);
466 				app_stats.rx.enqueue_pkts += ret;
467 				if (unlikely(ret < nb_rx_pkts)) {
468 					app_stats.rx.enqueue_failed_pkts +=
469 									(nb_rx_pkts-ret);
470 					pktmbuf_free_bulk(&pkts[ret], nb_rx_pkts - ret);
471 				}
472 			}
473 		}
474 	}
475 	return 0;
476 }
477 
478 /**
479  * This thread takes bursts of packets from the rx_to_workers ring and
480  * Changes the input port value to output port value. And feds it to
481  * workers_to_tx
482  */
483 static int
484 worker_thread(void *args_ptr)
485 {
486 	const uint16_t nb_ports = rte_eth_dev_count_avail();
487 	uint16_t i, ret = 0;
488 	uint16_t burst_size = 0;
489 	struct worker_thread_args *args;
490 	struct rte_mbuf *burst_buffer[MAX_PKTS_BURST] = { NULL };
491 	struct rte_ring *ring_in, *ring_out;
492 	const unsigned xor_val = (nb_ports > 1);
493 	unsigned int core_id = rte_lcore_id();
494 
495 	args = (struct worker_thread_args *) args_ptr;
496 	ring_in  = args->ring_in;
497 	ring_out = args->ring_out;
498 
499 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
500 							core_id);
501 
502 	while (!quit_signal) {
503 
504 		/* dequeue the mbufs from rx_to_workers ring */
505 		burst_size = rte_ring_dequeue_burst(ring_in,
506 				(void *)burst_buffer, MAX_PKTS_BURST, NULL);
507 		if (unlikely(burst_size == 0))
508 			continue;
509 
510 		wkr_stats[core_id].deq_pkts += burst_size;
511 
512 		/* just do some operation on mbuf */
513 		for (i = 0; i < burst_size;)
514 			burst_buffer[i++]->port ^= xor_val;
515 
516 		/* enqueue the modified mbufs to workers_to_tx ring */
517 		ret = rte_ring_enqueue_burst(ring_out, (void *)burst_buffer,
518 				burst_size, NULL);
519 		wkr_stats[core_id].enq_pkts += ret;
520 		if (unlikely(ret < burst_size)) {
521 			/* Return the mbufs to their respective pool, dropping packets */
522 			wkr_stats[core_id].enq_failed_pkts += burst_size - ret;
523 			pktmbuf_free_bulk(&burst_buffer[ret], burst_size - ret);
524 		}
525 	}
526 	return 0;
527 }
528 
529 /**
530  * Dequeue mbufs from the workers_to_tx ring and reorder them before
531  * transmitting.
532  */
533 static int
534 send_thread(struct send_thread_args *args)
535 {
536 	int ret;
537 	unsigned int i, dret;
538 	uint16_t nb_dq_mbufs;
539 	uint8_t outp;
540 	unsigned sent;
541 	struct rte_mbuf *mbufs[MAX_PKTS_BURST];
542 	struct rte_mbuf *rombufs[MAX_PKTS_BURST] = {NULL};
543 	static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
544 
545 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__, rte_lcore_id());
546 
547 	configure_tx_buffers(tx_buffer);
548 
549 	while (!quit_signal) {
550 
551 		/* deque the mbufs from workers_to_tx ring */
552 		nb_dq_mbufs = rte_ring_dequeue_burst(args->ring_in,
553 				(void *)mbufs, MAX_PKTS_BURST, NULL);
554 
555 		if (unlikely(nb_dq_mbufs == 0))
556 			continue;
557 
558 		app_stats.tx.dequeue_pkts += nb_dq_mbufs;
559 
560 		for (i = 0; i < nb_dq_mbufs; i++) {
561 			/* send dequeued mbufs for reordering */
562 			ret = rte_reorder_insert(args->buffer, mbufs[i]);
563 
564 			if (ret == -1 && rte_errno == ERANGE) {
565 				/* Too early pkts should be transmitted out directly */
566 				RTE_LOG_DP(DEBUG, REORDERAPP,
567 						"%s():Cannot reorder early packet "
568 						"direct enqueuing to TX\n", __func__);
569 				outp = mbufs[i]->port;
570 				if ((portmask & (1 << outp)) == 0) {
571 					rte_pktmbuf_free(mbufs[i]);
572 					continue;
573 				}
574 				if (rte_eth_tx_burst(outp, 0, (void *)mbufs[i], 1) != 1) {
575 					rte_pktmbuf_free(mbufs[i]);
576 					app_stats.tx.early_pkts_tx_failed_woro++;
577 				} else
578 					app_stats.tx.early_pkts_txtd_woro++;
579 			} else if (ret == -1 && rte_errno == ENOSPC) {
580 				/**
581 				 * Early pkts just outside of window should be dropped
582 				 */
583 				rte_pktmbuf_free(mbufs[i]);
584 			}
585 		}
586 
587 		/*
588 		 * drain MAX_PKTS_BURST of reordered
589 		 * mbufs for transmit
590 		 */
591 		dret = rte_reorder_drain(args->buffer, rombufs, MAX_PKTS_BURST);
592 		for (i = 0; i < dret; i++) {
593 
594 			struct rte_eth_dev_tx_buffer *outbuf;
595 			uint8_t outp1;
596 
597 			outp1 = rombufs[i]->port;
598 			/* skip ports that are not enabled */
599 			if ((portmask & (1 << outp1)) == 0) {
600 				rte_pktmbuf_free(rombufs[i]);
601 				continue;
602 			}
603 
604 			outbuf = tx_buffer[outp1];
605 			sent = rte_eth_tx_buffer(outp1, 0, outbuf, rombufs[i]);
606 			if (sent)
607 				app_stats.tx.ro_tx_pkts += sent;
608 		}
609 	}
610 
611 	free_tx_buffers(tx_buffer);
612 
613 	return 0;
614 }
615 
616 /**
617  * Dequeue mbufs from the workers_to_tx ring and transmit them
618  */
619 static int
620 tx_thread(struct rte_ring *ring_in)
621 {
622 	uint32_t i, dqnum;
623 	uint8_t outp;
624 	unsigned sent;
625 	struct rte_mbuf *mbufs[MAX_PKTS_BURST];
626 	struct rte_eth_dev_tx_buffer *outbuf;
627 	static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
628 
629 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
630 							rte_lcore_id());
631 
632 	configure_tx_buffers(tx_buffer);
633 
634 	while (!quit_signal) {
635 
636 		/* deque the mbufs from workers_to_tx ring */
637 		dqnum = rte_ring_dequeue_burst(ring_in,
638 				(void *)mbufs, MAX_PKTS_BURST, NULL);
639 
640 		if (unlikely(dqnum == 0))
641 			continue;
642 
643 		app_stats.tx.dequeue_pkts += dqnum;
644 
645 		for (i = 0; i < dqnum; i++) {
646 			outp = mbufs[i]->port;
647 			/* skip ports that are not enabled */
648 			if ((portmask & (1 << outp)) == 0) {
649 				rte_pktmbuf_free(mbufs[i]);
650 				continue;
651 			}
652 
653 			outbuf = tx_buffer[outp];
654 			sent = rte_eth_tx_buffer(outp, 0, outbuf, mbufs[i]);
655 			if (sent)
656 				app_stats.tx.ro_tx_pkts += sent;
657 		}
658 	}
659 
660 	return 0;
661 }
662 
663 int
664 main(int argc, char **argv)
665 {
666 	int ret;
667 	unsigned nb_ports;
668 	unsigned int lcore_id, last_lcore_id, main_lcore_id;
669 	uint16_t port_id;
670 	uint16_t nb_ports_available;
671 	struct worker_thread_args worker_args = {NULL, NULL};
672 	struct send_thread_args send_args = {NULL, NULL};
673 	struct rte_ring *rx_to_workers;
674 	struct rte_ring *workers_to_tx;
675 
676 	/* catch ctrl-c so we can print on exit */
677 	signal(SIGINT, int_handler);
678 
679 	/* Initialize EAL */
680 	ret = rte_eal_init(argc, argv);
681 	if (ret < 0)
682 		rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
683 
684 	argc -= ret;
685 	argv += ret;
686 
687 	/* Parse the application specific arguments */
688 	ret = parse_args(argc, argv);
689 	if (ret < 0)
690 		rte_exit(EXIT_FAILURE, "Invalid packet_ordering arguments\n");
691 
692 	/* Check if we have enought cores */
693 	if (rte_lcore_count() < 3)
694 		rte_exit(EXIT_FAILURE, "Error, This application needs at "
695 				"least 3 logical cores to run:\n"
696 				"1 lcore for packet RX\n"
697 				"1 lcore for packet TX\n"
698 				"and at least 1 lcore for worker threads\n");
699 
700 	nb_ports = rte_eth_dev_count_avail();
701 	if (nb_ports == 0)
702 		rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n");
703 	if (nb_ports != 1 && (nb_ports & 1))
704 		rte_exit(EXIT_FAILURE, "Error: number of ports must be even, except "
705 				"when using a single port\n");
706 
707 	mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL,
708 			MBUF_POOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
709 			rte_socket_id());
710 	if (mbuf_pool == NULL)
711 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
712 
713 	nb_ports_available = nb_ports;
714 
715 	/* initialize all ports */
716 	RTE_ETH_FOREACH_DEV(port_id) {
717 		/* skip ports that are not enabled */
718 		if ((portmask & (1 << port_id)) == 0) {
719 			printf("\nSkipping disabled port %d\n", port_id);
720 			nb_ports_available--;
721 			continue;
722 		}
723 		/* init port */
724 		printf("Initializing port %u... done\n", port_id);
725 
726 		if (configure_eth_port(port_id) != 0)
727 			rte_exit(EXIT_FAILURE, "Cannot initialize port %"PRIu8"\n",
728 					port_id);
729 	}
730 
731 	if (!nb_ports_available) {
732 		rte_exit(EXIT_FAILURE,
733 			"All available ports are disabled. Please set portmask.\n");
734 	}
735 
736 	/* Create rings for inter core communication */
737 	rx_to_workers = rte_ring_create("rx_to_workers", RING_SIZE, rte_socket_id(),
738 			RING_F_SP_ENQ);
739 	if (rx_to_workers == NULL)
740 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
741 
742 	workers_to_tx = rte_ring_create("workers_to_tx", RING_SIZE, rte_socket_id(),
743 			RING_F_SC_DEQ);
744 	if (workers_to_tx == NULL)
745 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
746 
747 	if (!disable_reorder) {
748 		send_args.buffer = rte_reorder_create("PKT_RO", rte_socket_id(),
749 				REORDER_BUFFER_SIZE);
750 		if (send_args.buffer == NULL)
751 			rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
752 	}
753 
754 	last_lcore_id   = get_last_lcore_id();
755 	main_lcore_id = rte_get_main_lcore();
756 
757 	worker_args.ring_in  = rx_to_workers;
758 	worker_args.ring_out = workers_to_tx;
759 
760 	/* Start worker_thread() on all the available worker cores but the last 1 */
761 	for (lcore_id = 0; lcore_id <= get_previous_lcore_id(last_lcore_id); lcore_id++)
762 		if (rte_lcore_is_enabled(lcore_id) && lcore_id != main_lcore_id)
763 			rte_eal_remote_launch(worker_thread, (void *)&worker_args,
764 					lcore_id);
765 
766 	if (disable_reorder) {
767 		/* Start tx_thread() on the last worker core */
768 		rte_eal_remote_launch((lcore_function_t *)tx_thread, workers_to_tx,
769 				last_lcore_id);
770 	} else {
771 		send_args.ring_in = workers_to_tx;
772 		/* Start send_thread() on the last worker core */
773 		rte_eal_remote_launch((lcore_function_t *)send_thread,
774 				(void *)&send_args, last_lcore_id);
775 	}
776 
777 	/* Start rx_thread() on the main core */
778 	rx_thread(rx_to_workers);
779 
780 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
781 		if (rte_eal_wait_lcore(lcore_id) < 0)
782 			return -1;
783 	}
784 
785 	print_stats();
786 	return 0;
787 }
788