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