Skip to content

Commit ef4fb27

Browse files
authored
Merge pull request #155 from rajatchopra/iommufd
cdi spec file should print devices with multiple name keys
2 parents fb3d3c3 + 3b7670f commit ef4fb27

File tree

9 files changed

+199
-12
lines changed

9 files changed

+199
-12
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ go 1.24.0
55
toolchain go1.24.9
66

77
require (
8-
github.com/NVIDIA/go-nvlib v0.8.1
8+
github.com/NVIDIA/go-nvlib v0.9.0
99
github.com/NVIDIA/nvidia-container-toolkit v1.18.0
1010
github.com/opencontainers/image-spec v1.1.1
1111
github.com/pelletier/go-toml v1.9.5

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
22
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
3-
github.com/NVIDIA/go-nvlib v0.8.1 h1:OPEHVvn3zcV5OXB68A7WRpeCnYMRSPl7LdeJH/d3gZI=
4-
github.com/NVIDIA/go-nvlib v0.8.1/go.mod h1:7mzx9FSdO9fXWP9NKuZmWkCwhkEcSWQFe2tmFwtLb9c=
3+
github.com/NVIDIA/go-nvlib v0.8.2-0.20251124190111-9a6788d93d8c h1:1w55FLYiTkHL1q1QjKDozZPu5Ca8lfyC3GHV+jMTSz8=
4+
github.com/NVIDIA/go-nvlib v0.8.2-0.20251124190111-9a6788d93d8c/go.mod h1:7mzx9FSdO9fXWP9NKuZmWkCwhkEcSWQFe2tmFwtLb9c=
5+
github.com/NVIDIA/go-nvlib v0.9.0 h1:GKLIvLJ0uhCtTLLZp2Q8QIDRxOYH45MM4Y5OO3U5Rho=
6+
github.com/NVIDIA/go-nvlib v0.9.0/go.mod h1:7mzx9FSdO9fXWP9NKuZmWkCwhkEcSWQFe2tmFwtLb9c=
57
github.com/NVIDIA/go-nvml v0.13.0-1 h1:OLX8Jq3dONuPOQPC7rndB6+iDmDakw0XTYgzMxObkEw=
68
github.com/NVIDIA/go-nvml v0.13.0-1/go.mod h1:+KNA7c7gIBH7SKSJ1ntlwkfN80zdx8ovl4hrK3LmPt4=
79
github.com/NVIDIA/nvidia-container-toolkit v1.18.0 h1:bXoKq9C1WHU5fF6VqXvX3RkMzpp4ihTUgBPrh66vTf0=

internal/cdi/lib-vfio.go

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,12 @@ import (
2929

3030
type vfiolib nvcdilib
3131

32+
const (
33+
VfioPath = "/dev/vfio"
34+
VfioDevicesPath = "/dev/vfio/devices"
35+
VfioDriver = "vfio-pci"
36+
)
37+
3238
var _ nvcdi.Interface = (*vfiolib)(nil)
3339

3440
// GetSpec returns the complete CDI spec
@@ -60,20 +66,49 @@ func (l *vfiolib) GetAllDeviceSpecs() ([]specs.Device, error) {
6066
return nil, fmt.Errorf("failed getting NVIDIA GPUs: %w", err)
6167
}
6268

69+
path := VfioPath
70+
devName := ""
71+
6372
for idx, dev := range devices {
64-
if dev.Driver == "vfio-pci" {
73+
if dev.Driver == VfioDriver {
6574
klog.Infof("Found NVIDIA device: address=%s, driver=%s, iommu_group=%d, deviceId=%x",
6675
dev.Address, dev.Driver, dev.IommuGroup, dev.Device)
67-
deviceSpecs = append(deviceSpecs, specs.Device{
68-
Name: fmt.Sprintf("%d", idx),
69-
ContainerEdits: specs.ContainerEdits{
70-
DeviceNodes: []*specs.DeviceNode{
71-
{
72-
Path: fmt.Sprintf("/dev/vfio/%d", dev.IommuGroup),
73-
},
76+
77+
if dev.IommuFD != "" {
78+
path = VfioDevicesPath
79+
devName = dev.IommuFD
80+
} else {
81+
devName = fmt.Sprintf("%d", dev.IommuGroup)
82+
}
83+
cedits := specs.ContainerEdits{
84+
DeviceNodes: []*specs.DeviceNode{
85+
{
86+
Path: fmt.Sprintf("%s/%s", path, devName),
7487
},
7588
},
89+
}
90+
// Add the same device multiple times with keys for meant for
91+
// various use cases:
92+
// key=idx: use case where cdi annotations are manually put
93+
// on pod spec e.g. 0,1,2 etc
94+
// key=IommuGroup e.g. 65 for /dev/vfio/65 in non-iommufd setup
95+
// and legacy device plugin case
96+
// key=IommuFD e.g. vfio0 for /dev/vfio/devices/vfio0 for
97+
// iommufd support
98+
deviceSpecs = append(deviceSpecs, specs.Device{
99+
Name: fmt.Sprintf("%d", idx),
100+
ContainerEdits: cedits,
101+
})
102+
deviceSpecs = append(deviceSpecs, specs.Device{
103+
Name: fmt.Sprintf("%d", dev.IommuGroup),
104+
ContainerEdits: cedits,
76105
})
106+
if dev.IommuFD != "" {
107+
deviceSpecs = append(deviceSpecs, specs.Device{
108+
Name: fmt.Sprintf("%s", dev.IommuFD),
109+
ContainerEdits: cedits,
110+
})
111+
}
77112
}
78113
}
79114

internal/cdi/lib-vfio_test.go

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,57 @@ func TestGetAllDeviceSpecs(t *testing.T) {
6161
},
6262
expectedDeviceSpecs: nil,
6363
},
64+
{
65+
description: "one IommuFD NVIDIA device, bound to vfio-pci",
66+
lib: &vfiolib{
67+
nvpcilib: &nvpciInterfaceMock{
68+
GetGPUsFunc: func() ([]*nvpci.NvidiaPCIDevice, error) {
69+
devices := []*nvpci.NvidiaPCIDevice{
70+
{
71+
Address: "000:3B:00.0",
72+
Device: 0x2331,
73+
IommuGroup: 60,
74+
IommuFD: "vfio0",
75+
Driver: "vfio-pci",
76+
},
77+
}
78+
return devices, nil
79+
},
80+
},
81+
},
82+
expectedDeviceSpecs: []specs.Device{
83+
{
84+
Name: "0",
85+
ContainerEdits: specs.ContainerEdits{
86+
DeviceNodes: []*specs.DeviceNode{
87+
{
88+
Path: "/dev/vfio/devices/vfio0",
89+
},
90+
},
91+
},
92+
},
93+
{
94+
Name: "60",
95+
ContainerEdits: specs.ContainerEdits{
96+
DeviceNodes: []*specs.DeviceNode{
97+
{
98+
Path: "/dev/vfio/devices/vfio0",
99+
},
100+
},
101+
},
102+
},
103+
{
104+
Name: "vfio0",
105+
ContainerEdits: specs.ContainerEdits{
106+
DeviceNodes: []*specs.DeviceNode{
107+
{
108+
Path: "/dev/vfio/devices/vfio0",
109+
},
110+
},
111+
},
112+
},
113+
},
114+
},
64115
{
65116
description: "one NVIDIA device, bound to vfio-pci",
66117
lib: &vfiolib{
@@ -89,6 +140,16 @@ func TestGetAllDeviceSpecs(t *testing.T) {
89140
},
90141
},
91142
},
143+
{
144+
Name: "60",
145+
ContainerEdits: specs.ContainerEdits{
146+
DeviceNodes: []*specs.DeviceNode{
147+
{
148+
Path: "/dev/vfio/60",
149+
},
150+
},
151+
},
152+
},
92153
},
93154
},
94155
{
@@ -125,6 +186,16 @@ func TestGetAllDeviceSpecs(t *testing.T) {
125186
},
126187
},
127188
},
189+
{
190+
Name: "60",
191+
ContainerEdits: specs.ContainerEdits{
192+
DeviceNodes: []*specs.DeviceNode{
193+
{
194+
Path: "/dev/vfio/60",
195+
},
196+
},
197+
},
198+
},
128199
},
129200
},
130201
{
@@ -161,6 +232,16 @@ func TestGetAllDeviceSpecs(t *testing.T) {
161232
},
162233
},
163234
},
235+
{
236+
Name: "60",
237+
ContainerEdits: specs.ContainerEdits{
238+
DeviceNodes: []*specs.DeviceNode{
239+
{
240+
Path: "/dev/vfio/60",
241+
},
242+
},
243+
},
244+
},
164245
{
165246
Name: "1",
166247
ContainerEdits: specs.ContainerEdits{
@@ -171,6 +252,16 @@ func TestGetAllDeviceSpecs(t *testing.T) {
171252
},
172253
},
173254
},
255+
{
256+
Name: "90",
257+
ContainerEdits: specs.ContainerEdits{
258+
DeviceNodes: []*specs.DeviceNode{
259+
{
260+
Path: "/dev/vfio/90",
261+
},
262+
},
263+
},
264+
},
174265
},
175266
},
176267
}

vendor/github.com/NVIDIA/go-nvlib/pkg/nvlib/device/device.go

Lines changed: 27 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/NVIDIA/go-nvlib/pkg/nvlib/info/property-extractor.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/NVIDIA/go-nvlib/pkg/nvpci/mock.go

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/NVIDIA/go-nvlib/pkg/nvpci/nvpci.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/modules.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
## explicit; go 1.18
33
github.com/BurntSushi/toml
44
github.com/BurntSushi/toml/internal
5-
# github.com/NVIDIA/go-nvlib v0.8.1
5+
# github.com/NVIDIA/go-nvlib v0.9.0
66
## explicit; go 1.20
77
github.com/NVIDIA/go-nvlib/pkg/nvlib/device
88
github.com/NVIDIA/go-nvlib/pkg/nvlib/info

0 commit comments

Comments
 (0)