xref: /dpdk/examples/packet_ordering/main.c (revision ea0c20ea95fd5d71a10757e6598ac66233ea1495)
1850f3733SSergio Gonzalez Monroy /*-
2850f3733SSergio Gonzalez Monroy  *   BSD LICENSE
3850f3733SSergio Gonzalez Monroy  *
4850f3733SSergio Gonzalez Monroy  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5850f3733SSergio Gonzalez Monroy  *   All rights reserved.
6850f3733SSergio Gonzalez Monroy  *
7850f3733SSergio Gonzalez Monroy  *   Redistribution and use in source and binary forms, with or without
8850f3733SSergio Gonzalez Monroy  *   modification, are permitted provided that the following conditions
9850f3733SSergio Gonzalez Monroy  *   are met:
10850f3733SSergio Gonzalez Monroy  *
11850f3733SSergio Gonzalez Monroy  *     * Redistributions of source code must retain the above copyright
12850f3733SSergio Gonzalez Monroy  *       notice, this list of conditions and the following disclaimer.
13850f3733SSergio Gonzalez Monroy  *     * Redistributions in binary form must reproduce the above copyright
14850f3733SSergio Gonzalez Monroy  *       notice, this list of conditions and the following disclaimer in
15850f3733SSergio Gonzalez Monroy  *       the documentation and/or other materials provided with the
16850f3733SSergio Gonzalez Monroy  *       distribution.
17850f3733SSergio Gonzalez Monroy  *     * Neither the name of Intel Corporation nor the names of its
18850f3733SSergio Gonzalez Monroy  *       contributors may be used to endorse or promote products derived
19850f3733SSergio Gonzalez Monroy  *       from this software without specific prior written permission.
20850f3733SSergio Gonzalez Monroy  *
21850f3733SSergio Gonzalez Monroy  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22850f3733SSergio Gonzalez Monroy  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23850f3733SSergio Gonzalez Monroy  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24850f3733SSergio Gonzalez Monroy  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25850f3733SSergio Gonzalez Monroy  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26850f3733SSergio Gonzalez Monroy  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27850f3733SSergio Gonzalez Monroy  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28850f3733SSergio Gonzalez Monroy  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29850f3733SSergio Gonzalez Monroy  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30850f3733SSergio Gonzalez Monroy  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31850f3733SSergio Gonzalez Monroy  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32850f3733SSergio Gonzalez Monroy  */
33850f3733SSergio Gonzalez Monroy 
34850f3733SSergio Gonzalez Monroy #include <signal.h>
35850f3733SSergio Gonzalez Monroy #include <getopt.h>
36850f3733SSergio Gonzalez Monroy 
37850f3733SSergio Gonzalez Monroy #include <rte_eal.h>
38850f3733SSergio Gonzalez Monroy #include <rte_common.h>
39850f3733SSergio Gonzalez Monroy #include <rte_errno.h>
40850f3733SSergio Gonzalez Monroy #include <rte_ethdev.h>
41850f3733SSergio Gonzalez Monroy #include <rte_lcore.h>
42850f3733SSergio Gonzalez Monroy #include <rte_mbuf.h>
43850f3733SSergio Gonzalez Monroy #include <rte_mempool.h>
44850f3733SSergio Gonzalez Monroy #include <rte_ring.h>
45850f3733SSergio Gonzalez Monroy #include <rte_reorder.h>
46850f3733SSergio Gonzalez Monroy 
47850f3733SSergio Gonzalez Monroy #define RX_DESC_PER_QUEUE 128
48850f3733SSergio Gonzalez Monroy #define TX_DESC_PER_QUEUE 512
49850f3733SSergio Gonzalez Monroy 
50850f3733SSergio Gonzalez Monroy #define MAX_PKTS_BURST 32
51850f3733SSergio Gonzalez Monroy #define REORDER_BUFFER_SIZE 8192
52850f3733SSergio Gonzalez Monroy #define MBUF_PER_POOL 65535
53*ea0c20eaSOlivier Matz #define MBUF_DATA_SIZE (1600 + RTE_PKTMBUF_HEADROOM)
54850f3733SSergio Gonzalez Monroy #define MBUF_POOL_CACHE_SIZE 250
55850f3733SSergio Gonzalez Monroy 
56850f3733SSergio Gonzalez Monroy #define RING_SIZE 16384
57850f3733SSergio Gonzalez Monroy 
58850f3733SSergio Gonzalez Monroy /* uncommnet below line to enable debug logs */
59850f3733SSergio Gonzalez Monroy /* #define DEBUG */
60850f3733SSergio Gonzalez Monroy 
61850f3733SSergio Gonzalez Monroy #ifdef DEBUG
62850f3733SSergio Gonzalez Monroy #define LOG_LEVEL RTE_LOG_DEBUG
63850f3733SSergio Gonzalez Monroy #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
64850f3733SSergio Gonzalez Monroy #else
65850f3733SSergio Gonzalez Monroy #define LOG_LEVEL RTE_LOG_INFO
66850f3733SSergio Gonzalez Monroy #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
67850f3733SSergio Gonzalez Monroy #endif
68850f3733SSergio Gonzalez Monroy 
69850f3733SSergio Gonzalez Monroy /* Macros for printing using RTE_LOG */
70850f3733SSergio Gonzalez Monroy #define RTE_LOGTYPE_REORDERAPP          RTE_LOGTYPE_USER1
71850f3733SSergio Gonzalez Monroy 
72850f3733SSergio Gonzalez Monroy unsigned int portmask;
73850f3733SSergio Gonzalez Monroy unsigned int disable_reorder;
74850f3733SSergio Gonzalez Monroy volatile uint8_t quit_signal;
75850f3733SSergio Gonzalez Monroy 
76850f3733SSergio Gonzalez Monroy static struct rte_mempool *mbuf_pool;
77850f3733SSergio Gonzalez Monroy 
78850f3733SSergio Gonzalez Monroy static struct rte_eth_conf port_conf_default;
79850f3733SSergio Gonzalez Monroy 
80850f3733SSergio Gonzalez Monroy struct worker_thread_args {
81850f3733SSergio Gonzalez Monroy 	struct rte_ring *ring_in;
82850f3733SSergio Gonzalez Monroy 	struct rte_ring *ring_out;
83850f3733SSergio Gonzalez Monroy };
84850f3733SSergio Gonzalez Monroy 
856ebc23d8SSergio Gonzalez Monroy struct send_thread_args {
866ebc23d8SSergio Gonzalez Monroy 	struct rte_ring *ring_in;
876ebc23d8SSergio Gonzalez Monroy 	struct rte_reorder_buffer *buffer;
886ebc23d8SSergio Gonzalez Monroy };
896ebc23d8SSergio Gonzalez Monroy 
90850f3733SSergio Gonzalez Monroy struct output_buffer {
91850f3733SSergio Gonzalez Monroy 	unsigned count;
92850f3733SSergio Gonzalez Monroy 	struct rte_mbuf *mbufs[MAX_PKTS_BURST];
93850f3733SSergio Gonzalez Monroy };
94850f3733SSergio Gonzalez Monroy 
95850f3733SSergio Gonzalez Monroy volatile struct app_stats {
96850f3733SSergio Gonzalez Monroy 	struct {
97850f3733SSergio Gonzalez Monroy 		uint64_t rx_pkts;
98850f3733SSergio Gonzalez Monroy 		uint64_t enqueue_pkts;
99850f3733SSergio Gonzalez Monroy 		uint64_t enqueue_failed_pkts;
100850f3733SSergio Gonzalez Monroy 	} rx __rte_cache_aligned;
101850f3733SSergio Gonzalez Monroy 
102850f3733SSergio Gonzalez Monroy 	struct {
103850f3733SSergio Gonzalez Monroy 		uint64_t dequeue_pkts;
104850f3733SSergio Gonzalez Monroy 		uint64_t enqueue_pkts;
105850f3733SSergio Gonzalez Monroy 		uint64_t enqueue_failed_pkts;
106850f3733SSergio Gonzalez Monroy 	} wkr __rte_cache_aligned;
107850f3733SSergio Gonzalez Monroy 
108850f3733SSergio Gonzalez Monroy 	struct {
109850f3733SSergio Gonzalez Monroy 		uint64_t dequeue_pkts;
110850f3733SSergio Gonzalez Monroy 		/* Too early pkts transmitted directly w/o reordering */
111850f3733SSergio Gonzalez Monroy 		uint64_t early_pkts_txtd_woro;
112850f3733SSergio Gonzalez Monroy 		/* Too early pkts failed from direct transmit */
113850f3733SSergio Gonzalez Monroy 		uint64_t early_pkts_tx_failed_woro;
114850f3733SSergio Gonzalez Monroy 		uint64_t ro_tx_pkts;
115850f3733SSergio Gonzalez Monroy 		uint64_t ro_tx_failed_pkts;
116850f3733SSergio Gonzalez Monroy 	} tx __rte_cache_aligned;
117850f3733SSergio Gonzalez Monroy } app_stats;
118850f3733SSergio Gonzalez Monroy 
119850f3733SSergio Gonzalez Monroy /**
120850f3733SSergio Gonzalez Monroy  * Get the last enabled lcore ID
121850f3733SSergio Gonzalez Monroy  *
122850f3733SSergio Gonzalez Monroy  * @return
123850f3733SSergio Gonzalez Monroy  *   The last enabled lcore ID.
124850f3733SSergio Gonzalez Monroy  */
125850f3733SSergio Gonzalez Monroy static unsigned int
126850f3733SSergio Gonzalez Monroy get_last_lcore_id(void)
127850f3733SSergio Gonzalez Monroy {
128850f3733SSergio Gonzalez Monroy 	int i;
129850f3733SSergio Gonzalez Monroy 
130850f3733SSergio Gonzalez Monroy 	for (i = RTE_MAX_LCORE - 1; i >= 0; i--)
131850f3733SSergio Gonzalez Monroy 		if (rte_lcore_is_enabled(i))
132850f3733SSergio Gonzalez Monroy 			return i;
133850f3733SSergio Gonzalez Monroy 	return 0;
134850f3733SSergio Gonzalez Monroy }
135850f3733SSergio Gonzalez Monroy 
136850f3733SSergio Gonzalez Monroy /**
137850f3733SSergio Gonzalez Monroy  * Get the previous enabled lcore ID
138850f3733SSergio Gonzalez Monroy  * @param id
139850f3733SSergio Gonzalez Monroy  *  The current lcore ID
140850f3733SSergio Gonzalez Monroy  * @return
141850f3733SSergio Gonzalez Monroy  *   The previous enabled lcore ID or the current lcore
142850f3733SSergio Gonzalez Monroy  *   ID if it is the first available core.
143850f3733SSergio Gonzalez Monroy  */
144850f3733SSergio Gonzalez Monroy static unsigned int
145850f3733SSergio Gonzalez Monroy get_previous_lcore_id(unsigned int id)
146850f3733SSergio Gonzalez Monroy {
147850f3733SSergio Gonzalez Monroy 	int i;
148850f3733SSergio Gonzalez Monroy 
149850f3733SSergio Gonzalez Monroy 	for (i = id - 1; i >= 0; i--)
150850f3733SSergio Gonzalez Monroy 		if (rte_lcore_is_enabled(i))
151850f3733SSergio Gonzalez Monroy 			return i;
152850f3733SSergio Gonzalez Monroy 	return id;
153850f3733SSergio Gonzalez Monroy }
154850f3733SSergio Gonzalez Monroy 
155850f3733SSergio Gonzalez Monroy static inline void
156850f3733SSergio Gonzalez Monroy pktmbuf_free_bulk(struct rte_mbuf *mbuf_table[], unsigned n)
157850f3733SSergio Gonzalez Monroy {
158850f3733SSergio Gonzalez Monroy 	unsigned int i;
159850f3733SSergio Gonzalez Monroy 
160850f3733SSergio Gonzalez Monroy 	for (i = 0; i < n; i++)
161850f3733SSergio Gonzalez Monroy 		rte_pktmbuf_free(mbuf_table[i]);
162850f3733SSergio Gonzalez Monroy }
163850f3733SSergio Gonzalez Monroy 
164850f3733SSergio Gonzalez Monroy /* display usage */
165850f3733SSergio Gonzalez Monroy static void
166850f3733SSergio Gonzalez Monroy print_usage(const char *prgname)
167850f3733SSergio Gonzalez Monroy {
168850f3733SSergio Gonzalez Monroy 	printf("%s [EAL options] -- -p PORTMASK\n"
169850f3733SSergio Gonzalez Monroy 			"  -p PORTMASK: hexadecimal bitmask of ports to configure\n",
170850f3733SSergio Gonzalez Monroy 			prgname);
171850f3733SSergio Gonzalez Monroy }
172850f3733SSergio Gonzalez Monroy 
173850f3733SSergio Gonzalez Monroy static int
174850f3733SSergio Gonzalez Monroy parse_portmask(const char *portmask)
175850f3733SSergio Gonzalez Monroy {
176850f3733SSergio Gonzalez Monroy 	unsigned long pm;
177850f3733SSergio Gonzalez Monroy 	char *end = NULL;
178850f3733SSergio Gonzalez Monroy 
179850f3733SSergio Gonzalez Monroy 	/* parse hexadecimal string */
180850f3733SSergio Gonzalez Monroy 	pm = strtoul(portmask, &end, 16);
181850f3733SSergio Gonzalez Monroy 	if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
182850f3733SSergio Gonzalez Monroy 		return -1;
183850f3733SSergio Gonzalez Monroy 
184850f3733SSergio Gonzalez Monroy 	if (pm == 0)
185850f3733SSergio Gonzalez Monroy 		return -1;
186850f3733SSergio Gonzalez Monroy 
187850f3733SSergio Gonzalez Monroy 	return pm;
188850f3733SSergio Gonzalez Monroy }
189850f3733SSergio Gonzalez Monroy 
190850f3733SSergio Gonzalez Monroy /* Parse the argument given in the command line of the application */
191850f3733SSergio Gonzalez Monroy static int
192850f3733SSergio Gonzalez Monroy parse_args(int argc, char **argv)
193850f3733SSergio Gonzalez Monroy {
194850f3733SSergio Gonzalez Monroy 	int opt;
195850f3733SSergio Gonzalez Monroy 	int option_index;
196850f3733SSergio Gonzalez Monroy 	char **argvopt;
197850f3733SSergio Gonzalez Monroy 	char *prgname = argv[0];
198850f3733SSergio Gonzalez Monroy 	static struct option lgopts[] = {
199850f3733SSergio Gonzalez Monroy 		{"disable-reorder", 0, 0, 0},
200850f3733SSergio Gonzalez Monroy 		{NULL, 0, 0, 0}
201850f3733SSergio Gonzalez Monroy 	};
202850f3733SSergio Gonzalez Monroy 
203850f3733SSergio Gonzalez Monroy 	argvopt = argv;
204850f3733SSergio Gonzalez Monroy 
205850f3733SSergio Gonzalez Monroy 	while ((opt = getopt_long(argc, argvopt, "p:",
206850f3733SSergio Gonzalez Monroy 					lgopts, &option_index)) != EOF) {
207850f3733SSergio Gonzalez Monroy 		switch (opt) {
208850f3733SSergio Gonzalez Monroy 		/* portmask */
209850f3733SSergio Gonzalez Monroy 		case 'p':
210850f3733SSergio Gonzalez Monroy 			portmask = parse_portmask(optarg);
211850f3733SSergio Gonzalez Monroy 			if (portmask == 0) {
212850f3733SSergio Gonzalez Monroy 				printf("invalid portmask\n");
213850f3733SSergio Gonzalez Monroy 				print_usage(prgname);
214850f3733SSergio Gonzalez Monroy 				return -1;
215850f3733SSergio Gonzalez Monroy 			}
216850f3733SSergio Gonzalez Monroy 			break;
217850f3733SSergio Gonzalez Monroy 		/* long options */
218850f3733SSergio Gonzalez Monroy 		case 0:
219850f3733SSergio Gonzalez Monroy 			if (!strcmp(lgopts[option_index].name, "disable-reorder")) {
220850f3733SSergio Gonzalez Monroy 				printf("reorder disabled\n");
221850f3733SSergio Gonzalez Monroy 				disable_reorder = 1;
222850f3733SSergio Gonzalez Monroy 			}
223850f3733SSergio Gonzalez Monroy 			break;
224850f3733SSergio Gonzalez Monroy 		default:
225850f3733SSergio Gonzalez Monroy 			print_usage(prgname);
226850f3733SSergio Gonzalez Monroy 			return -1;
227850f3733SSergio Gonzalez Monroy 		}
228850f3733SSergio Gonzalez Monroy 	}
229850f3733SSergio Gonzalez Monroy 	if (optind <= 1) {
230850f3733SSergio Gonzalez Monroy 		print_usage(prgname);
231850f3733SSergio Gonzalez Monroy 		return -1;
232850f3733SSergio Gonzalez Monroy 	}
233850f3733SSergio Gonzalez Monroy 
234850f3733SSergio Gonzalez Monroy 	argv[optind-1] = prgname;
235850f3733SSergio Gonzalez Monroy 	optind = 0; /* reset getopt lib */
236850f3733SSergio Gonzalez Monroy 	return 0;
237850f3733SSergio Gonzalez Monroy }
238850f3733SSergio Gonzalez Monroy 
239850f3733SSergio Gonzalez Monroy static inline int
240850f3733SSergio Gonzalez Monroy configure_eth_port(uint8_t port_id)
241850f3733SSergio Gonzalez Monroy {
242850f3733SSergio Gonzalez Monroy 	struct ether_addr addr;
243850f3733SSergio Gonzalez Monroy 	const uint16_t rxRings = 1, txRings = 1;
244850f3733SSergio Gonzalez Monroy 	const uint8_t nb_ports = rte_eth_dev_count();
245850f3733SSergio Gonzalez Monroy 	int ret;
246850f3733SSergio Gonzalez Monroy 	uint16_t q;
247850f3733SSergio Gonzalez Monroy 
248850f3733SSergio Gonzalez Monroy 	if (port_id > nb_ports)
249850f3733SSergio Gonzalez Monroy 		return -1;
250850f3733SSergio Gonzalez Monroy 
251850f3733SSergio Gonzalez Monroy 	ret = rte_eth_dev_configure(port_id, rxRings, txRings, &port_conf_default);
252850f3733SSergio Gonzalez Monroy 	if (ret != 0)
253850f3733SSergio Gonzalez Monroy 		return ret;
254850f3733SSergio Gonzalez Monroy 
255850f3733SSergio Gonzalez Monroy 	for (q = 0; q < rxRings; q++) {
256850f3733SSergio Gonzalez Monroy 		ret = rte_eth_rx_queue_setup(port_id, q, RX_DESC_PER_QUEUE,
257850f3733SSergio Gonzalez Monroy 				rte_eth_dev_socket_id(port_id), NULL,
258850f3733SSergio Gonzalez Monroy 				mbuf_pool);
259850f3733SSergio Gonzalez Monroy 		if (ret < 0)
260850f3733SSergio Gonzalez Monroy 			return ret;
261850f3733SSergio Gonzalez Monroy 	}
262850f3733SSergio Gonzalez Monroy 
263850f3733SSergio Gonzalez Monroy 	for (q = 0; q < txRings; q++) {
264850f3733SSergio Gonzalez Monroy 		ret = rte_eth_tx_queue_setup(port_id, q, TX_DESC_PER_QUEUE,
265850f3733SSergio Gonzalez Monroy 				rte_eth_dev_socket_id(port_id), NULL);
266850f3733SSergio Gonzalez Monroy 		if (ret < 0)
267850f3733SSergio Gonzalez Monroy 			return ret;
268850f3733SSergio Gonzalez Monroy 	}
269850f3733SSergio Gonzalez Monroy 
270850f3733SSergio Gonzalez Monroy 	ret = rte_eth_dev_start(port_id);
271850f3733SSergio Gonzalez Monroy 	if (ret < 0)
272850f3733SSergio Gonzalez Monroy 		return ret;
273850f3733SSergio Gonzalez Monroy 
274850f3733SSergio Gonzalez Monroy 	rte_eth_macaddr_get(port_id, &addr);
275850f3733SSergio Gonzalez Monroy 	printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
276850f3733SSergio Gonzalez Monroy 			" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
277850f3733SSergio Gonzalez Monroy 			(unsigned)port_id,
278850f3733SSergio Gonzalez Monroy 			addr.addr_bytes[0], addr.addr_bytes[1],
279850f3733SSergio Gonzalez Monroy 			addr.addr_bytes[2], addr.addr_bytes[3],
280850f3733SSergio Gonzalez Monroy 			addr.addr_bytes[4], addr.addr_bytes[5]);
281850f3733SSergio Gonzalez Monroy 
282850f3733SSergio Gonzalez Monroy 	rte_eth_promiscuous_enable(port_id);
283850f3733SSergio Gonzalez Monroy 
284850f3733SSergio Gonzalez Monroy 	return 0;
285850f3733SSergio Gonzalez Monroy }
286850f3733SSergio Gonzalez Monroy 
287850f3733SSergio Gonzalez Monroy static void
288850f3733SSergio Gonzalez Monroy print_stats(void)
289850f3733SSergio Gonzalez Monroy {
290850f3733SSergio Gonzalez Monroy 	const uint8_t nb_ports = rte_eth_dev_count();
291850f3733SSergio Gonzalez Monroy 	unsigned i;
292850f3733SSergio Gonzalez Monroy 	struct rte_eth_stats eth_stats;
293850f3733SSergio Gonzalez Monroy 
294850f3733SSergio Gonzalez Monroy 	printf("\nRX thread stats:\n");
295850f3733SSergio Gonzalez Monroy 	printf(" - Pkts rxd:				%"PRIu64"\n",
296850f3733SSergio Gonzalez Monroy 						app_stats.rx.rx_pkts);
297850f3733SSergio Gonzalez Monroy 	printf(" - Pkts enqd to workers ring:		%"PRIu64"\n",
298850f3733SSergio Gonzalez Monroy 						app_stats.rx.enqueue_pkts);
299850f3733SSergio Gonzalez Monroy 
300850f3733SSergio Gonzalez Monroy 	printf("\nWorker thread stats:\n");
301850f3733SSergio Gonzalez Monroy 	printf(" - Pkts deqd from workers ring:		%"PRIu64"\n",
302850f3733SSergio Gonzalez Monroy 						app_stats.wkr.dequeue_pkts);
303850f3733SSergio Gonzalez Monroy 	printf(" - Pkts enqd to tx ring:		%"PRIu64"\n",
304850f3733SSergio Gonzalez Monroy 						app_stats.wkr.enqueue_pkts);
305850f3733SSergio Gonzalez Monroy 	printf(" - Pkts enq to tx failed:		%"PRIu64"\n",
306850f3733SSergio Gonzalez Monroy 						app_stats.wkr.enqueue_failed_pkts);
307850f3733SSergio Gonzalez Monroy 
308850f3733SSergio Gonzalez Monroy 	printf("\nTX stats:\n");
309850f3733SSergio Gonzalez Monroy 	printf(" - Pkts deqd from tx ring:		%"PRIu64"\n",
310850f3733SSergio Gonzalez Monroy 						app_stats.tx.dequeue_pkts);
311850f3733SSergio Gonzalez Monroy 	printf(" - Ro Pkts transmitted:			%"PRIu64"\n",
312850f3733SSergio Gonzalez Monroy 						app_stats.tx.ro_tx_pkts);
313850f3733SSergio Gonzalez Monroy 	printf(" - Ro Pkts tx failed:			%"PRIu64"\n",
314850f3733SSergio Gonzalez Monroy 						app_stats.tx.ro_tx_failed_pkts);
315850f3733SSergio Gonzalez Monroy 	printf(" - Pkts transmitted w/o reorder:	%"PRIu64"\n",
316850f3733SSergio Gonzalez Monroy 						app_stats.tx.early_pkts_txtd_woro);
317850f3733SSergio Gonzalez Monroy 	printf(" - Pkts tx failed w/o reorder:		%"PRIu64"\n",
318850f3733SSergio Gonzalez Monroy 						app_stats.tx.early_pkts_tx_failed_woro);
319850f3733SSergio Gonzalez Monroy 
320850f3733SSergio Gonzalez Monroy 	for (i = 0; i < nb_ports; i++) {
321850f3733SSergio Gonzalez Monroy 		rte_eth_stats_get(i, &eth_stats);
322850f3733SSergio Gonzalez Monroy 		printf("\nPort %u stats:\n", i);
323850f3733SSergio Gonzalez Monroy 		printf(" - Pkts in:   %"PRIu64"\n", eth_stats.ipackets);
324850f3733SSergio Gonzalez Monroy 		printf(" - Pkts out:  %"PRIu64"\n", eth_stats.opackets);
325850f3733SSergio Gonzalez Monroy 		printf(" - In Errs:   %"PRIu64"\n", eth_stats.ierrors);
326850f3733SSergio Gonzalez Monroy 		printf(" - Out Errs:  %"PRIu64"\n", eth_stats.oerrors);
327850f3733SSergio Gonzalez Monroy 		printf(" - Mbuf Errs: %"PRIu64"\n", eth_stats.rx_nombuf);
328850f3733SSergio Gonzalez Monroy 	}
329850f3733SSergio Gonzalez Monroy }
330850f3733SSergio Gonzalez Monroy 
331850f3733SSergio Gonzalez Monroy static void
332850f3733SSergio Gonzalez Monroy int_handler(int sig_num)
333850f3733SSergio Gonzalez Monroy {
334850f3733SSergio Gonzalez Monroy 	printf("Exiting on signal %d\n", sig_num);
335850f3733SSergio Gonzalez Monroy 	quit_signal = 1;
336850f3733SSergio Gonzalez Monroy }
337850f3733SSergio Gonzalez Monroy 
338850f3733SSergio Gonzalez Monroy /**
339850f3733SSergio Gonzalez Monroy  * This thread receives mbufs from the port and affects them an internal
340850f3733SSergio Gonzalez Monroy  * sequence number to keep track of their order of arrival through an
341850f3733SSergio Gonzalez Monroy  * mbuf structure.
342850f3733SSergio Gonzalez Monroy  * The mbufs are then passed to the worker threads via the rx_to_workers
343850f3733SSergio Gonzalez Monroy  * ring.
344850f3733SSergio Gonzalez Monroy  */
345850f3733SSergio Gonzalez Monroy static int
346850f3733SSergio Gonzalez Monroy rx_thread(struct rte_ring *ring_out)
347850f3733SSergio Gonzalez Monroy {
348850f3733SSergio Gonzalez Monroy 	const uint8_t nb_ports = rte_eth_dev_count();
349850f3733SSergio Gonzalez Monroy 	uint32_t seqn = 0;
350850f3733SSergio Gonzalez Monroy 	uint16_t i, ret = 0;
351850f3733SSergio Gonzalez Monroy 	uint16_t nb_rx_pkts;
352850f3733SSergio Gonzalez Monroy 	uint8_t port_id;
353850f3733SSergio Gonzalez Monroy 	struct rte_mbuf *pkts[MAX_PKTS_BURST];
354850f3733SSergio Gonzalez Monroy 
355850f3733SSergio Gonzalez Monroy 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
356850f3733SSergio Gonzalez Monroy 							rte_lcore_id());
357850f3733SSergio Gonzalez Monroy 
358850f3733SSergio Gonzalez Monroy 	while (!quit_signal) {
359850f3733SSergio Gonzalez Monroy 
360850f3733SSergio Gonzalez Monroy 		for (port_id = 0; port_id < nb_ports; port_id++) {
361850f3733SSergio Gonzalez Monroy 			if ((portmask & (1 << port_id)) != 0) {
362850f3733SSergio Gonzalez Monroy 
363850f3733SSergio Gonzalez Monroy 				/* receive packets */
364850f3733SSergio Gonzalez Monroy 				nb_rx_pkts = rte_eth_rx_burst(port_id, 0,
365850f3733SSergio Gonzalez Monroy 								pkts, MAX_PKTS_BURST);
366850f3733SSergio Gonzalez Monroy 				if (nb_rx_pkts == 0) {
367850f3733SSergio Gonzalez Monroy 					LOG_DEBUG(REORDERAPP,
368850f3733SSergio Gonzalez Monroy 					"%s():Received zero packets\n",	__func__);
369850f3733SSergio Gonzalez Monroy 					continue;
370850f3733SSergio Gonzalez Monroy 				}
371850f3733SSergio Gonzalez Monroy 				app_stats.rx.rx_pkts += nb_rx_pkts;
372850f3733SSergio Gonzalez Monroy 
373850f3733SSergio Gonzalez Monroy 				/* mark sequence number */
374850f3733SSergio Gonzalez Monroy 				for (i = 0; i < nb_rx_pkts; )
375850f3733SSergio Gonzalez Monroy 					pkts[i++]->seqn = seqn++;
376850f3733SSergio Gonzalez Monroy 
377850f3733SSergio Gonzalez Monroy 				/* enqueue to rx_to_workers ring */
378850f3733SSergio Gonzalez Monroy 				ret = rte_ring_enqueue_burst(ring_out, (void *) pkts,
379850f3733SSergio Gonzalez Monroy 								nb_rx_pkts);
380850f3733SSergio Gonzalez Monroy 				app_stats.rx.enqueue_pkts += ret;
381850f3733SSergio Gonzalez Monroy 				if (unlikely(ret < nb_rx_pkts)) {
382850f3733SSergio Gonzalez Monroy 					app_stats.rx.enqueue_failed_pkts +=
383850f3733SSergio Gonzalez Monroy 									(nb_rx_pkts-ret);
384850f3733SSergio Gonzalez Monroy 					pktmbuf_free_bulk(&pkts[ret], nb_rx_pkts - ret);
385850f3733SSergio Gonzalez Monroy 				}
386850f3733SSergio Gonzalez Monroy 			}
387850f3733SSergio Gonzalez Monroy 		}
388850f3733SSergio Gonzalez Monroy 	}
389850f3733SSergio Gonzalez Monroy 	return 0;
390850f3733SSergio Gonzalez Monroy }
391850f3733SSergio Gonzalez Monroy 
392850f3733SSergio Gonzalez Monroy /**
393850f3733SSergio Gonzalez Monroy  * This thread takes bursts of packets from the rx_to_workers ring and
394850f3733SSergio Gonzalez Monroy  * Changes the input port value to output port value. And feds it to
395850f3733SSergio Gonzalez Monroy  * workers_to_tx
396850f3733SSergio Gonzalez Monroy  */
397850f3733SSergio Gonzalez Monroy static int
398850f3733SSergio Gonzalez Monroy worker_thread(void *args_ptr)
399850f3733SSergio Gonzalez Monroy {
400850f3733SSergio Gonzalez Monroy 	const uint8_t nb_ports = rte_eth_dev_count();
401850f3733SSergio Gonzalez Monroy 	uint16_t i, ret = 0;
402850f3733SSergio Gonzalez Monroy 	uint16_t burst_size = 0;
403850f3733SSergio Gonzalez Monroy 	struct worker_thread_args *args;
404850f3733SSergio Gonzalez Monroy 	struct rte_mbuf *burst_buffer[MAX_PKTS_BURST] = { NULL };
405850f3733SSergio Gonzalez Monroy 	struct rte_ring *ring_in, *ring_out;
406850f3733SSergio Gonzalez Monroy 	const unsigned xor_val = (nb_ports > 1);
407850f3733SSergio Gonzalez Monroy 
408850f3733SSergio Gonzalez Monroy 	args = (struct worker_thread_args *) args_ptr;
409850f3733SSergio Gonzalez Monroy 	ring_in  = args->ring_in;
410850f3733SSergio Gonzalez Monroy 	ring_out = args->ring_out;
411850f3733SSergio Gonzalez Monroy 
412850f3733SSergio Gonzalez Monroy 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
413850f3733SSergio Gonzalez Monroy 							rte_lcore_id());
414850f3733SSergio Gonzalez Monroy 
415850f3733SSergio Gonzalez Monroy 	while (!quit_signal) {
416850f3733SSergio Gonzalez Monroy 
417850f3733SSergio Gonzalez Monroy 		/* dequeue the mbufs from rx_to_workers ring */
418850f3733SSergio Gonzalez Monroy 		burst_size = rte_ring_dequeue_burst(ring_in,
419850f3733SSergio Gonzalez Monroy 				(void *)burst_buffer, MAX_PKTS_BURST);
420850f3733SSergio Gonzalez Monroy 		if (unlikely(burst_size == 0))
421850f3733SSergio Gonzalez Monroy 			continue;
422850f3733SSergio Gonzalez Monroy 
423850f3733SSergio Gonzalez Monroy 		__sync_fetch_and_add(&app_stats.wkr.dequeue_pkts, burst_size);
424850f3733SSergio Gonzalez Monroy 
425850f3733SSergio Gonzalez Monroy 		/* just do some operation on mbuf */
426850f3733SSergio Gonzalez Monroy 		for (i = 0; i < burst_size;)
427850f3733SSergio Gonzalez Monroy 			burst_buffer[i++]->port ^= xor_val;
428850f3733SSergio Gonzalez Monroy 
429850f3733SSergio Gonzalez Monroy 		/* enqueue the modified mbufs to workers_to_tx ring */
430850f3733SSergio Gonzalez Monroy 		ret = rte_ring_enqueue_burst(ring_out, (void *)burst_buffer, burst_size);
431850f3733SSergio Gonzalez Monroy 		__sync_fetch_and_add(&app_stats.wkr.enqueue_pkts, ret);
432850f3733SSergio Gonzalez Monroy 		if (unlikely(ret < burst_size)) {
433850f3733SSergio Gonzalez Monroy 			/* Return the mbufs to their respective pool, dropping packets */
434850f3733SSergio Gonzalez Monroy 			__sync_fetch_and_add(&app_stats.wkr.enqueue_failed_pkts,
435850f3733SSergio Gonzalez Monroy 					(int)burst_size - ret);
436850f3733SSergio Gonzalez Monroy 			pktmbuf_free_bulk(&burst_buffer[ret], burst_size - ret);
437850f3733SSergio Gonzalez Monroy 		}
438850f3733SSergio Gonzalez Monroy 	}
439850f3733SSergio Gonzalez Monroy 	return 0;
440850f3733SSergio Gonzalez Monroy }
441850f3733SSergio Gonzalez Monroy 
442850f3733SSergio Gonzalez Monroy static inline void
443850f3733SSergio Gonzalez Monroy flush_one_port(struct output_buffer *outbuf, uint8_t outp)
444850f3733SSergio Gonzalez Monroy {
445850f3733SSergio Gonzalez Monroy 	unsigned nb_tx = rte_eth_tx_burst(outp, 0, outbuf->mbufs,
446850f3733SSergio Gonzalez Monroy 			outbuf->count);
447850f3733SSergio Gonzalez Monroy 	app_stats.tx.ro_tx_pkts += nb_tx;
448850f3733SSergio Gonzalez Monroy 
449850f3733SSergio Gonzalez Monroy 	if (unlikely(nb_tx < outbuf->count)) {
450850f3733SSergio Gonzalez Monroy 		/* free the mbufs which failed from transmit */
451850f3733SSergio Gonzalez Monroy 		app_stats.tx.ro_tx_failed_pkts += (outbuf->count - nb_tx);
452850f3733SSergio Gonzalez Monroy 		LOG_DEBUG(REORDERAPP, "%s:Packet loss with tx_burst\n", __func__);
453850f3733SSergio Gonzalez Monroy 		pktmbuf_free_bulk(&outbuf->mbufs[nb_tx], outbuf->count - nb_tx);
454850f3733SSergio Gonzalez Monroy 	}
455850f3733SSergio Gonzalez Monroy 	outbuf->count = 0;
456850f3733SSergio Gonzalez Monroy }
457850f3733SSergio Gonzalez Monroy 
458850f3733SSergio Gonzalez Monroy /**
459850f3733SSergio Gonzalez Monroy  * Dequeue mbufs from the workers_to_tx ring and reorder them before
460850f3733SSergio Gonzalez Monroy  * transmitting.
461850f3733SSergio Gonzalez Monroy  */
462850f3733SSergio Gonzalez Monroy static int
4636ebc23d8SSergio Gonzalez Monroy send_thread(struct send_thread_args *args)
464850f3733SSergio Gonzalez Monroy {
465850f3733SSergio Gonzalez Monroy 	int ret;
466850f3733SSergio Gonzalez Monroy 	unsigned int i, dret;
467850f3733SSergio Gonzalez Monroy 	uint16_t nb_dq_mbufs;
468850f3733SSergio Gonzalez Monroy 	uint8_t outp;
469850f3733SSergio Gonzalez Monroy 	static struct output_buffer tx_buffers[RTE_MAX_ETHPORTS];
470850f3733SSergio Gonzalez Monroy 	struct rte_mbuf *mbufs[MAX_PKTS_BURST];
471850f3733SSergio Gonzalez Monroy 	struct rte_mbuf *rombufs[MAX_PKTS_BURST] = {NULL};
472850f3733SSergio Gonzalez Monroy 
4736ebc23d8SSergio Gonzalez Monroy 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__, rte_lcore_id());
4746ebc23d8SSergio Gonzalez Monroy 
475850f3733SSergio Gonzalez Monroy 	while (!quit_signal) {
476850f3733SSergio Gonzalez Monroy 
477850f3733SSergio Gonzalez Monroy 		/* deque the mbufs from workers_to_tx ring */
4786ebc23d8SSergio Gonzalez Monroy 		nb_dq_mbufs = rte_ring_dequeue_burst(args->ring_in,
479850f3733SSergio Gonzalez Monroy 				(void *)mbufs, MAX_PKTS_BURST);
480850f3733SSergio Gonzalez Monroy 
481850f3733SSergio Gonzalez Monroy 		if (unlikely(nb_dq_mbufs == 0))
482850f3733SSergio Gonzalez Monroy 			continue;
483850f3733SSergio Gonzalez Monroy 
484850f3733SSergio Gonzalez Monroy 		app_stats.tx.dequeue_pkts += nb_dq_mbufs;
485850f3733SSergio Gonzalez Monroy 
486850f3733SSergio Gonzalez Monroy 		for (i = 0; i < nb_dq_mbufs; i++) {
487850f3733SSergio Gonzalez Monroy 			/* send dequeued mbufs for reordering */
4886ebc23d8SSergio Gonzalez Monroy 			ret = rte_reorder_insert(args->buffer, mbufs[i]);
489850f3733SSergio Gonzalez Monroy 
490850f3733SSergio Gonzalez Monroy 			if (ret == -1 && rte_errno == ERANGE) {
491850f3733SSergio Gonzalez Monroy 				/* Too early pkts should be transmitted out directly */
492850f3733SSergio Gonzalez Monroy 				LOG_DEBUG(REORDERAPP, "%s():Cannot reorder early packet "
493850f3733SSergio Gonzalez Monroy 						"direct enqueuing to TX\n", __func__);
494850f3733SSergio Gonzalez Monroy 				outp = mbufs[i]->port;
495850f3733SSergio Gonzalez Monroy 				if ((portmask & (1 << outp)) == 0) {
496850f3733SSergio Gonzalez Monroy 					rte_pktmbuf_free(mbufs[i]);
497850f3733SSergio Gonzalez Monroy 					continue;
498850f3733SSergio Gonzalez Monroy 				}
499850f3733SSergio Gonzalez Monroy 				if (rte_eth_tx_burst(outp, 0, (void *)mbufs[i], 1) != 1) {
500850f3733SSergio Gonzalez Monroy 					rte_pktmbuf_free(mbufs[i]);
501850f3733SSergio Gonzalez Monroy 					app_stats.tx.early_pkts_tx_failed_woro++;
502850f3733SSergio Gonzalez Monroy 				} else
503850f3733SSergio Gonzalez Monroy 					app_stats.tx.early_pkts_txtd_woro++;
504850f3733SSergio Gonzalez Monroy 			} else if (ret == -1 && rte_errno == ENOSPC) {
505850f3733SSergio Gonzalez Monroy 				/**
506850f3733SSergio Gonzalez Monroy 				 * Early pkts just outside of window should be dropped
507850f3733SSergio Gonzalez Monroy 				 */
508850f3733SSergio Gonzalez Monroy 				rte_pktmbuf_free(mbufs[i]);
509850f3733SSergio Gonzalez Monroy 			}
510850f3733SSergio Gonzalez Monroy 		}
511850f3733SSergio Gonzalez Monroy 
512850f3733SSergio Gonzalez Monroy 		/*
513850f3733SSergio Gonzalez Monroy 		 * drain MAX_PKTS_BURST of reordered
514850f3733SSergio Gonzalez Monroy 		 * mbufs for transmit
515850f3733SSergio Gonzalez Monroy 		 */
5166ebc23d8SSergio Gonzalez Monroy 		dret = rte_reorder_drain(args->buffer, rombufs, MAX_PKTS_BURST);
517850f3733SSergio Gonzalez Monroy 		for (i = 0; i < dret; i++) {
518850f3733SSergio Gonzalez Monroy 
519850f3733SSergio Gonzalez Monroy 			struct output_buffer *outbuf;
520850f3733SSergio Gonzalez Monroy 			uint8_t outp1;
521850f3733SSergio Gonzalez Monroy 
522850f3733SSergio Gonzalez Monroy 			outp1 = rombufs[i]->port;
523850f3733SSergio Gonzalez Monroy 			/* skip ports that are not enabled */
524850f3733SSergio Gonzalez Monroy 			if ((portmask & (1 << outp1)) == 0) {
525850f3733SSergio Gonzalez Monroy 				rte_pktmbuf_free(rombufs[i]);
526850f3733SSergio Gonzalez Monroy 				continue;
527850f3733SSergio Gonzalez Monroy 			}
528850f3733SSergio Gonzalez Monroy 
529850f3733SSergio Gonzalez Monroy 			outbuf = &tx_buffers[outp1];
530850f3733SSergio Gonzalez Monroy 			outbuf->mbufs[outbuf->count++] = rombufs[i];
531850f3733SSergio Gonzalez Monroy 			if (outbuf->count == MAX_PKTS_BURST)
532850f3733SSergio Gonzalez Monroy 				flush_one_port(outbuf, outp1);
533850f3733SSergio Gonzalez Monroy 		}
534850f3733SSergio Gonzalez Monroy 	}
535850f3733SSergio Gonzalez Monroy 	return 0;
536850f3733SSergio Gonzalez Monroy }
537850f3733SSergio Gonzalez Monroy 
538850f3733SSergio Gonzalez Monroy /**
539850f3733SSergio Gonzalez Monroy  * Dequeue mbufs from the workers_to_tx ring and transmit them
540850f3733SSergio Gonzalez Monroy  */
541850f3733SSergio Gonzalez Monroy static int
542850f3733SSergio Gonzalez Monroy tx_thread(struct rte_ring *ring_in)
543850f3733SSergio Gonzalez Monroy {
544850f3733SSergio Gonzalez Monroy 	uint32_t i, dqnum;
545850f3733SSergio Gonzalez Monroy 	uint8_t outp;
546850f3733SSergio Gonzalez Monroy 	static struct output_buffer tx_buffers[RTE_MAX_ETHPORTS];
547850f3733SSergio Gonzalez Monroy 	struct rte_mbuf *mbufs[MAX_PKTS_BURST];
548850f3733SSergio Gonzalez Monroy 	struct output_buffer *outbuf;
549850f3733SSergio Gonzalez Monroy 
550850f3733SSergio Gonzalez Monroy 	RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
551850f3733SSergio Gonzalez Monroy 							rte_lcore_id());
552850f3733SSergio Gonzalez Monroy 	while (!quit_signal) {
553850f3733SSergio Gonzalez Monroy 
554850f3733SSergio Gonzalez Monroy 		/* deque the mbufs from workers_to_tx ring */
555850f3733SSergio Gonzalez Monroy 		dqnum = rte_ring_dequeue_burst(ring_in,
556850f3733SSergio Gonzalez Monroy 				(void *)mbufs, MAX_PKTS_BURST);
557850f3733SSergio Gonzalez Monroy 
558850f3733SSergio Gonzalez Monroy 		if (unlikely(dqnum == 0))
559850f3733SSergio Gonzalez Monroy 			continue;
560850f3733SSergio Gonzalez Monroy 
561850f3733SSergio Gonzalez Monroy 		app_stats.tx.dequeue_pkts += dqnum;
562850f3733SSergio Gonzalez Monroy 
563850f3733SSergio Gonzalez Monroy 		for (i = 0; i < dqnum; i++) {
564850f3733SSergio Gonzalez Monroy 			outp = mbufs[i]->port;
565850f3733SSergio Gonzalez Monroy 			/* skip ports that are not enabled */
566850f3733SSergio Gonzalez Monroy 			if ((portmask & (1 << outp)) == 0) {
567850f3733SSergio Gonzalez Monroy 				rte_pktmbuf_free(mbufs[i]);
568850f3733SSergio Gonzalez Monroy 				continue;
569850f3733SSergio Gonzalez Monroy 			}
570850f3733SSergio Gonzalez Monroy 
571850f3733SSergio Gonzalez Monroy 			outbuf = &tx_buffers[outp];
572850f3733SSergio Gonzalez Monroy 			outbuf->mbufs[outbuf->count++] = mbufs[i];
573850f3733SSergio Gonzalez Monroy 			if (outbuf->count == MAX_PKTS_BURST)
574850f3733SSergio Gonzalez Monroy 				flush_one_port(outbuf, outp);
575850f3733SSergio Gonzalez Monroy 		}
576850f3733SSergio Gonzalez Monroy 	}
577850f3733SSergio Gonzalez Monroy 
578850f3733SSergio Gonzalez Monroy 	return 0;
579850f3733SSergio Gonzalez Monroy }
580850f3733SSergio Gonzalez Monroy 
581850f3733SSergio Gonzalez Monroy int
582850f3733SSergio Gonzalez Monroy main(int argc, char **argv)
583850f3733SSergio Gonzalez Monroy {
584850f3733SSergio Gonzalez Monroy 	int ret;
585850f3733SSergio Gonzalez Monroy 	unsigned nb_ports;
586850f3733SSergio Gonzalez Monroy 	unsigned int lcore_id, last_lcore_id, master_lcore_id;
587850f3733SSergio Gonzalez Monroy 	uint8_t port_id;
588850f3733SSergio Gonzalez Monroy 	uint8_t nb_ports_available;
589850f3733SSergio Gonzalez Monroy 	struct worker_thread_args worker_args = {NULL, NULL};
5906ebc23d8SSergio Gonzalez Monroy 	struct send_thread_args send_args = {NULL, NULL};
591850f3733SSergio Gonzalez Monroy 	struct rte_ring *rx_to_workers;
592850f3733SSergio Gonzalez Monroy 	struct rte_ring *workers_to_tx;
593850f3733SSergio Gonzalez Monroy 
594850f3733SSergio Gonzalez Monroy 	/* catch ctrl-c so we can print on exit */
595850f3733SSergio Gonzalez Monroy 	signal(SIGINT, int_handler);
596850f3733SSergio Gonzalez Monroy 
597850f3733SSergio Gonzalez Monroy 	/* Initialize EAL */
598850f3733SSergio Gonzalez Monroy 	ret = rte_eal_init(argc, argv);
599850f3733SSergio Gonzalez Monroy 	if (ret < 0)
600850f3733SSergio Gonzalez Monroy 		return -1;
601850f3733SSergio Gonzalez Monroy 
602850f3733SSergio Gonzalez Monroy 	argc -= ret;
603850f3733SSergio Gonzalez Monroy 	argv += ret;
604850f3733SSergio Gonzalez Monroy 
605850f3733SSergio Gonzalez Monroy 	/* Parse the application specific arguments */
606850f3733SSergio Gonzalez Monroy 	ret = parse_args(argc, argv);
607850f3733SSergio Gonzalez Monroy 	if (ret < 0)
608850f3733SSergio Gonzalez Monroy 		return -1;
609850f3733SSergio Gonzalez Monroy 
610850f3733SSergio Gonzalez Monroy 	/* Check if we have enought cores */
611850f3733SSergio Gonzalez Monroy 	if (rte_lcore_count() < 3)
612850f3733SSergio Gonzalez Monroy 		rte_exit(EXIT_FAILURE, "Error, This application needs at "
613850f3733SSergio Gonzalez Monroy 				"least 3 logical cores to run:\n"
614850f3733SSergio Gonzalez Monroy 				"1 lcore for packet RX\n"
615850f3733SSergio Gonzalez Monroy 				"1 lcore for packet TX\n"
616850f3733SSergio Gonzalez Monroy 				"and at least 1 lcore for worker threads\n");
617850f3733SSergio Gonzalez Monroy 
618850f3733SSergio Gonzalez Monroy 	nb_ports = rte_eth_dev_count();
619850f3733SSergio Gonzalez Monroy 	if (nb_ports == 0)
620850f3733SSergio Gonzalez Monroy 		rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n");
621850f3733SSergio Gonzalez Monroy 	if (nb_ports != 1 && (nb_ports & 1))
622850f3733SSergio Gonzalez Monroy 		rte_exit(EXIT_FAILURE, "Error: number of ports must be even, except "
623850f3733SSergio Gonzalez Monroy 				"when using a single port\n");
624850f3733SSergio Gonzalez Monroy 
625*ea0c20eaSOlivier Matz 	mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL,
626*ea0c20eaSOlivier Matz 			MBUF_POOL_CACHE_SIZE, 0, MBUF_DATA_SIZE,
627*ea0c20eaSOlivier Matz 			rte_socket_id());
628850f3733SSergio Gonzalez Monroy 	if (mbuf_pool == NULL)
629850f3733SSergio Gonzalez Monroy 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
630850f3733SSergio Gonzalez Monroy 
631850f3733SSergio Gonzalez Monroy 	nb_ports_available = nb_ports;
632850f3733SSergio Gonzalez Monroy 
633850f3733SSergio Gonzalez Monroy 	/* initialize all ports */
634850f3733SSergio Gonzalez Monroy 	for (port_id = 0; port_id < nb_ports; port_id++) {
635850f3733SSergio Gonzalez Monroy 		/* skip ports that are not enabled */
636850f3733SSergio Gonzalez Monroy 		if ((portmask & (1 << port_id)) == 0) {
637850f3733SSergio Gonzalez Monroy 			printf("\nSkipping disabled port %d\n", port_id);
638850f3733SSergio Gonzalez Monroy 			nb_ports_available--;
639850f3733SSergio Gonzalez Monroy 			continue;
640850f3733SSergio Gonzalez Monroy 		}
641850f3733SSergio Gonzalez Monroy 		/* init port */
642850f3733SSergio Gonzalez Monroy 		printf("Initializing port %u... done\n", (unsigned) port_id);
643850f3733SSergio Gonzalez Monroy 
644850f3733SSergio Gonzalez Monroy 		if (configure_eth_port(port_id) != 0)
645850f3733SSergio Gonzalez Monroy 			rte_exit(EXIT_FAILURE, "Cannot initialize port %"PRIu8"\n",
646850f3733SSergio Gonzalez Monroy 					port_id);
647850f3733SSergio Gonzalez Monroy 	}
648850f3733SSergio Gonzalez Monroy 
649850f3733SSergio Gonzalez Monroy 	if (!nb_ports_available) {
650850f3733SSergio Gonzalez Monroy 		rte_exit(EXIT_FAILURE,
651850f3733SSergio Gonzalez Monroy 			"All available ports are disabled. Please set portmask.\n");
652850f3733SSergio Gonzalez Monroy 	}
653850f3733SSergio Gonzalez Monroy 
654850f3733SSergio Gonzalez Monroy 	/* Create rings for inter core communication */
655850f3733SSergio Gonzalez Monroy 	rx_to_workers = rte_ring_create("rx_to_workers", RING_SIZE, rte_socket_id(),
656850f3733SSergio Gonzalez Monroy 			RING_F_SP_ENQ);
657850f3733SSergio Gonzalez Monroy 	if (rx_to_workers == NULL)
658850f3733SSergio Gonzalez Monroy 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
659850f3733SSergio Gonzalez Monroy 
660850f3733SSergio Gonzalez Monroy 	workers_to_tx = rte_ring_create("workers_to_tx", RING_SIZE, rte_socket_id(),
661850f3733SSergio Gonzalez Monroy 			RING_F_SC_DEQ);
662850f3733SSergio Gonzalez Monroy 	if (workers_to_tx == NULL)
663850f3733SSergio Gonzalez Monroy 		rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
664850f3733SSergio Gonzalez Monroy 
6656ebc23d8SSergio Gonzalez Monroy 	if (!disable_reorder) {
6666ebc23d8SSergio Gonzalez Monroy 		send_args.buffer = rte_reorder_create("PKT_RO", rte_socket_id(),
6676ebc23d8SSergio Gonzalez Monroy 				REORDER_BUFFER_SIZE);
6686ebc23d8SSergio Gonzalez Monroy 		if (send_args.buffer == NULL)
6696ebc23d8SSergio Gonzalez Monroy 			rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
6706ebc23d8SSergio Gonzalez Monroy 	}
6716ebc23d8SSergio Gonzalez Monroy 
672850f3733SSergio Gonzalez Monroy 	last_lcore_id   = get_last_lcore_id();
673850f3733SSergio Gonzalez Monroy 	master_lcore_id = rte_get_master_lcore();
674850f3733SSergio Gonzalez Monroy 
675850f3733SSergio Gonzalez Monroy 	worker_args.ring_in  = rx_to_workers;
676850f3733SSergio Gonzalez Monroy 	worker_args.ring_out = workers_to_tx;
677850f3733SSergio Gonzalez Monroy 
678850f3733SSergio Gonzalez Monroy 	/* Start worker_thread() on all the available slave cores but the last 1 */
679850f3733SSergio Gonzalez Monroy 	for (lcore_id = 0; lcore_id <= get_previous_lcore_id(last_lcore_id); lcore_id++)
680850f3733SSergio Gonzalez Monroy 		if (rte_lcore_is_enabled(lcore_id) && lcore_id != master_lcore_id)
681850f3733SSergio Gonzalez Monroy 			rte_eal_remote_launch(worker_thread, (void *)&worker_args,
682850f3733SSergio Gonzalez Monroy 					lcore_id);
683850f3733SSergio Gonzalez Monroy 
6846ebc23d8SSergio Gonzalez Monroy 	if (disable_reorder) {
685850f3733SSergio Gonzalez Monroy 		/* Start tx_thread() on the last slave core */
686850f3733SSergio Gonzalez Monroy 		rte_eal_remote_launch((lcore_function_t *)tx_thread, workers_to_tx,
687850f3733SSergio Gonzalez Monroy 				last_lcore_id);
6886ebc23d8SSergio Gonzalez Monroy 	} else {
6896ebc23d8SSergio Gonzalez Monroy 		send_args.ring_in = workers_to_tx;
690850f3733SSergio Gonzalez Monroy 		/* Start send_thread() on the last slave core */
6916ebc23d8SSergio Gonzalez Monroy 		rte_eal_remote_launch((lcore_function_t *)send_thread,
6926ebc23d8SSergio Gonzalez Monroy 				(void *)&send_args, last_lcore_id);
6936ebc23d8SSergio Gonzalez Monroy 	}
694850f3733SSergio Gonzalez Monroy 
695850f3733SSergio Gonzalez Monroy 	/* Start rx_thread() on the master core */
696850f3733SSergio Gonzalez Monroy 	rx_thread(rx_to_workers);
697850f3733SSergio Gonzalez Monroy 
698850f3733SSergio Gonzalez Monroy 	RTE_LCORE_FOREACH_SLAVE(lcore_id) {
699850f3733SSergio Gonzalez Monroy 		if (rte_eal_wait_lcore(lcore_id) < 0)
700850f3733SSergio Gonzalez Monroy 			return -1;
701850f3733SSergio Gonzalez Monroy 	}
702850f3733SSergio Gonzalez Monroy 
703850f3733SSergio Gonzalez Monroy 	print_stats();
704850f3733SSergio Gonzalez Monroy 	return 0;
705850f3733SSergio Gonzalez Monroy }
706