2020-02-16 16:27:03 +02:00
|
|
|
/*
|
2021-02-23 10:05:13 +02:00
|
|
|
Copyright 2020-2021 The Kubernetes Authors.
|
2020-02-16 16:27:03 +02: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.
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
package busutils
|
2020-02-16 16:27:03 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2020-05-20 14:32:07 +03:00
|
|
|
|
2021-02-23 10:05:13 +02:00
|
|
|
"k8s.io/klog/v2"
|
|
|
|
|
2020-05-20 14:32:07 +03:00
|
|
|
"sigs.k8s.io/node-feature-discovery/source"
|
2020-02-16 16:27:03 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type PciDeviceInfo map[string]string
|
|
|
|
|
|
|
|
var DefaultPciDevAttrs = []string{"class", "vendor", "device", "subsystem_vendor", "subsystem_device"}
|
|
|
|
var ExtraPciDevAttrs = []string{"sriov_totalvfs"}
|
|
|
|
|
|
|
|
// Read a single PCI device attribute
|
|
|
|
// A PCI attribute in this context, maps to the corresponding sysfs file
|
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
|
|
|
func readSinglePciAttribute(devPath string, attrName string) (string, error) {
|
2020-02-16 16:27:03 +02:00
|
|
|
data, err := ioutil.ReadFile(path.Join(devPath, attrName))
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("failed to read device attribute %s: %v", attrName, err)
|
|
|
|
}
|
|
|
|
// Strip whitespace and '0x' prefix
|
|
|
|
attrVal := strings.TrimSpace(strings.TrimPrefix(string(data), "0x"))
|
|
|
|
|
|
|
|
if attrName == "class" && len(attrVal) > 4 {
|
|
|
|
// Take four first characters, so that the programming
|
|
|
|
// interface identifier gets stripped from the raw class code
|
|
|
|
attrVal = attrVal[0:4]
|
|
|
|
}
|
|
|
|
return attrVal, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read information of one PCI device
|
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
|
|
|
func readPciDevInfo(devPath string, deviceAttrSpec map[string]bool) (PciDeviceInfo, error) {
|
2020-02-16 16:27:03 +02:00
|
|
|
info := PciDeviceInfo{}
|
|
|
|
|
|
|
|
for attr, must := range deviceAttrSpec {
|
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
|
|
|
attrVal, err := readSinglePciAttribute(devPath, attr)
|
2020-02-16 16:27:03 +02:00
|
|
|
if err != nil {
|
|
|
|
if must {
|
|
|
|
return info, fmt.Errorf("Failed to read device %s: %s", attr, err)
|
|
|
|
}
|
2020-05-19 16:39:02 +03:00
|
|
|
continue
|
2020-02-16 16:27:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
info[attr] = attrVal
|
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// List available PCI devices and retrieve device attributes.
|
|
|
|
// deviceAttrSpec is a map which specifies which attributes to retrieve.
|
|
|
|
// a false value for a specific attribute marks the attribute as optional.
|
|
|
|
// a true value for a specific attribute marks the attribute as mandatory.
|
|
|
|
// "class" attribute is considered mandatory.
|
|
|
|
// DetectPci() will fail if the retrieval of a mandatory attribute fails.
|
|
|
|
func DetectPci(deviceAttrSpec map[string]bool) (map[string][]PciDeviceInfo, error) {
|
2020-05-20 14:32:07 +03:00
|
|
|
sysfsBasePath := source.SysfsDir.Path("bus/pci/devices")
|
2020-02-16 16:27:03 +02:00
|
|
|
devInfo := make(map[string][]PciDeviceInfo)
|
|
|
|
|
2020-05-20 14:32:07 +03:00
|
|
|
devices, err := ioutil.ReadDir(sysfsBasePath)
|
2020-02-16 16:27:03 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// "class" is a mandatory attribute, inject it to spec if needed.
|
|
|
|
deviceAttrSpec["class"] = true
|
|
|
|
|
|
|
|
// Iterate over devices
|
|
|
|
for _, device := range devices {
|
2020-05-20 14:32:07 +03:00
|
|
|
info, err := readPciDevInfo(path.Join(sysfsBasePath, device.Name()), deviceAttrSpec)
|
2020-02-16 16:27:03 +02:00
|
|
|
if err != nil {
|
2021-02-23 10:05:13 +02:00
|
|
|
klog.Error(err)
|
2020-02-16 16:27:03 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
class := info["class"]
|
|
|
|
devInfo[class] = append(devInfo[class], info)
|
|
|
|
}
|
|
|
|
|
|
|
|
return devInfo, nil
|
|
|
|
}
|