From e1eae5c430d8a768191d26a78d967daf64427a0a Mon Sep 17 00:00:00 2001 From: Peter Krempa Date: Fri, 26 Mar 2021 14:31:26 +0100 Subject: [PATCH] qemuBuildDeviceAddressPCIStr: Extract PCI bus name lookup MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Split up the bus lookup into a function called 'qemuBuildDeviceAddressPCIGetBus'. Signed-off-by: Peter Krempa Reviewed-by: Ján Tomko --- src/qemu/qemu_command.c | 48 +++++++++++++++++++++++++---------------- 1 file changed, 30 insertions(+), 18 deletions(-) diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 3745a3a776..4f4b24aa66 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -320,10 +320,9 @@ qemuVirCommandGetDevSet(virCommand *cmd, int fd) } -static int -qemuBuildDeviceAddressPCIStr(virBuffer *buf, - const virDomainDef *domainDef, - virDomainDeviceInfo *info) +static char * +qemuBuildDeviceAddressPCIGetBus(const virDomainDef *domainDef, + virDomainDeviceInfo *info) { g_autofree char *devStr = NULL; const char *contAlias = NULL; @@ -332,7 +331,7 @@ qemuBuildDeviceAddressPCIStr(virBuffer *buf, size_t i; if (!(devStr = virPCIDeviceAddressAsString(&info->addr.pci))) - return -1; + return NULL; for (i = 0; i < domainDef->ncontrollers; i++) { virDomainControllerDef *cont = domainDef->controllers[i]; @@ -347,7 +346,7 @@ qemuBuildDeviceAddressPCIStr(virBuffer *buf, virReportError(VIR_ERR_INTERNAL_ERROR, _("Device alias was not set for PCI controller with index '%u' required for device at address '%s'"), info->addr.pci.bus, devStr); - return -1; + return NULL; } if (virDomainDeviceAliasIsUserAlias(contAlias)) { @@ -372,20 +371,33 @@ qemuBuildDeviceAddressPCIStr(virBuffer *buf, virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not find PCI controller with index '%u' required for device at address '%s'"), info->addr.pci.bus, devStr); - return -1; + return NULL; } - if (contIsPHB && contTargetIndex > 0) { - /* The PCI bus created by a spapr-pci-host-bridge device with - * alias 'x' will be called 'x.0' rather than 'x'; however, - * this does not apply to the implicit PHB in a pSeries guest, - * which always has the hardcoded name 'pci.0' */ - virBufferAsprintf(buf, ",bus=%s.0", contAlias); - } else { - /* For all other controllers, the bus name matches the alias - * of the corresponding controller */ - virBufferAsprintf(buf, ",bus=%s", contAlias); - } + /* The PCI bus created by a spapr-pci-host-bridge device with + * alias 'x' will be called 'x.0' rather than 'x'; however, + * this does not apply to the implicit PHB in a pSeries guest, + * which always has the hardcoded name 'pci.0' */ + if (contIsPHB && contTargetIndex > 0) + return g_strdup_printf("%s.0", contAlias); + + /* For all other controllers, the bus name matches the alias + * of the corresponding controller */ + return g_strdup(contAlias); +} + + +static int +qemuBuildDeviceAddressPCIStr(virBuffer *buf, + const virDomainDef *domainDef, + virDomainDeviceInfo *info) +{ + g_autofree char *bus = NULL; + + if (!(bus = qemuBuildDeviceAddressPCIGetBus(domainDef, info))) + return -1; + + virBufferStrcat(buf, ",bus=", bus, NULL); if (info->addr.pci.multi == VIR_TRISTATE_SWITCH_ON) virBufferAddLit(buf, ",multifunction=on");