pci: introduce helpers for device name parsing/update
[dpdk.git] / lib / librte_vhost / virtio-net.c
index a03ff30..1785695 100644 (file)
@@ -53,7 +53,6 @@
 #include <rte_virtio_net.h>
 
 #include "vhost-net.h"
-#include "virtio-net.h"
 
 #define MAX_VHOST_DEVICE       1024
 static struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
@@ -296,7 +295,7 @@ vhost_destroy_device(int vid)
 
        if (dev->flags & VIRTIO_DEV_RUNNING) {
                dev->flags &= ~VIRTIO_DEV_RUNNING;
-               notify_ops->destroy_device(dev);
+               notify_ops->destroy_device(vid);
        }
 
        cleanup_device(dev, 1);
@@ -354,7 +353,7 @@ vhost_reset_owner(int vid)
 
        if (dev->flags & VIRTIO_DEV_RUNNING) {
                dev->flags &= ~VIRTIO_DEV_RUNNING;
-               notify_ops->destroy_device(dev);
+               notify_ops->destroy_device(vid);
        }
 
        cleanup_device(dev, 0);
@@ -388,8 +387,6 @@ int
 vhost_set_features(int vid, uint64_t *pu)
 {
        struct virtio_net *dev;
-       uint16_t vhost_hlen;
-       uint16_t i;
 
        dev = get_device(vid);
        if (dev == NULL)
@@ -400,9 +397,9 @@ vhost_set_features(int vid, uint64_t *pu)
        dev->features = *pu;
        if (dev->features &
                ((1 << VIRTIO_NET_F_MRG_RXBUF) | (1ULL << VIRTIO_F_VERSION_1))) {
-               vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
+               dev->vhost_hlen = sizeof(struct virtio_net_hdr_mrg_rxbuf);
        } else {
-               vhost_hlen = sizeof(struct virtio_net_hdr);
+               dev->vhost_hlen = sizeof(struct virtio_net_hdr);
        }
        LOG_DEBUG(VHOST_CONFIG,
                "(%d) mergeable RX buffers %s, virtio 1 %s\n",
@@ -410,13 +407,6 @@ vhost_set_features(int vid, uint64_t *pu)
                (dev->features & (1 << VIRTIO_NET_F_MRG_RXBUF)) ? "on" : "off",
                (dev->features & (1ULL << VIRTIO_F_VERSION_1)) ? "on" : "off");
 
-       for (i = 0; i < dev->virt_qp_nb; i++) {
-               uint16_t base_idx = i * VIRTIO_QNUM;
-
-               dev->virtqueue[base_idx + VIRTIO_RXQ]->vhost_hlen = vhost_hlen;
-               dev->virtqueue[base_idx + VIRTIO_TXQ]->vhost_hlen = vhost_hlen;
-       }
-
        return 0;
 }
 
@@ -571,6 +561,14 @@ vhost_set_vring_addr(int vid, struct vhost_vring_addr *addr)
                return -1;
        }
 
+       if (vq->last_used_idx != vq->used->idx) {
+               RTE_LOG(WARNING, VHOST_CONFIG,
+                       "last_used_idx (%u) and vq->used->idx (%u) mismatches; "
+                       "some packets maybe resent for Tx and dropped for Rx\n",
+                       vq->last_used_idx, vq->used->idx);
+               vq->last_used_idx     = vq->used->idx;
+       }
+
        vq->log_guest_addr = addr->log_guest_addr;
 
        LOG_DEBUG(VHOST_CONFIG, "(%d) mapped address desc: %p\n",
@@ -600,7 +598,6 @@ vhost_set_vring_base(int vid, struct vhost_vring_state *state)
 
        /* State->index refers to the queue index. The txq is 1, rxq is 0. */
        dev->virtqueue[state->index]->last_used_idx = state->num;
-       dev->virtqueue[state->index]->last_used_idx_res = state->num;
 
        return 0;
 }
@@ -718,13 +715,13 @@ vhost_set_backend(int vid, struct vhost_vring_file *file)
        if (!(dev->flags & VIRTIO_DEV_RUNNING)) {
                if (dev->virtqueue[VIRTIO_TXQ]->backend != VIRTIO_DEV_STOPPED &&
                    dev->virtqueue[VIRTIO_RXQ]->backend != VIRTIO_DEV_STOPPED) {
-                       if (notify_ops->new_device(dev) < 0)
+                       if (notify_ops->new_device(vid) < 0)
                                return -1;
                        dev->flags |= VIRTIO_DEV_RUNNING;
                }
        } else if (file->fd == VIRTIO_DEV_STOPPED) {
                dev->flags &= ~VIRTIO_DEV_RUNNING;
-               notify_ops->destroy_device(dev);
+               notify_ops->destroy_device(vid);
        }
 
        return 0;
@@ -767,9 +764,47 @@ rte_vhost_get_queue_num(int vid)
        return dev->virt_qp_nb;
 }
 
-int rte_vhost_enable_guest_notification(struct virtio_net *dev,
-       uint16_t queue_id, int enable)
+int
+rte_vhost_get_ifname(int vid, char *buf, size_t len)
+{
+       struct virtio_net *dev = get_device(vid);
+
+       if (dev == NULL)
+               return -1;
+
+       len = RTE_MIN(len, sizeof(dev->ifname));
+
+       strncpy(buf, dev->ifname, len);
+       buf[len - 1] = '\0';
+
+       return 0;
+}
+
+uint16_t
+rte_vhost_avail_entries(int vid, uint16_t queue_id)
 {
+       struct virtio_net *dev;
+       struct vhost_virtqueue *vq;
+
+       dev = get_device(vid);
+       if (!dev)
+               return 0;
+
+       vq = dev->virtqueue[queue_id];
+       if (!vq->enabled)
+               return 0;
+
+       return *(volatile uint16_t *)&vq->avail->idx - vq->last_used_idx;
+}
+
+int
+rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
+{
+       struct virtio_net *dev = get_device(vid);
+
+       if (dev == NULL)
+               return -1;
+
        if (enable) {
                RTE_LOG(ERR, VHOST_CONFIG,
                        "guest notification isn't supported.\n");