-
Notifications
You must be signed in to change notification settings - Fork 162
/
dataplane_test.go
1671 lines (1561 loc) · 52.7 KB
/
dataplane_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
// Copyright 2020 Anapaya Systems
// Copyright 2023 ETH Zurich
//
// 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 router_test
import (
"bytes"
"context"
"fmt"
"net"
"net/netip"
"sync"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/google/gopacket"
"github.com/google/gopacket/layers"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/net/ipv4"
"github.com/scionproto/scion/pkg/addr"
libepic "github.com/scionproto/scion/pkg/experimental/epic"
"github.com/scionproto/scion/pkg/private/util"
"github.com/scionproto/scion/pkg/private/xtest"
"github.com/scionproto/scion/pkg/scrypto"
"github.com/scionproto/scion/pkg/slayers"
"github.com/scionproto/scion/pkg/slayers/path"
"github.com/scionproto/scion/pkg/slayers/path/empty"
"github.com/scionproto/scion/pkg/slayers/path/epic"
"github.com/scionproto/scion/pkg/slayers/path/onehop"
"github.com/scionproto/scion/pkg/slayers/path/scion"
"github.com/scionproto/scion/private/topology"
underlayconn "github.com/scionproto/scion/private/underlay/conn"
"github.com/scionproto/scion/router"
"github.com/scionproto/scion/router/control"
"github.com/scionproto/scion/router/mock_router"
)
var metrics = router.GetMetrics()
func TestDataPlaneAddInternalInterface(t *testing.T) {
internalIP := net.ParseIP("198.51.100.1")
t.Run("fails after serve", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
d := &router.DataPlane{}
d.FakeStart()
assert.Error(t, d.AddInternalInterface(mock_router.NewMockBatchConn(ctrl), internalIP))
})
t.Run("setting nil value is not allowed", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
d := &router.DataPlane{}
assert.Error(t, d.AddInternalInterface(nil, nil))
})
t.Run("single set works", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
d := &router.DataPlane{}
assert.NoError(t, d.AddInternalInterface(mock_router.NewMockBatchConn(ctrl), internalIP))
})
t.Run("double set fails", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
d := &router.DataPlane{}
assert.NoError(t, d.AddInternalInterface(mock_router.NewMockBatchConn(ctrl), internalIP))
assert.Error(t, d.AddInternalInterface(mock_router.NewMockBatchConn(ctrl), internalIP))
})
}
func TestDataPlaneSetKey(t *testing.T) {
t.Run("fails after serve", func(t *testing.T) {
d := &router.DataPlane{}
d.FakeStart()
assert.Error(t, d.SetKey([]byte("dummy")))
})
t.Run("setting nil value is not allowed", func(t *testing.T) {
d := &router.DataPlane{}
d.FakeStart()
assert.Error(t, d.SetKey(nil))
})
t.Run("single set works", func(t *testing.T) {
d := &router.DataPlane{}
assert.NoError(t, d.SetKey([]byte("dummy key xxxxxx")))
})
t.Run("double set fails", func(t *testing.T) {
d := &router.DataPlane{}
assert.NoError(t, d.SetKey([]byte("dummy key xxxxxx")))
assert.Error(t, d.SetKey([]byte("dummy key xxxxxx")))
})
}
func TestDataPlaneAddExternalInterface(t *testing.T) {
t.Run("fails after serve", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
d := &router.DataPlane{}
d.FakeStart()
assert.Error(t, d.AddExternalInterface(42, mock_router.NewMockBatchConn(ctrl)))
})
t.Run("setting nil value is not allowed", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
d := &router.DataPlane{}
assert.Error(t, d.AddExternalInterface(42, nil))
})
t.Run("normal add works", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
d := &router.DataPlane{}
assert.NoError(t, d.AddExternalInterface(42, mock_router.NewMockBatchConn(ctrl)))
assert.NoError(t, d.AddExternalInterface(45, mock_router.NewMockBatchConn(ctrl)))
})
t.Run("overwrite fails", func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
d := &router.DataPlane{}
assert.NoError(t, d.AddExternalInterface(42, mock_router.NewMockBatchConn(ctrl)))
assert.Error(t, d.AddExternalInterface(42, mock_router.NewMockBatchConn(ctrl)))
})
}
func TestDataPlaneAddSVC(t *testing.T) {
t.Run("succeeds after serve", func(t *testing.T) {
d := &router.DataPlane{}
d.FakeStart()
assert.NoError(t, d.AddSvc(addr.SvcCS, &net.UDPAddr{}))
})
t.Run("adding nil value is not allowed", func(t *testing.T) {
d := &router.DataPlane{}
assert.Error(t, d.AddSvc(addr.SvcCS, nil))
})
t.Run("normal set works", func(t *testing.T) {
d := &router.DataPlane{}
assert.NoError(t, d.AddSvc(addr.SvcCS, &net.UDPAddr{}))
assert.NoError(t, d.AddSvc(addr.SvcDS, &net.UDPAddr{}))
})
t.Run("set multiple times works", func(t *testing.T) {
d := &router.DataPlane{}
assert.NoError(t, d.AddSvc(addr.SvcCS, &net.UDPAddr{}))
assert.NoError(t, d.AddSvc(addr.SvcCS, &net.UDPAddr{}))
})
}
func TestDataPlaneAddNextHop(t *testing.T) {
t.Run("fails after serve", func(t *testing.T) {
d := &router.DataPlane{}
d.FakeStart()
assert.Error(t, d.AddNextHop(45, &net.UDPAddr{}))
})
t.Run("setting nil value is not allowed", func(t *testing.T) {
d := &router.DataPlane{}
assert.Error(t, d.AddNextHop(45, nil))
})
t.Run("normal add works", func(t *testing.T) {
d := &router.DataPlane{}
assert.NoError(t, d.AddNextHop(45, &net.UDPAddr{}))
assert.NoError(t, d.AddNextHop(43, &net.UDPAddr{}))
})
t.Run("overwrite fails", func(t *testing.T) {
d := &router.DataPlane{}
assert.NoError(t, d.AddNextHop(45, &net.UDPAddr{}))
assert.Error(t, d.AddNextHop(45, &net.UDPAddr{}))
})
}
func TestDataPlaneRun(t *testing.T) {
t.Parallel()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
testCases := map[string]struct {
prepareDP func(*gomock.Controller, chan<- struct{}) *router.DataPlane
}{
"route 10 msg from external to internal": {
prepareDP: func(ctrl *gomock.Controller, done chan<- struct{}) *router.DataPlane {
ret := &router.DataPlane{Metrics: metrics}
key := []byte("testkey_xxxxxxxx")
local := xtest.MustParseIA("1-ff00:0:110")
totalCount := 10
mInternal := mock_router.NewMockBatchConn(ctrl)
mInternal.EXPECT().ReadBatch(gomock.Any()).Return(0, nil).AnyTimes()
mInternal.EXPECT().WriteBatch(gomock.Any(), 0).DoAndReturn(
func(ms underlayconn.Messages, flags int) (int, error) {
if totalCount == 0 {
t.Fail()
return 0, nil
}
for _, msg := range ms {
want := bytes.Repeat([]byte("actualpayloadbytes"), 10-totalCount)
if len(msg.Buffers[0]) != len(want)+84 {
return 1, nil
}
totalCount--
if totalCount == 0 {
done <- struct{}{}
}
}
return len(ms), nil
}).AnyTimes()
_ = ret.AddInternalInterface(mInternal, net.IP{})
mExternal := mock_router.NewMockBatchConn(ctrl)
mExternal.EXPECT().ReadBatch(gomock.Any()).DoAndReturn(
func(m underlayconn.Messages) (int, error) {
// 10 scion messages to external
for i := 0; i < totalCount; i++ {
spkt, dpath := prepBaseMsg(time.Now())
spkt.DstIA = local
dpath.HopFields = []path.HopField{
{ConsIngress: 41, ConsEgress: 40},
{ConsIngress: 31, ConsEgress: 30},
{ConsIngress: 1, ConsEgress: 0},
}
dpath.Base.PathMeta.CurrHF = 2
dpath.HopFields[2].Mac = computeMAC(t, key,
dpath.InfoFields[0], dpath.HopFields[2])
spkt.Path = dpath
payload := bytes.Repeat([]byte("actualpayloadbytes"), i)
buffer := gopacket.NewSerializeBuffer()
err := gopacket.SerializeLayers(buffer,
gopacket.SerializeOptions{FixLengths: true},
spkt, gopacket.Payload(payload))
require.NoError(t, err)
raw := buffer.Bytes()
copy(m[i].Buffers[0], raw)
m[i].N = len(raw)
m[i].Addr = &net.UDPAddr{IP: net.IP{10, 0, 200, 200}}
}
return 10, nil
},
).Times(1)
mExternal.EXPECT().ReadBatch(gomock.Any()).Return(0, nil).AnyTimes()
mExternal.EXPECT().WriteTo(gomock.Any(), gomock.Any()).Return(0, nil).AnyTimes()
_ = ret.AddExternalInterface(1, mExternal)
_ = ret.SetIA(local)
_ = ret.SetKey(key)
return ret
},
},
"bfd bootstrap internal session": {
prepareDP: func(ctrl *gomock.Controller, done chan<- struct{}) *router.DataPlane {
ret := &router.DataPlane{Metrics: metrics}
postInternalBFD := func(id layers.BFDDiscriminator, src *net.UDPAddr) []byte {
scn := &slayers.SCION{
NextHdr: slayers.L4BFD,
PathType: empty.PathType,
Path: &empty.Path{},
}
bfdL := &layers.BFD{
Version: 1,
DetectMultiplier: layers.BFDDetectMultiplier(2),
MyDiscriminator: id,
YourDiscriminator: 0,
}
srcIP, _ := netip.AddrFromSlice(src.IP)
_ = scn.SetSrcAddr(addr.HostIP(srcIP))
buffer := gopacket.NewSerializeBuffer()
_ = gopacket.SerializeLayers(buffer,
gopacket.SerializeOptions{FixLengths: true}, scn, bfdL)
return buffer.Bytes()
}
mtx := sync.Mutex{}
expectRemoteDiscriminators := map[layers.BFDDiscriminator]struct{}{}
routers := map[net.Addr][]uint16{
&net.UDPAddr{IP: net.ParseIP("10.0.200.200").To4()}: {2, 3},
&net.UDPAddr{IP: net.ParseIP("10.0.200.201").To4()}: {4},
}
mInternal := mock_router.NewMockBatchConn(ctrl)
mInternal.EXPECT().ReadBatch(gomock.Any()).DoAndReturn(
func(m underlayconn.Messages) (int, error) {
i := 0
for k := range routers { // post a BFD from each neighbor router
disc := layers.BFDDiscriminator(i)
raw := postInternalBFD(disc, k.(*net.UDPAddr))
copy(m[i].Buffers[0], raw)
m[i].Addr = &net.UDPAddr{IP: net.IP{10, 0, 200, 200}}
m[i].Buffers[0] = m[i].Buffers[0][:len(raw)]
m[i].N = len(raw)
expectRemoteDiscriminators[disc] = struct{}{}
i++
}
return len(routers), nil
},
).Times(1)
mInternal.EXPECT().ReadBatch(gomock.Any()).Return(0, nil).AnyTimes()
mInternal.EXPECT().WriteTo(gomock.Any(), gomock.Any()).DoAndReturn(
func(data []byte, _ net.Addr) (int, error) {
pkt := gopacket.NewPacket(data,
slayers.LayerTypeSCION, gopacket.Default)
if b := pkt.Layer(layers.LayerTypeBFD); b != nil {
v := b.(*layers.BFD).YourDiscriminator
mtx.Lock()
defer mtx.Unlock()
delete(expectRemoteDiscriminators, v)
if len(expectRemoteDiscriminators) == 0 {
done <- struct{}{}
}
return 1, nil
}
return 0, fmt.Errorf("no valid BFD message")
}).MinTimes(1)
mInternal.EXPECT().WriteTo(gomock.Any(), gomock.Any()).Return(0, nil).AnyTimes()
local := &net.UDPAddr{IP: net.ParseIP("10.0.200.100").To4()}
_ = ret.SetKey([]byte("randomkeyformacs"))
_ = ret.AddInternalInterface(mInternal, net.IP{})
for remote, ifIDs := range routers {
for _, ifID := range ifIDs {
_ = ret.AddNextHop(ifID, remote.(*net.UDPAddr))
_ = ret.AddNextHopBFD(ifID, local, remote.(*net.UDPAddr), bfd(), "")
}
}
return ret
},
},
"bfd sender internal": {
prepareDP: func(ctrl *gomock.Controller, done chan<- struct{}) *router.DataPlane {
ret := &router.DataPlane{Metrics: metrics}
localAddr := &net.UDPAddr{IP: net.ParseIP("10.0.200.100").To4()}
remoteAddr := &net.UDPAddr{IP: net.ParseIP("10.0.200.200").To4()}
mInternal := mock_router.NewMockBatchConn(ctrl)
mInternal.EXPECT().WriteTo(gomock.Any(), gomock.Any()).DoAndReturn(
func(data []byte, _ net.Addr) (int, error) {
pkt := gopacket.NewPacket(data,
slayers.LayerTypeSCION, gopacket.Default)
if b := pkt.Layer(layers.LayerTypeBFD); b == nil {
return 1, nil
}
if scnL := pkt.Layer(slayers.LayerTypeSCION); scnL != nil {
s := scnL.(*slayers.SCION)
a, err := s.SrcAddr()
if err != nil {
return 1, nil
}
if !bytes.Equal(a.IP().AsSlice(), localAddr.IP) {
return 1, nil
}
b, err := s.DstAddr()
if err != nil {
return 1, nil
}
if !bytes.Equal(b.IP().AsSlice(), remoteAddr.IP) {
return 1, nil
}
if s.PathType != empty.PathType {
return 1, nil
}
if _, ok := s.Path.(empty.Path); !ok {
return 1, nil
}
}
done <- struct{}{}
return 1, nil
}).MinTimes(1)
mInternal.EXPECT().ReadBatch(gomock.Any()).Return(0, nil).AnyTimes()
_ = ret.SetKey([]byte("randomkeyformacs"))
_ = ret.AddInternalInterface(mInternal, net.IP{})
_ = ret.AddNextHop(3, localAddr)
_ = ret.AddNextHopBFD(3, localAddr, remoteAddr, bfd(), "")
return ret
},
},
"bfd sender external": {
prepareDP: func(ctrl *gomock.Controller, done chan<- struct{}) *router.DataPlane {
ret := &router.DataPlane{Metrics: metrics}
ifID := uint16(1)
mInternal := mock_router.NewMockBatchConn(ctrl)
mInternal.EXPECT().ReadBatch(gomock.Any()).Return(0, nil).AnyTimes()
mExternal := mock_router.NewMockBatchConn(ctrl)
mExternal.EXPECT().ReadBatch(gomock.Any()).Return(0, nil).AnyTimes()
mExternal.EXPECT().WriteTo(gomock.Any(), gomock.Any()).DoAndReturn(
func(data []byte, _ net.Addr) (int, error) {
pkt := gopacket.NewPacket(data,
slayers.LayerTypeSCION, gopacket.Default)
if b := pkt.Layer(layers.LayerTypeBFD); b == nil {
return 1, nil
}
if scnL := pkt.Layer(slayers.LayerTypeSCION); scnL != nil {
s := scnL.(*slayers.SCION)
if s.PathType != onehop.PathType {
return 1, nil
}
v, ok := s.Path.(*onehop.Path)
if !ok {
return 1, nil
}
if v.FirstHop.ConsEgress != ifID {
return 1, nil
}
}
done <- struct{}{}
return 1, nil
}).MinTimes(1)
mExternal.EXPECT().WriteTo(gomock.Any(), gomock.Any()).Return(0, nil).AnyTimes()
local := control.LinkEnd{
IA: xtest.MustParseIA("1-ff00:0:1"),
Addr: &net.UDPAddr{IP: net.ParseIP("10.0.0.100")},
}
remote := control.LinkEnd{
IA: xtest.MustParseIA("1-ff00:0:3"),
Addr: &net.UDPAddr{IP: net.ParseIP("10.0.0.200")},
}
_ = ret.SetKey([]byte("randomkeyformacs"))
_ = ret.AddInternalInterface(mInternal, net.IP{})
_ = ret.AddExternalInterface(ifID, mExternal)
_ = ret.AddExternalInterfaceBFD(ifID, mExternal, local, remote, bfd())
return ret
},
},
"bfd bootstrap external session": {
prepareDP: func(ctrl *gomock.Controller, done chan<- struct{}) *router.DataPlane {
ret := &router.DataPlane{Metrics: metrics}
postExternalBFD := func(id layers.BFDDiscriminator, fromIfID uint16) []byte {
scn := &slayers.SCION{
NextHdr: slayers.L4BFD,
PathType: onehop.PathType,
Path: &onehop.Path{
FirstHop: path.HopField{ConsEgress: fromIfID},
},
}
bfdL := &layers.BFD{
Version: 1,
DetectMultiplier: layers.BFDDetectMultiplier(2),
MyDiscriminator: id,
YourDiscriminator: 0,
}
buffer := gopacket.NewSerializeBuffer()
_ = gopacket.SerializeLayers(buffer,
gopacket.SerializeOptions{FixLengths: true}, scn, bfdL)
return buffer.Bytes()
}
mInternal := mock_router.NewMockBatchConn(ctrl)
mInternal.EXPECT().ReadBatch(gomock.Any()).Return(0, nil).AnyTimes()
mtx := sync.Mutex{}
expectRemoteDiscriminators := map[int]struct{}{}
mExternal := mock_router.NewMockBatchConn(ctrl)
mExternal.EXPECT().ReadBatch(gomock.Any()).DoAndReturn(
func(m underlayconn.Messages) (int, error) {
raw := postExternalBFD(2, 1)
expectRemoteDiscriminators[2] = struct{}{}
copy(m[0].Buffers[0], raw)
m[0].Buffers[0] = m[0].Buffers[0][:len(raw)]
m[0].N = len(raw)
m[0].Addr = &net.UDPAddr{IP: net.IP{10, 0, 200, 200}}
return 1, nil
},
).Times(1)
mExternal.EXPECT().ReadBatch(gomock.Any()).Return(0, nil).AnyTimes()
mExternal.EXPECT().WriteTo(gomock.Any(), gomock.Any()).DoAndReturn(
func(data []byte, _ net.Addr) (int, error) {
pkt := gopacket.NewPacket(data,
slayers.LayerTypeSCION, gopacket.Default)
if b := pkt.Layer(layers.LayerTypeBFD); b != nil {
v := int(b.(*layers.BFD).YourDiscriminator)
mtx.Lock()
defer mtx.Unlock()
delete(expectRemoteDiscriminators, v)
if len(expectRemoteDiscriminators) == 0 {
done <- struct{}{}
}
return 1, nil
}
return 0, fmt.Errorf("no valid BFD message")
}).MinTimes(1)
mExternal.EXPECT().WriteTo(gomock.Any(), gomock.Any()).Return(0, nil).AnyTimes()
local := control.LinkEnd{
IA: xtest.MustParseIA("1-ff00:0:1"),
Addr: &net.UDPAddr{IP: net.ParseIP("10.0.0.100")},
}
remote := control.LinkEnd{
IA: xtest.MustParseIA("1-ff00:0:3"),
Addr: &net.UDPAddr{IP: net.ParseIP("10.0.0.200")},
}
_ = ret.SetKey([]byte("randomkeyformacs"))
_ = ret.AddInternalInterface(mInternal, net.IP{})
_ = ret.AddExternalInterface(1, mExternal)
_ = ret.AddExternalInterfaceBFD(1, mExternal, local, remote, bfd())
return ret
},
},
}
for name, tc := range testCases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
runConfig := &router.RunConfig{
NumProcessors: 8,
BatchSize: 256,
NumSlowPathProcessors: 1,
}
ch := make(chan struct{})
dp := tc.prepareDP(ctrl, ch)
errors := make(chan error)
ctx, cancelF := context.WithCancel(context.Background())
defer cancelF()
go func() {
errors <- dp.Run(ctx, runConfig)
}()
for done := false; !done; {
select {
case <-ch:
done = true
case err := <-errors:
require.NoError(t, err)
case <-time.After(3 * time.Second):
t.Fatalf("time out")
}
}
})
}
}
func TestProcessPkt(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
key := []byte("testkey_xxxxxxxx")
otherKey := []byte("testkey_yyyyyyyy")
now := time.Now()
epicTS, err := libepic.CreateTimestamp(now, now)
require.NoError(t, err)
// ProcessPacket assumes some pre-conditions:
// * The ingress interface has to exist. This fake map is good for most test cases.
// Others need a custom one.
// * InternalNextHops may not be nil. Empty is ok (sufficient unless testing AS transit).
fakeExternalInterfaces := map[uint16]router.BatchConn{1: nil, 2: nil, 3: nil}
fakeInternalNextHops := map[uint16]*net.UDPAddr{}
testCases := map[string]struct {
mockMsg func(bool) *ipv4.Message
prepareDP func(*gomock.Controller) *router.DataPlane
srcInterface uint16
egressInterface uint16
assertFunc assert.ErrorAssertionFunc
}{
"inbound": {
prepareDP: func(ctrl *gomock.Controller) *router.DataPlane {
return router.NewDP(fakeExternalInterfaces,
nil, mock_router.NewMockBatchConn(ctrl),
fakeInternalNextHops, nil,
xtest.MustParseIA("1-ff00:0:110"), nil, key)
},
mockMsg: func(afterProcessing bool) *ipv4.Message {
spkt, dpath := prepBaseMsg(now)
spkt.DstIA = xtest.MustParseIA("1-ff00:0:110")
dst := addr.MustParseHost("10.0.100.100")
_ = spkt.SetDstAddr(dst)
dpath.HopFields = []path.HopField{
{ConsIngress: 41, ConsEgress: 40},
{ConsIngress: 31, ConsEgress: 30},
{ConsIngress: 1, ConsEgress: 0},
}
dpath.Base.PathMeta.CurrHF = 2
dpath.HopFields[2].Mac = computeMAC(t, key, dpath.InfoFields[0], dpath.HopFields[2])
ret := toMsg(t, spkt, dpath)
if afterProcessing {
ret.Addr = &net.UDPAddr{IP: dst.IP().AsSlice(), Port: topology.EndhostPort}
ret.Flags, ret.NN, ret.N, ret.OOB = 0, 0, 0, nil
}
return ret
},
srcInterface: 1,
egressInterface: 0,
assertFunc: assert.NoError,
},
"inbound_longpath": {
prepareDP: func(ctrl *gomock.Controller) *router.DataPlane {
return router.NewDP(fakeExternalInterfaces,
nil, mock_router.NewMockBatchConn(ctrl),
fakeInternalNextHops, nil,
xtest.MustParseIA("1-ff00:0:110"), nil, key)
},
mockMsg: func(afterProcessing bool) *ipv4.Message {
spkt, dpath := prepBaseMsg(now)
spkt.DstIA = xtest.MustParseIA("1-ff00:0:110")
dst := addr.MustParseHost("10.0.100.100")
_ = spkt.SetDstAddr(dst)
dpath.HopFields = []path.HopField{
{ConsIngress: 41, ConsEgress: 40},
{ConsIngress: 31, ConsEgress: 30},
{ConsIngress: 1, ConsEgress: 0},
}
// Everything is the same a in the inbound test, except that we tossed in
// 64 extra hops and two extra segments.
dpath.Base.PathMeta.CurrHF = 2
dpath.Base.PathMeta.SegLen = [3]uint8{24, 24, 17}
dpath.Base.NumINF = 3
dpath.Base.NumHops = 65
dpath.HopFields[2].Mac = computeMAC(t, key, dpath.InfoFields[0], dpath.HopFields[2])
ret := toMsg(t, spkt, dpath)
if afterProcessing {
ret.Addr = &net.UDPAddr{IP: dst.IP().AsSlice(), Port: topology.EndhostPort}
ret.Flags, ret.NN, ret.N, ret.OOB = 0, 0, 0, nil
}
return ret
},
srcInterface: 1,
egressInterface: 0,
assertFunc: assert.Error,
},
"outbound": {
prepareDP: func(ctrl *gomock.Controller) *router.DataPlane {
return router.NewDP(
map[uint16]router.BatchConn{
uint16(1): mock_router.NewMockBatchConn(ctrl),
},
map[uint16]topology.LinkType{
1: topology.Child,
},
nil,
fakeInternalNextHops, nil,
xtest.MustParseIA("1-ff00:0:110"), nil, key)
},
mockMsg: func(afterProcessing bool) *ipv4.Message {
spkt, dpath := prepBaseMsg(now)
spkt.SrcIA = xtest.MustParseIA("1-ff00:0:110")
dpath.HopFields = []path.HopField{
{ConsIngress: 0, ConsEgress: 1},
{ConsIngress: 31, ConsEgress: 30},
{ConsIngress: 41, ConsEgress: 40},
}
dpath.Base.PathMeta.CurrHF = 0
dpath.HopFields[0].Mac = computeMAC(t, key, dpath.InfoFields[0], dpath.HopFields[0])
if !afterProcessing {
return toMsg(t, spkt, dpath)
}
_ = dpath.IncPath()
dpath.InfoFields[0].UpdateSegID(dpath.HopFields[0].Mac)
ret := toMsg(t, spkt, dpath)
ret.Addr = nil
ret.Flags, ret.NN, ret.N, ret.OOB = 0, 0, 0, nil
return ret
},
srcInterface: 0,
egressInterface: 1,
assertFunc: assert.NoError,
},
"brtransit": {
prepareDP: func(ctrl *gomock.Controller) *router.DataPlane {
return router.NewDP(
map[uint16]router.BatchConn{
uint16(1): mock_router.NewMockBatchConn(ctrl),
uint16(2): mock_router.NewMockBatchConn(ctrl),
},
map[uint16]topology.LinkType{
1: topology.Parent,
2: topology.Child,
},
nil,
fakeInternalNextHops, nil,
xtest.MustParseIA("1-ff00:0:110"), nil, key)
},
mockMsg: func(afterProcessing bool) *ipv4.Message {
spkt, dpath := prepBaseMsg(now)
dpath.HopFields = []path.HopField{
{ConsIngress: 31, ConsEgress: 30},
{ConsIngress: 1, ConsEgress: 2},
{ConsIngress: 40, ConsEgress: 41},
}
dpath.Base.PathMeta.CurrHF = 1
dpath.HopFields[1].Mac = computeMAC(t, key, dpath.InfoFields[0], dpath.HopFields[1])
if !afterProcessing {
return toMsg(t, spkt, dpath)
}
_ = dpath.IncPath()
dpath.InfoFields[0].UpdateSegID(dpath.HopFields[1].Mac)
ret := toMsg(t, spkt, dpath)
ret.Addr = nil
ret.Flags, ret.NN, ret.N, ret.OOB = 0, 0, 0, nil
return ret
},
srcInterface: 1,
egressInterface: 2,
assertFunc: assert.NoError,
},
"brtransit non consdir": {
prepareDP: func(ctrl *gomock.Controller) *router.DataPlane {
return router.NewDP(
map[uint16]router.BatchConn{
uint16(1): mock_router.NewMockBatchConn(ctrl),
uint16(2): mock_router.NewMockBatchConn(ctrl),
},
map[uint16]topology.LinkType{
2: topology.Parent,
1: topology.Child,
}, nil,
fakeInternalNextHops, nil,
xtest.MustParseIA("1-ff00:0:110"), nil, key)
},
mockMsg: func(afterProcessing bool) *ipv4.Message {
spkt, dpath := prepBaseMsg(now)
dpath.HopFields = []path.HopField{
{ConsIngress: 31, ConsEgress: 30},
{ConsIngress: 2, ConsEgress: 1},
{ConsIngress: 40, ConsEgress: 41},
}
dpath.Base.PathMeta.CurrHF = 1
dpath.InfoFields[0].ConsDir = false
dpath.HopFields[1].Mac = computeMAC(t, key, dpath.InfoFields[0], dpath.HopFields[1])
if !afterProcessing {
dpath.InfoFields[0].UpdateSegID(dpath.HopFields[1].Mac)
return toMsg(t, spkt, dpath)
}
require.NoError(t, dpath.IncPath())
ret := toMsg(t, spkt, dpath)
ret.Addr = nil
ret.Flags, ret.NN, ret.N, ret.OOB = 0, 0, 0, nil
return ret
},
srcInterface: 1,
egressInterface: 2,
assertFunc: assert.NoError,
},
"brtransit peering consdir": {
prepareDP: func(ctrl *gomock.Controller) *router.DataPlane {
return router.NewDP(
map[uint16]router.BatchConn{
uint16(1): mock_router.NewMockBatchConn(ctrl),
uint16(2): mock_router.NewMockBatchConn(ctrl),
},
map[uint16]topology.LinkType{
1: topology.Peer,
2: topology.Child,
},
nil,
fakeInternalNextHops, nil,
xtest.MustParseIA("1-ff00:0:110"), nil, key)
},
mockMsg: func(afterProcessing bool) *ipv4.Message {
// Story: the packet just left segment 0 which ends at
// (peering) hop 0 and is landing on segment 1 which
// begins at (peering) hop 1. We do not care what hop 0
// looks like. The forwarding code is looking at hop 1 and
// should leave the message in shape to be processed at hop 2.
spkt, _ := prepBaseMsg(now)
dpath := &scion.Decoded{
Base: scion.Base{
PathMeta: scion.MetaHdr{
CurrHF: 1,
CurrINF: 1,
SegLen: [3]uint8{1, 2, 0},
},
NumINF: 2,
NumHops: 3,
},
InfoFields: []path.InfoField{
// up seg
{SegID: 0x111, ConsDir: true, Timestamp: util.TimeToSecs(now), Peer: true},
// core seg
{SegID: 0x222, ConsDir: true, Timestamp: util.TimeToSecs(now), Peer: true},
},
HopFields: []path.HopField{
{ConsIngress: 31, ConsEgress: 30},
{ConsIngress: 1, ConsEgress: 2},
{ConsIngress: 40, ConsEgress: 41},
},
}
// Make obvious the unusual aspect of the path: two
// hopfield MACs (1 and 2) derive from the same SegID
// accumulator value. However, the forwarding code isn't
// supposed to even look at the second one. The SegID
// accumulator value can be anything (it comes from the
// parent hop of HF[1] in the original beaconned segment,
// which is not in the path). So, we use one from an
// info field because computeMAC makes that easy.
dpath.HopFields[1].Mac = computeMAC(
t, key, dpath.InfoFields[1], dpath.HopFields[1])
dpath.HopFields[2].Mac = computeMAC(
t, otherKey, dpath.InfoFields[1], dpath.HopFields[2])
if !afterProcessing {
return toMsg(t, spkt, dpath)
}
_ = dpath.IncPath()
// ... The SegID accumulator wasn't updated from HF[1],
// it is still the same. That is the key behavior.
ret := toMsg(t, spkt, dpath)
ret.Addr = nil
ret.Flags, ret.NN, ret.N, ret.OOB = 0, 0, 0, nil
return ret
},
srcInterface: 1, // from peering link
egressInterface: 2,
assertFunc: assert.NoError,
},
"brtransit peering non consdir": {
prepareDP: func(ctrl *gomock.Controller) *router.DataPlane {
return router.NewDP(
map[uint16]router.BatchConn{
uint16(1): mock_router.NewMockBatchConn(ctrl),
uint16(2): mock_router.NewMockBatchConn(ctrl),
},
map[uint16]topology.LinkType{
1: topology.Peer,
2: topology.Child,
},
nil,
fakeInternalNextHops, nil,
xtest.MustParseIA("1-ff00:0:110"), nil, key)
},
mockMsg: func(afterProcessing bool) *ipv4.Message {
// Story: the packet lands on the last (peering) hop of
// segment 0. After processing, the packet is ready to
// be processed by the first (peering) hop of segment 1.
spkt, _ := prepBaseMsg(now)
dpath := &scion.Decoded{
Base: scion.Base{
PathMeta: scion.MetaHdr{
CurrHF: 1,
CurrINF: 0,
SegLen: [3]uint8{2, 1, 0},
},
NumINF: 2,
NumHops: 3,
},
InfoFields: []path.InfoField{
// up seg
{SegID: 0x111, ConsDir: false, Timestamp: util.TimeToSecs(now), Peer: true},
// down seg
{SegID: 0x222, ConsDir: true, Timestamp: util.TimeToSecs(now), Peer: true},
},
HopFields: []path.HopField{
{ConsIngress: 31, ConsEgress: 30},
{ConsIngress: 1, ConsEgress: 2},
{ConsIngress: 40, ConsEgress: 41},
},
}
// Make obvious the unusual aspect of the path: two
// hopfield MACs (0 and 1) derive from the same SegID
// accumulator value. However, the forwarding code isn't
// supposed to even look at the first one. The SegID
// accumulator value can be anything (it comes from the
// parent hop of HF[1] in the original beaconned segment,
// which is not in the path). So, we use one from an
// info field because computeMAC makes that easy.
dpath.HopFields[0].Mac = computeMAC(
t, otherKey, dpath.InfoFields[0], dpath.HopFields[0])
dpath.HopFields[1].Mac = computeMAC(
t, key, dpath.InfoFields[0], dpath.HopFields[1])
// We're going against construction order, so the accumulator
// value is that of the previous hop in traversal order. The
// story starts with the packet arriving at hop 1, so the
// accumulator value must match hop field 0. In this case,
// it is identical to that for hop field 1, which we made
// identical to the original SegID. So, we're all set.
if !afterProcessing {
return toMsg(t, spkt, dpath)
}
_ = dpath.IncPath()
// The SegID should not get updated on arrival. If it is, then MAC validation
// of HF1 will fail. Otherwise, this isn't visible because we changed segment.
ret := toMsg(t, spkt, dpath)
ret.Addr = nil
ret.Flags, ret.NN, ret.N, ret.OOB = 0, 0, 0, nil
return ret
},
srcInterface: 2, // from child link
egressInterface: 1,
assertFunc: assert.NoError,
},
"peering consdir downstream": {
// Similar to previous test case but looking at what
// happens on the next hop.
prepareDP: func(ctrl *gomock.Controller) *router.DataPlane {
return router.NewDP(
map[uint16]router.BatchConn{
uint16(1): mock_router.NewMockBatchConn(ctrl),
uint16(2): mock_router.NewMockBatchConn(ctrl),
},
map[uint16]topology.LinkType{
1: topology.Peer,
2: topology.Child,
},
nil,
fakeInternalNextHops, nil,
xtest.MustParseIA("1-ff00:0:110"), nil, key)
},
mockMsg: func(afterProcessing bool) *ipv4.Message {
// Story: the packet just left hop 1 (the first hop
// of peering down segment 1) and is processed at hop 2
// which is not a peering hop.
spkt, _ := prepBaseMsg(now)
dpath := &scion.Decoded{
Base: scion.Base{
PathMeta: scion.MetaHdr{
CurrHF: 2,
CurrINF: 1,
SegLen: [3]uint8{1, 3, 0},
},
NumINF: 2,
NumHops: 4,
},
InfoFields: []path.InfoField{
// up seg
{SegID: 0x111, ConsDir: true, Timestamp: util.TimeToSecs(now), Peer: true},
// core seg
{SegID: 0x222, ConsDir: true, Timestamp: util.TimeToSecs(now), Peer: true},
},
HopFields: []path.HopField{
{ConsIngress: 31, ConsEgress: 30},
{ConsIngress: 40, ConsEgress: 41},
{ConsIngress: 1, ConsEgress: 2},
{ConsIngress: 50, ConsEgress: 51},
// There has to be a 4th hop to make
// the 3rd router agree that the packet
// is not at destination yet.
},
}
// Make obvious the unusual aspect of the path: two
// hopfield MACs (1 and 2) derive from the same SegID
// accumulator value. The router shouldn't need to
// know this or do anything special. The SegID
// accumulator value can be anything (it comes from the
// parent hop of HF[1] in the original beaconned segment,
// which is not in the path). So, we use one from an
// info field because computeMAC makes that easy.
dpath.HopFields[1].Mac = computeMAC(
t, otherKey, dpath.InfoFields[1], dpath.HopFields[1])
dpath.HopFields[2].Mac = computeMAC(
t, key, dpath.InfoFields[1], dpath.HopFields[2])
if !afterProcessing {
// The SegID we provide is that of HF[2] which happens to be SEG[1]'s SegID,
// so, already set.
return toMsg(t, spkt, dpath)
}
_ = dpath.IncPath()
// ... The SegID accumulator should have been updated.
dpath.InfoFields[1].UpdateSegID(dpath.HopFields[2].Mac)
ret := toMsg(t, spkt, dpath)
ret.Addr = nil
ret.Flags, ret.NN, ret.N, ret.OOB = 0, 0, 0, nil