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