virhostdev: introduce virHostdevReattachAllPCIDevices

This code that executes virPCIDeviceReattach in all
virPCIDevicePtr objects of a given virPCIDeviceListPtr
list is replicated twice in the code. Putting it in a helper
function helps with readability.

Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
This commit is contained in:
Daniel Henrique Barboza 2019-07-23 14:35:41 -03:00 committed by Michal Privoznik
parent 6b7f87d7f1
commit a12acec66a

View File

@ -638,6 +638,37 @@ virHostdevResetAllPCIDevices(virHostdevManagerPtr mgr,
return ret;
}
static void
virHostdevReattachAllPCIDevices(virHostdevManagerPtr mgr,
virPCIDeviceListPtr pcidevs)
{
size_t i;
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
virPCIDevicePtr pci = virPCIDeviceListGet(pcidevs, i);
virPCIDevicePtr actual;
/* We need to look up the actual device because that's what
* virPCIDeviceReattach() expects as its argument */
if (!(actual = virPCIDeviceListFind(mgr->inactivePCIHostdevs, pci)))
continue;
if (virPCIDeviceGetManaged(actual)) {
VIR_DEBUG("Reattaching managed PCI device %s",
virPCIDeviceGetName(pci));
if (virPCIDeviceReattach(actual,
mgr->activePCIHostdevs,
mgr->inactivePCIHostdevs) < 0) {
VIR_ERROR(_("Failed to re-attach PCI device: %s"),
virGetLastErrorMessage());
}
} else {
VIR_DEBUG("Not reattaching unmanaged PCI device %s",
virPCIDeviceGetName(actual));
}
}
}
int
virHostdevPreparePCIDevices(virHostdevManagerPtr mgr,
const char *drv_name,
@ -898,26 +929,7 @@ virHostdevPreparePCIDevices(virHostdevManagerPtr mgr,
}
reattachdevs:
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
virPCIDevicePtr pci = virPCIDeviceListGet(pcidevs, i);
virPCIDevicePtr actual;
/* We need to look up the actual device because that's what
* virPCIDeviceReattach() expects as its argument */
if (!(actual = virPCIDeviceListFind(mgr->inactivePCIHostdevs, pci)))
continue;
if (virPCIDeviceGetManaged(actual)) {
VIR_DEBUG("Reattaching managed PCI device %s",
virPCIDeviceGetName(pci));
ignore_value(virPCIDeviceReattach(actual,
mgr->activePCIHostdevs,
mgr->inactivePCIHostdevs));
} else {
VIR_DEBUG("Not reattaching unmanaged PCI device %s",
virPCIDeviceGetName(pci));
}
}
virHostdevReattachAllPCIDevices(mgr, pcidevs);
cleanup:
virObjectUnlock(mgr->activePCIHostdevs);
@ -1039,28 +1051,7 @@ virHostdevReAttachPCIDevices(virHostdevManagerPtr mgr,
/* Step 5: Reattach managed devices to their host drivers; unmanaged
* devices don't need to be processed further */
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
virPCIDevicePtr pci = virPCIDeviceListGet(pcidevs, i);
virPCIDevicePtr actual;
/* We need to look up the actual device because that's what
* virPCIDeviceReattach() expects as its argument */
if (!(actual = virPCIDeviceListFind(mgr->inactivePCIHostdevs, pci)))
continue;
if (virPCIDeviceGetManaged(actual)) {
if (virPCIDeviceReattach(actual,
mgr->activePCIHostdevs,
mgr->inactivePCIHostdevs) < 0) {
VIR_ERROR(_("Failed to re-attach PCI device: %s"),
virGetLastErrorMessage());
virResetLastError();
}
}
else
VIR_DEBUG("Not reattaching unmanaged PCI device %s",
virPCIDeviceGetName(actual));
}
virHostdevReattachAllPCIDevices(mgr, pcidevs);
virObjectUnlock(mgr->activePCIHostdevs);
virObjectUnlock(mgr->inactivePCIHostdevs);