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: port
345wait_local_port_listen()
346{
347	local listener_ns="${1}"
348	local port="${2}"
349
350	local port_hex i
351
352	port_hex="$(printf "%04X" "${port}")"
353	for i in $(seq 10); do
354		ip netns exec "${listener_ns}" cat /proc/net/tcp* | \
355			awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
356			break
357		sleep 0.1
358	done
359}
360
361do_transfer()
362{
363	local listener_ns="$1"
364	local connector_ns="$2"
365	local cl_proto="$3"
366	local srv_proto="$4"
367	local connect_addr="$5"
368	local local_addr="$6"
369	local extra_args="$7"
370
371	local port
372	port=$((10000+$TEST_COUNT))
373	TEST_COUNT=$((TEST_COUNT+1))
374
375	if [ "$rcvbuf" -gt 0 ]; then
376		extra_args="$extra_args -R $rcvbuf"
377	fi
378
379	if [ "$sndbuf" -gt 0 ]; then
380		extra_args="$extra_args -S $sndbuf"
381	fi
382
383	if [ -n "$testmode" ]; then
384		extra_args="$extra_args -m $testmode"
385	fi
386
387	if [ -n "$extra_args" ] && $options_log; then
388		echo "INFO: extra options: $extra_args"
389	fi
390	options_log=false
391
392	:> "$cout"
393	:> "$sout"
394	:> "$capout"
395
396	local addr_port
397	addr_port=$(printf "%s:%d" ${connect_addr} ${port})
398	local result_msg
399	result_msg="$(printf "%.3s %-5s -> %.3s (%-20s) %-5s" ${connector_ns} ${cl_proto} ${listener_ns} ${addr_port} ${srv_proto})"
400	printf "%s\t" "${result_msg}"
401
402	if $capture; then
403		local capuser
404		if [ -z $SUDO_USER ] ; then
405			capuser=""
406		else
407			capuser="-Z $SUDO_USER"
408		fi
409
410		local capfile="${rndh}-${connector_ns:0:3}-${listener_ns:0:3}-${cl_proto}-${srv_proto}-${connect_addr}-${port}"
411		local capopt="-i any -s 65535 -B 32768 ${capuser}"
412
413		ip netns exec ${listener_ns}  tcpdump ${capopt} -w "${capfile}-listener.pcap"  >> "${capout}" 2>&1 &
414		local cappid_listener=$!
415
416		ip netns exec ${connector_ns} tcpdump ${capopt} -w "${capfile}-connector.pcap" >> "${capout}" 2>&1 &
417		local cappid_connector=$!
418
419		sleep 1
420	fi
421
422	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
423		nstat -n
424	if [ ${listener_ns} != ${connector_ns} ]; then
425		NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
426			nstat -n
427	fi
428
429	local stat_synrx_last_l=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtMPCapableSYNRX")
430	local stat_ackrx_last_l=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtMPCapableACKRX")
431	local stat_cookietx_last=$(mptcp_lib_get_counter "${listener_ns}" "TcpExtSyncookiesSent")
432	local stat_cookierx_last=$(mptcp_lib_get_counter "${listener_ns}" "TcpExtSyncookiesRecv")
433	local stat_csum_err_s=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtDataCsumErr")
434	local stat_csum_err_c=$(mptcp_lib_get_counter "${connector_ns}" "MPTcpExtDataCsumErr")
435
436	timeout ${timeout_test} \
437		ip netns exec ${listener_ns} \
438			./mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
439				$extra_args $local_addr < "$sin" > "$sout" &
440	local spid=$!
441
442	wait_local_port_listen "${listener_ns}" "${port}"
443
444	local start
445	start=$(date +%s%3N)
446	timeout ${timeout_test} \
447		ip netns exec ${connector_ns} \
448			./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
449				$extra_args $connect_addr < "$cin" > "$cout" &
450	local cpid=$!
451
452	wait $cpid
453	local retc=$?
454	wait $spid
455	local rets=$?
456
457	local stop
458	stop=$(date +%s%3N)
459
460	if $capture; then
461		sleep 1
462		kill ${cappid_listener}
463		kill ${cappid_connector}
464	fi
465
466	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
467		nstat | grep Tcp > /tmp/${listener_ns}.out
468	if [ ${listener_ns} != ${connector_ns} ]; then
469		NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
470			nstat | grep Tcp > /tmp/${connector_ns}.out
471	fi
472
473	local duration
474	duration=$((stop-start))
475	result_msg+=" # time=${duration}ms"
476	printf "(duration %05sms) " "${duration}"
477	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
478		echo "[ FAIL ] client exit code $retc, server $rets" 1>&2
479		echo -e "\nnetns ${listener_ns} socket stat for ${port}:" 1>&2
480		ip netns exec ${listener_ns} ss -Menita 1>&2 -o "sport = :$port"
481		cat /tmp/${listener_ns}.out
482		echo -e "\nnetns ${connector_ns} socket stat for ${port}:" 1>&2
483		ip netns exec ${connector_ns} ss -Menita 1>&2 -o "dport = :$port"
484		[ ${listener_ns} != ${connector_ns} ] && cat /tmp/${connector_ns}.out
485
486		echo
487		cat "$capout"
488		mptcp_lib_result_fail "${TEST_GROUP}: ${result_msg}"
489		return 1
490	fi
491
492	check_transfer $sin $cout "file received by client"
493	retc=$?
494	check_transfer $cin $sout "file received by server"
495	rets=$?
496
497	local stat_synrx_now_l=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtMPCapableSYNRX")
498	local stat_ackrx_now_l=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtMPCapableACKRX")
499	local stat_cookietx_now=$(mptcp_lib_get_counter "${listener_ns}" "TcpExtSyncookiesSent")
500	local stat_cookierx_now=$(mptcp_lib_get_counter "${listener_ns}" "TcpExtSyncookiesRecv")
501	local stat_ooo_now=$(mptcp_lib_get_counter "${listener_ns}" "TcpExtTCPOFOQueue")
502
503	expect_synrx=$((stat_synrx_last_l))
504	expect_ackrx=$((stat_ackrx_last_l))
505
506	cookies=$(ip netns exec ${listener_ns} sysctl net.ipv4.tcp_syncookies)
507	cookies=${cookies##*=}
508
509	if [ ${cl_proto} = "MPTCP" ] && [ ${srv_proto} = "MPTCP" ]; then
510		expect_synrx=$((stat_synrx_last_l+$connect_per_transfer))
511		expect_ackrx=$((stat_ackrx_last_l+$connect_per_transfer))
512	fi
513
514	if [ ${stat_synrx_now_l} -lt ${expect_synrx} ]; then
515		printf "[ FAIL ] lower MPC SYN rx (%d) than expected (%d)\n" \
516			"${stat_synrx_now_l}" "${expect_synrx}" 1>&2
517		retc=1
518	fi
519	if [ ${stat_ackrx_now_l} -lt ${expect_ackrx} -a ${stat_ooo_now} -eq 0 ]; then
520		if [ ${stat_ooo_now} -eq 0 ]; then
521			printf "[ FAIL ] lower MPC ACK rx (%d) than expected (%d)\n" \
522				"${stat_ackrx_now_l}" "${expect_ackrx}" 1>&2
523			rets=1
524		else
525			printf "[ Note ] fallback due to TCP OoO"
526		fi
527	fi
528
529	if $checksum; then
530		local csum_err_s=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtDataCsumErr")
531		local csum_err_c=$(mptcp_lib_get_counter "${connector_ns}" "MPTcpExtDataCsumErr")
532
533		local csum_err_s_nr=$((csum_err_s - stat_csum_err_s))
534		if [ $csum_err_s_nr -gt 0 ]; then
535			printf "[ FAIL ]\nserver got $csum_err_s_nr data checksum error[s]"
536			rets=1
537		fi
538
539		local csum_err_c_nr=$((csum_err_c - stat_csum_err_c))
540		if [ $csum_err_c_nr -gt 0 ]; then
541			printf "[ FAIL ]\nclient got $csum_err_c_nr data checksum error[s]"
542			retc=1
543		fi
544	fi
545
546	if [ $retc -eq 0 ] && [ $rets -eq 0 ]; then
547		printf "[ OK ]"
548		mptcp_lib_result_pass "${TEST_GROUP}: ${result_msg}"
549	else
550		mptcp_lib_result_fail "${TEST_GROUP}: ${result_msg}"
551	fi
552
553	if [ $cookies -eq 2 ];then
554		if [ $stat_cookietx_last -ge $stat_cookietx_now ] ;then
555			printf " WARN: CookieSent: did not advance"
556		fi
557		if [ $stat_cookierx_last -ge $stat_cookierx_now ] ;then
558			printf " WARN: CookieRecv: did not advance"
559		fi
560	else
561		if [ $stat_cookietx_last -ne $stat_cookietx_now ] ;then
562			printf " WARN: CookieSent: changed"
563		fi
564		if [ $stat_cookierx_last -ne $stat_cookierx_now ] ;then
565			printf " WARN: CookieRecv: changed"
566		fi
567	fi
568
569	if [ ${stat_synrx_now_l} -gt ${expect_synrx} ]; then
570		printf " WARN: SYNRX: expect %d, got %d (probably retransmissions)" \
571			"${expect_synrx}" "${stat_synrx_now_l}"
572	fi
573	if [ ${stat_ackrx_now_l} -gt ${expect_ackrx} ]; then
574		printf " WARN: ACKRX: expect %d, got %d (probably retransmissions)" \
575			"${expect_ackrx}" "${stat_ackrx_now_l}"
576	fi
577
578	echo
579	cat "$capout"
580	[ $retc -eq 0 ] && [ $rets -eq 0 ]
581}
582
583make_file()
584{
585	local name=$1
586	local who=$2
587	local SIZE=$filesize
588	local ksize
589	local rem
590
591	if [ $SIZE -eq 0 ]; then
592		local MAXSIZE=$((1024 * 1024 * 8))
593		local MINSIZE=$((1024 * 256))
594
595		SIZE=$(((RANDOM * RANDOM + MINSIZE) % MAXSIZE))
596	fi
597
598	ksize=$((SIZE / 1024))
599	rem=$((SIZE - (ksize * 1024)))
600
601	dd if=/dev/urandom of="$name" bs=1024 count=$ksize 2> /dev/null
602	dd if=/dev/urandom conv=notrunc of="$name" bs=1 count=$rem 2> /dev/null
603	echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
604
605	echo "Created $name (size $(du -b "$name")) containing data sent by $who"
606}
607
608run_tests_lo()
609{
610	local listener_ns="$1"
611	local connector_ns="$2"
612	local connect_addr="$3"
613	local loopback="$4"
614	local extra_args="$5"
615	local lret=0
616
617	# skip if test programs are running inside same netns for subsequent runs.
618	if [ $loopback -eq 0 ] && [ ${listener_ns} = ${connector_ns} ]; then
619		return 0
620	fi
621
622	# skip if we don't want v6
623	if ! $ipv6 && is_v6 "${connect_addr}"; then
624		return 0
625	fi
626
627	local local_addr
628	if is_v6 "${connect_addr}"; then
629		local_addr="::"
630	else
631		local_addr="0.0.0.0"
632	fi
633
634	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP \
635		    ${connect_addr} ${local_addr} "${extra_args}"
636	lret=$?
637	if [ $lret -ne 0 ]; then
638		ret=$lret
639		return 1
640	fi
641
642	if [ $do_tcp -eq 0 ]; then
643		# don't bother testing fallback tcp except for loopback case.
644		if [ ${listener_ns} != ${connector_ns} ]; then
645			return 0
646		fi
647	fi
648
649	do_transfer ${listener_ns} ${connector_ns} MPTCP TCP \
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	do_transfer ${listener_ns} ${connector_ns} TCP MPTCP \
658		    ${connect_addr} ${local_addr} "${extra_args}"
659	lret=$?
660	if [ $lret -ne 0 ]; then
661		ret=$lret
662		return 1
663	fi
664
665	if [ $do_tcp -gt 1 ] ;then
666		do_transfer ${listener_ns} ${connector_ns} TCP TCP \
667			    ${connect_addr} ${local_addr} "${extra_args}"
668		lret=$?
669		if [ $lret -ne 0 ]; then
670			ret=$lret
671			return 1
672		fi
673	fi
674
675	return 0
676}
677
678run_tests()
679{
680	run_tests_lo $1 $2 $3 0
681}
682
683run_test_transparent()
684{
685	local connect_addr="$1"
686	local msg="$2"
687
688	local connector_ns="$ns1"
689	local listener_ns="$ns2"
690	local lret=0
691	local r6flag=""
692
693	TEST_GROUP="${msg}"
694
695	# skip if we don't want v6
696	if ! $ipv6 && is_v6 "${connect_addr}"; then
697		return 0
698	fi
699
700	# IP(V6)_TRANSPARENT has been added after TOS support which came with
701	# the required infrastructure in MPTCP sockopt code. To support TOS, the
702	# following function has been exported (T). Not great but better than
703	# checking for a specific kernel version.
704	if ! mptcp_lib_kallsyms_has "T __ip_sock_set_tos$"; then
705		echo "INFO: ${msg} not supported by the kernel: SKIP"
706		mptcp_lib_result_skip "${TEST_GROUP}"
707		return
708	fi
709
710ip netns exec "$listener_ns" nft -f /dev/stdin <<"EOF"
711flush ruleset
712table inet mangle {
713	chain divert {
714		type filter hook prerouting priority -150;
715
716		meta l4proto tcp socket transparent 1 meta mark set 1 accept
717		tcp dport 20000 tproxy to :20000 meta mark set 1 accept
718	}
719}
720EOF
721	if [ $? -ne 0 ]; then
722		echo "SKIP: $msg, could not load nft ruleset"
723		mptcp_lib_fail_if_expected_feature "nft rules"
724		mptcp_lib_result_skip "${TEST_GROUP}"
725		return
726	fi
727
728	local local_addr
729	if is_v6 "${connect_addr}"; then
730		local_addr="::"
731		r6flag="-6"
732	else
733		local_addr="0.0.0.0"
734	fi
735
736	ip -net "$listener_ns" $r6flag rule add fwmark 1 lookup 100
737	if [ $? -ne 0 ]; then
738		ip netns exec "$listener_ns" nft flush ruleset
739		echo "SKIP: $msg, ip $r6flag rule failed"
740		mptcp_lib_fail_if_expected_feature "ip rule"
741		mptcp_lib_result_skip "${TEST_GROUP}"
742		return
743	fi
744
745	ip -net "$listener_ns" route add local $local_addr/0 dev lo table 100
746	if [ $? -ne 0 ]; then
747		ip netns exec "$listener_ns" nft flush ruleset
748		ip -net "$listener_ns" $r6flag rule del fwmark 1 lookup 100
749		echo "SKIP: $msg, ip route add local $local_addr failed"
750		mptcp_lib_fail_if_expected_feature "ip route"
751		mptcp_lib_result_skip "${TEST_GROUP}"
752		return
753	fi
754
755	echo "INFO: test $msg"
756
757	TEST_COUNT=10000
758	local extra_args="-o TRANSPARENT"
759	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP \
760		    ${connect_addr} ${local_addr} "${extra_args}"
761	lret=$?
762
763	ip netns exec "$listener_ns" nft flush ruleset
764	ip -net "$listener_ns" $r6flag rule del fwmark 1 lookup 100
765	ip -net "$listener_ns" route del local $local_addr/0 dev lo table 100
766
767	if [ $lret -ne 0 ]; then
768		echo "FAIL: $msg, mptcp connection error" 1>&2
769		ret=$lret
770		return 1
771	fi
772
773	echo "PASS: $msg"
774	return 0
775}
776
777run_tests_peekmode()
778{
779	local peekmode="$1"
780
781	TEST_GROUP="peek mode: ${peekmode}"
782	echo "INFO: with peek mode: ${peekmode}"
783	run_tests_lo "$ns1" "$ns1" 10.0.1.1 1 "-P ${peekmode}"
784	run_tests_lo "$ns1" "$ns1" dead:beef:1::1 1 "-P ${peekmode}"
785}
786
787run_tests_mptfo()
788{
789	TEST_GROUP="MPTFO"
790
791	if ! mptcp_lib_kallsyms_has "mptcp_fastopen_"; then
792		echo "INFO: TFO not supported by the kernel: SKIP"
793		mptcp_lib_result_skip "${TEST_GROUP}"
794		return
795	fi
796
797	echo "INFO: with MPTFO start"
798	ip netns exec "$ns1" sysctl -q net.ipv4.tcp_fastopen=2
799	ip netns exec "$ns2" sysctl -q net.ipv4.tcp_fastopen=1
800
801	run_tests_lo "$ns1" "$ns2" 10.0.1.1 0 "-o MPTFO"
802	run_tests_lo "$ns1" "$ns2" 10.0.1.1 0 "-o MPTFO"
803
804	run_tests_lo "$ns1" "$ns2" dead:beef:1::1 0 "-o MPTFO"
805	run_tests_lo "$ns1" "$ns2" dead:beef:1::1 0 "-o MPTFO"
806
807	ip netns exec "$ns1" sysctl -q net.ipv4.tcp_fastopen=0
808	ip netns exec "$ns2" sysctl -q net.ipv4.tcp_fastopen=0
809	echo "INFO: with MPTFO end"
810}
811
812run_tests_disconnect()
813{
814	local old_cin=$cin
815	local old_sin=$sin
816
817	TEST_GROUP="full disconnect"
818
819	if ! mptcp_lib_kallsyms_has "mptcp_pm_data_reset$"; then
820		echo "INFO: Full disconnect not supported: SKIP"
821		mptcp_lib_result_skip "${TEST_GROUP}"
822		return
823	fi
824
825	cat $cin $cin $cin > "$cin".disconnect
826
827	# force do_transfer to cope with the multiple transmissions
828	sin="$cin.disconnect"
829	cin="$cin.disconnect"
830	cin_disconnect="$old_cin"
831	connect_per_transfer=3
832
833	echo "INFO: disconnect"
834	run_tests_lo "$ns1" "$ns1" 10.0.1.1 1 "-I 3 -i $old_cin"
835	run_tests_lo "$ns1" "$ns1" dead:beef:1::1 1 "-I 3 -i $old_cin"
836
837	# restore previous status
838	sin=$old_sin
839	cin=$old_cin
840	cin_disconnect="$cin".disconnect
841	connect_per_transfer=1
842}
843
844display_time()
845{
846	time_end=$(date +%s)
847	time_run=$((time_end-time_start))
848
849	echo "Time: ${time_run} seconds"
850}
851
852log_if_error()
853{
854	local msg="$1"
855
856	if [ ${ret} -ne 0 ]; then
857		echo "FAIL: ${msg}" 1>&2
858
859		final_ret=${ret}
860		ret=0
861
862		return ${final_ret}
863	fi
864}
865
866stop_if_error()
867{
868	if ! log_if_error "${@}"; then
869		display_time
870		mptcp_lib_result_print_all_tap
871		exit ${final_ret}
872	fi
873}
874
875make_file "$cin" "client"
876make_file "$sin" "server"
877
878check_mptcp_disabled
879
880stop_if_error "The kernel configuration is not valid for MPTCP"
881
882echo "INFO: validating network environment with pings"
883for sender in "$ns1" "$ns2" "$ns3" "$ns4";do
884	do_ping "$ns1" $sender 10.0.1.1
885	do_ping "$ns1" $sender dead:beef:1::1
886
887	do_ping "$ns2" $sender 10.0.1.2
888	do_ping "$ns2" $sender dead:beef:1::2
889	do_ping "$ns2" $sender 10.0.2.1
890	do_ping "$ns2" $sender dead:beef:2::1
891
892	do_ping "$ns3" $sender 10.0.2.2
893	do_ping "$ns3" $sender dead:beef:2::2
894	do_ping "$ns3" $sender 10.0.3.2
895	do_ping "$ns3" $sender dead:beef:3::2
896
897	do_ping "$ns4" $sender 10.0.3.1
898	do_ping "$ns4" $sender dead:beef:3::1
899done
900
901mptcp_lib_result_code "${ret}" "ping tests"
902
903stop_if_error "Could not even run ping tests"
904
905[ -n "$tc_loss" ] && tc -net "$ns2" qdisc add dev ns2eth3 root netem loss random $tc_loss delay ${tc_delay}ms
906echo -n "INFO: Using loss of $tc_loss "
907test "$tc_delay" -gt 0 && echo -n "delay $tc_delay ms "
908
909reorder_delay=$(($tc_delay / 4))
910
911if [ -z "${tc_reorder}" ]; then
912	reorder1=$((RANDOM%10))
913	reorder1=$((100 - reorder1))
914	reorder2=$((RANDOM%100))
915
916	if [ $reorder_delay -gt 0 ] && [ $reorder1 -lt 100 ] && [ $reorder2 -gt 0 ]; then
917		tc_reorder="reorder ${reorder1}% ${reorder2}%"
918		echo -n "$tc_reorder with delay ${reorder_delay}ms "
919	fi
920elif [ "$tc_reorder" = "0" ];then
921	tc_reorder=""
922elif [ "$reorder_delay" -gt 0 ];then
923	# reordering requires some delay
924	tc_reorder="reorder $tc_reorder"
925	echo -n "$tc_reorder with delay ${reorder_delay}ms "
926fi
927
928echo "on ns3eth4"
929
930tc -net "$ns3" qdisc add dev ns3eth4 root netem delay ${reorder_delay}ms $tc_reorder
931
932TEST_GROUP="loopback v4"
933run_tests_lo "$ns1" "$ns1" 10.0.1.1 1
934stop_if_error "Could not even run loopback test"
935
936TEST_GROUP="loopback v6"
937run_tests_lo "$ns1" "$ns1" dead:beef:1::1 1
938stop_if_error "Could not even run loopback v6 test"
939
940TEST_GROUP="multihosts"
941for sender in $ns1 $ns2 $ns3 $ns4;do
942	# ns1<->ns2 is not subject to reordering/tc delays. Use it to test
943	# mptcp syncookie support.
944	if [ $sender = $ns1 ]; then
945		ip netns exec "$ns2" sysctl -q net.ipv4.tcp_syncookies=2
946	else
947		ip netns exec "$ns2" sysctl -q net.ipv4.tcp_syncookies=1
948	fi
949
950	run_tests "$ns1" $sender 10.0.1.1
951	run_tests "$ns1" $sender dead:beef:1::1
952
953	run_tests "$ns2" $sender 10.0.1.2
954	run_tests "$ns2" $sender dead:beef:1::2
955	run_tests "$ns2" $sender 10.0.2.1
956	run_tests "$ns2" $sender dead:beef:2::1
957
958	run_tests "$ns3" $sender 10.0.2.2
959	run_tests "$ns3" $sender dead:beef:2::2
960	run_tests "$ns3" $sender 10.0.3.2
961	run_tests "$ns3" $sender dead:beef:3::2
962
963	run_tests "$ns4" $sender 10.0.3.1
964	run_tests "$ns4" $sender dead:beef:3::1
965
966	log_if_error "Tests with $sender as a sender have failed"
967done
968
969run_tests_peekmode "saveWithPeek"
970run_tests_peekmode "saveAfterPeek"
971log_if_error "Tests with peek mode have failed"
972
973# MPTFO (MultiPath TCP Fatopen tests)
974run_tests_mptfo
975log_if_error "Tests with MPTFO have failed"
976
977# connect to ns4 ip address, ns2 should intercept/proxy
978run_test_transparent 10.0.3.1 "tproxy ipv4"
979run_test_transparent dead:beef:3::1 "tproxy ipv6"
980log_if_error "Tests with tproxy have failed"
981
982run_tests_disconnect
983log_if_error "Tests of the full disconnection have failed"
984
985display_time
986mptcp_lib_result_print_all_tap
987exit ${final_ret}
988