xref: /linux/tools/testing/selftests/net/mptcp/simult_flows.sh (revision 71dfa617ea9f18e4585fe78364217cd32b1fc382)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# Double quotes to prevent globbing and word splitting is recommended in new
5# code but we accept it, especially because there were too many before having
6# address all other issues detected by shellcheck.
7#shellcheck disable=SC2086
8
9. "$(dirname "${0}")/mptcp_lib.sh"
10
11ns1=""
12ns2=""
13ns3=""
14capture=false
15timeout_poll=30
16timeout_test=$((timeout_poll * 2 + 1))
17# a bit more space: because we have more to display
18MPTCP_LIB_TEST_FORMAT="%02u %-60s"
19ret=0
20bail=0
21slack=50
22large=""
23small=""
24sout=""
25cout=""
26capout=""
27size=0
28
29usage() {
30	echo "Usage: $0 [ -b ] [ -c ] [ -d ]"
31	echo -e "\t-b: bail out after first error, otherwise runs al testcases"
32	echo -e "\t-c: capture packets for each test using tcpdump (default: no capture)"
33	echo -e "\t-d: debug this script"
34}
35
36# This function is used in the cleanup trap
37#shellcheck disable=SC2317
38cleanup()
39{
40	rm -f "$cout" "$sout"
41	rm -f "$large" "$small"
42	rm -f "$capout"
43
44	mptcp_lib_ns_exit "${ns1}" "${ns2}" "${ns3}"
45}
46
47mptcp_lib_check_mptcp
48mptcp_lib_check_tools ip
49
50#  "$ns1"              ns2                    ns3
51#     ns1eth1    ns2eth1   ns2eth3      ns3eth1
52#            netem
53#     ns1eth2    ns2eth2
54#            netem
55
56setup()
57{
58	large=$(mktemp)
59	small=$(mktemp)
60	sout=$(mktemp)
61	cout=$(mktemp)
62	capout=$(mktemp)
63	size=$((2 * 2048 * 4096))
64
65	dd if=/dev/zero of=$small bs=4096 count=20 >/dev/null 2>&1
66	dd if=/dev/zero of=$large bs=4096 count=$((size / 4096)) >/dev/null 2>&1
67
68	trap cleanup EXIT
69
70	mptcp_lib_ns_init ns1 ns2 ns3
71
72	ip link add ns1eth1 netns "$ns1" type veth peer name ns2eth1 netns "$ns2"
73	ip link add ns1eth2 netns "$ns1" type veth peer name ns2eth2 netns "$ns2"
74	ip link add ns2eth3 netns "$ns2" type veth peer name ns3eth1 netns "$ns3"
75
76	ip -net "$ns1" addr add 10.0.1.1/24 dev ns1eth1
77	ip -net "$ns1" addr add dead:beef:1::1/64 dev ns1eth1 nodad
78	ip -net "$ns1" link set ns1eth1 up mtu 1500
79	ip -net "$ns1" route add default via 10.0.1.2
80	ip -net "$ns1" route add default via dead:beef:1::2
81
82	ip -net "$ns1" addr add 10.0.2.1/24 dev ns1eth2
83	ip -net "$ns1" addr add dead:beef:2::1/64 dev ns1eth2 nodad
84	ip -net "$ns1" link set ns1eth2 up mtu 1500
85	ip -net "$ns1" route add default via 10.0.2.2 metric 101
86	ip -net "$ns1" route add default via dead:beef:2::2 metric 101
87
88	ip netns exec "$ns1" ./pm_nl_ctl limits 1 1
89	ip netns exec "$ns1" ./pm_nl_ctl add 10.0.2.1 dev ns1eth2 flags subflow
90
91	ip -net "$ns2" addr add 10.0.1.2/24 dev ns2eth1
92	ip -net "$ns2" addr add dead:beef:1::2/64 dev ns2eth1 nodad
93	ip -net "$ns2" link set ns2eth1 up mtu 1500
94
95	ip -net "$ns2" addr add 10.0.2.2/24 dev ns2eth2
96	ip -net "$ns2" addr add dead:beef:2::2/64 dev ns2eth2 nodad
97	ip -net "$ns2" link set ns2eth2 up mtu 1500
98
99	ip -net "$ns2" addr add 10.0.3.2/24 dev ns2eth3
100	ip -net "$ns2" addr add dead:beef:3::2/64 dev ns2eth3 nodad
101	ip -net "$ns2" link set ns2eth3 up mtu 1500
102	ip netns exec "$ns2" sysctl -q net.ipv4.ip_forward=1
103	ip netns exec "$ns2" sysctl -q net.ipv6.conf.all.forwarding=1
104
105	ip -net "$ns3" addr add 10.0.3.3/24 dev ns3eth1
106	ip -net "$ns3" addr add dead:beef:3::3/64 dev ns3eth1 nodad
107	ip -net "$ns3" link set ns3eth1 up mtu 1500
108	ip -net "$ns3" route add default via 10.0.3.2
109	ip -net "$ns3" route add default via dead:beef:3::2
110
111	ip netns exec "$ns3" ./pm_nl_ctl limits 1 1
112
113	# debug build can slow down measurably the test program
114	# we use quite tight time limit on the run-time, to ensure
115	# maximum B/W usage.
116	# Use kmemleak/lockdep/kasan/prove_locking presence as a rough
117	# estimate for this being a debug kernel and increase the
118	# maximum run-time accordingly. Observed run times for CI builds
119	# running selftests, including kbuild, were used to determine the
120	# amount of time to add.
121	grep -q ' kmemleak_init$\| lockdep_init$\| kasan_init$\| prove_locking$' /proc/kallsyms && slack=$((slack+550))
122}
123
124do_transfer()
125{
126	local cin=$1
127	local sin=$2
128	local max_time=$3
129	local port
130	port=$((10000+MPTCP_LIB_TEST_COUNTER))
131
132	:> "$cout"
133	:> "$sout"
134	:> "$capout"
135
136	if $capture; then
137		local capuser
138		local rndh="${ns1:4}"
139		if [ -z $SUDO_USER ] ; then
140			capuser=""
141		else
142			capuser="-Z $SUDO_USER"
143		fi
144
145		local capfile="${rndh}-${port}"
146		local capopt="-i any -s 65535 -B 32768 ${capuser}"
147
148		ip netns exec ${ns3}  tcpdump ${capopt} -w "${capfile}-listener.pcap"  >> "${capout}" 2>&1 &
149		local cappid_listener=$!
150
151		ip netns exec ${ns1} tcpdump ${capopt} -w "${capfile}-connector.pcap" >> "${capout}" 2>&1 &
152		local cappid_connector=$!
153
154		sleep 1
155	fi
156
157	timeout ${timeout_test} \
158		ip netns exec ${ns3} \
159			./mptcp_connect -jt ${timeout_poll} -l -p $port -T $max_time \
160				0.0.0.0 < "$sin" > "$sout" &
161	local spid=$!
162
163	mptcp_lib_wait_local_port_listen "${ns3}" "${port}"
164
165	timeout ${timeout_test} \
166		ip netns exec ${ns1} \
167			./mptcp_connect -jt ${timeout_poll} -p $port -T $max_time \
168				10.0.3.3 < "$cin" > "$cout" &
169	local cpid=$!
170
171	wait $cpid
172	local retc=$?
173	wait $spid
174	local rets=$?
175
176	if $capture; then
177		sleep 1
178		kill ${cappid_listener}
179		kill ${cappid_connector}
180	fi
181
182	cmp $sin $cout > /dev/null 2>&1
183	local cmps=$?
184	cmp $cin $sout > /dev/null 2>&1
185	local cmpc=$?
186
187	printf "%-16s" " max $max_time "
188	if [ $retc -eq 0 ] && [ $rets -eq 0 ] && \
189	   [ $cmpc -eq 0 ] && [ $cmps -eq 0 ]; then
190		mptcp_lib_pr_ok
191		cat "$capout"
192		return 0
193	fi
194
195	mptcp_lib_pr_fail
196	echo "client exit code $retc, server $rets" 1>&2
197	echo -e "\nnetns ${ns3} socket stat for $port:" 1>&2
198	ip netns exec ${ns3} ss -nita 1>&2 -o "sport = :$port"
199	echo -e "\nnetns ${ns1} socket stat for $port:" 1>&2
200	ip netns exec ${ns1} ss -nita 1>&2 -o "dport = :$port"
201	ls -l $sin $cout
202	ls -l $cin $sout
203
204	cat "$capout"
205	return 1
206}
207
208run_test()
209{
210	local rate1=$1
211	local rate2=$2
212	local delay1=$3
213	local delay2=$4
214	local lret
215	local dev
216	shift 4
217	local msg=$*
218
219	[ $delay1 -gt 0 ] && delay1="delay $delay1" || delay1=""
220	[ $delay2 -gt 0 ] && delay2="delay $delay2" || delay2=""
221
222	for dev in ns1eth1 ns1eth2; do
223		tc -n $ns1 qdisc del dev $dev root >/dev/null 2>&1
224	done
225	for dev in ns2eth1 ns2eth2; do
226		tc -n $ns2 qdisc del dev $dev root >/dev/null 2>&1
227	done
228	tc -n $ns1 qdisc add dev ns1eth1 root netem rate ${rate1}mbit $delay1
229	tc -n $ns1 qdisc add dev ns1eth2 root netem rate ${rate2}mbit $delay2
230	tc -n $ns2 qdisc add dev ns2eth1 root netem rate ${rate1}mbit $delay1
231	tc -n $ns2 qdisc add dev ns2eth2 root netem rate ${rate2}mbit $delay2
232
233	# time is measured in ms, account for transfer size, aggregated link speed
234	# and header overhead (10%)
235	#              ms    byte -> bit   10%        mbit      -> kbit -> bit  10%
236	local time=$((1000 * size  *  8  * 10 / ((rate1 + rate2) * 1000 * 1000 * 9) ))
237
238	# mptcp_connect will do some sleeps to allow the mp_join handshake
239	# completion (see mptcp_connect): 200ms on each side, add some slack
240	time=$((time + 400 + slack))
241
242	mptcp_lib_print_title "$msg"
243	do_transfer $small $large $time
244	lret=$?
245	mptcp_lib_result_code "${lret}" "${msg}"
246	if [ $lret -ne 0 ]; then
247		ret=$lret
248		[ $bail -eq 0 ] || exit $ret
249	fi
250
251	msg+=" - reverse direction"
252	mptcp_lib_print_title "${msg}"
253	do_transfer $large $small $time
254	lret=$?
255	mptcp_lib_result_code "${lret}" "${msg}"
256	if [ $lret -ne 0 ]; then
257		ret=$lret
258		[ $bail -eq 0 ] || exit $ret
259	fi
260}
261
262while getopts "bcdh" option;do
263	case "$option" in
264	"h")
265		usage $0
266		exit ${KSFT_PASS}
267		;;
268	"b")
269		bail=1
270		;;
271	"c")
272		capture=true
273		;;
274	"d")
275		set -x
276		;;
277	"?")
278		usage $0
279		exit ${KSFT_FAIL}
280		;;
281	esac
282done
283
284setup
285run_test 10 10 0 0 "balanced bwidth"
286run_test 10 10 1 25 "balanced bwidth with unbalanced delay"
287
288# we still need some additional infrastructure to pass the following test-cases
289run_test 10 3 0 0 "unbalanced bwidth"
290run_test 10 3 1 25 "unbalanced bwidth with unbalanced delay"
291run_test 10 3 25 1 "unbalanced bwidth with opposed, unbalanced delay"
292
293mptcp_lib_result_print_all_tap
294exit $ret
295