#include <stdint.h>
#include <stdbool.h>
#include <linux/virtio_net.h>
#include "main.h"
void
vs_vhost_net_setup(struct vhost_dev *dev)
{
uint16_t i;
int vid = dev->vid;
struct vhost_queue *queue;
"setting builtin vhost-user net driver\n");
if (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF))
dev->hdr_len = sizeof(struct virtio_net_hdr_mrg_rxbuf);
else
dev->hdr_len = sizeof(struct virtio_net_hdr);
for (i = 0; i < dev->nr_vrings; i++) {
queue = &dev->queues[i];
queue->last_used_idx = 0;
queue->last_avail_idx = 0;
}
}
void
vs_vhost_net_remove(struct vhost_dev *dev)
{
free(dev->mem);
}
static inline int __attribute__((always_inline))
enqueue_pkt(struct vhost_dev *dev, struct rte_vhost_vring *vr,
{
uint32_t desc_avail, desc_offset;
uint32_t mbuf_avail, mbuf_offset;
uint32_t cpy_len;
struct vring_desc *desc;
uint64_t desc_addr;
struct virtio_net_hdr virtio_hdr = {0, 0, 0, 0, 0, 0};
uint16_t nr_desc = 1;
desc = &vr->desc[desc_idx];
if (
unlikely(desc->len < dev->hdr_len) || !desc_addr)
return -1;
*(struct virtio_net_hdr *)(uintptr_t)desc_addr = virtio_hdr;
desc_offset = dev->hdr_len;
desc_avail = desc->len - dev->hdr_len;
mbuf_offset = 0;
while (mbuf_avail != 0 || m->next != NULL) {
if (mbuf_avail == 0) {
m = m->next;
mbuf_offset = 0;
}
if (desc_avail == 0) {
if ((desc->flags & VRING_DESC_F_NEXT) == 0) {
return -1;
}
++nr_desc > vr->size))
return -1;
desc = &vr->desc[desc->next];
return -1;
desc_offset = 0;
desc_avail = desc->len;
}
cpy_len =
RTE_MIN(desc_avail, mbuf_avail);
rte_memcpy((
void *)((uintptr_t)(desc_addr + desc_offset)),
cpy_len);
mbuf_avail -= cpy_len;
mbuf_offset += cpy_len;
desc_avail -= cpy_len;
desc_offset += cpy_len;
}
return 0;
}
uint16_t
vs_enqueue_pkts(struct vhost_dev *dev, uint16_t queue_id,
{
struct vhost_queue *queue;
struct rte_vhost_vring *vr;
uint16_t avail_idx, free_entries, start_idx;
uint16_t desc_indexes[MAX_PKT_BURST];
uint16_t used_idx;
uint32_t i;
queue = &dev->queues[queue_id];
vr = &queue->vr;
avail_idx = *((volatile uint16_t *)&vr->avail->idx);
start_idx = queue->last_used_idx;
free_entries = avail_idx - start_idx;
count =
RTE_MIN(count, free_entries);
count =
RTE_MIN(count, (uint32_t)MAX_PKT_BURST);
if (count == 0)
return 0;
for (i = 0; i < count; i++) {
used_idx = (start_idx + i) & (vr->size - 1);
desc_indexes[i] = vr->avail->ring[used_idx];
vr->used->ring[used_idx].id = desc_indexes[i];
vr->used->ring[used_idx].len = pkts[i]->
pkt_len +
dev->hdr_len;
}
for (i = 0; i < count; i++) {
uint16_t desc_idx = desc_indexes[i];
int err;
err = enqueue_pkt(dev, vr, pkts[i], desc_idx);
used_idx = (start_idx + i) & (vr->size - 1);
vr->used->ring[used_idx].len = dev->hdr_len;
}
if (i + 1 < count)
}
*(volatile uint16_t *)&vr->used->idx += count;
queue->last_used_idx += count;
if (!(vr->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
&& (vr->callfd >= 0))
eventfd_write(vr->callfd, (eventfd_t)1);
return count;
}
static inline int __attribute__((always_inline))
dequeue_pkt(struct vhost_dev *dev, struct rte_vhost_vring *vr,
{
struct vring_desc *desc;
uint64_t desc_addr;
uint32_t desc_avail, desc_offset;
uint32_t mbuf_avail, mbuf_offset;
uint32_t cpy_len;
uint32_t nr_desc = 1;
desc = &vr->desc[desc_idx];
if (
unlikely((desc->len < dev->hdr_len)) ||
(desc->flags & VRING_DESC_F_INDIRECT))
return -1;
return -1;
desc = &vr->desc[desc->next];
return -1;
desc_offset = 0;
desc_avail = desc->len;
nr_desc += 1;
mbuf_offset = 0;
mbuf_avail = m->buf_len - RTE_PKTMBUF_HEADROOM;
while (1) {
cpy_len =
RTE_MIN(desc_avail, mbuf_avail);
mbuf_offset),
(void *)((uintptr_t)(desc_addr + desc_offset)),
cpy_len);
mbuf_avail -= cpy_len;
mbuf_offset += cpy_len;
desc_avail -= cpy_len;
desc_offset += cpy_len;
if (desc_avail == 0) {
if ((desc->flags & VRING_DESC_F_NEXT) == 0)
break;
++nr_desc > vr->size))
return -1;
desc = &vr->desc[desc->next];
return -1;
desc_offset = 0;
desc_avail = desc->len;
}
if (mbuf_avail == 0) {
RTE_LOG(ERR, VHOST_DATA,
"Failed to "
"allocate memory for mbuf.\n");
return -1;
}
prev->next = cur;
m->nb_segs += 1;
m->pkt_len += mbuf_offset;
prev = cur;
mbuf_offset = 0;
mbuf_avail = cur->
buf_len - RTE_PKTMBUF_HEADROOM;
}
}
prev->data_len = mbuf_offset;
m->pkt_len += mbuf_offset;
return 0;
}
uint16_t
vs_dequeue_pkts(struct vhost_dev *dev, uint16_t queue_id,
{
struct vhost_queue *queue;
struct rte_vhost_vring *vr;
uint32_t desc_indexes[MAX_PKT_BURST];
uint32_t used_idx;
uint32_t i = 0;
uint16_t free_entries;
uint16_t avail_idx;
queue = &dev->queues[queue_id];
vr = &queue->vr;
free_entries = *((volatile uint16_t *)&vr->avail->idx) -
queue->last_avail_idx;
if (free_entries == 0)
return 0;
avail_idx = queue->last_avail_idx & (vr->size - 1);
used_idx = queue->last_used_idx & (vr->size - 1);
count =
RTE_MIN(count, MAX_PKT_BURST);
count =
RTE_MIN(count, free_entries);
return 0;
for (i = 0; i < count; i++) {
avail_idx = (queue->last_avail_idx + i) & (vr->size - 1);
used_idx = (queue->last_used_idx + i) & (vr->size - 1);
desc_indexes[i] = vr->avail->ring[avail_idx];
vr->used->ring[used_idx].id = desc_indexes[i];
vr->used->ring[used_idx].len = 0;
}
for (i = 0; i < count; i++) {
int err;
"Failed to allocate memory for mbuf.\n");
break;
}
err = dequeue_pkt(dev, vr, pkts[i], desc_indexes[i], mbuf_pool);
break;
}
}
queue->last_avail_idx += i;
queue->last_used_idx += i;
vr->used->idx += i;
if (!(vr->avail->flags & VRING_AVAIL_F_NO_INTERRUPT)
&& (vr->callfd >= 0))
eventfd_write(vr->callfd, (eventfd_t)1);
return i;
}