xref: /linux/tools/testing/selftests/net/forwarding/sch_red.sh (revision 55a42f78ffd386e01a5404419f8c5ded7db70a21)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# This test sends one stream of traffic from H1 through a TBF shaper, to a RED
5# within TBF shaper on $swp3. The two shapers have the same configuration, and
6# thus the resulting stream should fill all available bandwidth on the latter
7# shaper. A second stream is sent from H2 also via $swp3, and used to inject
8# additional traffic. Since all available bandwidth is taken, this traffic has
9# to go to backlog.
10#
11# +--------------------------+                     +--------------------------+
12# | H1                       |                     | H2                       |
13# |     + $h1                |                     |     + $h2                |
14# |     | 192.0.2.1/28       |                     |     | 192.0.2.2/28       |
15# |     | TBF 10Mbps         |                     |     |                    |
16# +-----|--------------------+                     +-----|--------------------+
17#       |                                                |
18# +-----|------------------------------------------------|--------------------+
19# | SW  |                                                |                    |
20# |  +--|------------------------------------------------|----------------+   |
21# |  |  + $swp1                                          + $swp2          |   |
22# |  |                               BR                                   |   |
23# |  |                                                                    |   |
24# |  |                                + $swp3                             |   |
25# |  |                                | TBF 10Mbps / RED                  |   |
26# |  +--------------------------------|-----------------------------------+   |
27# |                                   |                                       |
28# +-----------------------------------|---------------------------------------+
29#                                     |
30#                               +-----|--------------------+
31#			        | H3  |                    |
32#			        |     + $h1                |
33#			        |       192.0.2.3/28       |
34#			        |                          |
35#			        +--------------------------+
36
37ALL_TESTS="
38	ping_ipv4
39	ecn_test
40	ecn_nodrop_test
41	red_test
42	red_qevent_test
43	ecn_qevent_test
44"
45
46NUM_NETIFS=6
47CHECK_TC="yes"
48source lib.sh
49
50BACKLOG=30000
51PKTSZ=1400
52
53h1_create()
54{
55	adf_simple_if_init $h1 192.0.2.1/28
56
57	mtu_set $h1 10000
58	defer mtu_restore $h1
59
60	tc qdisc replace dev $h1 root handle 1: tbf \
61	   rate 10Mbit burst 10K limit 1M
62	defer tc qdisc del dev $h1 root
63}
64
65h2_create()
66{
67	adf_simple_if_init $h2 192.0.2.2/28
68
69	mtu_set $h2 10000
70	defer mtu_restore $h2
71}
72
73h3_create()
74{
75	adf_simple_if_init $h3 192.0.2.3/28
76
77	mtu_set $h3 10000
78	defer mtu_restore $h3
79}
80
81switch_create()
82{
83	ip link add dev br up type bridge
84	defer ip link del dev br
85
86	ip link set dev $swp1 up master br
87	defer ip link set dev $swp1 down nomaster
88
89	ip link set dev $swp2 up master br
90	defer ip link set dev $swp2 down nomaster
91
92	ip link set dev $swp3 up master br
93	defer ip link set dev $swp3 down nomaster
94
95	mtu_set $swp1 10000
96	defer mtu_restore $h1
97
98	mtu_set $swp2 10000
99	defer mtu_restore $h2
100
101	mtu_set $swp3 10000
102	defer mtu_restore $h3
103
104	tc qdisc replace dev $swp3 root handle 1: tbf \
105	   rate 10Mbit burst 10K limit 1M
106	defer tc qdisc del dev $swp3 root
107
108	ip link add name _drop_test up type dummy
109	defer ip link del dev _drop_test
110}
111
112setup_prepare()
113{
114	h1=${NETIFS[p1]}
115	swp1=${NETIFS[p2]}
116
117	h2=${NETIFS[p3]}
118	swp2=${NETIFS[p4]}
119
120	swp3=${NETIFS[p5]}
121	h3=${NETIFS[p6]}
122
123	h3_mac=$(mac_get $h3)
124
125	adf_vrf_prepare
126
127	h1_create
128	h2_create
129	h3_create
130	switch_create
131}
132
133ping_ipv4()
134{
135	ping_test $h1 192.0.2.3 " from host 1"
136	ping_test $h2 192.0.2.3 " from host 2"
137}
138
139get_qdisc_backlog()
140{
141	qdisc_stats_get $swp3 11: .backlog
142}
143
144get_nmarked()
145{
146	qdisc_stats_get $swp3 11: .marked
147}
148
149get_qdisc_npackets()
150{
151	qdisc_stats_get $swp3 11: .packets
152}
153
154get_nmirrored()
155{
156	link_stats_get _drop_test tx packets
157}
158
159send_packets()
160{
161	local proto=$1; shift
162	local pkts=$1; shift
163
164	$MZ $h2 -p $PKTSZ -a own -b $h3_mac -A 192.0.2.2 -B 192.0.2.3 -t $proto -q -c $pkts "$@"
165}
166
167# This sends traffic in an attempt to build a backlog of $size. Returns 0 on
168# success. After 10 failed attempts it bails out and returns 1. It dumps the
169# backlog size to stdout.
170build_backlog()
171{
172	local size=$1; shift
173	local proto=$1; shift
174
175	local i=0
176
177	while :; do
178		local cur=$(get_qdisc_backlog)
179		local diff=$((size - cur))
180		local pkts=$(((diff + PKTSZ - 1) / PKTSZ))
181
182		if ((cur >= size)); then
183			echo $cur
184			return 0
185		elif ((i++ > 10)); then
186			echo $cur
187			return 1
188		fi
189
190		send_packets $proto $pkts "$@"
191		sleep 1
192	done
193}
194
195check_marking()
196{
197	local cond=$1; shift
198
199	local npackets_0=$(get_qdisc_npackets)
200	local nmarked_0=$(get_nmarked)
201	sleep 5
202	local npackets_1=$(get_qdisc_npackets)
203	local nmarked_1=$(get_nmarked)
204
205	local nmarked_d=$((nmarked_1 - nmarked_0))
206	local npackets_d=$((npackets_1 - npackets_0))
207	local pct=$((100 * nmarked_d / npackets_d))
208
209	echo $pct
210	((pct $cond))
211}
212
213check_mirroring()
214{
215	local cond=$1; shift
216
217	local npackets_0=$(get_qdisc_npackets)
218	local nmirrored_0=$(get_nmirrored)
219	sleep 5
220	local npackets_1=$(get_qdisc_npackets)
221	local nmirrored_1=$(get_nmirrored)
222
223	local nmirrored_d=$((nmirrored_1 - nmirrored_0))
224	local npackets_d=$((npackets_1 - npackets_0))
225	local pct=$((100 * nmirrored_d / npackets_d))
226
227	echo $pct
228	((pct $cond))
229}
230
231ecn_test_common()
232{
233	local name=$1; shift
234	local limit=$1; shift
235	local backlog
236	local pct
237
238	# Build the below-the-limit backlog using UDP. We could use TCP just
239	# fine, but this way we get a proof that UDP is accepted when queue
240	# length is below the limit. The main stream is using TCP, and if the
241	# limit is misconfigured, we would see this traffic being ECN marked.
242	RET=0
243	backlog=$(build_backlog $((2 * limit / 3)) udp)
244	check_err $? "Could not build the requested backlog"
245	pct=$(check_marking "== 0")
246	check_err $? "backlog $backlog / $limit Got $pct% marked packets, expected == 0."
247	log_test "$name backlog < limit"
248
249	# Now push TCP, because non-TCP traffic would be early-dropped after the
250	# backlog crosses the limit, and we want to make sure that the backlog
251	# is above the limit.
252	RET=0
253	backlog=$(build_backlog $((3 * limit / 2)) tcp tos=0x01)
254	check_err $? "Could not build the requested backlog"
255	pct=$(check_marking ">= 95")
256	check_err $? "backlog $backlog / $limit Got $pct% marked packets, expected >= 95."
257	log_test "$name backlog > limit"
258}
259
260do_ecn_test()
261{
262	local limit=$1; shift
263	local name=ECN
264
265	$MZ $h1 -p $PKTSZ -A 192.0.2.1 -B 192.0.2.3 -c 0 \
266		-a own -b $h3_mac -t tcp -q tos=0x01 &
267	defer stop_traffic $!
268	sleep 1
269
270	ecn_test_common "$name" $limit
271
272	# Up there we saw that UDP gets accepted when backlog is below the
273	# limit. Now that it is above, it should all get dropped, and backlog
274	# building should fail.
275	RET=0
276	build_backlog $((2 * limit)) udp >/dev/null
277	check_fail $? "UDP traffic went into backlog instead of being early-dropped"
278	log_test "$name backlog > limit: UDP early-dropped"
279}
280
281do_ecn_nodrop_test()
282{
283	local limit=$1; shift
284	local name="ECN nodrop"
285
286	$MZ $h1 -p $PKTSZ -A 192.0.2.1 -B 192.0.2.3 -c 0 \
287		-a own -b $h3_mac -t tcp -q tos=0x01 &
288	defer stop_traffic $!
289	sleep 1
290
291	ecn_test_common "$name" $limit
292
293	# Up there we saw that UDP gets accepted when backlog is below the
294	# limit. Now that it is above, in nodrop mode, make sure it goes to
295	# backlog as well.
296	RET=0
297	build_backlog $((2 * limit)) udp >/dev/null
298	check_err $? "UDP traffic was early-dropped instead of getting into backlog"
299	log_test "$name backlog > limit: UDP not dropped"
300}
301
302do_red_test()
303{
304	local limit=$1; shift
305	local backlog
306	local pct
307
308	# Use ECN-capable TCP to verify there's no marking even though the queue
309	# is above limit.
310	$MZ $h1 -p $PKTSZ -A 192.0.2.1 -B 192.0.2.3 -c 0 \
311		-a own -b $h3_mac -t tcp -q tos=0x01 &
312	defer stop_traffic $!
313
314	# Pushing below the queue limit should work.
315	RET=0
316	backlog=$(build_backlog $((2 * limit / 3)) tcp tos=0x01)
317	check_err $? "Could not build the requested backlog"
318	pct=$(check_marking "== 0")
319	check_err $? "backlog $backlog / $limit Got $pct% marked packets, expected == 0."
320	log_test "RED backlog < limit"
321
322	# Pushing above should not.
323	RET=0
324	backlog=$(build_backlog $((3 * limit / 2)) tcp tos=0x01)
325	check_fail $? "Traffic went into backlog instead of being early-dropped"
326	pct=$(check_marking "== 0")
327	check_err $? "backlog $backlog / $limit Got $pct% marked packets, expected == 0."
328	log_test "RED backlog > limit"
329}
330
331do_red_qevent_test()
332{
333	local limit=$1; shift
334	local backlog
335	local base
336	local now
337	local pct
338
339	RET=0
340
341	$MZ $h1 -p $PKTSZ -A 192.0.2.1 -B 192.0.2.3 -c 0 \
342		-a own -b $h3_mac -t udp -q &
343	defer stop_traffic $!
344	sleep 1
345
346	tc filter add block 10 pref 1234 handle 102 matchall skip_hw \
347	   action mirred egress mirror dev _drop_test
348
349	# Push to the queue until it's at the limit. The configured limit is
350	# rounded by the qdisc, so this is the best we can do to get to the real
351	# limit.
352	build_backlog $((3 * limit / 2)) udp >/dev/null
353
354	base=$(get_nmirrored)
355	send_packets udp 100
356	sleep 1
357	now=$(get_nmirrored)
358	((now >= base + 100))
359	check_err $? "Dropped packets not observed: 100 expected, $((now - base)) seen"
360
361	tc filter del block 10 pref 1234 handle 102 matchall
362
363	base=$(get_nmirrored)
364	send_packets udp 100
365	sleep 1
366	now=$(get_nmirrored)
367	((now == base))
368	check_err $? "Dropped packets still observed: 0 expected, $((now - base)) seen"
369
370	log_test "RED early_dropped packets mirrored"
371}
372
373do_ecn_qevent_test()
374{
375	local limit=$1; shift
376	local name=ECN
377
378	RET=0
379
380	$MZ $h1 -p $PKTSZ -A 192.0.2.1 -B 192.0.2.3 -c 0 \
381		-a own -b $h3_mac -t tcp -q tos=0x01 &
382	defer stop_traffic $!
383	sleep 1
384
385	tc filter add block 10 pref 1234 handle 102 matchall skip_hw \
386	   action mirred egress mirror dev _drop_test
387
388	backlog=$(build_backlog $((2 * limit / 3)) tcp tos=0x01)
389	check_err $? "Could not build the requested backlog"
390	pct=$(check_mirroring "== 0")
391	check_err $? "backlog $backlog / $limit Got $pct% mirrored packets, expected == 0."
392
393	backlog=$(build_backlog $((3 * limit / 2)) tcp tos=0x01)
394	check_err $? "Could not build the requested backlog"
395	pct=$(check_mirroring ">= 95")
396	check_err $? "backlog $backlog / $limit Got $pct% mirrored packets, expected >= 95."
397
398	tc filter del block 10 pref 1234 handle 102 matchall
399
400	log_test "ECN marked packets mirrored"
401}
402
403install_qdisc()
404{
405	local -a args=("$@")
406
407	tc qdisc replace dev $swp3 parent 1:1 handle 11: red \
408	   limit 1M avpkt $PKTSZ probability 1 \
409	   min $BACKLOG max $((BACKLOG + 1)) burst 38 "${args[@]}"
410	sleep 1
411}
412
413uninstall_qdisc()
414{
415	tc qdisc del dev $swp3 parent 1:1
416}
417
418ecn_test()
419{
420	install_qdisc ecn
421	defer uninstall_qdisc
422	xfail_on_slow do_ecn_test $BACKLOG
423}
424
425ecn_nodrop_test()
426{
427	install_qdisc ecn nodrop
428	defer uninstall_qdisc
429	xfail_on_slow do_ecn_nodrop_test $BACKLOG
430}
431
432red_test()
433{
434	install_qdisc
435	defer uninstall_qdisc
436	xfail_on_slow do_red_test $BACKLOG
437}
438
439red_qevent_test()
440{
441	install_qdisc qevent early_drop block 10
442	defer uninstall_qdisc
443	xfail_on_slow do_red_qevent_test $BACKLOG
444}
445
446ecn_qevent_test()
447{
448	install_qdisc ecn qevent mark block 10
449	defer uninstall_qdisc
450	xfail_on_slow do_ecn_qevent_test $BACKLOG
451}
452
453trap cleanup EXIT
454
455setup_prepare
456setup_wait
457
458tests_run
459
460exit $EXIT_STATUS
461