|
| 1 | +/** |
| 2 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 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 modifier |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + "tags.cncf.io/container-device-interface/pkg/parser" |
| 23 | + |
| 24 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/config" |
| 25 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/logger" |
| 26 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/modifier/cdi" |
| 27 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/oci" |
| 28 | + "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi" |
| 29 | + "github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + automaticDeviceVendor = "runtime.nvidia.com" |
| 34 | + automaticDeviceClass = "gpu" |
| 35 | + automaticDeviceKind = automaticDeviceVendor + "/" + automaticDeviceClass |
| 36 | +) |
| 37 | + |
| 38 | +func newAutomaticCDISpecModifier(logger logger.Interface, cfg *config.Config, devices []string) (oci.SpecModifier, error) { |
| 39 | + logger.Debugf("Generating in-memory CDI specs for devices %v", devices) |
| 40 | + // TODO: We should try to load the kernel modules and create the device nodes here. |
| 41 | + // Failures should raise a warning and not error out. |
| 42 | + spec, err := generateAutomaticCDISpec(logger, cfg, devices) |
| 43 | + if err != nil { |
| 44 | + return nil, fmt.Errorf("failed to generate CDI spec: %w", err) |
| 45 | + } |
| 46 | + cdiModifier, err := cdi.New( |
| 47 | + cdi.WithLogger(logger), |
| 48 | + cdi.WithSpec(spec.Raw()), |
| 49 | + ) |
| 50 | + if err != nil { |
| 51 | + return nil, fmt.Errorf("failed to construct CDI modifier: %w", err) |
| 52 | + } |
| 53 | + |
| 54 | + return cdiModifier, nil |
| 55 | +} |
| 56 | + |
| 57 | +func generateAutomaticCDISpec(logger logger.Interface, cfg *config.Config, devices []string) (spec.Interface, error) { |
| 58 | + cdilib, err := nvcdi.New( |
| 59 | + nvcdi.WithLogger(logger), |
| 60 | + nvcdi.WithNVIDIACDIHookPath(cfg.NVIDIACTKConfig.Path), |
| 61 | + nvcdi.WithDriverRoot(cfg.NVIDIAContainerCLIConfig.Root), |
| 62 | + nvcdi.WithVendor("runtime.nvidia.com"), |
| 63 | + nvcdi.WithClass("gpu"), |
| 64 | + ) |
| 65 | + if err != nil { |
| 66 | + return nil, fmt.Errorf("failed to construct CDI library: %w", err) |
| 67 | + } |
| 68 | + |
| 69 | + identifiers := []string{} |
| 70 | + for _, device := range devices { |
| 71 | + _, _, id := parser.ParseDevice(device) |
| 72 | + identifiers = append(identifiers, id) |
| 73 | + } |
| 74 | + |
| 75 | + deviceSpecs, err := cdilib.GetDeviceSpecsByID(identifiers...) |
| 76 | + if err != nil { |
| 77 | + return nil, fmt.Errorf("failed to get CDI device specs: %w", err) |
| 78 | + } |
| 79 | + |
| 80 | + commonEdits, err := cdilib.GetCommonEdits() |
| 81 | + if err != nil { |
| 82 | + return nil, fmt.Errorf("failed to get common CDI spec edits: %w", err) |
| 83 | + } |
| 84 | + |
| 85 | + return spec.New( |
| 86 | + spec.WithDeviceSpecs(deviceSpecs), |
| 87 | + spec.WithEdits(*commonEdits.ContainerEdits), |
| 88 | + spec.WithVendor("runtime.nvidia.com"), |
| 89 | + spec.WithClass("gpu"), |
| 90 | + ) |
| 91 | +} |
| 92 | + |
| 93 | +// filterAutomaticDevices searches for "automatic" device names in the input slice. |
| 94 | +// "Automatic" devices are a well-defined list of CDI device names which, when requested, |
| 95 | +// trigger the generation of a CDI spec at runtime. This removes the need to generate a |
| 96 | +// CDI spec on the system a-priori as well as keep it up-to-date. |
| 97 | +func filterAutomaticDevices(devices []string) []string { |
| 98 | + var automatic []string |
| 99 | + for _, device := range devices { |
| 100 | + vendor, class, _ := parser.ParseDevice(device) |
| 101 | + if vendor == automaticDeviceVendor && class == automaticDeviceClass { |
| 102 | + automatic = append(automatic, device) |
| 103 | + } |
| 104 | + } |
| 105 | + return automatic |
| 106 | +} |
0 commit comments