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