-
Notifications
You must be signed in to change notification settings - Fork 430
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
elezar
wants to merge
1
commit into
NVIDIA:main
Choose a base branch
from
elezar:vfio-cdi-mode
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+267
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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), | ||
elezar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| 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"), | ||
elezar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| 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) | ||
elezar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| 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 | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()) | ||
| }) | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.