|
| 1 | +/** |
| 2 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | +**/ |
| 16 | + |
| 17 | +package nvdevices |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "path/filepath" |
| 22 | + "strings" |
| 23 | + |
| 24 | + "github.com/NVIDIA/nvidia-container-toolkit/internal/info/proc/devices" |
| 25 | +) |
| 26 | + |
| 27 | +// A controlDeviceNode represents an NVIDIA devices node for control or meta devices. |
| 28 | +// Such device nodes are typically required regardless of which GPU is being accessed. |
| 29 | +type controlDeviceNode string |
| 30 | + |
| 31 | +func (c controlDeviceNode) path() string { |
| 32 | + return filepath.Join("dev", string(c)) |
| 33 | +} |
| 34 | + |
| 35 | +// CreateNVIDIAControlDevices creates the NVIDIA control device nodes at the configured devRoot. |
| 36 | +func (m *Interface) CreateNVIDIAControlDevices() error { |
| 37 | + controlNodes := []controlDeviceNode{"nvidiactl", "nvidia-modeset", "nvidia-uvm", "nvidia-uvm-tools"} |
| 38 | + for _, node := range controlNodes { |
| 39 | + if err := m.createControlDeviceNode(node); err != nil { |
| 40 | + return fmt.Errorf("failed to create device node %s: %w", node, err) |
| 41 | + } |
| 42 | + } |
| 43 | + return nil |
| 44 | +} |
| 45 | + |
| 46 | +// createControlDeviceNode creates the specified NVIDIA device node at the configured devRoot. |
| 47 | +func (m *Interface) createControlDeviceNode(node controlDeviceNode) error { |
| 48 | + if !strings.HasPrefix(string(node), "nvidia") { |
| 49 | + return fmt.Errorf("invalid device node %q: %w", node, errInvalidDeviceNode) |
| 50 | + } |
| 51 | + |
| 52 | + major, err := m.controlDeviceNodeMajor(node) |
| 53 | + if err != nil { |
| 54 | + return fmt.Errorf("failed to determine major: %w", err) |
| 55 | + } |
| 56 | + |
| 57 | + minor, err := m.controlDeviceNodeMinor(node) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("failed to determine minor: %w", err) |
| 60 | + } |
| 61 | + |
| 62 | + return m.createDeviceNode(node.path(), int(major), int(minor)) |
| 63 | +} |
| 64 | + |
| 65 | +// controlDeviceNodeMajor returns the major number for the specified NVIDIA control device node. |
| 66 | +// If the device node is not supported, an error is returned. |
| 67 | +func (m *Interface) controlDeviceNodeMajor(node controlDeviceNode) (int64, error) { |
| 68 | + var valid bool |
| 69 | + var major devices.Major |
| 70 | + switch node { |
| 71 | + case "nvidia-uvm", "nvidia-uvm-tools": |
| 72 | + major, valid = m.Get(devices.NVIDIAUVM) |
| 73 | + case "nvidia-modeset", "nvidiactl": |
| 74 | + major, valid = m.Get(devices.NVIDIAGPU) |
| 75 | + } |
| 76 | + |
| 77 | + if valid { |
| 78 | + return int64(major), nil |
| 79 | + } |
| 80 | + |
| 81 | + return 0, errInvalidDeviceNode |
| 82 | +} |
| 83 | + |
| 84 | +// controlDeviceNodeMinor returns the minor number for the specified NVIDIA control device node. |
| 85 | +// If the device node is not supported, an error is returned. |
| 86 | +func (m *Interface) controlDeviceNodeMinor(node controlDeviceNode) (int64, error) { |
| 87 | + switch node { |
| 88 | + case "nvidia-modeset": |
| 89 | + return devices.NVIDIAModesetMinor, nil |
| 90 | + case "nvidia-uvm-tools": |
| 91 | + return devices.NVIDIAUVMToolsMinor, nil |
| 92 | + case "nvidia-uvm": |
| 93 | + return devices.NVIDIAUVMMinor, nil |
| 94 | + case "nvidiactl": |
| 95 | + return devices.NVIDIACTLMinor, nil |
| 96 | + } |
| 97 | + |
| 98 | + return 0, errInvalidDeviceNode |
| 99 | +} |
0 commit comments