2010-12-16 16:10:54 +00:00
|
|
|
/*
|
|
|
|
* qemu_hostdev.c: QEMU hostdev management
|
|
|
|
*
|
2013-03-18 19:56:12 +00:00
|
|
|
* Copyright (C) 2006-2007, 2009-2013 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
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2010-12-16 16:10:54 +00:00
|
|
|
*
|
|
|
|
* Author: Daniel P. Berrange <berrange@redhat.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2013-09-19 15:01:17 +00:00
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
|
2010-12-16 16:10:54 +00:00
|
|
|
#include "qemu_hostdev.h"
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2012-12-12 18:06:53 +00:00
|
|
|
#include "viralloc.h"
|
2012-12-13 14:52:25 +00:00
|
|
|
#include "virpci.h"
|
2012-12-12 17:04:51 +00:00
|
|
|
#include "virusb.h"
|
2013-05-03 18:07:29 +00:00
|
|
|
#include "virscsi.h"
|
2012-03-06 01:12:44 +00:00
|
|
|
#include "virnetdev.h"
|
2013-09-19 15:01:17 +00:00
|
|
|
#include "virfile.h"
|
2010-12-16 16:10:54 +00:00
|
|
|
|
2012-07-18 15:22:03 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_QEMU
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
static virPCIDeviceListPtr
|
2010-12-16 16:10:54 +00:00
|
|
|
qemuGetPciHostDeviceList(virDomainHostdevDefPtr *hostdevs, int nhostdevs)
|
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceListPtr list;
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2010-12-16 16:10:54 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (!(list = virPCIDeviceListNew()))
|
2010-12-16 16:10:54 +00:00
|
|
|
return NULL;
|
|
|
|
|
2013-05-21 07:21:20 +00:00
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
2010-12-16 16:10:54 +00:00
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr dev;
|
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_PCI)
|
|
|
|
continue;
|
|
|
|
|
2013-03-18 19:56:12 +00:00
|
|
|
dev = virPCIDeviceNew(hostdev->source.subsys.u.pci.addr.domain,
|
|
|
|
hostdev->source.subsys.u.pci.addr.bus,
|
|
|
|
hostdev->source.subsys.u.pci.addr.slot,
|
|
|
|
hostdev->source.subsys.u.pci.addr.function);
|
2010-12-16 16:10:54 +00:00
|
|
|
if (!dev) {
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectUnref(list);
|
2010-12-16 16:10:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceListAdd(list, dev) < 0) {
|
|
|
|
virPCIDeviceFree(dev);
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectUnref(list);
|
2010-12-16 16:10:54 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceSetManaged(dev, hostdev->managed);
|
2013-04-23 18:53:36 +00:00
|
|
|
if (hostdev->source.subsys.u.pci.backend
|
2013-04-26 20:44:05 +00:00
|
|
|
== VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
|
2013-05-31 18:26:56 +00:00
|
|
|
if (virPCIDeviceSetStubDriver(dev, "vfio-pci") < 0) {
|
|
|
|
virObjectUnref(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-04-23 18:53:36 +00:00
|
|
|
} else {
|
2013-05-31 18:26:56 +00:00
|
|
|
if (virPCIDeviceSetStubDriver(dev, "pci-stub") < 0) {
|
|
|
|
virObjectUnref(list);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-04-23 18:53:36 +00:00
|
|
|
}
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
2013-01-16 12:09:58 +00:00
|
|
|
/*
|
2013-06-25 02:04:43 +00:00
|
|
|
* qemuGetActivePciHostDeviceList - make a new list with a *copy* of
|
|
|
|
* every virPCIDevice object that is found on the activePciHostdevs
|
|
|
|
* list *and* is in the hostdev list for this domain.
|
|
|
|
*
|
|
|
|
* Return the new list, or NULL if there was a failure.
|
|
|
|
*
|
2013-01-16 12:09:58 +00:00
|
|
|
* Pre-condition: driver->activePciHostdevs is locked
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
static virPCIDeviceListPtr
|
2012-11-28 16:43:10 +00:00
|
|
|
qemuGetActivePciHostDeviceList(virQEMUDriverPtr driver,
|
2011-10-13 08:30:21 +00:00
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceListPtr list;
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2011-10-13 08:30:21 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (!(list = virPCIDeviceListNew()))
|
2011-10-13 08:30:21 +00:00
|
|
|
return NULL;
|
|
|
|
|
2013-05-21 07:21:20 +00:00
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
2011-10-13 08:30:21 +00:00
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
2013-06-25 02:04:43 +00:00
|
|
|
virDevicePCIAddressPtr addr;
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr activeDev;
|
2011-10-13 08:30:21 +00:00
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
|
|
|
continue;
|
|
|
|
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
|
|
|
|
continue;
|
|
|
|
|
2013-06-25 02:04:43 +00:00
|
|
|
addr = &hostdev->source.subsys.u.pci.addr;
|
|
|
|
activeDev = virPCIDeviceListFindByIDs(driver->activePciHostdevs,
|
|
|
|
addr->domain, addr->bus,
|
|
|
|
addr->slot, addr->function);
|
|
|
|
if (activeDev && virPCIDeviceListAddCopy(list, activeDev) < 0) {
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectUnref(list);
|
2011-10-13 08:30:21 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
2010-12-16 16:10:54 +00:00
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
qemuUpdateActivePciHostdevs(virQEMUDriverPtr driver,
|
|
|
|
virDomainDefPtr def)
|
2010-12-16 16:10:54 +00:00
|
|
|
{
|
2011-10-20 09:50:10 +00:00
|
|
|
virDomainHostdevDefPtr hostdev = NULL;
|
2013-05-31 18:26:56 +00:00
|
|
|
virPCIDevicePtr dev = NULL;
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2013-01-16 12:09:58 +00:00
|
|
|
int ret = -1;
|
2010-12-16 16:10:54 +00:00
|
|
|
|
|
|
|
if (!def->nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
2013-01-16 12:09:58 +00:00
|
|
|
virObjectLock(driver->activePciHostdevs);
|
|
|
|
virObjectLock(driver->inactivePciHostdevs);
|
|
|
|
|
2011-10-20 09:50:10 +00:00
|
|
|
for (i = 0; i < def->nhostdevs; i++) {
|
|
|
|
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;
|
|
|
|
|
2013-03-18 19:56:12 +00:00
|
|
|
dev = virPCIDeviceNew(hostdev->source.subsys.u.pci.addr.domain,
|
|
|
|
hostdev->source.subsys.u.pci.addr.bus,
|
|
|
|
hostdev->source.subsys.u.pci.addr.slot,
|
|
|
|
hostdev->source.subsys.u.pci.addr.function);
|
2011-10-20 09:50:10 +00:00
|
|
|
|
|
|
|
if (!dev)
|
2013-01-16 12:09:58 +00:00
|
|
|
goto cleanup;
|
2011-10-20 09:50:10 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceSetManaged(dev, hostdev->managed);
|
2013-04-23 18:53:36 +00:00
|
|
|
if (hostdev->source.subsys.u.pci.backend
|
2013-04-26 20:44:05 +00:00
|
|
|
== VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
|
2013-05-31 18:26:56 +00:00
|
|
|
if (virPCIDeviceSetStubDriver(dev, "vfio-pci") < 0)
|
|
|
|
goto cleanup;
|
2013-04-23 18:53:36 +00:00
|
|
|
} else {
|
2013-05-31 18:26:56 +00:00
|
|
|
if (virPCIDeviceSetStubDriver(dev, "pci-stub") < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2013-04-23 18:53:36 +00:00
|
|
|
}
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceSetUsedBy(dev, def->name);
|
2011-10-20 09:50:10 +00:00
|
|
|
|
|
|
|
/* Setup the original states for the PCI device */
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceSetUnbindFromStub(dev, hostdev->origstates.states.pci.unbind_from_stub);
|
|
|
|
virPCIDeviceSetRemoveSlot(dev, hostdev->origstates.states.pci.remove_slot);
|
|
|
|
virPCIDeviceSetReprobe(dev, hostdev->origstates.states.pci.reprobe);
|
2010-12-16 16:10:54 +00:00
|
|
|
|
2013-05-31 18:26:56 +00:00
|
|
|
if (virPCIDeviceListAdd(driver->activePciHostdevs, dev) < 0)
|
2013-01-16 12:09:58 +00:00
|
|
|
goto cleanup;
|
2013-05-31 18:26:56 +00:00
|
|
|
dev = NULL;
|
2012-03-26 14:44:19 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 18:30:09 +00:00
|
|
|
ret = 0;
|
2013-01-16 12:09:58 +00:00
|
|
|
cleanup:
|
2013-05-31 18:26:56 +00:00
|
|
|
virPCIDeviceFree(dev);
|
2013-01-16 12:09:58 +00:00
|
|
|
virObjectUnlock(driver->activePciHostdevs);
|
|
|
|
virObjectUnlock(driver->inactivePciHostdevs);
|
|
|
|
return ret;
|
2012-03-26 14:44:19 +00:00
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
2012-03-26 14:44:19 +00:00
|
|
|
int
|
2012-11-28 16:43:10 +00:00
|
|
|
qemuUpdateActiveUsbHostdevs(virQEMUDriverPtr driver,
|
2012-03-26 14:44:19 +00:00
|
|
|
virDomainDefPtr def)
|
|
|
|
{
|
|
|
|
virDomainHostdevDefPtr hostdev = NULL;
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2013-01-16 12:09:58 +00:00
|
|
|
int ret = -1;
|
2012-03-26 14:44:19 +00:00
|
|
|
|
|
|
|
if (!def->nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
2013-01-16 12:09:58 +00:00
|
|
|
virObjectLock(driver->activeUsbHostdevs);
|
2012-03-26 14:44:19 +00:00
|
|
|
for (i = 0; i < def->nhostdevs; i++) {
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDevicePtr usb = NULL;
|
2012-03-26 14:44:19 +00:00
|
|
|
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;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
usb = virUSBDeviceNew(hostdev->source.subsys.u.usb.bus,
|
|
|
|
hostdev->source.subsys.u.usb.device,
|
|
|
|
NULL);
|
2012-03-26 14:44:19 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceSetUsedBy(usb, def->name);
|
2012-03-26 14:44:19 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virUSBDeviceListAdd(driver->activeUsbHostdevs, usb) < 0) {
|
|
|
|
virUSBDeviceFree(usb);
|
2013-01-16 12:09:58 +00:00
|
|
|
goto cleanup;
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-16 12:09:58 +00:00
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virObjectUnlock(driver->activeUsbHostdevs);
|
|
|
|
return ret;
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 18:07:29 +00:00
|
|
|
int
|
|
|
|
qemuUpdateActiveScsiHostdevs(virQEMUDriverPtr driver,
|
|
|
|
virDomainDefPtr def)
|
|
|
|
{
|
|
|
|
virDomainHostdevDefPtr hostdev = NULL;
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2013-05-03 18:07:29 +00:00
|
|
|
int ret = -1;
|
2014-01-29 17:22:42 +00:00
|
|
|
virSCSIDevicePtr scsi = NULL;
|
|
|
|
virSCSIDevicePtr tmp = NULL;
|
2013-05-03 18:07:29 +00:00
|
|
|
|
|
|
|
if (!def->nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virObjectLock(driver->activeScsiHostdevs);
|
|
|
|
for (i = 0; i < def->nhostdevs; i++) {
|
|
|
|
hostdev = def->hostdevs[i];
|
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
|
|
|
|
hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI)
|
|
|
|
continue;
|
|
|
|
|
2014-01-30 07:05:59 +00:00
|
|
|
if (!(scsi = virSCSIDeviceNew(NULL,
|
|
|
|
hostdev->source.subsys.u.scsi.adapter,
|
2013-05-03 18:07:29 +00:00
|
|
|
hostdev->source.subsys.u.scsi.bus,
|
|
|
|
hostdev->source.subsys.u.scsi.target,
|
|
|
|
hostdev->source.subsys.u.scsi.unit,
|
2014-01-08 14:51:29 +00:00
|
|
|
hostdev->readonly,
|
|
|
|
hostdev->shareable)))
|
2013-05-03 18:07:29 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2014-01-29 17:22:42 +00:00
|
|
|
if ((tmp = virSCSIDeviceListFind(driver->activeScsiHostdevs, scsi))) {
|
|
|
|
if (virSCSIDeviceSetUsedBy(tmp, def->name) < 0) {
|
|
|
|
virSCSIDeviceFree(scsi);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-05-03 18:07:29 +00:00
|
|
|
virSCSIDeviceFree(scsi);
|
2014-01-29 17:22:42 +00:00
|
|
|
} else {
|
|
|
|
if (virSCSIDeviceSetUsedBy(scsi, def->name) < 0 ||
|
|
|
|
virSCSIDeviceListAdd(driver->activeScsiHostdevs, scsi) < 0) {
|
|
|
|
virSCSIDeviceFree(scsi);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2013-05-03 18:07:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virObjectUnlock(driver->activeScsiHostdevs);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
static int
|
2013-10-08 13:47:36 +00:00
|
|
|
qemuDomainHostdevPciSysfsPath(virDomainHostdevDefPtr hostdev,
|
|
|
|
char **sysfs_path)
|
2012-03-06 01:12:44 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceAddress config_address;
|
2012-03-06 01:12:44 +00:00
|
|
|
|
2013-03-18 19:56:12 +00:00
|
|
|
config_address.domain = hostdev->source.subsys.u.pci.addr.domain;
|
|
|
|
config_address.bus = hostdev->source.subsys.u.pci.addr.bus;
|
|
|
|
config_address.slot = hostdev->source.subsys.u.pci.addr.slot;
|
|
|
|
config_address.function = hostdev->source.subsys.u.pci.addr.function;
|
2012-03-06 01:12:44 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
return virPCIDeviceAddressGetSysfsFile(&config_address, sysfs_path);
|
2012-03-06 01:12:44 +00:00
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
int
|
|
|
|
qemuDomainHostdevIsVirtualFunction(virDomainHostdevDefPtr hostdev)
|
|
|
|
{
|
|
|
|
char *sysfs_path = NULL;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (qemuDomainHostdevPciSysfsPath(hostdev, &sysfs_path) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
ret = virPCIIsVirtualFunction(sysfs_path);
|
2012-03-06 01:12:44 +00:00
|
|
|
|
|
|
|
VIR_FREE(sysfs_path);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
static int
|
|
|
|
qemuDomainHostdevNetDevice(virDomainHostdevDefPtr hostdev, char **linkdev,
|
|
|
|
int *vf)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
char *sysfs_path = NULL;
|
|
|
|
|
|
|
|
if (qemuDomainHostdevPciSysfsPath(hostdev, &sysfs_path) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIIsVirtualFunction(sysfs_path) == 1) {
|
|
|
|
if (virPCIGetVirtualFunctionInfo(sysfs_path, linkdev,
|
|
|
|
vf) < 0)
|
2012-03-06 01:12:44 +00:00
|
|
|
goto cleanup;
|
|
|
|
} else {
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIGetNetName(sysfs_path, linkdev) < 0)
|
2012-03-06 01:12:44 +00:00
|
|
|
goto cleanup;
|
|
|
|
*vf = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FREE(sysfs_path);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
static int
|
|
|
|
qemuDomainHostdevNetConfigVirtPortProfile(const char *linkdev, int vf,
|
|
|
|
virNetDevVPortProfilePtr virtPort,
|
2013-10-08 17:07:53 +00:00
|
|
|
const virMacAddr *macaddr,
|
2012-03-06 01:12:44 +00:00
|
|
|
const unsigned char *uuid,
|
2013-05-24 10:47:17 +00:00
|
|
|
bool associate)
|
2012-03-06 01:12:44 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!virtPort)
|
|
|
|
return ret;
|
|
|
|
|
2012-10-17 09:23:12 +00:00
|
|
|
switch (virtPort->virtPortType) {
|
2012-03-06 01:12:44 +00:00
|
|
|
case VIR_NETDEV_VPORT_PROFILE_NONE:
|
|
|
|
case VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH:
|
|
|
|
case VIR_NETDEV_VPORT_PROFILE_8021QBG:
|
|
|
|
case VIR_NETDEV_VPORT_PROFILE_LAST:
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("virtualport type %s is "
|
|
|
|
"currently not supported on interfaces of type "
|
|
|
|
"hostdev"),
|
|
|
|
virNetDevVPortTypeToString(virtPort->virtPortType));
|
2012-03-06 01:12:44 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
int
|
|
|
|
qemuDomainHostdevNetConfigReplace(virDomainHostdevDefPtr hostdev,
|
|
|
|
const unsigned char *uuid,
|
|
|
|
char *stateDir)
|
|
|
|
{
|
|
|
|
char *linkdev = NULL;
|
2012-08-15 07:13:36 +00:00
|
|
|
virNetDevVlanPtr vlan;
|
2012-03-06 01:12:44 +00:00
|
|
|
virNetDevVPortProfilePtr virtPort;
|
|
|
|
int ret = -1;
|
|
|
|
int vf = -1;
|
|
|
|
int vlanid = -1;
|
2013-05-24 10:47:17 +00:00
|
|
|
bool port_profile_associate = true;
|
2012-03-06 01:12:44 +00:00
|
|
|
int isvf;
|
|
|
|
|
|
|
|
isvf = qemuDomainHostdevIsVirtualFunction(hostdev);
|
|
|
|
if (isvf <= 0) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("Interface type hostdev is currently supported on"
|
|
|
|
" SR-IOV Virtual Functions only"));
|
2012-03-06 01:12:44 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qemuDomainHostdevNetDevice(hostdev, &linkdev, &vf) < 0)
|
|
|
|
return ret;
|
|
|
|
|
2012-08-15 07:13:36 +00:00
|
|
|
vlan = virDomainNetGetActualVlan(hostdev->parent.data.net);
|
2012-03-06 01:12:44 +00:00
|
|
|
virtPort = virDomainNetGetActualVirtPortProfile(
|
|
|
|
hostdev->parent.data.net);
|
2012-08-15 07:13:36 +00:00
|
|
|
if (virtPort) {
|
|
|
|
if (vlan) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("direct setting of the vlan tag is not allowed "
|
|
|
|
"for hostdev devices using %s mode"),
|
|
|
|
virNetDevVPortTypeToString(virtPort->virtPortType));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2012-03-06 01:12:44 +00:00
|
|
|
ret = qemuDomainHostdevNetConfigVirtPortProfile(linkdev, vf,
|
2012-07-17 12:07:59 +00:00
|
|
|
virtPort, &hostdev->parent.data.net->mac, uuid,
|
2012-03-06 01:12:44 +00:00
|
|
|
port_profile_associate);
|
2012-08-15 07:13:36 +00:00
|
|
|
} else {
|
|
|
|
/* Set only mac and vlan */
|
|
|
|
if (vlan) {
|
|
|
|
if (vlan->nTags != 1 || vlan->trunk) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("vlan trunking is not supported "
|
|
|
|
"by SR-IOV network devices"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (vf == -1) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("vlan can only be set for SR-IOV VFs, but "
|
|
|
|
"%s is not a VF"), linkdev);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
vlanid = vlan->tag[0];
|
|
|
|
} else if (vf >= 0) {
|
|
|
|
vlanid = 0; /* assure any current vlan tag is reset */
|
|
|
|
}
|
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
ret = virNetDevReplaceNetConfig(linkdev, vf,
|
2012-08-15 07:13:36 +00:00
|
|
|
&hostdev->parent.data.net->mac,
|
|
|
|
vlanid, stateDir);
|
|
|
|
}
|
|
|
|
cleanup:
|
2012-03-06 01:12:44 +00:00
|
|
|
VIR_FREE(linkdev);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
int
|
|
|
|
qemuDomainHostdevNetConfigRestore(virDomainHostdevDefPtr hostdev,
|
|
|
|
char *stateDir)
|
|
|
|
{
|
|
|
|
char *linkdev = NULL;
|
|
|
|
virNetDevVPortProfilePtr virtPort;
|
|
|
|
int ret = -1;
|
|
|
|
int vf = -1;
|
2013-05-24 10:47:17 +00:00
|
|
|
bool port_profile_associate = false;
|
2012-03-06 01:12:44 +00:00
|
|
|
int isvf;
|
|
|
|
|
2013-10-18 08:39:08 +00:00
|
|
|
/* This is only needed for PCI devices that have been defined
|
|
|
|
* using <interface type='hostdev'>. For all others, it is a NOP.
|
|
|
|
*/
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
|
|
|
|
hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI ||
|
|
|
|
hostdev->parent.type != VIR_DOMAIN_DEVICE_NET ||
|
|
|
|
!hostdev->parent.data.net)
|
|
|
|
return 0;
|
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
isvf = qemuDomainHostdevIsVirtualFunction(hostdev);
|
|
|
|
if (isvf <= 0) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("Interface type hostdev is currently supported on"
|
|
|
|
" SR-IOV Virtual Functions only"));
|
2012-03-06 01:12:44 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (qemuDomainHostdevNetDevice(hostdev, &linkdev, &vf) < 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
virtPort = virDomainNetGetActualVirtPortProfile(
|
|
|
|
hostdev->parent.data.net);
|
|
|
|
if (virtPort)
|
|
|
|
ret = qemuDomainHostdevNetConfigVirtPortProfile(linkdev, vf, virtPort,
|
2012-07-17 12:07:59 +00:00
|
|
|
&hostdev->parent.data.net->mac, NULL,
|
2012-03-06 01:12:44 +00:00
|
|
|
port_profile_associate);
|
|
|
|
else
|
|
|
|
ret = virNetDevRestoreNetConfig(linkdev, vf, stateDir);
|
|
|
|
|
|
|
|
VIR_FREE(linkdev);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
qemu: default to vfio for nodedev-detach
This patch resolves:
https://bugzilla.redhat.com/show_bug.cgi?id=1035188
Commit f094aaac48a6 changed the PCI device assignment in qemu domains
to default to using VFIO rather than legacy KVM device assignment
(when VFIO is available). It didn't change which driver was used by
default for virNodeDeviceDetachFlags(), though, so that API (and the
virsh nodedev-detach command) was still binding to the pci-stub
driver, used by legacy KVM assignment, by default.
This patch publicizes (only within the qemu module, though, so no
additions to the symbol exports are needed) the functions that check
for presence of KVM and VFIO device assignment, then uses those
functions to decide what to do when no driver is specified for
virNodeDeviceDetachFlags(); if the vfio driver is loaded, the device
will be bound to vfio-pci, or if legacy KVM assignment is supported on
this system, the device will be bound to pci-stub; if neither method
is available, the detach will fail.
2013-11-29 11:19:26 +00:00
|
|
|
bool
|
2013-09-19 15:01:17 +00:00
|
|
|
qemuHostdevHostSupportsPassthroughVFIO(void)
|
|
|
|
{
|
|
|
|
DIR *iommuDir = NULL;
|
|
|
|
struct dirent *iommuGroup = NULL;
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
/* condition 1 - /sys/kernel/iommu_groups/ contains entries */
|
|
|
|
if (!(iommuDir = opendir("/sys/kernel/iommu_groups/")))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
while ((iommuGroup = readdir(iommuDir))) {
|
|
|
|
/* skip ./ ../ */
|
|
|
|
if (STRPREFIX(iommuGroup->d_name, "."))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* assume we found a group */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!iommuGroup)
|
|
|
|
goto cleanup;
|
|
|
|
/* okay, iommu is on and recognizes groups */
|
|
|
|
|
|
|
|
/* condition 2 - /dev/vfio/vfio exists */
|
|
|
|
if (!virFileExists("/dev/vfio/vfio"))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (iommuDir)
|
|
|
|
closedir(iommuDir);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if HAVE_LINUX_KVM_H
|
|
|
|
# include <linux/kvm.h>
|
qemu: default to vfio for nodedev-detach
This patch resolves:
https://bugzilla.redhat.com/show_bug.cgi?id=1035188
Commit f094aaac48a6 changed the PCI device assignment in qemu domains
to default to using VFIO rather than legacy KVM device assignment
(when VFIO is available). It didn't change which driver was used by
default for virNodeDeviceDetachFlags(), though, so that API (and the
virsh nodedev-detach command) was still binding to the pci-stub
driver, used by legacy KVM assignment, by default.
This patch publicizes (only within the qemu module, though, so no
additions to the symbol exports are needed) the functions that check
for presence of KVM and VFIO device assignment, then uses those
functions to decide what to do when no driver is specified for
virNodeDeviceDetachFlags(); if the vfio driver is loaded, the device
will be bound to vfio-pci, or if legacy KVM assignment is supported on
this system, the device will be bound to pci-stub; if neither method
is available, the detach will fail.
2013-11-29 11:19:26 +00:00
|
|
|
bool
|
2013-09-19 15:01:17 +00:00
|
|
|
qemuHostdevHostSupportsPassthroughLegacy(void)
|
|
|
|
{
|
|
|
|
int kvmfd = -1;
|
|
|
|
bool ret = false;
|
|
|
|
|
|
|
|
if ((kvmfd = open("/dev/kvm", O_RDONLY)) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
# ifdef KVM_CAP_IOMMU
|
|
|
|
if ((ioctl(kvmfd, KVM_CHECK_EXTENSION, KVM_CAP_IOMMU)) <= 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
ret = true;
|
|
|
|
# endif
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
VIR_FORCE_CLOSE(kvmfd);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
#else
|
qemu: default to vfio for nodedev-detach
This patch resolves:
https://bugzilla.redhat.com/show_bug.cgi?id=1035188
Commit f094aaac48a6 changed the PCI device assignment in qemu domains
to default to using VFIO rather than legacy KVM device assignment
(when VFIO is available). It didn't change which driver was used by
default for virNodeDeviceDetachFlags(), though, so that API (and the
virsh nodedev-detach command) was still binding to the pci-stub
driver, used by legacy KVM assignment, by default.
This patch publicizes (only within the qemu module, though, so no
additions to the symbol exports are needed) the functions that check
for presence of KVM and VFIO device assignment, then uses those
functions to decide what to do when no driver is specified for
virNodeDeviceDetachFlags(); if the vfio driver is loaded, the device
will be bound to vfio-pci, or if legacy KVM assignment is supported on
this system, the device will be bound to pci-stub; if neither method
is available, the detach will fail.
2013-11-29 11:19:26 +00:00
|
|
|
bool
|
2013-09-19 15:01:17 +00:00
|
|
|
qemuHostdevHostSupportsPassthroughLegacy(void)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static bool
|
|
|
|
qemuPrepareHostdevPCICheckSupport(virDomainHostdevDefPtr *hostdevs,
|
2013-09-20 08:39:51 +00:00
|
|
|
size_t nhostdevs,
|
|
|
|
virQEMUCapsPtr qemuCaps)
|
2013-09-19 15:01:17 +00:00
|
|
|
{
|
|
|
|
bool supportsPassthroughKVM = qemuHostdevHostSupportsPassthroughLegacy();
|
|
|
|
bool supportsPassthroughVFIO = qemuHostdevHostSupportsPassthroughVFIO();
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
/* assign defaults for hostdev passthrough */
|
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
int *backend = &hostdev->source.subsys.u.pci.backend;
|
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
|
|
|
continue;
|
|
|
|
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch ((virDomainHostdevSubsysPciBackendType) *backend) {
|
2013-09-20 08:39:51 +00:00
|
|
|
case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_DEFAULT:
|
|
|
|
if (supportsPassthroughVFIO &&
|
|
|
|
virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_VFIO_PCI)) {
|
|
|
|
*backend = VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO;
|
|
|
|
} else if (supportsPassthroughKVM &&
|
|
|
|
(virQEMUCapsGet(qemuCaps, QEMU_CAPS_PCIDEVICE) ||
|
|
|
|
virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE))) {
|
|
|
|
*backend = VIR_DOMAIN_HOSTDEV_PCI_BACKEND_KVM;
|
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("host doesn't support passthrough of "
|
|
|
|
"host PCI devices"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2013-09-19 15:01:17 +00:00
|
|
|
case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO:
|
|
|
|
if (!supportsPassthroughVFIO) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("host doesn't support VFIO PCI passthrough"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_KVM:
|
|
|
|
if (!supportsPassthroughKVM) {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("host doesn't support legacy PCI passthrough"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_DOMAIN_HOSTDEV_PCI_BACKEND_TYPE_LAST:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
int
|
|
|
|
qemuPrepareHostdevPCIDevices(virQEMUDriverPtr driver,
|
|
|
|
const char *name,
|
|
|
|
const unsigned char *uuid,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
2013-09-20 08:39:51 +00:00
|
|
|
int nhostdevs,
|
|
|
|
virQEMUCapsPtr qemuCaps)
|
2010-12-16 16:10:54 +00:00
|
|
|
{
|
2013-10-10 10:32:49 +00:00
|
|
|
virPCIDeviceListPtr pcidevs = NULL;
|
2012-03-06 01:12:44 +00:00
|
|
|
int last_processed_hostdev_vf = -1;
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2010-12-16 16:10:54 +00:00
|
|
|
int ret = -1;
|
2013-01-10 21:03:14 +00:00
|
|
|
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
|
2010-12-16 16:10:54 +00:00
|
|
|
|
2013-09-20 08:39:51 +00:00
|
|
|
if (!qemuPrepareHostdevPCICheckSupport(hostdevs, nhostdevs, qemuCaps))
|
2013-09-19 15:01:17 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2013-01-16 12:09:58 +00:00
|
|
|
virObjectLock(driver->activePciHostdevs);
|
|
|
|
virObjectLock(driver->inactivePciHostdevs);
|
|
|
|
|
2010-12-16 16:10:54 +00:00
|
|
|
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
|
2013-01-10 21:03:14 +00:00
|
|
|
goto cleanup;
|
2010-12-16 16:10:54 +00:00
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
|
|
|
virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i);
|
|
|
|
virPCIDevicePtr other;
|
2010-12-16 16:10:54 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (!virPCIDeviceIsAssignable(dev, !cfg->relaxedACS)) {
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("PCI device %s is not assignable"),
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceGetName(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
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
/* The device is in use by other active domain if
|
|
|
|
* the dev is in list driver->activePciHostdevs.
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
if ((other = virPCIDeviceListFind(driver->activePciHostdevs, dev))) {
|
|
|
|
const char *other_name = virPCIDeviceGetUsedBy(other);
|
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 (other_name)
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("PCI device %s is in use by domain %s"),
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceGetName(dev), other_name);
|
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
|
|
|
else
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("PCI device %s is already in use"),
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceGetName(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
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-23 18:53:36 +00:00
|
|
|
/* Loop 2: detach managed devices (i.e. bind to appropriate stub driver) */
|
2013-01-14 22:11:44 +00:00
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
|
|
|
virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i);
|
|
|
|
if (virPCIDeviceGetManaged(dev) &&
|
2013-05-30 18:14:46 +00:00
|
|
|
virPCIDeviceDetach(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 */
|
2013-01-14 22:11:44 +00:00
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
|
|
|
virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i);
|
2013-06-29 02:35:21 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceReset(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,
|
2013-01-10 21:03:14 +00:00
|
|
|
cfg->stateDir) < 0) {
|
2012-03-06 01:12:44 +00:00
|
|
|
goto resetvfnetconfig;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
last_processed_hostdev_vf = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop 5: Now mark all the devices as active */
|
2013-01-14 22:11:44 +00:00
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
|
|
|
virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i);
|
|
|
|
if (virPCIDeviceListAdd(driver->activePciHostdevs, dev) < 0)
|
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. */
|
2013-01-14 22:11:44 +00:00
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
|
|
|
virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i);
|
|
|
|
virPCIDeviceListDel(driver->inactivePciHostdevs, 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
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
|
|
|
virPCIDevicePtr dev, activeDev;
|
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
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
dev = virPCIDeviceListGet(pcidevs, i);
|
|
|
|
activeDev = virPCIDeviceListFind(driver->activePciHostdevs, 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
|
|
|
|
2013-01-17 19:17:11 +00:00
|
|
|
if (activeDev)
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceSetUsedBy(activeDev, name);
|
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
|
|
|
}
|
|
|
|
|
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++) {
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDevicePtr dev;
|
|
|
|
virPCIDevicePtr pcidev;
|
2011-10-20 09:50:10 +00:00
|
|
|
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;
|
|
|
|
|
2013-03-18 19:56:12 +00:00
|
|
|
dev = virPCIDeviceNew(hostdev->source.subsys.u.pci.addr.domain,
|
|
|
|
hostdev->source.subsys.u.pci.addr.bus,
|
|
|
|
hostdev->source.subsys.u.pci.addr.slot,
|
|
|
|
hostdev->source.subsys.u.pci.addr.function);
|
2011-10-20 09:50:10 +00:00
|
|
|
|
|
|
|
/* original states "unbind_from_stub", "remove_slot",
|
|
|
|
* "reprobe" were already set by pciDettachDevice in
|
|
|
|
* loop 2.
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
if ((pcidev = virPCIDeviceListFind(pcidevs, dev))) {
|
2011-10-20 09:50:10 +00:00
|
|
|
hostdev->origstates.states.pci.unbind_from_stub =
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceGetUnbindFromStub(pcidev);
|
2011-10-20 09:50:10 +00:00
|
|
|
hostdev->origstates.states.pci.remove_slot =
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceGetRemoveSlot(pcidev);
|
2011-10-20 09:50:10 +00:00
|
|
|
hostdev->origstates.states.pci.reprobe =
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceGetReprobe(pcidev);
|
2011-10-20 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFree(dev);
|
2011-10-20 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
/* Loop 9: Now steal all the devices from pcidevs */
|
2013-01-14 22:11:44 +00:00
|
|
|
while (virPCIDeviceListCount(pcidevs) > 0)
|
|
|
|
virPCIDeviceListStealIndex(pcidevs, 0);
|
2011-03-28 07:01:14 +00:00
|
|
|
|
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
|
2013-01-16 11:49:54 +00:00
|
|
|
* free them in virObjectUnref().
|
2011-03-28 07:01:14 +00:00
|
|
|
*/
|
2013-06-25 02:42:35 +00:00
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
|
|
|
virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i);
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceListSteal(driver->activePciHostdevs, dev);
|
2011-03-28 07:01:14 +00:00
|
|
|
}
|
2010-12-16 16:10:54 +00:00
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
resetvfnetconfig:
|
2013-10-18 08:39:08 +00:00
|
|
|
for (i = 0;
|
|
|
|
last_processed_hostdev_vf != -1 && i < last_processed_hostdev_vf; i++)
|
|
|
|
qemuDomainHostdevNetConfigRestore(hostdevs[i], cfg->stateDir);
|
2012-03-06 01:12:44 +00:00
|
|
|
|
2011-03-28 07:01:19 +00:00
|
|
|
reattachdevs:
|
2013-01-14 22:11:44 +00:00
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
|
|
|
virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i);
|
2013-04-23 18:53:36 +00:00
|
|
|
|
|
|
|
/* NB: This doesn't actually re-bind to original driver, just
|
|
|
|
* unbinds from the stub driver
|
|
|
|
*/
|
2013-11-05 12:55:54 +00:00
|
|
|
ignore_value(virPCIDeviceReattach(dev, driver->activePciHostdevs,
|
|
|
|
NULL));
|
2011-03-28 07:01:19 +00:00
|
|
|
}
|
|
|
|
|
2010-12-16 16:10:54 +00:00
|
|
|
cleanup:
|
2013-01-16 12:09:58 +00:00
|
|
|
virObjectUnlock(driver->activePciHostdevs);
|
|
|
|
virObjectUnlock(driver->inactivePciHostdevs);
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectUnref(pcidevs);
|
2013-01-10 21:03:14 +00:00
|
|
|
virObjectUnref(cfg);
|
2010-12-16 16:10:54 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
2011-12-21 17:58:29 +00:00
|
|
|
int
|
2012-11-28 16:43:10 +00:00
|
|
|
qemuPrepareHostdevUSBDevices(virQEMUDriverPtr driver,
|
2011-12-21 17:58:29 +00:00
|
|
|
const char *name,
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceListPtr list)
|
2010-12-16 16:10:54 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i, j;
|
2012-05-06 14:45:05 +00:00
|
|
|
unsigned int count;
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDevicePtr tmp;
|
2012-05-06 14:45:05 +00:00
|
|
|
|
2013-01-16 12:09:58 +00:00
|
|
|
virObjectLock(driver->activeUsbHostdevs);
|
2013-01-14 22:11:44 +00:00
|
|
|
count = virUSBDeviceListCount(list);
|
2012-05-06 14:45:05 +00:00
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDevicePtr usb = virUSBDeviceListGet(list, i);
|
|
|
|
if ((tmp = virUSBDeviceListFind(driver->activeUsbHostdevs, usb))) {
|
|
|
|
const char *other_name = virUSBDeviceGetUsedBy(tmp);
|
2012-05-06 14:45:05 +00:00
|
|
|
|
|
|
|
if (other_name)
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("USB device %s is in use by domain %s"),
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceGetName(tmp), other_name);
|
2012-05-06 14:45:05 +00:00
|
|
|
else
|
2012-07-18 15:22:03 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("USB device %s is already in use"),
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceGetName(tmp));
|
2012-05-16 14:42:02 +00:00
|
|
|
goto error;
|
2012-05-06 14:45:05 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceSetUsedBy(usb, name);
|
2012-05-06 14:45:05 +00:00
|
|
|
VIR_DEBUG("Adding %03d.%03d dom=%s to activeUsbHostdevs",
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceGetBus(usb), virUSBDeviceGetDevno(usb), name);
|
2012-05-06 14:45:05 +00:00
|
|
|
/*
|
|
|
|
* The caller is responsible to steal these usb devices
|
2013-01-14 22:11:44 +00:00
|
|
|
* from the virUSBDeviceList that passed in on success,
|
2012-05-06 14:45:05 +00:00
|
|
|
* perform rollback on failure.
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virUSBDeviceListAdd(driver->activeUsbHostdevs, usb) < 0)
|
2012-05-16 14:42:02 +00:00
|
|
|
goto error;
|
2012-05-06 14:45:05 +00:00
|
|
|
}
|
2013-01-16 12:09:58 +00:00
|
|
|
|
|
|
|
virObjectUnlock(driver->activeUsbHostdevs);
|
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++) {
|
2013-01-14 22:11:44 +00:00
|
|
|
tmp = virUSBDeviceListGet(list, i);
|
|
|
|
virUSBDeviceListSteal(driver->activeUsbHostdevs, tmp);
|
2012-05-16 14:42:02 +00:00
|
|
|
}
|
2013-01-16 12:09:58 +00:00
|
|
|
virObjectUnlock(driver->activeUsbHostdevs);
|
2012-05-16 14:42:02 +00:00
|
|
|
return -1;
|
2012-05-06 14:45:05 +00:00
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
2012-10-03 14:57:28 +00:00
|
|
|
int
|
|
|
|
qemuFindHostdevUSBDevice(virDomainHostdevDefPtr hostdev,
|
|
|
|
bool mandatory,
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDevicePtr *usb)
|
2012-10-03 11:55:35 +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;
|
2012-10-09 11:15:46 +00:00
|
|
|
bool autoAddress = hostdev->source.subsys.u.usb.autoAddress;
|
2012-10-03 14:57:28 +00:00
|
|
|
int rc;
|
2012-10-03 11:55:35 +00:00
|
|
|
|
2012-10-03 14:57:28 +00:00
|
|
|
*usb = NULL;
|
2012-10-03 11:55:35 +00:00
|
|
|
|
2012-10-03 14:57:28 +00:00
|
|
|
if (vendor && bus) {
|
2013-01-14 22:11:44 +00:00
|
|
|
rc = virUSBDeviceFind(vendor, product, bus, device,
|
|
|
|
NULL,
|
|
|
|
autoAddress ? false : mandatory,
|
|
|
|
usb);
|
2012-10-09 11:15:46 +00:00
|
|
|
if (rc < 0) {
|
2012-10-03 14:57:28 +00:00
|
|
|
return -1;
|
2012-10-09 11:15:46 +00:00
|
|
|
} else if (!autoAddress) {
|
|
|
|
goto out;
|
|
|
|
} else {
|
|
|
|
VIR_INFO("USB device %x:%x could not be found at previous"
|
|
|
|
" address (bus:%u device:%u)",
|
|
|
|
vendor, product, bus, device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* When vendor is specified, its USB address is either unspecified or the
|
|
|
|
* device could not be found at the USB device where it had been
|
|
|
|
* automatically found before.
|
|
|
|
*/
|
|
|
|
if (vendor) {
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceListPtr devs;
|
2012-10-03 11:55:35 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
rc = virUSBDeviceFindByVendor(vendor, product, NULL, mandatory, &devs);
|
2012-10-03 14:57:28 +00:00
|
|
|
if (rc < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (rc == 1) {
|
2013-01-14 22:11:44 +00:00
|
|
|
*usb = virUSBDeviceListGet(devs, 0);
|
|
|
|
virUSBDeviceListSteal(devs, *usb);
|
2012-10-03 11:55:35 +00:00
|
|
|
}
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectUnref(devs);
|
2012-10-03 11:55:35 +00:00
|
|
|
|
2012-10-03 14:57:28 +00:00
|
|
|
if (rc == 0) {
|
|
|
|
goto out;
|
|
|
|
} else if (rc > 1) {
|
2012-10-09 11:15:46 +00:00
|
|
|
if (autoAddress) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED,
|
|
|
|
_("Multiple USB devices for %x:%x were found,"
|
|
|
|
" but none of them is at bus:%u device:%u"),
|
|
|
|
vendor, product, bus, device);
|
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED,
|
|
|
|
_("Multiple USB devices for %x:%x, "
|
|
|
|
"use <address> to specify one"),
|
|
|
|
vendor, product);
|
|
|
|
}
|
2012-10-03 14:57:28 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-10-03 11:55:35 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
hostdev->source.subsys.u.usb.bus = virUSBDeviceGetBus(*usb);
|
|
|
|
hostdev->source.subsys.u.usb.device = virUSBDeviceGetDevno(*usb);
|
2012-10-09 11:15:46 +00:00
|
|
|
hostdev->source.subsys.u.usb.autoAddress = true;
|
|
|
|
|
|
|
|
if (autoAddress) {
|
|
|
|
VIR_INFO("USB device %x:%x found at bus:%u device:%u (moved"
|
|
|
|
" from bus:%u device:%u)",
|
|
|
|
vendor, product,
|
|
|
|
hostdev->source.subsys.u.usb.bus,
|
|
|
|
hostdev->source.subsys.u.usb.device,
|
|
|
|
bus, device);
|
|
|
|
}
|
2012-10-03 11:55:35 +00:00
|
|
|
} else if (!vendor && bus) {
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virUSBDeviceFindByBus(bus, device, NULL, mandatory, usb) < 0)
|
2012-10-03 14:57:28 +00:00
|
|
|
return -1;
|
2012-10-03 11:55:35 +00:00
|
|
|
}
|
|
|
|
|
2012-10-03 14:57:28 +00:00
|
|
|
out:
|
|
|
|
if (!*usb)
|
2013-04-10 10:46:56 +00:00
|
|
|
hostdev->missing = true;
|
2012-10-03 14:57:28 +00:00
|
|
|
return 0;
|
2012-10-03 11:55:35 +00:00
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
2012-05-06 14:45:05 +00:00
|
|
|
static int
|
2012-11-28 16:43:10 +00:00
|
|
|
qemuPrepareHostUSBDevices(virQEMUDriverPtr driver,
|
2012-10-04 14:18:16 +00:00
|
|
|
virDomainDefPtr def,
|
|
|
|
bool coldBoot)
|
2012-05-06 14:45:05 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
|
|
|
int ret = -1;
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceListPtr list;
|
|
|
|
virUSBDevicePtr 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()
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
if (!(list = virUSBDeviceListNew()))
|
2011-12-21 17:58:29 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2012-05-16 14:42:02 +00:00
|
|
|
/* Loop 1: build temporary list
|
2011-12-21 17:58:29 +00:00
|
|
|
*/
|
2013-05-21 07:21:20 +00:00
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
2011-12-21 17:58:29 +00:00
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
2012-10-04 14:18:16 +00:00
|
|
|
bool required = true;
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDevicePtr usb;
|
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-10-04 14:18:16 +00:00
|
|
|
if (hostdev->startupPolicy == VIR_DOMAIN_STARTUP_POLICY_OPTIONAL ||
|
|
|
|
(hostdev->startupPolicy == VIR_DOMAIN_STARTUP_POLICY_REQUISITE &&
|
|
|
|
!coldBoot))
|
|
|
|
required = false;
|
|
|
|
|
|
|
|
if (qemuFindHostdevUSBDevice(hostdev, required, &usb) < 0)
|
2012-05-06 14:45:05 +00:00
|
|
|
goto cleanup;
|
2011-12-21 17:58:29 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (usb && virUSBDeviceListAdd(list, usb) < 0) {
|
|
|
|
virUSBDeviceFree(usb);
|
2012-05-06 14:45:05 +00:00
|
|
|
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.
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
while (virUSBDeviceListCount(list) > 0) {
|
|
|
|
tmp = virUSBDeviceListGet(list, 0);
|
|
|
|
virUSBDeviceListSteal(list, tmp);
|
2011-12-21 17:58:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectUnref(list);
|
2011-12-21 17:58:29 +00:00
|
|
|
return ret;
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
2013-05-03 18:07:29 +00:00
|
|
|
int
|
|
|
|
qemuPrepareHostdevSCSIDevices(virQEMUDriverPtr driver,
|
|
|
|
const char *name,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i, j;
|
|
|
|
int count;
|
2013-05-03 18:07:29 +00:00
|
|
|
virSCSIDeviceListPtr list;
|
|
|
|
virSCSIDevicePtr tmp;
|
|
|
|
|
2013-05-03 18:07:38 +00:00
|
|
|
/* Loop 1: Add the shared scsi host device to shared device
|
|
|
|
* table.
|
|
|
|
*/
|
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainDeviceDef dev;
|
|
|
|
|
|
|
|
dev.type = VIR_DOMAIN_DEVICE_HOSTDEV;
|
|
|
|
dev.data.hostdev = hostdevs[i];
|
|
|
|
|
|
|
|
if (qemuAddSharedDevice(driver, &dev, name) < 0)
|
|
|
|
return -1;
|
2013-05-03 18:07:43 +00:00
|
|
|
|
|
|
|
if (qemuSetUnprivSGIO(&dev) < 0)
|
|
|
|
return -1;
|
2013-05-03 18:07:38 +00:00
|
|
|
}
|
|
|
|
|
2013-05-03 18:07:29 +00:00
|
|
|
/* To prevent situation where SCSI device is assigned to two domains
|
|
|
|
* we need to keep a list of currently assigned SCSI devices.
|
|
|
|
* This is done in several loops which cannot be joined into one big
|
|
|
|
* loop. See qemuPrepareHostdevPCIDevices()
|
|
|
|
*/
|
|
|
|
if (!(list = virSCSIDeviceListNew()))
|
|
|
|
goto cleanup;
|
|
|
|
|
2013-05-03 18:07:38 +00:00
|
|
|
/* Loop 2: build temporary list */
|
2013-05-21 07:21:20 +00:00
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
2013-05-03 18:07:29 +00:00
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
virSCSIDevicePtr scsi;
|
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
|
|
|
|
hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (hostdev->managed) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("SCSI host device doesn't support managed mode"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2014-01-30 07:05:59 +00:00
|
|
|
if (!(scsi = virSCSIDeviceNew(NULL,
|
|
|
|
hostdev->source.subsys.u.scsi.adapter,
|
2013-05-03 18:07:29 +00:00
|
|
|
hostdev->source.subsys.u.scsi.bus,
|
|
|
|
hostdev->source.subsys.u.scsi.target,
|
|
|
|
hostdev->source.subsys.u.scsi.unit,
|
2014-01-08 14:51:29 +00:00
|
|
|
hostdev->readonly,
|
|
|
|
hostdev->shareable)))
|
2013-05-03 18:07:29 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (scsi && virSCSIDeviceListAdd(list, scsi) < 0) {
|
|
|
|
virSCSIDeviceFree(scsi);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-03 18:07:38 +00:00
|
|
|
/* Loop 3: Mark devices in temporary list as used by @name
|
2013-05-03 18:07:29 +00:00
|
|
|
* and add them to driver list. However, if something goes
|
|
|
|
* wrong, perform rollback.
|
|
|
|
*/
|
|
|
|
virObjectLock(driver->activeScsiHostdevs);
|
|
|
|
count = virSCSIDeviceListCount(list);
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
virSCSIDevicePtr scsi = virSCSIDeviceListGet(list, i);
|
|
|
|
if ((tmp = virSCSIDeviceListFind(driver->activeScsiHostdevs, scsi))) {
|
2014-01-29 17:22:42 +00:00
|
|
|
bool scsi_shareable = virSCSIDeviceGetShareable(scsi);
|
|
|
|
bool tmp_shareable = virSCSIDeviceGetShareable(tmp);
|
2013-05-03 18:07:29 +00:00
|
|
|
|
2014-01-29 17:22:42 +00:00
|
|
|
if (!(scsi_shareable && tmp_shareable)) {
|
2013-05-03 18:07:29 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
2014-01-29 17:22:42 +00:00
|
|
|
_("SCSI device %s is already in use by "
|
|
|
|
"other domain(s) as '%s'"),
|
2014-01-30 08:47:19 +00:00
|
|
|
virSCSIDeviceGetName(tmp),
|
|
|
|
tmp_shareable ? "shareable" : "non-shareable");
|
2014-01-29 17:22:42 +00:00
|
|
|
goto error;
|
|
|
|
}
|
2013-05-03 18:07:29 +00:00
|
|
|
|
2014-01-29 17:22:42 +00:00
|
|
|
if (virSCSIDeviceSetUsedBy(tmp, name) < 0)
|
|
|
|
goto error;
|
|
|
|
} else {
|
|
|
|
if (virSCSIDeviceSetUsedBy(scsi, name) < 0)
|
|
|
|
goto error;
|
2013-05-03 18:07:29 +00:00
|
|
|
|
2014-01-29 17:22:42 +00:00
|
|
|
VIR_DEBUG("Adding %s to activeScsiHostdevs", virSCSIDeviceGetName(scsi));
|
|
|
|
|
|
|
|
if (virSCSIDeviceListAdd(driver->activeScsiHostdevs, scsi) < 0)
|
|
|
|
goto error;
|
|
|
|
}
|
2013-05-03 18:07:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virObjectUnlock(driver->activeScsiHostdevs);
|
|
|
|
|
2013-05-03 18:07:38 +00:00
|
|
|
/* Loop 4: Temporary list was successfully merged with
|
2013-05-03 18:07:29 +00:00
|
|
|
* driver list, so steal all items to avoid freeing them
|
|
|
|
* when freeing temporary list.
|
|
|
|
*/
|
|
|
|
while (virSCSIDeviceListCount(list) > 0) {
|
|
|
|
tmp = virSCSIDeviceListGet(list, 0);
|
|
|
|
virSCSIDeviceListSteal(list, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
virObjectUnref(list);
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
error:
|
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
tmp = virSCSIDeviceListGet(list, i);
|
|
|
|
virSCSIDeviceListSteal(driver->activeScsiHostdevs, tmp);
|
|
|
|
}
|
|
|
|
virObjectUnlock(driver->activeScsiHostdevs);
|
|
|
|
cleanup:
|
|
|
|
virObjectUnref(list);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
qemuPrepareHostDevices(virQEMUDriverPtr driver,
|
|
|
|
virDomainDefPtr def,
|
2013-09-20 08:39:51 +00:00
|
|
|
virQEMUCapsPtr qemuCaps,
|
2013-10-08 13:47:36 +00:00
|
|
|
bool coldBoot)
|
2010-12-16 16:10:54 +00:00
|
|
|
{
|
|
|
|
if (!def->nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
2013-01-16 12:09:58 +00:00
|
|
|
if (qemuPrepareHostdevPCIDevices(driver, def->name, def->uuid,
|
2013-09-20 08:39:51 +00:00
|
|
|
def->hostdevs, def->nhostdevs,
|
|
|
|
qemuCaps) < 0)
|
2010-12-16 16:10:54 +00:00
|
|
|
return -1;
|
|
|
|
|
2012-10-04 14:18:16 +00:00
|
|
|
if (qemuPrepareHostUSBDevices(driver, def, coldBoot) < 0)
|
2010-12-16 16:10:54 +00:00
|
|
|
return -1;
|
|
|
|
|
2013-05-03 18:07:29 +00:00
|
|
|
if (qemuPrepareHostdevSCSIDevices(driver, def->name,
|
|
|
|
def->hostdevs, def->nhostdevs) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2010-12-16 16:10:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-01-16 12:09:58 +00:00
|
|
|
/*
|
|
|
|
* Pre-condition: driver->inactivePciHostdevs & driver->activePciHostdevs
|
|
|
|
* are locked
|
|
|
|
*/
|
2013-10-08 13:47:36 +00:00
|
|
|
void
|
|
|
|
qemuReattachPciDevice(virPCIDevicePtr dev, virQEMUDriverPtr driver)
|
2010-12-16 16:10:54 +00:00
|
|
|
{
|
|
|
|
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.
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
if (!virPCIDeviceGetManaged(dev)) {
|
|
|
|
if (virPCIDeviceListAdd(driver->inactivePciHostdevs, dev) < 0)
|
|
|
|
virPCIDeviceFree(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
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
while (virPCIDeviceWaitForCleanup(dev, "kvm_assigned_device")
|
2010-12-16 16:10:54 +00:00
|
|
|
&& retries) {
|
|
|
|
usleep(100*1000);
|
|
|
|
retries--;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceReattach(dev, driver->activePciHostdevs,
|
pci: autolearn name of stub driver, remove from arglist
virPCIDeviceReattach and virPCIDeviceUnbindFromStub (called by
virPCIDeviceReattach) had previously required the name of the stub
driver as input. This is unnecessary, because the name of the driver
the device is currently bound to can be found by looking at the link:
/sys/bus/pci/dddd:bb:ss.ff/driver
Instead of requiring that the name of the expected stub driver name
and only unbinding if that one name is matched, we no longer take a
driver name in the arglist for either of these
functions. virPCIDeviceUnbindFromStub just compares the name of the
currently bound driver to a list of "well known" stubs (right now
contains "pci-stub" and "vfio-pci" for qemu, and "pciback" for xen),
and only performs the unbind if it's one of those devices.
This allows virsh nodedevice-reattach to work properly across a
libvirtd restart, and fixes a couple of cases where we were
erroneously still hard-coding "pci-stub" as the drive name.
For some unknown reason, virPCIDeviceReattach had been calling
modprobe on the stub driver prior to unbinding the device. This was
problematic because we no longer know the name of the stub driver in
that function. However, it is pointless to probe for the stub driver
at that time anyway - because the device is bound to the stub driver,
we are guaranteed that it is already loaded, and so that call to
modprobe has been removed.
2013-05-01 18:44:10 +00:00
|
|
|
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
|
|
|
}
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFree(dev);
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
void
|
|
|
|
qemuDomainReAttachHostdevDevices(virQEMUDriverPtr driver,
|
|
|
|
const char *name,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
2010-12-16 16:10:54 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceListPtr pcidevs;
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2013-01-10 21:03:14 +00:00
|
|
|
virQEMUDriverConfigPtr cfg = virQEMUDriverGetConfig(driver);
|
2010-12-16 16:10:54 +00:00
|
|
|
|
2013-01-16 12:09:58 +00:00
|
|
|
virObjectLock(driver->activePciHostdevs);
|
|
|
|
virObjectLock(driver->inactivePciHostdevs);
|
|
|
|
|
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();
|
2013-01-14 22:11:44 +00:00
|
|
|
VIR_ERROR(_("Failed to allocate PCI device list: %s"),
|
2010-12-16 16:10:54 +00:00
|
|
|
err ? err->message : _("unknown error"));
|
|
|
|
virResetError(err);
|
2013-01-10 21:03:14 +00:00
|
|
|
goto cleanup;
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
|
|
|
virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i);
|
|
|
|
virPCIDevicePtr activeDev = NULL;
|
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
|
|
|
|
2013-06-25 02:04:43 +00:00
|
|
|
/* delete the copy of the dev from pcidevs if it's used by
|
|
|
|
* other domain. Or delete it from activePciHostDevs if it had
|
|
|
|
* been used by this domain.
|
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
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
activeDev = virPCIDeviceListFind(driver->activePciHostdevs, 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
|
|
|
if (activeDev &&
|
2013-01-14 22:11:44 +00:00
|
|
|
STRNEQ_NULLABLE(name, virPCIDeviceGetUsedBy(activeDev))) {
|
2013-06-25 02:04:43 +00:00
|
|
|
virPCIDeviceListDel(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
|
|
|
|
2013-06-25 02:04:43 +00:00
|
|
|
virPCIDeviceListDel(driver->activePciHostdevs, dev);
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 02:04:43 +00:00
|
|
|
/* At this point, any device that had been used by the guest is in
|
|
|
|
* pcidevs, but has been removed from activePciHostdevs.
|
|
|
|
*/
|
|
|
|
|
2012-03-06 01:12:44 +00:00
|
|
|
/*
|
|
|
|
* For SRIOV net host devices, unset mac and port profile before
|
|
|
|
* reset and reattach device
|
|
|
|
*/
|
2013-10-18 08:39:08 +00:00
|
|
|
for (i = 0; i < nhostdevs; i++)
|
|
|
|
qemuDomainHostdevNetConfigRestore(hostdevs[i], cfg->stateDir);
|
2012-03-06 01:12:44 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
|
|
|
virPCIDevicePtr dev = virPCIDeviceListGet(pcidevs, i);
|
2013-06-29 02:35:21 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceReset(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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
while (virPCIDeviceListCount(pcidevs) > 0) {
|
|
|
|
virPCIDevicePtr dev = virPCIDeviceListStealIndex(pcidevs, 0);
|
2010-12-16 16:10:54 +00:00
|
|
|
qemuReattachPciDevice(dev, driver);
|
|
|
|
}
|
|
|
|
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectUnref(pcidevs);
|
2013-01-10 21:03:14 +00:00
|
|
|
cleanup:
|
2013-01-16 12:09:58 +00:00
|
|
|
virObjectUnlock(driver->activePciHostdevs);
|
|
|
|
virObjectUnlock(driver->inactivePciHostdevs);
|
2013-01-10 21:03:14 +00:00
|
|
|
virObjectUnref(cfg);
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
2012-03-26 14:40:01 +00:00
|
|
|
static void
|
2012-11-28 16:43:10 +00:00
|
|
|
qemuDomainReAttachHostUsbDevices(virQEMUDriverPtr driver,
|
2012-03-26 14:40:01 +00:00
|
|
|
const char *name,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2012-03-26 14:40:01 +00:00
|
|
|
|
2013-01-16 12:09:58 +00:00
|
|
|
virObjectLock(driver->activeUsbHostdevs);
|
2012-03-26 14:40:01 +00:00
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDevicePtr usb, tmp;
|
2012-03-26 14:40:01 +00:00
|
|
|
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;
|
2012-10-04 14:18:16 +00:00
|
|
|
if (hostdev->missing)
|
|
|
|
continue;
|
2012-03-26 14:40:01 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
usb = virUSBDeviceNew(hostdev->source.subsys.u.usb.bus,
|
|
|
|
hostdev->source.subsys.u.usb.device,
|
|
|
|
NULL);
|
2012-03-26 14:40:01 +00:00
|
|
|
|
|
|
|
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 */
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
tmp = virUSBDeviceListFind(driver->activeUsbHostdevs, usb);
|
|
|
|
virUSBDeviceFree(usb);
|
2012-03-26 14:40:01 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
used_by = virUSBDeviceGetUsedBy(tmp);
|
2012-03-26 14:40:01 +00:00
|
|
|
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);
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virUSBDeviceListDel(driver->activeUsbHostdevs, tmp);
|
2012-03-26 14:40:01 +00:00
|
|
|
}
|
|
|
|
}
|
2013-01-16 12:09:58 +00:00
|
|
|
virObjectUnlock(driver->activeUsbHostdevs);
|
2012-03-26 14:40:01 +00:00
|
|
|
}
|
2010-12-16 16:10:54 +00:00
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
|
2013-05-03 18:07:29 +00:00
|
|
|
void
|
|
|
|
qemuDomainReAttachHostScsiDevices(virQEMUDriverPtr driver,
|
|
|
|
const char *name,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/qemu files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2013-05-03 18:07:29 +00:00
|
|
|
|
|
|
|
virObjectLock(driver->activeScsiHostdevs);
|
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
2014-01-29 17:22:42 +00:00
|
|
|
virSCSIDevicePtr scsi;
|
|
|
|
virSCSIDevicePtr tmp;
|
2013-05-03 18:07:38 +00:00
|
|
|
virDomainDeviceDef dev;
|
|
|
|
|
|
|
|
dev.type = VIR_DOMAIN_DEVICE_HOSTDEV;
|
|
|
|
dev.data.hostdev = hostdev;
|
|
|
|
|
|
|
|
ignore_value(qemuRemoveSharedDevice(driver, &dev, name));
|
2013-05-03 18:07:29 +00:00
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
|
|
|
|
hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI)
|
|
|
|
continue;
|
|
|
|
|
2014-01-30 07:05:59 +00:00
|
|
|
if (!(scsi = virSCSIDeviceNew(NULL,
|
|
|
|
hostdev->source.subsys.u.scsi.adapter,
|
2013-05-03 18:07:29 +00:00
|
|
|
hostdev->source.subsys.u.scsi.bus,
|
|
|
|
hostdev->source.subsys.u.scsi.target,
|
|
|
|
hostdev->source.subsys.u.scsi.unit,
|
2014-01-08 14:51:29 +00:00
|
|
|
hostdev->readonly,
|
|
|
|
hostdev->shareable))) {
|
2013-05-03 18:07:29 +00:00
|
|
|
VIR_WARN("Unable to reattach SCSI device %s:%d:%d:%d on domain %s",
|
|
|
|
hostdev->source.subsys.u.scsi.adapter,
|
|
|
|
hostdev->source.subsys.u.scsi.bus,
|
|
|
|
hostdev->source.subsys.u.scsi.target,
|
|
|
|
hostdev->source.subsys.u.scsi.unit,
|
|
|
|
name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only delete the devices which are marked as being used by @name,
|
|
|
|
* because qemuProcessStart could fail on the half way. */
|
|
|
|
|
2014-01-29 17:22:42 +00:00
|
|
|
if (!(tmp = virSCSIDeviceListFind(driver->activeScsiHostdevs, scsi))) {
|
2013-05-03 18:07:29 +00:00
|
|
|
VIR_WARN("Unable to find device %s:%d:%d:%d "
|
|
|
|
"in list of active SCSI devices",
|
|
|
|
hostdev->source.subsys.u.scsi.adapter,
|
|
|
|
hostdev->source.subsys.u.scsi.bus,
|
|
|
|
hostdev->source.subsys.u.scsi.target,
|
|
|
|
hostdev->source.subsys.u.scsi.unit);
|
2014-01-29 17:22:42 +00:00
|
|
|
virSCSIDeviceFree(scsi);
|
2013-05-03 18:07:29 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-01-29 17:22:42 +00:00
|
|
|
VIR_DEBUG("Removing %s:%d:%d:%d dom=%s from activeScsiHostdevs",
|
|
|
|
hostdev->source.subsys.u.scsi.adapter,
|
|
|
|
hostdev->source.subsys.u.scsi.bus,
|
|
|
|
hostdev->source.subsys.u.scsi.target,
|
|
|
|
hostdev->source.subsys.u.scsi.unit,
|
|
|
|
name);
|
2013-05-03 18:07:29 +00:00
|
|
|
|
2014-01-29 17:22:42 +00:00
|
|
|
virSCSIDeviceListDel(driver->activeScsiHostdevs, tmp, name);
|
|
|
|
virSCSIDeviceFree(scsi);
|
2013-05-03 18:07:29 +00:00
|
|
|
}
|
|
|
|
virObjectUnlock(driver->activeScsiHostdevs);
|
|
|
|
}
|
|
|
|
|
2013-10-08 13:47:36 +00:00
|
|
|
void
|
|
|
|
qemuDomainReAttachHostDevices(virQEMUDriverPtr driver,
|
|
|
|
virDomainDefPtr def)
|
2010-12-16 16:10:54 +00:00
|
|
|
{
|
|
|
|
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);
|
2013-05-03 18:07:29 +00:00
|
|
|
|
|
|
|
qemuDomainReAttachHostScsiDevices(driver, def->name, def->hostdevs,
|
|
|
|
def->nhostdevs);
|
2010-12-16 16:10:54 +00:00
|
|
|
}
|