mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2024-12-22 13:45:38 +00:00
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>
This commit is contained in:
parent
97ebb98245
commit
5b1c525b1f
@ -2467,9 +2467,9 @@ virPCIDeviceAddressGetSysfsFile(virPCIDeviceAddressPtr addr,
|
||||
* virPCIGetNetName:
|
||||
* @device_link_sysfs_path: sysfs path to the PCI device
|
||||
* @idx: used to choose which netdev when there are several
|
||||
* (ignored if physPortID is set)
|
||||
* (ignored if physPortID is set or physPortName is available)
|
||||
* @physPortID: match this string in the netdev's phys_port_id
|
||||
* (or NULL to ignore and use idx instead)
|
||||
* (or NULL to ignore and use phys_port_name or idx instead)
|
||||
* @netname: used to return the name of the netdev
|
||||
* (set to NULL (but returns success) if there is no netdev)
|
||||
*
|
||||
@ -2501,6 +2501,14 @@ virPCIGetNetName(const char *device_link_sysfs_path,
|
||||
}
|
||||
|
||||
while (virDirRead(dir, &entry, pcidev_sysfs_net_path) > 0) {
|
||||
/* 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);
|
||||
|
||||
/* if the caller sent a physPortID, compare it to the
|
||||
* physportID of this netdev. If not, look for entry[idx].
|
||||
*/
|
||||
@ -2511,33 +2519,49 @@ virPCIGetNetName(const char *device_link_sysfs_path,
|
||||
return -1;
|
||||
|
||||
/* if this one doesn't match, keep looking */
|
||||
if (STRNEQ_NULLABLE(physPortID, thisPhysPortID)) {
|
||||
/* 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);
|
||||
if (STRNEQ_NULLABLE(physPortID, thisPhysPortID))
|
||||
continue;
|
||||
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if (i++ < idx)
|
||||
continue;
|
||||
/* 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;
|
||||
}
|
||||
}
|
||||
|
||||
*netname = g_strdup(entry->d_name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!physPortID)
|
||||
return 0;
|
||||
|
||||
if (firstEntryName) {
|
||||
/* we didn't match the provided phys_port_id, but this
|
||||
* is probably because phys_port_id isn't implemented
|
||||
* for this NIC driver, so just return the first
|
||||
/* 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
|
||||
* (probably only) netname we found.
|
||||
*/
|
||||
*netname = g_steal_pointer(&firstEntryName);
|
||||
@ -2545,9 +2569,8 @@ virPCIGetNetName(const char *device_link_sysfs_path,
|
||||
}
|
||||
|
||||
virReportError(VIR_ERR_INTERNAL_ERROR,
|
||||
_("Could not find network device with "
|
||||
"phys_port_id '%s' under PCI device at %s"),
|
||||
physPortID, device_link_sysfs_path);
|
||||
_("Could not find any network device under PCI device at %s"),
|
||||
device_link_sysfs_path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
@ -55,6 +55,11 @@ struct _virZPCIDeviceAddress {
|
||||
|
||||
#define VIR_PCI_DEVICE_ADDRESS_FMT "%04x:%02x:%02x.%d"
|
||||
|
||||
/* Represents format of PF's phys_port_name in switchdev mode:
|
||||
* 'p%u' or 'p%us%u'. New line checked since value is readed from sysfs file.
|
||||
*/
|
||||
#define VIR_PF_PHYS_PORT_NAME_REGEX "(p[0-9]+$)|(p[0-9]+s[0-9]+$)"
|
||||
|
||||
struct _virPCIDeviceAddress {
|
||||
unsigned int domain;
|
||||
unsigned int bus;
|
||||
|
Loading…
Reference in New Issue
Block a user