4 * Copyright(c) 2010-2014 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>
43 #include <rte_mempool.h>
45 #include <rte_reorder.h>
47 #define RX_DESC_PER_QUEUE 128
48 #define TX_DESC_PER_QUEUE 512
50 #define MAX_PKTS_BURST 32
51 #define REORDER_BUFFER_SIZE 8192
52 #define MBUF_PER_POOL 65535
53 #define MBUF_POOL_CACHE_SIZE 250
55 #define RING_SIZE 16384
57 /* uncommnet below line to enable debug logs */
61 #define LOG_LEVEL RTE_LOG_DEBUG
62 #define LOG_DEBUG(log_type, fmt, args...) RTE_LOG(DEBUG, log_type, fmt, ##args)
64 #define LOG_LEVEL RTE_LOG_INFO
65 #define LOG_DEBUG(log_type, fmt, args...) do {} while (0)
68 /* Macros for printing using RTE_LOG */
69 #define RTE_LOGTYPE_REORDERAPP RTE_LOGTYPE_USER1
71 unsigned int portmask;
72 unsigned int disable_reorder;
73 volatile uint8_t quit_signal;
75 static struct rte_mempool *mbuf_pool;
77 static struct rte_eth_conf port_conf_default;
79 struct worker_thread_args {
80 struct rte_ring *ring_in;
81 struct rte_ring *ring_out;
84 struct send_thread_args {
85 struct rte_ring *ring_in;
86 struct rte_reorder_buffer *buffer;
89 struct output_buffer {
91 struct rte_mbuf *mbufs[MAX_PKTS_BURST];
94 volatile struct app_stats {
97 uint64_t enqueue_pkts;
98 uint64_t enqueue_failed_pkts;
99 } rx __rte_cache_aligned;
102 uint64_t dequeue_pkts;
103 uint64_t enqueue_pkts;
104 uint64_t enqueue_failed_pkts;
105 } wkr __rte_cache_aligned;
108 uint64_t dequeue_pkts;
109 /* Too early pkts transmitted directly w/o reordering */
110 uint64_t early_pkts_txtd_woro;
111 /* Too early pkts failed from direct transmit */
112 uint64_t early_pkts_tx_failed_woro;
114 uint64_t ro_tx_failed_pkts;
115 } tx __rte_cache_aligned;
119 * Get the last enabled lcore ID
122 * The last enabled lcore ID.
125 get_last_lcore_id(void)
129 for (i = RTE_MAX_LCORE - 1; i >= 0; i--)
130 if (rte_lcore_is_enabled(i))
136 * Get the previous enabled lcore ID
138 * The current lcore ID
140 * The previous enabled lcore ID or the current lcore
141 * ID if it is the first available core.
144 get_previous_lcore_id(unsigned int id)
148 for (i = id - 1; i >= 0; i--)
149 if (rte_lcore_is_enabled(i))
155 pktmbuf_free_bulk(struct rte_mbuf *mbuf_table[], unsigned n)
159 for (i = 0; i < n; i++)
160 rte_pktmbuf_free(mbuf_table[i]);
165 print_usage(const char *prgname)
167 printf("%s [EAL options] -- -p PORTMASK\n"
168 " -p PORTMASK: hexadecimal bitmask of ports to configure\n",
173 parse_portmask(const char *portmask)
178 /* parse hexadecimal string */
179 pm = strtoul(portmask, &end, 16);
180 if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
189 /* Parse the argument given in the command line of the application */
191 parse_args(int argc, char **argv)
196 char *prgname = argv[0];
197 static struct option lgopts[] = {
198 {"disable-reorder", 0, 0, 0},
204 while ((opt = getopt_long(argc, argvopt, "p:",
205 lgopts, &option_index)) != EOF) {
209 portmask = parse_portmask(optarg);
211 printf("invalid portmask\n");
212 print_usage(prgname);
218 if (!strcmp(lgopts[option_index].name, "disable-reorder")) {
219 printf("reorder disabled\n");
224 print_usage(prgname);
229 print_usage(prgname);
233 argv[optind-1] = prgname;
234 optind = 0; /* reset getopt lib */
239 configure_eth_port(uint8_t port_id)
241 struct ether_addr addr;
242 const uint16_t rxRings = 1, txRings = 1;
243 const uint8_t nb_ports = rte_eth_dev_count();
247 if (port_id > nb_ports)
250 ret = rte_eth_dev_configure(port_id, rxRings, txRings, &port_conf_default);
254 for (q = 0; q < rxRings; q++) {
255 ret = rte_eth_rx_queue_setup(port_id, q, RX_DESC_PER_QUEUE,
256 rte_eth_dev_socket_id(port_id), NULL,
262 for (q = 0; q < txRings; q++) {
263 ret = rte_eth_tx_queue_setup(port_id, q, TX_DESC_PER_QUEUE,
264 rte_eth_dev_socket_id(port_id), NULL);
269 ret = rte_eth_dev_start(port_id);
273 rte_eth_macaddr_get(port_id, &addr);
274 printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
275 " %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
277 addr.addr_bytes[0], addr.addr_bytes[1],
278 addr.addr_bytes[2], addr.addr_bytes[3],
279 addr.addr_bytes[4], addr.addr_bytes[5]);
281 rte_eth_promiscuous_enable(port_id);
289 const uint8_t nb_ports = rte_eth_dev_count();
291 struct rte_eth_stats eth_stats;
293 printf("\nRX thread stats:\n");
294 printf(" - Pkts rxd: %"PRIu64"\n",
295 app_stats.rx.rx_pkts);
296 printf(" - Pkts enqd to workers ring: %"PRIu64"\n",
297 app_stats.rx.enqueue_pkts);
299 printf("\nWorker thread stats:\n");
300 printf(" - Pkts deqd from workers ring: %"PRIu64"\n",
301 app_stats.wkr.dequeue_pkts);
302 printf(" - Pkts enqd to tx ring: %"PRIu64"\n",
303 app_stats.wkr.enqueue_pkts);
304 printf(" - Pkts enq to tx failed: %"PRIu64"\n",
305 app_stats.wkr.enqueue_failed_pkts);
307 printf("\nTX stats:\n");
308 printf(" - Pkts deqd from tx ring: %"PRIu64"\n",
309 app_stats.tx.dequeue_pkts);
310 printf(" - Ro Pkts transmitted: %"PRIu64"\n",
311 app_stats.tx.ro_tx_pkts);
312 printf(" - Ro Pkts tx failed: %"PRIu64"\n",
313 app_stats.tx.ro_tx_failed_pkts);
314 printf(" - Pkts transmitted w/o reorder: %"PRIu64"\n",
315 app_stats.tx.early_pkts_txtd_woro);
316 printf(" - Pkts tx failed w/o reorder: %"PRIu64"\n",
317 app_stats.tx.early_pkts_tx_failed_woro);
319 for (i = 0; i < nb_ports; i++) {
320 rte_eth_stats_get(i, ð_stats);
321 printf("\nPort %u stats:\n", i);
322 printf(" - Pkts in: %"PRIu64"\n", eth_stats.ipackets);
323 printf(" - Pkts out: %"PRIu64"\n", eth_stats.opackets);
324 printf(" - In Errs: %"PRIu64"\n", eth_stats.ierrors);
325 printf(" - Out Errs: %"PRIu64"\n", eth_stats.oerrors);
326 printf(" - Mbuf Errs: %"PRIu64"\n", eth_stats.rx_nombuf);
331 int_handler(int sig_num)
333 printf("Exiting on signal %d\n", sig_num);
338 * This thread receives mbufs from the port and affects them an internal
339 * sequence number to keep track of their order of arrival through an
341 * The mbufs are then passed to the worker threads via the rx_to_workers
345 rx_thread(struct rte_ring *ring_out)
347 const uint8_t nb_ports = rte_eth_dev_count();
352 struct rte_mbuf *pkts[MAX_PKTS_BURST];
354 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
357 while (!quit_signal) {
359 for (port_id = 0; port_id < nb_ports; port_id++) {
360 if ((portmask & (1 << port_id)) != 0) {
362 /* receive packets */
363 nb_rx_pkts = rte_eth_rx_burst(port_id, 0,
364 pkts, MAX_PKTS_BURST);
365 if (nb_rx_pkts == 0) {
366 LOG_DEBUG(REORDERAPP,
367 "%s():Received zero packets\n", __func__);
370 app_stats.rx.rx_pkts += nb_rx_pkts;
372 /* mark sequence number */
373 for (i = 0; i < nb_rx_pkts; )
374 pkts[i++]->seqn = seqn++;
376 /* enqueue to rx_to_workers ring */
377 ret = rte_ring_enqueue_burst(ring_out, (void *) pkts,
379 app_stats.rx.enqueue_pkts += ret;
380 if (unlikely(ret < nb_rx_pkts)) {
381 app_stats.rx.enqueue_failed_pkts +=
383 pktmbuf_free_bulk(&pkts[ret], nb_rx_pkts - ret);
392 * This thread takes bursts of packets from the rx_to_workers ring and
393 * Changes the input port value to output port value. And feds it to
397 worker_thread(void *args_ptr)
399 const uint8_t nb_ports = rte_eth_dev_count();
401 uint16_t burst_size = 0;
402 struct worker_thread_args *args;
403 struct rte_mbuf *burst_buffer[MAX_PKTS_BURST] = { NULL };
404 struct rte_ring *ring_in, *ring_out;
405 const unsigned xor_val = (nb_ports > 1);
407 args = (struct worker_thread_args *) args_ptr;
408 ring_in = args->ring_in;
409 ring_out = args->ring_out;
411 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
414 while (!quit_signal) {
416 /* dequeue the mbufs from rx_to_workers ring */
417 burst_size = rte_ring_dequeue_burst(ring_in,
418 (void *)burst_buffer, MAX_PKTS_BURST);
419 if (unlikely(burst_size == 0))
422 __sync_fetch_and_add(&app_stats.wkr.dequeue_pkts, burst_size);
424 /* just do some operation on mbuf */
425 for (i = 0; i < burst_size;)
426 burst_buffer[i++]->port ^= xor_val;
428 /* enqueue the modified mbufs to workers_to_tx ring */
429 ret = rte_ring_enqueue_burst(ring_out, (void *)burst_buffer, burst_size);
430 __sync_fetch_and_add(&app_stats.wkr.enqueue_pkts, ret);
431 if (unlikely(ret < burst_size)) {
432 /* Return the mbufs to their respective pool, dropping packets */
433 __sync_fetch_and_add(&app_stats.wkr.enqueue_failed_pkts,
434 (int)burst_size - ret);
435 pktmbuf_free_bulk(&burst_buffer[ret], burst_size - ret);
442 flush_one_port(struct output_buffer *outbuf, uint8_t outp)
444 unsigned nb_tx = rte_eth_tx_burst(outp, 0, outbuf->mbufs,
446 app_stats.tx.ro_tx_pkts += nb_tx;
448 if (unlikely(nb_tx < outbuf->count)) {
449 /* free the mbufs which failed from transmit */
450 app_stats.tx.ro_tx_failed_pkts += (outbuf->count - nb_tx);
451 LOG_DEBUG(REORDERAPP, "%s:Packet loss with tx_burst\n", __func__);
452 pktmbuf_free_bulk(&outbuf->mbufs[nb_tx], outbuf->count - nb_tx);
458 * Dequeue mbufs from the workers_to_tx ring and reorder them before
462 send_thread(struct send_thread_args *args)
465 unsigned int i, dret;
466 uint16_t nb_dq_mbufs;
468 static struct output_buffer tx_buffers[RTE_MAX_ETHPORTS];
469 struct rte_mbuf *mbufs[MAX_PKTS_BURST];
470 struct rte_mbuf *rombufs[MAX_PKTS_BURST] = {NULL};
472 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__, rte_lcore_id());
474 while (!quit_signal) {
476 /* deque the mbufs from workers_to_tx ring */
477 nb_dq_mbufs = rte_ring_dequeue_burst(args->ring_in,
478 (void *)mbufs, MAX_PKTS_BURST);
480 if (unlikely(nb_dq_mbufs == 0))
483 app_stats.tx.dequeue_pkts += nb_dq_mbufs;
485 for (i = 0; i < nb_dq_mbufs; i++) {
486 /* send dequeued mbufs for reordering */
487 ret = rte_reorder_insert(args->buffer, mbufs[i]);
489 if (ret == -1 && rte_errno == ERANGE) {
490 /* Too early pkts should be transmitted out directly */
491 LOG_DEBUG(REORDERAPP, "%s():Cannot reorder early packet "
492 "direct enqueuing to TX\n", __func__);
493 outp = mbufs[i]->port;
494 if ((portmask & (1 << outp)) == 0) {
495 rte_pktmbuf_free(mbufs[i]);
498 if (rte_eth_tx_burst(outp, 0, (void *)mbufs[i], 1) != 1) {
499 rte_pktmbuf_free(mbufs[i]);
500 app_stats.tx.early_pkts_tx_failed_woro++;
502 app_stats.tx.early_pkts_txtd_woro++;
503 } else if (ret == -1 && rte_errno == ENOSPC) {
505 * Early pkts just outside of window should be dropped
507 rte_pktmbuf_free(mbufs[i]);
512 * drain MAX_PKTS_BURST of reordered
515 dret = rte_reorder_drain(args->buffer, rombufs, MAX_PKTS_BURST);
516 for (i = 0; i < dret; i++) {
518 struct output_buffer *outbuf;
521 outp1 = rombufs[i]->port;
522 /* skip ports that are not enabled */
523 if ((portmask & (1 << outp1)) == 0) {
524 rte_pktmbuf_free(rombufs[i]);
528 outbuf = &tx_buffers[outp1];
529 outbuf->mbufs[outbuf->count++] = rombufs[i];
530 if (outbuf->count == MAX_PKTS_BURST)
531 flush_one_port(outbuf, outp1);
538 * Dequeue mbufs from the workers_to_tx ring and transmit them
541 tx_thread(struct rte_ring *ring_in)
545 static struct output_buffer tx_buffers[RTE_MAX_ETHPORTS];
546 struct rte_mbuf *mbufs[MAX_PKTS_BURST];
547 struct output_buffer *outbuf;
549 RTE_LOG(INFO, REORDERAPP, "%s() started on lcore %u\n", __func__,
551 while (!quit_signal) {
553 /* deque the mbufs from workers_to_tx ring */
554 dqnum = rte_ring_dequeue_burst(ring_in,
555 (void *)mbufs, MAX_PKTS_BURST);
557 if (unlikely(dqnum == 0))
560 app_stats.tx.dequeue_pkts += dqnum;
562 for (i = 0; i < dqnum; i++) {
563 outp = mbufs[i]->port;
564 /* skip ports that are not enabled */
565 if ((portmask & (1 << outp)) == 0) {
566 rte_pktmbuf_free(mbufs[i]);
570 outbuf = &tx_buffers[outp];
571 outbuf->mbufs[outbuf->count++] = mbufs[i];
572 if (outbuf->count == MAX_PKTS_BURST)
573 flush_one_port(outbuf, outp);
581 main(int argc, char **argv)
585 unsigned int lcore_id, last_lcore_id, master_lcore_id;
587 uint8_t nb_ports_available;
588 struct worker_thread_args worker_args = {NULL, NULL};
589 struct send_thread_args send_args = {NULL, NULL};
590 struct rte_ring *rx_to_workers;
591 struct rte_ring *workers_to_tx;
593 /* catch ctrl-c so we can print on exit */
594 signal(SIGINT, int_handler);
597 ret = rte_eal_init(argc, argv);
604 /* Parse the application specific arguments */
605 ret = parse_args(argc, argv);
609 /* Check if we have enought cores */
610 if (rte_lcore_count() < 3)
611 rte_exit(EXIT_FAILURE, "Error, This application needs at "
612 "least 3 logical cores to run:\n"
613 "1 lcore for packet RX\n"
614 "1 lcore for packet TX\n"
615 "and at least 1 lcore for worker threads\n");
617 nb_ports = rte_eth_dev_count();
619 rte_exit(EXIT_FAILURE, "Error: no ethernet ports detected\n");
620 if (nb_ports != 1 && (nb_ports & 1))
621 rte_exit(EXIT_FAILURE, "Error: number of ports must be even, except "
622 "when using a single port\n");
624 mbuf_pool = rte_pktmbuf_pool_create("mbuf_pool", MBUF_PER_POOL,
625 MBUF_POOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
627 if (mbuf_pool == NULL)
628 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
630 nb_ports_available = nb_ports;
632 /* initialize all ports */
633 for (port_id = 0; port_id < nb_ports; port_id++) {
634 /* skip ports that are not enabled */
635 if ((portmask & (1 << port_id)) == 0) {
636 printf("\nSkipping disabled port %d\n", port_id);
637 nb_ports_available--;
641 printf("Initializing port %u... done\n", (unsigned) port_id);
643 if (configure_eth_port(port_id) != 0)
644 rte_exit(EXIT_FAILURE, "Cannot initialize port %"PRIu8"\n",
648 if (!nb_ports_available) {
649 rte_exit(EXIT_FAILURE,
650 "All available ports are disabled. Please set portmask.\n");
653 /* Create rings for inter core communication */
654 rx_to_workers = rte_ring_create("rx_to_workers", RING_SIZE, rte_socket_id(),
656 if (rx_to_workers == NULL)
657 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
659 workers_to_tx = rte_ring_create("workers_to_tx", RING_SIZE, rte_socket_id(),
661 if (workers_to_tx == NULL)
662 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
664 if (!disable_reorder) {
665 send_args.buffer = rte_reorder_create("PKT_RO", rte_socket_id(),
666 REORDER_BUFFER_SIZE);
667 if (send_args.buffer == NULL)
668 rte_exit(EXIT_FAILURE, "%s\n", rte_strerror(rte_errno));
671 last_lcore_id = get_last_lcore_id();
672 master_lcore_id = rte_get_master_lcore();
674 worker_args.ring_in = rx_to_workers;
675 worker_args.ring_out = workers_to_tx;
677 /* Start worker_thread() on all the available slave cores but the last 1 */
678 for (lcore_id = 0; lcore_id <= get_previous_lcore_id(last_lcore_id); lcore_id++)
679 if (rte_lcore_is_enabled(lcore_id) && lcore_id != master_lcore_id)
680 rte_eal_remote_launch(worker_thread, (void *)&worker_args,
683 if (disable_reorder) {
684 /* Start tx_thread() on the last slave core */
685 rte_eal_remote_launch((lcore_function_t *)tx_thread, workers_to_tx,
688 send_args.ring_in = workers_to_tx;
689 /* Start send_thread() on the last slave core */
690 rte_eal_remote_launch((lcore_function_t *)send_thread,
691 (void *)&send_args, last_lcore_id);
694 /* Start rx_thread() on the master core */
695 rx_thread(rx_to_workers);
697 RTE_LCORE_FOREACH_SLAVE(lcore_id) {
698 if (rte_eal_wait_lcore(lcore_id) < 0)