2022-12-02 13:17:52 +00:00
|
|
|
/**
|
|
|
|
# Copyright (c) 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 nvcdi
|
|
|
|
|
|
|
|
import (
|
2023-03-23 09:50:11 +00:00
|
|
|
"fmt"
|
|
|
|
|
2024-02-29 08:29:20 +00:00
|
|
|
"github.com/NVIDIA/go-nvlib/pkg/nvlib/device"
|
|
|
|
"github.com/NVIDIA/go-nvlib/pkg/nvlib/info"
|
|
|
|
"github.com/NVIDIA/go-nvlib/pkg/nvml"
|
|
|
|
|
2023-03-22 12:27:43 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2023-06-22 13:49:27 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/tegra/csv"
|
2023-02-20 12:29:52 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
|
2023-03-17 09:47:31 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/transform"
|
2022-12-02 13:17:52 +00:00
|
|
|
)
|
|
|
|
|
2023-02-20 12:29:52 +00:00
|
|
|
type wrapper struct {
|
|
|
|
Interface
|
2023-02-22 14:40:27 +00:00
|
|
|
|
|
|
|
vendor string
|
|
|
|
class string
|
2023-03-17 09:47:31 +00:00
|
|
|
|
|
|
|
mergedDeviceOptions []transform.MergedDeviceOption
|
2023-02-20 12:29:52 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 13:17:52 +00:00
|
|
|
type nvcdilib struct {
|
2023-07-18 10:02:37 +00:00
|
|
|
logger logger.Interface
|
|
|
|
nvmllib nvml.Interface
|
|
|
|
mode string
|
|
|
|
devicelib device.Interface
|
|
|
|
deviceNamer DeviceNamer
|
|
|
|
driverRoot string
|
|
|
|
nvidiaCTKPath string
|
|
|
|
librarySearchPaths []string
|
2023-02-13 15:04:30 +00:00
|
|
|
|
2023-09-22 14:30:20 +00:00
|
|
|
csvFiles []string
|
|
|
|
csvIgnorePatterns []string
|
2023-05-10 12:23:05 +00:00
|
|
|
|
2023-02-22 14:40:27 +00:00
|
|
|
vendor string
|
|
|
|
class string
|
|
|
|
|
2023-02-13 15:04:30 +00:00
|
|
|
infolib info.Interface
|
2023-03-17 09:47:31 +00:00
|
|
|
|
|
|
|
mergedDeviceOptions []transform.MergedDeviceOption
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new nvcdi library
|
2023-03-22 12:04:12 +00:00
|
|
|
func New(opts ...Option) (Interface, error) {
|
2022-12-02 13:17:52 +00:00
|
|
|
l := &nvcdilib{}
|
2023-02-15 00:32:40 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
opt(l)
|
|
|
|
}
|
2023-02-16 15:29:53 +00:00
|
|
|
if l.mode == "" {
|
2023-02-20 14:27:34 +00:00
|
|
|
l.mode = ModeAuto
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|
|
|
|
if l.logger == nil {
|
2023-03-22 12:27:43 +00:00
|
|
|
l.logger = logger.New()
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|
|
|
|
if l.deviceNamer == nil {
|
|
|
|
l.deviceNamer, _ = NewDeviceNamer(DeviceNameStrategyIndex)
|
|
|
|
}
|
|
|
|
if l.driverRoot == "" {
|
|
|
|
l.driverRoot = "/"
|
|
|
|
}
|
|
|
|
if l.nvidiaCTKPath == "" {
|
|
|
|
l.nvidiaCTKPath = "/usr/bin/nvidia-ctk"
|
|
|
|
}
|
2023-02-13 15:04:30 +00:00
|
|
|
if l.infolib == nil {
|
|
|
|
l.infolib = info.New()
|
|
|
|
}
|
2022-12-02 13:17:52 +00:00
|
|
|
|
2023-02-20 12:29:52 +00:00
|
|
|
var lib Interface
|
2023-02-13 15:04:30 +00:00
|
|
|
switch l.resolveMode() {
|
2023-05-10 12:23:05 +00:00
|
|
|
case ModeCSV:
|
|
|
|
if len(l.csvFiles) == 0 {
|
2023-05-10 14:28:59 +00:00
|
|
|
l.csvFiles = csv.DefaultFileList()
|
2023-05-10 12:23:05 +00:00
|
|
|
}
|
|
|
|
lib = (*csvlib)(l)
|
2023-03-01 10:16:38 +00:00
|
|
|
case ModeManagement:
|
|
|
|
if l.vendor == "" {
|
|
|
|
l.vendor = "management.nvidia.com"
|
|
|
|
}
|
|
|
|
lib = (*managementlib)(l)
|
2023-02-20 14:27:34 +00:00
|
|
|
case ModeNvml:
|
2023-02-16 15:29:53 +00:00
|
|
|
if l.nvmllib == nil {
|
|
|
|
l.nvmllib = nvml.New()
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|
2023-02-16 15:29:53 +00:00
|
|
|
if l.devicelib == nil {
|
|
|
|
l.devicelib = device.New(device.WithNvml(l.nvmllib))
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|
|
|
|
|
2023-02-20 12:29:52 +00:00
|
|
|
lib = (*nvmllib)(l)
|
2023-02-20 14:27:34 +00:00
|
|
|
case ModeWsl:
|
2023-02-20 12:29:52 +00:00
|
|
|
lib = (*wsllib)(l)
|
2023-03-06 12:34:40 +00:00
|
|
|
case ModeGds:
|
|
|
|
if l.class == "" {
|
|
|
|
l.class = "gds"
|
|
|
|
}
|
|
|
|
lib = (*gdslib)(l)
|
2023-03-06 12:41:07 +00:00
|
|
|
case ModeMofed:
|
|
|
|
if l.class == "" {
|
|
|
|
l.class = "mofed"
|
|
|
|
}
|
|
|
|
lib = (*mofedlib)(l)
|
2023-02-20 12:29:52 +00:00
|
|
|
default:
|
2023-03-22 12:04:12 +00:00
|
|
|
return nil, fmt.Errorf("unknown mode %q", l.mode)
|
2023-02-20 12:29:52 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 14:40:27 +00:00
|
|
|
w := wrapper{
|
2023-03-17 09:47:31 +00:00
|
|
|
Interface: lib,
|
|
|
|
vendor: l.vendor,
|
|
|
|
class: l.class,
|
|
|
|
mergedDeviceOptions: l.mergedDeviceOptions,
|
2023-02-22 14:40:27 +00:00
|
|
|
}
|
2023-03-22 12:04:12 +00:00
|
|
|
return &w, nil
|
2023-02-20 12:29:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetSpec combines the device specs and common edits from the wrapped Interface to a single spec.Interface.
|
|
|
|
func (l *wrapper) GetSpec() (spec.Interface, error) {
|
|
|
|
deviceSpecs, err := l.GetAllDeviceSpecs()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
edits, err := l.GetCommonEdits()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|
2023-02-16 15:29:53 +00:00
|
|
|
|
2023-02-22 14:19:22 +00:00
|
|
|
return spec.New(
|
|
|
|
spec.WithDeviceSpecs(deviceSpecs),
|
|
|
|
spec.WithEdits(*edits.ContainerEdits),
|
2023-02-22 14:40:27 +00:00
|
|
|
spec.WithVendor(l.vendor),
|
|
|
|
spec.WithClass(l.class),
|
2023-03-17 09:47:31 +00:00
|
|
|
spec.WithMergedDeviceOptions(l.mergedDeviceOptions...),
|
2023-02-22 14:19:22 +00:00
|
|
|
)
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|
2023-02-13 15:04:30 +00:00
|
|
|
|
|
|
|
// resolveMode resolves the mode for CDI spec generation based on the current system.
|
|
|
|
func (l *nvcdilib) resolveMode() (rmode string) {
|
2023-02-20 14:27:34 +00:00
|
|
|
if l.mode != ModeAuto {
|
2023-02-13 15:04:30 +00:00
|
|
|
return l.mode
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
l.logger.Infof("Auto-detected mode as %q", rmode)
|
|
|
|
}()
|
|
|
|
|
2023-02-13 15:04:30 +00:00
|
|
|
isWSL, reason := l.infolib.HasDXCore()
|
2023-02-13 15:04:30 +00:00
|
|
|
l.logger.Debugf("Is WSL-based system? %v: %v", isWSL, reason)
|
|
|
|
|
|
|
|
if isWSL {
|
2023-02-20 14:27:34 +00:00
|
|
|
return ModeWsl
|
2023-02-13 15:04:30 +00:00
|
|
|
}
|
|
|
|
|
2023-05-10 12:28:09 +00:00
|
|
|
isNvml, reason := l.infolib.HasNvml()
|
|
|
|
l.logger.Debugf("Is NVML-based system? %v: %v", isNvml, reason)
|
|
|
|
|
|
|
|
isTegra, reason := l.infolib.IsTegraSystem()
|
|
|
|
l.logger.Debugf("Is Tegra-based system? %v: %v", isTegra, reason)
|
|
|
|
|
|
|
|
if isTegra && !isNvml {
|
|
|
|
return ModeCSV
|
|
|
|
}
|
|
|
|
|
2023-02-20 14:27:34 +00:00
|
|
|
return ModeNvml
|
2023-02-13 15:04:30 +00:00
|
|
|
}
|
2023-03-23 09:50:11 +00:00
|
|
|
|
|
|
|
// getCudaVersion returns the CUDA version of the current system.
|
|
|
|
func (l *nvcdilib) getCudaVersion() (string, error) {
|
|
|
|
if hasNVML, reason := l.infolib.HasNvml(); !hasNVML {
|
|
|
|
return "", fmt.Errorf("nvml not detected: %v", reason)
|
|
|
|
}
|
|
|
|
if l.nvmllib == nil {
|
|
|
|
return "", fmt.Errorf("nvml library not initialized")
|
|
|
|
}
|
|
|
|
r := l.nvmllib.Init()
|
|
|
|
if r != nvml.SUCCESS {
|
|
|
|
return "", fmt.Errorf("failed to initialize nvml: %v", r)
|
|
|
|
}
|
|
|
|
defer l.nvmllib.Shutdown()
|
|
|
|
|
|
|
|
version, r := l.nvmllib.SystemGetDriverVersion()
|
|
|
|
if r != nvml.SUCCESS {
|
|
|
|
return "", fmt.Errorf("failed to get driver version: %v", r)
|
|
|
|
}
|
|
|
|
return version, nil
|
|
|
|
}
|