/
utils_virtual.go
487 lines (435 loc) · 15.1 KB
/
utils_virtual.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
package utils
import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"github.com/golang/glog"
"github.com/hashicorp/go-retryablehttp"
"github.com/jaypipes/ghw"
dputils "github.com/k8snetworkplumbingwg/sriov-network-device-plugin/pkg/utils"
sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
)
// PlatformType ...
type PlatformType int
const (
// Baremetal platform
Baremetal PlatformType = iota
// VirtualOpenStack ...
VirtualOpenStack
)
func (e PlatformType) String() string {
switch e {
case Baremetal:
return "Baremetal"
case VirtualOpenStack:
return "Virtual/Openstack"
default:
return fmt.Sprintf("%d", int(e))
}
}
var (
// PlatformMap contains supported platforms for virtual VF
PlatformMap = map[string]PlatformType{
"openstack": VirtualOpenStack,
}
)
const (
ospMetaDataDir = "/host/var/config/openstack/2018-08-27"
ospMetaDataBaseURL = "http://169.254.169.254/openstack/2018-08-27"
ospNetworkDataFile = ospMetaDataDir + "/network_data.json"
ospMetaDataFile = ospMetaDataDir + "/meta_data.json"
ospNetworkDataURL = ospMetaDataBaseURL + "/network_data.json"
ospMetaDataURL = ospMetaDataBaseURL + "/meta_data.json"
)
// OSPMetaDataDevice -- Device structure within meta_data.json
type OSPMetaDataDevice struct {
Vlan int `json:"vlan,omitempty"`
VfTrusted bool `json:"vf_trusted,omitempty"`
Type string `json:"type,omitempty"`
Mac string `json:"mac,omitempty"`
Bus string `json:"bus,omitempty"`
Address string `json:"address,omitempty"`
Tags []string `json:"tags,omitempty"`
}
// OSPMetaData -- Openstack meta_data.json format
type OSPMetaData struct {
UUID string `json:"uuid,omitempty"`
AdminPass string `json:"admin_pass,omitempty"`
Name string `json:"name,omitempty"`
LaunchIndex int `json:"launch_index,omitempty"`
AvailabilityZone string `json:"availability_zone,omitempty"`
ProjectID string `json:"project_id,omitempty"`
Devices []OSPMetaDataDevice `json:"devices,omitempty"`
}
// OSPNetworkLink OSP Link metadata
type OSPNetworkLink struct {
ID string `json:"id"`
VifID string `json:"vif_id,omitempty"`
Type string `json:"type"`
Mtu int `json:"mtu,omitempty"`
EthernetMac string `json:"ethernet_mac_address"`
}
// OSPNetwork OSP Network metadata
type OSPNetwork struct {
ID string `json:"id"`
Type string `json:"type"`
Link string `json:"link"`
NetworkID string `json:"network_id"`
}
// OSPNetworkData OSP Network metadata
type OSPNetworkData struct {
Links []OSPNetworkLink `json:"links,omitempty"`
Networks []OSPNetwork `json:"networks,omitempty"`
// Omit Services
}
type OSPDevicesInfo map[string]*OSPDeviceInfo
type OSPDeviceInfo struct {
MacAddress string
NetworkID string
}
// GetOpenstackData gets the metadata and network_data
func GetOpenstackData() (metaData *OSPMetaData, networkData *OSPNetworkData, err error) {
metaData, networkData, err = getOpenstackDataFromConfigDrive()
if err != nil {
metaData, networkData, err = getOpenstackDataFromMetadataService()
}
return metaData, networkData, err
}
// getOpenstackDataFromConfigDrive reads the meta_data and network_data files
func getOpenstackDataFromConfigDrive() (metaData *OSPMetaData, networkData *OSPNetworkData, err error) {
metaData = &OSPMetaData{}
networkData = &OSPNetworkData{}
glog.Infof("reading OpenStack meta_data from config-drive")
var metadataf *os.File
metadataf, err = os.Open(ospMetaDataFile)
if err != nil {
return metaData, networkData, fmt.Errorf("error opening file %s: %w", ospMetaDataFile, err)
}
defer func() {
if e := metadataf.Close(); err == nil && e != nil {
err = fmt.Errorf("error closing file %s: %w", ospMetaDataFile, e)
}
}()
if err = json.NewDecoder(metadataf).Decode(&metaData); err != nil {
return metaData, networkData, fmt.Errorf("error unmarshalling metadata from file %s: %w", ospMetaDataFile, err)
}
glog.Infof("reading OpenStack network_data from config-drive")
var networkDataf *os.File
networkDataf, err = os.Open(ospNetworkDataFile)
if err != nil {
return metaData, networkData, fmt.Errorf("error opening file %s: %w", ospNetworkDataFile, err)
}
defer func() {
if e := networkDataf.Close(); err == nil && e != nil {
err = fmt.Errorf("error closing file %s: %w", ospNetworkDataFile, e)
}
}()
if err = json.NewDecoder(networkDataf).Decode(&networkData); err != nil {
return metaData, networkData, fmt.Errorf("error unmarshalling metadata from file %s: %w", ospNetworkDataFile, err)
}
return metaData, networkData, err
}
func getBodyFromURL(url string) ([]byte, error) {
glog.V(2).Infof("Getting body from %s", url)
resp, err := retryablehttp.Get(url)
if err != nil {
return nil, err
}
rawBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
return rawBytes, nil
}
// getOpenstackDataFromMetadataService fetchs the metadata and network_data from the metadata service
func getOpenstackDataFromMetadataService() (metaData *OSPMetaData, networkData *OSPNetworkData, err error) {
metaData = &OSPMetaData{}
networkData = &OSPNetworkData{}
glog.Infof("getting OpenStack meta_data from metadata server")
metaDataRawBytes, err := getBodyFromURL(ospMetaDataURL)
if err != nil {
return metaData, networkData, fmt.Errorf("error getting OpenStack meta_data from %s: %v", ospMetaDataURL, err)
}
err = json.Unmarshal(metaDataRawBytes, metaData)
if err != nil {
return metaData, networkData, fmt.Errorf("error unmarshalling raw bytes %v from %s", err, ospMetaDataURL)
}
glog.Infof("getting OpenStack network_data from metadata server")
networkDataRawBytes, err := getBodyFromURL(ospNetworkDataURL)
if err != nil {
return metaData, networkData, fmt.Errorf("error getting OpenStack network_data from %s: %v", ospNetworkDataURL, err)
}
err = json.Unmarshal(networkDataRawBytes, networkData)
if err != nil {
return metaData, networkData, fmt.Errorf("error unmarshalling raw bytes %v from %s", err, ospNetworkDataURL)
}
return metaData, networkData, nil
}
// CreateOpenstackDevicesInfo create the openstack device info map
func CreateOpenstackDevicesInfo(metaData *OSPMetaData, networkData *OSPNetworkData) (OSPDevicesInfo, error) {
glog.Infof("CreateOpenstackDevicesInfo()")
devicesInfo := make(OSPDevicesInfo)
if metaData == nil || networkData == nil {
return nil, nil
}
// use this for hw pass throw interfaces
for _, device := range metaData.Devices {
for _, link := range networkData.Links {
if device.Mac == link.EthernetMac {
for _, network := range networkData.Networks {
if network.Link == link.ID {
networkID := sriovnetworkv1.OpenstackNetworkID.String() + ":" + network.NetworkID
devicesInfo[device.Address] = &OSPDeviceInfo{MacAddress: device.Mac, NetworkID: networkID}
}
}
}
}
}
// for vhostuser interface type we check the interfaces on the node
pci, err := ghw.PCI()
if err != nil {
return nil, fmt.Errorf("CreateOpenstackDevicesInfo(): error getting PCI info: %v", err)
}
devices := pci.ListDevices()
if len(devices) == 0 {
return nil, fmt.Errorf("CreateOpenstackDevicesInfo(): could not retrieve PCI devices")
}
for _, device := range devices {
if _, exist := devicesInfo[device.Address]; exist {
//we already discover the device via openstack metadata
continue
}
devClass, err := strconv.ParseInt(device.Class.ID, 16, 64)
if err != nil {
glog.Warningf("CreateOpenstackDevicesInfo(): unable to parse device class for device %+v %q", device, err)
continue
}
if devClass != netClass {
// Not network device
continue
}
macAddress := ""
if name := tryToGetVirtualInterfaceName(device.Address); name != "" {
if mac := getNetDevMac(name); mac != "" {
macAddress = mac
}
}
if macAddress == "" {
// we didn't manage to find a mac address for the nic skipping
continue
}
for _, link := range networkData.Links {
if macAddress == link.EthernetMac {
for _, network := range networkData.Networks {
if network.Link == link.ID {
networkID := sriovnetworkv1.OpenstackNetworkID.String() + ":" + network.NetworkID
devicesInfo[device.Address] = &OSPDeviceInfo{MacAddress: macAddress, NetworkID: networkID}
}
}
}
}
}
return devicesInfo, err
}
// DiscoverSriovDevicesVirtual discovers VFs on a virtual platform
func DiscoverSriovDevicesVirtual(devicesInfo OSPDevicesInfo) ([]sriovnetworkv1.InterfaceExt, error) {
glog.V(2).Info("DiscoverSriovDevicesVirtual()")
pfList := []sriovnetworkv1.InterfaceExt{}
pci, err := ghw.PCI()
if err != nil {
return nil, fmt.Errorf("DiscoverSriovDevicesVirtual(): error getting PCI info: %v", err)
}
devices := pci.ListDevices()
if len(devices) == 0 {
return nil, fmt.Errorf("DiscoverSriovDevicesVirtual(): could not retrieve PCI devices")
}
for _, device := range devices {
devClass, err := strconv.ParseInt(device.Class.ID, 16, 64)
if err != nil {
glog.Warningf("DiscoverSriovDevicesVirtual(): unable to parse device class for device %+v %q", device, err)
continue
}
if devClass != netClass {
// Not network device
continue
}
deviceInfo, exist := devicesInfo[device.Address]
if !exist {
glog.Warningf("DiscoverSriovDevicesVirtual(): unable to find device in devicesInfo list for pci %s", device.Address)
continue
}
netFilter := deviceInfo.NetworkID
metaMac := deviceInfo.MacAddress
driver, err := dputils.GetDriverName(device.Address)
if err != nil {
glog.Warningf("DiscoverSriovDevicesVirtual(): unable to parse device driver for device %+v %q", device, err)
continue
}
iface := sriovnetworkv1.InterfaceExt{
PciAddress: device.Address,
Driver: driver,
Vendor: device.Vendor.ID,
DeviceID: device.Product.ID,
NetFilter: netFilter,
}
if mtu := getNetdevMTU(device.Address); mtu > 0 {
iface.Mtu = mtu
}
if name := tryToGetVirtualInterfaceName(device.Address); name != "" {
iface.Name = name
if iface.Mac = getNetDevMac(name); iface.Mac == "" {
iface.Mac = metaMac
}
iface.LinkSpeed = getNetDevLinkSpeed(name)
}
iface.LinkType = getLinkType(iface)
iface.TotalVfs = 1
iface.NumVfs = 1
vf := sriovnetworkv1.VirtualFunction{
PciAddress: device.Address,
Driver: driver,
VfID: 0,
Vendor: iface.Vendor,
DeviceID: iface.DeviceID,
Mtu: iface.Mtu,
Mac: iface.Mac,
}
iface.VFs = append(iface.VFs, vf)
pfList = append(pfList, iface)
}
return pfList, nil
}
func CreateOpenstackDevicesInfoFromNodeStatus(networkState *sriovnetworkv1.SriovNetworkNodeState) OSPDevicesInfo {
devicesInfo := make(OSPDevicesInfo)
for _, iface := range networkState.Status.Interfaces {
devicesInfo[iface.PciAddress] = &OSPDeviceInfo{MacAddress: iface.Mac, NetworkID: iface.NetFilter}
}
return devicesInfo
}
// tryToGetVirtualInterfaceName get the interface name of a virtio interface
func tryToGetVirtualInterfaceName(pciAddr string) string {
glog.Infof("tryToGetVirtualInterfaceName() get interface name for device %s", pciAddr)
// To support different driver that is not virtio-pci like mlx
name := tryGetInterfaceName(pciAddr)
if name != "" {
return name
}
netDir, err := filepath.Glob(filepath.Join(sysBusPciDevices, pciAddr, "virtio*", "net"))
if err != nil || len(netDir) < 1 {
return ""
}
fInfos, err := ioutil.ReadDir(netDir[0])
if err != nil {
glog.Warningf("tryToGetVirtualInterfaceName(): failed to read net directory %s: %q", netDir, err)
return ""
}
names := make([]string, 0)
for _, f := range fInfos {
names = append(names, f.Name())
}
if len(names) < 1 {
return ""
}
return names[0]
}
// SyncNodeStateVirtual attempt to update the node state to match the desired state
// in virtual platforms
func SyncNodeStateVirtual(newState *sriovnetworkv1.SriovNetworkNodeState) error {
var err error
for _, ifaceStatus := range newState.Status.Interfaces {
for _, iface := range newState.Spec.Interfaces {
if iface.PciAddress == ifaceStatus.PciAddress {
if !needUpdateVirtual(&iface, &ifaceStatus) {
glog.V(2).Infof("SyncNodeStateVirtual(): no need update interface %s", iface.PciAddress)
break
}
if err = configSriovDeviceVirtual(&iface, &ifaceStatus); err != nil {
glog.Errorf("SyncNodeStateVirtual(): fail to config sriov interface %s: %v", iface.PciAddress, err)
return err
}
break
}
}
}
return nil
}
func needUpdateVirtual(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetworkv1.InterfaceExt) bool {
// The device MTU is set by the platorm
// The NumVfs is always 1
if iface.NumVfs > 0 {
for _, vf := range ifaceStatus.VFs {
ingroup := false
for _, group := range iface.VfGroups {
if sriovnetworkv1.IndexInRange(vf.VfID, group.VfRange) {
ingroup = true
if group.DeviceType != "netdevice" {
if group.DeviceType != vf.Driver {
glog.V(2).Infof("needUpdateVirtual(): Driver needs update, desired=%s, current=%s", group.DeviceType, vf.Driver)
return true
}
} else {
if sriovnetworkv1.StringInArray(vf.Driver, DpdkDrivers) {
glog.V(2).Infof("needUpdateVirtual(): Driver needs update, desired=%s, current=%s", group.DeviceType, vf.Driver)
return true
}
}
break
}
}
if !ingroup && sriovnetworkv1.StringInArray(vf.Driver, DpdkDrivers) {
// VF which has DPDK driver loaded but not in any group, needs to be reset to default driver.
return true
}
}
}
return false
}
func configSriovDeviceVirtual(iface *sriovnetworkv1.Interface, ifaceStatus *sriovnetworkv1.InterfaceExt) error {
glog.V(2).Infof("configSriovDeviceVirtual(): config interface %s with %v", iface.PciAddress, iface)
// Config VFs
if iface.NumVfs > 0 {
if iface.NumVfs > 1 {
glog.Warningf("configSriovDeviceVirtual(): in a virtual environment, only one VF per interface (NumVfs: %d)", iface.NumVfs)
return errors.New("NumVfs > 1")
}
if len(iface.VfGroups) != 1 {
glog.Warningf("configSriovDeviceVirtual(): missing VFGroup")
return errors.New("NumVfs != 1")
}
addr := iface.PciAddress
glog.V(2).Infof("configSriovDeviceVirtual(): addr %s", addr)
driver := ""
vfID := 0
for _, group := range iface.VfGroups {
glog.V(2).Infof("configSriovDeviceVirtual(): group %v", group)
if sriovnetworkv1.IndexInRange(vfID, group.VfRange) {
glog.V(2).Infof("configSriovDeviceVirtual(): indexInRange %d", vfID)
if sriovnetworkv1.StringInArray(group.DeviceType, DpdkDrivers) {
glog.V(2).Infof("configSriovDeviceVirtual(): driver %s", group.DeviceType)
driver = group.DeviceType
}
break
}
}
if driver == "" {
glog.V(2).Infof("configSriovDeviceVirtual(): bind default")
if err := BindDefaultDriver(addr); err != nil {
glog.Warningf("configSriovDeviceVirtual(): fail to bind default driver for device %s", addr)
return err
}
} else {
glog.V(2).Infof("configSriovDeviceVirtual(): bind driver %s", driver)
if err := BindDpdkDriver(addr, driver); err != nil {
glog.Warningf("configSriovDeviceVirtual(): fail to bind driver %s for device %s", driver, addr)
return err
}
}
}
return nil
}