xref: /linux/tools/testing/selftests/net/forwarding/router_mpath_nh.sh (revision 9410645520e9b820069761f3450ef6661418e279)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# +-------------------------+
5# |  H1                     |
6# |               $h1 +     |
7# |      192.0.2.2/24 |     |
8# |  2001:db8:1::2/64 |     |
9# +-------------------|-----+
10#                     |
11# +-------------------|----------------------+
12# |                   |                   R1 |
13# |             $rp11 +                      |
14# |      192.0.2.1/24                        |
15# |  2001:db8:1::1/64                        |
16# |                                          |
17# |  + $rp12              + $rp13            |
18# |  | 169.254.2.12/24    | 169.254.3.13/24  |
19# |  | fe80:2::12/64      | fe80:3::13/64    |
20# +--|--------------------|------------------+
21#    |                    |
22# +--|--------------------|------------------+
23# |  + $rp22              + $rp23            |
24# |    169.254.2.22/24      169.254.3.23/24  |
25# |    fe80:2::22/64        fe80:3::23/64    |
26# |                                          |
27# |             $rp21 +                      |
28# |   198.51.100.1/24 |                      |
29# |  2001:db8:2::1/64 |                   R2 |
30# +-------------------|----------------------+
31#                     |
32# +-------------------|-----+
33# |                   |     |
34# |               $h2 +     |
35# |   198.51.100.2/24       |
36# |  2001:db8:2::2/64    H2 |
37# +-------------------------+
38
39ALL_TESTS="
40	ping_ipv4
41	ping_ipv6
42	multipath_test
43	multipath16_test
44	ping_ipv4_blackhole
45	ping_ipv6_blackhole
46	nh_stats_test_v4
47	nh_stats_test_v6
48"
49NUM_NETIFS=8
50source lib.sh
51source router_mpath_nh_lib.sh
52
53h1_create()
54{
55	vrf_create "vrf-h1"
56	ip link set dev $h1 master vrf-h1
57
58	ip link set dev vrf-h1 up
59	ip link set dev $h1 up
60
61	ip address add 192.0.2.2/24 dev $h1
62	ip address add 2001:db8:1::2/64 dev $h1
63
64	ip route add 198.51.100.0/24 vrf vrf-h1 nexthop via 192.0.2.1
65	ip route add 2001:db8:2::/64 vrf vrf-h1 nexthop via 2001:db8:1::1
66}
67
68h1_destroy()
69{
70	ip route del 2001:db8:2::/64 vrf vrf-h1
71	ip route del 198.51.100.0/24 vrf vrf-h1
72
73	ip address del 2001:db8:1::2/64 dev $h1
74	ip address del 192.0.2.2/24 dev $h1
75
76	ip link set dev $h1 down
77	vrf_destroy "vrf-h1"
78}
79
80h2_create()
81{
82	vrf_create "vrf-h2"
83	ip link set dev $h2 master vrf-h2
84
85	ip link set dev vrf-h2 up
86	ip link set dev $h2 up
87
88	ip address add 198.51.100.2/24 dev $h2
89	ip address add 2001:db8:2::2/64 dev $h2
90
91	ip route add 192.0.2.0/24 vrf vrf-h2 nexthop via 198.51.100.1
92	ip route add 2001:db8:1::/64 vrf vrf-h2 nexthop via 2001:db8:2::1
93}
94
95h2_destroy()
96{
97	ip route del 2001:db8:1::/64 vrf vrf-h2
98	ip route del 192.0.2.0/24 vrf vrf-h2
99
100	ip address del 2001:db8:2::2/64 dev $h2
101	ip address del 198.51.100.2/24 dev $h2
102
103	ip link set dev $h2 down
104	vrf_destroy "vrf-h2"
105}
106
107router1_create()
108{
109	vrf_create "vrf-r1"
110	ip link set dev $rp11 master vrf-r1
111	ip link set dev $rp12 master vrf-r1
112	ip link set dev $rp13 master vrf-r1
113
114	ip link set dev vrf-r1 up
115	ip link set dev $rp11 up
116	ip link set dev $rp12 up
117	ip link set dev $rp13 up
118
119	ip address add 192.0.2.1/24 dev $rp11
120	ip address add 2001:db8:1::1/64 dev $rp11
121
122	ip address add 169.254.2.12/24 dev $rp12
123	ip address add fe80:2::12/64 dev $rp12
124
125	ip address add 169.254.3.13/24 dev $rp13
126	ip address add fe80:3::13/64 dev $rp13
127}
128
129router1_destroy()
130{
131	ip route del 2001:db8:2::/64 vrf vrf-r1
132	ip route del 198.51.100.0/24 vrf vrf-r1
133
134	ip address del fe80:3::13/64 dev $rp13
135	ip address del 169.254.3.13/24 dev $rp13
136
137	ip address del fe80:2::12/64 dev $rp12
138	ip address del 169.254.2.12/24 dev $rp12
139
140	ip address del 2001:db8:1::1/64 dev $rp11
141	ip address del 192.0.2.1/24 dev $rp11
142
143	ip nexthop del id 103
144	ip nexthop del id 101
145	ip nexthop del id 102
146	ip nexthop del id 106
147	ip nexthop del id 104
148	ip nexthop del id 105
149
150	ip link set dev $rp13 down
151	ip link set dev $rp12 down
152	ip link set dev $rp11 down
153
154	vrf_destroy "vrf-r1"
155}
156
157router2_create()
158{
159	vrf_create "vrf-r2"
160	ip link set dev $rp21 master vrf-r2
161	ip link set dev $rp22 master vrf-r2
162	ip link set dev $rp23 master vrf-r2
163
164	ip link set dev vrf-r2 up
165	ip link set dev $rp21 up
166	ip link set dev $rp22 up
167	ip link set dev $rp23 up
168
169	ip address add 198.51.100.1/24 dev $rp21
170	ip address add 2001:db8:2::1/64 dev $rp21
171
172	ip address add 169.254.2.22/24 dev $rp22
173	ip address add fe80:2::22/64 dev $rp22
174
175	ip address add 169.254.3.23/24 dev $rp23
176	ip address add fe80:3::23/64 dev $rp23
177}
178
179router2_destroy()
180{
181	ip route del 2001:db8:1::/64 vrf vrf-r2
182	ip route del 192.0.2.0/24 vrf vrf-r2
183
184	ip address del fe80:3::23/64 dev $rp23
185	ip address del 169.254.3.23/24 dev $rp23
186
187	ip address del fe80:2::22/64 dev $rp22
188	ip address del 169.254.2.22/24 dev $rp22
189
190	ip address del 2001:db8:2::1/64 dev $rp21
191	ip address del 198.51.100.1/24 dev $rp21
192
193	ip nexthop del id 201
194	ip nexthop del id 202
195	ip nexthop del id 204
196	ip nexthop del id 205
197
198	ip link set dev $rp23 down
199	ip link set dev $rp22 down
200	ip link set dev $rp21 down
201
202	vrf_destroy "vrf-r2"
203}
204
205routing_nh_obj()
206{
207	ip nexthop add id 101 via 169.254.2.22 dev $rp12
208	ip nexthop add id 102 via 169.254.3.23 dev $rp13
209	ip nexthop add id 103 group 101/102
210	ip route add 198.51.100.0/24 vrf vrf-r1 nhid 103
211
212	ip nexthop add id 104 via fe80:2::22 dev $rp12
213	ip nexthop add id 105 via fe80:3::23 dev $rp13
214	ip nexthop add id 106 group 104/105
215	ip route add 2001:db8:2::/64 vrf vrf-r1 nhid 106
216
217	ip nexthop add id 201 via 169.254.2.12 dev $rp22
218	ip nexthop add id 202 via 169.254.3.13 dev $rp23
219	ip nexthop add id 203 group 201/202
220	ip route add 192.0.2.0/24 vrf vrf-r2 nhid 203
221
222	ip nexthop add id 204 via fe80:2::12 dev $rp22
223	ip nexthop add id 205 via fe80:3::13 dev $rp23
224	ip nexthop add id 206 group 204/205
225	ip route add 2001:db8:1::/64 vrf vrf-r2 nhid 206
226}
227
228multipath4_test()
229{
230	local desc=$1; shift
231	local weight_rp12=$1; shift
232	local weight_rp13=$1; shift
233	local ports=${1-sp=1024,dp=0-32768}; shift
234
235	local t0_rp12 t0_rp13 t1_rp12 t1_rp13
236	local packets_rp12 packets_rp13
237
238	# Transmit multiple flows from h1 to h2 and make sure they are
239	# distributed between both multipath links (rp12 and rp13)
240	# according to the configured weights.
241	sysctl_set net.ipv4.fib_multipath_hash_policy 1
242	ip nexthop replace id 103 group 101,$weight_rp12/102,$weight_rp13
243
244	t0_rp12=$(link_stats_tx_packets_get $rp12)
245	t0_rp13=$(link_stats_tx_packets_get $rp13)
246
247	ip vrf exec vrf-h1 $MZ $h1 -q -p 64 -A 192.0.2.2 -B 198.51.100.2 \
248		-d $MZ_DELAY -t udp "$ports"
249	sleep 1
250
251	t1_rp12=$(link_stats_tx_packets_get $rp12)
252	t1_rp13=$(link_stats_tx_packets_get $rp13)
253
254	let "packets_rp12 = $t1_rp12 - $t0_rp12"
255	let "packets_rp13 = $t1_rp13 - $t0_rp13"
256	multipath_eval "$desc" $weight_rp12 $weight_rp13 $packets_rp12 $packets_rp13
257
258	# Restore settings.
259	ip nexthop replace id 103 group 101/102
260	sysctl_restore net.ipv4.fib_multipath_hash_policy
261}
262
263multipath6_test()
264{
265	local desc=$1; shift
266	local weight_rp12=$1; shift
267	local weight_rp13=$1; shift
268	local ports=${1-sp=1024,dp=0-32768}; shift
269
270	local t0_rp12 t0_rp13 t1_rp12 t1_rp13
271	local packets_rp12 packets_rp13
272
273	# Transmit multiple flows from h1 to h2 and make sure they are
274	# distributed between both multipath links (rp12 and rp13)
275	# according to the configured weights.
276	sysctl_set net.ipv6.fib_multipath_hash_policy 1
277
278	ip nexthop replace id 106 group 104,$weight_rp12/105,$weight_rp13
279
280	t0_rp12=$(link_stats_tx_packets_get $rp12)
281	t0_rp13=$(link_stats_tx_packets_get $rp13)
282
283	$MZ $h1 -6 -q -p 64 -A 2001:db8:1::2 -B 2001:db8:2::2 \
284		-d $MZ_DELAY -t udp "$ports"
285	sleep 1
286
287	t1_rp12=$(link_stats_tx_packets_get $rp12)
288	t1_rp13=$(link_stats_tx_packets_get $rp13)
289
290	let "packets_rp12 = $t1_rp12 - $t0_rp12"
291	let "packets_rp13 = $t1_rp13 - $t0_rp13"
292	multipath_eval "$desc" $weight_rp12 $weight_rp13 $packets_rp12 $packets_rp13
293
294	ip nexthop replace id 106 group 104/105
295
296	sysctl_restore net.ipv6.fib_multipath_hash_policy
297}
298
299multipath_test()
300{
301	log_info "Running IPv4 multipath tests"
302	multipath4_test "ECMP" 1 1
303	multipath4_test "Weighted MP 2:1" 2 1
304	multipath4_test "Weighted MP 11:45" 11 45
305
306	log_info "Running IPv4 multipath tests with IPv6 link-local nexthops"
307	ip nexthop replace id 101 via fe80:2::22 dev $rp12
308	ip nexthop replace id 102 via fe80:3::23 dev $rp13
309
310	multipath4_test "ECMP" 1 1
311	multipath4_test "Weighted MP 2:1" 2 1
312	multipath4_test "Weighted MP 11:45" 11 45
313
314	ip nexthop replace id 102 via 169.254.3.23 dev $rp13
315	ip nexthop replace id 101 via 169.254.2.22 dev $rp12
316
317	log_info "Running IPv6 multipath tests"
318	multipath6_test "ECMP" 1 1
319	multipath6_test "Weighted MP 2:1" 2 1
320	multipath6_test "Weighted MP 11:45" 11 45
321}
322
323multipath16_test()
324{
325	check_nhgw16 104 || return
326
327	log_info "Running 16-bit IPv4 multipath tests"
328	multipath4_test "65535:65535" 65535 65535
329	multipath4_test "128:512" 128 512
330	omit_on_slow \
331		multipath4_test "255:65535" 255 65535 sp=1024-1026,dp=0-65535
332
333	log_info "Running 16-bit IPv6 multipath tests"
334	multipath6_test "65535:65535" 65535 65535
335	multipath6_test "128:512" 128 512
336	omit_on_slow \
337		multipath6_test "255:65535" 255 65535 sp=1024-1026,dp=0-65535
338}
339
340ping_ipv4_blackhole()
341{
342	RET=0
343
344	ip nexthop add id 1001 blackhole
345	ip nexthop add id 1002 group 1001
346
347	ip route replace 198.51.100.0/24 vrf vrf-r1 nhid 1001
348	ping_do $h1 198.51.100.2
349	check_fail $? "ping did not fail when using a blackhole nexthop"
350
351	ip route replace 198.51.100.0/24 vrf vrf-r1 nhid 1002
352	ping_do $h1 198.51.100.2
353	check_fail $? "ping did not fail when using a blackhole nexthop group"
354
355	ip route replace 198.51.100.0/24 vrf vrf-r1 nhid 103
356	ping_do $h1 198.51.100.2
357	check_err $? "ping failed with a valid nexthop"
358
359	log_test "IPv4 blackhole ping"
360
361	ip nexthop del id 1002
362	ip nexthop del id 1001
363}
364
365ping_ipv6_blackhole()
366{
367	RET=0
368
369	ip -6 nexthop add id 1001 blackhole
370	ip nexthop add id 1002 group 1001
371
372	ip route replace 2001:db8:2::/64 vrf vrf-r1 nhid 1001
373	ping6_do $h1 2001:db8:2::2
374	check_fail $? "ping did not fail when using a blackhole nexthop"
375
376	ip route replace 2001:db8:2::/64 vrf vrf-r1 nhid 1002
377	ping6_do $h1 2001:db8:2::2
378	check_fail $? "ping did not fail when using a blackhole nexthop group"
379
380	ip route replace 2001:db8:2::/64 vrf vrf-r1 nhid 106
381	ping6_do $h1 2001:db8:2::2
382	check_err $? "ping failed with a valid nexthop"
383
384	log_test "IPv6 blackhole ping"
385
386	ip nexthop del id 1002
387	ip -6 nexthop del id 1001
388}
389
390nh_stats_test_v4()
391{
392	__nh_stats_test_v4 mpath
393}
394
395nh_stats_test_v6()
396{
397	__nh_stats_test_v6 mpath
398}
399
400setup_prepare()
401{
402	h1=${NETIFS[p1]}
403	rp11=${NETIFS[p2]}
404
405	rp12=${NETIFS[p3]}
406	rp22=${NETIFS[p4]}
407
408	rp13=${NETIFS[p5]}
409	rp23=${NETIFS[p6]}
410
411	rp21=${NETIFS[p7]}
412	h2=${NETIFS[p8]}
413
414	vrf_prepare
415
416	h1_create
417	h2_create
418
419	router1_create
420	router2_create
421
422	forwarding_enable
423}
424
425cleanup()
426{
427	pre_cleanup
428
429	forwarding_restore
430
431	router2_destroy
432	router1_destroy
433
434	h2_destroy
435	h1_destroy
436
437	vrf_cleanup
438}
439
440ping_ipv4()
441{
442	ping_test $h1 198.51.100.2
443}
444
445ping_ipv6()
446{
447	ping6_test $h1 2001:db8:2::2
448}
449
450ip nexthop ls >/dev/null 2>&1
451if [ $? -ne 0 ]; then
452	echo "Nexthop objects not supported; skipping tests"
453	exit $ksft_skip
454fi
455
456trap cleanup EXIT
457
458setup_prepare
459setup_wait
460routing_nh_obj
461
462tests_run
463
464exit $EXIT_STATUS
465