2018-09-20 12:49:45 +00:00
|
|
|
/*
|
2021-02-23 08:05:13 +00:00
|
|
|
Copyright 2018-2021 The Kubernetes Authors.
|
2018-09-20 12:49:45 +00:00
|
|
|
|
|
|
|
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 pci
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
2018-06-21 16:02:30 +00:00
|
|
|
|
2021-02-23 08:05:13 +00:00
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
2018-11-28 12:30:03 +00:00
|
|
|
"sigs.k8s.io/node-feature-discovery/source"
|
usb: Add support for USB device discovery
This builds on the PCI support to enable the discovery of USB devices.
This is primarily intended to be used for the discovery of Edge-based
heterogeneous accelerators that are connected via USB, such as the Coral
USB Accelerator and the Intel NCS2 - our main motivation for adding this
capability to NFD, and as part of our work in the SODALITE H2020
project.
USB devices may define their base class at either the device or
interface levels. In the case where no device class is set, the
per-device interfaces are enumerated instead. USB devices may
furthermore have multiple interfaces, which may or may not use the
identical class across each interface. We therefore report device
existence for each unique class definition to enable more fine-grained
labelling and node selection.
The default labelling format includes the class, vendor and device
(product) IDs, as follows:
feature.node.kubernetes.io/usb-fe_1a6e_089a.present=true
As with PCI, a subset of device classes are whitelisted for matching.
By default, there are only a subset of device classes under which
accelerators tend to be mapped, which is used as the basis for
the whitelist. These are:
- Video
- Miscellaneous
- Application Specific
- Vendor Specific
For those interested in matching other classes, this may be extended
by using the UsbId rule provided through the custom source. A full
list of class codes is provided by the USB-IF at:
https://www.usb.org/defined-class-codes
For the moment, owing to a lack of a demonstrable use case, neither
the subclass nor the protocol information are exposed. If this
becomes necessary, support for these attributes can be trivially
added.
Signed-off-by: Paul Mundt <paul.mundt@adaptant.io>
2020-05-14 20:32:55 +00:00
|
|
|
pciutils "sigs.k8s.io/node-feature-discovery/source/internal"
|
2018-09-20 12:49:45 +00:00
|
|
|
)
|
|
|
|
|
2020-04-21 19:03:37 +00:00
|
|
|
type Config struct {
|
2018-09-21 10:25:47 +00:00
|
|
|
DeviceClassWhitelist []string `json:"deviceClassWhitelist,omitempty"`
|
2018-09-21 12:16:41 +00:00
|
|
|
DeviceLabelFields []string `json:"deviceLabelFields,omitempty"`
|
2018-09-21 10:25:47 +00:00
|
|
|
}
|
|
|
|
|
2020-04-21 19:03:37 +00:00
|
|
|
// newDefaultConfig returns a new config with pre-populated defaults
|
|
|
|
func newDefaultConfig() *Config {
|
|
|
|
return &Config{
|
|
|
|
DeviceClassWhitelist: []string{"03", "0b40", "12"},
|
|
|
|
DeviceLabelFields: []string{"class", "vendor"},
|
|
|
|
}
|
2018-09-21 10:25:47 +00:00
|
|
|
}
|
2018-09-20 12:49:45 +00:00
|
|
|
|
|
|
|
// Implement FeatureSource interface
|
2020-04-21 19:03:37 +00:00
|
|
|
type Source struct {
|
|
|
|
config *Config
|
|
|
|
}
|
2018-09-20 12:49:45 +00:00
|
|
|
|
|
|
|
// Return name of the feature source
|
|
|
|
func (s Source) Name() string { return "pci" }
|
|
|
|
|
2020-04-21 19:03:37 +00:00
|
|
|
// NewConfig method of the FeatureSource interface
|
|
|
|
func (s *Source) NewConfig() source.Config { return newDefaultConfig() }
|
|
|
|
|
|
|
|
// GetConfig method of the FeatureSource interface
|
|
|
|
func (s *Source) GetConfig() source.Config { return s.config }
|
|
|
|
|
|
|
|
// SetConfig method of the FeatureSource interface
|
|
|
|
func (s *Source) SetConfig(conf source.Config) {
|
|
|
|
switch v := conf.(type) {
|
|
|
|
case *Config:
|
|
|
|
s.config = v
|
|
|
|
default:
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Fatalf("invalid config type: %T", conf)
|
2020-04-21 19:03:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-20 12:49:45 +00:00
|
|
|
// Discover features
|
2018-06-21 16:02:30 +00:00
|
|
|
func (s Source) Discover() (source.Features, error) {
|
|
|
|
features := source.Features{}
|
2018-09-20 12:49:45 +00:00
|
|
|
|
2018-09-21 12:16:41 +00:00
|
|
|
// Construct a device label format, a sorted list of valid attributes
|
|
|
|
deviceLabelFields := []string{}
|
|
|
|
configLabelFields := map[string]bool{}
|
2020-04-21 19:03:37 +00:00
|
|
|
for _, field := range s.config.DeviceLabelFields {
|
2018-09-21 12:16:41 +00:00
|
|
|
configLabelFields[field] = true
|
|
|
|
}
|
|
|
|
|
2020-02-16 14:27:03 +00:00
|
|
|
for _, attr := range pciutils.DefaultPciDevAttrs {
|
2018-09-21 12:16:41 +00:00
|
|
|
if _, ok := configLabelFields[attr]; ok {
|
|
|
|
deviceLabelFields = append(deviceLabelFields, attr)
|
|
|
|
delete(configLabelFields, attr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(configLabelFields) > 0 {
|
|
|
|
keys := []string{}
|
|
|
|
for key := range configLabelFields {
|
|
|
|
keys = append(keys, key)
|
|
|
|
}
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Warningf("invalid fields '%v' in deviceLabelFields, ignoring...", keys)
|
2018-09-21 12:16:41 +00:00
|
|
|
}
|
|
|
|
if len(deviceLabelFields) == 0 {
|
2021-02-23 08:05:13 +00:00
|
|
|
klog.Warningf("no valid fields in deviceLabelFields defined, using the defaults")
|
2018-09-21 12:16:41 +00:00
|
|
|
deviceLabelFields = []string{"class", "vendor"}
|
|
|
|
}
|
|
|
|
|
2020-02-14 11:22:12 +00:00
|
|
|
// Read extraDevAttrs + configured or default labels. Attributes
|
|
|
|
// set to 'true' are considered must-have.
|
|
|
|
deviceAttrs := map[string]bool{}
|
2020-02-16 14:27:03 +00:00
|
|
|
for _, label := range pciutils.ExtraPciDevAttrs {
|
2020-02-14 11:22:12 +00:00
|
|
|
deviceAttrs[label] = false
|
|
|
|
}
|
|
|
|
for _, label := range deviceLabelFields {
|
|
|
|
deviceAttrs[label] = true
|
|
|
|
}
|
|
|
|
|
2020-02-16 14:27:03 +00:00
|
|
|
devs, err := pciutils.DetectPci(deviceAttrs)
|
2020-02-14 11:22:12 +00:00
|
|
|
if err != nil {
|
2021-02-25 17:12:06 +00:00
|
|
|
return nil, fmt.Errorf("failed to detect PCI devices: %s", err.Error())
|
2020-02-14 11:22:12 +00:00
|
|
|
}
|
|
|
|
|
2018-09-21 12:16:41 +00:00
|
|
|
// Iterate over all device classes
|
2018-09-20 12:49:45 +00:00
|
|
|
for class, classDevs := range devs {
|
2020-04-21 19:03:37 +00:00
|
|
|
for _, white := range s.config.DeviceClassWhitelist {
|
2018-09-21 10:25:47 +00:00
|
|
|
if strings.HasPrefix(class, strings.ToLower(white)) {
|
2018-09-20 12:49:45 +00:00
|
|
|
for _, dev := range classDevs {
|
2018-09-21 12:16:41 +00:00
|
|
|
devLabel := ""
|
|
|
|
for i, attr := range deviceLabelFields {
|
|
|
|
devLabel += dev[attr]
|
|
|
|
if i < len(deviceLabelFields)-1 {
|
|
|
|
devLabel += "_"
|
|
|
|
}
|
|
|
|
}
|
2020-02-14 11:22:12 +00:00
|
|
|
features[devLabel+".present"] = true
|
|
|
|
|
|
|
|
if _, ok := dev["sriov_totalvfs"]; ok {
|
|
|
|
features[devLabel+".sriov.capable"] = true
|
|
|
|
}
|
2018-09-20 12:49:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-21 16:02:30 +00:00
|
|
|
return features, nil
|
2018-09-20 12:49:45 +00:00
|
|
|
}
|