|
| 1 | +/** |
| 2 | +# Copyright 2024 NVIDIA CORPORATION |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +**/ |
| 16 | + |
| 17 | +package nvcdi |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "path/filepath" |
| 22 | + "strconv" |
| 23 | + "strings" |
| 24 | + |
| 25 | + "tags.cncf.io/container-device-interface/pkg/cdi" |
| 26 | + "tags.cncf.io/container-device-interface/specs-go" |
| 27 | + |
| 28 | + "github.com/NVIDIA/go-nvlib/pkg/nvlib/device" |
| 29 | + |
| 30 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/discover" |
| 31 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/edits" |
| 32 | + "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec" |
| 33 | +) |
| 34 | + |
| 35 | +type imexlib nvcdilib |
| 36 | + |
| 37 | +var _ Interface = (*imexlib)(nil) |
| 38 | + |
| 39 | +const ( |
| 40 | + classImexChannel = "imex-channel" |
| 41 | +) |
| 42 | + |
| 43 | +// GetSpec should not be called for imexlib. |
| 44 | +func (l *imexlib) GetSpec() (spec.Interface, error) { |
| 45 | + return nil, fmt.Errorf("unexpected call to imexlib.GetSpec()") |
| 46 | +} |
| 47 | + |
| 48 | +// GetAllDeviceSpecs returns the device specs for all available devices. |
| 49 | +func (l *imexlib) GetAllDeviceSpecs() ([]specs.Device, error) { |
| 50 | + channelsDiscoverer := discover.NewCharDeviceDiscoverer( |
| 51 | + l.logger, |
| 52 | + l.devRoot, |
| 53 | + []string{"/dev/nvidia-caps-imex-channels/channel*"}, |
| 54 | + ) |
| 55 | + |
| 56 | + channels, err := channelsDiscoverer.Devices() |
| 57 | + if err != nil { |
| 58 | + return nil, err |
| 59 | + } |
| 60 | + |
| 61 | + var channelIDs []string |
| 62 | + for _, channel := range channels { |
| 63 | + channelIDs = append(channelIDs, filepath.Base(channel.Path)) |
| 64 | + } |
| 65 | + |
| 66 | + return l.GetDeviceSpecsByID(channelIDs...) |
| 67 | +} |
| 68 | + |
| 69 | +// GetCommonEdits returns an empty set of edits for IMEX devices. |
| 70 | +func (l *imexlib) GetCommonEdits() (*cdi.ContainerEdits, error) { |
| 71 | + return edits.FromDiscoverer(discover.None{}) |
| 72 | +} |
| 73 | + |
| 74 | +// GetDeviceSpecsByID returns the CDI device specs for the IMEX channels specified. |
| 75 | +func (l *imexlib) GetDeviceSpecsByID(ids ...string) ([]specs.Device, error) { |
| 76 | + var deviceSpecs []specs.Device |
| 77 | + for _, id := range ids { |
| 78 | + trimmed := strings.TrimPrefix(id, "channel") |
| 79 | + _, err := strconv.ParseUint(trimmed, 10, 64) |
| 80 | + if err != nil { |
| 81 | + return nil, fmt.Errorf("invalid channel ID %v: %w", id, err) |
| 82 | + } |
| 83 | + path := "/dev/nvidia-caps-imex-channels/channel" + trimmed |
| 84 | + deviceSpec := specs.Device{ |
| 85 | + Name: trimmed, |
| 86 | + ContainerEdits: specs.ContainerEdits{ |
| 87 | + DeviceNodes: []*specs.DeviceNode{ |
| 88 | + { |
| 89 | + Path: path, |
| 90 | + HostPath: filepath.Join(l.devRoot, path), |
| 91 | + }, |
| 92 | + }, |
| 93 | + }, |
| 94 | + } |
| 95 | + deviceSpecs = append(deviceSpecs, deviceSpec) |
| 96 | + } |
| 97 | + return deviceSpecs, nil |
| 98 | +} |
| 99 | + |
| 100 | +// GetGPUDeviceEdits is unsupported for the imexlib specs |
| 101 | +func (l *imexlib) GetGPUDeviceEdits(device.Device) (*cdi.ContainerEdits, error) { |
| 102 | + return nil, fmt.Errorf("GetGPUDeviceEdits is not supported") |
| 103 | +} |
| 104 | + |
| 105 | +// GetGPUDeviceSpecs is unsupported for the imexlib specs |
| 106 | +func (l *imexlib) GetGPUDeviceSpecs(int, device.Device) ([]specs.Device, error) { |
| 107 | + return nil, fmt.Errorf("GetGPUDeviceSpecs is not supported") |
| 108 | +} |
| 109 | + |
| 110 | +// GetMIGDeviceEdits is unsupported for the imexlib specs |
| 111 | +func (l *imexlib) GetMIGDeviceEdits(device.Device, device.MigDevice) (*cdi.ContainerEdits, error) { |
| 112 | + return nil, fmt.Errorf("GetMIGDeviceEdits is not supported") |
| 113 | +} |
| 114 | + |
| 115 | +// GetMIGDeviceSpecs is unsupported for the imexlib specs |
| 116 | +func (l *imexlib) GetMIGDeviceSpecs(int, device.Device, int, device.MigDevice) ([]specs.Device, error) { |
| 117 | + return nil, fmt.Errorf("GetMIGDeviceSpecs is not supported") |
| 118 | +} |
0 commit comments