xref: /dpdk/examples/packet_ordering/main.c (revision daa02b5cddbb8e11b31d41e2bf7bb1ae64dcae2f)
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 & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
298 		port_conf.txmode.offloads |=
299 			RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
300 	ret = rte_eth_dev_configure(port_id, rxRings, txRings, &port_conf);
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, RTE_ETHER_ADDR_BYTES(&addr));
339 
340 	ret = rte_eth_promiscuous_enable(port_id);
341 	if (ret != 0)
342 		return ret;
343 
344 	return 0;
345 }
346 
347 static void
348 print_stats(void)
349 {
350 	uint16_t i;
351 	struct rte_eth_stats eth_stats;
352 	unsigned int lcore_id, last_lcore_id, main_lcore_id, end_w_lcore_id;
353 
354 	last_lcore_id   = get_last_lcore_id();
355 	main_lcore_id = rte_get_main_lcore();
356 	end_w_lcore_id  = get_previous_lcore_id(last_lcore_id);
357 
358 	printf("\nRX thread stats:\n");
359 	printf(" - Pkts rxd:				%"PRIu64"\n",
360 						app_stats.rx.rx_pkts);
361 	printf(" - Pkts enqd to workers ring:		%"PRIu64"\n",
362 						app_stats.rx.enqueue_pkts);
363 
364 	for (lcore_id = 0; lcore_id <= end_w_lcore_id; lcore_id++) {
365 		if (insight_worker
366 			&& rte_lcore_is_enabled(lcore_id)
367 			&& lcore_id != main_lcore_id) {
368 			printf("\nWorker thread stats on core [%u]:\n",
369 					lcore_id);
370 			printf(" - Pkts deqd from workers ring:		%"PRIu64"\n",
371 					wkr_stats[lcore_id].deq_pkts);
372 			printf(" - Pkts enqd to tx ring:		%"PRIu64"\n",
373 					wkr_stats[lcore_id].enq_pkts);
374 			printf(" - Pkts enq to tx failed:		%"PRIu64"\n",
375 					wkr_stats[lcore_id].enq_failed_pkts);
376 		}
377 
378 		app_stats.wkr.dequeue_pkts += wkr_stats[lcore_id].deq_pkts;
379 		app_stats.wkr.enqueue_pkts += wkr_stats[lcore_id].enq_pkts;
380 		app_stats.wkr.enqueue_failed_pkts +=
381 			wkr_stats[lcore_id].enq_failed_pkts;
382 	}
383 
384 	printf("\nWorker thread stats:\n");
385 	printf(" - Pkts deqd from workers ring:		%"PRIu64"\n",
386 						app_stats.wkr.dequeue_pkts);
387 	printf(" - Pkts enqd to tx ring:		%"PRIu64"\n",
388 						app_stats.wkr.enqueue_pkts);
389 	printf(" - Pkts enq to tx failed:		%"PRIu64"\n",
390 						app_stats.wkr.enqueue_failed_pkts);
391 
392 	printf("\nTX stats:\n");
393 	printf(" - Pkts deqd from tx ring:		%"PRIu64"\n",
394 						app_stats.tx.dequeue_pkts);
395 	printf(" - Ro Pkts transmitted:			%"PRIu64"\n",
396 						app_stats.tx.ro_tx_pkts);
397 	printf(" - Ro Pkts tx failed:			%"PRIu64"\n",
398 						app_stats.tx.ro_tx_failed_pkts);
399 	printf(" - Pkts transmitted w/o reorder:	%"PRIu64"\n",
400 						app_stats.tx.early_pkts_txtd_woro);
401 	printf(" - Pkts tx failed w/o reorder:		%"PRIu64"\n",
402 						app_stats.tx.early_pkts_tx_failed_woro);
403 
404 	RTE_ETH_FOREACH_DEV(i) {
405 		rte_eth_stats_get(i, &eth_stats);
406 		printf("\nPort %u stats:\n", i);
407 		printf(" - Pkts in:   %"PRIu64"\n", eth_stats.ipackets);
408 		printf(" - Pkts out:  %"PRIu64"\n", eth_stats.opackets);
409 		printf(" - In Errs:   %"PRIu64"\n", eth_stats.ierrors);
410 		printf(" - Out Errs:  %"PRIu64"\n", eth_stats.oerrors);
411 		printf(" - Mbuf Errs: %"PRIu64"\n", eth_stats.rx_nombuf);
412 	}
413 }
414 
415 static void
416 int_handler(int sig_num)
417 {
418 	printf("Exiting on signal %d\n", sig_num);
419 	quit_signal = 1;
420 }
421 
422 /**
423  * This thread receives mbufs from the port and affects them an internal
424  * sequence number to keep track of their order of arrival through an
425  * mbuf structure.
426  * The mbufs are then passed to the worker threads via the rx_to_workers
427  * ring.
428  */
429 static int
430 rx_thread(struct rte_ring *ring_out)
431 {
432 	uint32_t seqn = 0;
433 	uint16_t i, ret = 0;
434 	uint16_t nb_rx_pkts;
435 	uint16_t port_id;
436 	struct rte_mbuf *pkts[MAX_PKTS_BURST];
437 
438 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
439 							rte_lcore_id());
440 
441 	while (!quit_signal) {
442 
443 		RTE_ETH_FOREACH_DEV(port_id) {
444 			if ((portmask & (1 << port_id)) != 0) {
445 
446 				/* receive packets */
447 				nb_rx_pkts = rte_eth_rx_burst(port_id, 0,
448 								pkts, MAX_PKTS_BURST);
449 				if (nb_rx_pkts == 0) {
450 					RTE_LOG_DP(DEBUG, REORDERAPP,
451 					"%s():Received zero packets\n",	__func__);
452 					continue;
453 				}
454 				app_stats.rx.rx_pkts += nb_rx_pkts;
455 
456 				/* mark sequence number */
457 				for (i = 0; i < nb_rx_pkts; )
458 					*rte_reorder_seqn(pkts[i++]) = seqn++;
459 
460 				/* enqueue to rx_to_workers ring */
461 				ret = rte_ring_enqueue_burst(ring_out,
462 						(void *)pkts, nb_rx_pkts, NULL);
463 				app_stats.rx.enqueue_pkts += ret;
464 				if (unlikely(ret < nb_rx_pkts)) {
465 					app_stats.rx.enqueue_failed_pkts +=
466 									(nb_rx_pkts-ret);
467 					pktmbuf_free_bulk(&pkts[ret], nb_rx_pkts - ret);
468 				}
469 			}
470 		}
471 	}
472 	return 0;
473 }
474 
475 /**
476  * This thread takes bursts of packets from the rx_to_workers ring and
477  * Changes the input port value to output port value. And feds it to
478  * workers_to_tx
479  */
480 static int
481 worker_thread(void *args_ptr)
482 {
483 	const uint16_t nb_ports = rte_eth_dev_count_avail();
484 	uint16_t i, ret = 0;
485 	uint16_t burst_size = 0;
486 	struct worker_thread_args *args;
487 	struct rte_mbuf *burst_buffer[MAX_PKTS_BURST] = { NULL };
488 	struct rte_ring *ring_in, *ring_out;
489 	const unsigned xor_val = (nb_ports > 1);
490 	unsigned int core_id = rte_lcore_id();
491 
492 	args = (struct worker_thread_args *) args_ptr;
493 	ring_in  = args->ring_in;
494 	ring_out = args->ring_out;
495 
496 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
497 							core_id);
498 
499 	while (!quit_signal) {
500 
501 		/* dequeue the mbufs from rx_to_workers ring */
502 		burst_size = rte_ring_dequeue_burst(ring_in,
503 				(void *)burst_buffer, MAX_PKTS_BURST, NULL);
504 		if (unlikely(burst_size == 0))
505 			continue;
506 
507 		wkr_stats[core_id].deq_pkts += burst_size;
508 
509 		/* just do some operation on mbuf */
510 		for (i = 0; i < burst_size;)
511 			burst_buffer[i++]->port ^= xor_val;
512 
513 		/* enqueue the modified mbufs to workers_to_tx ring */
514 		ret = rte_ring_enqueue_burst(ring_out, (void *)burst_buffer,
515 				burst_size, NULL);
516 		wkr_stats[core_id].enq_pkts += ret;
517 		if (unlikely(ret < burst_size)) {
518 			/* Return the mbufs to their respective pool, dropping packets */
519 			wkr_stats[core_id].enq_failed_pkts += burst_size - ret;
520 			pktmbuf_free_bulk(&burst_buffer[ret], burst_size - ret);
521 		}
522 	}
523 	return 0;
524 }
525 
526 /**
527  * Dequeue mbufs from the workers_to_tx ring and reorder them before
528  * transmitting.
529  */
530 static int
531 send_thread(struct send_thread_args *args)
532 {
533 	int ret;
534 	unsigned int i, dret;
535 	uint16_t nb_dq_mbufs;
536 	uint8_t outp;
537 	unsigned sent;
538 	struct rte_mbuf *mbufs[MAX_PKTS_BURST];
539 	struct rte_mbuf *rombufs[MAX_PKTS_BURST] = {NULL};
540 	static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
541 
542 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__, rte_lcore_id());
543 
544 	configure_tx_buffers(tx_buffer);
545 
546 	while (!quit_signal) {
547 
548 		/* deque the mbufs from workers_to_tx ring */
549 		nb_dq_mbufs = rte_ring_dequeue_burst(args->ring_in,
550 				(void *)mbufs, MAX_PKTS_BURST, NULL);
551 
552 		if (unlikely(nb_dq_mbufs == 0))
553 			continue;
554 
555 		app_stats.tx.dequeue_pkts += nb_dq_mbufs;
556 
557 		for (i = 0; i < nb_dq_mbufs; i++) {
558 			/* send dequeued mbufs for reordering */
559 			ret = rte_reorder_insert(args->buffer, mbufs[i]);
560 
561 			if (ret == -1 && rte_errno == ERANGE) {
562 				/* Too early pkts should be transmitted out directly */
563 				RTE_LOG_DP(DEBUG, REORDERAPP,
564 						"%s():Cannot reorder early packet "
565 						"direct enqueuing to TX\n", __func__);
566 				outp = mbufs[i]->port;
567 				if ((portmask & (1 << outp)) == 0) {
568 					rte_pktmbuf_free(mbufs[i]);
569 					continue;
570 				}
571 				if (rte_eth_tx_burst(outp, 0, (void *)mbufs[i], 1) != 1) {
572 					rte_pktmbuf_free(mbufs[i]);
573 					app_stats.tx.early_pkts_tx_failed_woro++;
574 				} else
575 					app_stats.tx.early_pkts_txtd_woro++;
576 			} else if (ret == -1 && rte_errno == ENOSPC) {
577 				/**
578 				 * Early pkts just outside of window should be dropped
579 				 */
580 				rte_pktmbuf_free(mbufs[i]);
581 			}
582 		}
583 
584 		/*
585 		 * drain MAX_PKTS_BURST of reordered
586 		 * mbufs for transmit
587 		 */
588 		dret = rte_reorder_drain(args->buffer, rombufs, MAX_PKTS_BURST);
589 		for (i = 0; i < dret; i++) {
590 
591 			struct rte_eth_dev_tx_buffer *outbuf;
592 			uint8_t outp1;
593 
594 			outp1 = rombufs[i]->port;
595 			/* skip ports that are not enabled */
596 			if ((portmask & (1 << outp1)) == 0) {
597 				rte_pktmbuf_free(rombufs[i]);
598 				continue;
599 			}
600 
601 			outbuf = tx_buffer[outp1];
602 			sent = rte_eth_tx_buffer(outp1, 0, outbuf, rombufs[i]);
603 			if (sent)
604 				app_stats.tx.ro_tx_pkts += sent;
605 		}
606 	}
607 
608 	free_tx_buffers(tx_buffer);
609 
610 	return 0;
611 }
612 
613 /**
614  * Dequeue mbufs from the workers_to_tx ring and transmit them
615  */
616 static int
617 tx_thread(struct rte_ring *ring_in)
618 {
619 	uint32_t i, dqnum;
620 	uint8_t outp;
621 	unsigned sent;
622 	struct rte_mbuf *mbufs[MAX_PKTS_BURST];
623 	struct rte_eth_dev_tx_buffer *outbuf;
624 	static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
625 
626 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
627 							rte_lcore_id());
628 
629 	configure_tx_buffers(tx_buffer);
630 
631 	while (!quit_signal) {
632 
633 		/* deque the mbufs from workers_to_tx ring */
634 		dqnum = rte_ring_dequeue_burst(ring_in,
635 				(void *)mbufs, MAX_PKTS_BURST, NULL);
636 
637 		if (unlikely(dqnum == 0))
638 			continue;
639 
640 		app_stats.tx.dequeue_pkts += dqnum;
641 
642 		for (i = 0; i < dqnum; i++) {
643 			outp = mbufs[i]->port;
644 			/* skip ports that are not enabled */
645 			if ((portmask & (1 << outp)) == 0) {
646 				rte_pktmbuf_free(mbufs[i]);
647 				continue;
648 			}
649 
650 			outbuf = tx_buffer[outp];
651 			sent = rte_eth_tx_buffer(outp, 0, outbuf, mbufs[i]);
652 			if (sent)
653 				app_stats.tx.ro_tx_pkts += sent;
654 		}
655 	}
656 
657 	return 0;
658 }
659 
660 int
661 main(int argc, char **argv)
662 {
663 	int ret;
664 	unsigned nb_ports;
665 	unsigned int lcore_id, last_lcore_id, main_lcore_id;
666 	uint16_t port_id;
667 	uint16_t nb_ports_available;
668 	struct worker_thread_args worker_args = {NULL, NULL};
669 	struct send_thread_args send_args = {NULL, NULL};
670 	struct rte_ring *rx_to_workers;
671 	struct rte_ring *workers_to_tx;
672 
673 	/* catch ctrl-c so we can print on exit */
674 	signal(SIGINT, int_handler);
675 
676 	/* Initialize EAL */
677 	ret = rte_eal_init(argc, argv);
678 	if (ret < 0)
679 		rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
680 
681 	argc -= ret;
682 	argv += ret;
683 
684 	/* Parse the application specific arguments */
685 	ret = parse_args(argc, argv);
686 	if (ret < 0)
687 		rte_exit(EXIT_FAILURE, "Invalid packet_ordering arguments\n");
688 
689 	/* Check if we have enought cores */
690 	if (rte_lcore_count() < 3)
691 		rte_exit(EXIT_FAILURE, "Error, This application needs at "
692 				"least 3 logical cores to run:\n"
693 				"1 lcore for packet RX\n"
694 				"1 lcore for packet TX\n"
695 				"and at least 1 lcore for worker threads\n");
696 
697 	nb_ports = rte_eth_dev_count_avail();
698 	if (nb_ports == 0)
699 		rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n");
700 	if (nb_ports != 1 && (nb_ports & 1))
701 		rte_exit(EXIT_FAILURE, "Error: number of ports must be even, except "
702 				"when using a single port\n");
703 
704 	mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL,
705 			MBUF_POOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
706 			rte_socket_id());
707 	if (mbuf_pool == NULL)
708 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
709 
710 	nb_ports_available = nb_ports;
711 
712 	/* initialize all ports */
713 	RTE_ETH_FOREACH_DEV(port_id) {
714 		/* skip ports that are not enabled */
715 		if ((portmask & (1 << port_id)) == 0) {
716 			printf("\nSkipping disabled port %d\n", port_id);
717 			nb_ports_available--;
718 			continue;
719 		}
720 		/* init port */
721 		printf("Initializing port %u... done\n", port_id);
722 
723 		if (configure_eth_port(port_id) != 0)
724 			rte_exit(EXIT_FAILURE, "Cannot initialize port %"PRIu8"\n",
725 					port_id);
726 	}
727 
728 	if (!nb_ports_available) {
729 		rte_exit(EXIT_FAILURE,
730 			"All available ports are disabled. Please set portmask.\n");
731 	}
732 
733 	/* Create rings for inter core communication */
734 	rx_to_workers = rte_ring_create("rx_to_workers", RING_SIZE, rte_socket_id(),
735 			RING_F_SP_ENQ);
736 	if (rx_to_workers == NULL)
737 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
738 
739 	workers_to_tx = rte_ring_create("workers_to_tx", RING_SIZE, rte_socket_id(),
740 			RING_F_SC_DEQ);
741 	if (workers_to_tx == NULL)
742 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
743 
744 	if (!disable_reorder) {
745 		send_args.buffer = rte_reorder_create("PKT_RO", rte_socket_id(),
746 				REORDER_BUFFER_SIZE);
747 		if (send_args.buffer == NULL)
748 			rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
749 	}
750 
751 	last_lcore_id   = get_last_lcore_id();
752 	main_lcore_id = rte_get_main_lcore();
753 
754 	worker_args.ring_in  = rx_to_workers;
755 	worker_args.ring_out = workers_to_tx;
756 
757 	/* Start worker_thread() on all the available worker cores but the last 1 */
758 	for (lcore_id = 0; lcore_id <= get_previous_lcore_id(last_lcore_id); lcore_id++)
759 		if (rte_lcore_is_enabled(lcore_id) && lcore_id != main_lcore_id)
760 			rte_eal_remote_launch(worker_thread, (void *)&worker_args,
761 					lcore_id);
762 
763 	if (disable_reorder) {
764 		/* Start tx_thread() on the last worker core */
765 		rte_eal_remote_launch((lcore_function_t *)tx_thread, workers_to_tx,
766 				last_lcore_id);
767 	} else {
768 		send_args.ring_in = workers_to_tx;
769 		/* Start send_thread() on the last worker core */
770 		rte_eal_remote_launch((lcore_function_t *)send_thread,
771 				(void *)&send_args, last_lcore_id);
772 	}
773 
774 	/* Start rx_thread() on the main core */
775 	rx_thread(rx_to_workers);
776 
777 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
778 		if (rte_eal_wait_lcore(lcore_id) < 0)
779 			return -1;
780 	}
781 
782 	print_stats();
783 
784 	/* clean up the EAL */
785 	rte_eal_cleanup();
786 
787 	return 0;
788 }
789