Implements QEMU support for vhost-user-blk together with live hotplug/unplug. Signed-off-by: Pavel Hrdina <phrdina@xxxxxxxxxx> --- src/qemu/qemu_block.c | 16 ++++ src/qemu/qemu_block.h | 5 + src/qemu/qemu_command.c | 91 +++++++++++++++++-- src/qemu/qemu_command.h | 8 ++ src/qemu/qemu_domain.c | 5 + src/qemu/qemu_hotplug.c | 5 +- src/qemu/qemu_validate.c | 13 +++ .../disk-vhostuser.x86_64-latest.args | 41 +++++++++ tests/qemuxml2argvtest.c | 1 + 9 files changed, 176 insertions(+), 9 deletions(-) create mode 100644 tests/qemuxml2argvdata/disk-vhostuser.x86_64-latest.args diff --git a/src/qemu/qemu_block.c b/src/qemu/qemu_block.c index f6e81a7503..dd7858b6cc 100644 --- a/src/qemu/qemu_block.c +++ b/src/qemu/qemu_block.c @@ -1640,6 +1640,8 @@ qemuBlockStorageSourceAttachDataFree(qemuBlockStorageSourceAttachDataPtr data) VIR_FREE(data->httpcookiesecretAlias); VIR_FREE(data->driveCmd); VIR_FREE(data->driveAlias); + VIR_FREE(data->chardevAlias); + VIR_FREE(data->chardevCmd); VIR_FREE(data); } @@ -1813,6 +1815,13 @@ qemuBlockStorageSourceAttachApply(qemuMonitorPtr mon, data->driveAdded = true; } + if (data->chardevDef) { + if (qemuMonitorAttachCharDev(mon, data->chardevAlias, data->chardevDef) < 0) + return -1; + + data->chardevAdded = true; + } + return 0; } @@ -1835,6 +1844,13 @@ qemuBlockStorageSourceAttachRollback(qemuMonitorPtr mon, virErrorPreserveLast(&orig_err); + if (data->chardevAdded) { + if (qemuMonitorDetachCharDev(mon, data->chardevAlias) < 0) { + VIR_WARN("Unable to remove chardev %s after failed " "qemuMonitorAddDevice", + data->chardevAlias); + } + } + if (data->driveAdded) { if (qemuMonitorDriveDel(mon, data->driveAlias) < 0) VIR_WARN("Unable to remove drive %s (%s) after failed " diff --git a/src/qemu/qemu_block.h b/src/qemu/qemu_block.h index 8f2a05f46a..d2efcfc3c0 100644 --- a/src/qemu/qemu_block.h +++ b/src/qemu/qemu_block.h @@ -99,6 +99,11 @@ struct qemuBlockStorageSourceAttachData { char *driveAlias; bool driveAdded; + virDomainChrSourceDefPtr chardevDef; + char *chardevAlias; + char *chardevCmd; + bool chardevAdded; + virJSONValuePtr authsecretProps; char *authsecretAlias; diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 3e652e18b7..059126cfeb 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -1712,9 +1712,16 @@ qemuBuildDiskDeviceStr(const virDomainDef *def, break; case VIR_DOMAIN_DISK_BUS_VIRTIO: - if (qemuBuildVirtioDevStr(&opt, "virtio-blk", qemuCaps, - VIR_DOMAIN_DEVICE_DISK, disk) < 0) { - return NULL; + if (virStorageSourceGetActualType(disk->src) == VIR_STORAGE_TYPE_VHOST_USER) { + if (qemuBuildVirtioDevStr(&opt, "vhost-user-blk", qemuCaps, + VIR_DOMAIN_DEVICE_DISK, disk) < 0) { + return NULL; + } + } else { + if (qemuBuildVirtioDevStr(&opt, "virtio-blk", qemuCaps, + VIR_DOMAIN_DEVICE_DISK, disk) < 0) { + return NULL; + } } if (disk->iothread) @@ -1773,11 +1780,17 @@ qemuBuildDiskDeviceStr(const virDomainDef *def, virQEMUCapsGet(qemuCaps, QEMU_CAPS_DISK_SHARE_RW)) virBufferAddLit(&opt, ",share-rw=on"); - if (qemuDomainDiskGetBackendAlias(disk, qemuCaps, &backendAlias) < 0) - return NULL; + if (virStorageSourceGetActualType(disk->src) == VIR_STORAGE_TYPE_VHOST_USER) { + backendAlias = qemuDomainGetVhostUserAlias(disk->info.alias); - if (backendAlias) - virBufferAsprintf(&opt, ",drive=%s", backendAlias); + virBufferAsprintf(&opt, ",chardev=%s", backendAlias); + } else { + if (qemuDomainDiskGetBackendAlias(disk, qemuCaps, &backendAlias) < 0) + return NULL; + + if (backendAlias) + virBufferAsprintf(&opt, ",drive=%s", backendAlias); + } virBufferAsprintf(&opt, ",id=%s", disk->info.alias); if (bootindex) @@ -1987,6 +2000,9 @@ qemuBuildBlockStorageSourceAttachDataCommandline(virCommandPtr cmd, if (data->driveCmd) virCommandAddArgList(cmd, "-drive", data->driveCmd, NULL); + if (data->chardevCmd) + virCommandAddArgList(cmd, "-chardev", data->chardevCmd, NULL); + if (data->storageProps) { if (!(tmp = virJSONValueToString(data->storageProps, false))) return -1; @@ -2025,7 +2041,10 @@ qemuBuildDiskSourceCommandLine(virCommandPtr cmd, g_autofree char *copyOnReadPropsStr = NULL; size_t i; - if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_BLOCKDEV) && + if (virStorageSourceGetActualType(disk->src) == VIR_STORAGE_TYPE_VHOST_USER) { + if (!(data = qemuBuildStorageSourceChainAttachPrepareChardev(disk))) + return -1; + } else if (virQEMUCapsGet(qemuCaps, QEMU_CAPS_BLOCKDEV) && !qemuDiskBusIsSD(disk->bus)) { if (virStorageSourceIsEmpty(disk->src)) return 0; @@ -10291,6 +10310,38 @@ qemuBuildStorageSourceAttachPrepareDrive(virDomainDiskDefPtr disk, } +/** + * qemuBuildStorageSourceAttachPrepareChardev: + * @src: disk source to prepare + * + * Prepare qemuBlockStorageSourceAttachDataPtr for use with -chardev. + */ +qemuBlockStorageSourceAttachDataPtr +qemuBuildStorageSourceAttachPrepareChardev(virDomainDiskDefPtr disk) +{ + g_autoptr(qemuBlockStorageSourceAttachData) data = NULL; + g_auto(virBuffer) chardev = VIR_BUFFER_INITIALIZER; + + data = g_new0(qemuBlockStorageSourceAttachData, 1); + + data->chardevDef = disk->src->vhostuser; + data->chardevAlias = qemuDomainGetVhostUserAlias(disk->info.alias); + + virBufferAddLit(&chardev, "socket"); + virBufferAsprintf(&chardev, ",id=%s", data->chardevAlias); + virBufferAddLit(&chardev, ",path="); + virQEMUBuildBufferEscapeComma(&chardev, disk->src->vhostuser->data.nix.path); + + qemuBuildChrChardevReconnectStr(&chardev, + &disk->src->vhostuser->data.nix.reconnect); + + if (!(data->chardevCmd = virBufferContentAndReset(&chardev))) + return NULL; + + return g_steal_pointer(&data); +} + + /** * qemuBuildStorageSourceAttachPrepareCommon: * @src: storage source @@ -10373,6 +10424,30 @@ qemuBuildStorageSourceChainAttachPrepareDrive(virDomainDiskDefPtr disk, } +/** + * qemuBuildStorageSourceChainAttachPrepareChardev: + * @src: disk definition + * + * Prepares qemuBlockStorageSourceChainDataPtr for attaching @disk via -drive. + */ +qemuBlockStorageSourceChainDataPtr +qemuBuildStorageSourceChainAttachPrepareChardev(virDomainDiskDefPtr disk) +{ + g_autoptr(qemuBlockStorageSourceAttachData) elem = NULL; + g_autoptr(qemuBlockStorageSourceChainData) data = NULL; + + data = g_new0(qemuBlockStorageSourceChainData, 1); + + if (!(elem = qemuBuildStorageSourceAttachPrepareChardev(disk))) + return NULL; + + if (VIR_APPEND_ELEMENT(data->srcdata, data->nsrcdata, elem) < 0) + return NULL; + + return g_steal_pointer(&data); +} + + static int qemuBuildStorageSourceChainAttachPrepareBlockdevOne(qemuBlockStorageSourceChainData *data, virStorageSourcePtr src, diff --git a/src/qemu/qemu_command.h b/src/qemu/qemu_command.h index 3cfe6ff3e9..a33fbf6f4e 100644 --- a/src/qemu/qemu_command.h +++ b/src/qemu/qemu_command.h @@ -115,6 +115,10 @@ bool qemuDiskBusIsSD(int bus); qemuBlockStorageSourceAttachDataPtr qemuBuildStorageSourceAttachPrepareDrive(virDomainDiskDefPtr disk, virQEMUCapsPtr qemuCaps); + +qemuBlockStorageSourceAttachDataPtr +qemuBuildStorageSourceAttachPrepareChardev(virDomainDiskDefPtr disk); + int qemuBuildStorageSourceAttachPrepareCommon(virStorageSourcePtr src, qemuBlockStorageSourceAttachDataPtr data, @@ -126,6 +130,10 @@ qemuBuildStorageSourceChainAttachPrepareDrive(virDomainDiskDefPtr disk, virQEMUCapsPtr qemuCaps); +qemuBlockStorageSourceChainDataPtr +qemuBuildStorageSourceChainAttachPrepareChardev(virDomainDiskDefPtr disk); + + qemuBlockStorageSourceChainDataPtr qemuBuildStorageSourceChainAttachPrepareBlockdev(virStorageSourcePtr top, virQEMUCapsPtr qemuCaps); diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 0c078a9388..aa6d539610 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -10581,6 +10581,11 @@ qemuDomainPrepareDiskSource(virDomainDiskDefPtr disk, disk->src->format = VIR_STORAGE_FILE_RAW; } + /* Nothing else to prepare as it will use -chardev instead + * of -blockdev/-drive option. */ + if (disk->src->type == VIR_STORAGE_TYPE_VHOST_USER) + return 0; + if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_BLOCKDEV) && !qemuDiskBusIsSD(disk->bus)) { if (qemuDomainPrepareDiskSourceBlockdev(disk, priv, cfg) < 0) diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 882e5d2384..dd6ba80cd0 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -714,7 +714,10 @@ qemuDomainAttachDiskGeneric(virQEMUDriverPtr driver, if (qemuDomainPrepareDiskSource(disk, priv, cfg) < 0) goto cleanup; - if (blockdev) { + if (virStorageSourceGetActualType(disk->src) == VIR_STORAGE_TYPE_VHOST_USER) { + if (!(data = qemuBuildStorageSourceChainAttachPrepareChardev(disk))) + goto cleanup; + } else if (blockdev) { if (disk->copy_on_read == VIR_TRISTATE_SWITCH_ON) { if (!(corProps = qemuBlockStorageGetCopyOnReadProps(disk))) goto cleanup; diff --git a/src/qemu/qemu_validate.c b/src/qemu/qemu_validate.c index 96fc7d4118..f0e518a0f1 100644 --- a/src/qemu/qemu_validate.c +++ b/src/qemu/qemu_validate.c @@ -2944,6 +2944,19 @@ qemuValidateDomainDeviceDefDisk(const virDomainDiskDef *disk, return -1; } + if (disk->src->type == VIR_STORAGE_TYPE_VHOST_USER) { + if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VHOST_USER_BLK)) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("vhostuser disk is not supported with this QEMU binary")); + return -1; + } + + if (qemuValidateDomainDefVhostUserRequireSharedMemory(def, "vhostuser", + qemuCaps) < 0) { + return -1; + } + } + return 0; } diff --git a/tests/qemuxml2argvdata/disk-vhostuser.x86_64-latest.args b/tests/qemuxml2argvdata/disk-vhostuser.x86_64-latest.args new file mode 100644 index 0000000000..b24b2c0b4f --- /dev/null +++ b/tests/qemuxml2argvdata/disk-vhostuser.x86_64-latest.args @@ -0,0 +1,41 @@ +LC_ALL=C \ +PATH=/bin \ +HOME=/tmp/lib/domain--1-QEMUGuest1 \ +USER=test \ +LOGNAME=test \ +XDG_DATA_HOME=/tmp/lib/domain--1-QEMUGuest1/.local/share \ +XDG_CACHE_HOME=/tmp/lib/domain--1-QEMUGuest1/.cache \ +XDG_CONFIG_HOME=/tmp/lib/domain--1-QEMUGuest1/.config \ +QEMU_AUDIO_DRV=none \ +/usr/bin/qemu-system-x86_64 \ +-name guest=QEMUGuest1,debug-threads=on \ +-S \ +-object secret,id=masterKey0,format=raw,\ +file=/tmp/lib/domain--1-QEMUGuest1/master-key.aes \ +-machine pc,accel=tcg,usb=off,dump-guest-core=off,memory-backend=pc.ram \ +-cpu qemu64 \ +-m 214 \ +-object memory-backend-memfd,id=pc.ram,share=yes,size=224395264 \ +-overcommit mem-lock=off \ +-smp 1,sockets=1,cores=1,threads=1 \ +-uuid c7a5fdbd-edaf-9455-926a-d65c16db1809 \ +-display none \ +-no-user-config \ +-nodefaults \ +-chardev socket,id=charmonitor,fd=1729,server,nowait \ +-mon chardev=charmonitor,id=monitor,mode=control \ +-rtc base=utc \ +-no-shutdown \ +-no-acpi \ +-boot strict=on \ +-device piix3-usb-uhci,id=usb,bus=pci.0,addr=0x1.0x2 \ +-chardev socket,id=chr-vu-virtio-disk0,path=/tmp/vhost1.sock \ +-device vhost-user-blk-pci,bus=pci.0,addr=0x2,chardev=chr-vu-virtio-disk0,\ +id=virtio-disk0,bootindex=1 \ +-chardev socket,id=chr-vu-virtio-disk1,path=/tmp/vhost1.sock,reconnect=10 \ +-device vhost-user-blk-pci,bus=pci.0,addr=0x3,chardev=chr-vu-virtio-disk1,\ +id=virtio-disk1 \ +-device virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x4 \ +-sandbox on,obsolete=deny,elevateprivileges=deny,spawn=deny,\ +resourcecontrol=deny \ +-msg timestamp=on diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index c5d82ac72e..0f29e166e3 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -1267,6 +1267,7 @@ mymain(void) VIR_FREE(driver.config->vxhsTLSx509certdir); DO_TEST("disk-no-boot", NONE); DO_TEST_CAPS_LATEST("disk-nvme"); + DO_TEST_CAPS_LATEST("disk-vhostuser"); DO_TEST_PARSE_ERROR("disk-device-lun-type-invalid", QEMU_CAPS_VIRTIO_SCSI); DO_TEST_CAPS_LATEST_PARSE_ERROR("disk-attaching-partition-nosupport"); -- 2.29.2