vhost: introduce API to start a specific driver
[dpdk.git] / drivers / net / vhost / rte_eth_vhost.c
index 2a38b19..65c5ef2 100644 (file)
@@ -33,9 +33,6 @@
 #include <unistd.h>
 #include <pthread.h>
 #include <stdbool.h>
-#ifdef RTE_LIBRTE_VHOST_NUMA
-#include <numaif.h>
-#endif
 
 #include <rte_mbuf.h>
 #include <rte_ethdev.h>
@@ -48,6 +45,8 @@
 
 #include "rte_eth_vhost.h"
 
+enum {VIRTIO_RXQ, VIRTIO_TXQ, VIRTIO_QNUM};
+
 #define ETH_VHOST_IFACE_ARG            "iface"
 #define ETH_VHOST_QUEUES_ARG           "queues"
 #define ETH_VHOST_CLIENT_ARG           "client"
@@ -129,9 +128,6 @@ static struct internal_list_head internal_list =
 
 static pthread_mutex_t internal_list_lock = PTHREAD_MUTEX_INITIALIZER;
 
-static rte_atomic16_t nb_started_ports;
-static pthread_t session_th;
-
 static struct rte_eth_link pmd_link = {
                .link_speed = 10000,
                .link_duplex = ETH_LINK_FULL_DUPLEX,
@@ -392,6 +388,7 @@ eth_vhost_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
 {
        struct vhost_queue *r = q;
        uint16_t i, nb_rx = 0;
+       uint16_t nb_receive = nb_bufs;
 
        if (unlikely(rte_atomic32_read(&r->allow_queuing) == 0))
                return 0;
@@ -402,8 +399,20 @@ eth_vhost_rx(void *q, struct rte_mbuf **bufs, uint16_t nb_bufs)
                goto out;
 
        /* Dequeue packets from guest TX queue */
-       nb_rx = rte_vhost_dequeue_burst(r->vid,
-                       r->virtqueue_id, r->mb_pool, bufs, nb_bufs);
+       while (nb_receive) {
+               uint16_t nb_pkts;
+               uint16_t num = (uint16_t)RTE_MIN(nb_receive,
+                                                VHOST_MAX_PKT_BURST);
+
+               nb_pkts = rte_vhost_dequeue_burst(r->vid, r->virtqueue_id,
+                                                 r->mb_pool, &bufs[nb_rx],
+                                                 num);
+
+               nb_rx += nb_pkts;
+               nb_receive -= nb_pkts;
+               if (nb_pkts < num)
+                       break;
+       }
 
        r->stats.pkts += nb_rx;
 
@@ -585,9 +594,11 @@ new_device(int vid)
                vq->port = eth_dev->data->port_id;
        }
 
-       for (i = 0; i < rte_vhost_get_queue_num(vid) * VIRTIO_QNUM; i++)
+       for (i = 0; i < rte_vhost_get_vring_num(vid); i++)
                rte_vhost_enable_guest_notification(vid, i, 0);
 
+       rte_vhost_get_mtu(vid, &eth_dev->data->mtu);
+
        eth_dev->data->dev_link.link_status = ETH_LINK_UP;
 
        rte_atomic32_set(&internal->dev_attached, 1);
@@ -683,6 +694,12 @@ vring_state_changed(int vid, uint16_t vring, int enable)
        return 0;
 }
 
+static struct vhost_device_ops vhost_ops = {
+       .new_device          = new_device,
+       .destroy_device      = destroy_device,
+       .vring_state_changed = vring_state_changed,
+};
+
 int
 rte_eth_vhost_get_queue_event(uint8_t port_id,
                struct rte_eth_vhost_queue_event *event)
@@ -749,51 +766,6 @@ rte_eth_vhost_get_vid_from_port_id(uint8_t port_id)
        return vid;
 }
 
-static void *
-vhost_driver_session(void *param __rte_unused)
-{
-       static struct virtio_net_device_ops vhost_ops;
-
-       /* set vhost arguments */
-       vhost_ops.new_device = new_device;
-       vhost_ops.destroy_device = destroy_device;
-       vhost_ops.vring_state_changed = vring_state_changed;
-       if (rte_vhost_driver_callback_register(&vhost_ops) < 0)
-               RTE_LOG(ERR, PMD, "Can't register callbacks\n");
-
-       /* start event handling */
-       rte_vhost_driver_session_start();
-
-       return NULL;
-}
-
-static int
-vhost_driver_session_start(void)
-{
-       int ret;
-
-       ret = pthread_create(&session_th,
-                       NULL, vhost_driver_session, NULL);
-       if (ret)
-               RTE_LOG(ERR, PMD, "Can't create a thread\n");
-
-       return ret;
-}
-
-static void
-vhost_driver_session_stop(void)
-{
-       int ret;
-
-       ret = pthread_cancel(session_th);
-       if (ret)
-               RTE_LOG(ERR, PMD, "Can't cancel the thread\n");
-
-       ret = pthread_join(session_th, NULL);
-       if (ret)
-               RTE_LOG(ERR, PMD, "Can't join the thread\n");
-}
-
 static int
 eth_dev_start(struct rte_eth_dev *dev)
 {
@@ -989,31 +961,6 @@ eth_link_update(struct rte_eth_dev *dev __rte_unused,
        return 0;
 }
 
-/**
- * Disable features in feature_mask. Returns 0 on success.
- */
-int
-rte_eth_vhost_feature_disable(uint64_t feature_mask)
-{
-       return rte_vhost_feature_disable(feature_mask);
-}
-
-/**
- * Enable features in feature_mask. Returns 0 on success.
- */
-int
-rte_eth_vhost_feature_enable(uint64_t feature_mask)
-{
-       return rte_vhost_feature_enable(feature_mask);
-}
-
-/* Returns currently supported vhost features */
-uint64_t
-rte_eth_vhost_feature_get(void)
-{
-       return rte_vhost_feature_get();
-}
-
 static const struct eth_dev_ops ops = {
        .dev_start = eth_dev_start,
        .dev_stop = eth_dev_stop,
@@ -1129,10 +1076,15 @@ eth_dev_vhost_create(const char *name, char *iface_name, int16_t queues,
        if (rte_vhost_driver_register(iface_name, flags))
                goto error;
 
-       /* We need only one message handling thread */
-       if (rte_atomic16_add_return(&nb_started_ports, 1) == 1) {
-               if (vhost_driver_session_start())
-                       goto error;
+       if (rte_vhost_driver_callback_register(iface_name, &vhost_ops) < 0) {
+               RTE_LOG(ERR, PMD, "Can't register callbacks\n");
+               goto error;
+       }
+
+       if (rte_vhost_driver_start(iface_name) < 0) {
+               RTE_LOG(ERR, PMD, "Failed to start driver for %s\n",
+                       iface_name);
+               goto error;
        }
 
        return data->port_id;
@@ -1259,9 +1211,6 @@ rte_pmd_vhost_remove(const char *name)
 
        eth_dev_close(eth_dev);
 
-       if (rte_atomic16_sub_return(&nb_started_ports, 1) == 0)
-               vhost_driver_session_stop();
-
        rte_free(vring_states[eth_dev->data->port_id]);
        vring_states[eth_dev->data->port_id] = NULL;