From aed9bcd11b9e151e4d910de550a379035cf10f85 Mon Sep 17 00:00:00 2001 From: Daniel Henrique Barboza Date: Wed, 18 Dec 2019 06:28:39 -0300 Subject: [PATCH] qemu_command: tidy up qemuBuildHostdevCommandLine loop MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The current 'for' loop with 5 consecutive 'ifs' inside qemuBuildHostdevCommandLine can be a bit smarter: - all 5 'ifs' fails if hostdev->mode is not equal to VIR_DOMAIN_HOSTDEV_MODE_SUBSYS. This check can be moved to the start of the loop, failing to the next element immediately in case it fails; - all 5 'ifs' checks for a specific subsys->type to build the proper command line argument (virHostdevIsSCSIDevice and virHostdevIsMdevDevice do that but within a helper). Problem is that the code will keep checking for matches even if one was already found, and there is no way a hostdev will fit more than one 'if' (i.e. a hostdev can't have 2+ different types). This means that a SUBSYS_TYPE_USB will create its command line argument in the first 'if', then all other conditionals will surely fail but will end up being checked anyway. All of this can be avoided by moving the hostdev->mode comparing to the start of the loop and using a switch statement with subsys->type to execute the proper code for a given hostdev type. Suggested-by: Ján Tomko Signed-off-by: Daniel Henrique Barboza Signed-off-by: Michal Privoznik Reviewed-by: Michal Privoznik --- src/qemu/qemu_command.c | 51 ++++++++++++++++++++++------------------- 1 file changed, 28 insertions(+), 23 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 836057a4ff..0cee8dcd85 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -5309,24 +5309,30 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd, for (i = 0; i < def->nhostdevs; i++) { virDomainHostdevDefPtr hostdev = def->hostdevs[i]; virDomainHostdevSubsysPtr subsys = &hostdev->source.subsys; + virDomainHostdevSubsysSCSIPtr scsisrc = &hostdev->source.subsys.u.scsi; + virDomainHostdevSubsysMediatedDevPtr mdevsrc = &subsys->u.mdev; g_autofree char *devstr = NULL; + g_autofree char *drvstr = NULL; + g_autofree char *vhostfdName = NULL; + unsigned int bootIndex = hostdev->info->bootIndex; + int vhostfd = -1; - /* USB */ - if (hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS && - subsys->type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) { + if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS) + continue; + switch ((virDomainHostdevSubsysType) subsys->type) { + /* USB */ + case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB: virCommandAddArg(cmd, "-device"); if (!(devstr = qemuBuildUSBHostdevDevStr(def, hostdev, qemuCaps))) return -1; virCommandAddArg(cmd, devstr); - } - /* PCI */ - if (hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS && - subsys->type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) { - unsigned int bootIndex = hostdev->info->bootIndex; + break; + /* PCI */ + case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI: /* bootNet will be non-0 if boot order was set and no other * net devices were encountered */ @@ -5343,14 +5349,11 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd, if (!devstr) return -1; virCommandAddArg(cmd, devstr); - } - /* SCSI */ - if (virHostdevIsSCSIDevice(hostdev)) { - virDomainHostdevSubsysSCSIPtr scsisrc = - &hostdev->source.subsys.u.scsi; - g_autofree char *drvstr = NULL; + break; + /* SCSI */ + case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI: if (scsisrc->protocol == VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI) { virDomainHostdevSubsysSCSIiSCSIPtr iscsisrc = &scsisrc->u.iscsi; @@ -5372,15 +5375,13 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd, if (!(devstr = qemuBuildSCSIHostdevDevStr(def, hostdev))) return -1; virCommandAddArg(cmd, devstr); - } + + break; /* SCSI_host */ - if (hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS && - subsys->type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST) { + case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST: if (hostdev->source.subsys.u.scsi_host.protocol == VIR_DOMAIN_HOSTDEV_SUBSYS_SCSI_HOST_PROTOCOL_TYPE_VHOST) { - g_autofree char *vhostfdName = NULL; - int vhostfd = -1; if (virSCSIVHostOpenVhostSCSI(&vhostfd) < 0) return -1; @@ -5399,12 +5400,11 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd, virCommandAddArg(cmd, devstr); } - } - /* MDEV */ - if (virHostdevIsMdevDevice(hostdev)) { - virDomainHostdevSubsysMediatedDevPtr mdevsrc = &subsys->u.mdev; + break; + /* MDEV */ + case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV: switch ((virMediatedDeviceModelType) mdevsrc->model) { case VIR_MDEV_MODEL_TYPE_VFIO_PCI: case VIR_MDEV_MODEL_TYPE_VFIO_CCW: @@ -5422,6 +5422,11 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd, qemuBuildHostdevMediatedDevStr(def, hostdev, qemuCaps))) return -1; virCommandAddArg(cmd, devstr); + + break; + + case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST: + break; } } -- GitLab