-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathpci.go
289 lines (248 loc) · 8.21 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package pci
import (
"bufio"
"context"
"encoding/json"
"fmt"
"regexp"
"strings"
"github.com/leptonai/gpud/pkg/file"
"github.com/leptonai/gpud/pkg/process"
"sigs.k8s.io/yaml"
)
// Lists all PCI devices.
func List(ctx context.Context, opts ...OpOption) (Devices, error) {
op := &Op{}
if err := op.applyOpts(opts); err != nil {
return nil, err
}
lspciPath, err := file.LocateExecutable("lspci")
if err != nil {
return nil, nil
}
p, err := process.New(
process.WithBashScriptContentsToRun(fmt.Sprintf("sudo %s -vvv", lspciPath)),
process.WithRunAsBashScript(),
)
if err != nil {
return nil, err
}
if err := p.Start(ctx); err != nil {
return nil, err
}
scanner := bufio.NewScanner(p.StdoutReader())
devs, err := parseLspciVVV(ctx, scanner, op.nameMatchFunc)
if err != nil {
return nil, err
}
select {
case err := <-p.Wait():
if err != nil {
return nil, err
}
case <-ctx.Done():
return nil, ctx.Err()
}
return devs, nil
}
type Devices []Device
func (devs Devices) JSON() ([]byte, error) {
return json.Marshal(devs)
}
func (devs Devices) YAML() ([]byte, error) {
return yaml.Marshal(devs)
}
type Device struct {
// ID of the PCI device.
// e.g., "00:0e.0" in "00:0e.0 PCI ..."
ID string `json:"id"`
// Name that comes after the ID.
// e.g., "3D controller" in "00:0e.0 3D controller"
Name string `json:"name"`
// Access control service.
// e.g., Capabilities: [170 v1] Access Control Services
AccessControlService *AccessControlService `json:"access_control_service,omitempty"`
// Kernel driver in use.
// e.g., Kernel driver in use: pcieport
KernelDriverInUse string `json:"kernel_driver_in_use,omitempty"`
// Kernel modules.
// e.g., Kernel modules: nvidiafb, nouveau, nvidia_drm, nvidia
KernelModules []string `json:"kernel_modules,omitempty"`
}
type AccessControlService struct {
ACSCap ACS `json:"acs_cap"`
// Access Control Service Control, useful to validate bare metal PCI config.
// e.g.,
// If this device sets SrcValid+ (SrcValid: true), then ACS is enabled:
// "IO virtualization (also known as VT-d or IOMMU) can interfere with GPU Direct
// by redirecting all PCI point-to-point traffic to the CPU root complex,
// causing a significant performance reduction or even a hang.
// If PCI switches have ACS enabled (on baremetal systems), it needs to be disabled."
// ref. https://docs.nvidia.com/deeplearning/nccl/user-guide/docs/troubleshooting.html#pci-access-control-services-acs
ACSCtl ACS `json:"acs_ctl"`
}
// e.g.,
// ACSCap: SrcValid+ TransBlk+ ReqRedir- CmpltRedir- UpstreamFwd+ EgressCtrl- DirectTrans-
// ACSCtl: SrcValid- TransBlk- ReqRedir- CmpltRedir- UpstreamFwd- EgressCtrl- DirectTrans-
type ACS struct {
SrcValid bool `json:"src_valid"`
TransBlk bool `json:"trans_blk"`
ReqRedir bool `json:"req_redir"`
CmpltRedir bool `json:"cmplt_redir"`
UpstreamFwd bool `json:"upstream_fwd"`
EgressCtrl bool `json:"egress_ctrl"`
DirectTrans bool `json:"direct_trans"`
}
func ParseACS(s string) ACS {
a := ACS{}
fields := strings.Fields(s)
for _, field := range fields {
if len(field) < 2 {
continue
}
enabled := field[len(field)-1] == '+'
switch field[:len(field)-1] {
case "SrcValid":
a.SrcValid = enabled
case "TransBlk":
a.TransBlk = enabled
case "ReqRedir":
a.ReqRedir = enabled
case "CmpltRedir":
a.CmpltRedir = enabled
case "UpstreamFwd":
a.UpstreamFwd = enabled
case "EgressCtrl":
a.EgressCtrl = enabled
case "DirectTrans":
a.DirectTrans = enabled
}
}
return a
}
const (
// regex for PCI device header/first line
// no leading whitespace, the ID always in these formats:
// [HEXADECIMAL]:[HEXADECIMAL].[HEXADECIMAL] [any string ...]
//
// e.g.,
// 00:0e.0 PCI ...
// 00:14.0 USB ...
// 85:00.0 Bridge ...
// ec:00.0 System ...
// ff:1e.7 System
pciDeviceHeaderRegex = `^[0-9a-fA-F]{2}:[0-9a-fA-F]{2}\.[0-9a-fA-F]{1} [^\s]+`
// regex for "Access Control Services", which may have leading spaces or tabs
// e.g.,
// Capabilities: [220 v1] Access Control Services
// Capabilities: [230 v1] Access Control Services
// Capabilities: [170 v1] Access Control Services
capAccessControlServicesRegex = `^[ \t]*Capabilities: \[\w+ v\d+\] Access Control Services`
// regex for "Kernel driver in use", which may have leading spaces or tabs
// e.g.,
// Kernel driver in use: pcieport
// Kernel driver in use: nvidia
// Kernel driver in use: nvidia test
// Kernel driver in use: mlx5_core
kernelDriverInUseRegex = `^[ \t]*Kernel driver in use: [\w\s]+`
// regex for "Kernel modules", which may have leading spaces or tabs
// e.g.,
// Kernel modules: isst_if_mbox_pci
// Kernel modules: nvidiafb, nouveau, nvidia_drm, nvidia
// Kernel modules: mlx5_core
kernelModulesRegex = `^[ \t]*Kernel modules: [\w,\s]+`
)
var (
pciDeviceHeaderRegexCompiled = regexp.MustCompile(pciDeviceHeaderRegex)
capAccessControlServicesRegexCompiled = regexp.MustCompile(capAccessControlServicesRegex)
kernelDriverInUseRegexCompiled = regexp.MustCompile(kernelDriverInUseRegex)
kernelModulesRegexCompiled = regexp.MustCompile(kernelModulesRegex)
)
func parseLspciVVV(ctx context.Context, scanner *bufio.Scanner, nameMatchFunc func(string) bool) (Devices, error) {
devs := []Device{}
curDev := &Device{}
allIDs := make(map[string]struct{})
for scanner.Scan() {
line := scanner.Text()
_ = line
// found new device start
if pciDeviceHeaderRegexCompiled.MatchString(line) {
splits := strings.Fields(line)
if len(splits) < 2 {
return nil, fmt.Errorf("invalid PCI device header: %q (expected at least 2 splits)", line)
}
id := strings.TrimSpace(splits[0])
name := strings.TrimSpace(strings.Join(splits[1:], " "))
// append before we reset
// "Kernel driver in use:" but no "Kernel modules:"
if curDev.ID != "" {
devs = append(devs, *curDev)
allIDs[curDev.ID] = struct{}{}
}
// reset the current device
curDev = &Device{
ID: id,
Name: name,
}
continue
}
// parsing starts from "Access Control Services"
// e.g., "Capabilities: [230 v1] Access Control Services"
if capAccessControlServicesRegexCompiled.MatchString(line) {
curDev.AccessControlService = &AccessControlService{}
continue
}
// curDev.AccessControlService != nil: "Capabilities: [230 v1] Access Control Services" was found in the previous line
if curDev.AccessControlService != nil && strings.HasPrefix(strings.TrimSpace(strings.TrimSpace(line)), "ACSCap:") {
curDev.AccessControlService.ACSCap = ParseACS(strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), "ACSCap:")))
continue
}
// curDev.AccessControlService != nil : "Capabilities: [230 v1] Access Control Services" was found in the previous line
// e.g., ACSCtl: SrcValid- TransBlk- ReqRedir- CmpltRedir- UpstreamFwd- EgressCtrl- DirectTrans-
if curDev.AccessControlService != nil && strings.HasPrefix(strings.TrimSpace(strings.TrimSpace(line)), "ACSCtl:") {
curDev.AccessControlService.ACSCtl = ParseACS(strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), "ACSCtl:")))
continue
}
// "Kernel driver in use:"
if kernelDriverInUseRegexCompiled.MatchString(line) {
trimmed := strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), "Kernel driver in use:"))
curDev.KernelDriverInUse = trimmed
continue
}
// parsing ends at "Kernel driver in use:" or "Kernel modules:"
// e.g., "Kernel driver in use: pcieport"
if kernelModulesRegexCompiled.MatchString(line) {
trimmed := strings.TrimSpace(strings.TrimPrefix(strings.TrimSpace(line), "Kernel modules:"))
curDev.KernelModules = strings.Fields(trimmed)
// we are at the end of the device
devs = append(devs, *curDev)
curDev = &Device{}
continue
}
select {
case <-ctx.Done():
return nil, ctx.Err()
default:
}
}
if serr := scanner.Err(); serr != nil {
// process already dead, thus ignore
// e.g., "read |0: file already closed"
if !strings.Contains(serr.Error(), "file already closed") {
return nil, serr
}
}
_, ok := allIDs[curDev.ID]
if curDev.ID != "" && !ok {
devs = append(devs, *curDev)
allIDs[curDev.ID] = struct{}{}
}
filtered := []Device{}
for _, dev := range devs {
if nameMatchFunc != nil && !nameMatchFunc(dev.Name) {
continue
}
filtered = append(filtered, dev)
}
return filtered, nil
}