#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <unistd.h>
#include <getopt.h>
#include <signal.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/queue.h>
#include <sys/file.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <rte_eal_memconfig.h>
#include "../include/common.h"
#define MAX_RX_QUEUE_PER_LCORE 16
#define MAX_TX_QUEUE_PER_PORT 16
struct lcore_queue_conf {
unsigned n_rx_port;
unsigned rx_port_list[MAX_RX_QUEUE_PER_LCORE];
struct mbuf_table rx_mbufs[RTE_MAX_ETHPORTS];
struct vm_port_param * port_param[MAX_RX_QUEUE_PER_LCORE];
static struct lcore_queue_conf lcore_queue_conf[RTE_MAX_LCORE];
static void
print_stats(void)
{
uint64_t total_packets_dropped, total_packets_tx, total_packets_rx;
unsigned portid;
total_packets_dropped = 0;
total_packets_tx = 0;
total_packets_rx = 0;
const char clr[] = { 27, '[', '2', 'J', '\0' };
const char topLeft[] = { 27, '[', '1', ';', '1', 'H','\0' };
printf("%s%s", clr, topLeft);
printf("\nPort statistics ====================================");
for (portid = 0; portid < ctrl->nb_ports; portid++) {
printf("\nStatistics for port %u ------------------------------"
"\nPackets sent: %24"PRIu64
"\nPackets received: %20"PRIu64
"\nPackets dropped: %21"PRIu64,
portid,
ctrl->vm_ports[portid].stats.tx,
ctrl->vm_ports[portid].stats.rx,
ctrl->vm_ports[portid].stats.dropped);
total_packets_dropped += ctrl->vm_ports[portid].stats.dropped;
total_packets_tx += ctrl->vm_ports[portid].stats.tx;
total_packets_rx += ctrl->vm_ports[portid].stats.rx;
}
printf("\nAggregate statistics ==============================="
"\nTotal packets sent: %18"PRIu64
"\nTotal packets received: %14"PRIu64
"\nTotal packets dropped: %15"PRIu64,
total_packets_tx,
total_packets_rx,
total_packets_dropped);
printf("\n====================================================\n");
}
static void
l2fwd_ivshmem_usage(const char *prgname)
{
printf("%s [EAL options] -- [-q NQ -T PERIOD]\n"
" -q NQ: number of queue (=ports) per lcore (default is 1)\n"
" -T PERIOD: statistics will be refreshed each PERIOD seconds (0 to disable, 10 default, 86400 maximum)\n",
prgname);
}
static unsigned int
l2fwd_ivshmem_parse_nqueue(const char *q_arg)
{
char *end = NULL;
unsigned long n;
n = strtoul(q_arg, &end, 10);
if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
return 0;
if (n == 0)
return 0;
if (n >= MAX_RX_QUEUE_PER_LCORE)
return 0;
return n;
}
static int
l2fwd_ivshmem_parse_timer_period(const char *q_arg)
{
char *end = NULL;
int n;
n = strtol(q_arg, &end, 10);
if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0'))
return -1;
if (n >= MAX_TIMER_PERIOD)
return -1;
return n;
}
static int
l2fwd_ivshmem_parse_args(int argc, char **argv)
{
int opt, ret;
char **argvopt;
int option_index;
char *prgname = argv[0];
static struct option lgopts[] = {
{NULL, 0, 0, 0}
};
argvopt = argv;
while ((opt = getopt_long(argc, argvopt, "q:p:T:",
lgopts, &option_index)) != EOF) {
switch (opt) {
case 'q':
l2fwd_ivshmem_rx_queue_per_lcore = l2fwd_ivshmem_parse_nqueue(optarg);
if (l2fwd_ivshmem_rx_queue_per_lcore == 0) {
printf("invalid queue number\n");
l2fwd_ivshmem_usage(prgname);
return -1;
}
break;
case 'T':
timer_period = l2fwd_ivshmem_parse_timer_period(optarg) * 1000 * TIMER_MILLISECOND;
if (timer_period < 0) {
printf("invalid timer period\n");
l2fwd_ivshmem_usage(prgname);
return -1;
}
break;
case 0:
l2fwd_ivshmem_usage(prgname);
return -1;
default:
l2fwd_ivshmem_usage(prgname);
return -1;
}
}
if (optind >= 0)
argv[optind-1] = prgname;
ret = optind-1;
optind = 0;
return ret;
}
static void
fwd_loop(void)
{
struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
unsigned lcore_id, len;
uint64_t prev_tsc, diff_tsc, cur_tsc, timer_tsc;
unsigned i, j, portid, nb_rx;
struct lcore_queue_conf *qconf;
void *tmp;
prev_tsc = 0;
timer_tsc = 0;
qconf = &lcore_queue_conf[lcore_id];
if (qconf->n_rx_port == 0) {
RTE_LOG(INFO, L2FWD_IVSHMEM,
"lcore %u has nothing to do\n", lcore_id);
return;
}
RTE_LOG(INFO, L2FWD_IVSHMEM,
"entering main loop on lcore %u\n", lcore_id);
for (i = 0; i < qconf->n_rx_port; i++) {
portid = qconf->rx_port_list[i];
RTE_LOG(INFO, L2FWD_IVSHMEM,
" -- lcoreid=%u portid=%u\n", lcore_id,
portid);
}
while (ctrl->state == STATE_FWD) {
cur_tsc = rte_rdtsc();
diff_tsc = cur_tsc - prev_tsc;
for (i = 0; i < qconf->n_rx_port; i++) {
portid = qconf->rx_port_list[i];
len = qconf->rx_mbufs[portid].len;
rx = ctrl->vm_ports[portid].rx_ring;
tx = ctrl->vm_ports[portid].dst->tx_ring;
m_table = qconf->rx_mbufs[portid].m_table;
if (len != 0) {
ctrl->vm_ports[portid].stats.tx += len;
qconf->rx_mbufs[portid].len = 0;
len = 0;
}
}
nb_rx =
RTE_MIN(nb_rx, (
unsigned) MAX_PKT_BURST);
if (nb_rx == 0)
continue;
if (nb_rx < (
RTE_DIM(qconf->rx_mbufs[portid].m_table) - len)) {
ctrl->state = STATE_FAIL;
return;
}
ctrl->vm_ports[portid].stats.rx += nb_rx;
for (j = 0; j < nb_rx; j++) {
m = pkts_burst[j];
m_table[len + j] = m;
*((uint64_t *)tmp) = 0x000000000002 + ((uint64_t)portid << 40);
}
qconf->rx_mbufs[portid].len += nb_rx;
}
}
if (timer_period > 0) {
timer_tsc += diff_tsc;
if (
unlikely(timer_tsc >= (uint64_t) timer_period)) {
print_stats();
timer_tsc = 0;
}
}
}
prev_tsc = cur_tsc;
}
}
static int
l2fwd_ivshmem_launch_one_lcore(__attribute__((unused)) void *dummy)
{
fwd_loop();
return 0;
}
int
main(int argc, char **argv)
{
struct lcore_queue_conf *qconf;
int ret;
uint8_t portid;
unsigned rx_lcore_id, lcore_id;
if (ret < 0)
rte_exit(EXIT_FAILURE,
"Invalid EAL arguments\n");
argc -= ret;
argv += ret;
ret = l2fwd_ivshmem_parse_args(argc, argv);
if (ret < 0)
rte_exit(EXIT_FAILURE,
"Invalid l2fwd-ivshmem arguments\n");
if (mz == NULL)
rte_exit(EXIT_FAILURE,
"Cannot find control memzone\n");
ctrl = (
struct ivshmem_ctrl*) mz->
addr;
if (ctrl->state == STATE_FWD)
rte_exit(EXIT_FAILURE,
"Forwarding already started!\n");
rx_lcore_id = 0;
qconf = NULL;
for (portid = 0; portid < ctrl->nb_ports; portid++) {
lcore_queue_conf[rx_lcore_id].n_rx_port ==
l2fwd_ivshmem_rx_queue_per_lcore) {
rx_lcore_id++;
if (rx_lcore_id >= RTE_MAX_LCORE)
rte_exit(EXIT_FAILURE,
"Not enough cores\n");
}
if (qconf != &lcore_queue_conf[rx_lcore_id])
qconf = &lcore_queue_conf[rx_lcore_id];
qconf->rx_port_list[qconf->n_rx_port] = portid;
qconf->port_param[qconf->n_rx_port] = &ctrl->vm_ports[portid];
qconf->n_rx_port++;
printf("Lcore %u: RX port %u\n", rx_lcore_id, (unsigned) portid);
}
sigsetup();
ctrl->state = STATE_FWD;
return -1;
}
return 0;
}