[libvirt] [PATCH 0/3 v3] qemu: Hotplug SCSI controllers/hostdevs

Today, hotplug of a SCSI hostdev relies on the presence of an existing SCSI controller, which can be defined in the initial domain XML. Hotplug of a SCSI controller relies on the presence of a PCI bus, which does not work in all environments. These patches correct this behavior, in order to allow hotplug (and unplug) work without incident. Changes from v2->v3: - Patch 2: Combine attach code within QEMU_CAPS_DEVICE check - Patch 2: Restructure detach to cover ..._TYPE_NONE controllers Changes from v1->v2: - Patch 2: Check for VIR_..._TYPE_NONE on controller->info.type - Patch 2: Check device address for TYPE_CCW controllers during detach - Patch 1: Fixed an indentation change that was missed Eric Farman (3): qemu: Rename controller hotplug functions to not be PCI-specific qemu: Separate calls based on controller bus type qemu: Auto-generate controller for hotplugged hostdev src/qemu/qemu_driver.c | 4 +-- src/qemu/qemu_hotplug.c | 62 +++++++++++++++++++++++++++++++++++------------ src/qemu/qemu_hotplug.h | 12 ++++----- 3 files changed, 54 insertions(+), 24 deletions(-) -- 1.7.9.5

For attach/detach of controller devices, we rename the functions to remove 'PCI' from their title. The actual separation of PCI-specific operations will be handled in the next patch. Signed-off-by: Eric Farman <farman@linux.vnet.ibm.com> --- src/qemu/qemu_driver.c | 4 ++-- src/qemu/qemu_hotplug.c | 16 ++++++++-------- src/qemu/qemu_hotplug.h | 12 ++++++------ 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 378b542..065e104 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -6328,7 +6328,7 @@ qemuDomainAttachDeviceControllerLive(virQEMUDriverPtr driver, switch (cont->type) { case VIR_DOMAIN_CONTROLLER_TYPE_SCSI: - ret = qemuDomainAttachPciControllerDevice(driver, vm, cont); + ret = qemuDomainAttachControllerDevice(driver, vm, cont); break; default: virReportError(VIR_ERR_OPERATION_UNSUPPORTED, @@ -6420,7 +6420,7 @@ qemuDomainDetachDeviceControllerLive(virQEMUDriverPtr driver, switch (cont->type) { case VIR_DOMAIN_CONTROLLER_TYPE_SCSI: - ret = qemuDomainDetachPciControllerDevice(driver, vm, dev); + ret = qemuDomainDetachControllerDevice(driver, vm, dev); break; default : virReportError(VIR_ERR_OPERATION_UNSUPPORTED, diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 0d9a3aa..c81d0f3 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -348,9 +348,9 @@ error: } -int qemuDomainAttachPciControllerDevice(virQEMUDriverPtr driver, - virDomainObjPtr vm, - virDomainControllerDefPtr controller) +int qemuDomainAttachControllerDevice(virQEMUDriverPtr driver, + virDomainObjPtr vm, + virDomainControllerDefPtr controller) { int ret = -1; const char* type = virDomainControllerTypeToString(controller->type); @@ -438,8 +438,8 @@ qemuDomainFindOrCreateSCSIDiskController(virQEMUDriverPtr driver, cont->model = -1; VIR_INFO("No SCSI controller present, hotplugging one"); - if (qemuDomainAttachPciControllerDevice(driver, - vm, cont) < 0) { + if (qemuDomainAttachControllerDevice(driver, + vm, cont) < 0) { VIR_FREE(cont); return NULL; } @@ -3029,9 +3029,9 @@ static bool qemuDomainControllerIsBusy(virDomainObjPtr vm, } } -int qemuDomainDetachPciControllerDevice(virQEMUDriverPtr driver, - virDomainObjPtr vm, - virDomainDeviceDefPtr dev) +int qemuDomainDetachControllerDevice(virQEMUDriverPtr driver, + virDomainObjPtr vm, + virDomainDeviceDefPtr dev) { int idx, ret = -1; virDomainControllerDefPtr detach = NULL; diff --git a/src/qemu/qemu_hotplug.h b/src/qemu/qemu_hotplug.h index 355d809..75789d6 100644 --- a/src/qemu/qemu_hotplug.h +++ b/src/qemu/qemu_hotplug.h @@ -36,9 +36,9 @@ int qemuDomainChangeEjectableMedia(virQEMUDriverPtr driver, int qemuDomainCheckEjectableMedia(virQEMUDriverPtr driver, virDomainObjPtr vm, enum qemuDomainAsyncJob asyncJob); -int qemuDomainAttachPciControllerDevice(virQEMUDriverPtr driver, - virDomainObjPtr vm, - virDomainControllerDefPtr controller); +int qemuDomainAttachControllerDevice(virQEMUDriverPtr driver, + virDomainObjPtr vm, + virDomainControllerDefPtr controller); int qemuDomainAttachDeviceDiskLive(virConnectPtr conn, virQEMUDriverPtr driver, virDomainObjPtr vm, @@ -78,9 +78,9 @@ int qemuDomainChangeNetLinkState(virQEMUDriverPtr driver, int qemuDomainDetachDeviceDiskLive(virQEMUDriverPtr driver, virDomainObjPtr vm, virDomainDeviceDefPtr dev); -int qemuDomainDetachPciControllerDevice(virQEMUDriverPtr driver, - virDomainObjPtr vm, - virDomainDeviceDefPtr dev); +int qemuDomainDetachControllerDevice(virQEMUDriverPtr driver, + virDomainObjPtr vm, + virDomainDeviceDefPtr dev); int qemuDomainDetachNetDevice(virQEMUDriverPtr driver, virDomainObjPtr vm, virDomainDeviceDefPtr dev); -- 1.7.9.5

For systems without a PCI bus, attaching a SCSI controller fails: [root@xxxxxxxx ~]# cat controller.xml <controller type='scsi' model='virtio-scsi' index='0' /> [root@xxxxxxxx ~]# virsh attach-device guest01 controller.xml error: Failed to attach device from controller.xml error: XML error: No PCI buses available A similar problem occurs with the detach of a controller: [root@xxxxxxxx ~]# virsh detach-device guest01 controller.xml error: Failed to detach device from controller.xml error: operation failed: controller scsi:0 not found The qemuDomainXXtachPciControllerDevice routines made assumptions that any caller had a PCI bus. These routines now selectively calls PCI functions where necessary, and assigns the device information type to one appropriate for the bus in use. Signed-off-by: Eric Farman <farman@linux.vnet.ibm.com> --- src/qemu/qemu_hotplug.c | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index c81d0f3..99496e5 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -366,8 +366,22 @@ int qemuDomainAttachControllerDevice(virQEMUDriverPtr driver, } if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_DEVICE)) { - if (qemuDomainPCIAddressEnsureAddr(priv->pciaddrs, &controller->info) < 0) - goto cleanup; + if (controller->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) { + if (STRPREFIX(vm->def->os.machine, "s390-ccw") && + virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_VIRTIO_CCW)) + controller->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW; + else if (virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_VIRTIO_S390)) + controller->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_S390; + } + + if (controller->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE || + controller->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) { + if (qemuDomainPCIAddressEnsureAddr(priv->pciaddrs, &controller->info) < 0) + goto cleanup; + } else if (controller->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW) { + if (qemuDomainCCWAddressAssign(&controller->info, priv->ccwaddrs, true) < 0) + goto cleanup; + } releaseaddr = true; if (qemuAssignDeviceControllerAlias(controller) < 0) goto cleanup; @@ -399,7 +413,8 @@ int qemuDomainAttachControllerDevice(virQEMUDriverPtr driver, qemuDomainObjExitMonitor(driver, vm); if (ret == 0) { - controller->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI; + if (controller->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE) + controller->info.type = VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI; virDomainControllerInsertPreAlloced(vm->def, controller); } @@ -3049,14 +3064,24 @@ int qemuDomainDetachControllerDevice(virQEMUDriverPtr driver, detach = vm->def->controllers[idx]; - if (!virDomainDeviceAddressIsValid(&detach->info, - VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI)) { - virReportError(VIR_ERR_OPERATION_FAILED, "%s", - _("device cannot be detached without a PCI address")); + if (detach->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI && + detach->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW && + detach->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_VIRTIO_S390) { + virReportError(VIR_ERR_OPERATION_FAILED, + _("device with '%s' address cannot be detached"), + virDomainDeviceAddressTypeToString(detach->info.type)); goto cleanup; } - if (qemuIsMultiFunctionDevice(vm->def, &detach->info)) { + if (!virDomainDeviceAddressIsValid(&detach->info, detach->info.type)) { + virReportError(VIR_ERR_OPERATION_FAILED, + _("device with invalid '%s' address cannot be detached"), + virDomainDeviceAddressTypeToString(detach->info.type)); + goto cleanup; + } + + if (detach->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI && + qemuIsMultiFunctionDevice(vm->def, &detach->info)) { virReportError(VIR_ERR_OPERATION_FAILED, _("cannot hot unplug multifunction PCI device: %s"), dev->data.disk->dst); -- 1.7.9.5

On 11/21/2013 04:36 AM, Eric Farman wrote:
For systems without a PCI bus, attaching a SCSI controller fails:
[root@xxxxxxxx ~]# cat controller.xml <controller type='scsi' model='virtio-scsi' index='0' /> [root@xxxxxxxx ~]# virsh attach-device guest01 controller.xml error: Failed to attach device from controller.xml error: XML error: No PCI buses available
A similar problem occurs with the detach of a controller:
[root@xxxxxxxx ~]# virsh detach-device guest01 controller.xml error: Failed to detach device from controller.xml error: operation failed: controller scsi:0 not found
The qemuDomainXXtachPciControllerDevice routines made assumptions that any caller had a PCI bus. These routines now selectively calls PCI functions where necessary, and assigns the device information type to one appropriate for the bus in use.
Signed-off-by: Eric Farman <farman@linux.vnet.ibm.com> --- src/qemu/qemu_hotplug.c | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-)
ACK and I've pushed the series with the following changes:
+ + if (controller->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE || + controller->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) { + if (qemuDomainPCIAddressEnsureAddr(priv->pciaddrs, &controller->info) < 0) + goto cleanup; + } else if (controller->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW) {
+ if (qemuDomainCCWAddressAssign(&controller->info, priv->ccwaddrs, true) < 0)
I have changed this to: if (qemuDomainCCWAddressAssign(&controller->info, priv->ccwaddrs, !controller->info.addr.ccw.assigned) < 0) to prevent overwriting the user-requested address and match the other CCWAddressAssign calls
+ goto cleanup; + } releaseaddr = true; if (qemuAssignDeviceControllerAlias(controller) < 0) goto cleanup;
+ } + + if (detach->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI &&
s/!=/==/
+ qemuIsMultiFunctionDevice(vm->def, &detach->info)) { virReportError(VIR_ERR_OPERATION_FAILED, _("cannot hot unplug multifunction PCI device: %s"), dev->data.disk->dst);
Jan

If a SCSI hostdev is included in an initial domain XML, without a corresponding controller statement, one is created silently when the guest is booted. When hotplugging a SCSI hostdev, a presumption is that the controller is already present in the domain either from the original XML, or via an earlier hotplug. [root@xxxxxxxx ~]# cat disk.xml <hostdev mode='subsystem' type='scsi'> <source> <adapter name='scsi_host0'/> <address bus='0' target='3' unit='1088438288'/> </source> </hostdev> [root@xxxxxxxx ~]# virsh attach-device guest01 disk.xml error: Failed to attach device from disk.xml error: internal error: unable to execute QEMU command 'device_add': Bus 'scsi0.0' not found Since the infrastructure is in place, we can also create a controller silently for use by the hotplugged hostdev device. Signed-off-by: Eric Farman <farman@linux.vnet.ibm.com> --- src/qemu/qemu_hotplug.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/qemu/qemu_hotplug.c b/src/qemu/qemu_hotplug.c index 99496e5..5f54c44 100644 --- a/src/qemu/qemu_hotplug.c +++ b/src/qemu/qemu_hotplug.c @@ -1508,6 +1508,7 @@ qemuDomainAttachHostScsiDevice(virQEMUDriverPtr driver, { int ret = -1; qemuDomainObjPrivatePtr priv = vm->privateData; + virDomainControllerDefPtr cont = NULL; char *devstr = NULL; char *drvstr = NULL; bool teardowncgroup = false; @@ -1520,6 +1521,10 @@ qemuDomainAttachHostScsiDevice(virQEMUDriverPtr driver, return -1; } + cont = qemuDomainFindOrCreateSCSIDiskController(driver, vm, hostdev->info->addr.drive.controller); + if (!cont) + return -1; + if (qemuPrepareHostdevSCSIDevices(driver, vm->def->name, &hostdev, 1)) { virReportError(VIR_ERR_INTERNAL_ERROR, -- 1.7.9.5
participants (2)
-
Eric Farman
-
Ján Tomko