From 311e7a1feb142652092240bc86a677ed25f450bc Mon Sep 17 00:00:00 2001 From: Evan Lezar Date: Mon, 12 Dec 2022 14:48:54 +0100 Subject: [PATCH] Ensure existence of DRM devices nodes is checked Signed-off-by: Evan Lezar --- cmd/nvidia-ctk/cdi/generate/device.go | 57 ----------------------- cmd/nvidia-ctk/cdi/generate/full-gpu.go | 43 +++++++++++------ cmd/nvidia-ctk/cdi/generate/mig-device.go | 27 ++++------- 3 files changed, 37 insertions(+), 90 deletions(-) delete mode 100644 cmd/nvidia-ctk/cdi/generate/device.go diff --git a/cmd/nvidia-ctk/cdi/generate/device.go b/cmd/nvidia-ctk/cdi/generate/device.go deleted file mode 100644 index 3d908ace..00000000 --- a/cmd/nvidia-ctk/cdi/generate/device.go +++ /dev/null @@ -1,57 +0,0 @@ -/** -# 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 generate - -import ( - "path/filepath" - - "github.com/NVIDIA/nvidia-container-toolkit/internal/discover" - "github.com/sirupsen/logrus" -) - -// deviceDiscoverer defines a discoverer for device nodes -type deviceDiscoverer struct { - logger *logrus.Logger - root string - deviceNodePaths []string -} - -var _ discover.Discover = (*deviceDiscoverer)(nil) - -// Devices returns the device nodes for the full GPU. -func (d *deviceDiscoverer) Devices() ([]discover.Device, error) { - var deviceNodes []discover.Device - for _, dn := range d.deviceNodePaths { - deviceNode := discover.Device{ - HostPath: filepath.Join(d.root, dn), - Path: dn, - } - deviceNodes = append(deviceNodes, deviceNode) - } - - return deviceNodes, nil -} - -// Hooks returns no hooks for a device discoverer -func (d *deviceDiscoverer) Hooks() ([]discover.Hook, error) { - return nil, nil -} - -// Mounts returns no mounts for a device discoverer -func (d *deviceDiscoverer) Mounts() ([]discover.Mount, error) { - return nil, nil -} diff --git a/cmd/nvidia-ctk/cdi/generate/full-gpu.go b/cmd/nvidia-ctk/cdi/generate/full-gpu.go index e9de7cb8..71fc7ce1 100644 --- a/cmd/nvidia-ctk/cdi/generate/full-gpu.go +++ b/cmd/nvidia-ctk/cdi/generate/full-gpu.go @@ -29,14 +29,14 @@ import ( "gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvml" ) -// fullGPUDiscoverer wraps a deviceDiscoverer and adds specifics required for discovering full GPUs -type fullGPUDiscoverer struct { - deviceDiscoverer - +// byPathHookDiscoverer discovers the entities required for injecting by-path DRM device links +type byPathHookDiscoverer struct { + logger *logrus.Logger + root string pciBusID string } -var _ discover.Discover = (*fullGPUDiscoverer)(nil) +var _ discover.Discover = (*byPathHookDiscoverer)(nil) // NewFullGPUDiscoverer creates a discoverer for the full GPU defined by the specified device. func NewFullGPUDiscoverer(logger *logrus.Logger, root string, d device.Device) (discover.Discover, error) { @@ -61,22 +61,35 @@ func NewFullGPUDiscoverer(logger *logrus.Logger, root string, d device.Device) ( deviceNodePaths := append([]string{path}, drmDeviceNodes...) - device := fullGPUDiscoverer{ - deviceDiscoverer: deviceDiscoverer{ - logger: logger, - root: root, - deviceNodePaths: deviceNodePaths, - }, + deviceNodes := discover.NewCharDeviceDiscoverer( + logger, + deviceNodePaths, + root, + ) + + byPathHooks := &byPathHookDiscoverer{ + logger: logger, + root: root, pciBusID: pciBusID, } - return &device, nil + dd := discover.Merge( + deviceNodes, + byPathHooks, + ) + + return dd, nil +} + +// Devices returns the empty list for the by-path hook discoverer +func (d *byPathHookDiscoverer) Devices() ([]discover.Device, error) { + return nil, nil } // Hooks returns the hooks for the GPU device. // The following hooks are detected: // 1. A hook to create /dev/dri/by-path symlinks -func (d *fullGPUDiscoverer) Hooks() ([]discover.Hook, error) { +func (d *byPathHookDiscoverer) Hooks() ([]discover.Hook, error) { links, err := d.deviceNodeLinks() if err != nil { return nil, fmt.Errorf("failed to discover DRA device links: %v", err) @@ -103,11 +116,11 @@ func (d *fullGPUDiscoverer) Hooks() ([]discover.Hook, error) { } // Mounts returns an empty slice for a full GPU -func (d *fullGPUDiscoverer) Mounts() ([]discover.Mount, error) { +func (d *byPathHookDiscoverer) Mounts() ([]discover.Mount, error) { return nil, nil } -func (d *fullGPUDiscoverer) deviceNodeLinks() ([]string, error) { +func (d *byPathHookDiscoverer) deviceNodeLinks() ([]string, error) { candidates := []string{ fmt.Sprintf("/dev/dri/by-path/pci-%s-card", d.pciBusID), fmt.Sprintf("/dev/dri/by-path/pci-%s-render", d.pciBusID), diff --git a/cmd/nvidia-ctk/cdi/generate/mig-device.go b/cmd/nvidia-ctk/cdi/generate/mig-device.go index 76a4df1f..f90ea413 100644 --- a/cmd/nvidia-ctk/cdi/generate/mig-device.go +++ b/cmd/nvidia-ctk/cdi/generate/mig-device.go @@ -26,13 +26,6 @@ import ( "gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvml" ) -// migDeviceDiscoverer wraps a deviceDiscoverer and adds specifics required for discovering MIG devices. -type migDeviceDiscoverer struct { - deviceDiscoverer -} - -var _ discover.Discover = (*migDeviceDiscoverer)(nil) - // NewMigDeviceDiscoverer creates a discoverer for the specified mig device and its parent. func NewMigDeviceDiscoverer(logger *logrus.Logger, root string, parent device.Device, d device.MigDevice) (discover.Discover, error) { minor, ret := parent.GetMinorNumber() @@ -68,17 +61,15 @@ func NewMigDeviceDiscoverer(logger *logrus.Logger, root string, parent device.De return nil, fmt.Errorf("failed to get CI cap device path: %v", err) } - m := migDeviceDiscoverer{ - deviceDiscoverer: deviceDiscoverer{ - logger: logger, - root: root, - deviceNodePaths: []string{ - parentPath, - giCapDevicePath, - ciCapDevicePath, - }, + deviceNodes := discover.NewCharDeviceDiscoverer( + logger, + []string{ + parentPath, + giCapDevicePath, + ciCapDevicePath, }, - } + root, + ) - return &m, nil + return deviceNodes, nil }