xref: /linux/tools/testing/selftests/net/forwarding/custom_multipath_hash.sh (revision 9410645520e9b820069761f3450ef6661418e279)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Test traffic distribution between two paths when using custom hash policy.
5#
6# +--------------------------------+
7# | H1                             |
8# |                     $h1 +      |
9# |   198.51.100.{2-253}/24 |      |
10# |   2001:db8:1::{2-fd}/64 |      |
11# +-------------------------|------+
12#                           |
13# +-------------------------|-------------------------+
14# | SW1                     |                         |
15# |                    $rp1 +                         |
16# |         198.51.100.1/24                           |
17# |        2001:db8:1::1/64                           |
18# |                                                   |
19# |                                                   |
20# |            $rp11 +             + $rp12            |
21# |     192.0.2.1/28 |             | 192.0.2.17/28    |
22# | 2001:db8:2::1/64 |             | 2001:db8:3::1/64 |
23# +------------------|-------------|------------------+
24#                    |             |
25# +------------------|-------------|------------------+
26# | SW2              |             |                  |
27# |                  |             |                  |
28# |            $rp21 +             + $rp22            |
29# |     192.0.2.2/28                 192.0.2.18/28    |
30# | 2001:db8:2::2/64                 2001:db8:3::2/64 |
31# |                                                   |
32# |                                                   |
33# |                    $rp2 +                         |
34# |          203.0.113.1/24 |                         |
35# |        2001:db8:4::1/64 |                         |
36# +-------------------------|-------------------------+
37#                           |
38# +-------------------------|------+
39# | H2                      |      |
40# |                     $h2 +      |
41# |    203.0.113.{2-253}/24        |
42# |   2001:db8:4::{2-fd}/64        |
43# +--------------------------------+
44
45ALL_TESTS="
46	ping_ipv4
47	ping_ipv6
48	custom_hash
49"
50
51NUM_NETIFS=8
52source lib.sh
53
54h1_create()
55{
56	simple_if_init $h1 198.51.100.2/24 2001:db8:1::2/64
57	ip route add vrf v$h1 default via 198.51.100.1 dev $h1
58	ip -6 route add vrf v$h1 default via 2001:db8:1::1 dev $h1
59}
60
61h1_destroy()
62{
63	ip -6 route del vrf v$h1 default
64	ip route del vrf v$h1 default
65	simple_if_fini $h1 198.51.100.2/24 2001:db8:1::2/64
66}
67
68sw1_create()
69{
70	simple_if_init $rp1 198.51.100.1/24 2001:db8:1::1/64
71	__simple_if_init $rp11 v$rp1 192.0.2.1/28 2001:db8:2::1/64
72	__simple_if_init $rp12 v$rp1 192.0.2.17/28 2001:db8:3::1/64
73
74	ip route add vrf v$rp1 203.0.113.0/24 \
75		nexthop via 192.0.2.2 dev $rp11 \
76		nexthop via 192.0.2.18 dev $rp12
77
78	ip -6 route add vrf v$rp1 2001:db8:4::/64 \
79		nexthop via 2001:db8:2::2 dev $rp11 \
80		nexthop via 2001:db8:3::2 dev $rp12
81}
82
83sw1_destroy()
84{
85	ip -6 route del vrf v$rp1 2001:db8:4::/64
86
87	ip route del vrf v$rp1 203.0.113.0/24
88
89	__simple_if_fini $rp12 192.0.2.17/28 2001:db8:3::1/64
90	__simple_if_fini $rp11 192.0.2.1/28 2001:db8:2::1/64
91	simple_if_fini $rp1 198.51.100.1/24 2001:db8:1::1/64
92}
93
94sw2_create()
95{
96	simple_if_init $rp2 203.0.113.1/24 2001:db8:4::1/64
97	__simple_if_init $rp21 v$rp2 192.0.2.2/28 2001:db8:2::2/64
98	__simple_if_init $rp22 v$rp2 192.0.2.18/28 2001:db8:3::2/64
99
100	ip route add vrf v$rp2 198.51.100.0/24 \
101		nexthop via 192.0.2.1 dev $rp21 \
102		nexthop via 192.0.2.17 dev $rp22
103
104	ip -6 route add vrf v$rp2 2001:db8:1::/64 \
105		nexthop via 2001:db8:2::1 dev $rp21 \
106		nexthop via 2001:db8:3::1 dev $rp22
107}
108
109sw2_destroy()
110{
111	ip -6 route del vrf v$rp2 2001:db8:1::/64
112
113	ip route del vrf v$rp2 198.51.100.0/24
114
115	__simple_if_fini $rp22 192.0.2.18/28 2001:db8:3::2/64
116	__simple_if_fini $rp21 192.0.2.2/28 2001:db8:2::2/64
117	simple_if_fini $rp2 203.0.113.1/24 2001:db8:4::1/64
118}
119
120h2_create()
121{
122	simple_if_init $h2 203.0.113.2/24 2001:db8:4::2/64
123	ip route add vrf v$h2 default via 203.0.113.1 dev $h2
124	ip -6 route add vrf v$h2 default via 2001:db8:4::1 dev $h2
125}
126
127h2_destroy()
128{
129	ip -6 route del vrf v$h2 default
130	ip route del vrf v$h2 default
131	simple_if_fini $h2 203.0.113.2/24 2001:db8:4::2/64
132}
133
134setup_prepare()
135{
136	h1=${NETIFS[p1]}
137
138	rp1=${NETIFS[p2]}
139
140	rp11=${NETIFS[p3]}
141	rp21=${NETIFS[p4]}
142
143	rp12=${NETIFS[p5]}
144	rp22=${NETIFS[p6]}
145
146	rp2=${NETIFS[p7]}
147
148	h2=${NETIFS[p8]}
149
150	vrf_prepare
151	h1_create
152	sw1_create
153	sw2_create
154	h2_create
155
156	forwarding_enable
157}
158
159cleanup()
160{
161	pre_cleanup
162
163	forwarding_restore
164
165	h2_destroy
166	sw2_destroy
167	sw1_destroy
168	h1_destroy
169	vrf_cleanup
170}
171
172ping_ipv4()
173{
174	ping_test $h1 203.0.113.2
175}
176
177ping_ipv6()
178{
179	ping6_test $h1 2001:db8:4::2
180}
181
182send_src_ipv4()
183{
184	ip vrf exec v$h1 $MZ $h1 -q -p 64 \
185		-A "198.51.100.2-198.51.100.253" -B 203.0.113.2 \
186		-d $MZ_DELAY -c 50 -t udp "sp=20000,dp=30000"
187}
188
189send_dst_ipv4()
190{
191	ip vrf exec v$h1 $MZ $h1 -q -p 64 \
192		-A 198.51.100.2 -B "203.0.113.2-203.0.113.253" \
193		-d $MZ_DELAY -c 50 -t udp "sp=20000,dp=30000"
194}
195
196send_src_udp4()
197{
198	ip vrf exec v$h1 $MZ $h1 -q -p 64 \
199		-A 198.51.100.2 -B 203.0.113.2 \
200		-d $MZ_DELAY -t udp "sp=0-32768,dp=30000"
201}
202
203send_dst_udp4()
204{
205	ip vrf exec v$h1 $MZ $h1 -q -p 64 \
206		-A 198.51.100.2 -B 203.0.113.2 \
207		-d $MZ_DELAY -t udp "sp=20000,dp=0-32768"
208}
209
210send_src_ipv6()
211{
212	ip vrf exec v$h1 $MZ -6 $h1 -q -p 64 \
213		-A "2001:db8:1::2-2001:db8:1::fd" -B 2001:db8:4::2 \
214		-d $MZ_DELAY -c 50 -t udp "sp=20000,dp=30000"
215}
216
217send_dst_ipv6()
218{
219	ip vrf exec v$h1 $MZ -6 $h1 -q -p 64 \
220		-A 2001:db8:1::2 -B "2001:db8:4::2-2001:db8:4::fd" \
221		-d $MZ_DELAY -c 50 -t udp "sp=20000,dp=30000"
222}
223
224send_flowlabel()
225{
226	# Generate 16384 echo requests, each with a random flow label.
227	ip vrf exec v$h1 sh -c \
228		"for _ in {1..16384}; do \
229			$PING6 2001:db8:4::2 -F 0 -c 1 -q >/dev/null 2>&1; \
230		done"
231}
232
233send_src_udp6()
234{
235	ip vrf exec v$h1 $MZ -6 $h1 -q -p 64 \
236		-A 2001:db8:1::2 -B 2001:db8:4::2 \
237		-d $MZ_DELAY -t udp "sp=0-32768,dp=30000"
238}
239
240send_dst_udp6()
241{
242	ip vrf exec v$h1 $MZ -6 $h1 -q -p 64 \
243		-A 2001:db8:1::2 -B 2001:db8:4::2 \
244		-d $MZ_DELAY -t udp "sp=20000,dp=0-32768"
245}
246
247custom_hash_test()
248{
249	local field="$1"; shift
250	local balanced="$1"; shift
251	local send_flows="$@"
252
253	RET=0
254
255	local t0_rp11=$(link_stats_tx_packets_get $rp11)
256	local t0_rp12=$(link_stats_tx_packets_get $rp12)
257
258	$send_flows
259
260	local t1_rp11=$(link_stats_tx_packets_get $rp11)
261	local t1_rp12=$(link_stats_tx_packets_get $rp12)
262
263	local d_rp11=$((t1_rp11 - t0_rp11))
264	local d_rp12=$((t1_rp12 - t0_rp12))
265
266	local diff=$((d_rp12 - d_rp11))
267	local sum=$((d_rp11 + d_rp12))
268
269	local pct=$(echo "$diff / $sum * 100" | bc -l)
270	local is_balanced=$(echo "-20 <= $pct && $pct <= 20" | bc)
271
272	[[ ( $is_balanced -eq 1 && $balanced == "balanced" ) ||
273	   ( $is_balanced -eq 0 && $balanced == "unbalanced" ) ]]
274	check_err $? "Expected traffic to be $balanced, but it is not"
275
276	log_test "Multipath hash field: $field ($balanced)"
277	log_info "Packets sent on path1 / path2: $d_rp11 / $d_rp12"
278}
279
280custom_hash_v4()
281{
282	log_info "Running IPv4 custom multipath hash tests"
283
284	sysctl_set net.ipv4.fib_multipath_hash_policy 3
285
286	# Prevent the neighbour table from overflowing, as different neighbour
287	# entries will be created on $ol4 when using different destination IPs.
288	sysctl_set net.ipv4.neigh.default.gc_thresh1 1024
289	sysctl_set net.ipv4.neigh.default.gc_thresh2 1024
290	sysctl_set net.ipv4.neigh.default.gc_thresh3 1024
291
292	sysctl_set net.ipv4.fib_multipath_hash_fields 0x0001
293	custom_hash_test "Source IP" "balanced" send_src_ipv4
294	custom_hash_test "Source IP" "unbalanced" send_dst_ipv4
295
296	sysctl_set net.ipv4.fib_multipath_hash_fields 0x0002
297	custom_hash_test "Destination IP" "balanced" send_dst_ipv4
298	custom_hash_test "Destination IP" "unbalanced" send_src_ipv4
299
300	sysctl_set net.ipv4.fib_multipath_hash_fields 0x0010
301	custom_hash_test "Source port" "balanced" send_src_udp4
302	custom_hash_test "Source port" "unbalanced" send_dst_udp4
303
304	sysctl_set net.ipv4.fib_multipath_hash_fields 0x0020
305	custom_hash_test "Destination port" "balanced" send_dst_udp4
306	custom_hash_test "Destination port" "unbalanced" send_src_udp4
307
308	sysctl_restore net.ipv4.neigh.default.gc_thresh3
309	sysctl_restore net.ipv4.neigh.default.gc_thresh2
310	sysctl_restore net.ipv4.neigh.default.gc_thresh1
311
312	sysctl_restore net.ipv4.fib_multipath_hash_policy
313}
314
315custom_hash_v6()
316{
317	log_info "Running IPv6 custom multipath hash tests"
318
319	sysctl_set net.ipv6.fib_multipath_hash_policy 3
320
321	# Prevent the neighbour table from overflowing, as different neighbour
322	# entries will be created on $ol4 when using different destination IPs.
323	sysctl_set net.ipv6.neigh.default.gc_thresh1 1024
324	sysctl_set net.ipv6.neigh.default.gc_thresh2 1024
325	sysctl_set net.ipv6.neigh.default.gc_thresh3 1024
326
327	sysctl_set net.ipv6.fib_multipath_hash_fields 0x0001
328	custom_hash_test "Source IP" "balanced" send_src_ipv6
329	custom_hash_test "Source IP" "unbalanced" send_dst_ipv6
330
331	sysctl_set net.ipv6.fib_multipath_hash_fields 0x0002
332	custom_hash_test "Destination IP" "balanced" send_dst_ipv6
333	custom_hash_test "Destination IP" "unbalanced" send_src_ipv6
334
335	sysctl_set net.ipv6.fib_multipath_hash_fields 0x0008
336	custom_hash_test "Flowlabel" "balanced" send_flowlabel
337	custom_hash_test "Flowlabel" "unbalanced" send_src_ipv6
338
339	sysctl_set net.ipv6.fib_multipath_hash_fields 0x0010
340	custom_hash_test "Source port" "balanced" send_src_udp6
341	custom_hash_test "Source port" "unbalanced" send_dst_udp6
342
343	sysctl_set net.ipv6.fib_multipath_hash_fields 0x0020
344	custom_hash_test "Destination port" "balanced" send_dst_udp6
345	custom_hash_test "Destination port" "unbalanced" send_src_udp6
346
347	sysctl_restore net.ipv6.neigh.default.gc_thresh3
348	sysctl_restore net.ipv6.neigh.default.gc_thresh2
349	sysctl_restore net.ipv6.neigh.default.gc_thresh1
350
351	sysctl_restore net.ipv6.fib_multipath_hash_policy
352}
353
354custom_hash()
355{
356	# Test that when the hash policy is set to custom, traffic is
357	# distributed only according to the fields set in the
358	# fib_multipath_hash_fields sysctl.
359	#
360	# Each time set a different field and make sure traffic is only
361	# distributed when the field is changed in the packet stream.
362	custom_hash_v4
363	custom_hash_v6
364}
365
366trap cleanup EXIT
367
368setup_prepare
369setup_wait
370tests_run
371
372exit $EXIT_STATUS
373