-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
hostdev.go
147 lines (128 loc) · 5.31 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* 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 2020 Red Hat, Inc.
*
*/
package sriov
import (
"errors"
"fmt"
"os"
"path"
"time"
"k8s.io/apimachinery/pkg/util/wait"
v1 "kubevirt.io/api/core/v1"
"kubevirt.io/client-go/log"
"kubevirt.io/kubevirt/pkg/network/sriov"
"kubevirt.io/kubevirt/pkg/network/vmispec"
"kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/api"
"kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/device"
"kubevirt.io/kubevirt/pkg/virt-launcher/virtwrap/device/hostdevice"
)
func CreateHostDevices(vmi *v1.VirtualMachineInstance) ([]api.HostDevice, error) {
SRIOVInterfaces := vmispec.FilterInterfacesSpec(vmi.Spec.Domain.Devices.Interfaces, func(iface v1.Interface) bool {
if iface.SRIOV == nil {
return false
}
ifaceStatus := vmispec.LookupInterfaceStatusByName(vmi.Status.Interfaces, iface.Name)
return ifaceStatus != nil && vmispec.ContainsInfoSource(ifaceStatus.InfoSource, vmispec.InfoSourceMultusStatus)
})
if len(SRIOVInterfaces) == 0 {
return []api.HostDevice{}, nil
}
netStatusPath := path.Join(sriov.MountPath, sriov.VolumePath)
pciAddressPoolWithNetworkStatus, err := newPCIAddressPoolWithNetworkStatusFromFile(netStatusPath)
if err != nil {
return nil, err
}
if pciAddressPoolWithNetworkStatus.Len() == 0 {
log.Log.Object(vmi).Warningf("found no SR-IOV networks to PCI-Address mapping. fall back to resource address pool")
return CreateHostDevicesFromIfacesAndPool(SRIOVInterfaces, NewPCIAddressPool(SRIOVInterfaces))
}
return CreateHostDevicesFromIfacesAndPool(SRIOVInterfaces, pciAddressPoolWithNetworkStatus)
}
// newPCIAddressPoolWithNetworkStatusFromFile polls the given file path until populated, then uses it to create the
// PCI-Address Pool.
// possible return values are:
// - file populated - return PCI-Address Pool using the data in file.
// - file empty post-polling (timeout) - return err to fail SyncVMI.
// - other error reading file (i.e. file not exist) - return no error but PCIAddressWithNetworkStatusPool.Len() will return 0.
func newPCIAddressPoolWithNetworkStatusFromFile(path string) (*PCIAddressWithNetworkStatusPool, error) {
networkPCIMapBytes, err := readFileUntilNotEmpty(path)
if err != nil {
if isFileEmptyAfterTimeout(err, networkPCIMapBytes) {
return nil, err
}
return nil, nil
}
return NewPCIAddressPoolWithNetworkStatus(networkPCIMapBytes)
}
func readFileUntilNotEmpty(networkPCIMapPath string) ([]byte, error) {
var networkPCIMapBytes []byte
err := wait.PollImmediate(100*time.Millisecond, time.Second, func() (bool, error) {
var err error
networkPCIMapBytes, err = os.ReadFile(networkPCIMapPath)
return len(networkPCIMapBytes) > 0, err
})
return networkPCIMapBytes, err
}
func isFileEmptyAfterTimeout(err error, data []byte) bool {
return errors.Is(err, wait.ErrWaitTimeout) && len(data) == 0
}
func CreateHostDevicesFromIfacesAndPool(ifaces []v1.Interface, pool hostdevice.AddressPooler) ([]api.HostDevice, error) {
hostDevicesMetaData := createHostDevicesMetadata(ifaces)
return hostdevice.CreatePCIHostDevices(hostDevicesMetaData, pool)
}
func createHostDevicesMetadata(ifaces []v1.Interface) []hostdevice.HostDeviceMetaData {
var hostDevicesMetaData []hostdevice.HostDeviceMetaData
for _, iface := range ifaces {
hostDevicesMetaData = append(hostDevicesMetaData, hostdevice.HostDeviceMetaData{
AliasPrefix: sriov.AliasPrefix,
Name: iface.Name,
ResourceName: iface.Name,
DecorateHook: newDecorateHook(iface),
})
}
return hostDevicesMetaData
}
func newDecorateHook(iface v1.Interface) func(hostDevice *api.HostDevice) error {
return func(hostDevice *api.HostDevice) error {
if guestPCIAddress := iface.PciAddress; guestPCIAddress != "" {
addr, err := device.NewPciAddressField(guestPCIAddress)
if err != nil {
return fmt.Errorf("failed to interpret the guest PCI address: %v", err)
}
hostDevice.Address = addr
}
if iface.BootOrder != nil {
hostDevice.BootOrder = &api.BootOrder{Order: *iface.BootOrder}
}
return nil
}
}
func SafelyDetachHostDevices(domainSpec *api.DomainSpec, eventDetach hostdevice.EventRegistrar, dom hostdevice.DeviceDetacher, timeout time.Duration) error {
sriovDevices := hostdevice.FilterHostDevicesByAlias(domainSpec.Devices.HostDevices, sriov.AliasPrefix)
return hostdevice.SafelyDetachHostDevices(sriovDevices, eventDetach, dom, timeout)
}
func GetHostDevicesToAttach(vmi *v1.VirtualMachineInstance, domainSpec *api.DomainSpec) ([]api.HostDevice, error) {
sriovDevices, err := CreateHostDevices(vmi)
if err != nil {
return nil, err
}
currentAttachedSRIOVHostDevices := hostdevice.FilterHostDevicesByAlias(domainSpec.Devices.HostDevices, sriov.AliasPrefix)
sriovHostDevicesToAttach := hostdevice.DifferenceHostDevicesByAlias(sriovDevices, currentAttachedSRIOVHostDevices)
return sriovHostDevicesToAttach, nil
}