2020-02-16 17:22:07 +02:00
|
|
|
/*
|
|
|
|
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 custom
|
|
|
|
|
|
|
|
import (
|
2020-02-16 17:33:27 +02:00
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
|
2020-02-16 17:22:07 +02:00
|
|
|
"sigs.k8s.io/node-feature-discovery/source"
|
2020-02-16 17:33:27 +02:00
|
|
|
"sigs.k8s.io/node-feature-discovery/source/custom/rules"
|
2020-02-16 17:22:07 +02:00
|
|
|
)
|
|
|
|
|
2020-02-16 17:24:06 +02:00
|
|
|
// Custom Features Configurations
|
2020-02-16 17:22:07 +02:00
|
|
|
type MatchRule struct {
|
2020-05-19 16:01:43 +03:00
|
|
|
PciID *rules.PciIDRule `json:"pciId,omitempty"`
|
|
|
|
UsbID *rules.UsbIDRule `json:"usbId,omitempty"`
|
|
|
|
LoadedKMod *rules.LoadedKModRule `json:"loadedKMod,omitempty"`
|
2020-02-16 17:22:07 +02:00
|
|
|
}
|
|
|
|
|
2020-05-19 16:01:43 +03:00
|
|
|
type FeatureSpec struct {
|
2020-02-16 17:22:07 +02:00
|
|
|
Name string `json:"name"`
|
|
|
|
MatchOn []MatchRule `json:"matchOn"`
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:01:43 +03:00
|
|
|
type NFDConfig []FeatureSpec
|
2020-02-16 17:22:07 +02:00
|
|
|
|
|
|
|
var Config = NFDConfig{}
|
|
|
|
|
|
|
|
// Implements FeatureSource Interface
|
|
|
|
type Source struct{}
|
|
|
|
|
|
|
|
// Return name of the feature source
|
|
|
|
func (s Source) Name() string { return "custom" }
|
|
|
|
|
|
|
|
// Discover features
|
|
|
|
func (s Source) Discover() (source.Features, error) {
|
|
|
|
features := source.Features{}
|
2020-02-16 17:33:27 +02:00
|
|
|
allFeatureConfig := append(getStaticFeatureConfig(), Config...)
|
|
|
|
log.Printf("INFO: Custom features: %+v", allFeatureConfig)
|
|
|
|
// Iterate over features
|
|
|
|
for _, customFeature := range allFeatureConfig {
|
|
|
|
featureExist, err := s.discoverFeature(customFeature)
|
|
|
|
if err != nil {
|
|
|
|
return features, fmt.Errorf("failed to discover feature: %s. %s", customFeature.Name, err.Error())
|
|
|
|
}
|
|
|
|
if featureExist {
|
|
|
|
features[customFeature.Name] = true
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 17:22:07 +02:00
|
|
|
return features, nil
|
|
|
|
}
|
2020-02-16 17:33:27 +02:00
|
|
|
|
|
|
|
// Process a single feature by Matching on the defined rules.
|
|
|
|
// A feature is present if all defined Rules in a MatchRule return a match.
|
2020-05-19 16:01:43 +03:00
|
|
|
func (s Source) discoverFeature(feature FeatureSpec) (bool, error) {
|
2020-02-16 17:33:27 +02:00
|
|
|
for _, rule := range feature.MatchOn {
|
|
|
|
// PCI ID rule
|
2020-05-19 16:01:43 +03:00
|
|
|
if rule.PciID != nil {
|
|
|
|
match, err := rule.PciID.Match()
|
2020-02-16 17:33:27 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
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 22:32:55 +02:00
|
|
|
// USB ID rule
|
2020-05-19 16:01:43 +03:00
|
|
|
if rule.UsbID != nil {
|
|
|
|
match, err := rule.UsbID.Match()
|
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 22:32:55 +02:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2020-02-16 17:33:27 +02:00
|
|
|
// Loaded kernel module rule
|
|
|
|
if rule.LoadedKMod != nil {
|
|
|
|
match, err := rule.LoadedKMod.Match()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, nil
|
|
|
|
}
|