2010-12-16 16:10:54 +00:00
|
|
|
/*
|
|
|
|
* qemu_hostdev.c: QEMU hostdev management
|
|
|
|
*
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
* Copyright (C) 2006-2007, 2009-2012 Red Hat, Inc.
|
2010-12-16 16:10:54 +00:00
|
|
|
* Copyright (C) 2006 Daniel P. Berrange
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
* Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include "qemu_hostdev.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "virterror_internal.h"
|
|
|
|
#include "memory.h"
|
|
|
|
#include "pci.h"
|
|
|
|
#include "hostusb.h"
|
2012-03-06 01:12:44 +00:00
|
|
|
#include "virnetdev.h"
|
2010-12-16 16:10:54 +00:00
|
|
|
|
|
|
|
static pciDeviceList *
|
|
|
|
qemuGetPciHostDeviceList(virDomainHostdevDefPtr *hostdevs, int nhostdevs)
|
|
|
|
{
|
|
|
|
pciDeviceList *list;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!(list = pciDeviceListNew()))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0 ; i < nhostdevs ; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
pciDevice *dev;
|
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
|
|
|
continue;
|
|
|
|
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dev = pciGetDevice(hostdev->source.subsys.u.pci.domain,
|
|
|
|
hostdev->source.subsys.u.pci.bus,
|
|
|
|
hostdev->source.subsys.u.pci.slot,
|
|
|
|
hostdev->source.subsys.u.pci.function);
|
|
|
|
if (!dev) {
|
|
|
|
pciDeviceListFree(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pciDeviceListAdd(list, dev) < 0) {
|
|
|
|
pciFreeDevice(dev);
|
|
|
|
pciDeviceListFree(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
pciDeviceSetManaged(dev, hostdev->managed);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2011-10-13 08:30:21 +00:00
|
|
|
static pciDeviceList *
|
|
|
|
qemuGetActivePciHostDeviceList(struct qemud_driver *driver,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
|
|
|
{
|
|
|
|
pciDeviceList *list;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!(list = pciDeviceListNew()))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0 ; i < nhostdevs ; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
pciDevice *dev;
|
|
|
|
pciDevice *activeDev;
|
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
|
|
|
continue;
|
|
|
|
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dev = pciGetDevice(hostdev->source.subsys.u.pci.domain,
|
|
|
|
hostdev->source.subsys.u.pci.bus,
|
|
|
|
hostdev->source.subsys.u.pci.slot,
|
|
|
|
hostdev->source.subsys.u.pci.function);
|
|
|
|
if (!dev) {
|
|
|
|
pciDeviceListFree(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((activeDev = pciDeviceListFind(driver->activePciHostdevs, dev))) {
|
|
|
|
if (pciDeviceListAdd(list, activeDev) < 0) {
|
|
|
|
pciFreeDevice(dev);
|
|
|
|
pciDeviceListFree(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pciFreeDevice(dev);
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2010-12-16 16:10:54 +00:00
|
|
|
|
|
|
|
int qemuUpdateActivePciHostdevs(struct qemud_driver *driver,
|
|
|
|
virDomainDefPtr def)
|
|
|
|
{
|
2011-10-20 09:50:10 +00:00
|
|
|
virDomainHostdevDefPtr hostdev = NULL;
|
|
|
|
int i;
|
2010-12-16 16:10:54 +00:00
|
|
|
|
|
|
|
if (!def->nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
2011-10-20 09:50:10 +00:00
|
|
|
for (i = 0; i < def->nhostdevs; i++) {
|
|
|
|
pciDevice *dev = NULL;
|
|
|
|
hostdev = def->hostdevs[i];
|
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
|
|
|
continue;
|
|
|
|
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dev = pciGetDevice(hostdev->source.subsys.u.pci.domain,
|
|
|
|
hostdev->source.subsys.u.pci.bus,
|
|
|
|
hostdev->source.subsys.u.pci.slot,
|
|
|
|
hostdev->source.subsys.u.pci.function);
|
|
|
|
|
|
|
|
if (!dev)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
pciDeviceSetManaged(dev, hostdev->managed);
|
|
|
|
pciDeviceSetUsedBy(dev, def->name);
|
|
|
|
|
|
|
|
/* Setup the original states for the PCI device */
|
|
|
|
pciDeviceSetUnbindFromStub(dev, hostdev->origstates.states.pci.unbind_from_stub);
|
|
|
|
pciDeviceSetRemoveSlot(dev, hostdev->origstates.states.pci.remove_slot);
|
|
|
|
pciDeviceSetReprobe(dev, hostdev->origstates.states.pci.reprobe);
|
2010-12-16 16:10:54 +00:00
|
|
|
|
|
|
|
if (pciDeviceListAdd(driver->activePciHostdevs, dev) < 0) {
|
|
|
|
pciFreeDevice(dev);
|
2011-10-20 09:50:10 +00:00
|
|
|
return -1;
|
2012-03-26 14:44:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuUpdateActiveUsbHostdevs(struct qemud_driver *driver,
|
|
|
|
virDomainDefPtr def)
|
|
|
|
{
|
|
|
|
virDomainHostdevDefPtr hostdev = NULL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!def->nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
for (i = 0; i < def->nhostdevs; i++) {
|
|
|
|
usbDevice *usb = NULL;
|
|
|
|
hostdev = def->hostdevs[i];
|
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
|
|
|
continue;
|
|
|
|
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
usb = usbGetDevice(hostdev->source.subsys.u.usb.bus,
|
|
|
|
hostdev->source.subsys.u.usb.device);
|
|
|
|
if (!usb) {
|
|
|
|
VIR_WARN("Unable to reattach USB device %03d.%03d on domain %s",
|
|
|
|
hostdev->source.subsys.u.usb.bus,
|
|
|
|
hostdev->source.subsys.u.usb.device,
|
|
|
|
def->name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
usbDeviceSetUsedBy(usb, def->name);
|
|
|
|
|
|
|
|
if (usbDeviceListAdd(driver->activeUsbHostdevs, usb) < 0) {
|
|
|
|
usbFreeDevice(usb);
|
|
|
|
return -1;
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-20 09:50:10 +00:00
|
|
|
return 0;
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
static int
|
|
|
|
qemuDomainHostdevPciSysfsPath(virDomainHostdevDefPtr hostdev, char **sysfs_path)
|
|
|
|
{
|
|
|
|
struct pci_config_address config_address;
|
|
|
|
|
|
|
|
config_address.domain = hostdev->source.subsys.u.pci.domain;
|
|
|
|
config_address.bus = hostdev->source.subsys.u.pci.bus;
|
|
|
|
config_address.slot = hostdev->source.subsys.u.pci.slot;
|
|
|
|
config_address.function = hostdev->source.subsys.u.pci.function;
|
|
|
|
|
|
|
|
return pciConfigAddressToSysfsFile(&config_address, sysfs_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuDomainHostdevIsVirtualFunction(virDomainHostdevDefPtr hostdev)
|
|
|
|
{
|
|
|
|
char *sysfs_path = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (qemuDomainHostdevPciSysfsPath(hostdev, &sysfs_path) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = pciDeviceIsVirtualFunction(sysfs_path);
|
|
|
|
|
|
|
|
VIR_FREE(sysfs_path);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qemuDomainHostdevNetDevice(virDomainHostdevDefPtr hostdev, char **linkdev,
|
|
|
|
int *vf)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
char *sysfs_path = NULL;
|
|
|
|
|
|
|
|
if (qemuDomainHostdevPciSysfsPath(hostdev, &sysfs_path) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (pciDeviceIsVirtualFunction(sysfs_path) == 1) {
|
|
|
|
if (pciDeviceGetVirtualFunctionInfo(sysfs_path, linkdev,
|
|
|
|
vf) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
} else {
|
|
|
|
if (pciDeviceNetName(sysfs_path, linkdev) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
*vf = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(sysfs_path);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qemuDomainHostdevNetConfigVirtPortProfile(const char *linkdev, int vf,
|
|
|
|
virNetDevVPortProfilePtr virtPort,
|
|
|
|
const unsigned char *macaddr,
|
|
|
|
const unsigned char *uuid,
|
|
|
|
int associate)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!virtPort)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch(virtPort->virtPortType) {
|
|
|
|
case VIR_NETDEV_VPORT_PROFILE_NONE:
|
|
|
|
case VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH:
|
|
|
|
case VIR_NETDEV_VPORT_PROFILE_8021QBG:
|
|
|
|
case VIR_NETDEV_VPORT_PROFILE_LAST:
|
|
|
|
qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("virtualport type %s is "
|
|
|
|
"currently not supported on interfaces of type "
|
|
|
|
"hostdev"),
|
|
|
|
virNetDevVPortTypeToString(virtPort->virtPortType));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_NETDEV_VPORT_PROFILE_8021QBH:
|
|
|
|
if (associate)
|
|
|
|
ret = virNetDevVPortProfileAssociate(NULL, virtPort, macaddr,
|
|
|
|
linkdev, vf, uuid,
|
|
|
|
VIR_NETDEV_VPORT_PROFILE_OP_CREATE, false);
|
|
|
|
else
|
|
|
|
ret = virNetDevVPortProfileDisassociate(NULL, virtPort,
|
|
|
|
macaddr, linkdev, vf,
|
|
|
|
VIR_NETDEV_VPORT_PROFILE_OP_DESTROY);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuDomainHostdevNetConfigReplace(virDomainHostdevDefPtr hostdev,
|
|
|
|
const unsigned char *uuid,
|
|
|
|
char *stateDir)
|
|
|
|
{
|
|
|
|
char *linkdev = NULL;
|
|
|
|
virNetDevVPortProfilePtr virtPort;
|
|
|
|
int ret = -1;
|
|
|
|
int vf = -1;
|
|
|
|
int vlanid = -1;
|
|
|
|
int port_profile_associate = 1;
|
|
|
|
int isvf;
|
|
|
|
|
|
|
|
isvf = qemuDomainHostdevIsVirtualFunction(hostdev);
|
|
|
|
if (isvf <= 0) {
|
|
|
|
qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Interface type hostdev is currently supported on"
|
|
|
|
" SR-IOV Virtual Functions only"));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qemuDomainHostdevNetDevice(hostdev, &linkdev, &vf) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
virtPort = virDomainNetGetActualVirtPortProfile(
|
|
|
|
hostdev->parent.data.net);
|
|
|
|
if (virtPort)
|
|
|
|
ret = qemuDomainHostdevNetConfigVirtPortProfile(linkdev, vf,
|
|
|
|
virtPort, hostdev->parent.data.net->mac, uuid,
|
|
|
|
port_profile_associate);
|
|
|
|
else
|
|
|
|
/* Set only mac */
|
|
|
|
ret = virNetDevReplaceNetConfig(linkdev, vf,
|
|
|
|
hostdev->parent.data.net->mac, vlanid,
|
|
|
|
stateDir);
|
|
|
|
VIR_FREE(linkdev);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
qemuDomainHostdevNetConfigRestore(virDomainHostdevDefPtr hostdev,
|
|
|
|
char *stateDir)
|
|
|
|
{
|
|
|
|
char *linkdev = NULL;
|
|
|
|
virNetDevVPortProfilePtr virtPort;
|
|
|
|
int ret = -1;
|
|
|
|
int vf = -1;
|
|
|
|
int port_profile_associate = 0;
|
|
|
|
int isvf;
|
|
|
|
|
|
|
|
isvf = qemuDomainHostdevIsVirtualFunction(hostdev);
|
|
|
|
if (isvf <= 0) {
|
|
|
|
qemuReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("Interface type hostdev is currently supported on"
|
|
|
|
" SR-IOV Virtual Functions only"));
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qemuDomainHostdevNetDevice(hostdev, &linkdev, &vf) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
virtPort = virDomainNetGetActualVirtPortProfile(
|
|
|
|
hostdev->parent.data.net);
|
|
|
|
if (virtPort)
|
|
|
|
ret = qemuDomainHostdevNetConfigVirtPortProfile(linkdev, vf, virtPort,
|
|
|
|
hostdev->parent.data.net->mac, NULL,
|
|
|
|
port_profile_associate);
|
|
|
|
else
|
|
|
|
ret = virNetDevRestoreNetConfig(linkdev, vf, stateDir);
|
|
|
|
|
|
|
|
VIR_FREE(linkdev);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-12-16 16:10:54 +00:00
|
|
|
int qemuPrepareHostdevPCIDevices(struct qemud_driver *driver,
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
const char *name,
|
2012-03-06 01:12:44 +00:00
|
|
|
const unsigned char *uuid,
|
2010-12-16 16:10:54 +00:00
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
|
|
|
{
|
|
|
|
pciDeviceList *pcidevs;
|
2012-03-06 01:12:44 +00:00
|
|
|
int last_processed_hostdev_vf = -1;
|
2010-12-16 16:10:54 +00:00
|
|
|
int i;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
|
|
|
|
return -1;
|
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
/* We have to use 9 loops here. *All* devices must
|
2010-12-16 16:10:54 +00:00
|
|
|
* be detached before we reset any of them, because
|
|
|
|
* in some cases you have to reset the whole PCI,
|
|
|
|
* which impacts all devices on it. Also, all devices
|
|
|
|
* must be reset before being marked as active.
|
|
|
|
*/
|
|
|
|
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
/* Loop 1: validate that non-managed device isn't in use, eg
|
2010-12-16 16:10:54 +00:00
|
|
|
* by checking that device is either un-bound, or bound
|
|
|
|
* to pci-stub.ko
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (i = 0; i < pciDeviceListCount(pcidevs); i++) {
|
|
|
|
pciDevice *dev = pciDeviceListGet(pcidevs, i);
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
pciDevice *other;
|
2010-12-16 16:10:54 +00:00
|
|
|
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
if (!pciDeviceIsAssignable(dev, !driver->relaxedACS)) {
|
|
|
|
qemuReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("PCI device %s is not assignable"),
|
|
|
|
pciDeviceGetName(dev));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
/* The device is in use by other active domain if
|
|
|
|
* the dev is in list driver->activePciHostdevs.
|
|
|
|
*/
|
|
|
|
if ((other = pciDeviceListFind(driver->activePciHostdevs, dev))) {
|
|
|
|
const char *other_name = pciDeviceGetUsedBy(other);
|
|
|
|
|
|
|
|
if (other_name)
|
|
|
|
qemuReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("PCI device %s is in use by domain %s"),
|
|
|
|
pciDeviceGetName(dev), other_name);
|
|
|
|
else
|
|
|
|
qemuReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("PCI device %s is already in use"),
|
|
|
|
pciDeviceGetName(dev));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop 2: detach managed devices */
|
|
|
|
for (i = 0; i < pciDeviceListCount(pcidevs); i++) {
|
|
|
|
pciDevice *dev = pciDeviceListGet(pcidevs, i);
|
2010-12-16 16:10:54 +00:00
|
|
|
if (pciDeviceGetManaged(dev) &&
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
pciDettachDevice(dev, driver->activePciHostdevs, NULL) < 0)
|
2011-03-28 07:01:19 +00:00
|
|
|
goto reattachdevs;
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
/* Loop 3: Now that all the PCI hostdevs have been detached, we
|
|
|
|
* can safely reset them */
|
2010-12-16 16:10:54 +00:00
|
|
|
for (i = 0; i < pciDeviceListCount(pcidevs); i++) {
|
|
|
|
pciDevice *dev = pciDeviceListGet(pcidevs, i);
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
if (pciResetDevice(dev, driver->activePciHostdevs,
|
|
|
|
driver->inactivePciHostdevs) < 0)
|
2011-03-28 07:01:19 +00:00
|
|
|
goto reattachdevs;
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
/* Loop 4: For SRIOV network devices, Now that we have detached the
|
|
|
|
* the network device, set the netdev config */
|
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
|
|
|
continue;
|
|
|
|
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
|
|
|
|
continue;
|
|
|
|
if (hostdev->parent.type == VIR_DOMAIN_DEVICE_NET &&
|
|
|
|
hostdev->parent.data.net) {
|
|
|
|
if (qemuDomainHostdevNetConfigReplace(hostdev, uuid,
|
|
|
|
driver->stateDir) < 0) {
|
|
|
|
goto resetvfnetconfig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_processed_hostdev_vf = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop 5: Now mark all the devices as active */
|
2010-12-16 16:10:54 +00:00
|
|
|
for (i = 0; i < pciDeviceListCount(pcidevs); i++) {
|
|
|
|
pciDevice *dev = pciDeviceListGet(pcidevs, i);
|
|
|
|
if (pciDeviceListAdd(driver->activePciHostdevs, dev) < 0) {
|
|
|
|
pciFreeDevice(dev);
|
2011-03-28 07:01:14 +00:00
|
|
|
goto inactivedevs;
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
/* Loop 6: Now remove the devices from inactive list. */
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
for (i = 0; i < pciDeviceListCount(pcidevs); i++) {
|
|
|
|
pciDevice *dev = pciDeviceListGet(pcidevs, i);
|
|
|
|
pciDeviceListDel(driver->inactivePciHostdevs, dev);
|
|
|
|
}
|
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
/* Loop 7: Now set the used_by_domain of the device in
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
* driver->activePciHostdevs as domain name.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < pciDeviceListCount(pcidevs); i++) {
|
|
|
|
pciDevice *dev, *activeDev;
|
|
|
|
|
|
|
|
dev = pciDeviceListGet(pcidevs, i);
|
|
|
|
activeDev = pciDeviceListFind(driver->activePciHostdevs, dev);
|
|
|
|
|
|
|
|
pciDeviceSetUsedBy(activeDev, name);
|
|
|
|
}
|
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
/* Loop 8: Now set the original states for hostdev def */
|
2011-10-20 09:50:10 +00:00
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
pciDevice *dev;
|
|
|
|
pciDevice *pcidev;
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
|
|
|
continue;
|
|
|
|
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
dev = pciGetDevice(hostdev->source.subsys.u.pci.domain,
|
|
|
|
hostdev->source.subsys.u.pci.bus,
|
|
|
|
hostdev->source.subsys.u.pci.slot,
|
|
|
|
hostdev->source.subsys.u.pci.function);
|
|
|
|
|
|
|
|
/* original states "unbind_from_stub", "remove_slot",
|
|
|
|
* "reprobe" were already set by pciDettachDevice in
|
|
|
|
* loop 2.
|
|
|
|
*/
|
|
|
|
if ((pcidev = pciDeviceListFind(pcidevs, dev))) {
|
|
|
|
hostdev->origstates.states.pci.unbind_from_stub =
|
|
|
|
pciDeviceGetUnbindFromStub(pcidev);
|
|
|
|
hostdev->origstates.states.pci.remove_slot =
|
|
|
|
pciDeviceGetRemoveSlot(pcidev);
|
|
|
|
hostdev->origstates.states.pci.reprobe =
|
|
|
|
pciDeviceGetReprobe(pcidev);
|
|
|
|
}
|
|
|
|
|
|
|
|
pciFreeDevice(dev);
|
|
|
|
}
|
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
/* Loop 9: Now steal all the devices from pcidevs */
|
2011-08-24 15:30:51 +00:00
|
|
|
while (pciDeviceListCount(pcidevs) > 0) {
|
|
|
|
pciDevice *dev = pciDeviceListGet(pcidevs, 0);
|
2011-03-28 07:01:14 +00:00
|
|
|
pciDeviceListSteal(pcidevs, dev);
|
|
|
|
}
|
|
|
|
|
2010-12-16 16:10:54 +00:00
|
|
|
ret = 0;
|
2011-03-28 07:01:14 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
inactivedevs:
|
|
|
|
/* Only steal all the devices from driver->activePciHostdevs. We will
|
|
|
|
* free them in pciDeviceListFree().
|
|
|
|
*/
|
2011-08-24 15:30:51 +00:00
|
|
|
while (pciDeviceListCount(pcidevs) > 0) {
|
|
|
|
pciDevice *dev = pciDeviceListGet(pcidevs, 0);
|
2011-03-28 07:01:14 +00:00
|
|
|
pciDeviceListSteal(driver->activePciHostdevs, dev);
|
|
|
|
}
|
2010-12-16 16:10:54 +00:00
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
resetvfnetconfig:
|
|
|
|
for (i = 0; i < last_processed_hostdev_vf; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
if (hostdev->parent.type == VIR_DOMAIN_DEVICE_NET &&
|
|
|
|
hostdev->parent.data.net) {
|
|
|
|
qemuDomainHostdevNetConfigRestore(hostdev, driver->stateDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-28 07:01:19 +00:00
|
|
|
reattachdevs:
|
|
|
|
for (i = 0; i < pciDeviceListCount(pcidevs); i++) {
|
|
|
|
pciDevice *dev = pciDeviceListGet(pcidevs, i);
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
pciReAttachDevice(dev, driver->activePciHostdevs, NULL);
|
2011-03-28 07:01:19 +00:00
|
|
|
}
|
|
|
|
|
2010-12-16 16:10:54 +00:00
|
|
|
cleanup:
|
|
|
|
pciDeviceListFree(pcidevs);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qemuPrepareHostPCIDevices(struct qemud_driver *driver,
|
|
|
|
virDomainDefPtr def)
|
|
|
|
{
|
2012-03-06 01:12:44 +00:00
|
|
|
return qemuPrepareHostdevPCIDevices(driver, def->name, def->uuid,
|
|
|
|
def->hostdevs, def->nhostdevs);
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
|
2011-12-21 17:58:29 +00:00
|
|
|
int
|
|
|
|
qemuPrepareHostdevUSBDevices(struct qemud_driver *driver,
|
|
|
|
const char *name,
|
2012-05-06 14:45:05 +00:00
|
|
|
usbDeviceList *list)
|
2010-12-16 16:10:54 +00:00
|
|
|
{
|
2012-05-16 14:42:02 +00:00
|
|
|
int i, j;
|
2012-05-06 14:45:05 +00:00
|
|
|
unsigned int count;
|
|
|
|
usbDevice *tmp;
|
|
|
|
|
|
|
|
count = usbDeviceListCount(list);
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
usbDevice *usb = usbDeviceListGet(list, i);
|
|
|
|
if ((tmp = usbDeviceListFind(driver->activeUsbHostdevs, usb))) {
|
|
|
|
const char *other_name = usbDeviceGetUsedBy(tmp);
|
|
|
|
|
|
|
|
if (other_name)
|
|
|
|
qemuReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("USB device %s is in use by domain %s"),
|
|
|
|
usbDeviceGetName(tmp), other_name);
|
|
|
|
else
|
|
|
|
qemuReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("USB device %s is already in use"),
|
|
|
|
usbDeviceGetName(tmp));
|
2012-05-16 14:42:02 +00:00
|
|
|
goto error;
|
2012-05-06 14:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
usbDeviceSetUsedBy(usb, name);
|
|
|
|
VIR_DEBUG("Adding %03d.%03d dom=%s to activeUsbHostdevs",
|
|
|
|
usbDeviceGetBus(usb), usbDeviceGetDevno(usb), name);
|
|
|
|
/*
|
|
|
|
* The caller is responsible to steal these usb devices
|
|
|
|
* from the usbDeviceList that passed in on success,
|
|
|
|
* perform rollback on failure.
|
|
|
|
*/
|
|
|
|
if (usbDeviceListAdd(driver->activeUsbHostdevs, usb) < 0)
|
2012-05-16 14:42:02 +00:00
|
|
|
goto error;
|
2012-05-06 14:45:05 +00:00
|
|
|
}
|
|
|
|
return 0;
|
2012-05-16 14:42:02 +00:00
|
|
|
|
|
|
|
error:
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
tmp = usbDeviceListGet(list, i);
|
|
|
|
usbDeviceListSteal(driver->activeUsbHostdevs, tmp);
|
|
|
|
}
|
|
|
|
return -1;
|
2012-05-06 14:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
qemuPrepareHostUSBDevices(struct qemud_driver *driver,
|
|
|
|
virDomainDefPtr def)
|
|
|
|
{
|
|
|
|
int i, ret = -1;
|
2011-12-21 17:58:29 +00:00
|
|
|
usbDeviceList *list;
|
|
|
|
usbDevice *tmp;
|
2012-05-06 14:45:05 +00:00
|
|
|
virDomainHostdevDefPtr *hostdevs = def->hostdevs;
|
|
|
|
int nhostdevs = def->nhostdevs;
|
2011-12-21 17:58:29 +00:00
|
|
|
|
|
|
|
/* To prevent situation where USB device is assigned to two domains
|
|
|
|
* we need to keep a list of currently assigned USB devices.
|
|
|
|
* This is done in several loops which cannot be joined into one big
|
|
|
|
* loop. See qemuPrepareHostdevPCIDevices()
|
|
|
|
*/
|
|
|
|
if (!(list = usbDeviceListNew()))
|
|
|
|
goto cleanup;
|
|
|
|
|
2012-05-16 14:42:02 +00:00
|
|
|
/* Loop 1: build temporary list
|
2011-12-21 17:58:29 +00:00
|
|
|
*/
|
|
|
|
for (i = 0 ; i < nhostdevs ; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
2012-05-06 14:45:05 +00:00
|
|
|
usbDevice *usb = NULL;
|
2010-12-16 16:10:54 +00:00
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
|
|
|
continue;
|
|
|
|
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
|
|
|
|
continue;
|
|
|
|
|
2012-05-06 14:45:05 +00:00
|
|
|
unsigned vendor = hostdev->source.subsys.u.usb.vendor;
|
|
|
|
unsigned product = hostdev->source.subsys.u.usb.product;
|
|
|
|
unsigned bus = hostdev->source.subsys.u.usb.bus;
|
|
|
|
unsigned device = hostdev->source.subsys.u.usb.device;
|
2010-12-16 16:10:54 +00:00
|
|
|
|
2012-05-06 14:45:05 +00:00
|
|
|
if (vendor && bus) {
|
|
|
|
usb = usbFindDevice(vendor, product, bus, device);
|
2012-05-04 07:49:58 +00:00
|
|
|
|
2012-05-06 14:45:05 +00:00
|
|
|
} else if (vendor && !bus) {
|
|
|
|
usbDeviceList *devs = usbFindDeviceByVendor(vendor, product);
|
2012-05-04 07:49:58 +00:00
|
|
|
if (!devs)
|
2012-03-26 14:33:58 +00:00
|
|
|
goto cleanup;
|
2010-12-16 16:10:54 +00:00
|
|
|
|
2012-05-06 14:45:05 +00:00
|
|
|
if (usbDeviceListCount(devs) > 1) {
|
|
|
|
qemuReportError(VIR_ERR_OPERATION_FAILED,
|
|
|
|
_("multiple USB devices for %x:%x, "
|
|
|
|
"use <address> to specify one"), vendor, product);
|
|
|
|
usbDeviceListFree(devs);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-05-04 07:49:58 +00:00
|
|
|
usb = usbDeviceListGet(devs, 0);
|
|
|
|
usbDeviceListSteal(devs, usb);
|
|
|
|
usbDeviceListFree(devs);
|
|
|
|
|
2012-03-26 14:33:58 +00:00
|
|
|
hostdev->source.subsys.u.usb.bus = usbDeviceGetBus(usb);
|
|
|
|
hostdev->source.subsys.u.usb.device = usbDeviceGetDevno(usb);
|
|
|
|
|
2012-05-06 14:45:05 +00:00
|
|
|
} else if (!vendor && bus) {
|
|
|
|
usb = usbFindDeviceByBus(bus, device);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!usb)
|
|
|
|
goto cleanup;
|
2011-12-21 17:58:29 +00:00
|
|
|
|
2012-05-06 14:45:05 +00:00
|
|
|
if (usbDeviceListAdd(list, usb) < 0) {
|
|
|
|
usbFreeDevice(usb);
|
|
|
|
goto cleanup;
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-06 14:45:05 +00:00
|
|
|
/* Mark devices in temporary list as used by @name
|
2011-12-21 17:58:29 +00:00
|
|
|
* and add them do driver list. However, if something goes
|
|
|
|
* wrong, perform rollback.
|
|
|
|
*/
|
2012-05-06 14:45:05 +00:00
|
|
|
if (qemuPrepareHostdevUSBDevices(driver, def->name, list) < 0)
|
2012-05-16 14:42:02 +00:00
|
|
|
goto cleanup;
|
2011-12-21 17:58:29 +00:00
|
|
|
|
2012-05-06 14:45:05 +00:00
|
|
|
/* Loop 2: Temporary list was successfully merged with
|
2011-12-21 17:58:29 +00:00
|
|
|
* driver list, so steal all items to avoid freeing them
|
|
|
|
* in cleanup label.
|
|
|
|
*/
|
|
|
|
while (usbDeviceListCount(list) > 0) {
|
|
|
|
tmp = usbDeviceListGet(list, 0);
|
|
|
|
usbDeviceListSteal(list, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
usbDeviceListFree(list);
|
|
|
|
return ret;
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int qemuPrepareHostDevices(struct qemud_driver *driver,
|
|
|
|
virDomainDefPtr def)
|
|
|
|
{
|
|
|
|
if (!def->nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (qemuPrepareHostPCIDevices(driver, def) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (qemuPrepareHostUSBDevices(driver, def) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void qemuReattachPciDevice(pciDevice *dev, struct qemud_driver *driver)
|
|
|
|
{
|
|
|
|
int retries = 100;
|
|
|
|
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
/* If the device is not managed and was attached to guest
|
|
|
|
* successfully, it must have been inactive.
|
|
|
|
*/
|
|
|
|
if (!pciDeviceGetManaged(dev)) {
|
|
|
|
pciDeviceListAdd(driver->inactivePciHostdevs, dev);
|
2011-10-17 10:19:58 +00:00
|
|
|
return;
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
}
|
2011-10-17 10:19:58 +00:00
|
|
|
|
2010-12-16 16:10:54 +00:00
|
|
|
while (pciWaitForDeviceCleanup(dev, "kvm_assigned_device")
|
|
|
|
&& retries) {
|
|
|
|
usleep(100*1000);
|
|
|
|
retries--;
|
|
|
|
}
|
|
|
|
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
if (pciReAttachDevice(dev, driver->activePciHostdevs,
|
|
|
|
driver->inactivePciHostdevs) < 0) {
|
2011-10-17 10:19:58 +00:00
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
VIR_ERROR(_("Failed to re-attach PCI device: %s"),
|
|
|
|
err ? err->message : _("unknown error"));
|
|
|
|
virResetError(err);
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void qemuDomainReAttachHostdevDevices(struct qemud_driver *driver,
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
const char *name,
|
2010-12-16 16:10:54 +00:00
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
|
|
|
{
|
|
|
|
pciDeviceList *pcidevs;
|
|
|
|
int i;
|
|
|
|
|
2011-10-13 08:30:21 +00:00
|
|
|
if (!(pcidevs = qemuGetActivePciHostDeviceList(driver,
|
|
|
|
hostdevs,
|
|
|
|
nhostdevs))) {
|
2010-12-16 16:10:54 +00:00
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
VIR_ERROR(_("Failed to allocate pciDeviceList: %s"),
|
|
|
|
err ? err->message : _("unknown error"));
|
|
|
|
virResetError(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
/* Again 4 loops; mark all devices as inactive before reset
|
|
|
|
* them and reset all the devices before re-attach.
|
|
|
|
* Attach mac and port profile parameters to devices
|
|
|
|
*/
|
2010-12-16 16:10:54 +00:00
|
|
|
for (i = 0; i < pciDeviceListCount(pcidevs); i++) {
|
|
|
|
pciDevice *dev = pciDeviceListGet(pcidevs, i);
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
pciDevice *activeDev = NULL;
|
|
|
|
|
|
|
|
/* Never delete the dev from list driver->activePciHostdevs
|
|
|
|
* if it's used by other domain.
|
|
|
|
*/
|
|
|
|
activeDev = pciDeviceListFind(driver->activePciHostdevs, dev);
|
|
|
|
if (activeDev &&
|
2011-11-29 10:23:06 +00:00
|
|
|
STRNEQ_NULLABLE(name, pciDeviceGetUsedBy(activeDev))) {
|
|
|
|
pciDeviceListSteal(pcidevs, dev);
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
continue;
|
2011-11-29 10:23:06 +00:00
|
|
|
}
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
|
2011-10-13 08:30:21 +00:00
|
|
|
/* pciDeviceListFree() will take care of freeing the dev. */
|
|
|
|
pciDeviceListSteal(driver->activePciHostdevs, dev);
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
/*
|
|
|
|
* For SRIOV net host devices, unset mac and port profile before
|
|
|
|
* reset and reattach device
|
|
|
|
*/
|
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
|
|
|
continue;
|
|
|
|
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
|
|
|
|
continue;
|
|
|
|
if (hostdev->parent.type == VIR_DOMAIN_DEVICE_NET &&
|
|
|
|
hostdev->parent.data.net) {
|
|
|
|
qemuDomainHostdevNetConfigRestore(hostdev, driver->stateDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-16 16:10:54 +00:00
|
|
|
for (i = 0; i < pciDeviceListCount(pcidevs); i++) {
|
|
|
|
pciDevice *dev = pciDeviceListGet(pcidevs, i);
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
if (pciResetDevice(dev, driver->activePciHostdevs,
|
|
|
|
driver->inactivePciHostdevs) < 0) {
|
2010-12-16 16:10:54 +00:00
|
|
|
virErrorPtr err = virGetLastError();
|
|
|
|
VIR_ERROR(_("Failed to reset PCI device: %s"),
|
|
|
|
err ? err->message : _("unknown error"));
|
|
|
|
virResetError(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < pciDeviceListCount(pcidevs); i++) {
|
|
|
|
pciDevice *dev = pciDeviceListGet(pcidevs, i);
|
|
|
|
qemuReattachPciDevice(dev, driver);
|
|
|
|
}
|
|
|
|
|
|
|
|
pciDeviceListFree(pcidevs);
|
|
|
|
}
|
|
|
|
|
2012-03-26 14:40:01 +00:00
|
|
|
static void
|
|
|
|
qemuDomainReAttachHostUsbDevices(struct qemud_driver *driver,
|
|
|
|
const char *name,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
usbDevice *usb, *tmp;
|
|
|
|
const char *used_by = NULL;
|
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
|
|
|
continue;
|
|
|
|
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
usb = usbGetDevice(hostdev->source.subsys.u.usb.bus,
|
|
|
|
hostdev->source.subsys.u.usb.device);
|
|
|
|
|
|
|
|
if (!usb) {
|
|
|
|
VIR_WARN("Unable to reattach USB device %03d.%03d on domain %s",
|
|
|
|
hostdev->source.subsys.u.usb.bus,
|
|
|
|
hostdev->source.subsys.u.usb.device,
|
|
|
|
name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Delete only those USB devices which belongs
|
|
|
|
* to domain @name because qemuProcessStart() might
|
|
|
|
* have failed because USB device is already taken.
|
|
|
|
* Therefore we want to steal only those devices from
|
|
|
|
* the list which were taken by @name */
|
|
|
|
|
|
|
|
tmp = usbDeviceListFind(driver->activeUsbHostdevs, usb);
|
|
|
|
usbFreeDevice(usb);
|
|
|
|
|
|
|
|
if (!tmp) {
|
|
|
|
VIR_WARN("Unable to find device %03d.%03d "
|
|
|
|
"in list of active USB devices",
|
|
|
|
hostdev->source.subsys.u.usb.bus,
|
|
|
|
hostdev->source.subsys.u.usb.device);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
used_by = usbDeviceGetUsedBy(tmp);
|
|
|
|
if (STREQ_NULLABLE(used_by, name)) {
|
|
|
|
VIR_DEBUG("Removing %03d.%03d dom=%s from activeUsbHostdevs",
|
|
|
|
hostdev->source.subsys.u.usb.bus,
|
|
|
|
hostdev->source.subsys.u.usb.device,
|
|
|
|
name);
|
|
|
|
|
|
|
|
usbDeviceListDel(driver->activeUsbHostdevs, tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-12-16 16:10:54 +00:00
|
|
|
|
|
|
|
void qemuDomainReAttachHostDevices(struct qemud_driver *driver,
|
|
|
|
virDomainDefPtr def)
|
|
|
|
{
|
|
|
|
if (!def->nhostdevs)
|
|
|
|
return;
|
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
qemuDomainReAttachHostdevDevices(driver, def->name, def->hostdevs,
|
|
|
|
def->nhostdevs);
|
2012-03-26 14:40:01 +00:00
|
|
|
|
|
|
|
qemuDomainReAttachHostUsbDevices(driver, def->name, def->hostdevs,
|
|
|
|
def->nhostdevs);
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|