qemu: propagate virQEMUDriver object to qemuDomainDeviceCalculatePCIConnectFlags

If libvirtd is running unprivileged, it can open a device's PCI config
data in sysfs, but can only read the first 64 bytes. But as part of
determining whether a device is Express or legacy PCI,
qemuDomainDeviceCalculatePCIConnectFlags() will be updated in a future
patch to call virPCIDeviceIsPCIExpress(), which tries to read beyond
the first 64 bytes of the PCI config data and fails with an error log
if the read is unsuccessful.

In order to avoid creating a parallel "quiet" version of
virPCIDeviceIsPCIExpress(), this patch passes a virQEMUDriverPtr down
through all the call chains that initialize the
qemuDomainFillDevicePCIConnectFlagsIterData, and saves the driver
pointer with the rest of the iterdata so that it can be used by
qemuDomainDeviceCalculatePCIConnectFlags(). This pointer isn't used
yet, but will be used in an upcoming patch (that detects Express vs
legacy PCI for VFIO assigned devices) to examine driver->privileged.
This commit is contained in:
Laine Stump 2016-11-03 16:33:32 -04:00
parent bfdc145153
commit 9b0848d523
6 changed files with 54 additions and 30 deletions

View File

@ -3120,7 +3120,7 @@ qemuDomainDefAssignAddresses(virDomainDef *def,
goto cleanup; goto cleanup;
} }
if (qemuDomainAssignAddresses(def, qemuCaps, NULL, newDomain) < 0) if (qemuDomainAssignAddresses(def, qemuCaps, driver, NULL, newDomain) < 0)
goto cleanup; goto cleanup;
ret = 0; ret = 0;

View File

@ -436,6 +436,7 @@ qemuDomainAssignARMVirtioMMIOAddresses(virDomainDefPtr def,
*/ */
static virDomainPCIConnectFlags static virDomainPCIConnectFlags
qemuDomainDeviceCalculatePCIConnectFlags(virDomainDeviceDefPtr dev, qemuDomainDeviceCalculatePCIConnectFlags(virDomainDeviceDefPtr dev,
virQEMUDriverPtr driver ATTRIBUTE_UNUSED,
virDomainPCIConnectFlags pcieFlags, virDomainPCIConnectFlags pcieFlags,
virDomainPCIConnectFlags virtioFlags) virDomainPCIConnectFlags virtioFlags)
{ {
@ -674,6 +675,7 @@ qemuDomainDeviceCalculatePCIConnectFlags(virDomainDeviceDefPtr dev,
typedef struct { typedef struct {
virDomainPCIConnectFlags virtioFlags; virDomainPCIConnectFlags virtioFlags;
virDomainPCIConnectFlags pcieFlags; virDomainPCIConnectFlags pcieFlags;
virQEMUDriverPtr driver;
} qemuDomainFillDevicePCIConnectFlagsIterData; } qemuDomainFillDevicePCIConnectFlagsIterData;
@ -686,8 +688,12 @@ typedef struct {
static void static void
qemuDomainFillDevicePCIConnectFlagsIterInit(virDomainDefPtr def, qemuDomainFillDevicePCIConnectFlagsIterInit(virDomainDefPtr def,
virQEMUCapsPtr qemuCaps, virQEMUCapsPtr qemuCaps,
virQEMUDriverPtr driver,
qemuDomainFillDevicePCIConnectFlagsIterData *data) qemuDomainFillDevicePCIConnectFlagsIterData *data)
{ {
data->driver = driver;
if (qemuDomainMachineHasPCIeRoot(def)) { if (qemuDomainMachineHasPCIeRoot(def)) {
data->pcieFlags = (VIR_PCI_CONNECT_TYPE_PCIE_DEVICE | data->pcieFlags = (VIR_PCI_CONNECT_TYPE_PCIE_DEVICE |
VIR_PCI_CONNECT_HOTPLUGGABLE); VIR_PCI_CONNECT_HOTPLUGGABLE);
@ -727,7 +733,8 @@ qemuDomainFillDevicePCIConnectFlagsIter(virDomainDefPtr def ATTRIBUTE_UNUSED,
qemuDomainFillDevicePCIConnectFlagsIterData *data = opaque; qemuDomainFillDevicePCIConnectFlagsIterData *data = opaque;
info->pciConnectFlags info->pciConnectFlags
= qemuDomainDeviceCalculatePCIConnectFlags(dev, data->pcieFlags, = qemuDomainDeviceCalculatePCIConnectFlags(dev, data->driver,
data->pcieFlags,
data->virtioFlags); data->virtioFlags);
return 0; return 0;
} }
@ -747,11 +754,12 @@ qemuDomainFillDevicePCIConnectFlagsIter(virDomainDefPtr def ATTRIBUTE_UNUSED,
*/ */
static int static int
qemuDomainFillAllPCIConnectFlags(virDomainDefPtr def, qemuDomainFillAllPCIConnectFlags(virDomainDefPtr def,
virQEMUCapsPtr qemuCaps) virQEMUCapsPtr qemuCaps,
virQEMUDriverPtr driver)
{ {
qemuDomainFillDevicePCIConnectFlagsIterData data; qemuDomainFillDevicePCIConnectFlagsIterData data;
qemuDomainFillDevicePCIConnectFlagsIterInit(def, qemuCaps, &data); qemuDomainFillDevicePCIConnectFlagsIterInit(def, qemuCaps, driver, &data);
return virDomainDeviceInfoIterate(def, return virDomainDeviceInfoIterate(def,
qemuDomainFillDevicePCIConnectFlagsIter, qemuDomainFillDevicePCIConnectFlagsIter,
@ -773,7 +781,8 @@ qemuDomainFillAllPCIConnectFlags(virDomainDefPtr def,
static void static void
qemuDomainFillDevicePCIConnectFlags(virDomainDefPtr def, qemuDomainFillDevicePCIConnectFlags(virDomainDefPtr def,
virDomainDeviceDefPtr dev, virDomainDeviceDefPtr dev,
virQEMUCapsPtr qemuCaps) virQEMUCapsPtr qemuCaps,
virQEMUDriverPtr driver)
{ {
virDomainDeviceInfoPtr info = virDomainDeviceGetInfo(dev); virDomainDeviceInfoPtr info = virDomainDeviceGetInfo(dev);
@ -789,10 +798,11 @@ qemuDomainFillDevicePCIConnectFlags(virDomainDefPtr def,
*/ */
qemuDomainFillDevicePCIConnectFlagsIterData data; qemuDomainFillDevicePCIConnectFlagsIterData data;
qemuDomainFillDevicePCIConnectFlagsIterInit(def, qemuCaps, &data); qemuDomainFillDevicePCIConnectFlagsIterInit(def, qemuCaps, driver, &data);
info->pciConnectFlags info->pciConnectFlags
= qemuDomainDeviceCalculatePCIConnectFlags(dev, data.pcieFlags, = qemuDomainDeviceCalculatePCIConnectFlags(dev, data.driver,
data.pcieFlags,
data.virtioFlags); data.virtioFlags);
} }
} }
@ -1829,6 +1839,7 @@ qemuDomainAddressFindNewBusNr(virDomainDefPtr def)
static int static int
qemuDomainAssignPCIAddresses(virDomainDefPtr def, qemuDomainAssignPCIAddresses(virDomainDefPtr def,
virQEMUCapsPtr qemuCaps, virQEMUCapsPtr qemuCaps,
virQEMUDriverPtr driver,
virDomainObjPtr obj) virDomainObjPtr obj)
{ {
int ret = -1; int ret = -1;
@ -1854,7 +1865,7 @@ qemuDomainAssignPCIAddresses(virDomainDefPtr def,
* of all devices. This will be used to pick an appropriate * of all devices. This will be used to pick an appropriate
* bus when assigning addresses. * bus when assigning addresses.
*/ */
if (qemuDomainFillAllPCIConnectFlags(def, qemuCaps) < 0) if (qemuDomainFillAllPCIConnectFlags(def, qemuCaps, driver) < 0)
goto cleanup; goto cleanup;
if (nbuses > 0 && if (nbuses > 0 &&
@ -1973,7 +1984,7 @@ qemuDomainAssignPCIAddresses(virDomainDefPtr def,
dev.type = VIR_DOMAIN_DEVICE_CONTROLLER; dev.type = VIR_DOMAIN_DEVICE_CONTROLLER;
dev.data.controller = def->controllers[contIndex]; dev.data.controller = def->controllers[contIndex];
/* set connect flags so it will be properly addressed */ /* set connect flags so it will be properly addressed */
qemuDomainFillDevicePCIConnectFlags(def, &dev, qemuCaps); qemuDomainFillDevicePCIConnectFlags(def, &dev, qemuCaps, driver);
if (qemuDomainPCIAddressReserveNextSlot(addrs, if (qemuDomainPCIAddressReserveNextSlot(addrs,
&dev.data.controller->info) < 0) &dev.data.controller->info) < 0)
goto cleanup; goto cleanup;
@ -2343,6 +2354,7 @@ qemuDomainAssignUSBAddresses(virDomainDefPtr def,
int int
qemuDomainAssignAddresses(virDomainDefPtr def, qemuDomainAssignAddresses(virDomainDefPtr def,
virQEMUCapsPtr qemuCaps, virQEMUCapsPtr qemuCaps,
virQEMUDriverPtr driver,
virDomainObjPtr obj, virDomainObjPtr obj,
bool newDomain) bool newDomain)
{ {
@ -2357,7 +2369,7 @@ qemuDomainAssignAddresses(virDomainDefPtr def,
qemuDomainAssignARMVirtioMMIOAddresses(def, qemuCaps); qemuDomainAssignARMVirtioMMIOAddresses(def, qemuCaps);
if (qemuDomainAssignPCIAddresses(def, qemuCaps, obj) < 0) if (qemuDomainAssignPCIAddresses(def, qemuCaps, driver, obj) < 0)
return -1; return -1;
if (qemuDomainAssignUSBAddresses(def, obj, newDomain) < 0) if (qemuDomainAssignUSBAddresses(def, obj, newDomain) < 0)
@ -2386,7 +2398,8 @@ qemuDomainAssignAddresses(virDomainDefPtr def,
*/ */
int int
qemuDomainEnsurePCIAddress(virDomainObjPtr obj, qemuDomainEnsurePCIAddress(virDomainObjPtr obj,
virDomainDeviceDefPtr dev) virDomainDeviceDefPtr dev,
virQEMUDriverPtr driver)
{ {
qemuDomainObjPrivatePtr priv = obj->privateData; qemuDomainObjPrivatePtr priv = obj->privateData;
virDomainDeviceInfoPtr info = virDomainDeviceGetInfo(dev); virDomainDeviceInfoPtr info = virDomainDeviceGetInfo(dev);
@ -2394,7 +2407,7 @@ qemuDomainEnsurePCIAddress(virDomainObjPtr obj,
if (!info) if (!info)
return 0; return 0;
qemuDomainFillDevicePCIConnectFlags(obj->def, dev, priv->qemuCaps); qemuDomainFillDevicePCIConnectFlags(obj->def, dev, priv->qemuCaps, driver);
return virDomainPCIAddressEnsureAddr(priv->pciaddrs, info, return virDomainPCIAddressEnsureAddr(priv->pciaddrs, info,
info->pciConnectFlags); info->pciConnectFlags);

View File

@ -25,6 +25,7 @@
# include "domain_addr.h" # include "domain_addr.h"
# include "domain_conf.h" # include "domain_conf.h"
# include "qemu_conf.h"
# include "qemu_capabilities.h" # include "qemu_capabilities.h"
int qemuDomainSetSCSIControllerModel(const virDomainDef *def, int qemuDomainSetSCSIControllerModel(const virDomainDef *def,
@ -33,13 +34,15 @@ int qemuDomainSetSCSIControllerModel(const virDomainDef *def,
int qemuDomainAssignAddresses(virDomainDefPtr def, int qemuDomainAssignAddresses(virDomainDefPtr def,
virQEMUCapsPtr qemuCaps, virQEMUCapsPtr qemuCaps,
virQEMUDriverPtr driver,
virDomainObjPtr obj, virDomainObjPtr obj,
bool newDomain) bool newDomain)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
int qemuDomainEnsurePCIAddress(virDomainObjPtr obj, int qemuDomainEnsurePCIAddress(virDomainObjPtr obj,
virDomainDeviceDefPtr dev) virDomainDeviceDefPtr dev,
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2); virQEMUDriverPtr driver)
ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_NONNULL(3);
void qemuDomainReleaseDeviceAddress(virDomainObjPtr vm, void qemuDomainReleaseDeviceAddress(virDomainObjPtr vm,
virDomainDeviceInfoPtr info, virDomainDeviceInfoPtr info,

View File

@ -345,7 +345,7 @@ qemuDomainAttachVirtioDiskDevice(virConnectPtr conn,
goto error; goto error;
} else if (!disk->info.type || } else if (!disk->info.type ||
disk->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) { disk->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
if (qemuDomainEnsurePCIAddress(vm, &dev) < 0) if (qemuDomainEnsurePCIAddress(vm, &dev, driver) < 0)
goto error; goto error;
} }
releaseaddr = true; releaseaddr = true;
@ -504,7 +504,7 @@ int qemuDomainAttachControllerDevice(virQEMUDriverPtr driver,
if (controller->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE || if (controller->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE ||
controller->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) { controller->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
if (qemuDomainEnsurePCIAddress(vm, &dev) < 0) if (qemuDomainEnsurePCIAddress(vm, &dev, driver) < 0)
goto cleanup; goto cleanup;
} else if (controller->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW) { } else if (controller->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW) {
if (!(ccwaddrs = qemuDomainCCWAddrSetCreateFromDomain(vm->def))) if (!(ccwaddrs = qemuDomainCCWAddrSetCreateFromDomain(vm->def)))
@ -1137,7 +1137,7 @@ qemuDomainAttachNetDevice(virQEMUDriverPtr driver,
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
_("virtio-s390 net device cannot be hotplugged.")); _("virtio-s390 net device cannot be hotplugged."));
goto cleanup; goto cleanup;
} else if (qemuDomainEnsurePCIAddress(vm, &dev) < 0) { } else if (qemuDomainEnsurePCIAddress(vm, &dev, driver) < 0) {
goto cleanup; goto cleanup;
} }
@ -1441,7 +1441,7 @@ qemuDomainAttachHostPCIDevice(virQEMUDriverPtr driver,
if (qemuAssignDeviceHostdevAlias(vm->def, &hostdev->info->alias, -1) < 0) if (qemuAssignDeviceHostdevAlias(vm->def, &hostdev->info->alias, -1) < 0)
goto error; goto error;
if (qemuDomainEnsurePCIAddress(vm, &dev) < 0) if (qemuDomainEnsurePCIAddress(vm, &dev, driver) < 0)
goto error; goto error;
releaseaddr = true; releaseaddr = true;
if (backend != VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO && if (backend != VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO &&
@ -1773,7 +1773,8 @@ qemuDomainChrRemove(virDomainDefPtr vmdef,
*/ */
static int static int
qemuDomainAttachChrDeviceAssignAddr(virDomainObjPtr vm, qemuDomainAttachChrDeviceAssignAddr(virDomainObjPtr vm,
virDomainChrDefPtr chr) virDomainChrDefPtr chr,
virQEMUDriverPtr driver)
{ {
virDomainDefPtr def = vm->def; virDomainDefPtr def = vm->def;
qemuDomainObjPrivatePtr priv = vm->privateData; qemuDomainObjPrivatePtr priv = vm->privateData;
@ -1787,7 +1788,7 @@ qemuDomainAttachChrDeviceAssignAddr(virDomainObjPtr vm,
} else if (chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL && } else if (chr->deviceType == VIR_DOMAIN_CHR_DEVICE_TYPE_SERIAL &&
chr->targetType == VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_PCI) { chr->targetType == VIR_DOMAIN_CHR_SERIAL_TARGET_TYPE_PCI) {
if (qemuDomainEnsurePCIAddress(vm, &dev) < 0) if (qemuDomainEnsurePCIAddress(vm, &dev, driver) < 0)
return -1; return -1;
return 1; return 1;
@ -1847,7 +1848,7 @@ int qemuDomainAttachChrDevice(virConnectPtr conn,
if (qemuAssignDeviceChrAlias(vmdef, chr, -1) < 0) if (qemuAssignDeviceChrAlias(vmdef, chr, -1) < 0)
goto cleanup; goto cleanup;
if ((rc = qemuDomainAttachChrDeviceAssignAddr(vm, chr)) < 0) if ((rc = qemuDomainAttachChrDeviceAssignAddr(vm, chr, driver)) < 0)
goto cleanup; goto cleanup;
if (rc == 1) if (rc == 1)
need_release = true; need_release = true;
@ -1996,7 +1997,7 @@ qemuDomainAttachRNGDevice(virConnectPtr conn,
if (rng->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE || if (rng->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE ||
rng->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) { rng->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
if (qemuDomainEnsurePCIAddress(vm, &dev) < 0) if (qemuDomainEnsurePCIAddress(vm, &dev, driver) < 0)
goto cleanup; goto cleanup;
} else if (rng->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW) { } else if (rng->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW) {
if (!(ccwaddrs = qemuDomainCCWAddrSetCreateFromDomain(vm->def))) if (!(ccwaddrs = qemuDomainCCWAddrSetCreateFromDomain(vm->def)))
@ -2516,7 +2517,7 @@ qemuDomainAttachSCSIVHostDevice(virQEMUDriverPtr driver,
if (hostdev->info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE || if (hostdev->info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE ||
hostdev->info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) { hostdev->info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
if (qemuDomainEnsurePCIAddress(vm, &dev) < 0) if (qemuDomainEnsurePCIAddress(vm, &dev, driver) < 0)
goto cleanup; goto cleanup;
} else if (hostdev->info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW) { } else if (hostdev->info->type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_CCW) {
if (!(ccwaddrs = qemuDomainCCWAddrSetCreateFromDomain(vm->def))) if (!(ccwaddrs = qemuDomainCCWAddrSetCreateFromDomain(vm->def)))
@ -2665,7 +2666,7 @@ qemuDomainAttachShmemDevice(virQEMUDriverPtr driver,
if ((shmem->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE || if ((shmem->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_NONE ||
shmem->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) && shmem->info.type == VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) &&
(qemuDomainEnsurePCIAddress(vm, &dev) < 0)) (qemuDomainEnsurePCIAddress(vm, &dev, driver) < 0))
return -1; return -1;
if (!(shmstr = qemuBuildShmemDevStr(vm->def, shmem, priv->qemuCaps))) if (!(shmstr = qemuBuildShmemDevStr(vm->def, shmem, priv->qemuCaps)))

View File

@ -3368,8 +3368,10 @@ qemuProcessReconnect(void *opaque)
goto cleanup; goto cleanup;
} }
if ((qemuDomainAssignAddresses(obj->def, priv->qemuCaps, obj, false)) < 0) if ((qemuDomainAssignAddresses(obj->def, priv->qemuCaps,
driver, obj, false)) < 0) {
goto error; goto error;
}
/* if domain requests security driver we haven't loaded, report error, but /* if domain requests security driver we haven't loaded, report error, but
* do not kill the domain * do not kill the domain
@ -5156,9 +5158,10 @@ qemuProcessPrepareDomain(virConnectPtr conn,
* use in hotplug * use in hotplug
*/ */
VIR_DEBUG("Assigning domain PCI addresses"); VIR_DEBUG("Assigning domain PCI addresses");
if ((qemuDomainAssignAddresses(vm->def, priv->qemuCaps, vm, if ((qemuDomainAssignAddresses(vm->def, priv->qemuCaps, driver, vm,
!!(flags & VIR_QEMU_PROCESS_START_NEW))) < 0) !!(flags & VIR_QEMU_PROCESS_START_NEW))) < 0) {
goto cleanup; goto cleanup;
}
if (qemuAssignDeviceAliases(vm->def, priv->qemuCaps) < 0) if (qemuAssignDeviceAliases(vm->def, priv->qemuCaps) < 0)
goto cleanup; goto cleanup;
@ -6355,8 +6358,10 @@ int qemuProcessAttach(virConnectPtr conn ATTRIBUTE_UNUSED,
* use in hotplug * use in hotplug
*/ */
VIR_DEBUG("Assigning domain PCI addresses"); VIR_DEBUG("Assigning domain PCI addresses");
if ((qemuDomainAssignAddresses(vm->def, priv->qemuCaps, vm, false)) < 0) if ((qemuDomainAssignAddresses(vm->def, priv->qemuCaps,
driver, vm, false)) < 0) {
goto error; goto error;
}
if ((timestamp = virTimeStringNow()) == NULL) if ((timestamp = virTimeStringNow()) == NULL)
goto error; goto error;

View File

@ -90,8 +90,10 @@ qemuHotplugCreateObjects(virDomainXMLOptionPtr xmlopt,
VIR_DOMAIN_DEF_PARSE_INACTIVE))) VIR_DOMAIN_DEF_PARSE_INACTIVE)))
goto cleanup; goto cleanup;
if (qemuDomainAssignAddresses((*vm)->def, priv->qemuCaps, *vm, true) < 0) if (qemuDomainAssignAddresses((*vm)->def, priv->qemuCaps,
&driver, *vm, true) < 0) {
goto cleanup; goto cleanup;
}
if (qemuAssignDeviceAliases((*vm)->def, priv->qemuCaps) < 0) if (qemuAssignDeviceAliases((*vm)->def, priv->qemuCaps) < 0)
goto cleanup; goto cleanup;