#include <stdint.h>
#include <stdlib.h>
#include <signal.h>
#include <stdbool.h>
#include <unistd.h>
#define RTE_LOGTYPE_DMA RTE_LOGTYPE_USER1
#define MAX_PKT_BURST 32
#define MEMPOOL_CACHE_SIZE 512
#define MIN_POOL_SIZE 65536U
#define CMD_LINE_OPT_PORTMASK_INDEX 1
#define CMD_LINE_OPT_COPY_TYPE_INDEX 2
#define RX_DEFAULT_RINGSIZE 1024
#define TX_DEFAULT_RINGSIZE 1024
#define MAX_RX_QUEUES_COUNT 8
struct rxtx_port_config {
uint16_t rxtx_port;
uint16_t nb_queues;
uint16_t dmadev_ids[MAX_RX_QUEUES_COUNT];
};
struct rxtx_transmission_config {
struct rxtx_port_config ports[RTE_MAX_ETHPORTS];
uint16_t nb_ports;
uint16_t nb_lcores;
};
struct dma_port_statistics {
uint64_t rx[RTE_MAX_ETHPORTS];
uint64_t tx[RTE_MAX_ETHPORTS];
uint64_t tx_dropped[RTE_MAX_ETHPORTS];
uint64_t copy_dropped[RTE_MAX_ETHPORTS];
};
struct dma_port_statistics port_statistics;
struct total_statistics {
uint64_t total_packets_dropped;
uint64_t total_packets_tx;
uint64_t total_packets_rx;
uint64_t total_submitted;
uint64_t total_completed;
uint64_t total_failed;
};
typedef enum copy_mode_t {
#define COPY_MODE_SW "sw"
COPY_MODE_SW_NUM,
#define COPY_MODE_DMA "hw"
COPY_MODE_DMA_NUM,
COPY_MODE_INVALID_NUM,
COPY_MODE_SIZE_NUM = COPY_MODE_INVALID_NUM
} copy_mode_t;
static uint32_t dma_enabled_port_mask;
static uint16_t nb_queues = 1;
static int mac_updating = 1;
static copy_mode_t copy_mode = COPY_MODE_DMA_NUM;
static uint16_t ring_size = 2048;
static uint16_t stats_interval = 1;
#define MBUF_RING_SIZE 2048
#define MBUF_RING_MASK (MBUF_RING_SIZE - 1)
struct dma_bufs {
struct rte_mbuf *copies[MBUF_RING_SIZE];
uint16_t sent;
};
static struct dma_bufs dma_bufs[RTE_DMADEV_DEFAULT_MAX];
struct rxtx_transmission_config cfg;
static uint16_t nb_rxd = RX_DEFAULT_RINGSIZE;
static uint16_t nb_txd = TX_DEFAULT_RINGSIZE;
static volatile bool force_quit;
static uint32_t dma_batch_sz = MAX_PKT_BURST;
static uint32_t max_frame_size;
static uint32_t force_min_copy_size;
static struct rte_ether_addr dma_ports_eth_addr[RTE_MAX_ETHPORTS];
static void
print_port_stats(uint16_t port_id)
{
printf("\nStatistics for port %u ------------------------------"
"\nPackets sent: %34"PRIu64
"\nPackets received: %30"PRIu64
"\nPackets dropped on tx: %25"PRIu64
"\nPackets dropped on copy: %23"PRIu64,
port_id,
port_statistics.tx[port_id],
port_statistics.rx[port_id],
port_statistics.tx_dropped[port_id],
port_statistics.copy_dropped[port_id]);
}
static void
{
printf("\nDMA channel %u", dev_id);
printf(
"\n\t Total submitted ops: %"PRIu64
"", stats.
submitted);
printf(
"\n\t Total completed ops: %"PRIu64
"", stats.
completed);
printf(
"\n\t Total failed ops: %"PRIu64
"", stats.
errors);
}
static void
print_total_stats(struct total_statistics *ts)
{
printf("\nAggregate statistics ==============================="
"\nTotal packets Tx: %22"PRIu64" [pkt/s]"
"\nTotal packets Rx: %22"PRIu64" [pkt/s]"
"\nTotal packets dropped: %17"PRIu64" [pkt/s]",
ts->total_packets_tx / stats_interval,
ts->total_packets_rx / stats_interval,
ts->total_packets_dropped / stats_interval);
if (copy_mode == COPY_MODE_DMA_NUM) {
printf("\nTotal submitted ops: %19"PRIu64" [ops/s]"
"\nTotal completed ops: %19"PRIu64" [ops/s]"
"\nTotal failed ops: %22"PRIu64" [ops/s]",
ts->total_submitted / stats_interval,
ts->total_completed / stats_interval,
ts->total_failed / stats_interval);
}
printf("\n====================================================\n");
}
static void
print_stats(char *prgname)
{
struct total_statistics ts, delta_ts;
uint32_t i, port_id, dev_id;
char status_string[255];
int status_strlen;
const char clr[] = { 27, '[', '2', 'J', '\0' };
const char topLeft[] = { 27, '[', '1', ';', '1', 'H', '\0' };
status_strlen = snprintf(status_string, sizeof(status_string),
"%s, ", prgname);
status_strlen += snprintf(status_string + status_strlen,
sizeof(status_string) - status_strlen,
"Worker Threads = %d, ",
status_strlen += snprintf(status_string + status_strlen,
sizeof(status_string) - status_strlen,
"Copy Mode = %s,\n", copy_mode == COPY_MODE_SW_NUM ?
COPY_MODE_SW : COPY_MODE_DMA);
status_strlen += snprintf(status_string + status_strlen,
sizeof(status_string) - status_strlen,
"Updating MAC = %s, ", mac_updating ?
"enabled" : "disabled");
status_strlen += snprintf(status_string + status_strlen,
sizeof(status_string) - status_strlen,
"Rx Queues = %d, ", nb_queues);
status_strlen += snprintf(status_string + status_strlen,
sizeof(status_string) - status_strlen,
"Ring Size = %d\n", ring_size);
status_strlen += snprintf(status_string + status_strlen,
sizeof(status_string) - status_strlen,
"Force Min Copy Size = %u Packet Data Room Size = %u",
force_min_copy_size,
RTE_PKTMBUF_HEADROOM);
memset(&ts, 0, sizeof(struct total_statistics));
while (!force_quit) {
sleep(stats_interval);
printf("%s%s", clr, topLeft);
memset(&delta_ts, 0, sizeof(struct total_statistics));
printf("%s\n", status_string);
for (i = 0; i < cfg.nb_ports; i++) {
port_id = cfg.ports[i].rxtx_port;
print_port_stats(port_id);
delta_ts.total_packets_dropped +=
port_statistics.tx_dropped[port_id]
+ port_statistics.copy_dropped[port_id];
delta_ts.total_packets_tx +=
port_statistics.tx[port_id];
delta_ts.total_packets_rx +=
port_statistics.rx[port_id];
if (copy_mode == COPY_MODE_DMA_NUM) {
uint32_t j;
for (j = 0; j < cfg.ports[i].nb_queues; j++) {
dev_id = cfg.ports[i].dmadev_ids[j];
print_dmadev_stats(dev_id, stats);
delta_ts.total_failed += stats.
errors;
}
}
}
delta_ts.total_packets_tx -= ts.total_packets_tx;
delta_ts.total_packets_rx -= ts.total_packets_rx;
delta_ts.total_packets_dropped -= ts.total_packets_dropped;
delta_ts.total_submitted -= ts.total_submitted;
delta_ts.total_completed -= ts.total_completed;
delta_ts.total_failed -= ts.total_failed;
printf("\n");
print_total_stats(&delta_ts);
fflush(stdout);
ts.total_packets_tx += delta_ts.total_packets_tx;
ts.total_packets_rx += delta_ts.total_packets_rx;
ts.total_packets_dropped += delta_ts.total_packets_dropped;
ts.total_submitted += delta_ts.total_submitted;
ts.total_completed += delta_ts.total_completed;
ts.total_failed += delta_ts.total_failed;
}
}
static void
update_mac_addrs(
struct rte_mbuf *m, uint32_t dest_portid)
{
void *tmp;
*((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)dest_portid << 40);
}
static inline void
{
dst->
data_off = src->data_off;
memcpy(&dst->
rx_descriptor_fields1, &src->rx_descriptor_fields1,
}
static inline void
{
}
static uint32_t
uint32_t nb_rx, uint16_t dev_id)
{
struct dma_bufs *dma = &dma_bufs[dev_id];
int ret;
uint32_t i;
for (i = 0; i < nb_rx; i++) {
force_min_copy_size),
0);
if (ret < 0)
break;
dma->bufs[ret & MBUF_RING_MASK] = pkts[i];
dma->copies[ret & MBUF_RING_MASK] = pkts_copy[i];
}
ret = i;
return ret;
}
static inline uint32_t
uint32_t num, uint32_t step, uint16_t dev_id)
{
uint32_t i, k, m, n;
k = 0;
for (i = 0; i < num; i += m) {
n = dma_enqueue_packets(pkts + i, pkts_copy + i, m, dev_id);
k += n;
if (n > 0)
if (n != m)
break;
}
return k;
}
static inline uint32_t
uint16_t dev_id)
{
struct dma_bufs *dma = &dma_bufs[dev_id];
uint16_t nb_dq, filled;
return nb_dq;
for (filled = 0; filled < nb_dq; filled++) {
src[filled] = dma->bufs[(dma->sent + filled) & MBUF_RING_MASK];
dst[filled] = dma->copies[(dma->sent + filled) & MBUF_RING_MASK];
}
dma->sent += nb_dq;
return filled;
}
static void
dma_rx_port(struct rxtx_port_config *rx_config)
{
int32_t ret;
uint32_t nb_rx, nb_enq, i, j;
struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
struct rte_mbuf *pkts_burst_copy[MAX_PKT_BURST];
for (i = 0; i < rx_config->nb_queues; i++) {
pkts_burst, MAX_PKT_BURST);
if (nb_rx == 0) {
if (copy_mode == COPY_MODE_DMA_NUM &&
(nb_rx = dma_dequeue(pkts_burst, pkts_burst_copy,
MAX_PKT_BURST, rx_config->dmadev_ids[i])) > 0)
goto handle_tx;
continue;
}
port_statistics.rx[rx_config->rxtx_port] += nb_rx;
(void *)pkts_burst_copy, nb_rx);
"Unable to allocate memory.\n");
for (j = 0; j < nb_rx; j++)
pktmbuf_metadata_copy(pkts_burst[j],
pkts_burst_copy[j]);
if (copy_mode == COPY_MODE_DMA_NUM) {
nb_enq = dma_enqueue(pkts_burst, pkts_burst_copy,
nb_rx, dma_batch_sz, rx_config->dmadev_ids[i]);
(void *)&pkts_burst[nb_enq],
nb_rx - nb_enq);
(void *)&pkts_burst_copy[nb_enq],
nb_rx - nb_enq);
port_statistics.copy_dropped[rx_config->rxtx_port] +=
(nb_rx - nb_enq);
nb_rx = dma_dequeue(pkts_burst, pkts_burst_copy,
MAX_PKT_BURST, rx_config->dmadev_ids[i]);
} else {
for (j = 0; j < nb_rx; j++)
pktmbuf_sw_copy(pkts_burst[j],
pkts_burst_copy[j]);
}
handle_tx:
(void *)pkts_burst, nb_rx);
(void *)pkts_burst_copy, nb_rx, NULL);
(void *)&pkts_burst_copy[nb_enq],
nb_rx - nb_enq);
port_statistics.copy_dropped[rx_config->rxtx_port] +=
(nb_rx - nb_enq);
}
}
static void
dma_tx_port(struct rxtx_port_config *tx_config)
{
uint32_t i, j, nb_dq, nb_tx;
for (i = 0; i < tx_config->nb_queues; i++) {
(void *)mbufs, MAX_PKT_BURST, NULL);
if (nb_dq == 0)
continue;
if (mac_updating) {
for (j = 0; j < nb_dq; j++)
update_mac_addrs(mbufs[j],
tx_config->rxtx_port);
}
(void *)mbufs, nb_dq);
port_statistics.tx[tx_config->rxtx_port] += nb_tx;
port_statistics.tx_dropped[tx_config->rxtx_port] +=
(nb_dq - nb_tx);
(void *)&mbufs[nb_tx], nb_dq - nb_tx);
}
}
}
static void
rx_main_loop(void)
{
uint16_t i;
uint16_t nb_ports = cfg.nb_ports;
RTE_LOG(INFO, DMA,
"Entering main rx loop for copy on lcore %u\n",
while (!force_quit)
for (i = 0; i < nb_ports; i++)
dma_rx_port(&cfg.ports[i]);
}
static void
tx_main_loop(void)
{
uint16_t i;
uint16_t nb_ports = cfg.nb_ports;
RTE_LOG(INFO, DMA,
"Entering main tx loop for copy on lcore %u\n",
while (!force_quit)
for (i = 0; i < nb_ports; i++)
dma_tx_port(&cfg.ports[i]);
}
static void
rxtx_main_loop(void)
{
uint16_t i;
uint16_t nb_ports = cfg.nb_ports;
RTE_LOG(INFO, DMA,
"Entering main rx and tx loop for copy on" while (!force_quit)
for (i = 0; i < nb_ports; i++) {
dma_rx_port(&cfg.ports[i]);
dma_tx_port(&cfg.ports[i]);
}
}
static void start_forwarding_cores(void)
{
RTE_LOG(INFO, DMA,
"Entering %s on lcore %u\n",
if (cfg.nb_lcores == 1) {
NULL, lcore_id);
} else if (cfg.nb_lcores > 1) {
NULL, lcore_id);
lcore_id);
}
}
static int
dma_parse_portmask(const char *portmask)
{
char *end = NULL;
unsigned long pm;
pm = strtoul(portmask, &end, 16);
if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
return 0;
return pm;
}
static copy_mode_t
dma_parse_copy_mode(const char *copy_mode)
{
if (strcmp(copy_mode, COPY_MODE_SW) == 0)
return COPY_MODE_SW_NUM;
else if (strcmp(copy_mode, COPY_MODE_DMA) == 0)
return COPY_MODE_DMA_NUM;
return COPY_MODE_INVALID_NUM;
}
static int
dma_parse_args_cb(uint32_t index, const char *value, void *opaque)
{
int port_mask;
if (index == CMD_LINE_OPT_PORTMASK_INDEX) {
port_mask = dma_parse_portmask(value);
if (port_mask & ~dma_enabled_port_mask || port_mask <= 0) {
printf("Invalid portmask, %s, suggest 0x%x\n",
value, dma_enabled_port_mask);
return -1;
}
dma_enabled_port_mask = port_mask;
} else if (index == CMD_LINE_OPT_COPY_TYPE_INDEX) {
copy_mode = dma_parse_copy_mode(value);
if (copy_mode == COPY_MODE_INVALID_NUM) {
printf("Invalid copy type. Use: sw, hw\n");
return -1;
}
} else {
printf("Invalid index %u\n", index);
return -1;
}
return 0;
}
static int
dma_parse_args(int argc, char **argv, unsigned int nb_ports)
{
.usage = "[EAL options] -- [optional parameters]",
.descriptor = NULL,
.epilog = NULL,
.exit_on_error = false,
.callback = dma_parse_args_cb,
.opaque = NULL,
.args = {
{ "--mac-updating", NULL, "Enable MAC addresses updating",
&mac_updating, (void *)1,
},
{ "--no-mac-updating", NULL, "Disable MAC addresses updating",
&mac_updating, (void *)0,
},
{ "--portmask", "-p", "hexadecimal bitmask of ports to configure",
NULL, (void *)CMD_LINE_OPT_PORTMASK_INDEX,
},
{ "--nb-queue", "-q", "number of RX queues per port (default is 1)",
&nb_queues, NULL,
},
{ "--copy-type", "-c", "type of copy: sw|hw",
NULL, (void *)CMD_LINE_OPT_COPY_TYPE_INDEX,
},
{ "--ring-size", "-s", "size of dmadev descriptor ring for hardware copy mode or rte_ring for software copy mode",
&ring_size, NULL,
},
{ "--dma-batch-size", "-b", "number of requests per DMA batch",
&dma_batch_sz, NULL,
},
{ "--max-frame-size", "-f", "max frame size",
&max_frame_size, NULL,
},
{ "--force-min-copy-size", "-m", "force a minimum copy length, even for smaller packets",
&force_min_copy_size, NULL,
},
{ "--stats-interval", "-i", "interval, in seconds, between stats prints (default is 1)",
&stats_interval, NULL,
},
ARGPARSE_ARG_END(),
},
};
const unsigned int default_port_mask = (1 << nb_ports) - 1;
int ret;
dma_enabled_port_mask = default_port_mask;
if (ret != 0)
return ret;
if (dma_batch_sz == 0 || dma_batch_sz > MAX_PKT_BURST) {
printf("Invalid dma batch size, %d.\n", dma_batch_sz);
return -1;
}
printf("Invalid max frame size, %d.\n", max_frame_size);
return -1;
}
if (nb_queues == 0 || nb_queues > MAX_RX_QUEUES_COUNT) {
printf("Invalid RX queues number %d. Max %u\n",
nb_queues, MAX_RX_QUEUES_COUNT);
return -1;
}
if (ring_size == 0) {
printf("Invalid ring size, %d.\n", ring_size);
return -1;
}
if (ring_size > MBUF_RING_SIZE) {
printf("Max ring_size is %d, setting ring_size to max",
MBUF_RING_SIZE);
ring_size = MBUF_RING_SIZE;
}
if (stats_interval == 0) {
printf("Invalid stats interval, setting to 1\n");
stats_interval = 1;
}
return 0;
}
static int
check_link_status(uint32_t port_mask)
{
uint16_t portid;
printf("\nChecking link status\n");
if ((port_mask & (1 << portid)) == 0)
continue;
memset(&link, 0, sizeof(link));
if (ret < 0) {
printf("Port %u link get failed: err=%d\n",
portid, ret);
continue;
}
sizeof(link_status_text), &link);
printf("Port %d %s\n", portid, link_status_text);
link_status = 1;
}
}
static void
configure_dmadev_queue(uint32_t dev_id)
{
.nb_desc = ring_size
};
uint16_t vchan = 0;
rte_exit(EXIT_FAILURE,
"Error with rte_dma_configure()\n");
printf("Error with queue configuration\n");
}
printf("Error, no configured queues reported on device id %u\n", dev_id);
}
rte_exit(EXIT_FAILURE,
"Error with rte_dma_start()\n");
}
static void
assign_dmadevs(void)
{
uint16_t nb_dmadev = 0;
uint32_t i, j;
for (i = 0; i < cfg.nb_ports; i++) {
for (j = 0; j < cfg.ports[i].nb_queues; j++) {
if (dev_id == -1)
goto end;
cfg.ports[i].dmadev_ids[j] = dev_id;
configure_dmadev_queue(cfg.ports[i].dmadev_ids[j]);
++nb_dmadev;
}
}
end:
if (nb_dmadev < cfg.nb_ports * cfg.ports[0].nb_queues)
"Not enough dmadevs (%u) for all queues (%u).\n",
nb_dmadev, cfg.nb_ports * cfg.ports[0].nb_queues);
RTE_LOG(INFO, DMA,
"Number of used dmadevs: %u.\n", nb_dmadev);
}
static void
assign_rings(void)
{
uint32_t i;
for (i = 0; i < cfg.nb_ports; i++) {
snprintf(ring_name, sizeof(ring_name), "rx_to_tx_ring_%u", i);
ring_name, ring_size,
if (cfg.ports[i].rx_to_tx_ring == NULL)
rte_exit(EXIT_FAILURE,
"Ring create failed: %s\n",
}
}
static uint32_t
eth_dev_get_overhead_len(uint32_t max_rx_pktlen, uint16_t max_mtu)
{
uint32_t overhead_len;
if (max_mtu != UINT16_MAX && max_rx_pktlen > max_mtu)
overhead_len = max_rx_pktlen - max_mtu;
else
return overhead_len;
}
static int
{
uint32_t overhead_len;
if (max_frame_size == 0)
return 0;
return -1;
conf->
rxmode.
mtu = max_frame_size - overhead_len;
return 0;
}
static inline void
port_init(uint16_t portid,
struct rte_mempool *mbuf_pool, uint16_t nb_queues)
{
},
.rx_adv_conf = {
.rss_conf = {
.rss_key = NULL,
}
}
};
int ret, i;
if ((dma_enabled_port_mask & (1 << portid)) == 0) {
printf("Skipping disabled port %u\n", portid);
return;
}
printf("Initializing port %u... ", portid);
fflush(stdout);
if (ret < 0)
rte_exit(EXIT_FAILURE,
"Cannot get device info: %s, port=%u\n",
ret = config_port_max_pkt_len(&local_port_conf, &dev_info);
if (ret != 0)
"Invalid max frame size: %u (port %u)\n",
max_frame_size, portid);
if (ret < 0)
rte_exit(EXIT_FAILURE,
"Cannot configure device:" " err=%d, port=%u\n", ret, portid);
&nb_txd);
if (ret < 0)
"Cannot adjust number of descriptors: err=%d, port=%u\n",
ret, portid);
for (i = 0; i < nb_queues; i++) {
mbuf_pool);
if (ret < 0)
"rte_eth_rx_queue_setup:err=%d,port=%u, queue_id=%u\n",
ret, portid, i);
}
&txq_conf);
if (ret < 0)
"rte_eth_tx_queue_setup:err=%d,port=%u\n",
ret, portid);
if (ret < 0)
"rte_eth_dev_start:err=%d, port=%u\n",
ret, portid);
portid,
cfg.ports[cfg.nb_ports].rxtx_port = portid;
cfg.ports[cfg.nb_ports++].nb_queues = nb_queues;
}
static void
dmadev_dump(void)
{
uint32_t i, j;
if (copy_mode != COPY_MODE_DMA_NUM)
return;
for (i = 0; i < cfg.nb_ports; i++)
for (j = 0; j < cfg.ports[i].nb_queues; j++)
}
static void
signal_handler(int signum)
{
if (signum == SIGINT || signum == SIGTERM) {
printf("\n\nSignal %d received, preparing to exit...\n",
signum);
force_quit = true;
} else if (signum == SIGUSR1) {
dmadev_dump();
}
}
int
main(int argc, char **argv)
{
int ret;
uint16_t nb_ports, portid;
uint32_t i;
unsigned int nb_mbufs;
size_t sz;
if (ret < 0)
rte_exit(EXIT_FAILURE,
"Invalid EAL arguments\n");
argc -= ret;
argv += ret;
force_quit = false;
signal(SIGINT, signal_handler);
signal(SIGTERM, signal_handler);
signal(SIGUSR1, signal_handler);
if (nb_ports == 0)
rte_exit(EXIT_FAILURE,
"No Ethernet ports - bye\n");
ret = dma_parse_args(argc, argv, nb_ports);
if (ret < 0)
rte_exit(EXIT_FAILURE,
"Invalid DMA arguments\n");
nb_mbufs =
RTE_MAX(nb_ports * (nb_queues * (nb_rxd + nb_txd +
4 * MAX_PKT_BURST + ring_size) + ring_size +
MIN_POOL_SIZE);
sz = max_frame_size + RTE_PKTMBUF_HEADROOM;
sz =
RTE_MAX(sz, (
size_t)RTE_MBUF_DEFAULT_BUF_SIZE);
if (dma_pktmbuf_pool == NULL)
rte_exit(EXIT_FAILURE,
"Cannot init mbuf pool\n");
if (force_min_copy_size >
RTE_PKTMBUF_HEADROOM))
"Force min copy size > packet mbuf size\n");
cfg.nb_ports = 0;
port_init(portid, dma_pktmbuf_pool, nb_queues);
memset(&port_statistics, 0, sizeof(port_statistics));
while (!check_link_status(dma_enabled_port_mask) && !force_quit)
sleep(1);
if (cfg.nb_lcores < 1)
"There should be at least one worker lcore.\n");
if (copy_mode == COPY_MODE_DMA_NUM)
assign_dmadevs();
assign_rings();
start_forwarding_cores();
print_stats(argv[0]);
uint32_t j;
for (i = 0; i < cfg.nb_ports; i++) {
printf("Closing port %d\n", cfg.ports[i].rxtx_port);
if (ret != 0)
RTE_LOG(ERR, DMA,
"rte_eth_dev_stop: err=%s, port=%u\n",
if (copy_mode == COPY_MODE_DMA_NUM) {
for (j = 0; j < cfg.ports[i].nb_queues; j++) {
printf("Stopping dmadev %d\n",
cfg.ports[i].dmadev_ids[j]);
}
} else
}
printf("Bye...\n");
return 0;
}