forked from vishvananda/netlink
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rdma_link_test.go
207 lines (177 loc) · 4.03 KB
/
rdma_link_test.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
// +build linux
package netlink
import (
"io/ioutil"
"strings"
"testing"
"github.com/vishvananda/netns"
)
func setupRdmaKModule(t *testing.T, name string) {
skipUnlessRoot(t)
file, err := ioutil.ReadFile("/proc/modules")
if err != nil {
t.Fatal("Failed to open /proc/modules", err)
}
for _, line := range strings.Split(string(file), "\n") {
n := strings.Split(line, " ")[0]
if n == name {
return
}
}
t.Skipf("Test requires kmodule %q.", name)
}
func TestRdmaGetRdmaLink(t *testing.T) {
minKernelRequired(t, 4, 16)
setupRdmaKModule(t, "ib_core")
_, err := RdmaLinkByName("foo")
if err != nil {
t.Fatal(err)
}
}
func TestRdmaSetRdmaLinkName(t *testing.T) {
minKernelRequired(t, 4, 19)
setupRdmaKModule(t, "ib_core")
link, err := RdmaLinkByName("foo")
if err != nil {
t.Fatal(err)
}
// Set new name
err = RdmaLinkSetName(link, "bar")
if err != nil {
t.Fatal(err)
}
// Revert back to old name
err = RdmaLinkSetName(link, "foo")
if err != nil {
t.Fatal(err)
}
}
func TestRdmaSystemGetNetnsMode(t *testing.T) {
minKernelRequired(t, 5, 2)
setupRdmaKModule(t, "ib_core")
mode, err := RdmaSystemGetNetnsMode()
if err != nil {
t.Fatal(err)
}
t.Log("rdma system netns mode =", mode)
}
func TestRdmaSystemSetNetnsMode(t *testing.T) {
var newMode string
var mode string
var err error
minKernelRequired(t, 5, 2)
setupRdmaKModule(t, "ib_core")
mode, err = RdmaSystemGetNetnsMode()
if err != nil {
t.Fatal(err)
}
t.Log("current rdma system mode =", mode)
err = RdmaSystemSetNetnsMode(mode)
if err != nil {
t.Fatal(err)
}
// Flip the mode from current mode
if mode == "exclusive" {
RdmaSystemSetNetnsMode("shared")
} else {
RdmaSystemSetNetnsMode("exclusive")
}
newMode, err = RdmaSystemGetNetnsMode()
if err != nil {
t.Fatal(err)
}
t.Log("new rdma system mode =", newMode)
// Change back to original mode
err = RdmaSystemSetNetnsMode(mode)
if err != nil {
t.Fatal(err)
}
}
func TestRdmaLinkSetNsFd(t *testing.T) {
minKernelRequired(t, 5, 2)
setupRdmaKModule(t, "ib_core")
mode, err := RdmaSystemGetNetnsMode()
if err != nil {
t.Fatal(err)
}
t.Log("current rdma netns mode", mode)
err = RdmaSystemSetNetnsMode("exclusive")
if err != nil {
t.Fatal(err)
}
basens, err := netns.Get()
if err != nil {
RdmaSystemSetNetnsMode(mode)
t.Fatal("Failed to get basens")
}
defer basens.Close()
newns, err := netns.New()
if err != nil {
RdmaSystemSetNetnsMode(mode)
t.Fatal("Failed to create newns")
}
netns.Set(basens)
link, err := RdmaLinkByName("foo")
if err != nil {
// Remove the namespace as RDMA subsystem requires
// no namespace to exist when changing net namespace mode
newns.Close()
RdmaSystemSetNetnsMode(mode)
t.Fatal(err)
}
t.Log("rdma link: ", link)
err = RdmaLinkSetNsFd(link, uint32(newns))
if err != nil {
newns.Close()
RdmaSystemSetNetnsMode(mode)
t.Fatal(err)
}
newns.Close()
//Set the old mode back at start of the test
err = RdmaSystemSetNetnsMode(mode)
if err != nil {
t.Fatal(err)
}
}
func TestRdmaLinkList(t *testing.T) {
minKernelRequired(t, 4, 16)
setupRdmaKModule(t, "ib_core")
links, err := RdmaLinkList()
if err != nil {
t.Fatal(err)
}
t.Log("RDMA devices:")
for _, link := range links {
t.Logf("%d: %s", link.Attrs.Index, link.Attrs.Name)
}
}
func TestRdmaLinkAddAndDel(t *testing.T) {
// related commit is https://github.com/torvalds/linux/commit/3856ec4b93c9463d36ee39098dde1fbbd29ec6dd.
minKernelRequired(t, 5, 1)
setupRdmaKModule(t, "rdma_rxe")
checkPresence := func(name string, exist bool) {
links, err := RdmaLinkList()
if err != nil {
t.Fatal(err)
}
found := false
for _, link := range links {
if link.Attrs.Name == name {
found = true
break
}
}
if found != exist {
t.Fatalf("expected rdma link %s presence=%v, but got presence=%v", name, exist, found)
}
}
linkName := t.Name()
if err := RdmaLinkAdd(linkName, "rxe", "lo"); err != nil {
t.Fatal(err)
}
checkPresence(linkName, true)
if err := RdmaLinkDel(linkName); err != nil {
t.Fatal(err)
}
checkPresence(linkName, false)
}