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