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