2014-03-01 06:28:59 +00:00
|
|
|
/* virhostdev.c: hostdev management
|
|
|
|
*
|
2017-02-03 13:04:59 +00:00
|
|
|
* Copyright (C) 2006-2007, 2009-2017 Red Hat, Inc.
|
2014-03-05 12:14:38 +00:00
|
|
|
* Copyright (C) 2006 Daniel P. Berrange
|
2014-03-01 06:28:59 +00:00
|
|
|
* Copyright (C) 2014 SUSE LINUX Products GmbH, Nuernberg, Germany.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library. If not, see
|
|
|
|
* <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "virhostdev.h"
|
|
|
|
#include "viralloc.h"
|
|
|
|
#include "virstring.h"
|
|
|
|
#include "virfile.h"
|
|
|
|
#include "virerror.h"
|
|
|
|
#include "virlog.h"
|
|
|
|
#include "virutil.h"
|
2014-03-05 12:14:38 +00:00
|
|
|
#include "virnetdev.h"
|
2014-03-01 06:28:59 +00:00
|
|
|
#include "configmake.h"
|
|
|
|
|
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
2014-02-28 12:16:17 +00:00
|
|
|
|
|
|
|
VIR_LOG_INIT("util.hostdev");
|
|
|
|
|
2019-08-20 15:05:12 +00:00
|
|
|
#define HOSTDEV_STATE_DIR RUNSTATEDIR "/libvirt/hostdevmgr"
|
2014-03-01 06:28:59 +00:00
|
|
|
|
2014-03-06 04:14:21 +00:00
|
|
|
static virHostdevManagerPtr manager; /* global hostdev manager, never freed */
|
|
|
|
|
|
|
|
static virClassPtr virHostdevManagerClass;
|
|
|
|
static void virHostdevManagerDispose(void *obj);
|
|
|
|
static virHostdevManagerPtr virHostdevManagerNew(void);
|
|
|
|
|
2015-07-14 11:56:33 +00:00
|
|
|
struct virHostdevIsPCINodeDeviceUsedData {
|
2016-03-01 18:53:37 +00:00
|
|
|
virHostdevManagerPtr mgr;
|
2019-08-14 09:13:21 +00:00
|
|
|
const char *driverName;
|
2015-07-14 11:56:33 +00:00
|
|
|
const char *domainName;
|
virhostdev: Unify virHostdevPreparePCIDevices behaviour for KVM and VFIO cases
The virHostdevPreparePCIDevices() function works in several
steps. In the very first one, it checks if devices we want to
detach from the host are not taken already by some other domain.
However, this piece of code returns different results depending
on the stub driver used (which is not wrong per se, but keep on
reading). If the stub driver is KVM then
virHostdevIsPCINodeDeviceUsed() is called which basically checks
if a PCI device from the detach list is not used by any domain
(including the one we are preparing the device for). If that is
the case, an error is reported ("device in use") and -1 is
returned.
However, that is not what happens if the stub driver is VFIO. If
the stub driver is VFIO, then we iterate over all PCI devices
from the same IOMMU group and check if they are taken by some
other domain (because a PCI device, well IOMMU group, can't be
shared between two or more qemu processes). But we fail to check,
if the device we are trying to detach from the host is not
already taken by a domain. That is, calling
virHostdevPreparePCIDevices() over a hostdev device twice
succeeds the first time and fails too late in the second run
(fortunately, virHostdevResetAllPCIDevices() will throw an error,
but this is already too late because the PCI device in question
was moved to the list of inactive PCI devices and now it appears
in both lists).
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2019-08-14 09:28:30 +00:00
|
|
|
bool usesVFIO;
|
2015-07-14 11:56:33 +00:00
|
|
|
};
|
|
|
|
|
2016-03-07 12:41:19 +00:00
|
|
|
/* This module makes heavy use of bookkeeping lists contained inside a
|
|
|
|
* virHostdevManager instance to keep track of the devices' status. To make
|
|
|
|
* it easy to spot potential ownership errors when moving devices from one
|
|
|
|
* list to the other, variable names should comply with the following
|
|
|
|
* conventions when it comes to virPCIDevice and virPCIDeviceList instances:
|
|
|
|
*
|
|
|
|
* pci - a short-lived virPCIDevice whose purpose is usually just to look
|
|
|
|
* up the actual PCI device in one of the bookkeeping lists; basically
|
|
|
|
* little more than a fancy virPCIDeviceAddress
|
|
|
|
*
|
|
|
|
* pcidevs - a list containing a bunch of the above
|
|
|
|
*
|
|
|
|
* actual - a virPCIDevice instance that has either been retrieved from one
|
|
|
|
* of the bookkeeping lists, or is intended to be added or copied
|
|
|
|
* to one at some point
|
|
|
|
*
|
|
|
|
* Passing an 'actual' to a function that requires a 'pci' is fine, but the
|
|
|
|
* opposite is usually not true; as a rule of thumb, functions in the virpci
|
|
|
|
* module usually expect an 'actual'. Even with these conventions in place,
|
|
|
|
* adding comments to highlight ownership-related issues is recommended */
|
|
|
|
|
2015-01-14 11:03:28 +00:00
|
|
|
static int virHostdevIsPCINodeDeviceUsed(virPCIDeviceAddressPtr devAddr, void *opaque)
|
|
|
|
{
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDevicePtr actual;
|
2015-07-14 11:56:33 +00:00
|
|
|
struct virHostdevIsPCINodeDeviceUsedData *helperData = opaque;
|
2015-01-14 11:03:28 +00:00
|
|
|
|
2016-02-24 13:59:25 +00:00
|
|
|
actual = virPCIDeviceListFindByIDs(helperData->mgr->activePCIHostdevs,
|
|
|
|
devAddr->domain, devAddr->bus,
|
|
|
|
devAddr->slot, devAddr->function);
|
|
|
|
if (actual) {
|
|
|
|
const char *actual_drvname = NULL;
|
|
|
|
const char *actual_domname = NULL;
|
|
|
|
virPCIDeviceGetUsedBy(actual, &actual_drvname, &actual_domname);
|
2015-01-14 11:03:28 +00:00
|
|
|
|
2016-03-01 19:13:20 +00:00
|
|
|
if (helperData->usesVFIO &&
|
2019-08-14 09:13:21 +00:00
|
|
|
STREQ_NULLABLE(actual_drvname, helperData->driverName) &&
|
|
|
|
STREQ_NULLABLE(actual_domname, helperData->domainName))
|
2015-07-14 11:56:33 +00:00
|
|
|
goto iommu_owner;
|
|
|
|
|
2016-02-24 13:59:25 +00:00
|
|
|
if (actual_drvname && actual_domname)
|
2015-01-14 11:03:28 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("PCI device %s is in use by "
|
|
|
|
"driver %s, domain %s"),
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDeviceGetName(actual),
|
|
|
|
actual_drvname, actual_domname);
|
2015-01-14 11:03:28 +00:00
|
|
|
else
|
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("PCI device %s is in use"),
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDeviceGetName(actual));
|
2019-10-21 18:19:04 +00:00
|
|
|
return -1;
|
2015-01-14 11:03:28 +00:00
|
|
|
}
|
2015-07-14 11:56:33 +00:00
|
|
|
iommu_owner:
|
2019-10-21 18:19:04 +00:00
|
|
|
return 0;
|
2015-01-14 11:03:28 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 04:14:21 +00:00
|
|
|
static int virHostdevManagerOnceInit(void)
|
|
|
|
{
|
2018-04-17 15:42:33 +00:00
|
|
|
if (!VIR_CLASS_NEW(virHostdevManager, virClassForObject()))
|
2014-03-06 04:14:21 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (!(manager = virHostdevManagerNew()))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-20 17:23:29 +00:00
|
|
|
VIR_ONCE_GLOBAL_INIT(virHostdevManager);
|
2014-03-01 06:28:59 +00:00
|
|
|
|
|
|
|
static void
|
2014-03-06 04:14:21 +00:00
|
|
|
virHostdevManagerDispose(void *obj)
|
2014-03-01 06:28:59 +00:00
|
|
|
{
|
2014-03-06 04:14:21 +00:00
|
|
|
virHostdevManagerPtr hostdevMgr = obj;
|
|
|
|
|
2014-03-12 16:38:18 +00:00
|
|
|
virObjectUnref(hostdevMgr->activePCIHostdevs);
|
|
|
|
virObjectUnref(hostdevMgr->inactivePCIHostdevs);
|
|
|
|
virObjectUnref(hostdevMgr->activeUSBHostdevs);
|
|
|
|
virObjectUnref(hostdevMgr->activeSCSIHostdevs);
|
2016-11-22 03:58:17 +00:00
|
|
|
virObjectUnref(hostdevMgr->activeSCSIVHostHostdevs);
|
2017-02-03 13:04:59 +00:00
|
|
|
virObjectUnref(hostdevMgr->activeMediatedHostdevs);
|
2019-06-06 09:58:12 +00:00
|
|
|
virObjectUnref(hostdevMgr->activeNVMeHostdevs);
|
2014-03-01 06:28:59 +00:00
|
|
|
VIR_FREE(hostdevMgr->stateDir);
|
|
|
|
}
|
|
|
|
|
2014-03-06 04:14:21 +00:00
|
|
|
static virHostdevManagerPtr
|
|
|
|
virHostdevManagerNew(void)
|
2014-03-01 06:28:59 +00:00
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virHostdevManager) hostdevMgr = NULL;
|
2014-03-28 08:44:34 +00:00
|
|
|
bool privileged = geteuid() == 0;
|
2014-03-06 04:14:21 +00:00
|
|
|
|
|
|
|
if (!(hostdevMgr = virObjectNew(virHostdevManagerClass)))
|
|
|
|
return NULL;
|
2014-03-01 06:28:59 +00:00
|
|
|
|
2016-02-25 13:50:54 +00:00
|
|
|
if (!(hostdevMgr->activePCIHostdevs = virPCIDeviceListNew()))
|
2019-06-15 07:03:47 +00:00
|
|
|
return NULL;
|
2014-03-01 06:28:59 +00:00
|
|
|
|
2016-02-25 13:50:54 +00:00
|
|
|
if (!(hostdevMgr->activeUSBHostdevs = virUSBDeviceListNew()))
|
2019-06-15 07:03:47 +00:00
|
|
|
return NULL;
|
2014-03-01 06:28:59 +00:00
|
|
|
|
2016-02-25 13:50:54 +00:00
|
|
|
if (!(hostdevMgr->inactivePCIHostdevs = virPCIDeviceListNew()))
|
2019-06-15 07:03:47 +00:00
|
|
|
return NULL;
|
2014-03-01 06:28:59 +00:00
|
|
|
|
2016-02-25 13:50:54 +00:00
|
|
|
if (!(hostdevMgr->activeSCSIHostdevs = virSCSIDeviceListNew()))
|
2019-06-15 07:03:47 +00:00
|
|
|
return NULL;
|
2014-03-01 06:28:59 +00:00
|
|
|
|
2016-11-22 03:58:17 +00:00
|
|
|
if (!(hostdevMgr->activeSCSIVHostHostdevs = virSCSIVHostDeviceListNew()))
|
2019-06-15 07:03:47 +00:00
|
|
|
return NULL;
|
2016-11-22 03:58:17 +00:00
|
|
|
|
2017-02-03 13:04:59 +00:00
|
|
|
if (!(hostdevMgr->activeMediatedHostdevs = virMediatedDeviceListNew()))
|
2019-06-15 07:03:47 +00:00
|
|
|
return NULL;
|
2017-02-03 13:04:59 +00:00
|
|
|
|
2019-06-06 09:58:12 +00:00
|
|
|
if (!(hostdevMgr->activeNVMeHostdevs = virNVMeDeviceListNew()))
|
|
|
|
return NULL;
|
|
|
|
|
2014-03-28 08:44:34 +00:00
|
|
|
if (privileged) {
|
2019-10-20 11:49:46 +00:00
|
|
|
hostdevMgr->stateDir = g_strdup(HOSTDEV_STATE_DIR);
|
2014-03-28 08:44:34 +00:00
|
|
|
|
|
|
|
if (virFileMakePath(hostdevMgr->stateDir) < 0) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED,
|
|
|
|
_("Failed to create state dir '%s'"),
|
|
|
|
hostdevMgr->stateDir);
|
2019-06-15 07:03:47 +00:00
|
|
|
return NULL;
|
2014-03-28 08:44:34 +00:00
|
|
|
}
|
|
|
|
} else {
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *rundir = NULL;
|
2014-03-28 08:44:34 +00:00
|
|
|
mode_t old_umask;
|
|
|
|
|
2019-12-19 09:16:21 +00:00
|
|
|
rundir = virGetUserRuntimeDirectory();
|
2014-03-28 08:44:34 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
hostdevMgr->stateDir = g_strdup_printf("%s/hostdevmgr", rundir);
|
2014-03-28 08:44:34 +00:00
|
|
|
|
|
|
|
old_umask = umask(077);
|
|
|
|
|
|
|
|
if (virFileMakePath(hostdevMgr->stateDir) < 0) {
|
|
|
|
umask(old_umask);
|
|
|
|
virReportError(VIR_ERR_OPERATION_FAILED,
|
|
|
|
_("Failed to create state dir '%s'"),
|
|
|
|
hostdevMgr->stateDir);
|
2019-06-15 07:03:47 +00:00
|
|
|
return NULL;
|
2014-03-28 08:44:34 +00:00
|
|
|
}
|
|
|
|
umask(old_umask);
|
2014-03-01 06:28:59 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 11:35:54 +00:00
|
|
|
return g_steal_pointer(&hostdevMgr);
|
2014-03-01 06:28:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virHostdevManagerPtr
|
|
|
|
virHostdevManagerGetDefault(void)
|
|
|
|
{
|
2014-03-06 04:14:21 +00:00
|
|
|
if (virHostdevManagerInitialize() < 0)
|
2014-03-01 06:28:59 +00:00
|
|
|
return NULL;
|
2014-03-06 04:14:21 +00:00
|
|
|
|
|
|
|
return virObjectRef(manager);
|
2014-03-01 06:28:59 +00:00
|
|
|
}
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2019-08-19 09:04:05 +00:00
|
|
|
/**
|
|
|
|
* virHostdevGetPCIHostDevice:
|
|
|
|
* @hostdev: domain hostdev definition
|
|
|
|
* @pci: returned PCI device
|
|
|
|
*
|
|
|
|
* For given @hostdev which represents a PCI device construct its
|
|
|
|
* virPCIDevice representation and return it in @pci. If @hostdev
|
|
|
|
* does not represent a PCI device then @pci is set to NULL and 0
|
|
|
|
* is returned.
|
|
|
|
*
|
|
|
|
* Returns: 0 on success (@pci might be NULL though),
|
|
|
|
* -1 otherwise (with error reported).
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
virHostdevGetPCIHostDevice(const virDomainHostdevDef *hostdev,
|
|
|
|
virPCIDevicePtr *pci)
|
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) actual = NULL;
|
2019-08-19 09:04:05 +00:00
|
|
|
const virDomainHostdevSubsysPCI *pcisrc = &hostdev->source.subsys.u.pci;
|
|
|
|
|
|
|
|
*pci = NULL;
|
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
|
|
|
|
hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
actual = virPCIDeviceNew(pcisrc->addr.domain, pcisrc->addr.bus,
|
|
|
|
pcisrc->addr.slot, pcisrc->addr.function);
|
|
|
|
|
|
|
|
if (!actual)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
virPCIDeviceSetManaged(actual, hostdev->managed);
|
|
|
|
|
2019-08-19 09:47:19 +00:00
|
|
|
if (pcisrc->backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_VFIO) {
|
2019-08-19 09:04:05 +00:00
|
|
|
virPCIDeviceSetStubDriver(actual, VIR_PCI_STUB_DRIVER_VFIO);
|
2019-08-19 09:47:19 +00:00
|
|
|
} else if (pcisrc->backend == VIR_DOMAIN_HOSTDEV_PCI_BACKEND_XEN) {
|
2019-08-19 09:04:05 +00:00
|
|
|
virPCIDeviceSetStubDriver(actual, VIR_PCI_STUB_DRIVER_XEN);
|
2019-08-19 09:47:19 +00:00
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("pci backend driver '%s' is not supported"),
|
|
|
|
virDomainHostdevSubsysPCIBackendTypeToString(pcisrc->backend));
|
|
|
|
return -1;
|
|
|
|
}
|
2019-08-19 09:04:05 +00:00
|
|
|
|
2019-10-16 11:43:52 +00:00
|
|
|
*pci = g_steal_pointer(&actual);
|
2019-08-19 09:04:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-05 12:14:38 +00:00
|
|
|
static virPCIDeviceListPtr
|
2014-03-06 07:35:03 +00:00
|
|
|
virHostdevGetPCIHostDeviceList(virDomainHostdevDefPtr *hostdevs, int nhostdevs)
|
2014-03-05 12:14:38 +00:00
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDeviceList) pcidevs = NULL;
|
2014-03-05 12:14:38 +00:00
|
|
|
size_t i;
|
|
|
|
|
2016-02-24 13:59:25 +00:00
|
|
|
if (!(pcidevs = virPCIDeviceListNew()))
|
2014-03-05 12:14:38 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) pci = NULL;
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2019-08-19 09:04:05 +00:00
|
|
|
if (virHostdevGetPCIHostDevice(hostdev, &pci) < 0)
|
2014-03-05 12:14:38 +00:00
|
|
|
return NULL;
|
|
|
|
|
2019-08-19 09:04:05 +00:00
|
|
|
if (!pci)
|
|
|
|
continue;
|
2019-06-15 06:25:33 +00:00
|
|
|
|
2019-06-15 07:03:47 +00:00
|
|
|
if (virPCIDeviceListAdd(pcidevs, pci) < 0)
|
2019-06-15 06:25:33 +00:00
|
|
|
return NULL;
|
2019-06-15 06:28:05 +00:00
|
|
|
pci = NULL;
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
|
2019-10-16 11:35:54 +00:00
|
|
|
return g_steal_pointer(&pcidevs);
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2014-03-06 07:35:03 +00:00
|
|
|
virHostdevPCISysfsPath(virDomainHostdevDefPtr hostdev,
|
2014-03-05 12:14:38 +00:00
|
|
|
char **sysfs_path)
|
|
|
|
{
|
|
|
|
virPCIDeviceAddress config_address;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
return virPCIDeviceAddressGetSysfsFile(&config_address, sysfs_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virHostdevIsVirtualFunction(virDomainHostdevDefPtr hostdev)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *sysfs_path = NULL;
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2014-03-06 07:35:03 +00:00
|
|
|
if (virHostdevPCISysfsPath(hostdev, &sysfs_path) < 0)
|
2018-07-24 15:52:33 +00:00
|
|
|
return -1;
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2018-07-24 15:52:33 +00:00
|
|
|
return virPCIIsVirtualFunction(sysfs_path);
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
util: save the correct VF's info when using a dual port SRIOV NIC in single port mode
Mellanox ConnectX-3 dual port SRIOV NICs present a bit of a challenge
when assigning one of their VFs to a guest using VFIO device
assignment.
These NICs have only a single PCI PF device, and that single PF has
two netdevs sharing the single PCI address - one for port 1 and one
for port 2. When a VF is created it can also have 2 netdevs, or it can
be setup in "single port" mode, where the VF has only a single netdev,
and that netdev is connected either to port 1 or to port 2.
When the VF is created in dual port mode, you get/set the MAC
address/vlan tag for the port 1 VF by sending a netlink message to the
PF's port1 netdev, and you get/set the MAC address/vlan tag for the
port 2 VF by sending a netlink message to the PF's port 2 netdev. (Of
course libvirt doesn't have any way to describe MAC/vlan info for 2
ports in a single hostdev interface, so that's a bit of a moot point)
When the VF is created in single port mode, you can *set* the MAC/vlan
info by sending a netlink message to *either* PF netdev - the driver
is smart enough to understand that there's only a single netdev, and
set the MAC/vlan for that netdev. When you want to *get* it, however,
the driver is more accurate - it will return 00:00:00:00:00:00 for the
MAC if you request it from the port 1 PF netdev when the VF was
configured to be single port on port 2, or if you request if from the
port 2 PF netdev when the VF was configured to be single port on port
1.
Based on this information, when *getting* the MAC/vlan info (to save
the original setting prior to assignment), we determine the correct PF
netdev by matching phys_port_id between VF and PF.
(IMPORTANT NOTE: this implies that to do PCI device assignment of the
VFs on dual port Mellanox cards using <interface type='hostdev'>
(i.e. if you want the MAC address/vlan tag to be set), not only must
the VFs be configured in single port mode, but also the VFs *must* be
bound to the host VF net driver, and libvirt must use managed='yes')
By the time libvirt is ready to set the new MAC/vlan tag, the VF has
already been unbound from the host net driver and bound to
vfio-pci. This isn't problematic though because, as stated earlier,
when a VF is created in single port mode, commands to configure it can
be sent to either the port 1 PF netdev or the port 2 PF netdev.
When it is time to restore the original MAC/vlan tag, again the VF
will *not* be bound to a host net driver, so it won't be possible to
learn from sysfs whether to use the port 1 or port 2 PF netdev for the
netlink commands. And again, it doesn't matter which netdev you
use. However, we must keep in mind that we saved the original settings
to a file called "${PF}_${VFNUM}". To solve this problem, we just
check for the existence of ${PF1}_${VFNUM} and ${PF2}_${VFNUM}, and
use whichever one we find (since we know that only one can be there)
2017-08-08 00:25:57 +00:00
|
|
|
virHostdevNetDevice(virDomainHostdevDefPtr hostdev,
|
|
|
|
int pfNetDevIdx,
|
|
|
|
char **linkdev,
|
2014-03-05 12:14:38 +00:00
|
|
|
int *vf)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *sysfs_path = NULL;
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2014-03-06 07:35:03 +00:00
|
|
|
if (virHostdevPCISysfsPath(hostdev, &sysfs_path) < 0)
|
2018-07-24 15:52:33 +00:00
|
|
|
return -1;
|
2014-03-05 12:14:38 +00:00
|
|
|
|
|
|
|
if (virPCIIsVirtualFunction(sysfs_path) == 1) {
|
util: save the correct VF's info when using a dual port SRIOV NIC in single port mode
Mellanox ConnectX-3 dual port SRIOV NICs present a bit of a challenge
when assigning one of their VFs to a guest using VFIO device
assignment.
These NICs have only a single PCI PF device, and that single PF has
two netdevs sharing the single PCI address - one for port 1 and one
for port 2. When a VF is created it can also have 2 netdevs, or it can
be setup in "single port" mode, where the VF has only a single netdev,
and that netdev is connected either to port 1 or to port 2.
When the VF is created in dual port mode, you get/set the MAC
address/vlan tag for the port 1 VF by sending a netlink message to the
PF's port1 netdev, and you get/set the MAC address/vlan tag for the
port 2 VF by sending a netlink message to the PF's port 2 netdev. (Of
course libvirt doesn't have any way to describe MAC/vlan info for 2
ports in a single hostdev interface, so that's a bit of a moot point)
When the VF is created in single port mode, you can *set* the MAC/vlan
info by sending a netlink message to *either* PF netdev - the driver
is smart enough to understand that there's only a single netdev, and
set the MAC/vlan for that netdev. When you want to *get* it, however,
the driver is more accurate - it will return 00:00:00:00:00:00 for the
MAC if you request it from the port 1 PF netdev when the VF was
configured to be single port on port 2, or if you request if from the
port 2 PF netdev when the VF was configured to be single port on port
1.
Based on this information, when *getting* the MAC/vlan info (to save
the original setting prior to assignment), we determine the correct PF
netdev by matching phys_port_id between VF and PF.
(IMPORTANT NOTE: this implies that to do PCI device assignment of the
VFs on dual port Mellanox cards using <interface type='hostdev'>
(i.e. if you want the MAC address/vlan tag to be set), not only must
the VFs be configured in single port mode, but also the VFs *must* be
bound to the host VF net driver, and libvirt must use managed='yes')
By the time libvirt is ready to set the new MAC/vlan tag, the VF has
already been unbound from the host net driver and bound to
vfio-pci. This isn't problematic though because, as stated earlier,
when a VF is created in single port mode, commands to configure it can
be sent to either the port 1 PF netdev or the port 2 PF netdev.
When it is time to restore the original MAC/vlan tag, again the VF
will *not* be bound to a host net driver, so it won't be possible to
learn from sysfs whether to use the port 1 or port 2 PF netdev for the
netlink commands. And again, it doesn't matter which netdev you
use. However, we must keep in mind that we saved the original settings
to a file called "${PF}_${VFNUM}". To solve this problem, we just
check for the existence of ${PF1}_${VFNUM} and ${PF2}_${VFNUM}, and
use whichever one we find (since we know that only one can be there)
2017-08-08 00:25:57 +00:00
|
|
|
if (virPCIGetVirtualFunctionInfo(sysfs_path, pfNetDevIdx,
|
2018-07-24 15:52:33 +00:00
|
|
|
linkdev, vf) < 0)
|
|
|
|
return -1;
|
2014-03-05 12:14:38 +00:00
|
|
|
} else {
|
2017-03-03 16:54:59 +00:00
|
|
|
/* In practice this should never happen, since we currently
|
|
|
|
* only support assigning SRIOV VFs via <interface
|
|
|
|
* type='hostdev'>, and it is only those devices that should
|
|
|
|
* end up calling this function.
|
|
|
|
*/
|
2017-07-31 04:21:45 +00:00
|
|
|
if (virPCIGetNetName(sysfs_path, 0, NULL, linkdev) < 0)
|
2018-07-24 15:52:33 +00:00
|
|
|
return -1;
|
2017-03-03 16:54:59 +00:00
|
|
|
|
2019-01-22 19:26:15 +00:00
|
|
|
if (!(*linkdev)) {
|
2017-03-03 16:54:59 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("The device at %s has no network device name"),
|
2018-09-19 08:38:14 +00:00
|
|
|
sysfs_path);
|
2018-07-24 15:52:33 +00:00
|
|
|
return -1;
|
2017-03-03 16:54:59 +00:00
|
|
|
}
|
|
|
|
|
2014-03-05 12:14:38 +00:00
|
|
|
*vf = -1;
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:52:33 +00:00
|
|
|
return 0;
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-02-25 14:39:25 +00:00
|
|
|
static bool
|
2019-08-06 13:38:06 +00:00
|
|
|
virHostdevIsPCINetDevice(const virDomainHostdevDef *hostdev)
|
2015-04-22 11:33:09 +00:00
|
|
|
{
|
|
|
|
return hostdev->mode == VIR_DOMAIN_HOSTDEV_MODE_SUBSYS &&
|
|
|
|
hostdev->source.subsys.type == VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI &&
|
2018-07-26 16:24:30 +00:00
|
|
|
hostdev->parentnet != NULL;
|
2015-04-22 11:33:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-05 12:14:38 +00:00
|
|
|
static int
|
|
|
|
virHostdevNetConfigVirtPortProfile(const char *linkdev, int vf,
|
2019-10-01 17:56:35 +00:00
|
|
|
const virNetDevVPortProfile *virtPort,
|
2014-03-05 12:14:38 +00:00
|
|
|
const virMacAddr *macaddr,
|
|
|
|
const unsigned char *uuid,
|
|
|
|
bool associate)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!virtPort)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
switch (virtPort->virtPortType) {
|
|
|
|
case VIR_NETDEV_VPORT_PROFILE_NONE:
|
|
|
|
case VIR_NETDEV_VPORT_PROFILE_OPENVSWITCH:
|
|
|
|
case VIR_NETDEV_VPORT_PROFILE_8021QBG:
|
|
|
|
case VIR_NETDEV_VPORT_PROFILE_LAST:
|
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
|
|
|
|
_("virtualport type %s is "
|
|
|
|
"currently not supported on interfaces of type "
|
|
|
|
"hostdev"),
|
|
|
|
virNetDevVPortTypeToString(virtPort->virtPortType));
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VIR_NETDEV_VPORT_PROFILE_8021QBH:
|
|
|
|
if (associate)
|
|
|
|
ret = virNetDevVPortProfileAssociate(NULL, virtPort, macaddr,
|
|
|
|
linkdev, vf, uuid,
|
|
|
|
VIR_NETDEV_VPORT_PROFILE_OP_CREATE, false);
|
|
|
|
else
|
|
|
|
ret = virNetDevVPortProfileDisassociate(NULL, virtPort,
|
|
|
|
macaddr, linkdev, vf,
|
|
|
|
VIR_NETDEV_VPORT_PROFILE_OP_DESTROY);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-02-28 18:47:12 +00:00
|
|
|
/**
|
|
|
|
* virHostdevSaveNetConfig:
|
|
|
|
* @hostdev: config object describing a hostdev device
|
|
|
|
* @stateDir: directory to save device state into
|
|
|
|
*
|
|
|
|
* If the given hostdev device is an SRIOV network VF and *does not*
|
|
|
|
* have a <virtualport> element (ie, it isn't being configured via
|
|
|
|
* 802.11Qbh), determine its PF+VF#, and use that to save its current
|
|
|
|
* "admin" MAC address and VF tag (the ones saved in the PF
|
|
|
|
* driver).
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure.
|
|
|
|
*/
|
2014-03-05 12:14:38 +00:00
|
|
|
static int
|
2017-02-28 18:47:12 +00:00
|
|
|
virHostdevSaveNetConfig(virDomainHostdevDefPtr hostdev,
|
|
|
|
const char *stateDir)
|
2014-03-05 12:14:38 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *linkdev = NULL;
|
2014-03-05 12:14:38 +00:00
|
|
|
int vf = -1;
|
2017-02-28 18:47:12 +00:00
|
|
|
|
|
|
|
if (!virHostdevIsPCINetDevice(hostdev) ||
|
2018-07-26 16:24:30 +00:00
|
|
|
virDomainNetGetActualVirtPortProfile(hostdev->parentnet))
|
2017-02-28 18:47:12 +00:00
|
|
|
return 0;
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2016-02-24 17:06:52 +00:00
|
|
|
if (virHostdevIsVirtualFunction(hostdev) != 1) {
|
2014-03-05 12:14:38 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("Interface type hostdev is currently supported on"
|
|
|
|
" SR-IOV Virtual Functions only"));
|
2018-07-24 15:52:33 +00:00
|
|
|
return -1;
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
|
util: save the correct VF's info when using a dual port SRIOV NIC in single port mode
Mellanox ConnectX-3 dual port SRIOV NICs present a bit of a challenge
when assigning one of their VFs to a guest using VFIO device
assignment.
These NICs have only a single PCI PF device, and that single PF has
two netdevs sharing the single PCI address - one for port 1 and one
for port 2. When a VF is created it can also have 2 netdevs, or it can
be setup in "single port" mode, where the VF has only a single netdev,
and that netdev is connected either to port 1 or to port 2.
When the VF is created in dual port mode, you get/set the MAC
address/vlan tag for the port 1 VF by sending a netlink message to the
PF's port1 netdev, and you get/set the MAC address/vlan tag for the
port 2 VF by sending a netlink message to the PF's port 2 netdev. (Of
course libvirt doesn't have any way to describe MAC/vlan info for 2
ports in a single hostdev interface, so that's a bit of a moot point)
When the VF is created in single port mode, you can *set* the MAC/vlan
info by sending a netlink message to *either* PF netdev - the driver
is smart enough to understand that there's only a single netdev, and
set the MAC/vlan for that netdev. When you want to *get* it, however,
the driver is more accurate - it will return 00:00:00:00:00:00 for the
MAC if you request it from the port 1 PF netdev when the VF was
configured to be single port on port 2, or if you request if from the
port 2 PF netdev when the VF was configured to be single port on port
1.
Based on this information, when *getting* the MAC/vlan info (to save
the original setting prior to assignment), we determine the correct PF
netdev by matching phys_port_id between VF and PF.
(IMPORTANT NOTE: this implies that to do PCI device assignment of the
VFs on dual port Mellanox cards using <interface type='hostdev'>
(i.e. if you want the MAC address/vlan tag to be set), not only must
the VFs be configured in single port mode, but also the VFs *must* be
bound to the host VF net driver, and libvirt must use managed='yes')
By the time libvirt is ready to set the new MAC/vlan tag, the VF has
already been unbound from the host net driver and bound to
vfio-pci. This isn't problematic though because, as stated earlier,
when a VF is created in single port mode, commands to configure it can
be sent to either the port 1 PF netdev or the port 2 PF netdev.
When it is time to restore the original MAC/vlan tag, again the VF
will *not* be bound to a host net driver, so it won't be possible to
learn from sysfs whether to use the port 1 or port 2 PF netdev for the
netlink commands. And again, it doesn't matter which netdev you
use. However, we must keep in mind that we saved the original settings
to a file called "${PF}_${VFNUM}". To solve this problem, we just
check for the existence of ${PF1}_${VFNUM} and ${PF2}_${VFNUM}, and
use whichever one we find (since we know that only one can be there)
2017-08-08 00:25:57 +00:00
|
|
|
if (virHostdevNetDevice(hostdev, -1, &linkdev, &vf) < 0)
|
2018-07-24 15:52:33 +00:00
|
|
|
return -1;
|
2017-02-28 18:47:12 +00:00
|
|
|
|
|
|
|
if (virNetDevSaveNetConfig(linkdev, vf, stateDir, true) < 0)
|
2018-07-24 15:52:33 +00:00
|
|
|
return -1;
|
2017-02-28 18:47:12 +00:00
|
|
|
|
2018-07-24 15:52:33 +00:00
|
|
|
return 0;
|
2017-02-28 18:47:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virHostdevSetNetConfig:
|
|
|
|
* @hostdev: config object describing a hostdev device
|
|
|
|
* @uuid: uuid of the domain
|
|
|
|
*
|
|
|
|
* If the given hostdev device is an SRIOV network VF, determine its
|
|
|
|
* PF+VF#, and use that to set the "admin" MAC address and VF tag (the
|
|
|
|
* ones saved in the PF driver).xs
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
virHostdevSetNetConfig(virDomainHostdevDefPtr hostdev,
|
|
|
|
const unsigned char *uuid)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *linkdev = NULL;
|
2019-10-01 16:25:47 +00:00
|
|
|
const virNetDevVlan *vlan;
|
2019-10-01 17:56:35 +00:00
|
|
|
const virNetDevVPortProfile *virtPort;
|
2017-02-28 18:47:12 +00:00
|
|
|
int vf = -1;
|
|
|
|
bool port_profile_associate = true;
|
|
|
|
|
|
|
|
if (!virHostdevIsPCINetDevice(hostdev))
|
|
|
|
return 0;
|
|
|
|
|
util: save the correct VF's info when using a dual port SRIOV NIC in single port mode
Mellanox ConnectX-3 dual port SRIOV NICs present a bit of a challenge
when assigning one of their VFs to a guest using VFIO device
assignment.
These NICs have only a single PCI PF device, and that single PF has
two netdevs sharing the single PCI address - one for port 1 and one
for port 2. When a VF is created it can also have 2 netdevs, or it can
be setup in "single port" mode, where the VF has only a single netdev,
and that netdev is connected either to port 1 or to port 2.
When the VF is created in dual port mode, you get/set the MAC
address/vlan tag for the port 1 VF by sending a netlink message to the
PF's port1 netdev, and you get/set the MAC address/vlan tag for the
port 2 VF by sending a netlink message to the PF's port 2 netdev. (Of
course libvirt doesn't have any way to describe MAC/vlan info for 2
ports in a single hostdev interface, so that's a bit of a moot point)
When the VF is created in single port mode, you can *set* the MAC/vlan
info by sending a netlink message to *either* PF netdev - the driver
is smart enough to understand that there's only a single netdev, and
set the MAC/vlan for that netdev. When you want to *get* it, however,
the driver is more accurate - it will return 00:00:00:00:00:00 for the
MAC if you request it from the port 1 PF netdev when the VF was
configured to be single port on port 2, or if you request if from the
port 2 PF netdev when the VF was configured to be single port on port
1.
Based on this information, when *getting* the MAC/vlan info (to save
the original setting prior to assignment), we determine the correct PF
netdev by matching phys_port_id between VF and PF.
(IMPORTANT NOTE: this implies that to do PCI device assignment of the
VFs on dual port Mellanox cards using <interface type='hostdev'>
(i.e. if you want the MAC address/vlan tag to be set), not only must
the VFs be configured in single port mode, but also the VFs *must* be
bound to the host VF net driver, and libvirt must use managed='yes')
By the time libvirt is ready to set the new MAC/vlan tag, the VF has
already been unbound from the host net driver and bound to
vfio-pci. This isn't problematic though because, as stated earlier,
when a VF is created in single port mode, commands to configure it can
be sent to either the port 1 PF netdev or the port 2 PF netdev.
When it is time to restore the original MAC/vlan tag, again the VF
will *not* be bound to a host net driver, so it won't be possible to
learn from sysfs whether to use the port 1 or port 2 PF netdev for the
netlink commands. And again, it doesn't matter which netdev you
use. However, we must keep in mind that we saved the original settings
to a file called "${PF}_${VFNUM}". To solve this problem, we just
check for the existence of ${PF1}_${VFNUM} and ${PF2}_${VFNUM}, and
use whichever one we find (since we know that only one can be there)
2017-08-08 00:25:57 +00:00
|
|
|
if (virHostdevNetDevice(hostdev, -1, &linkdev, &vf) < 0)
|
2018-07-24 15:52:33 +00:00
|
|
|
return -1;
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2018-07-26 16:24:30 +00:00
|
|
|
vlan = virDomainNetGetActualVlan(hostdev->parentnet);
|
|
|
|
virtPort = virDomainNetGetActualVirtPortProfile(hostdev->parentnet);
|
2014-03-05 12:14:38 +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));
|
2018-07-24 15:52:33 +00:00
|
|
|
return -1;
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
2017-02-26 18:27:48 +00:00
|
|
|
if (virHostdevNetConfigVirtPortProfile(linkdev, vf, virtPort,
|
2018-07-26 16:24:30 +00:00
|
|
|
&hostdev->parentnet->mac,
|
2018-07-24 15:52:33 +00:00
|
|
|
uuid, port_profile_associate) < 0)
|
|
|
|
return -1;
|
2014-03-05 12:14:38 +00:00
|
|
|
} else {
|
2018-07-26 16:24:30 +00:00
|
|
|
if (virNetDevSetNetConfig(linkdev, vf, &hostdev->parentnet->mac,
|
2018-07-24 15:52:33 +00:00
|
|
|
vlan, NULL, true) < 0)
|
|
|
|
return -1;
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
2017-02-26 18:27:48 +00:00
|
|
|
|
2018-07-24 15:52:33 +00:00
|
|
|
return 0;
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
|
2017-02-28 18:47:12 +00:00
|
|
|
|
2014-03-05 12:14:38 +00:00
|
|
|
/* @oldStateDir:
|
|
|
|
* For upgrade purpose:
|
|
|
|
* To an existing VM on QEMU, the hostdev netconfig file is originally stored
|
|
|
|
* in cfg->stateDir (/var/run/libvirt/qemu). Switch to new version, it uses new
|
2016-03-01 18:53:37 +00:00
|
|
|
* location (mgr->stateDir) but certainly will not find it. In this
|
2014-03-05 12:14:38 +00:00
|
|
|
* case, try to find in the old state dir.
|
|
|
|
*/
|
|
|
|
static int
|
2017-03-13 23:07:02 +00:00
|
|
|
virHostdevRestoreNetConfig(virDomainHostdevDefPtr hostdev,
|
2014-03-12 16:01:39 +00:00
|
|
|
const char *stateDir,
|
|
|
|
const char *oldStateDir)
|
2014-03-05 12:14:38 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *linkdev = NULL;
|
|
|
|
g_autofree virMacAddrPtr MAC = NULL;
|
|
|
|
g_autofree virMacAddrPtr adminMAC = NULL;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virNetDevVlan) vlan = NULL;
|
2019-10-01 17:56:35 +00:00
|
|
|
const virNetDevVPortProfile *virtPort;
|
2014-03-05 12:14:38 +00:00
|
|
|
int vf = -1;
|
|
|
|
bool port_profile_associate = false;
|
util: restructure virNetDevReadNetConfig() to eliminate false error logs
virHostdevRestoreNetConfig() calls virNetDevReadNetConfig() to try and
read the "original config" of a netdev, and if that fails, it tries
again with a different directory/netdev name. This achieves the
desired effect (we end up finding the config wherever it may be), but
for each failure, virNetDevReadNetConfig() places a nice error message
in the system logs. Experience has shown that false-positive error
logs like this lead to erroneous bug reports, and can often mislead
those searching for *real* bugs.
This patch changes virNetDevReadNetConfig() to explicitly check if the
file exists before calling virFileReadAll(); if it doesn't exist,
virNetDevReadNetConfig() returns a success, but leaves all the
variables holding the results as NULL. (This makes sense if you define
the purpose of the function as "read a netdev's config from its config
file *if that file exists*).
To take advantage of that change, the caller,
virHostdevRestoreNetConfig() is modified to fail immediately if
virNetDevReadNetConfig() returns an error, and otherwise to try the
different directory/netdev name if adminMAC & vlan & MAC are all NULL
after the preceding attempt.
2017-08-10 00:49:26 +00:00
|
|
|
|
2014-03-05 12:14:38 +00:00
|
|
|
|
|
|
|
/* This is only needed for PCI devices that have been defined
|
|
|
|
* using <interface type='hostdev'>. For all others, it is a NOP.
|
|
|
|
*/
|
2015-04-22 11:33:09 +00:00
|
|
|
if (!virHostdevIsPCINetDevice(hostdev))
|
2014-03-05 12:14:38 +00:00
|
|
|
return 0;
|
|
|
|
|
2016-02-24 17:06:52 +00:00
|
|
|
if (virHostdevIsVirtualFunction(hostdev) != 1) {
|
2014-03-05 12:14:38 +00:00
|
|
|
virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
|
|
|
|
_("Interface type hostdev is currently supported on"
|
|
|
|
" SR-IOV Virtual Functions only"));
|
2018-07-24 15:52:34 +00:00
|
|
|
return -1;
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
|
util: save the correct VF's info when using a dual port SRIOV NIC in single port mode
Mellanox ConnectX-3 dual port SRIOV NICs present a bit of a challenge
when assigning one of their VFs to a guest using VFIO device
assignment.
These NICs have only a single PCI PF device, and that single PF has
two netdevs sharing the single PCI address - one for port 1 and one
for port 2. When a VF is created it can also have 2 netdevs, or it can
be setup in "single port" mode, where the VF has only a single netdev,
and that netdev is connected either to port 1 or to port 2.
When the VF is created in dual port mode, you get/set the MAC
address/vlan tag for the port 1 VF by sending a netlink message to the
PF's port1 netdev, and you get/set the MAC address/vlan tag for the
port 2 VF by sending a netlink message to the PF's port 2 netdev. (Of
course libvirt doesn't have any way to describe MAC/vlan info for 2
ports in a single hostdev interface, so that's a bit of a moot point)
When the VF is created in single port mode, you can *set* the MAC/vlan
info by sending a netlink message to *either* PF netdev - the driver
is smart enough to understand that there's only a single netdev, and
set the MAC/vlan for that netdev. When you want to *get* it, however,
the driver is more accurate - it will return 00:00:00:00:00:00 for the
MAC if you request it from the port 1 PF netdev when the VF was
configured to be single port on port 2, or if you request if from the
port 2 PF netdev when the VF was configured to be single port on port
1.
Based on this information, when *getting* the MAC/vlan info (to save
the original setting prior to assignment), we determine the correct PF
netdev by matching phys_port_id between VF and PF.
(IMPORTANT NOTE: this implies that to do PCI device assignment of the
VFs on dual port Mellanox cards using <interface type='hostdev'>
(i.e. if you want the MAC address/vlan tag to be set), not only must
the VFs be configured in single port mode, but also the VFs *must* be
bound to the host VF net driver, and libvirt must use managed='yes')
By the time libvirt is ready to set the new MAC/vlan tag, the VF has
already been unbound from the host net driver and bound to
vfio-pci. This isn't problematic though because, as stated earlier,
when a VF is created in single port mode, commands to configure it can
be sent to either the port 1 PF netdev or the port 2 PF netdev.
When it is time to restore the original MAC/vlan tag, again the VF
will *not* be bound to a host net driver, so it won't be possible to
learn from sysfs whether to use the port 1 or port 2 PF netdev for the
netlink commands. And again, it doesn't matter which netdev you
use. However, we must keep in mind that we saved the original settings
to a file called "${PF}_${VFNUM}". To solve this problem, we just
check for the existence of ${PF1}_${VFNUM} and ${PF2}_${VFNUM}, and
use whichever one we find (since we know that only one can be there)
2017-08-08 00:25:57 +00:00
|
|
|
if (virHostdevNetDevice(hostdev, 0, &linkdev, &vf) < 0)
|
2018-07-24 15:52:34 +00:00
|
|
|
return -1;
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2018-07-26 16:24:30 +00:00
|
|
|
virtPort = virDomainNetGetActualVirtPortProfile(hostdev->parentnet);
|
2014-03-05 12:14:38 +00:00
|
|
|
if (virtPort) {
|
2018-07-24 15:52:34 +00:00
|
|
|
return virHostdevNetConfigVirtPortProfile(linkdev, vf, virtPort,
|
2018-07-26 16:24:30 +00:00
|
|
|
&hostdev->parentnet->mac,
|
2018-09-19 08:38:14 +00:00
|
|
|
NULL,
|
|
|
|
port_profile_associate);
|
2014-03-05 12:14:38 +00:00
|
|
|
} else {
|
util: restructure virNetDevReadNetConfig() to eliminate false error logs
virHostdevRestoreNetConfig() calls virNetDevReadNetConfig() to try and
read the "original config" of a netdev, and if that fails, it tries
again with a different directory/netdev name. This achieves the
desired effect (we end up finding the config wherever it may be), but
for each failure, virNetDevReadNetConfig() places a nice error message
in the system logs. Experience has shown that false-positive error
logs like this lead to erroneous bug reports, and can often mislead
those searching for *real* bugs.
This patch changes virNetDevReadNetConfig() to explicitly check if the
file exists before calling virFileReadAll(); if it doesn't exist,
virNetDevReadNetConfig() returns a success, but leaves all the
variables holding the results as NULL. (This makes sense if you define
the purpose of the function as "read a netdev's config from its config
file *if that file exists*).
To take advantage of that change, the caller,
virHostdevRestoreNetConfig() is modified to fail immediately if
virNetDevReadNetConfig() returns an error, and otherwise to try the
different directory/netdev name if adminMAC & vlan & MAC are all NULL
after the preceding attempt.
2017-08-10 00:49:26 +00:00
|
|
|
/* we need to try 3 different places for the config file:
|
|
|
|
* 1) ${stateDir}/${PF}_vf${vf}
|
|
|
|
* This is almost always where the saved config is
|
|
|
|
*
|
|
|
|
* 2) ${oldStateDir/${PF}_vf${vf}
|
|
|
|
* saved config is only here if this machine was running a
|
|
|
|
* (by now *very*) old version of libvirt that saved the
|
|
|
|
* file in a different directory
|
|
|
|
*
|
|
|
|
* 3) ${stateDir}${PF[1]}_vf${VF}
|
|
|
|
* PF[1] means "the netdev for port 2 of the PF device", and
|
|
|
|
* is only valid when the PF is a Mellanox dual port NIC with
|
|
|
|
* a VF that was created in "single port" mode.
|
|
|
|
*
|
|
|
|
* NB: if virNetDevReadNetConfig() returns < 0, then it found
|
|
|
|
* the file, but there was a problem, so we should
|
|
|
|
* immediately return an error to our caller. If it returns
|
|
|
|
* 0, but all of the interesting stuff is NULL, that means
|
|
|
|
* the file wasn't found, so we can/should check other
|
|
|
|
* locations for it.
|
|
|
|
*/
|
util: save the correct VF's info when using a dual port SRIOV NIC in single port mode
Mellanox ConnectX-3 dual port SRIOV NICs present a bit of a challenge
when assigning one of their VFs to a guest using VFIO device
assignment.
These NICs have only a single PCI PF device, and that single PF has
two netdevs sharing the single PCI address - one for port 1 and one
for port 2. When a VF is created it can also have 2 netdevs, or it can
be setup in "single port" mode, where the VF has only a single netdev,
and that netdev is connected either to port 1 or to port 2.
When the VF is created in dual port mode, you get/set the MAC
address/vlan tag for the port 1 VF by sending a netlink message to the
PF's port1 netdev, and you get/set the MAC address/vlan tag for the
port 2 VF by sending a netlink message to the PF's port 2 netdev. (Of
course libvirt doesn't have any way to describe MAC/vlan info for 2
ports in a single hostdev interface, so that's a bit of a moot point)
When the VF is created in single port mode, you can *set* the MAC/vlan
info by sending a netlink message to *either* PF netdev - the driver
is smart enough to understand that there's only a single netdev, and
set the MAC/vlan for that netdev. When you want to *get* it, however,
the driver is more accurate - it will return 00:00:00:00:00:00 for the
MAC if you request it from the port 1 PF netdev when the VF was
configured to be single port on port 2, or if you request if from the
port 2 PF netdev when the VF was configured to be single port on port
1.
Based on this information, when *getting* the MAC/vlan info (to save
the original setting prior to assignment), we determine the correct PF
netdev by matching phys_port_id between VF and PF.
(IMPORTANT NOTE: this implies that to do PCI device assignment of the
VFs on dual port Mellanox cards using <interface type='hostdev'>
(i.e. if you want the MAC address/vlan tag to be set), not only must
the VFs be configured in single port mode, but also the VFs *must* be
bound to the host VF net driver, and libvirt must use managed='yes')
By the time libvirt is ready to set the new MAC/vlan tag, the VF has
already been unbound from the host net driver and bound to
vfio-pci. This isn't problematic though because, as stated earlier,
when a VF is created in single port mode, commands to configure it can
be sent to either the port 1 PF netdev or the port 2 PF netdev.
When it is time to restore the original MAC/vlan tag, again the VF
will *not* be bound to a host net driver, so it won't be possible to
learn from sysfs whether to use the port 1 or port 2 PF netdev for the
netlink commands. And again, it doesn't matter which netdev you
use. However, we must keep in mind that we saved the original settings
to a file called "${PF}_${VFNUM}". To solve this problem, we just
check for the existence of ${PF1}_${VFNUM} and ${PF2}_${VFNUM}, and
use whichever one we find (since we know that only one can be there)
2017-08-08 00:25:57 +00:00
|
|
|
|
util: restructure virNetDevReadNetConfig() to eliminate false error logs
virHostdevRestoreNetConfig() calls virNetDevReadNetConfig() to try and
read the "original config" of a netdev, and if that fails, it tries
again with a different directory/netdev name. This achieves the
desired effect (we end up finding the config wherever it may be), but
for each failure, virNetDevReadNetConfig() places a nice error message
in the system logs. Experience has shown that false-positive error
logs like this lead to erroneous bug reports, and can often mislead
those searching for *real* bugs.
This patch changes virNetDevReadNetConfig() to explicitly check if the
file exists before calling virFileReadAll(); if it doesn't exist,
virNetDevReadNetConfig() returns a success, but leaves all the
variables holding the results as NULL. (This makes sense if you define
the purpose of the function as "read a netdev's config from its config
file *if that file exists*).
To take advantage of that change, the caller,
virHostdevRestoreNetConfig() is modified to fail immediately if
virNetDevReadNetConfig() returns an error, and otherwise to try the
different directory/netdev name if adminMAC & vlan & MAC are all NULL
after the preceding attempt.
2017-08-10 00:49:26 +00:00
|
|
|
/* 1) standard location */
|
|
|
|
if (virNetDevReadNetConfig(linkdev, vf, stateDir,
|
|
|
|
&adminMAC, &vlan, &MAC) < 0) {
|
2018-07-24 15:52:34 +00:00
|
|
|
return -1;
|
util: restructure virNetDevReadNetConfig() to eliminate false error logs
virHostdevRestoreNetConfig() calls virNetDevReadNetConfig() to try and
read the "original config" of a netdev, and if that fails, it tries
again with a different directory/netdev name. This achieves the
desired effect (we end up finding the config wherever it may be), but
for each failure, virNetDevReadNetConfig() places a nice error message
in the system logs. Experience has shown that false-positive error
logs like this lead to erroneous bug reports, and can often mislead
those searching for *real* bugs.
This patch changes virNetDevReadNetConfig() to explicitly check if the
file exists before calling virFileReadAll(); if it doesn't exist,
virNetDevReadNetConfig() returns a success, but leaves all the
variables holding the results as NULL. (This makes sense if you define
the purpose of the function as "read a netdev's config from its config
file *if that file exists*).
To take advantage of that change, the caller,
virHostdevRestoreNetConfig() is modified to fail immediately if
virNetDevReadNetConfig() returns an error, and otherwise to try the
different directory/netdev name if adminMAC & vlan & MAC are all NULL
after the preceding attempt.
2017-08-10 00:49:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 2) "old" (pre-1.2.3 circa 2014) location - whenever we get
|
|
|
|
* to the point that nobody will ever upgrade directly from
|
|
|
|
* 1.2.3 (or older) directly to current libvirt, we can
|
|
|
|
* eliminate this clause
|
|
|
|
**/
|
|
|
|
if (!(adminMAC || vlan || MAC) && oldStateDir &&
|
|
|
|
virNetDevReadNetConfig(linkdev, vf, oldStateDir,
|
|
|
|
&adminMAC, &vlan, &MAC) < 0) {
|
2018-07-24 15:52:34 +00:00
|
|
|
return -1;
|
util: save the correct VF's info when using a dual port SRIOV NIC in single port mode
Mellanox ConnectX-3 dual port SRIOV NICs present a bit of a challenge
when assigning one of their VFs to a guest using VFIO device
assignment.
These NICs have only a single PCI PF device, and that single PF has
two netdevs sharing the single PCI address - one for port 1 and one
for port 2. When a VF is created it can also have 2 netdevs, or it can
be setup in "single port" mode, where the VF has only a single netdev,
and that netdev is connected either to port 1 or to port 2.
When the VF is created in dual port mode, you get/set the MAC
address/vlan tag for the port 1 VF by sending a netlink message to the
PF's port1 netdev, and you get/set the MAC address/vlan tag for the
port 2 VF by sending a netlink message to the PF's port 2 netdev. (Of
course libvirt doesn't have any way to describe MAC/vlan info for 2
ports in a single hostdev interface, so that's a bit of a moot point)
When the VF is created in single port mode, you can *set* the MAC/vlan
info by sending a netlink message to *either* PF netdev - the driver
is smart enough to understand that there's only a single netdev, and
set the MAC/vlan for that netdev. When you want to *get* it, however,
the driver is more accurate - it will return 00:00:00:00:00:00 for the
MAC if you request it from the port 1 PF netdev when the VF was
configured to be single port on port 2, or if you request if from the
port 2 PF netdev when the VF was configured to be single port on port
1.
Based on this information, when *getting* the MAC/vlan info (to save
the original setting prior to assignment), we determine the correct PF
netdev by matching phys_port_id between VF and PF.
(IMPORTANT NOTE: this implies that to do PCI device assignment of the
VFs on dual port Mellanox cards using <interface type='hostdev'>
(i.e. if you want the MAC address/vlan tag to be set), not only must
the VFs be configured in single port mode, but also the VFs *must* be
bound to the host VF net driver, and libvirt must use managed='yes')
By the time libvirt is ready to set the new MAC/vlan tag, the VF has
already been unbound from the host net driver and bound to
vfio-pci. This isn't problematic though because, as stated earlier,
when a VF is created in single port mode, commands to configure it can
be sent to either the port 1 PF netdev or the port 2 PF netdev.
When it is time to restore the original MAC/vlan tag, again the VF
will *not* be bound to a host net driver, so it won't be possible to
learn from sysfs whether to use the port 1 or port 2 PF netdev for the
netlink commands. And again, it doesn't matter which netdev you
use. However, we must keep in mind that we saved the original settings
to a file called "${PF}_${VFNUM}". To solve this problem, we just
check for the existence of ${PF1}_${VFNUM} and ${PF2}_${VFNUM}, and
use whichever one we find (since we know that only one can be there)
2017-08-08 00:25:57 +00:00
|
|
|
}
|
|
|
|
|
util: restructure virNetDevReadNetConfig() to eliminate false error logs
virHostdevRestoreNetConfig() calls virNetDevReadNetConfig() to try and
read the "original config" of a netdev, and if that fails, it tries
again with a different directory/netdev name. This achieves the
desired effect (we end up finding the config wherever it may be), but
for each failure, virNetDevReadNetConfig() places a nice error message
in the system logs. Experience has shown that false-positive error
logs like this lead to erroneous bug reports, and can often mislead
those searching for *real* bugs.
This patch changes virNetDevReadNetConfig() to explicitly check if the
file exists before calling virFileReadAll(); if it doesn't exist,
virNetDevReadNetConfig() returns a success, but leaves all the
variables holding the results as NULL. (This makes sense if you define
the purpose of the function as "read a netdev's config from its config
file *if that file exists*).
To take advantage of that change, the caller,
virHostdevRestoreNetConfig() is modified to fail immediately if
virNetDevReadNetConfig() returns an error, and otherwise to try the
different directory/netdev name if adminMAC & vlan & MAC are all NULL
after the preceding attempt.
2017-08-10 00:49:26 +00:00
|
|
|
/* 3) try using the PF's "port 2" netdev as the name of the
|
|
|
|
* config file
|
|
|
|
*/
|
|
|
|
if (!(adminMAC || vlan || MAC)) {
|
|
|
|
VIR_FREE(linkdev);
|
|
|
|
|
|
|
|
if (virHostdevNetDevice(hostdev, 1, &linkdev, &vf) < 0 ||
|
|
|
|
virNetDevReadNetConfig(linkdev, vf, stateDir,
|
|
|
|
&adminMAC, &vlan, &MAC) < 0) {
|
2018-07-24 15:52:34 +00:00
|
|
|
return -1;
|
util: after hostdev assignment, restore VF MAC address via setting admin MAC
It takes longer to explain this than to fix it...
In the past we weren't able to save the VF's own MAC address *at all*
when using it for hostdev assignment, because we had already unbound
the VF from the host net driver prior to saving its config. With the
previous patch, that problem has been solved, so we now have the VF's
MAC address saved and can move on to the *next* problem, which is twofold:
1) during teardown we restore the config before we've re-bound, so the
VF doesn't have a net driver, and thus we can't set its MAC address
directly.
2) even if we delay restoring the config until the VF is bound to a
net driver, the request to set its MAC address would fail, since
(during device setup) we had set the "admin MAC" for the VF via an
RTM_SETLINK to the PF - once you've set the admin MAC for a VF, the
VF driver (either on host or on guest) is not allowed to change the
VF's MAC address "forever" (well, until you reload the PF driver,
but that requires destroying and recreating every single VF, which
isn't something you can require).
The solution is to keep the restoration of config at the same place,
but to set the *admin MAC* to the address you want the VF to have -
when the VF net driver is later initialized (as a part of re-binding
to the VF net driver) its MAC will be initialized to the current value
of the admin MAC.
2017-03-06 14:36:40 +00:00
|
|
|
}
|
util: restructure virNetDevReadNetConfig() to eliminate false error logs
virHostdevRestoreNetConfig() calls virNetDevReadNetConfig() to try and
read the "original config" of a netdev, and if that fails, it tries
again with a different directory/netdev name. This achieves the
desired effect (we end up finding the config wherever it may be), but
for each failure, virNetDevReadNetConfig() places a nice error message
in the system logs. Experience has shown that false-positive error
logs like this lead to erroneous bug reports, and can often mislead
those searching for *real* bugs.
This patch changes virNetDevReadNetConfig() to explicitly check if the
file exists before calling virFileReadAll(); if it doesn't exist,
virNetDevReadNetConfig() returns a success, but leaves all the
variables holding the results as NULL. (This makes sense if you define
the purpose of the function as "read a netdev's config from its config
file *if that file exists*).
To take advantage of that change, the caller,
virHostdevRestoreNetConfig() is modified to fail immediately if
virNetDevReadNetConfig() returns an error, and otherwise to try the
different directory/netdev name if adminMAC & vlan & MAC are all NULL
after the preceding attempt.
2017-08-10 00:49:26 +00:00
|
|
|
}
|
util: after hostdev assignment, restore VF MAC address via setting admin MAC
It takes longer to explain this than to fix it...
In the past we weren't able to save the VF's own MAC address *at all*
when using it for hostdev assignment, because we had already unbound
the VF from the host net driver prior to saving its config. With the
previous patch, that problem has been solved, so we now have the VF's
MAC address saved and can move on to the *next* problem, which is twofold:
1) during teardown we restore the config before we've re-bound, so the
VF doesn't have a net driver, and thus we can't set its MAC address
directly.
2) even if we delay restoring the config until the VF is bound to a
net driver, the request to set its MAC address would fail, since
(during device setup) we had set the "admin MAC" for the VF via an
RTM_SETLINK to the PF - once you've set the admin MAC for a VF, the
VF driver (either on host or on guest) is not allowed to change the
VF's MAC address "forever" (well, until you reload the PF driver,
but that requires destroying and recreating every single VF, which
isn't something you can require).
The solution is to keep the restoration of config at the same place,
but to set the *admin MAC* to the address you want the VF to have -
when the VF net driver is later initialized (as a part of re-binding
to the VF net driver) its MAC will be initialized to the current value
of the admin MAC.
2017-03-06 14:36:40 +00:00
|
|
|
|
util: restructure virNetDevReadNetConfig() to eliminate false error logs
virHostdevRestoreNetConfig() calls virNetDevReadNetConfig() to try and
read the "original config" of a netdev, and if that fails, it tries
again with a different directory/netdev name. This achieves the
desired effect (we end up finding the config wherever it may be), but
for each failure, virNetDevReadNetConfig() places a nice error message
in the system logs. Experience has shown that false-positive error
logs like this lead to erroneous bug reports, and can often mislead
those searching for *real* bugs.
This patch changes virNetDevReadNetConfig() to explicitly check if the
file exists before calling virFileReadAll(); if it doesn't exist,
virNetDevReadNetConfig() returns a success, but leaves all the
variables holding the results as NULL. (This makes sense if you define
the purpose of the function as "read a netdev's config from its config
file *if that file exists*).
To take advantage of that change, the caller,
virHostdevRestoreNetConfig() is modified to fail immediately if
virNetDevReadNetConfig() returns an error, and otherwise to try the
different directory/netdev name if adminMAC & vlan & MAC are all NULL
after the preceding attempt.
2017-08-10 00:49:26 +00:00
|
|
|
/* if a MAC was stored for the VF, we should now restore
|
|
|
|
* that as the adminMAC. We have to do it this way because
|
|
|
|
* the VF is still not bound to the host's net driver, so
|
|
|
|
* we can't directly set its MAC (and even after it is
|
|
|
|
* re-bound to the host net driver, it will still have its
|
|
|
|
* "administratively set" flag on, and that prohibits the
|
|
|
|
* VF's net driver from directly setting the MAC
|
|
|
|
* anyway). But it we set the desired VF MAC as the "admin
|
|
|
|
* MAC" *now*, then when the VF is re-bound to the host
|
|
|
|
* net driver (which will happen soon after returning from
|
|
|
|
* this function), that adminMAC will be set (by the PF)
|
|
|
|
* as the VF's new initial MAC.
|
|
|
|
*
|
|
|
|
* If no MAC was stored for the VF, that means it wasn't
|
|
|
|
* bound to a net driver before we used it anyway, so the
|
|
|
|
* adminMAC is all we have, and we can just restore it
|
|
|
|
* directly.
|
|
|
|
*/
|
|
|
|
if (MAC) {
|
|
|
|
VIR_FREE(adminMAC);
|
|
|
|
adminMAC = MAC;
|
|
|
|
MAC = NULL;
|
2017-03-06 01:28:08 +00:00
|
|
|
}
|
|
|
|
|
util: restructure virNetDevReadNetConfig() to eliminate false error logs
virHostdevRestoreNetConfig() calls virNetDevReadNetConfig() to try and
read the "original config" of a netdev, and if that fails, it tries
again with a different directory/netdev name. This achieves the
desired effect (we end up finding the config wherever it may be), but
for each failure, virNetDevReadNetConfig() places a nice error message
in the system logs. Experience has shown that false-positive error
logs like this lead to erroneous bug reports, and can often mislead
those searching for *real* bugs.
This patch changes virNetDevReadNetConfig() to explicitly check if the
file exists before calling virFileReadAll(); if it doesn't exist,
virNetDevReadNetConfig() returns a success, but leaves all the
variables holding the results as NULL. (This makes sense if you define
the purpose of the function as "read a netdev's config from its config
file *if that file exists*).
To take advantage of that change, the caller,
virHostdevRestoreNetConfig() is modified to fail immediately if
virNetDevReadNetConfig() returns an error, and otherwise to try the
different directory/netdev name if adminMAC & vlan & MAC are all NULL
after the preceding attempt.
2017-08-10 00:49:26 +00:00
|
|
|
ignore_value(virNetDevSetNetConfig(linkdev, vf,
|
|
|
|
adminMAC, vlan, MAC, true));
|
2018-07-24 15:52:34 +00:00
|
|
|
return 0;
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-23 17:35:39 +00:00
|
|
|
static int
|
|
|
|
virHostdevResetAllPCIDevices(virHostdevManagerPtr mgr,
|
|
|
|
virPCIDeviceListPtr pcidevs)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
|
|
|
virPCIDevicePtr pci = virPCIDeviceListGet(pcidevs, i);
|
|
|
|
|
|
|
|
/* We can avoid looking up the actual device here, because performing
|
|
|
|
* a PCI reset on a device doesn't require any information other than
|
|
|
|
* the address, which 'pci' already contains */
|
|
|
|
VIR_DEBUG("Resetting PCI device %s", virPCIDeviceGetName(pci));
|
|
|
|
if (virPCIDeviceReset(pci, mgr->activePCIHostdevs,
|
|
|
|
mgr->inactivePCIHostdevs) < 0) {
|
|
|
|
VIR_ERROR(_("Failed to reset PCI device: %s"),
|
|
|
|
virGetLastErrorMessage());
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-23 17:35:41 +00:00
|
|
|
static void
|
|
|
|
virHostdevReattachAllPCIDevices(virHostdevManagerPtr mgr,
|
|
|
|
virPCIDeviceListPtr pcidevs)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
|
|
|
virPCIDevicePtr pci = virPCIDeviceListGet(pcidevs, i);
|
|
|
|
virPCIDevicePtr actual;
|
|
|
|
|
|
|
|
/* We need to look up the actual device because that's what
|
|
|
|
* virPCIDeviceReattach() expects as its argument */
|
|
|
|
if (!(actual = virPCIDeviceListFind(mgr->inactivePCIHostdevs, pci)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (virPCIDeviceGetManaged(actual)) {
|
|
|
|
VIR_DEBUG("Reattaching managed PCI device %s",
|
|
|
|
virPCIDeviceGetName(pci));
|
|
|
|
if (virPCIDeviceReattach(actual,
|
|
|
|
mgr->activePCIHostdevs,
|
|
|
|
mgr->inactivePCIHostdevs) < 0) {
|
|
|
|
VIR_ERROR(_("Failed to re-attach PCI device: %s"),
|
|
|
|
virGetLastErrorMessage());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
VIR_DEBUG("Not reattaching unmanaged PCI device %s",
|
|
|
|
virPCIDeviceGetName(actual));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 12:13:24 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
virHostdevPreparePCIDevicesImpl(virHostdevManagerPtr mgr,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
const unsigned char *uuid,
|
|
|
|
virPCIDeviceListPtr pcidevs,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs,
|
|
|
|
unsigned int flags)
|
2014-03-05 12:14:38 +00:00
|
|
|
{
|
|
|
|
int last_processed_hostdev_vf = -1;
|
|
|
|
size_t i;
|
|
|
|
int ret = -1;
|
2015-01-14 11:03:28 +00:00
|
|
|
virPCIDeviceAddressPtr devAddr = NULL;
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectLock(mgr->activePCIHostdevs);
|
|
|
|
virObjectLock(mgr->inactivePCIHostdevs);
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2016-02-22 14:02:23 +00:00
|
|
|
/* Detaching devices from the host involves several steps; each
|
|
|
|
* of them is described at length below.
|
|
|
|
*
|
|
|
|
* All devices must 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 */
|
|
|
|
|
2016-03-18 17:03:50 +00:00
|
|
|
/* Step 1: Perform some initial checks on the devices */
|
2014-03-05 12:14:38 +00:00
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDevicePtr pci = virPCIDeviceListGet(pcidevs, i);
|
2014-03-05 12:14:38 +00:00
|
|
|
bool strict_acs_check = !!(flags & VIR_HOSTDEV_STRICT_ACS_CHECK);
|
2016-02-24 13:59:25 +00:00
|
|
|
bool usesVFIO = (virPCIDeviceGetStubDriver(pci) == VIR_PCI_STUB_DRIVER_VFIO);
|
virhostdev: Unify virHostdevPreparePCIDevices behaviour for KVM and VFIO cases
The virHostdevPreparePCIDevices() function works in several
steps. In the very first one, it checks if devices we want to
detach from the host are not taken already by some other domain.
However, this piece of code returns different results depending
on the stub driver used (which is not wrong per se, but keep on
reading). If the stub driver is KVM then
virHostdevIsPCINodeDeviceUsed() is called which basically checks
if a PCI device from the detach list is not used by any domain
(including the one we are preparing the device for). If that is
the case, an error is reported ("device in use") and -1 is
returned.
However, that is not what happens if the stub driver is VFIO. If
the stub driver is VFIO, then we iterate over all PCI devices
from the same IOMMU group and check if they are taken by some
other domain (because a PCI device, well IOMMU group, can't be
shared between two or more qemu processes). But we fail to check,
if the device we are trying to detach from the host is not
already taken by a domain. That is, calling
virHostdevPreparePCIDevices() over a hostdev device twice
succeeds the first time and fails too late in the second run
(fortunately, virHostdevResetAllPCIDevices() will throw an error,
but this is already too late because the PCI device in question
was moved to the list of inactive PCI devices and now it appears
in both lists).
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2019-08-14 09:28:30 +00:00
|
|
|
struct virHostdevIsPCINodeDeviceUsedData data = {mgr, drv_name, dom_name, false};
|
2017-01-23 13:36:29 +00:00
|
|
|
int hdrType = -1;
|
|
|
|
|
|
|
|
if (virPCIGetHeaderType(pci, &hdrType) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (hdrType != VIR_PCI_HEADER_ENDPOINT) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("Non-endpoint PCI devices cannot be assigned "
|
|
|
|
"to guests"));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2016-02-24 13:59:25 +00:00
|
|
|
if (!usesVFIO && !virPCIDeviceIsAssignable(pci, strict_acs_check)) {
|
2014-03-05 12:14:38 +00:00
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("PCI device %s is not assignable"),
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDeviceGetName(pci));
|
2014-03-05 12:14:38 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
2015-01-14 11:03:28 +00:00
|
|
|
|
2014-03-05 12:14:38 +00:00
|
|
|
/* The device is in use by other active domain if
|
virhostdev: Unify virHostdevPreparePCIDevices behaviour for KVM and VFIO cases
The virHostdevPreparePCIDevices() function works in several
steps. In the very first one, it checks if devices we want to
detach from the host are not taken already by some other domain.
However, this piece of code returns different results depending
on the stub driver used (which is not wrong per se, but keep on
reading). If the stub driver is KVM then
virHostdevIsPCINodeDeviceUsed() is called which basically checks
if a PCI device from the detach list is not used by any domain
(including the one we are preparing the device for). If that is
the case, an error is reported ("device in use") and -1 is
returned.
However, that is not what happens if the stub driver is VFIO. If
the stub driver is VFIO, then we iterate over all PCI devices
from the same IOMMU group and check if they are taken by some
other domain (because a PCI device, well IOMMU group, can't be
shared between two or more qemu processes). But we fail to check,
if the device we are trying to detach from the host is not
already taken by a domain. That is, calling
virHostdevPreparePCIDevices() over a hostdev device twice
succeeds the first time and fails too late in the second run
(fortunately, virHostdevResetAllPCIDevices() will throw an error,
but this is already too late because the PCI device in question
was moved to the list of inactive PCI devices and now it appears
in both lists).
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2019-08-14 09:28:30 +00:00
|
|
|
* the dev is in list activePCIHostdevs. */
|
2016-02-24 13:59:25 +00:00
|
|
|
devAddr = virPCIDeviceGetAddress(pci);
|
virhostdev: Unify virHostdevPreparePCIDevices behaviour for KVM and VFIO cases
The virHostdevPreparePCIDevices() function works in several
steps. In the very first one, it checks if devices we want to
detach from the host are not taken already by some other domain.
However, this piece of code returns different results depending
on the stub driver used (which is not wrong per se, but keep on
reading). If the stub driver is KVM then
virHostdevIsPCINodeDeviceUsed() is called which basically checks
if a PCI device from the detach list is not used by any domain
(including the one we are preparing the device for). If that is
the case, an error is reported ("device in use") and -1 is
returned.
However, that is not what happens if the stub driver is VFIO. If
the stub driver is VFIO, then we iterate over all PCI devices
from the same IOMMU group and check if they are taken by some
other domain (because a PCI device, well IOMMU group, can't be
shared between two or more qemu processes). But we fail to check,
if the device we are trying to detach from the host is not
already taken by a domain. That is, calling
virHostdevPreparePCIDevices() over a hostdev device twice
succeeds the first time and fails too late in the second run
(fortunately, virHostdevResetAllPCIDevices() will throw an error,
but this is already too late because the PCI device in question
was moved to the list of inactive PCI devices and now it appears
in both lists).
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2019-08-14 09:28:30 +00:00
|
|
|
if (virHostdevIsPCINodeDeviceUsed(devAddr, &data))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* VFIO devices belonging to same IOMMU group can't be
|
|
|
|
* shared across guests. Check if that's the case. */
|
2016-03-01 19:13:20 +00:00
|
|
|
if (usesVFIO) {
|
virhostdev: Unify virHostdevPreparePCIDevices behaviour for KVM and VFIO cases
The virHostdevPreparePCIDevices() function works in several
steps. In the very first one, it checks if devices we want to
detach from the host are not taken already by some other domain.
However, this piece of code returns different results depending
on the stub driver used (which is not wrong per se, but keep on
reading). If the stub driver is KVM then
virHostdevIsPCINodeDeviceUsed() is called which basically checks
if a PCI device from the detach list is not used by any domain
(including the one we are preparing the device for). If that is
the case, an error is reported ("device in use") and -1 is
returned.
However, that is not what happens if the stub driver is VFIO. If
the stub driver is VFIO, then we iterate over all PCI devices
from the same IOMMU group and check if they are taken by some
other domain (because a PCI device, well IOMMU group, can't be
shared between two or more qemu processes). But we fail to check,
if the device we are trying to detach from the host is not
already taken by a domain. That is, calling
virHostdevPreparePCIDevices() over a hostdev device twice
succeeds the first time and fails too late in the second run
(fortunately, virHostdevResetAllPCIDevices() will throw an error,
but this is already too late because the PCI device in question
was moved to the list of inactive PCI devices and now it appears
in both lists).
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Tested-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2019-08-14 09:28:30 +00:00
|
|
|
data.usesVFIO = true;
|
2015-01-14 11:03:28 +00:00
|
|
|
if (virPCIDeviceAddressIOMMUGroupIterate(devAddr,
|
|
|
|
virHostdevIsPCINodeDeviceUsed,
|
2015-07-14 11:56:33 +00:00
|
|
|
&data) < 0)
|
2015-01-14 11:03:28 +00:00
|
|
|
goto cleanup;
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-28 21:44:26 +00:00
|
|
|
/* Step 1.5: For non-802.11Qbh SRIOV network devices, save the
|
|
|
|
* current device config
|
|
|
|
*/
|
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
if (virHostdevSaveNetConfig(hostdevs[i], mgr->stateDir) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2016-03-17 16:41:31 +00:00
|
|
|
/* Step 2: detach managed devices and make sure unmanaged devices
|
|
|
|
* have already been taken care of */
|
2014-03-05 12:14:38 +00:00
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDevicePtr pci = virPCIDeviceListGet(pcidevs, i);
|
2015-12-17 10:54:14 +00:00
|
|
|
|
2016-02-24 13:59:25 +00:00
|
|
|
if (virPCIDeviceGetManaged(pci)) {
|
2016-03-07 12:41:19 +00:00
|
|
|
|
|
|
|
/* We can't look up the actual device because it has not been
|
|
|
|
* created yet: virPCIDeviceDetach() will insert a copy of 'pci'
|
|
|
|
* into the list of inactive devices, and that copy will be the
|
|
|
|
* actual device going forward */
|
2015-12-16 15:32:37 +00:00
|
|
|
VIR_DEBUG("Detaching managed PCI device %s",
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDeviceGetName(pci));
|
|
|
|
if (virPCIDeviceDetach(pci,
|
2016-03-01 18:53:37 +00:00
|
|
|
mgr->activePCIHostdevs,
|
|
|
|
mgr->inactivePCIHostdevs) < 0)
|
2015-12-17 10:54:14 +00:00
|
|
|
goto reattachdevs;
|
2015-12-16 15:32:37 +00:00
|
|
|
} else {
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *driverPath = NULL;
|
|
|
|
g_autofree char *driverName = NULL;
|
2016-03-17 16:41:31 +00:00
|
|
|
int stub;
|
|
|
|
|
|
|
|
/* Unmanaged devices should already have been marked as
|
|
|
|
* inactive: if that's the case, we can simply move on */
|
|
|
|
if (virPCIDeviceListFind(mgr->inactivePCIHostdevs, pci)) {
|
|
|
|
VIR_DEBUG("Not detaching unmanaged PCI device %s",
|
|
|
|
virPCIDeviceGetName(pci));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If that's not the case, though, it might be because the
|
|
|
|
* daemon has been restarted, causing us to lose track of the
|
|
|
|
* device. Try and recover by marking the device as inactive
|
|
|
|
* if it happens to be bound to a known stub driver.
|
|
|
|
*
|
|
|
|
* FIXME Get rid of this once a proper way to keep track of
|
|
|
|
* information about active / inactive device across
|
|
|
|
* daemon restarts has been implemented */
|
|
|
|
|
|
|
|
if (virPCIDeviceGetDriverPathAndName(pci,
|
|
|
|
&driverPath, &driverName) < 0)
|
|
|
|
goto reattachdevs;
|
|
|
|
|
|
|
|
stub = virPCIStubDriverTypeFromString(driverName);
|
|
|
|
|
|
|
|
if (stub > VIR_PCI_STUB_DRIVER_NONE &&
|
|
|
|
stub < VIR_PCI_STUB_DRIVER_LAST) {
|
|
|
|
|
|
|
|
/* The device is bound to a known stub driver: store this
|
|
|
|
* information and add a copy to the inactive list */
|
|
|
|
virPCIDeviceSetStubDriver(pci, stub);
|
|
|
|
|
|
|
|
VIR_DEBUG("Adding PCI device %s to inactive list",
|
|
|
|
virPCIDeviceGetName(pci));
|
|
|
|
if (virPCIDeviceListAddCopy(mgr->inactivePCIHostdevs, pci) < 0)
|
|
|
|
goto reattachdevs;
|
2016-03-04 15:16:16 +00:00
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("Unmanaged PCI device %s must be manually "
|
2018-09-19 08:38:14 +00:00
|
|
|
"detached from the host"),
|
2016-03-04 15:16:16 +00:00
|
|
|
virPCIDeviceGetName(pci));
|
|
|
|
goto reattachdevs;
|
2016-03-17 16:41:31 +00:00
|
|
|
}
|
2015-12-16 15:32:37 +00:00
|
|
|
}
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
|
2015-12-17 10:54:14 +00:00
|
|
|
/* At this point, all devices are attached to the stub driver and have
|
|
|
|
* been marked as inactive */
|
|
|
|
|
2016-02-22 14:02:23 +00:00
|
|
|
/* Step 3: Now that all the PCI hostdevs have been detached, we
|
2014-03-05 12:14:38 +00:00
|
|
|
* can safely reset them */
|
2019-07-23 17:35:39 +00:00
|
|
|
if (virHostdevResetAllPCIDevices(mgr, pcidevs) < 0)
|
|
|
|
goto reattachdevs;
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2016-02-22 14:02:23 +00:00
|
|
|
/* Step 4: For SRIOV network devices, Now that we have detached the
|
2017-02-28 18:47:12 +00:00
|
|
|
* the network device, set the new netdev config */
|
2014-03-05 12:14:38 +00:00
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
2017-02-28 18:47:12 +00:00
|
|
|
|
|
|
|
if (virHostdevSetNetConfig(hostdevs[i], uuid) < 0)
|
|
|
|
goto resetvfnetconfig;
|
|
|
|
|
|
|
|
last_processed_hostdev_vf = i;
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 17:03:50 +00:00
|
|
|
/* Step 5: Move devices from the inactive list to the active list */
|
2014-03-05 12:14:38 +00:00
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDevicePtr pci = virPCIDeviceListGet(pcidevs, i);
|
2016-03-18 17:03:50 +00:00
|
|
|
virPCIDevicePtr actual;
|
2015-12-16 15:32:37 +00:00
|
|
|
|
2016-03-18 17:03:50 +00:00
|
|
|
VIR_DEBUG("Removing PCI device %s from inactive list",
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDeviceGetName(pci));
|
2016-03-18 17:03:50 +00:00
|
|
|
actual = virPCIDeviceListSteal(mgr->inactivePCIHostdevs, pci);
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2016-03-18 17:03:50 +00:00
|
|
|
VIR_DEBUG("Adding PCI device %s to active list",
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDeviceGetName(pci));
|
2016-03-18 17:03:50 +00:00
|
|
|
if (!actual || virPCIDeviceListAdd(mgr->activePCIHostdevs, actual) < 0)
|
|
|
|
goto inactivedevs;
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 17:03:50 +00:00
|
|
|
/* Step 6: Set driver and domain information */
|
2014-03-05 12:14:38 +00:00
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDevicePtr pci, actual;
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2016-03-07 12:41:19 +00:00
|
|
|
/* We need to look up the actual device and set the information
|
|
|
|
* there because 'pci' only contain address information and will
|
|
|
|
* be released at the end of the function */
|
2016-02-24 13:59:25 +00:00
|
|
|
pci = virPCIDeviceListGet(pcidevs, i);
|
|
|
|
actual = virPCIDeviceListFind(mgr->activePCIHostdevs, pci);
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2015-12-16 15:32:37 +00:00
|
|
|
VIR_DEBUG("Setting driver and domain information for PCI device %s",
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDeviceGetName(pci));
|
|
|
|
if (actual)
|
|
|
|
virPCIDeviceSetUsedBy(actual, drv_name, dom_name);
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 17:03:50 +00:00
|
|
|
/* Step 7: Now set the original states for hostdev def */
|
2014-03-05 12:14:38 +00:00
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
2016-03-18 17:03:51 +00:00
|
|
|
virPCIDevicePtr actual;
|
2014-03-05 12:14:38 +00:00
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
2014-07-03 20:31:39 +00:00
|
|
|
virDomainHostdevSubsysPCIPtr pcisrc = &hostdev->source.subsys.u.pci;
|
2014-03-05 12:14:38 +00:00
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
|
|
|
continue;
|
|
|
|
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_PCI)
|
|
|
|
continue;
|
|
|
|
|
2016-03-18 17:03:51 +00:00
|
|
|
/* We need to look up the actual device because it's the one
|
|
|
|
* that contains the information we care about (unbind_from_stub,
|
|
|
|
* remove_slot, reprobe) */
|
|
|
|
actual = virPCIDeviceListFindByIDs(mgr->activePCIHostdevs,
|
|
|
|
pcisrc->addr.domain,
|
|
|
|
pcisrc->addr.bus,
|
|
|
|
pcisrc->addr.slot,
|
|
|
|
pcisrc->addr.function);
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2016-02-22 14:02:23 +00:00
|
|
|
/* Appropriate values for the unbind_from_stub, remove_slot
|
|
|
|
* and reprobe properties of the device were set earlier
|
|
|
|
* by virPCIDeviceDetach() */
|
2016-03-18 17:03:51 +00:00
|
|
|
if (actual) {
|
2016-02-23 16:46:48 +00:00
|
|
|
VIR_DEBUG("Saving network configuration of PCI device %s",
|
2016-03-18 17:03:51 +00:00
|
|
|
virPCIDeviceGetName(actual));
|
2014-03-05 12:14:38 +00:00
|
|
|
hostdev->origstates.states.pci.unbind_from_stub =
|
2016-03-18 17:03:51 +00:00
|
|
|
virPCIDeviceGetUnbindFromStub(actual);
|
2014-03-05 12:14:38 +00:00
|
|
|
hostdev->origstates.states.pci.remove_slot =
|
2016-03-18 17:03:51 +00:00
|
|
|
virPCIDeviceGetRemoveSlot(actual);
|
2014-03-05 12:14:38 +00:00
|
|
|
hostdev->origstates.states.pci.reprobe =
|
2016-03-18 17:03:51 +00:00
|
|
|
virPCIDeviceGetReprobe(actual);
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
inactivedevs:
|
2016-03-18 17:03:50 +00:00
|
|
|
/* Move devices back to the inactive list so that they can be
|
|
|
|
* processed properly below (reattachdevs label) */
|
2014-03-05 12:14:38 +00:00
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDevicePtr pci = virPCIDeviceListGet(pcidevs, i);
|
2016-03-18 17:03:50 +00:00
|
|
|
virPCIDevicePtr actual;
|
2015-12-16 15:32:37 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("Removing PCI device %s from active list",
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDeviceGetName(pci));
|
2016-03-18 17:03:50 +00:00
|
|
|
if (!(actual = virPCIDeviceListSteal(mgr->activePCIHostdevs, pci)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
VIR_DEBUG("Adding PCI device %s to inactive list",
|
|
|
|
virPCIDeviceGetName(pci));
|
|
|
|
if (virPCIDeviceListAdd(mgr->inactivePCIHostdevs, actual) < 0)
|
|
|
|
VIR_WARN("Failed to add PCI device %s to the inactive list",
|
|
|
|
virPCIDeviceGetName(pci));
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
resetvfnetconfig:
|
2016-02-25 18:04:30 +00:00
|
|
|
if (last_processed_hostdev_vf >= 0) {
|
|
|
|
for (i = 0; i <= last_processed_hostdev_vf; i++)
|
2017-03-13 23:07:02 +00:00
|
|
|
virHostdevRestoreNetConfig(hostdevs[i], mgr->stateDir, NULL);
|
2016-02-25 18:04:30 +00:00
|
|
|
}
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
reattachdevs:
|
2019-07-23 17:35:41 +00:00
|
|
|
virHostdevReattachAllPCIDevices(mgr, pcidevs);
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectUnlock(mgr->activePCIHostdevs);
|
|
|
|
virObjectUnlock(mgr->inactivePCIHostdevs);
|
2016-01-25 13:24:50 +00:00
|
|
|
|
2014-03-05 12:14:38 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-06-07 12:13:24 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
virHostdevPreparePCIDevices(virHostdevManagerPtr mgr,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
const unsigned char *uuid,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
g_autoptr(virPCIDeviceList) pcidevs = NULL;
|
|
|
|
|
|
|
|
if (!nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (!(pcidevs = virHostdevGetPCIHostDeviceList(hostdevs, nhostdevs)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return virHostdevPreparePCIDevicesImpl(mgr, drv_name, dom_name, uuid,
|
|
|
|
pcidevs, hostdevs, nhostdevs, flags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-11 08:24:04 +00:00
|
|
|
static void
|
|
|
|
virHostdevReAttachPCIDevicesImpl(virHostdevManagerPtr mgr,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
virPCIDeviceListPtr pcidevs,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs,
|
|
|
|
const char *oldStateDir)
|
2014-03-05 12:14:38 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2019-06-10 15:19:01 +00:00
|
|
|
virObjectLock(mgr->activePCIHostdevs);
|
|
|
|
virObjectLock(mgr->inactivePCIHostdevs);
|
|
|
|
|
2016-02-22 14:02:23 +00:00
|
|
|
/* Reattaching devices to the host involves several steps; each
|
|
|
|
* of them is described at length below */
|
2015-04-15 17:29:43 +00:00
|
|
|
|
2016-03-18 17:03:50 +00:00
|
|
|
/* Step 1: Filter out all devices that are either not active or not
|
|
|
|
* used by the current domain and driver */
|
2015-03-26 14:23:46 +00:00
|
|
|
i = 0;
|
|
|
|
while (i < virPCIDeviceListCount(pcidevs)) {
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDevicePtr pci = virPCIDeviceListGet(pcidevs, i);
|
|
|
|
virPCIDevicePtr actual = NULL;
|
|
|
|
|
2016-03-07 12:41:19 +00:00
|
|
|
/* We need to look up the actual device, which is the one containing
|
|
|
|
* information such as by which domain and driver it is used. As a
|
|
|
|
* side effect, by looking it up we can also tell whether it was
|
|
|
|
* really active in the first place */
|
2016-02-24 13:59:25 +00:00
|
|
|
actual = virPCIDeviceListFind(mgr->activePCIHostdevs, pci);
|
|
|
|
if (actual) {
|
|
|
|
const char *actual_drvname;
|
|
|
|
const char *actual_domname;
|
|
|
|
virPCIDeviceGetUsedBy(actual, &actual_drvname, &actual_domname);
|
|
|
|
if (STRNEQ_NULLABLE(drv_name, actual_drvname) ||
|
|
|
|
STRNEQ_NULLABLE(dom_name, actual_domname)) {
|
|
|
|
|
|
|
|
virPCIDeviceListDel(pcidevs, pci);
|
2016-02-24 12:44:09 +00:00
|
|
|
continue;
|
|
|
|
}
|
2016-02-24 12:37:44 +00:00
|
|
|
} else {
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDeviceListDel(pcidevs, pci);
|
2016-02-24 12:37:44 +00:00
|
|
|
continue;
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 17:03:50 +00:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Step 2: Move devices from the active list to the inactive list */
|
|
|
|
for (i = 0; i < virPCIDeviceListCount(pcidevs); i++) {
|
|
|
|
virPCIDevicePtr pci = virPCIDeviceListGet(pcidevs, i);
|
|
|
|
virPCIDevicePtr actual;
|
|
|
|
|
2015-12-16 15:32:37 +00:00
|
|
|
VIR_DEBUG("Removing PCI device %s from active list",
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDeviceGetName(pci));
|
2016-03-18 17:03:50 +00:00
|
|
|
actual = virPCIDeviceListSteal(mgr->activePCIHostdevs, pci);
|
|
|
|
|
|
|
|
VIR_DEBUG("Adding PCI device %s to inactive list",
|
|
|
|
virPCIDeviceGetName(pci));
|
|
|
|
if (!actual ||
|
|
|
|
virPCIDeviceListAdd(mgr->inactivePCIHostdevs, actual) < 0) {
|
|
|
|
|
|
|
|
VIR_ERROR(_("Failed to add PCI device %s to the inactive list"),
|
2016-05-19 19:10:19 +00:00
|
|
|
virGetLastErrorMessage());
|
|
|
|
virResetLastError();
|
2016-03-18 17:03:50 +00:00
|
|
|
}
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
|
|
|
|
2016-03-18 17:03:50 +00:00
|
|
|
/* At this point, any device that had been used by the guest has been
|
|
|
|
* moved to the inactive list */
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2016-03-18 17:03:50 +00:00
|
|
|
/* Step 3: restore original network config of hostdevs that used
|
2015-04-24 17:27:25 +00:00
|
|
|
* <interface type='hostdev'>
|
2014-03-05 12:14:38 +00:00
|
|
|
*/
|
2015-04-15 17:29:43 +00:00
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
|
|
|
|
if (virHostdevIsPCINetDevice(hostdev)) {
|
|
|
|
virDomainHostdevSubsysPCIPtr pcisrc = &hostdev->source.subsys.u.pci;
|
2016-03-18 17:03:51 +00:00
|
|
|
virPCIDevicePtr actual;
|
2016-02-23 16:46:48 +00:00
|
|
|
|
2016-03-18 17:03:51 +00:00
|
|
|
actual = virPCIDeviceListFindByIDs(mgr->inactivePCIHostdevs,
|
|
|
|
pcisrc->addr.domain,
|
|
|
|
pcisrc->addr.bus,
|
|
|
|
pcisrc->addr.slot,
|
|
|
|
pcisrc->addr.function);
|
2016-02-23 16:46:48 +00:00
|
|
|
|
2016-03-18 17:03:51 +00:00
|
|
|
if (actual) {
|
2016-02-23 16:46:48 +00:00
|
|
|
VIR_DEBUG("Restoring network configuration of PCI device %s",
|
2016-03-18 17:03:51 +00:00
|
|
|
virPCIDeviceGetName(actual));
|
2017-03-13 23:07:02 +00:00
|
|
|
virHostdevRestoreNetConfig(hostdev, mgr->stateDir,
|
2016-02-23 16:46:48 +00:00
|
|
|
oldStateDir);
|
2015-04-15 17:29:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2016-03-18 17:03:50 +00:00
|
|
|
/* Step 4: perform a PCI Reset on all devices */
|
2019-07-23 17:35:39 +00:00
|
|
|
virHostdevResetAllPCIDevices(mgr, pcidevs);
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2016-03-18 17:03:50 +00:00
|
|
|
/* Step 5: Reattach managed devices to their host drivers; unmanaged
|
|
|
|
* devices don't need to be processed further */
|
2019-07-23 17:35:41 +00:00
|
|
|
virHostdevReattachAllPCIDevices(mgr, pcidevs);
|
2014-03-05 12:14:38 +00:00
|
|
|
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectUnlock(mgr->activePCIHostdevs);
|
|
|
|
virObjectUnlock(mgr->inactivePCIHostdevs);
|
2014-03-05 12:14:38 +00:00
|
|
|
}
|
2014-03-06 05:13:00 +00:00
|
|
|
|
2019-06-11 08:24:04 +00:00
|
|
|
|
|
|
|
/* @oldStateDir:
|
|
|
|
* For upgrade purpose: see virHostdevRestoreNetConfig
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virHostdevReAttachPCIDevices(virHostdevManagerPtr mgr,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs,
|
|
|
|
const char *oldStateDir)
|
|
|
|
{
|
|
|
|
g_autoptr(virPCIDeviceList) pcidevs = NULL;
|
|
|
|
|
|
|
|
if (!nhostdevs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!(pcidevs = virHostdevGetPCIHostDeviceList(hostdevs, nhostdevs))) {
|
|
|
|
VIR_ERROR(_("Failed to allocate PCI device list: %s"),
|
|
|
|
virGetLastErrorMessage());
|
|
|
|
virResetLastError();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
virHostdevReAttachPCIDevicesImpl(mgr, drv_name, dom_name, pcidevs,
|
|
|
|
hostdevs, nhostdevs, oldStateDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-06 05:13:00 +00:00
|
|
|
int
|
2014-03-12 16:35:30 +00:00
|
|
|
virHostdevUpdateActivePCIDevices(virHostdevManagerPtr mgr,
|
2014-03-06 07:48:49 +00:00
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs,
|
2014-03-12 16:35:30 +00:00
|
|
|
const char *drv_name,
|
2014-03-06 07:48:49 +00:00
|
|
|
const char *dom_name)
|
2014-03-06 05:13:00 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int ret = -1;
|
|
|
|
|
2014-03-06 08:02:31 +00:00
|
|
|
if (!nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
2014-03-12 16:38:18 +00:00
|
|
|
virObjectLock(mgr->activePCIHostdevs);
|
|
|
|
virObjectLock(mgr->inactivePCIHostdevs);
|
2014-03-06 05:13:00 +00:00
|
|
|
|
2014-03-06 07:48:49 +00:00
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
2019-08-19 09:04:05 +00:00
|
|
|
const virDomainHostdevDef *hostdev = hostdevs[i];
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) actual = NULL;
|
2014-03-06 05:13:00 +00:00
|
|
|
|
2019-08-19 09:04:05 +00:00
|
|
|
if (virHostdevGetPCIHostDevice(hostdev, &actual) < 0)
|
2014-03-06 05:13:00 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2019-08-19 09:04:05 +00:00
|
|
|
if (!actual)
|
|
|
|
continue;
|
2014-03-06 05:13:00 +00:00
|
|
|
|
2019-08-19 09:04:05 +00:00
|
|
|
if (virPCIDeviceSetUsedBy(actual, drv_name, dom_name) < 0)
|
|
|
|
goto cleanup;
|
2015-10-23 09:54:07 +00:00
|
|
|
|
2014-03-06 05:13:00 +00:00
|
|
|
/* Setup the original states for the PCI device */
|
2016-02-24 13:59:25 +00:00
|
|
|
virPCIDeviceSetUnbindFromStub(actual, hostdev->origstates.states.pci.unbind_from_stub);
|
|
|
|
virPCIDeviceSetRemoveSlot(actual, hostdev->origstates.states.pci.remove_slot);
|
|
|
|
virPCIDeviceSetReprobe(actual, hostdev->origstates.states.pci.reprobe);
|
2014-03-06 05:13:00 +00:00
|
|
|
|
2016-02-24 13:59:25 +00:00
|
|
|
if (virPCIDeviceListAdd(mgr->activePCIHostdevs, actual) < 0)
|
2014-03-06 05:13:00 +00:00
|
|
|
goto cleanup;
|
2016-02-24 13:59:25 +00:00
|
|
|
actual = NULL;
|
2014-03-06 05:13:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2014-03-12 16:38:18 +00:00
|
|
|
virObjectUnlock(mgr->activePCIHostdevs);
|
|
|
|
virObjectUnlock(mgr->inactivePCIHostdevs);
|
2014-03-06 05:13:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2014-03-12 16:35:30 +00:00
|
|
|
virHostdevUpdateActiveUSBDevices(virHostdevManagerPtr mgr,
|
2014-03-06 07:48:49 +00:00
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs,
|
2014-03-12 16:35:30 +00:00
|
|
|
const char *drv_name,
|
2014-03-06 07:48:49 +00:00
|
|
|
const char *dom_name)
|
2014-03-06 05:13:00 +00:00
|
|
|
{
|
|
|
|
virDomainHostdevDefPtr hostdev = NULL;
|
|
|
|
size_t i;
|
|
|
|
int ret = -1;
|
|
|
|
|
2014-03-06 08:02:31 +00:00
|
|
|
if (!nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
2014-03-12 16:38:18 +00:00
|
|
|
virObjectLock(mgr->activeUSBHostdevs);
|
2014-03-06 07:48:49 +00:00
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
2014-07-03 19:43:05 +00:00
|
|
|
virDomainHostdevSubsysUSBPtr usbsrc;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virUSBDevice) usb = NULL;
|
2014-03-06 07:48:49 +00:00
|
|
|
hostdev = hostdevs[i];
|
2014-07-03 19:43:05 +00:00
|
|
|
usbsrc = &hostdev->source.subsys.u.usb;
|
2014-03-06 05:13:00 +00:00
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
|
|
|
continue;
|
|
|
|
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
|
|
|
|
continue;
|
|
|
|
|
2014-07-03 19:43:05 +00:00
|
|
|
if (!(usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device, NULL))) {
|
2014-03-06 05:13:00 +00:00
|
|
|
VIR_WARN("Unable to reattach USB device %03d.%03d on domain %s",
|
2014-07-03 19:43:05 +00:00
|
|
|
usbsrc->bus, usbsrc->device, dom_name);
|
2014-03-06 05:13:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-03-06 07:48:49 +00:00
|
|
|
virUSBDeviceSetUsedBy(usb, drv_name, dom_name);
|
2014-03-06 05:13:00 +00:00
|
|
|
|
2019-06-15 07:56:59 +00:00
|
|
|
if (virUSBDeviceListAdd(mgr->activeUSBHostdevs, &usb) < 0)
|
2014-03-06 05:13:00 +00:00
|
|
|
goto cleanup;
|
2019-06-15 07:56:59 +00:00
|
|
|
usb = NULL;
|
2014-03-06 05:13:00 +00:00
|
|
|
}
|
|
|
|
ret = 0;
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2014-03-12 16:38:18 +00:00
|
|
|
virObjectUnlock(mgr->activeUSBHostdevs);
|
2014-03-06 05:13:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2014-07-09 13:31:38 +00:00
|
|
|
static int
|
|
|
|
virHostdevUpdateActiveSCSIHostDevices(virHostdevManagerPtr mgr,
|
|
|
|
virDomainHostdevDefPtr hostdev,
|
|
|
|
virDomainHostdevSubsysSCSIPtr scsisrc,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name)
|
|
|
|
{
|
|
|
|
virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virSCSIDevice) scsi = NULL;
|
2014-07-09 13:31:38 +00:00
|
|
|
virSCSIDevicePtr tmp = NULL;
|
|
|
|
|
|
|
|
if (!(scsi = virSCSIDeviceNew(NULL,
|
|
|
|
scsihostsrc->adapter, scsihostsrc->bus,
|
|
|
|
scsihostsrc->target, scsihostsrc->unit,
|
|
|
|
hostdev->readonly, hostdev->shareable)))
|
2018-07-24 15:52:34 +00:00
|
|
|
return -1;
|
2014-07-09 13:31:38 +00:00
|
|
|
|
|
|
|
if ((tmp = virSCSIDeviceListFind(mgr->activeSCSIHostdevs, scsi))) {
|
2019-06-15 07:56:59 +00:00
|
|
|
if (virSCSIDeviceSetUsedBy(tmp, drv_name, dom_name) < 0)
|
2018-07-24 15:52:34 +00:00
|
|
|
return -1;
|
2014-07-09 13:31:38 +00:00
|
|
|
} else {
|
|
|
|
if (virSCSIDeviceSetUsedBy(scsi, drv_name, dom_name) < 0 ||
|
2019-06-15 07:56:59 +00:00
|
|
|
virSCSIDeviceListAdd(mgr->activeSCSIHostdevs, scsi) < 0)
|
2018-07-24 15:52:34 +00:00
|
|
|
return -1;
|
2019-06-15 07:56:59 +00:00
|
|
|
scsi = NULL;
|
2014-07-09 13:31:38 +00:00
|
|
|
}
|
2018-07-24 15:52:34 +00:00
|
|
|
return 0;
|
2014-07-09 13:31:38 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 05:13:00 +00:00
|
|
|
int
|
2014-03-12 16:35:30 +00:00
|
|
|
virHostdevUpdateActiveSCSIDevices(virHostdevManagerPtr mgr,
|
2014-03-06 07:48:49 +00:00
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs,
|
2014-03-12 16:35:30 +00:00
|
|
|
const char *drv_name,
|
2014-03-06 07:48:49 +00:00
|
|
|
const char *dom_name)
|
2014-03-06 05:13:00 +00:00
|
|
|
{
|
|
|
|
virDomainHostdevDefPtr hostdev = NULL;
|
|
|
|
size_t i;
|
|
|
|
int ret = -1;
|
|
|
|
|
2014-03-06 08:02:31 +00:00
|
|
|
if (!nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
2014-03-12 16:38:18 +00:00
|
|
|
virObjectLock(mgr->activeSCSIHostdevs);
|
2014-03-06 07:48:49 +00:00
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
2014-07-09 13:31:38 +00:00
|
|
|
virDomainHostdevSubsysSCSIPtr scsisrc;
|
2014-03-06 07:48:49 +00:00
|
|
|
hostdev = hostdevs[i];
|
2014-07-09 13:31:38 +00:00
|
|
|
scsisrc = &hostdev->source.subsys.u.scsi;
|
2014-03-06 05:13:00 +00:00
|
|
|
|
2016-11-15 18:25:41 +00:00
|
|
|
if (!virHostdevIsSCSIDevice(hostdev))
|
2014-03-06 05:13:00 +00:00
|
|
|
continue;
|
|
|
|
|
2014-07-09 13:31:38 +00:00
|
|
|
if (scsisrc->protocol == VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI) {
|
|
|
|
continue; /* Not supported for iSCSI */
|
2014-03-06 05:13:00 +00:00
|
|
|
} else {
|
2014-07-09 13:31:38 +00:00
|
|
|
if (virHostdevUpdateActiveSCSIHostDevices(mgr, hostdev, scsisrc,
|
|
|
|
drv_name, dom_name) < 0)
|
2014-03-06 05:13:00 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ret = 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2014-03-12 16:38:18 +00:00
|
|
|
virObjectUnlock(mgr->activeSCSIHostdevs);
|
2014-03-06 05:13:00 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2014-03-06 05:53:56 +00:00
|
|
|
|
2017-02-03 13:04:59 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
virHostdevUpdateActiveMediatedDevices(virHostdevManagerPtr mgr,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
size_t i;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virMediatedDevice) mdev = NULL;
|
2017-02-03 13:04:59 +00:00
|
|
|
|
|
|
|
if (nhostdevs == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virObjectLock(mgr->activeMediatedHostdevs);
|
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
virDomainHostdevSubsysMediatedDevPtr mdevsrc;
|
|
|
|
|
|
|
|
mdevsrc = &hostdev->source.subsys.u.mdev;
|
|
|
|
|
2018-05-07 14:41:13 +00:00
|
|
|
if (!virHostdevIsMdevDevice(hostdev))
|
2017-02-03 13:04:59 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!(mdev = virMediatedDeviceNew(mdevsrc->uuidstr, mdevsrc->model)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
virMediatedDeviceSetUsedBy(mdev, drv_name, dom_name);
|
|
|
|
|
2017-04-28 07:24:31 +00:00
|
|
|
if (virMediatedDeviceListAdd(mgr->activeMediatedHostdevs, &mdev) < 0)
|
2017-02-03 13:04:59 +00:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virObjectUnlock(mgr->activeMediatedHostdevs);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-03-06 05:53:56 +00:00
|
|
|
static int
|
2014-03-12 16:35:30 +00:00
|
|
|
virHostdevMarkUSBDevices(virHostdevManagerPtr mgr,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
virUSBDeviceListPtr list)
|
2014-03-06 05:53:56 +00:00
|
|
|
{
|
|
|
|
size_t i, j;
|
|
|
|
unsigned int count;
|
|
|
|
virUSBDevicePtr tmp;
|
|
|
|
|
2014-03-12 16:38:18 +00:00
|
|
|
virObjectLock(mgr->activeUSBHostdevs);
|
2014-03-06 05:53:56 +00:00
|
|
|
count = virUSBDeviceListCount(list);
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
virUSBDevicePtr usb = virUSBDeviceListGet(list, i);
|
2014-03-12 16:38:18 +00:00
|
|
|
if ((tmp = virUSBDeviceListFind(mgr->activeUSBHostdevs, usb))) {
|
2014-03-06 05:53:56 +00:00
|
|
|
const char *other_drvname;
|
|
|
|
const char *other_domname;
|
|
|
|
|
|
|
|
virUSBDeviceGetUsedBy(tmp, &other_drvname, &other_domname);
|
|
|
|
if (other_drvname && other_domname)
|
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("USB device %s is in use by "
|
|
|
|
"driver %s, domain %s"),
|
|
|
|
virUSBDeviceGetName(tmp),
|
|
|
|
other_drvname, other_domname);
|
|
|
|
else
|
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("USB device %s is already in use"),
|
|
|
|
virUSBDeviceGetName(tmp));
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2014-03-06 07:24:06 +00:00
|
|
|
virUSBDeviceSetUsedBy(usb, drv_name, dom_name);
|
2014-03-12 16:38:18 +00:00
|
|
|
VIR_DEBUG("Adding %03d.%03d dom=%s to activeUSBHostdevs",
|
2014-03-06 07:24:06 +00:00
|
|
|
virUSBDeviceGetBus(usb), virUSBDeviceGetDevno(usb),
|
|
|
|
dom_name);
|
2014-03-06 05:53:56 +00:00
|
|
|
/*
|
|
|
|
* The caller is responsible to steal these usb devices
|
|
|
|
* from the virUSBDeviceList that passed in on success,
|
|
|
|
* perform rollback on failure.
|
|
|
|
*/
|
2018-07-24 15:52:23 +00:00
|
|
|
if (virUSBDeviceListAdd(mgr->activeUSBHostdevs, &usb) < 0)
|
2014-03-06 05:53:56 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2014-03-12 16:38:18 +00:00
|
|
|
virObjectUnlock(mgr->activeUSBHostdevs);
|
2014-03-06 05:53:56 +00:00
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
error:
|
2014-03-06 05:53:56 +00:00
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
tmp = virUSBDeviceListGet(list, i);
|
2014-03-12 16:38:18 +00:00
|
|
|
virUSBDeviceListSteal(mgr->activeUSBHostdevs, tmp);
|
2014-03-06 05:53:56 +00:00
|
|
|
}
|
2014-03-12 16:38:18 +00:00
|
|
|
virObjectUnlock(mgr->activeUSBHostdevs);
|
2014-03-06 05:53:56 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-11 00:39:11 +00:00
|
|
|
int
|
2014-03-06 05:53:56 +00:00
|
|
|
virHostdevFindUSBDevice(virDomainHostdevDefPtr hostdev,
|
|
|
|
bool mandatory,
|
|
|
|
virUSBDevicePtr *usb)
|
|
|
|
{
|
2014-07-03 19:43:05 +00:00
|
|
|
virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
|
|
|
|
unsigned vendor = usbsrc->vendor;
|
|
|
|
unsigned product = usbsrc->product;
|
|
|
|
unsigned bus = usbsrc->bus;
|
|
|
|
unsigned device = usbsrc->device;
|
|
|
|
bool autoAddress = usbsrc->autoAddress;
|
2014-03-06 05:53:56 +00:00
|
|
|
int rc;
|
|
|
|
|
|
|
|
*usb = NULL;
|
|
|
|
|
|
|
|
if (vendor && bus) {
|
|
|
|
rc = virUSBDeviceFind(vendor, product, bus, device,
|
|
|
|
NULL,
|
|
|
|
autoAddress ? false : mandatory,
|
|
|
|
usb);
|
|
|
|
if (rc < 0) {
|
|
|
|
return -1;
|
|
|
|
} 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) {
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virUSBDeviceList) devs = NULL;
|
2014-03-06 05:53:56 +00:00
|
|
|
|
|
|
|
rc = virUSBDeviceFindByVendor(vendor, product, NULL, mandatory, &devs);
|
2019-06-15 07:22:12 +00:00
|
|
|
if (rc < 0) {
|
2014-03-06 05:53:56 +00:00
|
|
|
return -1;
|
2019-06-15 07:22:12 +00:00
|
|
|
} else if (rc == 0) {
|
2014-03-06 05:53:56 +00:00
|
|
|
goto out;
|
|
|
|
} else if (rc > 1) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2019-06-15 07:22:12 +00:00
|
|
|
*usb = virUSBDeviceListGet(devs, 0);
|
|
|
|
virUSBDeviceListSteal(devs, *usb);
|
|
|
|
|
2014-07-03 19:43:05 +00:00
|
|
|
usbsrc->bus = virUSBDeviceGetBus(*usb);
|
|
|
|
usbsrc->device = virUSBDeviceGetDevno(*usb);
|
|
|
|
usbsrc->autoAddress = true;
|
2014-03-06 05:53:56 +00:00
|
|
|
|
|
|
|
if (autoAddress) {
|
|
|
|
VIR_INFO("USB device %x:%x found at bus:%u device:%u (moved"
|
|
|
|
" from bus:%u device:%u)",
|
|
|
|
vendor, product,
|
2014-07-03 19:43:05 +00:00
|
|
|
usbsrc->bus, usbsrc->device,
|
2014-03-06 05:53:56 +00:00
|
|
|
bus, device);
|
|
|
|
}
|
2020-08-02 20:57:29 +00:00
|
|
|
} else if (bus) {
|
2014-03-06 05:53:56 +00:00
|
|
|
if (virUSBDeviceFindByBus(bus, device, NULL, mandatory, usb) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
out:
|
2014-03-06 05:53:56 +00:00
|
|
|
if (!*usb)
|
|
|
|
hostdev->missing = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-03-01 18:53:37 +00:00
|
|
|
virHostdevPrepareUSBDevices(virHostdevManagerPtr mgr,
|
2014-03-06 05:53:56 +00:00
|
|
|
const char *drv_name,
|
2014-03-06 07:24:06 +00:00
|
|
|
const char *dom_name,
|
2014-03-06 05:53:56 +00:00
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
size_t i;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virUSBDeviceList) list = NULL;
|
2014-03-06 05:53:56 +00:00
|
|
|
virUSBDevicePtr tmp;
|
|
|
|
bool coldBoot = !!(flags & VIR_HOSTDEV_COLD_BOOT);
|
|
|
|
|
2014-03-06 08:02:31 +00:00
|
|
|
if (!nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
2014-03-06 05:53:56 +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 virHostdevPreparePCIDevices()
|
|
|
|
*/
|
|
|
|
if (!(list = virUSBDeviceListNew()))
|
2019-06-15 07:03:47 +00:00
|
|
|
return -1;
|
2014-03-06 05:53:56 +00:00
|
|
|
|
|
|
|
/* Loop 1: build temporary list
|
|
|
|
*/
|
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
bool required = true;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virUSBDevice) usb = NULL;
|
2014-03-06 05:53:56 +00:00
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
|
|
|
continue;
|
|
|
|
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (hostdev->startupPolicy == VIR_DOMAIN_STARTUP_POLICY_OPTIONAL ||
|
|
|
|
(hostdev->startupPolicy == VIR_DOMAIN_STARTUP_POLICY_REQUISITE &&
|
|
|
|
!coldBoot))
|
|
|
|
required = false;
|
|
|
|
|
|
|
|
if (virHostdevFindUSBDevice(hostdev, required, &usb) < 0)
|
2019-06-15 07:03:47 +00:00
|
|
|
return -1;
|
2014-03-06 05:53:56 +00:00
|
|
|
|
2019-06-15 07:56:59 +00:00
|
|
|
if (usb && virUSBDeviceListAdd(list, &usb) < 0)
|
2019-06-15 07:03:47 +00:00
|
|
|
return -1;
|
2019-06-15 07:56:59 +00:00
|
|
|
usb = NULL;
|
2014-03-06 05:53:56 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 07:24:06 +00:00
|
|
|
/* Mark devices in temporary list as used by @dom_name
|
2014-03-06 05:53:56 +00:00
|
|
|
* and add them do driver list. However, if something goes
|
|
|
|
* wrong, perform rollback.
|
|
|
|
*/
|
2016-03-01 18:53:37 +00:00
|
|
|
if (virHostdevMarkUSBDevices(mgr, drv_name, dom_name, list) < 0)
|
2019-06-15 07:03:47 +00:00
|
|
|
return -1;
|
2014-03-06 05:53:56 +00:00
|
|
|
|
|
|
|
/* Loop 2: Temporary list was successfully merged with
|
|
|
|
* driver list, so steal all items to avoid freeing them
|
|
|
|
* in cleanup label.
|
|
|
|
*/
|
|
|
|
while (virUSBDeviceListCount(list) > 0) {
|
|
|
|
tmp = virUSBDeviceListGet(list, 0);
|
|
|
|
virUSBDeviceListSteal(list, tmp);
|
|
|
|
}
|
|
|
|
|
2019-06-15 07:03:47 +00:00
|
|
|
return 0;
|
2014-03-06 05:53:56 +00:00
|
|
|
}
|
2014-03-06 06:19:11 +00:00
|
|
|
|
2014-07-09 13:31:38 +00:00
|
|
|
static int
|
|
|
|
virHostdevPrepareSCSIHostDevices(virDomainHostdevDefPtr hostdev,
|
|
|
|
virDomainHostdevSubsysSCSIPtr scsisrc,
|
|
|
|
virSCSIDeviceListPtr list)
|
|
|
|
{
|
|
|
|
virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virSCSIDevice) scsi = NULL;
|
2014-07-09 13:31:38 +00:00
|
|
|
|
|
|
|
if (hostdev->managed) {
|
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("SCSI host device doesn't support managed mode"));
|
2018-07-24 15:52:34 +00:00
|
|
|
return -1;
|
2014-07-09 13:31:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(scsi = virSCSIDeviceNew(NULL,
|
|
|
|
scsihostsrc->adapter, scsihostsrc->bus,
|
|
|
|
scsihostsrc->target, scsihostsrc->unit,
|
|
|
|
hostdev->readonly, hostdev->shareable)))
|
2018-07-24 15:52:34 +00:00
|
|
|
return -1;
|
2014-07-09 13:31:38 +00:00
|
|
|
|
2019-06-15 07:56:59 +00:00
|
|
|
if (virSCSIDeviceListAdd(list, scsi) < 0)
|
2018-07-24 15:52:34 +00:00
|
|
|
return -1;
|
2019-06-15 07:56:59 +00:00
|
|
|
scsi = NULL;
|
2014-07-09 13:31:38 +00:00
|
|
|
|
2018-07-24 15:52:34 +00:00
|
|
|
return 0;
|
2014-07-09 13:31:38 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 06:19:11 +00:00
|
|
|
int
|
2016-03-01 18:53:37 +00:00
|
|
|
virHostdevPrepareSCSIDevices(virHostdevManagerPtr mgr,
|
2014-03-06 06:19:11 +00:00
|
|
|
const char *drv_name,
|
2014-03-06 07:24:06 +00:00
|
|
|
const char *dom_name,
|
2014-03-06 06:19:11 +00:00
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
|
|
|
{
|
|
|
|
size_t i, j;
|
|
|
|
int count;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virSCSIDeviceList) list = NULL;
|
2014-03-06 06:19:11 +00:00
|
|
|
virSCSIDevicePtr tmp;
|
|
|
|
|
2014-03-06 08:02:31 +00:00
|
|
|
if (!nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
2014-03-06 06:19:11 +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 virHostdevPreparePCIDevices()
|
|
|
|
*/
|
|
|
|
if (!(list = virSCSIDeviceListNew()))
|
2019-06-15 07:03:47 +00:00
|
|
|
return -1;
|
2014-03-06 06:19:11 +00:00
|
|
|
|
|
|
|
/* Loop 1: build temporary list */
|
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
2014-07-09 13:31:38 +00:00
|
|
|
virDomainHostdevSubsysSCSIPtr scsisrc = &hostdev->source.subsys.u.scsi;
|
2014-03-06 06:19:11 +00:00
|
|
|
|
2016-11-15 18:25:41 +00:00
|
|
|
if (!virHostdevIsSCSIDevice(hostdev))
|
2014-03-06 06:19:11 +00:00
|
|
|
continue;
|
|
|
|
|
2014-07-09 13:31:38 +00:00
|
|
|
if (scsisrc->protocol == VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI) {
|
|
|
|
continue; /* Not supported for iSCSI */
|
|
|
|
} else {
|
|
|
|
if (virHostdevPrepareSCSIHostDevices(hostdev, scsisrc, list) < 0)
|
2019-06-15 07:03:47 +00:00
|
|
|
return -1;
|
2014-03-06 06:19:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop 2: Mark devices in temporary list as used by @name
|
|
|
|
* and add them to driver list. However, if something goes
|
|
|
|
* wrong, perform rollback.
|
|
|
|
*/
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectLock(mgr->activeSCSIHostdevs);
|
2014-03-06 06:19:11 +00:00
|
|
|
count = virSCSIDeviceListCount(list);
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
virSCSIDevicePtr scsi = virSCSIDeviceListGet(list, i);
|
2016-03-01 18:53:37 +00:00
|
|
|
if ((tmp = virSCSIDeviceListFind(mgr->activeSCSIHostdevs,
|
2014-03-06 06:19:11 +00:00
|
|
|
scsi))) {
|
|
|
|
bool scsi_shareable = virSCSIDeviceGetShareable(scsi);
|
|
|
|
bool tmp_shareable = virSCSIDeviceGetShareable(tmp);
|
|
|
|
|
|
|
|
if (!(scsi_shareable && tmp_shareable)) {
|
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("SCSI device %s is already in use by "
|
|
|
|
"other domain(s) as '%s'"),
|
|
|
|
virSCSIDeviceGetName(tmp),
|
|
|
|
tmp_shareable ? "shareable" : "non-shareable");
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
|
2014-03-06 07:24:06 +00:00
|
|
|
if (virSCSIDeviceSetUsedBy(tmp, drv_name, dom_name) < 0)
|
2014-03-06 06:19:11 +00:00
|
|
|
goto error;
|
|
|
|
} else {
|
2014-03-06 07:24:06 +00:00
|
|
|
if (virSCSIDeviceSetUsedBy(scsi, drv_name, dom_name) < 0)
|
2014-03-06 06:19:11 +00:00
|
|
|
goto error;
|
|
|
|
|
2014-03-12 16:38:18 +00:00
|
|
|
VIR_DEBUG("Adding %s to activeSCSIHostdevs", virSCSIDeviceGetName(scsi));
|
2014-03-06 06:19:11 +00:00
|
|
|
|
2016-03-01 18:53:37 +00:00
|
|
|
if (virSCSIDeviceListAdd(mgr->activeSCSIHostdevs, scsi) < 0)
|
2014-03-06 06:19:11 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectUnlock(mgr->activeSCSIHostdevs);
|
2014-03-06 06:19:11 +00:00
|
|
|
|
|
|
|
/* Loop 3: Temporary list was successfully merged with
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
error:
|
2014-03-06 06:19:11 +00:00
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
tmp = virSCSIDeviceListGet(list, i);
|
2016-03-01 18:53:37 +00:00
|
|
|
virSCSIDeviceListSteal(mgr->activeSCSIHostdevs, tmp);
|
2014-03-06 06:19:11 +00:00
|
|
|
}
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectUnlock(mgr->activeSCSIHostdevs);
|
2014-03-06 06:19:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-06 06:37:30 +00:00
|
|
|
|
2016-11-22 03:58:17 +00:00
|
|
|
int
|
|
|
|
virHostdevPrepareSCSIVHostDevices(virHostdevManagerPtr mgr,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virSCSIVHostDeviceList) list = NULL;
|
2019-06-15 07:37:18 +00:00
|
|
|
virSCSIVHostDevicePtr tmp;
|
|
|
|
size_t i, j;
|
2016-11-22 03:58:17 +00:00
|
|
|
|
|
|
|
if (!nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* To prevent situation where scsi_host device is assigned to two domains
|
|
|
|
* we need to keep a list of currently assigned scsi_host devices.
|
|
|
|
* This is done in several loops which cannot be joined into one big
|
|
|
|
* loop. See virHostdevPreparePCIDevices()
|
|
|
|
*/
|
|
|
|
if (!(list = virSCSIVHostDeviceListNew()))
|
2019-06-15 07:03:47 +00:00
|
|
|
return -1;
|
2016-11-22 03:58:17 +00:00
|
|
|
|
|
|
|
/* Loop 1: build temporary list */
|
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
virDomainHostdevSubsysSCSIVHostPtr hostsrc = &hostdev->source.subsys.u.scsi_host;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virSCSIVHostDevice) host = NULL;
|
2016-11-22 03:58:17 +00:00
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
|
|
|
|
hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (hostsrc->protocol != VIR_DOMAIN_HOSTDEV_SUBSYS_SCSI_HOST_PROTOCOL_TYPE_VHOST)
|
|
|
|
continue; /* Not supported */
|
|
|
|
|
|
|
|
if (!(host = virSCSIVHostDeviceNew(hostsrc->wwpn)))
|
2019-06-15 07:03:47 +00:00
|
|
|
return -1;
|
2016-11-22 03:58:17 +00:00
|
|
|
|
2019-06-15 07:37:18 +00:00
|
|
|
if (virSCSIVHostDeviceSetUsedBy(host, drv_name, dom_name) < 0)
|
2019-06-15 07:03:47 +00:00
|
|
|
return -1;
|
2019-06-15 07:37:18 +00:00
|
|
|
|
|
|
|
if (virSCSIVHostDeviceListAdd(list, host) < 0)
|
|
|
|
return -1;
|
|
|
|
host = NULL;
|
2016-11-22 03:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Loop 2: Mark devices in temporary list as used by @name
|
|
|
|
* and add them to driver list. However, if something goes
|
|
|
|
* wrong, perform rollback.
|
|
|
|
*/
|
|
|
|
virObjectLock(mgr->activeSCSIVHostHostdevs);
|
|
|
|
|
2019-06-15 07:37:18 +00:00
|
|
|
for (i = 0; i < virSCSIVHostDeviceListCount(list); i++) {
|
|
|
|
tmp = virSCSIVHostDeviceListGet(list, i);
|
2016-11-22 03:58:17 +00:00
|
|
|
|
2019-06-15 07:37:18 +00:00
|
|
|
VIR_DEBUG("Adding %s to activeSCSIVHostHostdevs",
|
|
|
|
virSCSIVHostDeviceGetName(tmp));
|
2016-11-22 03:58:17 +00:00
|
|
|
|
2019-06-15 07:37:18 +00:00
|
|
|
if (virSCSIVHostDeviceListAdd(mgr->activeSCSIVHostHostdevs, tmp) < 0)
|
|
|
|
goto rollback;
|
2016-11-22 03:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
virObjectUnlock(mgr->activeSCSIVHostHostdevs);
|
|
|
|
|
|
|
|
/* Loop 3: Temporary list was successfully merged with
|
|
|
|
* driver list, so steal all items to avoid freeing them
|
|
|
|
* when freeing temporary list.
|
|
|
|
*/
|
|
|
|
while (virSCSIVHostDeviceListCount(list) > 0) {
|
|
|
|
tmp = virSCSIVHostDeviceListGet(list, 0);
|
|
|
|
virSCSIVHostDeviceListSteal(list, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2019-06-15 07:37:18 +00:00
|
|
|
|
|
|
|
rollback:
|
2016-11-22 03:58:17 +00:00
|
|
|
for (j = 0; j < i; j++) {
|
|
|
|
tmp = virSCSIVHostDeviceListGet(list, i);
|
|
|
|
virSCSIVHostDeviceListSteal(mgr->activeSCSIVHostHostdevs, tmp);
|
|
|
|
}
|
|
|
|
virObjectUnlock(mgr->activeSCSIVHostHostdevs);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-02-03 13:04:59 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
virHostdevPrepareMediatedDevices(virHostdevManagerPtr mgr,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
|
|
|
{
|
|
|
|
size_t i;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virMediatedDeviceList) list = NULL;
|
2017-02-03 13:04:59 +00:00
|
|
|
|
|
|
|
if (!nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* To prevent situation where mediated device is assigned to multiple
|
|
|
|
* domains we maintain a driver list of currently assigned mediated devices.
|
|
|
|
* A device is appended to the driver list after a series of preparations.
|
|
|
|
*/
|
|
|
|
if (!(list = virMediatedDeviceListNew()))
|
2019-06-15 07:03:47 +00:00
|
|
|
return -1;
|
2017-02-03 13:04:59 +00:00
|
|
|
|
|
|
|
/* Loop 1: Build a temporary list of ALL mediated devices. */
|
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
virDomainHostdevSubsysMediatedDevPtr src = &hostdev->source.subsys.u.mdev;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virMediatedDevice) mdev = NULL;
|
2017-02-03 13:04:59 +00:00
|
|
|
|
2018-05-07 14:41:13 +00:00
|
|
|
if (!virHostdevIsMdevDevice(hostdev))
|
2017-02-03 13:04:59 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!(mdev = virMediatedDeviceNew(src->uuidstr, src->model)))
|
2019-06-15 07:03:47 +00:00
|
|
|
return -1;
|
2017-02-03 13:04:59 +00:00
|
|
|
|
2019-06-15 07:56:59 +00:00
|
|
|
if (virMediatedDeviceListAdd(list, &mdev) < 0)
|
2019-06-15 07:03:47 +00:00
|
|
|
return -1;
|
2019-06-15 07:56:59 +00:00
|
|
|
mdev = NULL;
|
2017-02-03 13:04:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Mark the devices in the list as used by @drv_name-@dom_name and copy the
|
|
|
|
* references to the driver list
|
|
|
|
*/
|
|
|
|
if (virMediatedDeviceListMarkDevices(mgr->activeMediatedHostdevs,
|
|
|
|
list, drv_name, dom_name) < 0)
|
2019-06-15 07:03:47 +00:00
|
|
|
return -1;
|
2017-02-03 13:04:59 +00:00
|
|
|
|
|
|
|
/* Loop 2: Temporary list was successfully merged with
|
|
|
|
* driver list, so steal all items to avoid freeing them
|
|
|
|
* in cleanup label.
|
|
|
|
*/
|
|
|
|
while (virMediatedDeviceListCount(list) > 0) {
|
|
|
|
virMediatedDevicePtr tmp = virMediatedDeviceListGet(list, 0);
|
|
|
|
virMediatedDeviceListSteal(list, tmp);
|
|
|
|
}
|
|
|
|
|
2019-06-15 07:03:47 +00:00
|
|
|
return 0;
|
2017-02-03 13:04:59 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 06:37:30 +00:00
|
|
|
void
|
2016-03-01 18:53:37 +00:00
|
|
|
virHostdevReAttachUSBDevices(virHostdevManagerPtr mgr,
|
2014-03-12 16:35:30 +00:00
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
2014-03-06 06:37:30 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2014-03-06 08:02:31 +00:00
|
|
|
if (!nhostdevs)
|
|
|
|
return;
|
|
|
|
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectLock(mgr->activeUSBHostdevs);
|
2014-03-06 06:37:30 +00:00
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
2014-07-03 19:43:05 +00:00
|
|
|
virDomainHostdevSubsysUSBPtr usbsrc = &hostdev->source.subsys.u.usb;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virUSBDevice) usb = NULL;
|
2018-07-24 15:52:34 +00:00
|
|
|
virUSBDevicePtr tmp;
|
2014-03-06 06:37:30 +00:00
|
|
|
const char *usedby_drvname;
|
|
|
|
const char *usedby_domname;
|
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS)
|
|
|
|
continue;
|
|
|
|
if (hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_USB)
|
|
|
|
continue;
|
|
|
|
if (hostdev->missing)
|
|
|
|
continue;
|
|
|
|
|
2014-07-03 19:43:05 +00:00
|
|
|
if (!(usb = virUSBDeviceNew(usbsrc->bus, usbsrc->device, NULL))) {
|
2014-03-06 06:37:30 +00:00
|
|
|
VIR_WARN("Unable to reattach USB device %03d.%03d on domain %s",
|
2017-03-21 18:25:43 +00:00
|
|
|
usbsrc->bus, usbsrc->device, NULLSTR(dom_name));
|
2014-03-06 06:37:30 +00:00
|
|
|
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 */
|
|
|
|
|
2016-03-01 18:53:37 +00:00
|
|
|
tmp = virUSBDeviceListFind(mgr->activeUSBHostdevs, usb);
|
2014-03-06 06:37:30 +00:00
|
|
|
|
|
|
|
if (!tmp) {
|
|
|
|
VIR_WARN("Unable to find device %03d.%03d "
|
|
|
|
"in list of active USB devices",
|
2014-07-03 19:43:05 +00:00
|
|
|
usbsrc->bus, usbsrc->device);
|
2014-03-06 06:37:30 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
virUSBDeviceGetUsedBy(tmp, &usedby_drvname, &usedby_domname);
|
|
|
|
if (STREQ_NULLABLE(drv_name, usedby_drvname) &&
|
2014-03-06 07:24:06 +00:00
|
|
|
STREQ_NULLABLE(dom_name, usedby_domname)) {
|
2014-03-12 16:38:18 +00:00
|
|
|
VIR_DEBUG("Removing %03d.%03d dom=%s from activeUSBHostdevs",
|
2014-07-03 19:43:05 +00:00
|
|
|
usbsrc->bus, usbsrc->device, dom_name);
|
2016-03-01 18:53:37 +00:00
|
|
|
virUSBDeviceListDel(mgr->activeUSBHostdevs, tmp);
|
2014-03-06 06:37:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectUnlock(mgr->activeUSBHostdevs);
|
2014-03-06 06:37:30 +00:00
|
|
|
}
|
2014-03-06 06:49:23 +00:00
|
|
|
|
2014-07-09 13:31:38 +00:00
|
|
|
static void
|
2016-03-01 18:53:37 +00:00
|
|
|
virHostdevReAttachSCSIHostDevices(virHostdevManagerPtr mgr,
|
2014-07-09 13:31:38 +00:00
|
|
|
virDomainHostdevDefPtr hostdev,
|
|
|
|
virDomainHostdevSubsysSCSIPtr scsisrc,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name)
|
|
|
|
{
|
|
|
|
virDomainHostdevSubsysSCSIHostPtr scsihostsrc = &scsisrc->u.host;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virSCSIDevice) scsi = NULL;
|
2014-07-09 13:31:38 +00:00
|
|
|
virSCSIDevicePtr tmp;
|
|
|
|
|
|
|
|
if (!(scsi = virSCSIDeviceNew(NULL,
|
|
|
|
scsihostsrc->adapter, scsihostsrc->bus,
|
|
|
|
scsihostsrc->target, scsihostsrc->unit,
|
|
|
|
hostdev->readonly, hostdev->shareable))) {
|
2015-06-17 03:29:53 +00:00
|
|
|
VIR_WARN("Unable to reattach SCSI device %s:%u:%u:%llu on domain %s",
|
2014-07-09 13:31:38 +00:00
|
|
|
scsihostsrc->adapter, scsihostsrc->bus, scsihostsrc->target,
|
|
|
|
scsihostsrc->unit, dom_name);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only delete the devices which are marked as being used by @name,
|
|
|
|
* because qemuProcessStart could fail half way through. */
|
|
|
|
|
2016-03-01 18:53:37 +00:00
|
|
|
if (!(tmp = virSCSIDeviceListFind(mgr->activeSCSIHostdevs, scsi))) {
|
2015-06-17 03:29:53 +00:00
|
|
|
VIR_WARN("Unable to find device %s:%u:%u:%llu "
|
2014-07-09 13:31:38 +00:00
|
|
|
"in list of active SCSI devices",
|
|
|
|
scsihostsrc->adapter, scsihostsrc->bus,
|
|
|
|
scsihostsrc->target, scsihostsrc->unit);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-06-17 03:29:53 +00:00
|
|
|
VIR_DEBUG("Removing %s:%u:%u:%llu dom=%s from activeSCSIHostdevs",
|
2018-09-19 08:38:14 +00:00
|
|
|
scsihostsrc->adapter, scsihostsrc->bus, scsihostsrc->target,
|
|
|
|
scsihostsrc->unit, dom_name);
|
2014-07-09 13:31:38 +00:00
|
|
|
|
2016-03-01 18:53:37 +00:00
|
|
|
virSCSIDeviceListDel(mgr->activeSCSIHostdevs, tmp,
|
2014-07-09 13:31:38 +00:00
|
|
|
drv_name, dom_name);
|
|
|
|
}
|
|
|
|
|
2014-03-06 06:49:23 +00:00
|
|
|
void
|
2016-03-01 18:53:37 +00:00
|
|
|
virHostdevReAttachSCSIDevices(virHostdevManagerPtr mgr,
|
2014-03-12 16:35:30 +00:00
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
2014-03-06 06:49:23 +00:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
2014-03-06 08:02:31 +00:00
|
|
|
if (!nhostdevs)
|
|
|
|
return;
|
|
|
|
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectLock(mgr->activeSCSIHostdevs);
|
2014-03-06 06:49:23 +00:00
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
2014-07-09 13:31:38 +00:00
|
|
|
virDomainHostdevSubsysSCSIPtr scsisrc = &hostdev->source.subsys.u.scsi;
|
2014-03-06 06:49:23 +00:00
|
|
|
|
2016-11-15 18:25:41 +00:00
|
|
|
if (!virHostdevIsSCSIDevice(hostdev))
|
2014-03-06 06:49:23 +00:00
|
|
|
continue;
|
|
|
|
|
2014-07-09 13:31:38 +00:00
|
|
|
if (scsisrc->protocol == VIR_DOMAIN_HOSTDEV_SCSI_PROTOCOL_TYPE_ISCSI)
|
|
|
|
continue; /* Not supported for iSCSI */
|
|
|
|
else
|
2016-03-01 18:53:37 +00:00
|
|
|
virHostdevReAttachSCSIHostDevices(mgr, hostdev, scsisrc,
|
2014-07-09 13:31:38 +00:00
|
|
|
drv_name, dom_name);
|
2014-03-06 06:49:23 +00:00
|
|
|
}
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectUnlock(mgr->activeSCSIHostdevs);
|
2014-03-06 06:49:23 +00:00
|
|
|
}
|
2014-03-06 07:20:14 +00:00
|
|
|
|
2016-11-22 03:58:17 +00:00
|
|
|
void
|
|
|
|
virHostdevReAttachSCSIVHostDevices(virHostdevManagerPtr mgr,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!nhostdevs)
|
|
|
|
return;
|
|
|
|
|
|
|
|
virObjectLock(mgr->activeSCSIVHostHostdevs);
|
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virSCSIVHostDevice) host = NULL;
|
2018-07-24 15:52:34 +00:00
|
|
|
virSCSIVHostDevicePtr tmp;
|
2016-11-22 03:58:17 +00:00
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
virDomainHostdevSubsysSCSIVHostPtr hostsrc = &hostdev->source.subsys.u.scsi_host;
|
|
|
|
const char *usedby_drvname;
|
|
|
|
const char *usedby_domname;
|
|
|
|
|
|
|
|
if (hostdev->mode != VIR_DOMAIN_HOSTDEV_MODE_SUBSYS ||
|
|
|
|
hostdev->source.subsys.type != VIR_DOMAIN_HOSTDEV_SUBSYS_TYPE_SCSI_HOST)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (hostsrc->protocol != VIR_DOMAIN_HOSTDEV_SUBSYS_SCSI_HOST_PROTOCOL_TYPE_VHOST)
|
|
|
|
continue; /* Not supported */
|
|
|
|
|
|
|
|
if (!(host = virSCSIVHostDeviceNew(hostsrc->wwpn))) {
|
|
|
|
VIR_WARN("Unable to reattach SCSI_host device %s on domain %s",
|
2017-03-22 11:23:46 +00:00
|
|
|
hostsrc->wwpn, NULLSTR(dom_name));
|
2016-11-22 03:58:17 +00:00
|
|
|
virObjectUnlock(mgr->activeSCSIVHostHostdevs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Only delete the devices which are marked as being used by @name,
|
|
|
|
* because qemuProcessStart could fail half way through. */
|
|
|
|
|
|
|
|
if (!(tmp = virSCSIVHostDeviceListFind(mgr->activeSCSIVHostHostdevs,
|
|
|
|
host))) {
|
|
|
|
VIR_WARN("Unable to find device %s "
|
|
|
|
"in list of active SCSI_host devices",
|
|
|
|
hostsrc->wwpn);
|
|
|
|
virObjectUnlock(mgr->activeSCSIVHostHostdevs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
virSCSIVHostDeviceGetUsedBy(tmp, &usedby_drvname, &usedby_domname);
|
|
|
|
|
|
|
|
if (STREQ_NULLABLE(drv_name, usedby_drvname) &&
|
|
|
|
STREQ_NULLABLE(dom_name, usedby_domname)) {
|
|
|
|
VIR_DEBUG("Removing %s dom=%s from activeSCSIVHostHostdevs",
|
2018-09-19 08:38:14 +00:00
|
|
|
hostsrc->wwpn, dom_name);
|
2016-11-22 03:58:17 +00:00
|
|
|
|
|
|
|
virSCSIVHostDeviceListDel(mgr->activeSCSIVHostHostdevs, tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virObjectUnlock(mgr->activeSCSIVHostHostdevs);
|
|
|
|
}
|
|
|
|
|
2017-02-03 13:04:59 +00:00
|
|
|
/* TODO: Rename this function along with all virHostdevReAttach* functions that
|
|
|
|
* have nothing to do with an explicit re-attachment of a device back to the
|
|
|
|
* host driver (like PCI).
|
|
|
|
* Despite what the function name suggests, there's nothing to be re-attached
|
|
|
|
* for mediated devices, the function merely removes a mediated device from the
|
|
|
|
* list of active host devices.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
virHostdevReAttachMediatedDevices(virHostdevManagerPtr mgr,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
virDomainHostdevDefPtr *hostdevs,
|
|
|
|
int nhostdevs)
|
|
|
|
{
|
|
|
|
const char *used_by_drvname = NULL;
|
|
|
|
const char *used_by_domname = NULL;
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (nhostdevs == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
virObjectLock(mgr->activeMediatedHostdevs);
|
|
|
|
for (i = 0; i < nhostdevs; i++) {
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virMediatedDevice) mdev = NULL;
|
2018-07-24 15:52:34 +00:00
|
|
|
virMediatedDevicePtr tmp;
|
2017-02-03 13:04:59 +00:00
|
|
|
virDomainHostdevSubsysMediatedDevPtr mdevsrc;
|
|
|
|
virDomainHostdevDefPtr hostdev = hostdevs[i];
|
|
|
|
|
|
|
|
mdevsrc = &hostdev->source.subsys.u.mdev;
|
|
|
|
|
2018-05-07 14:41:13 +00:00
|
|
|
if (!virHostdevIsMdevDevice(hostdev))
|
2017-02-03 13:04:59 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!(mdev = virMediatedDeviceNew(mdevsrc->uuidstr,
|
|
|
|
mdevsrc->model)))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
/* Remove from the list only mdevs assigned to @drv_name/@dom_name */
|
|
|
|
|
|
|
|
tmp = virMediatedDeviceListFind(mgr->activeMediatedHostdevs, mdev);
|
|
|
|
|
|
|
|
/* skip inactive devices */
|
|
|
|
if (!tmp)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
virMediatedDeviceGetUsedBy(tmp, &used_by_drvname, &used_by_domname);
|
|
|
|
if (STREQ_NULLABLE(drv_name, used_by_drvname) &&
|
|
|
|
STREQ_NULLABLE(dom_name, used_by_domname)) {
|
|
|
|
VIR_DEBUG("Removing %s dom=%s from activeMediatedHostdevs",
|
|
|
|
mdevsrc->uuidstr, dom_name);
|
|
|
|
virMediatedDeviceListDel(mgr->activeMediatedHostdevs, tmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
virObjectUnlock(mgr->activeMediatedHostdevs);
|
|
|
|
}
|
|
|
|
|
2014-03-06 07:20:14 +00:00
|
|
|
int
|
2016-03-01 18:53:37 +00:00
|
|
|
virHostdevPCINodeDeviceDetach(virHostdevManagerPtr mgr,
|
2014-03-06 07:20:14 +00:00
|
|
|
virPCIDevicePtr pci)
|
|
|
|
{
|
2019-08-14 09:13:21 +00:00
|
|
|
struct virHostdevIsPCINodeDeviceUsedData data = {mgr, NULL, NULL, false};
|
2014-03-06 07:20:14 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectLock(mgr->activePCIHostdevs);
|
|
|
|
virObjectLock(mgr->inactivePCIHostdevs);
|
2014-03-06 07:20:14 +00:00
|
|
|
|
2015-12-15 08:44:35 +00:00
|
|
|
if (virHostdevIsPCINodeDeviceUsed(virPCIDeviceGetAddress(pci), &data))
|
2016-01-25 13:24:50 +00:00
|
|
|
goto cleanup;
|
2015-04-01 10:58:05 +00:00
|
|
|
|
2016-03-01 18:53:37 +00:00
|
|
|
if (virPCIDeviceDetach(pci, mgr->activePCIHostdevs,
|
|
|
|
mgr->inactivePCIHostdevs) < 0)
|
2016-01-25 13:24:50 +00:00
|
|
|
goto cleanup;
|
2014-03-06 07:20:14 +00:00
|
|
|
|
|
|
|
ret = 0;
|
2016-01-25 13:24:50 +00:00
|
|
|
|
|
|
|
cleanup:
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectUnlock(mgr->inactivePCIHostdevs);
|
|
|
|
virObjectUnlock(mgr->activePCIHostdevs);
|
2016-01-25 13:24:50 +00:00
|
|
|
|
2014-03-06 07:20:14 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-03-01 18:53:37 +00:00
|
|
|
virHostdevPCINodeDeviceReAttach(virHostdevManagerPtr mgr,
|
2014-03-06 07:20:14 +00:00
|
|
|
virPCIDevicePtr pci)
|
|
|
|
{
|
2019-08-14 09:13:21 +00:00
|
|
|
struct virHostdevIsPCINodeDeviceUsedData data = {mgr, NULL, NULL, false};
|
2014-03-06 07:20:14 +00:00
|
|
|
int ret = -1;
|
|
|
|
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectLock(mgr->activePCIHostdevs);
|
|
|
|
virObjectLock(mgr->inactivePCIHostdevs);
|
2014-03-06 07:20:14 +00:00
|
|
|
|
2015-12-15 08:44:35 +00:00
|
|
|
if (virHostdevIsPCINodeDeviceUsed(virPCIDeviceGetAddress(pci), &data))
|
2016-01-25 13:24:50 +00:00
|
|
|
goto cleanup;
|
2014-03-06 07:20:14 +00:00
|
|
|
|
2016-04-01 12:51:29 +00:00
|
|
|
virPCIDeviceSetUnbindFromStub(pci, true);
|
|
|
|
virPCIDeviceSetRemoveSlot(pci, true);
|
|
|
|
virPCIDeviceSetReprobe(pci, true);
|
2014-03-06 07:20:14 +00:00
|
|
|
|
2016-04-01 12:51:29 +00:00
|
|
|
if (virPCIDeviceReattach(pci, mgr->activePCIHostdevs,
|
2016-03-01 18:53:37 +00:00
|
|
|
mgr->inactivePCIHostdevs) < 0)
|
2016-01-25 13:24:50 +00:00
|
|
|
goto cleanup;
|
2014-03-06 07:20:14 +00:00
|
|
|
|
|
|
|
ret = 0;
|
2016-01-25 13:24:50 +00:00
|
|
|
|
|
|
|
cleanup:
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectUnlock(mgr->inactivePCIHostdevs);
|
|
|
|
virObjectUnlock(mgr->activePCIHostdevs);
|
2016-01-25 13:24:50 +00:00
|
|
|
|
2014-03-06 07:20:14 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2016-03-01 18:53:37 +00:00
|
|
|
virHostdevPCINodeDeviceReset(virHostdevManagerPtr mgr,
|
2014-03-06 07:20:14 +00:00
|
|
|
virPCIDevicePtr pci)
|
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectLock(mgr->activePCIHostdevs);
|
|
|
|
virObjectLock(mgr->inactivePCIHostdevs);
|
|
|
|
if (virPCIDeviceReset(pci, mgr->activePCIHostdevs,
|
|
|
|
mgr->inactivePCIHostdevs) < 0)
|
2014-03-06 07:20:14 +00:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
ret = 0;
|
2014-03-25 06:53:22 +00:00
|
|
|
out:
|
2016-03-01 18:53:37 +00:00
|
|
|
virObjectUnlock(mgr->inactivePCIHostdevs);
|
|
|
|
virObjectUnlock(mgr->activePCIHostdevs);
|
2014-03-06 07:20:14 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2014-03-06 07:57:03 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
virHostdevPrepareDomainDevices(virHostdevManagerPtr mgr,
|
|
|
|
const char *driver,
|
|
|
|
virDomainDefPtr def,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
if (!def->nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
2017-03-21 18:32:01 +00:00
|
|
|
if (!mgr) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
|
|
|
|
_("no host device manager defined"));
|
2014-03-06 07:57:03 +00:00
|
|
|
return -1;
|
2017-03-21 18:32:01 +00:00
|
|
|
}
|
2014-03-06 07:57:03 +00:00
|
|
|
|
|
|
|
if (flags & VIR_HOSTDEV_SP_PCI) {
|
|
|
|
if (virHostdevPreparePCIDevices(mgr, driver,
|
|
|
|
def->name, def->uuid,
|
|
|
|
def->hostdevs,
|
|
|
|
def->nhostdevs,
|
|
|
|
flags) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & VIR_HOSTDEV_SP_USB) {
|
|
|
|
if (virHostdevPrepareUSBDevices(mgr, driver, def->name,
|
2018-09-19 08:38:14 +00:00
|
|
|
def->hostdevs, def->nhostdevs,
|
|
|
|
flags) < 0)
|
2014-03-06 07:57:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & VIR_HOSTDEV_SP_SCSI) {
|
|
|
|
if (virHostdevPrepareSCSIDevices(mgr, driver, def->name,
|
|
|
|
def->hostdevs, def->nhostdevs) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* @oldStateDir
|
2014-03-13 11:59:32 +00:00
|
|
|
* For upgrade purpose: see virHostdevReAttachPCIHostdevs
|
2014-03-06 07:57:03 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
virHostdevReAttachDomainDevices(virHostdevManagerPtr mgr,
|
|
|
|
const char *driver,
|
|
|
|
virDomainDefPtr def,
|
|
|
|
unsigned int flags,
|
|
|
|
const char *oldStateDir)
|
|
|
|
{
|
|
|
|
if (!def->nhostdevs || !mgr)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (flags & VIR_HOSTDEV_SP_PCI) {
|
|
|
|
virHostdevReAttachPCIDevices(mgr, driver, def->name,
|
|
|
|
def->hostdevs, def->nhostdevs,
|
|
|
|
oldStateDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & VIR_HOSTDEV_SP_USB) {
|
|
|
|
virHostdevReAttachUSBDevices(mgr, driver, def->name,
|
|
|
|
def->hostdevs, def->nhostdevs);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & VIR_HOSTDEV_SP_SCSI) {
|
|
|
|
virHostdevReAttachSCSIDevices(mgr, driver, def->name,
|
|
|
|
def->hostdevs, def->nhostdevs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2015-10-26 10:59:41 +00:00
|
|
|
virHostdevUpdateActiveDomainDevices(virHostdevManagerPtr mgr,
|
2014-03-06 07:57:03 +00:00
|
|
|
const char *driver,
|
|
|
|
virDomainDefPtr def,
|
|
|
|
unsigned int flags)
|
|
|
|
{
|
|
|
|
if (!def->nhostdevs)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (flags & VIR_HOSTDEV_SP_PCI) {
|
|
|
|
if (virHostdevUpdateActivePCIDevices(mgr,
|
|
|
|
def->hostdevs,
|
|
|
|
def->nhostdevs,
|
|
|
|
driver, def->name) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & VIR_HOSTDEV_SP_USB) {
|
|
|
|
if (virHostdevUpdateActiveUSBDevices(mgr,
|
|
|
|
def->hostdevs,
|
|
|
|
def->nhostdevs,
|
|
|
|
driver, def->name) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (flags & VIR_HOSTDEV_SP_SCSI) {
|
|
|
|
if (virHostdevUpdateActiveSCSIDevices(mgr,
|
|
|
|
def->hostdevs,
|
|
|
|
def->nhostdevs,
|
|
|
|
driver, def->name) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-06-06 09:58:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
virHostdevGetNVMeDeviceList(virNVMeDeviceListPtr nvmeDevices,
|
|
|
|
virStorageSourcePtr src,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name)
|
|
|
|
{
|
|
|
|
virStorageSourcePtr n;
|
|
|
|
|
|
|
|
for (n = src; virStorageSourceIsBacking(n); n = n->backingStore) {
|
|
|
|
g_autoptr(virNVMeDevice) dev = NULL;
|
|
|
|
const virStorageSourceNVMeDef *srcNVMe = n->nvme;
|
|
|
|
|
|
|
|
if (n->type != VIR_STORAGE_TYPE_NVME)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!(dev = virNVMeDeviceNew(&srcNVMe->pciAddr,
|
2020-03-23 09:57:49 +00:00
|
|
|
srcNVMe->namespc,
|
2019-06-06 09:58:12 +00:00
|
|
|
srcNVMe->managed)))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
virNVMeDeviceUsedBySet(dev, drv_name, dom_name);
|
|
|
|
|
|
|
|
if (virNVMeDeviceListAdd(nvmeDevices, dev) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virHostdevPrepareOneNVMeDevice(virHostdevManagerPtr hostdev_mgr,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
virStorageSourcePtr src)
|
|
|
|
{
|
|
|
|
g_autoptr(virNVMeDeviceList) nvmeDevices = NULL;
|
|
|
|
g_autoptr(virPCIDeviceList) pciDevices = NULL;
|
|
|
|
const unsigned int pciFlags = 0;
|
|
|
|
virNVMeDevicePtr temp = NULL;
|
|
|
|
size_t i;
|
|
|
|
ssize_t lastGoodNVMeIdx = -1;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!(nvmeDevices = virNVMeDeviceListNew()))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virHostdevGetNVMeDeviceList(nvmeDevices, src, drv_name, dom_name) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virNVMeDeviceListCount(nvmeDevices) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virObjectLock(hostdev_mgr->activeNVMeHostdevs);
|
|
|
|
|
|
|
|
/* Firstly, let's check if all devices are free */
|
|
|
|
for (i = 0; i < virNVMeDeviceListCount(nvmeDevices); i++) {
|
|
|
|
const virNVMeDevice *dev = virNVMeDeviceListGet(nvmeDevices, i);
|
|
|
|
const virPCIDeviceAddress *addr = NULL;
|
|
|
|
g_autofree char *addrStr = NULL;
|
|
|
|
const char *actual_drvname = NULL;
|
|
|
|
const char *actual_domname = NULL;
|
|
|
|
|
|
|
|
temp = virNVMeDeviceListLookup(hostdev_mgr->activeNVMeHostdevs, dev);
|
|
|
|
|
|
|
|
/* Not on the list means not used */
|
|
|
|
if (!temp)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
virNVMeDeviceUsedByGet(temp, &actual_drvname, &actual_domname);
|
|
|
|
addr = virNVMeDeviceAddressGet(dev);
|
|
|
|
addrStr = virPCIDeviceAddressAsString(addr);
|
|
|
|
|
|
|
|
virReportError(VIR_ERR_OPERATION_INVALID,
|
|
|
|
_("NVMe device %s already in use by driver %s domain %s"),
|
|
|
|
NULLSTR(addrStr), actual_drvname, actual_domname);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(pciDevices = virNVMeDeviceListCreateDetachList(hostdev_mgr->activeNVMeHostdevs,
|
|
|
|
nvmeDevices)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
/* Let's check if all PCI devices are NVMe disks. */
|
|
|
|
for (i = 0; i < virPCIDeviceListCount(pciDevices); i++) {
|
|
|
|
virPCIDevicePtr pci = virPCIDeviceListGet(pciDevices, i);
|
|
|
|
g_autofree char *drvPath = NULL;
|
|
|
|
g_autofree char *drvName = NULL;
|
|
|
|
int stub = VIR_PCI_STUB_DRIVER_NONE;
|
|
|
|
|
|
|
|
if (virPCIDeviceGetDriverPathAndName(pci, &drvPath, &drvName) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (drvName)
|
|
|
|
stub = virPCIStubDriverTypeFromString(drvName);
|
|
|
|
|
|
|
|
if (stub == VIR_PCI_STUB_DRIVER_VFIO ||
|
|
|
|
STREQ_NULLABLE(drvName, "nvme"))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
VIR_WARN("Suspicious NVMe disk assignment. PCI device "
|
|
|
|
"%s is not an NVMe disk, it has %s driver",
|
|
|
|
virPCIDeviceGetName(pci), NULLSTR(drvName));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This looks like a good opportunity to merge inactive NVMe devices onto
|
|
|
|
* the active list. This, however, means that if something goes wrong we
|
|
|
|
* have to perform a rollback before returning. */
|
|
|
|
for (i = 0; i < virNVMeDeviceListCount(nvmeDevices); i++) {
|
|
|
|
temp = virNVMeDeviceListGet(nvmeDevices, i);
|
|
|
|
|
|
|
|
if (virNVMeDeviceListAdd(hostdev_mgr->activeNVMeHostdevs, temp) < 0)
|
|
|
|
goto rollback;
|
|
|
|
|
|
|
|
lastGoodNVMeIdx = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virHostdevPreparePCIDevicesImpl(hostdev_mgr,
|
|
|
|
drv_name, dom_name, NULL,
|
|
|
|
pciDevices, NULL, 0, pciFlags) < 0)
|
|
|
|
goto rollback;
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virObjectUnlock(hostdev_mgr->activeNVMeHostdevs);
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
rollback:
|
|
|
|
while (lastGoodNVMeIdx >= 0) {
|
|
|
|
temp = virNVMeDeviceListGet(nvmeDevices, lastGoodNVMeIdx);
|
|
|
|
|
|
|
|
virNVMeDeviceListDel(hostdev_mgr->activeNVMeHostdevs, temp);
|
|
|
|
|
|
|
|
lastGoodNVMeIdx--;
|
|
|
|
}
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virHostdevPrepareNVMeDevices(virHostdevManagerPtr hostdev_mgr,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
virDomainDiskDefPtr *disks,
|
|
|
|
size_t ndisks)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
ssize_t lastGoodDiskIdx = -1;
|
|
|
|
|
|
|
|
for (i = 0; i < ndisks; i++) {
|
|
|
|
if (virHostdevPrepareOneNVMeDevice(hostdev_mgr, drv_name,
|
|
|
|
dom_name, disks[i]->src) < 0)
|
|
|
|
goto rollback;
|
|
|
|
|
|
|
|
lastGoodDiskIdx = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
rollback:
|
|
|
|
while (lastGoodDiskIdx >= 0) {
|
|
|
|
if (virHostdevReAttachOneNVMeDevice(hostdev_mgr, drv_name, dom_name,
|
|
|
|
disks[lastGoodDiskIdx]->src) < 0) {
|
|
|
|
VIR_ERROR(_("Failed to reattach NVMe for disk target: %s"),
|
|
|
|
disks[lastGoodDiskIdx]->dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
lastGoodDiskIdx--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virHostdevReAttachOneNVMeDevice(virHostdevManagerPtr hostdev_mgr,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
virStorageSourcePtr src)
|
|
|
|
{
|
|
|
|
g_autoptr(virNVMeDeviceList) nvmeDevices = NULL;
|
|
|
|
g_autoptr(virPCIDeviceList) pciDevices = NULL;
|
|
|
|
size_t i;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!(nvmeDevices = virNVMeDeviceListNew()))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virHostdevGetNVMeDeviceList(nvmeDevices, src, drv_name, dom_name) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virNVMeDeviceListCount(nvmeDevices) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virObjectLock(hostdev_mgr->activeNVMeHostdevs);
|
|
|
|
|
|
|
|
if (!(pciDevices = virNVMeDeviceListCreateReAttachList(hostdev_mgr->activeNVMeHostdevs,
|
|
|
|
nvmeDevices)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
virHostdevReAttachPCIDevicesImpl(hostdev_mgr,
|
|
|
|
drv_name, dom_name, pciDevices,
|
|
|
|
NULL, 0, NULL);
|
|
|
|
|
|
|
|
for (i = 0; i < virNVMeDeviceListCount(nvmeDevices); i++) {
|
|
|
|
virNVMeDevicePtr temp = virNVMeDeviceListGet(nvmeDevices, i);
|
|
|
|
|
|
|
|
if (virNVMeDeviceListDel(hostdev_mgr->activeNVMeHostdevs, temp) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virObjectUnlock(hostdev_mgr->activeNVMeHostdevs);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virHostdevReAttachNVMeDevices(virHostdevManagerPtr hostdev_mgr,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
virDomainDiskDefPtr *disks,
|
|
|
|
size_t ndisks)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* Contrary to virHostdevPrepareNVMeDevices, this is a best
|
|
|
|
* effort approach. Just iterate over all disks and try to
|
|
|
|
* reattach them. Don't stop at the first failure. */
|
|
|
|
for (i = 0; i < ndisks; i++) {
|
|
|
|
if (virHostdevReAttachOneNVMeDevice(hostdev_mgr, drv_name,
|
|
|
|
dom_name, disks[i]->src) < 0) {
|
|
|
|
VIR_ERROR(_("Failed to reattach NVMe for disk target: %s"),
|
|
|
|
disks[i]->dst);
|
|
|
|
ret = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
virHostdevUpdateActiveNVMeDevices(virHostdevManagerPtr hostdev_mgr,
|
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name,
|
|
|
|
virDomainDiskDefPtr *disks,
|
|
|
|
size_t ndisks)
|
|
|
|
{
|
|
|
|
g_autoptr(virNVMeDeviceList) nvmeDevices = NULL;
|
|
|
|
g_autoptr(virPCIDeviceList) pciDevices = NULL;
|
|
|
|
virNVMeDevicePtr temp = NULL;
|
|
|
|
size_t i;
|
|
|
|
ssize_t lastGoodNVMeIdx = -1;
|
|
|
|
ssize_t lastGoodPCIIdx = -1;
|
|
|
|
int ret = -1;
|
|
|
|
|
|
|
|
if (!(nvmeDevices = virNVMeDeviceListNew()))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
for (i = 0; i < ndisks; i++) {
|
|
|
|
if (virHostdevGetNVMeDeviceList(nvmeDevices, disks[i]->src, drv_name, dom_name) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virNVMeDeviceListCount(nvmeDevices) == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
virObjectLock(hostdev_mgr->activeNVMeHostdevs);
|
|
|
|
virObjectLock(hostdev_mgr->activePCIHostdevs);
|
|
|
|
virObjectLock(hostdev_mgr->inactivePCIHostdevs);
|
|
|
|
|
|
|
|
if (!(pciDevices = virNVMeDeviceListCreateDetachList(hostdev_mgr->activeNVMeHostdevs,
|
|
|
|
nvmeDevices)))
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
for (i = 0; i < virNVMeDeviceListCount(nvmeDevices); i++) {
|
|
|
|
temp = virNVMeDeviceListGet(nvmeDevices, i);
|
|
|
|
|
|
|
|
if (virNVMeDeviceListAdd(hostdev_mgr->activeNVMeHostdevs, temp) < 0)
|
|
|
|
goto rollback;
|
|
|
|
|
|
|
|
lastGoodNVMeIdx = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < virPCIDeviceListCount(pciDevices); i++) {
|
|
|
|
virPCIDevicePtr actual = virPCIDeviceListGet(pciDevices, i);
|
|
|
|
|
|
|
|
/* We must restore some attributes that were lost on daemon restart. */
|
|
|
|
virPCIDeviceSetUnbindFromStub(actual, true);
|
|
|
|
if (virPCIDeviceSetUsedBy(actual, drv_name, dom_name) < 0)
|
|
|
|
goto rollback;
|
|
|
|
|
|
|
|
if (virPCIDeviceListAddCopy(hostdev_mgr->activePCIHostdevs, actual) < 0)
|
|
|
|
goto rollback;
|
|
|
|
|
|
|
|
lastGoodPCIIdx = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
|
|
|
cleanup:
|
|
|
|
virObjectUnlock(hostdev_mgr->inactivePCIHostdevs);
|
|
|
|
virObjectUnlock(hostdev_mgr->activePCIHostdevs);
|
|
|
|
virObjectUnlock(hostdev_mgr->activeNVMeHostdevs);
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
rollback:
|
|
|
|
while (lastGoodNVMeIdx >= 0) {
|
|
|
|
temp = virNVMeDeviceListGet(nvmeDevices, lastGoodNVMeIdx);
|
|
|
|
|
|
|
|
virNVMeDeviceListDel(hostdev_mgr->activeNVMeHostdevs, temp);
|
|
|
|
|
|
|
|
lastGoodNVMeIdx--;
|
|
|
|
}
|
|
|
|
while (lastGoodPCIIdx >= 0) {
|
|
|
|
virPCIDevicePtr actual = virPCIDeviceListGet(pciDevices, i);
|
|
|
|
|
|
|
|
virPCIDeviceListDel(hostdev_mgr->activePCIHostdevs, actual);
|
|
|
|
|
|
|
|
lastGoodPCIIdx--;
|
|
|
|
}
|
|
|
|
goto cleanup;
|
|
|
|
}
|