1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4time_start=$(date +%s)
5
6optstring="b:d:e:l:r:h4cm:"
7ret=0
8sin=""
9sout=""
10cin=""
11cout=""
12ksft_skip=4
13capture=false
14timeout=30
15ipv6=true
16ethtool_random_on=true
17tc_delay="$((RANDOM%400))"
18tc_loss=$((RANDOM%101))
19tc_reorder=""
20testmode=""
21sndbuf=0
22options_log=true
23
24if [ $tc_loss -eq 100 ];then
25	tc_loss=1%
26elif [ $tc_loss -ge 10 ]; then
27	tc_loss=0.$tc_loss%
28elif [ $tc_loss -ge 1 ]; then
29	tc_loss=0.0$tc_loss%
30else
31	tc_loss=""
32fi
33
34usage() {
35	echo "Usage: $0 [ -a ]"
36	echo -e "\t-d: tc/netem delay in milliseconds, e.g. \"-d 10\" (default random)"
37	echo -e "\t-l: tc/netem loss percentage, e.g. \"-l 0.02\" (default random)"
38	echo -e "\t-r: tc/netem reorder mode, e.g. \"-r 25% 50% gap 5\", use "-r 0" to disable reordering (default random)"
39	echo -e "\t-e: ethtool features to disable, e.g.: \"-e tso -e gso\" (default: randomly disable any of tso/gso/gro)"
40	echo -e "\t-4: IPv4 only: disable IPv6 tests (default: test both IPv4 and IPv6)"
41	echo -e "\t-c: capture packets for each test using tcpdump (default: no capture)"
42	echo -e "\t-b: set sndbuf value (default: use kernel default)"
43	echo -e "\t-m: test mode (poll, sendfile; default: poll)"
44}
45
46while getopts "$optstring" option;do
47	case "$option" in
48	"h")
49		usage $0
50		exit 0
51		;;
52	"d")
53		if [ $OPTARG -ge 0 ];then
54			tc_delay="$OPTARG"
55		else
56			echo "-d requires numeric argument, got \"$OPTARG\"" 1>&2
57			exit 1
58		fi
59		;;
60	"e")
61		ethtool_args="$ethtool_args $OPTARG off"
62		ethtool_random_on=false
63		;;
64	"l")
65		tc_loss="$OPTARG"
66		;;
67	"r")
68		tc_reorder="$OPTARG"
69		;;
70	"4")
71		ipv6=false
72		;;
73	"c")
74		capture=true
75		;;
76	"b")
77		if [ $OPTARG -ge 0 ];then
78			sndbuf="$OPTARG"
79		else
80			echo "-s requires numeric argument, got \"$OPTARG\"" 1>&2
81			exit 1
82		fi
83		;;
84	"m")
85		testmode="$OPTARG"
86		;;
87	"?")
88		usage $0
89		exit 1
90		;;
91	esac
92done
93
94sec=$(date +%s)
95rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
96ns1="ns1-$rndh"
97ns2="ns2-$rndh"
98ns3="ns3-$rndh"
99ns4="ns4-$rndh"
100
101TEST_COUNT=0
102
103cleanup()
104{
105	rm -f "$cin" "$cout"
106	rm -f "$sin" "$sout"
107	rm -f "$capout"
108
109	local netns
110	for netns in "$ns1" "$ns2" "$ns3" "$ns4";do
111		ip netns del $netns
112	done
113}
114
115ip -Version > /dev/null 2>&1
116if [ $? -ne 0 ];then
117	echo "SKIP: Could not run test without ip tool"
118	exit $ksft_skip
119fi
120
121sin=$(mktemp)
122sout=$(mktemp)
123cin=$(mktemp)
124cout=$(mktemp)
125capout=$(mktemp)
126trap cleanup EXIT
127
128for i in "$ns1" "$ns2" "$ns3" "$ns4";do
129	ip netns add $i || exit $ksft_skip
130	ip -net $i link set lo up
131done
132
133#  "$ns1"              ns2                    ns3                     ns4
134# ns1eth2    ns2eth1   ns2eth3      ns3eth2   ns3eth4       ns4eth3
135#                           - drop 1% ->            reorder 25%
136#                           <- TSO off -
137
138ip link add ns1eth2 netns "$ns1" type veth peer name ns2eth1 netns "$ns2"
139ip link add ns2eth3 netns "$ns2" type veth peer name ns3eth2 netns "$ns3"
140ip link add ns3eth4 netns "$ns3" type veth peer name ns4eth3 netns "$ns4"
141
142ip -net "$ns1" addr add 10.0.1.1/24 dev ns1eth2
143ip -net "$ns1" addr add dead:beef:1::1/64 dev ns1eth2 nodad
144
145ip -net "$ns1" link set ns1eth2 up
146ip -net "$ns1" route add default via 10.0.1.2
147ip -net "$ns1" route add default via dead:beef:1::2
148
149ip -net "$ns2" addr add 10.0.1.2/24 dev ns2eth1
150ip -net "$ns2" addr add dead:beef:1::2/64 dev ns2eth1 nodad
151ip -net "$ns2" link set ns2eth1 up
152
153ip -net "$ns2" addr add 10.0.2.1/24 dev ns2eth3
154ip -net "$ns2" addr add dead:beef:2::1/64 dev ns2eth3 nodad
155ip -net "$ns2" link set ns2eth3 up
156ip -net "$ns2" route add default via 10.0.2.2
157ip -net "$ns2" route add default via dead:beef:2::2
158ip netns exec "$ns2" sysctl -q net.ipv4.ip_forward=1
159ip netns exec "$ns2" sysctl -q net.ipv6.conf.all.forwarding=1
160
161ip -net "$ns3" addr add 10.0.2.2/24 dev ns3eth2
162ip -net "$ns3" addr add dead:beef:2::2/64 dev ns3eth2 nodad
163ip -net "$ns3" link set ns3eth2 up
164
165ip -net "$ns3" addr add 10.0.3.2/24 dev ns3eth4
166ip -net "$ns3" addr add dead:beef:3::2/64 dev ns3eth4 nodad
167ip -net "$ns3" link set ns3eth4 up
168ip -net "$ns3" route add default via 10.0.2.1
169ip -net "$ns3" route add default via dead:beef:2::1
170ip netns exec "$ns3" sysctl -q net.ipv4.ip_forward=1
171ip netns exec "$ns3" sysctl -q net.ipv6.conf.all.forwarding=1
172
173ip -net "$ns4" addr add 10.0.3.1/24 dev ns4eth3
174ip -net "$ns4" addr add dead:beef:3::1/64 dev ns4eth3 nodad
175ip -net "$ns4" link set ns4eth3 up
176ip -net "$ns4" route add default via 10.0.3.2
177ip -net "$ns4" route add default via dead:beef:3::2
178
179set_ethtool_flags() {
180	local ns="$1"
181	local dev="$2"
182	local flags="$3"
183
184	ip netns exec $ns ethtool -K $dev $flags 2>/dev/null
185	[ $? -eq 0 ] && echo "INFO: set $ns dev $dev: ethtool -K $flags"
186}
187
188set_random_ethtool_flags() {
189	local flags=""
190	local r=$RANDOM
191
192	local pick1=$((r & 1))
193	local pick2=$((r & 2))
194	local pick3=$((r & 4))
195
196	[ $pick1 -ne 0 ] && flags="tso off"
197	[ $pick2 -ne 0 ] && flags="$flags gso off"
198	[ $pick3 -ne 0 ] && flags="$flags gro off"
199
200	[ -z "$flags" ] && return
201
202	set_ethtool_flags "$1" "$2" "$flags"
203}
204
205if $ethtool_random_on;then
206	set_random_ethtool_flags "$ns3" ns3eth2
207	set_random_ethtool_flags "$ns4" ns4eth3
208else
209	set_ethtool_flags "$ns3" ns3eth2 "$ethtool_args"
210	set_ethtool_flags "$ns4" ns4eth3 "$ethtool_args"
211fi
212
213print_file_err()
214{
215	ls -l "$1" 1>&2
216	echo "Trailing bytes are: "
217	tail -c 27 "$1"
218}
219
220check_transfer()
221{
222	local in=$1
223	local out=$2
224	local what=$3
225
226	cmp "$in" "$out" > /dev/null 2>&1
227	if [ $? -ne 0 ] ;then
228		echo "[ FAIL ] $what does not match (in, out):"
229		print_file_err "$in"
230		print_file_err "$out"
231
232		return 1
233	fi
234
235	return 0
236}
237
238check_mptcp_disabled()
239{
240	local disabled_ns
241	disabled_ns="ns_disabled-$sech-$(mktemp -u XXXXXX)"
242	ip netns add ${disabled_ns} || exit $ksft_skip
243
244	# net.mptcp.enabled should be enabled by default
245	if [ "$(ip netns exec ${disabled_ns} sysctl net.mptcp.enabled | awk '{ print $3 }')" -ne 1 ]; then
246		echo -e "net.mptcp.enabled sysctl is not 1 by default\t\t[ FAIL ]"
247		ret=1
248		return 1
249	fi
250	ip netns exec ${disabled_ns} sysctl -q net.mptcp.enabled=0
251
252	local err=0
253	LANG=C ip netns exec ${disabled_ns} ./mptcp_connect -t $timeout -p 10000 -s MPTCP 127.0.0.1 < "$cin" 2>&1 | \
254		grep -q "^socket: Protocol not available$" && err=1
255	ip netns delete ${disabled_ns}
256
257	if [ ${err} -eq 0 ]; then
258		echo -e "New MPTCP socket cannot be blocked via sysctl\t\t[ FAIL ]"
259		ret=1
260		return 1
261	fi
262
263	echo -e "New MPTCP socket can be blocked via sysctl\t\t[ OK ]"
264	return 0
265}
266
267check_mptcp_ulp_setsockopt()
268{
269	local t retval
270	t="ns_ulp-$sech-$(mktemp -u XXXXXX)"
271
272	ip netns add ${t} || exit $ksft_skip
273	if ! ip netns exec ${t} ./mptcp_connect -u -p 10000 -s TCP 127.0.0.1 2>&1; then
274		printf "setsockopt(..., TCP_ULP, \"mptcp\", ...) allowed\t[ FAIL ]\n"
275		retval=1
276		ret=$retval
277	else
278		printf "setsockopt(..., TCP_ULP, \"mptcp\", ...) blocked\t[ OK ]\n"
279		retval=0
280	fi
281	ip netns del ${t}
282	return $retval
283}
284
285# $1: IP address
286is_v6()
287{
288	[ -z "${1##*:*}" ]
289}
290
291do_ping()
292{
293	local listener_ns="$1"
294	local connector_ns="$2"
295	local connect_addr="$3"
296	local ping_args="-q -c 1"
297
298	if is_v6 "${connect_addr}"; then
299		$ipv6 || return 0
300		ping_args="${ping_args} -6"
301	fi
302
303	ip netns exec ${connector_ns} ping ${ping_args} $connect_addr >/dev/null
304	if [ $? -ne 0 ] ; then
305		echo "$listener_ns -> $connect_addr connectivity [ FAIL ]" 1>&2
306		ret=1
307
308		return 1
309	fi
310
311	return 0
312}
313
314# $1: ns, $2: port
315wait_local_port_listen()
316{
317	local listener_ns="${1}"
318	local port="${2}"
319
320	local port_hex i
321
322	port_hex="$(printf "%04X" "${port}")"
323	for i in $(seq 10); do
324		ip netns exec "${listener_ns}" cat /proc/net/tcp* | \
325			awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
326			break
327		sleep 0.1
328	done
329}
330
331do_transfer()
332{
333	local listener_ns="$1"
334	local connector_ns="$2"
335	local cl_proto="$3"
336	local srv_proto="$4"
337	local connect_addr="$5"
338	local local_addr="$6"
339	local extra_args=""
340
341	local port
342	port=$((10000+$TEST_COUNT))
343	TEST_COUNT=$((TEST_COUNT+1))
344
345	if [ "$sndbuf" -gt 0 ]; then
346		extra_args="$extra_args -b $sndbuf"
347	fi
348
349	if [ -n "$testmode" ]; then
350		extra_args="$extra_args -m $testmode"
351	fi
352
353	if [ -n "$extra_args" ] && $options_log; then
354		options_log=false
355		echo "INFO: extra options: $extra_args"
356	fi
357
358	:> "$cout"
359	:> "$sout"
360	:> "$capout"
361
362	local addr_port
363	addr_port=$(printf "%s:%d" ${connect_addr} ${port})
364	printf "%.3s %-5s -> %.3s (%-20s) %-5s\t" ${connector_ns} ${cl_proto} ${listener_ns} ${addr_port} ${srv_proto}
365
366	if $capture; then
367		local capuser
368		if [ -z $SUDO_USER ] ; then
369			capuser=""
370		else
371			capuser="-Z $SUDO_USER"
372		fi
373
374		local capfile="${listener_ns}-${connector_ns}-${cl_proto}-${srv_proto}-${connect_addr}.pcap"
375
376		ip netns exec ${listener_ns} tcpdump -i any -s 65535 -B 32768 $capuser -w $capfile > "$capout" 2>&1 &
377		local cappid=$!
378
379		sleep 1
380	fi
381
382	ip netns exec ${listener_ns} ./mptcp_connect -t $timeout -l -p $port -s ${srv_proto} $extra_args $local_addr < "$sin" > "$sout" &
383	local spid=$!
384
385	wait_local_port_listen "${listener_ns}" "${port}"
386
387	local start
388	start=$(date +%s%3N)
389	ip netns exec ${connector_ns} ./mptcp_connect -t $timeout -p $port -s ${cl_proto} $extra_args $connect_addr < "$cin" > "$cout" &
390	local cpid=$!
391
392	wait $cpid
393	local retc=$?
394	wait $spid
395	local rets=$?
396
397	local stop
398	stop=$(date +%s%3N)
399
400	if $capture; then
401		sleep 1
402		kill $cappid
403	fi
404
405	local duration
406	duration=$((stop-start))
407	duration=$(printf "(duration %05sms)" $duration)
408	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
409		echo "$duration [ FAIL ] client exit code $retc, server $rets" 1>&2
410		echo "\nnetns ${listener_ns} socket stat for $port:" 1>&2
411		ip netns exec ${listener_ns} ss -nita 1>&2 -o "sport = :$port"
412		echo "\nnetns ${connector_ns} socket stat for $port:" 1>&2
413		ip netns exec ${connector_ns} ss -nita 1>&2 -o "dport = :$port"
414
415		cat "$capout"
416		return 1
417	fi
418
419	check_transfer $sin $cout "file received by client"
420	retc=$?
421	check_transfer $cin $sout "file received by server"
422	rets=$?
423
424	if [ $retc -eq 0 ] && [ $rets -eq 0 ];then
425		echo "$duration [ OK ]"
426		cat "$capout"
427		return 0
428	fi
429
430	cat "$capout"
431	return 1
432}
433
434make_file()
435{
436	local name=$1
437	local who=$2
438
439	local SIZE TSIZE
440	SIZE=$((RANDOM % (1024 * 8)))
441	TSIZE=$((SIZE * 1024))
442
443	dd if=/dev/urandom of="$name" bs=1024 count=$SIZE 2> /dev/null
444
445	SIZE=$((RANDOM % 1024))
446	SIZE=$((SIZE + 128))
447	TSIZE=$((TSIZE + SIZE))
448	dd if=/dev/urandom conv=notrunc of="$name" bs=1 count=$SIZE 2> /dev/null
449	echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
450
451	echo "Created $name (size $TSIZE) containing data sent by $who"
452}
453
454run_tests_lo()
455{
456	local listener_ns="$1"
457	local connector_ns="$2"
458	local connect_addr="$3"
459	local loopback="$4"
460	local lret=0
461
462	# skip if test programs are running inside same netns for subsequent runs.
463	if [ $loopback -eq 0 ] && [ ${listener_ns} = ${connector_ns} ]; then
464		return 0
465	fi
466
467	# skip if we don't want v6
468	if ! $ipv6 && is_v6 "${connect_addr}"; then
469		return 0
470	fi
471
472	local local_addr
473	if is_v6 "${connect_addr}"; then
474		local_addr="::"
475	else
476		local_addr="0.0.0.0"
477	fi
478
479	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP ${connect_addr} ${local_addr}
480	lret=$?
481	if [ $lret -ne 0 ]; then
482		ret=$lret
483		return 1
484	fi
485
486	# don't bother testing fallback tcp except for loopback case.
487	if [ ${listener_ns} != ${connector_ns} ]; then
488		return 0
489	fi
490
491	do_transfer ${listener_ns} ${connector_ns} MPTCP TCP ${connect_addr} ${local_addr}
492	lret=$?
493	if [ $lret -ne 0 ]; then
494		ret=$lret
495		return 1
496	fi
497
498	do_transfer ${listener_ns} ${connector_ns} TCP MPTCP ${connect_addr} ${local_addr}
499	lret=$?
500	if [ $lret -ne 0 ]; then
501		ret=$lret
502		return 1
503	fi
504
505	return 0
506}
507
508run_tests()
509{
510	run_tests_lo $1 $2 $3 0
511}
512
513make_file "$cin" "client"
514make_file "$sin" "server"
515
516check_mptcp_disabled
517
518check_mptcp_ulp_setsockopt
519
520echo "INFO: validating network environment with pings"
521for sender in "$ns1" "$ns2" "$ns3" "$ns4";do
522	do_ping "$ns1" $sender 10.0.1.1
523	do_ping "$ns1" $sender dead:beef:1::1
524
525	do_ping "$ns2" $sender 10.0.1.2
526	do_ping "$ns2" $sender dead:beef:1::2
527	do_ping "$ns2" $sender 10.0.2.1
528	do_ping "$ns2" $sender dead:beef:2::1
529
530	do_ping "$ns3" $sender 10.0.2.2
531	do_ping "$ns3" $sender dead:beef:2::2
532	do_ping "$ns3" $sender 10.0.3.2
533	do_ping "$ns3" $sender dead:beef:3::2
534
535	do_ping "$ns4" $sender 10.0.3.1
536	do_ping "$ns4" $sender dead:beef:3::1
537done
538
539[ -n "$tc_loss" ] && tc -net "$ns2" qdisc add dev ns2eth3 root netem loss random $tc_loss
540echo -n "INFO: Using loss of $tc_loss "
541test "$tc_delay" -gt 0 && echo -n "delay $tc_delay ms "
542
543if [ -z "${tc_reorder}" ]; then
544	reorder1=$((RANDOM%10))
545	reorder1=$((100 - reorder1))
546	reorder2=$((RANDOM%100))
547
548	if [ $tc_delay -gt 0 ] && [ $reorder1 -lt 100 ] && [ $reorder2 -gt 0 ]; then
549		tc_reorder="reorder ${reorder1}% ${reorder2}%"
550		echo -n "$tc_reorder "
551	fi
552elif [ "$tc_reorder" = "0" ];then
553	tc_reorder=""
554elif [ "$tc_delay" -gt 0 ];then
555	# reordering requires some delay
556	tc_reorder="reorder $tc_reorder"
557	echo -n "$tc_reorder "
558fi
559
560echo "on ns3eth4"
561
562tc -net "$ns3" qdisc add dev ns3eth4 root netem delay ${tc_delay}ms $tc_reorder
563
564for sender in $ns1 $ns2 $ns3 $ns4;do
565	run_tests_lo "$ns1" "$sender" 10.0.1.1 1
566	if [ $ret -ne 0 ] ;then
567		echo "FAIL: Could not even run loopback test" 1>&2
568		exit $ret
569	fi
570	run_tests_lo "$ns1" $sender dead:beef:1::1 1
571	if [ $ret -ne 0 ] ;then
572		echo "FAIL: Could not even run loopback v6 test" 2>&1
573		exit $ret
574	fi
575
576	run_tests "$ns2" $sender 10.0.1.2
577	run_tests "$ns2" $sender dead:beef:1::2
578	run_tests "$ns2" $sender 10.0.2.1
579	run_tests "$ns2" $sender dead:beef:2::1
580
581	run_tests "$ns3" $sender 10.0.2.2
582	run_tests "$ns3" $sender dead:beef:2::2
583	run_tests "$ns3" $sender 10.0.3.2
584	run_tests "$ns3" $sender dead:beef:3::2
585
586	run_tests "$ns4" $sender 10.0.3.1
587	run_tests "$ns4" $sender dead:beef:3::1
588done
589
590time_end=$(date +%s)
591time_run=$((time_end-time_start))
592
593echo "Time: ${time_run} seconds"
594
595exit $ret
596