Skip to contentSkip to conversation

virtio: add vhost-user-vsock device

PATCH Superseded Stefano Garzarella 2 participants 7 messages May 15, 2020 – May 22, 2020

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover virtio: add vhost-user-vsock device

This series add vhost-user-vsock base device and its PCI version (vhost-user-vsock-pci).
The vhost-user-vsock device can be used to implement the virtio-vsock device emulation in user-space.
An use case might be Kata, where they prefer to emulate devices in user-space.

The first patch creates a new vhost-vsock-common parent class with the common code usable for both vhost-vsock and vhost-user-vsock devices.

The other patches add the vhost-user-vsock base device and its PCI version.

I also implemented a vhost-user-vsock [1] (still WiP) application written in Rust using crates from Cloud Hypervisor.
The application implements the Firecracker hybrid vsock (vsock over unix domain socket) [2]

The vhost-user-vsock-pci device can be tested following these steps:

    # build vhost-user-vsock application
    git clone https://github.com/stefano-garzarella/cloud-hypervisor.git
    git checkout vhost-user-vsock
    cargo build
    # start vhost-user-vsock
    ./target/debug/vhost_user_vsock \
        --vsock-backend guest_cid=4,uds_path=/tmp/vm4.vsock,sock=/tmp/vhost4.socket
    # start QEMU
    qemu-system-x86_64 -m 1G -smp 2 -cpu host -M q35,accel=kvm \
        ... \
        -chardev socket,id=char0,reconnect=0,path=/tmp/vhost4.socket \
        -device vhost-user-vsock-pci,chardev=char0 -monitor stdio
    # Guest listening
    guest$ nc --vsock -l 1234

    host$ nc -U /tmp/vm4.vsock
    CONNECT 1234

    # Host listening
    host$ nc -l -U /tmp/vm4.vsock_1234

    guest$ nc --vsock 2 1234

[1] https://github.com/stefano-garzarella/cloud-hypervisor.git
(vhost-user-vsock branch)
[2] https://github.com/firecracker-microvm/firecracker/blob/master/docs/vsock.md

Stefano Garzarella (3):
vhost-vsock: add vhost-vsock-common abstraction
virtio: add vhost-user-vsock base device
virtio: add vhost-user-vsock-pci device

 configure                              |   3 +
 include/hw/virtio/vhost-user-vsock.h   |  36 ++++
 include/hw/virtio/vhost-vsock-common.h |  47 ++++
 include/hw/virtio/vhost-vsock.h        |  11 +-
 hw/virtio/vhost-user-vsock-pci.c       |  84 ++++++++
 hw/virtio/vhost-user-vsock.c           | 188 ++++++++++++++++
 hw/virtio/vhost-vsock-common.c         | 258 ++++++++++++++++++++++
 hw/virtio/vhost-vsock.c                | 283 ++++---------------------
 hw/virtio/Makefile.objs                |   4 +-
 9 files changed, 663 insertions(+), 251 deletions(-)
 create mode 100644 include/hw/virtio/vhost-user-vsock.h
 create mode 100644 include/hw/virtio/vhost-vsock-common.h
 create mode 100644 hw/virtio/vhost-user-vsock-pci.c
 create mode 100644 hw/virtio/vhost-user-vsock.c
 create mode 100644 hw/virtio/vhost-vsock-common.c
Signature · 1 line
2.25.4
#1 lore raw

PATCH 1/3 vhost-vsock: add vhost-vsock-common abstraction

This patch prepares the introduction of vhost-user-vsock, moving the common code usable for both vhost-vsock and vhost-user-vsock devices, in the new vhost-vsock-common parent class.

While moving the code, fixed checkpatch warnings about block comments.

---
 include/hw/virtio/vhost-vsock-common.h |  47 ++++
 include/hw/virtio/vhost-vsock.h        |  11 +-
 hw/virtio/vhost-vsock-common.c         | 258 ++++++++++++++++++++++
 hw/virtio/vhost-vsock.c                | 283 ++++---------------------
 hw/virtio/Makefile.objs                |   2 +-
 5 files changed, 350 insertions(+), 251 deletions(-)
 create mode 100644 include/hw/virtio/vhost-vsock-common.h
 create mode 100644 hw/virtio/vhost-vsock-common.c
include/hw/virtio/vhost-vsock-common.h new file +47 -0
new file mode 100644
index 0000000000..f8b4aaae00
@@ -0,0 +1,47 @@
+/*
+ * Parent class for vhost-vsock devices
+ *
+ * Copyright 2015-2020 Red Hat, Inc.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * (at your option) any later version.  See the COPYING file in the
+ * top-level directory.
+ */
+
+#ifndef _QEMU_VHOST_VSOCK_COMMON_H
+#define _QEMU_VHOST_VSOCK_COMMON_H
+
+#include "hw/virtio/virtio.h"
+#include "hw/virtio/vhost.h"
+
+#define TYPE_VHOST_VSOCK_COMMON "vhost-vsock-common"
+#define VHOST_VSOCK_COMMON(obj) \
+        OBJECT_CHECK(VHostVSockCommon, (obj), TYPE_VHOST_VSOCK_COMMON)
+
+enum {
+    VHOST_VSOCK_SAVEVM_VERSION = 0,
+
+    VHOST_VSOCK_QUEUE_SIZE = 128,
+};
+
+typedef struct {
+    VirtIODevice parent;
+
+    struct vhost_virtqueue vhost_vqs[2];
+    struct vhost_dev vhost_dev;
+
+    VirtQueue *event_vq;
+    VirtQueue *recv_vq;
+    VirtQueue *trans_vq;
+
+    QEMUTimer *post_load_timer;
+} VHostVSockCommon;
+
+int vhost_vsock_common_start(VirtIODevice *vdev);
+void vhost_vsock_common_stop(VirtIODevice *vdev);
+int vhost_vsock_common_pre_save(void *opaque);
+int vhost_vsock_common_post_load(void *opaque, int version_id);
+void vhost_vsock_common_realize(VirtIODevice *vdev, const char *name);
+void vhost_vsock_common_unrealize(VirtIODevice *vdev);
+
+#endif /* _QEMU_VHOST_VSOCK_COMMON_H */
include/hw/virtio/vhost-vsock.h +2 -9
index bc5a988ee5..8cbb7b90f9 100644
@@ -14,8 +14,7 @@
 #ifndef QEMU_VHOST_VSOCK_H
 #define QEMU_VHOST_VSOCK_H
 
-#include "hw/virtio/virtio.h"
-#include "hw/virtio/vhost.h"
+#include "hw/virtio/vhost-vsock-common.h"
 
 #define TYPE_VHOST_VSOCK "vhost-vsock-device"
 #define VHOST_VSOCK(obj) \
@@ -28,14 +27,8 @@ typedef struct {
 
 typedef struct {
     /*< private >*/
-    VirtIODevice parent;
+    VHostVSockCommon parent;
     VHostVSockConf conf;
-    struct vhost_virtqueue vhost_vqs[2];
-    struct vhost_dev vhost_dev;
-    VirtQueue *event_vq;
-    VirtQueue *recv_vq;
-    VirtQueue *trans_vq;
-    QEMUTimer *post_load_timer;
 
     /*< public >*/
 } VHostVSock;
hw/virtio/vhost-vsock-common.c new file +258 -0

258 lines not on this page · raw message

hw/virtio/vhost-vsock.c +42 -241

409 lines not on this page · raw message

hw/virtio/Makefile.objs +1 -1
index 4e4d39a0a4..b1eeb44eac 100644
@@ -17,7 +17,7 @@ obj-$(CONFIG_VIRTIO_PMEM) += virtio-pmem.o
 common-obj-$(call land,$(CONFIG_VIRTIO_PMEM),$(CONFIG_VIRTIO_PCI)) += virtio-pmem-pci.o
 obj-$(call land,$(CONFIG_VHOST_USER_FS),$(CONFIG_VIRTIO_PCI)) += vhost-user-fs-pci.o
 obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o
-obj-$(CONFIG_VHOST_VSOCK) += vhost-vsock.o
+obj-$(CONFIG_VHOST_VSOCK) += vhost-vsock-common.o vhost-vsock.o
 
 ifeq ($(CONFIG_VIRTIO_PCI),y)
 obj-$(CONFIG_VHOST_VSOCK) += vhost-vsock-pci.o
Signature · 1 line
2.25.4
#2 lore raw

PATCH 2/3 virtio: add vhost-user-vsock base device

In reply to Stefano Garzarella

This patch introduces a vhost-user device for vsock, using the vhost-vsock-common parent class.

The vhost-user-vsock device can be used to implement the virtio-vsock device emulation in user-space.

---
 configure                            |   3 +
 include/hw/virtio/vhost-user-vsock.h |  36 +++++
 hw/virtio/vhost-user-vsock.c         | 188 +++++++++++++++++++++++++++
 hw/virtio/Makefile.objs              |   1 +
 4 files changed, 228 insertions(+)
 create mode 100644 include/hw/virtio/vhost-user-vsock.h
 create mode 100644 hw/virtio/vhost-user-vsock.c
configure +3 -0
index 26084fc53a..367d474e89 100755
@@ -7175,6 +7175,9 @@ if test "$vhost_crypto" = "yes" ; then
 fi
 if test "$vhost_vsock" = "yes" ; then
   echo "CONFIG_VHOST_VSOCK=y" >> $config_host_mak
+  if test "$vhost_user" = "yes" ; then
+    echo "CONFIG_VHOST_USER_VSOCK=y" >> $config_host_mak
+  fi
 fi
 if test "$vhost_kernel" = "yes" ; then
   echo "CONFIG_VHOST_KERNEL=y" >> $config_host_mak
include/hw/virtio/vhost-user-vsock.h new file +36 -0
new file mode 100644
index 0000000000..4e128a4b9f
@@ -0,0 +1,36 @@
+/*
+ * Vhost-user vsock virtio device
+ *
+ * Copyright 2020 Red Hat, Inc.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * (at your option) any later version.  See the COPYING file in the
+ * top-level directory.
+ */
+
+#ifndef _QEMU_VHOST_USER_VSOCK_H
+#define _QEMU_VHOST_USER_VSOCK_H
+
+#include "hw/virtio/vhost-vsock-common.h"
+#include "hw/virtio/vhost-user.h"
+#include "standard-headers/linux/virtio_vsock.h"
+
+#define TYPE_VHOST_USER_VSOCK "vhost-user-vsock-device"
+#define VHOST_USER_VSOCK(obj) \
+        OBJECT_CHECK(VHostUserVSock, (obj), TYPE_VHOST_USER_VSOCK)
+
+typedef struct {
+    CharBackend chardev;
+} VHostUserVSockConf;
+
+typedef struct {
+    /*< private >*/
+    VHostVSockCommon parent;
+    VhostUserState vhost_user;
+    VHostUserVSockConf conf;
+    struct virtio_vsock_config vsockcfg;
+
+    /*< public >*/
+} VHostUserVSock;
+
+#endif /* _QEMU_VHOST_USER_VSOCK_H */
hw/virtio/vhost-user-vsock.c new file +188 -0

188 lines not on this page · raw message

hw/virtio/Makefile.objs +1 -0
index b1eeb44eac..dd42daedb1 100644
@@ -18,6 +18,7 @@ common-obj-$(call land,$(CONFIG_VIRTIO_PMEM),$(CONFIG_VIRTIO_PCI)) += virtio-pme
 obj-$(call land,$(CONFIG_VHOST_USER_FS),$(CONFIG_VIRTIO_PCI)) += vhost-user-fs-pci.o
 obj-$(CONFIG_VIRTIO_IOMMU) += virtio-iommu.o
 obj-$(CONFIG_VHOST_VSOCK) += vhost-vsock-common.o vhost-vsock.o
+obj-$(CONFIG_VHOST_USER_VSOCK) += vhost-vsock-common.o vhost-user-vsock.o
 
 ifeq ($(CONFIG_VIRTIO_PCI),y)
 obj-$(CONFIG_VHOST_VSOCK) += vhost-vsock-pci.o
Signature · 1 line
2.25.4
#3 lore raw

PATCH 3/3 virtio: add vhost-user-vsock-pci device

In reply to Stefano Garzarella

Add the PCI version of vhost-user-vsock

Launch QEMU like this:

  qemu -chardev socket,path=/tmp/vm.vsock,id=chr0 \
       -device vhost-user-vsock-pci,chardev=chr0
---
 hw/virtio/vhost-user-vsock-pci.c | 84 ++++++++++++++++++++++++++++++++
 hw/virtio/Makefile.objs          |  1 +
 2 files changed, 85 insertions(+)
 create mode 100644 hw/virtio/vhost-user-vsock-pci.c
hw/virtio/vhost-user-vsock-pci.c new file +84 -0
new file mode 100644
index 0000000000..0a6847e6fc
@@ -0,0 +1,84 @@
+/*
+ * Vhost-user vsock PCI Bindings
+ *
+ * Copyright 2020 Red Hat, Inc.
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or
+ * (at your option) any later version.  See the COPYING file in the
+ * top-level directory.
+ */
+
+#include "qemu/osdep.h"
+
+#include "virtio-pci.h"
+#include "hw/qdev-properties.h"
+#include "hw/virtio/vhost-user-vsock.h"
+
+typedef struct VHostUserVSockPCI VHostUserVSockPCI;
+
+/*
+ * vhost-user-vsock-pci: This extends VirtioPCIProxy.
+ */
+#define TYPE_VHOST_USER_VSOCK_PCI "vhost-user-vsock-pci-base"
+#define VHOST_USER_VSOCK_PCI(obj) \
+        OBJECT_CHECK(VHostUserVSockPCI, (obj), TYPE_VHOST_USER_VSOCK_PCI)
+
+struct VHostUserVSockPCI {
+    VirtIOPCIProxy parent_obj;
+    VHostUserVSock vdev;
+};
+
+/* vhost-user-vsock-pci */
+
+static Property vhost_user_vsock_pci_properties[] = {
+    DEFINE_PROP_UINT32("vectors", VirtIOPCIProxy, nvectors, 3),
+    DEFINE_PROP_END_OF_LIST(),
+};
+
+static void vhost_user_vsock_pci_realize(VirtIOPCIProxy *vpci_dev, Error **errp)
+{
+    VHostUserVSockPCI *dev = VHOST_USER_VSOCK_PCI(vpci_dev);
+    DeviceState *vdev = DEVICE(&dev->vdev);
+
+    qdev_set_parent_bus(vdev, BUS(&vpci_dev->bus));
+    object_property_set_bool(OBJECT(vdev), true, "realized", errp);
+}
+
+static void vhost_user_vsock_pci_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    VirtioPCIClass *k = VIRTIO_PCI_CLASS(klass);
+    PCIDeviceClass *pcidev_k = PCI_DEVICE_CLASS(klass);
+    k->realize = vhost_user_vsock_pci_realize;
+    set_bit(DEVICE_CATEGORY_MISC, dc->categories);
+    device_class_set_props(dc, vhost_user_vsock_pci_properties);
+    pcidev_k->vendor_id = PCI_VENDOR_ID_REDHAT_QUMRANET;
+    pcidev_k->device_id = PCI_DEVICE_ID_VIRTIO_VSOCK;
+    pcidev_k->revision = 0x00;
+    pcidev_k->class_id = PCI_CLASS_COMMUNICATION_OTHER;
+}
+
+static void vhost_user_vsock_pci_instance_init(Object *obj)
+{
+    VHostUserVSockPCI *dev = VHOST_USER_VSOCK_PCI(obj);
+
+    virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev),
+                                TYPE_VHOST_USER_VSOCK);
+}
+
+static const VirtioPCIDeviceTypeInfo vhost_user_vsock_pci_info = {
+    .base_name             = TYPE_VHOST_USER_VSOCK_PCI,
+    .generic_name          = "vhost-user-vsock-pci",
+    .transitional_name     = "vhost-user-vsock-pci-transitional",
+    .non_transitional_name = "vhost-user-vsock-pci-non-transitional",
+    .instance_size = sizeof(VHostUserVSockPCI),
+    .instance_init = vhost_user_vsock_pci_instance_init,
+    .class_init    = vhost_user_vsock_pci_class_init,
+};
+
+static void virtio_pci_vhost_register(void)
+{
+    virtio_pci_types_register(&vhost_user_vsock_pci_info);
+}
+
+type_init(virtio_pci_vhost_register)
hw/virtio/Makefile.objs +1 -0
index dd42daedb1..13e75f171f 100644
@@ -22,6 +22,7 @@ obj-$(CONFIG_VHOST_USER_VSOCK) += vhost-vsock-common.o vhost-user-vsock.o
 
 ifeq ($(CONFIG_VIRTIO_PCI),y)
 obj-$(CONFIG_VHOST_VSOCK) += vhost-vsock-pci.o
+obj-$(CONFIG_VHOST_USER_VSOCK) += vhost-user-vsock-pci.o
 obj-$(CONFIG_VHOST_USER_BLK) += vhost-user-blk-pci.o
 obj-$(CONFIG_VHOST_USER_INPUT) += vhost-user-input-pci.o
 obj-$(CONFIG_VHOST_USER_SCSI) += vhost-user-scsi-pci.o
Signature · 1 line
2.25.4
#4 lore raw
In reply to Stefano Garzarella
Quoted 6 lines · Stefano Garzarella

This series add vhost-user-vsock base device and its PCI version (vhost-user-vsock-pci).
The vhost-user-vsock device can be used to implement the virtio-vsock device emulation in user-space.
An use case might be Kata, where they prefer to emulate devices in user-space.

Looks good.

My main concern is live migration. If it hasn't been tested, please remove the live migration code.

Stefan

#5 lore raw
Quoted 8 lines · Stefan Hajnoczi
Quoted 6 lines · Stefano Garzarella

This series add vhost-user-vsock base device and its PCI version (vhost-user-vsock-pci).
The vhost-user-vsock device can be used to implement the virtio-vsock device emulation in user-space.
An use case might be Kata, where they prefer to emulate devices in user-space.

Quoted 5 lines

Looks good.

My main concern is live migration. If it hasn't been tested, please remove the live migration code.

Right, I didn't test live migration with vhost-user-vsock. I'll give it a try. If it doesn't work, I'll remove it.

Thanks for the review,
Stefano

#6 lore raw
In reply to Stefan Hajnoczi
Quoted 8 lines · Stefan Hajnoczi
Quoted 6 lines · Stefano Garzarella

This series add vhost-user-vsock base device and its PCI version (vhost-user-vsock-pci).
The vhost-user-vsock device can be used to implement the virtio-vsock device emulation in user-space.
An use case might be Kata, where they prefer to emulate devices in user-space.

Quoted 5 lines

Looks good.

My main concern is live migration. If it hasn't been tested, please remove the live migration code.

Migration is not working:

QEMU 5.0.50 monitor - type 'help' for more information
(qemu) migrate -d tcp:0:3333
Error: Migration disabled: vhost-user backend lacks VHOST_USER_PROTOCOL_F_LOG_SHMFD feature.

Thanks for pointing that out, I'll remove it in v2.

Stefano