xref: /linux/tools/testing/selftests/net/mptcp/mptcp_join.sh (revision d53b8e36925256097a08d7cb749198d85cbf9b2b)
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# ShellCheck incorrectly believes that most of the code here is unreachable
10# because it's invoked by variable name, see how the "tests" array is used
11#shellcheck disable=SC2317
12
13. "$(dirname "${0}")/mptcp_lib.sh"
14
15ret=0
16sin=""
17sinfail=""
18sout=""
19cin=""
20cinfail=""
21cinsent=""
22tmpfile=""
23cout=""
24err=""
25capout=""
26ns1=""
27ns2=""
28iptables="iptables"
29ip6tables="ip6tables"
30timeout_poll=30
31timeout_test=$((timeout_poll * 2 + 1))
32capture=false
33checksum=false
34check_invert=0
35validate_checksum=false
36init=0
37evts_ns1=""
38evts_ns2=""
39evts_ns1_pid=0
40evts_ns2_pid=0
41last_test_failed=0
42last_test_skipped=0
43last_test_ignored=1
44
45declare -A all_tests
46declare -a only_tests_ids
47declare -a only_tests_names
48declare -A failed_tests
49MPTCP_LIB_TEST_FORMAT="%03u %s\n"
50TEST_NAME=""
51nr_blank=6
52
53# These var are used only in some tests, make sure they are not already set
54unset FAILING_LINKS
55unset test_linkfail
56unset addr_nr_ns1
57unset addr_nr_ns2
58unset cestab_ns1
59unset cestab_ns2
60unset sflags
61unset fastclose
62unset fullmesh
63unset speed
64
65# generated using "nfbpf_compile '(ip && (ip[54] & 0xf0) == 0x30) ||
66#				  (ip6 && (ip6[74] & 0xf0) == 0x30)'"
67CBPF_MPTCP_SUBOPTION_ADD_ADDR="14,
68			       48 0 0 0,
69			       84 0 0 240,
70			       21 0 3 64,
71			       48 0 0 54,
72			       84 0 0 240,
73			       21 6 7 48,
74			       48 0 0 0,
75			       84 0 0 240,
76			       21 0 4 96,
77			       48 0 0 74,
78			       84 0 0 240,
79			       21 0 1 48,
80			       6 0 0 65535,
81			       6 0 0 0"
82
83init_partial()
84{
85	capout=$(mktemp)
86
87	mptcp_lib_ns_init ns1 ns2
88
89	local netns
90	for netns in "$ns1" "$ns2"; do
91		ip netns exec $netns sysctl -q net.mptcp.pm_type=0 2>/dev/null || true
92		if $checksum; then
93			ip netns exec $netns sysctl -q net.mptcp.checksum_enabled=1
94		fi
95	done
96
97	check_invert=0
98	validate_checksum=$checksum
99
100	#  ns1         ns2
101	# ns1eth1    ns2eth1
102	# ns1eth2    ns2eth2
103	# ns1eth3    ns2eth3
104	# ns1eth4    ns2eth4
105
106	local i
107	for i in $(seq 1 4); do
108		ip link add ns1eth$i netns "$ns1" type veth peer name ns2eth$i netns "$ns2"
109		ip -net "$ns1" addr add 10.0.$i.1/24 dev ns1eth$i
110		ip -net "$ns1" addr add dead:beef:$i::1/64 dev ns1eth$i nodad
111		ip -net "$ns1" link set ns1eth$i up
112
113		ip -net "$ns2" addr add 10.0.$i.2/24 dev ns2eth$i
114		ip -net "$ns2" addr add dead:beef:$i::2/64 dev ns2eth$i nodad
115		ip -net "$ns2" link set ns2eth$i up
116
117		# let $ns2 reach any $ns1 address from any interface
118		ip -net "$ns2" route add default via 10.0.$i.1 dev ns2eth$i metric 10$i
119		ip -net "$ns2" route add default via dead:beef:$i::1 dev ns2eth$i metric 10$i
120	done
121}
122
123init_shapers()
124{
125	local i
126	for i in $(seq 1 4); do
127		tc -n $ns1 qdisc add dev ns1eth$i root netem rate 20mbit delay 1ms
128		tc -n $ns2 qdisc add dev ns2eth$i root netem rate 20mbit delay 1ms
129	done
130}
131
132cleanup_partial()
133{
134	rm -f "$capout"
135
136	mptcp_lib_ns_exit "${ns1}" "${ns2}"
137}
138
139init() {
140	init=1
141
142	mptcp_lib_check_mptcp
143	mptcp_lib_check_kallsyms
144	mptcp_lib_check_tools ip tc ss "${iptables}" "${ip6tables}"
145
146	sin=$(mktemp)
147	sout=$(mktemp)
148	cin=$(mktemp)
149	cinsent=$(mktemp)
150	cout=$(mktemp)
151	err=$(mktemp)
152	evts_ns1=$(mktemp)
153	evts_ns2=$(mktemp)
154
155	trap cleanup EXIT
156
157	make_file "$cin" "client" 1 >/dev/null
158	make_file "$sin" "server" 1 >/dev/null
159}
160
161cleanup()
162{
163	rm -f "$cin" "$cout" "$sinfail"
164	rm -f "$sin" "$sout" "$cinsent" "$cinfail"
165	rm -f "$tmpfile"
166	rm -rf $evts_ns1 $evts_ns2
167	rm -f "$err"
168	cleanup_partial
169}
170
171print_check()
172{
173	printf "%-${nr_blank}s%-36s" " " "${*}"
174}
175
176print_info()
177{
178	# It can be empty, no need to print anything then
179	[ -z "${1}" ] && return
180
181	mptcp_lib_print_info "      Info: ${*}"
182}
183
184print_ok()
185{
186	mptcp_lib_pr_ok "${@}"
187}
188
189print_fail()
190{
191	mptcp_lib_pr_fail "${@}"
192}
193
194print_skip()
195{
196	mptcp_lib_pr_skip "${@}"
197}
198
199# [ $1: fail msg ]
200mark_as_skipped()
201{
202	local msg="${1:-"Feature not supported"}"
203
204	mptcp_lib_fail_if_expected_feature "${msg}"
205
206	print_check "${msg}"
207	print_skip
208
209	last_test_skipped=1
210}
211
212# $@: condition
213continue_if()
214{
215	if ! "${@}"; then
216		mark_as_skipped
217		return 1
218	fi
219}
220
221skip_test()
222{
223	if [ "${#only_tests_ids[@]}" -eq 0 ] && [ "${#only_tests_names[@]}" -eq 0 ]; then
224		return 1
225	fi
226
227	local i
228	for i in "${only_tests_ids[@]}"; do
229		if [ "$((MPTCP_LIB_TEST_COUNTER+1))" -eq "${i}" ]; then
230			return 1
231		fi
232	done
233	for i in "${only_tests_names[@]}"; do
234		if [ "${TEST_NAME}" = "${i}" ]; then
235			return 1
236		fi
237	done
238
239	return 0
240}
241
242append_prev_results()
243{
244	if [ ${last_test_failed} -eq 1 ]; then
245		mptcp_lib_result_fail "${TEST_NAME}"
246	elif [ ${last_test_skipped} -eq 1 ]; then
247		mptcp_lib_result_skip "${TEST_NAME}"
248	elif [ ${last_test_ignored} -ne 1 ]; then
249		mptcp_lib_result_pass "${TEST_NAME}"
250	fi
251
252	last_test_failed=0
253	last_test_skipped=0
254	last_test_ignored=0
255}
256
257# $1: test name
258reset()
259{
260	append_prev_results
261
262	TEST_NAME="${1}"
263
264	MPTCP_LIB_SUBTEST_FLAKY=0 # reset if modified
265
266	if skip_test; then
267		MPTCP_LIB_TEST_COUNTER=$((MPTCP_LIB_TEST_COUNTER+1))
268		last_test_ignored=1
269		return 1
270	fi
271
272	mptcp_lib_print_title "${TEST_NAME}"
273
274	if [ "${init}" != "1" ]; then
275		init
276	else
277		cleanup_partial
278	fi
279
280	init_partial
281
282	return 0
283}
284
285# $1: test name ; $2: counter to check
286reset_check_counter()
287{
288	reset "${1}" || return 1
289
290	local counter="${2}"
291
292	if ! nstat -asz "${counter}" | grep -wq "${counter}"; then
293		mark_as_skipped "counter '${counter}' is not available"
294		return 1
295	fi
296}
297
298# $1: test name
299reset_with_cookies()
300{
301	reset "${1}" || return 1
302
303	local netns
304	for netns in "$ns1" "$ns2"; do
305		ip netns exec $netns sysctl -q net.ipv4.tcp_syncookies=2
306	done
307}
308
309# $1: test name
310reset_with_add_addr_timeout()
311{
312	local ip="${2:-4}"
313	local tables
314
315	reset "${1}" || return 1
316
317	tables="${iptables}"
318	if [ $ip -eq 6 ]; then
319		tables="${ip6tables}"
320	fi
321
322	ip netns exec $ns1 sysctl -q net.mptcp.add_addr_timeout=1
323
324	if ! ip netns exec $ns2 $tables -A OUTPUT -p tcp \
325			-m tcp --tcp-option 30 \
326			-m bpf --bytecode \
327			"$CBPF_MPTCP_SUBOPTION_ADD_ADDR" \
328			-j DROP; then
329		mark_as_skipped "unable to set the 'add addr' rule"
330		return 1
331	fi
332}
333
334# $1: test name
335reset_with_checksum()
336{
337	local ns1_enable=$1
338	local ns2_enable=$2
339
340	reset "checksum test ${1} ${2}" || return 1
341
342	ip netns exec $ns1 sysctl -q net.mptcp.checksum_enabled=$ns1_enable
343	ip netns exec $ns2 sysctl -q net.mptcp.checksum_enabled=$ns2_enable
344
345	validate_checksum=true
346}
347
348reset_with_allow_join_id0()
349{
350	local ns1_enable=$2
351	local ns2_enable=$3
352
353	reset "${1}" || return 1
354
355	ip netns exec $ns1 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns1_enable
356	ip netns exec $ns2 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns2_enable
357}
358
359# Modify TCP payload without corrupting the TCP packet
360#
361# This rule inverts a 8-bit word at byte offset 148 for the 2nd TCP ACK packets
362# carrying enough data.
363# Once it is done, the TCP Checksum field is updated so the packet is still
364# considered as valid at the TCP level.
365# Because the MPTCP checksum, covering the TCP options and data, has not been
366# updated, the modification will be detected and an MP_FAIL will be emitted:
367# what we want to validate here without corrupting "random" MPTCP options.
368#
369# To avoid having tc producing this pr_info() message for each TCP ACK packets
370# not carrying enough data:
371#
372#     tc action pedit offset 162 out of bounds
373#
374# Netfilter is used to mark packets with enough data.
375setup_fail_rules()
376{
377	check_invert=1
378	validate_checksum=true
379	local i="$1"
380	local ip="${2:-4}"
381	local tables
382
383	tables="${iptables}"
384	if [ $ip -eq 6 ]; then
385		tables="${ip6tables}"
386	fi
387
388	ip netns exec $ns2 $tables \
389		-t mangle \
390		-A OUTPUT \
391		-o ns2eth$i \
392		-p tcp \
393		-m length --length 150:9999 \
394		-m statistic --mode nth --packet 1 --every 99999 \
395		-j MARK --set-mark 42 || return ${KSFT_SKIP}
396
397	tc -n $ns2 qdisc add dev ns2eth$i clsact || return ${KSFT_SKIP}
398	tc -n $ns2 filter add dev ns2eth$i egress \
399		protocol ip prio 1000 \
400		handle 42 fw \
401		action pedit munge offset 148 u8 invert \
402		pipe csum tcp \
403		index 100 || return ${KSFT_SKIP}
404}
405
406reset_with_fail()
407{
408	reset_check_counter "${1}" "MPTcpExtInfiniteMapTx" || return 1
409	shift
410
411	ip netns exec $ns1 sysctl -q net.mptcp.checksum_enabled=1
412	ip netns exec $ns2 sysctl -q net.mptcp.checksum_enabled=1
413
414	local rc=0
415	setup_fail_rules "${@}" || rc=$?
416
417	if [ ${rc} -eq ${KSFT_SKIP} ]; then
418		mark_as_skipped "unable to set the 'fail' rules"
419		return 1
420	fi
421}
422
423reset_with_events()
424{
425	reset "${1}" || return 1
426
427	mptcp_lib_events "${ns1}" "${evts_ns1}" evts_ns1_pid
428	mptcp_lib_events "${ns2}" "${evts_ns2}" evts_ns2_pid
429}
430
431reset_with_tcp_filter()
432{
433	reset "${1}" || return 1
434	shift
435
436	local ns="${!1}"
437	local src="${2}"
438	local target="${3}"
439	local chain="${4:-INPUT}"
440
441	if ! ip netns exec "${ns}" ${iptables} \
442			-A "${chain}" \
443			-s "${src}" \
444			-p tcp \
445			-j "${target}"; then
446		mark_as_skipped "unable to set the filter rules"
447		return 1
448	fi
449}
450
451# $1: err msg
452fail_test()
453{
454	if ! mptcp_lib_subtest_is_flaky; then
455		ret=${KSFT_FAIL}
456	fi
457
458	if [ ${#} -gt 0 ]; then
459		print_fail "${@}"
460	fi
461
462	# just in case a test is marked twice as failed
463	if [ ${last_test_failed} -eq 0 ]; then
464		failed_tests[${MPTCP_LIB_TEST_COUNTER}]="${TEST_NAME}"
465		dump_stats
466		last_test_failed=1
467	fi
468}
469
470get_failed_tests_ids()
471{
472	# sorted
473	local i
474	for i in "${!failed_tests[@]}"; do
475		echo "${i}"
476	done | sort -n
477}
478
479check_transfer()
480{
481	local in=$1
482	local out=$2
483	local what=$3
484	local bytes=$4
485	local i a b
486
487	local line
488	if [ -n "$bytes" ]; then
489		local out_size
490		# when truncating we must check the size explicitly
491		out_size=$(wc -c $out | awk '{print $1}')
492		if [ $out_size -ne $bytes ]; then
493			fail_test "$what output file has wrong size ($out_size, $bytes)"
494			return 1
495		fi
496
497		# note: BusyBox's "cmp" command doesn't support --bytes
498		tmpfile=$(mktemp)
499		head --bytes="$bytes" "$in" > "$tmpfile"
500		mv "$tmpfile" "$in"
501		head --bytes="$bytes" "$out" > "$tmpfile"
502		mv "$tmpfile" "$out"
503		tmpfile=""
504	fi
505	cmp -l "$in" "$out" | while read -r i a b; do
506		local sum=$((0${a} + 0${b}))
507		if [ $check_invert -eq 0 ] || [ $sum -ne $((0xff)) ]; then
508			fail_test "$what does not match (in, out):"
509			mptcp_lib_print_file_err "$in"
510			mptcp_lib_print_file_err "$out"
511
512			return 1
513		else
514			print_info "$what has inverted byte at ${i}"
515		fi
516	done
517
518	return 0
519}
520
521do_ping()
522{
523	local listener_ns="$1"
524	local connector_ns="$2"
525	local connect_addr="$3"
526
527	if ! ip netns exec ${connector_ns} ping -q -c 1 $connect_addr >/dev/null; then
528		fail_test "$listener_ns -> $connect_addr connectivity"
529	fi
530}
531
532link_failure()
533{
534	local ns="$1"
535
536	if [ -z "$FAILING_LINKS" ]; then
537		l=$((RANDOM%4))
538		FAILING_LINKS=$((l+1))
539	fi
540
541	local l
542	for l in $FAILING_LINKS; do
543		local veth="ns1eth$l"
544		ip -net "$ns" link set "$veth" down
545	done
546}
547
548rm_addr_count()
549{
550	mptcp_lib_get_counter "${1}" "MPTcpExtRmAddr"
551}
552
553# $1: ns, $2: old rm_addr counter in $ns
554wait_rm_addr()
555{
556	local ns="${1}"
557	local old_cnt="${2}"
558	local cnt
559
560	local i
561	for i in $(seq 10); do
562		cnt=$(rm_addr_count ${ns})
563		[ "$cnt" = "${old_cnt}" ] || break
564		sleep 0.1
565	done
566}
567
568rm_sf_count()
569{
570	mptcp_lib_get_counter "${1}" "MPTcpExtRmSubflow"
571}
572
573# $1: ns, $2: old rm_sf counter in $ns
574wait_rm_sf()
575{
576	local ns="${1}"
577	local old_cnt="${2}"
578	local cnt
579
580	local i
581	for i in $(seq 10); do
582		cnt=$(rm_sf_count ${ns})
583		[ "$cnt" = "${old_cnt}" ] || break
584		sleep 0.1
585	done
586}
587
588wait_mpj()
589{
590	local ns="${1}"
591	local cnt old_cnt
592
593	old_cnt=$(mptcp_lib_get_counter ${ns} "MPTcpExtMPJoinAckRx")
594
595	local i
596	for i in $(seq 10); do
597		cnt=$(mptcp_lib_get_counter ${ns} "MPTcpExtMPJoinAckRx")
598		[ "$cnt" = "${old_cnt}" ] || break
599		sleep 0.1
600	done
601}
602
603kill_events_pids()
604{
605	mptcp_lib_kill_wait $evts_ns1_pid
606	evts_ns1_pid=0
607	mptcp_lib_kill_wait $evts_ns2_pid
608	evts_ns2_pid=0
609}
610
611pm_nl_set_limits()
612{
613	mptcp_lib_pm_nl_set_limits "${@}"
614}
615
616pm_nl_add_endpoint()
617{
618	mptcp_lib_pm_nl_add_endpoint "${@}"
619}
620
621pm_nl_del_endpoint()
622{
623	mptcp_lib_pm_nl_del_endpoint "${@}"
624}
625
626pm_nl_flush_endpoint()
627{
628	mptcp_lib_pm_nl_flush_endpoint "${@}"
629}
630
631pm_nl_show_endpoints()
632{
633	mptcp_lib_pm_nl_show_endpoints "${@}"
634}
635
636pm_nl_change_endpoint()
637{
638	mptcp_lib_pm_nl_change_endpoint "${@}"
639}
640
641pm_nl_check_endpoint()
642{
643	local msg="$1"
644	local ns=$2
645	local addr=$3
646	local flags dev id port
647
648	print_check "${msg}"
649
650	shift 3
651	while [ -n "$1" ]; do
652		case "${1}" in
653		"flags" | "dev" | "id" | "port")
654			eval "${1}"="${2}"
655			shift
656			;;
657		*)
658			;;
659		esac
660
661		shift
662	done
663
664	if [ -z "${id}" ]; then
665		fail_test "bad test - missing endpoint id"
666		return
667	fi
668
669	check_output "mptcp_lib_pm_nl_get_endpoint ${ns} ${id}" \
670		"$(mptcp_lib_pm_nl_format_endpoints \
671			"${id},${addr},${flags//","/" "},${dev},${port}")"
672}
673
674pm_nl_set_endpoint()
675{
676	local listener_ns="$1"
677	local connector_ns="$2"
678	local connect_addr="$3"
679
680	local addr_nr_ns1=${addr_nr_ns1:-0}
681	local addr_nr_ns2=${addr_nr_ns2:-0}
682	local sflags=${sflags:-""}
683	local fullmesh=${fullmesh:-""}
684
685	local flags="subflow"
686	if [ -n "${fullmesh}" ]; then
687		flags="${flags},fullmesh"
688		addr_nr_ns2=${fullmesh}
689	fi
690
691	# let the mptcp subflow be established in background before
692	# do endpoint manipulation
693	if [ $addr_nr_ns1 != "0" ] || [ $addr_nr_ns2 != "0" ]; then
694		sleep 1
695	fi
696
697	if [ $addr_nr_ns1 -gt 0 ]; then
698		local counter=2
699		local add_nr_ns1=${addr_nr_ns1}
700		local id=10
701		while [ $add_nr_ns1 -gt 0 ]; do
702			local addr
703			if mptcp_lib_is_v6 "${connect_addr}"; then
704				addr="dead:beef:$counter::1"
705			else
706				addr="10.0.$counter.1"
707			fi
708			pm_nl_add_endpoint $ns1 $addr flags signal
709			counter=$((counter + 1))
710			add_nr_ns1=$((add_nr_ns1 - 1))
711			id=$((id + 1))
712		done
713	elif [ $addr_nr_ns1 -lt 0 ]; then
714		local rm_nr_ns1=$((-addr_nr_ns1))
715		if [ $rm_nr_ns1 -lt 8 ]; then
716			local counter=0
717			local line
718			pm_nl_show_endpoints ${listener_ns} | while read -r line; do
719				# shellcheck disable=SC2206 # we do want to split per word
720				local arr=($line)
721				local nr=0
722
723				local i
724				for i in "${arr[@]}"; do
725					if [ $i = "id" ]; then
726						if [ $counter -eq $rm_nr_ns1 ]; then
727							break
728						fi
729						id=${arr[$nr+1]}
730						rm_addr=$(rm_addr_count ${connector_ns})
731						pm_nl_del_endpoint ${listener_ns} $id
732						wait_rm_addr ${connector_ns} ${rm_addr}
733						counter=$((counter + 1))
734					fi
735					nr=$((nr + 1))
736				done
737			done
738		elif [ $rm_nr_ns1 -eq 8 ]; then
739			pm_nl_flush_endpoint ${listener_ns}
740		elif [ $rm_nr_ns1 -eq 9 ]; then
741			pm_nl_del_endpoint ${listener_ns} 0 ${connect_addr}
742		fi
743	fi
744
745	# if newly added endpoints must be deleted, give the background msk
746	# some time to created them
747	[ $addr_nr_ns1 -gt 0 ] && [ $addr_nr_ns2 -lt 0 ] && sleep 1
748
749	if [ $addr_nr_ns2 -gt 0 ]; then
750		local add_nr_ns2=${addr_nr_ns2}
751		local counter=3
752		local id=20
753		while [ $add_nr_ns2 -gt 0 ]; do
754			local addr
755			if mptcp_lib_is_v6 "${connect_addr}"; then
756				addr="dead:beef:$counter::2"
757			else
758				addr="10.0.$counter.2"
759			fi
760			pm_nl_add_endpoint $ns2 $addr flags $flags
761			counter=$((counter + 1))
762			add_nr_ns2=$((add_nr_ns2 - 1))
763			id=$((id + 1))
764		done
765	elif [ $addr_nr_ns2 -lt 0 ]; then
766		local rm_nr_ns2=$((-addr_nr_ns2))
767		if [ $rm_nr_ns2 -lt 8 ]; then
768			local counter=0
769			local line
770			pm_nl_show_endpoints ${connector_ns} | while read -r line; do
771				# shellcheck disable=SC2206 # we do want to split per word
772				local arr=($line)
773				local nr=0
774
775				local i
776				for i in "${arr[@]}"; do
777					if [ $i = "id" ]; then
778						if [ $counter -eq $rm_nr_ns2 ]; then
779							break
780						fi
781						local id rm_addr
782						# rm_addr are serialized, allow the previous one to
783						# complete
784						id=${arr[$nr+1]}
785						rm_addr=$(rm_addr_count ${listener_ns})
786						pm_nl_del_endpoint ${connector_ns} $id
787						wait_rm_addr ${listener_ns} ${rm_addr}
788						counter=$((counter + 1))
789					fi
790					nr=$((nr + 1))
791				done
792			done
793		elif [ $rm_nr_ns2 -eq 8 ]; then
794			pm_nl_flush_endpoint ${connector_ns}
795		elif [ $rm_nr_ns2 -eq 9 ]; then
796			local addr
797			if mptcp_lib_is_v6 "${connect_addr}"; then
798				addr="dead:beef:1::2"
799			else
800				addr="10.0.1.2"
801			fi
802			pm_nl_del_endpoint ${connector_ns} 0 $addr
803		fi
804	fi
805
806	if [ -n "${sflags}" ]; then
807		sleep 1
808
809		local netns
810		for netns in "$ns1" "$ns2"; do
811			local line
812			pm_nl_show_endpoints $netns | while read -r line; do
813				# shellcheck disable=SC2206 # we do want to split per word
814				local arr=($line)
815				local nr=0
816				local id
817
818				local i
819				for i in "${arr[@]}"; do
820					if [ $i = "id" ]; then
821						id=${arr[$nr+1]}
822					fi
823					nr=$((nr + 1))
824				done
825				pm_nl_change_endpoint $netns $id $sflags
826			done
827		done
828	fi
829}
830
831chk_cestab_nr()
832{
833	local ns=$1
834	local cestab=$2
835	local count
836
837	print_check "cestab $cestab"
838	count=$(mptcp_lib_get_counter ${ns} "MPTcpExtMPCurrEstab")
839	if [ -z "$count" ]; then
840		print_skip
841	elif [ "$count" != "$cestab" ]; then
842		fail_test "got $count current establish[s] expected $cestab"
843	else
844		print_ok
845	fi
846}
847
848# $1 namespace 1, $2 namespace 2
849check_cestab()
850{
851	if [ -n "${cestab_ns1}" ]; then
852		chk_cestab_nr ${1} ${cestab_ns1}
853	fi
854	if [ -n "${cestab_ns2}" ]; then
855		chk_cestab_nr ${2} ${cestab_ns2}
856	fi
857}
858
859do_transfer()
860{
861	local listener_ns="$1"
862	local connector_ns="$2"
863	local cl_proto="$3"
864	local srv_proto="$4"
865	local connect_addr="$5"
866
867	local port=$((10000 + MPTCP_LIB_TEST_COUNTER - 1))
868	local cappid
869	local FAILING_LINKS=${FAILING_LINKS:-""}
870	local fastclose=${fastclose:-""}
871	local speed=${speed:-"fast"}
872
873	:> "$cout"
874	:> "$sout"
875	:> "$capout"
876
877	if $capture; then
878		local capuser
879		if [ -z $SUDO_USER ] ; then
880			capuser=""
881		else
882			capuser="-Z $SUDO_USER"
883		fi
884
885		capfile=$(printf "mp_join-%02u-%s.pcap" "$MPTCP_LIB_TEST_COUNTER" "${listener_ns}")
886
887		echo "Capturing traffic for test $MPTCP_LIB_TEST_COUNTER into $capfile"
888		ip netns exec ${listener_ns} tcpdump -i any -s 65535 -B 32768 $capuser -w $capfile > "$capout" 2>&1 &
889		cappid=$!
890
891		sleep 1
892	fi
893
894	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
895		nstat -n
896	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
897		nstat -n
898
899	local extra_args
900	if [ $speed = "fast" ]; then
901		extra_args="-j"
902	elif [ $speed = "slow" ]; then
903		extra_args="-r 50"
904	elif [ $speed -gt 0 ]; then
905		extra_args="-r ${speed}"
906	fi
907
908	local extra_cl_args=""
909	local extra_srv_args=""
910	local trunc_size=""
911	if [ -n "${fastclose}" ]; then
912		if [ ${test_linkfail} -le 1 ]; then
913			fail_test "fastclose tests need test_linkfail argument"
914			return 1
915		fi
916
917		# disconnect
918		trunc_size=${test_linkfail}
919		local side=${fastclose}
920
921		if [ ${side} = "client" ]; then
922			extra_cl_args="-f ${test_linkfail}"
923			extra_srv_args="-f -1"
924		elif [ ${side} = "server" ]; then
925			extra_srv_args="-f ${test_linkfail}"
926			extra_cl_args="-f -1"
927		else
928			fail_test "wrong/unknown fastclose spec ${side}"
929			return 1
930		fi
931	fi
932
933	extra_srv_args="$extra_args $extra_srv_args"
934	if [ "$test_linkfail" -gt 1 ];then
935		timeout ${timeout_test} \
936			ip netns exec ${listener_ns} \
937				./mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
938					$extra_srv_args "::" < "$sinfail" > "$sout" &
939	else
940		timeout ${timeout_test} \
941			ip netns exec ${listener_ns} \
942				./mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
943					$extra_srv_args "::" < "$sin" > "$sout" &
944	fi
945	local spid=$!
946
947	mptcp_lib_wait_local_port_listen "${listener_ns}" "${port}"
948
949	extra_cl_args="$extra_args $extra_cl_args"
950	if [ "$test_linkfail" -eq 0 ];then
951		timeout ${timeout_test} \
952			ip netns exec ${connector_ns} \
953				./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
954					$extra_cl_args $connect_addr < "$cin" > "$cout" &
955	elif [ "$test_linkfail" -eq 1 ] || [ "$test_linkfail" -eq 2 ];then
956		( cat "$cinfail" ; sleep 2; link_failure $listener_ns ; cat "$cinfail" ) | \
957			tee "$cinsent" | \
958			timeout ${timeout_test} \
959				ip netns exec ${connector_ns} \
960					./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
961						$extra_cl_args $connect_addr > "$cout" &
962	else
963		tee "$cinsent" < "$cinfail" | \
964			timeout ${timeout_test} \
965				ip netns exec ${connector_ns} \
966					./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
967						$extra_cl_args $connect_addr > "$cout" &
968	fi
969	local cpid=$!
970
971	pm_nl_set_endpoint $listener_ns $connector_ns $connect_addr
972	check_cestab $listener_ns $connector_ns
973
974	wait $cpid
975	local retc=$?
976	wait $spid
977	local rets=$?
978
979	if $capture; then
980	    sleep 1
981	    kill $cappid
982	fi
983
984	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
985		nstat | grep Tcp > /tmp/${listener_ns}.out
986	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
987		nstat | grep Tcp > /tmp/${connector_ns}.out
988
989	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
990		fail_test "client exit code $retc, server $rets"
991		echo -e "\nnetns ${listener_ns} socket stat for ${port}:" 1>&2
992		ip netns exec ${listener_ns} ss -Menita 1>&2 -o "sport = :$port"
993		cat /tmp/${listener_ns}.out
994		echo -e "\nnetns ${connector_ns} socket stat for ${port}:" 1>&2
995		ip netns exec ${connector_ns} ss -Menita 1>&2 -o "dport = :$port"
996		cat /tmp/${connector_ns}.out
997
998		cat "$capout"
999		return 1
1000	fi
1001
1002	if [ "$test_linkfail" -gt 1 ];then
1003		check_transfer $sinfail $cout "file received by client" $trunc_size
1004	else
1005		check_transfer $sin $cout "file received by client" $trunc_size
1006	fi
1007	retc=$?
1008	if [ "$test_linkfail" -eq 0 ];then
1009		check_transfer $cin $sout "file received by server" $trunc_size
1010	else
1011		check_transfer $cinsent $sout "file received by server" $trunc_size
1012	fi
1013	rets=$?
1014
1015	if [ $retc -eq 0 ] && [ $rets -eq 0 ];then
1016		cat "$capout"
1017		return 0
1018	fi
1019
1020	cat "$capout"
1021	return 1
1022}
1023
1024make_file()
1025{
1026	local name=$1
1027	local who=$2
1028	local size=$3
1029
1030	mptcp_lib_make_file $name 1024 $size
1031
1032	print_info "Test file (size $size KB) for $who"
1033}
1034
1035run_tests()
1036{
1037	local listener_ns="$1"
1038	local connector_ns="$2"
1039	local connect_addr="$3"
1040
1041	local size
1042	local test_linkfail=${test_linkfail:-0}
1043
1044	# The values above 2 are reused to make test files
1045	# with the given sizes (KB)
1046	if [ "$test_linkfail" -gt 2 ]; then
1047		size=$test_linkfail
1048
1049		if [ -z "$cinfail" ]; then
1050			cinfail=$(mktemp)
1051		fi
1052		make_file "$cinfail" "client" $size
1053	# create the input file for the failure test when
1054	# the first failure test run
1055	elif [ "$test_linkfail" -ne 0 ] && [ -z "$cinfail" ]; then
1056		# the client file must be considerably larger
1057		# of the maximum expected cwin value, or the
1058		# link utilization will be not predicable
1059		size=$((RANDOM%2))
1060		size=$((size+1))
1061		size=$((size*8192))
1062		size=$((size + ( RANDOM % 8192) ))
1063
1064		cinfail=$(mktemp)
1065		make_file "$cinfail" "client" $size
1066	fi
1067
1068	if [ "$test_linkfail" -gt 2 ]; then
1069		size=$test_linkfail
1070
1071		if [ -z "$sinfail" ]; then
1072			sinfail=$(mktemp)
1073		fi
1074		make_file "$sinfail" "server" $size
1075	elif [ "$test_linkfail" -eq 2 ] && [ -z "$sinfail" ]; then
1076		size=$((RANDOM%16))
1077		size=$((size+1))
1078		size=$((size*2048))
1079
1080		sinfail=$(mktemp)
1081		make_file "$sinfail" "server" $size
1082	fi
1083
1084	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP ${connect_addr}
1085}
1086
1087dump_stats()
1088{
1089	echo Server ns stats
1090	ip netns exec $ns1 nstat -as | grep Tcp
1091	echo Client ns stats
1092	ip netns exec $ns2 nstat -as | grep Tcp
1093}
1094
1095chk_csum_nr()
1096{
1097	local csum_ns1=${1:-0}
1098	local csum_ns2=${2:-0}
1099	local count
1100	local extra_msg=""
1101	local allow_multi_errors_ns1=0
1102	local allow_multi_errors_ns2=0
1103
1104	if [[ "${csum_ns1}" = "+"* ]]; then
1105		allow_multi_errors_ns1=1
1106		csum_ns1=${csum_ns1:1}
1107	fi
1108	if [[ "${csum_ns2}" = "+"* ]]; then
1109		allow_multi_errors_ns2=1
1110		csum_ns2=${csum_ns2:1}
1111	fi
1112
1113	print_check "sum"
1114	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtDataCsumErr")
1115	if [ "$count" != "$csum_ns1" ]; then
1116		extra_msg+=" ns1=$count"
1117	fi
1118	if [ -z "$count" ]; then
1119		print_skip
1120	elif { [ "$count" != $csum_ns1 ] && [ $allow_multi_errors_ns1 -eq 0 ]; } ||
1121	   { [ "$count" -lt $csum_ns1 ] && [ $allow_multi_errors_ns1 -eq 1 ]; }; then
1122		fail_test "got $count data checksum error[s] expected $csum_ns1"
1123	else
1124		print_ok
1125	fi
1126	print_check "csum"
1127	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtDataCsumErr")
1128	if [ "$count" != "$csum_ns2" ]; then
1129		extra_msg+=" ns2=$count"
1130	fi
1131	if [ -z "$count" ]; then
1132		print_skip
1133	elif { [ "$count" != $csum_ns2 ] && [ $allow_multi_errors_ns2 -eq 0 ]; } ||
1134	   { [ "$count" -lt $csum_ns2 ] && [ $allow_multi_errors_ns2 -eq 1 ]; }; then
1135		fail_test "got $count data checksum error[s] expected $csum_ns2"
1136	else
1137		print_ok
1138	fi
1139
1140	print_info "$extra_msg"
1141}
1142
1143chk_fail_nr()
1144{
1145	local fail_tx=$1
1146	local fail_rx=$2
1147	local ns_invert=${3:-""}
1148	local count
1149	local ns_tx=$ns1
1150	local ns_rx=$ns2
1151	local extra_msg=""
1152	local allow_tx_lost=0
1153	local allow_rx_lost=0
1154
1155	if [[ $ns_invert = "invert" ]]; then
1156		ns_tx=$ns2
1157		ns_rx=$ns1
1158		extra_msg="invert"
1159	fi
1160
1161	if [[ "${fail_tx}" = "-"* ]]; then
1162		allow_tx_lost=1
1163		fail_tx=${fail_tx:1}
1164	fi
1165	if [[ "${fail_rx}" = "-"* ]]; then
1166		allow_rx_lost=1
1167		fail_rx=${fail_rx:1}
1168	fi
1169
1170	print_check "ftx"
1171	count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtMPFailTx")
1172	if [ "$count" != "$fail_tx" ]; then
1173		extra_msg+=",tx=$count"
1174	fi
1175	if [ -z "$count" ]; then
1176		print_skip
1177	elif { [ "$count" != "$fail_tx" ] && [ $allow_tx_lost -eq 0 ]; } ||
1178	   { [ "$count" -gt "$fail_tx" ] && [ $allow_tx_lost -eq 1 ]; }; then
1179		fail_test "got $count MP_FAIL[s] TX expected $fail_tx"
1180	else
1181		print_ok
1182	fi
1183
1184	print_check "failrx"
1185	count=$(mptcp_lib_get_counter ${ns_rx} "MPTcpExtMPFailRx")
1186	if [ "$count" != "$fail_rx" ]; then
1187		extra_msg+=",rx=$count"
1188	fi
1189	if [ -z "$count" ]; then
1190		print_skip
1191	elif { [ "$count" != "$fail_rx" ] && [ $allow_rx_lost -eq 0 ]; } ||
1192	   { [ "$count" -gt "$fail_rx" ] && [ $allow_rx_lost -eq 1 ]; }; then
1193		fail_test "got $count MP_FAIL[s] RX expected $fail_rx"
1194	else
1195		print_ok
1196	fi
1197
1198	print_info "$extra_msg"
1199}
1200
1201chk_fclose_nr()
1202{
1203	local fclose_tx=$1
1204	local fclose_rx=$2
1205	local ns_invert=$3
1206	local count
1207	local ns_tx=$ns2
1208	local ns_rx=$ns1
1209	local extra_msg=""
1210
1211	if [[ $ns_invert = "invert" ]]; then
1212		ns_tx=$ns1
1213		ns_rx=$ns2
1214		extra_msg="invert"
1215	fi
1216
1217	print_check "ctx"
1218	count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtMPFastcloseTx")
1219	if [ -z "$count" ]; then
1220		print_skip
1221	elif [ "$count" != "$fclose_tx" ]; then
1222		extra_msg+=",tx=$count"
1223		fail_test "got $count MP_FASTCLOSE[s] TX expected $fclose_tx"
1224	else
1225		print_ok
1226	fi
1227
1228	print_check "fclzrx"
1229	count=$(mptcp_lib_get_counter ${ns_rx} "MPTcpExtMPFastcloseRx")
1230	if [ -z "$count" ]; then
1231		print_skip
1232	elif [ "$count" != "$fclose_rx" ]; then
1233		extra_msg+=",rx=$count"
1234		fail_test "got $count MP_FASTCLOSE[s] RX expected $fclose_rx"
1235	else
1236		print_ok
1237	fi
1238
1239	print_info "$extra_msg"
1240}
1241
1242chk_rst_nr()
1243{
1244	local rst_tx=$1
1245	local rst_rx=$2
1246	local ns_invert=${3:-""}
1247	local count
1248	local ns_tx=$ns1
1249	local ns_rx=$ns2
1250	local extra_msg=""
1251
1252	if [[ $ns_invert = "invert" ]]; then
1253		ns_tx=$ns2
1254		ns_rx=$ns1
1255		extra_msg="invert"
1256	fi
1257
1258	print_check "rtx"
1259	count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtMPRstTx")
1260	if [ -z "$count" ]; then
1261		print_skip
1262	# accept more rst than expected except if we don't expect any
1263	elif { [ $rst_tx -ne 0 ] && [ $count -lt $rst_tx ]; } ||
1264	     { [ $rst_tx -eq 0 ] && [ $count -ne 0 ]; }; then
1265		fail_test "got $count MP_RST[s] TX expected $rst_tx"
1266	else
1267		print_ok
1268	fi
1269
1270	print_check "rstrx"
1271	count=$(mptcp_lib_get_counter ${ns_rx} "MPTcpExtMPRstRx")
1272	if [ -z "$count" ]; then
1273		print_skip
1274	# accept more rst than expected except if we don't expect any
1275	elif { [ $rst_rx -ne 0 ] && [ $count -lt $rst_rx ]; } ||
1276	     { [ $rst_rx -eq 0 ] && [ $count -ne 0 ]; }; then
1277		fail_test "got $count MP_RST[s] RX expected $rst_rx"
1278	else
1279		print_ok
1280	fi
1281
1282	print_info "$extra_msg"
1283}
1284
1285chk_infi_nr()
1286{
1287	local infi_tx=$1
1288	local infi_rx=$2
1289	local count
1290
1291	print_check "itx"
1292	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtInfiniteMapTx")
1293	if [ -z "$count" ]; then
1294		print_skip
1295	elif [ "$count" != "$infi_tx" ]; then
1296		fail_test "got $count infinite map[s] TX expected $infi_tx"
1297	else
1298		print_ok
1299	fi
1300
1301	print_check "infirx"
1302	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtInfiniteMapRx")
1303	if [ -z "$count" ]; then
1304		print_skip
1305	elif [ "$count" != "$infi_rx" ]; then
1306		fail_test "got $count infinite map[s] RX expected $infi_rx"
1307	else
1308		print_ok
1309	fi
1310}
1311
1312chk_join_nr()
1313{
1314	local syn_nr=$1
1315	local syn_ack_nr=$2
1316	local ack_nr=$3
1317	local csum_ns1=${4:-0}
1318	local csum_ns2=${5:-0}
1319	local fail_nr=${6:-0}
1320	local rst_nr=${7:-0}
1321	local infi_nr=${8:-0}
1322	local corrupted_pkts=${9:-0}
1323	local count
1324	local with_cookie
1325
1326	if [ "${corrupted_pkts}" -gt 0 ]; then
1327		print_info "${corrupted_pkts} corrupted pkts"
1328	fi
1329
1330	print_check "syn"
1331	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtMPJoinSynRx")
1332	if [ -z "$count" ]; then
1333		print_skip
1334	elif [ "$count" != "$syn_nr" ]; then
1335		fail_test "got $count JOIN[s] syn expected $syn_nr"
1336	else
1337		print_ok
1338	fi
1339
1340	print_check "synack"
1341	with_cookie=$(ip netns exec $ns2 sysctl -n net.ipv4.tcp_syncookies)
1342	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtMPJoinSynAckRx")
1343	if [ -z "$count" ]; then
1344		print_skip
1345	elif [ "$count" != "$syn_ack_nr" ]; then
1346		# simult connections exceeding the limit with cookie enabled could go up to
1347		# synack validation as the conn limit can be enforced reliably only after
1348		# the subflow creation
1349		if [ "$with_cookie" = 2 ] && [ "$count" -gt "$syn_ack_nr" ] && [ "$count" -le "$syn_nr" ]; then
1350			print_ok
1351		else
1352			fail_test "got $count JOIN[s] synack expected $syn_ack_nr"
1353		fi
1354	else
1355		print_ok
1356	fi
1357
1358	print_check "ack"
1359	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtMPJoinAckRx")
1360	if [ -z "$count" ]; then
1361		print_skip
1362	elif [ "$count" != "$ack_nr" ]; then
1363		fail_test "got $count JOIN[s] ack expected $ack_nr"
1364	else
1365		print_ok
1366	fi
1367	if $validate_checksum; then
1368		chk_csum_nr $csum_ns1 $csum_ns2
1369		chk_fail_nr $fail_nr $fail_nr
1370		chk_rst_nr $rst_nr $rst_nr
1371		chk_infi_nr $infi_nr $infi_nr
1372	fi
1373}
1374
1375# a negative value for 'stale_max' means no upper bound:
1376# for bidirectional transfer, if one peer sleep for a while
1377# - as these tests do - we can have a quite high number of
1378# stale/recover conversions, proportional to
1379# sleep duration/ MPTCP-level RTX interval.
1380chk_stale_nr()
1381{
1382	local ns=$1
1383	local stale_min=$2
1384	local stale_max=$3
1385	local stale_delta=$4
1386	local dump_stats
1387	local stale_nr
1388	local recover_nr
1389
1390	print_check "stale"
1391
1392	stale_nr=$(mptcp_lib_get_counter ${ns} "MPTcpExtSubflowStale")
1393	recover_nr=$(mptcp_lib_get_counter ${ns} "MPTcpExtSubflowRecover")
1394	if [ -z "$stale_nr" ] || [ -z "$recover_nr" ]; then
1395		print_skip
1396	elif [ $stale_nr -lt $stale_min ] ||
1397	   { [ $stale_max -gt 0 ] && [ $stale_nr -gt $stale_max ]; } ||
1398	   [ $((stale_nr - recover_nr)) -ne $stale_delta ]; then
1399		fail_test "got $stale_nr stale[s] $recover_nr recover[s], " \
1400		     " expected stale in range [$stale_min..$stale_max]," \
1401		     " stale-recover delta $stale_delta"
1402		dump_stats=1
1403	else
1404		print_ok
1405	fi
1406
1407	if [ "${dump_stats}" = 1 ]; then
1408		echo $ns stats
1409		ip netns exec $ns ip -s link show
1410		ip netns exec $ns nstat -as | grep MPTcp
1411	fi
1412}
1413
1414chk_add_nr()
1415{
1416	local add_nr=$1
1417	local echo_nr=$2
1418	local port_nr=${3:-0}
1419	local ns_invert=${4:-""}
1420	local syn_nr=$port_nr
1421	local syn_ack_nr=$port_nr
1422	local ack_nr=$port_nr
1423	local mis_syn_nr=0
1424	local mis_ack_nr=0
1425	local ns_tx=$ns1
1426	local ns_rx=$ns2
1427	local extra_msg=""
1428	local count
1429	local timeout
1430
1431	if [[ $ns_invert = "invert" ]]; then
1432		ns_tx=$ns2
1433		ns_rx=$ns1
1434		extra_msg="invert"
1435	fi
1436
1437	timeout=$(ip netns exec ${ns_tx} sysctl -n net.mptcp.add_addr_timeout)
1438
1439	print_check "add"
1440	count=$(mptcp_lib_get_counter ${ns_rx} "MPTcpExtAddAddr")
1441	if [ -z "$count" ]; then
1442		print_skip
1443	# if the test configured a short timeout tolerate greater then expected
1444	# add addrs options, due to retransmissions
1445	elif [ "$count" != "$add_nr" ] && { [ "$timeout" -gt 1 ] || [ "$count" -lt "$add_nr" ]; }; then
1446		fail_test "got $count ADD_ADDR[s] expected $add_nr"
1447	else
1448		print_ok
1449	fi
1450
1451	print_check "echo"
1452	count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtEchoAdd")
1453	if [ -z "$count" ]; then
1454		print_skip
1455	elif [ "$count" != "$echo_nr" ]; then
1456		fail_test "got $count ADD_ADDR echo[s] expected $echo_nr"
1457	else
1458		print_ok
1459	fi
1460
1461	if [ $port_nr -gt 0 ]; then
1462		print_check "pt"
1463		count=$(mptcp_lib_get_counter ${ns_rx} "MPTcpExtPortAdd")
1464		if [ -z "$count" ]; then
1465			print_skip
1466		elif [ "$count" != "$port_nr" ]; then
1467			fail_test "got $count ADD_ADDR[s] with a port-number expected $port_nr"
1468		else
1469			print_ok
1470		fi
1471
1472		print_check "syn"
1473		count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtMPJoinPortSynRx")
1474		if [ -z "$count" ]; then
1475			print_skip
1476		elif [ "$count" != "$syn_nr" ]; then
1477			fail_test "got $count JOIN[s] syn with a different \
1478				   port-number expected $syn_nr"
1479		else
1480			print_ok
1481		fi
1482
1483		print_check "synack"
1484		count=$(mptcp_lib_get_counter ${ns_rx} "MPTcpExtMPJoinPortSynAckRx")
1485		if [ -z "$count" ]; then
1486			print_skip
1487		elif [ "$count" != "$syn_ack_nr" ]; then
1488			fail_test "got $count JOIN[s] synack with a different \
1489				   port-number expected $syn_ack_nr"
1490		else
1491			print_ok
1492		fi
1493
1494		print_check "ack"
1495		count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtMPJoinPortAckRx")
1496		if [ -z "$count" ]; then
1497			print_skip
1498		elif [ "$count" != "$ack_nr" ]; then
1499			fail_test "got $count JOIN[s] ack with a different \
1500				   port-number expected $ack_nr"
1501		else
1502			print_ok
1503		fi
1504
1505		print_check "syn"
1506		count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtMismatchPortSynRx")
1507		if [ -z "$count" ]; then
1508			print_skip
1509		elif [ "$count" != "$mis_syn_nr" ]; then
1510			fail_test "got $count JOIN[s] syn with a mismatched \
1511				   port-number expected $mis_syn_nr"
1512		else
1513			print_ok
1514		fi
1515
1516		print_check "ack"
1517		count=$(mptcp_lib_get_counter ${ns_tx} "MPTcpExtMismatchPortAckRx")
1518		if [ -z "$count" ]; then
1519			print_skip
1520		elif [ "$count" != "$mis_ack_nr" ]; then
1521			fail_test "got $count JOIN[s] ack with a mismatched \
1522				   port-number expected $mis_ack_nr"
1523		else
1524			print_ok
1525		fi
1526	fi
1527
1528	print_info "$extra_msg"
1529}
1530
1531chk_add_tx_nr()
1532{
1533	local add_tx_nr=$1
1534	local echo_tx_nr=$2
1535	local timeout
1536	local count
1537
1538	timeout=$(ip netns exec $ns1 sysctl -n net.mptcp.add_addr_timeout)
1539
1540	print_check "add TX"
1541	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtAddAddrTx")
1542	if [ -z "$count" ]; then
1543		print_skip
1544	# if the test configured a short timeout tolerate greater then expected
1545	# add addrs options, due to retransmissions
1546	elif [ "$count" != "$add_tx_nr" ] && { [ "$timeout" -gt 1 ] || [ "$count" -lt "$add_tx_nr" ]; }; then
1547		fail_test "got $count ADD_ADDR[s] TX, expected $add_tx_nr"
1548	else
1549		print_ok
1550	fi
1551
1552	print_check "echo TX"
1553	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtEchoAddTx")
1554	if [ -z "$count" ]; then
1555		print_skip
1556	elif [ "$count" != "$echo_tx_nr" ]; then
1557		fail_test "got $count ADD_ADDR echo[s] TX, expected $echo_tx_nr"
1558	else
1559		print_ok
1560	fi
1561}
1562
1563chk_rm_nr()
1564{
1565	local rm_addr_nr=$1
1566	local rm_subflow_nr=$2
1567	local invert
1568	local simult
1569	local count
1570	local addr_ns=$ns1
1571	local subflow_ns=$ns2
1572	local extra_msg=""
1573
1574	shift 2
1575	while [ -n "$1" ]; do
1576		[ "$1" = "invert" ] && invert=true
1577		[ "$1" = "simult" ] && simult=true
1578		shift
1579	done
1580
1581	if [ -z $invert ]; then
1582		addr_ns=$ns1
1583		subflow_ns=$ns2
1584	elif [ $invert = "true" ]; then
1585		addr_ns=$ns2
1586		subflow_ns=$ns1
1587		extra_msg="invert"
1588	fi
1589
1590	print_check "rm"
1591	count=$(mptcp_lib_get_counter ${addr_ns} "MPTcpExtRmAddr")
1592	if [ -z "$count" ]; then
1593		print_skip
1594	elif [ "$count" != "$rm_addr_nr" ]; then
1595		fail_test "got $count RM_ADDR[s] expected $rm_addr_nr"
1596	else
1597		print_ok
1598	fi
1599
1600	print_check "rmsf"
1601	count=$(mptcp_lib_get_counter ${subflow_ns} "MPTcpExtRmSubflow")
1602	if [ -z "$count" ]; then
1603		print_skip
1604	elif [ -n "$simult" ]; then
1605		local cnt suffix
1606
1607		cnt=$(mptcp_lib_get_counter ${addr_ns} "MPTcpExtRmSubflow")
1608
1609		# in case of simult flush, the subflow removal count on each side is
1610		# unreliable
1611		count=$((count + cnt))
1612		if [ "$count" != "$rm_subflow_nr" ]; then
1613			suffix="$count in [$rm_subflow_nr:$((rm_subflow_nr*2))]"
1614			extra_msg+=" simult"
1615		fi
1616		if [ $count -ge "$rm_subflow_nr" ] && \
1617		   [ "$count" -le "$((rm_subflow_nr *2 ))" ]; then
1618			print_ok "$suffix"
1619		else
1620			fail_test "got $count RM_SUBFLOW[s] expected in range [$rm_subflow_nr:$((rm_subflow_nr*2))]"
1621		fi
1622	elif [ "$count" != "$rm_subflow_nr" ]; then
1623		fail_test "got $count RM_SUBFLOW[s] expected $rm_subflow_nr"
1624	else
1625		print_ok
1626	fi
1627
1628	print_info "$extra_msg"
1629}
1630
1631chk_rm_tx_nr()
1632{
1633	local rm_addr_tx_nr=$1
1634
1635	print_check "rm TX"
1636	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtRmAddrTx")
1637	if [ -z "$count" ]; then
1638		print_skip
1639	elif [ "$count" != "$rm_addr_tx_nr" ]; then
1640		fail_test "got $count RM_ADDR[s] expected $rm_addr_tx_nr"
1641	else
1642		print_ok
1643	fi
1644}
1645
1646chk_prio_nr()
1647{
1648	local mp_prio_nr_tx=$1
1649	local mp_prio_nr_rx=$2
1650	local mpj_syn=$3
1651	local mpj_syn_ack=$4
1652	local count
1653
1654	print_check "ptx"
1655	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtMPPrioTx")
1656	if [ -z "$count" ]; then
1657		print_skip
1658	elif [ "$count" != "$mp_prio_nr_tx" ]; then
1659		fail_test "got $count MP_PRIO[s] TX expected $mp_prio_nr_tx"
1660	else
1661		print_ok
1662	fi
1663
1664	print_check "prx"
1665	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtMPPrioRx")
1666	if [ -z "$count" ]; then
1667		print_skip
1668	elif [ "$count" != "$mp_prio_nr_rx" ]; then
1669		fail_test "got $count MP_PRIO[s] RX expected $mp_prio_nr_rx"
1670	else
1671		print_ok
1672	fi
1673
1674	print_check "syn backup"
1675	count=$(mptcp_lib_get_counter ${ns1} "MPTcpExtMPJoinSynBackupRx")
1676	if [ -z "$count" ]; then
1677		print_skip
1678	elif [ "$count" != "$mpj_syn" ]; then
1679		fail_test "got $count JOIN[s] syn with Backup expected $mpj_syn"
1680	else
1681		print_ok
1682	fi
1683
1684	print_check "synack backup"
1685	count=$(mptcp_lib_get_counter ${ns2} "MPTcpExtMPJoinSynAckBackupRx")
1686	if [ -z "$count" ]; then
1687		print_skip
1688	elif [ "$count" != "$mpj_syn_ack" ]; then
1689		fail_test "got $count JOIN[s] synack with Backup expected $mpj_syn_ack"
1690	else
1691		print_ok
1692	fi
1693}
1694
1695chk_subflow_nr()
1696{
1697	local msg="$1"
1698	local subflow_nr=$2
1699	local cnt1
1700	local cnt2
1701	local dump_stats
1702
1703	print_check "${msg}"
1704
1705	cnt1=$(ss -N $ns1 -tOni | grep -c token)
1706	cnt2=$(ss -N $ns2 -tOni | grep -c token)
1707	if [ "$cnt1" != "$subflow_nr" ] || [ "$cnt2" != "$subflow_nr" ]; then
1708		fail_test "got $cnt1:$cnt2 subflows expected $subflow_nr"
1709		dump_stats=1
1710	else
1711		print_ok
1712	fi
1713
1714	if [ "${dump_stats}" = 1 ]; then
1715		ss -N $ns1 -tOni
1716		ss -N $ns1 -tOni | grep token
1717		ip -n $ns1 mptcp endpoint
1718	fi
1719}
1720
1721chk_mptcp_info()
1722{
1723	local info1=$1
1724	local exp1=$2
1725	local info2=$3
1726	local exp2=$4
1727	local cnt1
1728	local cnt2
1729	local dump_stats
1730
1731	print_check "mptcp_info ${info1:0:15}=$exp1:$exp2"
1732
1733	cnt1=$(ss -N $ns1 -inmHM | mptcp_lib_get_info_value "$info1" "$info1")
1734	cnt2=$(ss -N $ns2 -inmHM | mptcp_lib_get_info_value "$info2" "$info2")
1735	# 'ss' only display active connections and counters that are not 0.
1736	[ -z "$cnt1" ] && cnt1=0
1737	[ -z "$cnt2" ] && cnt2=0
1738
1739	if [ "$cnt1" != "$exp1" ] || [ "$cnt2" != "$exp2" ]; then
1740		fail_test "got $cnt1:$cnt2 $info1:$info2 expected $exp1:$exp2"
1741		dump_stats=1
1742	else
1743		print_ok
1744	fi
1745
1746	if [ "$dump_stats" = 1 ]; then
1747		ss -N $ns1 -inmHM
1748		ss -N $ns2 -inmHM
1749	fi
1750}
1751
1752# $1: subflows in ns1 ; $2: subflows in ns2
1753# number of all subflows, including the initial subflow.
1754chk_subflows_total()
1755{
1756	local cnt1
1757	local cnt2
1758	local info="subflows_total"
1759	local dump_stats
1760
1761	# if subflows_total counter is supported, use it:
1762	if [ -n "$(ss -N $ns1 -inmHM | mptcp_lib_get_info_value $info $info)" ]; then
1763		chk_mptcp_info $info $1 $info $2
1764		return
1765	fi
1766
1767	print_check "$info $1:$2"
1768
1769	# if not, count the TCP connections that are in fact MPTCP subflows
1770	cnt1=$(ss -N $ns1 -ti state established state syn-sent state syn-recv |
1771	       grep -c tcp-ulp-mptcp)
1772	cnt2=$(ss -N $ns2 -ti state established state syn-sent state syn-recv |
1773	       grep -c tcp-ulp-mptcp)
1774
1775	if [ "$1" != "$cnt1" ] || [ "$2" != "$cnt2" ]; then
1776		fail_test "got subflows $cnt1:$cnt2 expected $1:$2"
1777		dump_stats=1
1778	else
1779		print_ok
1780	fi
1781
1782	if [ "$dump_stats" = 1 ]; then
1783		ss -N $ns1 -ti
1784		ss -N $ns2 -ti
1785	fi
1786}
1787
1788chk_link_usage()
1789{
1790	local ns=$1
1791	local link=$2
1792	local out=$3
1793	local expected_rate=$4
1794
1795	local tx_link tx_total
1796	tx_link=$(ip netns exec $ns cat /sys/class/net/$link/statistics/tx_bytes)
1797	tx_total=$(stat --format=%s $out)
1798	local tx_rate=$((tx_link * 100 / tx_total))
1799	local tolerance=5
1800
1801	print_check "link usage"
1802	if [ $tx_rate -lt $((expected_rate - tolerance)) ] || \
1803	   [ $tx_rate -gt $((expected_rate + tolerance)) ]; then
1804		fail_test "got $tx_rate% usage, expected $expected_rate%"
1805	else
1806		print_ok
1807	fi
1808}
1809
1810wait_attempt_fail()
1811{
1812	local timeout_ms=$((timeout_poll * 1000))
1813	local time=0
1814	local ns=$1
1815
1816	while [ $time -lt $timeout_ms ]; do
1817		local cnt
1818
1819		cnt=$(mptcp_lib_get_counter ${ns} "TcpAttemptFails")
1820
1821		[ "$cnt" = 1 ] && return 1
1822		time=$((time + 100))
1823		sleep 0.1
1824	done
1825	return 1
1826}
1827
1828set_userspace_pm()
1829{
1830	local ns=$1
1831
1832	ip netns exec $ns sysctl -q net.mptcp.pm_type=1
1833}
1834
1835subflows_tests()
1836{
1837	if reset "no JOIN"; then
1838		run_tests $ns1 $ns2 10.0.1.1
1839		chk_join_nr 0 0 0
1840	fi
1841
1842	# subflow limited by client
1843	if reset "single subflow, limited by client"; then
1844		pm_nl_set_limits $ns1 0 0
1845		pm_nl_set_limits $ns2 0 0
1846		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1847		run_tests $ns1 $ns2 10.0.1.1
1848		chk_join_nr 0 0 0
1849	fi
1850
1851	# subflow limited by server
1852	if reset "single subflow, limited by server"; then
1853		pm_nl_set_limits $ns1 0 0
1854		pm_nl_set_limits $ns2 0 1
1855		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1856		run_tests $ns1 $ns2 10.0.1.1
1857		chk_join_nr 1 1 0
1858	fi
1859
1860	# subflow
1861	if reset "single subflow"; then
1862		pm_nl_set_limits $ns1 0 1
1863		pm_nl_set_limits $ns2 0 1
1864		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1865		run_tests $ns1 $ns2 10.0.1.1
1866		chk_join_nr 1 1 1
1867	fi
1868
1869	# multiple subflows
1870	if reset "multiple subflows"; then
1871		pm_nl_set_limits $ns1 0 2
1872		pm_nl_set_limits $ns2 0 2
1873		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1874		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1875		run_tests $ns1 $ns2 10.0.1.1
1876		chk_join_nr 2 2 2
1877	fi
1878
1879	# multiple subflows limited by server
1880	if reset "multiple subflows, limited by server"; then
1881		pm_nl_set_limits $ns1 0 1
1882		pm_nl_set_limits $ns2 0 2
1883		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1884		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1885		run_tests $ns1 $ns2 10.0.1.1
1886		chk_join_nr 2 2 1
1887	fi
1888
1889	# single subflow, dev
1890	if reset "single subflow, dev"; then
1891		pm_nl_set_limits $ns1 0 1
1892		pm_nl_set_limits $ns2 0 1
1893		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow dev ns2eth3
1894		run_tests $ns1 $ns2 10.0.1.1
1895		chk_join_nr 1 1 1
1896	fi
1897}
1898
1899subflows_error_tests()
1900{
1901	# If a single subflow is configured, and matches the MPC src
1902	# address, no additional subflow should be created
1903	if reset "no MPC reuse with single endpoint"; then
1904		pm_nl_set_limits $ns1 0 1
1905		pm_nl_set_limits $ns2 0 1
1906		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow
1907		speed=slow \
1908			run_tests $ns1 $ns2 10.0.1.1
1909		chk_join_nr 0 0 0
1910	fi
1911
1912	# multiple subflows, with subflow creation error
1913	if reset_with_tcp_filter "multi subflows, with failing subflow" ns1 10.0.3.2 REJECT &&
1914	   continue_if mptcp_lib_kallsyms_has "mptcp_pm_subflow_check_next$"; then
1915		pm_nl_set_limits $ns1 0 2
1916		pm_nl_set_limits $ns2 0 2
1917		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1918		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1919		speed=slow \
1920			run_tests $ns1 $ns2 10.0.1.1
1921		chk_join_nr 1 1 1
1922	fi
1923
1924	# multiple subflows, with subflow timeout on MPJ
1925	if reset_with_tcp_filter "multi subflows, with subflow timeout" ns1 10.0.3.2 DROP &&
1926	   continue_if mptcp_lib_kallsyms_has "mptcp_pm_subflow_check_next$"; then
1927		pm_nl_set_limits $ns1 0 2
1928		pm_nl_set_limits $ns2 0 2
1929		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1930		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1931		speed=slow \
1932			run_tests $ns1 $ns2 10.0.1.1
1933		chk_join_nr 1 1 1
1934	fi
1935
1936	# multiple subflows, check that the endpoint corresponding to
1937	# closed subflow (due to reset) is not reused if additional
1938	# subflows are added later
1939	if reset_with_tcp_filter "multi subflows, fair usage on close" ns1 10.0.3.2 REJECT &&
1940	   continue_if mptcp_lib_kallsyms_has "mptcp_pm_subflow_check_next$"; then
1941		pm_nl_set_limits $ns1 0 1
1942		pm_nl_set_limits $ns2 0 1
1943		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1944		speed=slow \
1945			run_tests $ns1 $ns2 10.0.1.1 &
1946
1947		# mpj subflow will be in TW after the reset
1948		wait_attempt_fail $ns2
1949		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
1950		wait
1951
1952		# additional subflow could be created only if the PM select
1953		# the later endpoint, skipping the already used one
1954		chk_join_nr 1 1 1
1955	fi
1956}
1957
1958signal_address_tests()
1959{
1960	# add_address, unused
1961	if reset "unused signal address"; then
1962		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
1963		run_tests $ns1 $ns2 10.0.1.1
1964		chk_join_nr 0 0 0
1965		chk_add_tx_nr 1 1
1966		chk_add_nr 1 1
1967	fi
1968
1969	# accept and use add_addr
1970	if reset "signal address"; then
1971		pm_nl_set_limits $ns1 0 1
1972		pm_nl_set_limits $ns2 1 1
1973		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
1974		run_tests $ns1 $ns2 10.0.1.1
1975		chk_join_nr 1 1 1
1976		chk_add_nr 1 1
1977	fi
1978
1979	# accept and use add_addr with an additional subflow
1980	# note: signal address in server ns and local addresses in client ns must
1981	# belong to different subnets or one of the listed local address could be
1982	# used for 'add_addr' subflow
1983	if reset "subflow and signal"; then
1984		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
1985		pm_nl_set_limits $ns1 0 2
1986		pm_nl_set_limits $ns2 1 2
1987		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
1988		run_tests $ns1 $ns2 10.0.1.1
1989		chk_join_nr 2 2 2
1990		chk_add_nr 1 1
1991	fi
1992
1993	# uncommon: subflow and signal flags on the same endpoint
1994	# or because the user wrongly picked both, but still expects the client
1995	# to create additional subflows
1996	if reset "subflow and signal together"; then
1997		pm_nl_set_limits $ns1 0 2
1998		pm_nl_set_limits $ns2 0 2
1999		pm_nl_add_endpoint $ns2 10.0.3.2 flags signal,subflow
2000		run_tests $ns1 $ns2 10.0.1.1
2001		chk_join_nr 1 1 1
2002		chk_add_nr 1 1 0 invert  # only initiated by ns2
2003		chk_add_nr 0 0 0         # none initiated by ns1
2004		chk_rst_nr 0 0 invert    # no RST sent by the client
2005		chk_rst_nr 0 0           # no RST sent by the server
2006	fi
2007
2008	# accept and use add_addr with additional subflows
2009	if reset "multiple subflows and signal"; then
2010		pm_nl_set_limits $ns1 0 3
2011		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2012		pm_nl_set_limits $ns2 1 3
2013		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2014		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2015		run_tests $ns1 $ns2 10.0.1.1
2016		chk_join_nr 3 3 3
2017		chk_add_nr 1 1
2018	fi
2019
2020	# signal addresses
2021	if reset "signal addresses"; then
2022		pm_nl_set_limits $ns1 3 3
2023		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2024		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2025		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
2026		pm_nl_set_limits $ns2 3 3
2027		run_tests $ns1 $ns2 10.0.1.1
2028		chk_join_nr 3 3 3
2029		chk_add_nr 3 3
2030	fi
2031
2032	# signal invalid addresses
2033	if reset "signal invalid addresses"; then
2034		pm_nl_set_limits $ns1 3 3
2035		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
2036		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2037		pm_nl_add_endpoint $ns1 10.0.14.1 flags signal
2038		pm_nl_set_limits $ns2 3 3
2039		run_tests $ns1 $ns2 10.0.1.1
2040		chk_join_nr 1 1 1
2041		chk_add_nr 3 3
2042	fi
2043
2044	# signal addresses race test
2045	if reset "signal addresses race test"; then
2046		pm_nl_set_limits $ns1 4 4
2047		pm_nl_set_limits $ns2 4 4
2048		pm_nl_add_endpoint $ns1 10.0.1.1 flags signal
2049		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2050		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2051		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
2052		pm_nl_add_endpoint $ns2 10.0.1.2 flags signal
2053		pm_nl_add_endpoint $ns2 10.0.2.2 flags signal
2054		pm_nl_add_endpoint $ns2 10.0.3.2 flags signal
2055		pm_nl_add_endpoint $ns2 10.0.4.2 flags signal
2056
2057		# the peer could possibly miss some addr notification, allow retransmission
2058		ip netns exec $ns1 sysctl -q net.mptcp.add_addr_timeout=1
2059		speed=slow \
2060			run_tests $ns1 $ns2 10.0.1.1
2061
2062		# It is not directly linked to the commit introducing this
2063		# symbol but for the parent one which is linked anyway.
2064		if ! mptcp_lib_kallsyms_has "mptcp_pm_subflow_check_next$"; then
2065			chk_join_nr 3 3 2
2066			chk_add_nr 4 4
2067		else
2068			chk_join_nr 3 3 3
2069			# the server will not signal the address terminating
2070			# the MPC subflow
2071			chk_add_nr 3 3
2072		fi
2073	fi
2074}
2075
2076link_failure_tests()
2077{
2078	# accept and use add_addr with additional subflows and link loss
2079	if reset "multiple flows, signal, link failure"; then
2080		# without any b/w limit each veth could spool the packets and get
2081		# them acked at xmit time, so that the corresponding subflow will
2082		# have almost always no outstanding pkts, the scheduler will pick
2083		# always the first subflow and we will have hard time testing
2084		# active backup and link switch-over.
2085		# Let's set some arbitrary (low) virtual link limits.
2086		init_shapers
2087		pm_nl_set_limits $ns1 0 3
2088		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
2089		pm_nl_set_limits $ns2 1 3
2090		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow
2091		pm_nl_add_endpoint $ns2 10.0.4.2 dev ns2eth4 flags subflow
2092		test_linkfail=1 \
2093			run_tests $ns1 $ns2 10.0.1.1
2094		chk_join_nr 3 3 3
2095		chk_add_nr 1 1
2096		chk_stale_nr $ns2 1 5 1
2097	fi
2098
2099	# accept and use add_addr with additional subflows and link loss
2100	# for bidirectional transfer
2101	if reset "multi flows, signal, bidi, link fail"; then
2102		init_shapers
2103		pm_nl_set_limits $ns1 0 3
2104		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
2105		pm_nl_set_limits $ns2 1 3
2106		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow
2107		pm_nl_add_endpoint $ns2 10.0.4.2 dev ns2eth4 flags subflow
2108		test_linkfail=2 \
2109			run_tests $ns1 $ns2 10.0.1.1
2110		chk_join_nr 3 3 3
2111		chk_add_nr 1 1
2112		chk_stale_nr $ns2 1 -1 1
2113	fi
2114
2115	# 2 subflows plus 1 backup subflow with a lossy link, backup
2116	# will never be used
2117	if reset "backup subflow unused, link failure"; then
2118		init_shapers
2119		pm_nl_set_limits $ns1 0 2
2120		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
2121		pm_nl_set_limits $ns2 1 2
2122		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow,backup
2123		FAILING_LINKS="1" test_linkfail=1 \
2124			run_tests $ns1 $ns2 10.0.1.1
2125		chk_join_nr 2 2 2
2126		chk_add_nr 1 1
2127		chk_link_usage $ns2 ns2eth3 $cinsent 0
2128	fi
2129
2130	# 2 lossy links after half transfer, backup will get half of
2131	# the traffic
2132	if reset "backup flow used, multi links fail"; then
2133		init_shapers
2134		pm_nl_set_limits $ns1 0 2
2135		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
2136		pm_nl_set_limits $ns2 1 2
2137		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow,backup
2138		FAILING_LINKS="1 2" test_linkfail=1 \
2139			run_tests $ns1 $ns2 10.0.1.1
2140		chk_join_nr 2 2 2
2141		chk_add_nr 1 1
2142		chk_stale_nr $ns2 2 4 2
2143		chk_link_usage $ns2 ns2eth3 $cinsent 50
2144	fi
2145
2146	# use a backup subflow with the first subflow on a lossy link
2147	# for bidirectional transfer
2148	if reset "backup flow used, bidi, link failure"; then
2149		init_shapers
2150		pm_nl_set_limits $ns1 0 2
2151		pm_nl_add_endpoint $ns1 10.0.2.1 dev ns1eth2 flags signal
2152		pm_nl_set_limits $ns2 1 3
2153		pm_nl_add_endpoint $ns2 10.0.3.2 dev ns2eth3 flags subflow,backup
2154		FAILING_LINKS="1 2" test_linkfail=2 \
2155			run_tests $ns1 $ns2 10.0.1.1
2156		chk_join_nr 2 2 2
2157		chk_add_nr 1 1
2158		chk_stale_nr $ns2 1 -1 2
2159		chk_link_usage $ns2 ns2eth3 $cinsent 50
2160	fi
2161}
2162
2163add_addr_timeout_tests()
2164{
2165	# add_addr timeout
2166	if reset_with_add_addr_timeout "signal address, ADD_ADDR timeout"; then
2167		pm_nl_set_limits $ns1 0 1
2168		pm_nl_set_limits $ns2 1 1
2169		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2170		speed=slow \
2171			run_tests $ns1 $ns2 10.0.1.1
2172		chk_join_nr 1 1 1
2173		chk_add_tx_nr 4 4
2174		chk_add_nr 4 0
2175	fi
2176
2177	# add_addr timeout IPv6
2178	if reset_with_add_addr_timeout "signal address, ADD_ADDR6 timeout" 6; then
2179		pm_nl_set_limits $ns1 0 1
2180		pm_nl_set_limits $ns2 1 1
2181		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2182		speed=slow \
2183			run_tests $ns1 $ns2 dead:beef:1::1
2184		chk_join_nr 1 1 1
2185		chk_add_nr 4 0
2186	fi
2187
2188	# signal addresses timeout
2189	if reset_with_add_addr_timeout "signal addresses, ADD_ADDR timeout"; then
2190		pm_nl_set_limits $ns1 2 2
2191		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2192		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2193		pm_nl_set_limits $ns2 2 2
2194		speed=10 \
2195			run_tests $ns1 $ns2 10.0.1.1
2196		chk_join_nr 2 2 2
2197		chk_add_nr 8 0
2198	fi
2199
2200	# signal invalid addresses timeout
2201	if reset_with_add_addr_timeout "invalid address, ADD_ADDR timeout"; then
2202		pm_nl_set_limits $ns1 2 2
2203		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
2204		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2205		pm_nl_set_limits $ns2 2 2
2206		speed=10 \
2207			run_tests $ns1 $ns2 10.0.1.1
2208		chk_join_nr 1 1 1
2209		chk_add_nr 8 0
2210	fi
2211}
2212
2213remove_tests()
2214{
2215	# single subflow, remove
2216	if reset "remove single subflow"; then
2217		pm_nl_set_limits $ns1 0 1
2218		pm_nl_set_limits $ns2 0 1
2219		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2220		addr_nr_ns2=-1 speed=slow \
2221			run_tests $ns1 $ns2 10.0.1.1
2222		chk_join_nr 1 1 1
2223		chk_rm_tx_nr 1
2224		chk_rm_nr 1 1
2225		chk_rst_nr 0 0
2226	fi
2227
2228	# multiple subflows, remove
2229	if reset "remove multiple subflows"; then
2230		pm_nl_set_limits $ns1 0 2
2231		pm_nl_set_limits $ns2 0 2
2232		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
2233		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2234		addr_nr_ns2=-2 speed=slow \
2235			run_tests $ns1 $ns2 10.0.1.1
2236		chk_join_nr 2 2 2
2237		chk_rm_nr 2 2
2238		chk_rst_nr 0 0
2239	fi
2240
2241	# single address, remove
2242	if reset "remove single address"; then
2243		pm_nl_set_limits $ns1 0 1
2244		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2245		pm_nl_set_limits $ns2 1 1
2246		addr_nr_ns1=-1 speed=slow \
2247			run_tests $ns1 $ns2 10.0.1.1
2248		chk_join_nr 1 1 1
2249		chk_add_nr 1 1
2250		chk_rm_nr 1 1 invert
2251		chk_rst_nr 0 0
2252	fi
2253
2254	# subflow and signal, remove
2255	if reset "remove subflow and signal"; then
2256		pm_nl_set_limits $ns1 0 2
2257		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2258		pm_nl_set_limits $ns2 1 2
2259		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2260		addr_nr_ns1=-1 addr_nr_ns2=-1 speed=slow \
2261			run_tests $ns1 $ns2 10.0.1.1
2262		chk_join_nr 2 2 2
2263		chk_add_nr 1 1
2264		chk_rm_nr 1 1
2265		chk_rst_nr 0 0
2266	fi
2267
2268	# subflows and signal, remove
2269	if reset "remove subflows and signal"; then
2270		pm_nl_set_limits $ns1 0 3
2271		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2272		pm_nl_set_limits $ns2 1 3
2273		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2274		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2275		addr_nr_ns1=-1 addr_nr_ns2=-2 speed=10 \
2276			run_tests $ns1 $ns2 10.0.1.1
2277		chk_join_nr 3 3 3
2278		chk_add_nr 1 1
2279		chk_rm_nr 2 2
2280		chk_rst_nr 0 0
2281	fi
2282
2283	# addresses remove
2284	if reset "remove addresses"; then
2285		pm_nl_set_limits $ns1 3 3
2286		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal id 250
2287		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2288		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
2289		pm_nl_set_limits $ns2 3 3
2290		addr_nr_ns1=-3 speed=10 \
2291			run_tests $ns1 $ns2 10.0.1.1
2292		chk_join_nr 3 3 3
2293		chk_add_nr 3 3
2294		chk_rm_nr 3 3 invert
2295		chk_rst_nr 0 0
2296	fi
2297
2298	# invalid addresses remove
2299	if reset "remove invalid addresses"; then
2300		pm_nl_set_limits $ns1 3 3
2301		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
2302		# broadcast IP: no packet for this address will be received on ns1
2303		pm_nl_add_endpoint $ns1 224.0.0.1 flags signal
2304		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2305		pm_nl_set_limits $ns2 2 2
2306		addr_nr_ns1=-3 speed=10 \
2307			run_tests $ns1 $ns2 10.0.1.1
2308		chk_join_nr 1 1 1
2309		chk_add_nr 3 3
2310		chk_rm_nr 3 1 invert
2311		chk_rst_nr 0 0
2312	fi
2313
2314	# subflows and signal, flush
2315	if reset "flush subflows and signal"; then
2316		pm_nl_set_limits $ns1 0 3
2317		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2318		pm_nl_set_limits $ns2 1 3
2319		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2320		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2321		addr_nr_ns1=-8 addr_nr_ns2=-8 speed=slow \
2322			run_tests $ns1 $ns2 10.0.1.1
2323		chk_join_nr 3 3 3
2324		chk_add_nr 1 1
2325		chk_rm_nr 1 3 invert simult
2326		chk_rst_nr 0 0
2327	fi
2328
2329	# subflows flush
2330	if reset "flush subflows"; then
2331		pm_nl_set_limits $ns1 3 3
2332		pm_nl_set_limits $ns2 3 3
2333		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow id 150
2334		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2335		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2336		addr_nr_ns1=-8 addr_nr_ns2=-8 speed=slow \
2337			run_tests $ns1 $ns2 10.0.1.1
2338		chk_join_nr 3 3 3
2339
2340		if mptcp_lib_kversion_ge 5.18; then
2341			chk_rm_tx_nr 0
2342			chk_rm_nr 0 3 simult
2343		else
2344			chk_rm_nr 3 3
2345		fi
2346		chk_rst_nr 0 0
2347	fi
2348
2349	# addresses flush
2350	if reset "flush addresses"; then
2351		pm_nl_set_limits $ns1 3 3
2352		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal id 250
2353		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2354		pm_nl_add_endpoint $ns1 10.0.4.1 flags signal
2355		pm_nl_set_limits $ns2 3 3
2356		addr_nr_ns1=-8 addr_nr_ns2=-8 speed=slow \
2357			run_tests $ns1 $ns2 10.0.1.1
2358		chk_join_nr 3 3 3
2359		chk_add_nr 3 3
2360		chk_rm_nr 3 3 invert simult
2361		chk_rst_nr 0 0
2362	fi
2363
2364	# invalid addresses flush
2365	if reset "flush invalid addresses"; then
2366		pm_nl_set_limits $ns1 3 3
2367		pm_nl_add_endpoint $ns1 10.0.12.1 flags signal
2368		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal
2369		pm_nl_add_endpoint $ns1 10.0.14.1 flags signal
2370		pm_nl_set_limits $ns2 3 3
2371		addr_nr_ns1=-8 speed=slow \
2372			run_tests $ns1 $ns2 10.0.1.1
2373		chk_join_nr 1 1 1
2374		chk_add_nr 3 3
2375		chk_rm_nr 3 1 invert
2376		chk_rst_nr 0 0
2377	fi
2378
2379	# remove id 0 subflow
2380	if reset "remove id 0 subflow"; then
2381		pm_nl_set_limits $ns1 0 1
2382		pm_nl_set_limits $ns2 0 1
2383		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2384		addr_nr_ns2=-9 speed=slow \
2385			run_tests $ns1 $ns2 10.0.1.1
2386		chk_join_nr 1 1 1
2387		chk_rm_nr 1 1
2388		chk_rst_nr 0 0
2389	fi
2390
2391	# remove id 0 address
2392	if reset "remove id 0 address"; then
2393		pm_nl_set_limits $ns1 0 1
2394		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2395		pm_nl_set_limits $ns2 1 1
2396		addr_nr_ns1=-9 speed=slow \
2397			run_tests $ns1 $ns2 10.0.1.1
2398		chk_join_nr 1 1 1
2399		chk_add_nr 1 1
2400		chk_rm_nr 1 1 invert
2401		chk_rst_nr 0 0 invert
2402	fi
2403}
2404
2405add_tests()
2406{
2407	# add single subflow
2408	if reset "add single subflow"; then
2409		pm_nl_set_limits $ns1 0 1
2410		pm_nl_set_limits $ns2 0 1
2411		addr_nr_ns2=1 speed=slow cestab_ns2=1 \
2412			run_tests $ns1 $ns2 10.0.1.1
2413		chk_join_nr 1 1 1
2414		chk_cestab_nr $ns2 0
2415	fi
2416
2417	# add signal address
2418	if reset "add signal address"; then
2419		pm_nl_set_limits $ns1 0 1
2420		pm_nl_set_limits $ns2 1 1
2421		addr_nr_ns1=1 speed=slow cestab_ns1=1 \
2422			run_tests $ns1 $ns2 10.0.1.1
2423		chk_join_nr 1 1 1
2424		chk_add_nr 1 1
2425		chk_cestab_nr $ns1 0
2426	fi
2427
2428	# add multiple subflows
2429	if reset "add multiple subflows"; then
2430		pm_nl_set_limits $ns1 0 2
2431		pm_nl_set_limits $ns2 0 2
2432		addr_nr_ns2=2 speed=slow cestab_ns2=1 \
2433			run_tests $ns1 $ns2 10.0.1.1
2434		chk_join_nr 2 2 2
2435		chk_cestab_nr $ns2 0
2436	fi
2437
2438	# add multiple subflows IPv6
2439	if reset "add multiple subflows IPv6"; then
2440		pm_nl_set_limits $ns1 0 2
2441		pm_nl_set_limits $ns2 0 2
2442		addr_nr_ns2=2 speed=slow cestab_ns2=1 \
2443			run_tests $ns1 $ns2 dead:beef:1::1
2444		chk_join_nr 2 2 2
2445		chk_cestab_nr $ns2 0
2446	fi
2447
2448	# add multiple addresses IPv6
2449	if reset "add multiple addresses IPv6"; then
2450		pm_nl_set_limits $ns1 0 2
2451		pm_nl_set_limits $ns2 2 2
2452		addr_nr_ns1=2 speed=slow cestab_ns1=1 \
2453			run_tests $ns1 $ns2 dead:beef:1::1
2454		chk_join_nr 2 2 2
2455		chk_add_nr 2 2
2456		chk_cestab_nr $ns1 0
2457	fi
2458}
2459
2460ipv6_tests()
2461{
2462	# subflow IPv6
2463	if reset "single subflow IPv6"; then
2464		pm_nl_set_limits $ns1 0 1
2465		pm_nl_set_limits $ns2 0 1
2466		pm_nl_add_endpoint $ns2 dead:beef:3::2 dev ns2eth3 flags subflow
2467		speed=slow \
2468			run_tests $ns1 $ns2 dead:beef:1::1
2469		chk_join_nr 1 1 1
2470	fi
2471
2472	# add_address, unused IPv6
2473	if reset "unused signal address IPv6"; then
2474		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2475		speed=slow \
2476			run_tests $ns1 $ns2 dead:beef:1::1
2477		chk_join_nr 0 0 0
2478		chk_add_nr 1 1
2479	fi
2480
2481	# signal address IPv6
2482	if reset "single address IPv6"; then
2483		pm_nl_set_limits $ns1 0 1
2484		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2485		pm_nl_set_limits $ns2 1 1
2486		speed=slow \
2487			run_tests $ns1 $ns2 dead:beef:1::1
2488		chk_join_nr 1 1 1
2489		chk_add_nr 1 1
2490	fi
2491
2492	# single address IPv6, remove
2493	if reset "remove single address IPv6"; then
2494		pm_nl_set_limits $ns1 0 1
2495		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2496		pm_nl_set_limits $ns2 1 1
2497		addr_nr_ns1=-1 speed=slow \
2498			run_tests $ns1 $ns2 dead:beef:1::1
2499		chk_join_nr 1 1 1
2500		chk_add_nr 1 1
2501		chk_rm_nr 1 1 invert
2502	fi
2503
2504	# subflow and signal IPv6, remove
2505	if reset "remove subflow and signal IPv6"; then
2506		pm_nl_set_limits $ns1 0 2
2507		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2508		pm_nl_set_limits $ns2 1 2
2509		pm_nl_add_endpoint $ns2 dead:beef:3::2 dev ns2eth3 flags subflow
2510		addr_nr_ns1=-1 addr_nr_ns2=-1 speed=slow \
2511			run_tests $ns1 $ns2 dead:beef:1::1
2512		chk_join_nr 2 2 2
2513		chk_add_nr 1 1
2514		chk_rm_nr 1 1
2515	fi
2516}
2517
2518v4mapped_tests()
2519{
2520	# subflow IPv4-mapped to IPv4-mapped
2521	if reset "single subflow IPv4-mapped"; then
2522		pm_nl_set_limits $ns1 0 1
2523		pm_nl_set_limits $ns2 0 1
2524		pm_nl_add_endpoint $ns2 "::ffff:10.0.3.2" flags subflow
2525		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2526		chk_join_nr 1 1 1
2527	fi
2528
2529	# signal address IPv4-mapped with IPv4-mapped sk
2530	if reset "signal address IPv4-mapped"; then
2531		pm_nl_set_limits $ns1 0 1
2532		pm_nl_set_limits $ns2 1 1
2533		pm_nl_add_endpoint $ns1 "::ffff:10.0.2.1" flags signal
2534		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2535		chk_join_nr 1 1 1
2536		chk_add_nr 1 1
2537	fi
2538
2539	# subflow v4-map-v6
2540	if reset "single subflow v4-map-v6"; then
2541		pm_nl_set_limits $ns1 0 1
2542		pm_nl_set_limits $ns2 0 1
2543		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2544		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2545		chk_join_nr 1 1 1
2546	fi
2547
2548	# signal address v4-map-v6
2549	if reset "signal address v4-map-v6"; then
2550		pm_nl_set_limits $ns1 0 1
2551		pm_nl_set_limits $ns2 1 1
2552		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2553		run_tests $ns1 $ns2 "::ffff:10.0.1.1"
2554		chk_join_nr 1 1 1
2555		chk_add_nr 1 1
2556	fi
2557
2558	# subflow v6-map-v4
2559	if reset "single subflow v6-map-v4"; then
2560		pm_nl_set_limits $ns1 0 1
2561		pm_nl_set_limits $ns2 0 1
2562		pm_nl_add_endpoint $ns2 "::ffff:10.0.3.2" flags subflow
2563		run_tests $ns1 $ns2 10.0.1.1
2564		chk_join_nr 1 1 1
2565	fi
2566
2567	# signal address v6-map-v4
2568	if reset "signal address v6-map-v4"; then
2569		pm_nl_set_limits $ns1 0 1
2570		pm_nl_set_limits $ns2 1 1
2571		pm_nl_add_endpoint $ns1 "::ffff:10.0.2.1" flags signal
2572		run_tests $ns1 $ns2 10.0.1.1
2573		chk_join_nr 1 1 1
2574		chk_add_nr 1 1
2575	fi
2576
2577	# no subflow IPv6 to v4 address
2578	if reset "no JOIN with diff families v4-v6"; then
2579		pm_nl_set_limits $ns1 0 1
2580		pm_nl_set_limits $ns2 0 1
2581		pm_nl_add_endpoint $ns2 dead:beef:2::2 flags subflow
2582		run_tests $ns1 $ns2 10.0.1.1
2583		chk_join_nr 0 0 0
2584	fi
2585
2586	# no subflow IPv6 to v4 address even if v6 has a valid v4 at the end
2587	if reset "no JOIN with diff families v4-v6-2"; then
2588		pm_nl_set_limits $ns1 0 1
2589		pm_nl_set_limits $ns2 0 1
2590		pm_nl_add_endpoint $ns2 dead:beef:2::10.0.3.2 flags subflow
2591		run_tests $ns1 $ns2 10.0.1.1
2592		chk_join_nr 0 0 0
2593	fi
2594
2595	# no subflow IPv4 to v6 address, no need to slow down too then
2596	if reset "no JOIN with diff families v6-v4"; then
2597		pm_nl_set_limits $ns1 0 1
2598		pm_nl_set_limits $ns2 0 1
2599		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2600		run_tests $ns1 $ns2 dead:beef:1::1
2601		chk_join_nr 0 0 0
2602	fi
2603}
2604
2605mixed_tests()
2606{
2607	if reset "IPv4 sockets do not use IPv6 addresses" &&
2608	   continue_if mptcp_lib_kversion_ge 6.3; then
2609		pm_nl_set_limits $ns1 0 1
2610		pm_nl_set_limits $ns2 1 1
2611		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2612		speed=slow \
2613			run_tests $ns1 $ns2 10.0.1.1
2614		chk_join_nr 0 0 0
2615	fi
2616
2617	# Need an IPv6 mptcp socket to allow subflows of both families
2618	if reset "simult IPv4 and IPv6 subflows" &&
2619	   continue_if mptcp_lib_kversion_ge 6.3; then
2620		pm_nl_set_limits $ns1 0 1
2621		pm_nl_set_limits $ns2 1 1
2622		pm_nl_add_endpoint $ns1 10.0.1.1 flags signal
2623		speed=slow \
2624			run_tests $ns1 $ns2 dead:beef:2::1
2625		chk_join_nr 1 1 1
2626	fi
2627
2628	# cross families subflows will not be created even in fullmesh mode
2629	if reset "simult IPv4 and IPv6 subflows, fullmesh 1x1" &&
2630	   continue_if mptcp_lib_kversion_ge 6.3; then
2631		pm_nl_set_limits $ns1 0 4
2632		pm_nl_set_limits $ns2 1 4
2633		pm_nl_add_endpoint $ns2 dead:beef:2::2 flags subflow,fullmesh
2634		pm_nl_add_endpoint $ns1 10.0.1.1 flags signal
2635		speed=slow \
2636			run_tests $ns1 $ns2 dead:beef:2::1
2637		chk_join_nr 1 1 1
2638	fi
2639
2640	# fullmesh still tries to create all the possibly subflows with
2641	# matching family
2642	if reset "simult IPv4 and IPv6 subflows, fullmesh 2x2" &&
2643	   continue_if mptcp_lib_kversion_ge 6.3; then
2644		pm_nl_set_limits $ns1 0 4
2645		pm_nl_set_limits $ns2 2 4
2646		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2647		pm_nl_add_endpoint $ns1 dead:beef:2::1 flags signal
2648		fullmesh=1 speed=slow \
2649			run_tests $ns1 $ns2 dead:beef:1::1
2650		chk_join_nr 4 4 4
2651	fi
2652}
2653
2654backup_tests()
2655{
2656	# single subflow, backup
2657	if reset "single subflow, backup" &&
2658	   continue_if mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
2659		pm_nl_set_limits $ns1 0 1
2660		pm_nl_set_limits $ns2 0 1
2661		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow,backup
2662		sflags=nobackup speed=slow \
2663			run_tests $ns1 $ns2 10.0.1.1
2664		chk_join_nr 1 1 1
2665		chk_prio_nr 0 1 1 0
2666	fi
2667
2668	# single address, backup
2669	if reset "single address, backup" &&
2670	   continue_if mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
2671		pm_nl_set_limits $ns1 0 1
2672		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal,backup
2673		pm_nl_set_limits $ns2 1 1
2674		sflags=nobackup speed=slow \
2675			run_tests $ns1 $ns2 10.0.1.1
2676		chk_join_nr 1 1 1
2677		chk_add_nr 1 1
2678		chk_prio_nr 1 0 0 1
2679	fi
2680
2681	# single address, switch to backup
2682	if reset "single address, switch to backup" &&
2683	   continue_if mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
2684		pm_nl_set_limits $ns1 0 1
2685		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2686		pm_nl_set_limits $ns2 1 1
2687		sflags=backup speed=slow \
2688			run_tests $ns1 $ns2 10.0.1.1
2689		chk_join_nr 1 1 1
2690		chk_add_nr 1 1
2691		chk_prio_nr 1 1 0 0
2692	fi
2693
2694	# single address with port, backup
2695	if reset "single address with port, backup" &&
2696	   continue_if mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
2697		pm_nl_set_limits $ns1 0 1
2698		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal,backup port 10100
2699		pm_nl_set_limits $ns2 1 1
2700		sflags=nobackup speed=slow \
2701			run_tests $ns1 $ns2 10.0.1.1
2702		chk_join_nr 1 1 1
2703		chk_add_nr 1 1
2704		chk_prio_nr 1 0 0 1
2705	fi
2706
2707	if reset "mpc backup" &&
2708	   continue_if mptcp_lib_kallsyms_doesnt_have "T mptcp_subflow_send_ack$"; then
2709		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow,backup
2710		speed=slow \
2711			run_tests $ns1 $ns2 10.0.1.1
2712		chk_join_nr 0 0 0
2713		chk_prio_nr 0 1 0 0
2714	fi
2715
2716	if reset "mpc backup both sides" &&
2717	   continue_if mptcp_lib_kallsyms_doesnt_have "T mptcp_subflow_send_ack$"; then
2718		pm_nl_set_limits $ns1 0 2
2719		pm_nl_set_limits $ns2 1 2
2720		pm_nl_add_endpoint $ns1 10.0.1.1 flags signal,backup
2721		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow,backup
2722
2723		# 10.0.2.2 (non-backup) -> 10.0.1.1 (backup)
2724		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
2725		# 10.0.1.2 (backup) -> 10.0.2.1 (non-backup)
2726		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2727		ip -net "$ns2" route add 10.0.2.1 via 10.0.1.1 dev ns2eth1 # force this path
2728
2729		speed=slow \
2730			run_tests $ns1 $ns2 10.0.1.1
2731		chk_join_nr 2 2 2
2732		chk_prio_nr 1 1 1 1
2733	fi
2734
2735	if reset "mpc switch to backup" &&
2736	   continue_if mptcp_lib_kallsyms_doesnt_have "T mptcp_subflow_send_ack$"; then
2737		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow
2738		sflags=backup speed=slow \
2739			run_tests $ns1 $ns2 10.0.1.1
2740		chk_join_nr 0 0 0
2741		chk_prio_nr 0 1 0 0
2742	fi
2743
2744	if reset "mpc switch to backup both sides" &&
2745	   continue_if mptcp_lib_kallsyms_doesnt_have "T mptcp_subflow_send_ack$"; then
2746		pm_nl_add_endpoint $ns1 10.0.1.1 flags subflow
2747		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow
2748		sflags=backup speed=slow \
2749			run_tests $ns1 $ns2 10.0.1.1
2750		chk_join_nr 0 0 0
2751		chk_prio_nr 1 1 0 0
2752	fi
2753}
2754
2755verify_listener_events()
2756{
2757	local e_type=$2
2758	local e_saddr=$4
2759	local e_sport=$5
2760	local name
2761
2762	if [ $e_type = $MPTCP_LIB_EVENT_LISTENER_CREATED ]; then
2763		name="LISTENER_CREATED"
2764	elif [ $e_type = $MPTCP_LIB_EVENT_LISTENER_CLOSED ]; then
2765		name="LISTENER_CLOSED "
2766	else
2767		name="$e_type"
2768	fi
2769
2770	print_check "$name $e_saddr:$e_sport"
2771
2772	if ! mptcp_lib_kallsyms_has "mptcp_event_pm_listener$"; then
2773		print_skip "event not supported"
2774		return
2775	fi
2776
2777	if mptcp_lib_verify_listener_events "${@}"; then
2778		print_ok
2779		return 0
2780	fi
2781	fail_test
2782}
2783
2784add_addr_ports_tests()
2785{
2786	# signal address with port
2787	if reset "signal address with port"; then
2788		pm_nl_set_limits $ns1 0 1
2789		pm_nl_set_limits $ns2 1 1
2790		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2791		run_tests $ns1 $ns2 10.0.1.1
2792		chk_join_nr 1 1 1
2793		chk_add_nr 1 1 1
2794	fi
2795
2796	# subflow and signal with port
2797	if reset "subflow and signal with port"; then
2798		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2799		pm_nl_set_limits $ns1 0 2
2800		pm_nl_set_limits $ns2 1 2
2801		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2802		run_tests $ns1 $ns2 10.0.1.1
2803		chk_join_nr 2 2 2
2804		chk_add_nr 1 1 1
2805	fi
2806
2807	# single address with port, remove
2808	# pm listener events
2809	if reset_with_events "remove single address with port"; then
2810		pm_nl_set_limits $ns1 0 1
2811		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2812		pm_nl_set_limits $ns2 1 1
2813		addr_nr_ns1=-1 speed=slow \
2814			run_tests $ns1 $ns2 10.0.1.1
2815		chk_join_nr 1 1 1
2816		chk_add_nr 1 1 1
2817		chk_rm_nr 1 1 invert
2818
2819		verify_listener_events $evts_ns1 $MPTCP_LIB_EVENT_LISTENER_CREATED \
2820				       $MPTCP_LIB_AF_INET 10.0.2.1 10100
2821		verify_listener_events $evts_ns1 $MPTCP_LIB_EVENT_LISTENER_CLOSED \
2822				       $MPTCP_LIB_AF_INET 10.0.2.1 10100
2823		kill_events_pids
2824	fi
2825
2826	# subflow and signal with port, remove
2827	if reset "remove subflow and signal with port"; then
2828		pm_nl_set_limits $ns1 0 2
2829		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2830		pm_nl_set_limits $ns2 1 2
2831		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2832		addr_nr_ns1=-1 addr_nr_ns2=-1 speed=slow \
2833			run_tests $ns1 $ns2 10.0.1.1
2834		chk_join_nr 2 2 2
2835		chk_add_nr 1 1 1
2836		chk_rm_nr 1 1
2837	fi
2838
2839	# subflows and signal with port, flush
2840	if reset "flush subflows and signal with port"; then
2841		pm_nl_set_limits $ns1 0 3
2842		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2843		pm_nl_set_limits $ns2 1 3
2844		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2845		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2846		addr_nr_ns1=-8 addr_nr_ns2=-2 speed=slow \
2847			run_tests $ns1 $ns2 10.0.1.1
2848		chk_join_nr 3 3 3
2849		chk_add_nr 1 1
2850		chk_rm_nr 1 3 invert simult
2851	fi
2852
2853	# multiple addresses with port
2854	if reset "multiple addresses with port"; then
2855		pm_nl_set_limits $ns1 2 2
2856		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2857		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal port 10100
2858		pm_nl_set_limits $ns2 2 2
2859		run_tests $ns1 $ns2 10.0.1.1
2860		chk_join_nr 2 2 2
2861		chk_add_nr 2 2 2
2862	fi
2863
2864	# multiple addresses with ports
2865	if reset "multiple addresses with ports"; then
2866		pm_nl_set_limits $ns1 2 2
2867		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal port 10100
2868		pm_nl_add_endpoint $ns1 10.0.3.1 flags signal port 10101
2869		pm_nl_set_limits $ns2 2 2
2870		run_tests $ns1 $ns2 10.0.1.1
2871		chk_join_nr 2 2 2
2872		chk_add_nr 2 2 2
2873	fi
2874}
2875
2876syncookies_tests()
2877{
2878	# single subflow, syncookies
2879	if reset_with_cookies "single subflow with syn cookies"; then
2880		pm_nl_set_limits $ns1 0 1
2881		pm_nl_set_limits $ns2 0 1
2882		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2883		run_tests $ns1 $ns2 10.0.1.1
2884		chk_join_nr 1 1 1
2885	fi
2886
2887	# multiple subflows with syn cookies
2888	if reset_with_cookies "multiple subflows with syn cookies"; then
2889		pm_nl_set_limits $ns1 0 2
2890		pm_nl_set_limits $ns2 0 2
2891		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2892		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
2893		run_tests $ns1 $ns2 10.0.1.1
2894		chk_join_nr 2 2 2
2895	fi
2896
2897	# multiple subflows limited by server
2898	if reset_with_cookies "subflows limited by server w cookies"; then
2899		pm_nl_set_limits $ns1 0 1
2900		pm_nl_set_limits $ns2 0 2
2901		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2902		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow
2903		run_tests $ns1 $ns2 10.0.1.1
2904		chk_join_nr 2 1 1
2905	fi
2906
2907	# test signal address with cookies
2908	if reset_with_cookies "signal address with syn cookies"; then
2909		pm_nl_set_limits $ns1 0 1
2910		pm_nl_set_limits $ns2 1 1
2911		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2912		run_tests $ns1 $ns2 10.0.1.1
2913		chk_join_nr 1 1 1
2914		chk_add_nr 1 1
2915	fi
2916
2917	# test cookie with subflow and signal
2918	if reset_with_cookies "subflow and signal w cookies"; then
2919		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2920		pm_nl_set_limits $ns1 0 2
2921		pm_nl_set_limits $ns2 1 2
2922		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2923		run_tests $ns1 $ns2 10.0.1.1
2924		chk_join_nr 2 2 2
2925		chk_add_nr 1 1
2926	fi
2927
2928	# accept and use add_addr with additional subflows
2929	if reset_with_cookies "subflows and signal w. cookies"; then
2930		pm_nl_set_limits $ns1 0 3
2931		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
2932		pm_nl_set_limits $ns2 1 3
2933		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2934		pm_nl_add_endpoint $ns2 10.0.4.2 flags subflow
2935		run_tests $ns1 $ns2 10.0.1.1
2936		chk_join_nr 3 3 3
2937		chk_add_nr 1 1
2938	fi
2939}
2940
2941checksum_tests()
2942{
2943	# checksum test 0 0
2944	if reset_with_checksum 0 0; then
2945		pm_nl_set_limits $ns1 0 1
2946		pm_nl_set_limits $ns2 0 1
2947		run_tests $ns1 $ns2 10.0.1.1
2948		chk_join_nr 0 0 0
2949	fi
2950
2951	# checksum test 1 1
2952	if reset_with_checksum 1 1; then
2953		pm_nl_set_limits $ns1 0 1
2954		pm_nl_set_limits $ns2 0 1
2955		run_tests $ns1 $ns2 10.0.1.1
2956		chk_join_nr 0 0 0
2957	fi
2958
2959	# checksum test 0 1
2960	if reset_with_checksum 0 1; then
2961		pm_nl_set_limits $ns1 0 1
2962		pm_nl_set_limits $ns2 0 1
2963		run_tests $ns1 $ns2 10.0.1.1
2964		chk_join_nr 0 0 0
2965	fi
2966
2967	# checksum test 1 0
2968	if reset_with_checksum 1 0; then
2969		pm_nl_set_limits $ns1 0 1
2970		pm_nl_set_limits $ns2 0 1
2971		run_tests $ns1 $ns2 10.0.1.1
2972		chk_join_nr 0 0 0
2973	fi
2974}
2975
2976deny_join_id0_tests()
2977{
2978	# subflow allow join id0 ns1
2979	if reset_with_allow_join_id0 "single subflow allow join id0 ns1" 1 0; then
2980		pm_nl_set_limits $ns1 1 1
2981		pm_nl_set_limits $ns2 1 1
2982		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2983		run_tests $ns1 $ns2 10.0.1.1
2984		chk_join_nr 1 1 1
2985	fi
2986
2987	# subflow allow join id0 ns2
2988	if reset_with_allow_join_id0 "single subflow allow join id0 ns2" 0 1; then
2989		pm_nl_set_limits $ns1 1 1
2990		pm_nl_set_limits $ns2 1 1
2991		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
2992		run_tests $ns1 $ns2 10.0.1.1
2993		chk_join_nr 0 0 0
2994	fi
2995
2996	# signal address allow join id0 ns1
2997	# ADD_ADDRs are not affected by allow_join_id0 value.
2998	if reset_with_allow_join_id0 "signal address allow join id0 ns1" 1 0; then
2999		pm_nl_set_limits $ns1 1 1
3000		pm_nl_set_limits $ns2 1 1
3001		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3002		run_tests $ns1 $ns2 10.0.1.1
3003		chk_join_nr 1 1 1
3004		chk_add_nr 1 1
3005	fi
3006
3007	# signal address allow join id0 ns2
3008	# ADD_ADDRs are not affected by allow_join_id0 value.
3009	if reset_with_allow_join_id0 "signal address allow join id0 ns2" 0 1; then
3010		pm_nl_set_limits $ns1 1 1
3011		pm_nl_set_limits $ns2 1 1
3012		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3013		run_tests $ns1 $ns2 10.0.1.1
3014		chk_join_nr 1 1 1
3015		chk_add_nr 1 1
3016	fi
3017
3018	# subflow and address allow join id0 ns1
3019	if reset_with_allow_join_id0 "subflow and address allow join id0 1" 1 0; then
3020		pm_nl_set_limits $ns1 2 2
3021		pm_nl_set_limits $ns2 2 2
3022		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3023		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3024		run_tests $ns1 $ns2 10.0.1.1
3025		chk_join_nr 2 2 2
3026	fi
3027
3028	# subflow and address allow join id0 ns2
3029	if reset_with_allow_join_id0 "subflow and address allow join id0 2" 0 1; then
3030		pm_nl_set_limits $ns1 2 2
3031		pm_nl_set_limits $ns2 2 2
3032		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3033		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3034		run_tests $ns1 $ns2 10.0.1.1
3035		chk_join_nr 1 1 1
3036	fi
3037}
3038
3039fullmesh_tests()
3040{
3041	# fullmesh 1
3042	# 2 fullmesh addrs in ns2, added before the connection,
3043	# 1 non-fullmesh addr in ns1, added during the connection.
3044	if reset "fullmesh test 2x1"; then
3045		pm_nl_set_limits $ns1 0 4
3046		pm_nl_set_limits $ns2 1 4
3047		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow,fullmesh
3048		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow,fullmesh
3049		addr_nr_ns1=1 speed=slow \
3050			run_tests $ns1 $ns2 10.0.1.1
3051		chk_join_nr 4 4 4
3052		chk_add_nr 1 1
3053	fi
3054
3055	# fullmesh 2
3056	# 1 non-fullmesh addr in ns1, added before the connection,
3057	# 1 fullmesh addr in ns2, added during the connection.
3058	if reset "fullmesh test 1x1"; then
3059		pm_nl_set_limits $ns1 1 3
3060		pm_nl_set_limits $ns2 1 3
3061		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3062		pm_nl_add_endpoint $ns2 10.0.1.2 flags subflow,fullmesh
3063		fullmesh=1 speed=slow \
3064			run_tests $ns1 $ns2 10.0.1.1
3065		chk_join_nr 3 3 3
3066		chk_add_nr 1 1
3067	fi
3068
3069	# fullmesh 3
3070	# 1 non-fullmesh addr in ns1, added before the connection,
3071	# 2 fullmesh addrs in ns2, added during the connection.
3072	if reset "fullmesh test 1x2"; then
3073		pm_nl_set_limits $ns1 2 5
3074		pm_nl_set_limits $ns2 1 5
3075		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3076		fullmesh=2 speed=slow \
3077			run_tests $ns1 $ns2 10.0.1.1
3078		chk_join_nr 5 5 5
3079		chk_add_nr 1 1
3080	fi
3081
3082	# fullmesh 4
3083	# 1 non-fullmesh addr in ns1, added before the connection,
3084	# 2 fullmesh addrs in ns2, added during the connection,
3085	# limit max_subflows to 4.
3086	if reset "fullmesh test 1x2, limited"; then
3087		pm_nl_set_limits $ns1 2 4
3088		pm_nl_set_limits $ns2 1 4
3089		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3090		fullmesh=2 speed=slow \
3091			run_tests $ns1 $ns2 10.0.1.1
3092		chk_join_nr 4 4 4
3093		chk_add_nr 1 1
3094	fi
3095
3096	# set fullmesh flag
3097	if reset "set fullmesh flag test" &&
3098	   continue_if mptcp_lib_kversion_ge 5.18; then
3099		pm_nl_set_limits $ns1 4 4
3100		pm_nl_add_endpoint $ns1 10.0.2.1 flags subflow
3101		pm_nl_set_limits $ns2 4 4
3102		addr_nr_ns2=1 sflags=fullmesh speed=slow \
3103			run_tests $ns1 $ns2 10.0.1.1
3104		chk_join_nr 2 2 2
3105		chk_rm_nr 0 1
3106	fi
3107
3108	# set nofullmesh flag
3109	if reset "set nofullmesh flag test" &&
3110	   continue_if mptcp_lib_kversion_ge 5.18; then
3111		pm_nl_set_limits $ns1 4 4
3112		pm_nl_add_endpoint $ns1 10.0.2.1 flags subflow,fullmesh
3113		pm_nl_set_limits $ns2 4 4
3114		fullmesh=1 sflags=nofullmesh speed=slow \
3115			run_tests $ns1 $ns2 10.0.1.1
3116		chk_join_nr 2 2 2
3117		chk_rm_nr 0 1
3118	fi
3119
3120	# set backup,fullmesh flags
3121	if reset "set backup,fullmesh flags test" &&
3122	   continue_if mptcp_lib_kversion_ge 5.18; then
3123		pm_nl_set_limits $ns1 4 4
3124		pm_nl_add_endpoint $ns1 10.0.2.1 flags subflow
3125		pm_nl_set_limits $ns2 4 4
3126		addr_nr_ns2=1 sflags=backup,fullmesh speed=slow \
3127			run_tests $ns1 $ns2 10.0.1.1
3128		chk_join_nr 2 2 2
3129		chk_prio_nr 0 1 1 0
3130		chk_rm_nr 0 1
3131	fi
3132
3133	# set nobackup,nofullmesh flags
3134	if reset "set nobackup,nofullmesh flags test" &&
3135	   continue_if mptcp_lib_kversion_ge 5.18; then
3136		pm_nl_set_limits $ns1 4 4
3137		pm_nl_set_limits $ns2 4 4
3138		pm_nl_add_endpoint $ns2 10.0.2.2 flags subflow,backup,fullmesh
3139		sflags=nobackup,nofullmesh speed=slow \
3140			run_tests $ns1 $ns2 10.0.1.1
3141		chk_join_nr 2 2 2
3142		chk_prio_nr 0 1 1 0
3143		chk_rm_nr 0 1
3144	fi
3145}
3146
3147fastclose_tests()
3148{
3149	if reset_check_counter "fastclose test" "MPTcpExtMPFastcloseTx"; then
3150		MPTCP_LIB_SUBTEST_FLAKY=1
3151		test_linkfail=1024 fastclose=client \
3152			run_tests $ns1 $ns2 10.0.1.1
3153		chk_join_nr 0 0 0
3154		chk_fclose_nr 1 1
3155		chk_rst_nr 1 1 invert
3156	fi
3157
3158	if reset_check_counter "fastclose server test" "MPTcpExtMPFastcloseRx"; then
3159		MPTCP_LIB_SUBTEST_FLAKY=1
3160		test_linkfail=1024 fastclose=server \
3161			run_tests $ns1 $ns2 10.0.1.1
3162		chk_join_nr 0 0 0 0 0 0 1
3163		chk_fclose_nr 1 1 invert
3164		chk_rst_nr 1 1
3165	fi
3166}
3167
3168pedit_action_pkts()
3169{
3170	tc -n $ns2 -j -s action show action pedit index 100 | \
3171		mptcp_lib_get_info_value \"packets\" packets
3172}
3173
3174fail_tests()
3175{
3176	# single subflow
3177	if reset_with_fail "Infinite map" 1; then
3178		MPTCP_LIB_SUBTEST_FLAKY=1
3179		test_linkfail=128 \
3180			run_tests $ns1 $ns2 10.0.1.1
3181		chk_join_nr 0 0 0 +1 +0 1 0 1 "$(pedit_action_pkts)"
3182		chk_fail_nr 1 -1 invert
3183	fi
3184
3185	# multiple subflows
3186	if reset_with_fail "MP_FAIL MP_RST" 2; then
3187		MPTCP_LIB_SUBTEST_FLAKY=1
3188		tc -n $ns2 qdisc add dev ns2eth1 root netem rate 1mbit delay 5ms
3189		pm_nl_set_limits $ns1 0 1
3190		pm_nl_set_limits $ns2 0 1
3191		pm_nl_add_endpoint $ns2 10.0.2.2 dev ns2eth2 flags subflow
3192		test_linkfail=1024 \
3193			run_tests $ns1 $ns2 10.0.1.1
3194		chk_join_nr 1 1 1 1 0 1 1 0 "$(pedit_action_pkts)"
3195	fi
3196}
3197
3198# $1: ns ; $2: addr ; $3: id
3199userspace_pm_add_addr()
3200{
3201	local evts=$evts_ns1
3202	local tk
3203
3204	[ "$1" == "$ns2" ] && evts=$evts_ns2
3205	tk=$(mptcp_lib_evts_get_info token "$evts")
3206
3207	ip netns exec $1 ./pm_nl_ctl ann $2 token $tk id $3
3208	sleep 1
3209}
3210
3211# $1: ns ; $2: id
3212userspace_pm_rm_addr()
3213{
3214	local evts=$evts_ns1
3215	local tk
3216	local cnt
3217
3218	[ "$1" == "$ns2" ] && evts=$evts_ns2
3219	tk=$(mptcp_lib_evts_get_info token "$evts")
3220
3221	cnt=$(rm_addr_count ${1})
3222	ip netns exec $1 ./pm_nl_ctl rem token $tk id $2
3223	wait_rm_addr $1 "${cnt}"
3224}
3225
3226# $1: ns ; $2: addr ; $3: id
3227userspace_pm_add_sf()
3228{
3229	local evts=$evts_ns1
3230	local tk da dp
3231
3232	[ "$1" == "$ns2" ] && evts=$evts_ns2
3233	tk=$(mptcp_lib_evts_get_info token "$evts")
3234	da=$(mptcp_lib_evts_get_info daddr4 "$evts")
3235	dp=$(mptcp_lib_evts_get_info dport "$evts")
3236
3237	ip netns exec $1 ./pm_nl_ctl csf lip $2 lid $3 \
3238				rip $da rport $dp token $tk
3239	sleep 1
3240}
3241
3242# $1: ns ; $2: addr $3: event type
3243userspace_pm_rm_sf()
3244{
3245	local evts=$evts_ns1
3246	local t=${3:-1}
3247	local ip
3248	local tk da dp sp
3249	local cnt
3250
3251	[ "$1" == "$ns2" ] && evts=$evts_ns2
3252	[ -n "$(mptcp_lib_evts_get_info "saddr4" "$evts" $t)" ] && ip=4
3253	[ -n "$(mptcp_lib_evts_get_info "saddr6" "$evts" $t)" ] && ip=6
3254	tk=$(mptcp_lib_evts_get_info token "$evts")
3255	da=$(mptcp_lib_evts_get_info "daddr$ip" "$evts" $t $2)
3256	dp=$(mptcp_lib_evts_get_info dport "$evts" $t $2)
3257	sp=$(mptcp_lib_evts_get_info sport "$evts" $t $2)
3258
3259	cnt=$(rm_sf_count ${1})
3260	ip netns exec $1 ./pm_nl_ctl dsf lip $2 lport $sp \
3261				rip $da rport $dp token $tk
3262	wait_rm_sf $1 "${cnt}"
3263}
3264
3265check_output()
3266{
3267	local cmd="$1"
3268	local expected="$2"
3269	local msg="$3"
3270	local rc=0
3271
3272	mptcp_lib_check_output "${err}" "${cmd}" "${expected}" || rc=${?}
3273	if [ ${rc} -eq 2 ]; then
3274		fail_test "fail to check output # error ${rc}"
3275	elif [ ${rc} -eq 0 ]; then
3276		print_ok
3277	elif [ ${rc} -eq 1 ]; then
3278		fail_test "fail to check output # different output"
3279	fi
3280}
3281
3282# $1: ns
3283userspace_pm_dump()
3284{
3285	local evts=$evts_ns1
3286	local tk
3287
3288	[ "$1" == "$ns2" ] && evts=$evts_ns2
3289	tk=$(mptcp_lib_evts_get_info token "$evts")
3290
3291	ip netns exec $1 ./pm_nl_ctl dump token $tk
3292}
3293
3294# $1: ns ; $2: id
3295userspace_pm_get_addr()
3296{
3297	local evts=$evts_ns1
3298	local tk
3299
3300	[ "$1" == "$ns2" ] && evts=$evts_ns2
3301	tk=$(mptcp_lib_evts_get_info token "$evts")
3302
3303	ip netns exec $1 ./pm_nl_ctl get $2 token $tk
3304}
3305
3306userspace_pm_chk_dump_addr()
3307{
3308	local ns="${1}"
3309	local exp="${2}"
3310	local check="${3}"
3311
3312	print_check "dump addrs ${check}"
3313
3314	if mptcp_lib_kallsyms_has "mptcp_userspace_pm_dump_addr$"; then
3315		check_output "userspace_pm_dump ${ns}" "${exp}"
3316	else
3317		print_skip
3318	fi
3319}
3320
3321userspace_pm_chk_get_addr()
3322{
3323	local ns="${1}"
3324	local id="${2}"
3325	local exp="${3}"
3326
3327	print_check "get id ${id} addr"
3328
3329	if mptcp_lib_kallsyms_has "mptcp_userspace_pm_get_addr$"; then
3330		check_output "userspace_pm_get_addr ${ns} ${id}" "${exp}"
3331	else
3332		print_skip
3333	fi
3334}
3335
3336userspace_tests()
3337{
3338	# userspace pm type prevents add_addr
3339	if reset "userspace pm type prevents add_addr" &&
3340	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3341		set_userspace_pm $ns1
3342		pm_nl_set_limits $ns1 0 2
3343		pm_nl_set_limits $ns2 0 2
3344		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3345		run_tests $ns1 $ns2 10.0.1.1
3346		chk_join_nr 0 0 0
3347		chk_add_nr 0 0
3348	fi
3349
3350	# userspace pm type does not echo add_addr without daemon
3351	if reset "userspace pm no echo w/o daemon" &&
3352	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3353		set_userspace_pm $ns2
3354		pm_nl_set_limits $ns1 0 2
3355		pm_nl_set_limits $ns2 0 2
3356		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3357		run_tests $ns1 $ns2 10.0.1.1
3358		chk_join_nr 0 0 0
3359		chk_add_nr 1 0
3360	fi
3361
3362	# userspace pm type rejects join
3363	if reset "userspace pm type rejects join" &&
3364	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3365		set_userspace_pm $ns1
3366		pm_nl_set_limits $ns1 1 1
3367		pm_nl_set_limits $ns2 1 1
3368		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3369		run_tests $ns1 $ns2 10.0.1.1
3370		chk_join_nr 1 1 0
3371	fi
3372
3373	# userspace pm type does not send join
3374	if reset "userspace pm type does not send join" &&
3375	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3376		set_userspace_pm $ns2
3377		pm_nl_set_limits $ns1 1 1
3378		pm_nl_set_limits $ns2 1 1
3379		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3380		run_tests $ns1 $ns2 10.0.1.1
3381		chk_join_nr 0 0 0
3382	fi
3383
3384	# userspace pm type prevents mp_prio
3385	if reset "userspace pm type prevents mp_prio" &&
3386	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3387		set_userspace_pm $ns1
3388		pm_nl_set_limits $ns1 1 1
3389		pm_nl_set_limits $ns2 1 1
3390		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3391		sflags=backup speed=slow \
3392			run_tests $ns1 $ns2 10.0.1.1
3393		chk_join_nr 1 1 0
3394		chk_prio_nr 0 0 0 0
3395	fi
3396
3397	# userspace pm type prevents rm_addr
3398	if reset "userspace pm type prevents rm_addr" &&
3399	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3400		set_userspace_pm $ns1
3401		set_userspace_pm $ns2
3402		pm_nl_set_limits $ns1 0 1
3403		pm_nl_set_limits $ns2 0 1
3404		pm_nl_add_endpoint $ns2 10.0.3.2 flags subflow
3405		addr_nr_ns2=-1 speed=slow \
3406			run_tests $ns1 $ns2 10.0.1.1
3407		chk_join_nr 0 0 0
3408		chk_rm_nr 0 0
3409	fi
3410
3411	# userspace pm add & remove address
3412	if reset_with_events "userspace pm add & remove address" &&
3413	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3414		set_userspace_pm $ns1
3415		pm_nl_set_limits $ns2 2 2
3416		speed=5 \
3417			run_tests $ns1 $ns2 10.0.1.1 &
3418		local tests_pid=$!
3419		wait_mpj $ns1
3420		userspace_pm_add_addr $ns1 10.0.2.1 10
3421		userspace_pm_add_addr $ns1 10.0.3.1 20
3422		chk_join_nr 2 2 2
3423		chk_add_nr 2 2
3424		chk_mptcp_info subflows 2 subflows 2
3425		chk_subflows_total 3 3
3426		chk_mptcp_info add_addr_signal 2 add_addr_accepted 2
3427		userspace_pm_chk_dump_addr "${ns1}" \
3428			$'id 10 flags signal 10.0.2.1\nid 20 flags signal 10.0.3.1' \
3429			"signal"
3430		userspace_pm_chk_get_addr "${ns1}" "10" "id 10 flags signal 10.0.2.1"
3431		userspace_pm_chk_get_addr "${ns1}" "20" "id 20 flags signal 10.0.3.1"
3432		userspace_pm_rm_addr $ns1 10
3433		userspace_pm_rm_sf $ns1 "::ffff:10.0.2.1" $MPTCP_LIB_EVENT_SUB_ESTABLISHED
3434		userspace_pm_chk_dump_addr "${ns1}" \
3435			"id 20 flags signal 10.0.3.1" "after rm_addr 10"
3436		userspace_pm_rm_addr $ns1 20
3437		userspace_pm_rm_sf $ns1 10.0.3.1 $MPTCP_LIB_EVENT_SUB_ESTABLISHED
3438		userspace_pm_chk_dump_addr "${ns1}" "" "after rm_addr 20"
3439		chk_rm_nr 2 2 invert
3440		chk_mptcp_info subflows 0 subflows 0
3441		chk_subflows_total 1 1
3442		kill_events_pids
3443		mptcp_lib_kill_wait $tests_pid
3444	fi
3445
3446	# userspace pm create destroy subflow
3447	if reset_with_events "userspace pm create destroy subflow" &&
3448	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3449		set_userspace_pm $ns2
3450		pm_nl_set_limits $ns1 0 1
3451		speed=5 \
3452			run_tests $ns1 $ns2 10.0.1.1 &
3453		local tests_pid=$!
3454		wait_mpj $ns2
3455		userspace_pm_add_sf $ns2 10.0.3.2 20
3456		chk_join_nr 1 1 1
3457		chk_mptcp_info subflows 1 subflows 1
3458		chk_subflows_total 2 2
3459		userspace_pm_chk_dump_addr "${ns2}" \
3460			"id 20 flags subflow 10.0.3.2" \
3461			"subflow"
3462		userspace_pm_chk_get_addr "${ns2}" "20" "id 20 flags subflow 10.0.3.2"
3463		userspace_pm_rm_addr $ns2 20
3464		userspace_pm_rm_sf $ns2 10.0.3.2 $MPTCP_LIB_EVENT_SUB_ESTABLISHED
3465		userspace_pm_chk_dump_addr "${ns2}" \
3466			"" \
3467			"after rm_addr 20"
3468		chk_rm_nr 1 1
3469		chk_mptcp_info subflows 0 subflows 0
3470		chk_subflows_total 1 1
3471		kill_events_pids
3472		mptcp_lib_kill_wait $tests_pid
3473	fi
3474
3475	# userspace pm create id 0 subflow
3476	if reset_with_events "userspace pm create id 0 subflow" &&
3477	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3478		set_userspace_pm $ns2
3479		pm_nl_set_limits $ns1 0 1
3480		speed=5 \
3481			run_tests $ns1 $ns2 10.0.1.1 &
3482		local tests_pid=$!
3483		wait_mpj $ns2
3484		chk_mptcp_info subflows 0 subflows 0
3485		chk_subflows_total 1 1
3486		userspace_pm_add_sf $ns2 10.0.3.2 0
3487		userspace_pm_chk_dump_addr "${ns2}" \
3488			"id 0 flags subflow 10.0.3.2" "id 0 subflow"
3489		chk_join_nr 1 1 1
3490		chk_mptcp_info subflows 1 subflows 1
3491		chk_subflows_total 2 2
3492		kill_events_pids
3493		mptcp_lib_kill_wait $tests_pid
3494	fi
3495
3496	# userspace pm remove initial subflow
3497	if reset_with_events "userspace pm remove initial subflow" &&
3498	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3499		set_userspace_pm $ns2
3500		pm_nl_set_limits $ns1 0 1
3501		speed=5 \
3502			run_tests $ns1 $ns2 10.0.1.1 &
3503		local tests_pid=$!
3504		wait_mpj $ns2
3505		userspace_pm_add_sf $ns2 10.0.3.2 20
3506		chk_join_nr 1 1 1
3507		chk_mptcp_info subflows 1 subflows 1
3508		chk_subflows_total 2 2
3509		userspace_pm_rm_sf $ns2 10.0.1.2
3510		# we don't look at the counter linked to the RM_ADDR but
3511		# to the one linked to the subflows that have been removed
3512		chk_rm_nr 0 1
3513		chk_rst_nr 0 0 invert
3514		chk_mptcp_info subflows 1 subflows 1
3515		chk_subflows_total 1 1
3516		kill_events_pids
3517		mptcp_lib_kill_wait $tests_pid
3518	fi
3519
3520	# userspace pm send RM_ADDR for ID 0
3521	if reset_with_events "userspace pm send RM_ADDR for ID 0" &&
3522	   continue_if mptcp_lib_has_file '/proc/sys/net/mptcp/pm_type'; then
3523		set_userspace_pm $ns1
3524		pm_nl_set_limits $ns2 1 1
3525		speed=5 \
3526			run_tests $ns1 $ns2 10.0.1.1 &
3527		local tests_pid=$!
3528		wait_mpj $ns1
3529		userspace_pm_add_addr $ns1 10.0.2.1 10
3530		chk_join_nr 1 1 1
3531		chk_add_nr 1 1
3532		chk_mptcp_info subflows 1 subflows 1
3533		chk_subflows_total 2 2
3534		chk_mptcp_info add_addr_signal 1 add_addr_accepted 1
3535		userspace_pm_rm_addr $ns1 0
3536		# we don't look at the counter linked to the subflows that
3537		# have been removed but to the one linked to the RM_ADDR
3538		chk_rm_nr 1 0 invert
3539		chk_rst_nr 0 0 invert
3540		chk_mptcp_info subflows 1 subflows 1
3541		chk_subflows_total 1 1
3542		kill_events_pids
3543		mptcp_lib_kill_wait $tests_pid
3544	fi
3545}
3546
3547endpoint_tests()
3548{
3549	# subflow_rebuild_header is needed to support the implicit flag
3550	# userspace pm type prevents add_addr
3551	if reset "implicit EP" &&
3552	   mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
3553		pm_nl_set_limits $ns1 2 2
3554		pm_nl_set_limits $ns2 2 2
3555		pm_nl_add_endpoint $ns1 10.0.2.1 flags signal
3556		speed=slow \
3557			run_tests $ns1 $ns2 10.0.1.1 &
3558		local tests_pid=$!
3559
3560		wait_mpj $ns1
3561		pm_nl_check_endpoint "creation" \
3562			$ns2 10.0.2.2 id 1 flags implicit
3563		chk_mptcp_info subflows 1 subflows 1
3564		chk_mptcp_info add_addr_signal 1 add_addr_accepted 1
3565
3566		pm_nl_add_endpoint $ns2 10.0.2.2 id 33 2>/dev/null
3567		pm_nl_check_endpoint "ID change is prevented" \
3568			$ns2 10.0.2.2 id 1 flags implicit
3569
3570		pm_nl_add_endpoint $ns2 10.0.2.2 flags signal
3571		pm_nl_check_endpoint "modif is allowed" \
3572			$ns2 10.0.2.2 id 1 flags signal
3573		mptcp_lib_kill_wait $tests_pid
3574	fi
3575
3576	if reset_with_tcp_filter "delete and re-add" ns2 10.0.3.2 REJECT OUTPUT &&
3577	   mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
3578		pm_nl_set_limits $ns1 0 2
3579		pm_nl_set_limits $ns2 0 2
3580		pm_nl_add_endpoint $ns2 10.0.2.2 id 2 dev ns2eth2 flags subflow
3581		test_linkfail=4 speed=20 \
3582			run_tests $ns1 $ns2 10.0.1.1 &
3583		local tests_pid=$!
3584
3585		wait_mpj $ns2
3586		pm_nl_check_endpoint "creation" \
3587			$ns2 10.0.2.2 id 2 flags subflow dev ns2eth2
3588		chk_subflow_nr "before delete" 2
3589		chk_mptcp_info subflows 1 subflows 1
3590
3591		pm_nl_del_endpoint $ns2 2 10.0.2.2
3592		sleep 0.5
3593		chk_subflow_nr "after delete" 1
3594		chk_mptcp_info subflows 0 subflows 0
3595
3596		pm_nl_add_endpoint $ns2 10.0.2.2 id 2 dev ns2eth2 flags subflow
3597		wait_mpj $ns2
3598		chk_subflow_nr "after re-add" 2
3599		chk_mptcp_info subflows 1 subflows 1
3600
3601		pm_nl_add_endpoint $ns2 10.0.3.2 id 3 flags subflow
3602		wait_attempt_fail $ns2
3603		chk_subflow_nr "after new reject" 2
3604		chk_mptcp_info subflows 1 subflows 1
3605
3606		ip netns exec "${ns2}" ${iptables} -D OUTPUT -s "10.0.3.2" -p tcp -j REJECT
3607		pm_nl_del_endpoint $ns2 3 10.0.3.2
3608		pm_nl_add_endpoint $ns2 10.0.3.2 id 3 flags subflow
3609		wait_mpj $ns2
3610		chk_subflow_nr "after no reject" 3
3611		chk_mptcp_info subflows 2 subflows 2
3612
3613		mptcp_lib_kill_wait $tests_pid
3614
3615		chk_join_nr 3 3 3
3616		chk_rm_nr 1 1
3617	fi
3618
3619	# remove and re-add
3620	if reset "delete re-add signal" &&
3621	   mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
3622		pm_nl_set_limits $ns1 0 2
3623		pm_nl_set_limits $ns2 2 2
3624		pm_nl_add_endpoint $ns1 10.0.2.1 id 1 flags signal
3625		# broadcast IP: no packet for this address will be received on ns1
3626		pm_nl_add_endpoint $ns1 224.0.0.1 id 2 flags signal
3627		test_linkfail=4 speed=20 \
3628			run_tests $ns1 $ns2 10.0.1.1 &
3629		local tests_pid=$!
3630
3631		wait_mpj $ns2
3632		pm_nl_check_endpoint "creation" \
3633			$ns1 10.0.2.1 id 1 flags signal
3634		chk_subflow_nr "before delete" 2
3635		chk_mptcp_info subflows 1 subflows 1
3636
3637		pm_nl_del_endpoint $ns1 1 10.0.2.1
3638		pm_nl_del_endpoint $ns1 2 224.0.0.1
3639		sleep 0.5
3640		chk_subflow_nr "after delete" 1
3641		chk_mptcp_info subflows 0 subflows 0
3642
3643		pm_nl_add_endpoint $ns1 10.0.2.1 id 1 flags signal
3644		pm_nl_add_endpoint $ns1 10.0.3.1 id 2 flags signal
3645		wait_mpj $ns2
3646		chk_subflow_nr "after re-add" 3
3647		chk_mptcp_info subflows 2 subflows 2
3648		mptcp_lib_kill_wait $tests_pid
3649
3650		chk_join_nr 3 3 3
3651		chk_add_nr 4 4
3652		chk_rm_nr 2 1 invert
3653	fi
3654
3655	# flush and re-add
3656	if reset_with_tcp_filter "flush re-add" ns2 10.0.3.2 REJECT OUTPUT &&
3657	   mptcp_lib_kallsyms_has "subflow_rebuild_header$"; then
3658		pm_nl_set_limits $ns1 0 2
3659		pm_nl_set_limits $ns2 1 2
3660		# broadcast IP: no packet for this address will be received on ns1
3661		pm_nl_add_endpoint $ns1 224.0.0.1 id 2 flags signal
3662		pm_nl_add_endpoint $ns2 10.0.3.2 id 3 flags subflow
3663		test_linkfail=4 speed=20 \
3664			run_tests $ns1 $ns2 10.0.1.1 &
3665		local tests_pid=$!
3666
3667		wait_attempt_fail $ns2
3668		chk_subflow_nr "before flush" 1
3669		chk_mptcp_info subflows 0 subflows 0
3670
3671		pm_nl_flush_endpoint $ns2
3672		pm_nl_flush_endpoint $ns1
3673		wait_rm_addr $ns2 0
3674		ip netns exec "${ns2}" ${iptables} -D OUTPUT -s "10.0.3.2" -p tcp -j REJECT
3675		pm_nl_add_endpoint $ns2 10.0.3.2 id 3 flags subflow
3676		wait_mpj $ns2
3677		pm_nl_add_endpoint $ns1 10.0.3.1 id 2 flags signal
3678		wait_mpj $ns2
3679		mptcp_lib_kill_wait $tests_pid
3680
3681		chk_join_nr 2 2 2
3682		chk_add_nr 2 2
3683		chk_rm_nr 1 0 invert
3684	fi
3685}
3686
3687# [$1: error message]
3688usage()
3689{
3690	if [ -n "${1}" ]; then
3691		echo "${1}"
3692		ret=${KSFT_FAIL}
3693	fi
3694
3695	echo "mptcp_join usage:"
3696
3697	local key
3698	for key in "${!all_tests[@]}"; do
3699		echo "  -${key} ${all_tests[${key}]}"
3700	done
3701
3702	echo "  -c capture pcap files"
3703	echo "  -C enable data checksum"
3704	echo "  -i use ip mptcp"
3705	echo "  -h help"
3706
3707	echo "[test ids|names]"
3708
3709	exit ${ret}
3710}
3711
3712
3713# Use a "simple" array to force an specific order we cannot have with an associative one
3714all_tests_sorted=(
3715	f@subflows_tests
3716	e@subflows_error_tests
3717	s@signal_address_tests
3718	l@link_failure_tests
3719	t@add_addr_timeout_tests
3720	r@remove_tests
3721	a@add_tests
3722	6@ipv6_tests
3723	4@v4mapped_tests
3724	M@mixed_tests
3725	b@backup_tests
3726	p@add_addr_ports_tests
3727	k@syncookies_tests
3728	S@checksum_tests
3729	d@deny_join_id0_tests
3730	m@fullmesh_tests
3731	z@fastclose_tests
3732	F@fail_tests
3733	u@userspace_tests
3734	I@endpoint_tests
3735)
3736
3737all_tests_args=""
3738all_tests_names=()
3739for subtests in "${all_tests_sorted[@]}"; do
3740	key="${subtests%@*}"
3741	value="${subtests#*@}"
3742
3743	all_tests_args+="${key}"
3744	all_tests_names+=("${value}")
3745	all_tests[${key}]="${value}"
3746done
3747
3748tests=()
3749while getopts "${all_tests_args}cCih" opt; do
3750	case $opt in
3751		["${all_tests_args}"])
3752			tests+=("${all_tests[${opt}]}")
3753			;;
3754		c)
3755			capture=true
3756			;;
3757		C)
3758			checksum=true
3759			;;
3760		i)
3761			mptcp_lib_set_ip_mptcp
3762			;;
3763		h)
3764			usage
3765			;;
3766		*)
3767			usage "Unknown option: -${opt}"
3768			;;
3769	esac
3770done
3771
3772shift $((OPTIND - 1))
3773
3774for arg in "${@}"; do
3775	if [[ "${arg}" =~ ^[0-9]+$ ]]; then
3776		only_tests_ids+=("${arg}")
3777	else
3778		only_tests_names+=("${arg}")
3779	fi
3780done
3781
3782if [ ${#tests[@]} -eq 0 ]; then
3783	tests=("${all_tests_names[@]}")
3784fi
3785
3786for subtests in "${tests[@]}"; do
3787	"${subtests}"
3788done
3789
3790if [ ${ret} -ne 0 ]; then
3791	echo
3792	echo "${#failed_tests[@]} failure(s) has(ve) been detected:"
3793	for i in $(get_failed_tests_ids); do
3794		echo -e "\t- ${i}: ${failed_tests[${i}]}"
3795	done
3796	echo
3797fi
3798
3799append_prev_results
3800mptcp_lib_result_print_all_tap
3801
3802exit $ret
3803