vhost: make notify ops per vhost driver
authorYuanhan Liu <yuanhan.liu@linux.intel.com>
Sat, 1 Apr 2017 07:22:42 +0000 (15:22 +0800)
committerYuanhan Liu <yuanhan.liu@linux.intel.com>
Sat, 1 Apr 2017 08:40:13 +0000 (10:40 +0200)
Assume there is an application both support vhost-user net and
vhost-user scsi, the callback should be different. Making notify
ops per vhost driver allow application define different set of
callbacks for different driver.

Signed-off-by: Yuanhan Liu <yuanhan.liu@linux.intel.com>
Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>
doc/guides/prog_guide/vhost_lib.rst
doc/guides/rel_notes/release_17_05.rst
drivers/net/vhost/rte_eth_vhost.c
examples/tep_termination/main.c
examples/vhost/main.c
lib/librte_vhost/rte_virtio_net.h
lib/librte_vhost/socket.c
lib/librte_vhost/vhost.c
lib/librte_vhost/vhost.h
lib/librte_vhost/vhost_user.c

index 6a4d206..40f3b3b 100644 (file)
@@ -122,7 +122,7 @@ The following is an overview of some key Vhost API functions:
   starts an infinite loop, therefore it should be called in a dedicated
   thread.
 
-* ``rte_vhost_driver_callback_register(virtio_net_device_ops)``
+* ``rte_vhost_driver_callback_register(path, virtio_net_device_ops)``
 
   This function registers a set of callbacks, to let DPDK applications take
   the appropriate action when some events happen. The following events are
index da95637..5cdbe36 100644 (file)
@@ -292,6 +292,10 @@ API Changes
     * ``rte_eth_vhost_feature_enable``
     * ``rte_eth_vhost_feature_get``
 
+  * The vhost API ``rte_vhost_driver_callback_register(ops)`` is reworked to
+    be per vhost-user socket file. Thus, it takes one more argument:
+    ``rte_vhost_driver_callback_register(path, ops)``.
+
 
 ABI Changes
 -----------
index fbdfecb..33cfe5f 100644 (file)
@@ -695,6 +695,12 @@ vring_state_changed(int vid, uint16_t vring, int enable)
        return 0;
 }
 
+static struct virtio_net_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)
@@ -764,15 +770,6 @@ rte_eth_vhost_get_vid_from_port_id(uint8_t port_id)
 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();
 
@@ -1116,6 +1113,11 @@ eth_dev_vhost_create(const char *name, char *iface_name, int16_t queues,
        if (rte_vhost_driver_register(iface_name, flags))
                goto error;
 
+       if (rte_vhost_driver_callback_register(iface_name, &vhost_ops) < 0) {
+               RTE_LOG(ERR, PMD, "Can't register callbacks\n");
+               goto error;
+       }
+
        /* We need only one message handling thread */
        if (rte_atomic16_add_return(&nb_started_ports, 1) == 1) {
                if (vhost_driver_session_start())
index 8097dcd..18b977e 100644 (file)
@@ -1256,7 +1256,12 @@ main(int argc, char *argv[])
        rte_vhost_driver_disable_features(dev_basename,
                1ULL << VIRTIO_NET_F_MRG_RXBUF);
 
-       rte_vhost_driver_callback_register(&virtio_net_device_ops);
+       ret = rte_vhost_driver_callback_register(dev_basename,
+               &virtio_net_device_ops);
+       if (ret != 0) {
+               rte_exit(EXIT_FAILURE,
+                       "failed to register vhost driver callbacks.\n");
+       }
 
        rte_vhost_driver_session_start();
 
index 972a6a8..72a9d69 100644 (file)
@@ -1538,9 +1538,14 @@ main(int argc, char *argv[])
                        rte_vhost_driver_enable_features(file,
                                1ULL << VIRTIO_NET_F_CTRL_RX);
                }
-       }
 
-       rte_vhost_driver_callback_register(&virtio_net_device_ops);
+               ret = rte_vhost_driver_callback_register(file,
+                       &virtio_net_device_ops);
+               if (ret != 0) {
+                       rte_exit(EXIT_FAILURE,
+                               "failed to register vhost driver callbacks.\n");
+               }
+       }
 
        rte_vhost_driver_session_start();
        return 0;
index 08bf965..39e3611 100644 (file)
@@ -137,7 +137,8 @@ int rte_vhost_driver_disable_features(const char *path, uint64_t features);
 int rte_vhost_driver_get_features(const char *path, uint64_t *features);
 
 /* Register callbacks. */
-int rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const);
+int rte_vhost_driver_callback_register(const char *path,
+       struct virtio_net_device_ops const * const ops);
 /* Start vhost driver session blocking loop. */
 int rte_vhost_driver_session_start(void);
 
index 416b1fd..aa948b9 100644 (file)
@@ -77,6 +77,8 @@ struct vhost_user_socket {
         */
        uint64_t supported_features;
        uint64_t features;
+
+       struct virtio_net_device_ops const *notify_ops;
 };
 
 struct vhost_user_connection {
@@ -743,6 +745,36 @@ rte_vhost_driver_unregister(const char *path)
        return -1;
 }
 
+/*
+ * Register ops so that we can add/remove device to data core.
+ */
+int
+rte_vhost_driver_callback_register(const char *path,
+       struct virtio_net_device_ops const * const ops)
+{
+       struct vhost_user_socket *vsocket;
+
+       pthread_mutex_lock(&vhost_user.mutex);
+       vsocket = find_vhost_user_socket(path);
+       if (vsocket)
+               vsocket->notify_ops = ops;
+       pthread_mutex_unlock(&vhost_user.mutex);
+
+       return vsocket ? 0 : -1;
+}
+
+struct virtio_net_device_ops const *
+vhost_driver_callback_get(const char *path)
+{
+       struct vhost_user_socket *vsocket;
+
+       pthread_mutex_lock(&vhost_user.mutex);
+       vsocket = find_vhost_user_socket(path);
+       pthread_mutex_unlock(&vhost_user.mutex);
+
+       return vsocket ? vsocket->notify_ops : NULL;
+}
+
 int
 rte_vhost_driver_session_start(void)
 {
index 7b40a92..7d7bb3c 100644 (file)
@@ -51,9 +51,6 @@
 
 struct virtio_net *vhost_devices[MAX_VHOST_DEVICE];
 
-/* device ops to add/remove device to/from data core. */
-struct virtio_net_device_ops const *notify_ops;
-
 struct virtio_net *
 get_device(int vid)
 {
@@ -253,7 +250,7 @@ vhost_destroy_device(int vid)
 
        if (dev->flags & VIRTIO_DEV_RUNNING) {
                dev->flags &= ~VIRTIO_DEV_RUNNING;
-               notify_ops->destroy_device(vid);
+               dev->notify_ops->destroy_device(vid);
        }
 
        cleanup_device(dev, 1);
@@ -396,14 +393,3 @@ rte_vhost_enable_guest_notification(int vid, uint16_t queue_id, int enable)
        dev->virtqueue[queue_id]->used->flags = VRING_USED_F_NO_NOTIFY;
        return 0;
 }
-
-/*
- * Register ops so that we can add/remove device to data core.
- */
-int
-rte_vhost_driver_callback_register(struct virtio_net_device_ops const * const ops)
-{
-       notify_ops = ops;
-
-       return 0;
-}
index 692691b..6186216 100644 (file)
@@ -185,6 +185,8 @@ struct virtio_net {
        struct ether_addr       mac;
        uint16_t                mtu;
 
+       struct virtio_net_device_ops const *notify_ops;
+
        uint32_t                nr_guest_pages;
        uint32_t                max_guest_pages;
        struct guest_page       *guest_pages;
@@ -288,7 +290,6 @@ gpa_to_hpa(struct virtio_net *dev, uint64_t gpa, uint64_t size)
        return 0;
 }
 
-struct virtio_net_device_ops const *notify_ops;
 struct virtio_net *get_device(int vid);
 
 int vhost_new_device(void);
@@ -301,6 +302,8 @@ int alloc_vring_queue_pair(struct virtio_net *dev, uint32_t qp_idx);
 void vhost_set_ifname(int, const char *if_name, unsigned int if_len);
 void vhost_enable_dequeue_zero_copy(int vid);
 
+struct virtio_net_device_ops const *vhost_driver_callback_get(const char *path);
+
 /*
  * Backend-specific cleanup.
  *
index 65fd0fc..2083640 100644 (file)
@@ -135,7 +135,7 @@ vhost_user_reset_owner(struct virtio_net *dev)
 {
        if (dev->flags & VIRTIO_DEV_RUNNING) {
                dev->flags &= ~VIRTIO_DEV_RUNNING;
-               notify_ops->destroy_device(dev->vid);
+               dev->notify_ops->destroy_device(dev->vid);
        }
 
        cleanup_device(dev, 0);
@@ -509,7 +509,7 @@ vhost_user_set_mem_table(struct virtio_net *dev, struct VhostUserMsg *pmsg)
        /* Remove from the data plane. */
        if (dev->flags & VIRTIO_DEV_RUNNING) {
                dev->flags &= ~VIRTIO_DEV_RUNNING;
-               notify_ops->destroy_device(dev->vid);
+               dev->notify_ops->destroy_device(dev->vid);
        }
 
        if (dev->mem) {
@@ -693,7 +693,7 @@ vhost_user_set_vring_kick(struct virtio_net *dev, struct VhostUserMsg *pmsg)
                                                "dequeue zero copy is enabled\n");
                        }
 
-                       if (notify_ops->new_device(dev->vid) == 0)
+                       if (dev->notify_ops->new_device(dev->vid) == 0)
                                dev->flags |= VIRTIO_DEV_RUNNING;
                }
        }
@@ -727,7 +727,7 @@ vhost_user_get_vring_base(struct virtio_net *dev,
        /* We have to stop the queue (virtio) if it is running. */
        if (dev->flags & VIRTIO_DEV_RUNNING) {
                dev->flags &= ~VIRTIO_DEV_RUNNING;
-               notify_ops->destroy_device(dev->vid);
+               dev->notify_ops->destroy_device(dev->vid);
        }
 
        dev->flags &= ~VIRTIO_DEV_READY;
@@ -769,8 +769,8 @@ vhost_user_set_vring_enable(struct virtio_net *dev,
                "set queue enable: %d to qp idx: %d\n",
                enable, state->index);
 
-       if (notify_ops->vring_state_changed)
-               notify_ops->vring_state_changed(dev->vid, state->index, enable);
+       if (dev->notify_ops->vring_state_changed)
+               dev->notify_ops->vring_state_changed(dev->vid, state->index, enable);
 
        dev->virtqueue[state->index]->enabled = enable;
 
@@ -984,6 +984,16 @@ vhost_user_msg_handler(int vid, int fd)
        if (dev == NULL)
                return -1;
 
+       if (!dev->notify_ops) {
+               dev->notify_ops = vhost_driver_callback_get(dev->ifname);
+               if (!dev->notify_ops) {
+                       RTE_LOG(ERR, VHOST_CONFIG,
+                               "failed to get callback ops for driver %s\n",
+                               dev->ifname);
+                       return -1;
+               }
+       }
+
        ret = read_vhost_message(fd, &msg);
        if (ret <= 0 || msg.request >= VHOST_USER_MAX) {
                if (ret < 0)