提交 aed9bcd1 编写于 作者: D Daniel Henrique Barboza 提交者: Michal Privoznik

qemu_command: tidy up qemuBuildHostdevCommandLine loop

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: NJán Tomko <jtomko@redhat.com>
Signed-off-by: NDaniel Henrique Barboza <danielhb413@gmail.com>
Signed-off-by: NMichal Privoznik <mprivozn@redhat.com>
Reviewed-by: NMichal Privoznik <mprivozn@redhat.com>
上级 2e07a1e1
...@@ -5309,24 +5309,30 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd, ...@@ -5309,24 +5309,30 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd,
for (i = 0; i < def->nhostdevs; i++) { for (i = 0; i < def->nhostdevs; i++) {
virDomainHostdevDefPtr hostdev = def->hostdevs[i]; virDomainHostdevDefPtr hostdev = def->hostdevs[i];
virDomainHostdevSubsysPtr subsys = &hostdev->source.subsys; 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 *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)
if (hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS && continue;
subsys->type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB) {
switch ((virDomainHostdevSubsysType) subsys->type) {
/* USB */
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB:
virCommandAddArg(cmd, "-device"); virCommandAddArg(cmd, "-device");
if (!(devstr = if (!(devstr =
qemuBuildUSBHostdevDevStr(def, hostdev, qemuCaps))) qemuBuildUSBHostdevDevStr(def, hostdev, qemuCaps)))
return -1; return -1;
virCommandAddArg(cmd, devstr); virCommandAddArg(cmd, devstr);
}
/* PCI */ break;
if (hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
subsys->type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI) {
unsigned int bootIndex = hostdev->info->bootIndex;
/* PCI */
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI:
/* bootNet will be non-0 if boot order was set and no other /* bootNet will be non-0 if boot order was set and no other
* net devices were encountered * net devices were encountered
*/ */
...@@ -5343,14 +5349,11 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd, ...@@ -5343,14 +5349,11 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd,
if (!devstr) if (!devstr)
return -1; return -1;
virCommandAddArg(cmd, devstr); virCommandAddArg(cmd, devstr);
}
/* SCSI */ break;
if (virHostdevIsSCSIDevice(hostdev)) {
virDomainHostdevSubsysSCSIPtr scsisrc =
&hostdev->source.subsys.u.scsi;
g_autofree char *drvstr = NULL;
/* SCSI */
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI:
if (scsisrc->protocol == VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI) { if (scsisrc->protocol == VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI) {
virDomainHostdevSubsysSCSIiSCSIPtr iscsisrc = virDomainHostdevSubsysSCSIiSCSIPtr iscsisrc =
&scsisrc->u.iscsi; &scsisrc->u.iscsi;
...@@ -5372,15 +5375,13 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd, ...@@ -5372,15 +5375,13 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd,
if (!(devstr = qemuBuildSCSIHostdevDevStr(def, hostdev))) if (!(devstr = qemuBuildSCSIHostdevDevStr(def, hostdev)))
return -1; return -1;
virCommandAddArg(cmd, devstr); virCommandAddArg(cmd, devstr);
}
break;
/* SCSI_host */ /* SCSI_host */
if (hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS && case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST:
subsys->type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST) {
if (hostdev->source.subsys.u.scsi_host.protocol == if (hostdev->source.subsys.u.scsi_host.protocol ==
VIR_DOMAIN_HOSTDEV_SUBSYS_SCSI_HOST_PROTOCOL_TYPE_VHOST) { VIR_DOMAIN_HOSTDEV_SUBSYS_SCSI_HOST_PROTOCOL_TYPE_VHOST) {
g_autofree char *vhostfdName = NULL;
int vhostfd = -1;
if (virSCSIVHostOpenVhostSCSI(&vhostfd) < 0) if (virSCSIVHostOpenVhostSCSI(&vhostfd) < 0)
return -1; return -1;
...@@ -5399,12 +5400,11 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd, ...@@ -5399,12 +5400,11 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd,
virCommandAddArg(cmd, devstr); virCommandAddArg(cmd, devstr);
} }
}
/* MDEV */ break;
if (virHostdevIsMdevDevice(hostdev)) {
virDomainHostdevSubsysMediatedDevPtr mdevsrc = &subsys->u.mdev;
/* MDEV */
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_MDEV:
switch ((virMediatedDeviceModelType) mdevsrc->model) { switch ((virMediatedDeviceModelType) mdevsrc->model) {
case VIR_MDEV_MODEL_TYPE_VFIO_PCI: case VIR_MDEV_MODEL_TYPE_VFIO_PCI:
case VIR_MDEV_MODEL_TYPE_VFIO_CCW: case VIR_MDEV_MODEL_TYPE_VFIO_CCW:
...@@ -5422,6 +5422,11 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd, ...@@ -5422,6 +5422,11 @@ qemuBuildHostdevCommandLine(virCommandPtr cmd,
qemuBuildHostdevMediatedDevStr(def, hostdev, qemuCaps))) qemuBuildHostdevMediatedDevStr(def, hostdev, qemuCaps)))
return -1; return -1;
virCommandAddArg(cmd, devstr); virCommandAddArg(cmd, devstr);
break;
case VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_LAST:
break;
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册