/
main.go
181 lines (152 loc) · 4.56 KB
/
main.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
package main
import (
"errors"
"fmt"
"runtime"
"github.com/containernetworking/cni/pkg/skel"
"github.com/containernetworking/cni/pkg/types"
"github.com/containernetworking/cni/pkg/types/current"
"github.com/containernetworking/cni/pkg/version"
"github.com/containernetworking/plugins/pkg/ipam"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/intel/sriov-cni/pkg/config"
"github.com/intel/sriov-cni/pkg/sriov"
"github.com/intel/sriov-cni/pkg/utils"
"github.com/vishvananda/netlink"
)
func init() {
// this ensures that main runs only on main thread (thread group leader).
// since namespace ops (unshare, setns) are done for a single thread, we
// must ensure that the goroutine does not jump from OS thread to thread
runtime.LockOSThread()
}
func cmdAdd(args *skel.CmdArgs) error {
var macAddr string
netConf, err := config.LoadConf(args.StdinData)
if err != nil {
return fmt.Errorf("SRIOV-CNI failed to load netconf: %v", err)
}
netns, err := ns.GetNS(args.Netns)
if err != nil {
return fmt.Errorf("failed to open netns %q: %v", netns, err)
}
defer netns.Close()
sm := sriov.NewSriovManager()
if err := sm.ApplyVFConfig(netConf); err != nil {
return fmt.Errorf("SRIOV-CNI failed to configure VF %q", err)
}
result := ¤t.Result{}
result.Interfaces = []*current.Interface{{
Name: args.IfName,
Sandbox: netns.Path(),
}}
// skip the IPAM allocation for the DPDK
if netConf.DPDKMode {
// Cache NetConf for CmdDel
if err = utils.SaveNetConf(args.ContainerID, config.DefaultCNIDir, args.IfName, netConf); err != nil {
return fmt.Errorf("error saving NetConf %q", err)
}
return result.Print()
}
macAddr, err = sm.SetupVF(netConf, args.IfName, args.ContainerID, netns)
defer func() {
if err != nil {
err := netns.Do(func(_ ns.NetNS) error {
_, err := netlink.LinkByName(args.IfName)
return err
})
if err == nil {
sm.ReleaseVF(netConf, args.IfName, args.ContainerID, netns)
}
}
}()
if err != nil {
return fmt.Errorf("failed to set up pod interface %q from the device %q: %v", args.IfName, netConf.Master, err)
}
// run the IPAM plugin
if netConf.IPAM.Type != "" {
r, err := ipam.ExecAdd(netConf.IPAM.Type, args.StdinData)
if err != nil {
return fmt.Errorf("failed to set up IPAM plugin type %q from the device %q: %v", netConf.IPAM.Type, netConf.Master, err)
}
defer func() {
if err != nil {
ipam.ExecDel(netConf.IPAM.Type, args.StdinData)
}
}()
// Convert the IPAM result into the current Result type
newResult, err := current.NewResultFromResult(r)
if err != nil {
return err
}
if len(newResult.IPs) == 0 {
return errors.New("IPAM plugin returned missing IP config")
}
newResult.Interfaces = result.Interfaces
newResult.Interfaces[0].Mac = macAddr
for _, ipc := range newResult.IPs {
// All addresses apply to the container interface (move from host)
ipc.Interface = current.Int(0)
}
err = netns.Do(func(_ ns.NetNS) error {
return ipam.ConfigureIface(args.IfName, newResult)
})
if err != nil {
return err
}
result = newResult
}
// Cache NetConf for CmdDel
if err = utils.SaveNetConf(args.ContainerID, config.DefaultCNIDir, args.IfName, netConf); err != nil {
return fmt.Errorf("error saving NetConf %q", err)
}
return types.PrintResult(result, current.ImplementedSpecVersion)
}
func cmdDel(args *skel.CmdArgs) error {
// https://github.com/kubernetes/kubernetes/pull/35240
if args.Netns == "" {
return nil
}
netConf, cRefPath, err := config.LoadConfFromCache(args)
if err != nil {
return err
}
defer func() {
if err == nil && cRefPath != "" {
utils.CleanCachedNetConf(cRefPath)
}
}()
sm := sriov.NewSriovManager()
if !netConf.DPDKMode {
if netConf.IPAM.Type != "" {
err = ipam.ExecDel(netConf.IPAM.Type, args.StdinData)
if err != nil {
return err
}
}
netns, err := ns.GetNS(args.Netns)
if err != nil {
// according to:
// https://github.com/kubernetes/kubernetes/issues/43014#issuecomment-287164444
// if provided path does not exist (e.x. when node was restarted)
// plugin should silently return with success after releasing
// IPAM resources
_, ok := err.(ns.NSPathNotExistErr)
if ok {
return nil
}
return fmt.Errorf("failed to open netns %s: %q", netns, err)
}
defer netns.Close()
if err = sm.ReleaseVF(netConf, args.IfName, args.ContainerID, netns); err != nil {
return err
}
}
if err := sm.ResetVFConfig(netConf); err != nil {
return fmt.Errorf("cmdDel() error reseting VF: %q", err)
}
return nil
}
func main() {
skel.PluginMain(cmdAdd, cmdDel, version.All)
}