summaryrefslogtreecommitdiff
path: root/tools/testing/selftests/net/forwarding/fib_offload_lib.sh
blob: 66496659bea70656faf10931a1ddae135d876313 (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
870
871
872
873
# SPDX-License-Identifier: GPL-2.0
#
# Various helpers and tests to verify FIB offload.

__fib_trap_check()
{
	local ns=$1; shift
	local family=$1; shift
	local route=$1; shift
	local should_fail=$1; shift
	local ret

	ip -n $ns -j -p -$family route show $route \
		| jq -e '.[]["flags"] | contains(["trap"])' &> /dev/null
	ret=$?
	if [[ $should_fail == "true" ]]; then
		if [[ $ret -ne 0 ]]; then
			return 0
		else
			return 1
		fi
	fi

	return $ret
}

fib_trap_check()
{
	local ns=$1; shift
	local family=$1; shift
	local route=$1; shift
	local should_fail=$1; shift

	busywait 5000 __fib_trap_check $ns $family "$route" $should_fail
}

fib4_trap_check()
{
	local ns=$1; shift
	local route=$1; shift
	local should_fail=$1; shift

	fib_trap_check $ns 4 "$route" $should_fail
}

fib6_trap_check()
{
	local ns=$1; shift
	local route=$1; shift
	local should_fail=$1; shift

	fib_trap_check $ns 6 "$route" $should_fail
}

fib_ipv4_identical_routes_test()
{
	local ns=$1; shift
	local i

	RET=0

	for i in $(seq 1 3); do
		ip -n $ns link add name dummy$i type dummy
		ip -n $ns link set dev dummy$i up
	done

	ip -n $ns route add 192.0.2.0/24 dev dummy1 tos 0 metric 1024
	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 tos 0 metric 1024" false
	check_err $? "Route not in hardware when should"

	ip -n $ns route append 192.0.2.0/24 dev dummy2 tos 0 metric 1024
	fib4_trap_check $ns "192.0.2.0/24 dev dummy2 tos 0 metric 1024" true
	check_err $? "Appended route in hardware when should not"

	ip -n $ns route prepend 192.0.2.0/24 dev dummy3 tos 0 metric 1024
	fib4_trap_check $ns "192.0.2.0/24 dev dummy3 tos 0 metric 1024" false
	check_err $? "Prepended route not in hardware when should"

	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 tos 0 metric 1024" true
	check_err $? "Route was not replaced in hardware by prepended one"

	log_test "IPv4 identical routes"

	for i in $(seq 1 3); do
		ip -n $ns link del dev dummy$i
	done
}

fib_ipv4_tos_test()
{
	local ns=$1; shift

	RET=0

	ip -n $ns link add name dummy1 type dummy
	ip -n $ns link set dev dummy1 up

	ip -n $ns route add 192.0.2.0/24 dev dummy1 tos 0 metric 1024
	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 tos 0 metric 1024" false
	check_err $? "Route not in hardware when should"

	ip -n $ns route add 192.0.2.0/24 dev dummy1 tos 2 metric 1024
	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 tos 2 metric 1024" false
	check_err $? "Highest TOS route not in hardware when should"

	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 tos 0 metric 1024" true
	check_err $? "Lowest TOS route still in hardware when should not"

	ip -n $ns route add 192.0.2.0/24 dev dummy1 tos 1 metric 1024
	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 tos 1 metric 1024" true
	check_err $? "Middle TOS route in hardware when should not"

	log_test "IPv4 routes with TOS"

	ip -n $ns link del dev dummy1
}

fib_ipv4_metric_test()
{
	local ns=$1; shift

	RET=0

	ip -n $ns link add name dummy1 type dummy
	ip -n $ns link set dev dummy1 up

	ip -n $ns route add 192.0.2.0/24 dev dummy1 metric 1024
	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 metric 1024" false
	check_err $? "Route not in hardware when should"

	ip -n $ns route add 192.0.2.0/24 dev dummy1 metric 1022
	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 metric 1022" false
	check_err $? "Lowest metric route not in hardware when should"

	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 metric 1024" true
	check_err $? "Highest metric route still in hardware when should not"

	ip -n $ns route add 192.0.2.0/24 dev dummy1 metric 1023
	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 metric 1023" true
	check_err $? "Middle metric route in hardware when should not"

	log_test "IPv4 routes with metric"

	ip -n $ns link del dev dummy1
}

fib_ipv4_replace_test()
{
	local ns=$1; shift
	local i

	RET=0

	for i in $(seq 1 2); do
		ip -n $ns link add name dummy$i type dummy
		ip -n $ns link set dev dummy$i up
	done

	ip -n $ns route add 192.0.2.0/24 dev dummy1 metric 1024
	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 metric 1024" false
	check_err $? "Route not in hardware when should"

	ip -n $ns route replace 192.0.2.0/24 dev dummy2 metric 1024
	fib4_trap_check $ns "192.0.2.0/24 dev dummy2 metric 1024" false
	check_err $? "Replacement route not in hardware when should"

	# Add a route with an higher metric and make sure that replacing it
	# does not affect the lower metric one.
	ip -n $ns route add 192.0.2.0/24 dev dummy1 metric 1025
	ip -n $ns route replace 192.0.2.0/24 dev dummy2 metric 1025

	fib4_trap_check $ns "192.0.2.0/24 dev dummy2 metric 1024" false
	check_err $? "Lowest metric route not in hardware when should"
	fib4_trap_check $ns "192.0.2.0/24 dev dummy2 metric 1025" true
	check_err $? "Highest metric route in hardware when should not"

	log_test "IPv4 route replace"

	for i in $(seq 1 2); do
		ip -n $ns link del dev dummy$i
	done
}

fib_ipv4_delete_test()
{
	local ns=$1; shift
	local metric

	RET=0

	ip -n $ns link add name dummy1 type dummy
	ip -n $ns link set dev dummy1 up

	# Insert multiple routes with the same prefix and length and varying
	# metrics. Make sure that throughout delete operations the lowest
	# metric route is the one in hardware.
	for metric in $(seq 1024 1026); do
		ip -n $ns route add 192.0.2.0/24 dev dummy1 metric $metric
	done

	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 metric 1024" false
	check_err $? "Route not in hardware when should"

	ip -n $ns route del 192.0.2.0/24 dev dummy1 metric 1024
	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 metric 1025" false
	check_err $? "Lowest metric route not in hardware when should"

	ip -n $ns route del 192.0.2.0/24 dev dummy1 metric 1026
	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 metric 1025" false
	check_err $? "Sole route not in hardware when should"

	log_test "IPv4 route delete"

	ip -n $ns link del dev dummy1
}

fib_ipv4_plen_test()
{
	local ns=$1; shift

	RET=0

	ip -n $ns link add name dummy1 type dummy
	ip -n $ns link set dev dummy1 up

	# Add two routes with the same key and different prefix length and
	# make sure both are in hardware. It can be verfied that both are
	# sharing the same leaf by checking the /proc/net/fib_trie
	ip -n $ns route add 192.0.2.0/24 dev dummy1
	ip -n $ns route add 192.0.2.0/25 dev dummy1

	fib4_trap_check $ns "192.0.2.0/24 dev dummy1" false
	check_err $? "/24 not in hardware when should"

	fib4_trap_check $ns "192.0.2.0/25 dev dummy1" false
	check_err $? "/25 not in hardware when should"

	log_test "IPv4 routes with different prefix length"

	ip -n $ns link del dev dummy1
}

fib_ipv4_replay_metric_test()
{
	local ns=$1; shift
	local devlink_dev=$1; shift

	RET=0

	ip -n $ns link add name dummy1 type dummy
	ip -n $ns link set dev dummy1 up

	ip -n $ns route add 192.0.2.0/24 dev dummy1 metric 1024
	ip -n $ns route add 192.0.2.0/24 dev dummy1 metric 1025

	devlink -N $ns dev reload $devlink_dev

	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 metric 1024" false
	check_err $? "Lowest metric route not in hardware when should"

	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 metric 1025" true
	check_err $? "Highest metric route in hardware when should not"

	log_test "IPv4 routes replay - metric"

	ip -n $ns link del dev dummy1
}

fib_ipv4_replay_tos_test()
{
	local ns=$1; shift
	local devlink_dev=$1; shift

	RET=0

	ip -n $ns link add name dummy1 type dummy
	ip -n $ns link set dev dummy1 up

	ip -n $ns route add 192.0.2.0/24 dev dummy1 tos 0
	ip -n $ns route add 192.0.2.0/24 dev dummy1 tos 1

	devlink -N $ns dev reload $devlink_dev

	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 tos 1" false
	check_err $? "Highest TOS route not in hardware when should"

	fib4_trap_check $ns "192.0.2.0/24 dev dummy1 tos 0" true
	check_err $? "Lowest TOS route in hardware when should not"

	log_test "IPv4 routes replay - TOS"

	ip -n $ns link del dev dummy1
}

fib_ipv4_replay_plen_test()
{
	local ns=$1; shift
	local devlink_dev=$1; shift

	RET=0

	ip -n $ns link add name dummy1 type dummy
	ip -n $ns link set dev dummy1 up

	ip -n $ns route add 192.0.2.0/24 dev dummy1
	ip -n $ns route add 192.0.2.0/25 dev dummy1

	devlink -N $ns dev reload $devlink_dev

	fib4_trap_check $ns "192.0.2.0/24 dev dummy1" false
	check_err $? "/24 not in hardware when should"

	fib4_trap_check $ns "192.0.2.0/25 dev dummy1" false
	check_err $? "/25 not in hardware when should"

	log_test "IPv4 routes replay - prefix length"

	ip -n $ns link del dev dummy1
}

fib_ipv4_flush_test()
{
	local ns=$1; shift
	local metric

	RET=0

	ip -n $ns link add name dummy1 type dummy
	ip -n $ns link set dev dummy1 up

	# Exercise the routes flushing code paths by inserting various
	# prefix routes on a netdev and then deleting it.
	for metric in $(seq 1 20); do
		ip -n $ns route add 192.0.2.0/24 dev dummy1 metric $metric
	done

	ip -n $ns link del dev dummy1

	log_test "IPv4 routes flushing"
}

fib_ipv6_add_test()
{
	local ns=$1; shift

	RET=0

	for i in $(seq 1 2); do
		ip -n $ns link add name dummy$i type dummy
		ip -n $ns link set dev dummy$i up
	done

	ip -n $ns route add 2001:db8:1::/64 dev dummy1 metric 1024
	fib6_trap_check $ns "2001:db8:1::/64 dev dummy1 metric 1024" false
	check_err $? "Route not in hardware when should"

	ip -n $ns route append 2001:db8:1::/64 dev dummy2 metric 1024
	fib6_trap_check $ns "2001:db8:1::/64 dev dummy2 metric 1024" true
	check_err $? "Route in hardware when should not"

	fib6_trap_check $ns "2001:db8:1::/64 dev dummy1 metric 1024" false
	check_err $? "Route not in hardware after appending route"

	log_test "IPv6 single route add"

	for i in $(seq 1 2); do
		ip -n $ns link del dev dummy$i
	done
}

fib_ipv6_metric_test()
{
	local ns=$1; shift

	RET=0

	ip -n $ns link add name dummy1 type dummy
	ip -n $ns link set dev dummy1 up

	ip -n $ns route add 2001:db8:1::/64 dev dummy1 metric 1024
	fib6_trap_check $ns "2001:db8:1::/64 dev dummy1 metric 1024" false
	check_err $? "Route not in hardware when should"

	ip -n $ns route add 2001:db8:1::/64 dev dummy1 metric 1022
	fib6_trap_check $ns "2001:db8:1::/64 dev dummy1 metric 1022" false
	check_err $? "Lowest metric route not in hardware when should"

	fib6_trap_check $ns "2001:db8:1::/64 dev dummy1 metric 1024" true
	check_err $? "Highest metric route still in hardware when should not"

	ip -n $ns route add 2001:db8:1::/64 dev dummy1 metric 1023
	fib6_trap_check $ns "2001:db8:1::/64 dev dummy1 metric 1023" true
	check_err $? "Middle metric route in hardware when should not"

	log_test "IPv6 routes with metric"

	ip -n $ns link del dev dummy1
}

fib_ipv6_append_single_test()
{
	local ns=$1; shift

	# When an IPv6 multipath route is added without the 'nexthop' keyword,
	# different code paths are taken compared to when the keyword is used.
	# This test tries to verify the former.
	RET=0

	for i in $(seq 1 2); do
		ip -n $ns link add name dummy$i type dummy
		ip -n $ns link set dev dummy$i up
		ip -n $ns address add 2001:db8:$i::1/64 dev dummy$i
	done

	ip -n $ns route add 2001:db8:10::/64 via 2001:db8:1::2 metric 1024
	fib6_trap_check $ns "2001:db8:10::/64 metric 1024" false
	check_err $? "Route not in hardware when should"

	ip -n $ns route append 2001:db8:10::/64 via 2001:db8:2::2 metric 1024
	fib6_trap_check $ns "2001:db8:10::/64 metric 1024" false
	check_err $? "Route not in hardware after appending"

	ip -n $ns route add 2001:db8:10::/64 via 2001:db8:1::2 metric 1025
	fib6_trap_check $ns "2001:db8:10::/64 metric 1025" true
	check_err $? "Route in hardware when should not"

	ip -n $ns route append 2001:db8:10::/64 via 2001:db8:2::2 metric 1025
	fib6_trap_check $ns "2001:db8:10::/64 metric 1025" true
	check_err $? "Route in hardware when should not after appending"

	fib6_trap_check $ns "2001:db8:10::/64 metric 1024" false
	check_err $? "Lowest metric route not in hardware when should"

	log_test "IPv6 append single route without 'nexthop' keyword"

	for i in $(seq 1 2); do
		ip -n $ns link del dev dummy$i
	done
}

fib_ipv6_replace_single_test()
{
	local ns=$1; shift
	local i

	RET=0

	for i in $(seq 1 2); do
		ip -n $ns link add name dummy$i type dummy
		ip -n $ns link set dev dummy$i up
	done

	ip -n $ns route add 2001:db8:1::/64 dev dummy1 metric 1024
	fib6_trap_check $ns "2001:db8:1::/64 dev dummy1 metric 1024" false
	check_err $? "Route not in hardware when should"

	ip -n $ns route replace 2001:db8:1::/64 dev dummy2 metric 1024
	fib6_trap_check $ns "2001:db8:1::/64 dev dummy2 metric 1024" false
	check_err $? "Replacement route not in hardware when should"

	# Add a route with an higher metric and make sure that replacing it
	# does not affect the lower metric one.
	ip -n $ns route add 2001:db8:1::/64 dev dummy1 metric 1025
	ip -n $ns route replace 2001:db8:1::/64 dev dummy2 metric 1025

	fib6_trap_check $ns "2001:db8:1::/64 dev dummy2 metric 1024" false
	check_err $? "Lowest metric route not in hardware when should"
	fib6_trap_check $ns "2001:db8:1::/64 dev dummy2 metric 1025" true
	check_err $? "Highest metric route in hardware when should not"

	log_test "IPv6 single route replace"

	for i in $(seq 1 2); do
		ip -n $ns link del dev dummy$i
	done
}

fib_ipv6_metric_multipath_test()
{
	local ns=$1; shift

	RET=0

	for i in $(seq 1 2); do
		ip -n $ns link add name dummy$i type dummy
		ip -n $ns link set dev dummy$i up
		ip -n $ns address add 2001:db8:$i::1/64 dev dummy$i
	done

	ip -n $ns route add 2001:db8:10::/64 metric 1024 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:2::2 dev dummy2
	fib6_trap_check $ns "2001:db8:10::/64 metric 1024" false
	check_err $? "Route not in hardware when should"

	ip -n $ns route add 2001:db8:10::/64 metric 1022 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:2::2 dev dummy2
	fib6_trap_check $ns "2001:db8:10::/64 metric 1022" false
	check_err $? "Lowest metric route not in hardware when should"

	ip -n $ns route add 2001:db8:10::/64 metric 1023 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:2::2 dev dummy2
	fib6_trap_check $ns "2001:db8:10::/64 metric 1024" true
	check_err $? "Highest metric route still in hardware when should not"

	fib6_trap_check $ns "2001:db8:10::/64 metric 1023" true
	check_err $? "Middle metric route in hardware when should not"

	log_test "IPv6 multipath routes with metric"

	for i in $(seq 1 2); do
		ip -n $ns link del dev dummy$i
	done
}

fib_ipv6_append_multipath_test()
{
	local ns=$1; shift

	RET=0

	for i in $(seq 1 3); do
		ip -n $ns link add name dummy$i type dummy
		ip -n $ns link set dev dummy$i up
		ip -n $ns address add 2001:db8:$i::1/64 dev dummy$i
	done

	ip -n $ns route add 2001:db8:10::/64 metric 1024 \
		nexthop via 2001:db8:1::2 dev dummy1
	fib6_trap_check $ns "2001:db8:10::/64 metric 1024" false
	check_err $? "Route not in hardware when should"

	ip -n $ns route append 2001:db8:10::/64 metric 1024 \
		nexthop via 2001:db8:2::2 dev dummy2 \
		nexthop via 2001:db8:3::2 dev dummy3
	fib6_trap_check $ns "2001:db8:10::/64 metric 1024" false
	check_err $? "Route not in hardware after appending"

	ip -n $ns route add 2001:db8:10::/64 metric 1025 \
		nexthop via 2001:db8:1::2 dev dummy1
	fib6_trap_check $ns "2001:db8:10::/64 metric 1025" true
	check_err $? "Route in hardware when should not"

	ip -n $ns route append 2001:db8:10::/64 metric 1025 \
		nexthop via 2001:db8:2::2 dev dummy2 \
		nexthop via 2001:db8:3::2 dev dummy3
	fib6_trap_check $ns "2001:db8:10::/64 metric 1025" true
	check_err $? "Route in hardware when should not after appending"

	fib6_trap_check $ns "2001:db8:10::/64 metric 1024" false
	check_err $? "Lowest metric route not in hardware when should"

	log_test "IPv6 append multipath route with 'nexthop' keyword"

	for i in $(seq 1 3); do
		ip -n $ns link del dev dummy$i
	done
}

fib_ipv6_replace_multipath_test()
{
	local ns=$1; shift
	local i

	RET=0

	for i in $(seq 1 3); do
		ip -n $ns link add name dummy$i type dummy
		ip -n $ns link set dev dummy$i up
		ip -n $ns address add 2001:db8:$i::1/64 dev dummy$i
	done

	ip -n $ns route add 2001:db8:10::/64 metric 1024 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:2::2 dev dummy2
	fib6_trap_check $ns "2001:db8:10::/64 metric 1024" false
	check_err $? "Route not in hardware when should"

	ip -n $ns route replace 2001:db8:10::/64 metric 1024 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:3::2 dev dummy3
	fib6_trap_check $ns "2001:db8:10::/64 metric 1024" false
	check_err $? "Replacement route not in hardware when should"

	# Add a route with an higher metric and make sure that replacing it
	# does not affect the lower metric one.
	ip -n $ns route add 2001:db8:10::/64 metric 1025 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:2::2 dev dummy2
	ip -n $ns route replace 2001:db8:10::/64 metric 1025 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:3::2 dev dummy3

	fib6_trap_check $ns "2001:db8:10::/64 metric 1024" false
	check_err $? "Lowest metric route not in hardware when should"
	fib6_trap_check $ns "2001:db8:10::/64 metric 1025" true
	check_err $? "Highest metric route in hardware when should not"

	log_test "IPv6 multipath route replace"

	for i in $(seq 1 3); do
		ip -n $ns link del dev dummy$i
	done
}

fib_ipv6_append_multipath_to_single_test()
{
	local ns=$1; shift

	# Test that when the first route in the leaf is not a multipath route
	# and we try to append a multipath route with the same metric to it, it
	# is not notified.
	RET=0

	for i in $(seq 1 2); do
		ip -n $ns link add name dummy$i type dummy
		ip -n $ns link set dev dummy$i up
		ip -n $ns address add 2001:db8:$i::1/64 dev dummy$i
	done

	ip -n $ns route add 2001:db8:10::/64 dev dummy1 metric 1024
	fib6_trap_check $ns "2001:db8:10::/64 dev dummy1 metric 1024" false
	check_err $? "Route not in hardware when should"

	ip -n $ns route append 2001:db8:10::/64 metric 1024 \
		nexthop via 2001:db8:2::2 dev dummy2
	fib6_trap_check $ns "2001:db8:10::/64 dev dummy2 metric 1024" true
	check_err $? "Route in hardware when should not"

	fib6_trap_check $ns "2001:db8:10::/64 dev dummy1 metric 1024" false
	check_err $? "Route not in hardware after append"

	log_test "IPv6 append multipath route to non-multipath route"

	for i in $(seq 1 2); do
		ip -n $ns link del dev dummy$i
	done
}

fib_ipv6_delete_single_test()
{
	local ns=$1; shift

	# Test various deletion scenarios, where only a single route is
	# deleted from the FIB node.
	for i in $(seq 1 2); do
		ip -n $ns link add name dummy$i type dummy
		ip -n $ns link set dev dummy$i up
		ip -n $ns address add 2001:db8:$i::1/64 dev dummy$i
	done

	# Test deletion of a single route when it is the only route in the FIB
	# node.
	RET=0

	ip -n $ns route add 2001:db8:10::/64 dev dummy1 metric 1024
	ip -n $ns route del 2001:db8:10::/64 dev dummy1 metric 1024

	log_test "IPv6 delete sole single route"

	# Test that deletion of last route does not affect the first one.
	RET=0

	ip -n $ns route add 2001:db8:10::/64 dev dummy1 metric 1024
	ip -n $ns route add 2001:db8:10::/64 dev dummy1 metric 1025
	ip -n $ns route del 2001:db8:10::/64 dev dummy1 metric 1025

	fib6_trap_check $ns "2001:db8:10::/64 dev dummy1 metric 1024" false
	check_err $? "Route not in hardware after deleting higher metric route"

	log_test "IPv6 delete single route not in hardware"

	ip -n $ns route del 2001:db8:10::/64 dev dummy1 metric 1024

	# Test that first route is replaced by next single route in the FIB
	# node.
	RET=0

	ip -n $ns route add 2001:db8:10::/64 dev dummy1 metric 1024
	ip -n $ns route add 2001:db8:10::/64 dev dummy1 metric 1025
	ip -n $ns route del 2001:db8:10::/64 dev dummy1 metric 1024

	fib6_trap_check $ns "2001:db8:10::/64 dev dummy1 metric 1025" false
	check_err $? "Route not in hardware after deleting lowest metric route"

	log_test "IPv6 delete single route - replaced by single"

	ip -n $ns route del 2001:db8:10::/64 dev dummy1 metric 1025

	# Test that first route is replaced by next multipath route in the FIB
	# node.
	RET=0

	ip -n $ns route add 2001:db8:10::/64 dev dummy1 metric 1024
	ip -n $ns route add 2001:db8:10::/64 metric 1025 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:2::2 dev dummy2
	ip -n $ns route del 2001:db8:10::/64 dev dummy1 metric 1024

	fib6_trap_check $ns "2001:db8:10::/64 metric 1025" false
	check_err $? "Route not in hardware after deleting lowest metric route"

	log_test "IPv6 delete single route - replaced by multipath"

	ip -n $ns route del 2001:db8:10::/64 metric 1025

	# Test deletion of a single nexthop from a multipath route.
	ip -n $ns route add 2001:db8:10::/64 metric 1024 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:2::2 dev dummy2
	ip -n $ns route del 2001:db8:10::/64 metric 1024 \
		nexthop via 2001:db8:1::2 dev dummy1

	fib6_trap_check $ns "2001:db8:10::/64 metric 1024" false
	check_err $? "Route not in hardware after deleting a single nexthop"

	log_test "IPv6 delete single nexthop"

	ip -n $ns route del 2001:db8:10::/64 metric 1024

	for i in $(seq 1 2); do
		ip -n $ns link del dev dummy$i
	done
}

fib_ipv6_delete_multipath_test()
{
	local ns=$1; shift

	# Test various deletion scenarios, where an entire multipath route is
	# deleted from the FIB node.
	for i in $(seq 1 2); do
		ip -n $ns link add name dummy$i type dummy
		ip -n $ns link set dev dummy$i up
		ip -n $ns address add 2001:db8:$i::1/64 dev dummy$i
	done

	# Test deletion of a multipath route when it is the only route in the
	# FIB node.
	RET=0

	ip -n $ns route add 2001:db8:10::/64 metric 1024 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:2::2 dev dummy2
	ip -n $ns route del 2001:db8:10::/64 metric 1024

	log_test "IPv6 delete sole multipath route"

	# Test that deletion of last route does not affect the first one.
	RET=0

	ip -n $ns route add 2001:db8:10::/64 metric 1024 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:2::2 dev dummy2
	ip -n $ns route add 2001:db8:10::/64 metric 1025 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:2::2 dev dummy2
	ip -n $ns route del 2001:db8:10::/64 metric 1025

	fib6_trap_check $ns "2001:db8:10::/64 metric 1024" false
	check_err $? "Route not in hardware after deleting higher metric route"

	log_test "IPv6 delete multipath route not in hardware"

	ip -n $ns route del 2001:db8:10::/64 metric 1024

	# Test that first route is replaced by next single route in the FIB
	# node.
	RET=0

	ip -n $ns route add 2001:db8:10::/64 metric 1024 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:2::2 dev dummy2
	ip -n $ns route add 2001:db8:10::/64 dev dummy1 metric 1025
	ip -n $ns route del 2001:db8:10::/64 metric 1024

	fib6_trap_check $ns "2001:db8:10::/64 dev dummy1 metric 1025" false
	check_err $? "Route not in hardware after deleting lowest metric route"

	log_test "IPv6 delete multipath route - replaced by single"

	ip -n $ns route del 2001:db8:10::/64 dev dummy1 metric 1025

	# Test that first route is replaced by next multipath route in the FIB
	# node.
	RET=0

	ip -n $ns route add 2001:db8:10::/64 metric 1024 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:2::2 dev dummy2
	ip -n $ns route add 2001:db8:10::/64 metric 1025 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:2::2 dev dummy2
	ip -n $ns route del 2001:db8:10::/64 metric 1024

	fib6_trap_check $ns "2001:db8:10::/64 metric 1025" false
	check_err $? "Route not in hardware after deleting lowest metric route"

	log_test "IPv6 delete multipath route - replaced by multipath"

	ip -n $ns route del 2001:db8:10::/64 metric 1025

	for i in $(seq 1 2); do
		ip -n $ns link del dev dummy$i
	done
}

fib_ipv6_replay_single_test()
{
	local ns=$1; shift
	local devlink_dev=$1; shift

	RET=0

	for i in $(seq 1 2); do
		ip -n $ns link add name dummy$i type dummy
		ip -n $ns link set dev dummy$i up
	done

	ip -n $ns route add 2001:db8:1::/64 dev dummy1
	ip -n $ns route append 2001:db8:1::/64 dev dummy2

	devlink -N $ns dev reload $devlink_dev

	fib6_trap_check $ns "2001:db8:1::/64 dev dummy1" false
	check_err $? "First route not in hardware when should"

	fib6_trap_check $ns "2001:db8:1::/64 dev dummy2" true
	check_err $? "Second route in hardware when should not"

	log_test "IPv6 routes replay - single route"

	for i in $(seq 1 2); do
		ip -n $ns link del dev dummy$i
	done
}

fib_ipv6_replay_multipath_test()
{
	local ns=$1; shift
	local devlink_dev=$1; shift

	RET=0

	for i in $(seq 1 2); do
		ip -n $ns link add name dummy$i type dummy
		ip -n $ns link set dev dummy$i up
		ip -n $ns address add 2001:db8:$i::1/64 dev dummy$i
	done

	ip -n $ns route add 2001:db8:10::/64 metric 1024 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:2::2 dev dummy2
	ip -n $ns route add 2001:db8:10::/64 metric 1025 \
		nexthop via 2001:db8:1::2 dev dummy1 \
		nexthop via 2001:db8:2::2 dev dummy2

	devlink -N $ns dev reload $devlink_dev

	fib6_trap_check $ns "2001:db8:10::/64 metric 1024" false
	check_err $? "First route not in hardware when should"

	fib6_trap_check $ns "2001:db8:10::/64 metric 1025" true
	check_err $? "Second route in hardware when should not"

	log_test "IPv6 routes replay - multipath route"

	for i in $(seq 1 2); do
		ip -n $ns link del dev dummy$i
	done
}