2023-01-20 14:58:04 +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
|
2023-01-20 14:58:04 +00:00
|
|
|
|
|
|
|
import (
|
2023-07-18 12:12:33 +00:00
|
|
|
"errors"
|
2023-01-20 14:58:04 +00:00
|
|
|
"fmt"
|
|
|
|
|
2023-11-15 20:36:23 +00:00
|
|
|
"github.com/NVIDIA/go-nvlib/pkg/nvml"
|
2023-01-20 14:58:04 +00:00
|
|
|
)
|
|
|
|
|
2023-07-18 12:12:33 +00:00
|
|
|
// UUIDer is an interface for getting UUIDs.
|
|
|
|
type UUIDer interface {
|
|
|
|
GetUUID() (string, error)
|
|
|
|
}
|
|
|
|
|
2022-12-02 13:17:52 +00:00
|
|
|
// DeviceNamer is an interface for getting device names
|
|
|
|
type DeviceNamer interface {
|
2023-07-18 12:12:33 +00:00
|
|
|
GetDeviceName(int, UUIDer) (string, error)
|
|
|
|
GetMigDeviceName(int, UUIDer, int, UUIDer) (string, error)
|
2023-01-20 14:58:04 +00:00
|
|
|
}
|
|
|
|
|
2022-12-02 13:17:52 +00:00
|
|
|
// Supported device naming strategies
|
2023-01-20 14:58:04 +00:00
|
|
|
const (
|
2022-12-02 13:17:52 +00:00
|
|
|
// DeviceNameStrategyIndex generates devices names such as 0 or 1:0
|
|
|
|
DeviceNameStrategyIndex = "index"
|
|
|
|
// DeviceNameStrategyTypeIndex generates devices names such as gpu0 or mig1:0
|
|
|
|
DeviceNameStrategyTypeIndex = "type-index"
|
|
|
|
// DeviceNameStrategyUUID uses the device UUID as the name
|
|
|
|
DeviceNameStrategyUUID = "uuid"
|
2023-01-20 14:58:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type deviceNameIndex struct {
|
|
|
|
gpuPrefix string
|
|
|
|
migPrefix string
|
|
|
|
}
|
|
|
|
type deviceNameUUID struct{}
|
|
|
|
|
2022-12-02 13:17:52 +00:00
|
|
|
// NewDeviceNamer creates a Device Namer based on the supplied strategy.
|
2023-01-20 14:58:04 +00:00
|
|
|
// This namer can be used to construct the names for MIG and GPU devices when generating the CDI spec.
|
2022-12-02 13:17:52 +00:00
|
|
|
func NewDeviceNamer(strategy string) (DeviceNamer, error) {
|
2023-01-20 14:58:04 +00:00
|
|
|
switch strategy {
|
2022-12-02 13:17:52 +00:00
|
|
|
case DeviceNameStrategyIndex:
|
2023-01-20 14:58:04 +00:00
|
|
|
return deviceNameIndex{}, nil
|
2022-12-02 13:17:52 +00:00
|
|
|
case DeviceNameStrategyTypeIndex:
|
2023-01-20 14:58:04 +00:00
|
|
|
return deviceNameIndex{gpuPrefix: "gpu", migPrefix: "mig"}, nil
|
2022-12-02 13:17:52 +00:00
|
|
|
case DeviceNameStrategyUUID:
|
2023-01-20 14:58:04 +00:00
|
|
|
return deviceNameUUID{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("invalid device name strategy: %v", strategy)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDeviceName returns the name for the specified device based on the naming strategy
|
2023-07-18 12:12:33 +00:00
|
|
|
func (s deviceNameIndex) GetDeviceName(i int, _ UUIDer) (string, error) {
|
2023-01-20 14:58:04 +00:00
|
|
|
return fmt.Sprintf("%s%d", s.gpuPrefix, i), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMigDeviceName returns the name for the specified device based on the naming strategy
|
2023-07-18 12:12:33 +00:00
|
|
|
func (s deviceNameIndex) GetMigDeviceName(i int, _ UUIDer, j int, _ UUIDer) (string, error) {
|
2023-01-20 14:58:04 +00:00
|
|
|
return fmt.Sprintf("%s%d:%d", s.migPrefix, i, j), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetDeviceName returns the name for the specified device based on the naming strategy
|
2023-07-18 12:12:33 +00:00
|
|
|
func (s deviceNameUUID) GetDeviceName(i int, d UUIDer) (string, error) {
|
|
|
|
uuid, err := d.GetUUID()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to get device UUID: %v", err)
|
2023-01-20 14:58:04 +00:00
|
|
|
}
|
|
|
|
return uuid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetMigDeviceName returns the name for the specified device based on the naming strategy
|
2023-07-18 12:12:33 +00:00
|
|
|
func (s deviceNameUUID) GetMigDeviceName(i int, _ UUIDer, j int, mig UUIDer) (string, error) {
|
|
|
|
uuid, err := mig.GetUUID()
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to get device UUID: %v", err)
|
|
|
|
}
|
|
|
|
return uuid, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:generate moq -stub -out namer_nvml_mock.go . nvmlUUIDer
|
|
|
|
type nvmlUUIDer interface {
|
|
|
|
GetUUID() (string, nvml.Return)
|
|
|
|
}
|
|
|
|
|
|
|
|
type convert struct {
|
|
|
|
nvmlUUIDer
|
|
|
|
}
|
|
|
|
|
|
|
|
type uuidUnsupported struct{}
|
|
|
|
|
|
|
|
func (m convert) GetUUID() (string, error) {
|
|
|
|
if m.nvmlUUIDer == nil {
|
|
|
|
return uuidUnsupported{}.GetUUID()
|
|
|
|
}
|
|
|
|
uuid, ret := m.nvmlUUIDer.GetUUID()
|
2023-01-20 14:58:04 +00:00
|
|
|
if ret != nvml.SUCCESS {
|
2023-07-18 12:12:33 +00:00
|
|
|
return "", ret
|
2023-01-20 14:58:04 +00:00
|
|
|
}
|
|
|
|
return uuid, nil
|
|
|
|
}
|
2023-07-18 12:12:33 +00:00
|
|
|
|
|
|
|
var errUUIDUnsupported = errors.New("GetUUID is not supported")
|
|
|
|
|
|
|
|
func (m uuidUnsupported) GetUUID() (string, error) {
|
|
|
|
return "", errUUIDUnsupported
|
|
|
|
}
|