-
Notifications
You must be signed in to change notification settings - Fork 493
Expand file tree
/
Copy pathlib.go
More file actions
237 lines (205 loc) · 6.19 KB
/
lib.go
File metadata and controls
237 lines (205 loc) · 6.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
# 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 (
"fmt"
"slices"
"github.com/NVIDIA/go-nvml/pkg/nvml"
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
"github.com/NVIDIA/nvidia-container-toolkit/internal/edits"
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
"github.com/NVIDIA/nvidia-container-toolkit/internal/lookup/root"
"github.com/NVIDIA/nvidia-container-toolkit/internal/nvsandboxutils"
)
type nvcdilib struct {
logger logger.Interface
platformlibs
deviceNamers DeviceNamers
// TODO: We should use the devRoot associated with the driver.
devRoot string
librarySearchPaths []string
csv csvOptions
driver *root.Driver
featureFlags map[FeatureFlag]bool
hookCreator discover.HookCreator
editsFactory edits.Factory
}
// New creates a new nvcdi library
func New(opts ...Option) (Interface, error) {
o := populateOptions(opts...)
l := &nvcdilib{
logger: o.logger,
platformlibs: o.platformlibs,
driver: root.New(
o.getDriverOptions()...,
),
devRoot: o.devRoot,
deviceNamers: o.deviceNamers,
librarySearchPaths: slices.Clone(o.librarySearchPaths),
featureFlags: o.featureFlags,
csv: o.csv,
hookCreator: discover.NewHookCreator(
discover.WithNVIDIACDIHookPath(o.nvidiaCDIHookPath),
discover.WithEnabledHooks(o.enabledHooks...),
discover.WithLdconfigPath(o.ldconfigPath),
discover.WithDisabledHooks(o.disabledHooks...),
),
editsFactory: o.editsFactory,
}
var factory deviceSpecGeneratorFactory
switch o.mode {
case ModeCSV:
factory = (*csvlib)(l)
case ModeManagement:
factory = (*managementlib)(l)
case ModeNvml:
factory = (*nvmllib)(l)
case ModeWsl:
factory = (*wsllib)(l)
case ModeGdrcopy, ModeGds, ModeMofed, ModeNvswitch:
factory = &gatedlib{
nvcdilib: l,
mode: o.mode,
}
case ModeSandbox:
factory = &sandboxlib{
nvcdilib: l,
emptyDeviceSpecGenerator: "all",
}
case ModeImex:
factory = (*imexlib)(l)
default:
return nil, fmt.Errorf("unknown mode %q", o.mode)
}
w := wrapper{
factory: factory,
vendor: o.getVendorOrDefault(),
class: o.getClassOrDefault(),
mergedDeviceOptions: o.mergedDeviceOptions,
}
return &w, nil
}
type nvmllibAsVersioner struct {
nvml.Interface
}
func nvmllibWithVersion(nvmllib nvml.Interface) *nvmllibAsVersioner {
if nvmllib == nil {
return nil
}
return &nvmllibAsVersioner{
Interface: nvmllib,
}
}
func (l *nvmllibAsVersioner) Version() (string, error) {
if l == nil || l.Interface == nil {
return "", fmt.Errorf("nvml library not initialized")
}
r := l.Init()
if r != nvml.SUCCESS {
return "", fmt.Errorf("failed to initialize nvml: %v", r)
}
defer func() {
_ = l.Shutdown()
}()
version, r := l.SystemGetDriverVersion()
if r != nvml.SUCCESS {
return "", fmt.Errorf("failed to get driver version: %v", r)
}
return version, nil
}
type nvsandboxutilslibAsVersioner struct {
nvsandboxutils.Interface
}
func nvsandboxutilslibWithVersion(nvsandboxutilslib nvsandboxutils.Interface) *nvsandboxutilslibAsVersioner {
if nvsandboxutilslib == nil {
return nil
}
return &nvsandboxutilslibAsVersioner{
Interface: nvsandboxutilslib,
}
}
func (l *nvsandboxutilslibAsVersioner) Version() (string, error) {
if l == nil || l.Interface == nil {
return "", fmt.Errorf("libnvsandboxutils is not available")
}
// Sandboxutils initialization should happen before this function is called
version, ret := l.GetDriverVersion()
if ret != nvsandboxutils.SUCCESS {
return "", fmt.Errorf("%v", ret)
}
return version, nil
}
func (o *options) getNvmlLib() nvml.Interface {
if o.nvmllib != nil {
return o.nvmllib
}
var nvmlOpts []nvml.LibraryOption
candidates, err := o.driverLibraryLocator().Locate("libnvidia-ml.so.1")
if err != nil {
o.logger.Warningf("Ignoring error in locating libnvidia-ml.so.1: %v", err)
} else {
libNvidiaMlPath := candidates[0]
o.logger.Infof("Using %v", libNvidiaMlPath)
nvmlOpts = append(nvmlOpts, nvml.WithLibraryPath(libNvidiaMlPath))
}
return nvml.New(nvmlOpts...)
}
// getNvsandboxUtilsLib returns the nvsandboxutilslib to use for CDI spec
// generation.
func (o *options) getNvsandboxUtilsLib() nvsandboxutils.Interface {
if o.featureFlags[FeatureDisableNvsandboxUtils] {
return nil
}
if o.nvsandboxutilslib != nil {
return o.nvsandboxutilslib
}
var nvsandboxutilsOpts []nvsandboxutils.LibraryOption
// Set the library path for libnvidia-sandboxutils
candidates, err := o.driverLibraryLocator().Locate("libnvidia-sandboxutils.so.1")
if err != nil {
o.logger.Warningf("Ignoring error in locating libnvidia-sandboxutils.so.1: %v", err)
} else {
libNvidiaSandboxutilsPath := candidates[0]
o.logger.Infof("Using %v", libNvidiaSandboxutilsPath)
nvsandboxutilsOpts = append(nvsandboxutilsOpts, nvsandboxutils.WithLibraryPath(libNvidiaSandboxutilsPath))
}
// We try to initialize the library once to ensure that we have a valid installation.
lib := nvsandboxutils.New(nvsandboxutilsOpts...)
// TODO: Should this accept the driver root or the devRoot?
if r := lib.Init(o.driverRoot); r != nvsandboxutils.SUCCESS {
o.logger.Warningf("Failed to init nvsandboxutils: %v; ignoring", r)
return nil
}
defer func() {
_ = lib.Shutdown()
}()
return lib
}
func (o *options) getDriverOptions() []root.Option {
return []root.Option{
root.WithLogger(o.logger),
root.WithDriverRoot(o.driverRoot),
root.WithDevRoot(o.devRoot),
root.WithLibrarySearchPaths(o.librarySearchPaths...),
root.WithConfigSearchPaths(o.configSearchPaths...),
root.WithVersioner(
root.FirstOf(
nvsandboxutilslibWithVersion(o.nvsandboxutilslib),
nvmllibWithVersion(o.nvmllib),
),
),
}
}