1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4. "$(dirname "${0}")/mptcp_lib.sh"
5
6time_start=$(date +%s)
7
8optstring="S:R:d:e:l:r:h4cm:f:tC"
9ret=0
10final_ret=0
11sin=""
12sout=""
13cin_disconnect=""
14cin=""
15cout=""
16ksft_skip=4
17capture=false
18timeout_poll=30
19timeout_test=$((timeout_poll * 2 + 1))
20ipv6=true
21ethtool_random_on=true
22tc_delay="$((RANDOM%50))"
23tc_loss=$((RANDOM%101))
24testmode=""
25sndbuf=0
26rcvbuf=0
27options_log=true
28do_tcp=0
29checksum=false
30filesize=0
31connect_per_transfer=1
32
33if [ $tc_loss -eq 100 ];then
34	tc_loss=1%
35elif [ $tc_loss -ge 10 ]; then
36	tc_loss=0.$tc_loss%
37elif [ $tc_loss -ge 1 ]; then
38	tc_loss=0.0$tc_loss%
39else
40	tc_loss=""
41fi
42
43usage() {
44	echo "Usage: $0 [ -a ]"
45	echo -e "\t-d: tc/netem delay in milliseconds, e.g. \"-d 10\" (default random)"
46	echo -e "\t-l: tc/netem loss percentage, e.g. \"-l 0.02\" (default random)"
47	echo -e "\t-r: tc/netem reorder mode, e.g. \"-r 25% 50% gap 5\", use "-r 0" to disable reordering (default random)"
48	echo -e "\t-e: ethtool features to disable, e.g.: \"-e tso -e gso\" (default: randomly disable any of tso/gso/gro)"
49	echo -e "\t-4: IPv4 only: disable IPv6 tests (default: test both IPv4 and IPv6)"
50	echo -e "\t-c: capture packets for each test using tcpdump (default: no capture)"
51	echo -e "\t-f: size of file to transfer in bytes (default random)"
52	echo -e "\t-S: set sndbuf value (default: use kernel default)"
53	echo -e "\t-R: set rcvbuf value (default: use kernel default)"
54	echo -e "\t-m: test mode (poll, sendfile; default: poll)"
55	echo -e "\t-t: also run tests with TCP (use twice to non-fallback tcp)"
56	echo -e "\t-C: enable the MPTCP data checksum"
57}
58
59while getopts "$optstring" option;do
60	case "$option" in
61	"h")
62		usage $0
63		exit 0
64		;;
65	"d")
66		if [ $OPTARG -ge 0 ];then
67			tc_delay="$OPTARG"
68		else
69			echo "-d requires numeric argument, got \"$OPTARG\"" 1>&2
70			exit 1
71		fi
72		;;
73	"e")
74		ethtool_args="$ethtool_args $OPTARG off"
75		ethtool_random_on=false
76		;;
77	"l")
78		tc_loss="$OPTARG"
79		;;
80	"r")
81		tc_reorder="$OPTARG"
82		;;
83	"4")
84		ipv6=false
85		;;
86	"c")
87		capture=true
88		;;
89	"S")
90		if [ $OPTARG -ge 0 ];then
91			sndbuf="$OPTARG"
92		else
93			echo "-S requires numeric argument, got \"$OPTARG\"" 1>&2
94			exit 1
95		fi
96		;;
97	"R")
98		if [ $OPTARG -ge 0 ];then
99			rcvbuf="$OPTARG"
100		else
101			echo "-R requires numeric argument, got \"$OPTARG\"" 1>&2
102			exit 1
103		fi
104		;;
105	"m")
106		testmode="$OPTARG"
107		;;
108	"f")
109		filesize="$OPTARG"
110		;;
111	"t")
112		do_tcp=$((do_tcp+1))
113		;;
114	"C")
115		checksum=true
116		;;
117	"?")
118		usage $0
119		exit 1
120		;;
121	esac
122done
123
124sec=$(date +%s)
125rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
126ns1="ns1-$rndh"
127ns2="ns2-$rndh"
128ns3="ns3-$rndh"
129ns4="ns4-$rndh"
130
131TEST_COUNT=0
132TEST_GROUP=""
133
134cleanup()
135{
136	rm -f "$cin_disconnect" "$cout_disconnect"
137	rm -f "$cin" "$cout"
138	rm -f "$sin" "$sout"
139	rm -f "$capout"
140
141	local netns
142	for netns in "$ns1" "$ns2" "$ns3" "$ns4";do
143		ip netns del $netns
144		rm -f /tmp/$netns.{nstat,out}
145	done
146}
147
148mptcp_lib_check_mptcp
149mptcp_lib_check_kallsyms
150
151ip -Version > /dev/null 2>&1
152if [ $? -ne 0 ];then
153	echo "SKIP: Could not run test without ip tool"
154	exit $ksft_skip
155fi
156
157sin=$(mktemp)
158sout=$(mktemp)
159cin=$(mktemp)
160cout=$(mktemp)
161capout=$(mktemp)
162cin_disconnect="$cin".disconnect
163cout_disconnect="$cout".disconnect
164trap cleanup EXIT
165
166for i in "$ns1" "$ns2" "$ns3" "$ns4";do
167	ip netns add $i || exit $ksft_skip
168	ip -net $i link set lo up
169done
170
171#  "$ns1"              ns2                    ns3                     ns4
172# ns1eth2    ns2eth1   ns2eth3      ns3eth2   ns3eth4       ns4eth3
173#                           - drop 1% ->            reorder 25%
174#                           <- TSO off -
175
176ip link add ns1eth2 netns "$ns1" type veth peer name ns2eth1 netns "$ns2"
177ip link add ns2eth3 netns "$ns2" type veth peer name ns3eth2 netns "$ns3"
178ip link add ns3eth4 netns "$ns3" type veth peer name ns4eth3 netns "$ns4"
179
180ip -net "$ns1" addr add 10.0.1.1/24 dev ns1eth2
181ip -net "$ns1" addr add dead:beef:1::1/64 dev ns1eth2 nodad
182
183ip -net "$ns1" link set ns1eth2 up
184ip -net "$ns1" route add default via 10.0.1.2
185ip -net "$ns1" route add default via dead:beef:1::2
186
187ip -net "$ns2" addr add 10.0.1.2/24 dev ns2eth1
188ip -net "$ns2" addr add dead:beef:1::2/64 dev ns2eth1 nodad
189ip -net "$ns2" link set ns2eth1 up
190
191ip -net "$ns2" addr add 10.0.2.1/24 dev ns2eth3
192ip -net "$ns2" addr add dead:beef:2::1/64 dev ns2eth3 nodad
193ip -net "$ns2" link set ns2eth3 up
194ip -net "$ns2" route add default via 10.0.2.2
195ip -net "$ns2" route add default via dead:beef:2::2
196ip netns exec "$ns2" sysctl -q net.ipv4.ip_forward=1
197ip netns exec "$ns2" sysctl -q net.ipv6.conf.all.forwarding=1
198
199ip -net "$ns3" addr add 10.0.2.2/24 dev ns3eth2
200ip -net "$ns3" addr add dead:beef:2::2/64 dev ns3eth2 nodad
201ip -net "$ns3" link set ns3eth2 up
202
203ip -net "$ns3" addr add 10.0.3.2/24 dev ns3eth4
204ip -net "$ns3" addr add dead:beef:3::2/64 dev ns3eth4 nodad
205ip -net "$ns3" link set ns3eth4 up
206ip -net "$ns3" route add default via 10.0.2.1
207ip -net "$ns3" route add default via dead:beef:2::1
208ip netns exec "$ns3" sysctl -q net.ipv4.ip_forward=1
209ip netns exec "$ns3" sysctl -q net.ipv6.conf.all.forwarding=1
210
211ip -net "$ns4" addr add 10.0.3.1/24 dev ns4eth3
212ip -net "$ns4" addr add dead:beef:3::1/64 dev ns4eth3 nodad
213ip -net "$ns4" link set ns4eth3 up
214ip -net "$ns4" route add default via 10.0.3.2
215ip -net "$ns4" route add default via dead:beef:3::2
216
217if $checksum; then
218	for i in "$ns1" "$ns2" "$ns3" "$ns4";do
219		ip netns exec $i sysctl -q net.mptcp.checksum_enabled=1
220	done
221fi
222
223set_ethtool_flags() {
224	local ns="$1"
225	local dev="$2"
226	local flags="$3"
227
228	ip netns exec $ns ethtool -K $dev $flags 2>/dev/null
229	[ $? -eq 0 ] && echo "INFO: set $ns dev $dev: ethtool -K $flags"
230}
231
232set_random_ethtool_flags() {
233	local flags=""
234	local r=$RANDOM
235
236	local pick1=$((r & 1))
237	local pick2=$((r & 2))
238	local pick3=$((r & 4))
239
240	[ $pick1 -ne 0 ] && flags="tso off"
241	[ $pick2 -ne 0 ] && flags="$flags gso off"
242	[ $pick3 -ne 0 ] && flags="$flags gro off"
243
244	[ -z "$flags" ] && return
245
246	set_ethtool_flags "$1" "$2" "$flags"
247}
248
249if $ethtool_random_on;then
250	set_random_ethtool_flags "$ns3" ns3eth2
251	set_random_ethtool_flags "$ns4" ns4eth3
252else
253	set_ethtool_flags "$ns3" ns3eth2 "$ethtool_args"
254	set_ethtool_flags "$ns4" ns4eth3 "$ethtool_args"
255fi
256
257print_file_err()
258{
259	ls -l "$1" 1>&2
260	echo "Trailing bytes are: "
261	tail -c 27 "$1"
262}
263
264check_transfer()
265{
266	local in=$1
267	local out=$2
268	local what=$3
269
270	cmp "$in" "$out" > /dev/null 2>&1
271	if [ $? -ne 0 ] ;then
272		echo "[ FAIL ] $what does not match (in, out):"
273		print_file_err "$in"
274		print_file_err "$out"
275
276		return 1
277	fi
278
279	return 0
280}
281
282check_mptcp_disabled()
283{
284	local disabled_ns="ns_disabled-$rndh"
285	ip netns add ${disabled_ns} || exit $ksft_skip
286
287	# net.mptcp.enabled should be enabled by default
288	if [ "$(ip netns exec ${disabled_ns} sysctl net.mptcp.enabled | awk '{ print $3 }')" -ne 1 ]; then
289		echo -e "net.mptcp.enabled sysctl is not 1 by default\t\t[ FAIL ]"
290		mptcp_lib_result_fail "net.mptcp.enabled sysctl is not 1 by default"
291		ret=1
292		return 1
293	fi
294	ip netns exec ${disabled_ns} sysctl -q net.mptcp.enabled=0
295
296	local err=0
297	LC_ALL=C ip netns exec ${disabled_ns} ./mptcp_connect -p 10000 -s MPTCP 127.0.0.1 < "$cin" 2>&1 | \
298		grep -q "^socket: Protocol not available$" && err=1
299	ip netns delete ${disabled_ns}
300
301	if [ ${err} -eq 0 ]; then
302		echo -e "New MPTCP socket cannot be blocked via sysctl\t\t[ FAIL ]"
303		mptcp_lib_result_fail "New MPTCP socket cannot be blocked via sysctl"
304		ret=1
305		return 1
306	fi
307
308	echo -e "New MPTCP socket can be blocked via sysctl\t\t[ OK ]"
309	mptcp_lib_result_pass "New MPTCP socket can be blocked via sysctl"
310	return 0
311}
312
313# $1: IP address
314is_v6()
315{
316	[ -z "${1##*:*}" ]
317}
318
319do_ping()
320{
321	local listener_ns="$1"
322	local connector_ns="$2"
323	local connect_addr="$3"
324	local ping_args="-q -c 1"
325	local rc=0
326
327	if is_v6 "${connect_addr}"; then
328		$ipv6 || return 0
329		ping_args="${ping_args} -6"
330	fi
331
332	ip netns exec ${connector_ns} ping ${ping_args} $connect_addr >/dev/null || rc=1
333
334	if [ $rc -ne 0 ] ; then
335		echo "$listener_ns -> $connect_addr connectivity [ FAIL ]" 1>&2
336		ret=1
337
338		return 1
339	fi
340
341	return 0
342}
343
344# $1: ns, $2: MIB counter
345get_mib_counter()
346{
347	local listener_ns="${1}"
348	local mib="${2}"
349
350	# strip the header
351	ip netns exec "${listener_ns}" \
352		nstat -z -a "${mib}" | \
353			tail -n+2 | \
354			while read a count c rest; do
355				echo $count
356			done
357}
358
359# $1: ns, $2: port
360wait_local_port_listen()
361{
362	local listener_ns="${1}"
363	local port="${2}"
364
365	local port_hex i
366
367	port_hex="$(printf "%04X" "${port}")"
368	for i in $(seq 10); do
369		ip netns exec "${listener_ns}" cat /proc/net/tcp* | \
370			awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
371			break
372		sleep 0.1
373	done
374}
375
376do_transfer()
377{
378	local listener_ns="$1"
379	local connector_ns="$2"
380	local cl_proto="$3"
381	local srv_proto="$4"
382	local connect_addr="$5"
383	local local_addr="$6"
384	local extra_args="$7"
385
386	local port
387	port=$((10000+$TEST_COUNT))
388	TEST_COUNT=$((TEST_COUNT+1))
389
390	if [ "$rcvbuf" -gt 0 ]; then
391		extra_args="$extra_args -R $rcvbuf"
392	fi
393
394	if [ "$sndbuf" -gt 0 ]; then
395		extra_args="$extra_args -S $sndbuf"
396	fi
397
398	if [ -n "$testmode" ]; then
399		extra_args="$extra_args -m $testmode"
400	fi
401
402	if [ -n "$extra_args" ] && $options_log; then
403		echo "INFO: extra options: $extra_args"
404	fi
405	options_log=false
406
407	:> "$cout"
408	:> "$sout"
409	:> "$capout"
410
411	local addr_port
412	addr_port=$(printf "%s:%d" ${connect_addr} ${port})
413	local result_msg
414	result_msg="$(printf "%.3s %-5s -> %.3s (%-20s) %-5s" ${connector_ns} ${cl_proto} ${listener_ns} ${addr_port} ${srv_proto})"
415	printf "%s\t" "${result_msg}"
416
417	if $capture; then
418		local capuser
419		if [ -z $SUDO_USER ] ; then
420			capuser=""
421		else
422			capuser="-Z $SUDO_USER"
423		fi
424
425		local capfile="${rndh}-${connector_ns:0:3}-${listener_ns:0:3}-${cl_proto}-${srv_proto}-${connect_addr}-${port}"
426		local capopt="-i any -s 65535 -B 32768 ${capuser}"
427
428		ip netns exec ${listener_ns}  tcpdump ${capopt} -w "${capfile}-listener.pcap"  >> "${capout}" 2>&1 &
429		local cappid_listener=$!
430
431		ip netns exec ${connector_ns} tcpdump ${capopt} -w "${capfile}-connector.pcap" >> "${capout}" 2>&1 &
432		local cappid_connector=$!
433
434		sleep 1
435	fi
436
437	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
438		nstat -n
439	if [ ${listener_ns} != ${connector_ns} ]; then
440		NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
441			nstat -n
442	fi
443
444	local stat_synrx_last_l=$(get_mib_counter "${listener_ns}" "MPTcpExtMPCapableSYNRX")
445	local stat_ackrx_last_l=$(get_mib_counter "${listener_ns}" "MPTcpExtMPCapableACKRX")
446	local stat_cookietx_last=$(get_mib_counter "${listener_ns}" "TcpExtSyncookiesSent")
447	local stat_cookierx_last=$(get_mib_counter "${listener_ns}" "TcpExtSyncookiesRecv")
448	local stat_csum_err_s=$(get_mib_counter "${listener_ns}" "MPTcpExtDataCsumErr")
449	local stat_csum_err_c=$(get_mib_counter "${connector_ns}" "MPTcpExtDataCsumErr")
450
451	timeout ${timeout_test} \
452		ip netns exec ${listener_ns} \
453			./mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
454				$extra_args $local_addr < "$sin" > "$sout" &
455	local spid=$!
456
457	wait_local_port_listen "${listener_ns}" "${port}"
458
459	local start
460	start=$(date +%s%3N)
461	timeout ${timeout_test} \
462		ip netns exec ${connector_ns} \
463			./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
464				$extra_args $connect_addr < "$cin" > "$cout" &
465	local cpid=$!
466
467	wait $cpid
468	local retc=$?
469	wait $spid
470	local rets=$?
471
472	local stop
473	stop=$(date +%s%3N)
474
475	if $capture; then
476		sleep 1
477		kill ${cappid_listener}
478		kill ${cappid_connector}
479	fi
480
481	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
482		nstat | grep Tcp > /tmp/${listener_ns}.out
483	if [ ${listener_ns} != ${connector_ns} ]; then
484		NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
485			nstat | grep Tcp > /tmp/${connector_ns}.out
486	fi
487
488	local duration
489	duration=$((stop-start))
490	result_msg+=" # time=${duration}ms"
491	printf "(duration %05sms) " "${duration}"
492	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
493		echo "[ FAIL ] client exit code $retc, server $rets" 1>&2
494		echo -e "\nnetns ${listener_ns} socket stat for ${port}:" 1>&2
495		ip netns exec ${listener_ns} ss -Menita 1>&2 -o "sport = :$port"
496		cat /tmp/${listener_ns}.out
497		echo -e "\nnetns ${connector_ns} socket stat for ${port}:" 1>&2
498		ip netns exec ${connector_ns} ss -Menita 1>&2 -o "dport = :$port"
499		[ ${listener_ns} != ${connector_ns} ] && cat /tmp/${connector_ns}.out
500
501		echo
502		cat "$capout"
503		mptcp_lib_result_fail "${TEST_GROUP}: ${result_msg}"
504		return 1
505	fi
506
507	check_transfer $sin $cout "file received by client"
508	retc=$?
509	check_transfer $cin $sout "file received by server"
510	rets=$?
511
512	local stat_synrx_now_l=$(get_mib_counter "${listener_ns}" "MPTcpExtMPCapableSYNRX")
513	local stat_ackrx_now_l=$(get_mib_counter "${listener_ns}" "MPTcpExtMPCapableACKRX")
514	local stat_cookietx_now=$(get_mib_counter "${listener_ns}" "TcpExtSyncookiesSent")
515	local stat_cookierx_now=$(get_mib_counter "${listener_ns}" "TcpExtSyncookiesRecv")
516	local stat_ooo_now=$(get_mib_counter "${listener_ns}" "TcpExtTCPOFOQueue")
517
518	expect_synrx=$((stat_synrx_last_l))
519	expect_ackrx=$((stat_ackrx_last_l))
520
521	cookies=$(ip netns exec ${listener_ns} sysctl net.ipv4.tcp_syncookies)
522	cookies=${cookies##*=}
523
524	if [ ${cl_proto} = "MPTCP" ] && [ ${srv_proto} = "MPTCP" ]; then
525		expect_synrx=$((stat_synrx_last_l+$connect_per_transfer))
526		expect_ackrx=$((stat_ackrx_last_l+$connect_per_transfer))
527	fi
528
529	if [ ${stat_synrx_now_l} -lt ${expect_synrx} ]; then
530		printf "[ FAIL ] lower MPC SYN rx (%d) than expected (%d)\n" \
531			"${stat_synrx_now_l}" "${expect_synrx}" 1>&2
532		retc=1
533	fi
534	if [ ${stat_ackrx_now_l} -lt ${expect_ackrx} -a ${stat_ooo_now} -eq 0 ]; then
535		if [ ${stat_ooo_now} -eq 0 ]; then
536			printf "[ FAIL ] lower MPC ACK rx (%d) than expected (%d)\n" \
537				"${stat_ackrx_now_l}" "${expect_ackrx}" 1>&2
538			rets=1
539		else
540			printf "[ Note ] fallback due to TCP OoO"
541		fi
542	fi
543
544	if $checksum; then
545		local csum_err_s=$(get_mib_counter "${listener_ns}" "MPTcpExtDataCsumErr")
546		local csum_err_c=$(get_mib_counter "${connector_ns}" "MPTcpExtDataCsumErr")
547
548		local csum_err_s_nr=$((csum_err_s - stat_csum_err_s))
549		if [ $csum_err_s_nr -gt 0 ]; then
550			printf "[ FAIL ]\nserver got $csum_err_s_nr data checksum error[s]"
551			rets=1
552		fi
553
554		local csum_err_c_nr=$((csum_err_c - stat_csum_err_c))
555		if [ $csum_err_c_nr -gt 0 ]; then
556			printf "[ FAIL ]\nclient got $csum_err_c_nr data checksum error[s]"
557			retc=1
558		fi
559	fi
560
561	if [ $retc -eq 0 ] && [ $rets -eq 0 ]; then
562		printf "[ OK ]"
563		mptcp_lib_result_pass "${TEST_GROUP}: ${result_msg}"
564	else
565		mptcp_lib_result_fail "${TEST_GROUP}: ${result_msg}"
566	fi
567
568	if [ $cookies -eq 2 ];then
569		if [ $stat_cookietx_last -ge $stat_cookietx_now ] ;then
570			printf " WARN: CookieSent: did not advance"
571		fi
572		if [ $stat_cookierx_last -ge $stat_cookierx_now ] ;then
573			printf " WARN: CookieRecv: did not advance"
574		fi
575	else
576		if [ $stat_cookietx_last -ne $stat_cookietx_now ] ;then
577			printf " WARN: CookieSent: changed"
578		fi
579		if [ $stat_cookierx_last -ne $stat_cookierx_now ] ;then
580			printf " WARN: CookieRecv: changed"
581		fi
582	fi
583
584	if [ ${stat_synrx_now_l} -gt ${expect_synrx} ]; then
585		printf " WARN: SYNRX: expect %d, got %d (probably retransmissions)" \
586			"${expect_synrx}" "${stat_synrx_now_l}"
587	fi
588	if [ ${stat_ackrx_now_l} -gt ${expect_ackrx} ]; then
589		printf " WARN: ACKRX: expect %d, got %d (probably retransmissions)" \
590			"${expect_ackrx}" "${stat_ackrx_now_l}"
591	fi
592
593	echo
594	cat "$capout"
595	[ $retc -eq 0 ] && [ $rets -eq 0 ]
596}
597
598make_file()
599{
600	local name=$1
601	local who=$2
602	local SIZE=$filesize
603	local ksize
604	local rem
605
606	if [ $SIZE -eq 0 ]; then
607		local MAXSIZE=$((1024 * 1024 * 8))
608		local MINSIZE=$((1024 * 256))
609
610		SIZE=$(((RANDOM * RANDOM + MINSIZE) % MAXSIZE))
611	fi
612
613	ksize=$((SIZE / 1024))
614	rem=$((SIZE - (ksize * 1024)))
615
616	dd if=/dev/urandom of="$name" bs=1024 count=$ksize 2> /dev/null
617	dd if=/dev/urandom conv=notrunc of="$name" bs=1 count=$rem 2> /dev/null
618	echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
619
620	echo "Created $name (size $(du -b "$name")) containing data sent by $who"
621}
622
623run_tests_lo()
624{
625	local listener_ns="$1"
626	local connector_ns="$2"
627	local connect_addr="$3"
628	local loopback="$4"
629	local extra_args="$5"
630	local lret=0
631
632	# skip if test programs are running inside same netns for subsequent runs.
633	if [ $loopback -eq 0 ] && [ ${listener_ns} = ${connector_ns} ]; then
634		return 0
635	fi
636
637	# skip if we don't want v6
638	if ! $ipv6 && is_v6 "${connect_addr}"; then
639		return 0
640	fi
641
642	local local_addr
643	if is_v6 "${connect_addr}"; then
644		local_addr="::"
645	else
646		local_addr="0.0.0.0"
647	fi
648
649	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP \
650		    ${connect_addr} ${local_addr} "${extra_args}"
651	lret=$?
652	if [ $lret -ne 0 ]; then
653		ret=$lret
654		return 1
655	fi
656
657	if [ $do_tcp -eq 0 ]; then
658		# don't bother testing fallback tcp except for loopback case.
659		if [ ${listener_ns} != ${connector_ns} ]; then
660			return 0
661		fi
662	fi
663
664	do_transfer ${listener_ns} ${connector_ns} MPTCP TCP \
665		    ${connect_addr} ${local_addr} "${extra_args}"
666	lret=$?
667	if [ $lret -ne 0 ]; then
668		ret=$lret
669		return 1
670	fi
671
672	do_transfer ${listener_ns} ${connector_ns} TCP MPTCP \
673		    ${connect_addr} ${local_addr} "${extra_args}"
674	lret=$?
675	if [ $lret -ne 0 ]; then
676		ret=$lret
677		return 1
678	fi
679
680	if [ $do_tcp -gt 1 ] ;then
681		do_transfer ${listener_ns} ${connector_ns} TCP TCP \
682			    ${connect_addr} ${local_addr} "${extra_args}"
683		lret=$?
684		if [ $lret -ne 0 ]; then
685			ret=$lret
686			return 1
687		fi
688	fi
689
690	return 0
691}
692
693run_tests()
694{
695	run_tests_lo $1 $2 $3 0
696}
697
698run_test_transparent()
699{
700	local connect_addr="$1"
701	local msg="$2"
702
703	local connector_ns="$ns1"
704	local listener_ns="$ns2"
705	local lret=0
706	local r6flag=""
707
708	TEST_GROUP="${msg}"
709
710	# skip if we don't want v6
711	if ! $ipv6 && is_v6 "${connect_addr}"; then
712		return 0
713	fi
714
715	# IP(V6)_TRANSPARENT has been added after TOS support which came with
716	# the required infrastructure in MPTCP sockopt code. To support TOS, the
717	# following function has been exported (T). Not great but better than
718	# checking for a specific kernel version.
719	if ! mptcp_lib_kallsyms_has "T __ip_sock_set_tos$"; then
720		echo "INFO: ${msg} not supported by the kernel: SKIP"
721		mptcp_lib_result_skip "${TEST_GROUP}"
722		return
723	fi
724
725ip netns exec "$listener_ns" nft -f /dev/stdin <<"EOF"
726flush ruleset
727table inet mangle {
728	chain divert {
729		type filter hook prerouting priority -150;
730
731		meta l4proto tcp socket transparent 1 meta mark set 1 accept
732		tcp dport 20000 tproxy to :20000 meta mark set 1 accept
733	}
734}
735EOF
736	if [ $? -ne 0 ]; then
737		echo "SKIP: $msg, could not load nft ruleset"
738		mptcp_lib_fail_if_expected_feature "nft rules"
739		mptcp_lib_result_skip "${TEST_GROUP}"
740		return
741	fi
742
743	local local_addr
744	if is_v6 "${connect_addr}"; then
745		local_addr="::"
746		r6flag="-6"
747	else
748		local_addr="0.0.0.0"
749	fi
750
751	ip -net "$listener_ns" $r6flag rule add fwmark 1 lookup 100
752	if [ $? -ne 0 ]; then
753		ip netns exec "$listener_ns" nft flush ruleset
754		echo "SKIP: $msg, ip $r6flag rule failed"
755		mptcp_lib_fail_if_expected_feature "ip rule"
756		mptcp_lib_result_skip "${TEST_GROUP}"
757		return
758	fi
759
760	ip -net "$listener_ns" route add local $local_addr/0 dev lo table 100
761	if [ $? -ne 0 ]; then
762		ip netns exec "$listener_ns" nft flush ruleset
763		ip -net "$listener_ns" $r6flag rule del fwmark 1 lookup 100
764		echo "SKIP: $msg, ip route add local $local_addr failed"
765		mptcp_lib_fail_if_expected_feature "ip route"
766		mptcp_lib_result_skip "${TEST_GROUP}"
767		return
768	fi
769
770	echo "INFO: test $msg"
771
772	TEST_COUNT=10000
773	local extra_args="-o TRANSPARENT"
774	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP \
775		    ${connect_addr} ${local_addr} "${extra_args}"
776	lret=$?
777
778	ip netns exec "$listener_ns" nft flush ruleset
779	ip -net "$listener_ns" $r6flag rule del fwmark 1 lookup 100
780	ip -net "$listener_ns" route del local $local_addr/0 dev lo table 100
781
782	if [ $lret -ne 0 ]; then
783		echo "FAIL: $msg, mptcp connection error" 1>&2
784		ret=$lret
785		return 1
786	fi
787
788	echo "PASS: $msg"
789	return 0
790}
791
792run_tests_peekmode()
793{
794	local peekmode="$1"
795
796	TEST_GROUP="peek mode: ${peekmode}"
797	echo "INFO: with peek mode: ${peekmode}"
798	run_tests_lo "$ns1" "$ns1" 10.0.1.1 1 "-P ${peekmode}"
799	run_tests_lo "$ns1" "$ns1" dead:beef:1::1 1 "-P ${peekmode}"
800}
801
802run_tests_mptfo()
803{
804	TEST_GROUP="MPTFO"
805
806	if ! mptcp_lib_kallsyms_has "mptcp_fastopen_"; then
807		echo "INFO: TFO not supported by the kernel: SKIP"
808		mptcp_lib_result_skip "${TEST_GROUP}"
809		return
810	fi
811
812	echo "INFO: with MPTFO start"
813	ip netns exec "$ns1" sysctl -q net.ipv4.tcp_fastopen=2
814	ip netns exec "$ns2" sysctl -q net.ipv4.tcp_fastopen=1
815
816	run_tests_lo "$ns1" "$ns2" 10.0.1.1 0 "-o MPTFO"
817	run_tests_lo "$ns1" "$ns2" 10.0.1.1 0 "-o MPTFO"
818
819	run_tests_lo "$ns1" "$ns2" dead:beef:1::1 0 "-o MPTFO"
820	run_tests_lo "$ns1" "$ns2" dead:beef:1::1 0 "-o MPTFO"
821
822	ip netns exec "$ns1" sysctl -q net.ipv4.tcp_fastopen=0
823	ip netns exec "$ns2" sysctl -q net.ipv4.tcp_fastopen=0
824	echo "INFO: with MPTFO end"
825}
826
827run_tests_disconnect()
828{
829	local old_cin=$cin
830	local old_sin=$sin
831
832	TEST_GROUP="full disconnect"
833
834	if ! mptcp_lib_kallsyms_has "mptcp_pm_data_reset$"; then
835		echo "INFO: Full disconnect not supported: SKIP"
836		mptcp_lib_result_skip "${TEST_GROUP}"
837		return
838	fi
839
840	cat $cin $cin $cin > "$cin".disconnect
841
842	# force do_transfer to cope with the multiple transmissions
843	sin="$cin.disconnect"
844	cin="$cin.disconnect"
845	cin_disconnect="$old_cin"
846	connect_per_transfer=3
847
848	echo "INFO: disconnect"
849	run_tests_lo "$ns1" "$ns1" 10.0.1.1 1 "-I 3 -i $old_cin"
850	run_tests_lo "$ns1" "$ns1" dead:beef:1::1 1 "-I 3 -i $old_cin"
851
852	# restore previous status
853	sin=$old_sin
854	cin=$old_cin
855	cin_disconnect="$cin".disconnect
856	connect_per_transfer=1
857}
858
859display_time()
860{
861	time_end=$(date +%s)
862	time_run=$((time_end-time_start))
863
864	echo "Time: ${time_run} seconds"
865}
866
867log_if_error()
868{
869	local msg="$1"
870
871	if [ ${ret} -ne 0 ]; then
872		echo "FAIL: ${msg}" 1>&2
873
874		final_ret=${ret}
875		ret=0
876
877		return ${final_ret}
878	fi
879}
880
881stop_if_error()
882{
883	if ! log_if_error "${@}"; then
884		display_time
885		mptcp_lib_result_print_all_tap
886		exit ${final_ret}
887	fi
888}
889
890make_file "$cin" "client"
891make_file "$sin" "server"
892
893check_mptcp_disabled
894
895stop_if_error "The kernel configuration is not valid for MPTCP"
896
897echo "INFO: validating network environment with pings"
898for sender in "$ns1" "$ns2" "$ns3" "$ns4";do
899	do_ping "$ns1" $sender 10.0.1.1
900	do_ping "$ns1" $sender dead:beef:1::1
901
902	do_ping "$ns2" $sender 10.0.1.2
903	do_ping "$ns2" $sender dead:beef:1::2
904	do_ping "$ns2" $sender 10.0.2.1
905	do_ping "$ns2" $sender dead:beef:2::1
906
907	do_ping "$ns3" $sender 10.0.2.2
908	do_ping "$ns3" $sender dead:beef:2::2
909	do_ping "$ns3" $sender 10.0.3.2
910	do_ping "$ns3" $sender dead:beef:3::2
911
912	do_ping "$ns4" $sender 10.0.3.1
913	do_ping "$ns4" $sender dead:beef:3::1
914done
915
916mptcp_lib_result_code "${ret}" "ping tests"
917
918stop_if_error "Could not even run ping tests"
919
920[ -n "$tc_loss" ] && tc -net "$ns2" qdisc add dev ns2eth3 root netem loss random $tc_loss delay ${tc_delay}ms
921echo -n "INFO: Using loss of $tc_loss "
922test "$tc_delay" -gt 0 && echo -n "delay $tc_delay ms "
923
924reorder_delay=$(($tc_delay / 4))
925
926if [ -z "${tc_reorder}" ]; then
927	reorder1=$((RANDOM%10))
928	reorder1=$((100 - reorder1))
929	reorder2=$((RANDOM%100))
930
931	if [ $reorder_delay -gt 0 ] && [ $reorder1 -lt 100 ] && [ $reorder2 -gt 0 ]; then
932		tc_reorder="reorder ${reorder1}% ${reorder2}%"
933		echo -n "$tc_reorder with delay ${reorder_delay}ms "
934	fi
935elif [ "$tc_reorder" = "0" ];then
936	tc_reorder=""
937elif [ "$reorder_delay" -gt 0 ];then
938	# reordering requires some delay
939	tc_reorder="reorder $tc_reorder"
940	echo -n "$tc_reorder with delay ${reorder_delay}ms "
941fi
942
943echo "on ns3eth4"
944
945tc -net "$ns3" qdisc add dev ns3eth4 root netem delay ${reorder_delay}ms $tc_reorder
946
947TEST_GROUP="loopback v4"
948run_tests_lo "$ns1" "$ns1" 10.0.1.1 1
949stop_if_error "Could not even run loopback test"
950
951TEST_GROUP="loopback v6"
952run_tests_lo "$ns1" "$ns1" dead:beef:1::1 1
953stop_if_error "Could not even run loopback v6 test"
954
955TEST_GROUP="multihosts"
956for sender in $ns1 $ns2 $ns3 $ns4;do
957	# ns1<->ns2 is not subject to reordering/tc delays. Use it to test
958	# mptcp syncookie support.
959	if [ $sender = $ns1 ]; then
960		ip netns exec "$ns2" sysctl -q net.ipv4.tcp_syncookies=2
961	else
962		ip netns exec "$ns2" sysctl -q net.ipv4.tcp_syncookies=1
963	fi
964
965	run_tests "$ns1" $sender 10.0.1.1
966	run_tests "$ns1" $sender dead:beef:1::1
967
968	run_tests "$ns2" $sender 10.0.1.2
969	run_tests "$ns2" $sender dead:beef:1::2
970	run_tests "$ns2" $sender 10.0.2.1
971	run_tests "$ns2" $sender dead:beef:2::1
972
973	run_tests "$ns3" $sender 10.0.2.2
974	run_tests "$ns3" $sender dead:beef:2::2
975	run_tests "$ns3" $sender 10.0.3.2
976	run_tests "$ns3" $sender dead:beef:3::2
977
978	run_tests "$ns4" $sender 10.0.3.1
979	run_tests "$ns4" $sender dead:beef:3::1
980
981	log_if_error "Tests with $sender as a sender have failed"
982done
983
984run_tests_peekmode "saveWithPeek"
985run_tests_peekmode "saveAfterPeek"
986log_if_error "Tests with peek mode have failed"
987
988# MPTFO (MultiPath TCP Fatopen tests)
989run_tests_mptfo
990log_if_error "Tests with MPTFO have failed"
991
992# connect to ns4 ip address, ns2 should intercept/proxy
993run_test_transparent 10.0.3.1 "tproxy ipv4"
994run_test_transparent dead:beef:3::1 "tproxy ipv6"
995log_if_error "Tests with tproxy have failed"
996
997run_tests_disconnect
998log_if_error "Tests of the full disconnection have failed"
999
1000display_time
1001mptcp_lib_result_print_all_tap
1002exit ${final_ret}
1003