summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/net/srv6_end_flavors_test.sh
blob: 50563443a4adced4784c08413ea37504417da08e (plain)
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
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0
#
# author: Andrea Mayer <andrea.mayer@uniroma2.it>
# author: Paolo Lungaroni <paolo.lungaroni@uniroma2.it>
#
# This script is designed to test the support for "flavors" in the SRv6 End
# behavior.
#
# Flavors defined in RFC8986 [1] represent additional operations that can modify
# or extend the existing SRv6 End, End.X and End.T behaviors. For the sake of
# convenience, we report the list of flavors described in [1] hereafter:
#   - Penultimate Segment Pop (PSP);
#   - Ultimate Segment Pop (USP);
#   - Ultimate Segment Decapsulation (USD).
#
# The End, End.X, and End.T behaviors can support these flavors either
# individually or in combinations.
# Currently in this selftest we consider only the PSP flavor for the SRv6 End
# behavior. However, it is possible to extend the script as soon as other
# flavors will be supported in the kernel.
#
# The purpose of the PSP flavor consists in instructing the penultimate node
# listed in the SRv6 policy to remove (i.e. pop) the outermost SRH from the IPv6
# header.
# A PSP enabled SRv6 End behavior instance processes the SRH by:
#  - decrementing the Segment Left (SL) value from 1 to 0;
#  - copying the last SID from the SID List into the IPv6 Destination Address
#    (DA);
#  - removing the SRH from the extension headers following the IPv6 header.
#
# Once the SRH is removed, the IPv6 packet is forwarded to the destination using
# the IPv6 DA updated during the PSP operation (i.e. the IPv6 DA corresponding
# to the last SID carried by the removed SRH).
#
# Although the PSP flavor can be set for any SRv6 End behavior instance on any
# SR node, it will be active only on such behaviors bound to a penultimate SID
# for a given SRv6 policy.
#                                                SL=2 SL=1 SL=0
#                                                  |    |    |
# For example, given the SRv6 policy (SID List := <X,   Y,   Z>):
#  - a PSP enabled SRv6 End behavior bound to SID Y will apply the PSP operation
#    as Segment Left (SL) is 1, corresponding to the Penultimate Segment of the
#    SID List;
#  - a PSP enabled SRv6 End behavior bound to SID X will *NOT* apply the PSP
#    operation as the Segment Left is 2. This behavior instance will apply the
#    "standard" End packet processing, ignoring the configured PSP flavor at
#    all.
#
# [1] RFC8986: https://datatracker.ietf.org/doc/html/rfc8986
#
# Network topology
# ================
#
# The network topology used in this selftest is depicted hereafter, composed by
# two hosts (hs-1, hs-2) and four routers (rt-1, rt-2, rt-3, rt-4).
# Hosts hs-1 and hs-2 are connected to routers rt-1 and rt-2, respectively,
# allowing them to communicate with each other.
# Traffic exchanged between hs-1 and hs-2 can follow different network paths.
# The network operator, through specific SRv6 Policies can steer traffic to one
# path rather than another. In this selftest this is implemented as follows:
#
#   i) The SRv6 H.Insert behavior applies SRv6 Policies on traffic received by
#      connected hosts. It pushes the Segment Routing Header (SRH) after the
#      IPv6 header. The SRH contains the SID List (i.e. SRv6 Policy) needed for
#      steering traffic across the segments/waypoints specified in that list;
#
#  ii) The SRv6 End behavior advances the active SID in the SID List carried by
#      the SRH;
#
# iii) The PSP enabled SRv6 End behavior is used to remove the SRH when such
#      behavior is configured on a node bound to the Penultimate Segment carried
#      by the SID List.
#
#                cafe::1                      cafe::2
#              +--------+                   +--------+
#              |        |                   |        |
#              |  hs-1  |                   |  hs-2  |
#              |        |                   |        |
#              +---+----+                   +--- +---+
#     cafe::/64    |                             |      cafe::/64
#                  |                             |
#              +---+----+                   +----+---+
#              |        |  fcf0:0:1:2::/64  |        |
#              |  rt-1  +-------------------+  rt-2  |
#              |        |                   |        |
#              +---+----+                   +----+---+
#                  |      .               .      |
#                  |  fcf0:0:1:3::/64   .        |
#                  |          .       .          |
#                  |            .   .            |
#  fcf0:0:1:4::/64 |              .              | fcf0:0:2:3::/64
#                  |            .   .            |
#                  |          .       .          |
#                  |  fcf0:0:2:4::/64   .        |
#                  |      .               .      |
#              +---+----+                   +----+---+
#              |        |                   |        |
#              |  rt-4  +-------------------+  rt-3  |
#              |        |  fcf0:0:3:4::/64  |        |
#              +---+----+                   +----+---+
#
# Every fcf0:0:x:y::/64 network interconnects the SRv6 routers rt-x with rt-y in
# the IPv6 operator network.
#
#
# Local SID table
# ===============
#
# Each SRv6 router is configured with a Local SID table in which SIDs are
# stored. Considering the given SRv6 router rt-x, at least two SIDs are
# configured in the Local SID table:
#
#   Local SID table for SRv6 router rt-x
#   +---------------------------------------------------------------------+
#   |fcff:x::e is associated with the SRv6 End behavior                   |
#   |fcff:x::ef1 is associated with the SRv6 End behavior with PSP flavor |
#   +---------------------------------------------------------------------+
#
# The fcff::/16 prefix is reserved by the operator for the SIDs. Reachability of
# SIDs is ensured by proper configuration of the IPv6 operator's network and
# SRv6 routers.
#
#
# SRv6 Policies
# =============
#
# An SRv6 ingress router applies different SRv6 Policies to the traffic received
# from connected hosts on the basis of the destination addresses.
# In case of SRv6 H.Insert behavior, the SRv6 Policy enforcement consists of
# pushing the SRH (carrying a given SID List) after the existing IPv6 header.
# Note that in the inserting mode, there is no encapsulation at all.
#
#   Before applying an SRv6 Policy using the SRv6 H.Insert behavior
#   +------+---------+
#   | IPv6 | Payload |
#   +------+---------+
#
#   After applying an SRv6 Policy using the SRv6 H.Insert behavior
#   +------+-----+---------+
#   | IPv6 | SRH | Payload |
#   +------+-----+---------+
#
# Traffic from hs-1 to hs-2
# -------------------------
#
# Packets generated from hs-1 and directed towards hs-2 are
# handled by rt-1 which applies the following SRv6 Policy:
#
#   i.a) IPv6 traffic, SID List=fcff:3::e,fcff:4::ef1,fcff:2::ef1,cafe::2
#
# Router rt-1 is configured to enforce the Policy (i.a) through the SRv6
# H.Insert behavior which pushes the SRH after the existing IPv6 header. This
# Policy steers the traffic from hs-1 across rt-3, rt-4, rt-2 and finally to the
# destination hs-2.
#
# As the packet reaches the router rt-3, the SRv6 End behavior bound to SID
# fcff:3::e is triggered. The behavior updates the Segment Left (from SL=3 to
# SL=2) in the SRH, the IPv6 DA with fcff:4::ef1 and forwards the packet to the
# next router on the path, i.e. rt-4.
#
# When router rt-4 receives the packet, the PSP enabled SRv6 End behavior bound
# to SID fcff:4::ef1 is executed. Since the SL=2, the PSP operation is *NOT*
# kicked in and the behavior applies the default End processing: the Segment
# Left is decreased (from SL=2 to SL=1), the IPv6 DA is updated with the SID
# fcff:2::ef1 and the packet is forwarded to router rt-2.
#
# The PSP enabled SRv6 End behavior on rt-2 is associated with SID fcff:2::ef1
# and is executed as the packet is received. Because SL=1, the behavior applies
# the PSP processing on the packet as follows: i) SL is decreased, i.e. from
# SL=1 to SL=0; ii) last SID (cafe::2) is copied into the IPv6 DA; iii) the
# outermost SRH is removed from the extension headers following the IPv6 header.
# Once the PSP processing is completed, the packet is forwarded to the host hs-2
# (destination).
#
# Traffic from hs-2 to hs-1
# -------------------------
#
# Packets generated from hs-2 and directed to hs-1 are handled by rt-2 which
# applies the following SRv6 Policy:
#
#   i.b) IPv6 traffic, SID List=fcff:1::ef1,cafe::1
#
# Router rt-2 is configured to enforce the Policy (i.b) through the SRv6
# H.Insert behavior which pushes the SRH after the existing IPv6 header. This
# Policy steers the traffic from hs-2 across rt-1 and finally to the
# destination hs-1
#
#
# When the router rt-1 receives the packet, the PSP enabled SRv6 End behavior
# associated with the SID fcff:1::ef1 is triggered. Since the SL=1,
# the PSP operation takes place: i) the SL is decremented; ii) the IPv6 DA is
# set with the last SID; iii) the SRH is removed from the extension headers
# after the IPv6 header. At this point, the packet with IPv6 DA=cafe::1 is sent
# to the destination, i.e. hs-1.

# Kselftest framework requirement - SKIP code is 4.
readonly ksft_skip=4

readonly RDMSUFF="$(mktemp -u XXXXXXXX)"
readonly DUMMY_DEVNAME="dum0"
readonly RT2HS_DEVNAME="veth1"
readonly LOCALSID_TABLE_ID=90
readonly IPv6_RT_NETWORK=fcf0:0
readonly IPv6_HS_NETWORK=cafe
readonly IPv6_TESTS_ADDR=2001:db8::1
readonly LOCATOR_SERVICE=fcff
readonly END_FUNC=000e
readonly END_PSP_FUNC=0ef1

PING_TIMEOUT_SEC=4
PAUSE_ON_FAIL=${PAUSE_ON_FAIL:=no}

# IDs of routers and hosts are initialized during the setup of the testing
# network
ROUTERS=''
HOSTS=''

SETUP_ERR=1

ret=${ksft_skip}
nsuccess=0
nfail=0

log_test()
{
	local rc="$1"
	local expected="$2"
	local msg="$3"

	if [ "${rc}" -eq "${expected}" ]; then
		nsuccess=$((nsuccess+1))
		printf "\n    TEST: %-60s  [ OK ]\n" "${msg}"
	else
		ret=1
		nfail=$((nfail+1))
		printf "\n    TEST: %-60s  [FAIL]\n" "${msg}"
		if [ "${PAUSE_ON_FAIL}" = "yes" ]; then
			echo
			echo "hit enter to continue, 'q' to quit"
			read a
			[ "$a" = "q" ] && exit 1
		fi
	fi
}

print_log_test_results()
{
	printf "\nTests passed: %3d\n" "${nsuccess}"
	printf "Tests failed: %3d\n"   "${nfail}"

	# when a test fails, the value of 'ret' is set to 1 (error code).
	# Conversely, when all tests are passed successfully, the 'ret' value
	# is set to 0 (success code).
	if [ "${ret}" -ne 1 ]; then
		ret=0
	fi
}

log_section()
{
	echo
	echo "################################################################################"
	echo "TEST SECTION: $*"
	echo "################################################################################"
}

test_command_or_ksft_skip()
{
	local cmd="$1"

	if [ ! -x "$(command -v "${cmd}")" ]; then
		echo "SKIP: Could not run test without \"${cmd}\" tool";
		exit "${ksft_skip}"
	fi
}

get_nodename()
{
	local name="$1"

	echo "${name}-${RDMSUFF}"
}

get_rtname()
{
	local rtid="$1"

	get_nodename "rt-${rtid}"
}

get_hsname()
{
	local hsid="$1"

	get_nodename "hs-${hsid}"
}

__create_namespace()
{
	local name="$1"

	ip netns add "${name}"
}

create_router()
{
	local rtid="$1"
	local nsname

	nsname="$(get_rtname "${rtid}")"

	__create_namespace "${nsname}"
}

create_host()
{
	local hsid="$1"
	local nsname

	nsname="$(get_hsname "${hsid}")"

	__create_namespace "${nsname}"
}

cleanup()
{
	local nsname
	local i

	# destroy routers
	for i in ${ROUTERS}; do
		nsname="$(get_rtname "${i}")"

		ip netns del "${nsname}" &>/dev/null || true
	done

	# destroy hosts
	for i in ${HOSTS}; do
		nsname="$(get_hsname "${i}")"

		ip netns del "${nsname}" &>/dev/null || true
	done

	# check whether the setup phase was completed successfully or not. In
	# case of an error during the setup phase of the testing environment,
	# the selftest is considered as "skipped".
	if