-
Notifications
You must be signed in to change notification settings - Fork 259
/
Copy pathutils.go
122 lines (103 loc) · 3.75 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Copyright 2020-2021 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 pci
import (
"bytes"
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"k8s.io/klog/v2"
nfdv1alpha1 "sigs.k8s.io/node-feature-discovery/api/nfd/v1alpha1"
"sigs.k8s.io/node-feature-discovery/pkg/utils/hostpath"
)
var mandatoryDevAttrs = []string{"class", "vendor", "device", "subsystem_vendor", "subsystem_device"}
var optionalDevAttrs = []string{"sriov_totalvfs", "iommu_group/type", "iommu/intel-iommu/version"}
// Read a single PCI device attribute
// A PCI attribute in this context, maps to the corresponding sysfs file
func readSinglePciAttribute(devPath string, attrName string) (string, error) {
data, err := os.ReadFile(filepath.Join(devPath, attrName))
if err != nil {
return "", fmt.Errorf("failed to read device attribute %s: %w", 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 readPciDevInfo(devPath string) (*nfdv1alpha1.InstanceFeature, error) {
attrs := make(map[string]string)
for _, attr := range mandatoryDevAttrs {
attrVal, err := readSinglePciAttribute(devPath, attr)
if err != nil {
return nil, fmt.Errorf("failed to read device %s: %w", attr, err)
}
attrs[attr] = attrVal
}
for _, attr := range optionalDevAttrs {
attrVal, err := readSinglePciAttribute(devPath, attr)
if err == nil {
attrs[attr] = attrVal
}
}
return nfdv1alpha1.NewInstanceFeature(attrs), nil
}
type DevGroupedEntry struct {
Count int
Bytes []byte
}
// detectPci detects available PCI devices and retrieves their device attributes.
// An error is returned if reading any of the mandatory attributes fails.
func detectPci() ([]nfdv1alpha1.InstanceFeature, error) {
sysfsBasePath := hostpath.SysfsDir.Path("bus/pci/devices")
devices, err := os.ReadDir(sysfsBasePath)
if err != nil {
return nil, err
}
// Iterate over devices
devInfo := make([]nfdv1alpha1.InstanceFeature, 0, len(devices))
devGrouped := make(map[string]map[string]DevGroupedEntry)
for _, device := range devices {
info, err := readPciDevInfo(filepath.Join(sysfsBasePath, device.Name()))
if err != nil {
klog.ErrorS(err, "failed to read PCI device info")
continue
}
b, err := json.Marshal(info.Attributes)
if err != nil {
return nil, err
}
if entry, ok := devGrouped[info.Attributes["vendor"]][info.Attributes["device"]]; !ok {
devGrouped[info.Attributes["vendor"]] = make(map[string]DevGroupedEntry)
devGrouped[info.Attributes["vendor"]][info.Attributes["device"]] = DevGroupedEntry{Bytes: b, Count: 1}
devInfo = append(devInfo, *info)
} else {
result := bytes.Compare(b, devGrouped[info.Attributes["vendor"]][info.Attributes["device"]].Bytes)
if result == 0 {
entry.Count += 1
devGrouped[info.Attributes["vendor"]][info.Attributes["device"]] = entry
}
}
}
for _, dev := range devInfo {
entry := devGrouped[dev.Attributes["vendor"]][dev.Attributes["device"]]
dev.Attributes["count"] = string(entry.Count)
}
return devInfo, nil
}