-
Notifications
You must be signed in to change notification settings - Fork 148
/
sriov.go
414 lines (356 loc) · 13.3 KB
/
sriov.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package sriov
import (
"fmt"
"github.com/containernetworking/plugins/pkg/ns"
"github.com/k8snetworkplumbingwg/sriov-cni/pkg/logging"
sriovtypes "github.com/k8snetworkplumbingwg/sriov-cni/pkg/types"
"github.com/k8snetworkplumbingwg/sriov-cni/pkg/utils"
"github.com/vishvananda/netlink"
)
type pciUtils interface {
GetSriovNumVfs(ifName string) (int, error)
GetVFLinkNamesFromVFID(pfName string, vfID int) ([]string, error)
GetPciAddress(ifName string, vf int) (string, error)
EnableArpAndNdiscNotify(ifName string) error
EnableOptimisticDad(ifName string) error
}
type pciUtilsImpl struct{}
func (p *pciUtilsImpl) GetSriovNumVfs(ifName string) (int, error) {
return utils.GetSriovNumVfs(ifName)
}
func (p *pciUtilsImpl) GetVFLinkNamesFromVFID(pfName string, vfID int) ([]string, error) {
return utils.GetVFLinkNamesFromVFID(pfName, vfID)
}
func (p *pciUtilsImpl) GetPciAddress(ifName string, vf int) (string, error) {
return utils.GetPciAddress(ifName, vf)
}
func (p *pciUtilsImpl) EnableArpAndNdiscNotify(ifName string) error {
return utils.EnableArpAndNdiscNotify(ifName)
}
func (p *pciUtilsImpl) EnableOptimisticDad(ifName string) error {
return utils.EnableOptimisticDad(ifName)
}
// Manager provides interface invoke sriov nic related operations
type Manager interface {
SetupVF(conf *sriovtypes.NetConf, podifName string, netns ns.NetNS) error
ReleaseVF(conf *sriovtypes.NetConf, podifName string, netns ns.NetNS) error
ResetVFConfig(conf *sriovtypes.NetConf) error
ApplyVFConfig(conf *sriovtypes.NetConf) error
FillOriginalVfInfo(conf *sriovtypes.NetConf) error
}
type sriovManager struct {
nLink utils.NetlinkManager
utils pciUtils
}
// NewSriovManager returns an instance of SriovManager
func NewSriovManager() Manager {
return &sriovManager{
nLink: &utils.MyNetlink{},
utils: &pciUtilsImpl{},
}
}
// SetupVF sets up a VF in Pod netns
func (s *sriovManager) SetupVF(conf *sriovtypes.NetConf, podifName string, netns ns.NetNS) error {
linkName := conf.OrigVfState.HostIFName
linkObj, err := s.nLink.LinkByName(linkName)
if err != nil {
return fmt.Errorf("error getting VF netdevice with name %s", linkName)
}
// Save the original effective MAC address before overriding it
conf.OrigVfState.EffectiveMAC = linkObj.Attrs().HardwareAddr.String()
// tempName used as intermediary name to avoid name conflicts
tempName := fmt.Sprintf("%s%d", "temp_", linkObj.Attrs().Index)
// 1. Set link down
logging.Debug("1. Set link down",
"func", "SetupVF",
"linkObj", linkObj)
if err := s.nLink.LinkSetDown(linkObj); err != nil {
return fmt.Errorf("failed to down vf device %q: %v", linkName, err)
}
// 2. Set temp name
logging.Debug("2. Set temp name",
"func", "SetupVF",
"linkObj", linkObj,
"tempName", tempName)
if err := s.nLink.LinkSetName(linkObj, tempName); err != nil {
return fmt.Errorf("error setting temp IF name %s for %s", tempName, linkName)
}
// 3. Remove alt name from the nic
logging.Debug("3. Remove interface original name from alt names",
"func", "SetupVF",
"linkObj", linkObj,
"OriginalLinkName", linkName,
"tempName", tempName)
linkObj, err = s.nLink.LinkByName(tempName)
if err != nil {
return fmt.Errorf("error getting VF netdevice with name %s: %v", tempName, err)
}
for _, altName := range linkObj.Attrs().AltNames {
if altName == linkName {
if err := s.nLink.LinkDelAltName(linkObj, linkName); err != nil {
return fmt.Errorf("error removing VF altname %s: %v", linkName, err)
}
}
}
// 4. Change netns
logging.Debug("4. Change netns",
"func", "SetupVF",
"linkObj", linkObj,
"netns.Fd()", int(netns.Fd()))
if err := s.nLink.LinkSetNsFd(linkObj, int(netns.Fd())); err != nil {
return fmt.Errorf("failed to move IF %s to netns: %q", tempName, err)
}
if err := netns.Do(func(_ ns.NetNS) error {
// 5. Set Pod IF name
logging.Debug("5. Set Pod IF name",
"func", "SetupVF",
"linkObj", linkObj,
"podifName", podifName)
if err := s.nLink.LinkSetName(linkObj, podifName); err != nil {
return fmt.Errorf("error setting container interface name %s for %s", linkName, tempName)
}
// 6. Enable IPv4 ARP notify and IPv6 Network Discovery notify
// Error is ignored here because enabling this feature is only a performance enhancement.
logging.Debug("6. Enable IPv4 ARP notify and IPv6 Network Discovery notify",
"func", "SetupVF",
"podifName", podifName)
_ = s.utils.EnableArpAndNdiscNotify(podifName)
// 7. Set MAC address
if conf.MAC != "" {
logging.Debug("7. Set MAC address",
"func", "SetupVF",
"s.nLink", s.nLink,
"podifName", podifName,
"conf.MAC", conf.MAC)
err = utils.SetVFEffectiveMAC(s.nLink, podifName, conf.MAC)
if err != nil {
return fmt.Errorf("failed to set netlink MAC address to %s: %v", conf.MAC, err)
}
}
logging.Debug("8. Enable Optimistic DAD for IPv6 addresses", "func", "SetupVF",
"linkObj", linkObj)
_ = s.utils.EnableOptimisticDad(podifName)
// 9. Bring IF up in Pod netns
logging.Debug("9. Bring IF up in Pod netns",
"func", "SetupVF",
"linkObj", linkObj)
if err := s.nLink.LinkSetUp(linkObj); err != nil {
return fmt.Errorf("error bringing interface up in container ns: %q", err)
}
return nil
}); err != nil {
return fmt.Errorf("error setting up interface in container namespace: %q", err)
}
// Copy the MTU value to a new variable
// and use it as a pointer
vfMTU := linkObj.Attrs().MTU
conf.MTU = &vfMTU
return nil
}
// ReleaseVF reset a VF from Pod netns and return it to init netns
func (s *sriovManager) ReleaseVF(conf *sriovtypes.NetConf, podifName string, netns ns.NetNS) error {
initns, err := ns.GetCurrentNS()
if err != nil {
return fmt.Errorf("failed to get init netns: %v", err)
}
return netns.Do(func(_ ns.NetNS) error {
// get VF device
logging.Debug("Get VF device",
"func", "ReleaseVF",
"podifName", podifName)
linkObj, err := s.nLink.LinkByName(podifName)
if err != nil {
return fmt.Errorf("failed to get netlink device with name %s: %q", podifName, err)
}
// shutdown VF device
logging.Debug("Shutdown VF device",
"func", "ReleaseVF",
"linkObj", linkObj)
if err = s.nLink.LinkSetDown(linkObj); err != nil {
return fmt.Errorf("failed to set link %s down: %q", podifName, err)
}
// rename VF device
logging.Debug("Rename VF device",
"func", "ReleaseVF",
"linkObj", linkObj,
"conf.OrigVfState.HostIFName", conf.OrigVfState.HostIFName)
err = s.nLink.LinkSetName(linkObj, conf.OrigVfState.HostIFName)
if err != nil {
return fmt.Errorf("failed to rename link %s to host name %s: %q", podifName, conf.OrigVfState.HostIFName, err)
}
if conf.MAC != "" {
// reset effective MAC address
logging.Debug("Reset effective MAC address",
"func", "ReleaseVF",
"s.nLink", s.nLink,
"conf.OrigVfState.HostIFName", conf.OrigVfState.HostIFName,
"conf.OrigVfState.EffectiveMAC", conf.OrigVfState.EffectiveMAC)
err = utils.SetVFEffectiveMAC(s.nLink, conf.OrigVfState.HostIFName, conf.OrigVfState.EffectiveMAC)
if err != nil {
return fmt.Errorf("failed to restore original effective netlink MAC address %s: %v", conf.OrigVfState.EffectiveMAC, err)
}
}
// move VF device to init netns
logging.Debug("Move VF device to init netns",
"func", "ReleaseVF",
"linkObj", linkObj,
"initns.Fd()", int(initns.Fd()))
if err = s.nLink.LinkSetNsFd(linkObj, int(initns.Fd())); err != nil {
return fmt.Errorf("failed to move interface %s to init netns: %v", conf.OrigVfState.HostIFName, err)
}
return nil
})
}
func getVfInfo(link netlink.Link, id int) *netlink.VfInfo {
attrs := link.Attrs()
for _, vf := range attrs.Vfs {
if vf.ID == id {
return &vf
}
}
return nil
}
// ApplyVFConfig configure a VF with parameters given in NetConf
func (s *sriovManager) ApplyVFConfig(conf *sriovtypes.NetConf) error {
pfLink, err := s.nLink.LinkByName(conf.Master)
if err != nil {
return fmt.Errorf("failed to lookup master %q: %v", conf.Master, err)
}
// 1. Set vlan
if conf.Vlan != nil {
if err = s.nLink.LinkSetVfVlanQosProto(pfLink, conf.VFID, *conf.Vlan, *conf.VlanQoS, sriovtypes.VlanProtoInt[*conf.VlanProto]); err != nil {
return fmt.Errorf("failed to set vf %d vlan configuration - id %d, qos %d and proto %s: %v", conf.VFID, *conf.Vlan, *conf.VlanQoS, *conf.VlanProto, err)
}
}
// 2. Set mac address
if conf.MAC != "" {
// when we restore the original hardware mac address we may get a device or resource busy. so we introduce retry
if err := utils.SetVFHardwareMAC(s.nLink, conf.Master, conf.VFID, conf.MAC); err != nil {
return fmt.Errorf("failed to set MAC address to %s: %v", conf.MAC, err)
}
}
// 3. Set min/max tx link rate. 0 means no rate limiting. Support depends on NICs and driver.
var minTxRate, maxTxRate int
rateConfigured := false
if conf.MinTxRate != nil {
minTxRate = *conf.MinTxRate
rateConfigured = true
}
if conf.MaxTxRate != nil {
maxTxRate = *conf.MaxTxRate
rateConfigured = true
}
if rateConfigured {
if err = s.nLink.LinkSetVfRate(pfLink, conf.VFID, minTxRate, maxTxRate); err != nil {
return fmt.Errorf("failed to set vf %d min_tx_rate to %d Mbps: max_tx_rate to %d Mbps: %v",
conf.VFID, minTxRate, maxTxRate, err)
}
}
// 4. Set spoofchk flag
if conf.SpoofChk != "" {
spoofChk := false
if conf.SpoofChk == "on" {
spoofChk = true
}
if err = s.nLink.LinkSetVfSpoofchk(pfLink, conf.VFID, spoofChk); err != nil {
return fmt.Errorf("failed to set vf %d spoofchk flag to %s: %v", conf.VFID, conf.SpoofChk, err)
}
}
// 5. Set trust flag
if conf.Trust != "" {
trust := false
if conf.Trust == "on" {
trust = true
}
if err = s.nLink.LinkSetVfTrust(pfLink, conf.VFID, trust); err != nil {
return fmt.Errorf("failed to set vf %d trust flag to %s: %v", conf.VFID, conf.Trust, err)
}
}
// 6. Set link state
if conf.LinkState != "" {
var state uint32
switch conf.LinkState {
case "auto":
state = netlink.VF_LINK_STATE_AUTO
case "enable":
state = netlink.VF_LINK_STATE_ENABLE
case "disable":
state = netlink.VF_LINK_STATE_DISABLE
default:
// the value should have been validated earlier, return error if we somehow got here
return fmt.Errorf("unknown link state %s when setting it for vf %d: %v", conf.LinkState, conf.VFID, err)
}
if err = s.nLink.LinkSetVfState(pfLink, conf.VFID, state); err != nil {
return fmt.Errorf("failed to set vf %d link state to %d: %v", conf.VFID, state, err)
}
}
// Copy the MTU value to a new variable
// and use it as a pointer
pfMtu := pfLink.Attrs().MTU
conf.MTU = &pfMtu
return nil
}
// FillOriginalVfInfo fills the original vf info
func (s *sriovManager) FillOriginalVfInfo(conf *sriovtypes.NetConf) error {
pfLink, err := s.nLink.LinkByName(conf.Master)
if err != nil {
return fmt.Errorf("failed to lookup master %q: %v", conf.Master, err)
}
// Save current the VF state before modifying it
vfState := getVfInfo(pfLink, conf.VFID)
if vfState == nil {
return fmt.Errorf("failed to find vf %d", conf.VFID)
}
conf.OrigVfState.FillFromVfInfo(vfState)
return err
}
// ResetVFConfig reset a VF to its original state
func (s *sriovManager) ResetVFConfig(conf *sriovtypes.NetConf) error {
pfLink, err := s.nLink.LinkByName(conf.Master)
if err != nil {
return fmt.Errorf("failed to lookup master %q: %v", conf.Master, err)
}
// Set 802.1q as default in case cache config does not have a value for vlan proto.
if conf.OrigVfState.VlanProto == 0 {
conf.OrigVfState.VlanProto = sriovtypes.VlanProtoInt[sriovtypes.Proto8021q]
}
if conf.Vlan != nil {
if err = s.nLink.LinkSetVfVlanQosProto(pfLink, conf.VFID, conf.OrigVfState.Vlan, conf.OrigVfState.VlanQoS, conf.OrigVfState.VlanProto); err != nil {
return fmt.Errorf("failed to set vf %d vlan configuration - id %d, qos %d and proto %d: %v", conf.VFID, conf.OrigVfState.Vlan, conf.OrigVfState.VlanQoS, conf.OrigVfState.VlanProto, err)
}
}
// Restore spoofchk
if conf.SpoofChk != "" {
if err = s.nLink.LinkSetVfSpoofchk(pfLink, conf.VFID, conf.OrigVfState.SpoofChk); err != nil {
return fmt.Errorf("failed to restore spoofchk for vf %d: %v", conf.VFID, err)
}
}
// Restore the original administrative MAC address
if conf.MAC != "" {
// when we restore the original hardware mac address we may get a device or resource busy. so we introduce retry
if err := utils.SetVFHardwareMAC(s.nLink, conf.Master, conf.VFID, conf.OrigVfState.AdminMAC); err != nil {
return fmt.Errorf("failed to restore original administrative MAC address %s: %v", conf.OrigVfState.AdminMAC, err)
}
}
// Restore VF trust
if conf.Trust != "" {
if err = s.nLink.LinkSetVfTrust(pfLink, conf.VFID, conf.OrigVfState.Trust); err != nil {
return fmt.Errorf("failed to set trust for vf %d: %v", conf.VFID, err)
}
}
// Restore rate limiting
if conf.MinTxRate != nil || conf.MaxTxRate != nil {
if err = s.nLink.LinkSetVfRate(pfLink, conf.VFID, conf.OrigVfState.MinTxRate, conf.OrigVfState.MaxTxRate); err != nil {
return fmt.Errorf("failed to disable rate limiting for vf %d %v", conf.VFID, err)
}
}
// Restore link state to `auto`
if conf.LinkState != "" {
// Reset only when link_state was explicitly specified, to accommodate for drivers / NICs
// that don't support the netlink command (e.g. igb driver)
if err = s.nLink.LinkSetVfState(pfLink, conf.VFID, conf.OrigVfState.LinkState); err != nil {
return fmt.Errorf("failed to set link state to auto for vf %d: %v", conf.VFID, err)
}
}
return nil
}