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