-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpci.go
249 lines (216 loc) · 6.51 KB
/
pci.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Source: https://github.com/TimRots/gutil-linux/blob/master/pci/pci.go
package main
import (
"bufio"
"embed"
"errors"
"fmt"
"os"
"path/filepath"
"regexp"
"strings"
)
const (
PATH_SYS_BUS_PCI_DEVICES = "/sys/bus/pci/devices"
PATH_SYS_DEVICES_PCI = "/sys/devices/pci"
)
//go:embed pci.ids
var pciIDs embed.FS
var nonWhitespaceRegex = regexp.MustCompile(`[\S]+`)
type PciDevice struct {
Bus string
VendorID string
DeviceID string
Class string
SubsysVendor string
SubsysDevice string
Irq string
Revision string
VendorName string
DeviceName string
DeviceClass string
Subsystem string
KernelModule string
KernelModuleAlias string
KernelDriver string
IommuGroup string
}
func readFromFile(f string, w, start, end int) (string, error) {
if start < 0 || end < 0 || start > end {
return "", errors.New("invalid start:end")
}
var value []string
file, err := os.Open(f)
if err != nil {
return "", err
}
defer file.Close()
if w == 0 {
w = 1
}
scanner, i, w := bufio.NewScanner(file), 0, w
for scanner.Scan() {
for _, word := range nonWhitespaceRegex.FindAllString(scanner.Text(), -1) {
i++
if i == w {
value = append(value, word)
}
ret := strings.Join(value, " ")
if start != 0 || end != 0 {
if start >= len(ret) || end > len(ret) {
return "", errors.New("invalid start:end")
}
return ret[start:end], nil
} else {
return ret, nil
}
}
}
return "", nil
}
func ParsePciDevices() (pciDevices []PciDevice, err error) {
var devices []string
var path string
read := func(bus, filename string, start, end int) (string, error) {
return readFromFile(filepath.Join(PATH_SYS_BUS_PCI_DEVICES, bus, filename), 1, start, end)
}
lookupKernelDriver := func(bus string) (string, error) {
path = filepath.Join(PATH_SYS_BUS_PCI_DEVICES, bus, "uevent")
read, err := readFromFile(path, 1, 0, 0)
if err != nil {
return "", err
}
if strings.Contains(read, "DRIVER=") {
return read[7:], nil
}
return "", nil
}
// Find all devices in /sys/bus/pci/devices/ and append each device to devices[]
if err = filepath.Walk(
PATH_SYS_BUS_PCI_DEVICES,
func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
devices = append(devices, info.Name())
}
return nil
}); err != nil {
err = fmt.Errorf("failed to walk %s: %w", PATH_SYS_BUS_PCI_DEVICES, err)
return pciDevices, err
}
var errs []error
// Iterate over each bus and parse & append values to PciDevices[]
for _, bus := range devices {
var (
dev string
ven string
class string
subDev string
subVen string
mod string
irq string
rev string
venName string
devName string
devClass string
subSys string
kernelDriver string
iommuGroup string
)
if dev, err = read(bus, "device", 2, 6); err != nil {
errs = append(errs, fmt.Errorf("failed to read device value from bus %s: %w", bus, err))
continue
}
if ven, err = read(bus, "vendor", 2, 6); err != nil {
errs = append(errs, fmt.Errorf("failed to read vendor value from bus %s: %w", bus, err))
continue
}
if class, err = read(bus, "class", 2, 6); err != nil {
errs = append(errs, fmt.Errorf("failed to read class value from bus %s: %w", bus, err))
continue
}
if subDev, err = read(bus, "subsystem_device", 2, 6); err != nil {
errs = append(errs, fmt.Errorf("failed to read subsystem_device value from bus %s: %w", bus, err))
continue
}
if subVen, err = read(bus, "subsystem_vendor", 2, 6); err != nil {
errs = append(errs, fmt.Errorf("failed to read subsystem_vendor value from bus %s: %w", bus, err))
continue
}
if mod, err = read(bus, "modalias", 0, 0); err != nil {
errs = append(errs, fmt.Errorf("failed to read modalias value from bus %s: %w", bus, err))
continue
}
if irq, err = read(bus, "irq", 0, 0); err != nil {
errs = append(errs, fmt.Errorf("failed to read irq value from bus %s: %w", bus, err))
continue
}
if rev, err = read(bus, "revision", 2, 4); err != nil {
errs = append(errs, fmt.Errorf("failed to read irq value from bus %s: %w", bus, err))
continue
}
if kernelDriver, err = lookupKernelDriver(bus); err != nil {
errs = append(errs, fmt.Errorf("failed to look up kernel driver used for bus %s: %w", bus, err))
continue
}
venName, _ = Lookup("vendor", ven, "", "", "")
devName, _ = Lookup("device", ven, dev, "", "")
devClass, _ = Lookup("class", "", "", class, "")
if subVen != "0000" {
subSys, _ = Lookup("subsystem", ven, "", "", subDev)
}
ln, _ := os.Readlink(filepath.Join(PATH_SYS_BUS_PCI_DEVICES, bus, "iommu_group"))
if len(ln) > 0 {
g := strings.Split(ln, "/")
iommuGroup = g[len(g)-1]
}
pciDevices = append(pciDevices,
PciDevice{
bus, ven, dev, class, subVen, subDev, irq, rev,
venName, devName, devClass, subSys, "", mod, kernelDriver,
iommuGroup,
},
)
}
if len(errs) > 0 {
err = errors.Join(errs...)
}
return pciDevices, err
}
func Lookup(searchType, ven, dev, class, subclass string) (string, error) {
var found bool = false
// Open pci.ids
f, _ := pciIDs.Open("pci.ids")
// close pci.ids file when done
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
switch searchType {
case "vendor": // Return first occurrence of ven that does not have a \t prefix.
if strings.Contains(scanner.Text(), ven) && !strings.HasPrefix(scanner.Text(), "\t") {
return strings.TrimLeft(scanner.Text(), ven+" "), nil
}
case "device": // Return first occurrence of dev after vendor is found
if strings.Contains(scanner.Text(), ven) && !strings.HasPrefix(scanner.Text(), "\t") {
found = true
}
if strings.HasPrefix(scanner.Text(), "\t"+dev) && found {
return strings.TrimLeft(scanner.Text(), "\t\t"+dev+" "), nil
}
case "class": // Split class (eg: 0600), search for "C 06" and return first occurrence of "\t\t00" thereafter.
if strings.Contains(scanner.Text(), "C"+" "+class[0:2]) && !strings.HasPrefix(scanner.Text(), "\t") {
found = true
}
if strings.HasPrefix(scanner.Text(), "\t"+class[2:4]) && found {
return strings.TrimLeft(scanner.Text(), "\t\t"+class+" "), nil
}
case "subsystem": // Match and return line "\t\t vendor subsystem_device"
if strings.Contains(scanner.Text(), ven+" "+subclass) {
return strings.TrimLeft(scanner.Text(), "\t\t"+ven+" "+subclass), nil
}
}
}
if err := scanner.Err(); err != nil {
return "", err
}
return "Unknown " + searchType, nil
}