2022-12-02 13:17:52 +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.
|
|
|
|
**/
|
|
|
|
|
|
|
|
package nvcdi
|
|
|
|
|
|
|
|
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-12-02 13:17:52 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/edits"
|
2024-07-02 14:49:15 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/platform-support/dgpu"
|
2022-12-02 13:17:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// GetMIGDeviceSpecs returns the CDI device specs for the full GPU represented by 'device'.
|
2023-03-21 13:51:36 +00:00
|
|
|
func (l *nvmllib) GetMIGDeviceSpecs(i int, d device.Device, j int, mig device.MigDevice) ([]specs.Device, error) {
|
2022-12-02 13:17:52 +00:00
|
|
|
edits, err := l.GetMIGDeviceEdits(d, mig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get edits for device: %v", err)
|
|
|
|
}
|
|
|
|
|
2023-03-21 13:51:36 +00:00
|
|
|
names, err := l.deviceNamers.GetMigDeviceNames(i, convert{d}, j, convert{mig})
|
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
|
|
|
var deviceSpecs []specs.Device
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// GetMIGDeviceEdits returns the CDI edits for the MIG device represented by 'mig' on 'parent'.
|
2023-02-16 15:29:53 +00:00
|
|
|
func (l *nvmllib) GetMIGDeviceEdits(parent device.Device, mig device.MigDevice) (*cdi.ContainerEdits, error) {
|
2024-07-02 14:49:15 +00:00
|
|
|
deviceNodes, err := dgpu.NewForMigDevice(parent, mig,
|
|
|
|
dgpu.WithDevRoot(l.devRoot),
|
|
|
|
dgpu.WithLogger(l.logger),
|
|
|
|
dgpu.WithNVIDIACDIHookPath(l.nvidiaCDIHookPath),
|
2024-09-24 17:05:14 +00:00
|
|
|
dgpu.WithNvsandboxuitilsLib(l.nvsandboxutilslib),
|
2024-07-02 14:49:15 +00:00
|
|
|
)
|
2022-12-02 13:17:52 +00:00
|
|
|
if err != nil {
|
2024-07-02 14:49:15 +00:00
|
|
|
return nil, fmt.Errorf("failed to create device discoverer: %v", err)
|
2022-12-02 13:17:52 +00:00
|
|
|
}
|
|
|
|
|
2024-07-02 14:49:15 +00:00
|
|
|
editsForDevice, err := edits.FromDiscoverer(deviceNodes)
|
2022-12-02 13:17:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create container edits for Compute Instance: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return editsForDevice, nil
|
|
|
|
}
|