pve-manager/configs/virtual-function-pinning-helper
Stefan Hanreich 017359f376 configs: add udev helper for pinning virtual function names
This commmit adds a udev rule that triggers for every network device
that gets added. It checks if the network device is a VF and if the
parent device is pinned. If it is pinned, then generate a new name for
the VF which consists of the pinned name of the parent device, as well
as the index of the VF.

It relies on the network device driver exposing the information via
sysfs, which was the case in my tests for mlx5_core, igb and bnxt_en.

Specifically it checks if a device is a virtual function by checking
for the existence of:

  /sys/class/net/<iface>/device/physfn

It then follows that symlink and infers the vf index by looking at the
virtfnX symlinks in the folder above.

Signed-off-by: Stefan Hanreich <s.hanreich@proxmox.com>
Link: https://lore.proxmox.com/20250722145223.351778-2-s.hanreich@proxmox.com
2025-07-22 17:30:47 +02:00

37 lines
1,008 B
Bash

#!/bin/sh
set -eu
DEVICE_SYSFS_PCI_PATH=$(realpath "/sys${DEVPATH}/../..");
if [ ! -L "$DEVICE_SYSFS_PCI_PATH/physfn" ]; then
exit;
fi
PHYSFN_SYSFS_PCI_PATH=$(realpath "${DEVICE_SYSFS_PCI_PATH}/physfn");
PHYSFN_IFACE_NAME=$(ls "${PHYSFN_SYSFS_PCI_PATH}/net")
# interface is not pinned
if [ ! -f "/usr/local/lib/systemd/network/50-pve-${PHYSFN_IFACE_NAME}.link" ]; then
exit;
fi
# pin is not applied - or interface doesn't exist
if ! ip link show "$PHYSFN_IFACE_NAME" > /dev/null 2>&1 ; then
exit;
fi
DEVICE_PCI_ID=$(basename "$DEVICE_SYSFS_PCI_PATH");
for file in $(find "${PHYSFN_SYSFS_PCI_PATH=$}/" -maxdepth 1 -type l -name 'virtfn*' ); do
VF_PCI_ID=$(basename "$(realpath "$file")");
if [ "$DEVICE_PCI_ID" = "$VF_PCI_ID" ]; then
VF_INDEX=$(basename "$file" | grep -Eo '[[:digit:]]+$' -);
echo "${PHYSFN_IFACE_NAME}v${VF_INDEX}";
exit;
fi
done
echo "interface seems to be a VF of ${PHYSFN_IFACE_NAME}, but could not find the VF index" 1>&2;
exit;