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"
|
|
|
|
|
2024-02-07 11:20:58 +00:00
|
|
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvlib/device"
|
|
|
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvml"
|
|
|
|
"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/discover"
|
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/edits"
|
2023-03-22 12:27:43 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/logger"
|
2022-12-02 13:17:52 +00:00
|
|
|
"github.com/NVIDIA/nvidia-container-toolkit/internal/nvcaps"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GetMIGDeviceSpecs returns the CDI device specs for the full GPU represented by 'device'.
|
2023-02-16 15:29:53 +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-07-18 12:12:33 +00:00
|
|
|
name, err := l.deviceNamer.GetMigDeviceName(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)
|
|
|
|
}
|
|
|
|
|
|
|
|
spec := specs.Device{
|
|
|
|
Name: name,
|
|
|
|
ContainerEdits: *edits.ContainerEdits,
|
|
|
|
}
|
|
|
|
|
|
|
|
return &spec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
2022-12-02 13:17:52 +00:00
|
|
|
gpu, ret := parent.GetMinorNumber()
|
|
|
|
if ret != nvml.SUCCESS {
|
|
|
|
return nil, fmt.Errorf("error getting GPU minor: %v", ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
gi, ret := mig.GetGpuInstanceId()
|
|
|
|
if ret != nvml.SUCCESS {
|
|
|
|
return nil, fmt.Errorf("error getting GPU Instance ID: %v", ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
ci, ret := mig.GetComputeInstanceId()
|
|
|
|
if ret != nvml.SUCCESS {
|
|
|
|
return nil, fmt.Errorf("error getting Compute Instance ID: %v", ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
editsForDevice, err := GetEditsForComputeInstance(l.logger, l.driverRoot, gpu, gi, ci)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create container edits for MIG device: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return editsForDevice, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetEditsForComputeInstance returns the CDI edits for a particular compute instance defined by the (gpu, gi, ci) tuple
|
2023-03-22 12:27:43 +00:00
|
|
|
func GetEditsForComputeInstance(logger logger.Interface, driverRoot string, gpu int, gi int, ci int) (*cdi.ContainerEdits, error) {
|
2022-12-02 13:17:52 +00:00
|
|
|
computeInstance, err := newComputeInstanceDiscoverer(logger, driverRoot, gpu, gi, ci)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create discoverer for Compute Instance: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
editsForDevice, err := edits.FromDiscoverer(computeInstance)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create container edits for Compute Instance: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return editsForDevice, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// newComputeInstanceDiscoverer returns a discoverer for the specified compute instance
|
2023-03-22 12:27:43 +00:00
|
|
|
func newComputeInstanceDiscoverer(logger logger.Interface, driverRoot string, gpu int, gi int, ci int) (discover.Discover, error) {
|
2022-12-02 13:17:52 +00:00
|
|
|
parentPath := fmt.Sprintf("/dev/nvidia%d", gpu)
|
|
|
|
|
|
|
|
migCaps, err := nvcaps.NewMigCaps()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error getting MIG capability device paths: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
giCap := nvcaps.NewGPUInstanceCap(gpu, gi)
|
|
|
|
giCapDevicePath, err := migCaps.GetCapDevicePath(giCap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get GI cap device path: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ciCap := nvcaps.NewComputeInstanceCap(gpu, gi, ci)
|
|
|
|
ciCapDevicePath, err := migCaps.GetCapDevicePath(ciCap)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to get CI cap device path: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
deviceNodes := discover.NewCharDeviceDiscoverer(
|
|
|
|
logger,
|
|
|
|
[]string{
|
|
|
|
parentPath,
|
|
|
|
giCapDevicePath,
|
|
|
|
ciCapDevicePath,
|
|
|
|
},
|
|
|
|
driverRoot,
|
|
|
|
)
|
|
|
|
|
|
|
|
return deviceNodes, nil
|
|
|
|
}
|