mirror of
https://github.com/kubernetes-sigs/node-feature-discovery.git
synced 2025-03-14 20:56:42 +00:00
Move PCI introspection logic to its own internal package
This will enable code reuse across sources while preventing packages which are not under 'source' to import it. subsequent commits will introduce the 'custom' source which will use the logic.
This commit is contained in:
parent
4b799b7e8f
commit
a1a2429df1
2 changed files with 102 additions and 59 deletions
98
source/internal/pci_utils.go
Normal file
98
source/internal/pci_utils.go
Normal file
|
@ -0,0 +1,98 @@
|
|||
/*
|
||||
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 pciutils
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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
|
||||
func readSingleAttribute(devPath string, attrName string) (string, error) {
|
||||
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
|
||||
func readDevInfo(devPath string, deviceAttrSpec map[string]bool) (PciDeviceInfo, error) {
|
||||
info := PciDeviceInfo{}
|
||||
|
||||
for attr, must := range deviceAttrSpec {
|
||||
attrVal, err := readSingleAttribute(devPath, attr)
|
||||
if err != nil {
|
||||
if must {
|
||||
return info, fmt.Errorf("Failed to read device %s: %s", attr, err)
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
|
||||
}
|
||||
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) {
|
||||
const basePath = "/sys/bus/pci/devices/"
|
||||
devInfo := make(map[string][]PciDeviceInfo)
|
||||
|
||||
devices, err := ioutil.ReadDir(basePath)
|
||||
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 {
|
||||
info, err := readDevInfo(path.Join(basePath, device.Name()), deviceAttrSpec)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
continue
|
||||
}
|
||||
class := info["class"]
|
||||
devInfo[class] = append(devInfo[class], info)
|
||||
}
|
||||
|
||||
return devInfo, nil
|
||||
}
|
|
@ -18,16 +18,13 @@ package pci
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path"
|
||||
"strings"
|
||||
|
||||
"sigs.k8s.io/node-feature-discovery/source"
|
||||
"sigs.k8s.io/node-feature-discovery/source/internal"
|
||||
)
|
||||
|
||||
type pciDeviceInfo map[string]string
|
||||
|
||||
type NFDConfig struct {
|
||||
DeviceClassWhitelist []string `json:"deviceClassWhitelist,omitempty"`
|
||||
DeviceLabelFields []string `json:"deviceLabelFields,omitempty"`
|
||||
|
@ -38,9 +35,6 @@ var Config = NFDConfig{
|
|||
DeviceLabelFields: []string{"class", "vendor"},
|
||||
}
|
||||
|
||||
var devLabelAttrs = []string{"class", "vendor", "device", "subsystem_vendor", "subsystem_device"}
|
||||
var extraDevAttrs = []string{"sriov_totalvfs"}
|
||||
|
||||
// Implement FeatureSource interface
|
||||
type Source struct{}
|
||||
|
||||
|
@ -58,7 +52,7 @@ func (s Source) Discover() (source.Features, error) {
|
|||
configLabelFields[field] = true
|
||||
}
|
||||
|
||||
for _, attr := range devLabelAttrs {
|
||||
for _, attr := range pciutils.DefaultPciDevAttrs {
|
||||
if _, ok := configLabelFields[attr]; ok {
|
||||
deviceLabelFields = append(deviceLabelFields, attr)
|
||||
delete(configLabelFields, attr)
|
||||
|
@ -79,14 +73,14 @@ func (s Source) Discover() (source.Features, error) {
|
|||
// Read extraDevAttrs + configured or default labels. Attributes
|
||||
// set to 'true' are considered must-have.
|
||||
deviceAttrs := map[string]bool{}
|
||||
for _, label := range extraDevAttrs {
|
||||
for _, label := range pciutils.ExtraPciDevAttrs {
|
||||
deviceAttrs[label] = false
|
||||
}
|
||||
for _, label := range deviceLabelFields {
|
||||
deviceAttrs[label] = true
|
||||
}
|
||||
|
||||
devs, err := detectPci(deviceAttrs)
|
||||
devs, err := pciutils.DetectPci(deviceAttrs)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("Failed to detect PCI devices: %s", err.Error())
|
||||
}
|
||||
|
@ -112,54 +106,5 @@ func (s Source) Discover() (source.Features, error) {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
return features, nil
|
||||
}
|
||||
|
||||
// Read information of one PCI device
|
||||
func readDevInfo(devPath string, devAttrs map[string]bool) (pciDeviceInfo, error) {
|
||||
info := pciDeviceInfo{}
|
||||
for attr, must := range devAttrs {
|
||||
data, err := ioutil.ReadFile(path.Join(devPath, attr))
|
||||
if err != nil {
|
||||
if must {
|
||||
return info, fmt.Errorf("Failed to read device %s: %s", attr, err)
|
||||
} else {
|
||||
continue
|
||||
}
|
||||
}
|
||||
// Strip whitespace and '0x' prefix
|
||||
info[attr] = strings.TrimSpace(strings.TrimPrefix(string(data), "0x"))
|
||||
|
||||
if attr == "class" && len(info[attr]) > 4 {
|
||||
// Take four first characters, so that the programming
|
||||
// interface identifier gets stripped from the raw class code
|
||||
info[attr] = info[attr][0:4]
|
||||
}
|
||||
}
|
||||
return info, nil
|
||||
}
|
||||
|
||||
// List available PCI devices
|
||||
func detectPci(devAttrs map[string]bool) (map[string][]pciDeviceInfo, error) {
|
||||
const basePath = "/sys/bus/pci/devices/"
|
||||
devInfo := make(map[string][]pciDeviceInfo)
|
||||
|
||||
devices, err := ioutil.ReadDir(basePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Iterate over devices
|
||||
for _, device := range devices {
|
||||
info, err := readDevInfo(path.Join(basePath, device.Name()), devAttrs)
|
||||
if err != nil {
|
||||
log.Print(err)
|
||||
continue
|
||||
}
|
||||
class := info["class"]
|
||||
devInfo[class] = append(devInfo[class], info)
|
||||
}
|
||||
|
||||
return devInfo, nil
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue