-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
hostdev.go
84 lines (72 loc) · 2.84 KB
/
hostdev.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* This file is part of the KubeVirt project
*
* 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.
*
* Copyright 2021 Red Hat, Inc.
*
*/
package gpu
import (
"fmt"
v1 "kubevirt.io/client-go/api/v1"
"kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/api"
"kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/device/hostdevice"
)
const (
AliasPrefix = "gpu-"
)
func CreateHostDevices(vmiGPUs []v1.GPU) ([]api.HostDevice, error) {
return CreateHostDevicesFromPools(vmiGPUs, NewPCIAddressPool(vmiGPUs), NewMDEVAddressPool(vmiGPUs))
}
func CreateHostDevicesFromPools(vmiGPUs []v1.GPU, pciAddressPool, mdevAddressPool hostdevice.AddressPooler) ([]api.HostDevice, error) {
pciPool := hostdevice.NewBestEffortAddressPool(pciAddressPool)
mdevPool := hostdevice.NewBestEffortAddressPool(mdevAddressPool)
hostDevicesMetaData := createHostDevicesMetadata(vmiGPUs)
pciHostDevices, err := hostdevice.CreatePCIHostDevices(hostDevicesMetaData, pciPool)
if err != nil {
return nil, fmt.Errorf("failed to creade GPU host-devices: %v", err)
}
mdevHostDevices, err := hostdevice.CreateMDEVHostDevices(hostDevicesMetaData, mdevPool)
if err != nil {
return nil, fmt.Errorf("failed to creade GPU host-devices: %v", err)
}
hostDevices := append(pciHostDevices, mdevHostDevices...)
if err := validateCreationOfAllDevices(vmiGPUs, hostDevices); err != nil {
return nil, fmt.Errorf("failed to creade GPU host-devices: %v", err)
}
return hostDevices, nil
}
func createHostDevicesMetadata(vmiGPUs []v1.GPU) []hostdevice.HostDeviceMetaData {
var hostDevicesMetaData []hostdevice.HostDeviceMetaData
for _, dev := range vmiGPUs {
hostDevicesMetaData = append(hostDevicesMetaData, hostdevice.HostDeviceMetaData{
AliasPrefix: AliasPrefix,
Name: dev.Name,
ResourceName: dev.DeviceName,
})
}
return hostDevicesMetaData
}
// validateCreationOfAllDevices validates that all specified GPU/s have a matching host-device.
// On validation failure, an error is returned.
// The validation assumes that the assignment of a device to a specified GPU is correct,
// therefore a simple quantity check is sufficient.
func validateCreationOfAllDevices(gpus []v1.GPU, hostDevices []api.HostDevice) error {
if len(gpus) != len(hostDevices) {
return fmt.Errorf(
"the number of GPU/s do not match the number of devices:\nGPU: %v\nDevice: %v", gpus, hostDevices,
)
}
return nil
}