#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdint.h>
#include <stdarg.h>
#include <inttypes.h>
#include <sys/queue.h>
#include <errno.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include "common.h"
#include "args.h"
#include "init.h"
#define PACKET_READ_SIZE 32
struct node_rx_buf {
struct rte_mbuf *buffer[PACKET_READ_SIZE];
uint16_t count;
};
struct efd_stats {
uint64_t distributed;
uint64_t drop;
} flow_dist_stats;
static struct node_rx_buf *cl_rx_buf;
static const char *
get_printable_mac_addr(uint16_t port)
{
static const char err_address[] = "00:00:00:00:00:00";
static char addresses[RTE_MAX_ETHPORTS][sizeof(err_address)];
int ret;
return err_address;
if (
unlikely(addresses[port][0] ==
'\0')) {
if (ret != 0) {
printf("Failed to get MAC address (port %u): %s\n",
return err_address;
}
snprintf(addresses[port], sizeof(addresses[port]),
}
return addresses[port];
}
static void
do_stats_display(void)
{
unsigned int i, j;
const char clr[] = {27, '[', '2', 'J', '\0'};
const char topLeft[] = {27, '[', '1', ';', '1', 'H', '\0'};
uint64_t port_tx[RTE_MAX_ETHPORTS], port_tx_drop[RTE_MAX_ETHPORTS];
uint64_t node_tx[MAX_NODES], node_tx_drop[MAX_NODES];
memset(port_tx, 0, sizeof(port_tx));
memset(port_tx_drop, 0, sizeof(port_tx_drop));
memset(node_tx, 0, sizeof(node_tx));
memset(node_tx_drop, 0, sizeof(node_tx_drop));
for (i = 0; i < num_nodes; i++) {
const struct tx_stats *tx = &info->tx_stats[i];
for (j = 0; j < info->num_ports; j++) {
const uint64_t tx_val = tx->tx[info->id[j]];
const uint64_t drop_val = tx->tx_drop[info->id[j]];
port_tx[j] += tx_val;
port_tx_drop[j] += drop_val;
node_tx[i] += tx_val;
node_tx_drop[i] += drop_val;
}
}
printf("%s%s", clr, topLeft);
printf("PORTS\n");
printf("-----\n");
for (i = 0; i < info->num_ports; i++)
printf("Port %u: '%s'\t", (unsigned int)info->id[i],
get_printable_mac_addr(info->id[i]));
printf("\n\n");
for (i = 0; i < info->num_ports; i++) {
printf("Port %u - rx: %9"PRIu64"\t"
"tx: %9"PRIu64"\n",
(unsigned int)info->id[i], info->rx_stats.rx[i],
port_tx[i]);
}
printf("\nSERVER\n");
printf("-----\n");
printf("distributed: %9"PRIu64", drop: %9"PRIu64"\n",
flow_dist_stats.distributed, flow_dist_stats.drop);
printf("\nNODES\n");
printf("-------\n");
for (i = 0; i < num_nodes; i++) {
const unsigned long long rx = nodes[i].stats.rx;
const unsigned long long rx_drop = nodes[i].stats.rx_drop;
const struct filter_stats *filter = &info->filter_stats[i];
printf("Node %2u - rx: %9llu, rx_drop: %9llu\n"
" tx: %9"PRIu64", tx_drop: %9"PRIu64"\n"
" filter_passed: %9"PRIu64", "
"filter_drop: %9"PRIu64"\n",
i, rx, rx_drop, node_tx[i], node_tx_drop[i],
filter->passed, filter->drop);
}
printf("\n");
}
static int
{
static uint32_t display_stats;
uint32_t display_init = 0;
if (__atomic_compare_exchange_n(&display_stats, &display_init, 1, 0,
__ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
const unsigned int sleeptime = 1;
sleep(sleeptime * 3);
while (sleep(sleeptime) <= sleeptime)
do_stats_display();
}
return 0;
}
static void
clear_stats(void)
{
unsigned int i;
for (i = 0; i < num_nodes; i++)
nodes[i].stats.rx = nodes[i].stats.rx_drop = 0;
}
static void
flush_rx_queue(uint16_t node)
{
uint16_t j;
struct node *cl;
if (cl_rx_buf[node].count == 0)
return;
cl = &nodes[node];
cl_rx_buf[node].count, NULL) != cl_rx_buf[node].count){
for (j = 0; j < cl_rx_buf[node].count; j++)
cl->stats.rx_drop += cl_rx_buf[node].count;
} else
cl->stats.rx += cl_rx_buf[node].count;
cl_rx_buf[node].count = 0;
}
static inline void
enqueue_rx_packet(uint8_t node,
struct rte_mbuf *buf)
{
cl_rx_buf[node].buffer[cl_rx_buf[node].count++] = buf;
}
static void
uint16_t rx_count, unsigned int socket_id)
{
uint16_t i;
uint8_t node;
for (i = 0; i < rx_count; i++) {
key_ptrs[i] = (void *)&ipv4_dst_ip[i];
}
(const void **) key_ptrs, data);
for (i = 0; i < rx_count; i++) {
node = (uint8_t) ((uintptr_t)data[i]);
if (node >= num_nodes) {
flow_dist_stats.drop++;
} else {
flow_dist_stats.distributed++;
enqueue_rx_packet(node, pkts[i]);
}
}
for (i = 0; i < num_nodes; i++)
flush_rx_queue(i);
}
static void
do_packet_forwarding(void)
{
unsigned int port_num = 0;
for (;;) {
uint16_t rx_count;
buf, PACKET_READ_SIZE);
info->rx_stats.rx[port_num] += rx_count;
process_packets(port_num, buf, rx_count, socket_id);
if (++port_num == info->num_ports)
port_num = 0;
}
}
int
main(int argc, char *argv[])
{
if (init(argc, argv) < 0)
return -1;
RTE_LOG(INFO, APP,
"Finished Process Init.\n");
cl_rx_buf = calloc(num_nodes, sizeof(cl_rx_buf[0]));
clear_stats();
do_packet_forwarding();
return 0;
}