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 -q -p 64 -A 192.0.2.2 -B 198.51.100.2 \
182	       -d 1msec -t udp "sp=1024,dp=0-32768"
183
184       t1_rp12=$(link_stats_tx_packets_get $rp12)
185       t1_rp13=$(link_stats_tx_packets_get $rp13)
186
187       let "packets_rp12 = $t1_rp12 - $t0_rp12"
188       let "packets_rp13 = $t1_rp13 - $t0_rp13"
189       multipath_eval "$desc" $weight_rp12 $weight_rp13 $packets_rp12 $packets_rp13
190
191       # Restore settings.
192       ip route replace 198.51.100.0/24 vrf vrf-r1 \
193               nexthop via 169.254.2.22 dev $rp12 \
194               nexthop via 169.254.3.23 dev $rp13
195       sysctl_restore net.ipv4.fib_multipath_hash_policy
196}
197
198multipath6_l4_test()
199{
200       local desc="$1"
201       local weight_rp12=$2
202       local weight_rp13=$3
203       local t0_rp12 t0_rp13 t1_rp12 t1_rp13
204       local packets_rp12 packets_rp13
205
206       # Transmit multiple flows from h1 to h2 and make sure they are
207       # distributed between both multipath links (rp12 and rp13)
208       # according to the configured weights.
209       sysctl_set net.ipv6.fib_multipath_hash_policy 1
210
211       ip route replace 2001:db8:2::/64 vrf vrf-r1 \
212	       nexthop via fe80:2::22 dev $rp12 weight $weight_rp12 \
213	       nexthop via fe80:3::23 dev $rp13 weight $weight_rp13
214
215       t0_rp12=$(link_stats_tx_packets_get $rp12)
216       t0_rp13=$(link_stats_tx_packets_get $rp13)
217
218       $MZ $h1 -6 -q -p 64 -A 2001:db8:1::2 -B 2001:db8:2::2 \
219	       -d 1msec -t udp "sp=1024,dp=0-32768"
220
221       t1_rp12=$(link_stats_tx_packets_get $rp12)
222       t1_rp13=$(link_stats_tx_packets_get $rp13)
223
224       let "packets_rp12 = $t1_rp12 - $t0_rp12"
225       let "packets_rp13 = $t1_rp13 - $t0_rp13"
226       multipath_eval "$desc" $weight_rp12 $weight_rp13 $packets_rp12 $packets_rp13
227
228       ip route replace 2001:db8:2::/64 vrf vrf-r1 \
229	       nexthop via fe80:2::22 dev $rp12 \
230	       nexthop via fe80:3::23 dev $rp13
231
232       sysctl_restore net.ipv6.fib_multipath_hash_policy
233}
234
235multipath6_test()
236{
237       local desc="$1"
238       local weight_rp12=$2
239       local weight_rp13=$3
240       local t0_rp12 t0_rp13 t1_rp12 t1_rp13
241       local packets_rp12 packets_rp13
242
243       ip route replace 2001:db8:2::/64 vrf vrf-r1 \
244	       nexthop via fe80:2::22 dev $rp12 weight $weight_rp12 \
245	       nexthop via fe80:3::23 dev $rp13 weight $weight_rp13
246
247       t0_rp12=$(link_stats_tx_packets_get $rp12)
248       t0_rp13=$(link_stats_tx_packets_get $rp13)
249
250       # Generate 16384 echo requests, each with a random flow label.
251       for _ in $(seq 1 16384); do
252	       ip vrf exec vrf-h1 $PING6 2001:db8:2::2 -F 0 -c 1 -q &> /dev/null
253       done
254
255       t1_rp12=$(link_stats_tx_packets_get $rp12)
256       t1_rp13=$(link_stats_tx_packets_get $rp13)
257
258       let "packets_rp12 = $t1_rp12 - $t0_rp12"
259       let "packets_rp13 = $t1_rp13 - $t0_rp13"
260       multipath_eval "$desc" $weight_rp12 $weight_rp13 $packets_rp12 $packets_rp13
261
262       ip route replace 2001:db8:2::/64 vrf vrf-r1 \
263	       nexthop via fe80:2::22 dev $rp12 \
264	       nexthop via fe80:3::23 dev $rp13
265}
266
267multipath_test()
268{
269	log_info "Running IPv4 multipath tests"
270	multipath4_test "ECMP" 1 1
271	multipath4_test "Weighted MP 2:1" 2 1
272	multipath4_test "Weighted MP 11:45" 11 45
273
274	log_info "Running IPv6 multipath tests"
275	multipath6_test "ECMP" 1 1
276	multipath6_test "Weighted MP 2:1" 2 1
277	multipath6_test "Weighted MP 11:45" 11 45
278
279	log_info "Running IPv6 L4 hash multipath tests"
280	multipath6_l4_test "ECMP" 1 1
281	multipath6_l4_test "Weighted MP 2:1" 2 1
282	multipath6_l4_test "Weighted MP 11:45" 11 45
283}
284
285setup_prepare()
286{
287	h1=${NETIFS[p1]}
288	rp11=${NETIFS[p2]}
289
290	rp12=${NETIFS[p3]}
291	rp22=${NETIFS[p4]}
292
293	rp13=${NETIFS[p5]}
294	rp23=${NETIFS[p6]}
295
296	rp21=${NETIFS[p7]}
297	h2=${NETIFS[p8]}
298
299	vrf_prepare
300
301	h1_create
302	h2_create
303
304	router1_create
305	router2_create
306
307	forwarding_enable
308}
309
310cleanup()
311{
312	pre_cleanup
313
314	forwarding_restore
315
316	router2_destroy
317	router1_destroy
318
319	h2_destroy
320	h1_destroy
321
322	vrf_cleanup
323}
324
325ping_ipv4()
326{
327	ping_test $h1 198.51.100.2
328}
329
330ping_ipv6()
331{
332	ping6_test $h1 2001:db8:2::2
333}
334
335trap cleanup EXIT
336
337setup_prepare
338setup_wait
339
340tests_run
341
342exit $EXIT_STATUS
343