xref: /dpdk/examples/packet_ordering/main.c (revision 7e06c0de1952d3109a5b0c4779d7e7d8059c9d78)
13998e2a0SBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
23998e2a0SBruce Richardson  * Copyright(c) 2010-2016 Intel Corporation
3850f3733SSergio Gonzalez Monroy  */
4850f3733SSergio Gonzalez Monroy 
572b452c5SDmitry Kozlyuk #include <stdlib.h>
6850f3733SSergio Gonzalez Monroy #include <signal.h>
7850f3733SSergio Gonzalez Monroy #include <getopt.h>
87ba49dc7SQian Hao #include <stdbool.h>
9850f3733SSergio Gonzalez Monroy 
10850f3733SSergio Gonzalez Monroy #include <rte_eal.h>
11850f3733SSergio Gonzalez Monroy #include <rte_common.h>
12850f3733SSergio Gonzalez Monroy #include <rte_errno.h>
13850f3733SSergio Gonzalez Monroy #include <rte_ethdev.h>
14850f3733SSergio Gonzalez Monroy #include <rte_lcore.h>
15e2366e74STomasz Kulasek #include <rte_malloc.h>
16850f3733SSergio Gonzalez Monroy #include <rte_mbuf.h>
17850f3733SSergio Gonzalez Monroy #include <rte_mempool.h>
18850f3733SSergio Gonzalez Monroy #include <rte_ring.h>
19850f3733SSergio Gonzalez Monroy #include <rte_reorder.h>
20850f3733SSergio Gonzalez Monroy 
21867a6c66SKevin Laatz #define RX_DESC_PER_QUEUE 1024
22867a6c66SKevin Laatz #define TX_DESC_PER_QUEUE 1024
23850f3733SSergio Gonzalez Monroy 
24850f3733SSergio Gonzalez Monroy #define MAX_PKTS_BURST 32
25850f3733SSergio Gonzalez Monroy #define REORDER_BUFFER_SIZE 8192
26850f3733SSergio Gonzalez Monroy #define MBUF_PER_POOL 65535
27850f3733SSergio Gonzalez Monroy #define MBUF_POOL_CACHE_SIZE 250
28850f3733SSergio Gonzalez Monroy 
29850f3733SSergio Gonzalez Monroy #define RING_SIZE 16384
30850f3733SSergio Gonzalez Monroy 
31850f3733SSergio Gonzalez Monroy /* Macros for printing using RTE_LOG */
32850f3733SSergio Gonzalez Monroy #define RTE_LOGTYPE_REORDERAPP          RTE_LOGTYPE_USER1
33850f3733SSergio Gonzalez Monroy 
343bb3ebb5SIbtisam Tariq enum {
353bb3ebb5SIbtisam Tariq #define OPT_DISABLE_REORDER "disable-reorder"
363bb3ebb5SIbtisam Tariq 	OPT_DISABLE_REORDER_NUM = 256,
373bb3ebb5SIbtisam Tariq #define OPT_INSIGHT_WORKER  "insight-worker"
383bb3ebb5SIbtisam Tariq 	OPT_INSIGHT_WORKER_NUM,
393bb3ebb5SIbtisam Tariq };
403bb3ebb5SIbtisam Tariq 
41850f3733SSergio Gonzalez Monroy unsigned int portmask;
42850f3733SSergio Gonzalez Monroy unsigned int disable_reorder;
4301649330SPhil Yang unsigned int insight_worker;
44850f3733SSergio Gonzalez Monroy volatile uint8_t quit_signal;
45850f3733SSergio Gonzalez Monroy 
46850f3733SSergio Gonzalez Monroy static struct rte_mempool *mbuf_pool;
47850f3733SSergio Gonzalez Monroy 
48ab3ce1e0SFerruh Yigit static struct rte_eth_conf port_conf_default;
49850f3733SSergio Gonzalez Monroy 
50850f3733SSergio Gonzalez Monroy struct worker_thread_args {
51850f3733SSergio Gonzalez Monroy 	struct rte_ring *ring_in;
52850f3733SSergio Gonzalez Monroy 	struct rte_ring *ring_out;
53850f3733SSergio Gonzalez Monroy };
54850f3733SSergio Gonzalez Monroy 
556ebc23d8SSergio Gonzalez Monroy struct send_thread_args {
566ebc23d8SSergio Gonzalez Monroy 	struct rte_ring *ring_in;
576ebc23d8SSergio Gonzalez Monroy 	struct rte_reorder_buffer *buffer;
586ebc23d8SSergio Gonzalez Monroy };
596ebc23d8SSergio Gonzalez Monroy 
60850f3733SSergio Gonzalez Monroy volatile struct app_stats {
61*7e06c0deSTyler Retzlaff 	alignas(RTE_CACHE_LINE_SIZE) struct {
62850f3733SSergio Gonzalez Monroy 		uint64_t rx_pkts;
63850f3733SSergio Gonzalez Monroy 		uint64_t enqueue_pkts;
64850f3733SSergio Gonzalez Monroy 		uint64_t enqueue_failed_pkts;
65*7e06c0deSTyler Retzlaff 	} rx;
66850f3733SSergio Gonzalez Monroy 
67*7e06c0deSTyler Retzlaff 	alignas(RTE_CACHE_LINE_SIZE) struct {
68850f3733SSergio Gonzalez Monroy 		uint64_t dequeue_pkts;
69850f3733SSergio Gonzalez Monroy 		uint64_t enqueue_pkts;
70850f3733SSergio Gonzalez Monroy 		uint64_t enqueue_failed_pkts;
71*7e06c0deSTyler Retzlaff 	} wkr;
72850f3733SSergio Gonzalez Monroy 
73*7e06c0deSTyler Retzlaff 	alignas(RTE_CACHE_LINE_SIZE) struct {
74850f3733SSergio Gonzalez Monroy 		uint64_t dequeue_pkts;
75850f3733SSergio Gonzalez Monroy 		/* Too early pkts transmitted directly w/o reordering */
76850f3733SSergio Gonzalez Monroy 		uint64_t early_pkts_txtd_woro;
77850f3733SSergio Gonzalez Monroy 		/* Too early pkts failed from direct transmit */
78850f3733SSergio Gonzalez Monroy 		uint64_t early_pkts_tx_failed_woro;
79850f3733SSergio Gonzalez Monroy 		uint64_t ro_tx_pkts;
80850f3733SSergio Gonzalez Monroy 		uint64_t ro_tx_failed_pkts;
81*7e06c0deSTyler Retzlaff 	} tx;
82850f3733SSergio Gonzalez Monroy } app_stats;
83850f3733SSergio Gonzalez Monroy 
8401649330SPhil Yang /* per worker lcore stats */
85*7e06c0deSTyler Retzlaff struct __rte_cache_aligned wkr_stats_per {
8601649330SPhil Yang 		uint64_t deq_pkts;
8701649330SPhil Yang 		uint64_t enq_pkts;
8801649330SPhil Yang 		uint64_t enq_failed_pkts;
89*7e06c0deSTyler Retzlaff };
9001649330SPhil Yang 
9101649330SPhil Yang static struct wkr_stats_per wkr_stats[RTE_MAX_LCORE] = { {0} };
92850f3733SSergio Gonzalez Monroy /**
93850f3733SSergio Gonzalez Monroy  * Get the last enabled lcore ID
94850f3733SSergio Gonzalez Monroy  *
95850f3733SSergio Gonzalez Monroy  * @return
96850f3733SSergio Gonzalez Monroy  *   The last enabled lcore ID.
97850f3733SSergio Gonzalez Monroy  */
98850f3733SSergio Gonzalez Monroy static unsigned int
get_last_lcore_id(void)99850f3733SSergio Gonzalez Monroy get_last_lcore_id(void)
100850f3733SSergio Gonzalez Monroy {
101850f3733SSergio Gonzalez Monroy 	int i;
102850f3733SSergio Gonzalez Monroy 
103850f3733SSergio Gonzalez Monroy 	for (i = RTE_MAX_LCORE - 1; i >= 0; i--)
104850f3733SSergio Gonzalez Monroy 		if (rte_lcore_is_enabled(i))
105850f3733SSergio Gonzalez Monroy 			return i;
106850f3733SSergio Gonzalez Monroy 	return 0;
107850f3733SSergio Gonzalez Monroy }
108850f3733SSergio Gonzalez Monroy 
109850f3733SSergio Gonzalez Monroy /**
110850f3733SSergio Gonzalez Monroy  * Get the previous enabled lcore ID
111850f3733SSergio Gonzalez Monroy  * @param id
112850f3733SSergio Gonzalez Monroy  *  The current lcore ID
113850f3733SSergio Gonzalez Monroy  * @return
114850f3733SSergio Gonzalez Monroy  *   The previous enabled lcore ID or the current lcore
115850f3733SSergio Gonzalez Monroy  *   ID if it is the first available core.
116850f3733SSergio Gonzalez Monroy  */
117850f3733SSergio Gonzalez Monroy static unsigned int
get_previous_lcore_id(unsigned int id)118850f3733SSergio Gonzalez Monroy get_previous_lcore_id(unsigned int id)
119850f3733SSergio Gonzalez Monroy {
120850f3733SSergio Gonzalez Monroy 	int i;
121850f3733SSergio Gonzalez Monroy 
122850f3733SSergio Gonzalez Monroy 	for (i = id - 1; i >= 0; i--)
123850f3733SSergio Gonzalez Monroy 		if (rte_lcore_is_enabled(i))
124850f3733SSergio Gonzalez Monroy 			return i;
125850f3733SSergio Gonzalez Monroy 	return id;
126850f3733SSergio Gonzalez Monroy }
127850f3733SSergio Gonzalez Monroy 
128850f3733SSergio Gonzalez Monroy static inline void
pktmbuf_free_bulk(struct rte_mbuf * mbuf_table[],unsigned n)129850f3733SSergio Gonzalez Monroy pktmbuf_free_bulk(struct rte_mbuf *mbuf_table[], unsigned n)
130850f3733SSergio Gonzalez Monroy {
131850f3733SSergio Gonzalez Monroy 	unsigned int i;
132850f3733SSergio Gonzalez Monroy 
133850f3733SSergio Gonzalez Monroy 	for (i = 0; i < n; i++)
134850f3733SSergio Gonzalez Monroy 		rte_pktmbuf_free(mbuf_table[i]);
135850f3733SSergio Gonzalez Monroy }
136850f3733SSergio Gonzalez Monroy 
137850f3733SSergio Gonzalez Monroy /* display usage */
138850f3733SSergio Gonzalez Monroy static void
print_usage(const char * prgname)139850f3733SSergio Gonzalez Monroy print_usage(const char *prgname)
140850f3733SSergio Gonzalez Monroy {
141850f3733SSergio Gonzalez Monroy 	printf("%s [EAL options] -- -p PORTMASK\n"
142850f3733SSergio Gonzalez Monroy 			"  -p PORTMASK: hexadecimal bitmask of ports to configure\n",
143850f3733SSergio Gonzalez Monroy 			prgname);
144850f3733SSergio Gonzalez Monroy }
145850f3733SSergio Gonzalez Monroy 
146850f3733SSergio Gonzalez Monroy static int
parse_portmask(const char * portmask)147850f3733SSergio Gonzalez Monroy parse_portmask(const char *portmask)
148850f3733SSergio Gonzalez Monroy {
149850f3733SSergio Gonzalez Monroy 	unsigned long pm;
150850f3733SSergio Gonzalez Monroy 	char *end = NULL;
151850f3733SSergio Gonzalez Monroy 
152850f3733SSergio Gonzalez Monroy 	/* parse hexadecimal string */
153850f3733SSergio Gonzalez Monroy 	pm = strtoul(portmask, &end, 16);
154850f3733SSergio Gonzalez Monroy 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
155ce6b8c31SSarosh Arif 		return 0;
156850f3733SSergio Gonzalez Monroy 
157850f3733SSergio Gonzalez Monroy 	return pm;
158850f3733SSergio Gonzalez Monroy }
159850f3733SSergio Gonzalez Monroy 
160850f3733SSergio Gonzalez Monroy /* Parse the argument given in the command line of the application */
161850f3733SSergio Gonzalez Monroy static int
parse_args(int argc,char ** argv)162850f3733SSergio Gonzalez Monroy parse_args(int argc, char **argv)
163850f3733SSergio Gonzalez Monroy {
164850f3733SSergio Gonzalez Monroy 	int opt;
165850f3733SSergio Gonzalez Monroy 	int option_index;
166850f3733SSergio Gonzalez Monroy 	char **argvopt;
167850f3733SSergio Gonzalez Monroy 	char *prgname = argv[0];
168850f3733SSergio Gonzalez Monroy 	static struct option lgopts[] = {
1693bb3ebb5SIbtisam Tariq 		{OPT_DISABLE_REORDER, 0, NULL, OPT_DISABLE_REORDER_NUM},
1703bb3ebb5SIbtisam Tariq 		{OPT_INSIGHT_WORKER,  0, NULL, OPT_INSIGHT_WORKER_NUM },
171850f3733SSergio Gonzalez Monroy 		{NULL,                0, 0,    0                      }
172850f3733SSergio Gonzalez Monroy 	};
173850f3733SSergio Gonzalez Monroy 
174850f3733SSergio Gonzalez Monroy 	argvopt = argv;
175850f3733SSergio Gonzalez Monroy 
176850f3733SSergio Gonzalez Monroy 	while ((opt = getopt_long(argc, argvopt, "p:",
177850f3733SSergio Gonzalez Monroy 					lgopts, &option_index)) != EOF) {
178850f3733SSergio Gonzalez Monroy 		switch (opt) {
179850f3733SSergio Gonzalez Monroy 		/* portmask */
180850f3733SSergio Gonzalez Monroy 		case 'p':
181850f3733SSergio Gonzalez Monroy 			portmask = parse_portmask(optarg);
182850f3733SSergio Gonzalez Monroy 			if (portmask == 0) {
183850f3733SSergio Gonzalez Monroy 				printf("invalid portmask\n");
184850f3733SSergio Gonzalez Monroy 				print_usage(prgname);
185850f3733SSergio Gonzalez Monroy 				return -1;
186850f3733SSergio Gonzalez Monroy 			}
187850f3733SSergio Gonzalez Monroy 			break;
1883bb3ebb5SIbtisam Tariq 
189850f3733SSergio Gonzalez Monroy 		/* long options */
1903bb3ebb5SIbtisam Tariq 		case OPT_DISABLE_REORDER_NUM:
191850f3733SSergio Gonzalez Monroy 			printf("reorder disabled\n");
192850f3733SSergio Gonzalez Monroy 			disable_reorder = 1;
1933bb3ebb5SIbtisam Tariq 			break;
1943bb3ebb5SIbtisam Tariq 
1953bb3ebb5SIbtisam Tariq 		case OPT_INSIGHT_WORKER_NUM:
19601649330SPhil Yang 			printf("print all worker statistics\n");
19701649330SPhil Yang 			insight_worker = 1;
198850f3733SSergio Gonzalez Monroy 			break;
1993bb3ebb5SIbtisam Tariq 
200850f3733SSergio Gonzalez Monroy 		default:
201850f3733SSergio Gonzalez Monroy 			print_usage(prgname);
202850f3733SSergio Gonzalez Monroy 			return -1;
203850f3733SSergio Gonzalez Monroy 		}
204850f3733SSergio Gonzalez Monroy 	}
205850f3733SSergio Gonzalez Monroy 	if (optind <= 1) {
206850f3733SSergio Gonzalez Monroy 		print_usage(prgname);
207850f3733SSergio Gonzalez Monroy 		return -1;
208850f3733SSergio Gonzalez Monroy 	}
209850f3733SSergio Gonzalez Monroy 
210850f3733SSergio Gonzalez Monroy 	argv[optind-1] = prgname;
2119d5ca532SKeith Wiles 	optind = 1; /* reset getopt lib */
212850f3733SSergio Gonzalez Monroy 	return 0;
213850f3733SSergio Gonzalez Monroy }
214850f3733SSergio Gonzalez Monroy 
215e2366e74STomasz Kulasek /*
216e2366e74STomasz Kulasek  * Tx buffer error callback
217e2366e74STomasz Kulasek  */
218e2366e74STomasz Kulasek static void
flush_tx_error_callback(struct rte_mbuf ** unsent,uint16_t count,void * userdata __rte_unused)219e2366e74STomasz Kulasek flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
220e2366e74STomasz Kulasek 		void *userdata __rte_unused) {
221e2366e74STomasz Kulasek 
222e2366e74STomasz Kulasek 	/* free the mbufs which failed from transmit */
223e2366e74STomasz Kulasek 	app_stats.tx.ro_tx_failed_pkts += count;
2245d8f0bafSOlivier Matz 	RTE_LOG_DP(DEBUG, REORDERAPP, "%s:Packet loss with tx_burst\n", __func__);
225e2366e74STomasz Kulasek 	pktmbuf_free_bulk(unsent, count);
226e2366e74STomasz Kulasek 
227e2366e74STomasz Kulasek }
228e2366e74STomasz Kulasek 
229e2366e74STomasz Kulasek static inline int
free_tx_buffers(struct rte_eth_dev_tx_buffer * tx_buffer[])230e2366e74STomasz Kulasek free_tx_buffers(struct rte_eth_dev_tx_buffer *tx_buffer[]) {
2318728ccf3SThomas Monjalon 	uint16_t port_id;
232e2366e74STomasz Kulasek 
233e2366e74STomasz Kulasek 	/* initialize buffers for all ports */
2348728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(port_id) {
235e2366e74STomasz Kulasek 		/* skip ports that are not enabled */
236e2366e74STomasz Kulasek 		if ((portmask & (1 << port_id)) == 0)
237e2366e74STomasz Kulasek 			continue;
238e2366e74STomasz Kulasek 
239e2366e74STomasz Kulasek 		rte_free(tx_buffer[port_id]);
240e2366e74STomasz Kulasek 	}
241e2366e74STomasz Kulasek 	return 0;
242e2366e74STomasz Kulasek }
243e2366e74STomasz Kulasek 
244e2366e74STomasz Kulasek static inline int
configure_tx_buffers(struct rte_eth_dev_tx_buffer * tx_buffer[])245e2366e74STomasz Kulasek configure_tx_buffers(struct rte_eth_dev_tx_buffer *tx_buffer[])
246e2366e74STomasz Kulasek {
2478728ccf3SThomas Monjalon 	uint16_t port_id;
248e2366e74STomasz Kulasek 	int ret;
249e2366e74STomasz Kulasek 
250e2366e74STomasz Kulasek 	/* initialize buffers for all ports */
2518728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(port_id) {
252e2366e74STomasz Kulasek 		/* skip ports that are not enabled */
253e2366e74STomasz Kulasek 		if ((portmask & (1 << port_id)) == 0)
254e2366e74STomasz Kulasek 			continue;
255e2366e74STomasz Kulasek 
256e2366e74STomasz Kulasek 		/* Initialize TX buffers */
257e2366e74STomasz Kulasek 		tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
258e2366e74STomasz Kulasek 				RTE_ETH_TX_BUFFER_SIZE(MAX_PKTS_BURST), 0,
259e2366e74STomasz Kulasek 				rte_eth_dev_socket_id(port_id));
260e2366e74STomasz Kulasek 		if (tx_buffer[port_id] == NULL)
261e2366e74STomasz Kulasek 			rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
262f8244c63SZhiyong Yang 				 port_id);
263e2366e74STomasz Kulasek 
264e2366e74STomasz Kulasek 		rte_eth_tx_buffer_init(tx_buffer[port_id], MAX_PKTS_BURST);
265e2366e74STomasz Kulasek 
266e2366e74STomasz Kulasek 		ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
267e2366e74STomasz Kulasek 				flush_tx_error_callback, NULL);
268e2366e74STomasz Kulasek 		if (ret < 0)
269f8244c63SZhiyong Yang 			rte_exit(EXIT_FAILURE,
270f8244c63SZhiyong Yang 			"Cannot set error callback for tx buffer on port %u\n",
271f8244c63SZhiyong Yang 				 port_id);
272e2366e74STomasz Kulasek 	}
273e2366e74STomasz Kulasek 	return 0;
274e2366e74STomasz Kulasek }
275e2366e74STomasz Kulasek 
276850f3733SSergio Gonzalez Monroy static inline int
configure_eth_port(uint16_t port_id)277f8244c63SZhiyong Yang configure_eth_port(uint16_t port_id)
278850f3733SSergio Gonzalez Monroy {
2796d13ea8eSOlivier Matz 	struct rte_ether_addr addr;
280850f3733SSergio Gonzalez Monroy 	const uint16_t rxRings = 1, txRings = 1;
281850f3733SSergio Gonzalez Monroy 	int ret;
282850f3733SSergio Gonzalez Monroy 	uint16_t q;
28360efb44fSRoman Zhukov 	uint16_t nb_rxd = RX_DESC_PER_QUEUE;
28460efb44fSRoman Zhukov 	uint16_t nb_txd = TX_DESC_PER_QUEUE;
2856833f919SShahaf Shuler 	struct rte_eth_dev_info dev_info;
2866833f919SShahaf Shuler 	struct rte_eth_txconf txconf;
2876833f919SShahaf Shuler 	struct rte_eth_conf port_conf = port_conf_default;
288850f3733SSergio Gonzalez Monroy 
289a9dbe180SThomas Monjalon 	if (!rte_eth_dev_is_valid_port(port_id))
290850f3733SSergio Gonzalez Monroy 		return -1;
291850f3733SSergio Gonzalez Monroy 
292089e5ed7SIvan Ilchenko 	ret = rte_eth_dev_info_get(port_id, &dev_info);
293089e5ed7SIvan Ilchenko 	if (ret != 0) {
294089e5ed7SIvan Ilchenko 		printf("Error during getting device (port %u) info: %s\n",
295089e5ed7SIvan Ilchenko 				port_id, strerror(-ret));
296089e5ed7SIvan Ilchenko 		return ret;
297089e5ed7SIvan Ilchenko 	}
298089e5ed7SIvan Ilchenko 
299295968d1SFerruh Yigit 	if (dev_info.tx_offload_capa & RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE)
3006833f919SShahaf Shuler 		port_conf.txmode.offloads |=
301295968d1SFerruh Yigit 			RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE;
302c99f115bSDapeng Yu 	ret = rte_eth_dev_configure(port_id, rxRings, txRings, &port_conf);
303850f3733SSergio Gonzalez Monroy 	if (ret != 0)
304850f3733SSergio Gonzalez Monroy 		return ret;
305850f3733SSergio Gonzalez Monroy 
30660efb44fSRoman Zhukov 	ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_id, &nb_rxd, &nb_txd);
30760efb44fSRoman Zhukov 	if (ret != 0)
30860efb44fSRoman Zhukov 		return ret;
30960efb44fSRoman Zhukov 
310850f3733SSergio Gonzalez Monroy 	for (q = 0; q < rxRings; q++) {
31160efb44fSRoman Zhukov 		ret = rte_eth_rx_queue_setup(port_id, q, nb_rxd,
312850f3733SSergio Gonzalez Monroy 				rte_eth_dev_socket_id(port_id), NULL,
313850f3733SSergio Gonzalez Monroy 				mbuf_pool);
314850f3733SSergio Gonzalez Monroy 		if (ret < 0)
315850f3733SSergio Gonzalez Monroy 			return ret;
316850f3733SSergio Gonzalez Monroy 	}
317850f3733SSergio Gonzalez Monroy 
3186833f919SShahaf Shuler 	txconf = dev_info.default_txconf;
3196833f919SShahaf Shuler 	txconf.offloads = port_conf.txmode.offloads;
320850f3733SSergio Gonzalez Monroy 	for (q = 0; q < txRings; q++) {
32160efb44fSRoman Zhukov 		ret = rte_eth_tx_queue_setup(port_id, q, nb_txd,
3226833f919SShahaf Shuler 				rte_eth_dev_socket_id(port_id), &txconf);
323850f3733SSergio Gonzalez Monroy 		if (ret < 0)
324850f3733SSergio Gonzalez Monroy 			return ret;
325850f3733SSergio Gonzalez Monroy 	}
326850f3733SSergio Gonzalez Monroy 
327850f3733SSergio Gonzalez Monroy 	ret = rte_eth_dev_start(port_id);
328850f3733SSergio Gonzalez Monroy 	if (ret < 0)
329850f3733SSergio Gonzalez Monroy 		return ret;
330850f3733SSergio Gonzalez Monroy 
33170febdcfSIgor Romanov 	ret = rte_eth_macaddr_get(port_id, &addr);
33270febdcfSIgor Romanov 	if (ret != 0) {
33370febdcfSIgor Romanov 		printf("Failed to get MAC address (port %u): %s\n",
33470febdcfSIgor Romanov 				port_id, rte_strerror(-ret));
33570febdcfSIgor Romanov 		return ret;
33670febdcfSIgor Romanov 	}
33770febdcfSIgor Romanov 
338850f3733SSergio Gonzalez Monroy 	printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
339850f3733SSergio Gonzalez Monroy 			" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
340a7db3afcSAman Deep Singh 			port_id, RTE_ETHER_ADDR_BYTES(&addr));
341850f3733SSergio Gonzalez Monroy 
342f430bbceSIvan Ilchenko 	ret = rte_eth_promiscuous_enable(port_id);
343f430bbceSIvan Ilchenko 	if (ret != 0)
344f430bbceSIvan Ilchenko 		return ret;
345850f3733SSergio Gonzalez Monroy 
346850f3733SSergio Gonzalez Monroy 	return 0;
347850f3733SSergio Gonzalez Monroy }
348850f3733SSergio Gonzalez Monroy 
349850f3733SSergio Gonzalez Monroy static void
print_stats(void)350850f3733SSergio Gonzalez Monroy print_stats(void)
351850f3733SSergio Gonzalez Monroy {
3528728ccf3SThomas Monjalon 	uint16_t i;
353850f3733SSergio Gonzalez Monroy 	struct rte_eth_stats eth_stats;
354cb056611SStephen Hemminger 	unsigned int lcore_id, last_lcore_id, main_lcore_id, end_w_lcore_id;
35501649330SPhil Yang 
35601649330SPhil Yang 	last_lcore_id   = get_last_lcore_id();
357cb056611SStephen Hemminger 	main_lcore_id = rte_get_main_lcore();
35801649330SPhil Yang 	end_w_lcore_id  = get_previous_lcore_id(last_lcore_id);
359850f3733SSergio Gonzalez Monroy 
360850f3733SSergio Gonzalez Monroy 	printf("\nRX thread stats:\n");
361850f3733SSergio Gonzalez Monroy 	printf(" - Pkts rxd:				%"PRIu64"\n",
362850f3733SSergio Gonzalez Monroy 						app_stats.rx.rx_pkts);
363850f3733SSergio Gonzalez Monroy 	printf(" - Pkts enqd to workers ring:		%"PRIu64"\n",
364850f3733SSergio Gonzalez Monroy 						app_stats.rx.enqueue_pkts);
365850f3733SSergio Gonzalez Monroy 
36601649330SPhil Yang 	for (lcore_id = 0; lcore_id <= end_w_lcore_id; lcore_id++) {
36701649330SPhil Yang 		if (insight_worker
36801649330SPhil Yang 			&& rte_lcore_is_enabled(lcore_id)
369cb056611SStephen Hemminger 			&& lcore_id != main_lcore_id) {
37001649330SPhil Yang 			printf("\nWorker thread stats on core [%u]:\n",
37101649330SPhil Yang 					lcore_id);
37201649330SPhil Yang 			printf(" - Pkts deqd from workers ring:		%"PRIu64"\n",
37301649330SPhil Yang 					wkr_stats[lcore_id].deq_pkts);
37401649330SPhil Yang 			printf(" - Pkts enqd to tx ring:		%"PRIu64"\n",
37501649330SPhil Yang 					wkr_stats[lcore_id].enq_pkts);
37601649330SPhil Yang 			printf(" - Pkts enq to tx failed:		%"PRIu64"\n",
37701649330SPhil Yang 					wkr_stats[lcore_id].enq_failed_pkts);
37801649330SPhil Yang 		}
37901649330SPhil Yang 
38001649330SPhil Yang 		app_stats.wkr.dequeue_pkts += wkr_stats[lcore_id].deq_pkts;
38101649330SPhil Yang 		app_stats.wkr.enqueue_pkts += wkr_stats[lcore_id].enq_pkts;
38201649330SPhil Yang 		app_stats.wkr.enqueue_failed_pkts +=
38301649330SPhil Yang 			wkr_stats[lcore_id].enq_failed_pkts;
38401649330SPhil Yang 	}
38501649330SPhil Yang 
386850f3733SSergio Gonzalez Monroy 	printf("\nWorker thread stats:\n");
387850f3733SSergio Gonzalez Monroy 	printf(" - Pkts deqd from workers ring:		%"PRIu64"\n",
388850f3733SSergio Gonzalez Monroy 						app_stats.wkr.dequeue_pkts);
389850f3733SSergio Gonzalez Monroy 	printf(" - Pkts enqd to tx ring:		%"PRIu64"\n",
390850f3733SSergio Gonzalez Monroy 						app_stats.wkr.enqueue_pkts);
391850f3733SSergio Gonzalez Monroy 	printf(" - Pkts enq to tx failed:		%"PRIu64"\n",
392850f3733SSergio Gonzalez Monroy 						app_stats.wkr.enqueue_failed_pkts);
393850f3733SSergio Gonzalez Monroy 
394850f3733SSergio Gonzalez Monroy 	printf("\nTX stats:\n");
395850f3733SSergio Gonzalez Monroy 	printf(" - Pkts deqd from tx ring:		%"PRIu64"\n",
396850f3733SSergio Gonzalez Monroy 						app_stats.tx.dequeue_pkts);
397850f3733SSergio Gonzalez Monroy 	printf(" - Ro Pkts transmitted:			%"PRIu64"\n",
398850f3733SSergio Gonzalez Monroy 						app_stats.tx.ro_tx_pkts);
399850f3733SSergio Gonzalez Monroy 	printf(" - Ro Pkts tx failed:			%"PRIu64"\n",
400850f3733SSergio Gonzalez Monroy 						app_stats.tx.ro_tx_failed_pkts);
401850f3733SSergio Gonzalez Monroy 	printf(" - Pkts transmitted w/o reorder:	%"PRIu64"\n",
402850f3733SSergio Gonzalez Monroy 						app_stats.tx.early_pkts_txtd_woro);
403850f3733SSergio Gonzalez Monroy 	printf(" - Pkts tx failed w/o reorder:		%"PRIu64"\n",
404850f3733SSergio Gonzalez Monroy 						app_stats.tx.early_pkts_tx_failed_woro);
405850f3733SSergio Gonzalez Monroy 
4068728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(i) {
407850f3733SSergio Gonzalez Monroy 		rte_eth_stats_get(i, &eth_stats);
408850f3733SSergio Gonzalez Monroy 		printf("\nPort %u stats:\n", i);
409850f3733SSergio Gonzalez Monroy 		printf(" - Pkts in:   %"PRIu64"\n", eth_stats.ipackets);
410850f3733SSergio Gonzalez Monroy 		printf(" - Pkts out:  %"PRIu64"\n", eth_stats.opackets);
411850f3733SSergio Gonzalez Monroy 		printf(" - In Errs:   %"PRIu64"\n", eth_stats.ierrors);
412850f3733SSergio Gonzalez Monroy 		printf(" - Out Errs:  %"PRIu64"\n", eth_stats.oerrors);
413850f3733SSergio Gonzalez Monroy 		printf(" - Mbuf Errs: %"PRIu64"\n", eth_stats.rx_nombuf);
414850f3733SSergio Gonzalez Monroy 	}
415850f3733SSergio Gonzalez Monroy }
416850f3733SSergio Gonzalez Monroy 
417850f3733SSergio Gonzalez Monroy static void
int_handler(int sig_num)418850f3733SSergio Gonzalez Monroy int_handler(int sig_num)
419850f3733SSergio Gonzalez Monroy {
420850f3733SSergio Gonzalez Monroy 	printf("Exiting on signal %d\n", sig_num);
421850f3733SSergio Gonzalez Monroy 	quit_signal = 1;
422850f3733SSergio Gonzalez Monroy }
423850f3733SSergio Gonzalez Monroy 
424850f3733SSergio Gonzalez Monroy /**
425850f3733SSergio Gonzalez Monroy  * This thread receives mbufs from the port and affects them an internal
426850f3733SSergio Gonzalez Monroy  * sequence number to keep track of their order of arrival through an
427850f3733SSergio Gonzalez Monroy  * mbuf structure.
428850f3733SSergio Gonzalez Monroy  * The mbufs are then passed to the worker threads via the rx_to_workers
429850f3733SSergio Gonzalez Monroy  * ring.
430850f3733SSergio Gonzalez Monroy  */
4317ba49dc7SQian Hao static __rte_always_inline int
rx_thread(struct rte_ring * ring_out,bool disable_reorder_flag)4327ba49dc7SQian Hao rx_thread(struct rte_ring *ring_out, bool disable_reorder_flag)
433850f3733SSergio Gonzalez Monroy {
434850f3733SSergio Gonzalez Monroy 	uint32_t seqn = 0;
435850f3733SSergio Gonzalez Monroy 	uint16_t i, ret = 0;
436850f3733SSergio Gonzalez Monroy 	uint16_t nb_rx_pkts;
437f8244c63SZhiyong Yang 	uint16_t port_id;
438850f3733SSergio Gonzalez Monroy 	struct rte_mbuf *pkts[MAX_PKTS_BURST];
439850f3733SSergio Gonzalez Monroy 
440850f3733SSergio Gonzalez Monroy 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
441850f3733SSergio Gonzalez Monroy 							rte_lcore_id());
442850f3733SSergio Gonzalez Monroy 
443850f3733SSergio Gonzalez Monroy 	while (!quit_signal) {
444850f3733SSergio Gonzalez Monroy 
4458728ccf3SThomas Monjalon 		RTE_ETH_FOREACH_DEV(port_id) {
446850f3733SSergio Gonzalez Monroy 			if ((portmask & (1 << port_id)) != 0) {
447850f3733SSergio Gonzalez Monroy 
448850f3733SSergio Gonzalez Monroy 				/* receive packets */
449850f3733SSergio Gonzalez Monroy 				nb_rx_pkts = rte_eth_rx_burst(port_id, 0,
450850f3733SSergio Gonzalez Monroy 								pkts, MAX_PKTS_BURST);
451850f3733SSergio Gonzalez Monroy 				if (nb_rx_pkts == 0) {
4525d8f0bafSOlivier Matz 					RTE_LOG_DP(DEBUG, REORDERAPP,
453850f3733SSergio Gonzalez Monroy 					"%s():Received zero packets\n",	__func__);
454850f3733SSergio Gonzalez Monroy 					continue;
455850f3733SSergio Gonzalez Monroy 				}
456850f3733SSergio Gonzalez Monroy 				app_stats.rx.rx_pkts += nb_rx_pkts;
457850f3733SSergio Gonzalez Monroy 
4587ba49dc7SQian Hao 				/* mark sequence number if reorder is enabled */
4597ba49dc7SQian Hao 				if (!disable_reorder_flag) {
460850f3733SSergio Gonzalez Monroy 					for (i = 0; i < nb_rx_pkts;)
46101f34966SDavid Marchand 						*rte_reorder_seqn(pkts[i++]) = seqn++;
4627ba49dc7SQian Hao 				}
463850f3733SSergio Gonzalez Monroy 
464850f3733SSergio Gonzalez Monroy 				/* enqueue to rx_to_workers ring */
46514fbffb0SBruce Richardson 				ret = rte_ring_enqueue_burst(ring_out,
46614fbffb0SBruce Richardson 						(void *)pkts, nb_rx_pkts, NULL);
467850f3733SSergio Gonzalez Monroy 				app_stats.rx.enqueue_pkts += ret;
468850f3733SSergio Gonzalez Monroy 				if (unlikely(ret < nb_rx_pkts)) {
469850f3733SSergio Gonzalez Monroy 					app_stats.rx.enqueue_failed_pkts +=
470850f3733SSergio Gonzalez Monroy 									(nb_rx_pkts-ret);
471850f3733SSergio Gonzalez Monroy 					pktmbuf_free_bulk(&pkts[ret], nb_rx_pkts - ret);
472850f3733SSergio Gonzalez Monroy 				}
473850f3733SSergio Gonzalez Monroy 			}
474850f3733SSergio Gonzalez Monroy 		}
475850f3733SSergio Gonzalez Monroy 	}
476850f3733SSergio Gonzalez Monroy 	return 0;
477850f3733SSergio Gonzalez Monroy }
478850f3733SSergio Gonzalez Monroy 
4797ba49dc7SQian Hao static __rte_noinline int
rx_thread_reorder(struct rte_ring * ring_out)4807ba49dc7SQian Hao rx_thread_reorder(struct rte_ring *ring_out)
4817ba49dc7SQian Hao {
4827ba49dc7SQian Hao 	return rx_thread(ring_out, false);
4837ba49dc7SQian Hao }
4847ba49dc7SQian Hao 
4857ba49dc7SQian Hao static __rte_noinline int
rx_thread_reorder_disabled(struct rte_ring * ring_out)4867ba49dc7SQian Hao rx_thread_reorder_disabled(struct rte_ring *ring_out)
4877ba49dc7SQian Hao {
4887ba49dc7SQian Hao 	return rx_thread(ring_out, true);
4897ba49dc7SQian Hao }
4907ba49dc7SQian Hao 
491850f3733SSergio Gonzalez Monroy /**
492850f3733SSergio Gonzalez Monroy  * This thread takes bursts of packets from the rx_to_workers ring and
493850f3733SSergio Gonzalez Monroy  * Changes the input port value to output port value. And feds it to
494850f3733SSergio Gonzalez Monroy  * workers_to_tx
495850f3733SSergio Gonzalez Monroy  */
496850f3733SSergio Gonzalez Monroy static int
worker_thread(void * args_ptr)497850f3733SSergio Gonzalez Monroy worker_thread(void *args_ptr)
498850f3733SSergio Gonzalez Monroy {
499d9a42a69SThomas Monjalon 	const uint16_t nb_ports = rte_eth_dev_count_avail();
500850f3733SSergio Gonzalez Monroy 	uint16_t i, ret = 0;
501850f3733SSergio Gonzalez Monroy 	uint16_t burst_size = 0;
502850f3733SSergio Gonzalez Monroy 	struct worker_thread_args *args;
503850f3733SSergio Gonzalez Monroy 	struct rte_mbuf *burst_buffer[MAX_PKTS_BURST] = { NULL };
504850f3733SSergio Gonzalez Monroy 	struct rte_ring *ring_in, *ring_out;
505850f3733SSergio Gonzalez Monroy 	const unsigned xor_val = (nb_ports > 1);
50601649330SPhil Yang 	unsigned int core_id = rte_lcore_id();
507850f3733SSergio Gonzalez Monroy 
508850f3733SSergio Gonzalez Monroy 	args = (struct worker_thread_args *) args_ptr;
509850f3733SSergio Gonzalez Monroy 	ring_in  = args->ring_in;
510850f3733SSergio Gonzalez Monroy 	ring_out = args->ring_out;
511850f3733SSergio Gonzalez Monroy 
512850f3733SSergio Gonzalez Monroy 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
51301649330SPhil Yang 							core_id);
514850f3733SSergio Gonzalez Monroy 
515850f3733SSergio Gonzalez Monroy 	while (!quit_signal) {
516850f3733SSergio Gonzalez Monroy 
517850f3733SSergio Gonzalez Monroy 		/* dequeue the mbufs from rx_to_workers ring */
518850f3733SSergio Gonzalez Monroy 		burst_size = rte_ring_dequeue_burst(ring_in,
519ecaed092SBruce Richardson 				(void *)burst_buffer, MAX_PKTS_BURST, NULL);
520850f3733SSergio Gonzalez Monroy 		if (unlikely(burst_size == 0))
521850f3733SSergio Gonzalez Monroy 			continue;
522850f3733SSergio Gonzalez Monroy 
52301649330SPhil Yang 		wkr_stats[core_id].deq_pkts += burst_size;
524850f3733SSergio Gonzalez Monroy 
525850f3733SSergio Gonzalez Monroy 		/* just do some operation on mbuf */
526850f3733SSergio Gonzalez Monroy 		for (i = 0; i < burst_size;)
527850f3733SSergio Gonzalez Monroy 			burst_buffer[i++]->port ^= xor_val;
528850f3733SSergio Gonzalez Monroy 
529850f3733SSergio Gonzalez Monroy 		/* enqueue the modified mbufs to workers_to_tx ring */
53014fbffb0SBruce Richardson 		ret = rte_ring_enqueue_burst(ring_out, (void *)burst_buffer,
53114fbffb0SBruce Richardson 				burst_size, NULL);
53201649330SPhil Yang 		wkr_stats[core_id].enq_pkts += ret;
533850f3733SSergio Gonzalez Monroy 		if (unlikely(ret < burst_size)) {
534850f3733SSergio Gonzalez Monroy 			/* Return the mbufs to their respective pool, dropping packets */
53501649330SPhil Yang 			wkr_stats[core_id].enq_failed_pkts += burst_size - ret;
536850f3733SSergio Gonzalez Monroy 			pktmbuf_free_bulk(&burst_buffer[ret], burst_size - ret);
537850f3733SSergio Gonzalez Monroy 		}
538850f3733SSergio Gonzalez Monroy 	}
539850f3733SSergio Gonzalez Monroy 	return 0;
540850f3733SSergio Gonzalez Monroy }
541850f3733SSergio Gonzalez Monroy 
542850f3733SSergio Gonzalez Monroy /**
543850f3733SSergio Gonzalez Monroy  * Dequeue mbufs from the workers_to_tx ring and reorder them before
544850f3733SSergio Gonzalez Monroy  * transmitting.
545850f3733SSergio Gonzalez Monroy  */
546850f3733SSergio Gonzalez Monroy static int
send_thread(struct send_thread_args * args)5476ebc23d8SSergio Gonzalez Monroy send_thread(struct send_thread_args *args)
548850f3733SSergio Gonzalez Monroy {
549850f3733SSergio Gonzalez Monroy 	int ret;
550850f3733SSergio Gonzalez Monroy 	unsigned int i, dret;
551850f3733SSergio Gonzalez Monroy 	uint16_t nb_dq_mbufs;
552850f3733SSergio Gonzalez Monroy 	uint8_t outp;
553e2366e74STomasz Kulasek 	unsigned sent;
554850f3733SSergio Gonzalez Monroy 	struct rte_mbuf *mbufs[MAX_PKTS_BURST];
555850f3733SSergio Gonzalez Monroy 	struct rte_mbuf *rombufs[MAX_PKTS_BURST] = {NULL};
556e2366e74STomasz Kulasek 	static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
557850f3733SSergio Gonzalez Monroy 
5586ebc23d8SSergio Gonzalez Monroy 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__, rte_lcore_id());
5596ebc23d8SSergio Gonzalez Monroy 
560e2366e74STomasz Kulasek 	configure_tx_buffers(tx_buffer);
561e2366e74STomasz Kulasek 
562850f3733SSergio Gonzalez Monroy 	while (!quit_signal) {
563850f3733SSergio Gonzalez Monroy 
564850f3733SSergio Gonzalez Monroy 		/* deque the mbufs from workers_to_tx ring */
5656ebc23d8SSergio Gonzalez Monroy 		nb_dq_mbufs = rte_ring_dequeue_burst(args->ring_in,
566ecaed092SBruce Richardson 				(void *)mbufs, MAX_PKTS_BURST, NULL);
567850f3733SSergio Gonzalez Monroy 
568850f3733SSergio Gonzalez Monroy 		if (unlikely(nb_dq_mbufs == 0))
569850f3733SSergio Gonzalez Monroy 			continue;
570850f3733SSergio Gonzalez Monroy 
571850f3733SSergio Gonzalez Monroy 		app_stats.tx.dequeue_pkts += nb_dq_mbufs;
572850f3733SSergio Gonzalez Monroy 
573850f3733SSergio Gonzalez Monroy 		for (i = 0; i < nb_dq_mbufs; i++) {
574850f3733SSergio Gonzalez Monroy 			/* send dequeued mbufs for reordering */
5756ebc23d8SSergio Gonzalez Monroy 			ret = rte_reorder_insert(args->buffer, mbufs[i]);
576850f3733SSergio Gonzalez Monroy 
577850f3733SSergio Gonzalez Monroy 			if (ret == -1 && rte_errno == ERANGE) {
578850f3733SSergio Gonzalez Monroy 				/* Too early pkts should be transmitted out directly */
5795d8f0bafSOlivier Matz 				RTE_LOG_DP(DEBUG, REORDERAPP,
5801f49ec15SThomas Monjalon 						"%s():Cannot reorder early packet "
581850f3733SSergio Gonzalez Monroy 						"direct enqueuing to TX\n", __func__);
582850f3733SSergio Gonzalez Monroy 				outp = mbufs[i]->port;
583850f3733SSergio Gonzalez Monroy 				if ((portmask & (1 << outp)) == 0) {
584850f3733SSergio Gonzalez Monroy 					rte_pktmbuf_free(mbufs[i]);
585850f3733SSergio Gonzalez Monroy 					continue;
586850f3733SSergio Gonzalez Monroy 				}
587850f3733SSergio Gonzalez Monroy 				if (rte_eth_tx_burst(outp, 0, (void *)mbufs[i], 1) != 1) {
588850f3733SSergio Gonzalez Monroy 					rte_pktmbuf_free(mbufs[i]);
589850f3733SSergio Gonzalez Monroy 					app_stats.tx.early_pkts_tx_failed_woro++;
590850f3733SSergio Gonzalez Monroy 				} else
591850f3733SSergio Gonzalez Monroy 					app_stats.tx.early_pkts_txtd_woro++;
592850f3733SSergio Gonzalez Monroy 			} else if (ret == -1 && rte_errno == ENOSPC) {
593850f3733SSergio Gonzalez Monroy 				/**
594850f3733SSergio Gonzalez Monroy 				 * Early pkts just outside of window should be dropped
595850f3733SSergio Gonzalez Monroy 				 */
596850f3733SSergio Gonzalez Monroy 				rte_pktmbuf_free(mbufs[i]);
597850f3733SSergio Gonzalez Monroy 			}
598850f3733SSergio Gonzalez Monroy 		}
599850f3733SSergio Gonzalez Monroy 
600850f3733SSergio Gonzalez Monroy 		/*
601850f3733SSergio Gonzalez Monroy 		 * drain MAX_PKTS_BURST of reordered
602850f3733SSergio Gonzalez Monroy 		 * mbufs for transmit
603850f3733SSergio Gonzalez Monroy 		 */
6046ebc23d8SSergio Gonzalez Monroy 		dret = rte_reorder_drain(args->buffer, rombufs, MAX_PKTS_BURST);
605850f3733SSergio Gonzalez Monroy 		for (i = 0; i < dret; i++) {
606850f3733SSergio Gonzalez Monroy 
607e2366e74STomasz Kulasek 			struct rte_eth_dev_tx_buffer *outbuf;
608850f3733SSergio Gonzalez Monroy 			uint8_t outp1;
609850f3733SSergio Gonzalez Monroy 
610850f3733SSergio Gonzalez Monroy 			outp1 = rombufs[i]->port;
611850f3733SSergio Gonzalez Monroy 			/* skip ports that are not enabled */
612850f3733SSergio Gonzalez Monroy 			if ((portmask & (1 << outp1)) == 0) {
613850f3733SSergio Gonzalez Monroy 				rte_pktmbuf_free(rombufs[i]);
614850f3733SSergio Gonzalez Monroy 				continue;
615850f3733SSergio Gonzalez Monroy 			}
616850f3733SSergio Gonzalez Monroy 
617e2366e74STomasz Kulasek 			outbuf = tx_buffer[outp1];
618e2366e74STomasz Kulasek 			sent = rte_eth_tx_buffer(outp1, 0, outbuf, rombufs[i]);
619e2366e74STomasz Kulasek 			if (sent)
620e2366e74STomasz Kulasek 				app_stats.tx.ro_tx_pkts += sent;
621850f3733SSergio Gonzalez Monroy 		}
622850f3733SSergio Gonzalez Monroy 	}
623e2366e74STomasz Kulasek 
624e2366e74STomasz Kulasek 	free_tx_buffers(tx_buffer);
625e2366e74STomasz Kulasek 
626850f3733SSergio Gonzalez Monroy 	return 0;
627850f3733SSergio Gonzalez Monroy }
628850f3733SSergio Gonzalez Monroy 
629850f3733SSergio Gonzalez Monroy /**
630850f3733SSergio Gonzalez Monroy  * Dequeue mbufs from the workers_to_tx ring and transmit them
631850f3733SSergio Gonzalez Monroy  */
632850f3733SSergio Gonzalez Monroy static int
tx_thread(struct rte_ring * ring_in)633850f3733SSergio Gonzalez Monroy tx_thread(struct rte_ring *ring_in)
634850f3733SSergio Gonzalez Monroy {
635850f3733SSergio Gonzalez Monroy 	uint32_t i, dqnum;
636850f3733SSergio Gonzalez Monroy 	uint8_t outp;
637e2366e74STomasz Kulasek 	unsigned sent;
638850f3733SSergio Gonzalez Monroy 	struct rte_mbuf *mbufs[MAX_PKTS_BURST];
639e2366e74STomasz Kulasek 	struct rte_eth_dev_tx_buffer *outbuf;
640e2366e74STomasz Kulasek 	static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
641850f3733SSergio Gonzalez Monroy 
642850f3733SSergio Gonzalez Monroy 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
643850f3733SSergio Gonzalez Monroy 							rte_lcore_id());
644e2366e74STomasz Kulasek 
645e2366e74STomasz Kulasek 	configure_tx_buffers(tx_buffer);
646e2366e74STomasz Kulasek 
647850f3733SSergio Gonzalez Monroy 	while (!quit_signal) {
648850f3733SSergio Gonzalez Monroy 
649850f3733SSergio Gonzalez Monroy 		/* deque the mbufs from workers_to_tx ring */
650850f3733SSergio Gonzalez Monroy 		dqnum = rte_ring_dequeue_burst(ring_in,
651ecaed092SBruce Richardson 				(void *)mbufs, MAX_PKTS_BURST, NULL);
652850f3733SSergio Gonzalez Monroy 
653850f3733SSergio Gonzalez Monroy 		if (unlikely(dqnum == 0))
654850f3733SSergio Gonzalez Monroy 			continue;
655850f3733SSergio Gonzalez Monroy 
656850f3733SSergio Gonzalez Monroy 		app_stats.tx.dequeue_pkts += dqnum;
657850f3733SSergio Gonzalez Monroy 
658850f3733SSergio Gonzalez Monroy 		for (i = 0; i < dqnum; i++) {
659850f3733SSergio Gonzalez Monroy 			outp = mbufs[i]->port;
660850f3733SSergio Gonzalez Monroy 			/* skip ports that are not enabled */
661850f3733SSergio Gonzalez Monroy 			if ((portmask & (1 << outp)) == 0) {
662850f3733SSergio Gonzalez Monroy 				rte_pktmbuf_free(mbufs[i]);
663850f3733SSergio Gonzalez Monroy 				continue;
664850f3733SSergio Gonzalez Monroy 			}
665850f3733SSergio Gonzalez Monroy 
666e2366e74STomasz Kulasek 			outbuf = tx_buffer[outp];
667e2366e74STomasz Kulasek 			sent = rte_eth_tx_buffer(outp, 0, outbuf, mbufs[i]);
668e2366e74STomasz Kulasek 			if (sent)
669e2366e74STomasz Kulasek 				app_stats.tx.ro_tx_pkts += sent;
670850f3733SSergio Gonzalez Monroy 		}
671850f3733SSergio Gonzalez Monroy 	}
672850f3733SSergio Gonzalez Monroy 
673850f3733SSergio Gonzalez Monroy 	return 0;
674850f3733SSergio Gonzalez Monroy }
675850f3733SSergio Gonzalez Monroy 
676850f3733SSergio Gonzalez Monroy int
main(int argc,char ** argv)677850f3733SSergio Gonzalez Monroy main(int argc, char **argv)
678850f3733SSergio Gonzalez Monroy {
679850f3733SSergio Gonzalez Monroy 	int ret;
680850f3733SSergio Gonzalez Monroy 	unsigned nb_ports;
681cb056611SStephen Hemminger 	unsigned int lcore_id, last_lcore_id, main_lcore_id;
682f8244c63SZhiyong Yang 	uint16_t port_id;
683f8244c63SZhiyong Yang 	uint16_t nb_ports_available;
684850f3733SSergio Gonzalez Monroy 	struct worker_thread_args worker_args = {NULL, NULL};
6856ebc23d8SSergio Gonzalez Monroy 	struct send_thread_args send_args = {NULL, NULL};
686850f3733SSergio Gonzalez Monroy 	struct rte_ring *rx_to_workers;
687850f3733SSergio Gonzalez Monroy 	struct rte_ring *workers_to_tx;
688850f3733SSergio Gonzalez Monroy 
689850f3733SSergio Gonzalez Monroy 	/* catch ctrl-c so we can print on exit */
690850f3733SSergio Gonzalez Monroy 	signal(SIGINT, int_handler);
691850f3733SSergio Gonzalez Monroy 
692850f3733SSergio Gonzalez Monroy 	/* Initialize EAL */
693850f3733SSergio Gonzalez Monroy 	ret = rte_eal_init(argc, argv);
694850f3733SSergio Gonzalez Monroy 	if (ret < 0)
695d74fab8eSSarosh Arif 		rte_exit(EXIT_FAILURE, "Invalid EAL arguments\n");
696850f3733SSergio Gonzalez Monroy 
697850f3733SSergio Gonzalez Monroy 	argc -= ret;
698850f3733SSergio Gonzalez Monroy 	argv += ret;
699850f3733SSergio Gonzalez Monroy 
700850f3733SSergio Gonzalez Monroy 	/* Parse the application specific arguments */
701850f3733SSergio Gonzalez Monroy 	ret = parse_args(argc, argv);
702850f3733SSergio Gonzalez Monroy 	if (ret < 0)
703d74fab8eSSarosh Arif 		rte_exit(EXIT_FAILURE, "Invalid packet_ordering arguments\n");
704850f3733SSergio Gonzalez Monroy 
7057be78d02SJosh Soref 	/* Check if we have enough cores */
706850f3733SSergio Gonzalez Monroy 	if (rte_lcore_count() < 3)
707850f3733SSergio Gonzalez Monroy 		rte_exit(EXIT_FAILURE, "Error, This application needs at "
708850f3733SSergio Gonzalez Monroy 				"least 3 logical cores to run:\n"
709850f3733SSergio Gonzalez Monroy 				"1 lcore for packet RX\n"
710850f3733SSergio Gonzalez Monroy 				"1 lcore for packet TX\n"
711850f3733SSergio Gonzalez Monroy 				"and at least 1 lcore for worker threads\n");
712850f3733SSergio Gonzalez Monroy 
713d9a42a69SThomas Monjalon 	nb_ports = rte_eth_dev_count_avail();
714850f3733SSergio Gonzalez Monroy 	if (nb_ports == 0)
715850f3733SSergio Gonzalez Monroy 		rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n");
716850f3733SSergio Gonzalez Monroy 	if (nb_ports != 1 && (nb_ports & 1))
717850f3733SSergio Gonzalez Monroy 		rte_exit(EXIT_FAILURE, "Error: number of ports must be even, except "
718850f3733SSergio Gonzalez Monroy 				"when using a single port\n");
719850f3733SSergio Gonzalez Monroy 
720ea0c20eaSOlivier Matz 	mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL,
721824cb29cSKonstantin Ananyev 			MBUF_POOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
722ea0c20eaSOlivier Matz 			rte_socket_id());
723850f3733SSergio Gonzalez Monroy 	if (mbuf_pool == NULL)
724850f3733SSergio Gonzalez Monroy 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
725850f3733SSergio Gonzalez Monroy 
726850f3733SSergio Gonzalez Monroy 	nb_ports_available = nb_ports;
727850f3733SSergio Gonzalez Monroy 
728850f3733SSergio Gonzalez Monroy 	/* initialize all ports */
7298728ccf3SThomas Monjalon 	RTE_ETH_FOREACH_DEV(port_id) {
730850f3733SSergio Gonzalez Monroy 		/* skip ports that are not enabled */
731850f3733SSergio Gonzalez Monroy 		if ((portmask & (1 << port_id)) == 0) {
732850f3733SSergio Gonzalez Monroy 			printf("\nSkipping disabled port %d\n", port_id);
733850f3733SSergio Gonzalez Monroy 			nb_ports_available--;
734850f3733SSergio Gonzalez Monroy 			continue;
735850f3733SSergio Gonzalez Monroy 		}
736850f3733SSergio Gonzalez Monroy 		/* init port */
737f8244c63SZhiyong Yang 		printf("Initializing port %u... done\n", port_id);
738850f3733SSergio Gonzalez Monroy 
739850f3733SSergio Gonzalez Monroy 		if (configure_eth_port(port_id) != 0)
740850f3733SSergio Gonzalez Monroy 			rte_exit(EXIT_FAILURE, "Cannot initialize port %"PRIu8"\n",
741850f3733SSergio Gonzalez Monroy 					port_id);
742850f3733SSergio Gonzalez Monroy 	}
743850f3733SSergio Gonzalez Monroy 
744850f3733SSergio Gonzalez Monroy 	if (!nb_ports_available) {
745850f3733SSergio Gonzalez Monroy 		rte_exit(EXIT_FAILURE,
746850f3733SSergio Gonzalez Monroy 			"All available ports are disabled. Please set portmask.\n");
747850f3733SSergio Gonzalez Monroy 	}
748850f3733SSergio Gonzalez Monroy 
749850f3733SSergio Gonzalez Monroy 	/* Create rings for inter core communication */
750850f3733SSergio Gonzalez Monroy 	rx_to_workers = rte_ring_create("rx_to_workers", RING_SIZE, rte_socket_id(),
751850f3733SSergio Gonzalez Monroy 			RING_F_SP_ENQ);
752850f3733SSergio Gonzalez Monroy 	if (rx_to_workers == NULL)
753850f3733SSergio Gonzalez Monroy 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
754850f3733SSergio Gonzalez Monroy 
755850f3733SSergio Gonzalez Monroy 	workers_to_tx = rte_ring_create("workers_to_tx", RING_SIZE, rte_socket_id(),
756850f3733SSergio Gonzalez Monroy 			RING_F_SC_DEQ);
757850f3733SSergio Gonzalez Monroy 	if (workers_to_tx == NULL)
758850f3733SSergio Gonzalez Monroy 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
759850f3733SSergio Gonzalez Monroy 
7606ebc23d8SSergio Gonzalez Monroy 	if (!disable_reorder) {
7616ebc23d8SSergio Gonzalez Monroy 		send_args.buffer = rte_reorder_create("PKT_RO", rte_socket_id(),
7626ebc23d8SSergio Gonzalez Monroy 				REORDER_BUFFER_SIZE);
7636ebc23d8SSergio Gonzalez Monroy 		if (send_args.buffer == NULL)
7646ebc23d8SSergio Gonzalez Monroy 			rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
7656ebc23d8SSergio Gonzalez Monroy 	}
7666ebc23d8SSergio Gonzalez Monroy 
767850f3733SSergio Gonzalez Monroy 	last_lcore_id   = get_last_lcore_id();
768cb056611SStephen Hemminger 	main_lcore_id = rte_get_main_lcore();
769850f3733SSergio Gonzalez Monroy 
770850f3733SSergio Gonzalez Monroy 	worker_args.ring_in  = rx_to_workers;
771850f3733SSergio Gonzalez Monroy 	worker_args.ring_out = workers_to_tx;
772850f3733SSergio Gonzalez Monroy 
773cb056611SStephen Hemminger 	/* Start worker_thread() on all the available worker cores but the last 1 */
774850f3733SSergio Gonzalez Monroy 	for (lcore_id = 0; lcore_id <= get_previous_lcore_id(last_lcore_id); lcore_id++)
775cb056611SStephen Hemminger 		if (rte_lcore_is_enabled(lcore_id) && lcore_id != main_lcore_id)
776850f3733SSergio Gonzalez Monroy 			rte_eal_remote_launch(worker_thread, (void *)&worker_args,
777850f3733SSergio Gonzalez Monroy 					lcore_id);
778850f3733SSergio Gonzalez Monroy 
7796ebc23d8SSergio Gonzalez Monroy 	if (disable_reorder) {
780cb056611SStephen Hemminger 		/* Start tx_thread() on the last worker core */
781850f3733SSergio Gonzalez Monroy 		rte_eal_remote_launch((lcore_function_t *)tx_thread, workers_to_tx,
782850f3733SSergio Gonzalez Monroy 				last_lcore_id);
7836ebc23d8SSergio Gonzalez Monroy 	} else {
7846ebc23d8SSergio Gonzalez Monroy 		send_args.ring_in = workers_to_tx;
785cb056611SStephen Hemminger 		/* Start send_thread() on the last worker core */
7866ebc23d8SSergio Gonzalez Monroy 		rte_eal_remote_launch((lcore_function_t *)send_thread,
7876ebc23d8SSergio Gonzalez Monroy 				(void *)&send_args, last_lcore_id);
7886ebc23d8SSergio Gonzalez Monroy 	}
789850f3733SSergio Gonzalez Monroy 
7907ba49dc7SQian Hao 	/* Start rx_thread_xxx() on the main core */
7917ba49dc7SQian Hao 	if (disable_reorder)
7927ba49dc7SQian Hao 		rx_thread_reorder_disabled(rx_to_workers);
7937ba49dc7SQian Hao 	else
7947ba49dc7SQian Hao 		rx_thread_reorder(rx_to_workers);
795850f3733SSergio Gonzalez Monroy 
796cb056611SStephen Hemminger 	RTE_LCORE_FOREACH_WORKER(lcore_id) {
797850f3733SSergio Gonzalez Monroy 		if (rte_eal_wait_lcore(lcore_id) < 0)
798850f3733SSergio Gonzalez Monroy 			return -1;
799850f3733SSergio Gonzalez Monroy 	}
800850f3733SSergio Gonzalez Monroy 
801850f3733SSergio Gonzalez Monroy 	print_stats();
80210aa3757SChengchang Tang 
80310aa3757SChengchang Tang 	/* clean up the EAL */
80410aa3757SChengchang Tang 	rte_eal_cleanup();
80510aa3757SChengchang Tang 
806850f3733SSergio Gonzalez Monroy 	return 0;
807850f3733SSergio Gonzalez Monroy }
808