-
Notifications
You must be signed in to change notification settings - Fork 145
/
config.go
96 lines (77 loc) · 2.51 KB
/
config.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
package config
import (
"encoding/json"
"fmt"
"path/filepath"
"strings"
"github.com/containernetworking/cni/pkg/skel"
sriovtypes "github.com/intel/sriov-cni/pkg/types"
"github.com/intel/sriov-cni/pkg/utils"
)
var (
// DefaultCNIDir used for caching NetConf
DefaultCNIDir = "/var/lib/cni/sriov"
)
// LoadConf parses and validates stdin netconf and returns NetConf object
func LoadConf(bytes []byte) (*sriovtypes.NetConf, error) {
n := &sriovtypes.NetConf{}
if err := json.Unmarshal(bytes, n); err != nil {
return nil, fmt.Errorf("LoadConf(): failed to load netconf: %v", err)
}
// DeviceID takes precedence; if we are given a VF pciaddr then work from there
if n.DeviceID != "" {
// Get rest of the VF information
pfName, vfID, err := getVfInfo(n.DeviceID)
if err != nil {
return nil, fmt.Errorf("LoadConf(): failed to get VF information: %q", err)
}
n.VFID = vfID
n.Master = pfName
} else {
return nil, fmt.Errorf("LoadConf(): VF pci addr is required")
}
// Assuming VF is netdev interface; Get interface name(s)
hostIFNames, err := utils.GetVFLinkNames(n.DeviceID)
if err != nil || hostIFNames == "" {
// VF interface not found; check if VF has dpdk driver
hasDpdkDriver, err := utils.HasDpdkDriver(n.DeviceID)
if err != nil {
return nil, fmt.Errorf("LoadConf(): failed to detect if VF %s has dpdk driver %q", n.DeviceID, err)
}
n.DPDKMode = hasDpdkDriver
}
if hostIFNames != "" {
n.HostIFNames = hostIFNames
}
if hostIFNames == "" && !n.DPDKMode {
return nil, fmt.Errorf("LoadConf(): the VF %s does not have a interface name or a dpdk driver", n.DeviceID)
}
return n, nil
}
func getVfInfo(vfPci string) (string, int, error) {
var vfID int
pf, err := utils.GetPfName(vfPci)
if err != nil {
return "", vfID, err
}
vfID, err = utils.GetVfid(vfPci, pf)
if err != nil {
return "", vfID, err
}
return pf, vfID, nil
}
// LoadConfFromCache retrieves cached NetConf returns it along with a handle for removal
func LoadConfFromCache(args *skel.CmdArgs) (*sriovtypes.NetConf, string, error) {
netConf := &sriovtypes.NetConf{}
s := []string{args.ContainerID, args.IfName}
cRef := strings.Join(s, "-")
cRefPath := filepath.Join(DefaultCNIDir, cRef)
netConfBytes, err := utils.ReadScratchNetConf(cRefPath)
if err != nil {
return nil, "", fmt.Errorf("error reading cached NetConf in %s with name %s", DefaultCNIDir, cRef)
}
if err = json.Unmarshal(netConfBytes, netConf); err != nil {
return nil, "", fmt.Errorf("failed to parse NetConf: %q", err)
}
return netConf, cRefPath, nil
}