Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/nvidia.com/resource/v1beta1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const (

GpuConfigKind = "GpuConfig"
MigDeviceConfigKind = "MigDeviceConfig"
VfioDeviceConfigKind = "VfioDeviceConfig"
ComputeDomainChannelConfigKind = "ComputeDomainChannelConfig"
ComputeDomainDaemonConfigKind = "ComputeDomainDaemonConfig"
ComputeDomainKind = "ComputeDomain"
Expand Down Expand Up @@ -66,6 +67,7 @@ func init() {
scheme.AddKnownTypes(schemeGroupVersion,
&GpuConfig{},
&MigDeviceConfig{},
&VfioDeviceConfig{},
&ComputeDomainChannelConfig{},
&ComputeDomainDaemonConfig{},
&ComputeDomain{},
Expand Down
53 changes: 53 additions & 0 deletions api/nvidia.com/resource/v1beta1/vfiodeviceconfig.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* Copyright (c) 2025, 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 v1beta1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/NVIDIA/k8s-dra-driver-gpu/pkg/featuregates"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// VfioDeviceConfig holds the set of parameters for configuring a VFIO device.
type VfioDeviceConfig struct {
metav1.TypeMeta `json:",inline"`
}

// DefaultVfioDeviceConfig provides the default configuration of a VFIO device.
func DefaultVfioDeviceConfig() *VfioDeviceConfig {
if !featuregates.Enabled(featuregates.PassthroughSupport) {
return nil
}
return &VfioDeviceConfig{
TypeMeta: metav1.TypeMeta{
APIVersion: GroupName + "/" + Version,
Kind: VfioDeviceConfigKind,
},
}
}

// Normalize updates a VfioDeviceConfig config with implied default values based on other settings.
func (c *VfioDeviceConfig) Normalize() error {
return nil
}

// Validate ensures that VfioDeviceConfig has a valid set of values.
func (c *VfioDeviceConfig) Validate() error {
return nil
}
24 changes: 24 additions & 0 deletions api/nvidia.com/resource/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

127 changes: 122 additions & 5 deletions cmd/gpu-kubelet-plugin/allocatable.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, NVIDIA CORPORATION. All rights reserved.
* Copyright (c) 2022-2025, 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.
Expand All @@ -22,11 +22,10 @@ import (
resourceapi "k8s.io/api/resource/v1"
)

type AllocatableDevices map[string]*AllocatableDevice

type AllocatableDevice struct {
Gpu *GpuInfo
Mig *MigDeviceInfo
Gpu *GpuInfo
Mig *MigDeviceInfo
Vfio *VfioDeviceInfo
}

func (d AllocatableDevice) Type() string {
Expand All @@ -36,6 +35,9 @@ func (d AllocatableDevice) Type() string {
if d.Mig != nil {
return MigDeviceType
}
if d.Vfio != nil {
return VfioDeviceType
}
return UnknownDeviceType
}

Expand All @@ -45,6 +47,8 @@ func (d *AllocatableDevice) CanonicalName() string {
return d.Gpu.CanonicalName()
case MigDeviceType:
return d.Mig.CanonicalName()
case VfioDeviceType:
return d.Vfio.CanonicalName()
}
panic("unexpected type for AllocatableDevice")
}
Expand All @@ -55,6 +59,8 @@ func (d *AllocatableDevice) GetDevice() resourceapi.Device {
return d.Gpu.GetDevice()
case MigDeviceType:
return d.Mig.GetDevice()
case VfioDeviceType:
return d.Vfio.GetDevice()
}
panic("unexpected type for AllocatableDevice")
}
Expand All @@ -66,9 +72,79 @@ func (d AllocatableDevice) UUID() string {
if d.Mig != nil {
return d.Mig.UUID
}
if d.Vfio != nil {
return d.Vfio.UUID
}
panic("unexpected type for AllocatableDevice")
}

type AllocatableDeviceList []*AllocatableDevice

type AllocatableDevices map[string]*AllocatableDevice

func (d AllocatableDevices) getDevicesByGPUPCIBusID(pcieBusID string) AllocatableDeviceList {
var devices AllocatableDeviceList
for _, device := range d {
switch device.Type() {
case GpuDeviceType:
if device.Gpu.pcieBusID == pcieBusID {
devices = append(devices, device)
}
case MigDeviceType:
if device.Mig.parent.pcieBusID == pcieBusID {
devices = append(devices, device)
}
case VfioDeviceType:
if device.Vfio.pcieBusID == pcieBusID {
devices = append(devices, device)
}
}
}
return devices
}

func (d AllocatableDevices) GetGPUByPCIeBusID(pcieBusID string) *AllocatableDevice {
for _, device := range d {
if device.Type() != GpuDeviceType {
continue
}
if device.Gpu.pcieBusID == pcieBusID {
return device
}
}
return nil
}

func (d AllocatableDevices) GetGPUs() AllocatableDeviceList {
var devices AllocatableDeviceList
for _, device := range d {
if device.Type() == GpuDeviceType {
devices = append(devices, device)
}
}
return devices
}

func (d AllocatableDevices) GetMigDevices() AllocatableDeviceList {
var devices AllocatableDeviceList
for _, device := range d {
if device.Type() == MigDeviceType {
devices = append(devices, device)
}
}
return devices
}

func (d AllocatableDevices) GetVfioDevices() AllocatableDeviceList {
var devices AllocatableDeviceList
for _, device := range d {
if device.Type() == VfioDeviceType {
devices = append(devices, device)
}
}
return devices
}

func (d AllocatableDevices) GpuUUIDs() []string {
var uuids []string
for _, device := range d {
Expand All @@ -91,8 +167,49 @@ func (d AllocatableDevices) MigDeviceUUIDs() []string {
return uuids
}

func (d AllocatableDevices) VfioDeviceUUIDs() []string {
var uuids []string
for _, device := range d {
if device.Type() == VfioDeviceType {
uuids = append(uuids, device.Vfio.UUID)
}
}
slices.Sort(uuids)
return uuids
}

func (d AllocatableDevices) UUIDs() []string {
uuids := append(d.GpuUUIDs(), d.MigDeviceUUIDs()...)
uuids = append(uuids, d.VfioDeviceUUIDs()...)
slices.Sort(uuids)
return uuids
}

func (d AllocatableDevices) RemoveSiblingDevices(device *AllocatableDevice) {
var pciBusID string
switch device.Type() {
case GpuDeviceType:
pciBusID = device.Gpu.pcieBusID
case VfioDeviceType:
pciBusID = device.Vfio.pcieBusID
case MigDeviceType:
// TODO: Implement once dynamic MIG is supported.
return
}

siblings := d.getDevicesByGPUPCIBusID(pciBusID)
for _, sibling := range siblings {
if sibling.Type() == device.Type() {
continue
}
switch sibling.Type() {
case GpuDeviceType:
delete(d, sibling.Gpu.CanonicalName())
case VfioDeviceType:
delete(d, sibling.Vfio.CanonicalName())
case MigDeviceType:
// TODO: Implement once dynamic MIG is supported.
continue
}
}
}
Loading