forked from k8snetworkplumbingwg/sriov-network-device-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
336 lines (288 loc) · 9.54 KB
/
manager.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
// Copyright 2018 Intel Corp. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
"github.com/golang/glog"
"github.com/jaypipes/ghw"
"github.com/vishvananda/netlink"
"github.com/intel/sriov-network-device-plugin/pkg/resources"
"github.com/intel/sriov-network-device-plugin/pkg/types"
"github.com/intel/sriov-network-device-plugin/pkg/utils"
)
const (
socketSuffix = "sock"
netClass = 0x02 // Device class - Network controller. ref: https://pci-ids.ucw.cz/read/PD/02 (for Sub-Classes)
accClass = 0x12 // Device class - Processing accelerator
rsuDevice = 0x0b30 // RSU device - only used for programming the FPGA
)
/*
Network controller subclasses. ref: https://pci-ids.ucw.cz/read/PD/02
00 Ethernet controller
01 Token ring network controller
02 FDDI network controller
03 ATM network controller
04 ISDN controller
05 WorldFip controller
06 PICMG controller
07 Infiniband controller
08 Fabric controller
80 Network controller
12 Processing Accelerator
*/
type cliParams struct {
configFile string
resourcePrefix string
}
type resourceManager struct {
cliParams
pluginWatchMode bool
socketSuffix string
rFactory types.ResourceFactory
configList []*types.ResourceConfig // resourceName -> resourcePool
resourceServers []types.ResourceServer
netDeviceList []types.PciNetDevice // all network devices in host
linkWatchList map[string]types.LinkWatcher // SRIOV PF list - for watching link status
}
func newResourceManager(cp *cliParams) *resourceManager {
pluginWatchMode := utils.DetectPluginWatchMode(types.SockDir)
if pluginWatchMode {
glog.Infof("Using Kubelet Plugin Registry Mode")
} else {
glog.Infof("Using Deprecated Device Plugin Registry Path")
}
return &resourceManager{
cliParams: *cp,
pluginWatchMode: pluginWatchMode,
rFactory: resources.NewResourceFactory(cp.resourcePrefix, socketSuffix, pluginWatchMode),
netDeviceList: make([]types.PciNetDevice, 0),
linkWatchList: make(map[string]types.LinkWatcher, 0),
}
}
// Read and validate configurations from Config file
func (rm *resourceManager) readConfig() error {
resources := &types.ResourceConfList{}
rawBytes, err := ioutil.ReadFile(rm.configFile)
if err != nil {
return fmt.Errorf("error reading file %s, %v", rm.configFile, err)
}
if err = json.Unmarshal(rawBytes, resources); err != nil {
return fmt.Errorf("error unmarshalling raw bytes %v", err)
}
glog.Infof("ResourceList: %+v", resources.ResourceList)
for i := range resources.ResourceList {
rm.configList = append(rm.configList, &resources.ResourceList[i])
}
return nil
}
func (rm *resourceManager) initServers() error {
rf := rm.rFactory
glog.Infof("number of config: %d\n", len(rm.configList))
for _, rc := range rm.configList {
// Create new ResourcePool
glog.Infof("")
glog.Infof("Creating new ResourcePool: %s", rc.ResourceName)
rPool, err := rm.rFactory.GetResourcePool(rc, rm.netDeviceList)
if err != nil {
glog.Errorf("initServers(): error creating ResourcePool with config %+v: %q", rc, err)
return err
}
// Create ResourceServer with this ResourcePool
s, err := rf.GetResourceServer(rPool)
if err != nil {
glog.Errorf("initServers(): error creating ResourceServer: %v", err)
return err
}
glog.Infof("New resource server is created for %s ResourcePool", rc.ResourceName)
rm.resourceServers = append(rm.resourceServers, s)
}
return nil
}
func (rm *resourceManager) startAllServers() error {
for _, rs := range rm.resourceServers {
if err := rs.Start(); err != nil {
return err
}
// start watcher
if !rm.pluginWatchMode {
go rs.Watch()
}
}
return nil
}
func (rm *resourceManager) stopAllServers() error {
for _, rs := range rm.resourceServers {
if err := rs.Stop(); err != nil {
return err
}
}
return nil
}
// Validate configurations
func (rm *resourceManager) validConfigs() bool {
resourceName := make(map[string]string) // resource name placeholder
for _, conf := range rm.configList {
// check if name contains acceptable characters
if !utils.ValidResourceName(conf.ResourceName) {
glog.Errorf("resource name \"%s\" contains invalid characters", conf.ResourceName)
return false
}
// check resource names are unique
_, ok := resourceName[conf.ResourceName]
if ok {
// resource name already exist
glog.Errorf("resource name \"%s\" already exists", conf.ResourceName)
return false
}
resourceName[conf.ResourceName] = conf.ResourceName
}
return true
}
func (rm *resourceManager) discoverHostDevices() error {
glog.Infoln("discovering host network devices")
pci, err := ghw.PCI()
if err != nil {
return fmt.Errorf("discoverDevices(): error getting PCI info: %v", err)
}
devices := pci.ListDevices()
if len(devices) == 0 {
glog.Warningf("discoverDevices(): no PCI network device found")
}
for _, device := range devices {
devClass, err := strconv.ParseInt(device.Class.ID, 16, 64)
if err != nil {
glog.Warningf("discoverDevices(): unable to parse device class for device %+v %q", device, err)
continue
}
// only interested in network class and accelerator class
if devClass == netClass || devClass == accClass {
vendor := device.Vendor
vendorName := vendor.Name
if len(vendor.Name) > 20 {
vendorName = string([]byte(vendorName)[0:17]) + "..."
}
product := device.Product
productName := product.Name
if len(product.Name) > 40 {
productName = string([]byte(productName)[0:37]) + "..."
}
glog.Infof("discoverDevices(): device found: %-12s\t%-12s\t%-20s\t%-40s", device.Address, device.Class.ID, vendorName, productName)
// Network devices
if devClass == netClass {
rm.netDevHandler(device)
}
// Processing accelerators
if devClass == accClass {
rm.accDevHandler(device)
}
}
}
return nil
}
// hasDefaultRoute returns true if PCI network device is default route interface
func hasDefaultRoute(pciAddr string) (bool, error) {
// inUse := false
// Get net interface name
ifNames, err := utils.GetNetNames(pciAddr)
if err != nil {
return false, fmt.Errorf("error trying get net device name for device %s", pciAddr)
}
if len(ifNames) > 0 { // there's at least one interface name found
for _, ifName := range ifNames {
link, err := netlink.LinkByName(ifName)
if err != nil {
glog.Errorf("expected to get valid host interface with name %s: %q", ifName, err)
}
routes, err := netlink.RouteList(link, netlink.FAMILY_V4) // IPv6 routes: all interface has at least one link local route entry
for _, r := range routes {
if r.Dst == nil {
glog.Infof("excluding interface %s: default route found: %+v", ifName, r)
return true, nil
}
}
}
}
return false, nil
}
func (rm *resourceManager) addToLinkWatchList(pciAddr string) {
netNames, err := utils.GetNetNames(pciAddr)
if err == nil {
// There are some cases, where we may get multiple netdevice name for a PCI device
// Only add one device
if len(netNames) > 0 {
netName := netNames[0]
lw := &linkWatcher{ifName: netName}
if _, ok := rm.linkWatchList[pciAddr]; !ok {
rm.linkWatchList[netName] = lw
glog.Infof("%s added to linkWatchList", netName)
}
}
}
}
type linkWatcher struct {
ifName string
// subscribers []LinkSubscriber
}
func (lw *linkWatcher) Subscribe() {
}
func (rm *resourceManager) netDevHandler(dev *ghw.PCIDevice) {
// exclude device in-use in host
if isDefaultRoute, _ := hasDefaultRoute(dev.Address); !isDefaultRoute {
aPF := utils.IsSriovPF(dev.Address)
aVF := utils.IsSriovVF(dev.Address)
if aPF || !aVF {
// add to linkWatchList
rm.addToLinkWatchList(dev.Address)
}
if aPF && utils.SriovConfigured(dev.Address) {
// do not add this device in net device list
return
}
if newDevice, err := resources.NewPciNetDevice(dev, rm.rFactory); err == nil {
rm.netDeviceList = append(rm.netDeviceList, newDevice)
} else {
glog.Errorf("discoverDevices() error adding new device: %q", err)
}
}
}
func (rm *resourceManager) accDevHandler(dev *ghw.PCIDevice) {
//Check if device is a physical function (PF)
aPF := utils.IsSriovPF(dev.Address)
devID, err := strconv.ParseInt(dev.Product.ID, 16, 64)
if err != nil {
glog.Warningf("discoverDevices(): unable to parse device ID for device %+v %q", dev, err)
return
}
//Check if device ID = 0b30, it means it is a RSU (Remote System Update) device and not accelerator
if devID != rsuDevice {
if aPF && utils.SriovConfigured(dev.Address) {
// If it is a PF and SRIOV is configured do not add to device list
return
}
if newDevice, err := resources.NewPciNetDevice(dev, rm.rFactory); err == nil {
if newDevice.GetDriver() == "igb_uio" || newDevice.GetDriver() == "vfio-pci" {
rm.netDeviceList = append(rm.netDeviceList, newDevice)
} else {
glog.Infof("Excluding FEC device %-12s because it is not bound to userspace driver.", dev.Address)
}
} else {
glog.Errorf("discoverDevices() error adding new device: %q", err)
}
} else {
glog.Infof("Excluding device %-12s , it is a RSU device.", dev.Address)
}
}