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