forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pci.go
177 lines (166 loc) · 4.13 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
//
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//
package ghw
import (
"bytes"
"fmt"
"regexp"
"strings"
"github.com/jaypipes/pcidb"
)
var (
regexPCIAddress *regexp.Regexp = regexp.MustCompile(
`^(([0-9a-f]{0,4}):)?([0-9a-f]{2}):([0-9a-f]{2})\.([0-9a-f]{1})$`,
)
)
type PCIDevice struct {
// The PCI address of the device
Address string `json:"address"`
Vendor *pcidb.Vendor `json:"vendor"`
Product *pcidb.Product `json:"product"`
Subsystem *pcidb.Product `json:"subsystem"`
// optional subvendor/sub-device information
Class *pcidb.Class `json:"class"`
// optional sub-class for the device
Subclass *pcidb.Subclass `json:"subclass"`
// optional programming interface
ProgrammingInterface *pcidb.ProgrammingInterface `json:"programming_interface"`
}
// NOTE(jaypipes) PCIDevice has a custom JSON marshaller because we don't want
// to serialize the entire PCIDB information for the Vendor (which includes all
// of the vendor's products, etc). Instead, we simply serialize the ID and
// human-readable name of the vendor, product, class, etc.
func (pd *PCIDevice) MarshalJSON() ([]byte, error) {
b := bytes.NewBufferString("{")
b.WriteString(fmt.Sprintf("\"address\":\"%s\"", pd.Address))
b.WriteString(",\"vendor\": {")
b.WriteString(
fmt.Sprintf(
"\"id\":\"%s\",\"name\":\"%s\"",
pd.Vendor.ID,
pd.Vendor.Name,
),
)
b.WriteString("},")
b.WriteString("\"product\": {")
b.WriteString(
fmt.Sprintf(
"\"id\":\"%s\",\"name\":\"%s\"",
pd.Product.ID,
pd.Product.Name,
),
)
b.WriteString("},")
b.WriteString("\"subsystem\": {")
b.WriteString(
fmt.Sprintf(
"\"id\":\"%s\",\"name\":\"%s\"",
pd.Subsystem.ID,
pd.Subsystem.Name,
),
)
b.WriteString("},")
b.WriteString("\"class\": {")
b.WriteString(
fmt.Sprintf(
"\"id\":\"%s\",\"name\":\"%s\"",
pd.Class.ID,
pd.Class.Name,
),
)
b.WriteString("},")
b.WriteString("\"subclass\": {")
b.WriteString(
fmt.Sprintf(
"\"id\":\"%s\",\"name\":\"%s\"",
pd.Subclass.ID,
pd.Subclass.Name,
),
)
b.WriteString("},")
b.WriteString("\"programming_interface\": {")
b.WriteString(
fmt.Sprintf(
"\"id\":\"%s\",\"name\":\"%s\"",
pd.ProgrammingInterface.ID,
pd.ProgrammingInterface.Name,
),
)
b.WriteString("}")
b.WriteString("}")
return b.Bytes(), nil
}
func (di *PCIDevice) String() string {
vendorName := UNKNOWN
if di.Vendor != nil {
vendorName = di.Vendor.Name
}
productName := UNKNOWN
if di.Product != nil {
productName = di.Product.Name
}
className := UNKNOWN
if di.Class != nil {
className = di.Class.Name
}
return fmt.Sprintf(
"%s -> class: '%s' vendor: '%s' product: '%s'",
di.Address,
className,
vendorName,
productName,
)
}
type PCIInfo struct {
ctx *context
// hash of class ID -> class information
Classes map[string]*pcidb.Class
// hash of vendor ID -> vendor information
Vendors map[string]*pcidb.Vendor
// hash of vendor ID + product/device ID -> product information
Products map[string]*pcidb.Product
}
type PCIAddress struct {
Domain string
Bus string
Slot string
Function string
}
// Given a string address, returns a complete PCIAddress struct, filled in with
// domain, bus, slot and function components. The address string may either
// be in $BUS:$SLOT.$FUNCTION (BSF) format or it can be a full PCI address
// that includes the 4-digit $DOMAIN information as well:
// $DOMAIN:$BUS:$SLOT.$FUNCTION.
//
// Returns "" if the address string wasn't a valid PCI address.
func PCIAddressFromString(address string) *PCIAddress {
addrLowered := strings.ToLower(address)
matches := regexPCIAddress.FindStringSubmatch(addrLowered)
if len(matches) == 6 {
dom := "0000"
if matches[1] != "" {
dom = matches[2]
}
return &PCIAddress{
Domain: dom,
Bus: matches[3],
Slot: matches[4],
Function: matches[5],
}
}
return nil
}
func PCI(opts ...*WithOption) (*PCIInfo, error) {
mergeOpts := mergeOptions(opts...)
ctx := &context{
chroot: *mergeOpts.Chroot,
}
info := &PCIInfo{}
if err := ctx.pciFillInfo(info); err != nil {
return nil, err
}
return info, nil
}