From c8644dd0eb5e1385ee32029bfa58327d4e01460a Mon Sep 17 00:00:00 2001 From: Michael Aspinwall Date: Thu, 12 Jun 2025 23:41:18 +0000 Subject: [PATCH] feat: add pcieroot as a standard value https://github.com/kubernetes/enhancements/pull/5316 provides a standardized device attribute for pcieroot. Use that definition. --- pkg/inventory/db.go | 20 +++++++------------- pkg/inventory/sysfs.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 13 deletions(-) diff --git a/pkg/inventory/db.go b/pkg/inventory/db.go index 064246b6..4c7c3cad 100644 --- a/pkg/inventory/db.go +++ b/pkg/inventory/db.go @@ -294,22 +294,16 @@ func (db *DB) netdevToDRAdev(link netlink.Link) (*resourceapi.Device, error) { func addPCIAttributes(device *resourceapi.BasicDevice, ifName string, path string) { device.Attributes["dra.net/virtual"] = resourceapi.DeviceAttribute{BoolValue: ptr.To(false)} - address, err := bdfAddress(ifName, path) + root, err := bdfRoot(ifName, path) if err == nil { - if address.domain != "" { - device.Attributes["dra.net/pciAddressDomain"] = resourceapi.DeviceAttribute{StringValue: &address.domain} - } - if address.bus != "" { - device.Attributes["dra.net/pciAddressBus"] = resourceapi.DeviceAttribute{StringValue: &address.bus} - } - if address.device != "" { - device.Attributes["dra.net/pciAddressDevice"] = resourceapi.DeviceAttribute{StringValue: &address.device} - } - if address.function != "" { - device.Attributes["dra.net/pciAddressFunction"] = resourceapi.DeviceAttribute{StringValue: &address.function} + if root.domain != "" && root.bus != "" { + // standardized attribute for all dra drivers + // ref: https://github.com/kubernetes/enhancements/pull/5316 + pcieRoot := fmt.Sprintf("pci%s:%s", root.domain, root.bus) + device.Attributes["resource.kubernetes.io/pcieRoot"] = resourceapi.DeviceAttribute{StringValue: &pcieRoot} } } else { - klog.Infof("could not get pci address : %v", err) + klog.Infof("could not get pci root : %v", err) } entry, err := ids(ifName, path) diff --git a/pkg/inventory/sysfs.go b/pkg/inventory/sysfs.go index 0eee007e..1bacecdc 100644 --- a/pkg/inventory/sysfs.go +++ b/pkg/inventory/sysfs.go @@ -124,6 +124,16 @@ type pciAddress struct { function string } +// The PCI root is the root PCI device, derived from the +// pciAddress of a device. Spec is defined from the DRA KEP. +// https://github.com/kubernetes/enhancements/pull/5316 +type pciRoot struct { + domain string + // The root may have a different host bus than the PCI device. + // e.g https://uefi.org/specs/UEFI/2.10/14_Protocols_PCI_Bus_Support.html#server-system-with-four-pci-root-bridges + bus string +} + func bdfAddress(ifName string, path string) (*pciAddress, error) { address := &pciAddress{} // https://docs.kernel.org/PCI/sysfs-pci.html @@ -164,6 +174,28 @@ func bdfAddress(ifName string, path string) (*pciAddress, error) { return address, nil } +// Obtain the root of the pci device for the interface. +// TODO(@michaelasp): Change this to use k8s helper function when available for consistency with other DRA solutions. +func bdfRoot(ifName, path string) (*pciRoot, error) { + root := &pciRoot{} + + sysfsPath := realpath(ifName, path) + bfd := strings.Split(sysfsPath, "/") + if len(bfd) < 5 { + return nil, fmt.Errorf("could not find corresponding PCI address: %v", bfd) + } + klog.V(4).Infof("pci root: %s", bfd[3]) + rootString := strings.Split(bfd[3], ":") + switch len(rootString) { + case 2: + root.domain = strings.TrimPrefix(rootString[0], "pci") + root.bus = rootString[1] + default: + return nil, fmt.Errorf("could not find corresponding PCI root: %v", rootString) + } + return root, nil +} + func ids(ifName string, path string) (*pcidb.Entry, error) { // PCI data var device, subsystemVendor, subsystemDevice []byte