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-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
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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-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)
|
|
|
|
default:
|
|
|
|
// TODO: We would like to return an error here instead of panicking
|
|
|
|
panic("Unknown mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
return &wrapper{Interface: lib}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-20 12:29:52 +00:00
|
|
|
return spec.New(deviceSpecs, *edits.ContainerEdits)
|
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
|
|
|
}
|