2009-03-02 16:18:11 +00:00
|
|
|
/*
|
2012-12-13 14:52:25 +00:00
|
|
|
* virpci.c: helper APIs for managing host PCI devices
|
|
|
|
*
|
nodedev: report maxCount for virtual_functions capability
A PCI device may have the capability to setup virtual functions (VFs)
but have them currently all disabled. Prior to this patch, if that was
the case the the node device XML for the device wouldn't report any
virtual_functions capability.
With this patch, if a file called "sriov_totalvfs" is found in the
device's sysfs directory, its contents will be interpreted as a
decimal number, and that value will be reported as "maxCount" in a
capability element of the device's XML, e.g.:
<capability type='virtual_functions' maxCount='7'/>
This will be reported regardless of whether or not any VFs are
currently enabled for the device.
NB: sriov_numvfs (the number of VFs currently active) is also
available in sysfs, but that value is implied by the number of items
in the list that is inside the capability element, so there is no
reason to explicitly provide it as an attribute.
sriov_totalvfs and sriov_numvfs are available in kernels at least as far
back as the 2.6.32 that is in RHEL6.7, but in the case that they
simply aren't there, libvirt will behave as it did prior to this patch
- no maxCount will be displayed, and the virtual_functions capability
will be absent from the device's XML when 0 VFs are enabled.
2015-11-23 19:19:13 +00:00
|
|
|
* Copyright (C) 2009-2015 Red Hat, Inc.
|
2009-03-02 16:18:11 +00:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2012-09-20 22:30:55 +00:00
|
|
|
* License along with this library. If not, see
|
2012-07-21 10:06:23 +00:00
|
|
|
* <http://www.gnu.org/licenses/>.
|
2009-03-02 16:18:11 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2012-12-13 14:52:25 +00:00
|
|
|
#include "virpci.h"
|
2017-07-31 04:21:45 +00:00
|
|
|
#include "virnetdev.h"
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2012-12-12 17:59:27 +00:00
|
|
|
#include "virlog.h"
|
2012-12-12 16:27:01 +00:00
|
|
|
#include "vircommand.h"
|
2012-12-13 18:21:53 +00:00
|
|
|
#include "virerror.h"
|
2011-07-19 18:32:58 +00:00
|
|
|
#include "virfile.h"
|
2014-01-24 15:47:20 +00:00
|
|
|
#include "virkmod.h"
|
2013-04-03 10:36:23 +00:00
|
|
|
#include "virstring.h"
|
2019-04-01 14:28:05 +00:00
|
|
|
#include "viralloc.h"
|
2021-10-20 08:30:32 +00:00
|
|
|
#include "virpcivpd.h"
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2014-02-28 12:16:17 +00:00
|
|
|
VIR_LOG_INIT("util.pci");
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
#define PCI_SYSFS "/sys/bus/pci/"
|
|
|
|
#define PCI_ID_LEN 10 /* "XXXX XXXX" */
|
|
|
|
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virPCIELinkSpeed,
|
|
|
|
VIR_PCIE_LINK_SPEED_LAST,
|
2019-01-20 16:30:15 +00:00
|
|
|
"", "2.5", "5", "8", "16",
|
|
|
|
);
|
2014-07-24 01:52:22 +00:00
|
|
|
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virPCIStubDriver,
|
|
|
|
VIR_PCI_STUB_DRIVER_LAST,
|
2015-10-23 09:54:07 +00:00
|
|
|
"none",
|
|
|
|
"pciback", /* XEN */
|
|
|
|
"vfio-pci", /* VFIO */
|
2019-01-20 16:30:15 +00:00
|
|
|
);
|
2015-10-23 09:54:07 +00:00
|
|
|
|
2019-03-16 18:20:32 +00:00
|
|
|
VIR_ENUM_IMPL(virPCIHeader,
|
|
|
|
VIR_PCI_HEADER_LAST,
|
2016-03-15 11:22:03 +00:00
|
|
|
"endpoint",
|
|
|
|
"pci-bridge",
|
|
|
|
"cardbus-bridge",
|
2019-01-20 16:30:15 +00:00
|
|
|
);
|
2016-03-15 11:22:03 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
struct _virPCIDevice {
|
2015-12-15 08:44:35 +00:00
|
|
|
virPCIDeviceAddress address;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2019-07-30 09:37:43 +00:00
|
|
|
char *name; /* domain:bus:slot.function */
|
2009-03-02 16:18:11 +00:00
|
|
|
char id[PCI_ID_LEN]; /* product vendor */
|
2011-06-22 20:52:32 +00:00
|
|
|
char *path;
|
2014-03-01 06:28:56 +00:00
|
|
|
|
|
|
|
/* The driver:domain which uses the device */
|
|
|
|
char *used_by_drvname;
|
|
|
|
char *used_by_domname;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2020-12-08 19:34:19 +00:00
|
|
|
/* The following 5 items are only valid after virPCIDeviceInit()
|
|
|
|
* has been called for the virPCIDevice object. This is *not* done
|
|
|
|
* in most cases (because it creates extra overhead, and parts of
|
|
|
|
* it can fail if libvirtd is running unprivileged)
|
|
|
|
*/
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int pcie_cap_pos;
|
|
|
|
unsigned int pci_pm_cap_pos;
|
2013-04-10 10:44:41 +00:00
|
|
|
bool has_flr;
|
|
|
|
bool has_pm_reset;
|
2020-12-08 19:34:19 +00:00
|
|
|
bool is_pcie;
|
|
|
|
/**/
|
|
|
|
|
2013-04-10 10:09:23 +00:00
|
|
|
bool managed;
|
2015-10-23 09:54:07 +00:00
|
|
|
|
|
|
|
virPCIStubDriver stubDriver;
|
2011-04-06 07:13:14 +00:00
|
|
|
|
|
|
|
/* used by reattach function */
|
2013-04-10 10:44:41 +00:00
|
|
|
bool unbind_from_stub;
|
|
|
|
bool remove_slot;
|
|
|
|
bool reprobe;
|
2009-03-02 16:18:11 +00:00
|
|
|
};
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
struct _virPCIDeviceList {
|
2013-01-16 11:49:54 +00:00
|
|
|
virObjectLockable parent;
|
|
|
|
|
2013-07-05 18:46:35 +00:00
|
|
|
size_t count;
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice **devs;
|
2009-10-27 17:30:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
#define VIR_FROM_THIS VIR_FROM_NONE
|
|
|
|
|
|
|
|
/* Specifications referenced in comments:
|
|
|
|
* PCI30 - PCI Local Bus Specification 3.0
|
|
|
|
* PCIe20 - PCI Express Base Specification 2.0
|
|
|
|
* BR12 - PCI-to-PCI Bridge Architecture Specification 1.2
|
|
|
|
* PM12 - PCI Bus Power Management Interface Specification 1.2
|
|
|
|
* ECN_AF - Advanced Capabilities for Conventional PCI ECN
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Type 0 config space header length; PCI30 Section 6.1 Configuration Space Organization */
|
|
|
|
#define PCI_CONF_LEN 0x100
|
|
|
|
#define PCI_CONF_HEADER_LEN 0x40
|
|
|
|
|
|
|
|
/* PCI30 6.2.1 */
|
|
|
|
#define PCI_HEADER_TYPE 0x0e /* Header type */
|
2010-03-09 18:22:22 +00:00
|
|
|
#define PCI_HEADER_TYPE_BRIDGE 0x1
|
|
|
|
#define PCI_HEADER_TYPE_MASK 0x7f
|
|
|
|
#define PCI_HEADER_TYPE_MULTI 0x80
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* PCI30 6.2.1 Device Identification */
|
|
|
|
#define PCI_CLASS_DEVICE 0x0a /* Device class */
|
|
|
|
|
|
|
|
/* Class Code for bridge; PCI30 D.7 Base Class 06h */
|
|
|
|
#define PCI_CLASS_BRIDGE_PCI 0x0604
|
|
|
|
|
|
|
|
/* PCI30 6.2.3 Device Status */
|
|
|
|
#define PCI_STATUS 0x06 /* 16 bits */
|
2010-03-09 18:22:22 +00:00
|
|
|
#define PCI_STATUS_CAP_LIST 0x10 /* Support Capability List */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* PCI30 6.7 Capabilities List */
|
|
|
|
#define PCI_CAPABILITY_LIST 0x34 /* Offset of first capability list entry */
|
2014-05-15 08:04:28 +00:00
|
|
|
#define PCI_CAP_FLAGS 2 /* Capability defined flags (16 bits) */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* PM12 3.2.1 Capability Identifier */
|
|
|
|
#define PCI_CAP_ID_PM 0x01 /* Power Management */
|
|
|
|
/* PCI30 H Capability IDs */
|
|
|
|
#define PCI_CAP_ID_EXP 0x10 /* PCI Express */
|
|
|
|
/* ECN_AF 6.x.1.1 Capability ID for AF */
|
|
|
|
#define PCI_CAP_ID_AF 0x13 /* Advanced Features */
|
|
|
|
|
|
|
|
/* PCIe20 7.8.3 Device Capabilities Register (Offset 04h) */
|
|
|
|
#define PCI_EXP_DEVCAP 0x4 /* Device capabilities */
|
2014-05-15 08:04:28 +00:00
|
|
|
#define PCI_EXP_DEVCAP_FLR (1<<28) /* Function Level Reset */
|
|
|
|
#define PCI_EXP_LNKCAP 0xc /* Link Capabilities */
|
2017-05-16 13:19:19 +00:00
|
|
|
#define PCI_EXP_LNKCAP_SPEED 0x0000f /* Maximum Link Speed */
|
2014-05-15 08:04:28 +00:00
|
|
|
#define PCI_EXP_LNKCAP_WIDTH 0x003f0 /* Maximum Link Width */
|
|
|
|
#define PCI_EXP_LNKSTA 0x12 /* Link Status */
|
|
|
|
#define PCI_EXP_LNKSTA_SPEED 0x000f /* Negotiated Link Speed */
|
|
|
|
#define PCI_EXP_LNKSTA_WIDTH 0x03f0 /* Negotiated Link Width */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* Header type 1 BR12 3.2 PCI-to-PCI Bridge Configuration Space Header Format */
|
|
|
|
#define PCI_PRIMARY_BUS 0x18 /* BR12 3.2.5.2 Primary bus number */
|
|
|
|
#define PCI_SECONDARY_BUS 0x19 /* BR12 3.2.5.3 Secondary bus number */
|
|
|
|
#define PCI_SUBORDINATE_BUS 0x1a /* BR12 3.2.5.4 Highest bus number behind the bridge */
|
|
|
|
#define PCI_BRIDGE_CONTROL 0x3e
|
|
|
|
/* BR12 3.2.5.18 Bridge Control Register */
|
2010-03-09 18:22:22 +00:00
|
|
|
#define PCI_BRIDGE_CTL_RESET 0x40 /* Secondary bus reset */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* PM12 3.2.4 Power Management Control/Status (Offset = 4) */
|
|
|
|
#define PCI_PM_CTRL 4 /* PM control and status register */
|
2010-03-09 18:22:22 +00:00
|
|
|
#define PCI_PM_CTRL_STATE_MASK 0x3 /* Current power state (D0 to D3) */
|
|
|
|
#define PCI_PM_CTRL_STATE_D0 0x0 /* D0 state */
|
|
|
|
#define PCI_PM_CTRL_STATE_D3hot 0x3 /* D3 state */
|
|
|
|
#define PCI_PM_CTRL_NO_SOFT_RESET 0x8 /* No reset for D3hot->D0 */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* ECN_AF 6.x.1 Advanced Features Capability Structure */
|
|
|
|
#define PCI_AF_CAP 0x3 /* Advanced features capabilities */
|
2010-03-09 18:22:22 +00:00
|
|
|
#define PCI_AF_CAP_FLR 0x2 /* Function Level Reset */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2009-12-22 17:21:15 +00:00
|
|
|
#define PCI_EXP_FLAGS 0x2
|
|
|
|
#define PCI_EXP_FLAGS_TYPE 0x00f0
|
|
|
|
#define PCI_EXP_TYPE_DOWNSTREAM 0x6
|
|
|
|
|
|
|
|
#define PCI_EXT_CAP_BASE 0x100
|
|
|
|
#define PCI_EXT_CAP_LIMIT 0x1000
|
|
|
|
#define PCI_EXT_CAP_ID_MASK 0x0000ffff
|
|
|
|
#define PCI_EXT_CAP_OFFSET_SHIFT 20
|
|
|
|
#define PCI_EXT_CAP_OFFSET_MASK 0x00000ffc
|
|
|
|
|
|
|
|
#define PCI_EXT_CAP_ID_ACS 0x000d
|
|
|
|
#define PCI_EXT_ACS_CTRL 0x06
|
|
|
|
|
|
|
|
#define PCI_EXT_CAP_ACS_SV 0x01
|
|
|
|
#define PCI_EXT_CAP_ACS_RR 0x04
|
|
|
|
#define PCI_EXT_CAP_ACS_CR 0x08
|
|
|
|
#define PCI_EXT_CAP_ACS_UF 0x10
|
2017-11-03 12:09:47 +00:00
|
|
|
#define PCI_EXT_CAP_ACS_ENABLED (PCI_EXT_CAP_ACS_SV | \
|
|
|
|
PCI_EXT_CAP_ACS_RR | \
|
|
|
|
PCI_EXT_CAP_ACS_CR | \
|
2009-12-22 17:21:15 +00:00
|
|
|
PCI_EXT_CAP_ACS_UF)
|
|
|
|
|
2014-05-15 08:04:28 +00:00
|
|
|
#define PCI_EXP_TYPE_ROOT_INT_EP 0x9 /* Root Complex Integrated Endpoint */
|
|
|
|
#define PCI_EXP_TYPE_ROOT_EC 0xa /* Root Complex Event Collector */
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
static virClass *virPCIDeviceListClass;
|
2013-01-16 11:49:54 +00:00
|
|
|
|
|
|
|
static void virPCIDeviceListDispose(void *obj);
|
|
|
|
|
|
|
|
static int virPCIOnceInit(void)
|
|
|
|
{
|
2018-04-17 15:42:33 +00:00
|
|
|
if (!VIR_CLASS_NEW(virPCIDeviceList, virClassForObjectLockable()))
|
2013-01-16 11:49:54 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-01-20 17:23:29 +00:00
|
|
|
VIR_ONCE_GLOBAL_INIT(virPCI);
|
2013-01-16 11:49:54 +00:00
|
|
|
|
2013-07-01 03:51:00 +00:00
|
|
|
|
2015-06-30 19:53:53 +00:00
|
|
|
static char *
|
|
|
|
virPCIDriverDir(const char *driver)
|
2013-07-01 03:51:00 +00:00
|
|
|
{
|
2020-10-10 21:50:11 +00:00
|
|
|
return g_strdup_printf(PCI_SYSFS "drivers/%s", driver);
|
2013-07-01 03:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-06-30 19:23:31 +00:00
|
|
|
static char *
|
|
|
|
virPCIFile(const char *device, const char *file)
|
2013-07-01 03:51:00 +00:00
|
|
|
{
|
2020-10-10 21:46:20 +00:00
|
|
|
return g_strdup_printf(PCI_SYSFS "devices/%s/%s", device, file);
|
2013-07-01 03:51:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* virPCIDeviceGetDriverPathAndName - put the path to the driver
|
|
|
|
* directory of the driver in use for this device in @path and the
|
|
|
|
* name of the driver in @name. Both could be NULL if it's not bound
|
|
|
|
* to any driver.
|
|
|
|
*
|
|
|
|
* Return 0 for success, -1 for error.
|
|
|
|
*/
|
2014-01-16 11:27:23 +00:00
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceGetDriverPathAndName(virPCIDevice *dev, char **path, char **name)
|
2013-07-01 03:51:00 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *drvlink = NULL;
|
2013-07-01 03:51:00 +00:00
|
|
|
|
|
|
|
*path = *name = NULL;
|
2020-10-10 21:46:20 +00:00
|
|
|
|
2013-07-01 03:51:00 +00:00
|
|
|
/* drvlink = "/sys/bus/pci/dddd:bb:ss.ff/driver" */
|
2020-10-10 21:46:20 +00:00
|
|
|
drvlink = virPCIFile(dev->name, "driver");
|
2013-07-01 03:51:00 +00:00
|
|
|
|
2014-01-15 10:44:53 +00:00
|
|
|
if (!virFileExists(drvlink)) {
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2013-07-01 03:51:00 +00:00
|
|
|
if (virFileIsLink(drvlink) != 1) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid device %s driver file %s is not a symlink"),
|
|
|
|
dev->name, drvlink);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
if (virFileResolveLink(drvlink, path) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to resolve device %s driver symlink %s"),
|
|
|
|
dev->name, drvlink);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
/* path = "/sys/bus/pci/drivers/${drivername}" */
|
|
|
|
|
2019-12-23 15:47:21 +00:00
|
|
|
*name = g_path_get_basename(*path);
|
2013-07-01 03:51:00 +00:00
|
|
|
/* name = "${drivername}" */
|
|
|
|
|
|
|
|
ret = 0;
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-07-01 03:51:00 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
VIR_FREE(*path);
|
|
|
|
VIR_FREE(*name);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceConfigOpenInternal(virPCIDevice *dev, bool readonly, bool fatal)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2019-08-13 13:11:14 +00:00
|
|
|
fd = open(dev->path, readonly ? O_RDONLY : O_RDWR);
|
2012-12-04 21:50:58 +00:00
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
if (fd < 0) {
|
2012-12-04 21:50:58 +00:00
|
|
|
if (fatal) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to open config space file '%s'"),
|
|
|
|
dev->path);
|
|
|
|
} else {
|
|
|
|
VIR_WARN("Failed to open config space file '%s': %s",
|
2020-02-26 17:57:34 +00:00
|
|
|
dev->path, g_strerror(errno));
|
2012-12-04 21:50:58 +00:00
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-12-04 21:50:58 +00:00
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
VIR_DEBUG("%s %s: opened %s", dev->id, dev->name, dev->path);
|
2012-12-04 21:50:58 +00:00
|
|
|
return fd;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 12:58:25 +00:00
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceConfigOpen(virPCIDevice *dev)
|
2019-08-13 12:58:25 +00:00
|
|
|
{
|
2019-08-13 13:17:44 +00:00
|
|
|
return virPCIDeviceConfigOpenInternal(dev, true, true);
|
2019-08-13 12:58:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-13 13:14:05 +00:00
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceConfigOpenTry(virPCIDevice *dev)
|
2019-08-13 13:14:05 +00:00
|
|
|
{
|
|
|
|
return virPCIDeviceConfigOpenInternal(dev, true, false);
|
|
|
|
}
|
|
|
|
|
2019-08-13 13:07:53 +00:00
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceConfigOpenWrite(virPCIDevice *dev)
|
2019-08-13 13:07:53 +00:00
|
|
|
{
|
2019-08-13 13:11:14 +00:00
|
|
|
return virPCIDeviceConfigOpenInternal(dev, false, true);
|
2019-08-13 13:07:53 +00:00
|
|
|
}
|
|
|
|
|
2010-07-23 19:03:29 +00:00
|
|
|
static void
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceConfigClose(virPCIDevice *dev, int cfgfd)
|
2010-07-23 19:03:29 +00:00
|
|
|
{
|
2012-12-04 21:50:58 +00:00
|
|
|
if (VIR_CLOSE(cfgfd) < 0) {
|
|
|
|
VIR_WARN("Failed to close config space file '%s': %s",
|
2020-02-26 17:57:34 +00:00
|
|
|
dev->path, g_strerror(errno));
|
2012-12-04 21:50:58 +00:00
|
|
|
}
|
2010-07-23 19:03:29 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 21:50:58 +00:00
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceRead(virPCIDevice *dev,
|
2013-01-14 22:11:44 +00:00
|
|
|
int cfgfd,
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int pos,
|
2013-01-14 22:11:44 +00:00
|
|
|
uint8_t *buf,
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int buflen)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
memset(buf, 0, buflen);
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
errno = 0;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2012-12-04 21:50:58 +00:00
|
|
|
if (lseek(cfgfd, pos, SEEK_SET) != pos ||
|
|
|
|
saferead(cfgfd, buf, buflen) != buflen) {
|
2020-12-07 01:26:01 +00:00
|
|
|
VIR_DEBUG("Failed to read %u bytes at %u from '%s' : %s",
|
|
|
|
buflen, pos, dev->path, g_strerror(errno));
|
2009-03-02 16:18:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virPCIDeviceReadN:
|
|
|
|
* @dev: virPCIDevice object (used only to log name of config file)
|
|
|
|
* @cfgfd: open file descriptor for device config file in sysfs
|
|
|
|
* @pos: byte offset in the file to read from
|
|
|
|
*
|
|
|
|
* read "N" (where "N" is "8", "16", or "32", and appears at the end
|
|
|
|
* of the function name) bytes from a PCI device's already-opened
|
|
|
|
* sysfs config file and return them as the return value from the
|
|
|
|
* function.
|
|
|
|
*
|
|
|
|
* Returns the value at @pos in the file, or 0 if there was an
|
2021-04-15 08:12:12 +00:00
|
|
|
* error. NB: since 0 could be a valid value, occurrence of an error
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
* must be determined by examining errno. errno is always reset to 0
|
|
|
|
* before the seek/read is attempted (see virPCIDeviceRead()), so if
|
|
|
|
* errno != 0 on return from one of these functions, then either the
|
|
|
|
* seek or the read operation failed for some reason. If errno == 0
|
|
|
|
* and the return value is 0, then the config file really does contain
|
|
|
|
* the value 0 at @pos.
|
|
|
|
*/
|
2009-03-02 16:18:11 +00:00
|
|
|
static uint8_t
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceRead8(virPCIDevice *dev, int cfgfd, unsigned int pos)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
uint8_t buf;
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceRead(dev, cfgfd, pos, &buf, sizeof(buf));
|
2009-03-02 16:18:11 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint16_t
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceRead16(virPCIDevice *dev, int cfgfd, unsigned int pos)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
uint8_t buf[2];
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceRead(dev, cfgfd, pos, &buf[0], sizeof(buf));
|
2009-03-02 16:18:11 +00:00
|
|
|
return (buf[0] << 0) | (buf[1] << 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
static uint32_t
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceRead32(virPCIDevice *dev, int cfgfd, unsigned int pos)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
uint8_t buf[4];
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceRead(dev, cfgfd, pos, &buf[0], sizeof(buf));
|
2009-03-02 16:18:11 +00:00
|
|
|
return (buf[0] << 0) | (buf[1] << 8) | (buf[2] << 16) | (buf[3] << 24);
|
|
|
|
}
|
|
|
|
|
2013-12-24 18:07:27 +00:00
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceReadClass(virPCIDevice *dev, uint16_t *device_class)
|
2013-12-24 18:07:27 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
|
|
|
g_autofree char *id_str = NULL;
|
2013-12-24 18:07:27 +00:00
|
|
|
unsigned int value;
|
|
|
|
|
2020-10-10 21:46:20 +00:00
|
|
|
path = virPCIFile(dev->name, "class");
|
2013-12-24 18:07:27 +00:00
|
|
|
|
|
|
|
/* class string is '0xNNNNNN\n' ... i.e. 9 bytes */
|
|
|
|
if (virFileReadAll(path, 9, &id_str) < 0)
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2013-12-24 18:07:27 +00:00
|
|
|
|
|
|
|
id_str[8] = '\0';
|
|
|
|
if (virStrToLong_ui(id_str, NULL, 16, &value) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unusual value in %s/devices/%s/class: %s"),
|
|
|
|
PCI_SYSFS, dev->name, id_str);
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2013-12-24 18:07:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*device_class = (value >> 8) & 0xFFFF;
|
2018-07-24 15:52:21 +00:00
|
|
|
return 0;
|
2013-12-24 18:07:27 +00:00
|
|
|
}
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceWrite(virPCIDevice *dev,
|
2013-01-14 22:11:44 +00:00
|
|
|
int cfgfd,
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int pos,
|
2013-01-14 22:11:44 +00:00
|
|
|
uint8_t *buf,
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int buflen)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2012-12-04 21:50:58 +00:00
|
|
|
if (lseek(cfgfd, pos, SEEK_SET) != pos ||
|
|
|
|
safewrite(cfgfd, buf, buflen) != buflen) {
|
2010-05-19 10:00:18 +00:00
|
|
|
VIR_WARN("Failed to write to '%s' : %s", dev->path,
|
2020-02-26 17:57:34 +00:00
|
|
|
g_strerror(errno));
|
2009-03-02 16:18:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceWrite16(virPCIDevice *dev, int cfgfd, unsigned int pos, uint16_t val)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
uint8_t buf[2] = { (val >> 0), (val >> 8) };
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceWrite(dev, cfgfd, pos, &buf[0], sizeof(buf));
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceWrite32(virPCIDevice *dev, int cfgfd, unsigned int pos, uint32_t val)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2012-12-05 10:58:39 +00:00
|
|
|
uint8_t buf[4] = { (val >> 0), (val >> 8), (val >> 16), (val >> 24) };
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceWrite(dev, cfgfd, pos, &buf[0], sizeof(buf));
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
typedef int (*virPCIDeviceIterPredicate)(virPCIDevice *, virPCIDevice *,
|
2013-11-19 23:00:32 +00:00
|
|
|
void *);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* Iterate over available PCI devices calling @predicate
|
|
|
|
* to compare each one to @dev.
|
|
|
|
* Return -1 on error since we don't want to assume it is
|
|
|
|
* safe to reset if there is an error.
|
|
|
|
*/
|
|
|
|
static int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceIterDevices(virPCIDeviceIterPredicate predicate,
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice *dev,
|
|
|
|
virPCIDevice **matched,
|
2013-01-14 22:11:44 +00:00
|
|
|
void *data)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2020-10-25 21:50:51 +00:00
|
|
|
g_autoptr(DIR) dir = NULL;
|
2009-03-02 16:18:11 +00:00
|
|
|
struct dirent *entry;
|
2009-03-03 11:25:35 +00:00
|
|
|
int ret = 0;
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
int rc;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
*matched = NULL;
|
|
|
|
|
|
|
|
VIR_DEBUG("%s %s: iterating over " PCI_SYSFS "devices", dev->id, dev->name);
|
|
|
|
|
2016-06-21 14:34:08 +00:00
|
|
|
if (virDirOpen(&dir, PCI_SYSFS "devices") < 0)
|
2009-03-02 16:18:11 +00:00
|
|
|
return -1;
|
|
|
|
|
2014-04-25 20:45:49 +00:00
|
|
|
while ((ret = virDirRead(dir, &entry, PCI_SYSFS "devices")) > 0) {
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) check = NULL;
|
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 12:54:28 +00:00
|
|
|
virPCIDeviceAddress devAddr;
|
2010-03-30 15:31:19 +00:00
|
|
|
char *tmp;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2010-03-30 15:31:19 +00:00
|
|
|
/* expected format: <domain>:<bus>:<slot>.<function> */
|
|
|
|
if (/* domain */
|
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 12:54:28 +00:00
|
|
|
virStrToLong_ui(entry->d_name, &tmp, 16, &devAddr.domain) < 0 || *tmp != ':' ||
|
2010-03-30 15:31:19 +00:00
|
|
|
/* bus */
|
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 12:54:28 +00:00
|
|
|
virStrToLong_ui(tmp + 1, &tmp, 16, &devAddr.bus) < 0 || *tmp != ':' ||
|
2010-03-30 15:31:19 +00:00
|
|
|
/* slot */
|
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 12:54:28 +00:00
|
|
|
virStrToLong_ui(tmp + 1, &tmp, 16, &devAddr.slot) < 0 || *tmp != '.' ||
|
2010-03-30 15:31:19 +00:00
|
|
|
/* function */
|
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 12:54:28 +00:00
|
|
|
virStrToLong_ui(tmp + 1, NULL, 16, &devAddr.function) < 0) {
|
2009-03-02 16:18:11 +00:00
|
|
|
VIR_WARN("Unusual entry in " PCI_SYSFS "devices: %s", entry->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 12:54:28 +00:00
|
|
|
check = virPCIDeviceNew(&devAddr);
|
2009-08-17 14:05:23 +00:00
|
|
|
if (!check) {
|
2009-03-03 11:25:35 +00:00
|
|
|
ret = -1;
|
|
|
|
break;
|
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
rc = predicate(dev, check, data);
|
|
|
|
if (rc < 0) {
|
|
|
|
/* the predicate returned an error, bail */
|
|
|
|
ret = -1;
|
|
|
|
break;
|
2014-09-03 19:39:21 +00:00
|
|
|
} else if (rc == 1) {
|
2009-08-17 14:05:23 +00:00
|
|
|
VIR_DEBUG("%s %s: iter matched on %s", dev->id, dev->name, check->name);
|
2019-10-16 11:43:52 +00:00
|
|
|
*matched = g_steal_pointer(&check);
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
ret = 1;
|
2009-03-02 16:18:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2009-03-03 11:25:35 +00:00
|
|
|
return ret;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virPCIDeviceFindCapabilityOffset:
|
|
|
|
* @dev: virPCIDevice object (used only to log name of config file)
|
|
|
|
* @cfgfd: open file descriptor for device config file in sysfs
|
|
|
|
* @capability: PCI_CAP_ID_* being requested
|
|
|
|
* @offset: used to return the offset of @capability in the file
|
|
|
|
*
|
|
|
|
* Find the offset of @capability within the PCI config file @cfgfd of
|
|
|
|
* the device @dev. if found, the offset is returned in @offset,
|
|
|
|
* otherwise @offset is set to 0.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure.
|
|
|
|
*/
|
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceFindCapabilityOffset(virPCIDevice *dev,
|
2013-04-15 10:29:23 +00:00
|
|
|
int cfgfd,
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
unsigned int capability,
|
|
|
|
unsigned int *offset)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
uint16_t status;
|
|
|
|
uint8_t pos;
|
|
|
|
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
*offset = 0; /* assume failure (*nothing* can be at offset 0) */
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
status = virPCIDeviceRead16(dev, cfgfd, PCI_STATUS);
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
if (errno != 0 || !(status & PCI_STATUS_CAP_LIST))
|
|
|
|
goto error;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
pos = virPCIDeviceRead8(dev, cfgfd, PCI_CAPABILITY_LIST);
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
if (errno != 0)
|
|
|
|
goto error;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* Zero indicates last capability, capabilities can't
|
|
|
|
* be in the config space header and 0xff is returned
|
|
|
|
* by the kernel if we don't have access to this region
|
|
|
|
*
|
|
|
|
* Note: we're not handling loops or extended
|
|
|
|
* capabilities here.
|
|
|
|
*/
|
|
|
|
while (pos >= PCI_CONF_HEADER_LEN && pos != 0xff) {
|
2013-01-14 22:11:44 +00:00
|
|
|
uint8_t capid = virPCIDeviceRead8(dev, cfgfd, pos);
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
if (errno != 0)
|
|
|
|
goto error;
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
if (capid == capability) {
|
|
|
|
VIR_DEBUG("%s %s: found cap 0x%.2x at 0x%.2x",
|
|
|
|
dev->id, dev->name, capability, pos);
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
*offset = pos;
|
|
|
|
return 0;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
pos = virPCIDeviceRead8(dev, cfgfd, pos + 1);
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
if (errno != 0)
|
|
|
|
goto error;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
error:
|
|
|
|
VIR_DEBUG("%s %s: failed to find cap 0x%.2x (%s)",
|
|
|
|
dev->id, dev->name, capability, g_strerror(errno));
|
2009-03-02 16:18:11 +00:00
|
|
|
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
/* reset errno in case the failure was due to insufficient
|
|
|
|
* privileges to read the entire PCI config file
|
|
|
|
*/
|
|
|
|
errno = 0;
|
|
|
|
|
|
|
|
return -1;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2009-12-22 17:21:15 +00:00
|
|
|
static unsigned int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceFindExtendedCapabilityOffset(virPCIDevice *dev,
|
2013-01-14 22:11:44 +00:00
|
|
|
int cfgfd,
|
2013-04-15 10:29:23 +00:00
|
|
|
unsigned int capability)
|
2009-12-22 17:21:15 +00:00
|
|
|
{
|
|
|
|
int ttl;
|
|
|
|
unsigned int pos;
|
|
|
|
uint32_t header;
|
|
|
|
|
|
|
|
/* minimum 8 bytes per capability */
|
|
|
|
ttl = (PCI_EXT_CAP_LIMIT - PCI_EXT_CAP_BASE) / 8;
|
|
|
|
pos = PCI_EXT_CAP_BASE;
|
|
|
|
|
|
|
|
while (ttl > 0 && pos >= PCI_EXT_CAP_BASE) {
|
2013-01-14 22:11:44 +00:00
|
|
|
header = virPCIDeviceRead32(dev, cfgfd, pos);
|
2009-12-22 17:21:15 +00:00
|
|
|
|
|
|
|
if ((header & PCI_EXT_CAP_ID_MASK) == capability)
|
|
|
|
return pos;
|
|
|
|
|
|
|
|
pos = (header >> PCI_EXT_CAP_OFFSET_SHIFT) & PCI_EXT_CAP_OFFSET_MASK;
|
|
|
|
ttl--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-07-23 19:03:29 +00:00
|
|
|
/* detects whether this device has FLR. Returns 0 if the device does
|
|
|
|
* not have FLR, 1 if it does, and -1 on error
|
|
|
|
*/
|
2020-12-06 21:05:03 +00:00
|
|
|
static bool
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceDetectFunctionLevelReset(virPCIDevice *dev, int cfgfd)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2009-07-31 14:35:53 +00:00
|
|
|
uint32_t caps;
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
unsigned int pos;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
2010-07-23 19:03:29 +00:00
|
|
|
int found;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* The PCIe Function Level Reset capability allows
|
|
|
|
* individual device functions to be reset without
|
|
|
|
* affecting any other functions on the device or
|
|
|
|
* any other devices on the bus. This is only common
|
|
|
|
* on SR-IOV NICs at the moment.
|
|
|
|
*/
|
|
|
|
if (dev->pcie_cap_pos) {
|
2013-01-14 22:11:44 +00:00
|
|
|
caps = virPCIDeviceRead32(dev, cfgfd, dev->pcie_cap_pos + PCI_EXP_DEVCAP);
|
2009-03-02 16:18:11 +00:00
|
|
|
if (caps & PCI_EXP_DEVCAP_FLR) {
|
|
|
|
VIR_DEBUG("%s %s: detected PCIe FLR capability", dev->id, dev->name);
|
2020-12-06 21:05:03 +00:00
|
|
|
return true;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The PCI AF Function Level Reset capability is
|
|
|
|
* the same thing, except for conventional PCI
|
|
|
|
* devices. This is not common yet.
|
|
|
|
*/
|
2020-12-08 19:34:19 +00:00
|
|
|
if (virPCIDeviceFindCapabilityOffset(dev, cfgfd, PCI_CAP_ID_AF, &pos) < 0)
|
|
|
|
goto error;
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
if (pos) {
|
2013-01-14 22:11:44 +00:00
|
|
|
caps = virPCIDeviceRead16(dev, cfgfd, pos + PCI_AF_CAP);
|
2009-03-02 16:18:11 +00:00
|
|
|
if (caps & PCI_AF_CAP_FLR) {
|
|
|
|
VIR_DEBUG("%s %s: detected PCI FLR capability", dev->id, dev->name);
|
2020-12-06 21:05:03 +00:00
|
|
|
return true;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-23 19:03:29 +00:00
|
|
|
/* there are some buggy devices that do support FLR, but forget to
|
|
|
|
* advertise that fact in their capabilities. However, FLR is *required*
|
|
|
|
* to be present for virtual functions (VFs), so if we see that this
|
|
|
|
* device is a VF, we just assume FLR works
|
|
|
|
*/
|
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
path = g_strdup_printf(PCI_SYSFS "devices/%s/physfn", dev->name);
|
2010-07-23 19:03:29 +00:00
|
|
|
|
|
|
|
found = virFileExists(path);
|
|
|
|
if (found) {
|
|
|
|
VIR_DEBUG("%s %s: buggy device didn't advertise FLR, but is a VF; forcing flr on",
|
|
|
|
dev->id, dev->name);
|
2020-12-06 21:05:03 +00:00
|
|
|
return true;
|
2010-07-23 19:03:29 +00:00
|
|
|
}
|
|
|
|
|
2020-12-08 19:34:19 +00:00
|
|
|
error:
|
2009-03-02 16:18:11 +00:00
|
|
|
VIR_DEBUG("%s %s: no FLR capability found", dev->id, dev->name);
|
2020-12-06 21:05:03 +00:00
|
|
|
return false;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Require the device has the PCI Power Management capability
|
|
|
|
* and that a D3hot->D0 transition will results in a full
|
|
|
|
* internal reset, not just a soft reset.
|
|
|
|
*/
|
2020-12-06 21:20:23 +00:00
|
|
|
static bool
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceDetectPowerManagementReset(virPCIDevice *dev, int cfgfd)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
if (dev->pci_pm_cap_pos) {
|
|
|
|
uint32_t ctl;
|
|
|
|
|
|
|
|
/* require the NO_SOFT_RESET bit is clear */
|
2013-01-14 22:11:44 +00:00
|
|
|
ctl = virPCIDeviceRead32(dev, cfgfd, dev->pci_pm_cap_pos + PCI_PM_CTRL);
|
2009-03-02 16:18:11 +00:00
|
|
|
if (!(ctl & PCI_PM_CTRL_NO_SOFT_RESET)) {
|
|
|
|
VIR_DEBUG("%s %s: detected PM reset capability", dev->id, dev->name);
|
2020-12-06 21:20:23 +00:00
|
|
|
return true;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("%s %s: no PM reset capability found", dev->id, dev->name);
|
|
|
|
|
2020-12-06 21:20:23 +00:00
|
|
|
return false;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2010-07-26 16:43:04 +00:00
|
|
|
/* Any active devices on the same domain/bus ? */
|
2009-03-02 16:18:11 +00:00
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceSharesBusWithActive(virPCIDevice *dev, virPCIDevice *check, void *data)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceList *inactiveDevs = data;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2010-07-23 09:25:24 +00:00
|
|
|
/* Different domain, different bus, or simply identical device */
|
2015-12-15 08:44:35 +00:00
|
|
|
if (dev->address.domain != check->address.domain ||
|
|
|
|
dev->address.bus != check->address.bus ||
|
|
|
|
(dev->address.slot == check->address.slot &&
|
|
|
|
dev->address.function == check->address.function))
|
2009-08-17 14:05:23 +00:00
|
|
|
return 0;
|
|
|
|
|
2010-07-26 16:43:04 +00:00
|
|
|
/* same bus, but inactive, i.e. about to be assigned to guest */
|
2021-01-04 12:54:41 +00:00
|
|
|
if (inactiveDevs && virPCIDeviceListFind(inactiveDevs, &check->address))
|
2009-08-17 14:05:22 +00:00
|
|
|
return 0;
|
2009-08-17 14:05:23 +00:00
|
|
|
|
2009-08-17 14:05:22 +00:00
|
|
|
return 1;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
static virPCIDevice *
|
|
|
|
virPCIDeviceBusContainsActiveDevices(virPCIDevice *dev,
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceList *inactiveDevs)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice *active = NULL;
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceIterDevices(virPCIDeviceSharesBusWithActive,
|
|
|
|
dev, &active, inactiveDevs) < 0)
|
2009-08-17 14:05:23 +00:00
|
|
|
return NULL;
|
|
|
|
return active;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Is @check the parent of @dev ? */
|
2009-03-02 16:18:11 +00:00
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceIsParent(virPCIDevice *dev, virPCIDevice *check, void *data)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
uint16_t device_class;
|
|
|
|
uint8_t header_type, secondary, subordinate;
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice **best = data;
|
2012-12-04 21:50:58 +00:00
|
|
|
int ret = 0;
|
|
|
|
int fd;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2015-12-15 08:44:35 +00:00
|
|
|
if (dev->address.domain != check->address.domain)
|
2009-03-02 16:18:11 +00:00
|
|
|
return 0;
|
|
|
|
|
2019-08-13 13:14:05 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpenTry(check)) < 0)
|
2012-12-04 21:50:58 +00:00
|
|
|
return 0;
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
/* Is it a bridge? */
|
2013-12-24 18:07:27 +00:00
|
|
|
ret = virPCIDeviceReadClass(check, &device_class);
|
|
|
|
if (ret < 0 || device_class != PCI_CLASS_BRIDGE_PCI)
|
2012-12-04 21:50:58 +00:00
|
|
|
goto cleanup;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* Is it a plane? */
|
2013-01-14 22:11:44 +00:00
|
|
|
header_type = virPCIDeviceRead8(check, fd, PCI_HEADER_TYPE);
|
2009-03-02 16:18:11 +00:00
|
|
|
if ((header_type & PCI_HEADER_TYPE_MASK) != PCI_HEADER_TYPE_BRIDGE)
|
2012-12-04 21:50:58 +00:00
|
|
|
goto cleanup;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
secondary = virPCIDeviceRead8(check, fd, PCI_SECONDARY_BUS);
|
|
|
|
subordinate = virPCIDeviceRead8(check, fd, PCI_SUBORDINATE_BUS);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2010-01-19 13:17:20 +00:00
|
|
|
VIR_DEBUG("%s %s: found parent device %s", dev->id, dev->name, check->name);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
/* if the secondary bus exactly equals the device's bus, then we found
|
|
|
|
* the direct parent. No further work is necessary
|
|
|
|
*/
|
2015-12-15 08:44:35 +00:00
|
|
|
if (dev->address.bus == secondary) {
|
2012-12-04 21:50:58 +00:00
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
|
2013-09-10 18:10:55 +00:00
|
|
|
/* otherwise, SRIOV allows VFs to be on different buses than their PFs.
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
* In this case, what we need to do is look for the "best" match; i.e.
|
|
|
|
* the most restrictive match that still satisfies all of the conditions.
|
|
|
|
*/
|
2015-12-15 08:44:35 +00:00
|
|
|
if (dev->address.bus > secondary && dev->address.bus <= subordinate) {
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
if (*best == NULL) {
|
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 12:54:28 +00:00
|
|
|
*best = virPCIDeviceNew(&check->address);
|
2012-12-04 21:50:58 +00:00
|
|
|
if (*best == NULL) {
|
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
} else {
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
/* OK, we had already recorded a previous "best" match for the
|
|
|
|
* parent. See if the current device is more restrictive than the
|
|
|
|
* best, and if so, make it the new best
|
|
|
|
*/
|
2012-12-04 21:50:58 +00:00
|
|
|
int bestfd;
|
|
|
|
uint8_t best_secondary;
|
|
|
|
|
2019-08-13 13:14:05 +00:00
|
|
|
if ((bestfd = virPCIDeviceConfigOpenTry(*best)) < 0)
|
2012-12-04 21:50:58 +00:00
|
|
|
goto cleanup;
|
2013-01-14 22:11:44 +00:00
|
|
|
best_secondary = virPCIDeviceRead8(*best, bestfd, PCI_SECONDARY_BUS);
|
|
|
|
virPCIDeviceConfigClose(*best, bestfd);
|
2012-12-04 21:50:58 +00:00
|
|
|
|
|
|
|
if (secondary > best_secondary) {
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFree(*best);
|
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 12:54:28 +00:00
|
|
|
*best = virPCIDeviceNew(&check->address);
|
2012-12-04 21:50:58 +00:00
|
|
|
if (*best == NULL) {
|
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceConfigClose(check, fd);
|
2012-12-04 21:50:58 +00:00
|
|
|
return ret;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceGetParent(virPCIDevice *dev, virPCIDevice **parent)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice *best = NULL;
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
*parent = NULL;
|
2013-01-14 22:11:44 +00:00
|
|
|
ret = virPCIDeviceIterDevices(virPCIDeviceIsParent, dev, parent, &best);
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
if (ret == 1)
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFree(best);
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
else if (ret == 0)
|
|
|
|
*parent = best;
|
|
|
|
return ret;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Secondary Bus Reset is our sledgehammer - it resets all
|
|
|
|
* devices behind a bus.
|
|
|
|
*/
|
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceTrySecondaryBusReset(virPCIDevice *dev,
|
2013-01-14 22:11:44 +00:00
|
|
|
int cfgfd,
|
|
|
|
virPCIDeviceList *inactiveDevs)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) parent = NULL;
|
|
|
|
g_autoptr(virPCIDevice) conflict = NULL;
|
2009-03-02 16:18:11 +00:00
|
|
|
uint8_t config_space[PCI_CONF_LEN];
|
|
|
|
uint16_t ctl;
|
|
|
|
int ret = -1;
|
2012-12-04 21:50:58 +00:00
|
|
|
int parentfd;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2012-08-31 13:44:21 +00:00
|
|
|
/* Refuse to do a secondary bus reset if there are other
|
|
|
|
* devices/functions behind the bus are used by the host
|
|
|
|
* or other guests.
|
2009-03-02 16:18:11 +00:00
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
if ((conflict = virPCIDeviceBusContainsActiveDevices(dev, inactiveDevs))) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-08-17 14:05:23 +00:00
|
|
|
_("Active %s devices on bus with %s, not doing bus reset"),
|
|
|
|
conflict->name, dev->name);
|
2009-03-02 16:18:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Find the parent bus */
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceGetParent(dev, &parent) < 0)
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
return -1;
|
2009-03-02 16:18:11 +00:00
|
|
|
if (!parent) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-08-14 07:31:11 +00:00
|
|
|
_("Failed to find parent device for %s"),
|
|
|
|
dev->name);
|
2009-03-02 16:18:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2019-08-13 13:07:53 +00:00
|
|
|
if ((parentfd = virPCIDeviceConfigOpenWrite(parent)) < 0)
|
2012-12-04 21:50:58 +00:00
|
|
|
goto out;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("%s %s: doing a secondary bus reset", dev->id, dev->name);
|
|
|
|
|
|
|
|
/* Save and restore the device's config space; we only do this
|
|
|
|
* for the supplied device since we refuse to do a reset if there
|
|
|
|
* are multiple devices/functions
|
|
|
|
*/
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceRead(dev, cfgfd, 0, config_space, PCI_CONF_LEN) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-02-19 15:04:35 +00:00
|
|
|
_("Failed to read PCI config space for %s"),
|
2009-08-14 07:31:11 +00:00
|
|
|
dev->name);
|
2009-03-02 16:18:11 +00:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the control register, set the reset flag, wait 200ms,
|
|
|
|
* unset the reset flag and wait 200ms.
|
|
|
|
*/
|
2019-08-15 09:44:06 +00:00
|
|
|
ctl = virPCIDeviceRead16(dev, parentfd, PCI_BRIDGE_CONTROL);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceWrite16(parent, parentfd, PCI_BRIDGE_CONTROL,
|
|
|
|
ctl | PCI_BRIDGE_CTL_RESET);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2019-10-02 17:01:11 +00:00
|
|
|
g_usleep(200 * 1000); /* sleep 200ms */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceWrite16(parent, parentfd, PCI_BRIDGE_CONTROL, ctl);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2019-10-02 17:01:11 +00:00
|
|
|
g_usleep(200 * 1000); /* sleep 200ms */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceWrite(dev, cfgfd, 0, config_space, PCI_CONF_LEN) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-08-14 07:31:11 +00:00
|
|
|
_("Failed to restore PCI config space for %s"),
|
|
|
|
dev->name);
|
|
|
|
goto out;
|
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
ret = 0;
|
2012-12-04 21:50:58 +00:00
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
out:
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceConfigClose(parent, parentfd);
|
2009-03-02 16:18:11 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Power management reset attempts to reset a device using a
|
|
|
|
* D-state transition from D3hot to D0. Note, in detect_pm_reset()
|
|
|
|
* above we require the device supports a full internal reset.
|
|
|
|
*/
|
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceTryPowerManagementReset(virPCIDevice *dev, int cfgfd)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
|
|
|
uint8_t config_space[PCI_CONF_LEN];
|
|
|
|
uint32_t ctl;
|
|
|
|
|
|
|
|
if (!dev->pci_pm_cap_pos)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Save and restore the device's config space. */
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceRead(dev, cfgfd, 0, &config_space[0], PCI_CONF_LEN) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-02-19 15:04:35 +00:00
|
|
|
_("Failed to read PCI config space for %s"),
|
2009-08-14 07:31:11 +00:00
|
|
|
dev->name);
|
2009-03-02 16:18:11 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
VIR_DEBUG("%s %s: doing a power management reset", dev->id, dev->name);
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
ctl = virPCIDeviceRead32(dev, cfgfd, dev->pci_pm_cap_pos + PCI_PM_CTRL);
|
2009-03-02 16:18:11 +00:00
|
|
|
ctl &= ~PCI_PM_CTRL_STATE_MASK;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceWrite32(dev, cfgfd, dev->pci_pm_cap_pos + PCI_PM_CTRL,
|
|
|
|
ctl | PCI_PM_CTRL_STATE_D3hot);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2019-10-02 17:01:11 +00:00
|
|
|
g_usleep(10 * 1000); /* sleep 10ms */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceWrite32(dev, cfgfd, dev->pci_pm_cap_pos + PCI_PM_CTRL,
|
|
|
|
ctl | PCI_PM_CTRL_STATE_D0);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2019-10-02 17:01:11 +00:00
|
|
|
g_usleep(10 * 1000); /* sleep 10ms */
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceWrite(dev, cfgfd, 0, &config_space[0], PCI_CONF_LEN) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-08-14 07:31:11 +00:00
|
|
|
_("Failed to restore PCI config space for %s"),
|
|
|
|
dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-08 19:34:19 +00:00
|
|
|
/**
|
|
|
|
* virPCIDeviceInit:
|
|
|
|
* @dev: virPCIDevice object needing its PCI capabilities info initialized
|
|
|
|
* @cfgfd: open file descriptor for device config file in sysfs
|
|
|
|
*
|
|
|
|
* Initialize the PCI capabilities attributes of a virPCIDevice object
|
|
|
|
* (i.e. pcie_cap_pos, pci_pm_cap_pos, has_flr, has_pm_reset, and
|
|
|
|
* is_pcie). This is done by walking the info in the (already-opened)
|
|
|
|
* device PCI config file in sysfs. This function can be called
|
|
|
|
* regardless of whether a process has sufficient privilege to read
|
|
|
|
* the entire file (unprivileged processes can only read the 1st 64
|
|
|
|
* bytes, while the Express Capabilities are all located beyond that
|
|
|
|
* boundary).
|
|
|
|
*
|
|
|
|
* In the case that we are unable to read a capability
|
|
|
|
* directly, we will attempt to infer its value by other means. In
|
|
|
|
* particular, we can determine that a device is (almost surely) PCIe
|
|
|
|
* by checking that the length of the config file is != 256 (since all
|
|
|
|
* conventional PCI config files are 256 bytes), and we know that any
|
|
|
|
* device that is an SR-IOV VF will have FLR available (since that is
|
|
|
|
* required by the SR-IOV spec.)
|
|
|
|
*
|
|
|
|
* Always returns success (0) (for now)
|
|
|
|
*/
|
2009-03-02 16:18:11 +00:00
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceInit(virPCIDevice *dev, int cfgfd)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2020-12-08 19:34:19 +00:00
|
|
|
dev->is_pcie = false;
|
|
|
|
if (virPCIDeviceFindCapabilityOffset(dev, cfgfd, PCI_CAP_ID_EXP, &dev->pcie_cap_pos) < 0) {
|
|
|
|
/* an unprivileged process is unable to read *all* of a
|
|
|
|
* device's PCI config (it can only read the first 64
|
|
|
|
* bytes, which isn't enough for see the Express
|
|
|
|
* Capabilities data). If virPCIDeviceFindCapabilityOffset
|
|
|
|
* returns failure (and not just a pcie_cap_pos == 0,
|
|
|
|
* which is *success* at determining the device is *not*
|
|
|
|
* PCIe) we make an educated guess based on the length of
|
|
|
|
* the device's config file - if it is 256 bytes, then it
|
|
|
|
* is definitely a legacy PCI device. If it's larger than
|
|
|
|
* that, then it is *probably PCIe (although it could be
|
|
|
|
* PCI-x, but those are extremely rare). If the config
|
|
|
|
* file can't be found (in which case the "length" will be
|
|
|
|
* -1), then we blindly assume the most likely outcome -
|
|
|
|
* PCIe.
|
|
|
|
*/
|
|
|
|
off_t configLen = virFileLength(virPCIDeviceGetConfigPath(dev), -1);
|
|
|
|
|
|
|
|
if (configLen != 256)
|
|
|
|
dev->is_pcie = true;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
dev->is_pcie = (dev->pcie_cap_pos != 0);
|
|
|
|
}
|
|
|
|
|
util: change call sequence for virPCIDeviceFindCapabilityOffset()
Previously there was no way to differentiate between this function 1)
encountering an error while reading the pci config, and 2) determining
that the device in question is a conventional PCI device, and so has
no Express Capabilities.
The difference between these two conditions is important, because an
unprivileged libvirtd will be unable to read all of the pci config (it
can only read the first 64 bytes, and will get ENOENT when it tries to
seek past that limit) even though the device is in fact a PCIe device.
This patch changes virPCIDeviceFindCapabilityOffset() to put the
determined offset into an argument of the function (rather than
sending it back as the return value), and to return the standard "0 on
success, -1 on failure". Failure is determined by checking the value
of errno after each attemptd read of the config file (which can only
work reliably if errno is reset to 0 before each read, and after
virPCIDeviceFindCapabilityOffset() has finished examining it).
(NB: if the config file is read successfully, but no Express
Capabilities are found, then the function returns success, but the
returned offset will be 0 (which is an impossible offset for Express
Capabilities, and so easily recognizeable).
An upcoming patch will take advantage of the change made here.
Signed-off-by: Laine Stump <laine@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2020-12-07 20:46:54 +00:00
|
|
|
virPCIDeviceFindCapabilityOffset(dev, cfgfd, PCI_CAP_ID_PM, &dev->pci_pm_cap_pos);
|
2020-12-06 21:05:03 +00:00
|
|
|
dev->has_flr = virPCIDeviceDetectFunctionLevelReset(dev, cfgfd);
|
2020-12-06 21:20:23 +00:00
|
|
|
dev->has_pm_reset = virPCIDeviceDetectPowerManagementReset(dev, cfgfd);
|
2012-12-04 21:50:58 +00:00
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceReset(virPCIDevice *dev,
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceList *activeDevs,
|
|
|
|
virPCIDeviceList *inactiveDevs)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *drvPath = NULL;
|
|
|
|
g_autofree char *drvName = NULL;
|
2009-03-02 16:18:11 +00:00
|
|
|
int ret = -1;
|
2013-06-29 02:35:21 +00:00
|
|
|
int fd = -1;
|
2017-01-23 13:37:10 +00:00
|
|
|
int hdrType = -1;
|
|
|
|
|
|
|
|
if (virPCIGetHeaderType(dev, &hdrType) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (hdrType != VIR_PCI_HEADER_ENDPOINT) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid attempt to reset PCI device %s. "
|
|
|
|
"Only PCI endpoint devices can be reset"),
|
|
|
|
dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2021-01-04 12:54:41 +00:00
|
|
|
if (activeDevs && virPCIDeviceListFind(activeDevs, &dev->address)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-08-17 14:05:23 +00:00
|
|
|
_("Not resetting active device %s"), dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-06-29 02:35:21 +00:00
|
|
|
/* If the device is currently bound to vfio-pci, ignore all
|
|
|
|
* requests to reset it, since the vfio-pci driver will always
|
|
|
|
* reset it whenever appropriate, so doing it ourselves would just
|
|
|
|
* be redundant.
|
|
|
|
*/
|
|
|
|
if (virPCIDeviceGetDriverPathAndName(dev, &drvPath, &drvName) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2015-10-23 09:54:07 +00:00
|
|
|
if (virPCIStubDriverTypeFromString(drvName) == VIR_PCI_STUB_DRIVER_VFIO) {
|
2013-06-29 02:35:21 +00:00
|
|
|
VIR_DEBUG("Device %s is bound to vfio-pci - skip reset",
|
|
|
|
dev->name);
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
VIR_DEBUG("Resetting device %s", dev->name);
|
|
|
|
|
2019-08-13 13:07:53 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpenWrite(dev)) < 0)
|
2013-06-29 02:35:21 +00:00
|
|
|
goto cleanup;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceInit(dev, fd) < 0)
|
2012-12-04 21:50:58 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
/* KVM will perform FLR when starting and stopping
|
|
|
|
* a guest, so there is no need for us to do it here.
|
|
|
|
*/
|
2012-12-04 21:50:58 +00:00
|
|
|
if (dev->has_flr) {
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2009-08-14 07:31:11 +00:00
|
|
|
/* If the device supports PCI power management reset,
|
|
|
|
* that's the next best thing because it only resets
|
|
|
|
* the function, not the whole device.
|
|
|
|
*/
|
|
|
|
if (dev->has_pm_reset)
|
2013-01-14 22:11:44 +00:00
|
|
|
ret = virPCIDeviceTryPowerManagementReset(dev, fd);
|
2009-08-14 07:31:11 +00:00
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
/* Bus reset is not an option with the root bus */
|
2015-12-15 08:44:35 +00:00
|
|
|
if (ret < 0 && dev->address.bus != 0)
|
2013-01-14 22:11:44 +00:00
|
|
|
ret = virPCIDeviceTrySecondaryBusReset(dev, fd, inactiveDevs);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2009-08-14 07:31:11 +00:00
|
|
|
if (ret < 0) {
|
|
|
|
virErrorPtr err = virGetLastError();
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-08-14 07:31:11 +00:00
|
|
|
_("Unable to reset PCI device %s: %s"),
|
|
|
|
dev->name,
|
2013-06-29 02:35:21 +00:00
|
|
|
err ? err->message :
|
|
|
|
_("no FLR, PM reset or bus reset available"));
|
2009-08-14 07:31:11 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceConfigClose(dev, fd);
|
2009-03-02 16:18:11 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2009-04-03 12:38:52 +00:00
|
|
|
|
2013-01-10 07:51:43 +00:00
|
|
|
static int
|
2015-10-23 09:54:07 +00:00
|
|
|
virPCIProbeStubDriver(virPCIStubDriver driver)
|
2009-04-03 12:38:52 +00:00
|
|
|
{
|
2015-10-23 09:54:07 +00:00
|
|
|
const char *drvname = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *drvpath = NULL;
|
2020-10-11 01:47:16 +00:00
|
|
|
g_autofree char *errbuf = NULL;
|
2009-04-03 12:38:52 +00:00
|
|
|
|
2015-10-23 09:54:07 +00:00
|
|
|
if (driver == VIR_PCI_STUB_DRIVER_NONE ||
|
|
|
|
!(drvname = virPCIStubDriverTypeToString(driver))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
"%s",
|
|
|
|
_("Attempting to use unknown stub driver"));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-10-10 21:50:11 +00:00
|
|
|
drvpath = virPCIDriverDir(drvname);
|
|
|
|
|
2020-10-11 01:47:16 +00:00
|
|
|
/* driver previously loaded, return */
|
2020-10-10 21:50:11 +00:00
|
|
|
if (virFileExists(drvpath))
|
2013-01-10 07:51:43 +00:00
|
|
|
return 0;
|
2009-04-03 12:38:52 +00:00
|
|
|
|
2020-10-11 01:47:16 +00:00
|
|
|
if ((errbuf = virKModLoad(drvname))) {
|
|
|
|
VIR_WARN("failed to load driver %s: %s", drvname, errbuf);
|
|
|
|
goto cleanup;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 01:47:16 +00:00
|
|
|
/* driver loaded after probing */
|
|
|
|
if (virFileExists(drvpath))
|
|
|
|
return 0;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2020-06-16 11:12:09 +00:00
|
|
|
/* If we know failure was because of admin config, let's report that;
|
2014-01-24 15:47:20 +00:00
|
|
|
* otherwise, report a more generic failure message
|
|
|
|
*/
|
2020-06-16 11:12:09 +00:00
|
|
|
if (virKModIsProhibited(drvname)) {
|
2014-01-24 15:47:20 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to load PCI stub module %s: "
|
|
|
|
"administratively prohibited"),
|
2015-10-23 09:54:07 +00:00
|
|
|
drvname);
|
2014-01-24 15:47:20 +00:00
|
|
|
} else {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to load PCI stub module %s"),
|
2015-10-23 09:54:07 +00:00
|
|
|
drvname);
|
2014-01-24 15:47:20 +00:00
|
|
|
}
|
|
|
|
|
2013-01-10 07:51:43 +00:00
|
|
|
return -1;
|
2009-04-03 12:38:52 +00:00
|
|
|
}
|
|
|
|
|
2014-01-16 11:27:23 +00:00
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceUnbind(virPCIDevice *dev)
|
2014-01-16 11:27:23 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
|
|
|
g_autofree char *drvpath = NULL;
|
|
|
|
g_autofree char *driver = NULL;
|
2014-01-16 11:27:23 +00:00
|
|
|
|
|
|
|
if (virPCIDeviceGetDriverPathAndName(dev, &drvpath, &driver) < 0)
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2014-01-16 11:27:23 +00:00
|
|
|
|
2018-07-24 15:52:21 +00:00
|
|
|
if (!driver)
|
2014-01-16 11:27:23 +00:00
|
|
|
/* The device is not bound to any driver */
|
2018-07-24 15:52:21 +00:00
|
|
|
return 0;
|
2014-01-16 11:27:23 +00:00
|
|
|
|
2020-10-10 21:46:20 +00:00
|
|
|
path = virPCIFile(dev->name, "driver/unbind");
|
2014-01-16 11:27:23 +00:00
|
|
|
|
|
|
|
if (virFileExists(path)) {
|
|
|
|
if (virFileWriteStr(path, dev->name, 0) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to unbind PCI device '%s' from %s"),
|
|
|
|
dev->name, driver);
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2014-01-16 11:27:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 15:52:21 +00:00
|
|
|
return 0;
|
2014-01-16 11:27:23 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 17:58:15 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* virPCIDeviceRebind:
|
|
|
|
* @dev: virPCIDevice object describing the device to rebind
|
|
|
|
*
|
|
|
|
* unbind a device from its driver, then immediately rebind it.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure
|
|
|
|
*/
|
2021-03-11 07:16:13 +00:00
|
|
|
int virPCIDeviceRebind(virPCIDevice *dev)
|
2017-03-07 17:58:15 +00:00
|
|
|
{
|
|
|
|
if (virPCIDeviceUnbind(dev) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (virFileWriteStr(PCI_SYSFS "drivers_probe", dev->name, 0) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to trigger a probe for PCI device '%s'"),
|
|
|
|
dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-08-02 03:36:45 +00:00
|
|
|
/*
|
|
|
|
* Bind a PCI device to a driver using driver_override sysfs interface.
|
|
|
|
* E.g.
|
|
|
|
*
|
|
|
|
* echo driver-name > /sys/bus/pci/devices/0000:03:00.0/driver_override
|
|
|
|
* echo 0000:03:00.0 > /sys/bus/pci/devices/0000:03:00.0/driver/unbind
|
|
|
|
* echo 0000:03:00.0 > /sys/bus/pci/drivers_probe
|
|
|
|
*
|
|
|
|
* An empty driverName will cause the device to be bound to its
|
|
|
|
* preferred driver.
|
|
|
|
*/
|
2011-04-06 07:13:10 +00:00
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceBindWithDriverOverride(virPCIDevice *dev,
|
2016-08-02 03:36:45 +00:00
|
|
|
const char *driverName)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
2016-08-02 03:36:45 +00:00
|
|
|
|
2020-10-10 21:46:20 +00:00
|
|
|
path = virPCIFile(dev->name, "driver_override");
|
2016-08-02 03:36:45 +00:00
|
|
|
|
|
|
|
if (virFileWriteStr(path, driverName, 0) < 0) {
|
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to add driver '%s' to driver_override "
|
|
|
|
" interface of PCI device '%s'"),
|
|
|
|
driverName, dev->name);
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2016-08-02 03:36:45 +00:00
|
|
|
}
|
|
|
|
|
2017-03-07 17:58:15 +00:00
|
|
|
if (virPCIDeviceRebind(dev) < 0)
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2016-08-02 03:36:45 +00:00
|
|
|
|
2018-07-24 15:52:21 +00:00
|
|
|
return 0;
|
2016-08-02 03:36:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceUnbindFromStub(virPCIDevice *dev)
|
2016-08-02 03:36:45 +00:00
|
|
|
{
|
|
|
|
if (!dev->unbind_from_stub) {
|
|
|
|
VIR_DEBUG("Unbind from stub skipped for PCI device %s", dev->name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return virPCIDeviceBindWithDriverOverride(dev, "\n");
|
|
|
|
}
|
2009-04-03 12:38:52 +00:00
|
|
|
|
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceBindToStub(virPCIDevice *dev)
|
2016-08-02 03:36:45 +00:00
|
|
|
{
|
|
|
|
const char *stubDriverName;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *stubDriverPath = NULL;
|
|
|
|
g_autofree char *driverLink = NULL;
|
2016-08-02 03:36:45 +00:00
|
|
|
|
|
|
|
/* Check the device is configured to use one of the known stub drivers */
|
|
|
|
if (dev->stubDriver == VIR_PCI_STUB_DRIVER_NONE) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("No stub driver configured for PCI device %s"),
|
|
|
|
dev->name);
|
|
|
|
return -1;
|
|
|
|
} else if (!(stubDriverName = virPCIStubDriverTypeToString(dev->stubDriver))) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unknown stub driver configured for PCI device %s"),
|
|
|
|
dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2020-10-10 21:50:11 +00:00
|
|
|
stubDriverPath = virPCIDriverDir(stubDriverName);
|
2020-10-10 21:46:20 +00:00
|
|
|
driverLink = virPCIFile(dev->name, "driver");
|
|
|
|
|
2016-08-02 03:36:45 +00:00
|
|
|
if (virFileExists(driverLink)) {
|
|
|
|
if (virFileLinkPointsTo(driverLink, stubDriverPath)) {
|
|
|
|
/* The device is already bound to the correct driver */
|
|
|
|
VIR_DEBUG("Device %s is already bound to %s",
|
|
|
|
dev->name, stubDriverName);
|
2018-07-24 15:52:21 +00:00
|
|
|
return 0;
|
2016-08-02 03:36:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (virPCIDeviceBindWithDriverOverride(dev, stubDriverName) < 0)
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2016-08-02 03:36:45 +00:00
|
|
|
|
|
|
|
dev->unbind_from_stub = true;
|
2018-07-24 15:52:21 +00:00
|
|
|
return 0;
|
2016-08-02 03:36:45 +00:00
|
|
|
}
|
|
|
|
|
2013-06-04 19:54:45 +00:00
|
|
|
/* virPCIDeviceDetach:
|
|
|
|
*
|
|
|
|
* Detach this device from the host driver, attach it to the stub
|
|
|
|
* driver (previously set with virPCIDeviceSetStubDriver(), and add *a
|
|
|
|
* copy* of the object to the inactiveDevs list (if provided). This
|
|
|
|
* function will *never* consume dev, so the caller should free it.
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on failure (will fail if the device is
|
|
|
|
* already in the activeDevs list, but will be a NOP if the device is
|
|
|
|
* already bound to the stub).
|
|
|
|
*
|
|
|
|
* GENERAL NOTE: activeDevs should be a list of all PCI devices
|
|
|
|
* currently in use by a domain. inactiveDevs is a list of all PCI
|
|
|
|
* devices that libvirt has detached from the host driver + attached
|
|
|
|
* to the stub driver, but hasn't yet assigned to a domain. Any device
|
|
|
|
* that is still attached to its host driver should not be on either
|
|
|
|
* list.
|
|
|
|
*/
|
2009-03-02 16:18:11 +00:00
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceDetach(virPCIDevice *dev,
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceList *activeDevs,
|
2013-05-30 18:14:46 +00:00
|
|
|
virPCIDeviceList *inactiveDevs)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2014-01-24 15:47:20 +00:00
|
|
|
if (virPCIProbeStubDriver(dev->stubDriver) < 0)
|
2009-04-03 12:38:52 +00:00
|
|
|
return -1;
|
|
|
|
|
2021-01-04 12:54:41 +00:00
|
|
|
if (activeDevs && virPCIDeviceListFind(activeDevs, &dev->address)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-06-14 21:12:35 +00:00
|
|
|
_("Not detaching active device %s"), dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-10-22 16:15:11 +00:00
|
|
|
if (virPCIDeviceBindToStub(dev) < 0)
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
return -1;
|
|
|
|
|
2013-06-04 19:54:45 +00:00
|
|
|
/* Add *a copy of* the dev into list inactiveDevs, if
|
|
|
|
* it's not already there.
|
|
|
|
*/
|
2021-01-04 12:54:41 +00:00
|
|
|
if (inactiveDevs && !virPCIDeviceListFind(inactiveDevs, &dev->address)) {
|
2016-01-07 16:50:15 +00:00
|
|
|
VIR_DEBUG("Adding PCI device %s to inactive list", dev->name);
|
|
|
|
if (virPCIDeviceListAddCopy(inactiveDevs, dev) < 0)
|
|
|
|
return -1;
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
virhostdev: remove virHostdevReattachPCIDevice
virHostdevReattachPCIDevice() is a static that simply does
a wait loop with virPCIDeviceWaitForCleanup() before
calling virPCIDeviceReattach().
This loop traces back to commit d1e5676c0d, aiming to
solve a race condition between Libvirt returning the
device back to the host and QEMU trying to access it in
the meantime, which resulted in QEMU exiting on error
and killing the guest. This happens because device_del
is asynchronous, returning OK even if the guest didn't
release the device. Commit 01abc8a1b8 moved this code
to qemu_hostdev.c, 82e8dd4cf8 added the pci-stub conditional
for the loop, 899b261127 moved the code to virhostdev.c
where it stood until now.
The intent of this wait loop is still valid: device_del
is still not bullet proof into preventing the conditions
that commit d1e5676c0d aimed to fix, especially when considering
all the architectures we must support. However, this loop
is executed only in virHostdevReattachPCIDevice(), leaving
every other virPCIDeviceReattach() call prone to that error.
Let's move the wait loop code to virPCIDeviceReattach(). This
will:
- make every reattach call safe from this race condition
with the pci-stub;
- allow for a bit of code cleanup (virHostdevReattachPCIDevice()
can be erased, and virHostdevReAttachPCIDevices() can use
virPCIDeviceReattach() directly);
- make it easier to understand the overall reattach mechanisms in
Libvirt, without the risk of a newcomer wondering why reattach
is done slightly different in some instances.
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
2019-07-23 17:35:40 +00:00
|
|
|
/*
|
|
|
|
* Pre-condition: inactivePCIHostdevs & activePCIHostdevs
|
|
|
|
* are locked
|
|
|
|
*/
|
2009-03-02 16:18:11 +00:00
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceReattach(virPCIDevice *dev,
|
|
|
|
virPCIDeviceList *activeDevs,
|
|
|
|
virPCIDeviceList *inactiveDevs)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2021-01-04 12:54:41 +00:00
|
|
|
if (activeDevs && virPCIDeviceListFind(activeDevs, &dev->address)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-06-14 21:12:35 +00:00
|
|
|
_("Not reattaching active device %s"), dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
pci: autolearn name of stub driver, remove from arglist
virPCIDeviceReattach and virPCIDeviceUnbindFromStub (called by
virPCIDeviceReattach) had previously required the name of the stub
driver as input. This is unnecessary, because the name of the driver
the device is currently bound to can be found by looking at the link:
/sys/bus/pci/dddd:bb:ss.ff/driver
Instead of requiring that the name of the expected stub driver name
and only unbinding if that one name is matched, we no longer take a
driver name in the arglist for either of these
functions. virPCIDeviceUnbindFromStub just compares the name of the
currently bound driver to a list of "well known" stubs (right now
contains "pci-stub" and "vfio-pci" for qemu, and "pciback" for xen),
and only performs the unbind if it's one of those devices.
This allows virsh nodedevice-reattach to work properly across a
libvirtd restart, and fixes a couple of cases where we were
erroneously still hard-coding "pci-stub" as the drive name.
For some unknown reason, virPCIDeviceReattach had been calling
modprobe on the stub driver prior to unbinding the device. This was
problematic because we no longer know the name of the stub driver in
that function. However, it is pointless to probe for the stub driver
at that time anyway - because the device is bound to the stub driver,
we are guaranteed that it is already loaded, and so that call to
modprobe has been removed.
2013-05-01 18:44:10 +00:00
|
|
|
if (virPCIDeviceUnbindFromStub(dev) < 0)
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Steal the dev from list inactiveDevs */
|
2016-01-07 16:50:15 +00:00
|
|
|
if (inactiveDevs) {
|
|
|
|
VIR_DEBUG("Removing PCI device %s from inactive list", dev->name);
|
2021-01-04 12:54:43 +00:00
|
|
|
virPCIDeviceListDel(inactiveDevs, &dev->address);
|
2016-01-07 16:50:15 +00:00
|
|
|
}
|
qemu: Introduce inactive PCI device list
pciTrySecondaryBusReset checks if there is active device on the
same bus, however, qemu driver doesn't maintain an effective
list for the inactive devices, and it passes meaningless argument
for parameter "inactiveDevs". e.g. (qemuPrepareHostdevPCIDevices)
if (!(pcidevs = qemuGetPciHostDeviceList(hostdevs, nhostdevs)))
return -1;
..skipped...
if (pciResetDevice(dev, driver->activePciHostdevs, pcidevs) < 0)
goto reattachdevs;
NB, the "pcidevs" used above are extracted from domain def, and
thus one won't be able to attach a device of which bus has other
device even detached from host (nodedev-detach). To see more
details of the problem:
RHBZ: https://bugzilla.redhat.com/show_bug.cgi?id=773667
This patch is to resolve the problem by introducing an inactive
PCI device list (just like qemu_driver->activePciHostdevs), and
the whole logic is:
* Add the device to inactive list during nodedev-dettach
* Remove the device from inactive list during nodedev-reattach
* Remove the device from inactive list during attach-device
(for non-managed device)
* Add the device to inactive list after detach-device, only
if the device is not managed
With the above, we have a sufficient inactive PCI device list, and thus
we can use it for pciResetDevice. e.g.(qemuPrepareHostdevPCIDevices)
if (pciResetDevice(dev, driver->activePciHostdevs,
driver->inactivePciHostdevs) < 0)
goto reattachdevs;
2012-01-17 20:02:05 +00:00
|
|
|
|
|
|
|
return 0;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceReadID(virPCIDevice *dev, const char *id_name)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *path = NULL;
|
2020-10-11 03:05:49 +00:00
|
|
|
g_autofree char *id_str = NULL;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2020-10-10 21:46:20 +00:00
|
|
|
path = virPCIFile(dev->name, id_name);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
/* ID string is '0xNNNN\n' ... i.e. 7 bytes */
|
2018-07-24 15:52:21 +00:00
|
|
|
if (virFileReadAll(path, 7, &id_str) < 0)
|
2009-03-02 16:18:11 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Check for 0x suffix */
|
2020-10-11 03:05:49 +00:00
|
|
|
if (id_str[0] != '0' || id_str[1] != 'x')
|
2009-03-02 16:18:11 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Chop off the newline; we know the string is 7 bytes */
|
|
|
|
id_str[6] = '\0';
|
|
|
|
|
2020-10-11 03:05:49 +00:00
|
|
|
return g_steal_pointer(&id_str);
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2018-09-13 14:36:25 +00:00
|
|
|
bool
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceAddressIsValid(virPCIDeviceAddress *addr,
|
2018-09-13 14:36:25 +00:00
|
|
|
bool report)
|
|
|
|
{
|
|
|
|
if (addr->bus > 0xFF) {
|
|
|
|
if (report)
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("Invalid PCI address bus='0x%x', "
|
|
|
|
"must be <= 0xFF"),
|
|
|
|
addr->bus);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (addr->slot > 0x1F) {
|
|
|
|
if (report)
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("Invalid PCI address slot='0x%x', "
|
|
|
|
"must be <= 0x1F"),
|
|
|
|
addr->slot);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (addr->function > 7) {
|
|
|
|
if (report)
|
|
|
|
virReportError(VIR_ERR_XML_ERROR,
|
|
|
|
_("Invalid PCI address function=0x%x, "
|
|
|
|
"must be <= 7"),
|
|
|
|
addr->function);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (virPCIDeviceAddressIsEmpty(addr)) {
|
|
|
|
if (report)
|
|
|
|
virReportError(VIR_ERR_XML_ERROR, "%s",
|
|
|
|
_("Invalid PCI address 0000:00:00, at least "
|
|
|
|
"one of domain, bus, or slot must be > 0"));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
virPCIDeviceAddressIsEmpty(const virPCIDeviceAddress *addr)
|
|
|
|
{
|
|
|
|
return !(addr->domain || addr->bus || addr->slot);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2019-06-04 08:43:40 +00:00
|
|
|
virPCIDeviceAddressEqual(const virPCIDeviceAddress *addr1,
|
|
|
|
const virPCIDeviceAddress *addr2)
|
2018-09-13 14:36:25 +00:00
|
|
|
{
|
|
|
|
if (addr1->domain == addr2->domain &&
|
|
|
|
addr1->bus == addr2->bus &&
|
|
|
|
addr1->slot == addr2->slot &&
|
|
|
|
addr1->function == addr2->function) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-06-07 14:32:37 +00:00
|
|
|
/**
|
|
|
|
* virPCIDeviceAddressCopy:
|
|
|
|
* @dst: where to store address
|
|
|
|
* @src: source address to copy
|
|
|
|
*
|
|
|
|
* Creates a deep copy of given @src address and stores it into
|
|
|
|
* @dst which has to be pre-allocated by caller.
|
|
|
|
*/
|
2021-03-11 07:16:13 +00:00
|
|
|
void virPCIDeviceAddressCopy(virPCIDeviceAddress *dst,
|
2019-06-07 14:32:37 +00:00
|
|
|
const virPCIDeviceAddress *src)
|
|
|
|
{
|
|
|
|
memcpy(dst, src, sizeof(*src));
|
|
|
|
}
|
|
|
|
|
2018-09-04 16:32:38 +00:00
|
|
|
char *
|
2019-06-06 15:02:11 +00:00
|
|
|
virPCIDeviceAddressAsString(const virPCIDeviceAddress *addr)
|
2018-09-04 16:32:38 +00:00
|
|
|
{
|
2021-10-22 08:56:01 +00:00
|
|
|
return g_strdup_printf(VIR_PCI_DEVICE_ADDRESS_FMT, addr->domain,
|
|
|
|
addr->bus, addr->slot, addr->function);
|
2018-09-04 16:32:38 +00:00
|
|
|
}
|
|
|
|
|
2021-01-04 12:54:29 +00:00
|
|
|
bool
|
|
|
|
virPCIDeviceExists(const virPCIDeviceAddress *addr)
|
|
|
|
{
|
|
|
|
g_autofree char *devName = virPCIDeviceAddressAsString(addr);
|
|
|
|
g_autofree char *devPath = g_strdup_printf(PCI_SYSFS "devices/%s/config",
|
|
|
|
devName);
|
|
|
|
|
|
|
|
return virFileExists(devPath);
|
|
|
|
}
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice *
|
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 12:54:28 +00:00
|
|
|
virPCIDeviceNew(const virPCIDeviceAddress *address)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) dev = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *vendor = NULL;
|
|
|
|
g_autofree char *product = NULL;
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2020-10-05 17:13:12 +00:00
|
|
|
dev = g_new0(virPCIDevice, 1);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 12:54:28 +00:00
|
|
|
virPCIDeviceAddressCopy(&dev->address, address);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2021-01-04 12:54:25 +00:00
|
|
|
dev->name = virPCIDeviceAddressAsString(&dev->address);
|
2019-07-30 09:37:43 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
dev->path = g_strdup_printf(PCI_SYSFS "devices/%s/config", dev->name);
|
2009-03-02 16:18:11 +00:00
|
|
|
|
2013-09-13 13:32:43 +00:00
|
|
|
if (!virFileExists(dev->path)) {
|
2010-04-30 15:44:19 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Device %s not found: could not access %s"),
|
|
|
|
dev->name, dev->path);
|
2019-07-30 09:31:53 +00:00
|
|
|
return NULL;
|
2010-04-30 15:44:19 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
vendor = virPCIDeviceReadID(dev, "vendor");
|
|
|
|
product = virPCIDeviceReadID(dev, "device");
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
if (!vendor || !product) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-03-02 16:18:11 +00:00
|
|
|
_("Failed to read product/vendor ID for %s"),
|
|
|
|
dev->name);
|
2019-07-30 09:31:53 +00:00
|
|
|
return NULL;
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* strings contain '0x' prefix */
|
2019-11-13 13:53:42 +00:00
|
|
|
if (g_snprintf(dev->id, sizeof(dev->id), "%s %s", &vendor[2],
|
|
|
|
&product[2]) >= sizeof(dev->id)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2011-06-22 20:52:32 +00:00
|
|
|
_("dev->id buffer overflow: %s %s"),
|
|
|
|
&vendor[2], &product[2]);
|
2019-07-30 09:31:53 +00:00
|
|
|
return NULL;
|
2011-06-22 20:52:32 +00:00
|
|
|
}
|
2009-03-02 16:18:11 +00:00
|
|
|
|
|
|
|
VIR_DEBUG("%s %s: initialized", dev->id, dev->name);
|
|
|
|
|
2019-10-16 11:35:54 +00:00
|
|
|
return g_steal_pointer(&dev);
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
|
|
|
|
2013-05-31 15:06:32 +00:00
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice *
|
|
|
|
virPCIDeviceCopy(virPCIDevice *dev)
|
2013-05-31 15:06:32 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice *copy;
|
2013-05-31 15:06:32 +00:00
|
|
|
|
2020-10-05 17:13:12 +00:00
|
|
|
copy = g_new0(virPCIDevice, 1);
|
2013-05-31 15:06:32 +00:00
|
|
|
|
|
|
|
/* shallow copy to take care of most attributes */
|
|
|
|
*copy = *dev;
|
2015-10-23 09:54:07 +00:00
|
|
|
copy->path = NULL;
|
2014-03-01 06:28:56 +00:00
|
|
|
copy->used_by_drvname = copy->used_by_domname = NULL;
|
2019-10-20 11:49:46 +00:00
|
|
|
copy->name = g_strdup(dev->name);
|
|
|
|
copy->path = g_strdup(dev->path);
|
|
|
|
copy->used_by_drvname = g_strdup(dev->used_by_drvname);
|
|
|
|
copy->used_by_domname = g_strdup(dev->used_by_domname);
|
2013-05-31 15:06:32 +00:00
|
|
|
return copy;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-03-02 16:18:11 +00:00
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceFree(virPCIDevice *dev)
|
2009-03-02 16:18:11 +00:00
|
|
|
{
|
2009-08-17 14:05:23 +00:00
|
|
|
if (!dev)
|
|
|
|
return;
|
2009-03-02 16:18:11 +00:00
|
|
|
VIR_DEBUG("%s %s: freeing", dev->id, dev->name);
|
2021-02-03 19:32:34 +00:00
|
|
|
g_free(dev->name);
|
|
|
|
g_free(dev->path);
|
|
|
|
g_free(dev->used_by_drvname);
|
|
|
|
g_free(dev->used_by_domname);
|
|
|
|
g_free(dev);
|
2009-03-02 16:18:11 +00:00
|
|
|
}
|
2009-08-17 14:05:23 +00:00
|
|
|
|
2015-01-14 11:02:40 +00:00
|
|
|
/**
|
|
|
|
* virPCIDeviceGetAddress:
|
|
|
|
* @dev: device to get address from
|
|
|
|
*
|
|
|
|
* Take a PCI device on input and return its PCI address. The
|
2015-12-15 08:44:35 +00:00
|
|
|
* returned object is owned by the device and must not be freed.
|
2015-01-14 11:02:40 +00:00
|
|
|
*
|
2015-12-15 08:44:35 +00:00
|
|
|
* Returns: a pointer to the address, which can never be NULL.
|
2015-01-14 11:02:40 +00:00
|
|
|
*/
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceAddress *
|
|
|
|
virPCIDeviceGetAddress(virPCIDevice *dev)
|
2015-01-14 11:02:40 +00:00
|
|
|
{
|
2015-12-15 08:44:35 +00:00
|
|
|
return &(dev->address);
|
2015-01-14 11:02:40 +00:00
|
|
|
}
|
|
|
|
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
const char *
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceGetName(virPCIDevice *dev)
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
{
|
|
|
|
return dev->name;
|
|
|
|
}
|
|
|
|
|
2016-11-19 19:30:03 +00:00
|
|
|
/**
|
|
|
|
* virPCIDeviceGetConfigPath:
|
|
|
|
*
|
|
|
|
* Returns a pointer to a string containing the path of @dev's PCI
|
|
|
|
* config file.
|
|
|
|
*/
|
|
|
|
const char *
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceGetConfigPath(virPCIDevice *dev)
|
2016-11-19 19:30:03 +00:00
|
|
|
{
|
|
|
|
return dev->path;
|
|
|
|
}
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
void virPCIDeviceSetManaged(virPCIDevice *dev, bool managed)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
2013-04-10 10:09:23 +00:00
|
|
|
dev->managed = managed;
|
2009-08-17 14:05:23 +00:00
|
|
|
}
|
|
|
|
|
2016-01-28 08:21:43 +00:00
|
|
|
bool
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceGetManaged(virPCIDevice *dev)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
|
|
|
return dev->managed;
|
|
|
|
}
|
|
|
|
|
2015-10-23 09:54:07 +00:00
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceSetStubDriver(virPCIDevice *dev, virPCIStubDriver driver)
|
2013-04-23 18:50:15 +00:00
|
|
|
{
|
2015-10-23 09:54:07 +00:00
|
|
|
dev->stubDriver = driver;
|
2013-04-23 18:50:15 +00:00
|
|
|
}
|
|
|
|
|
2015-10-23 09:54:07 +00:00
|
|
|
virPCIStubDriver
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceGetStubDriver(virPCIDevice *dev)
|
2013-04-23 18:50:15 +00:00
|
|
|
{
|
|
|
|
return dev->stubDriver;
|
|
|
|
}
|
|
|
|
|
2016-01-28 08:21:43 +00:00
|
|
|
bool
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceGetUnbindFromStub(virPCIDevice *dev)
|
2011-10-20 09:50:10 +00:00
|
|
|
{
|
|
|
|
return dev->unbind_from_stub;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceSetUnbindFromStub(virPCIDevice *dev, bool unbind)
|
2011-10-20 09:50:10 +00:00
|
|
|
{
|
2013-04-10 10:44:41 +00:00
|
|
|
dev->unbind_from_stub = unbind;
|
2011-10-20 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-28 08:21:43 +00:00
|
|
|
bool
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceGetRemoveSlot(virPCIDevice *dev)
|
2011-10-20 09:50:10 +00:00
|
|
|
{
|
|
|
|
return dev->remove_slot;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceSetRemoveSlot(virPCIDevice *dev, bool remove_slot)
|
2011-10-20 09:50:10 +00:00
|
|
|
{
|
2013-04-10 10:44:41 +00:00
|
|
|
dev->remove_slot = remove_slot;
|
2011-10-20 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
2016-01-28 08:21:43 +00:00
|
|
|
bool
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceGetReprobe(virPCIDevice *dev)
|
2011-10-20 09:50:10 +00:00
|
|
|
{
|
|
|
|
return dev->reprobe;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceSetReprobe(virPCIDevice *dev, bool reprobe)
|
2011-10-20 09:50:10 +00:00
|
|
|
{
|
2013-04-10 10:44:41 +00:00
|
|
|
dev->reprobe = reprobe;
|
2011-10-20 09:50:10 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 06:28:56 +00:00
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceSetUsedBy(virPCIDevice *dev,
|
2014-03-01 06:28:56 +00:00
|
|
|
const char *drv_name,
|
|
|
|
const char *dom_name)
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
{
|
2014-03-01 06:28:56 +00:00
|
|
|
VIR_FREE(dev->used_by_drvname);
|
|
|
|
VIR_FREE(dev->used_by_domname);
|
2019-10-20 11:49:46 +00:00
|
|
|
dev->used_by_drvname = g_strdup(drv_name);
|
|
|
|
dev->used_by_domname = g_strdup(dom_name);
|
2014-03-01 06:28:56 +00:00
|
|
|
|
|
|
|
return 0;
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
}
|
|
|
|
|
2014-03-01 06:28:56 +00:00
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceGetUsedBy(virPCIDevice *dev,
|
2014-03-01 06:28:56 +00:00
|
|
|
const char **drv_name,
|
|
|
|
const char **dom_name)
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
{
|
2014-03-01 06:28:56 +00:00
|
|
|
*drv_name = dev->used_by_drvname;
|
|
|
|
*dom_name = dev->used_by_domname;
|
qemu: Do not reattach PCI device used by other domain when shutdown
When failing on starting a domain, it tries to reattach all the PCI
devices defined in the domain conf, regardless of whether the devices
are still used by other domain. This will cause the devices to be deleted
from the list qemu_driver->activePciHostdevs, thus the devices will be
thought as usable even if it's not true. And following commands
nodedev-{reattach,reset} will be successful.
How to reproduce:
1) Define two domains with same PCI device defined in the confs.
2) # virsh start domain1
3) # virsh start domain2
4) # virsh nodedev-reattach $pci_device
You will see the device will be reattached to host successfully.
As pciDeviceReattach just check if the device is still used by
other domain via checking if the device is in list driver->activePciHostdevs,
however, the device is deleted from the list by step 2).
This patch is to prohibit the bug by:
1) Prohibit a domain starting or device attachment right at
preparation period (qemuPrepareHostdevPCIDevices) if the
device is in list driver->activePciHostdevs, which means
it's used by other domain.
2) Introduces a new field for struct _pciDevice, (const char *used_by),
it will be set as the domain name at preparation period,
(qemuPrepareHostdevPCIDevices). Thus we can prohibit deleting
the device from driver->activePciHostdevs if it's still used by
other domain when stopping the domain process.
* src/pci.h (define two internal functions, pciDeviceSetUsedBy and
pciDevceGetUsedBy)
* src/pci.c (new field "const char *used_by" for struct _pciDevice,
implementations for the two new functions)
* src/libvirt_private.syms (Add the two new internal functions)
* src/qemu_hostdev.h (Modify the definition of functions
qemuPrepareHostdevPCIDevices, and qemuDomainReAttachHostdevDevices)
* src/qemu_hostdev.c (Prohibit preparation and don't delete the
device from activePciHostdevs list if it's still used by other domain)
* src/qemu_hotplug.c (Update function usage, as the definitions are
changed)
Signed-off-by: Eric Blake <eblake@redhat.com>
2011-10-13 04:05:04 +00:00
|
|
|
}
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceList *
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceListNew(void)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceList *list;
|
2009-08-17 14:05:23 +00:00
|
|
|
|
2013-01-16 11:49:54 +00:00
|
|
|
if (virPCIInitialize() < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (!(list = virObjectLockableNew(virPCIDeviceListClass)))
|
2009-08-17 14:05:23 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2013-01-16 11:49:54 +00:00
|
|
|
static void
|
|
|
|
virPCIDeviceListDispose(void *obj)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceList *list = obj;
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2009-08-17 14:05:23 +00:00
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++) {
|
2022-01-28 17:42:45 +00:00
|
|
|
g_clear_pointer(&list->devs[i], virPCIDeviceFree);
|
2009-08-17 14:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list->count = 0;
|
2021-02-03 20:15:23 +00:00
|
|
|
g_free(list->devs);
|
2009-08-17 14:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceListAdd(virPCIDeviceList *list,
|
|
|
|
virPCIDevice *dev)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
2021-01-04 12:54:41 +00:00
|
|
|
if (virPCIDeviceListFind(list, &dev->address)) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-08-17 14:05:23 +00:00
|
|
|
_("Device %s is already in use"), dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2021-08-03 12:14:20 +00:00
|
|
|
VIR_APPEND_ELEMENT(list->devs, list->count, dev);
|
|
|
|
|
|
|
|
return 0;
|
2009-08-17 14:05:23 +00:00
|
|
|
}
|
|
|
|
|
2013-06-25 01:27:52 +00:00
|
|
|
|
|
|
|
/* virPCIDeviceListAddCopy - add a *copy* of the device to this list */
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceListAddCopy(virPCIDeviceList *list, virPCIDevice *dev)
|
2013-06-25 01:27:52 +00:00
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) copy = virPCIDeviceCopy(dev);
|
2013-06-25 01:27:52 +00:00
|
|
|
|
|
|
|
if (!copy)
|
|
|
|
return -1;
|
2018-07-24 15:52:22 +00:00
|
|
|
if (virPCIDeviceListAdd(list, copy) < 0)
|
2013-06-25 01:27:52 +00:00
|
|
|
return -1;
|
2018-07-24 15:52:22 +00:00
|
|
|
|
|
|
|
copy = NULL;
|
2013-06-25 01:27:52 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice *
|
|
|
|
virPCIDeviceListGet(virPCIDeviceList *list,
|
2013-01-14 22:11:44 +00:00
|
|
|
int idx)
|
2009-10-27 17:30:16 +00:00
|
|
|
{
|
|
|
|
if (idx >= list->count)
|
|
|
|
return NULL;
|
|
|
|
if (idx < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return list->devs[idx];
|
|
|
|
}
|
|
|
|
|
2014-01-07 14:44:27 +00:00
|
|
|
size_t
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceListCount(virPCIDeviceList *list)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
2009-10-27 17:30:16 +00:00
|
|
|
return list->count;
|
|
|
|
}
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice *
|
|
|
|
virPCIDeviceListStealIndex(virPCIDeviceList *list,
|
2013-01-14 22:11:44 +00:00
|
|
|
int idx)
|
2009-10-27 17:30:16 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice *ret;
|
2009-08-17 14:05:23 +00:00
|
|
|
|
2012-12-04 07:30:46 +00:00
|
|
|
if (idx < 0 || idx >= list->count)
|
|
|
|
return NULL;
|
2009-08-17 14:05:23 +00:00
|
|
|
|
2012-12-04 07:30:46 +00:00
|
|
|
ret = list->devs[idx];
|
2013-07-05 18:46:35 +00:00
|
|
|
VIR_DELETE_ELEMENT(list->devs, idx, list->count);
|
2009-10-27 17:30:16 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice *
|
|
|
|
virPCIDeviceListSteal(virPCIDeviceList *list,
|
|
|
|
virPCIDeviceAddress *devAddr)
|
2012-12-04 07:30:46 +00:00
|
|
|
{
|
2021-01-04 12:54:42 +00:00
|
|
|
return virPCIDeviceListStealIndex(list, virPCIDeviceListFindIndex(list, devAddr));
|
2012-12-04 07:30:46 +00:00
|
|
|
}
|
|
|
|
|
2009-10-27 17:30:16 +00:00
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceListDel(virPCIDeviceList *list,
|
|
|
|
virPCIDeviceAddress *devAddr)
|
2009-10-27 17:30:16 +00:00
|
|
|
{
|
2021-01-04 12:54:43 +00:00
|
|
|
virPCIDeviceFree(virPCIDeviceListSteal(list, devAddr));
|
2009-08-17 14:05:23 +00:00
|
|
|
}
|
|
|
|
|
2012-12-04 07:30:46 +00:00
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceListFindIndex(virPCIDeviceList *list,
|
|
|
|
virPCIDeviceAddress *devAddr)
|
2009-08-17 14:05:23 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2009-08-17 14:05:23 +00:00
|
|
|
|
2015-12-15 08:44:35 +00:00
|
|
|
for (i = 0; i < list->count; i++) {
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice *other = list->devs[i];
|
2021-01-04 12:54:40 +00:00
|
|
|
if (other->address.domain == devAddr->domain &&
|
|
|
|
other->address.bus == devAddr->bus &&
|
|
|
|
other->address.slot == devAddr->slot &&
|
|
|
|
other->address.function == devAddr->function)
|
2012-12-04 07:30:46 +00:00
|
|
|
return i;
|
2015-12-15 08:44:35 +00:00
|
|
|
}
|
2012-12-04 07:30:46 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2013-05-31 15:06:32 +00:00
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice *
|
|
|
|
virPCIDeviceListFindByIDs(virPCIDeviceList *list,
|
2013-05-31 15:06:32 +00:00
|
|
|
unsigned int domain,
|
|
|
|
unsigned int bus,
|
|
|
|
unsigned int slot,
|
|
|
|
unsigned int function)
|
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2013-05-31 15:06:32 +00:00
|
|
|
|
|
|
|
for (i = 0; i < list->count; i++) {
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice *other = list->devs[i];
|
2015-12-15 08:44:35 +00:00
|
|
|
if (other->address.domain == domain &&
|
|
|
|
other->address.bus == bus &&
|
|
|
|
other->address.slot == slot &&
|
|
|
|
other->address.function == function)
|
2013-05-31 15:06:32 +00:00
|
|
|
return list->devs[i];
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDevice *
|
|
|
|
virPCIDeviceListFind(virPCIDeviceList *list, virPCIDeviceAddress *devAddr)
|
2012-12-04 07:30:46 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
int idx;
|
2012-12-04 07:30:46 +00:00
|
|
|
|
2021-01-04 12:54:41 +00:00
|
|
|
if ((idx = virPCIDeviceListFindIndex(list, devAddr)) >= 0)
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
return list->devs[idx];
|
2012-12-04 07:30:46 +00:00
|
|
|
else
|
|
|
|
return NULL;
|
2009-08-17 14:05:23 +00:00
|
|
|
}
|
2009-08-14 13:20:40 +00:00
|
|
|
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
int virPCIDeviceFileIterate(virPCIDevice *dev,
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceFileActor actor,
|
|
|
|
void *opaque)
|
2009-08-14 13:20:40 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *pcidir = NULL;
|
2020-10-25 21:50:51 +00:00
|
|
|
g_autoptr(DIR) dir = NULL;
|
2009-08-14 13:20:40 +00:00
|
|
|
struct dirent *ent;
|
2014-04-25 20:45:49 +00:00
|
|
|
int direrr;
|
2009-08-14 13:20:40 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
pcidir = g_strdup_printf("/sys/bus/pci/devices/" VIR_PCI_DEVICE_ADDRESS_FMT,
|
|
|
|
dev->address.domain, dev->address.bus, dev->address.slot,
|
|
|
|
dev->address.function);
|
2009-08-14 13:20:40 +00:00
|
|
|
|
2016-06-21 14:34:08 +00:00
|
|
|
if (virDirOpen(&dir, pcidir) < 0)
|
2020-10-27 21:49:11 +00:00
|
|
|
return -1;
|
2009-08-14 13:20:40 +00:00
|
|
|
|
2014-04-25 20:45:49 +00:00
|
|
|
while ((direrr = virDirRead(dir, &ent, pcidir)) > 0) {
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *file = NULL;
|
2009-08-14 13:20:40 +00:00
|
|
|
/* Device assignment requires:
|
2011-03-17 20:26:36 +00:00
|
|
|
* $PCIDIR/config, $PCIDIR/resource, $PCIDIR/resourceNNN,
|
2015-04-23 07:32:16 +00:00
|
|
|
* $PCIDIR/rom, $PCIDIR/reset, $PCIDIR/vendor, $PCIDIR/device
|
2009-08-14 13:20:40 +00:00
|
|
|
*/
|
|
|
|
if (STREQ(ent->d_name, "config") ||
|
|
|
|
STRPREFIX(ent->d_name, "resource") ||
|
2011-03-17 20:26:36 +00:00
|
|
|
STREQ(ent->d_name, "rom") ||
|
2015-04-23 07:32:16 +00:00
|
|
|
STREQ(ent->d_name, "vendor") ||
|
|
|
|
STREQ(ent->d_name, "device") ||
|
2011-03-17 20:26:36 +00:00
|
|
|
STREQ(ent->d_name, "reset")) {
|
2019-10-22 13:26:14 +00:00
|
|
|
file = g_strdup_printf("%s/%s", pcidir, ent->d_name);
|
2010-02-10 09:55:39 +00:00
|
|
|
if ((actor)(dev, file, opaque) < 0)
|
2020-10-27 21:49:11 +00:00
|
|
|
return -1;
|
2009-08-14 13:20:40 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-25 20:45:49 +00:00
|
|
|
if (direrr < 0)
|
2020-10-27 21:49:11 +00:00
|
|
|
return -1;
|
2009-08-14 13:20:40 +00:00
|
|
|
|
2020-10-27 21:49:11 +00:00
|
|
|
return 0;
|
2009-08-14 13:20:40 +00:00
|
|
|
}
|
2009-12-22 17:21:15 +00:00
|
|
|
|
2013-06-23 18:47:57 +00:00
|
|
|
|
|
|
|
/* virPCIDeviceAddressIOMMUGroupIterate:
|
|
|
|
* Call @actor for all devices in the same iommu_group as orig
|
|
|
|
* (including orig itself) Even if there is no iommu_group for the
|
|
|
|
* device, call @actor once for orig.
|
|
|
|
*/
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceAddressIOMMUGroupIterate(virPCIDeviceAddress *orig,
|
2013-06-23 18:47:57 +00:00
|
|
|
virPCIDeviceAddressActor actor,
|
|
|
|
void *opaque)
|
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *groupPath = NULL;
|
2020-10-25 21:50:51 +00:00
|
|
|
g_autoptr(DIR) groupDir = NULL;
|
2013-06-23 18:47:57 +00:00
|
|
|
struct dirent *ent;
|
2014-04-25 20:45:49 +00:00
|
|
|
int direrr;
|
2013-06-23 18:47:57 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
groupPath = g_strdup_printf(PCI_SYSFS "devices/" VIR_PCI_DEVICE_ADDRESS_FMT "/iommu_group/devices",
|
|
|
|
orig->domain, orig->bus, orig->slot, orig->function);
|
2013-06-23 18:47:57 +00:00
|
|
|
|
2016-06-21 14:52:37 +00:00
|
|
|
if (virDirOpenQuiet(&groupDir, groupPath) < 0) {
|
2013-06-23 18:47:57 +00:00
|
|
|
/* just process the original device, nothing more */
|
2020-10-27 21:49:11 +00:00
|
|
|
return (actor)(orig, opaque);
|
2013-06-23 18:47:57 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 20:45:49 +00:00
|
|
|
while ((direrr = virDirRead(groupDir, &ent, groupPath)) > 0) {
|
2022-02-11 13:30:09 +00:00
|
|
|
virPCIDeviceAddress newDev = { 0 };
|
2013-06-23 18:47:57 +00:00
|
|
|
|
|
|
|
if (virPCIDeviceAddressParse(ent->d_name, &newDev) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Found invalid device link '%s' in '%s'"),
|
|
|
|
ent->d_name, groupPath);
|
2020-10-27 21:49:11 +00:00
|
|
|
return -1;
|
2013-06-23 18:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((actor)(&newDev, opaque) < 0)
|
2020-10-27 21:49:11 +00:00
|
|
|
return -1;
|
2013-06-23 18:47:57 +00:00
|
|
|
}
|
2014-04-25 20:45:49 +00:00
|
|
|
if (direrr < 0)
|
2020-10-27 21:49:11 +00:00
|
|
|
return -1;
|
2013-06-23 18:47:57 +00:00
|
|
|
|
2020-10-27 21:49:11 +00:00
|
|
|
return 0;
|
2013-06-23 18:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceGetIOMMUGroupAddOne(virPCIDeviceAddress *newDevAddr, void *opaque)
|
2013-06-23 18:47:57 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceList *groupList = opaque;
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) newDev = NULL;
|
2013-06-23 18:47:57 +00:00
|
|
|
|
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 12:54:28 +00:00
|
|
|
if (!(newDev = virPCIDeviceNew(newDevAddr)))
|
2018-07-24 15:52:22 +00:00
|
|
|
return -1;
|
2013-06-23 18:47:57 +00:00
|
|
|
|
|
|
|
if (virPCIDeviceListAdd(groupList, newDev) < 0)
|
2018-07-24 15:52:22 +00:00
|
|
|
return -1;
|
2013-06-23 18:47:57 +00:00
|
|
|
|
|
|
|
newDev = NULL; /* it's now on the list */
|
2018-07-24 15:52:22 +00:00
|
|
|
return 0;
|
2013-06-23 18:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* virPCIDeviceGetIOMMUGroupList - return a virPCIDeviceList containing
|
|
|
|
* all of the devices in the same iommu_group as @dev.
|
|
|
|
*
|
|
|
|
* Return the new list, or NULL on failure
|
|
|
|
*/
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceList *
|
|
|
|
virPCIDeviceGetIOMMUGroupList(virPCIDevice *dev)
|
2013-06-23 18:47:57 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceList *groupList = virPCIDeviceListNew();
|
2013-06-23 18:47:57 +00:00
|
|
|
|
|
|
|
if (!groupList)
|
|
|
|
goto error;
|
|
|
|
|
2015-12-15 08:44:35 +00:00
|
|
|
if (virPCIDeviceAddressIOMMUGroupIterate(&(dev->address),
|
2013-06-23 18:47:57 +00:00
|
|
|
virPCIDeviceGetIOMMUGroupAddOne,
|
|
|
|
groupList) < 0)
|
|
|
|
goto error;
|
|
|
|
|
|
|
|
return groupList;
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
error:
|
2013-06-23 18:47:57 +00:00
|
|
|
virObjectUnref(groupList);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct {
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceAddress ***iommuGroupDevices;
|
2013-06-23 18:47:57 +00:00
|
|
|
size_t *nIommuGroupDevices;
|
|
|
|
} virPCIDeviceAddressList;
|
|
|
|
|
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIGetIOMMUGroupAddressesAddOne(virPCIDeviceAddress *newDevAddr, void *opaque)
|
2013-06-23 18:47:57 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceAddressList *addrList = opaque;
|
|
|
|
g_autofree virPCIDeviceAddress *copyAddr = NULL;
|
2013-06-23 18:47:57 +00:00
|
|
|
|
|
|
|
/* make a copy to insert onto the list */
|
2020-10-05 17:13:12 +00:00
|
|
|
copyAddr = g_new0(virPCIDeviceAddress, 1);
|
2013-06-23 18:47:57 +00:00
|
|
|
|
|
|
|
*copyAddr = *newDevAddr;
|
|
|
|
|
2021-08-03 12:14:20 +00:00
|
|
|
VIR_APPEND_ELEMENT(*addrList->iommuGroupDevices,
|
|
|
|
*addrList->nIommuGroupDevices, copyAddr);
|
2013-06-23 18:47:57 +00:00
|
|
|
|
2020-10-12 21:25:56 +00:00
|
|
|
return 0;
|
2013-06-23 18:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* virPCIDeviceAddressGetIOMMUGroupAddresses - return a
|
|
|
|
* virPCIDeviceList containing all of the devices in the same
|
|
|
|
* iommu_group as @dev.
|
|
|
|
*
|
|
|
|
* Return the new list, or NULL on failure
|
|
|
|
*/
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceAddressGetIOMMUGroupAddresses(virPCIDeviceAddress *devAddr,
|
|
|
|
virPCIDeviceAddress ***iommuGroupDevices,
|
2013-06-23 18:47:57 +00:00
|
|
|
size_t *nIommuGroupDevices)
|
|
|
|
{
|
|
|
|
virPCIDeviceAddressList addrList = { iommuGroupDevices,
|
|
|
|
nIommuGroupDevices };
|
|
|
|
|
|
|
|
if (virPCIDeviceAddressIOMMUGroupIterate(devAddr,
|
|
|
|
virPCIGetIOMMUGroupAddressesAddOne,
|
|
|
|
&addrList) < 0)
|
2019-10-21 18:19:04 +00:00
|
|
|
return -1;
|
2013-06-23 18:47:57 +00:00
|
|
|
|
2019-10-21 18:19:04 +00:00
|
|
|
return 0;
|
2013-06-23 18:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* virPCIDeviceAddressGetIOMMUGroupNum - return the group number of
|
|
|
|
* this PCI device's iommu_group, or -2 if there is no iommu_group for
|
|
|
|
* the device (or -1 if there was any other error)
|
|
|
|
*/
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceAddressGetIOMMUGroupNum(virPCIDeviceAddress *addr)
|
2013-06-23 18:47:57 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *devName = NULL;
|
|
|
|
g_autofree char *devPath = NULL;
|
|
|
|
g_autofree char *groupPath = NULL;
|
2019-12-23 15:47:21 +00:00
|
|
|
g_autofree char *groupNumStr = NULL;
|
2013-06-23 18:47:57 +00:00
|
|
|
unsigned int groupNum;
|
|
|
|
|
2021-01-04 12:54:25 +00:00
|
|
|
devName = virPCIDeviceAddressAsString(addr);
|
2013-06-23 18:47:57 +00:00
|
|
|
|
2020-10-10 21:46:20 +00:00
|
|
|
devPath = virPCIFile(devName, "iommu_group");
|
|
|
|
|
2018-07-24 15:52:21 +00:00
|
|
|
if (virFileIsLink(devPath) != 1)
|
|
|
|
return -2;
|
2013-06-23 18:47:57 +00:00
|
|
|
if (virFileResolveLink(devPath, &groupPath) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to resolve device %s iommu_group symlink %s"),
|
|
|
|
devName, devPath);
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2013-06-23 18:47:57 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 15:47:21 +00:00
|
|
|
groupNumStr = g_path_get_basename(groupPath);
|
2013-06-23 18:47:57 +00:00
|
|
|
if (virStrToLong_ui(groupNumStr, NULL, 10, &groupNum) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("device %s iommu_group symlink %s has "
|
|
|
|
"invalid group number %s"),
|
|
|
|
devName, groupPath, groupNumStr);
|
2018-07-24 15:52:21 +00:00
|
|
|
return -1;
|
2013-06-23 18:47:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-24 15:52:21 +00:00
|
|
|
return groupNum;
|
2013-06-23 18:47:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-09-19 12:43:13 +00:00
|
|
|
char *
|
|
|
|
virPCIDeviceAddressGetIOMMUGroupDev(const virPCIDeviceAddress *devAddr)
|
|
|
|
{
|
|
|
|
g_autoptr(virPCIDevice) pci = NULL;
|
|
|
|
|
virpci.c: simplify virPCIDeviceNew() signature
The current virPCIDeviceNew() signature, receiving 4 uints in sequence
(domain, bus, slot, function), is not neat.
We already have a way to represent a PCI address in virPCIDeviceAddress
that is used in the code. Aside from the test files, most of
virPCIDeviceNew() callers have access to a virPCIDeviceAddress reference,
but then we need to retrieve the 4 required uints (addr.domain, addr.bus,
addr.slot, addr.function) to satisfy virPCIDeviceNew(). The result is
that we have extra verbosity/boilerplate to retrieve an information that
is already available in virPCIDeviceAddress.
A better way is presented by virNVMEDeviceNew(), where the caller just
supplies a virPCIDeviceAddress pointer and the function handles the
details internally.
This patch changes virPCIDeviceNew() to receive a virPCIDeviceAddress
pointer instead of 4 uints.
Reviewed-by: Laine Stump <laine@redhat.com>
Signed-off-by: Daniel Henrique Barboza <danielhb413@gmail.com>
2021-01-04 12:54:28 +00:00
|
|
|
if (!(pci = virPCIDeviceNew(devAddr)))
|
2019-09-19 12:43:13 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return virPCIDeviceGetIOMMUGroupDev(pci);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-14 20:18:44 +00:00
|
|
|
/* virPCIDeviceGetIOMMUGroupDev - return the name of the device used
|
|
|
|
* to control this PCI device's group (e.g. "/dev/vfio/15")
|
2013-04-25 10:34:43 +00:00
|
|
|
*/
|
|
|
|
char *
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceGetIOMMUGroupDev(virPCIDevice *dev)
|
2013-04-25 10:34:43 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *devPath = NULL;
|
|
|
|
g_autofree char *groupPath = NULL;
|
2019-12-23 15:47:21 +00:00
|
|
|
g_autofree char *groupFile = NULL;
|
2013-04-25 10:34:43 +00:00
|
|
|
|
2020-10-10 21:46:20 +00:00
|
|
|
devPath = virPCIFile(dev->name, "iommu_group");
|
|
|
|
|
2013-04-25 10:34:43 +00:00
|
|
|
if (virFileIsLink(devPath) != 1) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Invalid device %s iommu_group file %s is not a symlink"),
|
|
|
|
dev->name, devPath);
|
2018-07-24 15:52:21 +00:00
|
|
|
return NULL;
|
2013-04-25 10:34:43 +00:00
|
|
|
}
|
|
|
|
if (virFileResolveLink(devPath, &groupPath) < 0) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unable to resolve device %s iommu_group symlink %s"),
|
|
|
|
dev->name, devPath);
|
2018-07-24 15:52:21 +00:00
|
|
|
return NULL;
|
2013-04-25 10:34:43 +00:00
|
|
|
}
|
2019-12-23 15:47:21 +00:00
|
|
|
groupFile = g_path_get_basename(groupPath);
|
2018-07-24 15:52:21 +00:00
|
|
|
|
2019-12-23 15:47:21 +00:00
|
|
|
return g_strdup_printf("/dev/vfio/%s", groupFile);
|
2013-04-25 10:34:43 +00:00
|
|
|
}
|
|
|
|
|
2009-12-22 17:21:15 +00:00
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceDownstreamLacksACS(virPCIDevice *dev)
|
2009-12-22 17:21:15 +00:00
|
|
|
{
|
|
|
|
uint16_t flags;
|
|
|
|
uint16_t ctrl;
|
|
|
|
unsigned int pos;
|
2012-12-04 21:50:58 +00:00
|
|
|
int fd;
|
|
|
|
int ret = 0;
|
2013-12-24 18:07:27 +00:00
|
|
|
uint16_t device_class;
|
2009-12-22 17:21:15 +00:00
|
|
|
|
2019-08-13 13:17:44 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpen(dev)) < 0)
|
2009-12-22 17:21:15 +00:00
|
|
|
return -1;
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceInit(dev, fd) < 0) {
|
2012-12-04 21:50:58 +00:00
|
|
|
ret = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2013-12-24 18:07:27 +00:00
|
|
|
if (virPCIDeviceReadClass(dev, &device_class) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2009-12-22 17:21:15 +00:00
|
|
|
pos = dev->pcie_cap_pos;
|
2013-12-24 18:07:27 +00:00
|
|
|
if (!pos || device_class != PCI_CLASS_BRIDGE_PCI)
|
2012-12-04 21:50:58 +00:00
|
|
|
goto cleanup;
|
2009-12-22 17:21:15 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
flags = virPCIDeviceRead16(dev, fd, pos + PCI_EXP_FLAGS);
|
2009-12-22 17:21:15 +00:00
|
|
|
if (((flags & PCI_EXP_FLAGS_TYPE) >> 4) != PCI_EXP_TYPE_DOWNSTREAM)
|
2012-12-04 21:50:58 +00:00
|
|
|
goto cleanup;
|
2009-12-22 17:21:15 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
pos = virPCIDeviceFindExtendedCapabilityOffset(dev, fd, PCI_EXT_CAP_ID_ACS);
|
2009-12-22 17:21:15 +00:00
|
|
|
if (!pos) {
|
|
|
|
VIR_DEBUG("%s %s: downstream port lacks ACS", dev->id, dev->name);
|
2012-12-04 21:50:58 +00:00
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
2009-12-22 17:21:15 +00:00
|
|
|
}
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
ctrl = virPCIDeviceRead16(dev, fd, pos + PCI_EXT_ACS_CTRL);
|
2009-12-22 17:21:15 +00:00
|
|
|
if ((ctrl & PCI_EXT_CAP_ACS_ENABLED) != PCI_EXT_CAP_ACS_ENABLED) {
|
|
|
|
VIR_DEBUG("%s %s: downstream port has ACS disabled",
|
|
|
|
dev->id, dev->name);
|
2012-12-04 21:50:58 +00:00
|
|
|
ret = 1;
|
|
|
|
goto cleanup;
|
2009-12-22 17:21:15 +00:00
|
|
|
}
|
|
|
|
|
2014-03-25 06:53:22 +00:00
|
|
|
cleanup:
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIDeviceConfigClose(dev, fd);
|
2012-12-04 21:50:58 +00:00
|
|
|
return ret;
|
2009-12-22 17:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceIsBehindSwitchLackingACS(virPCIDevice *dev)
|
2009-12-22 17:21:15 +00:00
|
|
|
{
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) parent = NULL;
|
2009-12-22 17:21:15 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceGetParent(dev, &parent) < 0)
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
return -1;
|
2010-01-19 16:26:54 +00:00
|
|
|
if (!parent) {
|
|
|
|
/* if we have no parent, and this is the root bus, ACS doesn't come
|
|
|
|
* into play since devices on the root bus can't P2P without going
|
|
|
|
* through the root IOMMU.
|
|
|
|
*/
|
2015-12-15 08:44:35 +00:00
|
|
|
if (dev->address.bus == 0) {
|
2010-01-19 16:26:54 +00:00
|
|
|
return 0;
|
2014-09-03 19:39:21 +00:00
|
|
|
} else {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2010-01-19 16:26:54 +00:00
|
|
|
_("Failed to find parent device for %s"),
|
|
|
|
dev->name);
|
|
|
|
return -1;
|
|
|
|
}
|
2009-12-22 17:21:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* XXX we should rather fail when we can't find device's parent and
|
|
|
|
* stop the loop when we get to root instead of just stopping when no
|
|
|
|
* parent can be found
|
|
|
|
*/
|
|
|
|
do {
|
2019-10-15 12:47:50 +00:00
|
|
|
g_autoptr(virPCIDevice) tmp = NULL;
|
2009-12-22 17:21:15 +00:00
|
|
|
int acs;
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
int ret;
|
2009-12-22 17:21:15 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
acs = virPCIDeviceDownstreamLacksACS(parent);
|
2009-12-22 17:21:15 +00:00
|
|
|
|
|
|
|
if (acs) {
|
|
|
|
if (acs < 0)
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2021-08-23 10:47:07 +00:00
|
|
|
tmp = g_steal_pointer(&parent);
|
|
|
|
ret = virPCIDeviceGetParent(tmp, &parent);
|
Fix the ACS checking in the PCI code.
When trying to assign a PCI device to a guest, we have
to check that all bridges upstream of that device support
ACS. That means that we have to find the parent bridge of
the current device, check for ACS, then find the parent bridge
of that device, check for ACS, etc. As it currently stands,
the code to do this iterates through all PCI devices on the
system, looking for a device that has a range of busses that
included the current device's bus.
That check is not restrictive enough, though. Depending on
how we iterated through the list of PCI devices, we could first
find the *topmost* bridge in the system; since it necessarily had
a range of busses including the current device's bus, we
would only ever check the topmost bridge, and not check
any of the intermediate bridges.
Note that this also caused a fairly serious bug in the
secondary bus reset code, where we could erroneously
find and reset the topmost bus instead of the inner bus.
This patch changes pciGetParentDevice() so that it first
checks if a bridge device's secondary bus exactly matches
the bus of the device we are looking for. If it does, we've
found the correct parent bridge and we are done. If it does not,
then we check to see if this bridge device's busses *include* the
bus of the device we care about. If so, we mark this bridge device
as best, and go on. If we later find another bridge device whose
busses include this device, but is more restrictive, then we
free up the previous best and mark the new one as best. This
algorithm ensures that in the normal case we find the direct
parent, but in the case that the parent bridge secondary bus
is not exactly the same as the device, we still find the
correct bridge.
This patch was tested by me on a 4-port NIC with a
bridge without ACS (where assignment failed), a 4-port
NIC with a bridge with ACS (where assignment succeeded),
and a 2-port NIC with no bridges (where assignment
succeeded).
Signed-off-by: Chris Lalancette <clalance@redhat.com>
2010-07-28 20:53:00 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return -1;
|
2009-12-22 17:21:15 +00:00
|
|
|
} while (parent);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
int virPCIDeviceIsAssignable(virPCIDevice *dev,
|
2013-01-14 22:11:44 +00:00
|
|
|
int strict_acs_check)
|
2009-12-22 17:21:15 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* XXX This could be a great place to actually check that a non-managed
|
|
|
|
* device isn't in use, e.g. by checking that device is either un-bound
|
|
|
|
* or bound to a stub driver.
|
|
|
|
*/
|
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
ret = virPCIDeviceIsBehindSwitchLackingACS(dev);
|
2009-12-22 17:21:15 +00:00
|
|
|
if (ret < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (ret) {
|
|
|
|
if (!strict_acs_check) {
|
|
|
|
VIR_DEBUG("%s %s: strict ACS check disabled; device assignment allowed",
|
|
|
|
dev->id, dev->name);
|
|
|
|
} else {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2009-12-22 17:21:15 +00:00
|
|
|
_("Device %s is behind a switch lacking ACS and "
|
|
|
|
"cannot be assigned"),
|
|
|
|
dev->name);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2011-08-16 04:28:43 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
logStrToLong_ui(char const *s,
|
|
|
|
char **end_ptr,
|
|
|
|
int base,
|
|
|
|
unsigned int *result)
|
|
|
|
{
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
ret = virStrToLong_ui(s, end_ptr, base, result);
|
2015-12-20 22:58:58 +00:00
|
|
|
if (ret != 0)
|
2011-08-16 04:28:43 +00:00
|
|
|
VIR_ERROR(_("Failed to convert '%s' to unsigned int"), s);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-17 15:57:19 +00:00
|
|
|
int
|
|
|
|
virPCIDeviceAddressParse(char *address,
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceAddress *bdf)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
|
|
|
char *p = NULL;
|
|
|
|
|
|
|
|
if ((address == NULL) || (logStrToLong_ui(address, &p, 16,
|
|
|
|
&bdf->domain) == -1)) {
|
2020-01-06 21:57:45 +00:00
|
|
|
return -1;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((p == NULL) || (logStrToLong_ui(p+1, &p, 16,
|
|
|
|
&bdf->bus) == -1)) {
|
2020-01-06 21:57:45 +00:00
|
|
|
return -1;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((p == NULL) || (logStrToLong_ui(p+1, &p, 16,
|
|
|
|
&bdf->slot) == -1)) {
|
2020-01-06 21:57:45 +00:00
|
|
|
return -1;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((p == NULL) || (logStrToLong_ui(p+1, &p, 16,
|
|
|
|
&bdf->function) == -1)) {
|
2020-01-06 21:57:45 +00:00
|
|
|
return -1;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2020-01-06 21:57:45 +00:00
|
|
|
return 0;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2013-06-28 08:00:54 +00:00
|
|
|
|
2018-11-08 11:00:26 +00:00
|
|
|
bool
|
conf: fix zPCI address auto-generation on s390
Let us fix the issues with zPCI address validation and auto-generation
on s390.
Currently, there are two issues with handling the ZPCI address
extension. Firstly, when the uid is to be auto-generated with a
specified fid, .i.e.:
...
<address type='pci'>
<zpci fid='0x0000001f'/>
</address>
...
we expect uid='0x0001' (or the next available uid for the domain).
However, we get a parsing error:
$ virsh define zpci.xml
error: XML error: Invalid PCI address uid='0x0000', must be > 0x0000
and <= 0xffff
Secondly, when the uid is specified explicitly with the invalid
numerical value '0x0000', we actually expect the parsing error above.
However, the domain is being defined and the uid value is silently
changed to a valid value.
The first issue is a bug and the second one is undesired behaviour, and
both issues are related to how we (in-band) signal invalid values for
uid and fid. So let's fix the XML parsing to do validation based on what
is actually specified in the XML.
The first issue is also related to the current code behaviour, which
is, if either uid or fid is specified by the user, it is incorrectly
assumed that both uid and fid are specified. This bug is fixed by
identifying when the user specified ZPCI address is incomplete and
auto-generating the missing ZPCI address.
Signed-off-by: Bjoern Walk <bwalk@linux.ibm.com>
Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Signed-off-by: Shalini Chellathurai Saroja <shalini@linux.ibm.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
2020-06-18 08:25:15 +00:00
|
|
|
virZPCIDeviceAddressIsIncomplete(const virZPCIDeviceAddress *addr)
|
2018-11-08 11:00:26 +00:00
|
|
|
{
|
conf: fix zPCI address auto-generation on s390
Let us fix the issues with zPCI address validation and auto-generation
on s390.
Currently, there are two issues with handling the ZPCI address
extension. Firstly, when the uid is to be auto-generated with a
specified fid, .i.e.:
...
<address type='pci'>
<zpci fid='0x0000001f'/>
</address>
...
we expect uid='0x0001' (or the next available uid for the domain).
However, we get a parsing error:
$ virsh define zpci.xml
error: XML error: Invalid PCI address uid='0x0000', must be > 0x0000
and <= 0xffff
Secondly, when the uid is specified explicitly with the invalid
numerical value '0x0000', we actually expect the parsing error above.
However, the domain is being defined and the uid value is silently
changed to a valid value.
The first issue is a bug and the second one is undesired behaviour, and
both issues are related to how we (in-band) signal invalid values for
uid and fid. So let's fix the XML parsing to do validation based on what
is actually specified in the XML.
The first issue is also related to the current code behaviour, which
is, if either uid or fid is specified by the user, it is incorrectly
assumed that both uid and fid are specified. This bug is fixed by
identifying when the user specified ZPCI address is incomplete and
auto-generating the missing ZPCI address.
Signed-off-by: Bjoern Walk <bwalk@linux.ibm.com>
Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Signed-off-by: Shalini Chellathurai Saroja <shalini@linux.ibm.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
2020-06-18 08:25:15 +00:00
|
|
|
return !addr->uid.isSet || !addr->fid.isSet;
|
2018-11-08 11:00:26 +00:00
|
|
|
}
|
|
|
|
|
conf: fix zPCI address auto-generation on s390
Let us fix the issues with zPCI address validation and auto-generation
on s390.
Currently, there are two issues with handling the ZPCI address
extension. Firstly, when the uid is to be auto-generated with a
specified fid, .i.e.:
...
<address type='pci'>
<zpci fid='0x0000001f'/>
</address>
...
we expect uid='0x0001' (or the next available uid for the domain).
However, we get a parsing error:
$ virsh define zpci.xml
error: XML error: Invalid PCI address uid='0x0000', must be > 0x0000
and <= 0xffff
Secondly, when the uid is specified explicitly with the invalid
numerical value '0x0000', we actually expect the parsing error above.
However, the domain is being defined and the uid value is silently
changed to a valid value.
The first issue is a bug and the second one is undesired behaviour, and
both issues are related to how we (in-band) signal invalid values for
uid and fid. So let's fix the XML parsing to do validation based on what
is actually specified in the XML.
The first issue is also related to the current code behaviour, which
is, if either uid or fid is specified by the user, it is incorrectly
assumed that both uid and fid are specified. This bug is fixed by
identifying when the user specified ZPCI address is incomplete and
auto-generating the missing ZPCI address.
Signed-off-by: Bjoern Walk <bwalk@linux.ibm.com>
Signed-off-by: Boris Fiuczynski <fiuczy@linux.ibm.com>
Signed-off-by: Shalini Chellathurai Saroja <shalini@linux.ibm.com>
Reviewed-by: Andrea Bolognani <abologna@redhat.com>
2020-06-18 08:25:15 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
virZPCIDeviceAddressIsPresent(const virZPCIDeviceAddress *addr)
|
|
|
|
{
|
|
|
|
return addr->uid.isSet || addr->fid.isSet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-03 14:45:36 +00:00
|
|
|
void
|
|
|
|
virPCIVirtualFunctionListFree(virPCIVirtualFunctionList *list)
|
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
if (!list)
|
|
|
|
return;
|
|
|
|
|
|
|
|
for (i = 0; i < list->nfunctions; i++) {
|
|
|
|
g_free(list->functions[i].addr);
|
2021-08-04 15:37:44 +00:00
|
|
|
g_free(list->functions[i].ifname);
|
2021-08-03 14:45:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
g_free(list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-08-04 15:37:44 +00:00
|
|
|
int
|
|
|
|
virPCIGetVirtualFunctions(const char *sysfs_path,
|
|
|
|
virPCIVirtualFunctionList **vfs)
|
|
|
|
{
|
|
|
|
return virPCIGetVirtualFunctionsFull(sysfs_path, vfs, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-15 14:50:21 +00:00
|
|
|
#ifdef __linux__
|
2018-11-08 11:00:26 +00:00
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceAddress *
|
2016-05-17 13:16:05 +00:00
|
|
|
virPCIGetDeviceAddressFromSysfsLink(const char *device_link)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
g_autofree virPCIDeviceAddress *bdf = NULL;
|
2019-12-23 15:47:21 +00:00
|
|
|
g_autofree char *config_address = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *device_path = NULL;
|
2011-08-16 04:28:43 +00:00
|
|
|
|
|
|
|
if (!virFileExists(device_link)) {
|
2015-12-20 22:58:58 +00:00
|
|
|
VIR_DEBUG("'%s' does not exist", device_link);
|
2016-05-17 13:16:05 +00:00
|
|
|
return NULL;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2018-05-03 08:04:38 +00:00
|
|
|
device_path = virFileCanonicalizePath(device_link);
|
2011-08-16 04:28:43 +00:00
|
|
|
if (device_path == NULL) {
|
2012-11-23 08:02:07 +00:00
|
|
|
virReportSystemError(errno,
|
|
|
|
_("Failed to resolve device link '%s'"),
|
|
|
|
device_link);
|
2016-05-17 13:16:05 +00:00
|
|
|
return NULL;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2019-12-23 15:47:21 +00:00
|
|
|
config_address = g_path_get_basename(device_path);
|
2020-10-05 17:13:12 +00:00
|
|
|
bdf = g_new0(virPCIDeviceAddress, 1);
|
2011-08-16 04:28:43 +00:00
|
|
|
|
2016-05-17 13:16:05 +00:00
|
|
|
if (virPCIDeviceAddressParse(config_address, bdf) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2011-08-16 04:28:43 +00:00
|
|
|
_("Failed to parse PCI config address '%s'"),
|
|
|
|
config_address);
|
2018-07-24 15:52:21 +00:00
|
|
|
return NULL;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-11 03:05:49 +00:00
|
|
|
return g_steal_pointer(&bdf);
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-25 08:27:29 +00:00
|
|
|
/**
|
|
|
|
* virPCIGetPhysicalFunction:
|
|
|
|
* @vf_sysfs_path: sysfs path for the virtual function
|
|
|
|
* @pf: where to store the physical function's address
|
|
|
|
*
|
|
|
|
* Given @vf_sysfs_path, this function will store the pointer
|
|
|
|
* to a newly-allocated virPCIDeviceAddress in @pf.
|
|
|
|
*
|
|
|
|
* @pf might be NULL if @vf_sysfs_path does not point to a
|
|
|
|
* virtual function. If it's not NULL, then it should be
|
|
|
|
* freed by the caller when no longer needed.
|
|
|
|
*
|
|
|
|
* Returns: >=0 on success, <0 on failure
|
2011-08-16 04:28:43 +00:00
|
|
|
*/
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetPhysicalFunction(const char *vf_sysfs_path,
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceAddress **pf)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *device_link = NULL;
|
2011-08-16 04:28:43 +00:00
|
|
|
|
2016-05-25 08:27:12 +00:00
|
|
|
*pf = NULL;
|
|
|
|
|
2021-02-23 16:28:09 +00:00
|
|
|
virBuildPath(&device_link, vf_sysfs_path, "physfn");
|
2011-08-16 04:28:43 +00:00
|
|
|
|
2016-05-17 13:16:05 +00:00
|
|
|
if ((*pf = virPCIGetDeviceAddressFromSysfsLink(device_link))) {
|
2019-07-30 12:43:44 +00:00
|
|
|
VIR_DEBUG("PF for VF device '%s': " VIR_PCI_DEVICE_ADDRESS_FMT,
|
|
|
|
vf_sysfs_path,
|
2015-12-20 22:58:58 +00:00
|
|
|
(*pf)->domain, (*pf)->bus, (*pf)->slot, (*pf)->function);
|
|
|
|
}
|
2011-08-16 04:28:43 +00:00
|
|
|
|
2016-05-17 14:38:47 +00:00
|
|
|
return 0;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 10:13:05 +00:00
|
|
|
|
2021-08-04 15:37:44 +00:00
|
|
|
/**
|
|
|
|
* virPCIGetVirtualFunctionsFull:
|
|
|
|
* @sysfs_path: path to physical function sysfs entry
|
|
|
|
* @vfs: filled with the virtual function data
|
2021-12-02 19:13:18 +00:00
|
|
|
* @pfNetDevName: Optional netdev name of this PF. If provided, the netdev
|
|
|
|
* names of the VFs are queried too.
|
2021-08-04 15:37:44 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Returns virtual functions of a physical function.
|
2011-08-16 04:28:43 +00:00
|
|
|
*/
|
|
|
|
int
|
2021-08-04 15:37:44 +00:00
|
|
|
virPCIGetVirtualFunctionsFull(const char *sysfs_path,
|
|
|
|
virPCIVirtualFunctionList **vfs,
|
2021-12-02 19:13:18 +00:00
|
|
|
const char *pfNetDevName)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *totalvfs_file = NULL;
|
|
|
|
g_autofree char *totalvfs_str = NULL;
|
2021-08-03 14:45:36 +00:00
|
|
|
g_autoptr(virPCIVirtualFunctionList) list = g_new0(virPCIVirtualFunctionList, 1);
|
2011-08-16 04:28:43 +00:00
|
|
|
|
2021-08-03 14:45:36 +00:00
|
|
|
*vfs = NULL;
|
nodedev: report maxCount for virtual_functions capability
A PCI device may have the capability to setup virtual functions (VFs)
but have them currently all disabled. Prior to this patch, if that was
the case the the node device XML for the device wouldn't report any
virtual_functions capability.
With this patch, if a file called "sriov_totalvfs" is found in the
device's sysfs directory, its contents will be interpreted as a
decimal number, and that value will be reported as "maxCount" in a
capability element of the device's XML, e.g.:
<capability type='virtual_functions' maxCount='7'/>
This will be reported regardless of whether or not any VFs are
currently enabled for the device.
NB: sriov_numvfs (the number of VFs currently active) is also
available in sysfs, but that value is implied by the number of items
in the list that is inside the capability element, so there is no
reason to explicitly provide it as an attribute.
sriov_totalvfs and sriov_numvfs are available in kernels at least as far
back as the 2.6.32 that is in RHEL6.7, but in the case that they
simply aren't there, libvirt will behave as it did prior to this patch
- no maxCount will be displayed, and the virtual_functions capability
will be absent from the device's XML when 0 VFs are enabled.
2015-11-23 19:19:13 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
totalvfs_file = g_strdup_printf("%s/sriov_totalvfs", sysfs_path);
|
nodedev: report maxCount for virtual_functions capability
A PCI device may have the capability to setup virtual functions (VFs)
but have them currently all disabled. Prior to this patch, if that was
the case the the node device XML for the device wouldn't report any
virtual_functions capability.
With this patch, if a file called "sriov_totalvfs" is found in the
device's sysfs directory, its contents will be interpreted as a
decimal number, and that value will be reported as "maxCount" in a
capability element of the device's XML, e.g.:
<capability type='virtual_functions' maxCount='7'/>
This will be reported regardless of whether or not any VFs are
currently enabled for the device.
NB: sriov_numvfs (the number of VFs currently active) is also
available in sysfs, but that value is implied by the number of items
in the list that is inside the capability element, so there is no
reason to explicitly provide it as an attribute.
sriov_totalvfs and sriov_numvfs are available in kernels at least as far
back as the 2.6.32 that is in RHEL6.7, but in the case that they
simply aren't there, libvirt will behave as it did prior to this patch
- no maxCount will be displayed, and the virtual_functions capability
will be absent from the device's XML when 0 VFs are enabled.
2015-11-23 19:19:13 +00:00
|
|
|
if (virFileExists(totalvfs_file)) {
|
|
|
|
char *end = NULL; /* so that terminating \n doesn't create error */
|
2021-08-03 14:45:36 +00:00
|
|
|
unsigned long long maxfunctions = 0;
|
nodedev: report maxCount for virtual_functions capability
A PCI device may have the capability to setup virtual functions (VFs)
but have them currently all disabled. Prior to this patch, if that was
the case the the node device XML for the device wouldn't report any
virtual_functions capability.
With this patch, if a file called "sriov_totalvfs" is found in the
device's sysfs directory, its contents will be interpreted as a
decimal number, and that value will be reported as "maxCount" in a
capability element of the device's XML, e.g.:
<capability type='virtual_functions' maxCount='7'/>
This will be reported regardless of whether or not any VFs are
currently enabled for the device.
NB: sriov_numvfs (the number of VFs currently active) is also
available in sysfs, but that value is implied by the number of items
in the list that is inside the capability element, so there is no
reason to explicitly provide it as an attribute.
sriov_totalvfs and sriov_numvfs are available in kernels at least as far
back as the 2.6.32 that is in RHEL6.7, but in the case that they
simply aren't there, libvirt will behave as it did prior to this patch
- no maxCount will be displayed, and the virtual_functions capability
will be absent from the device's XML when 0 VFs are enabled.
2015-11-23 19:19:13 +00:00
|
|
|
|
|
|
|
if (virFileReadAll(totalvfs_file, 16, &totalvfs_str) < 0)
|
2021-08-03 14:45:36 +00:00
|
|
|
return -1;
|
|
|
|
if (virStrToLong_ull(totalvfs_str, &end, 10, &maxfunctions) < 0) {
|
nodedev: report maxCount for virtual_functions capability
A PCI device may have the capability to setup virtual functions (VFs)
but have them currently all disabled. Prior to this patch, if that was
the case the the node device XML for the device wouldn't report any
virtual_functions capability.
With this patch, if a file called "sriov_totalvfs" is found in the
device's sysfs directory, its contents will be interpreted as a
decimal number, and that value will be reported as "maxCount" in a
capability element of the device's XML, e.g.:
<capability type='virtual_functions' maxCount='7'/>
This will be reported regardless of whether or not any VFs are
currently enabled for the device.
NB: sriov_numvfs (the number of VFs currently active) is also
available in sysfs, but that value is implied by the number of items
in the list that is inside the capability element, so there is no
reason to explicitly provide it as an attribute.
sriov_totalvfs and sriov_numvfs are available in kernels at least as far
back as the 2.6.32 that is in RHEL6.7, but in the case that they
simply aren't there, libvirt will behave as it did prior to this patch
- no maxCount will be displayed, and the virtual_functions capability
will be absent from the device's XML when 0 VFs are enabled.
2015-11-23 19:19:13 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Unrecognized value in %s: %s"),
|
|
|
|
totalvfs_file, totalvfs_str);
|
2021-08-03 14:45:36 +00:00
|
|
|
return -1;
|
nodedev: report maxCount for virtual_functions capability
A PCI device may have the capability to setup virtual functions (VFs)
but have them currently all disabled. Prior to this patch, if that was
the case the the node device XML for the device wouldn't report any
virtual_functions capability.
With this patch, if a file called "sriov_totalvfs" is found in the
device's sysfs directory, its contents will be interpreted as a
decimal number, and that value will be reported as "maxCount" in a
capability element of the device's XML, e.g.:
<capability type='virtual_functions' maxCount='7'/>
This will be reported regardless of whether or not any VFs are
currently enabled for the device.
NB: sriov_numvfs (the number of VFs currently active) is also
available in sysfs, but that value is implied by the number of items
in the list that is inside the capability element, so there is no
reason to explicitly provide it as an attribute.
sriov_totalvfs and sriov_numvfs are available in kernels at least as far
back as the 2.6.32 that is in RHEL6.7, but in the case that they
simply aren't there, libvirt will behave as it did prior to this patch
- no maxCount will be displayed, and the virtual_functions capability
will be absent from the device's XML when 0 VFs are enabled.
2015-11-23 19:19:13 +00:00
|
|
|
}
|
2021-08-03 14:45:36 +00:00
|
|
|
list->maxfunctions = maxfunctions;
|
nodedev: report maxCount for virtual_functions capability
A PCI device may have the capability to setup virtual functions (VFs)
but have them currently all disabled. Prior to this patch, if that was
the case the the node device XML for the device wouldn't report any
virtual_functions capability.
With this patch, if a file called "sriov_totalvfs" is found in the
device's sysfs directory, its contents will be interpreted as a
decimal number, and that value will be reported as "maxCount" in a
capability element of the device's XML, e.g.:
<capability type='virtual_functions' maxCount='7'/>
This will be reported regardless of whether or not any VFs are
currently enabled for the device.
NB: sriov_numvfs (the number of VFs currently active) is also
available in sysfs, but that value is implied by the number of items
in the list that is inside the capability element, so there is no
reason to explicitly provide it as an attribute.
sriov_totalvfs and sriov_numvfs are available in kernels at least as far
back as the 2.6.32 that is in RHEL6.7, but in the case that they
simply aren't there, libvirt will behave as it did prior to this patch
- no maxCount will be displayed, and the virtual_functions capability
will be absent from the device's XML when 0 VFs are enabled.
2015-11-23 19:19:13 +00:00
|
|
|
}
|
2013-07-01 03:52:43 +00:00
|
|
|
|
2013-11-05 10:13:05 +00:00
|
|
|
do {
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *device_link = NULL;
|
2021-08-04 15:37:44 +00:00
|
|
|
struct virPCIVirtualFunction fnc = { NULL, NULL };
|
2021-08-03 14:45:36 +00:00
|
|
|
|
2013-11-05 10:13:05 +00:00
|
|
|
/* look for virtfn%d links until one isn't found */
|
2021-08-03 14:45:36 +00:00
|
|
|
device_link = g_strdup_printf("%s/virtfn%zu", sysfs_path, list->nfunctions);
|
2011-08-16 04:28:43 +00:00
|
|
|
|
2013-11-05 10:13:05 +00:00
|
|
|
if (!virFileExists(device_link))
|
|
|
|
break;
|
2011-08-16 04:28:43 +00:00
|
|
|
|
2021-08-03 14:45:36 +00:00
|
|
|
if (!(fnc.addr = virPCIGetDeviceAddressFromSysfsLink(device_link))) {
|
2013-11-05 10:13:05 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("Failed to get SRIOV function from device link '%s'"),
|
|
|
|
device_link);
|
2021-08-03 14:45:36 +00:00
|
|
|
return -1;
|
2013-11-05 10:13:05 +00:00
|
|
|
}
|
2013-03-25 13:10:51 +00:00
|
|
|
|
2021-12-02 18:52:48 +00:00
|
|
|
if (pfNetDevName &&
|
|
|
|
virPCIGetNetName(device_link, 0, pfNetDevName, &fnc.ifname) < 0) {
|
|
|
|
g_free(fnc.addr);
|
|
|
|
return -1;
|
2021-08-04 15:37:44 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 14:45:36 +00:00
|
|
|
VIR_APPEND_ELEMENT(list->functions, list->nfunctions, fnc);
|
2013-11-05 10:13:05 +00:00
|
|
|
} while (1);
|
2011-08-16 04:28:43 +00:00
|
|
|
|
2021-08-03 14:45:36 +00:00
|
|
|
VIR_DEBUG("Found %zu virtual functions for %s", list->nfunctions, sysfs_path);
|
2013-03-25 13:10:51 +00:00
|
|
|
|
2021-08-03 14:45:36 +00:00
|
|
|
*vfs = g_steal_pointer(&list);
|
|
|
|
return 0;
|
2011-08-16 04:28:43 +00:00
|
|
|
}
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2013-11-05 10:13:05 +00:00
|
|
|
|
2011-08-16 04:28:48 +00:00
|
|
|
/*
|
|
|
|
* Returns 1 if vf device is a virtual function, 0 if not, -1 on error
|
|
|
|
*/
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIIsVirtualFunction(const char *vf_sysfs_device_link)
|
2011-08-16 04:28:48 +00:00
|
|
|
{
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *vf_sysfs_physfn_link = NULL;
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2019-10-22 13:26:14 +00:00
|
|
|
vf_sysfs_physfn_link = g_strdup_printf("%s/physfn", vf_sysfs_device_link);
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2018-07-24 15:52:21 +00:00
|
|
|
return virFileExists(vf_sysfs_physfn_link);
|
2011-08-16 04:28:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the sriov virtual function index of vf given its pf
|
|
|
|
*/
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetVirtualFunctionIndex(const char *pf_sysfs_device_link,
|
|
|
|
const char *vf_sysfs_device_link,
|
|
|
|
int *vf_index)
|
2011-08-16 04:28:48 +00:00
|
|
|
{
|
Convert 'int i' to 'size_t i' in src/util/ files
Convert the type of loop iterators named 'i', 'j', k',
'ii', 'jj', 'kk', to be 'size_t' instead of 'int' or
'unsigned int', also santizing 'ii', 'jj', 'kk' to use
the normal 'i', 'j', 'k' naming
Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
2013-07-08 14:09:33 +00:00
|
|
|
size_t i;
|
2021-03-11 07:16:13 +00:00
|
|
|
g_autofree virPCIDeviceAddress *vf_bdf = NULL;
|
2021-08-03 14:45:36 +00:00
|
|
|
g_autoptr(virPCIVirtualFunctionList) virt_fns = NULL;
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2016-05-17 13:16:05 +00:00
|
|
|
if (!(vf_bdf = virPCIGetDeviceAddressFromSysfsLink(vf_sysfs_device_link)))
|
2021-08-04 14:35:31 +00:00
|
|
|
return -1;
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2021-08-03 14:45:36 +00:00
|
|
|
if (virPCIGetVirtualFunctions(pf_sysfs_device_link, &virt_fns) < 0) {
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2011-08-16 04:28:48 +00:00
|
|
|
_("Error getting physical function's '%s' "
|
2013-01-14 22:11:44 +00:00
|
|
|
"virtual_functions"), pf_sysfs_device_link);
|
2021-08-04 14:35:31 +00:00
|
|
|
return -1;
|
2011-08-16 04:28:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-03 14:45:36 +00:00
|
|
|
for (i = 0; i < virt_fns->nfunctions; i++) {
|
|
|
|
if (virPCIDeviceAddressEqual(vf_bdf, virt_fns->functions[i].addr)) {
|
2013-01-14 22:11:44 +00:00
|
|
|
*vf_index = i;
|
2021-08-04 14:35:31 +00:00
|
|
|
return 0;
|
2013-01-14 22:11:44 +00:00
|
|
|
}
|
2011-08-16 04:28:48 +00:00
|
|
|
}
|
|
|
|
|
2021-08-04 14:35:31 +00:00
|
|
|
return -1;
|
2011-08-16 04:28:48 +00:00
|
|
|
}
|
|
|
|
|
2011-12-14 10:50:01 +00:00
|
|
|
/*
|
|
|
|
* Returns a path to the PCI sysfs file given the BDF of the PCI function
|
|
|
|
*/
|
|
|
|
|
2012-03-06 01:12:23 +00:00
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceAddressGetSysfsFile(virPCIDeviceAddress *addr,
|
2013-01-14 22:11:44 +00:00
|
|
|
char **pci_sysfs_device_link)
|
2012-03-06 01:12:23 +00:00
|
|
|
{
|
2019-10-22 13:26:14 +00:00
|
|
|
*pci_sysfs_device_link = g_strdup_printf(PCI_SYSFS "devices/" VIR_PCI_DEVICE_ADDRESS_FMT, addr->domain,
|
|
|
|
addr->bus, addr->slot, addr->function);
|
2013-07-18 10:13:46 +00:00
|
|
|
return 0;
|
2012-03-06 01:12:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-31 04:21:45 +00:00
|
|
|
/**
|
|
|
|
* virPCIGetNetName:
|
|
|
|
* @device_link_sysfs_path: sysfs path to the PCI device
|
|
|
|
* @idx: used to choose which netdev when there are several
|
util: Add phys_port_name support on virPCIGetNetName
virPCIGetNetName is used to get the name of the netdev associated with
a particular PCI device. This is used when we have a VF name, but need
the PF name in order to send a netlink command (e.g. in order to
get/set the MAC address of the VF).
In simple cases there is a single netdev associated with any PCI
device, so it is easy to figure out the PF netdev for a VF - just look
for the PCI device that has the VF listed in its "virtfns" directory;
the only name in the "net" subdirectory of that PCI device's sysfs
directory is the PF netdev that is upstream of the VF in question.
In some cases there can be more than one netdev in a PCI device's net
directory though. In the past, the only case of this was for SR-IOV
NICs that could have multiple PF's per PCI device. In this case, all
PF netdevs associated with a PCI address would be listed in the "net"
subdirectory of the PCI device's directory in sysfs. At the same time,
all VF netdevs and all PF netdevs have a phys_port_id in their sysfs,
so the way to learn the correct PF netdev for a particular VF netdev
is to search through the list of devices in the net subdirectory of
the PF's PCI device, looking for the one netdev with a "phys_port_id"
matching that of the VF netdev.
But starting in kernel 5.8, the NVIDIA Mellanox driver began linking
the VFs' representor netdevs to the PF PCI address [1], and so the VF
representor netdevs would also show up in the net
subdirectory. However, all of the devices that do so also only have a
single PF netdev for any given PCI address.
This means that the net directory of the PCI device can still hold
multiple net devices, but only one of them will be the PF netdev (the
others are VF representors):
$ ls '/sys/bus/pci/devices/0000:82:00.0/net'
ens1f0 eth0 eth1
In this case the way to find the PF device is to look at the
"phys_port_name" attribute of each netdev in sysfs. All PF devices
have a phys_port_name matching a particular regex
(p[0-9]+$)|(p[0-9]+s[0-9]+$)
Since there can only be one PF in the entire list of devices, once we
match that regex, we've found the PF netdev.
[1] - https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/
commit/?id=123f0f53dd64b67e34142485fe866a8a581f12f1
Co-Authored-by: Moshe Levi <moshele@nvidia.com>
Signed-off-by: Dmytro Linkin <dlinkin@nvidia.com>
Reviewed-by: Adrian Chiris <adrianc@nvidia.com>
Reviewed-by: Laine Stump <laine@redhat.com>
2021-01-21 12:15:22 +00:00
|
|
|
* (ignored if physPortID is set or physPortName is available)
|
2021-12-02 18:52:48 +00:00
|
|
|
|
|
|
|
* @physPortNetDevName: if non-null, attempt to learn the phys_port_id
|
|
|
|
* of the netdev interface named
|
|
|
|
* @physPortNetDevName, and find a netdev for
|
|
|
|
* this PCI device that has the same
|
|
|
|
* phys_port_id. if @physPortNetDevName is NULL,
|
|
|
|
* or has no phys_port_id, then use
|
|
|
|
* phys_port_name or idx to determine which
|
|
|
|
* netdev to return. (NB: as of today, only mlx
|
|
|
|
* drivers/cards can have multiple phys_ports for
|
|
|
|
* a single PCI device; on all other devices
|
|
|
|
* there is only a single choice of netdev, and
|
|
|
|
* phys_port_id, phys_port_name, and idx are
|
|
|
|
* unavailable/unused)
|
2017-07-31 04:21:45 +00:00
|
|
|
* @netname: used to return the name of the netdev
|
|
|
|
* (set to NULL (but returns success) if there is no netdev)
|
|
|
|
*
|
|
|
|
* Returns 0 on success, -1 on error (error has been logged)
|
2011-08-16 04:28:48 +00:00
|
|
|
*/
|
|
|
|
int
|
2017-07-31 04:21:45 +00:00
|
|
|
virPCIGetNetName(const char *device_link_sysfs_path,
|
|
|
|
size_t idx,
|
2021-12-02 18:52:48 +00:00
|
|
|
const char *physPortNetDevName,
|
2017-07-31 04:21:45 +00:00
|
|
|
char **netname)
|
2013-01-14 22:11:44 +00:00
|
|
|
{
|
2021-12-02 18:52:48 +00:00
|
|
|
g_autofree char *physPortID = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *pcidev_sysfs_net_path = NULL;
|
|
|
|
g_autofree char *firstEntryName = NULL;
|
2020-10-25 21:50:51 +00:00
|
|
|
g_autoptr(DIR) dir = NULL;
|
2013-01-14 22:11:44 +00:00
|
|
|
struct dirent *entry = NULL;
|
2017-07-31 04:21:45 +00:00
|
|
|
size_t i = 0;
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2019-01-23 09:38:48 +00:00
|
|
|
*netname = NULL;
|
|
|
|
|
2021-12-02 18:52:48 +00:00
|
|
|
if (physPortNetDevName &&
|
|
|
|
virNetDevGetPhysPortID(physPortNetDevName, &physPortID) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2021-02-23 16:28:09 +00:00
|
|
|
virBuildPath(&pcidev_sysfs_net_path, device_link_sysfs_path, "net");
|
2013-01-14 22:11:44 +00:00
|
|
|
|
2017-03-03 16:54:59 +00:00
|
|
|
if (virDirOpenQuiet(&dir, pcidev_sysfs_net_path) < 0) {
|
|
|
|
/* this *isn't* an error - caller needs to check for netname == NULL */
|
2020-10-27 02:04:31 +00:00
|
|
|
return 0;
|
2017-03-03 16:54:59 +00:00
|
|
|
}
|
2011-08-16 04:28:48 +00:00
|
|
|
|
2014-04-25 20:45:49 +00:00
|
|
|
while (virDirRead(dir, &entry, pcidev_sysfs_net_path) > 0) {
|
util: Add phys_port_name support on virPCIGetNetName
virPCIGetNetName is used to get the name of the netdev associated with
a particular PCI device. This is used when we have a VF name, but need
the PF name in order to send a netlink command (e.g. in order to
get/set the MAC address of the VF).
In simple cases there is a single netdev associated with any PCI
device, so it is easy to figure out the PF netdev for a VF - just look
for the PCI device that has the VF listed in its "virtfns" directory;
the only name in the "net" subdirectory of that PCI device's sysfs
directory is the PF netdev that is upstream of the VF in question.
In some cases there can be more than one netdev in a PCI device's net
directory though. In the past, the only case of this was for SR-IOV
NICs that could have multiple PF's per PCI device. In this case, all
PF netdevs associated with a PCI address would be listed in the "net"
subdirectory of the PCI device's directory in sysfs. At the same time,
all VF netdevs and all PF netdevs have a phys_port_id in their sysfs,
so the way to learn the correct PF netdev for a particular VF netdev
is to search through the list of devices in the net subdirectory of
the PF's PCI device, looking for the one netdev with a "phys_port_id"
matching that of the VF netdev.
But starting in kernel 5.8, the NVIDIA Mellanox driver began linking
the VFs' representor netdevs to the PF PCI address [1], and so the VF
representor netdevs would also show up in the net
subdirectory. However, all of the devices that do so also only have a
single PF netdev for any given PCI address.
This means that the net directory of the PCI device can still hold
multiple net devices, but only one of them will be the PF netdev (the
others are VF representors):
$ ls '/sys/bus/pci/devices/0000:82:00.0/net'
ens1f0 eth0 eth1
In this case the way to find the PF device is to look at the
"phys_port_name" attribute of each netdev in sysfs. All PF devices
have a phys_port_name matching a particular regex
(p[0-9]+$)|(p[0-9]+s[0-9]+$)
Since there can only be one PF in the entire list of devices, once we
match that regex, we've found the PF netdev.
[1] - https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/
commit/?id=123f0f53dd64b67e34142485fe866a8a581f12f1
Co-Authored-by: Moshe Levi <moshele@nvidia.com>
Signed-off-by: Dmytro Linkin <dlinkin@nvidia.com>
Reviewed-by: Adrian Chiris <adrianc@nvidia.com>
Reviewed-by: Laine Stump <laine@redhat.com>
2021-01-21 12:15:22 +00:00
|
|
|
/* save the first entry we find to use as a failsafe
|
|
|
|
* in case we don't match the phys_port_id. This is
|
|
|
|
* needed because some NIC drivers (e.g. i40e)
|
|
|
|
* implement phys_port_id for PFs, but not for VFs
|
|
|
|
*/
|
|
|
|
if (!firstEntryName)
|
|
|
|
firstEntryName = g_strdup(entry->d_name);
|
|
|
|
|
2017-07-31 04:21:45 +00:00
|
|
|
/* if the caller sent a physPortID, compare it to the
|
|
|
|
* physportID of this netdev. If not, look for entry[idx].
|
|
|
|
*/
|
|
|
|
if (physPortID) {
|
2020-10-11 02:56:23 +00:00
|
|
|
g_autofree char *thisPhysPortID = NULL;
|
|
|
|
|
2017-07-31 04:21:45 +00:00
|
|
|
if (virNetDevGetPhysPortID(entry->d_name, &thisPhysPortID) < 0)
|
2020-10-27 02:04:31 +00:00
|
|
|
return -1;
|
2017-07-31 04:21:45 +00:00
|
|
|
|
|
|
|
/* if this one doesn't match, keep looking */
|
util: Add phys_port_name support on virPCIGetNetName
virPCIGetNetName is used to get the name of the netdev associated with
a particular PCI device. This is used when we have a VF name, but need
the PF name in order to send a netlink command (e.g. in order to
get/set the MAC address of the VF).
In simple cases there is a single netdev associated with any PCI
device, so it is easy to figure out the PF netdev for a VF - just look
for the PCI device that has the VF listed in its "virtfns" directory;
the only name in the "net" subdirectory of that PCI device's sysfs
directory is the PF netdev that is upstream of the VF in question.
In some cases there can be more than one netdev in a PCI device's net
directory though. In the past, the only case of this was for SR-IOV
NICs that could have multiple PF's per PCI device. In this case, all
PF netdevs associated with a PCI address would be listed in the "net"
subdirectory of the PCI device's directory in sysfs. At the same time,
all VF netdevs and all PF netdevs have a phys_port_id in their sysfs,
so the way to learn the correct PF netdev for a particular VF netdev
is to search through the list of devices in the net subdirectory of
the PF's PCI device, looking for the one netdev with a "phys_port_id"
matching that of the VF netdev.
But starting in kernel 5.8, the NVIDIA Mellanox driver began linking
the VFs' representor netdevs to the PF PCI address [1], and so the VF
representor netdevs would also show up in the net
subdirectory. However, all of the devices that do so also only have a
single PF netdev for any given PCI address.
This means that the net directory of the PCI device can still hold
multiple net devices, but only one of them will be the PF netdev (the
others are VF representors):
$ ls '/sys/bus/pci/devices/0000:82:00.0/net'
ens1f0 eth0 eth1
In this case the way to find the PF device is to look at the
"phys_port_name" attribute of each netdev in sysfs. All PF devices
have a phys_port_name matching a particular regex
(p[0-9]+$)|(p[0-9]+s[0-9]+$)
Since there can only be one PF in the entire list of devices, once we
match that regex, we've found the PF netdev.
[1] - https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/
commit/?id=123f0f53dd64b67e34142485fe866a8a581f12f1
Co-Authored-by: Moshe Levi <moshele@nvidia.com>
Signed-off-by: Dmytro Linkin <dlinkin@nvidia.com>
Reviewed-by: Adrian Chiris <adrianc@nvidia.com>
Reviewed-by: Laine Stump <laine@redhat.com>
2021-01-21 12:15:22 +00:00
|
|
|
if (STRNEQ_NULLABLE(physPortID, thisPhysPortID))
|
2017-07-31 04:21:45 +00:00
|
|
|
continue;
|
util: Add phys_port_name support on virPCIGetNetName
virPCIGetNetName is used to get the name of the netdev associated with
a particular PCI device. This is used when we have a VF name, but need
the PF name in order to send a netlink command (e.g. in order to
get/set the MAC address of the VF).
In simple cases there is a single netdev associated with any PCI
device, so it is easy to figure out the PF netdev for a VF - just look
for the PCI device that has the VF listed in its "virtfns" directory;
the only name in the "net" subdirectory of that PCI device's sysfs
directory is the PF netdev that is upstream of the VF in question.
In some cases there can be more than one netdev in a PCI device's net
directory though. In the past, the only case of this was for SR-IOV
NICs that could have multiple PF's per PCI device. In this case, all
PF netdevs associated with a PCI address would be listed in the "net"
subdirectory of the PCI device's directory in sysfs. At the same time,
all VF netdevs and all PF netdevs have a phys_port_id in their sysfs,
so the way to learn the correct PF netdev for a particular VF netdev
is to search through the list of devices in the net subdirectory of
the PF's PCI device, looking for the one netdev with a "phys_port_id"
matching that of the VF netdev.
But starting in kernel 5.8, the NVIDIA Mellanox driver began linking
the VFs' representor netdevs to the PF PCI address [1], and so the VF
representor netdevs would also show up in the net
subdirectory. However, all of the devices that do so also only have a
single PF netdev for any given PCI address.
This means that the net directory of the PCI device can still hold
multiple net devices, but only one of them will be the PF netdev (the
others are VF representors):
$ ls '/sys/bus/pci/devices/0000:82:00.0/net'
ens1f0 eth0 eth1
In this case the way to find the PF device is to look at the
"phys_port_name" attribute of each netdev in sysfs. All PF devices
have a phys_port_name matching a particular regex
(p[0-9]+$)|(p[0-9]+s[0-9]+$)
Since there can only be one PF in the entire list of devices, once we
match that regex, we've found the PF netdev.
[1] - https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/
commit/?id=123f0f53dd64b67e34142485fe866a8a581f12f1
Co-Authored-by: Moshe Levi <moshele@nvidia.com>
Signed-off-by: Dmytro Linkin <dlinkin@nvidia.com>
Reviewed-by: Adrian Chiris <adrianc@nvidia.com>
Reviewed-by: Laine Stump <laine@redhat.com>
2021-01-21 12:15:22 +00:00
|
|
|
|
2017-07-31 04:21:45 +00:00
|
|
|
} else {
|
util: Add phys_port_name support on virPCIGetNetName
virPCIGetNetName is used to get the name of the netdev associated with
a particular PCI device. This is used when we have a VF name, but need
the PF name in order to send a netlink command (e.g. in order to
get/set the MAC address of the VF).
In simple cases there is a single netdev associated with any PCI
device, so it is easy to figure out the PF netdev for a VF - just look
for the PCI device that has the VF listed in its "virtfns" directory;
the only name in the "net" subdirectory of that PCI device's sysfs
directory is the PF netdev that is upstream of the VF in question.
In some cases there can be more than one netdev in a PCI device's net
directory though. In the past, the only case of this was for SR-IOV
NICs that could have multiple PF's per PCI device. In this case, all
PF netdevs associated with a PCI address would be listed in the "net"
subdirectory of the PCI device's directory in sysfs. At the same time,
all VF netdevs and all PF netdevs have a phys_port_id in their sysfs,
so the way to learn the correct PF netdev for a particular VF netdev
is to search through the list of devices in the net subdirectory of
the PF's PCI device, looking for the one netdev with a "phys_port_id"
matching that of the VF netdev.
But starting in kernel 5.8, the NVIDIA Mellanox driver began linking
the VFs' representor netdevs to the PF PCI address [1], and so the VF
representor netdevs would also show up in the net
subdirectory. However, all of the devices that do so also only have a
single PF netdev for any given PCI address.
This means that the net directory of the PCI device can still hold
multiple net devices, but only one of them will be the PF netdev (the
others are VF representors):
$ ls '/sys/bus/pci/devices/0000:82:00.0/net'
ens1f0 eth0 eth1
In this case the way to find the PF device is to look at the
"phys_port_name" attribute of each netdev in sysfs. All PF devices
have a phys_port_name matching a particular regex
(p[0-9]+$)|(p[0-9]+s[0-9]+$)
Since there can only be one PF in the entire list of devices, once we
match that regex, we've found the PF netdev.
[1] - https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/
commit/?id=123f0f53dd64b67e34142485fe866a8a581f12f1
Co-Authored-by: Moshe Levi <moshele@nvidia.com>
Signed-off-by: Dmytro Linkin <dlinkin@nvidia.com>
Reviewed-by: Adrian Chiris <adrianc@nvidia.com>
Reviewed-by: Laine Stump <laine@redhat.com>
2021-01-21 12:15:22 +00:00
|
|
|
/* Most switch devices use phys_port_name instead of
|
|
|
|
* phys_port_id.
|
|
|
|
* NOTE: VFs' representors net devices can be linked to PF's PCI
|
|
|
|
* device, which mean that there'll be multiple net devices
|
|
|
|
* instances and to get a proper net device need to match on
|
|
|
|
* specific regex.
|
|
|
|
* To get PF netdev, for ex., used following regex:
|
|
|
|
* "(p[0-9]+$)|(p[0-9]+s[0-9]+$)"
|
|
|
|
* or to get exact VF's netdev next regex is used:
|
|
|
|
* "pf0vf1$"
|
|
|
|
*/
|
|
|
|
g_autofree char *thisPhysPortName = NULL;
|
|
|
|
|
|
|
|
if (virNetDevGetPhysPortName(entry->d_name, &thisPhysPortName) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (thisPhysPortName) {
|
|
|
|
|
|
|
|
/* if this one doesn't match, keep looking */
|
|
|
|
if (!virStringMatch(thisPhysPortName, VIR_PF_PHYS_PORT_NAME_REGEX))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if (i++ < idx)
|
|
|
|
continue;
|
|
|
|
}
|
2017-07-31 04:21:45 +00:00
|
|
|
}
|
|
|
|
|
2019-10-20 11:49:46 +00:00
|
|
|
*netname = g_strdup(entry->d_name);
|
2020-10-27 02:04:31 +00:00
|
|
|
return 0;
|
2013-01-14 22:11:44 +00:00
|
|
|
}
|
|
|
|
|
2020-10-27 02:04:31 +00:00
|
|
|
if (firstEntryName) {
|
util: Add phys_port_name support on virPCIGetNetName
virPCIGetNetName is used to get the name of the netdev associated with
a particular PCI device. This is used when we have a VF name, but need
the PF name in order to send a netlink command (e.g. in order to
get/set the MAC address of the VF).
In simple cases there is a single netdev associated with any PCI
device, so it is easy to figure out the PF netdev for a VF - just look
for the PCI device that has the VF listed in its "virtfns" directory;
the only name in the "net" subdirectory of that PCI device's sysfs
directory is the PF netdev that is upstream of the VF in question.
In some cases there can be more than one netdev in a PCI device's net
directory though. In the past, the only case of this was for SR-IOV
NICs that could have multiple PF's per PCI device. In this case, all
PF netdevs associated with a PCI address would be listed in the "net"
subdirectory of the PCI device's directory in sysfs. At the same time,
all VF netdevs and all PF netdevs have a phys_port_id in their sysfs,
so the way to learn the correct PF netdev for a particular VF netdev
is to search through the list of devices in the net subdirectory of
the PF's PCI device, looking for the one netdev with a "phys_port_id"
matching that of the VF netdev.
But starting in kernel 5.8, the NVIDIA Mellanox driver began linking
the VFs' representor netdevs to the PF PCI address [1], and so the VF
representor netdevs would also show up in the net
subdirectory. However, all of the devices that do so also only have a
single PF netdev for any given PCI address.
This means that the net directory of the PCI device can still hold
multiple net devices, but only one of them will be the PF netdev (the
others are VF representors):
$ ls '/sys/bus/pci/devices/0000:82:00.0/net'
ens1f0 eth0 eth1
In this case the way to find the PF device is to look at the
"phys_port_name" attribute of each netdev in sysfs. All PF devices
have a phys_port_name matching a particular regex
(p[0-9]+$)|(p[0-9]+s[0-9]+$)
Since there can only be one PF in the entire list of devices, once we
match that regex, we've found the PF netdev.
[1] - https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/
commit/?id=123f0f53dd64b67e34142485fe866a8a581f12f1
Co-Authored-by: Moshe Levi <moshele@nvidia.com>
Signed-off-by: Dmytro Linkin <dlinkin@nvidia.com>
Reviewed-by: Adrian Chiris <adrianc@nvidia.com>
Reviewed-by: Laine Stump <laine@redhat.com>
2021-01-21 12:15:22 +00:00
|
|
|
/* we didn't match the provided phys_port_id / find a
|
|
|
|
* phys_port_name matching VIR_PF_PHYS_PORT_NAME_REGEX / find
|
|
|
|
* as many net devices as the value of idx, but this is
|
|
|
|
* probably because phys_port_id / phys_port_name isn't
|
|
|
|
* implemented for this NIC driver, so just return the first
|
2020-10-27 02:04:31 +00:00
|
|
|
* (probably only) netname we found.
|
|
|
|
*/
|
|
|
|
*netname = g_steal_pointer(&firstEntryName);
|
|
|
|
return 0;
|
2017-07-31 04:21:45 +00:00
|
|
|
}
|
2020-10-27 02:04:31 +00:00
|
|
|
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
util: Add phys_port_name support on virPCIGetNetName
virPCIGetNetName is used to get the name of the netdev associated with
a particular PCI device. This is used when we have a VF name, but need
the PF name in order to send a netlink command (e.g. in order to
get/set the MAC address of the VF).
In simple cases there is a single netdev associated with any PCI
device, so it is easy to figure out the PF netdev for a VF - just look
for the PCI device that has the VF listed in its "virtfns" directory;
the only name in the "net" subdirectory of that PCI device's sysfs
directory is the PF netdev that is upstream of the VF in question.
In some cases there can be more than one netdev in a PCI device's net
directory though. In the past, the only case of this was for SR-IOV
NICs that could have multiple PF's per PCI device. In this case, all
PF netdevs associated with a PCI address would be listed in the "net"
subdirectory of the PCI device's directory in sysfs. At the same time,
all VF netdevs and all PF netdevs have a phys_port_id in their sysfs,
so the way to learn the correct PF netdev for a particular VF netdev
is to search through the list of devices in the net subdirectory of
the PF's PCI device, looking for the one netdev with a "phys_port_id"
matching that of the VF netdev.
But starting in kernel 5.8, the NVIDIA Mellanox driver began linking
the VFs' representor netdevs to the PF PCI address [1], and so the VF
representor netdevs would also show up in the net
subdirectory. However, all of the devices that do so also only have a
single PF netdev for any given PCI address.
This means that the net directory of the PCI device can still hold
multiple net devices, but only one of them will be the PF netdev (the
others are VF representors):
$ ls '/sys/bus/pci/devices/0000:82:00.0/net'
ens1f0 eth0 eth1
In this case the way to find the PF device is to look at the
"phys_port_name" attribute of each netdev in sysfs. All PF devices
have a phys_port_name matching a particular regex
(p[0-9]+$)|(p[0-9]+s[0-9]+$)
Since there can only be one PF in the entire list of devices, once we
match that regex, we've found the PF netdev.
[1] - https://git.kernel.org/pub/scm/linux/kernel/git/netdev/net.git/
commit/?id=123f0f53dd64b67e34142485fe866a8a581f12f1
Co-Authored-by: Moshe Levi <moshele@nvidia.com>
Signed-off-by: Dmytro Linkin <dlinkin@nvidia.com>
Reviewed-by: Adrian Chiris <adrianc@nvidia.com>
Reviewed-by: Laine Stump <laine@redhat.com>
2021-01-21 12:15:22 +00:00
|
|
|
_("Could not find any network device under PCI device at %s"),
|
|
|
|
device_link_sysfs_path);
|
2020-10-27 02:04:31 +00:00
|
|
|
return -1;
|
2011-08-16 04:28:48 +00:00
|
|
|
}
|
2012-03-06 01:12:23 +00:00
|
|
|
|
|
|
|
int
|
2013-01-14 22:11:44 +00:00
|
|
|
virPCIGetVirtualFunctionInfo(const char *vf_sysfs_device_path,
|
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
|
|
|
int pfNetDevIdx,
|
|
|
|
char **pfname,
|
|
|
|
int *vf_index)
|
2012-03-06 01:12:23 +00:00
|
|
|
{
|
2021-03-11 07:16:13 +00:00
|
|
|
g_autofree virPCIDeviceAddress *pf_config_address = NULL;
|
2019-10-15 13:16:31 +00:00
|
|
|
g_autofree char *pf_sysfs_device_path = NULL;
|
|
|
|
g_autofree char *vfname = NULL;
|
2012-03-06 01:12:23 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIGetPhysicalFunction(vf_sysfs_device_path, &pf_config_address) < 0)
|
2020-10-12 21:25:56 +00:00
|
|
|
return -1;
|
2012-03-06 01:12:23 +00:00
|
|
|
|
2016-05-25 08:01:58 +00:00
|
|
|
if (!pf_config_address)
|
2020-10-12 21:25:56 +00:00
|
|
|
return -1;
|
2016-05-25 08:01:58 +00:00
|
|
|
|
2013-01-14 22:11:44 +00:00
|
|
|
if (virPCIDeviceAddressGetSysfsFile(pf_config_address,
|
|
|
|
&pf_sysfs_device_path) < 0) {
|
2020-10-12 21:25:56 +00:00
|
|
|
return -1;
|
2017-03-03 16:54:59 +00:00
|
|
|
}
|
2012-03-06 01:12:23 +00:00
|
|
|
|
2017-03-03 16:54:59 +00:00
|
|
|
if (virPCIGetVirtualFunctionIndex(pf_sysfs_device_path,
|
|
|
|
vf_sysfs_device_path, vf_index) < 0) {
|
2020-10-12 21:25:56 +00:00
|
|
|
return -1;
|
2012-03-06 01:12:23 +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 the caller hasn't asked for a specific pfNetDevIdx, and VF
|
|
|
|
* is bound to a netdev, learn that netdev's phys_port_id (if
|
|
|
|
* available). This can be used to disambiguate when the PF has
|
|
|
|
* multiple netdevs. If the VF isn't bound to a netdev, then we
|
|
|
|
* return netdev[pfNetDevIdx] on the PF, which may or may not be
|
|
|
|
* correct.
|
|
|
|
*/
|
|
|
|
if (pfNetDevIdx == -1) {
|
|
|
|
if (virPCIGetNetName(vf_sysfs_device_path, 0, NULL, &vfname) < 0)
|
2020-10-12 21:25:56 +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
|
|
|
|
|
|
|
pfNetDevIdx = 0;
|
|
|
|
}
|
|
|
|
|
2021-12-02 18:52:48 +00:00
|
|
|
if (virPCIGetNetName(pf_sysfs_device_path, pfNetDevIdx, vfname, pfname) < 0)
|
2020-10-12 21:25:56 +00:00
|
|
|
return -1;
|
2012-03-06 01:12:23 +00:00
|
|
|
|
2017-03-03 16:54:59 +00:00
|
|
|
if (!*pfname) {
|
|
|
|
/* this shouldn't be possible. A VF can't exist unless its
|
|
|
|
* PF device is bound to a network driver
|
|
|
|
*/
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("The PF device for VF %s has no network device name"),
|
|
|
|
vf_sysfs_device_path);
|
2020-10-12 21:25:56 +00:00
|
|
|
return -1;
|
2017-03-03 16:54:59 +00:00
|
|
|
}
|
2012-03-06 01:12:23 +00:00
|
|
|
|
2020-10-12 21:25:56 +00:00
|
|
|
return 0;
|
2012-03-06 01:12:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 08:30:32 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
virPCIDeviceHasVPD(virPCIDevice *dev)
|
|
|
|
{
|
|
|
|
g_autofree char *vpdPath = NULL;
|
|
|
|
|
|
|
|
vpdPath = virPCIFile(dev->name, "vpd");
|
|
|
|
if (!virFileExists(vpdPath)) {
|
|
|
|
VIR_INFO("Device VPD file does not exist %s", vpdPath);
|
|
|
|
return false;
|
|
|
|
} else if (!virFileIsRegular(vpdPath)) {
|
|
|
|
VIR_WARN("VPD path does not point to a regular file %s", vpdPath);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* virPCIDeviceGetVPD:
|
|
|
|
* @dev: a PCI device to get a PCI VPD for.
|
|
|
|
*
|
|
|
|
* Obtain a PCI device's Vital Product Data (VPD). VPD is optional in
|
|
|
|
* both PCI Local Bus and PCIe specifications so there is no guarantee it
|
|
|
|
* will be there for a particular device.
|
|
|
|
*
|
|
|
|
* Returns: a pointer to virPCIVPDResource which needs to be freed by the caller
|
|
|
|
* or NULL if getting it failed for some reason (e.g. invalid format, I/O error).
|
|
|
|
*/
|
|
|
|
virPCIVPDResource *
|
|
|
|
virPCIDeviceGetVPD(virPCIDevice *dev)
|
|
|
|
{
|
|
|
|
g_autofree char *vpdPath = NULL;
|
|
|
|
int fd;
|
|
|
|
g_autoptr(virPCIVPDResource) res = NULL;
|
|
|
|
|
|
|
|
vpdPath = virPCIFile(dev->name, "vpd");
|
|
|
|
if (!virPCIDeviceHasVPD(dev)) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, _("Device %s does not have a VPD"),
|
|
|
|
virPCIDeviceGetName(dev));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if ((fd = open(vpdPath, O_RDONLY)) < 0) {
|
|
|
|
virReportSystemError(-fd, _("Failed to open a VPD file '%s'"), vpdPath);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
res = virPCIVPDParse(fd);
|
|
|
|
|
|
|
|
if (VIR_CLOSE(fd) < 0) {
|
|
|
|
virReportSystemError(errno, _("Unable to close the VPD file, fd: %d"), fd);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_steal_pointer(&res);
|
|
|
|
}
|
|
|
|
|
2011-08-16 04:28:43 +00:00
|
|
|
#else
|
2012-03-08 20:41:53 +00:00
|
|
|
static const char *unsupported = N_("not supported on non-linux platforms");
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceAddress *
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIGetDeviceAddressFromSysfsLink(const char *device_link G_GNUC_UNUSED)
|
2017-03-07 19:23:01 +00:00
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2017-03-25 13:12:42 +00:00
|
|
|
return NULL;
|
2017-03-07 19:23:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-08-16 04:28:43 +00:00
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIGetPhysicalFunction(const char *vf_sysfs_path G_GNUC_UNUSED,
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceAddress **pf G_GNUC_UNUSED)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2011-08-16 04:28:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-08-04 15:37:44 +00:00
|
|
|
virPCIGetVirtualFunctionsFull(const char *sysfs_path G_GNUC_UNUSED,
|
|
|
|
virPCIVirtualFunctionList **vfs G_GNUC_UNUSED,
|
2021-12-02 19:13:18 +00:00
|
|
|
const char *pfNetDevName G_GNUC_UNUSED)
|
2011-08-16 04:28:43 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2011-08-16 04:28:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2011-08-16 04:28:48 +00:00
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIIsVirtualFunction(const char *vf_sysfs_device_link G_GNUC_UNUSED)
|
2011-08-16 04:28:48 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2011-08-16 04:28:48 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIGetVirtualFunctionIndex(const char *pf_sysfs_device_link G_GNUC_UNUSED,
|
|
|
|
const char *vf_sysfs_device_link G_GNUC_UNUSED,
|
|
|
|
int *vf_index G_GNUC_UNUSED)
|
2011-08-16 04:28:48 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2011-08-16 04:28:48 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-07-19 06:10:12 +00:00
|
|
|
|
2012-03-08 19:19:36 +00:00
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceAddressGetSysfsFile(virPCIDeviceAddress *dev G_GNUC_UNUSED,
|
2019-10-14 12:45:33 +00:00
|
|
|
char **pci_sysfs_device_link G_GNUC_UNUSED)
|
2012-03-08 19:19:36 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2012-03-08 19:19:36 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2011-08-16 04:28:48 +00:00
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIGetNetName(const char *device_link_sysfs_path G_GNUC_UNUSED,
|
|
|
|
size_t idx G_GNUC_UNUSED,
|
2021-12-02 18:52:48 +00:00
|
|
|
const char *physPortNetDevName G_GNUC_UNUSED,
|
2019-10-14 12:45:33 +00:00
|
|
|
char **netname G_GNUC_UNUSED)
|
2011-08-16 04:28:48 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2011-08-16 04:28:48 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2012-03-06 01:12:23 +00:00
|
|
|
|
|
|
|
int
|
2019-10-14 12:45:33 +00:00
|
|
|
virPCIGetVirtualFunctionInfo(const char *vf_sysfs_device_path G_GNUC_UNUSED,
|
|
|
|
int pfNetDevIdx G_GNUC_UNUSED,
|
|
|
|
char **pfname G_GNUC_UNUSED,
|
|
|
|
int *vf_index G_GNUC_UNUSED)
|
2012-03-06 01:12:23 +00:00
|
|
|
{
|
2012-07-18 10:26:24 +00:00
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
2012-03-06 01:12:23 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2021-10-20 08:30:32 +00:00
|
|
|
|
|
|
|
bool
|
|
|
|
virPCIDeviceHasVPD(virPCIDevice *dev G_GNUC_UNUSED)
|
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
virPCIVPDResource *
|
|
|
|
virPCIDeviceGetVPD(virPCIDevice *dev G_GNUC_UNUSED)
|
|
|
|
{
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _(unsupported));
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-08-16 04:28:43 +00:00
|
|
|
#endif /* __linux__ */
|
2014-05-15 08:04:28 +00:00
|
|
|
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceIsPCIExpress(virPCIDevice *dev)
|
2014-05-15 08:04:28 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int ret = -1;
|
|
|
|
|
2019-08-13 13:17:44 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpen(dev)) < 0)
|
2014-05-15 08:04:28 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (virPCIDeviceInit(dev, fd) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2020-12-08 19:34:19 +00:00
|
|
|
ret = dev->is_pcie;
|
2014-05-15 08:04:28 +00:00
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virPCIDeviceConfigClose(dev, fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceHasPCIExpressLink(virPCIDevice *dev)
|
2014-05-15 08:04:28 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int ret = -1;
|
|
|
|
uint16_t cap, type;
|
|
|
|
|
2019-08-13 13:17:44 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpen(dev)) < 0)
|
2014-05-15 08:04:28 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (virPCIDeviceInit(dev, fd) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2021-01-06 20:42:47 +00:00
|
|
|
if (dev->pcie_cap_pos == 0) {
|
|
|
|
ret = 0;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2014-05-15 08:04:28 +00:00
|
|
|
cap = virPCIDeviceRead16(dev, fd, dev->pcie_cap_pos + PCI_CAP_FLAGS);
|
|
|
|
type = (cap & PCI_EXP_FLAGS_TYPE) >> 4;
|
|
|
|
|
|
|
|
ret = type != PCI_EXP_TYPE_ROOT_INT_EP && type != PCI_EXP_TYPE_ROOT_EC;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virPCIDeviceConfigClose(dev, fd);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceGetLinkCapSta(virPCIDevice *dev,
|
2014-05-15 08:04:28 +00:00
|
|
|
int *cap_port,
|
|
|
|
unsigned int *cap_speed,
|
|
|
|
unsigned int *cap_width,
|
|
|
|
unsigned int *sta_speed,
|
|
|
|
unsigned int *sta_width)
|
|
|
|
{
|
|
|
|
uint32_t t;
|
|
|
|
int fd;
|
|
|
|
int ret = -1;
|
|
|
|
|
2019-08-13 13:17:44 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpen(dev)) < 0)
|
2014-05-15 08:04:28 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (virPCIDeviceInit(dev, fd) < 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
if (!dev->pcie_cap_pos) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
|
|
|
_("pci device %s is not a PCI-Express device"),
|
|
|
|
dev->name);
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
t = virPCIDeviceRead32(dev, fd, dev->pcie_cap_pos + PCI_EXP_LNKCAP);
|
|
|
|
|
|
|
|
*cap_port = t >> 24;
|
|
|
|
*cap_speed = t & PCI_EXP_LNKCAP_SPEED;
|
|
|
|
*cap_width = (t & PCI_EXP_LNKCAP_WIDTH) >> 4;
|
|
|
|
|
|
|
|
t = virPCIDeviceRead16(dev, fd, dev->pcie_cap_pos + PCI_EXP_LNKSTA);
|
|
|
|
|
|
|
|
*sta_speed = t & PCI_EXP_LNKSTA_SPEED;
|
|
|
|
*sta_width = (t & PCI_EXP_LNKSTA_WIDTH) >> 4;
|
|
|
|
ret = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
virPCIDeviceConfigClose(dev, fd);
|
|
|
|
return ret;
|
|
|
|
}
|
2014-07-23 04:38:30 +00:00
|
|
|
|
|
|
|
|
2021-03-11 07:16:13 +00:00
|
|
|
int virPCIGetHeaderType(virPCIDevice *dev, int *hdrType)
|
2016-03-15 11:22:03 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
uint8_t type;
|
|
|
|
|
|
|
|
*hdrType = -1;
|
|
|
|
|
2019-08-13 13:17:44 +00:00
|
|
|
if ((fd = virPCIDeviceConfigOpen(dev)) < 0)
|
2016-03-15 11:22:03 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
type = virPCIDeviceRead8(dev, fd, PCI_HEADER_TYPE);
|
|
|
|
|
|
|
|
virPCIDeviceConfigClose(dev, fd);
|
|
|
|
|
|
|
|
type &= PCI_HEADER_TYPE_MASK;
|
|
|
|
if (type >= VIR_PCI_HEADER_LAST) {
|
|
|
|
virReportError(VIR_ERR_INTERNAL_ERROR,
|
2019-06-21 15:58:28 +00:00
|
|
|
_("Unknown PCI header type '%d' for device '%s'"),
|
|
|
|
type, dev->name);
|
2016-03-15 11:22:03 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*hdrType = type;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-23 04:38:30 +00:00
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIEDeviceInfoFree(virPCIEDeviceInfo *dev)
|
2014-07-23 04:38:30 +00:00
|
|
|
{
|
|
|
|
if (!dev)
|
|
|
|
return;
|
|
|
|
|
2021-02-03 19:32:34 +00:00
|
|
|
g_free(dev->link_cap);
|
|
|
|
g_free(dev->link_sta);
|
|
|
|
g_free(dev);
|
2014-07-23 04:38:30 +00:00
|
|
|
}
|
2018-07-24 15:52:20 +00:00
|
|
|
|
|
|
|
void
|
2021-03-11 07:16:13 +00:00
|
|
|
virPCIDeviceAddressFree(virPCIDeviceAddress *address)
|
2018-07-24 15:52:20 +00:00
|
|
|
{
|
2021-02-03 19:32:34 +00:00
|
|
|
g_free(address);
|
2018-07-24 15:52:20 +00:00
|
|
|
}
|