Signed-off-by: Huawei Xie <huawei.xie at intel.com>
---
 lib/librte_vhost/vhost_user/vhost-net-user.c | 57 +++++++++++++++++++---------
 lib/librte_vhost/vhost_user/vhost-net-user.h |  1 -
 2 files changed, 40 insertions(+), 18 deletions(-)

diff --git a/lib/librte_vhost/vhost_user/vhost-net-user.c 
b/lib/librte_vhost/vhost_user/vhost-net-user.c
index 71e5bbd..3a45a5e 100644
--- a/lib/librte_vhost/vhost_user/vhost-net-user.c
+++ b/lib/librte_vhost/vhost_user/vhost-net-user.c
@@ -54,7 +54,18 @@ static void vserver_new_vq_conn(int fd, void *data);
 static void vserver_message_handler(int fd, void *dat);
 struct vhost_net_device_ops const *ops;

-static struct vhost_server *g_vhost_server;
+struct connfd_ctx {
+       struct vhost_server *vserver;
+       uint32_t fh;
+};
+
+#define MAX_VHOST_SERVER 1024
+static struct {
+       struct vhost_server *server[MAX_VHOST_SERVER];
+       struct fdset fdset;     /**< The fd list this vhost server manages. */
+} g_vhost_server;
+
+static int vserver_idx;

 static const char *vhost_message_str[VHOST_USER_MAX] = {
        [VHOST_USER_NONE] = "VHOST_USER_NONE",
@@ -251,6 +262,7 @@ vserver_new_vq_conn(int fd, void *dat)
 {
        struct vhost_server *vserver = (struct vhost_server *)dat;
        int conn_fd;
+       struct connfd_ctx *ctx;
        int fh;
        struct vhost_device_ctx vdev_ctx = { 0 };

@@ -260,15 +272,24 @@ vserver_new_vq_conn(int fd, void *dat)
        if (conn_fd < 0)
                return;

+       ctx = calloc(1, sizeof(*ctx));
+       if (ctx == NULL) {
+               close(conn_fd);
+               return;
+       }
+
        fh = ops->new_device(vdev_ctx);
        if (fh == -1) {
+               free(ctx);
                close(conn_fd);
                return;
        }
        RTE_LOG(INFO, VHOST_CONFIG, "new device, handle is %d\n", fh);

-       fdset_add(&vserver->fdset,
-               conn_fd, vserver_message_handler, NULL, (void *)fh);
+       ctx->vserver = vserver;
+       ctx->fh = fh;
+       fdset_add(&g_vhost_server.fdset,
+               conn_fd, vserver_message_handler, NULL, ctx);
 }

 /* callback when there is message on the connfd */
@@ -276,19 +297,20 @@ static void
 vserver_message_handler(int connfd, void *dat)
 {
        struct vhost_device_ctx ctx;
-       uint32_t fh = (uint32_t)dat;
+       struct connfd_ctx *cfd_ctx = (struct connfd_ctx *)dat;
        struct VhostUserMsg msg;
        uint64_t features;
        int ret;

-       ctx.fh = fh;
+       ctx.fh = cfd_ctx->fh;
        ret = read_vhost_message(connfd, &msg);
        if (ret < 0) {
                RTE_LOG(ERR, VHOST_CONFIG,
                        "vhost read message failed\n");

                close(connfd);
-               fdset_del(&g_vhost_server->fdset, connfd);
+               fdset_del(&g_vhost_server.fdset, connfd);
+               free(cfd_ctx);
                user_destroy_device(ctx);
                ops->destroy_device(ctx);

@@ -298,7 +320,8 @@ vserver_message_handler(int connfd, void *dat)
                        "vhost peer closed\n");

                close(connfd);
-               fdset_del(&g_vhost_server->fdset, connfd);
+               fdset_del(&g_vhost_server.fdset, connfd);
+               free(cfd_ctx);
                user_destroy_device(ctx);
                ops->destroy_device(ctx);

@@ -309,7 +332,8 @@ vserver_message_handler(int connfd, void *dat)
                        "vhost read incorrect message\n");

                close(connfd);
-               fdset_del(&g_vhost_server->fdset, connfd);
+               fdset_del(&g_vhost_server.fdset, connfd);
+               free(cfd_ctx);
                user_destroy_device(ctx);
                ops->destroy_device(ctx);

@@ -390,18 +414,19 @@ vserver_message_handler(int connfd, void *dat)
 int
 rte_vhost_driver_register(const char *path)
 {
-
        struct vhost_server *vserver;

-       if (g_vhost_server != NULL)
+       if (vserver_idx == 0) {
+               fdset_init(&g_vhost_server.fdset);
+               ops = get_virtio_net_callbacks();
+       }
+       if (vserver_idx == MAX_VHOST_SERVER)
                return -1;

        vserver = calloc(sizeof(struct vhost_server), 1);
        if (vserver == NULL)
                return -1;

-       fdset_init(&vserver->fdset);
-
        unlink(path);

        vserver->listenfd = uds_socket(path);
@@ -411,13 +436,11 @@ rte_vhost_driver_register(const char *path)
        }
        vserver->path = path;

-       fdset_add(&vserver->fdset, vserver->listenfd,
+       fdset_add(&g_vhost_server.fdset, vserver->listenfd,
                vserver_new_vq_conn, NULL,
                vserver);

-       ops = get_virtio_net_callbacks();
-
-       g_vhost_server = vserver;
+       g_vhost_server.server[vserver_idx++] = vserver;

        return 0;
 }
@@ -426,7 +449,7 @@ rte_vhost_driver_register(const char *path)
 int
 rte_vhost_driver_session_start(void)
 {
-       fdset_event_dispatch(&g_vhost_server->fdset);
+       fdset_event_dispatch(&g_vhost_server.fdset);
        return 0;
 }

diff --git a/lib/librte_vhost/vhost_user/vhost-net-user.h 
b/lib/librte_vhost/vhost_user/vhost-net-user.h
index 91e8fc3..e2a91a9 100644
--- a/lib/librte_vhost/vhost_user/vhost-net-user.h
+++ b/lib/librte_vhost/vhost_user/vhost-net-user.h
@@ -43,7 +43,6 @@
 struct vhost_server {
        const char *path; /**< The path the uds is bind to. */
        int listenfd;     /**< The listener sockfd. */
-       struct fdset fdset; /**< The fd list this vhost server manages. */
 };

 /* refer to hw/virtio/vhost-user.c */
-- 
1.8.1.4

Reply via email to