1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2016 Intel Corporation
9 #include <rte_common.h>
10 #include <rte_errno.h>
11 #include <rte_ethdev.h>
12 #include <rte_lcore.h>
13 #include <rte_malloc.h>
15 #include <rte_mempool.h>
17 #include <rte_reorder.h>
19 #define RX_DESC_PER_QUEUE 1024
20 #define TX_DESC_PER_QUEUE 1024
22 #define MAX_PKTS_BURST 32
23 #define REORDER_BUFFER_SIZE 8192
24 #define MBUF_PER_POOL 65535
25 #define MBUF_POOL_CACHE_SIZE 250
27 #define RING_SIZE 16384
29 /* Macros for printing using RTE_LOG */
30 #define RTE_LOGTYPE_REORDERAPP RTE_LOGTYPE_USER1
32 unsigned int portmask;
33 unsigned int disable_reorder;
34 unsigned int insight_worker;
35 volatile uint8_t quit_signal;
37 static struct rte_mempool *mbuf_pool;
39 static struct rte_eth_conf port_conf_default;
41 struct worker_thread_args {
42 struct rte_ring *ring_in;
43 struct rte_ring *ring_out;
46 struct send_thread_args {
47 struct rte_ring *ring_in;
48 struct rte_reorder_buffer *buffer;
51 volatile struct app_stats {
54 uint64_t enqueue_pkts;
55 uint64_t enqueue_failed_pkts;
56 } rx __rte_cache_aligned;
59 uint64_t dequeue_pkts;
60 uint64_t enqueue_pkts;
61 uint64_t enqueue_failed_pkts;
62 } wkr __rte_cache_aligned;
65 uint64_t dequeue_pkts;
66 /* Too early pkts transmitted directly w/o reordering */
67 uint64_t early_pkts_txtd_woro;
68 /* Too early pkts failed from direct transmit */
69 uint64_t early_pkts_tx_failed_woro;
71 uint64_t ro_tx_failed_pkts;
72 } tx __rte_cache_aligned;
75 /* per worker lcore stats */
76 struct wkr_stats_per {
79 uint64_t enq_failed_pkts;
80 } __rte_cache_aligned;
82 static struct wkr_stats_per wkr_stats[RTE_MAX_LCORE] = { {0} };
84 * Get the last enabled lcore ID
87 * The last enabled lcore ID.
90 get_last_lcore_id(void)
94 for (i = RTE_MAX_LCORE - 1; i >= 0; i--)
95 if (rte_lcore_is_enabled(i))
101 * Get the previous enabled lcore ID
103 * The current lcore ID
105 * The previous enabled lcore ID or the current lcore
106 * ID if it is the first available core.
109 get_previous_lcore_id(unsigned int id)
113 for (i = id - 1; i >= 0; i--)
114 if (rte_lcore_is_enabled(i))
120 pktmbuf_free_bulk(struct rte_mbuf *mbuf_table[], unsigned n)
124 for (i = 0; i < n; i++)
125 rte_pktmbuf_free(mbuf_table[i]);
130 print_usage(const char *prgname)
132 printf("%s [EAL options] -- -p PORTMASK\n"
133 " -p PORTMASK: hexadecimal bitmask of ports to configure\n",
138 parse_portmask(const char *portmask)
143 /* parse hexadecimal string */
144 pm = strtoul(portmask, &end, 16);
145 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
154 /* Parse the argument given in the command line of the application */
156 parse_args(int argc, char **argv)
161 char *prgname = argv[0];
162 static struct option lgopts[] = {
163 {"disable-reorder", 0, 0, 0},
164 {"insight-worker", 0, 0, 0},
170 while ((opt = getopt_long(argc, argvopt, "p:",
171 lgopts, &option_index)) != EOF) {
175 portmask = parse_portmask(optarg);
177 printf("invalid portmask\n");
178 print_usage(prgname);
184 if (!strcmp(lgopts[option_index].name, "disable-reorder")) {
185 printf("reorder disabled\n");
188 if (!strcmp(lgopts[option_index].name,
190 printf("print all worker statistics\n");
195 print_usage(prgname);
200 print_usage(prgname);
204 argv[optind-1] = prgname;
205 optind = 1; /* reset getopt lib */
210 * Tx buffer error callback
213 flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
214 void *userdata __rte_unused) {
216 /* free the mbufs which failed from transmit */
217 app_stats.tx.ro_tx_failed_pkts += count;
218 RTE_LOG_DP(DEBUG, REORDERAPP, "%s:Packet loss with tx_burst\n", __func__);
219 pktmbuf_free_bulk(unsent, count);
224 free_tx_buffers(struct rte_eth_dev_tx_buffer *tx_buffer[]) {
227 /* initialize buffers for all ports */
228 RTE_ETH_FOREACH_DEV(port_id) {
229 /* skip ports that are not enabled */
230 if ((portmask & (1 << port_id)) == 0)
233 rte_free(tx_buffer[port_id]);
239 configure_tx_buffers(struct rte_eth_dev_tx_buffer *tx_buffer[])
244 /* initialize buffers for all ports */
245 RTE_ETH_FOREACH_DEV(port_id) {
246 /* skip ports that are not enabled */
247 if ((portmask & (1 << port_id)) == 0)
250 /* Initialize TX buffers */
251 tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
252 RTE_ETH_TX_BUFFER_SIZE(MAX_PKTS_BURST), 0,
253 rte_eth_dev_socket_id(port_id));
254 if (tx_buffer[port_id] == NULL)
255 rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
258 rte_eth_tx_buffer_init(tx_buffer[port_id], MAX_PKTS_BURST);
260 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
261 flush_tx_error_callback, NULL);
263 rte_exit(EXIT_FAILURE,
264 "Cannot set error callback for tx buffer on port %u\n",
271 configure_eth_port(uint16_t port_id)
273 struct rte_ether_addr addr;
274 const uint16_t rxRings = 1, txRings = 1;
277 uint16_t nb_rxd = RX_DESC_PER_QUEUE;
278 uint16_t nb_txd = TX_DESC_PER_QUEUE;
279 struct rte_eth_dev_info dev_info;
280 struct rte_eth_txconf txconf;
281 struct rte_eth_conf port_conf = port_conf_default;
283 if (!rte_eth_dev_is_valid_port(port_id))
286 rte_eth_dev_info_get(port_id, &dev_info);
287 if (dev_info.tx_offload_capa & DEV_TX_OFFLOAD_MBUF_FAST_FREE)
288 port_conf.txmode.offloads |=
289 DEV_TX_OFFLOAD_MBUF_FAST_FREE;
290 ret = rte_eth_dev_configure(port_id, rxRings, txRings, &port_conf_default);
294 ret = rte_eth_dev_adjust_nb_rx_tx_desc(port_id, &nb_rxd, &nb_txd);
298 for (q = 0; q < rxRings; q++) {
299 ret = rte_eth_rx_queue_setup(port_id, q, nb_rxd,
300 rte_eth_dev_socket_id(port_id), NULL,
306 txconf = dev_info.default_txconf;
307 txconf.offloads = port_conf.txmode.offloads;
308 for (q = 0; q < txRings; q++) {
309 ret = rte_eth_tx_queue_setup(port_id, q, nb_txd,
310 rte_eth_dev_socket_id(port_id), &txconf);
315 ret = rte_eth_dev_start(port_id);
319 rte_eth_macaddr_get(port_id, &addr);
320 printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
321 " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
323 addr.addr_bytes[0], addr.addr_bytes[1],
324 addr.addr_bytes[2], addr.addr_bytes[3],
325 addr.addr_bytes[4], addr.addr_bytes[5]);
327 rte_eth_promiscuous_enable(port_id);
336 struct rte_eth_stats eth_stats;
337 unsigned int lcore_id, last_lcore_id, master_lcore_id, end_w_lcore_id;
339 last_lcore_id = get_last_lcore_id();
340 master_lcore_id = rte_get_master_lcore();
341 end_w_lcore_id = get_previous_lcore_id(last_lcore_id);
343 printf("\nRX thread stats:\n");
344 printf(" - Pkts rxd: %"PRIu64"\n",
345 app_stats.rx.rx_pkts);
346 printf(" - Pkts enqd to workers ring: %"PRIu64"\n",
347 app_stats.rx.enqueue_pkts);
349 for (lcore_id = 0; lcore_id <= end_w_lcore_id; lcore_id++) {
351 && rte_lcore_is_enabled(lcore_id)
352 && lcore_id != master_lcore_id) {
353 printf("\nWorker thread stats on core [%u]:\n",
355 printf(" - Pkts deqd from workers ring: %"PRIu64"\n",
356 wkr_stats[lcore_id].deq_pkts);
357 printf(" - Pkts enqd to tx ring: %"PRIu64"\n",
358 wkr_stats[lcore_id].enq_pkts);
359 printf(" - Pkts enq to tx failed: %"PRIu64"\n",
360 wkr_stats[lcore_id].enq_failed_pkts);
363 app_stats.wkr.dequeue_pkts += wkr_stats[lcore_id].deq_pkts;
364 app_stats.wkr.enqueue_pkts += wkr_stats[lcore_id].enq_pkts;
365 app_stats.wkr.enqueue_failed_pkts +=
366 wkr_stats[lcore_id].enq_failed_pkts;
369 printf("\nWorker thread stats:\n");
370 printf(" - Pkts deqd from workers ring: %"PRIu64"\n",
371 app_stats.wkr.dequeue_pkts);
372 printf(" - Pkts enqd to tx ring: %"PRIu64"\n",
373 app_stats.wkr.enqueue_pkts);
374 printf(" - Pkts enq to tx failed: %"PRIu64"\n",
375 app_stats.wkr.enqueue_failed_pkts);
377 printf("\nTX stats:\n");
378 printf(" - Pkts deqd from tx ring: %"PRIu64"\n",
379 app_stats.tx.dequeue_pkts);
380 printf(" - Ro Pkts transmitted: %"PRIu64"\n",
381 app_stats.tx.ro_tx_pkts);
382 printf(" - Ro Pkts tx failed: %"PRIu64"\n",
383 app_stats.tx.ro_tx_failed_pkts);
384 printf(" - Pkts transmitted w/o reorder: %"PRIu64"\n",
385 app_stats.tx.early_pkts_txtd_woro);
386 printf(" - Pkts tx failed w/o reorder: %"PRIu64"\n",
387 app_stats.tx.early_pkts_tx_failed_woro);
389 RTE_ETH_FOREACH_DEV(i) {
390 rte_eth_stats_get(i, ð_stats);
391 printf("\nPort %u stats:\n", i);
392 printf(" - Pkts in: %"PRIu64"\n", eth_stats.ipackets);
393 printf(" - Pkts out: %"PRIu64"\n", eth_stats.opackets);
394 printf(" - In Errs: %"PRIu64"\n", eth_stats.ierrors);
395 printf(" - Out Errs: %"PRIu64"\n", eth_stats.oerrors);
396 printf(" - Mbuf Errs: %"PRIu64"\n", eth_stats.rx_nombuf);
401 int_handler(int sig_num)
403 printf("Exiting on signal %d\n", sig_num);
408 * This thread receives mbufs from the port and affects them an internal
409 * sequence number to keep track of their order of arrival through an
411 * The mbufs are then passed to the worker threads via the rx_to_workers
415 rx_thread(struct rte_ring *ring_out)
421 struct rte_mbuf *pkts[MAX_PKTS_BURST];
423 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
426 while (!quit_signal) {
428 RTE_ETH_FOREACH_DEV(port_id) {
429 if ((portmask & (1 << port_id)) != 0) {
431 /* receive packets */
432 nb_rx_pkts = rte_eth_rx_burst(port_id, 0,
433 pkts, MAX_PKTS_BURST);
434 if (nb_rx_pkts == 0) {
435 RTE_LOG_DP(DEBUG, REORDERAPP,
436 "%s():Received zero packets\n", __func__);
439 app_stats.rx.rx_pkts += nb_rx_pkts;
441 /* mark sequence number */
442 for (i = 0; i < nb_rx_pkts; )
443 pkts[i++]->seqn = seqn++;
445 /* enqueue to rx_to_workers ring */
446 ret = rte_ring_enqueue_burst(ring_out,
447 (void *)pkts, nb_rx_pkts, NULL);
448 app_stats.rx.enqueue_pkts += ret;
449 if (unlikely(ret < nb_rx_pkts)) {
450 app_stats.rx.enqueue_failed_pkts +=
452 pktmbuf_free_bulk(&pkts[ret], nb_rx_pkts - ret);
461 * This thread takes bursts of packets from the rx_to_workers ring and
462 * Changes the input port value to output port value. And feds it to
466 worker_thread(void *args_ptr)
468 const uint16_t nb_ports = rte_eth_dev_count_avail();
470 uint16_t burst_size = 0;
471 struct worker_thread_args *args;
472 struct rte_mbuf *burst_buffer[MAX_PKTS_BURST] = { NULL };
473 struct rte_ring *ring_in, *ring_out;
474 const unsigned xor_val = (nb_ports > 1);
475 unsigned int core_id = rte_lcore_id();
477 args = (struct worker_thread_args *) args_ptr;
478 ring_in = args->ring_in;
479 ring_out = args->ring_out;
481 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
484 while (!quit_signal) {
486 /* dequeue the mbufs from rx_to_workers ring */
487 burst_size = rte_ring_dequeue_burst(ring_in,
488 (void *)burst_buffer, MAX_PKTS_BURST, NULL);
489 if (unlikely(burst_size == 0))
492 wkr_stats[core_id].deq_pkts += burst_size;
494 /* just do some operation on mbuf */
495 for (i = 0; i < burst_size;)
496 burst_buffer[i++]->port ^= xor_val;
498 /* enqueue the modified mbufs to workers_to_tx ring */
499 ret = rte_ring_enqueue_burst(ring_out, (void *)burst_buffer,
501 wkr_stats[core_id].enq_pkts += ret;
502 if (unlikely(ret < burst_size)) {
503 /* Return the mbufs to their respective pool, dropping packets */
504 wkr_stats[core_id].enq_failed_pkts += burst_size - ret;
505 pktmbuf_free_bulk(&burst_buffer[ret], burst_size - ret);
512 * Dequeue mbufs from the workers_to_tx ring and reorder them before
516 send_thread(struct send_thread_args *args)
519 unsigned int i, dret;
520 uint16_t nb_dq_mbufs;
523 struct rte_mbuf *mbufs[MAX_PKTS_BURST];
524 struct rte_mbuf *rombufs[MAX_PKTS_BURST] = {NULL};
525 static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
527 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__, rte_lcore_id());
529 configure_tx_buffers(tx_buffer);
531 while (!quit_signal) {
533 /* deque the mbufs from workers_to_tx ring */
534 nb_dq_mbufs = rte_ring_dequeue_burst(args->ring_in,
535 (void *)mbufs, MAX_PKTS_BURST, NULL);
537 if (unlikely(nb_dq_mbufs == 0))
540 app_stats.tx.dequeue_pkts += nb_dq_mbufs;
542 for (i = 0; i < nb_dq_mbufs; i++) {
543 /* send dequeued mbufs for reordering */
544 ret = rte_reorder_insert(args->buffer, mbufs[i]);
546 if (ret == -1 && rte_errno == ERANGE) {
547 /* Too early pkts should be transmitted out directly */
548 RTE_LOG_DP(DEBUG, REORDERAPP,
549 "%s():Cannot reorder early packet "
550 "direct enqueuing to TX\n", __func__);
551 outp = mbufs[i]->port;
552 if ((portmask & (1 << outp)) == 0) {
553 rte_pktmbuf_free(mbufs[i]);
556 if (rte_eth_tx_burst(outp, 0, (void *)mbufs[i], 1) != 1) {
557 rte_pktmbuf_free(mbufs[i]);
558 app_stats.tx.early_pkts_tx_failed_woro++;
560 app_stats.tx.early_pkts_txtd_woro++;
561 } else if (ret == -1 && rte_errno == ENOSPC) {
563 * Early pkts just outside of window should be dropped
565 rte_pktmbuf_free(mbufs[i]);
570 * drain MAX_PKTS_BURST of reordered
573 dret = rte_reorder_drain(args->buffer, rombufs, MAX_PKTS_BURST);
574 for (i = 0; i < dret; i++) {
576 struct rte_eth_dev_tx_buffer *outbuf;
579 outp1 = rombufs[i]->port;
580 /* skip ports that are not enabled */
581 if ((portmask & (1 << outp1)) == 0) {
582 rte_pktmbuf_free(rombufs[i]);
586 outbuf = tx_buffer[outp1];
587 sent = rte_eth_tx_buffer(outp1, 0, outbuf, rombufs[i]);
589 app_stats.tx.ro_tx_pkts += sent;
593 free_tx_buffers(tx_buffer);
599 * Dequeue mbufs from the workers_to_tx ring and transmit them
602 tx_thread(struct rte_ring *ring_in)
607 struct rte_mbuf *mbufs[MAX_PKTS_BURST];
608 struct rte_eth_dev_tx_buffer *outbuf;
609 static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
611 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
614 configure_tx_buffers(tx_buffer);
616 while (!quit_signal) {
618 /* deque the mbufs from workers_to_tx ring */
619 dqnum = rte_ring_dequeue_burst(ring_in,
620 (void *)mbufs, MAX_PKTS_BURST, NULL);
622 if (unlikely(dqnum == 0))
625 app_stats.tx.dequeue_pkts += dqnum;
627 for (i = 0; i < dqnum; i++) {
628 outp = mbufs[i]->port;
629 /* skip ports that are not enabled */
630 if ((portmask & (1 << outp)) == 0) {
631 rte_pktmbuf_free(mbufs[i]);
635 outbuf = tx_buffer[outp];
636 sent = rte_eth_tx_buffer(outp, 0, outbuf, mbufs[i]);
638 app_stats.tx.ro_tx_pkts += sent;
646 main(int argc, char **argv)
650 unsigned int lcore_id, last_lcore_id, master_lcore_id;
652 uint16_t nb_ports_available;
653 struct worker_thread_args worker_args = {NULL, NULL};
654 struct send_thread_args send_args = {NULL, NULL};
655 struct rte_ring *rx_to_workers;
656 struct rte_ring *workers_to_tx;
658 /* catch ctrl-c so we can print on exit */
659 signal(SIGINT, int_handler);
662 ret = rte_eal_init(argc, argv);
669 /* Parse the application specific arguments */
670 ret = parse_args(argc, argv);
674 /* Check if we have enought cores */
675 if (rte_lcore_count() < 3)
676 rte_exit(EXIT_FAILURE, "Error, This application needs at "
677 "least 3 logical cores to run:\n"
678 "1 lcore for packet RX\n"
679 "1 lcore for packet TX\n"
680 "and at least 1 lcore for worker threads\n");
682 nb_ports = rte_eth_dev_count_avail();
684 rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n");
685 if (nb_ports != 1 && (nb_ports & 1))
686 rte_exit(EXIT_FAILURE, "Error: number of ports must be even, except "
687 "when using a single port\n");
689 mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL,
690 MBUF_POOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
692 if (mbuf_pool == NULL)
693 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
695 nb_ports_available = nb_ports;
697 /* initialize all ports */
698 RTE_ETH_FOREACH_DEV(port_id) {
699 /* skip ports that are not enabled */
700 if ((portmask & (1 << port_id)) == 0) {
701 printf("\nSkipping disabled port %d\n", port_id);
702 nb_ports_available--;
706 printf("Initializing port %u... done\n", port_id);
708 if (configure_eth_port(port_id) != 0)
709 rte_exit(EXIT_FAILURE, "Cannot initialize port %"PRIu8"\n",
713 if (!nb_ports_available) {
714 rte_exit(EXIT_FAILURE,
715 "All available ports are disabled. Please set portmask.\n");
718 /* Create rings for inter core communication */
719 rx_to_workers = rte_ring_create("rx_to_workers", RING_SIZE, rte_socket_id(),
721 if (rx_to_workers == NULL)
722 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
724 workers_to_tx = rte_ring_create("workers_to_tx", RING_SIZE, rte_socket_id(),
726 if (workers_to_tx == NULL)
727 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
729 if (!disable_reorder) {
730 send_args.buffer = rte_reorder_create("PKT_RO", rte_socket_id(),
731 REORDER_BUFFER_SIZE);
732 if (send_args.buffer == NULL)
733 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
736 last_lcore_id = get_last_lcore_id();
737 master_lcore_id = rte_get_master_lcore();
739 worker_args.ring_in = rx_to_workers;
740 worker_args.ring_out = workers_to_tx;
742 /* Start worker_thread() on all the available slave cores but the last 1 */
743 for (lcore_id = 0; lcore_id <= get_previous_lcore_id(last_lcore_id); lcore_id++)
744 if (rte_lcore_is_enabled(lcore_id) && lcore_id != master_lcore_id)
745 rte_eal_remote_launch(worker_thread, (void *)&worker_args,
748 if (disable_reorder) {
749 /* Start tx_thread() on the last slave core */
750 rte_eal_remote_launch((lcore_function_t *)tx_thread, workers_to_tx,
753 send_args.ring_in = workers_to_tx;
754 /* Start send_thread() on the last slave core */
755 rte_eal_remote_launch((lcore_function_t *)send_thread,
756 (void *)&send_args, last_lcore_id);
759 /* Start rx_thread() on the master core */
760 rx_thread(rx_to_workers);
762 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
763 if (rte_eal_wait_lcore(lcore_id) < 0)