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

k8snetworkplumbingwg / sriov-network-operator / 4253702965

pending completion
4253702965

Pull #377

github

GitHub
Merge f01bcd9a4 into 331d1ae13
Pull Request #377: Vdpa introduction

36 of 36 new or added lines in 5 files covered. (100.0%)

1949 of 7564 relevant lines covered (25.77%)

0.29 hits per line

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

23.29
/pkg/utils/switchdev.go
1
package utils
2

3
import (
4
        "bytes"
5
        "encoding/json"
6
        "io/ioutil"
7
        "os"
8

9
        "github.com/golang/glog"
10

11
        sriovnetworkv1 "github.com/k8snetworkplumbingwg/sriov-network-operator/api/v1"
12
)
13

14
const (
15
        switchDevConfPath = "/host/etc/sriov_config.json"
16
)
17

18
type config struct {
19
        Interfaces []sriovnetworkv1.Interface `json:"interfaces"`
20
}
21

22
func IsSwitchdevModeSpec(spec sriovnetworkv1.SriovNetworkNodeStateSpec) bool {
×
23
        for _, iface := range spec.Interfaces {
×
24
                if iface.EswitchMode == sriovnetworkv1.ESwithModeSwitchDev {
×
25
                        return true
×
26
                }
×
27
        }
28
        return false
×
29
}
30

31
func WriteSwitchdevConfFile(newState *sriovnetworkv1.SriovNetworkNodeState) (update bool, err error) {
1✔
32
        // Create a map with all the PFs we will need to SKIP for systemd configuration
1✔
33
        pfsToSkip, err := GetPfsToSkip(newState)
1✔
34
        if err != nil {
1✔
35
                return false, err
×
36
        }
×
37

38
        cfg := config{}
1✔
39
        for _, iface := range newState.Spec.Interfaces {
2✔
40
                for _, ifaceStatus := range newState.Status.Interfaces {
2✔
41
                        if iface.PciAddress != ifaceStatus.PciAddress {
1✔
42
                                continue
×
43
                        }
44

45
                        if skip := pfsToSkip[iface.PciAddress]; !skip {
2✔
46
                                continue
1✔
47
                        }
48

49
                        i := sriovnetworkv1.Interface{}
×
50
                        if iface.NumVfs > 0 {
×
51
                                i = sriovnetworkv1.Interface{
×
52
                                        // Not passing all the contents, since only NumVfs and EswitchMode can be configured by configure-switchdev.sh currently.
×
53
                                        Name:       iface.Name,
×
54
                                        PciAddress: iface.PciAddress,
×
55
                                        NumVfs:     iface.NumVfs,
×
56
                                        VfGroups:   iface.VfGroups,
×
57
                                }
×
58

×
59
                                if iface.EswitchMode == sriovnetworkv1.ESwithModeSwitchDev {
×
60
                                        i.EswitchMode = iface.EswitchMode
×
61
                                }
×
62
                                cfg.Interfaces = append(cfg.Interfaces, i)
×
63
                        }
64
                }
65
        }
66
        _, err = os.Stat(switchDevConfPath)
1✔
67
        if err != nil {
2✔
68
                if os.IsNotExist(err) {
2✔
69
                        if len(cfg.Interfaces) == 0 {
2✔
70
                                err = nil
1✔
71
                                return
1✔
72
                        }
1✔
73
                        glog.V(2).Infof("WriteSwitchdevConfFile(): file not existed, create it")
×
74
                        _, err = os.Create(switchDevConfPath)
×
75
                        if err != nil {
×
76
                                glog.Errorf("WriteSwitchdevConfFile(): fail to create file: %v", err)
×
77
                                return
×
78
                        }
×
79
                } else {
×
80
                        return
×
81
                }
×
82
        }
83
        oldContent, err := ioutil.ReadFile(switchDevConfPath)
×
84
        if err != nil {
×
85
                glog.Errorf("WriteSwitchdevConfFile(): fail to read file: %v", err)
×
86
                return
×
87
        }
×
88
        var newContent []byte
×
89
        if len(cfg.Interfaces) != 0 {
×
90
                newContent, err = json.Marshal(cfg)
×
91
                if err != nil {
×
92
                        glog.Errorf("WriteSwitchdevConfFile(): fail to marshal config: %v", err)
×
93
                        return
×
94
                }
×
95
        }
96

97
        if bytes.Equal(newContent, oldContent) {
×
98
                glog.V(2).Info("WriteSwitchdevConfFile(): no update")
×
99
                return
×
100
        }
×
101
        update = true
×
102
        glog.V(2).Infof("WriteSwitchdevConfFile(): write '%s' to switchdev.conf", newContent)
×
103
        err = ioutil.WriteFile(switchDevConfPath, newContent, 0644)
×
104
        if err != nil {
×
105
                glog.Errorf("WriteSwitchdevConfFile(): fail to write file: %v", err)
×
106
                return
×
107
        }
×
108
        return
×
109
}
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