xref: /linux/tools/testing/selftests/net/forwarding/router_multipath.sh (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4ALL_TESTS="ping_ipv4 ping_ipv6 multipath_test"
5NUM_NETIFS=8
6source lib.sh
7
8h1_create()
9{
10	vrf_create "vrf-h1"
11	ip link set dev $h1 master vrf-h1
12
13	ip link set dev vrf-h1 up
14	ip link set dev $h1 up
15
16	ip address add 192.0.2.2/24 dev $h1
17	ip address add 2001:db8:1::2/64 dev $h1
18
19	ip route add 198.51.100.0/24 vrf vrf-h1 nexthop via 192.0.2.1
20	ip route add 2001:db8:2::/64 vrf vrf-h1 nexthop via 2001:db8:1::1
21}
22
23h1_destroy()
24{
25	ip route del 2001:db8:2::/64 vrf vrf-h1
26	ip route del 198.51.100.0/24 vrf vrf-h1
27
28	ip address del 2001:db8:1::2/64 dev $h1
29	ip address del 192.0.2.2/24 dev $h1
30
31	ip link set dev $h1 down
32	vrf_destroy "vrf-h1"
33}
34
35h2_create()
36{
37	vrf_create "vrf-h2"
38	ip link set dev $h2 master vrf-h2
39
40	ip link set dev vrf-h2 up
41	ip link set dev $h2 up
42
43	ip address add 198.51.100.2/24 dev $h2
44	ip address add 2001:db8:2::2/64 dev $h2
45
46	ip route add 192.0.2.0/24 vrf vrf-h2 nexthop via 198.51.100.1
47	ip route add 2001:db8:1::/64 vrf vrf-h2 nexthop via 2001:db8:2::1
48}
49
50h2_destroy()
51{
52	ip route del 2001:db8:1::/64 vrf vrf-h2
53	ip route del 192.0.2.0/24 vrf vrf-h2
54
55	ip address del 2001:db8:2::2/64 dev $h2
56	ip address del 198.51.100.2/24 dev $h2
57
58	ip link set dev $h2 down
59	vrf_destroy "vrf-h2"
60}
61
62router1_create()
63{
64	vrf_create "vrf-r1"
65	ip link set dev $rp11 master vrf-r1
66	ip link set dev $rp12 master vrf-r1
67	ip link set dev $rp13 master vrf-r1
68
69	ip link set dev vrf-r1 up
70	ip link set dev $rp11 up
71	ip link set dev $rp12 up
72	ip link set dev $rp13 up
73
74	ip address add 192.0.2.1/24 dev $rp11
75	ip address add 2001:db8:1::1/64 dev $rp11
76
77	ip address add 169.254.2.12/24 dev $rp12
78	ip address add fe80:2::12/64 dev $rp12
79
80	ip address add 169.254.3.13/24 dev $rp13
81	ip address add fe80:3::13/64 dev $rp13
82
83	ip route add 198.51.100.0/24 vrf vrf-r1 \
84		nexthop via 169.254.2.22 dev $rp12 \
85		nexthop via 169.254.3.23 dev $rp13
86	ip route add 2001:db8:2::/64 vrf vrf-r1 \
87		nexthop via fe80:2::22 dev $rp12 \
88		nexthop via fe80:3::23 dev $rp13
89}
90
91router1_destroy()
92{
93	ip route del 2001:db8:2::/64 vrf vrf-r1
94	ip route del 198.51.100.0/24 vrf vrf-r1
95
96	ip address del fe80:3::13/64 dev $rp13
97	ip address del 169.254.3.13/24 dev $rp13
98
99	ip address del fe80:2::12/64 dev $rp12
100	ip address del 169.254.2.12/24 dev $rp12
101
102	ip address del 2001:db8:1::1/64 dev $rp11
103	ip address del 192.0.2.1/24 dev $rp11
104
105	ip link set dev $rp13 down
106	ip link set dev $rp12 down
107	ip link set dev $rp11 down
108
109	vrf_destroy "vrf-r1"
110}
111
112router2_create()
113{
114	vrf_create "vrf-r2"
115	ip link set dev $rp21 master vrf-r2
116	ip link set dev $rp22 master vrf-r2
117	ip link set dev $rp23 master vrf-r2
118
119	ip link set dev vrf-r2 up
120	ip link set dev $rp21 up
121	ip link set dev $rp22 up
122	ip link set dev $rp23 up
123
124	ip address add 198.51.100.1/24 dev $rp21
125	ip address add 2001:db8:2::1/64 dev $rp21
126
127	ip address add 169.254.2.22/24 dev $rp22
128	ip address add fe80:2::22/64 dev $rp22
129
130	ip address add 169.254.3.23/24 dev $rp23
131	ip address add fe80:3::23/64 dev $rp23
132
133	ip route add 192.0.2.0/24 vrf vrf-r2 \
134		nexthop via 169.254.2.12 dev $rp22 \
135		nexthop via 169.254.3.13 dev $rp23
136	ip route add 2001:db8:1::/64 vrf vrf-r2 \
137		nexthop via fe80:2::12 dev $rp22 \
138		nexthop via fe80:3::13 dev $rp23
139}
140
141router2_destroy()
142{
143	ip route del 2001:db8:1::/64 vrf vrf-r2
144	ip route del 192.0.2.0/24 vrf vrf-r2
145
146	ip address del fe80:3::23/64 dev $rp23
147	ip address del 169.254.3.23/24 dev $rp23
148
149	ip address del fe80:2::22/64 dev $rp22
150	ip address del 169.254.2.22/24 dev $rp22
151
152	ip address del 2001:db8:2::1/64 dev $rp21
153	ip address del 198.51.100.1/24 dev $rp21
154
155	ip link set dev $rp23 down
156	ip link set dev $rp22 down
157	ip link set dev $rp21 down
158
159	vrf_destroy "vrf-r2"
160}
161
162multipath4_test()
163{
164       local desc="$1"
165       local weight_rp12=$2
166       local weight_rp13=$3
167       local t0_rp12 t0_rp13 t1_rp12 t1_rp13
168       local packets_rp12 packets_rp13
169
170       # Transmit multiple flows from h1 to h2 and make sure they are
171       # distributed between both multipath links (rp12 and rp13)
172       # according to the configured weights.
173       sysctl_set net.ipv4.fib_multipath_hash_policy 1
174       ip route replace 198.51.100.0/24 vrf vrf-r1 \
175               nexthop via 169.254.2.22 dev $rp12 weight $weight_rp12 \
176               nexthop via 169.254.3.23 dev $rp13 weight $weight_rp13
177
178       t0_rp12=$(link_stats_tx_packets_get $rp12)
179       t0_rp13=$(link_stats_tx_packets_get $rp13)
180
181       ip vrf exec vrf-h1 $MZ $h1 -q -p 64 -A 192.0.2.2 -B 198.51.100.2 \
182	       -d $MZ_DELAY -t udp "sp=1024,dp=0-32768"
183       sleep 1
184
185       t1_rp12=$(link_stats_tx_packets_get $rp12)
186       t1_rp13=$(link_stats_tx_packets_get $rp13)
187
188       let "packets_rp12 = $t1_rp12 - $t0_rp12"
189       let "packets_rp13 = $t1_rp13 - $t0_rp13"
190       multipath_eval "$desc" $weight_rp12 $weight_rp13 $packets_rp12 $packets_rp13
191
192       # Restore settings.
193       ip route replace 198.51.100.0/24 vrf vrf-r1 \
194               nexthop via 169.254.2.22 dev $rp12 \
195               nexthop via 169.254.3.23 dev $rp13
196       sysctl_restore net.ipv4.fib_multipath_hash_policy
197}
198
199multipath6_test()
200{
201       local desc="$1"
202       local weight_rp12=$2
203       local weight_rp13=$3
204       local t0_rp12 t0_rp13 t1_rp12 t1_rp13
205       local packets_rp12 packets_rp13
206
207       # Transmit multiple flows from h1 to h2 and make sure they are
208       # distributed between both multipath links (rp12 and rp13)
209       # according to the configured weights.
210       sysctl_set net.ipv6.fib_multipath_hash_policy 1
211
212       ip route replace 2001:db8:2::/64 vrf vrf-r1 \
213	       nexthop via fe80:2::22 dev $rp12 weight $weight_rp12 \
214	       nexthop via fe80:3::23 dev $rp13 weight $weight_rp13
215
216       t0_rp12=$(link_stats_tx_packets_get $rp12)
217       t0_rp13=$(link_stats_tx_packets_get $rp13)
218
219       $MZ $h1 -6 -q -p 64 -A 2001:db8:1::2 -B 2001:db8:2::2 \
220	       -d $MZ_DELAY -t udp "sp=1024,dp=0-32768"
221       sleep 1
222
223       t1_rp12=$(link_stats_tx_packets_get $rp12)
224       t1_rp13=$(link_stats_tx_packets_get $rp13)
225
226       let "packets_rp12 = $t1_rp12 - $t0_rp12"
227       let "packets_rp13 = $t1_rp13 - $t0_rp13"
228       multipath_eval "$desc" $weight_rp12 $weight_rp13 $packets_rp12 $packets_rp13
229
230       ip route replace 2001:db8:2::/64 vrf vrf-r1 \
231	       nexthop via fe80:2::22 dev $rp12 \
232	       nexthop via fe80:3::23 dev $rp13
233
234       sysctl_restore net.ipv6.fib_multipath_hash_policy
235}
236
237multipath_test()
238{
239	log_info "Running IPv4 multipath tests"
240	multipath4_test "ECMP" 1 1
241	multipath4_test "Weighted MP 2:1" 2 1
242	multipath4_test "Weighted MP 11:45" 11 45
243
244	log_info "Running IPv6 multipath tests"
245	multipath6_test "ECMP" 1 1
246	multipath6_test "Weighted MP 2:1" 2 1
247	multipath6_test "Weighted MP 11:45" 11 45
248}
249
250setup_prepare()
251{
252	h1=${NETIFS[p1]}
253	rp11=${NETIFS[p2]}
254
255	rp12=${NETIFS[p3]}
256	rp22=${NETIFS[p4]}
257
258	rp13=${NETIFS[p5]}
259	rp23=${NETIFS[p6]}
260
261	rp21=${NETIFS[p7]}
262	h2=${NETIFS[p8]}
263
264	vrf_prepare
265
266	h1_create
267	h2_create
268
269	router1_create
270	router2_create
271
272	forwarding_enable
273}
274
275cleanup()
276{
277	pre_cleanup
278
279	forwarding_restore
280
281	router2_destroy
282	router1_destroy
283
284	h2_destroy
285	h1_destroy
286
287	vrf_cleanup
288}
289
290ping_ipv4()
291{
292	ping_test $h1 198.51.100.2
293}
294
295ping_ipv6()
296{
297	ping6_test $h1 2001:db8:2::2
298}
299
300trap cleanup EXIT
301
302setup_prepare
303setup_wait
304
305tests_run
306
307exit $EXIT_STATUS
308