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