2022-11-23 15:29:18 +00:00
|
|
|
/**
|
|
|
|
# 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.
|
|
|
|
**/
|
|
|
|
|
2022-12-02 13:17:52 +00:00
|
|
|
package nvcdi
|
2022-11-23 15:29:18 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2023-11-15 20:36:23 +00:00
|
|
|
"github.com/NVIDIA/go-nvlib/pkg/nvlib/device"
|
2023-12-01 01:10:10 +00:00
|
|
|
"tags.cncf.io/container-device-interface/pkg/cdi"
|
|
|
|
"tags.cncf.io/container-device-interface/specs-go"
|
|
|
|
|
2022-11-23 15:29:18 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/discover"
|
2022-12-02 13:17:52 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/edits"
|
2024-06-11 13:12:57 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/dgpu"
|
2022-11-23 15:29:18 +00:00
|
|
|
)
|
|
|
|
|
2022-12-02 13:17:52 +00:00
|
|
|
// GetGPUDeviceSpecs returns the CDI device specs for the full GPU represented by 'device'.
|
2023-03-21 13:51:36 +00:00
|
|
|
func (l *nvmllib) GetGPUDeviceSpecs(i int, d device.Device) ([]specs.Device, error) {
|
2022-12-02 13:17:52 +00:00
|
|
|
edits, err := l.GetGPUDeviceEdits(d)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get edits for device: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-03-21 13:51:36 +00:00
|
|
|
var deviceSpecs []specs.Device
|
|
|
|
names, err := l.deviceNamers.GetDeviceNames(i, convert{d})
|
2022-12-02 13:17:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get device name: %v", err)
|
|
|
|
}
|
2023-03-21 13:51:36 +00:00
|
|
|
for _, name := range names {
|
|
|
|
spec := specs.Device{
|
|
|
|
Name: name,
|
|
|
|
ContainerEdits: *edits.ContainerEdits,
|
|
|
|
}
|
|
|
|
deviceSpecs = append(deviceSpecs, spec)
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|
|
|
|
|
2023-03-21 13:51:36 +00:00
|
|
|
return deviceSpecs, nil
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetGPUDeviceEdits returns the CDI edits for the full GPU represented by 'device'.
|
2023-02-16 15:29:53 +00:00
|
|
|
func (l *nvmllib) GetGPUDeviceEdits(d device.Device) (*cdi.ContainerEdits, error) {
|
2024-06-11 13:12:57 +00:00
|
|
|
device, err := l.newFullGPUDiscoverer(d)
|
2022-12-02 13:17:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create device discoverer: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
editsForDevice, err := edits.FromDiscoverer(device)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create container edits for device: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return editsForDevice, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newFullGPUDiscoverer creates a discoverer for the full GPU defined by the specified device.
|
2024-06-11 13:12:57 +00:00
|
|
|
func (l *nvmllib) newFullGPUDiscoverer(d device.Device) (discover.Discover, error) {
|
|
|
|
deviceNodes, err := dgpu.NewForDevice(d,
|
|
|
|
dgpu.WithDevRoot(l.devRoot),
|
|
|
|
dgpu.WithLogger(l.logger),
|
|
|
|
dgpu.WithNVIDIACDIHookPath(l.nvidiaCDIHookPath),
|
2024-09-24 17:05:14 +00:00
|
|
|
dgpu.WithNvsandboxuitilsLib(l.nvsandboxutilslib),
|
2022-12-12 13:48:54 +00:00
|
|
|
)
|
2024-06-11 13:12:57 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create device discoverer: %v", err)
|
2022-11-23 15:29:18 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 15:10:28 +00:00
|
|
|
deviceFolderPermissionHooks := newDeviceFolderPermissionHookDiscoverer(
|
2024-06-11 13:12:57 +00:00
|
|
|
l.logger,
|
|
|
|
l.devRoot,
|
|
|
|
l.nvidiaCDIHookPath,
|
2023-02-22 15:10:28 +00:00
|
|
|
deviceNodes,
|
|
|
|
)
|
|
|
|
|
2022-12-12 13:48:54 +00:00
|
|
|
dd := discover.Merge(
|
|
|
|
deviceNodes,
|
2023-02-22 15:10:28 +00:00
|
|
|
deviceFolderPermissionHooks,
|
2022-12-12 13:48:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return dd, nil
|
|
|
|
}
|