#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <string.h>
#include <sys/queue.h>
#include <stdarg.h>
#include <errno.h>
#include <getopt.h>
#include <netinet/in.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <signal.h>
#define RTE_LOGTYPE_APP RTE_LOGTYPE_USER1
#define FATAL_ERROR(fmt, args...) rte_exit(EXIT_FAILURE, fmt "\n", ##args)
#define PRINT_INFO(fmt, args...) RTE_LOG(INFO, APP, fmt "\n", ##args)
#define MAX_PORTS (RTE_MAX_LCORE / 2)
#define MAX_PACKET_SZ (2048)
#define MBUF_DATA_SZ (MAX_PACKET_SZ + RTE_PKTMBUF_HEADROOM)
#define NB_MBUF 8192
#define PKT_BURST_SZ 32
#define MEMPOOL_CACHE_SZ PKT_BURST_SZ
#define NB_RXD 128
#define NB_TXD 512
.hw_ip_checksum = 0,
.hw_vlan_filter = 0,
.jumbo_frame = 0,
.hw_strip_crc = 1,
},
.txmode = {
},
};
static uint32_t ports_mask = 0;
static uint64_t input_cores_mask = 0;
static uint64_t output_cores_mask = 0;
static uint8_t port_ids[RTE_MAX_LCORE];
struct stats {
uint64_t rx;
uint64_t tx;
uint64_t dropped;
};
static struct stats lcore_stats[RTE_MAX_LCORE];
static void
print_stats(void)
{
unsigned i;
printf("\n**Exception-Path example application statistics**\n"
"======= ====== ============ ============ ===============\n"
" Lcore Port RX TX Dropped on TX\n"
"------- ------ ------------ ------------ ---------------\n");
printf("%6u %7u %13"PRIu64" %13"PRIu64" %16"PRIu64"\n",
i, (unsigned)port_ids[i],
lcore_stats[i].rx, lcore_stats[i].tx,
lcore_stats[i].dropped);
}
printf("======= ====== ============ ============ ===============\n");
}
static void
signal_handler(int signum)
{
if (signum == SIGUSR1) {
print_stats();
}
if (signum == SIGUSR2) {
memset(&lcore_stats, 0, sizeof(lcore_stats));
printf("\n**Statistics have been reset**\n");
return;
}
}
static int tap_create(char *name)
{
struct ifreq ifr;
int fd, ret;
fd = open("/dev/net/tun", O_RDWR);
if (fd < 0)
return fd;
memset(&ifr, 0, sizeof(ifr));
ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
if (name && *name)
snprintf(ifr.ifr_name, IFNAMSIZ, "%s", name);
ret = ioctl(fd, TUNSETIFF, (void *) &ifr);
if (ret < 0) {
close(fd);
return ret;
}
if (name)
snprintf(name, IFNAMSIZ, "%s", ifr.ifr_name);
return fd;
}
static int
main_loop(__attribute__((unused)) void *arg)
{
char tap_name[IFNAMSIZ];
int tap_fd;
if ((1ULL << lcore_id) & input_cores_mask) {
snprintf(tap_name, IFNAMSIZ, "tap_dpdk_%.2u", lcore_id);
tap_fd = tap_create(tap_name);
if (tap_fd < 0)
FATAL_ERROR("Could not create tap interface \"%s\" (%d)",
tap_name, tap_fd);
PRINT_INFO("Lcore %u is reading from port %u and writing to %s",
lcore_id, (unsigned)port_ids[lcore_id], tap_name);
fflush(stdout);
for (;;) {
struct rte_mbuf *pkts_burst[PKT_BURST_SZ];
unsigned i;
const unsigned nb_rx =
pkts_burst, PKT_BURST_SZ);
lcore_stats[lcore_id].rx += nb_rx;
for (i = 0;
likely(i < nb_rx); i++) {
int ret = write(tap_fd,
lcore_stats[lcore_id].dropped++;
else
lcore_stats[lcore_id].tx++;
}
}
}
else if ((1ULL << lcore_id) & output_cores_mask) {
snprintf(tap_name, IFNAMSIZ, "tap_dpdk_%.2u", lcore_id);
tap_fd = tap_create(tap_name);
if (tap_fd < 0)
FATAL_ERROR("Could not create tap interface \"%s\" (%d)",
tap_name, tap_fd);
PRINT_INFO("Lcore %u is reading from %s and writing to port %u",
lcore_id, tap_name, (unsigned)port_ids[lcore_id]);
fflush(stdout);
for (;;) {
int ret;
if (m == NULL)
continue;
MAX_PACKET_SZ);
lcore_stats[lcore_id].rx++;
FATAL_ERROR("Reading from %s interface failed",
tap_name);
}
lcore_stats[lcore_id].dropped++;
}
else {
lcore_stats[lcore_id].tx++;
}
}
}
else {
PRINT_INFO("Lcore %u has nothing to do", lcore_id);
return 0;
}
}
static void
print_usage(const char *prgname)
{
PRINT_INFO("\nUsage: %s [EAL options] -- -p PORTMASK -i IN_CORES -o OUT_CORES\n"
" -p PORTMASK: hex bitmask of ports to use\n"
" -i IN_CORES: hex bitmask of cores which read from NIC\n"
" -o OUT_CORES: hex bitmask of cores which write to NIC",
prgname);
}
static uint64_t
parse_unsigned(const char *portmask)
{
char *end = NULL;
uint64_t num;
num = strtoull(portmask, &end, 16);
if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0'))
return 0;
return (uint64_t)num;
}
static void
setup_port_lcore_affinities(void)
{
unsigned long i;
uint8_t tx_port = 0;
uint8_t rx_port = 0;
if (input_cores_mask & (1ULL << i)) {
while ((ports_mask & (1 << rx_port)) == 0) {
rx_port++;
if (rx_port > (sizeof(ports_mask) * 8))
goto fail;
}
port_ids[i] = rx_port++;
} else if (output_cores_mask & (1ULL << (i & 0x3f))) {
while ((ports_mask & (1 << tx_port)) == 0) {
tx_port++;
if (tx_port > (sizeof(ports_mask) * 8))
goto fail;
}
port_ids[i] = tx_port++;
}
}
if (rx_port != tx_port)
goto fail;
if (ports_mask & (~((1 << rx_port) - 1)))
goto fail;
return;
fail:
FATAL_ERROR("Invalid core/port masks specified on command line");
}
static void
parse_args(int argc, char **argv)
{
int opt;
const char *prgname = argv[0];
opterr = 0;
while ((opt = getopt(argc, argv, "i:o:p:")) != EOF) {
switch (opt) {
case 'i':
input_cores_mask = parse_unsigned(optarg);
break;
case 'o':
output_cores_mask = parse_unsigned(optarg);
break;
case 'p':
ports_mask = parse_unsigned(optarg);
break;
default:
print_usage(prgname);
FATAL_ERROR("Invalid option specified");
}
}
if (input_cores_mask == 0) {
print_usage(prgname);
FATAL_ERROR("IN_CORES not specified correctly");
}
if (output_cores_mask == 0) {
print_usage(prgname);
FATAL_ERROR("OUT_CORES not specified correctly");
}
if (ports_mask == 0) {
print_usage(prgname);
FATAL_ERROR("PORTMASK not specified correctly");
}
setup_port_lcore_affinities();
}
static void
{
int ret;
PRINT_INFO("Initialising port %u ...", (unsigned)port);
fflush(stdout);
if (ret < 0)
FATAL_ERROR("Could not configure port%u (%d)",
(unsigned)port, ret);
NULL,
pktmbuf_pool);
if (ret < 0)
FATAL_ERROR("Could not setup up RX queue for port%u (%d)",
(unsigned)port, ret);
NULL);
if (ret < 0)
FATAL_ERROR("Could not setup up TX queue for port%u (%d)",
(unsigned)port, ret);
if (ret < 0)
FATAL_ERROR("Could not start port%u (%d)", (unsigned)port, ret);
}
static void
check_all_ports_link_status(uint8_t port_num, uint32_t port_mask)
{
#define CHECK_INTERVAL 100
#define MAX_CHECK_TIME 90
uint8_t portid, count, all_ports_up, print_flag = 0;
printf("\nChecking link status");
fflush(stdout);
for (count = 0; count <= MAX_CHECK_TIME; count++) {
all_ports_up = 1;
for (portid = 0; portid < port_num; portid++) {
if ((port_mask & (1 << portid)) == 0)
continue;
memset(&link, 0, sizeof(link));
if (print_flag == 1) {
if (link.link_status)
printf("Port %d Link Up - speed %u "
"Mbps - %s\n", (uint8_t)portid,
(unsigned)link.link_speed,
("full-duplex") : ("half-duplex\n"));
else
printf("Port %d Link Down\n",
(uint8_t)portid);
continue;
}
all_ports_up = 0;
break;
}
}
if (print_flag == 1)
break;
if (all_ports_up == 0) {
printf(".");
fflush(stdout);
}
if (all_ports_up == 1 || count == (MAX_CHECK_TIME - 1)) {
print_flag = 1;
printf("done\n");
}
}
}
int
main(int argc, char** argv)
{
int ret;
unsigned i,high_port;
uint8_t nb_sys_ports, port;
signal(SIGUSR1, signal_handler);
signal(SIGUSR2, signal_handler);
if (ret < 0)
FATAL_ERROR("Could not initialise EAL (%d)", ret);
argc -= ret;
argv += ret;
parse_args(argc, argv);
if (pktmbuf_pool == NULL) {
FATAL_ERROR("Could not initialise mbuf pool");
return -1;
}
if (nb_sys_ports == 0)
FATAL_ERROR("No supported Ethernet device found");
for (high_port = (sizeof(ports_mask) * 8) - 1;
(high_port != 0) && !(ports_mask & (1 << high_port));
high_port--)
;
if (high_port > nb_sys_ports)
FATAL_ERROR("Port mask requires more ports than available");
for (port = 0; port < nb_sys_ports; port++) {
if ((ports_mask & (1 << port)) == 0) {
continue;
}
init_port(port);
}
check_all_ports_link_status(nb_sys_ports, ports_mask);
return -1;
}
return 0;
}