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