-
Notifications
You must be signed in to change notification settings - Fork 114
/
kernel.go
670 lines (575 loc) · 22.4 KB
/
kernel.go
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
package kernel
import (
"errors"
"fmt"
"os"
"path/filepath"
"strings"
"sigs.k8s.io/controller-runtime/pkg/log"
sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/consts"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
"github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/vars"
)
type kernel struct {
utilsHelper utils.CmdInterface
}
func New(utilsHelper utils.CmdInterface) types.KernelInterface {
return &kernel{utilsHelper: utilsHelper}
}
func (k *kernel) LoadKernelModule(name string, args ...string) error {
log.Log.Info("LoadKernelModule(): try to load kernel module", "name", name, "args", args)
chrootDefinition := utils.GetChrootExtension()
cmdArgs := strings.Join(args, " ")
// check if the driver is already loaded in to the system
isLoaded, err := k.IsKernelModuleLoaded(name)
if err != nil {
log.Log.Error(err, "LoadKernelModule(): failed to check if kernel module is already loaded", "name", name)
}
if isLoaded {
log.Log.Info("LoadKernelModule(): kernel module already loaded", "name", name)
return nil
}
_, _, err = k.utilsHelper.RunCommand("/bin/sh", "-c", fmt.Sprintf("%s modprobe %s %s", chrootDefinition, name, cmdArgs))
if err != nil {
log.Log.Error(err, "LoadKernelModule(): failed to load kernel module with arguments", "name", name, "args", args)
return err
}
return nil
}
func (k *kernel) IsKernelModuleLoaded(kernelModuleName string) (bool, error) {
log.Log.Info("IsKernelModuleLoaded(): check if kernel module is loaded", "name", kernelModuleName)
chrootDefinition := utils.GetChrootExtension()
stdout, stderr, err := k.utilsHelper.RunCommand("/bin/sh", "-c", fmt.Sprintf("%s lsmod | grep \"^%s\"", chrootDefinition, kernelModuleName))
if err != nil && len(stderr) != 0 {
log.Log.Error(err, "IsKernelModuleLoaded(): failed to check if kernel module is loaded",
"name", kernelModuleName, "stderr", stderr)
return false, err
}
log.Log.V(2).Info("IsKernelModuleLoaded():", "stdout", stdout)
if len(stderr) != 0 {
log.Log.Error(err, "IsKernelModuleLoaded(): failed to check if kernel module is loaded", "name", kernelModuleName, "stderr", stderr)
return false, fmt.Errorf(stderr)
}
if len(stdout) != 0 {
log.Log.Info("IsKernelModuleLoaded(): kernel module already loaded", "name", kernelModuleName)
return true, nil
}
return false, nil
}
func (k *kernel) TryEnableTun() {
if err := k.LoadKernelModule("tun"); err != nil {
log.Log.Error(err, "tryEnableTun(): TUN kernel module not loaded")
}
}
func (k *kernel) TryEnableVhostNet() {
if err := k.LoadKernelModule("vhost_net"); err != nil {
log.Log.Error(err, "tryEnableVhostNet(): VHOST_NET kernel module not loaded")
}
}
// GetCurrentKernelArgs This retrieves the kernel cmd line arguments
func (k *kernel) GetCurrentKernelArgs() (string, error) {
path := consts.ProcKernelCmdLine
if !vars.UsingSystemdMode {
path = filepath.Join(consts.Host, path)
}
path = filepath.Join(vars.FilesystemRoot, path)
cmdLine, err := os.ReadFile(path)
if err != nil {
return "", fmt.Errorf("GetCurrentKernelArgs(): Error reading %s: %v", path, err)
}
return string(cmdLine), nil
}
// IsKernelArgsSet This checks if the kernel cmd line is set properly. Please note that the same key could be repeated
// several times in the kernel cmd line. We can only ensure that the kernel cmd line has the key/val kernel arg that we set.
func (k *kernel) IsKernelArgsSet(cmdLine string, karg string) bool {
elements := strings.Fields(cmdLine)
for _, element := range elements {
if element == karg {
return true
}
}
return false
}
// Unbind unbind driver for one device
func (k *kernel) Unbind(pciAddr string) error {
log.Log.V(2).Info("Unbind(): unbind device driver for device", "device", pciAddr)
return k.UnbindDriverByBusAndDevice(consts.BusPci, pciAddr)
}
// BindDpdkDriver bind dpdk driver for one device
// Bind the device given by "pciAddr" to the driver "driver"
func (k *kernel) BindDpdkDriver(pciAddr, driver string) error {
log.Log.V(2).Info("BindDpdkDriver(): bind device to driver",
"device", pciAddr, "driver", driver)
if err := k.BindDriverByBusAndDevice(consts.BusPci, pciAddr, driver); err != nil {
_, innerErr := os.Readlink(filepath.Join(vars.FilesystemRoot, consts.SysBusPciDevices, pciAddr, "iommu_group"))
if innerErr != nil {
log.Log.Error(err, "Could not read IOMMU group for device", "device", pciAddr)
return fmt.Errorf(
"cannot bind driver %s to device %s, make sure IOMMU is enabled in BIOS. %w", driver, pciAddr, innerErr)
}
return err
}
return nil
}
// BindDefaultDriver bind driver for one device
// Bind the device given by "pciAddr" to the default driver
func (k *kernel) BindDefaultDriver(pciAddr string) error {
log.Log.V(2).Info("BindDefaultDriver(): bind device to default driver", "device", pciAddr)
curDriver, err := getDriverByBusAndDevice(consts.BusPci, pciAddr)
if err != nil {
return err
}
if curDriver != "" {
if !sriovnetworkv1.StringInArray(curDriver, vars.DpdkDrivers) {
log.Log.V(2).Info("BindDefaultDriver(): device already bound to default driver",
"device", pciAddr, "driver", curDriver)
return nil
}
if err := k.UnbindDriverByBusAndDevice(consts.BusPci, pciAddr); err != nil {
return err
}
}
if err := setDriverOverride(consts.BusPci, pciAddr, ""); err != nil {
return err
}
if err := probeDriver(consts.BusPci, pciAddr); err != nil {
return err
}
return nil
}
// BindDriverByBusAndDevice binds device to the provided driver
// bus - the bus path in the sysfs, e.g. "pci" or "vdpa"
// device - the name of the device on the bus, e.g. 0000:85:1e.5 for PCI or vpda1 for VDPA
// driver - the name of the driver, e.g. vfio-pci or vhost_vdpa.
func (k *kernel) BindDriverByBusAndDevice(bus, device, driver string) error {
log.Log.V(2).Info("BindDriverByBusAndDevice(): bind device to driver",
"bus", bus, "device", device, "driver", driver)
curDriver, err := getDriverByBusAndDevice(bus, device)
if err != nil {
return err
}
if curDriver != "" {
if curDriver == driver {
log.Log.V(2).Info("BindDriverByBusAndDevice(): device already bound to driver",
"bus", bus, "device", device, "driver", driver)
return nil
}
if err := k.UnbindDriverByBusAndDevice(bus, device); err != nil {
return err
}
}
if err := setDriverOverride(bus, device, driver); err != nil {
return err
}
if err := bindDriver(bus, device, driver); err != nil {
return err
}
return setDriverOverride(bus, device, "")
}
// Workaround function to handle a case where the vf default driver is stuck and not able to create the vf kernel interface.
// This function unbind the VF from the default driver and try to bind it again
// bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2045087
func (k *kernel) RebindVfToDefaultDriver(vfAddr string) error {
log.Log.Info("RebindVfToDefaultDriver()", "vf", vfAddr)
if err := k.Unbind(vfAddr); err != nil {
return err
}
if err := k.BindDefaultDriver(vfAddr); err != nil {
log.Log.Error(err, "RebindVfToDefaultDriver(): fail to bind default driver", "device", vfAddr)
return err
}
log.Log.Info("RebindVfToDefaultDriver(): workaround implemented", "vf", vfAddr)
return nil
}
func (k *kernel) UnbindDriverIfNeeded(vfAddr string, isRdma bool) error {
if isRdma {
log.Log.Info("UnbindDriverIfNeeded(): unbinding driver", "device", vfAddr)
if err := k.Unbind(vfAddr); err != nil {
return err
}
log.Log.Info("UnbindDriverIfNeeded(): unbounded driver", "device", vfAddr)
}
return nil
}
// UnbindDriverByBusAndDevice unbind device identified by bus and device ID from the driver
// bus - the bus path in the sysfs, e.g. "pci" or "vdpa"
// device - the name of the device on the bus, e.g. 0000:85:1e.5 for PCI or vpda1 for VDPA
func (k *kernel) UnbindDriverByBusAndDevice(bus, device string) error {
log.Log.V(2).Info("UnbindDriverByBusAndDevice(): unbind device driver for device", "bus", bus, "device", device)
driver, err := getDriverByBusAndDevice(bus, device)
if err != nil {
return err
}
if driver == "" {
log.Log.V(2).Info("UnbindDriverByBusAndDevice(): device has no driver", "bus", bus, "device", device)
return nil
}
return unbindDriver(bus, device, driver)
}
func (k *kernel) HasDriver(pciAddr string) (bool, string) {
driver, err := getDriverByBusAndDevice(consts.BusPci, pciAddr)
if err != nil {
log.Log.V(2).Info("HasDriver(): device driver is empty for device", "device", pciAddr)
return false, ""
}
if driver != "" {
log.Log.V(2).Info("HasDriver(): device driver for device", "device", pciAddr, "driver", driver)
return true, driver
}
return false, ""
}
// GetDriverByBusAndDevice returns driver for the device or error.
// returns "", nil if the device has no driver.
// bus - the bus path in the sysfs, e.g. "pci" or "vdpa"
// device - the name of the device on the bus, e.g. 0000:85:1e.5 for PCI or vpda1 for VDPA
func (k *kernel) GetDriverByBusAndDevice(bus, device string) (string, error) {
log.Log.V(2).Info("GetDriverByBusAndDevice(): get driver for device", "bus", bus, "device", device)
return getDriverByBusAndDevice(bus, device)
}
func (k *kernel) TryEnableRdma() (bool, error) {
log.Log.V(2).Info("tryEnableRdma()")
chrootDefinition := utils.GetChrootExtension()
// check if the driver is already loaded in to the system
_, stderr, mlx4Err := k.utilsHelper.RunCommand("/bin/sh", "-c", fmt.Sprintf("grep --quiet 'mlx4_en' <(%s lsmod)", chrootDefinition))
if mlx4Err != nil && len(stderr) != 0 {
log.Log.Error(mlx4Err, "tryEnableRdma(): failed to check for kernel module 'mlx4_en'", "stderr", stderr)
return false, fmt.Errorf(stderr)
}
_, stderr, mlx5Err := k.utilsHelper.RunCommand("/bin/sh", "-c", fmt.Sprintf("grep --quiet 'mlx5_core' <(%s lsmod)", chrootDefinition))
if mlx5Err != nil && len(stderr) != 0 {
log.Log.Error(mlx5Err, "tryEnableRdma(): failed to check for kernel module 'mlx5_core'", "stderr", stderr)
return false, fmt.Errorf(stderr)
}
if mlx4Err != nil && mlx5Err != nil {
log.Log.Error(nil, "tryEnableRdma(): no RDMA capable devices")
return false, nil
}
isRhelSystem, err := k.IsRHELSystem()
if err != nil {
log.Log.Error(err, "tryEnableRdma(): failed to check if the machine is base on RHEL")
return false, err
}
// RHEL check
if isRhelSystem {
return k.EnableRDMAOnRHELMachine()
}
isUbuntuSystem, err := k.IsUbuntuSystem()
if err != nil {
log.Log.Error(err, "tryEnableRdma(): failed to check if the machine is base on Ubuntu")
return false, err
}
if isUbuntuSystem {
return k.EnableRDMAOnUbuntuMachine()
}
osName, err := k.GetOSPrettyName()
if err != nil {
log.Log.Error(err, "tryEnableRdma(): failed to check OS name")
return false, err
}
log.Log.Error(nil, "tryEnableRdma(): Unsupported OS", "name", osName)
return false, fmt.Errorf("unable to load RDMA unsupported OS: %s", osName)
}
func (k *kernel) EnableRDMAOnRHELMachine() (bool, error) {
log.Log.Info("EnableRDMAOnRHELMachine()")
isCoreOsSystem, err := k.IsCoreOS()
if err != nil {
log.Log.Error(err, "EnableRDMAOnRHELMachine(): failed to check if the machine runs CoreOS")
return false, err
}
// CoreOS check
if isCoreOsSystem {
isRDMALoaded, err := k.RdmaIsLoaded()
if err != nil {
log.Log.Error(err, "EnableRDMAOnRHELMachine(): failed to check if RDMA kernel modules are loaded")
return false, err
}
return isRDMALoaded, nil
}
// RHEL
log.Log.Info("EnableRDMAOnRHELMachine(): enabling RDMA on RHEL machine")
isRDMAEnable, err := k.EnableRDMA(internal.RhelRDMAConditionFile, internal.RhelRDMAServiceName, internal.RhelPackageManager)
if err != nil {
log.Log.Error(err, "EnableRDMAOnRHELMachine(): failed to enable RDMA on RHEL machine")
return false, err
}
// check if we need to install rdma-core package
if isRDMAEnable {
isRDMALoaded, err := k.RdmaIsLoaded()
if err != nil {
log.Log.Error(err, "EnableRDMAOnRHELMachine(): failed to check if RDMA kernel modules are loaded")
return false, err
}
// if ib kernel module is not loaded trigger a loading
if isRDMALoaded {
err = k.TriggerUdevEvent()
if err != nil {
log.Log.Error(err, "EnableRDMAOnRHELMachine() failed to trigger udev event")
return false, err
}
}
}
return true, nil
}
func (k *kernel) EnableRDMAOnUbuntuMachine() (bool, error) {
log.Log.Info("EnableRDMAOnUbuntuMachine(): enabling RDMA on RHEL machine")
isRDMAEnable, err := k.EnableRDMA(internal.UbuntuRDMAConditionFile, internal.UbuntuRDMAServiceName, internal.UbuntuPackageManager)
if err != nil {
log.Log.Error(err, "EnableRDMAOnUbuntuMachine(): failed to enable RDMA on Ubuntu machine")
return false, err
}
// check if we need to install rdma-core package
if isRDMAEnable {
isRDMALoaded, err := k.RdmaIsLoaded()
if err != nil {
log.Log.Error(err, "EnableRDMAOnUbuntuMachine(): failed to check if RDMA kernel modules are loaded")
return false, err
}
// if ib kernel module is not loaded trigger a loading
if isRDMALoaded {
err = k.TriggerUdevEvent()
if err != nil {
log.Log.Error(err, "EnableRDMAOnUbuntuMachine() failed to trigger udev event")
return false, err
}
}
}
return true, nil
}
func (k *kernel) IsRHELSystem() (bool, error) {
log.Log.Info("IsRHELSystem(): checking for RHEL machine")
path := internal.RedhatReleaseFile
if !vars.UsingSystemdMode {
path = filepath.Join(internal.HostPathFromDaemon, path)
}
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
log.Log.V(2).Info("IsRHELSystem() not a RHEL machine")
return false, nil
}
log.Log.Error(err, "IsRHELSystem() failed to check for os release file", "path", path)
return false, err
}
return true, nil
}
func (k *kernel) IsCoreOS() (bool, error) {
log.Log.Info("IsCoreOS(): checking for CoreOS machine")
path := internal.RedhatReleaseFile
if !vars.UsingSystemdMode {
path = filepath.Join(internal.HostPathFromDaemon, path)
}
data, err := os.ReadFile(path)
if err != nil {
log.Log.Error(err, "IsCoreOS(): failed to read RHEL release file on path", "path", path)
return false, err
}
if strings.Contains(string(data), "CoreOS") {
return true, nil
}
return false, nil
}
func (k *kernel) IsUbuntuSystem() (bool, error) {
log.Log.Info("IsUbuntuSystem(): checking for Ubuntu machine")
path := internal.GenericOSReleaseFile
if !vars.UsingSystemdMode {
path = filepath.Join(internal.HostPathFromDaemon, path)
}
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
log.Log.Error(nil, "IsUbuntuSystem() os-release on path doesn't exist", "path", path)
return false, err
}
log.Log.Error(err, "IsUbuntuSystem() failed to check for os release file", "path", path)
return false, err
}
stdout, stderr, err := k.utilsHelper.RunCommand("/bin/sh", "-c", fmt.Sprintf("grep -i --quiet 'ubuntu' %s", path))
if err != nil && len(stderr) != 0 {
log.Log.Error(err, "IsUbuntuSystem(): failed to check for ubuntu operating system name in os-releasae file", "stderr", stderr)
return false, fmt.Errorf(stderr)
}
if len(stdout) > 0 {
return true, nil
}
return false, nil
}
func (k *kernel) RdmaIsLoaded() (bool, error) {
log.Log.V(2).Info("RdmaIsLoaded()")
chrootDefinition := utils.GetChrootExtension()
// check if the driver is already loaded in to the system
_, stderr, err := k.utilsHelper.RunCommand("/bin/sh", "-c", fmt.Sprintf("grep --quiet '\\(^ib\\|^rdma\\)' <(%s lsmod)", chrootDefinition))
if err != nil && len(stderr) != 0 {
log.Log.Error(err, "RdmaIsLoaded(): fail to check if ib and rdma kernel modules are loaded", "stderr", stderr)
return false, fmt.Errorf(stderr)
}
if err != nil {
return false, nil
}
return true, nil
}
func (k *kernel) EnableRDMA(conditionFilePath, serviceName, packageManager string) (bool, error) {
path := conditionFilePath
if !vars.UsingSystemdMode {
path = filepath.Join(internal.HostPathFromDaemon, path)
}
log.Log.Info("EnableRDMA(): checking for service file", "path", path)
if _, err := os.Stat(path); err != nil {
if os.IsNotExist(err) {
log.Log.V(2).Info("EnableRDMA(): RDMA server doesn't exist")
err = k.InstallRDMA(packageManager)
if err != nil {
log.Log.Error(err, "EnableRDMA() failed to install RDMA package")
return false, err
}
err = k.TriggerUdevEvent()
if err != nil {
log.Log.Error(err, "EnableRDMA() failed to trigger udev event")
return false, err
}
return false, nil
}
log.Log.Error(err, "EnableRDMA() failed to check for os release file", "path", path)
return false, err
}
log.Log.Info("EnableRDMA(): service installed", "name", serviceName)
return true, nil
}
func (k *kernel) InstallRDMA(packageManager string) error {
log.Log.Info("InstallRDMA(): installing RDMA")
chrootDefinition := utils.GetChrootExtension()
stdout, stderr, err := k.utilsHelper.RunCommand("/bin/sh", "-c", fmt.Sprintf("%s %s install -y rdma-core", chrootDefinition, packageManager))
if err != nil && len(stderr) != 0 {
log.Log.Error(err, "InstallRDMA(): failed to install RDMA package", "stdout", stdout, "stderr", stderr)
return err
}
return nil
}
func (k *kernel) TriggerUdevEvent() error {
log.Log.Info("TriggerUdevEvent(): installing RDMA")
err := k.ReloadDriver("mlx4_en")
if err != nil {
return err
}
err = k.ReloadDriver("mlx5_core")
if err != nil {
return err
}
return nil
}
func (k *kernel) ReloadDriver(driverName string) error {
log.Log.Info("ReloadDriver(): reload driver", "name", driverName)
chrootDefinition := utils.GetChrootExtension()
_, stderr, err := k.utilsHelper.RunCommand("/bin/sh", "-c", fmt.Sprintf("%s modprobe -r %s && %s modprobe %s", chrootDefinition, driverName, chrootDefinition, driverName))
if err != nil && len(stderr) != 0 {
log.Log.Error(err, "ReloadDriver(): failed to reload kernel module",
"name", driverName, "stderr", stderr)
return err
}
return nil
}
func (k *kernel) GetOSPrettyName() (string, error) {
path := internal.GenericOSReleaseFile
if !vars.UsingSystemdMode {
path = filepath.Join(internal.HostPathFromDaemon, path)
}
log.Log.Info("GetOSPrettyName(): getting os name from os-release file")
stdout, stderr, err := k.utilsHelper.RunCommand("/bin/sh", "-c", fmt.Sprintf("cat %s | grep PRETTY_NAME | cut -c 13-", path))
if err != nil && len(stderr) != 0 {
log.Log.Error(err, "GetOSPrettyName(): failed to check for operating system name in os-release file", "stderr", stderr)
return "", fmt.Errorf(stderr)
}
if len(stdout) > 0 {
return stdout, nil
}
return "", fmt.Errorf("failed to find pretty operating system name")
}
// IsKernelLockdownMode returns true when kernel lockdown mode is enabled
// TODO: change this to return error
func (k *kernel) IsKernelLockdownMode() bool {
path := utils.GetHostExtension()
path = filepath.Join(path, "/sys/kernel/security/lockdown")
stdout, stderr, err := k.utilsHelper.RunCommand("cat", path)
log.Log.V(2).Info("IsKernelLockdownMode()", "output", stdout, "error", err)
if err != nil {
log.Log.Error(err, "IsKernelLockdownMode(): failed to check for lockdown file", "stderr", stderr)
return false
}
return strings.Contains(stdout, "[integrity]") || strings.Contains(stdout, "[confidentiality]")
}
// returns driver for device on the bus
func getDriverByBusAndDevice(bus, device string) (string, error) {
driverLink := filepath.Join(vars.FilesystemRoot, consts.SysBus, bus, "devices", device, "driver")
driverInfo, err := os.Readlink(driverLink)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
log.Log.V(2).Info("getDriverByBusAndDevice(): driver path for device not exist", "bus", bus, "device", device, "driver", driverInfo)
return "", nil
}
log.Log.Error(err, "getDriverByBusAndDevice(): error getting driver info for device", "bus", bus, "device", device)
return "", err
}
log.Log.V(2).Info("getDriverByBusAndDevice(): driver for device", "bus", bus, "device", device, "driver", driverInfo)
return filepath.Base(driverInfo), nil
}
// binds device to the provide driver
func bindDriver(bus, device, driver string) error {
log.Log.V(2).Info("bindDriver(): bind to driver", "bus", bus, "device", device, "driver", driver)
bindPath := filepath.Join(vars.FilesystemRoot, consts.SysBus, bus, "drivers", driver, "bind")
err := os.WriteFile(bindPath, []byte(device), os.ModeAppend)
if err != nil {
log.Log.Error(err, "bindDriver(): failed to bind driver", "bus", bus, "device", device, "driver", driver)
return err
}
return nil
}
// unbind device from the driver
func unbindDriver(bus, device, driver string) error {
log.Log.V(2).Info("unbindDriver(): unbind from driver", "bus", bus, "device", device, "driver", driver)
unbindPath := filepath.Join(vars.FilesystemRoot, consts.SysBus, bus, "drivers", driver, "unbind")
err := os.WriteFile(unbindPath, []byte(device), os.ModeAppend)
if err != nil {
log.Log.Error(err, "unbindDriver(): failed to unbind driver", "bus", bus, "device", device, "driver", driver)
return err
}
return nil
}
// probes driver for device on the bus
func probeDriver(bus, device string) error {
log.Log.V(2).Info("probeDriver(): drivers probe", "bus", bus, "device", device)
probePath := filepath.Join(vars.FilesystemRoot, consts.SysBus, bus, "drivers_probe")
err := os.WriteFile(probePath, []byte(device), os.ModeAppend)
if err != nil {
log.Log.Error(err, "probeDriver(): failed to trigger driver probe", "bus", bus, "device", device)
return err
}
return nil
}
// set driver override for the bus/device,
// resets override if override arg is "",
// if device doesn't support overriding (has no driver_override path), does nothing
func setDriverOverride(bus, device, override string) error {
driverOverridePath := filepath.Join(vars.FilesystemRoot, consts.SysBus, bus, "devices", device, "driver_override")
if _, err := os.Stat(driverOverridePath); err != nil {
if os.IsNotExist(err) {
log.Log.V(2).Info("setDriverOverride(): device doesn't support driver override, skip", "bus", bus, "device", device)
return nil
}
return err
}
var overrideData []byte
if override != "" {
log.Log.V(2).Info("setDriverOverride(): configure driver override for device", "bus", bus, "device", device, "driver", override)
overrideData = []byte(override)
} else {
log.Log.V(2).Info("setDriverOverride(): reset driver override for device", "bus", bus, "device", device)
overrideData = []byte("\x00")
}
err := os.WriteFile(driverOverridePath, overrideData, os.ModeAppend)
if err != nil {
log.Log.Error(err, "setDriverOverride(): fail to write driver_override for device",
"bus", bus, "device", device, "driver", override)
return err
}
return nil
}