-
Notifications
You must be signed in to change notification settings - Fork 0
/
fpga.go
278 lines (255 loc) · 7.03 KB
/
fpga.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
// Copyright 2018-2020 Xilinx Corporation. All Rights Reserved.
// Author: Brian Xu(brianx@xilinx.com)
//
// 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 main
import (
"fmt"
"io/ioutil"
pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1"
"os"
"path"
"strconv"
"strings"
)
const (
SysfsDevices = "/sys/bus/pci/devices"
UserPrefix = "/dev/dri"
SubdevPrefix = "/dev/xfpga"
QDMASTR = "dma.qdma.u"
UserPFKeyword = "drm"
DRMSTR = "renderD"
ROMSTR = "rom.u"
DSAverFile = "VBNV"
DSAtsFile = "timestamp"
XMCSTR = "xmc.u"
SerialNumFile = "serial_num"
MgmtFile = "mgmt_pf"
UserFile = "user_pf"
VendorFile = "vendor"
DeviceFile = "device"
ReadyFile = "ready"
FPGAReady = "0x1"
XilinxVendorID = "0x10ee"
ADVANTECH_ID = "0x13fe"
AWS_ID = "0x1d0f"
)
type Node struct {
User string
SubdevPath string
Qdma string
DBDF string // this is for user pf
deviceID string //devid of the user pf
}
type Device struct {
sn string
shellVer string
timestamp string
Healthy string
Nodes []Node
}
func GetInstance(DBDF string) (string, error) {
strArray := strings.Split(DBDF, ":")
domain, err := strconv.ParseUint(strArray[0], 16, 16)
if err != nil {
return "", fmt.Errorf("strconv failed: %s", strArray[0])
}
bus, err := strconv.ParseUint(strArray[1], 16, 8)
if err != nil {
return "", fmt.Errorf("strconv failed: %s", strArray[1])
}
strArray = strings.Split(strArray[2], ".")
dev, err := strconv.ParseUint(strArray[0], 16, 8)
if err != nil {
return "", fmt.Errorf("strconv failed: %s", strArray[0])
}
fc, err := strconv.ParseUint(strArray[1], 16, 8)
if err != nil {
return "", fmt.Errorf("strconv failed: %s", strArray[1])
}
ret := domain*65536 + bus*256 + dev*8 + fc
return strconv.FormatUint(ret, 10), nil
}
func GetFileNameFromPrefix(dir string, prefix string) (string, error) {
userFiles, err := ioutil.ReadDir(dir)
if err != nil {
return "", fmt.Errorf("Can't read folder %s", dir)
}
for _, userFile := range userFiles {
fname := userFile.Name()
if !strings.HasPrefix(fname, prefix) {
continue
}
return fname, nil
}
return "", nil
}
func GetFileContent(file string) (string, error) {
if buf, err := ioutil.ReadFile(file); err != nil {
return "", fmt.Errorf("Can't read file %s", file)
} else {
ret := strings.Trim(string(buf), "\n")
return ret, nil
}
}
//Prior to 2018.3 release, Xilinx FPGA has mgmt PF as func 1 and user PF
//as func 0. The func numbers of the 2 PFs are swapped after 2018.3 release.
//The FPGA device driver in (and after) 2018.3 release creates sysfs file --
//mgmt_pf and user_pf accordingly to reflect what a PF really is.
//
//The plugin will rely on this info to determine whether the a entry is mgmtPF,
//userPF, or none. This also means, it will not support 2018.2 any more.
func FileExist(fname string) bool {
if _, err := os.Stat(fname); err != nil {
if os.IsNotExist(err) {
return false
}
}
return true
}
func IsMgmtPf(pciID string) bool {
fname := path.Join(SysfsDevices, pciID, MgmtFile)
return FileExist(fname)
}
func IsUserPf(pciID string) bool {
fname := path.Join(SysfsDevices, pciID, UserFile)
return FileExist(fname)
}
func GetDevices() (map[string]Device, error) {
devices := make(map[string]Device)
pciFiles, err := ioutil.ReadDir(SysfsDevices)
if err != nil {
return nil, fmt.Errorf("Can't read folder %s", SysfsDevices)
}
for _, pciFile := range pciFiles {
pciID := pciFile.Name()
fname := path.Join(SysfsDevices, pciID, VendorFile)
vendorID, err := GetFileContent(fname)
if err != nil {
continue
}
if strings.EqualFold(vendorID, XilinxVendorID) != true &&
strings.EqualFold(vendorID, AWS_ID) != true &&
strings.EqualFold(vendorID, ADVANTECH_ID) != true {
continue
}
// For containers deployed either on top of baremetal machines,
// or deployed on top of VM, there may be only user PF assigned
// to vm(mgmt PF is not assigned to the VM)
if IsUserPf(pciID) { //user pf
userDBDF := pciID
// skip FPGAs that are not ready
fname = path.Join(SysfsDevices, pciID, ReadyFile)
content, err := GetFileContent(fname)
if err != nil {
continue
}
if strings.Compare(content, FPGAReady) != 0 {
continue
}
// get SN
xmcFolder, err := GetFileNameFromPrefix(path.Join(SysfsDevices, pciID), XMCSTR)
if err != nil {
continue
}
fname = path.Join(SysfsDevices, pciID, xmcFolder, SerialNumFile)
content, err = GetFileContent(fname)
if err != nil {
continue
}
sn := content
// get dsa version
romFolder, err := GetFileNameFromPrefix(path.Join(SysfsDevices, pciID), ROMSTR)
if err != nil {
continue
}
fname = path.Join(SysfsDevices, pciID, romFolder, DSAverFile)
content, err = GetFileContent(fname)
if err != nil {
continue
}
dsaVer := content
// get dsa timestamp
fname = path.Join(SysfsDevices, pciID, romFolder, DSAtsFile)
content, err = GetFileContent(fname)
if err != nil {
continue
}
dsaTs := content
// get device id
fname = path.Join(SysfsDevices, pciID, DeviceFile)
content, err = GetFileContent(fname)
if err != nil {
continue
}
devid := content
// get user PF node
userpf, err := GetFileNameFromPrefix(path.Join(SysfsDevices, pciID, UserPFKeyword), DRMSTR)
if err != nil {
continue
}
userNode := path.Join(UserPrefix, userpf)
node := Node{
DBDF: userDBDF,
deviceID: devid,
User: userNode,
SubdevPath: SubdevPrefix,
Qdma: "",
}
//get qdma device node if it exists
instance, err := GetInstance(userDBDF)
if err != nil {
continue
}
qdmaFolder, err := GetFileNameFromPrefix(path.Join(SysfsDevices, pciID), QDMASTR)
if err != nil {
continue
}
if qdmaFolder != "" {
node.Qdma = path.Join(SubdevPrefix, QDMASTR+instance)
}
//TODO: check temp, power, fan speed etc, to give a healthy level
//so far, return Healthy
healthy := pluginapi.Healthy
if _, ok := devices[sn]; ok {
device := devices[sn]
nodes := device.Nodes
nodes = append(nodes, node)
device.Nodes = nodes
devices[sn] = device
} else {
devices[sn] = Device{
sn: sn,
shellVer: dsaVer,
timestamp: dsaTs,
Healthy: healthy,
Nodes: []Node{node},
}
}
}
}
return devices, nil
}
/*
func main() {
devices, err := GetDevices()
if err != nil {
fmt.Printf("%s !!!\n", err)
return
}
for sn, device := range devices {
fmt.Printf("S/N: %s\n", sn)
fmt.Printf("%v\n", device)
}
}
*/