forked from vishvananda/netlink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
link_test.go
3546 lines (2941 loc) · 78.6 KB
/
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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//go:build linux
// +build linux
package netlink
import (
"bytes"
"errors"
"fmt"
"net"
"os"
"os/exec"
"sort"
"strings"
"syscall"
"testing"
"time"
"github.com/vishvananda/netlink/nl"
"github.com/vishvananda/netns"
"golang.org/x/sys/unix"
)
const (
testTxQLen int = 100
defaultTxQLen int = 1000
testTxQueues int = 4
testRxQueues int = 8
)
func testLinkAddDel(t *testing.T, link Link) {
_, err := LinkList()
if err != nil {
t.Fatal(err)
}
if err := LinkAdd(link); err != nil {
t.Fatal(err)
}
base := link.Attrs()
result, err := LinkByName(base.Name)
if err != nil {
t.Fatal(err)
}
rBase := result.Attrs()
if base.Index != 0 {
if base.Index != rBase.Index {
t.Fatalf("index is %d, should be %d", rBase.Index, base.Index)
}
}
if base.Group > 0 {
if base.Group != rBase.Group {
t.Fatalf("group is %d, should be %d", rBase.Group, base.Group)
}
}
if vlan, ok := link.(*Vlan); ok {
other, ok := result.(*Vlan)
if !ok {
t.Fatal("Result of create is not a vlan")
}
if vlan.VlanId != other.VlanId {
t.Fatal("Link.VlanId id doesn't match")
}
}
if resultPrimary, ok := result.(*Netkit); ok {
if inputPrimary, ok := link.(*Netkit); ok {
if resultPrimary.Policy != inputPrimary.Policy {
t.Fatalf("Policy is %d, should be %d", int(resultPrimary.Policy), int(inputPrimary.Policy))
}
if resultPrimary.PeerPolicy != inputPrimary.PeerPolicy {
t.Fatalf("Peer Policy is %d, should be %d", int(resultPrimary.PeerPolicy), int(inputPrimary.PeerPolicy))
}
if resultPrimary.Mode != inputPrimary.Mode {
t.Fatalf("Mode is %d, should be %d", int(resultPrimary.Mode), int(inputPrimary.Mode))
}
if resultPrimary.SupportsScrub() && resultPrimary.Scrub != inputPrimary.Scrub {
t.Fatalf("Scrub is %d, should be %d", int(resultPrimary.Scrub), int(inputPrimary.Scrub))
}
if resultPrimary.SupportsScrub() && resultPrimary.PeerScrub != inputPrimary.PeerScrub {
t.Fatalf("Peer Scrub is %d, should be %d", int(resultPrimary.PeerScrub), int(inputPrimary.PeerScrub))
}
if inputPrimary.peerLinkAttrs.Name != "" {
var resultPeer *Netkit
pLink, err := LinkByName(inputPrimary.peerLinkAttrs.Name)
if err != nil {
t.Fatalf("Failed to get Peer netkit %s", inputPrimary.peerLinkAttrs.Name)
}
if resultPeer, ok = pLink.(*Netkit); !ok {
t.Fatalf("Peer %s is incorrect type", inputPrimary.peerLinkAttrs.Name)
}
if resultPrimary.PeerPolicy != resultPeer.Policy {
t.Fatalf("Peer Policy from primary is %d, should be %d", int(resultPrimary.PeerPolicy), int(resultPeer.Policy))
}
if resultPeer.PeerPolicy != resultPrimary.Policy {
t.Fatalf("PeerPolicy from peer is %d, should be %d", int(resultPeer.PeerPolicy), int(resultPrimary.Policy))
}
if resultPrimary.Mode != resultPeer.Mode {
t.Fatalf("Peer Mode from primary is %d, should be %d", int(resultPrimary.Mode), int(resultPeer.Mode))
}
if resultPrimary.IsPrimary() == resultPeer.IsPrimary() {
t.Fatalf("Both primary and peer device has the same value in IsPrimary() %t", resultPrimary.IsPrimary())
}
if resultPrimary.SupportsScrub() != resultPeer.SupportsScrub() {
t.Fatalf("Peer SupportsScrub() should return %v", resultPrimary.SupportsScrub())
}
if resultPrimary.PeerScrub != resultPeer.Scrub {
t.Fatalf("Scrub from peer is %d, should be %d", int(resultPeer.Scrub), int(resultPrimary.PeerScrub))
}
if resultPrimary.Scrub != resultPeer.PeerScrub {
t.Fatalf("PeerScrub from peer is %d, should be %d", int(resultPeer.PeerScrub), int(resultPrimary.Scrub))
}
}
}
}
if veth, ok := result.(*Veth); ok {
if rBase.TxQLen != base.TxQLen {
t.Fatalf("qlen is %d, should be %d", rBase.TxQLen, base.TxQLen)
}
if rBase.NumTxQueues != base.NumTxQueues {
t.Fatalf("txQueues is %d, should be %d", rBase.NumTxQueues, base.NumTxQueues)
}
if rBase.NumRxQueues != base.NumRxQueues {
t.Fatalf("rxQueues is %d, should be %d", rBase.NumRxQueues, base.NumRxQueues)
}
if rBase.MTU != base.MTU {
t.Fatalf("MTU is %d, should be %d", rBase.MTU, base.MTU)
}
if original, ok := link.(*Veth); ok {
if original.PeerName != "" {
var peer *Veth
other, err := LinkByName(original.PeerName)
if err != nil {
t.Fatalf("Peer %s not created", veth.PeerName)
}
if peer, ok = other.(*Veth); !ok {
t.Fatalf("Peer %s is incorrect type", veth.PeerName)
}
if peer.TxQLen != testTxQLen {
t.Fatalf("TxQLen of peer is %d, should be %d", peer.TxQLen, testTxQLen)
}
if peer.NumTxQueues != testTxQueues {
t.Fatalf("NumTxQueues of peer is %d, should be %d", peer.NumTxQueues, testTxQueues)
}
if peer.NumRxQueues != testRxQueues {
t.Fatalf("NumRxQueues of peer is %d, should be %d", peer.NumRxQueues, testRxQueues)
}
if !bytes.Equal(peer.Attrs().HardwareAddr, original.PeerHardwareAddr) {
t.Fatalf("Peer MAC addr is %s, should be %s", peer.Attrs().HardwareAddr, original.PeerHardwareAddr)
}
}
}
}
if _, ok := result.(*Veth); !ok {
if _, ok := result.(*Netkit); !ok {
// recent kernels set the parent index for veths/netkit in the response
if rBase.ParentIndex == 0 && base.ParentIndex != 0 {
t.Fatalf("Created link doesn't have parent %d but it should", base.ParentIndex)
} else if rBase.ParentIndex != 0 && base.ParentIndex == 0 {
t.Fatalf("Created link has parent %d but it shouldn't", rBase.ParentIndex)
} else if rBase.ParentIndex != 0 && base.ParentIndex != 0 {
if rBase.ParentIndex != base.ParentIndex {
t.Fatalf("Link.ParentIndex doesn't match %d != %d", rBase.ParentIndex, base.ParentIndex)
}
}
}
}
if _, ok := link.(*Wireguard); ok {
_, ok := result.(*Wireguard)
if !ok {
t.Fatal("Result of create is not a wireguard")
}
}
if vxlan, ok := link.(*Vxlan); ok {
other, ok := result.(*Vxlan)
if !ok {
t.Fatal("Result of create is not a vxlan")
}
compareVxlan(t, vxlan, other)
}
if ipv, ok := link.(*IPVlan); ok {
other, ok := result.(*IPVlan)
if !ok {
t.Fatal("Result of create is not a ipvlan")
}
if ipv.Mode != other.Mode {
t.Fatalf("Got unexpected mode: %d, expected: %d", other.Mode, ipv.Mode)
}
if ipv.Flag != other.Flag {
t.Fatalf("Got unexpected flag: %d, expected: %d", other.Flag, ipv.Flag)
}
}
if macv, ok := link.(*Macvlan); ok {
other, ok := result.(*Macvlan)
if !ok {
t.Fatal("Result of create is not a macvlan")
}
if macv.Mode != other.Mode {
t.Fatalf("Got unexpected mode: %d, expected: %d", other.Mode, macv.Mode)
}
if other.BCQueueLen > 0 || other.UsedBCQueueLen > 0 {
if other.UsedBCQueueLen < other.BCQueueLen {
t.Fatalf("UsedBCQueueLen (%d) is smaller than BCQueueLen (%d)", other.UsedBCQueueLen, other.BCQueueLen)
}
}
if macv.BCQueueLen > 0 {
if macv.BCQueueLen != other.BCQueueLen {
t.Fatalf("BCQueueLen not set correctly: %d, expected: %d", other.BCQueueLen, macv.BCQueueLen)
}
}
}
if macv, ok := link.(*Macvtap); ok {
other, ok := result.(*Macvtap)
if !ok {
t.Fatal("Result of create is not a macvtap")
}
if macv.Mode != other.Mode {
t.Fatalf("Got unexpected mode: %d, expected: %d", other.Mode, macv.Mode)
}
if other.BCQueueLen > 0 || other.UsedBCQueueLen > 0 {
if other.UsedBCQueueLen < other.BCQueueLen {
t.Fatalf("UsedBCQueueLen (%d) is smaller than BCQueueLen (%d)", other.UsedBCQueueLen, other.BCQueueLen)
}
}
if macv.BCQueueLen > 0 {
if macv.BCQueueLen != other.BCQueueLen {
t.Fatalf("BCQueueLen not set correctly: %d, expected: %d", other.BCQueueLen, macv.BCQueueLen)
}
}
}
if _, ok := link.(*Vti); ok {
_, ok := result.(*Vti)
if !ok {
t.Fatal("Result of create is not a vti")
}
}
if bond, ok := link.(*Bond); ok {
other, ok := result.(*Bond)
if !ok {
t.Fatal("Result of create is not a bond")
}
if bond.Mode != other.Mode {
t.Fatalf("Got unexpected mode: %d, expected: %d", other.Mode, bond.Mode)
}
if bond.ArpIpTargets != nil {
if other.ArpIpTargets == nil {
t.Fatalf("Got unexpected ArpIpTargets: nil")
}
if len(bond.ArpIpTargets) != len(other.ArpIpTargets) {
t.Fatalf("Got unexpected ArpIpTargets len: %d, expected: %d",
len(other.ArpIpTargets), len(bond.ArpIpTargets))
}
for i := range bond.ArpIpTargets {
if !bond.ArpIpTargets[i].Equal(other.ArpIpTargets[i]) {
t.Fatalf("Got unexpected ArpIpTargets: %s, expected: %s",
other.ArpIpTargets[i], bond.ArpIpTargets[i])
}
}
}
switch mode := bondModeToString[bond.Mode]; mode {
case "802.3ad":
if bond.AdSelect != other.AdSelect {
t.Fatalf("Got unexpected AdSelect: %d, expected: %d", other.AdSelect, bond.AdSelect)
}
if bond.AdActorSysPrio != other.AdActorSysPrio {
t.Fatalf("Got unexpected AdActorSysPrio: %d, expected: %d", other.AdActorSysPrio, bond.AdActorSysPrio)
}
if bond.AdUserPortKey != other.AdUserPortKey {
t.Fatalf("Got unexpected AdUserPortKey: %d, expected: %d", other.AdUserPortKey, bond.AdUserPortKey)
}
if !bytes.Equal(bond.AdActorSystem, other.AdActorSystem) {
t.Fatalf("Got unexpected AdActorSystem: %d, expected: %d", other.AdActorSystem, bond.AdActorSystem)
}
case "balance-tlb":
if bond.TlbDynamicLb != other.TlbDynamicLb {
t.Fatalf("Got unexpected TlbDynamicLb: %d, expected: %d", other.TlbDynamicLb, bond.TlbDynamicLb)
}
}
}
if iptun, ok := link.(*Iptun); ok {
other, ok := result.(*Iptun)
if !ok {
t.Fatal("Result of create is not a iptun")
}
if iptun.FlowBased != other.FlowBased {
t.Fatal("Iptun.FlowBased doesn't match")
}
}
if ip6tnl, ok := link.(*Ip6tnl); ok {
other, ok := result.(*Ip6tnl)
if !ok {
t.Fatal("Result of create is not a ip6tnl")
}
if ip6tnl.FlowBased != other.FlowBased {
t.Fatal("Ip6tnl.FlowBased doesn't match")
}
}
if _, ok := link.(*Sittun); ok {
_, ok := result.(*Sittun)
if !ok {
t.Fatal("Result of create is not a sittun")
}
}
if geneve, ok := link.(*Geneve); ok {
other, ok := result.(*Geneve)
if !ok {
t.Fatal("Result of create is not a Geneve")
}
compareGeneve(t, geneve, other)
}
if gretap, ok := link.(*Gretap); ok {
other, ok := result.(*Gretap)
if !ok {
t.Fatal("Result of create is not a Gretap")
}
compareGretap(t, gretap, other)
}
if gretun, ok := link.(*Gretun); ok {
other, ok := result.(*Gretun)
if !ok {
t.Fatal("Result of create is not a Gretun")
}
compareGretun(t, gretun, other)
}
if xfrmi, ok := link.(*Xfrmi); ok {
other, ok := result.(*Xfrmi)
if !ok {
t.Fatal("Result of create is not a xfrmi")
}
compareXfrmi(t, xfrmi, other)
}
if tuntap, ok := link.(*Tuntap); ok {
other, ok := result.(*Tuntap)
if !ok {
t.Fatal("Result of create is not a tuntap")
}
compareTuntap(t, tuntap, other)
}
if bareudp, ok := link.(*BareUDP); ok {
other, ok := result.(*BareUDP)
if !ok {
t.Fatal("Result of create is not a BareUDP")
}
compareBareUDP(t, bareudp, other)
}
if err = LinkDel(link); err != nil {
t.Fatal(err)
}
links, err := LinkList()
if err != nil {
t.Fatal(err)
}
for _, l := range links {
if l.Attrs().Name == link.Attrs().Name {
t.Fatal("Link not removed properly")
}
}
}
func compareGeneve(t *testing.T, expected, actual *Geneve) {
if actual.ID != expected.ID {
t.Fatalf("Geneve.ID doesn't match: %d %d", actual.ID, expected.ID)
}
// set the Dport to 6081 (the linux default) if it wasn't specified at creation
if expected.Dport == 0 {
expected.Dport = 6081
}
if actual.Dport != expected.Dport {
t.Fatal("Geneve.Dport doesn't match")
}
if actual.Ttl != expected.Ttl {
t.Fatal("Geneve.Ttl doesn't match")
}
if actual.Tos != expected.Tos {
t.Fatal("Geneve.Tos doesn't match")
}
if !actual.Remote.Equal(expected.Remote) {
t.Fatalf("Geneve.Remote is not equal: %s!=%s", actual.Remote, expected.Remote)
}
if actual.FlowBased != expected.FlowBased {
t.Fatal("Geneve.FlowBased doesn't match")
}
if actual.InnerProtoInherit != expected.InnerProtoInherit {
t.Fatal("Geneve.InnerProtoInherit doesn't match")
}
// TODO: we should implement the rest of the geneve methods
}
func compareGretap(t *testing.T, expected, actual *Gretap) {
if actual.IKey != expected.IKey {
t.Fatal("Gretap.IKey doesn't match")
}
if actual.OKey != expected.OKey {
t.Fatal("Gretap.OKey doesn't match")
}
if actual.EncapSport != expected.EncapSport {
t.Fatal("Gretap.EncapSport doesn't match")
}
if actual.EncapDport != expected.EncapDport {
t.Fatal("Gretap.EncapDport doesn't match")
}
if expected.Local != nil && !actual.Local.Equal(expected.Local) {
t.Fatal("Gretap.Local doesn't match")
}
if expected.Remote != nil && !actual.Remote.Equal(expected.Remote) {
t.Fatal("Gretap.Remote doesn't match")
}
if actual.IFlags != expected.IFlags {
t.Fatal("Gretap.IFlags doesn't match")
}
if actual.OFlags != expected.OFlags {
t.Fatal("Gretap.OFlags doesn't match")
}
if actual.PMtuDisc != expected.PMtuDisc {
t.Fatal("Gretap.PMtuDisc doesn't match")
}
if actual.Ttl != expected.Ttl {
t.Fatal("Gretap.Ttl doesn't match")
}
if actual.Tos != expected.Tos {
t.Fatal("Gretap.Tos doesn't match")
}
if actual.EncapType != expected.EncapType {
t.Fatal("Gretap.EncapType doesn't match")
}
if actual.EncapFlags != expected.EncapFlags {
t.Fatal("Gretap.EncapFlags doesn't match")
}
if actual.Link != expected.Link {
t.Fatal("Gretap.Link doesn't match")
}
if actual.FlowBased != expected.FlowBased {
t.Fatal("Gretap.FlowBased doesn't match")
}
}
func compareGretun(t *testing.T, expected, actual *Gretun) {
if actual.Link != expected.Link {
t.Fatal("Gretun.Link doesn't match")
}
if actual.IFlags != expected.IFlags {
t.Fatal("Gretun.IFlags doesn't match")
}
if actual.OFlags != expected.OFlags {
t.Fatal("Gretun.OFlags doesn't match")
}
if actual.IKey != expected.IKey {
t.Fatal("Gretun.IKey doesn't match")
}
if actual.OKey != expected.OKey {
t.Fatal("Gretun.OKey doesn't match")
}
if expected.Local != nil && !actual.Local.Equal(expected.Local) {
t.Fatal("Gretun.Local doesn't match")
}
if expected.Remote != nil && !actual.Remote.Equal(expected.Remote) {
t.Fatal("Gretun.Remote doesn't match")
}
if actual.Ttl != expected.Ttl {
t.Fatal("Gretun.Ttl doesn't match")
}
if actual.Tos != expected.Tos {
t.Fatal("Gretun.Tos doesn't match")
}
if actual.PMtuDisc != expected.PMtuDisc {
t.Fatal("Gretun.PMtuDisc doesn't match")
}
if actual.EncapType != expected.EncapType {
t.Fatal("Gretun.EncapType doesn't match")
}
if actual.EncapFlags != expected.EncapFlags {
t.Fatal("Gretun.EncapFlags doesn't match")
}
if actual.EncapSport != expected.EncapSport {
t.Fatal("Gretun.EncapSport doesn't match")
}
if actual.EncapDport != expected.EncapDport {
t.Fatal("Gretun.EncapDport doesn't match")
}
if actual.FlowBased != expected.FlowBased {
t.Fatal("Gretun.FlowBased doesn't match")
}
}
func compareVxlan(t *testing.T, expected, actual *Vxlan) {
if actual.VxlanId != expected.VxlanId {
t.Fatal("Vxlan.VxlanId doesn't match")
}
if expected.SrcAddr != nil && !actual.SrcAddr.Equal(expected.SrcAddr) {
t.Fatal("Vxlan.SrcAddr doesn't match")
}
if expected.Group != nil && !actual.Group.Equal(expected.Group) {
t.Fatal("Vxlan.Group doesn't match")
}
if expected.TTL != -1 && actual.TTL != expected.TTL {
t.Fatal("Vxlan.TTL doesn't match")
}
if expected.TOS != -1 && actual.TOS != expected.TOS {
t.Fatal("Vxlan.TOS doesn't match")
}
if actual.Learning != expected.Learning {
t.Fatal("Vxlan.Learning doesn't match")
}
if actual.Proxy != expected.Proxy {
t.Fatal("Vxlan.Proxy doesn't match")
}
if actual.RSC != expected.RSC {
t.Fatal("Vxlan.RSC doesn't match")
}
if actual.L2miss != expected.L2miss {
t.Fatal("Vxlan.L2miss doesn't match")
}
if actual.L3miss != expected.L3miss {
t.Fatal("Vxlan.L3miss doesn't match")
}
if actual.GBP != expected.GBP {
t.Fatal("Vxlan.GBP doesn't match")
}
if actual.FlowBased != expected.FlowBased {
t.Fatal("Vxlan.FlowBased doesn't match")
}
if actual.UDP6ZeroCSumTx != expected.UDP6ZeroCSumTx {
t.Fatal("Vxlan.UDP6ZeroCSumTx doesn't match")
}
if actual.UDP6ZeroCSumRx != expected.UDP6ZeroCSumRx {
t.Fatal("Vxlan.UDP6ZeroCSumRx doesn't match")
}
if expected.NoAge {
if !actual.NoAge {
t.Fatal("Vxlan.NoAge doesn't match")
}
} else if expected.Age > 0 && actual.Age != expected.Age {
t.Fatal("Vxlan.Age doesn't match")
}
if expected.Limit > 0 && actual.Limit != expected.Limit {
t.Fatal("Vxlan.Limit doesn't match")
}
if expected.Port > 0 && actual.Port != expected.Port {
t.Fatal("Vxlan.Port doesn't match")
}
if expected.PortLow > 0 || expected.PortHigh > 0 {
if actual.PortLow != expected.PortLow {
t.Fatal("Vxlan.PortLow doesn't match")
}
if actual.PortHigh != expected.PortHigh {
t.Fatal("Vxlan.PortHigh doesn't match")
}
}
}
func compareXfrmi(t *testing.T, expected, actual *Xfrmi) {
if expected.Ifid != actual.Ifid {
t.Fatal("Xfrmi.Ifid doesn't match")
}
}
func compareTuntap(t *testing.T, expected, actual *Tuntap) {
if expected.Mode != actual.Mode {
t.Fatalf("Tuntap.Mode doesn't match: expected : %+v, got %+v", expected.Mode, actual.Mode)
}
if expected.Owner != actual.Owner {
t.Fatal("Tuntap.Owner doesn't match")
}
if expected.Group != actual.Group {
t.Fatal("Tuntap.Group doesn't match")
}
if expected.NonPersist != actual.NonPersist {
t.Fatal("Tuntap.Group doesn't match")
}
}
func compareBareUDP(t *testing.T, expected, actual *BareUDP) {
// set the Port to 6635 (the linux default) if it wasn't specified at creation
if expected.Port == 0 {
expected.Port = 6635
}
if actual.Port != expected.Port {
t.Fatalf("BareUDP.Port doesn't match: %d %d", actual.Port, expected.Port)
}
if actual.EtherType != expected.EtherType {
t.Fatalf("BareUDP.EtherType doesn't match: %x %x", actual.EtherType, expected.EtherType)
}
if actual.SrcPortMin != expected.SrcPortMin {
t.Fatalf("BareUDP.SrcPortMin doesn't match: %d %d", actual.SrcPortMin, expected.SrcPortMin)
}
if actual.MultiProto != expected.MultiProto {
t.Fatal("BareUDP.MultiProto doesn't match")
}
}
func TestLinkAddDelWithIndex(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Dummy{LinkAttrs{Index: 1000, Name: "foo"}})
}
func TestLinkAddDelDummy(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Dummy{LinkAttrs{Name: "foo"}})
}
func TestLinkAddDelDummyWithGroup(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Dummy{LinkAttrs{Name: "foo", Group: 42}})
}
func TestLinkModify(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
linkName := "foo"
originalMTU := 1500
updatedMTU := 1442
link := &Dummy{LinkAttrs{Name: linkName, MTU: originalMTU}}
base := link.Attrs()
if err := LinkAdd(link); err != nil {
t.Fatal(err)
}
link.MTU = updatedMTU
if err := LinkModify(link); err != nil {
t.Fatal(err)
}
result, err := LinkByName(linkName)
if err != nil {
t.Fatal(err)
}
rBase := result.Attrs()
if rBase.MTU != updatedMTU {
t.Fatalf("MTU is %d, should be %d", rBase.MTU, base.MTU)
}
}
func TestLinkAddDelIfb(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Ifb{LinkAttrs{Name: "foo"}})
}
func TestLinkAddDelBridge(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Bridge{LinkAttrs: LinkAttrs{Name: "foo", MTU: 1400}})
}
func TestLinkAddDelGeneve(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Geneve{
LinkAttrs: LinkAttrs{Name: "foo4", EncapType: "geneve"},
ID: 0x1000,
Remote: net.IPv4(127, 0, 0, 1)})
testLinkAddDel(t, &Geneve{
LinkAttrs: LinkAttrs{Name: "foo6", EncapType: "geneve"},
ID: 0x1000,
Remote: net.ParseIP("2001:db8:ef33::2")})
}
func TestLinkAddDelGeneveFlowBased(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Geneve{
LinkAttrs: LinkAttrs{Name: "foo"},
Dport: 1234,
FlowBased: true})
}
func TestGeneveCompareToIP(t *testing.T) {
ns, tearDown := setUpNamedNetlinkTest(t)
defer tearDown()
expected := &Geneve{
ID: 0x764332, // 23 bits
Remote: net.ParseIP("1.2.3.4"),
Dport: 6081,
}
// Create interface
cmd := exec.Command("ip", "netns", "exec", ns,
"ip", "link", "add", "gen0",
"type", "geneve",
"vni", fmt.Sprint(expected.ID),
"remote", expected.Remote.String(),
// TODO: unit tests are currently done on ubuntu 16, and the version of iproute2 there doesn't support dstport
// We can still do most of the testing by verifying that we do read the default port
// "dstport", fmt.Sprint(expected.Dport),
)
out := &bytes.Buffer{}
cmd.Stdout = out
cmd.Stderr = out
if rc := cmd.Run(); rc != nil {
t.Fatal("failed creating link:", rc, out.String())
}
link, err := LinkByName("gen0")
if err != nil {
t.Fatal("Failed getting link: ", err)
}
actual, ok := link.(*Geneve)
if !ok {
t.Fatalf("resulted interface is not geneve: %T", link)
}
compareGeneve(t, expected, actual)
}
func TestLinkAddDelGretap(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Gretap{
LinkAttrs: LinkAttrs{Name: "foo4"},
IKey: 0x101,
OKey: 0x101,
PMtuDisc: 1,
Local: net.IPv4(127, 0, 0, 1),
Remote: net.IPv4(127, 0, 0, 1)})
testLinkAddDel(t, &Gretap{
LinkAttrs: LinkAttrs{Name: "foo6"},
IKey: 0x101,
OKey: 0x101,
Local: net.ParseIP("2001:db8:abcd::1"),
Remote: net.ParseIP("2001:db8:ef33::2")})
}
func TestLinkAddDelGretun(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Gretun{
LinkAttrs: LinkAttrs{Name: "foo4"},
Local: net.IPv4(127, 0, 0, 1),
Remote: net.IPv4(127, 0, 0, 1)})
testLinkAddDel(t, &Gretun{
LinkAttrs: LinkAttrs{Name: "foo6"},
Local: net.ParseIP("2001:db8:abcd::1"),
Remote: net.ParseIP("2001:db8:ef33::2")})
}
func TestLinkAddDelGretunPointToMultiPoint(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Gretun{
LinkAttrs: LinkAttrs{Name: "foo"},
Local: net.IPv4(127, 0, 0, 1),
IKey: 1234,
OKey: 1234})
testLinkAddDel(t, &Gretun{
LinkAttrs: LinkAttrs{Name: "foo6"},
Local: net.ParseIP("2001:db8:1234::4"),
IKey: 5678,
OKey: 7890})
}
func TestLinkAddDelGretunFlowBased(t *testing.T) {
minKernelRequired(t, 4, 3)
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Gretun{
LinkAttrs: LinkAttrs{Name: "foo"},
FlowBased: true})
}
func TestLinkAddDelGretapFlowBased(t *testing.T) {
minKernelRequired(t, 4, 3)
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Gretap{
LinkAttrs: LinkAttrs{Name: "foo"},
FlowBased: true})
}
func TestLinkAddDelVlan(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
parent := &Dummy{LinkAttrs{Name: "foo"}}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}
testLinkAddDel(t, &Vlan{LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index}, 900, VLAN_PROTOCOL_8021Q})
if err := LinkDel(parent); err != nil {
t.Fatal(err)
}
}
func TestLinkAddDelMacvlan(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
parent := &Dummy{LinkAttrs{Name: "foo"}}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}
testLinkAddDel(t, &Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_PRIVATE,
})
testLinkAddDel(t, &Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_BRIDGE,
})
testLinkAddDel(t, &Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_VEPA,
})
if err := LinkDel(parent); err != nil {
t.Fatal(err)
}
}
func TestLinkAddDelMacvtap(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
parent := &Dummy{LinkAttrs{Name: "foo"}}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}
testLinkAddDel(t, &Macvtap{
Macvlan: Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_PRIVATE,
},
})
testLinkAddDel(t, &Macvtap{
Macvlan: Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_BRIDGE,
},
})
testLinkAddDel(t, &Macvtap{
Macvlan: Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_VEPA,
},
})
if err := LinkDel(parent); err != nil {
t.Fatal(err)
}
}
func TestLinkMacvBCQueueLen(t *testing.T) {
minKernelRequired(t, 5, 11)
tearDown := setUpNetlinkTest(t)
defer tearDown()
parent := &Dummy{LinkAttrs{Name: "foo"}}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}
testLinkAddDel(t, &Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_PRIVATE,
BCQueueLen: 10000,
})
testLinkAddDel(t, &Macvtap{
Macvlan: Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_PRIVATE,
BCQueueLen: 10000,
},
})
if err := LinkDel(parent); err != nil {
t.Fatal(err)
}
}
func TestNetkitPeerNs(t *testing.T) {
minKernelRequired(t, 6, 7)
tearDown := setUpNetlinkTest(t)
defer tearDown()
basens, err := netns.Get()
if err != nil {
t.Fatal("Failed to get basens")
}
defer basens.Close()
nsOne, err := netns.New()
if err != nil {
t.Fatal("Failed to create nsOne")
}
defer nsOne.Close()