#include <arpa/inet.h>
#include <getopt.h>
#include <linux/if_ether.h>
#include <linux/if_vlan.h>
#include <linux/virtio_net.h>
#include <linux/virtio_ring.h>
#include <signal.h>
#include <stdint.h>
#include <sys/eventfd.h>
#include <sys/param.h>
#include <unistd.h>
#include "main.h"
#include "virtio-net.h"
#include "xen_vhost.h"
#define MAX_QUEUES 128
#define MAX_SUP_PORTS 1
#define NUM_MBUFS_PER_PORT ((MAX_QUEUES*RTE_TEST_RX_DESC_DEFAULT) + \
(num_switching_cores*MAX_PKT_BURST) + \
(num_switching_cores*RTE_TEST_TX_DESC_DEFAULT) +\
(num_switching_cores*MBUF_CACHE_SIZE))
#define MBUF_CACHE_SIZE 64
#define RX_PTHRESH 8
#define RX_HTHRESH 8
#define RX_WTHRESH 4
#define TX_PTHRESH 36
#define TX_HTHRESH 0
#define TX_WTHRESH 0
#define MAX_PKT_BURST 32
#define MAX_MRG_PKT_BURST 16
#define BURST_TX_DRAIN_US 100
#define DEVICE_NOT_READY 0
#define DEVICE_READY 1
#define DEVICE_SAFE_REMOVE 2
#define REQUEST_DEV_REMOVAL 1
#define ACK_DEV_REMOVAL 0
#define RTE_TEST_RX_DESC_DEFAULT 128
#define RTE_TEST_TX_DESC_DEFAULT 512
#define INVALID_PORT_ID 0xFF
#define MAX_DEVICES 64
#define MAX_PRINT_BUFF 6072
#define MAX_LONG_OPT_SZ 64
#define MAC_ADDR_CMP 0xFFFFFFFFFFFF
static uint32_t enabled_port_mask = 0;
static uint32_t num_switching_cores = 0;
static uint32_t num_queues = 0;
uint32_t num_devices = 0;
static uint32_t enable_vm2vm = 1;
static uint32_t enable_stats = 0;
.split_hdr_size = 0,
.header_split = 0,
.hw_ip_checksum = 0,
.hw_vlan_filter = 0,
.hw_vlan_strip = 1,
.jumbo_frame = 0,
.hw_strip_crc = 1,
},
.txmode = {
},
.rx_adv_conf = {
.vmdq_rx_conf = {
.enable_default_pool = 0,
.default_pool = 0,
.nb_pool_maps = 0,
.pool_map = {{0, 0},},
},
},
};
static unsigned lcore_ids[RTE_MAX_LCORE];
static uint8_t ports[RTE_MAX_ETHPORTS];
static unsigned num_ports = 0;
const uint16_t vlan_tags[] = {
1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007,
1008, 1009, 1010, 1011, 1012, 1013, 1014, 1015,
1016, 1017, 1018, 1019, 1020, 1021, 1022, 1023,
1024, 1025, 1026, 1027, 1028, 1029, 1030, 1031,
1032, 1033, 1034, 1035, 1036, 1037, 1038, 1039,
1040, 1041, 1042, 1043, 1044, 1045, 1046, 1047,
1048, 1049, 1050, 1051, 1052, 1053, 1054, 1055,
1056, 1057, 1058, 1059, 1060, 1061, 1062, 1063,
};
static struct ether_addr vmdq_ports_eth_addr[RTE_MAX_ETHPORTS];
static struct virtio_net_data_ll *ll_root_used = NULL;
static struct virtio_net_data_ll *ll_root_free = NULL;
static struct lcore_info lcore_info[RTE_MAX_LCORE];
struct mbuf_table {
unsigned len;
unsigned txq_id;
struct rte_mbuf *m_table[MAX_PKT_BURST];
};
struct mbuf_table lcore_tx_queue[RTE_MAX_LCORE];
struct vlan_ethhdr {
unsigned char h_dest[ETH_ALEN];
unsigned char h_source[ETH_ALEN];
__be16 h_vlan_proto;
__be16 h_vlan_TCI;
__be16 h_vlan_encapsulated_proto;
};
#define VLAN_HLEN 4
#define VLAN_ETH_HLEN 18
struct device_statistics {
uint64_t tx_total;
uint64_t tx;
struct device_statistics dev_statistics[MAX_DEVICES];
static inline int
get_eth_conf(
struct rte_eth_conf *eth_conf, uint32_t num_devices)
{
struct rte_eth_vmdq_rx_conf conf;
unsigned i;
memset(&conf, 0, sizeof(conf));
conf.nb_pool_maps = num_devices;
for (i = 0; i < conf.nb_pool_maps; i++) {
conf.pool_map[i].vlan_id = vlan_tags[ i ];
conf.pool_map[i].pools = (1UL << i);
}
(void)(
rte_memcpy(eth_conf, &vmdq_conf_default,
sizeof(*eth_conf)));
return 0;
}
static inline int
validate_num_devices(uint32_t max_nb_devices)
{
if (num_devices > max_nb_devices) {
RTE_LOG(ERR, VHOST_PORT,
"invalid number of devices\n");
return -1;
}
return 0;
}
static inline int
{
const uint16_t rx_ring_size = RTE_TEST_RX_DESC_DEFAULT, tx_ring_size = RTE_TEST_TX_DESC_DEFAULT;
int retval;
uint16_t q;
num_devices = dev_info.max_vmdq_pools;
num_queues = dev_info.max_rx_queues;
retval = validate_num_devices(MAX_DEVICES);
if (retval < 0)
return retval;
retval = get_eth_conf(&port_conf, num_devices);
if (retval < 0)
return retval;
rx_rings = (uint16_t)num_queues,
if (retval != 0)
return retval;
rxconf = &dev_info.default_rxconf;
for (q = 0; q < rx_rings; q ++) {
mbuf_pool);
if (retval < 0)
return retval;
}
for (q = 0; q < tx_rings; q ++) {
NULL);
if (retval < 0)
return retval;
}
if (retval < 0)
return retval;
RTE_LOG(INFO, VHOST_PORT,
"Max virtio devices supported: %u\n", num_devices);
RTE_LOG(INFO, VHOST_PORT,
"Port %u MAC: %02"PRIx8
" %02"PRIx8
" %02"PRIx8
" %02"PRIx8" %02"PRIx8" %02"PRIx8"\n",
(unsigned)port,
vmdq_ports_eth_addr[port].addr_bytes[0],
vmdq_ports_eth_addr[port].addr_bytes[1],
vmdq_ports_eth_addr[port].addr_bytes[2],
vmdq_ports_eth_addr[port].addr_bytes[3],
vmdq_ports_eth_addr[port].addr_bytes[4],
vmdq_ports_eth_addr[port].addr_bytes[5]);
return 0;
}
static int
parse_portmask(const char *portmask)
{
char *end = NULL;
unsigned long pm;
errno = 0;
pm = strtoul(portmask, &end, 16);
if ((portmask[0] == '\0') || (end == NULL) || (*end != '\0') || (errno != 0))
return -1;
if (pm == 0)
return -1;
return pm;
}
static int
parse_num_opt(const char *q_arg, uint32_t max_valid_value)
{
char *end = NULL;
unsigned long num;
errno = 0;
num = strtoul(q_arg, &end, 10);
if ((q_arg[0] == '\0') || (end == NULL) || (*end != '\0') || (errno != 0))
return -1;
if (num > max_valid_value)
return -1;
return num;
}
static void
us_vhost_usage(const char *prgname)
{
RTE_LOG(INFO, VHOST_CONFIG,
"%s [EAL options] -- -p PORTMASK --vm2vm [0|1] --stats [0-N] --nb-devices ND\n"
" -p PORTMASK: Set mask for ports to be used by application\n"
" --vm2vm [0|1]: disable/enable(default) vm2vm comms\n"
" --stats [0-N]: 0: Disable stats, N: Time in seconds to print stats\n",
prgname);
}
static int
us_vhost_parse_args(int argc, char **argv)
{
int opt, ret;
int option_index;
unsigned i;
const char *prgname = argv[0];
static struct option long_option[] = {
{"vm2vm", required_argument, NULL, 0},
{"stats", required_argument, NULL, 0},
{NULL, 0, 0, 0}
};
while ((opt = getopt_long(argc, argv, "p:",long_option, &option_index)) != EOF) {
switch (opt) {
case 'p':
enabled_port_mask = parse_portmask(optarg);
if (enabled_port_mask == 0) {
RTE_LOG(INFO, VHOST_CONFIG,
"Invalid portmask\n");
us_vhost_usage(prgname);
return -1;
}
break;
case 0:
if (!strncmp(long_option[option_index].name, "vm2vm", MAX_LONG_OPT_SZ)) {
ret = parse_num_opt(optarg, 1);
if (ret == -1) {
RTE_LOG(INFO, VHOST_CONFIG,
"Invalid argument for vm2vm [0|1]\n");
us_vhost_usage(prgname);
return -1;
} else {
enable_vm2vm = ret;
}
}
if (!strncmp(long_option[option_index].name, "stats", MAX_LONG_OPT_SZ)) {
ret = parse_num_opt(optarg, INT32_MAX);
if (ret == -1) {
RTE_LOG(INFO, VHOST_CONFIG,
"Invalid argument for stats [0..N]\n");
us_vhost_usage(prgname);
return -1;
} else {
enable_stats = ret;
}
}
break;
default:
us_vhost_usage(prgname);
return -1;
}
}
for (i = 0; i < RTE_MAX_ETHPORTS; i++) {
if (enabled_port_mask & (1 << i))
ports[num_ports++] = (uint8_t)i;
}
if ((num_ports == 0) || (num_ports > MAX_SUP_PORTS)) {
RTE_LOG(INFO, VHOST_PORT,
"Current enabled port number is %u,"
"but only %u port can be enabled\n",num_ports, MAX_SUP_PORTS);
return -1;
}
return 0;
}
static unsigned check_ports_num(unsigned nb_ports)
{
unsigned valid_num_ports = num_ports;
unsigned portid;
if (num_ports > nb_ports) {
RTE_LOG(INFO, VHOST_PORT,
"\nSpecified port number(%u) exceeds total system port number(%u)\n",
num_ports, nb_ports);
num_ports = nb_ports;
}
for (portid = 0; portid < num_ports; portid ++) {
if (ports[portid] >= nb_ports) {
RTE_LOG(INFO, VHOST_PORT,
"\nSpecified port ID(%u) exceeds max system port ID(%u)\n",
ports[portid], (nb_ports - 1));
ports[portid] = INVALID_PORT_ID;
valid_num_ports--;
}
}
return valid_num_ports;
}
static inline uint64_t __attribute__((always_inline))
gpa_to_vva(struct virtio_net *dev, uint64_t guest_pa)
{
struct virtio_memory_regions *region;
uint32_t regionidx;
uint64_t vhost_va = 0;
for (regionidx = 0; regionidx < dev->mem->nregions; regionidx++) {
region = &dev->mem->regions[regionidx];
if ((guest_pa >= region->guest_phys_address) &&
(guest_pa <= region->guest_phys_address_end)) {
vhost_va = region->address_offset + guest_pa;
break;
}
}
RTE_LOG(DEBUG, VHOST_DATA,
"(%" PRIu64
") GPA %p| VVA %p\n",
dev->device_fh, (void*)(uintptr_t)guest_pa, (void*)(uintptr_t)vhost_va);
return vhost_va;
}
static inline uint32_t __attribute__((always_inline))
virtio_dev_rx(struct virtio_net *dev, struct
rte_mbuf **pkts, uint32_t count)
{
struct vhost_virtqueue *vq;
struct vring_desc *desc;
struct virtio_net_hdr_mrg_rxbuf virtio_hdr = {{0,0,0,0,0,0},0};
uint64_t buff_addr = 0;
uint64_t buff_hdr_addr = 0;
uint32_t head[MAX_PKT_BURST], packet_len = 0;
uint32_t head_idx, packet_success = 0;
uint16_t avail_idx, res_cur_idx;
uint16_t res_base_idx, res_end_idx;
uint16_t free_entries;
uint8_t success = 0;
void *userdata;
RTE_LOG(DEBUG, VHOST_DATA,
"(%" PRIu64
") virtio_dev_rx()\n", dev->device_fh);
vq = dev->virtqueue_rx;
count = (count > MAX_PKT_BURST) ? MAX_PKT_BURST : count;
do {
res_base_idx = vq->last_used_idx_res;
avail_idx = *((volatile uint16_t *)&vq->avail->idx);
free_entries = (avail_idx - res_base_idx);
count = free_entries;
if (count == 0)
return 0;
res_end_idx = res_base_idx + count;
res_end_idx);
res_cur_idx = res_base_idx;
RTE_LOG(DEBUG, VHOST_DATA,
"(%" PRIu64
") Current Index %d| End Index %d\n",
dev->device_fh, res_cur_idx, res_end_idx);
for (head_idx = 0; head_idx < count; head_idx++)
head[head_idx] = vq->avail->ring[(res_cur_idx + head_idx) & (vq->size - 1)];
while (res_cur_idx != res_end_idx) {
desc = &vq->desc[head[packet_success]];
buff = pkts[packet_success];
buff_addr = gpa_to_vva(dev, desc->addr);
{
buff_hdr_addr = buff_addr;
if (desc->flags & VRING_DESC_F_NEXT) {
desc->len = vq->vhost_hlen;
desc = &vq->desc[desc->next];
buff_addr = gpa_to_vva(dev, desc->addr);
} else {
buff_addr += vq->vhost_hlen;
desc->len = packet_len;
}
}
vq->used->ring[res_cur_idx & (vq->size - 1)].id = head[packet_success];
vq->used->ring[res_cur_idx & (vq->size - 1)].len = packet_len;
res_cur_idx++;
packet_success++;
rte_memcpy((
void *)(uintptr_t)buff_hdr_addr, (
const void *)&virtio_hdr, vq->vhost_hlen);
if (res_cur_idx < res_end_idx) {
}
}
while (
unlikely(vq->last_used_idx != res_base_idx))
rte_pause();
*(volatile uint16_t *)&vq->used->idx += count;
vq->last_used_idx = res_end_idx;
return count;
}
static inline int __attribute__((always_inline))
{
return ((*(uint64_t *)ea ^ *(uint64_t *)eb) & MAC_ADDR_CMP) == 0;
}
static int
link_vmdq(struct virtio_net *dev)
{
int ret;
struct virtio_net_data_ll *dev_ll;
dev_ll = ll_root_used;
while (dev_ll != NULL) {
if ((dev != dev_ll->dev) && ether_addr_cmp(&dev->mac_address, &dev_ll->dev->mac_address)) {
RTE_LOG(INFO, VHOST_DATA,
"(%"PRIu64
") WARNING: This device is using an existing MAC address and has not been registered.\n", dev->device_fh);
return -1;
}
dev_ll = dev_ll->next;
}
dev->vlan_tag = vlan_tags[dev->device_fh];
dev->vmdq_rx_q = dev->device_fh * (num_queues/num_devices);
RTE_LOG(INFO, VHOST_DATA,
"(%"PRIu64
") MAC_ADDRESS %02x:%02x:%02x:%02x:%02x:%02x and VLAN_TAG %d registered\n",
dev->device_fh,
dev->mac_address.addr_bytes[0], dev->mac_address.addr_bytes[1],
dev->mac_address.addr_bytes[2], dev->mac_address.addr_bytes[3],
dev->mac_address.addr_bytes[4], dev->mac_address.addr_bytes[5],
dev->vlan_tag);
if (ret) {
RTE_LOG(ERR, VHOST_DATA,
"(%"PRIu64
") Failed to add device MAC address to VMDQ\n",
dev->device_fh);
return -1;
}
dev->ready = DEVICE_READY;
return 0;
}
static inline void
unlink_vmdq(struct virtio_net *dev)
{
unsigned i = 0;
unsigned rx_count;
struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
if (dev->ready == DEVICE_READY) {
for (i = 0; i < 6; i++)
dev->mac_address.addr_bytes[i] = 0;
dev->vlan_tag = 0;
(uint16_t)dev->vmdq_rx_q, pkts_burst, MAX_PKT_BURST);
while (rx_count) {
for (i = 0; i < rx_count; i++)
(uint16_t)dev->vmdq_rx_q, pkts_burst, MAX_PKT_BURST);
}
dev->ready = DEVICE_NOT_READY;
}
}
static inline unsigned __attribute__((always_inline))
virtio_tx_local(struct virtio_net *dev, struct
rte_mbuf *m)
{
struct virtio_net_data_ll *dev_ll;
uint64_t ret = 0;
dev_ll = ll_root_used;
while (dev_ll != NULL) {
if (
likely(dev_ll->dev->ready == DEVICE_READY) && ether_addr_cmp(&(pkt_hdr->
d_addr),
&dev_ll->dev->mac_address)) {
if (dev_ll->dev->device_fh == dev->device_fh) {
RTE_LOG(DEBUG, VHOST_DATA,
"(%" PRIu64
") TX: "
"Source and destination MAC addresses are the same. "
"Dropping packet.\n",
dev_ll->dev->device_fh);
return 0;
}
RTE_LOG(DEBUG, VHOST_DATA,
"(%" PRIu64
") TX: "
"MAC address is local\n", dev_ll->dev->device_fh);
if (dev_ll->dev->remove) {
RTE_LOG(DEBUG, VHOST_DATA,
"(%" PRIu64
") "
"Device is marked for removal\n",
dev_ll->dev->device_fh);
} else {
ret = virtio_dev_rx(dev_ll->dev, &m, 1);
if (enable_stats) {
dev_statistics[dev->device_fh].tx_total++;
dev_statistics[dev->device_fh].tx += ret;
}
}
return 0;
}
dev_ll = dev_ll->next;
}
return -1;
}
static inline void __attribute__((always_inline))
virtio_tx_route(struct virtio_net* dev, struct
rte_mbuf *m, struct
rte_mempool *mbuf_pool, uint16_t vlan_tag)
{
struct mbuf_table *tx_q;
unsigned len, ret;
if (enable_vm2vm && (virtio_tx_local(dev, m) == 0)) {
return;
}
RTE_LOG(DEBUG, VHOST_DATA,
"(%" PRIu64
") TX: "
"MAC address is external\n", dev->device_fh);
tx_q = &lcore_tx_queue[lcore_id];
len = tx_q->len;
if(!mbuf)
return;
mbuf->
data_len = m->data_len + VLAN_HLEN;
vlan_hdr->h_vlan_encapsulated_proto = vlan_hdr->h_vlan_proto;
vlan_hdr->h_vlan_proto = htons(ETH_P_8021Q);
vlan_hdr->h_vlan_TCI = htons(vlan_tag);
(m->data_len - ETH_HLEN));
tx_q->m_table[len] = mbuf;
len++;
if (enable_stats) {
dev_statistics[dev->device_fh].tx_total++;
dev_statistics[dev->device_fh].tx++;
}
m_table = (
struct rte_mbuf **)tx_q->m_table;
ret =
rte_eth_tx_burst(ports[0], (uint16_t)tx_q->txq_id, m_table, (uint16_t) len);
do {
} while (++ret < len);
}
len = 0;
}
tx_q->len = len;
return;
}
static inline void __attribute__((always_inline))
virtio_dev_tx(struct virtio_net* dev, struct
rte_mempool *mbuf_pool)
{
struct vhost_virtqueue *vq;
struct vring_desc *desc;
uint64_t buff_addr = 0;
uint32_t head[MAX_PKT_BURST];
uint32_t used_idx;
uint32_t i;
uint16_t free_entries, packet_success = 0;
uint16_t avail_idx;
vq = dev->virtqueue_tx;
avail_idx = *((volatile uint16_t *)&vq->avail->idx);
if (vq->last_used_idx == avail_idx)
return;
RTE_LOG(DEBUG, VHOST_DATA,
"(%" PRIu64
") virtio_dev_tx()\n",
dev->device_fh);
rte_prefetch0(&vq->avail->ring[vq->last_used_idx & (vq->size - 1)]);
free_entries = avail_idx - vq->last_used_idx;
free_entries =
unlikely(free_entries < MAX_PKT_BURST) ? free_entries : MAX_PKT_BURST;
RTE_LOG(DEBUG, VHOST_DATA,
"(%" PRIu64
") Buffers available %d\n",
dev->device_fh, free_entries);
for (i = 0; i < free_entries; i++)
head[i] = vq->avail->ring[(vq->last_used_idx + i) & (vq->size - 1)];
while (packet_success < free_entries) {
desc = &vq->desc[head[packet_success]];
if (packet_success < (free_entries - 1)) {
}
used_idx = vq->last_used_idx & (vq->size - 1);
vq->used->ring[used_idx].id = head[packet_success];
vq->used->ring[used_idx].len = 0;
desc = &vq->desc[desc->next];
buff_addr = gpa_to_vva(dev, desc->addr);
m.data_len = desc->len;
m.data_off = 0;
m.nb_segs = 1;
virtio_tx_route(dev, &m, mbuf_pool, 0);
vq->last_used_idx++;
packet_success++;
}
vq->used->idx += packet_success;
}
static int
switch_worker(__attribute__((unused)) void *arg)
{
struct virtio_net *dev = NULL;
struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
struct virtio_net_data_ll *dev_ll;
struct mbuf_table *tx_q;
volatile struct lcore_ll_info *lcore_ll;
const uint64_t drain_tsc = (
rte_get_tsc_hz() + US_PER_S - 1) / US_PER_S * BURST_TX_DRAIN_US;
uint64_t prev_tsc, diff_tsc, cur_tsc, ret_count = 0;
unsigned ret, i;
uint16_t rx_count = 0;
RTE_LOG(INFO, VHOST_DATA,
"Procesing on Core %u started \n", lcore_id);
lcore_ll = lcore_info[lcore_id].lcore_ll;
prev_tsc = 0;
tx_q = &lcore_tx_queue[lcore_id];
for (i = 0; i < num_cores; i ++) {
if (lcore_ids[i] == lcore_id) {
tx_q->txq_id = i;
break;
}
}
while(1) {
cur_tsc = rte_rdtsc();
diff_tsc = cur_tsc - prev_tsc;
if (tx_q->len) {
"TX queue drained after timeout with burst size %u\n",
tx_q->len);
(uint16_t)tx_q->len);
do {
} while (++ret < tx_q->len);
}
tx_q->len = 0;
}
prev_tsc = cur_tsc;
}
if (lcore_ll->dev_removal_flag == REQUEST_DEV_REMOVAL)
lcore_ll->dev_removal_flag = ACK_DEV_REMOVAL;
dev_ll = lcore_ll->ll_root_used;
while (dev_ll != NULL) {
dev = dev_ll->dev;
dev_ll = dev_ll->next;
unlink_vmdq(dev);
dev->ready = DEVICE_SAFE_REMOVE;
continue;
}
if (
likely(dev->ready == DEVICE_READY)) {
(uint16_t)dev->vmdq_rx_q, pkts_burst, MAX_PKT_BURST);
if (rx_count) {
ret_count = virtio_dev_rx(dev, pkts_burst, rx_count);
if (enable_stats) {
}
rx_count--;
}
}
}
virtio_dev_tx(dev, mbuf_pool);
dev_ll = dev_ll->next;
}
}
return 0;
}
static void
add_data_ll_entry(struct virtio_net_data_ll **ll_root_addr, struct virtio_net_data_ll *ll_dev)
{
struct virtio_net_data_ll *ll = *ll_root_addr;
ll_dev->next = NULL;
if (ll) {
while ((ll->next != NULL) )
ll = ll->next;
ll->next = ll_dev;
} else {
*ll_root_addr = ll_dev;
}
}
static void
rm_data_ll_entry(struct virtio_net_data_ll **ll_root_addr, struct virtio_net_data_ll *ll_dev, struct virtio_net_data_ll *ll_dev_last)
{
struct virtio_net_data_ll *ll = *ll_root_addr;
if (ll_dev == ll)
*ll_root_addr = ll_dev->next;
else
ll_dev_last->next = ll_dev->next;
}
static struct virtio_net_data_ll *
get_data_ll_free_entry(struct virtio_net_data_ll **ll_root_addr)
{
struct virtio_net_data_ll *ll_free = *ll_root_addr;
struct virtio_net_data_ll *ll_dev;
if (ll_free == NULL)
return NULL;
ll_dev = ll_free;
*ll_root_addr = ll_free->next;
return ll_dev;
}
static void
put_data_ll_free_entry(struct virtio_net_data_ll **ll_root_addr, struct virtio_net_data_ll *ll_dev)
{
struct virtio_net_data_ll *ll_free = *ll_root_addr;
ll_dev->next = ll_free;
*ll_root_addr = ll_dev;
}
static struct virtio_net_data_ll *
alloc_data_ll(uint32_t size)
{
struct virtio_net_data_ll *ll_new;
uint32_t i;
ll_new = malloc(size * sizeof(struct virtio_net_data_ll));
if (ll_new == NULL) {
RTE_LOG(ERR, VHOST_CONFIG,
"Failed to allocate memory for ll_new.\n");
return NULL;
}
for (i = 0; i < size - 1; i++) {
ll_new[i].dev = NULL;
ll_new[i].next = &ll_new[i+1];
}
ll_new[i].next = NULL;
return ll_new;
}
static int
init_data_ll (void)
{
int lcore;
lcore_info[lcore].lcore_ll = malloc(sizeof(struct lcore_ll_info));
if (lcore_info[lcore].lcore_ll == NULL) {
RTE_LOG(ERR, VHOST_CONFIG,
"Failed to allocate memory for lcore_ll.\n");
return -1;
}
lcore_info[lcore].lcore_ll->device_num = 0;
lcore_info[lcore].lcore_ll->dev_removal_flag = ACK_DEV_REMOVAL;
lcore_info[lcore].lcore_ll->ll_root_used = NULL;
if (num_devices % num_switching_cores)
lcore_info[lcore].lcore_ll->ll_root_free = alloc_data_ll((num_devices / num_switching_cores) + 1);
else
lcore_info[lcore].lcore_ll->ll_root_free = alloc_data_ll(num_devices / num_switching_cores);
}
ll_root_free = alloc_data_ll(MIN((num_devices), MAX_DEVICES));
return 0;
}
static void
destroy_device (volatile struct virtio_net *dev)
{
struct virtio_net_data_ll *ll_lcore_dev_cur;
struct virtio_net_data_ll *ll_main_dev_cur;
struct virtio_net_data_ll *ll_lcore_dev_last = NULL;
struct virtio_net_data_ll *ll_main_dev_last = NULL;
int lcore;
dev->flags &= ~VIRTIO_DEV_RUNNING;
dev->remove = 1;
while(dev->ready != DEVICE_SAFE_REMOVE) {
rte_pause();
}
ll_lcore_dev_cur = lcore_info[dev->coreid].lcore_ll->ll_root_used;
while (ll_lcore_dev_cur != NULL) {
if (ll_lcore_dev_cur->dev == dev) {
break;
} else {
ll_lcore_dev_last = ll_lcore_dev_cur;
ll_lcore_dev_cur = ll_lcore_dev_cur->next;
}
}
ll_main_dev_cur = ll_root_used;
ll_main_dev_last = NULL;
while (ll_main_dev_cur != NULL) {
if (ll_main_dev_cur->dev == dev) {
break;
} else {
ll_main_dev_last = ll_main_dev_cur;
ll_main_dev_cur = ll_main_dev_cur->next;
}
}
if (ll_lcore_dev_cur == NULL || ll_main_dev_cur == NULL) {
RTE_LOG(ERR, XENHOST,
"%s: could find device in per_cpu list or main_list\n", __func__);
return;
}
rm_data_ll_entry(&lcore_info[ll_lcore_dev_cur->dev->coreid].lcore_ll->ll_root_used, ll_lcore_dev_cur, ll_lcore_dev_last);
rm_data_ll_entry(&ll_root_used, ll_main_dev_cur, ll_main_dev_last);
lcore_info[lcore].lcore_ll->dev_removal_flag = REQUEST_DEV_REMOVAL;
}
while (lcore_info[lcore].lcore_ll->dev_removal_flag != ACK_DEV_REMOVAL) {
rte_pause();
}
}
put_data_ll_free_entry(&lcore_info[ll_lcore_dev_cur->dev->coreid].lcore_ll->ll_root_free, ll_lcore_dev_cur);
put_data_ll_free_entry(&ll_root_free, ll_main_dev_cur);
lcore_info[ll_lcore_dev_cur->dev->coreid].lcore_ll->device_num--;
RTE_LOG(INFO, VHOST_DATA,
" #####(%"PRIu64
") Device has been removed from data core\n", dev->device_fh);
}
static int
new_device (struct virtio_net *dev)
{
struct virtio_net_data_ll *ll_dev;
int lcore, core_add = 0;
uint32_t device_num_min = num_devices;
ll_dev = get_data_ll_free_entry(&ll_root_free);
if (ll_dev == NULL) {
RTE_LOG(INFO, VHOST_DATA,
"(%"PRIu64
") No free entry found in linked list. Device limit "
"of %d devices per core has been reached\n",
dev->device_fh, num_devices);
return -1;
}
ll_dev->dev = dev;
add_data_ll_entry(&ll_root_used, ll_dev);
dev->ready = DEVICE_NOT_READY;
dev->remove = 0;
if (lcore_info[lcore].lcore_ll->device_num < device_num_min) {
device_num_min = lcore_info[lcore].lcore_ll->device_num;
core_add = lcore;
}
}
ll_dev->dev->coreid = core_add;
ll_dev = get_data_ll_free_entry(&lcore_info[ll_dev->dev->coreid].lcore_ll->ll_root_free);
if (ll_dev == NULL) {
RTE_LOG(INFO, VHOST_DATA,
"(%"PRIu64
") Failed to add device to data core\n", dev->device_fh);
destroy_device(dev);
return -1;
}
ll_dev->dev = dev;
add_data_ll_entry(&lcore_info[ll_dev->dev->coreid].lcore_ll->ll_root_used, ll_dev);
memset(&dev_statistics[dev->device_fh], 0, sizeof(struct device_statistics));
lcore_info[ll_dev->dev->coreid].lcore_ll->device_num++;
dev->flags |= VIRTIO_DEV_RUNNING;
RTE_LOG(INFO, VHOST_DATA,
"(%"PRIu64
") Device has been added to data core %d\n", dev->device_fh, dev->coreid);
link_vmdq(dev);
return 0;
}
{
};
static void
print_stats(void)
{
struct virtio_net_data_ll *dev_ll;
uint64_t tx_dropped, rx_dropped;
uint64_t tx, tx_total, rx, rx_total;
uint32_t device_fh;
const char clr[] = { 27, '[', '2', 'J', '\0' };
const char top_left[] = { 27, '[', '1', ';', '1', 'H','\0' };
while(1) {
sleep(enable_stats);
printf("%s%s", clr, top_left);
printf("\nDevice statistics ====================================");
dev_ll = ll_root_used;
while (dev_ll != NULL) {
device_fh = (uint32_t)dev_ll->dev->device_fh;
tx_total = dev_statistics[device_fh].tx_total;
tx = dev_statistics[device_fh].tx;
tx_dropped = tx_total - tx;
rx_dropped = rx_total - rx;
printf("\nStatistics for device %"PRIu32" ------------------------------"
"\nTX total: %"PRIu64""
"\nTX dropped: %"PRIu64""
"\nTX successful: %"PRIu64""
"\nRX total: %"PRIu64""
"\nRX dropped: %"PRIu64""
"\nRX successful: %"PRIu64"",
device_fh,
tx_total,
tx_dropped,
tx,
rx_total,
rx_dropped,
rx);
dev_ll = dev_ll->next;
}
printf("\n======================================================\n");
}
}
int init_virtio_net(struct virtio_net_device_ops const * const ops);
int
main(int argc, char *argv[])
{
unsigned lcore_id, core_id = 0;
unsigned nb_ports, valid_num_ports;
int ret;
uint8_t portid;
static pthread_t tid;
char thread_name[RTE_MAX_THREAD_NAME_LEN];
if (ret < 0)
rte_exit(EXIT_FAILURE,
"Error with EAL initialization\n");
argc -= ret;
argv += ret;
ret = us_vhost_parse_args(argc, argv);
if (ret < 0)
rte_exit(EXIT_FAILURE,
"Invalid argument\n");
for (lcore_id = 0; lcore_id < RTE_MAX_LCORE; lcore_id ++)
lcore_ids[core_id ++] = lcore_id;
rte_exit(EXIT_FAILURE,
"Not enough cores\n");
valid_num_ports = check_ports_num(nb_ports);
if ((valid_num_ports == 0) || (valid_num_ports > MAX_SUP_PORTS)) {
RTE_LOG(INFO, VHOST_PORT,
"Current enabled port number is %u,"
"but only %u port can be enabled\n",num_ports, MAX_SUP_PORTS);
return -1;
}
NUM_MBUFS_PER_PORT * valid_num_ports, MBUF_CACHE_SIZE, 0,
if (mbuf_pool == NULL)
rte_exit(EXIT_FAILURE,
"Cannot create mbuf pool\n");
for (portid = 0; portid < nb_ports; portid++) {
if ((enabled_port_mask & (1 << portid)) == 0) {
RTE_LOG(INFO, VHOST_PORT,
"Skipping disabled port %d\n", portid);
continue;
}
if (port_init(portid, mbuf_pool) != 0)
rte_exit(EXIT_FAILURE,
"Cannot initialize network ports\n");
}
if (init_data_ll() == -1)
rte_exit(EXIT_FAILURE,
"Failed to initialize linked list\n");
memset(&dev_statistics, 0, sizeof(dev_statistics));
if (enable_stats) {
ret = pthread_create(&tid, NULL, (void *)print_stats, NULL);
if (ret != 0)
"Cannot create print-stats thread\n");
snprintf(thread_name, RTE_MAX_THREAD_NAME_LEN, "print-xen-stats");
if (ret != 0)
"Cannot set print-stats name\n");
}
}
init_virtio_xen(&virtio_net_device_ops);
virtio_monitor_loop();
return 0;
}