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
|
package sysfs
import (
"fmt"
"io/ioutil"
"path"
"path/filepath"
"strconv"
"strings"
)
const (
sysFsPCIDevicesPath = "/sys/bus/pci/devices/"
)
type PCIDevice struct {
NumaNode int
ID string
Device, Vendor uint64
SubVendor, SubDevice uint64
Class uint64
MSIs []int
}
func ScanPCIDevices() []PCIDevice {
devices, err := ioutil.ReadDir(sysFsPCIDevicesPath)
if err != nil {
panic(err)
}
pciDevices := []PCIDevice{}
for _, device := range devices {
dpath := filepath.Join(sysFsPCIDevicesPath, device.Name())
pcid, err := NewPCIDevice(dpath, device.Name())
if err != nil {
panic(err)
}
pciDevices = append(pciDevices, pcid)
}
return pciDevices
}
func getPCIDeviceClass(path string) (uint64, error) {
return ContentUint64(filepath.Join(path, "class"))
}
func getPCIDeviceVendor(path string) (uint64, error) {
return ContentUint64(filepath.Join(path, "vendor"))
}
func getPCIDeviceId(path string) (uint64, error) {
return ContentUint64(filepath.Join(path, "device"))
}
func getPCIDeviceSubsystemDevice(path string) (uint64, error) {
return ContentUint64(filepath.Join(path, "subsystem_device"))
}
func getPCIDeviceSubsystemVendor(path string) (uint64, error) {
return ContentUint64(filepath.Join(path, "subsystem_vendor"))
}
func getPCIDeviceNumaNode(path string) int {
content, err := ioutil.ReadFile(filepath.Join(path, "numa_node"))
if err != nil {
panic(err)
}
nodeNum, err := strconv.Atoi(strings.TrimSpace(string(content)))
if err != nil {
panic(err)
}
return nodeNum
}
func getPCIDeviceMSIx(p string) []int {
g := fmt.Sprintf("%s/*", filepath.Join(p, "msi_irqs"))
files, err := filepath.Glob(g)
if err != nil {
panic(err)
}
if len(files) == 0 {
return []int{}
}
msix := []int{}
for _, f := range files {
content, err := ioutil.ReadFile(f)
if err != nil {
panic(err)
}
if strings.TrimSpace(string(content)) == "msix" {
base := path.Base(f)
v, err := strconv.Atoi(base)
if err != nil {
panic(err)
}
msix = append(msix, v)
}
}
return msix
}
func NewPCIDevice(path, name string) (PCIDevice, error) {
nodeNum := getPCIDeviceNumaNode(path)
device, err := getPCIDeviceId(path)
if err != nil {
return PCIDevice{}, err
}
vendor, err := getPCIDeviceVendor(path)
if err != nil {
return PCIDevice{}, err
}
subvendor, err := getPCIDeviceSubsystemVendor(path)
if err != nil {
return PCIDevice{}, err
}
subdevice, err := getPCIDeviceSubsystemDevice(path)
if err != nil {
return PCIDevice{}, err
}
deviceClass, err := getPCIDeviceClass(path)
if err != nil {
return PCIDevice{}, err
}
msix := getPCIDeviceMSIx(path)
return PCIDevice{
ID: name,
Device: device,
Class: deviceClass,
NumaNode: nodeNum,
Vendor: vendor,
SubVendor: subvendor,
SubDevice: subdevice,
MSIs: msix,
}, nil
}
|