#include <stdlib.h>
#include <signal.h>
#include <getopt.h>
#include <stdbool.h>
#define RX_DESC_PER_QUEUE 1024
#define TX_DESC_PER_QUEUE 1024
#define MAX_PKTS_BURST 32
#define REORDER_BUFFER_SIZE 8192
#define MBUF_PER_POOL 65535
#define MBUF_POOL_CACHE_SIZE 250
#define RING_SIZE 16384
#define RTE_LOGTYPE_REORDERAPP RTE_LOGTYPE_USER1
enum {
#define OPT_DISABLE_REORDER "disable-reorder"
OPT_DISABLE_REORDER_NUM = 256,
#define OPT_INSIGHT_WORKER "insight-worker"
OPT_INSIGHT_WORKER_NUM,
};
unsigned int portmask;
unsigned int disable_reorder;
unsigned int insight_worker;
volatile uint8_t quit_signal;
struct worker_thread_args {
};
struct send_thread_args {
struct rte_reorder_buffer *buffer;
};
volatile struct app_stats {
alignas(RTE_CACHE_LINE_SIZE) struct {
uint64_t rx_pkts;
uint64_t enqueue_pkts;
uint64_t enqueue_failed_pkts;
} rx;
alignas(RTE_CACHE_LINE_SIZE) struct {
uint64_t dequeue_pkts;
uint64_t enqueue_pkts;
uint64_t enqueue_failed_pkts;
} wkr;
alignas(RTE_CACHE_LINE_SIZE) struct {
uint64_t dequeue_pkts;
uint64_t early_pkts_txtd_woro;
uint64_t early_pkts_tx_failed_woro;
uint64_t ro_tx_pkts;
uint64_t ro_tx_failed_pkts;
} tx;
} app_stats;
uint64_t deq_pkts;
uint64_t enq_pkts;
uint64_t enq_failed_pkts;
};
static struct wkr_stats_per wkr_stats[RTE_MAX_LCORE] = { {0} };
static unsigned int
get_last_lcore_id(void)
{
int i;
for (i = RTE_MAX_LCORE - 1; i >= 0; i--)
return i;
return 0;
}
static unsigned int
get_previous_lcore_id(unsigned int id)
{
int i;
for (i = id - 1; i >= 0; i--)
return i;
return id;
}
static inline void
pktmbuf_free_bulk(
struct rte_mbuf *mbuf_table[],
unsigned n)
{
unsigned int i;
for (i = 0; i < n; i++)
}
static void
print_usage(const char *prgname)
{
printf("%s [EAL options] -- -p PORTMASK\n"
" -p PORTMASK: hexadecimal bitmask of ports to configure\n",
prgname);
}
static int
parse_portmask(const char *portmask)
{
unsigned long pm;
char *end = NULL;
pm = strtoul(portmask, &end, 16);
if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
return 0;
return pm;
}
static int
parse_args(int argc, char **argv)
{
int opt;
int option_index;
char **argvopt;
char *prgname = argv[0];
static struct option lgopts[] = {
{OPT_DISABLE_REORDER, 0, NULL, OPT_DISABLE_REORDER_NUM},
{OPT_INSIGHT_WORKER, 0, NULL, OPT_INSIGHT_WORKER_NUM },
{NULL, 0, 0, 0 }
};
argvopt = argv;
while ((opt = getopt_long(argc, argvopt, "p:",
lgopts, &option_index)) != EOF) {
switch (opt) {
case 'p':
portmask = parse_portmask(optarg);
if (portmask == 0) {
printf("invalid portmask\n");
print_usage(prgname);
return -1;
}
break;
case OPT_DISABLE_REORDER_NUM:
printf("reorder disabled\n");
disable_reorder = 1;
break;
case OPT_INSIGHT_WORKER_NUM:
printf("print all worker statistics\n");
insight_worker = 1;
break;
default:
print_usage(prgname);
return -1;
}
}
if (optind <= 1) {
print_usage(prgname);
return -1;
}
argv[optind-1] = prgname;
optind = 1;
return 0;
}
static void
flush_tx_error_callback(
struct rte_mbuf **unsent, uint16_t count,
app_stats.tx.ro_tx_failed_pkts += count;
RTE_LOG_DP(DEBUG, REORDERAPP,
"%s:Packet loss with tx_burst\n", __func__);
pktmbuf_free_bulk(unsent, count);
}
static inline int
uint16_t port_id;
if ((portmask & (1 << port_id)) == 0)
continue;
}
return 0;
}
static inline int
{
uint16_t port_id;
int ret;
if ((portmask & (1 << port_id)) == 0)
continue;
if (tx_buffer[port_id] == NULL)
rte_exit(EXIT_FAILURE,
"Cannot allocate buffer for tx on port %u\n",
port_id);
flush_tx_error_callback, NULL);
if (ret < 0)
"Cannot set error callback for tx buffer on port %u\n",
port_id);
}
return 0;
}
static inline int
configure_eth_port(uint16_t port_id)
{
struct rte_ether_addr addr;
const uint16_t rxRings = 1, txRings = 1;
int ret;
uint16_t q;
uint16_t nb_rxd = RX_DESC_PER_QUEUE;
uint16_t nb_txd = TX_DESC_PER_QUEUE;
return -1;
if (ret != 0) {
printf("Error during getting device (port %u) info: %s\n",
port_id, strerror(-ret));
return ret;
}
if (ret != 0)
return ret;
if (ret != 0)
return ret;
for (q = 0; q < rxRings; q++) {
mbuf_pool);
if (ret < 0)
return ret;
}
txconf = dev_info.default_txconf;
for (q = 0; q < txRings; q++) {
if (ret < 0)
return ret;
}
if (ret < 0)
return ret;
if (ret != 0) {
printf("Failed to get MAC address (port %u): %s\n",
return ret;
}
printf("Port %u MAC: %02"PRIx8" %02"PRIx8" %02"PRIx8
" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
if (ret != 0)
return ret;
return 0;
}
static void
print_stats(void)
{
uint16_t i;
unsigned int lcore_id, last_lcore_id, main_lcore_id, end_w_lcore_id;
last_lcore_id = get_last_lcore_id();
end_w_lcore_id = get_previous_lcore_id(last_lcore_id);
printf("\nRX thread stats:\n");
printf(" - Pkts rxd: %"PRIu64"\n",
app_stats.rx.rx_pkts);
printf(" - Pkts enqd to workers ring: %"PRIu64"\n",
app_stats.rx.enqueue_pkts);
for (lcore_id = 0; lcore_id <= end_w_lcore_id; lcore_id++) {
if (insight_worker
&& lcore_id != main_lcore_id) {
printf("\nWorker thread stats on core [%u]:\n",
lcore_id);
printf(" - Pkts deqd from workers ring: %"PRIu64"\n",
wkr_stats[lcore_id].deq_pkts);
printf(" - Pkts enqd to tx ring: %"PRIu64"\n",
wkr_stats[lcore_id].enq_pkts);
printf(" - Pkts enq to tx failed: %"PRIu64"\n",
wkr_stats[lcore_id].enq_failed_pkts);
}
app_stats.wkr.dequeue_pkts += wkr_stats[lcore_id].deq_pkts;
app_stats.wkr.enqueue_pkts += wkr_stats[lcore_id].enq_pkts;
app_stats.wkr.enqueue_failed_pkts +=
wkr_stats[lcore_id].enq_failed_pkts;
}
printf("\nWorker thread stats:\n");
printf(" - Pkts deqd from workers ring: %"PRIu64"\n",
app_stats.wkr.dequeue_pkts);
printf(" - Pkts enqd to tx ring: %"PRIu64"\n",
app_stats.wkr.enqueue_pkts);
printf(" - Pkts enq to tx failed: %"PRIu64"\n",
app_stats.wkr.enqueue_failed_pkts);
printf("\nTX stats:\n");
printf(" - Pkts deqd from tx ring: %"PRIu64"\n",
app_stats.tx.dequeue_pkts);
printf(" - Ro Pkts transmitted: %"PRIu64"\n",
app_stats.tx.ro_tx_pkts);
printf(" - Ro Pkts tx failed: %"PRIu64"\n",
app_stats.tx.ro_tx_failed_pkts);
printf(" - Pkts transmitted w/o reorder: %"PRIu64"\n",
app_stats.tx.early_pkts_txtd_woro);
printf(" - Pkts tx failed w/o reorder: %"PRIu64"\n",
app_stats.tx.early_pkts_tx_failed_woro);
printf("\nPort %u stats:\n", i);
printf(" - Pkts in: %"PRIu64"\n", eth_stats.ipackets);
printf(" - Pkts out: %"PRIu64"\n", eth_stats.opackets);
printf(" - In Errs: %"PRIu64"\n", eth_stats.ierrors);
printf(" - Out Errs: %"PRIu64"\n", eth_stats.oerrors);
printf(" - Mbuf Errs: %"PRIu64"\n", eth_stats.rx_nombuf);
}
}
static void
int_handler(int sig_num)
{
printf("Exiting on signal %d\n", sig_num);
quit_signal = 1;
}
rx_thread(
struct rte_ring *ring_out,
bool disable_reorder_flag)
{
uint32_t seqn = 0;
uint16_t i, ret = 0;
uint16_t nb_rx_pkts;
uint16_t port_id;
RTE_LOG(INFO, REORDERAPP,
"%s() started on lcore %u\n", __func__,
while (!quit_signal) {
if ((portmask & (1 << port_id)) != 0) {
pkts, MAX_PKTS_BURST);
if (nb_rx_pkts == 0) {
"%s():Received zero packets\n", __func__);
continue;
}
app_stats.rx.rx_pkts += nb_rx_pkts;
if (!disable_reorder_flag) {
for (i = 0; i < nb_rx_pkts;)
}
(void *)pkts, nb_rx_pkts, NULL);
app_stats.rx.enqueue_pkts += ret;
app_stats.rx.enqueue_failed_pkts +=
(nb_rx_pkts-ret);
pktmbuf_free_bulk(&pkts[ret], nb_rx_pkts - ret);
}
}
}
}
return 0;
}
rx_thread_reorder(
struct rte_ring *ring_out)
{
return rx_thread(ring_out, false);
}
rx_thread_reorder_disabled(
struct rte_ring *ring_out)
{
return rx_thread(ring_out, true);
}
static int
worker_thread(void *args_ptr)
{
uint16_t i, ret = 0;
uint16_t burst_size = 0;
struct worker_thread_args *args;
struct rte_mbuf *burst_buffer[MAX_PKTS_BURST] = { NULL };
const unsigned xor_val = (nb_ports > 1);
args = (struct worker_thread_args *) args_ptr;
ring_in = args->ring_in;
ring_out = args->ring_out;
RTE_LOG(INFO, REORDERAPP,
"%s() started on lcore %u\n", __func__,
core_id);
while (!quit_signal) {
(void *)burst_buffer, MAX_PKTS_BURST, NULL);
continue;
wkr_stats[core_id].deq_pkts += burst_size;
for (i = 0; i < burst_size;)
burst_buffer[i++]->port ^= xor_val;
burst_size, NULL);
wkr_stats[core_id].enq_pkts += ret;
wkr_stats[core_id].enq_failed_pkts += burst_size - ret;
pktmbuf_free_bulk(&burst_buffer[ret], burst_size - ret);
}
}
return 0;
}
static int
send_thread(struct send_thread_args *args)
{
int ret;
unsigned int i, dret;
uint16_t nb_dq_mbufs;
uint8_t outp;
unsigned sent;
struct rte_mbuf *rombufs[MAX_PKTS_BURST] = {NULL};
configure_tx_buffers(tx_buffer);
while (!quit_signal) {
(void *)mbufs, MAX_PKTS_BURST, NULL);
continue;
app_stats.tx.dequeue_pkts += nb_dq_mbufs;
for (i = 0; i < nb_dq_mbufs; i++) {
"%s():Cannot reorder early packet "
"direct enqueuing to TX\n", __func__);
if ((portmask & (1 << outp)) == 0) {
continue;
}
app_stats.tx.early_pkts_tx_failed_woro++;
} else
app_stats.tx.early_pkts_txtd_woro++;
}
else if (ret == -1 &&
rte_errno == ENOSPC) {
}
}
for (i = 0; i < dret; i++) {
uint8_t outp1;
outp1 = rombufs[i]->
port;
if ((portmask & (1 << outp1)) == 0) {
continue;
}
outbuf = tx_buffer[outp1];
if (sent)
app_stats.tx.ro_tx_pkts += sent;
}
}
free_tx_buffers(tx_buffer);
return 0;
}
static int
{
uint32_t i, dqnum;
uint8_t outp;
unsigned sent;
RTE_LOG(INFO, REORDERAPP,
"%s() started on lcore %u\n", __func__,
configure_tx_buffers(tx_buffer);
while (!quit_signal) {
(void *)mbufs, MAX_PKTS_BURST, NULL);
continue;
app_stats.tx.dequeue_pkts += dqnum;
for (i = 0; i < dqnum; i++) {
if ((portmask & (1 << outp)) == 0) {
continue;
}
outbuf = tx_buffer[outp];
if (sent)
app_stats.tx.ro_tx_pkts += sent;
}
}
return 0;
}
int
main(int argc, char **argv)
{
int ret;
unsigned nb_ports;
unsigned int lcore_id, last_lcore_id, main_lcore_id;
uint16_t port_id;
uint16_t nb_ports_available;
struct worker_thread_args worker_args = {NULL, NULL};
struct send_thread_args send_args = {NULL, NULL};
signal(SIGINT, int_handler);
if (ret < 0)
rte_exit(EXIT_FAILURE,
"Invalid EAL arguments\n");
argc -= ret;
argv += ret;
ret = parse_args(argc, argv);
if (ret < 0)
rte_exit(EXIT_FAILURE,
"Invalid packet_ordering arguments\n");
rte_exit(EXIT_FAILURE,
"Error, This application needs at "
"least 3 logical cores to run:\n"
"1 lcore for packet RX\n"
"1 lcore for packet TX\n"
"and at least 1 lcore for worker threads\n");
if (nb_ports == 0)
rte_exit(EXIT_FAILURE,
"Error: no ethernet ports detected\n");
if (nb_ports != 1 && (nb_ports & 1))
rte_exit(EXIT_FAILURE,
"Error: number of ports must be even, except "
"when using a single port\n");
MBUF_POOL_CACHE_SIZE, 0, RTE_MBUF_DEFAULT_BUF_SIZE,
if (mbuf_pool == NULL)
nb_ports_available = nb_ports;
if ((portmask & (1 << port_id)) == 0) {
printf("\nSkipping disabled port %d\n", port_id);
nb_ports_available--;
continue;
}
printf("Initializing port %u... done\n", port_id);
if (configure_eth_port(port_id) != 0)
rte_exit(EXIT_FAILURE,
"Cannot initialize port %"PRIu8
"\n",
port_id);
}
if (!nb_ports_available) {
"All available ports are disabled. Please set portmask.\n");
}
if (rx_to_workers == NULL)
if (workers_to_tx == NULL)
if (!disable_reorder) {
REORDER_BUFFER_SIZE);
if (send_args.buffer == NULL)
}
last_lcore_id = get_last_lcore_id();
worker_args.ring_in = rx_to_workers;
worker_args.ring_out = workers_to_tx;
for (lcore_id = 0; lcore_id <= get_previous_lcore_id(last_lcore_id); lcore_id++)
lcore_id);
if (disable_reorder) {
last_lcore_id);
} else {
send_args.ring_in = workers_to_tx;
(void *)&send_args, last_lcore_id);
}
if (disable_reorder)
rx_thread_reorder_disabled(rx_to_workers);
else
rx_thread_reorder(rx_to_workers);
return -1;
}
print_stats();
return 0;
}
__rte_noreturn void rte_exit(int exit_code, const char *format,...) __rte_format_printf(2
#define __rte_cache_aligned
#define __rte_always_inline
int rte_eal_init(int argc, char **argv)
int rte_eal_cleanup(void)
const char * rte_strerror(int errnum)
static __rte_always_inline uint16_t rte_eth_tx_buffer(uint16_t port_id, uint16_t queue_id, struct rte_eth_dev_tx_buffer *buffer, struct rte_mbuf *tx_pkt)
int rte_eth_dev_configure(uint16_t port_id, uint16_t nb_rx_queue, uint16_t nb_tx_queue, const struct rte_eth_conf *eth_conf)
int rte_eth_dev_is_valid_port(uint16_t port_id)
int rte_eth_rx_queue_setup(uint16_t port_id, uint16_t rx_queue_id, uint16_t nb_rx_desc, unsigned int socket_id, const struct rte_eth_rxconf *rx_conf, struct rte_mempool *mb_pool)
static uint16_t rte_eth_rx_burst(uint16_t port_id, uint16_t queue_id, struct rte_mbuf **rx_pkts, const uint16_t nb_pkts)
#define RTE_ETH_TX_OFFLOAD_MBUF_FAST_FREE
int rte_eth_dev_info_get(uint16_t port_id, struct rte_eth_dev_info *dev_info) __rte_warn_unused_result
int rte_eth_promiscuous_enable(uint16_t port_id)
int rte_eth_tx_queue_setup(uint16_t port_id, uint16_t tx_queue_id, uint16_t nb_tx_desc, unsigned int socket_id, const struct rte_eth_txconf *tx_conf)
static uint16_t rte_eth_tx_burst(uint16_t port_id, uint16_t queue_id, struct rte_mbuf **tx_pkts, uint16_t nb_pkts)
int rte_eth_macaddr_get(uint16_t port_id, struct rte_ether_addr *mac_addr)
uint16_t rte_eth_dev_count_avail(void)
int rte_eth_tx_buffer_set_err_callback(struct rte_eth_dev_tx_buffer *buffer, buffer_tx_error_fn callback, void *userdata)
int rte_eth_tx_buffer_init(struct rte_eth_dev_tx_buffer *buffer, uint16_t size)
int rte_eth_dev_socket_id(uint16_t port_id)
int rte_eth_dev_adjust_nb_rx_tx_desc(uint16_t port_id, uint16_t *nb_rx_desc, uint16_t *nb_tx_desc)
#define RTE_ETH_FOREACH_DEV(p)
int rte_eth_stats_get(uint16_t port_id, struct rte_eth_stats *stats)
int rte_eth_dev_start(uint16_t port_id)
#define RTE_ETH_TX_BUFFER_SIZE(sz)
#define RTE_ETHER_ADDR_BYTES(mac_addrs)
int() lcore_function_t(void *)
int rte_eal_remote_launch(lcore_function_t *f, void *arg, unsigned worker_id)
int rte_eal_wait_lcore(unsigned worker_id)
unsigned int rte_lcore_count(void)
int rte_lcore_is_enabled(unsigned int lcore_id)
unsigned int rte_get_main_lcore(void)
unsigned int rte_socket_id(void)
#define RTE_LCORE_FOREACH_WORKER(i)
static unsigned rte_lcore_id(void)
#define RTE_LOG(l, t,...)
#define RTE_LOG_DP(l, t,...)
void * rte_zmalloc_socket(const char *type, size_t size, unsigned align, int socket) __rte_alloc_size(2) __rte_alloc_align(3) __rte_malloc __rte_dealloc_free
static void rte_pktmbuf_free(struct rte_mbuf *m)
struct rte_mempool * rte_pktmbuf_pool_create(const char *name, unsigned n, unsigned cache_size, uint16_t priv_size, uint16_t data_room_size, int socket_id)
int rte_reorder_insert(struct rte_reorder_buffer *b, struct rte_mbuf *mbuf)
unsigned int rte_reorder_drain(struct rte_reorder_buffer *b, struct rte_mbuf **mbufs, unsigned max_mbufs)
static __rte_experimental rte_reorder_seqn_t * rte_reorder_seqn(struct rte_mbuf *mbuf)
struct rte_reorder_buffer * rte_reorder_create(const char *name, unsigned socket_id, unsigned int size)
static __rte_always_inline unsigned int rte_ring_enqueue_burst(struct rte_ring *r, void *const *obj_table, unsigned int n, unsigned int *free_space)
struct rte_ring * rte_ring_create(const char *name, unsigned int count, int socket_id, unsigned int flags)
static __rte_always_inline unsigned int rte_ring_dequeue_burst(struct rte_ring *r, void **obj_table, unsigned int n, unsigned int *available)
struct rte_eth_txmode txmode