Skip to content

Add vfio mode to generate CDI specs for NVIDIA passthrough GPUs #315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/NVIDIA/nvidia-container-toolkit
go 1.23.0

require (
github.com/NVIDIA/go-nvlib v0.7.3
github.com/NVIDIA/go-nvlib v0.7.4-0.20250707141440-532907f49628
github.com/NVIDIA/go-nvml v0.12.9-0
github.com/cyphar/filepath-securejoin v0.4.1
github.com/moby/sys/reexec v0.1.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/NVIDIA/go-nvlib v0.7.3 h1:kXc8PkWUlrwedSpM4fR8xT/DAq1NKy8HqhpgteFcGAw=
github.com/NVIDIA/go-nvlib v0.7.3/go.mod h1:i95Je7GinMy/+BDs++DAdbPmT2TubjNP8i8joC7DD7I=
github.com/NVIDIA/go-nvlib v0.7.4-0.20250707141440-532907f49628 h1:epoT+CVQMISAldX5fx78gjJs23Kx17B/6dOwcXvmPaE=
github.com/NVIDIA/go-nvlib v0.7.4-0.20250707141440-532907f49628/go.mod h1:i95Je7GinMy/+BDs++DAdbPmT2TubjNP8i8joC7DD7I=
github.com/NVIDIA/go-nvml v0.12.9-0 h1:e344UK8ZkeMeeLkdQtRhmXRxNf+u532LDZPGMtkdus0=
github.com/NVIDIA/go-nvml v0.12.9-0/go.mod h1:+KNA7c7gIBH7SKSJ1ntlwkfN80zdx8ovl4hrK3LmPt4=
github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM=
Expand Down
122 changes: 122 additions & 0 deletions pkg/nvcdi/lib-vfio.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/**
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/

package nvcdi

import (
"fmt"
"path/filepath"
"strconv"

"tags.cncf.io/container-device-interface/pkg/cdi"
"tags.cncf.io/container-device-interface/specs-go"
)

type vfiolib nvcdilib

type vfioDevice struct {
index int
group int
devRoot string
}

var _ deviceSpecGeneratorFactory = (*vfiolib)(nil)

func (l *vfiolib) DeviceSpecGenerators(ids ...string) (DeviceSpecGenerator, error) {
vfioDevices, err := l.getVfioDevices(ids...)
if err != nil {
return nil, err
}
var deviceSpecGenerators DeviceSpecGenerators
for _, vfioDevice := range vfioDevices {
deviceSpecGenerators = append(deviceSpecGenerators, vfioDevice)
}

return deviceSpecGenerators, nil
}

// GetDeviceSpecs returns the CDI device specs for a vfio device.
func (l *vfioDevice) GetDeviceSpecs() ([]specs.Device, error) {
path := fmt.Sprintf("/dev/vfio/%d", l.group)
deviceSpec := specs.Device{
Name: fmt.Sprintf("%d", l.index),
ContainerEdits: specs.ContainerEdits{
DeviceNodes: []*specs.DeviceNode{
{
Path: path,
HostPath: filepath.Join(l.devRoot, path),
Copy link
Preview

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When path starts with a slash, filepath.Join(l.devRoot, path) will ignore l.devRoot. You may need to strip the leading slash or construct the host path as filepath.Join(l.devRoot, path[1:]).

Suggested change
HostPath: filepath.Join(l.devRoot, path),
HostPath: filepath.Join(l.devRoot, path[1:]),

Copilot uses AI. Check for mistakes.

},
},
},
}
return []specs.Device{deviceSpec}, nil
}

// GetCommonEdits returns common edits for ALL devices.
// Note, currently there are no common edits.
func (l *vfiolib) GetCommonEdits() (*cdi.ContainerEdits, error) {
e := cdi.ContainerEdits{
ContainerEdits: &specs.ContainerEdits{
DeviceNodes: []*specs.DeviceNode{
{
Path: "/dev/vfio/vfio",
HostPath: filepath.Join(l.devRoot, "/dev/vfio/vfio"),
Copy link
Preview

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same issue here: joining an absolute path to l.devRoot will ignore the root override. Consider constructing this path without the leading slash or using path.Join on relative segments.

Suggested change
HostPath: filepath.Join(l.devRoot, "/dev/vfio/vfio"),
HostPath: filepath.Join(l.devRoot, "dev/vfio/vfio"),

Copilot uses AI. Check for mistakes.

},
},
},
}
return &e, nil
}

func (l *vfiolib) getVfioDevices(ids ...string) ([]*vfioDevice, error) {
var vfioDevices []*vfioDevice
for _, id := range ids {
if id == "all" {
return l.getAllVfioDevices()
}
index, err := strconv.ParseInt(id, 10, 32)
if err != nil {
return nil, fmt.Errorf("invalid channel ID %v: %w", id, err)
Copy link
Preview

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use %q instead of %v to quote the invalid channel ID in the error message for better readability: fmt.Errorf("invalid channel ID %q: %w", id, err).

Suggested change
return nil, fmt.Errorf("invalid channel ID %v: %w", id, err)
return nil, fmt.Errorf("invalid channel ID %q: %w", id, err)

Copilot uses AI. Check for mistakes.

}
i := int(index)
dev, err := l.nvpcilib.GetGPUByIndex(i)
if err != nil {
return nil, fmt.Errorf("failed to get device: %w", err)
}
vfioDevices = append(vfioDevices, &vfioDevice{index: i, group: dev.IommuGroup, devRoot: l.devRoot})
}

return vfioDevices, nil
}

func (l *vfiolib) getAllVfioDevices() ([]*vfioDevice, error) {
devices, err := l.nvpcilib.GetGPUs()
if err != nil {
return nil, fmt.Errorf("failed getting NVIDIA GPUs: %v", err)
}

var vfioDevices []*vfioDevice
for i, dev := range devices {
if dev.Driver != "vfio-pci" {
continue
}
l.logger.Debugf("Found NVIDIA device: address=%s, driver=%s, iommu_group=%d, deviceId=%x",
dev.Address, dev.Driver, dev.IommuGroup, dev.Device)
vfioDevices = append(vfioDevices, &vfioDevice{index: i, group: dev.IommuGroup, devRoot: l.devRoot})
}
return vfioDevices, nil
}
123 changes: 123 additions & 0 deletions pkg/nvcdi/lib-vfio_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/**
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
**/

package nvcdi

import (
"bytes"
"testing"

"github.com/NVIDIA/go-nvlib/pkg/nvpci"
"github.com/stretchr/testify/require"
)

func TestModeVfio(t *testing.T) {
testCases := []struct {
description string
pcilib *nvpci.InterfaceMock
ids []string
expectedError error
expectedSpec string
}{
{
description: "get all specs single device",
pcilib: &nvpci.InterfaceMock{
GetGPUsFunc: func() ([]*nvpci.NvidiaPCIDevice, error) {
devices := []*nvpci.NvidiaPCIDevice{
{
Driver: "vfio-pci",
IommuGroup: 5,
},
}
return devices, nil
},
},
expectedSpec: `---
cdiVersion: 0.5.0
kind: nvidia.com/pgpu
devices:
- name: "0"
containerEdits:
deviceNodes:
- path: /dev/vfio/5
hostPath: /dev/vfio/5
containerEdits:
env:
- NVIDIA_VISIBLE_DEVICES=void
deviceNodes:
- path: /dev/vfio/vfio
hostPath: /dev/vfio/vfio
`,
},
{
description: "get single device spec by index",
pcilib: &nvpci.InterfaceMock{
GetGPUByIndexFunc: func(n int) (*nvpci.NvidiaPCIDevice, error) {
devices := []*nvpci.NvidiaPCIDevice{
{
Driver: "vfio-pci",
IommuGroup: 45,
},
{
Driver: "vfio-pci",
IommuGroup: 5,
},
}
return devices[n], nil
},
},
ids: []string{"1"},
expectedSpec: `---
cdiVersion: 0.5.0
kind: nvidia.com/pgpu
devices:
- name: "1"
containerEdits:
deviceNodes:
- path: /dev/vfio/5
hostPath: /dev/vfio/5
containerEdits:
env:
- NVIDIA_VISIBLE_DEVICES=void
deviceNodes:
- path: /dev/vfio/vfio
hostPath: /dev/vfio/vfio
`,
},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
lib, err := New(
WithMode(ModeVfio),
WithPCILib(tc.pcilib),
)
require.NoError(t, err)

spec, err := lib.GetSpec(tc.ids...)
require.EqualValues(t, tc.expectedError, err)

var output bytes.Buffer

_, err = spec.WriteTo(&output)
require.NoError(t, err)

require.Equal(t, tc.expectedSpec, output.String())
})
}

}
11 changes: 11 additions & 0 deletions pkg/nvcdi/lib.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (

"github.com/NVIDIA/go-nvlib/pkg/nvlib/device"
"github.com/NVIDIA/go-nvlib/pkg/nvlib/info"
"github.com/NVIDIA/go-nvlib/pkg/nvpci"
"github.com/NVIDIA/go-nvml/pkg/nvml"

"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
Expand Down Expand Up @@ -54,6 +55,8 @@ type nvcdilib struct {
driver *root.Driver
infolib info.Interface

nvpcilib nvpci.Interface

mergedDeviceOptions []transform.MergedDeviceOption

featureFlags map[FeatureFlag]bool
Expand Down Expand Up @@ -151,6 +154,14 @@ func New(opts ...Option) (Interface, error) {
l.class = classImexChannel
}
factory = (*imexlib)(l)
case ModeVfio:
if l.class == "" {
l.class = "pgpu"
}
if l.nvpcilib == nil {
l.nvpcilib = nvpci.New()
}
factory = (*vfiolib)(l)
default:
return nil, fmt.Errorf("unknown mode %q", l.mode)
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/nvcdi/mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ const (
ModeCSV = Mode("csv")
// ModeImex configures the CDI spec generated to generate a spec for the available IMEX channels.
ModeImex = Mode("imex")
// ModeVfio configures the CDI spec generator to generate a VFIO spec.
ModeVfio = Mode("vfio")
)

type modeConstraint interface {
Expand All @@ -66,6 +68,7 @@ func getModes() modes {
ModeGds,
ModeMofed,
ModeCSV,
ModeVfio,
}
lookup := make(map[Mode]bool)

Expand Down
8 changes: 8 additions & 0 deletions pkg/nvcdi/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package nvcdi
import (
"github.com/NVIDIA/go-nvlib/pkg/nvlib/device"
"github.com/NVIDIA/go-nvlib/pkg/nvlib/info"
"github.com/NVIDIA/go-nvlib/pkg/nvpci"
"github.com/NVIDIA/go-nvml/pkg/nvml"

"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
Expand All @@ -43,6 +44,13 @@ func WithInfoLib(infolib info.Interface) Option {
}
}

// WithPCILib sets the pci library to be used for CDI spec generation.
Copy link
Preview

Copilot AI Jul 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment should use the uppercase acronym PCI for consistency with the function name (WithPCILib).

Suggested change
// WithPCILib sets the pci library to be used for CDI spec generation.
// WithPCILib sets the PCI library to be used for CDI spec generation.

Copilot uses AI. Check for mistakes.

func WithPCILib(pcilib nvpci.Interface) Option {
return func(l *nvcdilib) {
l.nvpcilib = pcilib
}
}

// WithDeviceNamers sets the device namer for the library
func WithDeviceNamers(namers ...DeviceNamer) Option {
return func(l *nvcdilib) {
Expand Down
2 changes: 2 additions & 0 deletions vendor/github.com/NVIDIA/go-nvlib/pkg/nvpci/nvpci.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading