]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemu: add support for vhost-vsock-pci
authorJán Tomko <jtomko@redhat.com>
Tue, 22 May 2018 13:57:47 +0000 (15:57 +0200)
committerJán Tomko <jtomko@redhat.com>
Tue, 29 May 2018 13:42:04 +0000 (15:42 +0200)
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@redhat.com>
src/qemu/qemu_alias.c
src/qemu/qemu_command.c
src/qemu/qemu_domain.c
src/qemu/qemu_domain.h
src/qemu/qemu_process.c
tests/qemuxml2argvdata/vhost-vsock-auto.x86_64-latest.args [new file with mode: 0644]
tests/qemuxml2argvdata/vhost-vsock.x86_64-latest.args [new file with mode: 0644]
tests/qemuxml2argvtest.c

index 578a33b2848ab107c2e3f659ada09a545d80cb20..89dec91ed11d3f94e3c755cb1491b7dba48a5a2a 100644 (file)
@@ -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;
 }
index c7ff074e29167cb380052f55019de5b15a19de76..0b5ec4f2bac767037a45e61746b087c61139a784 100644 (file)
@@ -9912,6 +9912,42 @@ qemuBuildSeccompSandboxCommandLine(virCommandPtr cmd,
 }
 
 
+static int
+qemuBuildVsockCommandLine(virCommandPtr cmd,
+                          virDomainDefPtr def,
+                          virDomainVsockDefPtr vsock,
+                          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=%u", priv->vhostfd);
+    if (qemuBuildDeviceAddressStr(&buf, def, &vsock->info, qemuCaps) < 0)
+        goto cleanup;
+
+    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.
@@ -10161,6 +10197,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));
index 10f22b1cc2fdc9470509c26ccb960ea6dbe491d7..2c51e4c0d898f63d68bcbd1b772b49e3b88bda04 100644 (file)
@@ -1160,6 +1160,8 @@ qemuDomainVsockPrivateNew(void)
     if (!(priv = virObjectNew(qemuDomainVsockPrivateClass)))
         return NULL;
 
+    priv->vhostfd = -1;
+
     return (virObjectPtr) priv;
 }
 
@@ -1167,6 +1169,9 @@ qemuDomainVsockPrivateNew(void)
 static void
 qemuDomainVsockPrivateDispose(void *obj ATTRIBUTE_UNUSED)
 {
+    qemuDomainVsockPrivatePtr priv = obj;
+
+    VIR_FORCE_CLOSE(priv->vhostfd);
 }
 
 
index f298ebf7859917469840230f2861b1dfb8312101..2e0f4df0fba7f752e94342858e3e833d2d0f8c97 100644 (file)
@@ -447,7 +447,7 @@ typedef qemuDomainVsockPrivate *qemuDomainVsockPrivatePtr;
 struct _qemuDomainVsockPrivate {
     virObject parent;
 
-    virTristateBool maybe;
+    int vhostfd;
 };
 
 
index 5f5759c9c812d8c01305913db99be2d55baefaac..30cc5904e0a7442d75d2bacd1c467603adb51103 100644 (file)
@@ -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
 
@@ -5947,6 +5948,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->auto_cid == VIR_TRISTATE_BOOL_YES) {
+        if (virVsockAcquireGuestCid(fd, &vsock->guest_cid) < 0)
+            goto error;
+    } else {
+        if (virVsockSetGuestCid(fd, vsock->guest_cid) < 0)
+            goto error;
+    }
+
+    priv->vhostfd = fd;
+    return 0;
+
+ error:
+    VIR_FORCE_CLOSE(fd);
+    return -1;
+}
+
+
 /**
  * qemuProcessPrepareHost:
  * @driver: qemu driver
@@ -5972,6 +6003,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 (file)
index 0000000..dd9b60b
--- /dev/null
@@ -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-i440fx-2.9,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 (file)
index 0000000..907af8b
--- /dev/null
@@ -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
index 2fe4390104b46ad2d58843589c35c742386866b7..ddd2b88c0a875972828e95e4d1f017aceac66916 100644 (file)
@@ -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))) {
@@ -2856,6 +2867,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);