2022-06-02 13:41:04 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
|
|
|
|
#
|
|
|
|
# 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 modifier
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config"
|
2022-06-29 15:33:57 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/config/image"
|
2022-06-02 13:41:04 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
|
2023-03-22 12:27:43 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2022-06-02 13:41:04 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/oci"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2023-11-22 13:16:34 +00:00
|
|
|
nvidiaGDSEnvvar = "NVIDIA_GDS"
|
|
|
|
nvidiaMOFEDEnvvar = "NVIDIA_MOFED"
|
|
|
|
nvidiaNVSWITCHEnvvar = "NVIDIA_NVSWITCH"
|
2024-01-03 23:32:21 +00:00
|
|
|
nvidiaGDRCOPYEnvvar = "NVIDIA_GDRCOPY"
|
2022-06-02 13:41:04 +00:00
|
|
|
)
|
|
|
|
|
2023-11-22 13:23:28 +00:00
|
|
|
// NewFeatureGatedModifier creates the modifiers for optional features.
|
|
|
|
// These include:
|
|
|
|
//
|
|
|
|
// NVIDIA_GDS=enabled
|
|
|
|
// NVIDIA_MOFED=enabled
|
2023-11-22 13:16:34 +00:00
|
|
|
// NVIDIA_NVSWITCH=enabled
|
2024-01-03 23:32:21 +00:00
|
|
|
// NVIDIA_GDRCOPY=enabled
|
2023-11-22 13:23:28 +00:00
|
|
|
//
|
|
|
|
// If not devices are selected, no changes are made.
|
|
|
|
func NewFeatureGatedModifier(logger logger.Interface, cfg *config.Config, image image.CUDA) (oci.SpecModifier, error) {
|
2022-10-26 10:37:23 +00:00
|
|
|
if devices := image.DevicesFromEnvvars(visibleDevicesEnvvar); len(devices.List()) == 0 {
|
2022-06-29 15:33:57 +00:00
|
|
|
logger.Infof("No modification required; no devices requested")
|
2022-06-02 13:41:04 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-11-22 13:23:28 +00:00
|
|
|
var discoverers []discover.Discover
|
2022-06-02 13:41:04 +00:00
|
|
|
|
2023-11-14 15:57:37 +00:00
|
|
|
driverRoot := cfg.NVIDIAContainerCLIConfig.Root
|
|
|
|
devRoot := cfg.NVIDIAContainerCLIConfig.Root
|
2023-11-22 13:23:28 +00:00
|
|
|
|
|
|
|
if image.Getenv(nvidiaGDSEnvvar) == "enabled" {
|
|
|
|
d, err := discover.NewGDSDiscoverer(logger, driverRoot, devRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to construct discoverer for GDS devices: %w", err)
|
|
|
|
}
|
|
|
|
discoverers = append(discoverers, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
if image.Getenv(nvidiaMOFEDEnvvar) == "enabled" {
|
|
|
|
d, err := discover.NewMOFEDDiscoverer(logger, devRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to construct discoverer for MOFED devices: %w", err)
|
|
|
|
}
|
|
|
|
discoverers = append(discoverers, d)
|
2022-06-02 13:41:04 +00:00
|
|
|
}
|
|
|
|
|
2023-11-22 13:16:34 +00:00
|
|
|
if image.Getenv(nvidiaNVSWITCHEnvvar) == "enabled" {
|
|
|
|
d, err := discover.NewNvSwitchDiscoverer(logger, devRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to construct discoverer for NVSWITCH devices: %w", err)
|
|
|
|
}
|
|
|
|
discoverers = append(discoverers, d)
|
|
|
|
}
|
|
|
|
|
2024-01-03 23:32:21 +00:00
|
|
|
if image.Getenv(nvidiaGDRCOPYEnvvar) == "enabled" {
|
|
|
|
d, err := discover.NewGDRCopyDiscoverer(logger, devRoot)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to construct discoverer for GDRCopy devices: %w", err)
|
|
|
|
}
|
|
|
|
discoverers = append(discoverers, d)
|
|
|
|
}
|
|
|
|
|
2023-11-22 13:23:28 +00:00
|
|
|
return NewModifierFromDiscoverer(logger, discover.Merge(discoverers...))
|
2022-06-02 13:41:04 +00:00
|
|
|
}
|