forked from NVIDIA/go-nvlib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnvpassthrough.go
More file actions
379 lines (326 loc) · 11.7 KB
/
Copy pathnvpassthrough.go
File metadata and controls
379 lines (326 loc) · 11.7 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* 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 nvpassthrough
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/NVIDIA/go-nvlib/pkg/nvpci"
)
const (
pciRootDir = "/sys/bus/pci/"
pciDevicesRoot = pciRootDir + "devices"
pciDriversRoot = pciRootDir + "drivers"
vfioPCIDriverName = "vfio-pci"
consumerPrefix = "consumer:pci:"
libModulesRoot = "/lib/modules/"
)
type Interface interface {
FindBestVFIOVariant(string) (string, error)
BindToVFIODriver(string) error
BindToDriver(string, string) error
Unbind(string) error
}
type nvpassthrough struct {
logger basicLogger
hostRoot string
nvpciLib nvpci.Interface
}
var _ Interface = (*nvpassthrough)(nil)
type nvidiaPCIAuxDevice struct {
Path string
Address string
Driver string
}
func New(opts ...Option) Interface {
n := &nvpassthrough{}
for _, opt := range opts {
opt(n)
}
if n.logger == nil {
n.logger = &nullLogger{}
}
if n.hostRoot == "" {
n.hostRoot = "/"
}
if n.nvpciLib == nil {
n.nvpciLib = nvpci.New()
}
return n
}
// Option defines a function for passing options to the New() call.
type Option func(*nvpassthrough)
// WithLogger provides an Option to set the logger for the library.
func WithLogger(logger basicLogger) Option {
return func(w *nvpassthrough) {
w.logger = logger
}
}
// WithHostRoot provides an Option to set the path to the host root filesystem.
func WithHostRoot(hostRoot string) Option {
return func(w *nvpassthrough) {
w.hostRoot = hostRoot
}
}
// WithNvpciLib provides an Option to set the nvpci lib used.
func WithNvpciLib(lib nvpci.Interface) Option {
return func(w *nvpassthrough) {
w.nvpciLib = lib
}
}
// FindBestVFIOVariant finds the "best" match of all vfio_pci aliases for
// device in the host modules.alias file. This uses the algorithm of
// finding every modules.alias line that begins with "alias vfio_pci:",
// then picking the one that matches the device's own modalias value
// (from the file of that name in the device's sysfs directory) with the
// fewest "wildcards" (* character, meaning "match any value for this
// attribute").
//
// (cdesiniotis) this code is inspired by:
// https://gitlab.com/libvirt/libvirt/-/commit/82e2fac297105f554f57fb589002933231b4f711
func (n *nvpassthrough) FindBestVFIOVariant(address string) (string, error) {
device, err := n.nvpciLib.GetNvidiaDeviceByPciBusID(address)
if err != nil {
return "", fmt.Errorf("failed to get NVIDIA PCI device by bus id %q: %w", address, err)
}
if device == nil {
return "", fmt.Errorf("device at %q is not an NVIDIA PCI device", address)
}
vfioAliases, err := getVFIOAliases()
if err != nil {
return "", fmt.Errorf("failed to get vfio_pci aliases in modules.alias file: %w", err)
}
if len(vfioAliases) == 0 {
n.logger.Debugf("No vfio_pci entries found in modules.alias file, falling back to default vfio-pci driver")
return vfioPCIDriverName, nil
}
modAliasPath := filepath.Join(device.Path, "modalias")
modAliasContent, err := os.ReadFile(modAliasPath)
if err != nil {
return "", fmt.Errorf("failed to read modalias file for %s: %w", device.Address, err)
}
modAliasStr := strings.TrimSpace(string(modAliasContent))
modAlias, err := parseModAliasString(modAliasStr)
if err != nil {
return "", fmt.Errorf("failed to parse modalias string %q for device %q: %w", modAliasStr, device.Address, err)
}
// Find the best matching VFIO driver for this device
bestMatch := findBestMatch(modAlias, vfioAliases)
if bestMatch == "" {
n.logger.Debugf("No matching vfio driver found for device %s in modules.alias file, falling back to default vfio-pci driver", device.Address)
return vfioPCIDriverName, nil
}
return bestMatch, nil
}
// BindToVFIODriver binds the provided NVIDIA PCI device to the
// vfio-pci driver (or a variant VFIO driver if one is preferred).
// This function takes care of additional logic, like making sure
// the vfio-pci driver is loaded first and that an auxiliary graphics
// device also get bound to the vfio-pci driver.
func (n *nvpassthrough) BindToVFIODriver(address string) error {
device, err := n.nvpciLib.GetNvidiaDeviceByPciBusID(address)
if err != nil {
return fmt.Errorf("failed to get NVIDIA PCI device by bus id %q: %w", address, err)
}
if device == nil {
return fmt.Errorf("device at %q is not an NVIDIA PCI device", address)
}
vfioDriverName, err := n.FindBestVFIOVariant(address)
if err != nil {
return fmt.Errorf("failed to find best vfio variant driver: %w", err)
}
km := newKernelModules(n.hostRoot)
if err := km.load(vfioDriverName); err != nil {
return fmt.Errorf("failed to load %q driver: %w", vfioDriverName, err)
}
// (cdesiniotis) Module names in the modules.alias file will only ever contain
// underscores characters and not dashes -- this aligns with how the linux kernel
// stores module names internally. This can sometimes differ from the name of the
// directory in /sys/bus/pci/driver/ for a given module. For example, this
// contradiction exists for the standard vfio-pci module:
//
// $ file /sys/bus/pci/drivers/vfio-pci
// sys/bus/pci/drivers/vfio-pci: directory
//
// $ modinfo vfio-pci | grep ^name:
// name: vfio_pci
//
// To account for this difference, we check if the module name returned by
// findBestVFIOVariant() exists in /sys/bus/pci/drivers, and if not, we try
// again but with any underscore characters converted to dashes.
driverDir := filepath.Join(pciDriversRoot, vfioDriverName)
if _, err := os.Stat(driverDir); err != nil {
vfioDriverNameNormalized := strings.ReplaceAll(vfioDriverName, "_", "-")
driverDir = filepath.Join(pciDriversRoot, vfioDriverNameNormalized)
if _, err := os.Stat(driverDir); err != nil {
return fmt.Errorf("failed to find directory for vfio driver %s at %s, is the module loaded?", vfioDriverName, pciDriversRoot)
}
vfioDriverName = vfioDriverNameNormalized
}
n.logger.Infof("Binding device %s to driver: %s", device.Address, vfioDriverName)
if device.Driver != vfioDriverName {
if err := unbind(device.Address); err != nil {
return fmt.Errorf("failed to unbind device %s: %w", device.Address, err)
}
if err := bind(device.Address, vfioDriverName); err != nil {
return fmt.Errorf("failed to bind device %s to %s: %w", device.Address, vfioDriverName, err)
}
}
// For graphics mode, bind the auxiliary device as well
auxDev, err := getGraphicsAuxDev(device)
if err != nil {
return fmt.Errorf("failed to get graphics auxiliary device for %s: %w", device.Address, err)
}
if auxDev == nil {
return nil
}
if auxDev.Driver == vfioDriverName {
return nil
}
n.logger.Infof("Binding graphics auxiliary device %s to driver: %s", auxDev.Address, vfioDriverName)
if err := unbind(auxDev.Address); err != nil {
return fmt.Errorf("failed to unbind graphics auxiliary device %s: %w", auxDev.Address, err)
}
if err := bind(auxDev.Address, vfioDriverName); err != nil {
return fmt.Errorf("failed to bind graphics auxiliary device %s to %s: %w", auxDev, vfioDriverName, err)
}
return nil
}
// BindToDriver binds an NVIDIA PCI device to the driver supplied as input.
func (n *nvpassthrough) BindToDriver(address string, driver string) error {
device, err := n.nvpciLib.GetNvidiaDeviceByPciBusID(address)
if err != nil {
return fmt.Errorf("failed to get NVIDIA PCI device by bus id %q: %w", address, err)
}
if device == nil {
return fmt.Errorf("device at %q is not an NVIDIA PCI device", address)
}
return bind(address, driver)
}
// Unbind unbinds the provided NVIDIA PCI Device from
// any driver it is currently bound to. This function also ensures
// an auxiliary graphics device is also unbound.
func (n *nvpassthrough) Unbind(address string) error {
device, err := n.nvpciLib.GetNvidiaDeviceByPciBusID(address)
if err != nil {
return fmt.Errorf("failed to get NVIDIA PCI device by bus id %q: %w", address, err)
}
if device == nil {
return fmt.Errorf("device at %q is not an NVIDIA PCI device", address)
}
if err := unbind(address); err != nil {
return fmt.Errorf("failed to unbind device %s: %w", address, err)
}
// For graphics mode, unbind the auxiliary device as well
auxDev, err := getGraphicsAuxDev(device)
if err != nil {
return fmt.Errorf("failed to get graphics auxiliary device for %s: %w", address, err)
}
if auxDev != nil {
if err := unbind(auxDev.Address); err != nil {
return fmt.Errorf("failed to unbind graphics auxiliary device %s: %w", auxDev.Address, err)
}
}
return nil
}
func bind(address string, driver string) error {
driverOverridePath := filepath.Join(pciDevicesRoot, address, "driver_override")
if err := writeFile(driverOverridePath, driver); err != nil {
return fmt.Errorf("failed to set driver_override for %s: %w", address, err)
}
bindPath := filepath.Join(pciDriversRoot, driver, "bind")
if err := writeFile(bindPath, address); err != nil {
return fmt.Errorf("failed to bind %s to %s: %w", address, driver, err)
}
return nil
}
func unbind(address string) error {
driverOverridePath := filepath.Join(pciDevicesRoot, address, "driver_override")
if err := writeFile(driverOverridePath, "\n"); err != nil {
return fmt.Errorf("failed to clear driver_override for %s: %w", address, err)
}
driverPath := filepath.Join(pciDevicesRoot, address, "driver")
if _, err := os.Stat(driverPath); os.IsNotExist(err) {
return nil
}
driverLink, err := os.Readlink(driverPath)
if err != nil {
return fmt.Errorf("failed to read driver link for %s: %w", address, err)
}
driverName := filepath.Base(driverLink)
unbindPath := filepath.Join(driverPath, "unbind")
if err := writeFile(unbindPath, address); err != nil {
return fmt.Errorf("failed to unbind %s from %s: %w", address, driverName, err)
}
return nil
}
func getGraphicsAuxDev(device *nvpci.NvidiaPCIDevice) (*nvidiaPCIAuxDevice, error) {
if device.Class != nvpci.PCIVgaControllerClass {
return nil, nil
}
// Look for consumer symlink
entries, err := os.ReadDir(device.Path)
if err != nil {
return nil, err
}
for _, entry := range entries {
if strings.HasPrefix(entry.Name(), "consumer") {
// Extract aux device name from consumer:pci:XXXX:XX:XX.X format
_, address, ok := strings.Cut(entry.Name(), consumerPrefix)
if !ok || address == "" {
continue
}
// Check if aux device exists
path := filepath.Join(pciDevicesRoot, address)
if _, err := os.Stat(path); err != nil {
continue
}
auxDev := &nvidiaPCIAuxDevice{
Path: path,
Address: address,
}
driver, err := getDriver(path)
if err != nil {
return nil, fmt.Errorf("failed to get driver for graphics auxiliary device %s: %w", address, err)
}
auxDev.Driver = driver
return auxDev, nil
}
}
return nil, nil
}
func getDriver(devicePath string) (string, error) {
driver, err := filepath.EvalSymlinks(filepath.Join(devicePath, "driver"))
switch {
case os.IsNotExist(err):
return "", nil
case err == nil:
return filepath.Base(driver), nil
}
return "", err
}
func writeFile(path string, s string) error {
f, err := os.OpenFile(path, os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return fmt.Errorf("failed to open file for writing: %w", err)
}
defer f.Close()
if _, err = f.WriteString(s); err != nil {
return fmt.Errorf("failed writing to file: %w", err)
}
return nil
}