-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathlink_live_test.go
More file actions
401 lines (344 loc) · 9.07 KB
/
link_live_test.go
File metadata and controls
401 lines (344 loc) · 9.07 KB
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
//go:build integration
// +build integration
package rtnetlink
import (
"testing"
"github.com/cilium/ebpf"
"github.com/cilium/ebpf/asm"
"github.com/cilium/ebpf/rlimit"
"github.com/jsimonetti/rtnetlink/v2/internal/testutils"
"github.com/jsimonetti/rtnetlink/v2/internal/unix"
"github.com/mdlayher/netlink"
)
// lo accesses the loopback interface present in every network namespace.
var lo uint32 = 1
func xdpPrograms(tb testing.TB) (int32, int32) {
tb.Helper()
// Load XDP_PASS into the return value register.
bpfProgram := &ebpf.ProgramSpec{
Type: ebpf.XDP,
Instructions: asm.Instructions{
asm.LoadImm(asm.R0, int64(2), asm.DWord),
asm.Return(),
},
License: "MIT",
}
prog1, err := ebpf.NewProgram(bpfProgram)
if err != nil {
tb.Fatal(err)
}
prog2, err := ebpf.NewProgram(bpfProgram)
if err != nil {
tb.Fatal(err)
}
tb.Cleanup(func() {
prog1.Close()
prog2.Close()
})
// Use the file descriptor of the programs
return int32(prog1.FD()), int32(prog2.FD())
}
func attachXDP(tb testing.TB, conn *Conn, ifIndex uint32, xdp *LinkXDP) {
tb.Helper()
message := LinkMessage{
Family: unix.AF_UNSPEC,
Index: ifIndex,
Attributes: &LinkAttributes{
XDP: xdp,
},
}
if err := conn.Link.Set(&message); err != nil {
tb.Fatalf("attaching program with fd %d to link at ifindex %d: %s", xdp.FD, ifIndex, err)
}
}
// getXDP returns the XDP attach, XDP prog ID and errors when the
// interface could not be fetched
func getXDP(tb testing.TB, conn *Conn, ifIndex uint32) (uint8, uint32) {
tb.Helper()
interf, err := conn.Link.Get(ifIndex)
if err != nil {
tb.Fatalf("getting link xdp properties: %s", err)
}
return interf.Attributes.XDP.Attached, interf.Attributes.XDP.ProgID
}
func TestLinkXDPAttach(t *testing.T) {
if err := rlimit.RemoveMemlock(); err != nil {
t.Fatal(err)
}
conn, err := Dial(&netlink.Config{NetNS: testutils.NetNS(t)})
if err != nil {
t.Fatalf("failed to establish netlink socket: %v", err)
}
defer conn.Close()
progFD1, progFD2 := xdpPrograms(t)
tests := []struct {
name string
xdp *LinkXDP
}{
{
name: "with FD, no expected FD",
xdp: &LinkXDP{
FD: progFD1,
Flags: unix.XDP_FLAGS_SKB_MODE,
},
},
{
name: "with FD, expected FD == FD",
xdp: &LinkXDP{
FD: progFD1,
ExpectedFD: progFD1,
Flags: unix.XDP_FLAGS_SKB_MODE,
},
},
{
name: "with FD, expected FD != FD",
xdp: &LinkXDP{
FD: progFD1,
ExpectedFD: progFD2,
Flags: unix.XDP_FLAGS_SKB_MODE,
},
},
{
name: "with FD, expected FD < 0",
xdp: &LinkXDP{
FD: progFD1,
ExpectedFD: -1,
Flags: unix.XDP_FLAGS_SKB_MODE,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
attachXDP(t, conn, lo, tt.xdp)
attached, progID := getXDP(t, conn, lo)
if attached != unix.XDP_FLAGS_SKB_MODE {
t.Fatalf("XDP attached state does not match. Got: %d, wanted: %d", attached, unix.XDP_FLAGS_SKB_MODE)
}
if attached == unix.XDP_FLAGS_SKB_MODE && progID == 0 {
t.Fatalf("XDP program should be attached but program ID is 0")
}
})
}
}
func TestLinkXDPClear(t *testing.T) {
if err := rlimit.RemoveMemlock(); err != nil {
t.Fatal(err)
}
conn, err := Dial(&netlink.Config{NetNS: testutils.NetNS(t)})
if err != nil {
t.Fatalf("failed to establish netlink socket: %v", err)
}
defer conn.Close()
progFD1, _ := xdpPrograms(t)
attachXDP(t, conn, lo, &LinkXDP{
FD: progFD1,
Flags: unix.XDP_FLAGS_SKB_MODE,
})
// clear the BPF program from the link
attachXDP(t, conn, lo, &LinkXDP{
FD: -1,
Flags: unix.XDP_FLAGS_SKB_MODE,
})
attached, progID := getXDP(t, conn, lo)
if progID != 0 {
t.Fatalf("there is still a program loaded, while we cleared the link")
}
if attached != 0 {
t.Fatalf(
"XDP attached state does not match. Got: %d, wanted: %d\nThere should be no program loaded",
attached, 0,
)
}
}
func TestLinkXDPReplace(t *testing.T) {
// As of kernel version 5.7, the use of EXPECTED_FD and XDP_FLAGS_REPLACE
// is supported. We check here if the test host kernel fills this
// requirement. If the requirement is not met, we skip this test and
// output a notice. Running the code on a kernel version lower then 5.7
// will throw an "invalid argument" error.
// source kernel 5.6:
// https://elixir.bootlin.com/linux/v5.6/source/net/core/dev.c#L8662
// source kernel 5.7:
// https://elixir.bootlin.com/linux/v5.7/source/net/core/dev.c#L8674
testutils.SkipOnOldKernel(t, "5.7", "XDP_FLAGS_REPLACE")
if err := rlimit.RemoveMemlock(); err != nil {
t.Fatal(err)
}
conn, err := Dial(&netlink.Config{NetNS: testutils.NetNS(t)})
if err != nil {
t.Fatalf("failed to establish netlink socket: %v", err)
}
defer conn.Close()
progFD1, progFD2 := xdpPrograms(t)
attachXDP(t, conn, lo, &LinkXDP{
FD: progFD1,
Flags: unix.XDP_FLAGS_SKB_MODE,
})
_, progID1 := getXDP(t, conn, lo)
if err := conn.Link.Set(&LinkMessage{
Family: unix.AF_UNSPEC,
Index: lo,
Attributes: &LinkAttributes{
XDP: &LinkXDP{
FD: progFD2,
ExpectedFD: progFD2,
Flags: unix.XDP_FLAGS_SKB_MODE | unix.XDP_FLAGS_REPLACE,
},
},
}); err == nil {
t.Fatalf("replaced XDP program while expected FD did not match: %v", err)
}
_, progID2 := getXDP(t, conn, lo)
if progID2 != progID1 {
t.Fatal("XDP prog ID does not match previous program ID, which it should")
}
attachXDP(t, conn, lo, &LinkXDP{
FD: progFD2,
ExpectedFD: progFD1,
Flags: unix.XDP_FLAGS_SKB_MODE | unix.XDP_FLAGS_REPLACE,
})
_, progID2 = getXDP(t, conn, lo)
if progID2 == progID1 {
t.Fatal("XDP prog ID does match previous program ID, which it shouldn't")
}
}
func TestLinkListByKind(t *testing.T) {
if err := rlimit.RemoveMemlock(); err != nil {
t.Fatal(err)
}
conn, err := Dial(&netlink.Config{NetNS: testutils.NetNS(t)})
if err != nil {
t.Fatalf("failed to establish netlink socket: %v", err)
}
defer conn.Close()
links, err := conn.Link.ListByKind("SomeImpossibleLinkKind")
if err != nil {
t.Fatalf("LinkListByKind() finished with error: %v", err)
}
if len(links) > 0 {
t.Fatalf("LinkListByKind() found %d links with impossible kind", len(links))
}
}
func TestLinkSetMaster(t *testing.T) {
ns := testutils.NetNS(t)
conn, err := Dial(&netlink.Config{NetNS: ns})
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
defer conn.Close()
const (
bridgeIndex = 2001
vethIndex = 2002
)
// Create a bridge
err = conn.Link.New(&LinkMessage{
Index: bridgeIndex,
Attributes: &LinkAttributes{
Name: "testbr0",
Info: &LinkInfo{
Kind: "bridge",
},
},
})
if err != nil {
t.Fatalf("failed to create bridge: %v", err)
}
defer conn.Link.Delete(bridgeIndex)
// Create a dummy interface
err = conn.Link.New(&LinkMessage{
Index: vethIndex,
Attributes: &LinkAttributes{
Name: "testdum0",
Info: &LinkInfo{
Kind: "dummy",
},
},
})
if err != nil {
t.Fatalf("failed to create dummy interface: %v", err)
}
defer conn.Link.Delete(vethIndex)
// Enslave the dummy to the bridge
err = conn.Link.SetMaster(vethIndex, bridgeIndex, nil)
if err != nil {
t.Fatalf("failed to set master: %v", err)
}
// Verify it's enslaved
got, err := conn.Link.Get(vethIndex)
if err != nil {
t.Fatalf("failed to get link: %v", err)
}
if got.Attributes.Master == nil {
t.Fatal("expected Master to be set, got nil")
}
if *got.Attributes.Master != bridgeIndex {
t.Fatalf("unexpected master index:\n got: %d\nwant: %d", *got.Attributes.Master, bridgeIndex)
}
}
func TestLinkRemoveMaster(t *testing.T) {
ns := testutils.NetNS(t)
conn, err := Dial(&netlink.Config{NetNS: ns})
if err != nil {
t.Fatalf("failed to dial: %v", err)
}
defer conn.Close()
const (
bridgeIndex = 2101
dummyIndex = 2102
)
// Create a bridge
err = conn.Link.New(&LinkMessage{
Index: bridgeIndex,
Attributes: &LinkAttributes{
Name: "testbr1",
Info: &LinkInfo{
Kind: "bridge",
},
},
})
if err != nil {
t.Fatalf("failed to create bridge: %v", err)
}
defer conn.Link.Delete(bridgeIndex)
// Create a dummy interface
err = conn.Link.New(&LinkMessage{
Index: dummyIndex,
Attributes: &LinkAttributes{
Name: "testdum1",
Info: &LinkInfo{
Kind: "dummy",
},
},
})
if err != nil {
t.Fatalf("failed to create dummy interface: %v", err)
}
defer conn.Link.Delete(dummyIndex)
// Enslave it to the bridge
err = conn.Link.SetMaster(dummyIndex, bridgeIndex, nil)
if err != nil {
t.Fatalf("failed to set master: %v", err)
}
// Verify it's enslaved
got, err := conn.Link.Get(dummyIndex)
if err != nil {
t.Fatalf("failed to get link: %v", err)
}
if got.Attributes.Master == nil || *got.Attributes.Master != bridgeIndex {
t.Fatal("interface was not enslaved")
}
// Remove from master
err = conn.Link.RemoveMaster(dummyIndex)
if err != nil {
t.Fatalf("failed to remove master: %v", err)
}
// Verify it's un-enslaved
got, err = conn.Link.Get(dummyIndex)
if err != nil {
t.Fatalf("failed to get link: %v", err)
}
// Master should be nil or 0
if got.Attributes.Master != nil && *got.Attributes.Master != 0 {
t.Fatalf("expected Master to be 0 or nil, got %d", *got.Attributes.Master)
}
}