Skip to content
This repository was archived by the owner on May 6, 2026. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions pkg/inventory/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment thread
aojea marked this conversation as resolved.
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}
Comment thread
michaelasp marked this conversation as resolved.
}
} else {
klog.Infof("could not get pci address : %v", err)
klog.Infof("could not get pci root : %v", err)
}

entry, err := ids(ifName, path)
Expand Down
32 changes: 32 additions & 0 deletions pkg/inventory/sysfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Loading