#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <signal.h>
#include <sched.h>
#include <stdbool.h>
#define MAX_NUM_STAGES 8
#define BATCH_SIZE 16
#define MAX_NUM_CORE 64
struct prod_data {
uint8_t dev_id;
uint8_t port_id;
int32_t qid;
unsigned int num_nic_ports;
struct cons_data {
uint8_t dev_id;
uint8_t port_id;
static struct prod_data prod_data;
static struct cons_data cons_data;
struct worker_data {
uint8_t dev_id;
uint8_t port_id;
struct fastpath_data {
volatile int done;
uint32_t rx_lock;
uint32_t tx_lock;
uint32_t sched_lock;
uint32_t evdev_service_id;
bool rx_single;
bool tx_single;
bool sched_single;
unsigned int rx_core[MAX_NUM_CORE];
unsigned int tx_core[MAX_NUM_CORE];
unsigned int sched_core[MAX_NUM_CORE];
unsigned int worker_core[MAX_NUM_CORE];
};
static struct fastpath_data *fdata;
struct config_data {
unsigned int active_cores;
unsigned int num_workers;
int64_t num_packets;
unsigned int num_fids;
int queue_type;
int worker_cycles;
int enable_queue_priorities;
int quiet;
int dump_dev;
int dump_dev_signal;
unsigned int num_stages;
unsigned int worker_cq_depth;
int16_t next_qid[MAX_NUM_STAGES+2];
int16_t qid[MAX_NUM_STAGES];
};
static struct config_data cdata = {
.num_packets = (1L << 25),
.num_fids = 512,
.next_qid = {-1},
.qid = {-1},
.num_stages = 1,
.worker_cq_depth = 16
};
static bool
core_in_use(unsigned int lcore_id) {
return (fdata->rx_core[lcore_id] || fdata->sched_core[lcore_id] ||
fdata->tx_core[lcore_id] || fdata->worker_core[lcore_id]);
}
static void
eth_tx_buffer_retry(
struct rte_mbuf **pkts, uint16_t unsent,
void *userdata)
{
int port_id = (uintptr_t) userdata;
unsigned int _sent = 0;
do {
unsent - _sent);
} while (_sent != unsent);
}
static int
consumer(void)
{
static uint64_t received;
static uint64_t last_pkts;
static uint64_t last_time;
static uint64_t start_time;
unsigned int i, j;
uint8_t dev_id = cons_data.dev_id;
uint8_t port_id = cons_data.port_id;
if (n == 0) {
return 0;
}
if (start_time == 0)
received += n;
for (i = 0; i < n; i++) {
uint8_t outport = packets[i].mbuf->port;
packets[i].mbuf);
}
if (!cdata.quiet && received >= last_pkts + (1<<22)) {
const uint64_t total_ms = (now - start_time) / freq_khz;
const uint64_t delta_ms = (now - last_time) / freq_khz;
uint64_t delta_pkts = received - last_pkts;
printf("# consumer RX=%"PRIu64", time %"PRIu64 "ms, "
"avg %.3f mpps [current %.3f mpps]\n",
received,
total_ms,
received / (total_ms * 1000.0),
delta_pkts / (delta_ms * 1000.0));
last_pkts = received;
last_time = now;
}
cdata.num_packets -= n;
if (cdata.num_packets <= 0)
fdata->done = 1;
return 0;
}
static int
producer(void)
{
static uint8_t eth_port;
uint32_t i, num_ports = prod_data.num_nic_ports;
int32_t qid = prod_data.qid;
uint8_t dev_id = prod_data.dev_id;
uint8_t port_id = prod_data.port_id;
uint32_t prio_idx = 0;
if (++eth_port == num_ports)
eth_port = 0;
if (nb_rx == 0) {
return 0;
}
for (i = 0; i < nb_rx; i++) {
ev[i].flow_id = mbufs[i]->
hash.rss;
ev[i].sched_type = cdata.queue_type;
ev[i].queue_id = qid;
ev[i].sub_event_type = 0;
ev[i].mbuf = mbufs[i];
}
if (nb_tx != nb_rx) {
for (i = nb_tx; i < nb_rx; i++)
}
return 0;
}
static inline void
schedule_devices(unsigned int lcore_id)
{
if (fdata->rx_core[lcore_id] && (fdata->rx_single ||
producer();
}
if (fdata->sched_core[lcore_id] && (fdata->sched_single ||
if (cdata.dump_dev_signal) {
cdata.dump_dev_signal = 0;
}
}
if (fdata->tx_core[lcore_id] && (fdata->tx_single ||
consumer();
}
}
static inline void
{
volatile uint64_t start_tsc = rte_rdtsc();
while (rte_rdtsc() < start_tsc + cdata.worker_cycles)
}
static int
worker(void *arg)
{
struct worker_data *data = (struct worker_data *)arg;
uint8_t dev_id = data->dev_id;
uint8_t port_id = data->port_id;
size_t sent = 0, received = 0;
while (!fdata->done) {
uint16_t i;
schedule_devices(lcore_id);
if (!fdata->worker_core[lcore_id]) {
continue;
}
if (nb_rx == 0) {
continue;
}
received += nb_rx;
for (i = 0; i < nb_rx; i++) {
if (events[i].queue_id == cdata.qid[0])
events[i].flow_id = events[i].mbuf->hash.rss
% cdata.num_fids;
events[i].queue_id = cdata.next_qid[events[i].queue_id];
events[i].sched_type = cdata.queue_type;
work(events[i].mbuf);
}
events, nb_rx);
while (nb_tx < nb_rx && !fdata->done)
events + nb_tx,
nb_rx - nb_tx);
sent += nb_tx;
}
if (!cdata.quiet)
printf(" worker %u thread done. RX=%zu TX=%zu\n",
return 0;
}
static int xdigit2val(unsigned char c)
{
int val;
if (isdigit(c))
val = c - '0';
else if (isupper(c))
val = c - 'A' + 10;
else
val = c - 'a' + 10;
return val;
}
static uint64_t
parse_coremask(const char *coremask)
{
int i, j, idx = 0;
unsigned int count = 0;
char c;
int val;
uint64_t mask = 0;
const int32_t BITS_HEX = 4;
if (coremask == NULL)
return -1;
while (isblank(*coremask))
coremask++;
if (coremask[0] == '0' && ((coremask[1] == 'x')
|| (coremask[1] == 'X')))
coremask += 2;
i = strlen(coremask);
while ((i > 0) && isblank(coremask[i - 1]))
i--;
if (i == 0)
return -1;
for (i = i - 1; i >= 0 && idx < MAX_NUM_CORE; i--) {
c = coremask[i];
if (isxdigit(c) == 0) {
return -1;
}
val = xdigit2val(c);
for (j = 0; j < BITS_HEX && idx < MAX_NUM_CORE; j++, idx++) {
if ((1 << j) & val) {
mask |= (1UL << idx);
count++;
}
}
}
for (; i >= 0; i--)
if (coremask[i] != '0')
return -1;
if (count == 0)
return -1;
return mask;
}
static struct option long_options[] = {
{"workers", required_argument, 0, 'w'},
{"packets", required_argument, 0, 'n'},
{"atomic-flows", required_argument, 0, 'f'},
{"num_stages", required_argument, 0, 's'},
{"rx-mask", required_argument, 0, 'r'},
{"tx-mask", required_argument, 0, 't'},
{"sched-mask", required_argument, 0, 'e'},
{"cq-depth", required_argument, 0, 'c'},
{"work-cycles", required_argument, 0, 'W'},
{"queue-priority", no_argument, 0, 'P'},
{"parallel", no_argument, 0, 'p'},
{"ordered", no_argument, 0, 'o'},
{"quiet", no_argument, 0, 'q'},
{"dump", no_argument, 0, 'D'},
{0, 0, 0, 0}
};
static void
usage(void)
{
const char *usage_str =
" Usage: eventdev_demo [options]\n"
" Options:\n"
" -n, --packets=N Send N packets (default ~32M), 0 implies no limit\n"
" -f, --atomic-flows=N Use N random flows from 1 to N (default 16)\n"
" -s, --num_stages=N Use N atomic stages (default 1)\n"
" -r, --rx-mask=core mask Run NIC rx on CPUs in core mask\n"
" -w, --worker-mask=core mask Run worker on CPUs in core mask\n"
" -t, --tx-mask=core mask Run NIC tx on CPUs in core mask\n"
" -e --sched-mask=core mask Run scheduler on CPUs in core mask\n"
" -c --cq-depth=N Worker CQ depth (default 16)\n"
" -W --work-cycles=N Worker cycles (default 0)\n"
" -P --queue-priority Enable scheduler queue prioritization\n"
" -o, --ordered Use ordered scheduling\n"
" -p, --parallel Use parallel scheduling\n"
" -q, --quiet Minimize printed output\n"
" -D, --dump Print detailed statistics before exit"
"\n";
fprintf(stderr, "%s", usage_str);
exit(1);
}
static void
parse_app_args(int argc, char **argv)
{
int option_index;
int c;
opterr = 0;
uint64_t rx_lcore_mask = 0;
uint64_t tx_lcore_mask = 0;
uint64_t sched_lcore_mask = 0;
uint64_t worker_lcore_mask = 0;
int i;
for (;;) {
c = getopt_long(argc, argv, "r:t:e:c:w:n:f:s:poPqDW:",
long_options, &option_index);
if (c == -1)
break;
int popcnt = 0;
switch (c) {
case 'n':
cdata.num_packets = (int64_t)atol(optarg);
if (cdata.num_packets == 0)
cdata.num_packets = INT64_MAX;
break;
case 'f':
cdata.num_fids = (unsigned int)atoi(optarg);
break;
case 's':
cdata.num_stages = (unsigned int)atoi(optarg);
break;
case 'c':
cdata.worker_cq_depth = (unsigned int)atoi(optarg);
break;
case 'W':
cdata.worker_cycles = (unsigned int)atoi(optarg);
break;
case 'P':
cdata.enable_queue_priorities = 1;
break;
case 'o':
break;
case 'p':
break;
case 'q':
cdata.quiet = 1;
break;
case 'D':
cdata.dump_dev = 1;
break;
case 'w':
worker_lcore_mask = parse_coremask(optarg);
break;
case 'r':
rx_lcore_mask = parse_coremask(optarg);
popcnt = __builtin_popcountll(rx_lcore_mask);
fdata->rx_single = (popcnt == 1);
break;
case 't':
tx_lcore_mask = parse_coremask(optarg);
popcnt = __builtin_popcountll(tx_lcore_mask);
fdata->tx_single = (popcnt == 1);
break;
case 'e':
sched_lcore_mask = parse_coremask(optarg);
popcnt = __builtin_popcountll(sched_lcore_mask);
fdata->sched_single = (popcnt == 1);
break;
default:
usage();
}
}
if (worker_lcore_mask == 0 || rx_lcore_mask == 0 ||
sched_lcore_mask == 0 || tx_lcore_mask == 0) {
printf("Core part of pipeline was not assigned any cores. "
"This will stall the pipeline, please check core masks "
"(use -h for details on setting core masks):\n"
"\trx: %"PRIu64"\n\ttx: %"PRIu64"\n\tsched: %"PRIu64
"\n\tworkers: %"PRIu64"\n",
rx_lcore_mask, tx_lcore_mask, sched_lcore_mask,
worker_lcore_mask);
}
if (cdata.num_stages == 0 || cdata.num_stages > MAX_NUM_STAGES)
usage();
for (i = 0; i < MAX_NUM_CORE; i++) {
fdata->rx_core[i] = !!(rx_lcore_mask & (1UL << i));
fdata->tx_core[i] = !!(tx_lcore_mask & (1UL << i));
fdata->sched_core[i] = !!(sched_lcore_mask & (1UL << i));
fdata->worker_core[i] = !!(worker_lcore_mask & (1UL << i));
if (fdata->worker_core[i])
cdata.num_workers++;
if (core_in_use(i))
cdata.active_cores++;
}
}
static inline int
{
},
.rx_adv_conf = {
.rss_conf = {
.rss_hf = ETH_RSS_IP |
ETH_RSS_TCP |
ETH_RSS_UDP,
}
}
};
const uint16_t rx_rings = 1, tx_rings = 1;
const uint16_t rx_ring_size = 512, tx_ring_size = 512;
int retval;
uint16_t q;
return -1;
if (retval != 0)
return retval;
for (q = 0; q < rx_rings; q++) {
if (retval < 0)
return retval;
}
for (q = 0; q < tx_rings; q++) {
if (retval < 0)
return retval;
}
if (retval < 0)
return retval;
printf("Port %u MAC: %02" PRIx8 " %02" PRIx8 " %02" PRIx8
" %02" PRIx8 " %02" PRIx8 " %02" PRIx8 "\n",
(unsigned int)port,
addr.addr_bytes[0], addr.addr_bytes[1],
addr.addr_bytes[2], addr.addr_bytes[3],
addr.addr_bytes[4], addr.addr_bytes[5]);
return 0;
}
static int
init_ports(unsigned int num_ports)
{
uint8_t portid;
unsigned int i;
16384 * num_ports,
512,
0,
RTE_MBUF_DEFAULT_BUF_SIZE,
for (portid = 0; portid < num_ports; portid++)
if (port_init(portid, mp) != 0)
rte_exit(EXIT_FAILURE,
"Cannot init port %"PRIu8
"\n",
portid);
for (i = 0; i < num_ports; i++) {
void *userdata = (void *)(uintptr_t) i;
fdata->tx_buf[i] =
if (fdata->tx_buf[i] == NULL)
eth_tx_buffer_retry,
userdata);
}
return 0;
}
struct port_link {
uint8_t queue_id;
uint8_t priority;
};
static int
setup_eventdev(struct prod_data *prod_data,
struct cons_data *cons_data,
struct worker_data *worker_data)
{
const uint8_t dev_id = 0;
const uint8_t nb_queues = cdata.num_stages + 1;
const uint8_t nb_ports = cdata.num_workers + 2;
.nb_event_ports = nb_ports,
.nb_events_limit = 4096,
.nb_event_queue_flows = 1024,
.nb_event_port_dequeue_depth = 128,
.nb_event_port_enqueue_depth = 128,
};
.enqueue_depth = 64,
.new_event_threshold = 4096,
};
.nb_atomic_flows = 1024,
.nb_atomic_order_sequences = 1024,
};
.enqueue_depth = 128,
.new_event_threshold = 4096,
};
};
struct port_link worker_queues[MAX_NUM_STAGES];
struct port_link tx_queue;
unsigned int i;
if (ndev < 1) {
printf("%d: No Eventdev Devices Found\n", __LINE__);
return -1;
}
printf("\tEventdev %d: %s\n", dev_id, dev_info.driver_name);
if (dev_info.max_event_port_dequeue_depth <
dev_info.max_event_port_dequeue_depth;
if (dev_info.max_event_port_enqueue_depth <
dev_info.max_event_port_enqueue_depth;
if (ret < 0) {
printf("%d: Error configuring device\n", __LINE__);
return -1;
}
printf(" Stages:\n");
for (i = 0; i < cdata.num_stages; i++) {
printf("%d: error creating qid %d\n", __LINE__, i);
return -1;
}
cdata.qid[i] = i;
cdata.next_qid[i] = i+1;
worker_queues[i].queue_id = i;
if (cdata.enable_queue_priorities) {
const uint32_t prio_delta =
}
const char *type_str = "Atomic";
type_str = "Ordered";
break;
type_str = "Parallel";
break;
}
printf("\tStage %d, Type %s\tPriority = %d\n", i, type_str,
}
printf("\n");
printf("%d: error creating qid %d\n", __LINE__, i);
return -1;
}
tx_queue.queue_id = i;
for (i = 0; i < cdata.num_workers; i++) {
struct worker_data *w = &worker_data[i];
w->dev_id = dev_id;
printf("Error setting up port %d\n", i);
return -1;
}
uint32_t s;
for (s = 0; s < cdata.num_stages; s++) {
&worker_queues[s].queue_id,
&worker_queues[s].priority,
1) != 1) {
printf("%d: error creating link for port %d\n",
__LINE__, i);
return -1;
}
}
w->port_id = i;
}
printf("Error setting up port %d\n", i);
return -1;
}
&tx_queue.priority, 1) != 1) {
printf("%d: error creating link for port %d\n",
__LINE__, i);
return -1;
}
.enqueue_depth = 8,
.new_event_threshold = 1200,
};
printf("Error setting up port %d\n", i);
return -1;
}
*prod_data = (struct prod_data){.dev_id = dev_id,
.port_id = i + 1,
.qid = cdata.qid[0] };
*cons_data = (struct cons_data){.dev_id = dev_id,
.port_id = i };
&fdata->evdev_service_id);
if (ret != -ESRCH && ret != 0) {
printf("Error getting the service ID for sw eventdev\n");
return -1;
}
printf("Error starting eventdev\n");
return -1;
}
return dev_id;
}
static void
signal_handler(int signum)
{
if (fdata->done)
rte_exit(1,
"Exiting on signal %d\n", signum);
if (signum == SIGINT || signum == SIGTERM) {
printf("\n\nSignal %d received, preparing to exit...\n",
signum);
fdata->done = 1;
}
if (signum == SIGTSTP)
}
static inline uint64_t
port_stat(int dev_id, int32_t p)
{
char statname[64];
snprintf(statname, sizeof(statname), "port_%u_rx", p);
}
int
main(int argc, char **argv)
{
struct worker_data *worker_data;
unsigned int num_ports;
int lcore_id;
int err;
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGTSTP, signal_handler);
if (err < 0)
argc -= err;
argv += err;
fdata =
rte_malloc(NULL,
sizeof(
struct fastpath_data), 0);
if (fdata == NULL)
parse_app_args(argc, argv);
if (num_ports == 0)
const unsigned int cores_needed = cdata.active_cores;
if (!cdata.quiet) {
printf(" Config:\n");
printf("\tports: %u\n", num_ports);
printf("\tworkers: %u\n", cdata.num_workers);
printf("\tpackets: %"PRIi64"\n", cdata.num_packets);
printf("\tQueue-prio: %u\n", cdata.enable_queue_priorities);
printf("\tqid0 type: ordered\n");
printf("\tqid0 type: atomic\n");
printf("\tCores used: %u\n", cores_needed);
}
cores_needed);
if (ndevs == 0)
rte_panic(
"No dev_id devs found. Pasl in a --vdev eventdev.\n");
if (ndevs > 1)
fprintf(stderr, "Warning: More than one eventdev, using idx 0");
sizeof(worker_data[0]), 0);
if (worker_data == NULL)
int dev_id = setup_eventdev(&prod_data, &cons_data, worker_data);
if (dev_id < 0)
rte_exit(EXIT_FAILURE,
"Error setting up eventdev\n");
prod_data.num_nic_ports = num_ports;
init_ports(num_ports);
int worker_idx = 0;
if (lcore_id >= MAX_NUM_CORE)
break;
if (!fdata->rx_core[lcore_id] &&
!fdata->worker_core[lcore_id] &&
!fdata->tx_core[lcore_id] &&
!fdata->sched_core[lcore_id])
continue;
if (fdata->rx_core[lcore_id])
printf(
"[%s()] lcore %d executing NIC Rx, and using eventdev port %u\n",
__func__, lcore_id, prod_data.port_id);
if (fdata->tx_core[lcore_id])
printf(
"[%s()] lcore %d executing NIC Tx, and using eventdev port %u\n",
__func__, lcore_id, cons_data.port_id);
if (fdata->sched_core[lcore_id])
printf("[%s()] lcore %d executing scheduler\n",
__func__, lcore_id);
if (fdata->worker_core[lcore_id])
printf(
"[%s()] lcore %d executing worker, using eventdev port %u\n",
__func__, lcore_id,
worker_data[worker_idx].port_id);
lcore_id);
if (err) {
rte_panic(
"Failed to launch worker on core %d\n",
lcore_id);
continue;
}
if (fdata->worker_core[lcore_id])
worker_idx++;
}
if (core_in_use(lcore_id))
worker(&worker_data[worker_idx++]);
if (cdata.dump_dev)
if (!cdata.quiet && (port_stat(dev_id, worker_data[0].port_id) !=
(uint64_t)-ENOTSUP)) {
printf("\nPort Workload distribution:\n");
uint32_t i;
uint64_t tot_pkts = 0;
uint64_t pkts_per_wkr[RTE_MAX_LCORE] = {0};
for (i = 0; i < cdata.num_workers; i++) {
pkts_per_wkr[i] =
port_stat(dev_id, worker_data[i].port_id);
tot_pkts += pkts_per_wkr[i];
}
for (i = 0; i < cdata.num_workers; i++) {
float pc = pkts_per_wkr[i] * 100 /
((float)tot_pkts);
printf("worker %i :\t%.1f %% (%"PRIu64" pkts)\n",
i, pc, pkts_per_wkr[i]);
}
}
return 0;
}