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