mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2025-03-16 13:28:18 +00:00
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>
105 lines
3.1 KiB
Go
105 lines
3.1 KiB
Go
/*
|
|
Copyright 2020 The Kubernetes Authors.
|
|
|
|
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 usb
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"strings"
|
|
|
|
"sigs.k8s.io/node-feature-discovery/source"
|
|
usbutils "sigs.k8s.io/node-feature-discovery/source/internal"
|
|
)
|
|
|
|
type NFDConfig struct {
|
|
DeviceClassWhitelist []string `json:"deviceClassWhitelist,omitempty"`
|
|
DeviceLabelFields []string `json:"deviceLabelFields,omitempty"`
|
|
}
|
|
|
|
var Config = NFDConfig{
|
|
// Whitelist specific USB classes: https://www.usb.org/defined-class-codes
|
|
// By default these include classes where different accelerators are typically mapped:
|
|
// Video (0e), Miscellaneous (ef), Application Specific (fe), and Vendor Specific (ff).
|
|
DeviceClassWhitelist: []string{"0e", "ef", "fe", "ff"},
|
|
DeviceLabelFields: []string{"class", "vendor", "device"},
|
|
}
|
|
|
|
// Implement FeatureSource interface
|
|
type Source struct{}
|
|
|
|
// Return name of the feature source
|
|
func (s Source) Name() string { return "usb" }
|
|
|
|
// Discover features
|
|
func (s Source) Discover() (source.Features, error) {
|
|
features := source.Features{}
|
|
|
|
// Construct a device label format, a sorted list of valid attributes
|
|
deviceLabelFields := []string{}
|
|
configLabelFields := map[string]bool{}
|
|
for _, field := range Config.DeviceLabelFields {
|
|
configLabelFields[field] = true
|
|
}
|
|
|
|
for _, attr := range usbutils.DefaultUsbDevAttrs {
|
|
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)
|
|
}
|
|
log.Printf("WARNING: invalid fields '%v' in deviceLabelFields, ignoring...", keys)
|
|
}
|
|
if len(deviceLabelFields) == 0 {
|
|
log.Printf("WARNING: no valid fields in deviceLabelFields defined, using the defaults")
|
|
deviceLabelFields = []string{"vendor", "device"}
|
|
}
|
|
|
|
// Read configured or default labels. Attributes set to 'true' are considered must-have.
|
|
deviceAttrs := map[string]bool{}
|
|
for _, label := range deviceLabelFields {
|
|
deviceAttrs[label] = true
|
|
}
|
|
|
|
devs, err := usbutils.DetectUsb(deviceAttrs)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Failed to detect USB devices: %s", err.Error())
|
|
}
|
|
|
|
// Iterate over all device classes
|
|
for class, classDevs := range devs {
|
|
for _, white := range Config.DeviceClassWhitelist {
|
|
if strings.HasPrefix(class, strings.ToLower(white)) {
|
|
for _, dev := range classDevs {
|
|
devLabel := ""
|
|
for i, attr := range deviceLabelFields {
|
|
devLabel += dev[attr]
|
|
if i < len(deviceLabelFields)-1 {
|
|
devLabel += "_"
|
|
}
|
|
}
|
|
features[devLabel+".present"] = true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return features, nil
|
|
}
|