forked from k8snetworkplumbingwg/sriovnet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsriovnet_helper.go
129 lines (107 loc) · 3.05 KB
/
sriovnet_helper.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
package sriovnet
import (
"fmt"
"log"
"os"
"path/filepath"
)
const (
NetSysDir = "/sys/class/net"
PciSysDir = "/sys/bus/pci/devices"
pcidevPrefix = "device"
netdevDriverDir = "device/driver"
netdevUnbindFile = "unbind"
netdevBindFile = "bind"
netDevMaxVfCountFile = "sriov_totalvfs"
netDevCurrentVfCountFile = "sriov_numvfs"
netDevVfDevicePrefix = "virtfn"
)
type VfObject struct {
NetdevName string
PCIDevName string
}
func netDevDeviceDir(netDevName string) string {
devDirName := filepath.Join(NetSysDir, netDevName, pcidevPrefix)
return devDirName
}
func getMaxVfCount(pfNetdevName string) (int, error) {
devDirName := netDevDeviceDir(pfNetdevName)
maxDevFile := fileObject{
Path: filepath.Join(devDirName, netDevMaxVfCountFile),
}
maxVfs, err := maxDevFile.ReadInt()
if err != nil {
return 0, err
}
log.Println("max_vfs = ", maxVfs)
return maxVfs, nil
}
func setMaxVfCount(pfNetdevName string, maxVfs int) error {
devDirName := netDevDeviceDir(pfNetdevName)
maxDevFile := fileObject{
Path: filepath.Join(devDirName, netDevCurrentVfCountFile),
}
return maxDevFile.WriteInt(maxVfs)
}
func getCurrentVfCount(pfNetdevName string) (int, error) {
devDirName := netDevDeviceDir(pfNetdevName)
maxDevFile := fileObject{
Path: filepath.Join(devDirName, netDevCurrentVfCountFile),
}
curVfs, err := maxDevFile.ReadInt()
if err != nil {
return 0, err
}
log.Println("cur_vfs = ", curVfs)
return curVfs, nil
}
func vfNetdevNameFromParent(pfNetdevName string, vfIndex int) string {
devDirName := netDevDeviceDir(pfNetdevName)
vfNetdev, _ := lsFilesWithPrefix(fmt.Sprintf("%s/%s%v/net", devDirName,
netDevVfDevicePrefix, vfIndex), "", false)
if len(vfNetdev) == 0 {
return ""
}
return vfNetdev[0]
}
func readPCIsymbolicLink(symbolicLink string) (string, error) {
pciDevDir, err := os.Readlink(symbolicLink)
//nolint:gomnd
if len(pciDevDir) <= 3 {
return "", fmt.Errorf("could not find PCI Address")
}
return pciDevDir[3:], err
}
func vfPCIDevNameFromVfIndex(pfNetdevName string, vfIndex int) (string, error) {
symbolicLink := filepath.Join(NetSysDir, pfNetdevName, pcidevPrefix, fmt.Sprintf("%s%v",
netDevVfDevicePrefix, vfIndex))
pciAddress, err := readPCIsymbolicLink(symbolicLink)
if err != nil {
err = fmt.Errorf("%v for VF %s%v of PF %s", err,
netDevVfDevicePrefix, vfIndex, pfNetdevName)
}
return pciAddress, err
}
func getPCIFromDeviceName(netdevName string) (string, error) {
symbolicLink := filepath.Join(NetSysDir, netdevName, pcidevPrefix)
pciAddress, err := readPCIsymbolicLink(symbolicLink)
if err != nil {
err = fmt.Errorf("%v for netdevice %s", err, netdevName)
}
return pciAddress, err
}
func GetVfPciDevList(pfNetdevName string) ([]string, error) {
var i int
devDirName := netDevDeviceDir(pfNetdevName)
virtFnDirs, err := lsFilesWithPrefix(devDirName, netDevVfDevicePrefix, true)
if err != nil {
return nil, err
}
i = 0
vfDirList := make([]string, 0, len(virtFnDirs))
for _, vfDir := range virtFnDirs {
vfDirList = append(vfDirList, vfDir)
i++
}
return vfDirList, nil
}