26ba25
From 080469ac5a5f9b0ab1d3001001b9939425023f93 Mon Sep 17 00:00:00 2001
26ba25
From: "plai@redhat.com" <plai@redhat.com>
26ba25
Date: Thu, 21 Jun 2018 18:54:43 +0200
26ba25
Subject: [PATCH 164/268] vhost-user: introduce shared vhost-user state
26ba25
26ba25
RH-Author: plai@redhat.com
26ba25
Message-id: <1529607285-9942-9-git-send-email-plai@redhat.com>
26ba25
Patchwork-id: 80942
26ba25
O-Subject: [RHEL7.6 PATCH BZ 1526645 08/10] vhost-user: introduce shared vhost-user state
26ba25
Bugzilla: 1526645
26ba25
RH-Acked-by: Michael S. Tsirkin <mst@redhat.com>
26ba25
RH-Acked-by: Maxime Coquelin <maxime.coquelin@redhat.com>
26ba25
RH-Acked-by: Laurent Vivier <lvivier@redhat.com>
26ba25
26ba25
From: Tiwei Bie <tiwei.bie@intel.com>
26ba25
26ba25
When multi queue is enabled e.g. for a virtio-net device,
26ba25
each queue pair will have a vhost_dev, and the only thing
26ba25
shared between vhost devs currently is the chardev. This
26ba25
patch introduces a vhost-user state structure which will
26ba25
be shared by all vhost devs of the same virtio device.
26ba25
26ba25
Signed-off-by: Tiwei Bie <tiwei.bie@intel.com>
26ba25
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
26ba25
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
26ba25
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
26ba25
(cherry picked from commit 4d0cf552d3a9585f380e8abdc313e4d416a56aa0)
26ba25
Signed-off-by: Paul Lai <plai@redhat.com>
26ba25
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
26ba25
---
26ba25
 backends/cryptodev-vhost-user.c     | 20 ++++++++++++++++-
26ba25
 hw/block/vhost-user-blk.c           | 22 ++++++++++++++++++-
26ba25
 hw/scsi/vhost-user-scsi.c           | 20 ++++++++++++++++-
26ba25
 hw/virtio/Makefile.objs             |  2 +-
26ba25
 hw/virtio/vhost-stub.c              | 10 +++++++++
26ba25
 hw/virtio/vhost-user.c              | 31 ++++++++++++++++++--------
26ba25
 include/hw/virtio/vhost-user-blk.h  |  2 ++
26ba25
 include/hw/virtio/vhost-user-scsi.h |  2 ++
26ba25
 include/hw/virtio/vhost-user.h      | 20 +++++++++++++++++
26ba25
 net/vhost-user.c                    | 44 ++++++++++++++++++++++++++++++-------
26ba25
 10 files changed, 152 insertions(+), 21 deletions(-)
26ba25
 create mode 100644 include/hw/virtio/vhost-user.h
26ba25
26ba25
diff --git a/backends/cryptodev-vhost-user.c b/backends/cryptodev-vhost-user.c
26ba25
index 862d4f2..d52dacc 100644
26ba25
--- a/backends/cryptodev-vhost-user.c
26ba25
+++ b/backends/cryptodev-vhost-user.c
26ba25
@@ -26,6 +26,7 @@
26ba25
 #include "qapi/error.h"
26ba25
 #include "qapi/qmp/qerror.h"
26ba25
 #include "qemu/error-report.h"
26ba25
+#include "hw/virtio/vhost-user.h"
26ba25
 #include "standard-headers/linux/virtio_crypto.h"
26ba25
 #include "sysemu/cryptodev-vhost.h"
26ba25
 #include "chardev/char-fe.h"
26ba25
@@ -46,6 +47,7 @@
26ba25
 typedef struct CryptoDevBackendVhostUser {
26ba25
     CryptoDevBackend parent_obj;
26ba25
 
26ba25
+    VhostUserState *vhost_user;
26ba25
     CharBackend chr;
26ba25
     char *chr_name;
26ba25
     bool opened;
26ba25
@@ -102,7 +104,7 @@ cryptodev_vhost_user_start(int queues,
26ba25
             continue;
26ba25
         }
26ba25
 
26ba25
-        options.opaque = &s->chr;
26ba25
+        options.opaque = s->vhost_user;
26ba25
         options.backend_type = VHOST_BACKEND_TYPE_USER;
26ba25
         options.cc = b->conf.peers.ccs[i];
26ba25
         s->vhost_crypto[i] = cryptodev_vhost_init(&options);
26ba25
@@ -185,6 +187,7 @@ static void cryptodev_vhost_user_init(
26ba25
     size_t i;
26ba25
     Error *local_err = NULL;
26ba25
     Chardev *chr;
26ba25
+    VhostUserState *user;
26ba25
     CryptoDevBackendClient *cc;
26ba25
     CryptoDevBackendVhostUser *s =
26ba25
                       CRYPTODEV_BACKEND_VHOST_USER(backend);
26ba25
@@ -215,6 +218,15 @@ static void cryptodev_vhost_user_init(
26ba25
         }
26ba25
     }
26ba25
 
26ba25
+    user = vhost_user_init();
26ba25
+    if (!user) {
26ba25
+        error_setg(errp, "Failed to init vhost_user");
26ba25
+        return;
26ba25
+    }
26ba25
+
26ba25
+    user->chr = &s->chr;
26ba25
+    s->vhost_user = user;
26ba25
+
26ba25
     qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
26ba25
                      cryptodev_vhost_user_event, NULL, s, NULL, true);
26ba25
 
26ba25
@@ -299,6 +311,12 @@ static void cryptodev_vhost_user_cleanup(
26ba25
             backend->conf.peers.ccs[i] = NULL;
26ba25
         }
26ba25
     }
26ba25
+
26ba25
+    if (s->vhost_user) {
26ba25
+        vhost_user_cleanup(s->vhost_user);
26ba25
+        g_free(s->vhost_user);
26ba25
+        s->vhost_user = NULL;
26ba25
+    }
26ba25
 }
26ba25
 
26ba25
 static void cryptodev_vhost_user_set_chardev(Object *obj,
26ba25
diff --git a/hw/block/vhost-user-blk.c b/hw/block/vhost-user-blk.c
26ba25
index 262baca..4021d71 100644
26ba25
--- a/hw/block/vhost-user-blk.c
26ba25
+++ b/hw/block/vhost-user-blk.c
26ba25
@@ -229,6 +229,7 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
26ba25
 {
26ba25
     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
26ba25
     VHostUserBlk *s = VHOST_USER_BLK(vdev);
26ba25
+    VhostUserState *user;
26ba25
     int i, ret;
26ba25
 
26ba25
     if (!s->chardev.chr) {
26ba25
@@ -246,6 +247,15 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
26ba25
         return;
26ba25
     }
26ba25
 
26ba25
+    user = vhost_user_init();
26ba25
+    if (!user) {
26ba25
+        error_setg(errp, "vhost-user-blk: failed to init vhost_user");
26ba25
+        return;
26ba25
+    }
26ba25
+
26ba25
+    user->chr = &s->chardev;
26ba25
+    s->vhost_user = user;
26ba25
+
26ba25
     virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
26ba25
                 sizeof(struct virtio_blk_config));
26ba25
 
26ba25
@@ -261,7 +271,7 @@ static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
26ba25
 
26ba25
     vhost_dev_set_config_notifier(&s->dev, &blk_ops);
26ba25
 
26ba25
-    ret = vhost_dev_init(&s->dev, &s->chardev, VHOST_BACKEND_TYPE_USER, 0);
26ba25
+    ret = vhost_dev_init(&s->dev, s->vhost_user, VHOST_BACKEND_TYPE_USER, 0);
26ba25
     if (ret < 0) {
26ba25
         error_setg(errp, "vhost-user-blk: vhost initialization failed: %s",
26ba25
                    strerror(-ret));
26ba25
@@ -286,6 +296,10 @@ vhost_err:
26ba25
 virtio_err:
26ba25
     g_free(s->dev.vqs);
26ba25
     virtio_cleanup(vdev);
26ba25
+
26ba25
+    vhost_user_cleanup(user);
26ba25
+    g_free(user);
26ba25
+    s->vhost_user = NULL;
26ba25
 }
26ba25
 
26ba25
 static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
26ba25
@@ -297,6 +311,12 @@ static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
26ba25
     vhost_dev_cleanup(&s->dev);
26ba25
     g_free(s->dev.vqs);
26ba25
     virtio_cleanup(vdev);
26ba25
+
26ba25
+    if (s->vhost_user) {
26ba25
+        vhost_user_cleanup(s->vhost_user);
26ba25
+        g_free(s->vhost_user);
26ba25
+        s->vhost_user = NULL;
26ba25
+    }
26ba25
 }
26ba25
 
26ba25
 static void vhost_user_blk_instance_init(Object *obj)
26ba25
diff --git a/hw/scsi/vhost-user-scsi.c b/hw/scsi/vhost-user-scsi.c
26ba25
index 9389ed4..9355cfd 100644
26ba25
--- a/hw/scsi/vhost-user-scsi.c
26ba25
+++ b/hw/scsi/vhost-user-scsi.c
26ba25
@@ -69,6 +69,7 @@ static void vhost_user_scsi_realize(DeviceState *dev, Error **errp)
26ba25
     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
26ba25
     VHostUserSCSI *s = VHOST_USER_SCSI(dev);
26ba25
     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
26ba25
+    VhostUserState *user;
26ba25
     Error *err = NULL;
26ba25
     int ret;
26ba25
 
26ba25
@@ -85,19 +86,30 @@ static void vhost_user_scsi_realize(DeviceState *dev, Error **errp)
26ba25
         return;
26ba25
     }
26ba25
 
26ba25
+    user = vhost_user_init();
26ba25
+    if (!user) {
26ba25
+        error_setg(errp, "vhost-user-scsi: failed to init vhost_user");
26ba25
+        return;
26ba25
+    }
26ba25
+    user->chr = &vs->conf.chardev;
26ba25
+
26ba25
     vsc->dev.nvqs = 2 + vs->conf.num_queues;
26ba25
     vsc->dev.vqs = g_new(struct vhost_virtqueue, vsc->dev.nvqs);
26ba25
     vsc->dev.vq_index = 0;
26ba25
     vsc->dev.backend_features = 0;
26ba25
 
26ba25
-    ret = vhost_dev_init(&vsc->dev, (void *)&vs->conf.chardev,
26ba25
+    ret = vhost_dev_init(&vsc->dev, user,
26ba25
                          VHOST_BACKEND_TYPE_USER, 0);
26ba25
     if (ret < 0) {
26ba25
         error_setg(errp, "vhost-user-scsi: vhost initialization failed: %s",
26ba25
                    strerror(-ret));
26ba25
+        vhost_user_cleanup(user);
26ba25
+        g_free(user);
26ba25
         return;
26ba25
     }
26ba25
 
26ba25
+    s->vhost_user = user;
26ba25
+
26ba25
     /* Channel and lun both are 0 for bootable vhost-user-scsi disk */
26ba25
     vsc->channel = 0;
26ba25
     vsc->lun = 0;
26ba25
@@ -117,6 +129,12 @@ static void vhost_user_scsi_unrealize(DeviceState *dev, Error **errp)
26ba25
     g_free(vsc->dev.vqs);
26ba25
 
26ba25
     virtio_scsi_common_unrealize(dev, errp);
26ba25
+
26ba25
+    if (s->vhost_user) {
26ba25
+        vhost_user_cleanup(s->vhost_user);
26ba25
+        g_free(s->vhost_user);
26ba25
+        s->vhost_user = NULL;
26ba25
+    }
26ba25
 }
26ba25
 
26ba25
 static uint64_t vhost_user_scsi_get_features(VirtIODevice *vdev,
26ba25
diff --git a/hw/virtio/Makefile.objs b/hw/virtio/Makefile.objs
26ba25
index a5a0936..00c6696 100644
26ba25
--- a/hw/virtio/Makefile.objs
26ba25
+++ b/hw/virtio/Makefile.objs
26ba25
@@ -12,5 +12,5 @@ obj-$(CONFIG_VHOST_VSOCK) += vhost-vsock.o
26ba25
 #obj-$(CONFIG_VIRTIO_PCI) += virtio-crypto-pci.o
26ba25
 endif
26ba25
 
26ba25
-common-obj-$(call lnot,$(CONFIG_LINUX)) += vhost-stub.o
26ba25
+common-obj-$(call lnot,$(call land,$(CONFIG_VIRTIO),$(CONFIG_LINUX))) += vhost-stub.o
26ba25
 common-obj-$(CONFIG_ALL) += vhost-stub.o
26ba25
diff --git a/hw/virtio/vhost-stub.c b/hw/virtio/vhost-stub.c
26ba25
index 2d76cde..049089b 100644
26ba25
--- a/hw/virtio/vhost-stub.c
26ba25
+++ b/hw/virtio/vhost-stub.c
26ba25
@@ -1,7 +1,17 @@
26ba25
 #include "qemu/osdep.h"
26ba25
 #include "hw/virtio/vhost.h"
26ba25
+#include "hw/virtio/vhost-user.h"
26ba25
 
26ba25
 bool vhost_has_free_slot(void)
26ba25
 {
26ba25
     return true;
26ba25
 }
26ba25
+
26ba25
+VhostUserState *vhost_user_init(void)
26ba25
+{
26ba25
+    return NULL;
26ba25
+}
26ba25
+
26ba25
+void vhost_user_cleanup(VhostUserState *user)
26ba25
+{
26ba25
+}
26ba25
diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c
26ba25
index e8027ad..a715c5c 100644
26ba25
--- a/hw/virtio/vhost-user.c
26ba25
+++ b/hw/virtio/vhost-user.c
26ba25
@@ -11,6 +11,7 @@
26ba25
 #include "qemu/osdep.h"
26ba25
 #include "qapi/error.h"
26ba25
 #include "hw/virtio/vhost.h"
26ba25
+#include "hw/virtio/vhost-user.h"
26ba25
 #include "hw/virtio/vhost-backend.h"
26ba25
 #include "hw/virtio/virtio-net.h"
26ba25
 #include "chardev/char-fe.h"
26ba25
@@ -175,7 +176,8 @@ static VhostUserMsg m __attribute__ ((unused));
26ba25
 
26ba25
 struct vhost_user {
26ba25
     struct vhost_dev *dev;
26ba25
-    CharBackend *chr;
26ba25
+    /* Shared between vhost devs of the same virtio device */
26ba25
+    VhostUserState *user;
26ba25
     int slave_fd;
26ba25
     NotifierWithReturn postcopy_notifier;
26ba25
     struct PostCopyFD  postcopy_fd;
26ba25
@@ -201,7 +203,7 @@ static bool ioeventfd_enabled(void)
26ba25
 static int vhost_user_read(struct vhost_dev *dev, VhostUserMsg *msg)
26ba25
 {
26ba25
     struct vhost_user *u = dev->opaque;
26ba25
-    CharBackend *chr = u->chr;
26ba25
+    CharBackend *chr = u->user->chr;
26ba25
     uint8_t *p = (uint8_t *) msg;
26ba25
     int r, size = VHOST_USER_HDR_SIZE;
26ba25
 
26ba25
@@ -287,7 +289,7 @@ static int vhost_user_write(struct vhost_dev *dev, VhostUserMsg *msg,
26ba25
                             int *fds, int fd_num)
26ba25
 {
26ba25
     struct vhost_user *u = dev->opaque;
26ba25
-    CharBackend *chr = u->chr;
26ba25
+    CharBackend *chr = u->user->chr;
26ba25
     int ret, size = VHOST_USER_HDR_SIZE + msg->hdr.size;
26ba25
 
26ba25
     /*
26ba25
@@ -1090,7 +1092,7 @@ static int vhost_user_postcopy_waker(struct PostCopyFD *pcfd, RAMBlock *rb,
26ba25
 static int vhost_user_postcopy_advise(struct vhost_dev *dev, Error **errp)
26ba25
 {
26ba25
     struct vhost_user *u = dev->opaque;
26ba25
-    CharBackend *chr = u->chr;
26ba25
+    CharBackend *chr = u->user->chr;
26ba25
     int ufd;
26ba25
     VhostUserMsg msg = {
26ba25
         .hdr.request = VHOST_USER_POSTCOPY_ADVISE,
26ba25
@@ -1228,7 +1230,7 @@ static int vhost_user_postcopy_notifier(NotifierWithReturn *notifier,
26ba25
     return 0;
26ba25
 }
26ba25
 
26ba25
-static int vhost_user_init(struct vhost_dev *dev, void *opaque)
26ba25
+static int vhost_user_backend_init(struct vhost_dev *dev, void *opaque)
26ba25
 {
26ba25
     uint64_t features, protocol_features;
26ba25
     struct vhost_user *u;
26ba25
@@ -1237,7 +1239,7 @@ static int vhost_user_init(struct vhost_dev *dev, void *opaque)
26ba25
     assert(dev->vhost_ops->backend_type == VHOST_BACKEND_TYPE_USER);
26ba25
 
26ba25
     u = g_new0(struct vhost_user, 1);
26ba25
-    u->chr = opaque;
26ba25
+    u->user = opaque;
26ba25
     u->slave_fd = -1;
26ba25
     u->dev = dev;
26ba25
     dev->opaque = u;
26ba25
@@ -1313,7 +1315,7 @@ static int vhost_user_init(struct vhost_dev *dev, void *opaque)
26ba25
     return 0;
26ba25
 }
26ba25
 
26ba25
-static int vhost_user_cleanup(struct vhost_dev *dev)
26ba25
+static int vhost_user_backend_cleanup(struct vhost_dev *dev)
26ba25
 {
26ba25
     struct vhost_user *u;
26ba25
 
26ba25
@@ -1637,10 +1639,21 @@ static bool vhost_user_mem_section_filter(struct vhost_dev *dev,
26ba25
     return result;
26ba25
 }
26ba25
 
26ba25
+VhostUserState *vhost_user_init(void)
26ba25
+{
26ba25
+    VhostUserState *user = g_new0(struct VhostUserState, 1);
26ba25
+
26ba25
+    return user;
26ba25
+}
26ba25
+
26ba25
+void vhost_user_cleanup(VhostUserState *user)
26ba25
+{
26ba25
+}
26ba25
+
26ba25
 const VhostOps user_ops = {
26ba25
         .backend_type = VHOST_BACKEND_TYPE_USER,
26ba25
-        .vhost_backend_init = vhost_user_init,
26ba25
-        .vhost_backend_cleanup = vhost_user_cleanup,
26ba25
+        .vhost_backend_init = vhost_user_backend_init,
26ba25
+        .vhost_backend_cleanup = vhost_user_backend_cleanup,
26ba25
         .vhost_backend_memslots_limit = vhost_user_memslots_limit,
26ba25
         .vhost_set_log_base = vhost_user_set_log_base,
26ba25
         .vhost_set_mem_table = vhost_user_set_mem_table,
26ba25
diff --git a/include/hw/virtio/vhost-user-blk.h b/include/hw/virtio/vhost-user-blk.h
26ba25
index 5804cc9..f1258ae 100644
26ba25
--- a/include/hw/virtio/vhost-user-blk.h
26ba25
+++ b/include/hw/virtio/vhost-user-blk.h
26ba25
@@ -21,6 +21,7 @@
26ba25
 #include "hw/block/block.h"
26ba25
 #include "chardev/char-fe.h"
26ba25
 #include "hw/virtio/vhost.h"
26ba25
+#include "hw/virtio/vhost-user.h"
26ba25
 
26ba25
 #define TYPE_VHOST_USER_BLK "vhost-user-blk"
26ba25
 #define VHOST_USER_BLK(obj) \
26ba25
@@ -36,6 +37,7 @@ typedef struct VHostUserBlk {
26ba25
     uint32_t config_wce;
26ba25
     uint32_t config_ro;
26ba25
     struct vhost_dev dev;
26ba25
+    VhostUserState *vhost_user;
26ba25
 } VHostUserBlk;
26ba25
 
26ba25
 #endif
26ba25
diff --git a/include/hw/virtio/vhost-user-scsi.h b/include/hw/virtio/vhost-user-scsi.h
26ba25
index 01861f7..3ec34ae 100644
26ba25
--- a/include/hw/virtio/vhost-user-scsi.h
26ba25
+++ b/include/hw/virtio/vhost-user-scsi.h
26ba25
@@ -21,6 +21,7 @@
26ba25
 #include "hw/qdev.h"
26ba25
 #include "hw/virtio/virtio-scsi.h"
26ba25
 #include "hw/virtio/vhost.h"
26ba25
+#include "hw/virtio/vhost-user.h"
26ba25
 #include "hw/virtio/vhost-scsi-common.h"
26ba25
 
26ba25
 #define TYPE_VHOST_USER_SCSI "vhost-user-scsi"
26ba25
@@ -30,6 +31,7 @@
26ba25
 typedef struct VHostUserSCSI {
26ba25
     VHostSCSICommon parent_obj;
26ba25
     uint64_t host_features;
26ba25
+    VhostUserState *vhost_user;
26ba25
 } VHostUserSCSI;
26ba25
 
26ba25
 #endif /* VHOST_USER_SCSI_H */
26ba25
diff --git a/include/hw/virtio/vhost-user.h b/include/hw/virtio/vhost-user.h
26ba25
new file mode 100644
26ba25
index 0000000..eb8bc0d
26ba25
--- /dev/null
26ba25
+++ b/include/hw/virtio/vhost-user.h
26ba25
@@ -0,0 +1,20 @@
26ba25
+/*
26ba25
+ * Copyright (c) 2017-2018 Intel Corporation
26ba25
+ *
26ba25
+ * This work is licensed under the terms of the GNU GPL, version 2.
26ba25
+ * See the COPYING file in the top-level directory.
26ba25
+ */
26ba25
+
26ba25
+#ifndef HW_VIRTIO_VHOST_USER_H
26ba25
+#define HW_VIRTIO_VHOST_USER_H
26ba25
+
26ba25
+#include "chardev/char-fe.h"
26ba25
+
26ba25
+typedef struct VhostUserState {
26ba25
+    CharBackend *chr;
26ba25
+} VhostUserState;
26ba25
+
26ba25
+VhostUserState *vhost_user_init(void);
26ba25
+void vhost_user_cleanup(VhostUserState *user);
26ba25
+
26ba25
+#endif
26ba25
diff --git a/net/vhost-user.c b/net/vhost-user.c
26ba25
index fa28aad..608b837 100644
26ba25
--- a/net/vhost-user.c
26ba25
+++ b/net/vhost-user.c
26ba25
@@ -12,6 +12,7 @@
26ba25
 #include "clients.h"
26ba25
 #include "net/vhost_net.h"
26ba25
 #include "net/vhost-user.h"
26ba25
+#include "hw/virtio/vhost-user.h"
26ba25
 #include "chardev/char-fe.h"
26ba25
 #include "qapi/error.h"
26ba25
 #include "qapi/qapi-commands-net.h"
26ba25
@@ -23,6 +24,7 @@
26ba25
 typedef struct NetVhostUserState {
26ba25
     NetClientState nc;
26ba25
     CharBackend chr; /* only queue index 0 */
26ba25
+    VhostUserState *vhost_user;
26ba25
     VHostNetState *vhost_net;
26ba25
     guint watch;
26ba25
     uint64_t acked_features;
26ba25
@@ -64,7 +66,8 @@ static void vhost_user_stop(int queues, NetClientState *ncs[])
26ba25
     }
26ba25
 }
26ba25
 
26ba25
-static int vhost_user_start(int queues, NetClientState *ncs[], CharBackend *be)
26ba25
+static int vhost_user_start(int queues, NetClientState *ncs[],
26ba25
+                            VhostUserState *be)
26ba25
 {
26ba25
     VhostNetOptions options;
26ba25
     struct vhost_net *net = NULL;
26ba25
@@ -144,7 +147,7 @@ static ssize_t vhost_user_receive(NetClientState *nc, const uint8_t *buf,
26ba25
     return size;
26ba25
 }
26ba25
 
26ba25
-static void vhost_user_cleanup(NetClientState *nc)
26ba25
+static void net_vhost_user_cleanup(NetClientState *nc)
26ba25
 {
26ba25
     NetVhostUserState *s = DO_UPCAST(NetVhostUserState, nc, nc);
26ba25
 
26ba25
@@ -159,6 +162,11 @@ static void vhost_user_cleanup(NetClientState *nc)
26ba25
             s->watch = 0;
26ba25
         }
26ba25
         qemu_chr_fe_deinit(&s->chr, true);
26ba25
+        if (s->vhost_user) {
26ba25
+            vhost_user_cleanup(s->vhost_user);
26ba25
+            g_free(s->vhost_user);
26ba25
+            s->vhost_user = NULL;
26ba25
+        }
26ba25
     }
26ba25
 
26ba25
     qemu_purge_queued_packets(nc);
26ba25
@@ -182,7 +190,7 @@ static NetClientInfo net_vhost_user_info = {
26ba25
         .type = NET_CLIENT_DRIVER_VHOST_USER,
26ba25
         .size = sizeof(NetVhostUserState),
26ba25
         .receive = vhost_user_receive,
26ba25
-        .cleanup = vhost_user_cleanup,
26ba25
+        .cleanup = net_vhost_user_cleanup,
26ba25
         .has_vnet_hdr = vhost_user_has_vnet_hdr,
26ba25
         .has_ufo = vhost_user_has_ufo,
26ba25
 };
26ba25
@@ -244,7 +252,7 @@ static void net_vhost_user_event(void *opaque, int event)
26ba25
     trace_vhost_user_event(chr->label, event);
26ba25
     switch (event) {
26ba25
     case CHR_EVENT_OPENED:
26ba25
-        if (vhost_user_start(queues, ncs, &s->chr) < 0) {
26ba25
+        if (vhost_user_start(queues, ncs, s->vhost_user) < 0) {
26ba25
             qemu_chr_fe_disconnect(&s->chr);
26ba25
             return;
26ba25
         }
26ba25
@@ -283,12 +291,19 @@ static int net_vhost_user_init(NetClientState *peer, const char *device,
26ba25
 {
26ba25
     Error *err = NULL;
26ba25
     NetClientState *nc, *nc0 = NULL;
26ba25
-    NetVhostUserState *s;
26ba25
+    VhostUserState *user = NULL;
26ba25
+    NetVhostUserState *s = NULL;
26ba25
     int i;
26ba25
 
26ba25
     assert(name);
26ba25
     assert(queues > 0);
26ba25
 
26ba25
+    user = vhost_user_init();
26ba25
+    if (!user) {
26ba25
+        error_report("failed to init vhost_user");
26ba25
+        goto err;
26ba25
+    }
26ba25
+
26ba25
     for (i = 0; i < queues; i++) {
26ba25
         nc = qemu_new_net_client(&net_vhost_user_info, peer, device, name);
26ba25
         snprintf(nc->info_str, sizeof(nc->info_str), "vhost-user%d to %s",
26ba25
@@ -299,17 +314,19 @@ static int net_vhost_user_init(NetClientState *peer, const char *device,
26ba25
             s = DO_UPCAST(NetVhostUserState, nc, nc);
26ba25
             if (!qemu_chr_fe_init(&s->chr, chr, &err)) {
26ba25
                 error_report_err(err);
26ba25
-                return -1;
26ba25
+                goto err;
26ba25
             }
26ba25
+            user->chr = &s->chr;
26ba25
         }
26ba25
-
26ba25
+        s = DO_UPCAST(NetVhostUserState, nc, nc);
26ba25
+        s->vhost_user = user;
26ba25
     }
26ba25
 
26ba25
     s = DO_UPCAST(NetVhostUserState, nc, nc0);
26ba25
     do {
26ba25
         if (qemu_chr_fe_wait_connected(&s->chr, &err) < 0) {
26ba25
             error_report_err(err);
26ba25
-            return -1;
26ba25
+            goto err;
26ba25
         }
26ba25
         qemu_chr_fe_set_handlers(&s->chr, NULL, NULL,
26ba25
                                  net_vhost_user_event, NULL, nc0->name, NULL,
26ba25
@@ -319,6 +336,17 @@ static int net_vhost_user_init(NetClientState *peer, const char *device,
26ba25
     assert(s->vhost_net);
26ba25
 
26ba25
     return 0;
26ba25
+
26ba25
+err:
26ba25
+    if (user) {
26ba25
+        vhost_user_cleanup(user);
26ba25
+        g_free(user);
26ba25
+        if (s) {
26ba25
+            s->vhost_user = NULL;
26ba25
+        }
26ba25
+    }
26ba25
+
26ba25
+    return -1;
26ba25
 }
26ba25
 
26ba25
 static Chardev *net_vhost_claim_chardev(
26ba25
-- 
26ba25
1.8.3.1
26ba25