Skip to content
This repository was archived by the owner on Dec 9, 2025. It is now read-only.

Commit c8644dd

Browse files
committed
feat: add pcieroot as a standard value
kubernetes/enhancements#5316 provides a standardized device attribute for pcieroot. Use that definition.
1 parent 1627d9e commit c8644dd

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

pkg/inventory/db.go

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -294,22 +294,16 @@ func (db *DB) netdevToDRAdev(link netlink.Link) (*resourceapi.Device, error) {
294294
func addPCIAttributes(device *resourceapi.BasicDevice, ifName string, path string) {
295295
device.Attributes["dra.net/virtual"] = resourceapi.DeviceAttribute{BoolValue: ptr.To(false)}
296296

297-
address, err := bdfAddress(ifName, path)
297+
root, err := bdfRoot(ifName, path)
298298
if err == nil {
299-
if address.domain != "" {
300-
device.Attributes["dra.net/pciAddressDomain"] = resourceapi.DeviceAttribute{StringValue: &address.domain}
301-
}
302-
if address.bus != "" {
303-
device.Attributes["dra.net/pciAddressBus"] = resourceapi.DeviceAttribute{StringValue: &address.bus}
304-
}
305-
if address.device != "" {
306-
device.Attributes["dra.net/pciAddressDevice"] = resourceapi.DeviceAttribute{StringValue: &address.device}
307-
}
308-
if address.function != "" {
309-
device.Attributes["dra.net/pciAddressFunction"] = resourceapi.DeviceAttribute{StringValue: &address.function}
299+
if root.domain != "" && root.bus != "" {
300+
// standardized attribute for all dra drivers
301+
// ref: https://github.com/kubernetes/enhancements/pull/5316
302+
pcieRoot := fmt.Sprintf("pci%s:%s", root.domain, root.bus)
303+
device.Attributes["resource.kubernetes.io/pcieRoot"] = resourceapi.DeviceAttribute{StringValue: &pcieRoot}
310304
}
311305
} else {
312-
klog.Infof("could not get pci address : %v", err)
306+
klog.Infof("could not get pci root : %v", err)
313307
}
314308

315309
entry, err := ids(ifName, path)

pkg/inventory/sysfs.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,16 @@ type pciAddress struct {
124124
function string
125125
}
126126

127+
// The PCI root is the root PCI device, derived from the
128+
// pciAddress of a device. Spec is defined from the DRA KEP.
129+
// https://github.com/kubernetes/enhancements/pull/5316
130+
type pciRoot struct {
131+
domain string
132+
// The root may have a different host bus than the PCI device.
133+
// e.g https://uefi.org/specs/UEFI/2.10/14_Protocols_PCI_Bus_Support.html#server-system-with-four-pci-root-bridges
134+
bus string
135+
}
136+
127137
func bdfAddress(ifName string, path string) (*pciAddress, error) {
128138
address := &pciAddress{}
129139
// https://docs.kernel.org/PCI/sysfs-pci.html
@@ -164,6 +174,28 @@ func bdfAddress(ifName string, path string) (*pciAddress, error) {
164174
return address, nil
165175
}
166176

177+
// Obtain the root of the pci device for the interface.
178+
// TODO(@michaelasp): Change this to use k8s helper function when available for consistency with other DRA solutions.
179+
func bdfRoot(ifName, path string) (*pciRoot, error) {
180+
root := &pciRoot{}
181+
182+
sysfsPath := realpath(ifName, path)
183+
bfd := strings.Split(sysfsPath, "/")
184+
if len(bfd) < 5 {
185+
return nil, fmt.Errorf("could not find corresponding PCI address: %v", bfd)
186+
}
187+
klog.V(4).Infof("pci root: %s", bfd[3])
188+
rootString := strings.Split(bfd[3], ":")
189+
switch len(rootString) {
190+
case 2:
191+
root.domain = strings.TrimPrefix(rootString[0], "pci")
192+
root.bus = rootString[1]
193+
default:
194+
return nil, fmt.Errorf("could not find corresponding PCI root: %v", rootString)
195+
}
196+
return root, nil
197+
}
198+
167199
func ids(ifName string, path string) (*pcidb.Entry, error) {
168200
// PCI data
169201
var device, subsystemVendor, subsystemDevice []byte

0 commit comments

Comments
 (0)