#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;
}
int rte_eal_cleanup(void)
void rte_efd_lookup_bulk(const struct rte_efd_table *table, unsigned int socket_id, int num_keys, const void **key_list, efd_value_t *value_list)
#define RTE_EFD_BURST_MAX
const char * rte_strerror(int errnum)
static uint16_t rte_eth_rx_burst(uint16_t port_id, uint16_t queue_id, struct rte_mbuf **rx_pkts, const uint16_t nb_pkts)
int rte_eth_macaddr_get(uint16_t port_id, struct rte_ether_addr *mac_addr)
#define RTE_ETHER_ADDR_PRT_FMT
#define RTE_ETHER_ADDR_BYTES(mac_addrs)
int rte_eal_mp_remote_launch(lcore_function_t *f, void *arg, enum rte_rmt_call_main_t call_main)
unsigned int rte_socket_id(void)
static unsigned rte_lcore_id(void)
#define RTE_LOG(l, t,...)
static void rte_pktmbuf_free(struct rte_mbuf *m)
#define rte_pktmbuf_mtod_offset(m, t, o)
static __rte_always_inline unsigned int rte_ring_enqueue_bulk(struct rte_ring *r, void *const *obj_table, unsigned int n, unsigned int *free_space)