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"
|
|
|
|
|
2023-02-20 12:29:52 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/pkg/nvcdi/spec"
|
2022-12-02 13:17:52 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvlib/device"
|
2023-02-13 15:04:30 +00:00
|
|
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvlib/info"
|
2022-12-02 13:17:52 +00:00
|
|
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvml"
|
|
|
|
)
|
|
|
|
|
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-02-20 12:29:52 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 13:17:52 +00:00
|
|
|
type nvcdilib struct {
|
|
|
|
logger *logrus.Logger
|
|
|
|
nvmllib nvml.Interface
|
2023-02-16 14:52:39 +00:00
|
|
|
mode string
|
2022-12-02 13:17:52 +00:00
|
|
|
devicelib device.Interface
|
|
|
|
deviceNamer DeviceNamer
|
|
|
|
driverRoot string
|
|
|
|
nvidiaCTKPath string
|
2023-02-13 15:04:30 +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
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// New creates a new nvcdi library
|
|
|
|
func New(opts ...Option) Interface {
|
|
|
|
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 {
|
|
|
|
l.logger = logrus.StandardLogger()
|
|
|
|
}
|
|
|
|
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-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:
|
|
|
|
// TODO: We would like to return an error here instead of panicking
|
|
|
|
panic("Unknown mode")
|
|
|
|
}
|
|
|
|
|
2023-02-22 14:40:27 +00:00
|
|
|
w := wrapper{
|
|
|
|
Interface: lib,
|
|
|
|
vendor: l.vendor,
|
|
|
|
class: l.class,
|
|
|
|
}
|
|
|
|
return &w
|
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-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-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
|
|
|
|
}
|