-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathsriovnet.go
547 lines (474 loc) · 14.5 KB
/
sriovnet.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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
Copyright 2023 NVIDIA CORPORATION & AFFILIATES
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 sriovnet
import (
"fmt"
"log"
"net"
"os"
"path"
"path/filepath"
"regexp"
"strconv"
"strings"
"github.com/google/uuid"
"github.com/vishvananda/netlink"
utilfs "github.com/k8snetworkplumbingwg/sriovnet/pkg/utils/filesystem"
"github.com/k8snetworkplumbingwg/sriovnet/pkg/utils/netlinkops"
)
const (
// Used locally
etherEncapType = "ether"
ibEncapType = "infiniband"
)
var (
virtFnRe = regexp.MustCompile(`virtfn(\d+)`)
pciAddressRe = regexp.MustCompile(`^[0-9a-f]{4}:[0-9a-f]{2}:[01][0-9a-f].[0-7]$`)
auxiliaryDeviceRe = regexp.MustCompile(`^(\S+\.){2}\d+$`)
)
type VfObj struct {
Index int
PciAddress string
Bound bool
Allocated bool
}
type PfNetdevHandle struct {
PfNetdevName string
pfLinkHandle netlink.Link
List []*VfObj
}
func SetPFLinkUp(pfNetdevName string) error {
handle, err := netlinkops.GetNetlinkOps().LinkByName(pfNetdevName)
if err != nil {
return err
}
return netlinkops.GetNetlinkOps().LinkSetUp(handle)
}
func IsVfPciVfioBound(pciAddr string) bool {
driverLink := filepath.Join(PciSysDir, pciAddr, "driver")
driverPath, err := utilfs.Fs.Readlink(driverLink)
if err != nil {
return false
}
driverName := filepath.Base(driverPath)
return driverName == "vfio-pci"
}
func IsSriovSupported(netdevName string) bool {
maxvfs, err := getMaxVfCount(netdevName)
if maxvfs == 0 || err != nil {
return false
}
return true
}
func IsSriovEnabled(netdevName string) bool {
curvfs, err := getCurrentVfCount(netdevName)
if curvfs == 0 || err != nil {
return false
}
return true
}
func EnableSriov(pfNetdevName string) error {
var maxVfCount int
var err error
devDirName := netDevDeviceDir(pfNetdevName)
devExist := dirExists(devDirName)
if !devExist {
return fmt.Errorf("device %s not found", pfNetdevName)
}
maxVfCount, err = getMaxVfCount(pfNetdevName)
if err != nil {
log.Println("Fail to read max vf count of PF", pfNetdevName)
return err
}
if maxVfCount == 0 {
return fmt.Errorf("sriov unsupported for device: %s", pfNetdevName)
}
curVfCount, err2 := getCurrentVfCount(pfNetdevName)
if err2 != nil {
log.Println("Fail to read current vf count of PF", pfNetdevName)
return err
}
if curVfCount == 0 {
return setMaxVfCount(pfNetdevName, maxVfCount)
}
return nil
}
func DisableSriov(pfNetdevName string) error {
devDirName := netDevDeviceDir(pfNetdevName)
devExist := dirExists(devDirName)
if !devExist {
return fmt.Errorf("device %s not found", pfNetdevName)
}
return setMaxVfCount(pfNetdevName, 0)
}
func GetPfNetdevHandle(pfNetdevName string) (*PfNetdevHandle, error) {
pfLinkHandle, err := netlinkops.GetNetlinkOps().LinkByName(pfNetdevName)
if err != nil {
return nil, err
}
handle := PfNetdevHandle{
PfNetdevName: pfNetdevName,
pfLinkHandle: pfLinkHandle,
}
list, err := GetVfPciDevList(pfNetdevName)
if err != nil {
return nil, err
}
for _, vfDir := range list {
vfIndexStr := strings.TrimPrefix(vfDir, netDevVfDevicePrefix)
vfIndex, _ := strconv.Atoi(vfIndexStr)
vfNetdevName := vfNetdevNameFromParent(pfNetdevName, vfIndex)
pciAddress, err := vfPCIDevNameFromVfIndex(pfNetdevName, vfIndex)
if err != nil {
log.Printf("Failed to read PCI Address for VF %v from PF %v: %v\n",
vfNetdevName, pfNetdevName, err)
continue
}
vfObj := VfObj{
Index: vfIndex,
PciAddress: pciAddress,
}
if vfNetdevName != "" {
vfObj.Bound = true
} else {
vfObj.Bound = false
}
vfObj.Allocated = false
handle.List = append(handle.List, &vfObj)
}
return &handle, nil
}
func UnbindVf(handle *PfNetdevHandle, vf *VfObj) error {
cmdFile := filepath.Join(NetSysDir, handle.PfNetdevName, netdevDriverDir, netdevUnbindFile)
cmdFileObj := fileObject{
Path: cmdFile,
}
err := cmdFileObj.Write(vf.PciAddress)
if err != nil {
vf.Bound = false
}
return err
}
func BindVf(handle *PfNetdevHandle, vf *VfObj) error {
cmdFile := filepath.Join(NetSysDir, handle.PfNetdevName, netdevDriverDir, netdevBindFile)
cmdFileObj := fileObject{
Path: cmdFile,
}
err := cmdFileObj.Write(vf.PciAddress)
if err != nil {
vf.Bound = true
}
return err
}
func GetVfDefaultMacAddr(vfNetdevName string) (string, error) {
ethHandle, err1 := netlinkops.GetNetlinkOps().LinkByName(vfNetdevName)
if err1 != nil {
return "", err1
}
ethAttr := ethHandle.Attrs()
return ethAttr.HardwareAddr.String(), nil
}
func SetVfDefaultMacAddress(handle *PfNetdevHandle, vf *VfObj) error {
netdevName := vfNetdevNameFromParent(handle.PfNetdevName, vf.Index)
ethHandle, err1 := netlinkops.GetNetlinkOps().LinkByName(netdevName)
if err1 != nil {
return err1
}
ethAttr := ethHandle.Attrs()
return netlinkops.GetNetlinkOps().LinkSetVfHardwareAddr(handle.pfLinkHandle, vf.Index, ethAttr.HardwareAddr)
}
func SetVfVlan(handle *PfNetdevHandle, vf *VfObj, vlan int) error {
return netlinkops.GetNetlinkOps().LinkSetVfVlan(handle.pfLinkHandle, vf.Index, vlan)
}
func setVfNodeGUID(handle *PfNetdevHandle, vf *VfObj, guid []byte) error {
var err error
nodeGUIDHwAddr := net.HardwareAddr(guid)
err = ibSetNodeGUID(handle.PfNetdevName, vf.Index, nodeGUIDHwAddr)
if err == nil {
return nil
}
err = netlinkops.GetNetlinkOps().LinkSetVfNodeGUID(handle.pfLinkHandle, vf.Index, guid)
return err
}
func setVfPortGUID(handle *PfNetdevHandle, vf *VfObj, guid []byte) error {
var err error
portGUIDHwAddr := net.HardwareAddr(guid)
err = ibSetPortGUID(handle.PfNetdevName, vf.Index, portGUIDHwAddr)
if err == nil {
return nil
}
err = netlinkops.GetNetlinkOps().LinkSetVfPortGUID(handle.pfLinkHandle, vf.Index, guid)
return err
}
func SetVfDefaultGUID(handle *PfNetdevHandle, vf *VfObj) error {
randUUID, err := uuid.NewRandom()
if err != nil {
return err
}
guid := randUUID[0:8]
guid[7] = byte(vf.Index)
err = setVfNodeGUID(handle, vf, guid)
if err != nil {
return err
}
err = setVfPortGUID(handle, vf, guid)
return err
}
func SetVfPrivileged(handle *PfNetdevHandle, vf *VfObj, privileged bool) error {
var spoofChk bool
var trusted bool
ethAttr := handle.pfLinkHandle.Attrs()
if ethAttr.EncapType != etherEncapType {
return nil
}
// Only ether type is supported
if privileged {
spoofChk = false
trusted = true
} else {
spoofChk = true
trusted = false
}
/* do not check for error status as older kernels doesn't
* have support for it.
* golangci-lint complains on missing error check. ignore it
* with nolint comment until we update the code to ignore ENOTSUP error
*/
netlinkops.GetNetlinkOps().LinkSetVfTrust(handle.pfLinkHandle, vf.Index, trusted) //nolint
netlinkops.GetNetlinkOps().LinkSetVfSpoofchk(handle.pfLinkHandle, vf.Index, spoofChk) //nolint
return nil
}
func setDefaultHwAddr(handle *PfNetdevHandle, vf *VfObj) error {
var err error
ethAttr := handle.pfLinkHandle.Attrs()
if ethAttr.EncapType == etherEncapType {
err = SetVfDefaultMacAddress(handle, vf)
} else if ethAttr.EncapType == ibEncapType {
err = SetVfDefaultGUID(handle, vf)
}
return err
}
func setPortAdminState(handle *PfNetdevHandle, vf *VfObj) error {
ethAttr := handle.pfLinkHandle.Attrs()
if ethAttr.EncapType == ibEncapType {
state, err2 := ibGetPortAdminState(handle.PfNetdevName, vf.Index)
// Ignore the error where this file is not available
if err2 != nil {
return nil
}
log.Printf("Admin state = %v", state)
err2 = ibSetPortAdminState(handle.PfNetdevName, vf.Index, ibSriovPortAdminStateFollow)
if err2 != nil {
// If file exist, we must be able to write
log.Printf("Admin state setting error = %v", err2)
return err2
}
}
return nil
}
func ConfigVfs(handle *PfNetdevHandle, privileged bool) error {
var err error
for _, vf := range handle.List {
log.Printf("vf = %v\n", vf)
err = setPortAdminState(handle, vf)
if err != nil {
break
}
// skip VFs in another namespace
netdevName := vfNetdevNameFromParent(handle.PfNetdevName, vf.Index)
if _, err = netlinkops.GetNetlinkOps().LinkByName(netdevName); err != nil {
continue
}
err = setDefaultHwAddr(handle, vf)
if err != nil {
break
}
_ = SetVfPrivileged(handle, vf, privileged)
}
if err != nil {
return err
}
for _, vf := range handle.List {
if !vf.Bound {
continue
}
err = UnbindVf(handle, vf)
if err != nil {
log.Printf("Fail to unbind err=%v\n", err)
break
}
err = BindVf(handle, vf)
if err != nil {
log.Printf("Fail to bind err=%v\n", err)
break
}
log.Printf("vf = %v unbind/bind completed", vf)
}
return nil
}
func AllocateVf(handle *PfNetdevHandle) (*VfObj, error) {
for _, vf := range handle.List {
if vf.Allocated {
continue
}
vf.Allocated = true
log.Printf("Allocated vf = %v\n", *vf)
return vf, nil
}
return nil, fmt.Errorf("all Vfs for %v are allocated", handle.PfNetdevName)
}
func AllocateVfByMacAddress(handle *PfNetdevHandle, vfMacAddress string) (*VfObj, error) {
for _, vf := range handle.List {
if vf.Allocated {
continue
}
netdevName := vfNetdevNameFromParent(handle.PfNetdevName, vf.Index)
macAddr, _ := GetVfDefaultMacAddr(netdevName)
if macAddr != vfMacAddress {
continue
}
vf.Allocated = true
log.Printf("Allocated vf by mac = %v\n", *vf)
return vf, nil
}
return nil, fmt.Errorf("all Vfs for %v are allocated for mac address %v",
handle.PfNetdevName, vfMacAddress)
}
func FreeVf(_ *PfNetdevHandle, vf *VfObj) {
vf.Allocated = false
log.Printf("Free vf = %v\n", *vf)
}
func FreeVfByNetdevName(handle *PfNetdevHandle, vfIndex int) error {
vfNetdevName := fmt.Sprintf("%s%v", netDevVfDevicePrefix, vfIndex)
for _, vf := range handle.List {
netdevName := vfNetdevNameFromParent(handle.PfNetdevName, vf.Index)
if vf.Allocated && netdevName == vfNetdevName {
vf.Allocated = true
return nil
}
}
return fmt.Errorf("vf netdev %v not found", vfNetdevName)
}
func GetVfNetdevName(handle *PfNetdevHandle, vf *VfObj) string {
return vfNetdevNameFromParent(handle.PfNetdevName, vf.Index)
}
// GetVfIndexByPciAddress gets a VF PCI address (e.g '0000:03:00.4') and
// returns the correlate VF index.
func GetVfIndexByPciAddress(vfPciAddress string) (int, error) {
vfPath := filepath.Join(PciSysDir, vfPciAddress, "physfn", "virtfn*")
matches, err := filepath.Glob(vfPath)
if err != nil {
return -1, err
}
for _, match := range matches {
tmp, err := os.Readlink(match)
if err != nil {
continue
}
if strings.Contains(tmp, vfPciAddress) {
result := virtFnRe.FindStringSubmatch(match)
vfIndex, err := strconv.Atoi(result[1])
if err != nil {
continue
}
return vfIndex, nil
}
}
return -1, fmt.Errorf("vf index for %s not found", vfPciAddress)
}
// gets the PF index that's associated with a VF PCI address (e.g '0000:03:00.4')
func GetPfIndexByVfPciAddress(vfPciAddress string) (int, error) {
const pciParts = 4
pfPciAddress, err := GetPfPciFromVfPci(vfPciAddress)
if err != nil {
return -1, err
}
var domain, bus, dev, fn int
parsed, err := fmt.Sscanf(pfPciAddress, "%04x:%02x:%02x.%d", &domain, &bus, &dev, &fn)
if err != nil {
return -1, fmt.Errorf("error trying to parse PF PCI address %s: %v", pfPciAddress, err)
}
if parsed != pciParts {
return -1, fmt.Errorf("failed to parse PF PCI address %s. Unexpected format", pfPciAddress)
}
return fn, err
}
// GetPfPciFromVfPci retrieves the parent PF PCI address of the provided VF PCI address in D:B:D.f format
func GetPfPciFromVfPci(vfPciAddress string) (string, error) {
pfPath := filepath.Join(PciSysDir, vfPciAddress, "physfn")
pciDevDir, err := utilfs.Fs.Readlink(pfPath)
if err != nil {
return "", fmt.Errorf("failed to read physfn link, provided address may not be a VF. %v", err)
}
pf := path.Base(pciDevDir)
if pf == "" {
return pf, fmt.Errorf("could not find PF PCI Address")
}
return pf, err
}
// GetNetDevicesFromPci gets a PCI address (e.g '0000:03:00.1') and
// returns the correlate list of netdevices
func GetNetDevicesFromPci(pciAddress string) ([]string, error) {
pciDir := filepath.Join(PciSysDir, pciAddress, "net")
return getFileNamesFromPath(pciDir)
}
// GetPciFromNetDevice returns the PCI address associated with a network device name
func GetPciFromNetDevice(name string) (string, error) {
devPath := filepath.Join(NetSysDir, name)
realPath, err := utilfs.Fs.Readlink(devPath)
if err != nil {
return "", fmt.Errorf("device %s not found: %s", name, err)
}
parent := filepath.Dir(realPath)
base := filepath.Base(parent)
// Devices can have their PCI device sysfs entry at different levels:
// PF, VF, SF representor:
// /sys/devices/pci0000:00/.../0000:03:00.0/net/p0
// /sys/devices/pci0000:00/.../0000:03:00.0/net/pf0hpf
// /sys/devices/pci0000:00/.../0000:03:00.0/net/pf0vf0
// /sys/devices/pci0000:00/.../0000:03:00.0/net/pf0sf0
// SF port:
// /sys/devices/pci0000:00/.../0000:03:00.0/mlx5_core.sf.3/net/enp3s0f0s1
// This loop allows detecting any of them.
for parent != "/" && !pciAddressRe.MatchString(base) {
parent = filepath.Dir(parent)
base = filepath.Base(parent)
}
// If we stopped on '/' and the base was never a proper PCI address,
// then 'netdev' is not a PCI device.
if !pciAddressRe.MatchString(base) {
return "", fmt.Errorf("device %s is not a PCI device: %s", name, realPath)
}
return base, nil
}
// GetPKeyByIndexFromPci returns the PKey stored under given index for the IB PCI device
func GetPKeyByIndexFromPci(pciAddress string, index int) (string, error) {
pciDir := filepath.Join(PciSysDir, pciAddress, "infiniband")
dirEntries, err := utilfs.Fs.ReadDir(pciDir)
if err != nil {
return "", fmt.Errorf("failed to read infiniband directory: %v", err)
}
if len(dirEntries) == 0 {
return "", fmt.Errorf("infiniband directory is empty for device: %s", pciAddress)
}
indexFilePath := filepath.Join(pciDir, dirEntries[0].Name(), "ports", "1", "pkeys", strconv.Itoa(index))
pKeyBytes, err := utilfs.Fs.ReadFile(indexFilePath)
if err != nil {
return "", fmt.Errorf("failed to read PKey file: %v", err)
}
return strings.TrimSpace(string(pKeyBytes)), nil
}
// GetDefaultPKeyFromPci returns the index0 PKey for the IB PCI device
func GetDefaultPKeyFromPci(pciAddress string) (string, error) {
return GetPKeyByIndexFromPci(pciAddress, 0)
}