[libvirt] [PATCHv2 7/7] qemu: add support for vhost-vsock-pci

Ján Tomko jtomko at redhat.com
Thu May 24 10:39:15 UTC 2018


Create a new vsock endpoint by opening /dev/vhost-vsock,
set the requested CID via ioctl (or assign a free one if auto='yes'),
pass the file descriptor to QEMU and build the command line.

https://bugzilla.redhat.com/show_bug.cgi?id=1291851
Signed-off-by: Ján Tomko <jtomko at redhat.com>
---
 src/qemu/qemu_alias.c                              | 16 ++++++++
 src/qemu/qemu_command.c                            | 45 ++++++++++++++++++++++
 src/qemu/qemu_domain.c                             |  5 +++
 src/qemu/qemu_domain.h                             |  2 +-
 src/qemu/qemu_process.c                            | 35 +++++++++++++++++
 .../vhost-vsock-auto.x86_64-latest.args            | 32 +++++++++++++++
 .../vhost-vsock.x86_64-latest.args                 | 32 +++++++++++++++
 tests/qemuxml2argvtest.c                           | 14 +++++++
 8 files changed, 180 insertions(+), 1 deletion(-)
 create mode 100644 tests/qemuxml2argvdata/vhost-vsock-auto.x86_64-latest.args
 create mode 100644 tests/qemuxml2argvdata/vhost-vsock.x86_64-latest.args

diff --git a/src/qemu/qemu_alias.c b/src/qemu/qemu_alias.c
index 578a33b284..89dec91ed1 100644
--- a/src/qemu/qemu_alias.c
+++ b/src/qemu/qemu_alias.c
@@ -533,6 +533,18 @@ qemuAssignDeviceInputAlias(virDomainDefPtr def,
 }
 
 
+static int
+qemuAssignDeviceVsockAlias(virDomainVsockDefPtr vsock)
+{
+    if (vsock->info.alias)
+        return 0;
+    if (VIR_STRDUP(vsock->info.alias, "vsock0") < 0)
+        return -1;
+
+    return 0;
+}
+
+
 int
 qemuAssignDeviceAliases(virDomainDefPtr def, virQEMUCapsPtr qemuCaps)
 {
@@ -629,6 +641,10 @@ qemuAssignDeviceAliases(virDomainDefPtr def, virQEMUCapsPtr qemuCaps)
         if (qemuAssignDeviceMemoryAlias(NULL, def->mems[i], false) < 0)
             return -1;
     }
+    if (def->vsock) {
+        if (qemuAssignDeviceVsockAlias(def->vsock) < 0)
+            return -1;
+    }
 
     return 0;
 }
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index 9da2d609e8..ef0716d683 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -9865,6 +9865,47 @@ qemuBuildSeccompSandboxCommandLine(virCommandPtr cmd,
 }
 
 
+static int
+qemuBuildVsockCommandLine(virCommandPtr cmd,
+                          virDomainDefPtr def,
+                          virDomainVsockDefPtr vsock,
+                          const char *fdprefix,
+                          virQEMUCapsPtr qemuCaps)
+{
+    qemuDomainVsockPrivatePtr priv = (qemuDomainVsockPrivatePtr)vsock->privateData;
+    const char *device = "vhost-vsock-pci";
+    virBuffer buf = VIR_BUFFER_INITIALIZER;
+    char *devstr = NULL;
+    int ret = -1;
+
+    virBufferAsprintf(&buf, "%s", device);
+    virBufferAsprintf(&buf, ",id=%s", vsock->info.alias);
+    virBufferAsprintf(&buf, ",guest-cid=%u", vsock->guest_cid);
+    virBufferAsprintf(&buf, ",vhostfd=%s%u", fdprefix, priv->vhostfd);
+    if (qemuBuildDeviceAddressStr(&buf, def, &vsock->info, qemuCaps) < 0)
+        goto cleanup;
+#if 0
+    if (qemuBuildVirtioOptionsStr(&buf, net->virtio, qemuCaps) < 0)
+        goto error;
+#endif
+
+    if (virBufferCheckError(&buf) < 0)
+        goto cleanup;
+
+    devstr = virBufferContentAndReset(&buf);
+
+    virCommandPassFD(cmd, priv->vhostfd, VIR_COMMAND_PASS_FD_CLOSE_PARENT);
+    priv->vhostfd = -1;
+    virCommandAddArgList(cmd, "-device", devstr, NULL);
+
+    ret = 0;
+ cleanup:
+    virBufferFreeAndReset(&buf);
+    VIR_FREE(devstr);
+    return ret;
+}
+
+
 /*
  * Constructs a argv suitable for launching qemu with config defined
  * for a given virtual machine.
@@ -10111,6 +10152,10 @@ qemuBuildCommandLine(virQEMUDriverPtr driver,
             goto error;
     }
 
+    if (def->vsock &&
+        qemuBuildVsockCommandLine(cmd, def, def->vsock, "", qemuCaps) < 0)
+        goto error;
+
     /* In some situations, eg. VFIO passthrough, QEMU might need to lock a
      * significant amount of memory, so we need to set the limit accordingly */
     virCommandSetMaxMemLock(cmd, qemuDomainGetMemLockLimitBytes(def));
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index 527abe3b63..ff24f4b0d2 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -1159,6 +1159,8 @@ qemuDomainVsockPrivateNew(void)
     if (!(priv = virObjectNew(qemuDomainVsockPrivateClass)))
         return NULL;
 
+    priv->vhostfd = -1;
+
     return (virObjectPtr) priv;
 }
 
@@ -1166,6 +1168,9 @@ qemuDomainVsockPrivateNew(void)
 static void
 qemuDomainVsockPrivateDispose(void *obj ATTRIBUTE_UNUSED)
 {
+    qemuDomainVsockPrivatePtr priv = obj;
+
+    VIR_FORCE_CLOSE(priv->vhostfd);
 }
 
 
diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h
index 2046741a17..6ddf016eb7 100644
--- a/src/qemu/qemu_domain.h
+++ b/src/qemu/qemu_domain.h
@@ -446,7 +446,7 @@ typedef qemuDomainVsockPrivate *qemuDomainVsockPrivatePtr;
 struct _qemuDomainVsockPrivate {
     virObject parent;
 
-    virTristateBool maybe;
+    int vhostfd;
 };
 
 
diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c
index 174d932ae7..e318639963 100644
--- a/src/qemu/qemu_process.c
+++ b/src/qemu/qemu_process.c
@@ -79,6 +79,7 @@
 #include "nwfilter_conf.h"
 #include "netdev_bandwidth_conf.h"
 #include "virresctrl.h"
+#include "virvsock.h"
 
 #define VIR_FROM_THIS VIR_FROM_QEMU
 
@@ -5975,6 +5976,36 @@ qemuProcessPrepareHostStorage(virQEMUDriverPtr driver,
 }
 
 
+static int
+qemuProcessOpenVhostVsock(virDomainVsockDefPtr vsock)
+{
+    qemuDomainVsockPrivatePtr priv = (qemuDomainVsockPrivatePtr)vsock->privateData;
+    const char *vsock_path = "/dev/vhost-vsock";
+    int fd;
+
+    if ((fd = open(vsock_path, O_RDWR)) < 0) {
+        virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
+                       "%s", _("unable to open vhost-vsock device"));
+        return -1;
+    }
+
+    if (vsock->guest_cid) {
+        if (virVsockSetGuestCid(fd, vsock->guest_cid) < 0)
+            goto error;
+    } else {
+        if (virVsockAcquireGuestCid(fd, &vsock->guest_cid) < 0)
+            goto error;
+    }
+
+    priv->vhostfd = fd;
+    return 0;
+
+ error:
+    VIR_FORCE_CLOSE(fd);
+    return -1;
+}
+
+
 /**
  * qemuProcessPrepareHost:
  * @driver: qemu driver
@@ -6000,6 +6031,10 @@ qemuProcessPrepareHost(virQEMUDriverPtr driver,
     if (qemuPrepareNVRAM(cfg, vm) < 0)
         goto cleanup;
 
+    if (vm->def->vsock) {
+        if (qemuProcessOpenVhostVsock(vm->def->vsock) < 0)
+            goto cleanup;
+    }
     /* network devices must be "prepared" before hostdevs, because
      * setting up a network device might create a new hostdev that
      * will need to be setup.
diff --git a/tests/qemuxml2argvdata/vhost-vsock-auto.x86_64-latest.args b/tests/qemuxml2argvdata/vhost-vsock-auto.x86_64-latest.args
new file mode 100644
index 0000000000..cefd4e3009
--- /dev/null
+++ b/tests/qemuxml2argvdata/vhost-vsock-auto.x86_64-latest.args
@@ -0,0 +1,32 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/home/test \
+USER=test \
+LOGNAME=test \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-x86_64 \
+-name guest=test,debug-threads=on \
+-S \
+-object secret,id=masterKey0,format=raw,\
+file=/tmp/lib/domain--1-test/master-key.aes \
+-machine pc-0.13,accel=tcg,usb=off,dump-guest-core=off \
+-m 1024 \
+-realtime mlock=off \
+-smp 1,sockets=1,cores=1,threads=1 \
+-uuid bba65c0e-c049-934f-b6aa-4e2c0582acdf \
+-display none \
+-no-user-config \
+-nodefaults \
+-chardev socket,id=charmonitor,path=/tmp/lib/domain--1-test/monitor.sock,\
+server,nowait \
+-mon chardev=charmonitor,id=monitor,mode=control \
+-rtc base=utc \
+-no-shutdown \
+-no-acpi \
+-boot menu=on,strict=on \
+-device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 \
+-device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 \
+-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,\
+resourcecontrol=deny \
+-device vhost-vsock-pci,id=vsock0,guest-cid=42,vhostfd=6789,bus=pci.0,addr=0x2 \
+-msg timestamp=on
diff --git a/tests/qemuxml2argvdata/vhost-vsock.x86_64-latest.args b/tests/qemuxml2argvdata/vhost-vsock.x86_64-latest.args
new file mode 100644
index 0000000000..907af8bb99
--- /dev/null
+++ b/tests/qemuxml2argvdata/vhost-vsock.x86_64-latest.args
@@ -0,0 +1,32 @@
+LC_ALL=C \
+PATH=/bin \
+HOME=/home/test \
+USER=test \
+LOGNAME=test \
+QEMU_AUDIO_DRV=none \
+/usr/bin/qemu-system-x86_64 \
+-name guest=test,debug-threads=on \
+-S \
+-object secret,id=masterKey0,format=raw,\
+file=/tmp/lib/domain--1-test/master-key.aes \
+-machine pc-0.13,accel=tcg,usb=off,dump-guest-core=off \
+-m 1024 \
+-realtime mlock=off \
+-smp 1,sockets=1,cores=1,threads=1 \
+-uuid bba65c0e-c049-934f-b6aa-4e2c0582acdf \
+-display none \
+-no-user-config \
+-nodefaults \
+-chardev socket,id=charmonitor,path=/tmp/lib/domain--1-test/monitor.sock,\
+server,nowait \
+-mon chardev=charmonitor,id=monitor,mode=control \
+-rtc base=utc \
+-no-shutdown \
+-no-acpi \
+-boot menu=on,strict=on \
+-device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 \
+-device virtio-serial-pci,id=virtio-serial0,bus=pci.0,addr=0x6 \
+-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,\
+resourcecontrol=deny \
+-device vhost-vsock-pci,id=vsock0,guest-cid=4,vhostfd=6789,bus=pci.0,addr=0x7 \
+-msg timestamp=on
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index 78454acb1a..759b045e4c 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -532,6 +532,17 @@ testCompareXMLToArgv(const void *data)
         }
     }
 
+    if (vm->def->vsock) {
+        virDomainVsockDefPtr vsock = vm->def->vsock;
+        qemuDomainVsockPrivatePtr vsockPriv =
+            (qemuDomainVsockPrivatePtr)vsock->privateData;
+
+        if (vsock->auto_cid == VIR_TRISTATE_BOOL_YES)
+            vsock->guest_cid = 42;
+
+        vsockPriv->vhostfd = 6789;
+    }
+
     if (!(cmd = qemuProcessCreatePretendCmd(&driver, vm, migrateURI,
                                             (flags & FLAG_FIPS), false,
                                             VIR_QEMU_PROCESS_START_COLD))) {
@@ -2848,6 +2859,9 @@ mymain(void)
             QEMU_CAPS_DEVICE_VIRTIO_MOUSE_CCW,
             QEMU_CAPS_DEVICE_VIRTIO_TABLET_CCW);
 
+    DO_TEST_CAPS_LATEST("vhost-vsock");
+    DO_TEST_CAPS_LATEST("vhost-vsock-auto");
+
     if (getenv("LIBVIRT_SKIP_CLEANUP") == NULL)
         virFileDeleteTree(fakerootdir);
 
-- 
2.16.1




More information about the libvir-list mailing list