vhost-cuse and vhost-user should have a independent device list.
This patch moves vhost-cuse device list and list accessor functions
to 'virtio-net-cdev.c'.

Signed-off-by: Tetsuya Mukawa <mukawa at igel.co.jp>
---
 lib/librte_vhost/vhost-net-cdev.c  |   1 +
 lib/librte_vhost/vhost-net.h       |   5 +-
 lib/librte_vhost/virtio-net-cdev.c | 133 +++++++++++++++++++++++++++++++++++--
 lib/librte_vhost/virtio-net.c      | 121 ++++++++++++++-------------------
 4 files changed, 181 insertions(+), 79 deletions(-)

diff --git a/lib/librte_vhost/vhost-net-cdev.c 
b/lib/librte_vhost/vhost-net-cdev.c
index 12d0f68..090c6fc 100644
--- a/lib/librte_vhost/vhost-net-cdev.c
+++ b/lib/librte_vhost/vhost-net-cdev.c
@@ -66,6 +66,7 @@ fuse_req_to_vhost_ctx(fuse_req_t req, struct fuse_file_info 
*fi)
        struct vhost_device_ctx ctx;
        struct fuse_ctx const *const req_ctx = fuse_req_ctx(req);

+       ctx.type = VHOST_DRV_CUSE;
        ctx.pid = req_ctx->pid;
        ctx.fh = fi->fh;

diff --git a/lib/librte_vhost/vhost-net.h b/lib/librte_vhost/vhost-net.h
index 09a99ce..64873d0 100644
--- a/lib/librte_vhost/vhost-net.h
+++ b/lib/librte_vhost/vhost-net.h
@@ -77,8 +77,9 @@
  * Structure used to identify device context.
  */
 struct vhost_device_ctx {
-       pid_t           pid;    /* PID of process calling the IOCTL. */
-       uint64_t        fh;     /* Populated with fi->fh to track the device 
index. */
+       vhost_driver_type_t     type;   /* driver type. */
+       pid_t                   pid;    /* PID of process calling the IOCTL. */
+       uint64_t                fh;     /* Populated with fi->fh to track the 
device index. */
 };

 /*
diff --git a/lib/librte_vhost/virtio-net-cdev.c 
b/lib/librte_vhost/virtio-net-cdev.c
index f225bf5..70bc578 100644
--- a/lib/librte_vhost/virtio-net-cdev.c
+++ b/lib/librte_vhost/virtio-net-cdev.c
@@ -41,6 +41,24 @@
 #include "vhost-net.h"
 #include "eventfd_link/eventfd_link.h"

+/* Functions defined in virtio_net.c */
+static void init_device(struct virtio_net *dev);
+static void cleanup_device(struct virtio_net *dev);
+static void free_device(struct virtio_net_config_ll *ll_dev);
+static int new_device(struct vhost_device_ctx ctx);
+static void destroy_device(struct vhost_device_ctx ctx);
+static int set_owner(struct vhost_device_ctx ctx);
+static int reset_owner(struct vhost_device_ctx ctx);
+static int get_features(struct vhost_device_ctx ctx, uint64_t *pu);
+static int set_features(struct vhost_device_ctx ctx, uint64_t *pu);
+static int set_vring_num(struct vhost_device_ctx ctx, struct vhost_vring_state 
*state);
+static int set_vring_addr(struct vhost_device_ctx ctx, struct vhost_vring_addr 
*addr);
+static int set_vring_base(struct vhost_device_ctx ctx, struct 
vhost_vring_state *state);
+static int set_backend(struct vhost_device_ctx ctx, struct vhost_vring_file 
*file);
+
+/* Root address of the linked list in the configuration core. */
+static struct virtio_net_config_ll *cdev_ll_root;
+
 const char eventfd_cdev[] = "/dev/eventfd-link";

 /* Line size for reading maps file. */
@@ -65,11 +83,114 @@ struct procmap {
        char            fname[PATH_MAX];/* File name. */
 };

+/**
+ * Retrieves an entry from the devices configuration linked list.
+ */
+static struct virtio_net_config_ll *
+cdev_get_config_ll_entry(struct vhost_device_ctx ctx)
+{
+       struct virtio_net_config_ll *ll_dev = cdev_ll_root;
+
+       /* Loop through linked list until the device_fh is found. */
+       while (ll_dev != NULL) {
+               if (ll_dev->dev.device_fh == ctx.fh)
+                       return ll_dev;
+               ll_dev = ll_dev->next;
+       }
+
+       return NULL;
+}
+
+/**
+ * Searches the configuration core linked list and retrieves the device if it 
exists.
+ */
+static struct virtio_net *
+cdev_get_device(struct vhost_device_ctx ctx)
+{
+       struct virtio_net_config_ll *ll_dev;
+
+       ll_dev = cdev_get_config_ll_entry(ctx);
+
+       /* If a matching entry is found in the linked list, return the device 
in that entry. */
+       if (ll_dev)
+               return &ll_dev->dev;
+
+       RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Device not found in linked 
list.\n", ctx.fh);
+       return NULL;
+}
+
+/**
+ * Add entry containing a device to the device configuration linked list.
+ */
+static void
+cdev_add_config_ll_entry(struct virtio_net_config_ll *new_ll_dev)
+{
+       struct virtio_net_config_ll *ll_dev = cdev_ll_root;
+
+       /* If ll_dev == NULL then this is the first device so go to else */
+       if (ll_dev) {
+               /* If the 1st device_fh != 0 then we insert our device here. */
+               if (ll_dev->dev.device_fh != 0) {
+                       new_ll_dev->dev.device_fh = 0;
+                       new_ll_dev->next = ll_dev;
+                       cdev_ll_root = new_ll_dev;
+               } else {
+                       /* Increment through the ll until we find un unused 
device_fh. Insert the device at that entry*/
+                       while ((ll_dev->next != NULL) && (ll_dev->dev.device_fh 
== (ll_dev->next->dev.device_fh - 1)))
+                               ll_dev = ll_dev->next;
+
+                       new_ll_dev->dev.device_fh = ll_dev->dev.device_fh + 1;
+                       new_ll_dev->next = ll_dev->next;
+                       ll_dev->next = new_ll_dev;
+               }
+       } else {
+               cdev_ll_root = new_ll_dev;
+               cdev_ll_root->dev.device_fh = 0;
+       }
+
+}
+
+/**
+ * Remove an entry from the device configuration linked list.
+ */
+static struct virtio_net_config_ll *
+cdev_rm_config_ll_entry(struct virtio_net_config_ll *ll_dev, struct 
virtio_net_config_ll *ll_dev_last)
+{
+       /* First remove the device and then clean it up. */
+       if (ll_dev == cdev_ll_root) {
+               cdev_ll_root = ll_dev->next;
+               cleanup_device(&ll_dev->dev);
+               free_device(ll_dev);
+               return cdev_ll_root;
+       } else {
+               if (likely(ll_dev_last != NULL)) {
+                       ll_dev_last->next = ll_dev->next;
+                       cleanup_device(&ll_dev->dev);
+                       free_device(ll_dev);
+                       return ll_dev_last->next;
+               } else {
+                       cleanup_device(&ll_dev->dev);
+                       free_device(ll_dev);
+                       RTE_LOG(ERR, VHOST_CONFIG, "Remove entry from config_ll 
failed\n");
+                       return NULL;
+               }
+       }
+}
+
+/**
+ * Returns the root entry of linked list
+ */
+static struct virtio_net_config_ll *
+cdev_get_config_ll_root(void)
+{
+       return cdev_ll_root;
+}
+
 /*
  * Locate the file containing QEMU's memory space and map it to our address 
space.
  */
 static int
-host_memory_map(struct virtio_net *dev, struct virtio_memory *mem,
+cdev_host_memory_map(struct virtio_net *dev, struct virtio_memory *mem,
        pid_t pid, uint64_t addr)
 {
        struct dirent *dptr = NULL;
@@ -247,7 +368,7 @@ cuse_set_mem_table(struct vhost_device_ctx ctx, const void 
*mem_regions_addr,
        uint64_t size = offsetof(struct vhost_memory, regions);
        uint32_t regionidx, valid_regions;

-       dev = get_device(ctx);
+       dev = cdev_get_device(ctx);
        if (dev == NULL)
                return -1;

@@ -291,7 +412,7 @@ cuse_set_mem_table(struct vhost_device_ctx ctx, const void 
*mem_regions_addr,
                if (mem->regions[regionidx].guest_phys_address == 0x0) {
                        mem->base_address = 
mem->regions[regionidx].userspace_address;
                        /* Map VM memory file */
-                       if (host_memory_map(dev, mem, ctx.pid, 
mem->base_address) != 0) {
+                       if (cdev_host_memory_map(dev, mem, ctx.pid, 
mem->base_address) != 0) {
                                free(mem);
                                return -1;
                        }
@@ -356,7 +477,7 @@ cuse_get_vring_base(struct vhost_device_ctx ctx, uint32_t 
index,
 {
        struct virtio_net *dev;

-       dev = get_device(ctx);
+       dev = cdev_get_device(ctx);
        if (dev == NULL)
                return -1;

@@ -408,7 +529,7 @@ cuse_set_vring_call(struct vhost_device_ctx ctx, struct 
vhost_vring_file *file)
        struct eventfd_copy     eventfd_kick;
        struct vhost_virtqueue *vq;

-       dev = get_device(ctx);
+       dev = cdev_get_device(ctx);
        if (dev == NULL)
                return -1;

@@ -442,7 +563,7 @@ cuse_set_vring_kick(struct vhost_device_ctx ctx, struct 
vhost_vring_file *file)
        struct eventfd_copy eventfd_call;
        struct vhost_virtqueue *vq;

-       dev = get_device(ctx);
+       dev = cdev_get_device(ctx);
        if (dev == NULL)
                return -1;

diff --git a/lib/librte_vhost/virtio-net.c b/lib/librte_vhost/virtio-net.c
index 985c66b..603bb09 100644
--- a/lib/librte_vhost/virtio-net.c
+++ b/lib/librte_vhost/virtio-net.c
@@ -61,8 +61,6 @@ struct virtio_net_config_ll {

 /* device ops to add/remove device to data core. */
 static struct virtio_net_device_ops const *notify_ops;
-/* Root address of the linked list in the configuration core. */
-static struct virtio_net_config_ll     *ll_root;

 /* Features supported by this application. RX merge buffers are enabled by 
default. */
 #define VHOST_SUPPORTED_FEATURES (1ULL << VIRTIO_NET_F_MRG_RXBUF)
@@ -93,21 +91,23 @@ qva_to_vva(struct virtio_net *dev, uint64_t qemu_va)
        return vhost_va;
 }

+/**
+ * Include cuse depend functions and definitions.
+ */
+#include "virtio-net-cdev.c"
+
 /*
  * Retrieves an entry from the devices configuration linked list.
  */
 static struct virtio_net_config_ll *
 get_config_ll_entry(struct vhost_device_ctx ctx)
 {
-       struct virtio_net_config_ll *ll_dev = ll_root;
-
-       /* Loop through linked list until the device_fh is found. */
-       while (ll_dev != NULL) {
-               if (ll_dev->dev.device_fh == ctx.fh)
-                       return ll_dev;
-               ll_dev = ll_dev->next;
+       switch (ctx.type) {
+       case VHOST_DRV_CUSE:
+               return cdev_get_config_ll_entry(ctx);
+       default:
+               break;
        }
-
        return NULL;
 }

@@ -117,15 +117,12 @@ get_config_ll_entry(struct vhost_device_ctx ctx)
 static struct virtio_net *
 get_device(struct vhost_device_ctx ctx)
 {
-       struct virtio_net_config_ll *ll_dev;
-
-       ll_dev = get_config_ll_entry(ctx);
-
-       /* If a matching entry is found in the linked list, return the device 
in that entry. */
-       if (ll_dev)
-               return &ll_dev->dev;
-
-       RTE_LOG(ERR, VHOST_CONFIG, "(%"PRIu64") Device not found in linked 
list.\n", ctx.fh);
+       switch (ctx.type) {
+       case VHOST_DRV_CUSE:
+               return cdev_get_device(ctx);
+       default:
+               break;
+       }
        return NULL;
 }

@@ -133,31 +130,15 @@ get_device(struct vhost_device_ctx ctx)
  * Add entry containing a device to the device configuration linked list.
  */
 static void
-add_config_ll_entry(struct virtio_net_config_ll *new_ll_dev)
+add_config_ll_entry(vhost_driver_type_t type,
+               struct virtio_net_config_ll *new_ll_dev)
 {
-       struct virtio_net_config_ll *ll_dev = ll_root;
-
-       /* If ll_dev == NULL then this is the first device so go to else */
-       if (ll_dev) {
-               /* If the 1st device_fh != 0 then we insert our device here. */
-               if (ll_dev->dev.device_fh != 0) {
-                       new_ll_dev->dev.device_fh = 0;
-                       new_ll_dev->next = ll_dev;
-                       ll_root = new_ll_dev;
-               } else {
-                       /* Increment through the ll until we find un unused 
device_fh. Insert the device at that entry*/
-                       while ((ll_dev->next != NULL) && (ll_dev->dev.device_fh 
== (ll_dev->next->dev.device_fh - 1)))
-                               ll_dev = ll_dev->next;
-
-                       new_ll_dev->dev.device_fh = ll_dev->dev.device_fh + 1;
-                       new_ll_dev->next = ll_dev->next;
-                       ll_dev->next = new_ll_dev;
-               }
-       } else {
-               ll_root = new_ll_dev;
-               ll_root->dev.device_fh = 0;
+       switch (type) {
+       case VHOST_DRV_CUSE:
+               return cdev_add_config_ll_entry(new_ll_dev);
+       default:
+               break;
        }
-
 }

 /*
@@ -199,29 +180,32 @@ free_device(struct virtio_net_config_ll *ll_dev)
  * Remove an entry from the device configuration linked list.
  */
 static struct virtio_net_config_ll *
-rm_config_ll_entry(struct virtio_net_config_ll *ll_dev,
-       struct virtio_net_config_ll *ll_dev_last)
+rm_config_ll_entry(vhost_driver_type_t type,
+               struct virtio_net_config_ll *ll_dev,
+               struct virtio_net_config_ll *ll_dev_last)
 {
-       /* First remove the device and then clean it up. */
-       if (ll_dev == ll_root) {
-               ll_root = ll_dev->next;
-               cleanup_device(&ll_dev->dev);
-               free_device(ll_dev);
-               return ll_root;
-       } else {
-               if (likely(ll_dev_last != NULL)) {
-                       ll_dev_last->next = ll_dev->next;
-                       cleanup_device(&ll_dev->dev);
-                       free_device(ll_dev);
-                       return ll_dev_last->next;
-               } else {
-                       cleanup_device(&ll_dev->dev);
-                       free_device(ll_dev);
-                       RTE_LOG(ERR, VHOST_CONFIG,
-                               "Remove entry from config_ll failed\n");
-                       return NULL;
-               }
+       switch (type) {
+       case VHOST_DRV_CUSE:
+               return cdev_rm_config_ll_entry(ll_dev, ll_dev_last);
+       default:
+               break;
+       }
+       return NULL;
+}
+
+/**
+ * Get a root entry of linked list.
+ */
+static struct virtio_net_config_ll *
+get_config_ll_root(struct vhost_device_ctx ctx)
+{
+       switch (ctx.type) {
+       case VHOST_DRV_CUSE:
+               return cdev_get_config_ll_root(ctx);
+       default:
+               break;
        }
+       return NULL;
 }

 /*
@@ -294,7 +278,7 @@ new_device(struct vhost_device_ctx ctx)
        new_ll_dev->next = NULL;

        /* Add entry to device configuration linked list. */
-       add_config_ll_entry(new_ll_dev);
+       add_config_ll_entry(ctx.type, new_ll_dev);

        return new_ll_dev->dev.device_fh;
 }
@@ -307,7 +291,7 @@ static void
 destroy_device(struct vhost_device_ctx ctx)
 {
        struct virtio_net_config_ll *ll_dev_cur_ctx, *ll_dev_last = NULL;
-       struct virtio_net_config_ll *ll_dev_cur = ll_root;
+       struct virtio_net_config_ll *ll_dev_cur = get_config_ll_root(ctx);

        /* Find the linked list entry for the device to be removed. */
        ll_dev_cur_ctx = get_config_ll_entry(ctx);
@@ -320,7 +304,7 @@ destroy_device(struct vhost_device_ctx ctx)
                         */
                        if ((ll_dev_cur->dev.flags & VIRTIO_DEV_RUNNING))
                                notify_ops->destroy_device(&(ll_dev_cur->dev));
-                       ll_dev_cur = rm_config_ll_entry(ll_dev_cur, 
ll_dev_last);
+                       ll_dev_cur = rm_config_ll_entry(ctx.type, ll_dev_cur, 
ll_dev_last);
                } else {
                        ll_dev_last = ll_dev_cur;
                        ll_dev_cur = ll_dev_cur->next;
@@ -528,11 +512,6 @@ set_backend(struct vhost_device_ctx ctx, struct 
vhost_vring_file *file)
 }

 /*
- * Include cuse depend functions and definitions.
- */
-#include "virtio-net-cdev.c"
-
-/*
  * Called by main to setup callbacks when registering device.
  */
 struct vhost_net_device_ops const *
-- 
1.9.1

Reply via email to