forked from hustcat/k8s-rdma-device-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdma.go
148 lines (131 loc) · 3.06 KB
/
rdma.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
package main
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"strconv"
"github.com/hustcat/k8s-rdma-device-plugin/ibverbs"
pluginapi "k8s.io/kubernetes/pkg/kubelet/apis/deviceplugin/v1beta1"
)
const (
RdmaDeviceRource = "/sys/class/infiniband/%s/device/resource"
NetDeviceRource = "/sys/class/net/%s/device/resource"
RdmaDeviceNumaNode = "/sys/class/infiniband/%s/device/numa_node"
)
func GetDevices(masterNetDevice string) ([]Device, error) {
if masterNetDevice == "" {
return getAllRdmaDeivces()
} else {
return getRdmaDeivces(masterNetDevice)
}
}
func getAllRdmaDeivces() ([]Device, error) {
var devs []Device
// Get all RDMA device list
ibvDevList, err := ibverbs.IbvGetDeviceList()
if err != nil {
return nil, err
}
netDevList, err := GetAllNetDevice()
if err != nil {
return nil, err
}
for _, d := range ibvDevList {
for _, n := range netDevList {
dResource, err := getRdmaDeviceResoure(d.Name)
if err != nil {
return nil, err
}
nResource, err := getNetDeviceResoure(n)
if err != nil {
return nil, err
}
nn, err := getRdmaDeviceNumaNode(d.Name)
if err != nil {
return nil, err
}
// the same device
if bytes.Compare(dResource, nResource) == 0 {
devs = append(devs, Device{
RdmaDevice: d,
NetDevice: n,
NumaNode: int64(nn),
})
}
}
}
return devs, nil
}
func getRdmaDeivces(masterNetDevice string) ([]Device, error) {
var devs []Device
// Get all RDMA device list
ibvDevList, err := ibverbs.IbvGetDeviceList()
if err != nil {
return nil, err
}
netDevList, err := GetVfNetDevice(masterNetDevice)
if err != nil {
return nil, err
}
for _, d := range ibvDevList {
for _, n := range netDevList {
dResource, err := getRdmaDeviceResoure(d.Name)
if err != nil {
return nil, err
}
nResource, err := getNetDeviceResoure(n)
if err != nil {
return nil, err
}
nn, err := getRdmaDeviceNumaNode(d.Name)
if err != nil {
return nil, err
}
// the same device
if bytes.Compare(dResource, nResource) == 0 {
devs = append(devs, Device{
RdmaDevice: d,
NetDevice: n,
NumaNode: int64(nn),
})
}
}
}
return devs, nil
}
func getRdmaDeviceResoure(name string) ([]byte, error) {
resourceFile := fmt.Sprintf(RdmaDeviceRource, name)
data, err := ioutil.ReadFile(resourceFile)
return data, err
}
func getNetDeviceResoure(name string) ([]byte, error) {
resourceFile := fmt.Sprintf(NetDeviceRource, name)
data, err := ioutil.ReadFile(resourceFile)
return data, err
}
func getRdmaDeviceNumaNode(name string) (int, error) {
numaNodeFile := fmt.Sprintf(RdmaDeviceNumaNode, name)
data, err := ioutil.ReadFile(numaNodeFile)
if err != nil {
return 0, err
}
return strconv.Atoi(string(data))
}
func deviceExists(devs []*pluginapi.Device, id string) bool {
for _, d := range devs {
if d.ID == id {
return true
}
}
return false
}
func watchXIDs(ctx context.Context, devs []*pluginapi.Device, xids chan<- *pluginapi.Device) {
for {
select {
case <-ctx.Done():
return
}
// TODO: check RDMA device healthy status
}
}