4 * Copyright(c) 2010-2016 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <rte_common.h>
39 #include <rte_errno.h>
40 #include <rte_ethdev.h>
41 #include <rte_lcore.h>
42 #include <rte_malloc.h>
44 #include <rte_mempool.h>
46 #include <rte_reorder.h>
48 #define RX_DESC_PER_QUEUE 128
49 #define TX_DESC_PER_QUEUE 512
51 #define MAX_PKTS_BURST 32
52 #define REORDER_BUFFER_SIZE 8192
53 #define MBUF_PER_POOL 65535
54 #define MBUF_POOL_CACHE_SIZE 250
56 #define RING_SIZE 16384
58 /* Macros for printing using RTE_LOG */
59 #define RTE_LOGTYPE_REORDERAPP RTE_LOGTYPE_USER1
61 unsigned int portmask;
62 unsigned int disable_reorder;
63 volatile uint8_t quit_signal;
65 static struct rte_mempool *mbuf_pool;
67 static struct rte_eth_conf port_conf_default;
69 struct worker_thread_args {
70 struct rte_ring *ring_in;
71 struct rte_ring *ring_out;
74 struct send_thread_args {
75 struct rte_ring *ring_in;
76 struct rte_reorder_buffer *buffer;
79 volatile struct app_stats {
82 uint64_t enqueue_pkts;
83 uint64_t enqueue_failed_pkts;
84 } rx __rte_cache_aligned;
87 uint64_t dequeue_pkts;
88 uint64_t enqueue_pkts;
89 uint64_t enqueue_failed_pkts;
90 } wkr __rte_cache_aligned;
93 uint64_t dequeue_pkts;
94 /* Too early pkts transmitted directly w/o reordering */
95 uint64_t early_pkts_txtd_woro;
96 /* Too early pkts failed from direct transmit */
97 uint64_t early_pkts_tx_failed_woro;
99 uint64_t ro_tx_failed_pkts;
100 } tx __rte_cache_aligned;
104 * Get the last enabled lcore ID
107 * The last enabled lcore ID.
110 get_last_lcore_id(void)
114 for (i = RTE_MAX_LCORE - 1; i >= 0; i--)
115 if (rte_lcore_is_enabled(i))
121 * Get the previous enabled lcore ID
123 * The current lcore ID
125 * The previous enabled lcore ID or the current lcore
126 * ID if it is the first available core.
129 get_previous_lcore_id(unsigned int id)
133 for (i = id - 1; i >= 0; i--)
134 if (rte_lcore_is_enabled(i))
140 pktmbuf_free_bulk(struct rte_mbuf *mbuf_table[], unsigned n)
144 for (i = 0; i < n; i++)
145 rte_pktmbuf_free(mbuf_table[i]);
150 print_usage(const char *prgname)
152 printf("%s [EAL options] -- -p PORTMASK\n"
153 " -p PORTMASK: hexadecimal bitmask of ports to configure\n",
158 parse_portmask(const char *portmask)
163 /* parse hexadecimal string */
164 pm = strtoul(portmask, &end, 16);
165 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
174 /* Parse the argument given in the command line of the application */
176 parse_args(int argc, char **argv)
181 char *prgname = argv[0];
182 static struct option lgopts[] = {
183 {"disable-reorder", 0, 0, 0},
189 while ((opt = getopt_long(argc, argvopt, "p:",
190 lgopts, &option_index)) != EOF) {
194 portmask = parse_portmask(optarg);
196 printf("invalid portmask\n");
197 print_usage(prgname);
203 if (!strcmp(lgopts[option_index].name, "disable-reorder")) {
204 printf("reorder disabled\n");
209 print_usage(prgname);
214 print_usage(prgname);
218 argv[optind-1] = prgname;
219 optind = 1; /* reset getopt lib */
224 * Tx buffer error callback
227 flush_tx_error_callback(struct rte_mbuf **unsent, uint16_t count,
228 void *userdata __rte_unused) {
230 /* free the mbufs which failed from transmit */
231 app_stats.tx.ro_tx_failed_pkts += count;
232 RTE_LOG_DP(DEBUG, REORDERAPP, "%s:Packet loss with tx_burst\n", __func__);
233 pktmbuf_free_bulk(unsent, count);
238 free_tx_buffers(struct rte_eth_dev_tx_buffer *tx_buffer[]) {
239 const uint8_t nb_ports = rte_eth_dev_count();
242 /* initialize buffers for all ports */
243 for (port_id = 0; port_id < nb_ports; port_id++) {
244 /* skip ports that are not enabled */
245 if ((portmask & (1 << port_id)) == 0)
248 rte_free(tx_buffer[port_id]);
254 configure_tx_buffers(struct rte_eth_dev_tx_buffer *tx_buffer[])
256 const uint8_t nb_ports = rte_eth_dev_count();
260 /* initialize buffers for all ports */
261 for (port_id = 0; port_id < nb_ports; port_id++) {
262 /* skip ports that are not enabled */
263 if ((portmask & (1 << port_id)) == 0)
266 /* Initialize TX buffers */
267 tx_buffer[port_id] = rte_zmalloc_socket("tx_buffer",
268 RTE_ETH_TX_BUFFER_SIZE(MAX_PKTS_BURST), 0,
269 rte_eth_dev_socket_id(port_id));
270 if (tx_buffer[port_id] == NULL)
271 rte_exit(EXIT_FAILURE, "Cannot allocate buffer for tx on port %u\n",
274 rte_eth_tx_buffer_init(tx_buffer[port_id], MAX_PKTS_BURST);
276 ret = rte_eth_tx_buffer_set_err_callback(tx_buffer[port_id],
277 flush_tx_error_callback, NULL);
279 rte_exit(EXIT_FAILURE, "Cannot set error callback for "
280 "tx buffer on port %u\n", (unsigned) port_id);
286 configure_eth_port(uint8_t port_id)
288 struct ether_addr addr;
289 const uint16_t rxRings = 1, txRings = 1;
290 const uint8_t nb_ports = rte_eth_dev_count();
294 if (port_id > nb_ports)
297 ret = rte_eth_dev_configure(port_id, rxRings, txRings, &port_conf_default);
301 for (q = 0; q < rxRings; q++) {
302 ret = rte_eth_rx_queue_setup(port_id, q, RX_DESC_PER_QUEUE,
303 rte_eth_dev_socket_id(port_id), NULL,
309 for (q = 0; q < txRings; q++) {
310 ret = rte_eth_tx_queue_setup(port_id, q, TX_DESC_PER_QUEUE,
311 rte_eth_dev_socket_id(port_id), NULL);
316 ret = rte_eth_dev_start(port_id);
320 rte_eth_macaddr_get(port_id, &addr);
321 printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
322 " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
324 addr.addr_bytes[0], addr.addr_bytes[1],
325 addr.addr_bytes[2], addr.addr_bytes[3],
326 addr.addr_bytes[4], addr.addr_bytes[5]);
328 rte_eth_promiscuous_enable(port_id);
336 const uint8_t nb_ports = rte_eth_dev_count();
338 struct rte_eth_stats eth_stats;
340 printf("\nRX thread stats:\n");
341 printf(" - Pkts rxd: %"PRIu64"\n",
342 app_stats.rx.rx_pkts);
343 printf(" - Pkts enqd to workers ring: %"PRIu64"\n",
344 app_stats.rx.enqueue_pkts);
346 printf("\nWorker thread stats:\n");
347 printf(" - Pkts deqd from workers ring: %"PRIu64"\n",
348 app_stats.wkr.dequeue_pkts);
349 printf(" - Pkts enqd to tx ring: %"PRIu64"\n",
350 app_stats.wkr.enqueue_pkts);
351 printf(" - Pkts enq to tx failed: %"PRIu64"\n",
352 app_stats.wkr.enqueue_failed_pkts);
354 printf("\nTX stats:\n");
355 printf(" - Pkts deqd from tx ring: %"PRIu64"\n",
356 app_stats.tx.dequeue_pkts);
357 printf(" - Ro Pkts transmitted: %"PRIu64"\n",
358 app_stats.tx.ro_tx_pkts);
359 printf(" - Ro Pkts tx failed: %"PRIu64"\n",
360 app_stats.tx.ro_tx_failed_pkts);
361 printf(" - Pkts transmitted w/o reorder: %"PRIu64"\n",
362 app_stats.tx.early_pkts_txtd_woro);
363 printf(" - Pkts tx failed w/o reorder: %"PRIu64"\n",
364 app_stats.tx.early_pkts_tx_failed_woro);
366 for (i = 0; i < nb_ports; i++) {
367 rte_eth_stats_get(i, ð_stats);
368 printf("\nPort %u stats:\n", i);
369 printf(" - Pkts in: %"PRIu64"\n", eth_stats.ipackets);
370 printf(" - Pkts out: %"PRIu64"\n", eth_stats.opackets);
371 printf(" - In Errs: %"PRIu64"\n", eth_stats.ierrors);
372 printf(" - Out Errs: %"PRIu64"\n", eth_stats.oerrors);
373 printf(" - Mbuf Errs: %"PRIu64"\n", eth_stats.rx_nombuf);
378 int_handler(int sig_num)
380 printf("Exiting on signal %d\n", sig_num);
385 * This thread receives mbufs from the port and affects them an internal
386 * sequence number to keep track of their order of arrival through an
388 * The mbufs are then passed to the worker threads via the rx_to_workers
392 rx_thread(struct rte_ring *ring_out)
394 const uint8_t nb_ports = rte_eth_dev_count();
399 struct rte_mbuf *pkts[MAX_PKTS_BURST];
401 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
404 while (!quit_signal) {
406 for (port_id = 0; port_id < nb_ports; port_id++) {
407 if ((portmask & (1 << port_id)) != 0) {
409 /* receive packets */
410 nb_rx_pkts = rte_eth_rx_burst(port_id, 0,
411 pkts, MAX_PKTS_BURST);
412 if (nb_rx_pkts == 0) {
413 RTE_LOG_DP(DEBUG, REORDERAPP,
414 "%s():Received zero packets\n", __func__);
417 app_stats.rx.rx_pkts += nb_rx_pkts;
419 /* mark sequence number */
420 for (i = 0; i < nb_rx_pkts; )
421 pkts[i++]->seqn = seqn++;
423 /* enqueue to rx_to_workers ring */
424 ret = rte_ring_enqueue_burst(ring_out,
425 (void *)pkts, nb_rx_pkts, NULL);
426 app_stats.rx.enqueue_pkts += ret;
427 if (unlikely(ret < nb_rx_pkts)) {
428 app_stats.rx.enqueue_failed_pkts +=
430 pktmbuf_free_bulk(&pkts[ret], nb_rx_pkts - ret);
439 * This thread takes bursts of packets from the rx_to_workers ring and
440 * Changes the input port value to output port value. And feds it to
444 worker_thread(void *args_ptr)
446 const uint8_t nb_ports = rte_eth_dev_count();
448 uint16_t burst_size = 0;
449 struct worker_thread_args *args;
450 struct rte_mbuf *burst_buffer[MAX_PKTS_BURST] = { NULL };
451 struct rte_ring *ring_in, *ring_out;
452 const unsigned xor_val = (nb_ports > 1);
454 args = (struct worker_thread_args *) args_ptr;
455 ring_in = args->ring_in;
456 ring_out = args->ring_out;
458 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
461 while (!quit_signal) {
463 /* dequeue the mbufs from rx_to_workers ring */
464 burst_size = rte_ring_dequeue_burst(ring_in,
465 (void *)burst_buffer, MAX_PKTS_BURST, NULL);
466 if (unlikely(burst_size == 0))
469 __sync_fetch_and_add(&app_stats.wkr.dequeue_pkts, burst_size);
471 /* just do some operation on mbuf */
472 for (i = 0; i < burst_size;)
473 burst_buffer[i++]->port ^= xor_val;
475 /* enqueue the modified mbufs to workers_to_tx ring */
476 ret = rte_ring_enqueue_burst(ring_out, (void *)burst_buffer,
478 __sync_fetch_and_add(&app_stats.wkr.enqueue_pkts, ret);
479 if (unlikely(ret < burst_size)) {
480 /* Return the mbufs to their respective pool, dropping packets */
481 __sync_fetch_and_add(&app_stats.wkr.enqueue_failed_pkts,
482 (int)burst_size - ret);
483 pktmbuf_free_bulk(&burst_buffer[ret], burst_size - ret);
490 * Dequeue mbufs from the workers_to_tx ring and reorder them before
494 send_thread(struct send_thread_args *args)
497 unsigned int i, dret;
498 uint16_t nb_dq_mbufs;
501 struct rte_mbuf *mbufs[MAX_PKTS_BURST];
502 struct rte_mbuf *rombufs[MAX_PKTS_BURST] = {NULL};
503 static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
505 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__, rte_lcore_id());
507 configure_tx_buffers(tx_buffer);
509 while (!quit_signal) {
511 /* deque the mbufs from workers_to_tx ring */
512 nb_dq_mbufs = rte_ring_dequeue_burst(args->ring_in,
513 (void *)mbufs, MAX_PKTS_BURST, NULL);
515 if (unlikely(nb_dq_mbufs == 0))
518 app_stats.tx.dequeue_pkts += nb_dq_mbufs;
520 for (i = 0; i < nb_dq_mbufs; i++) {
521 /* send dequeued mbufs for reordering */
522 ret = rte_reorder_insert(args->buffer, mbufs[i]);
524 if (ret == -1 && rte_errno == ERANGE) {
525 /* Too early pkts should be transmitted out directly */
526 RTE_LOG_DP(DEBUG, REORDERAPP,
527 "%s():Cannot reorder early packet "
528 "direct enqueuing to TX\n", __func__);
529 outp = mbufs[i]->port;
530 if ((portmask & (1 << outp)) == 0) {
531 rte_pktmbuf_free(mbufs[i]);
534 if (rte_eth_tx_burst(outp, 0, (void *)mbufs[i], 1) != 1) {
535 rte_pktmbuf_free(mbufs[i]);
536 app_stats.tx.early_pkts_tx_failed_woro++;
538 app_stats.tx.early_pkts_txtd_woro++;
539 } else if (ret == -1 && rte_errno == ENOSPC) {
541 * Early pkts just outside of window should be dropped
543 rte_pktmbuf_free(mbufs[i]);
548 * drain MAX_PKTS_BURST of reordered
551 dret = rte_reorder_drain(args->buffer, rombufs, MAX_PKTS_BURST);
552 for (i = 0; i < dret; i++) {
554 struct rte_eth_dev_tx_buffer *outbuf;
557 outp1 = rombufs[i]->port;
558 /* skip ports that are not enabled */
559 if ((portmask & (1 << outp1)) == 0) {
560 rte_pktmbuf_free(rombufs[i]);
564 outbuf = tx_buffer[outp1];
565 sent = rte_eth_tx_buffer(outp1, 0, outbuf, rombufs[i]);
567 app_stats.tx.ro_tx_pkts += sent;
571 free_tx_buffers(tx_buffer);
577 * Dequeue mbufs from the workers_to_tx ring and transmit them
580 tx_thread(struct rte_ring *ring_in)
585 struct rte_mbuf *mbufs[MAX_PKTS_BURST];
586 struct rte_eth_dev_tx_buffer *outbuf;
587 static struct rte_eth_dev_tx_buffer *tx_buffer[RTE_MAX_ETHPORTS];
589 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
592 configure_tx_buffers(tx_buffer);
594 while (!quit_signal) {
596 /* deque the mbufs from workers_to_tx ring */
597 dqnum = rte_ring_dequeue_burst(ring_in,
598 (void *)mbufs, MAX_PKTS_BURST, NULL);
600 if (unlikely(dqnum == 0))
603 app_stats.tx.dequeue_pkts += dqnum;
605 for (i = 0; i < dqnum; i++) {
606 outp = mbufs[i]->port;
607 /* skip ports that are not enabled */
608 if ((portmask & (1 << outp)) == 0) {
609 rte_pktmbuf_free(mbufs[i]);
613 outbuf = tx_buffer[outp];
614 sent = rte_eth_tx_buffer(outp, 0, outbuf, mbufs[i]);
616 app_stats.tx.ro_tx_pkts += sent;
624 main(int argc, char **argv)
628 unsigned int lcore_id, last_lcore_id, master_lcore_id;
630 uint8_t nb_ports_available;
631 struct worker_thread_args worker_args = {NULL, NULL};
632 struct send_thread_args send_args = {NULL, NULL};
633 struct rte_ring *rx_to_workers;
634 struct rte_ring *workers_to_tx;
636 /* catch ctrl-c so we can print on exit */
637 signal(SIGINT, int_handler);
640 ret = rte_eal_init(argc, argv);
647 /* Parse the application specific arguments */
648 ret = parse_args(argc, argv);
652 /* Check if we have enought cores */
653 if (rte_lcore_count() < 3)
654 rte_exit(EXIT_FAILURE, "Error, This application needs at "
655 "least 3 logical cores to run:\n"
656 "1 lcore for packet RX\n"
657 "1 lcore for packet TX\n"
658 "and at least 1 lcore for worker threads\n");
660 nb_ports = rte_eth_dev_count();
662 rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n");
663 if (nb_ports != 1 && (nb_ports & 1))
664 rte_exit(EXIT_FAILURE, "Error: number of ports must be even, except "
665 "when using a single port\n");
667 mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL,
668 MBUF_POOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
670 if (mbuf_pool == NULL)
671 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
673 nb_ports_available = nb_ports;
675 /* initialize all ports */
676 for (port_id = 0; port_id < nb_ports; port_id++) {
677 /* skip ports that are not enabled */
678 if ((portmask & (1 << port_id)) == 0) {
679 printf("\nSkipping disabled port %d\n", port_id);
680 nb_ports_available--;
684 printf("Initializing port %u... done\n", (unsigned) port_id);
686 if (configure_eth_port(port_id) != 0)
687 rte_exit(EXIT_FAILURE, "Cannot initialize port %"PRIu8"\n",
691 if (!nb_ports_available) {
692 rte_exit(EXIT_FAILURE,
693 "All available ports are disabled. Please set portmask.\n");
696 /* Create rings for inter core communication */
697 rx_to_workers = rte_ring_create("rx_to_workers", RING_SIZE, rte_socket_id(),
699 if (rx_to_workers == NULL)
700 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
702 workers_to_tx = rte_ring_create("workers_to_tx", RING_SIZE, rte_socket_id(),
704 if (workers_to_tx == NULL)
705 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
707 if (!disable_reorder) {
708 send_args.buffer = rte_reorder_create("PKT_RO", rte_socket_id(),
709 REORDER_BUFFER_SIZE);
710 if (send_args.buffer == NULL)
711 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
714 last_lcore_id = get_last_lcore_id();
715 master_lcore_id = rte_get_master_lcore();
717 worker_args.ring_in = rx_to_workers;
718 worker_args.ring_out = workers_to_tx;
720 /* Start worker_thread() on all the available slave cores but the last 1 */
721 for (lcore_id = 0; lcore_id <= get_previous_lcore_id(last_lcore_id); lcore_id++)
722 if (rte_lcore_is_enabled(lcore_id) && lcore_id != master_lcore_id)
723 rte_eal_remote_launch(worker_thread, (void *)&worker_args,
726 if (disable_reorder) {
727 /* Start tx_thread() on the last slave core */
728 rte_eal_remote_launch((lcore_function_t *)tx_thread, workers_to_tx,
731 send_args.ring_in = workers_to_tx;
732 /* Start send_thread() on the last slave core */
733 rte_eal_remote_launch((lcore_function_t *)send_thread,
734 (void *)&send_args, last_lcore_id);
737 /* Start rx_thread() on the master core */
738 rx_thread(rx_to_workers);
740 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
741 if (rte_eal_wait_lcore(lcore_id) < 0)