• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

k8snetworkplumbingwg / sriov-network-operator / 10058808149

23 Jul 2024 12:22PM UTC coverage: 43.964% (+0.6%) from 43.351%
10058808149

Pull #659

github

web-flow
Merge f199eb95f into 588abb449
Pull Request #659: Configure IB VFs' GUIDs using a statically provided GUID pool

212 of 280 new or added lines in 13 files covered. (75.71%)

2 existing lines in 1 file now uncovered.

6526 of 14844 relevant lines covered (43.96%)

0.48 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.0
/pkg/host/internal/infiniband/ib_guid_config.go
1
package infiniband
2

3
import (
4
        "encoding/json"
5
        "fmt"
6
        "os"
7

8
        "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/internal/lib/netlink"
9
        "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/host/types"
10
        "github.com/k8snetworkplumbingwg/sriov-network-operator/pkg/utils"
11
)
12

13
type ibPfGUIDJSONConfig struct {
14
        PciAddress string         `json:"pciAddress,omitempty"`
15
        PfGUID     string         `json:"pfGuid,omitempty"`
16
        GUIDs      []string       `json:"guids,omitempty"`
17
        GUIDsRange *GUIDRangeJSON `json:"guidsRange,omitempty"`
18
}
19

20
type GUIDRangeJSON struct {
21
        Start string `json:"start,omitempty"`
22
        End   string `json:"end,omitempty"`
23
}
24

25
type ibPfGUIDConfig struct {
26
        GUIDs     []GUID
27
        GUIDRange *GUIDRange
28
}
29

30
type GUIDRange struct {
31
        Start GUID
32
        End   GUID
33
}
34

35
func getIbGUIDConfig(configPath string, netlinkLib netlink.NetlinkLib, networkHelper types.NetworkInterface) (map[string]ibPfGUIDConfig, error) {
1✔
36
        links, err := netlinkLib.LinkList()
1✔
37
        if err != nil {
1✔
NEW
38
                return nil, err
×
NEW
39
        }
×
40
        rawConfigs, err := readJSONConfig(configPath)
1✔
41
        if err != nil {
2✔
42
                return nil, err
1✔
43
        }
1✔
44
        resultConfigs := map[string]ibPfGUIDConfig{}
1✔
45
        // Parse JSON config into an internal struct
1✔
46
        for _, rawConfig := range rawConfigs {
2✔
47
                pciAddress, err := getPfPciAddressFromRawConfig(rawConfig, links, networkHelper)
1✔
48
                if err != nil {
2✔
49
                        return nil, fmt.Errorf("failed to extract pci address from ib guid config: %w", err)
1✔
50
                }
1✔
51
                if len(rawConfig.GUIDs) == 0 && (rawConfig.GUIDsRange == nil || (rawConfig.GUIDsRange.Start == "" || rawConfig.GUIDsRange.End == "")) {
2✔
52
                        return nil, fmt.Errorf("either guid list or guid range should be provided, got none")
1✔
53
                }
1✔
54
                if len(rawConfig.GUIDs) != 0 && rawConfig.GUIDsRange != nil {
2✔
55
                        return nil, fmt.Errorf("either guid list or guid range should be provided, got both")
1✔
56
                }
1✔
57
                if rawConfig.GUIDsRange != nil && ((rawConfig.GUIDsRange.Start != "" && rawConfig.GUIDsRange.End == "") || (rawConfig.GUIDsRange.Start == "" && rawConfig.GUIDsRange.End != "")) {
1✔
NEW
58
                        return nil, fmt.Errorf("both guid rangeStart and rangeEnd should be provided, got one")
×
NEW
59
                }
×
60
                if len(rawConfig.GUIDs) != 0 {
2✔
61
                        var guids []GUID
1✔
62
                        for _, guidStr := range rawConfig.GUIDs {
2✔
63
                                guid, err := ParseGUID(guidStr)
1✔
64
                                if err != nil {
2✔
65
                                        return nil, fmt.Errorf("failed to parse ib guid %s: %w", guidStr, err)
1✔
66
                                }
1✔
67
                                guids = append(guids, guid)
1✔
68
                        }
69
                        resultConfigs[pciAddress] = ibPfGUIDConfig{
1✔
70
                                GUIDs: guids,
1✔
71
                        }
1✔
72
                        continue
1✔
73
                }
74

75
                rangeStart, err := ParseGUID(rawConfig.GUIDsRange.Start)
1✔
76
                if err != nil {
2✔
77
                        return nil, fmt.Errorf("failed to parse ib guid range start: %w", err)
1✔
78
                }
1✔
79
                rangeEnd, err := ParseGUID(rawConfig.GUIDsRange.End)
1✔
80
                if err != nil {
2✔
81
                        return nil, fmt.Errorf("failed to parse ib guid range end: %w", err)
1✔
82
                }
1✔
83
                if rangeEnd < rangeStart {
2✔
84
                        return nil, fmt.Errorf("range end cannot be less then range start")
1✔
85
                }
1✔
86
                resultConfigs[pciAddress] = ibPfGUIDConfig{
1✔
87
                        GUIDRange: &GUIDRange{
1✔
88
                                Start: rangeStart,
1✔
89
                                End:   rangeEnd,
1✔
90
                        },
1✔
91
                }
1✔
92
        }
93
        return resultConfigs, nil
1✔
94
}
95

96
// readJSONConfig reads the file at the given path and unmarshals the contents into an array of ibPfGUIDJSONConfig structs
97
func readJSONConfig(configPath string) ([]ibPfGUIDJSONConfig, error) {
1✔
98
        data, err := os.ReadFile(utils.GetHostExtensionPath(configPath))
1✔
99
        if err != nil {
2✔
100
                return nil, fmt.Errorf("failed to read ib guid config: %w", err)
1✔
101
        }
1✔
102
        var configs []ibPfGUIDJSONConfig
1✔
103
        if err := json.Unmarshal(data, &configs); err != nil {
2✔
104
                return nil, fmt.Errorf("failed to unmarshal content of ib guid config: %w", err)
1✔
105
        }
1✔
106
        return configs, nil
1✔
107
}
108

109
func getPfPciAddressFromRawConfig(pfRawConfig ibPfGUIDJSONConfig, links []netlink.Link, networkHelper types.NetworkInterface) (string, error) {
1✔
110
        if pfRawConfig.PciAddress != "" && pfRawConfig.PfGUID != "" {
2✔
111
                return "", fmt.Errorf("either PCI address or PF GUID required to describe an interface, both provided")
1✔
112
        }
1✔
113
        if pfRawConfig.PciAddress == "" && pfRawConfig.PfGUID == "" {
2✔
114
                return "", fmt.Errorf("either PCI address or PF GUID required to describe an interface, none provided")
1✔
115
        }
1✔
116
        if pfRawConfig.PciAddress != "" {
2✔
117
                return pfRawConfig.PciAddress, nil
1✔
118
        }
1✔
119
        // PfGUID is provided, need to resolve the pci address
120
        for _, link := range links {
2✔
121
                if link.Attrs().HardwareAddr.String() == pfRawConfig.PfGUID {
2✔
122
                        return networkHelper.GetPciAddressFromInterfaceName(link.Attrs().Name)
1✔
123
                }
1✔
124
        }
125
        return "", fmt.Errorf("no matching link found for pf guid: %s", pfRawConfig.PfGUID)
1✔
126
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc