vhost: fix fd leaks for vhost-user server mode
[dpdk.git] / lib / librte_vhost / vhost.c
index 46095c3..dfb08db 100644 (file)
@@ -56,7 +56,7 @@
                                (1ULL << VIRTIO_NET_F_CTRL_VQ) | \
                                (1ULL << VIRTIO_NET_F_CTRL_RX) | \
                                (1ULL << VIRTIO_NET_F_GUEST_ANNOUNCE) | \
-                               (VHOST_SUPPORTS_MQ)            | \
+                               (1ULL << VIRTIO_NET_F_MQ)      | \
                                (1ULL << VIRTIO_F_VERSION_1)   | \
                                (1ULL << VHOST_F_LOG_ALL)      | \
                                (1ULL << VHOST_USER_F_PROTOCOL_FEATURES) | \
@@ -65,7 +65,9 @@
                                (1ULL << VIRTIO_NET_F_CSUM)    | \
                                (1ULL << VIRTIO_NET_F_GUEST_CSUM) | \
                                (1ULL << VIRTIO_NET_F_GUEST_TSO4) | \
-                               (1ULL << VIRTIO_NET_F_GUEST_TSO6))
+                               (1ULL << VIRTIO_NET_F_GUEST_TSO6) | \
+                               (1ULL << VIRTIO_RING_F_INDIRECT_DESC) | \
+                               (1ULL << VIRTIO_NET_F_MTU))
 
 uint64_t VHOST_FEATURES = VHOST_SUPPORTED_FEATURES;
 
@@ -120,9 +122,18 @@ static void
 free_device(struct virtio_net *dev)
 {
        uint32_t i;
+       struct vhost_virtqueue *rxq, *txq;
 
-       for (i = 0; i < dev->virt_qp_nb; i++)
-               rte_free(dev->virtqueue[i * VIRTIO_QNUM]);
+       for (i = 0; i < dev->virt_qp_nb; i++) {
+               rxq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_RXQ];
+               txq = dev->virtqueue[i * VIRTIO_QNUM + VIRTIO_TXQ];
+
+               rte_free(rxq->shadow_used_ring);
+               rte_free(txq->shadow_used_ring);
+
+               /* rxq and txq are allocated together as queue-pair */
+               rte_free(rxq);
+       }
 
        rte_free(dev);
 }
@@ -141,6 +152,8 @@ init_vring_queue(struct vhost_virtqueue *vq, int qp_idx)
        /* always set the default vq pair to enabled */
        if (qp_idx == 0)
                vq->enabled = 1;
+
+       TAILQ_INIT(&vq->zmbuf_list);
 }
 
 static void
@@ -215,9 +228,8 @@ reset_device(struct virtio_net *dev)
 }
 
 /*
- * Function is called from the CUSE open function. The device structure is
- * initialised and a new entry is added to the device configuration linked
- * list.
+ * Invoked when there is a new vhost-user connection established (when
+ * there is a new virtio device being attached).
  */
 int
 vhost_new_device(void)
@@ -239,6 +251,7 @@ vhost_new_device(void)
        if (i == MAX_VHOST_DEVICE) {
                RTE_LOG(ERR, VHOST_CONFIG,
                        "Failed to find a free slot for new device.\n");
+               rte_free(dev);
                return -1;
        }
 
@@ -249,8 +262,8 @@ vhost_new_device(void)
 }
 
 /*
- * Function is called from the CUSE release function. This function will
- * cleanup the device and remove it from device configuration linked list.
+ * Invoked when there is the vhost-user connection is broken (when
+ * the virtio device is being detached).
  */
 void
 vhost_destroy_device(int vid)
@@ -288,6 +301,35 @@ vhost_set_ifname(int vid, const char *if_name, unsigned int if_len)
        dev->ifname[sizeof(dev->ifname) - 1] = '\0';
 }
 
+void
+vhost_enable_dequeue_zero_copy(int vid)
+{
+       struct virtio_net *dev = get_device(vid);
+
+       if (dev == NULL)
+               return;
+
+       dev->dequeue_zero_copy = 1;
+}
+
+int
+rte_vhost_get_mtu(int vid, uint16_t *mtu)
+{
+       struct virtio_net *dev = get_device(vid);
+
+       if (!dev)
+               return -ENODEV;
+
+       if (!(dev->flags & VIRTIO_DEV_READY))
+               return -EAGAIN;
+
+       if (!(dev->features & VIRTIO_NET_F_MTU))
+               return -ENOTSUP;
+
+       *mtu = dev->mtu;
+
+       return 0;
+}
 
 int
 rte_vhost_get_numa_node(int vid)