1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4ret=0
5sin=""
6sinfail=""
7sout=""
8cin=""
9cinfail=""
10cinsent=""
11cout=""
12ksft_skip=4
13timeout_poll=30
14timeout_test=$((timeout_poll * 2 + 1))
15mptcp_connect=""
16capture=0
17checksum=0
18do_all_tests=1
19
20TEST_COUNT=0
21
22# generated using "nfbpf_compile '(ip && (ip[54] & 0xf0) == 0x30) ||
23#				  (ip6 && (ip6[74] & 0xf0) == 0x30)'"
24CBPF_MPTCP_SUBOPTION_ADD_ADDR="14,
25			       48 0 0 0,
26			       84 0 0 240,
27			       21 0 3 64,
28			       48 0 0 54,
29			       84 0 0 240,
30			       21 6 7 48,
31			       48 0 0 0,
32			       84 0 0 240,
33			       21 0 4 96,
34			       48 0 0 74,
35			       84 0 0 240,
36			       21 0 1 48,
37			       6 0 0 65535,
38			       6 0 0 0"
39
40init()
41{
42	capout=$(mktemp)
43
44	rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
45
46	ns1="ns1-$rndh"
47	ns2="ns2-$rndh"
48
49	for netns in "$ns1" "$ns2";do
50		ip netns add $netns || exit $ksft_skip
51		ip -net $netns link set lo up
52		ip netns exec $netns sysctl -q net.mptcp.enabled=1
53		ip netns exec $netns sysctl -q net.ipv4.conf.all.rp_filter=0
54		ip netns exec $netns sysctl -q net.ipv4.conf.default.rp_filter=0
55		if [ $checksum -eq 1 ]; then
56			ip netns exec $netns sysctl -q net.mptcp.checksum_enabled=1
57		fi
58	done
59
60	#  ns1              ns2
61	# ns1eth1    ns2eth1
62	# ns1eth2    ns2eth2
63	# ns1eth3    ns2eth3
64	# ns1eth4    ns2eth4
65
66	for i in `seq 1 4`; do
67		ip link add ns1eth$i netns "$ns1" type veth peer name ns2eth$i netns "$ns2"
68		ip -net "$ns1" addr add 10.0.$i.1/24 dev ns1eth$i
69		ip -net "$ns1" addr add dead:beef:$i::1/64 dev ns1eth$i nodad
70		ip -net "$ns1" link set ns1eth$i up
71
72		ip -net "$ns2" addr add 10.0.$i.2/24 dev ns2eth$i
73		ip -net "$ns2" addr add dead:beef:$i::2/64 dev ns2eth$i nodad
74		ip -net "$ns2" link set ns2eth$i up
75
76		# let $ns2 reach any $ns1 address from any interface
77		ip -net "$ns2" route add default via 10.0.$i.1 dev ns2eth$i metric 10$i
78	done
79}
80
81init_shapers()
82{
83	for i in `seq 1 4`; do
84		tc -n $ns1 qdisc add dev ns1eth$i root netem rate 20mbit delay 1
85		tc -n $ns2 qdisc add dev ns2eth$i root netem rate 20mbit delay 1
86	done
87}
88
89cleanup_partial()
90{
91	rm -f "$capout"
92
93	for netns in "$ns1" "$ns2"; do
94		ip netns del $netns
95		rm -f /tmp/$netns.{nstat,out}
96	done
97}
98
99cleanup()
100{
101	rm -f "$cin" "$cout" "$sinfail"
102	rm -f "$sin" "$sout" "$cinsent" "$cinfail"
103	cleanup_partial
104}
105
106reset()
107{
108	cleanup_partial
109	init
110}
111
112reset_with_cookies()
113{
114	reset
115
116	for netns in "$ns1" "$ns2";do
117		ip netns exec $netns sysctl -q net.ipv4.tcp_syncookies=2
118	done
119}
120
121reset_with_add_addr_timeout()
122{
123	local ip="${1:-4}"
124	local tables
125
126	tables="iptables"
127	if [ $ip -eq 6 ]; then
128		tables="ip6tables"
129	fi
130
131	reset
132
133	ip netns exec $ns1 sysctl -q net.mptcp.add_addr_timeout=1
134	ip netns exec $ns2 $tables -A OUTPUT -p tcp \
135		-m tcp --tcp-option 30 \
136		-m bpf --bytecode \
137		"$CBPF_MPTCP_SUBOPTION_ADD_ADDR" \
138		-j DROP
139}
140
141reset_with_checksum()
142{
143	local ns1_enable=$1
144	local ns2_enable=$2
145
146	reset
147
148	ip netns exec $ns1 sysctl -q net.mptcp.checksum_enabled=$ns1_enable
149	ip netns exec $ns2 sysctl -q net.mptcp.checksum_enabled=$ns2_enable
150}
151
152reset_with_allow_join_id0()
153{
154	local ns1_enable=$1
155	local ns2_enable=$2
156
157	reset
158
159	ip netns exec $ns1 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns1_enable
160	ip netns exec $ns2 sysctl -q net.mptcp.allow_join_initial_addr_port=$ns2_enable
161}
162
163ip -Version > /dev/null 2>&1
164if [ $? -ne 0 ];then
165	echo "SKIP: Could not run test without ip tool"
166	exit $ksft_skip
167fi
168
169iptables -V > /dev/null 2>&1
170if [ $? -ne 0 ];then
171	echo "SKIP: Could not run all tests without iptables tool"
172	exit $ksft_skip
173fi
174
175ip6tables -V > /dev/null 2>&1
176if [ $? -ne 0 ];then
177	echo "SKIP: Could not run all tests without ip6tables tool"
178	exit $ksft_skip
179fi
180
181print_file_err()
182{
183	ls -l "$1" 1>&2
184	echo "Trailing bytes are: "
185	tail -c 27 "$1"
186}
187
188check_transfer()
189{
190	in=$1
191	out=$2
192	what=$3
193
194	cmp "$in" "$out" > /dev/null 2>&1
195	if [ $? -ne 0 ] ;then
196		echo "[ FAIL ] $what does not match (in, out):"
197		print_file_err "$in"
198		print_file_err "$out"
199		ret=1
200
201		return 1
202	fi
203
204	return 0
205}
206
207do_ping()
208{
209	listener_ns="$1"
210	connector_ns="$2"
211	connect_addr="$3"
212
213	ip netns exec ${connector_ns} ping -q -c 1 $connect_addr >/dev/null
214	if [ $? -ne 0 ] ; then
215		echo "$listener_ns -> $connect_addr connectivity [ FAIL ]" 1>&2
216		ret=1
217	fi
218}
219
220link_failure()
221{
222	ns="$1"
223
224	if [ -z "$FAILING_LINKS" ]; then
225		l=$((RANDOM%4))
226		FAILING_LINKS=$((l+1))
227	fi
228
229	for l in $FAILING_LINKS; do
230		veth="ns1eth$l"
231		ip -net "$ns" link set "$veth" down
232	done
233}
234
235# $1: IP address
236is_v6()
237{
238	[ -z "${1##*:*}" ]
239}
240
241# $1: ns, $2: port
242wait_local_port_listen()
243{
244	local listener_ns="${1}"
245	local port="${2}"
246
247	local port_hex i
248
249	port_hex="$(printf "%04X" "${port}")"
250	for i in $(seq 10); do
251		ip netns exec "${listener_ns}" cat /proc/net/tcp* | \
252			awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
253			break
254		sleep 0.1
255	done
256}
257
258rm_addr_count()
259{
260	ns=${1}
261
262	ip netns exec ${ns} nstat -as | grep MPTcpExtRmAddr | awk '{print $2}'
263}
264
265# $1: ns, $2: old rm_addr counter in $ns
266wait_rm_addr()
267{
268	local ns="${1}"
269	local old_cnt="${2}"
270	local cnt
271	local i
272
273	for i in $(seq 10); do
274		cnt=$(rm_addr_count ${ns})
275		[ "$cnt" = "${old_cnt}" ] || break
276		sleep 0.1
277	done
278}
279
280do_transfer()
281{
282	listener_ns="$1"
283	connector_ns="$2"
284	cl_proto="$3"
285	srv_proto="$4"
286	connect_addr="$5"
287	test_link_fail="$6"
288	addr_nr_ns1="$7"
289	addr_nr_ns2="$8"
290	speed="$9"
291	bkup="${10}"
292
293	port=$((10000+$TEST_COUNT))
294	TEST_COUNT=$((TEST_COUNT+1))
295
296	:> "$cout"
297	:> "$sout"
298	:> "$capout"
299
300	if [ $capture -eq 1 ]; then
301		if [ -z $SUDO_USER ] ; then
302			capuser=""
303		else
304			capuser="-Z $SUDO_USER"
305		fi
306
307		capfile=$(printf "mp_join-%02u-%s.pcap" "$TEST_COUNT" "${listener_ns}")
308
309		echo "Capturing traffic for test $TEST_COUNT into $capfile"
310		ip netns exec ${listener_ns} tcpdump -i any -s 65535 -B 32768 $capuser -w $capfile > "$capout" 2>&1 &
311		cappid=$!
312
313		sleep 1
314	fi
315
316	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
317		nstat -n
318	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
319		nstat -n
320
321	if [ $speed = "fast" ]; then
322		mptcp_connect="./mptcp_connect -j"
323	elif [ $speed = "slow" ]; then
324		mptcp_connect="./mptcp_connect -r 50"
325	elif [ $speed = "least" ]; then
326		mptcp_connect="./mptcp_connect -r 10"
327	fi
328
329	local local_addr
330	if is_v6 "${connect_addr}"; then
331		local_addr="::"
332	else
333		local_addr="0.0.0.0"
334	fi
335
336	if [ "$test_link_fail" -eq 2 ];then
337		timeout ${timeout_test} \
338			ip netns exec ${listener_ns} \
339				$mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
340					${local_addr} < "$sinfail" > "$sout" &
341	else
342		timeout ${timeout_test} \
343			ip netns exec ${listener_ns} \
344				$mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
345					${local_addr} < "$sin" > "$sout" &
346	fi
347	spid=$!
348
349	wait_local_port_listen "${listener_ns}" "${port}"
350
351	if [ "$test_link_fail" -eq 0 ];then
352		timeout ${timeout_test} \
353			ip netns exec ${connector_ns} \
354				$mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
355					$connect_addr < "$cin" > "$cout" &
356	else
357		( cat "$cinfail" ; sleep 2; link_failure $listener_ns ; cat "$cinfail" ) | \
358			tee "$cinsent" | \
359			timeout ${timeout_test} \
360				ip netns exec ${connector_ns} \
361					$mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
362						$connect_addr > "$cout" &
363	fi
364	cpid=$!
365
366	# let the mptcp subflow be established in background before
367	# do endpoint manipulation
368	[ $addr_nr_ns1 = "0" -a $addr_nr_ns2 = "0" ] || sleep 1
369
370	if [ $addr_nr_ns1 -gt 0 ]; then
371		let add_nr_ns1=addr_nr_ns1
372		counter=2
373		while [ $add_nr_ns1 -gt 0 ]; do
374			local addr
375			if is_v6 "${connect_addr}"; then
376				addr="dead:beef:$counter::1"
377			else
378				addr="10.0.$counter.1"
379			fi
380			ip netns exec $ns1 ./pm_nl_ctl add $addr flags signal
381			let counter+=1
382			let add_nr_ns1-=1
383		done
384	elif [ $addr_nr_ns1 -lt 0 ]; then
385		let rm_nr_ns1=-addr_nr_ns1
386		if [ $rm_nr_ns1 -lt 8 ]; then
387			counter=1
388			pos=1
389			dump=(`ip netns exec ${listener_ns} ./pm_nl_ctl dump`)
390			if [ ${#dump[@]} -gt 0 ]; then
391				while [ $counter -le $rm_nr_ns1 ]
392				do
393					id=${dump[$pos]}
394					rm_addr=$(rm_addr_count ${connector_ns})
395					ip netns exec ${listener_ns} ./pm_nl_ctl del $id
396					wait_rm_addr ${connector_ns} ${rm_addr}
397					let counter+=1
398					let pos+=5
399				done
400			fi
401		elif [ $rm_nr_ns1 -eq 8 ]; then
402			ip netns exec ${listener_ns} ./pm_nl_ctl flush
403		elif [ $rm_nr_ns1 -eq 9 ]; then
404			ip netns exec ${listener_ns} ./pm_nl_ctl del 0 ${connect_addr}
405		fi
406	fi
407
408	flags="subflow"
409	if [[ "${addr_nr_ns2}" = "fullmesh_"* ]]; then
410		flags="${flags},fullmesh"
411		addr_nr_ns2=${addr_nr_ns2:9}
412	fi
413
414	# if newly added endpoints must be deleted, give the background msk
415	# some time to created them
416	[ $addr_nr_ns1 -gt 0 -a $addr_nr_ns2 -lt 0 ] && sleep 1
417
418	if [ $addr_nr_ns2 -gt 0 ]; then
419		let add_nr_ns2=addr_nr_ns2
420		counter=3
421		while [ $add_nr_ns2 -gt 0 ]; do
422			local addr
423			if is_v6 "${connect_addr}"; then
424				addr="dead:beef:$counter::2"
425			else
426				addr="10.0.$counter.2"
427			fi
428			ip netns exec $ns2 ./pm_nl_ctl add $addr flags $flags
429			let counter+=1
430			let add_nr_ns2-=1
431		done
432	elif [ $addr_nr_ns2 -lt 0 ]; then
433		let rm_nr_ns2=-addr_nr_ns2
434		if [ $rm_nr_ns2 -lt 8 ]; then
435			counter=1
436			pos=1
437			dump=(`ip netns exec ${connector_ns} ./pm_nl_ctl dump`)
438			if [ ${#dump[@]} -gt 0 ]; then
439				while [ $counter -le $rm_nr_ns2 ]
440				do
441					# rm_addr are serialized, allow the previous one to complete
442					id=${dump[$pos]}
443					rm_addr=$(rm_addr_count ${listener_ns})
444					ip netns exec ${connector_ns} ./pm_nl_ctl del $id
445					wait_rm_addr ${listener_ns} ${rm_addr}
446					let counter+=1
447					let pos+=5
448				done
449			fi
450		elif [ $rm_nr_ns2 -eq 8 ]; then
451			ip netns exec ${connector_ns} ./pm_nl_ctl flush
452		elif [ $rm_nr_ns2 -eq 9 ]; then
453			local addr
454			if is_v6 "${connect_addr}"; then
455				addr="dead:beef:1::2"
456			else
457				addr="10.0.1.2"
458			fi
459			ip netns exec ${connector_ns} ./pm_nl_ctl del 0 $addr
460		fi
461	fi
462
463	if [ ! -z $bkup ]; then
464		sleep 1
465		for netns in "$ns1" "$ns2"; do
466			dump=(`ip netns exec $netns ./pm_nl_ctl dump`)
467			if [ ${#dump[@]} -gt 0 ]; then
468				addr=${dump[${#dump[@]} - 1]}
469				backup="ip netns exec $netns ./pm_nl_ctl set $addr flags $bkup"
470				$backup
471			fi
472		done
473	fi
474
475	wait $cpid
476	retc=$?
477	wait $spid
478	rets=$?
479
480	if [ $capture -eq 1 ]; then
481	    sleep 1
482	    kill $cappid
483	fi
484
485	NSTAT_HISTORY=/tmp/${listener_ns}.nstat ip netns exec ${listener_ns} \
486		nstat | grep Tcp > /tmp/${listener_ns}.out
487	NSTAT_HISTORY=/tmp/${connector_ns}.nstat ip netns exec ${connector_ns} \
488		nstat | grep Tcp > /tmp/${connector_ns}.out
489
490	if [ ${rets} -ne 0 ] || [ ${retc} -ne 0 ]; then
491		echo " client exit code $retc, server $rets" 1>&2
492		echo -e "\nnetns ${listener_ns} socket stat for ${port}:" 1>&2
493		ip netns exec ${listener_ns} ss -Menita 1>&2 -o "sport = :$port"
494		cat /tmp/${listener_ns}.out
495		echo -e "\nnetns ${connector_ns} socket stat for ${port}:" 1>&2
496		ip netns exec ${connector_ns} ss -Menita 1>&2 -o "dport = :$port"
497		cat /tmp/${connector_ns}.out
498
499		cat "$capout"
500		ret=1
501		return 1
502	fi
503
504	if [ "$test_link_fail" -eq 2 ];then
505		check_transfer $sinfail $cout "file received by client"
506	else
507		check_transfer $sin $cout "file received by client"
508	fi
509	retc=$?
510	if [ "$test_link_fail" -eq 0 ];then
511		check_transfer $cin $sout "file received by server"
512	else
513		check_transfer $cinsent $sout "file received by server"
514	fi
515	rets=$?
516
517	if [ $retc -eq 0 ] && [ $rets -eq 0 ];then
518		cat "$capout"
519		return 0
520	fi
521
522	cat "$capout"
523	return 1
524}
525
526make_file()
527{
528	name=$1
529	who=$2
530	size=$3
531
532	dd if=/dev/urandom of="$name" bs=1024 count=$size 2> /dev/null
533	echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
534
535	echo "Created $name (size $size KB) containing data sent by $who"
536}
537
538run_tests()
539{
540	listener_ns="$1"
541	connector_ns="$2"
542	connect_addr="$3"
543	test_linkfail="${4:-0}"
544	addr_nr_ns1="${5:-0}"
545	addr_nr_ns2="${6:-0}"
546	speed="${7:-fast}"
547	bkup="${8:-""}"
548	lret=0
549	oldin=""
550
551	# create the input file for the failure test when
552	# the first failure test run
553	if [ "$test_linkfail" -ne 0 -a -z "$cinfail" ]; then
554		# the client file must be considerably larger
555		# of the maximum expected cwin value, or the
556		# link utilization will be not predicable
557		size=$((RANDOM%2))
558		size=$((size+1))
559		size=$((size*8192))
560		size=$((size + ( $RANDOM % 8192) ))
561
562		cinfail=$(mktemp)
563		make_file "$cinfail" "client" $size
564	fi
565
566	if [ "$test_linkfail" -eq 2 -a -z "$sinfail" ]; then
567		size=$((RANDOM%16))
568		size=$((size+1))
569		size=$((size*2048))
570
571		sinfail=$(mktemp)
572		make_file "$sinfail" "server" $size
573	fi
574
575	do_transfer ${listener_ns} ${connector_ns} MPTCP MPTCP ${connect_addr} \
576		${test_linkfail} ${addr_nr_ns1} ${addr_nr_ns2} ${speed} ${bkup}
577	lret=$?
578}
579
580dump_stats()
581{
582	echo Server ns stats
583	ip netns exec $ns1 nstat -as | grep Tcp
584	echo Client ns stats
585	ip netns exec $ns2 nstat -as | grep Tcp
586}
587
588chk_csum_nr()
589{
590	local msg=${1:-""}
591	local count
592	local dump_stats
593
594	if [ ! -z "$msg" ]; then
595		printf "%02u" "$TEST_COUNT"
596	else
597		echo -n "  "
598	fi
599	printf " %-36s %s" "$msg" "sum"
600	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtDataCsumErr | awk '{print $2}'`
601	[ -z "$count" ] && count=0
602	if [ "$count" != 0 ]; then
603		echo "[fail] got $count data checksum error[s] expected 0"
604		ret=1
605		dump_stats=1
606	else
607		echo -n "[ ok ]"
608	fi
609	echo -n " - csum  "
610	count=`ip netns exec $ns2 nstat -as | grep MPTcpExtDataCsumErr | awk '{print $2}'`
611	[ -z "$count" ] && count=0
612	if [ "$count" != 0 ]; then
613		echo "[fail] got $count data checksum error[s] expected 0"
614		ret=1
615		dump_stats=1
616	else
617		echo "[ ok ]"
618	fi
619	[ "${dump_stats}" = 1 ] && dump_stats
620}
621
622chk_fail_nr()
623{
624	local mp_fail_nr_tx=$1
625	local mp_fail_nr_rx=$2
626	local count
627	local dump_stats
628
629	printf "%-39s %s" " " "ftx"
630	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPFailTx | awk '{print $2}'`
631	[ -z "$count" ] && count=0
632	if [ "$count" != "$mp_fail_nr_tx" ]; then
633		echo "[fail] got $count MP_FAIL[s] TX expected $mp_fail_nr_tx"
634		ret=1
635		dump_stats=1
636	else
637		echo -n "[ ok ]"
638	fi
639
640	echo -n " - frx   "
641	count=`ip netns exec $ns2 nstat -as | grep MPTcpExtMPFailRx | awk '{print $2}'`
642	[ -z "$count" ] && count=0
643	if [ "$count" != "$mp_fail_nr_rx" ]; then
644		echo "[fail] got $count MP_FAIL[s] RX expected $mp_fail_nr_rx"
645		ret=1
646		dump_stats=1
647	else
648		echo "[ ok ]"
649	fi
650
651	[ "${dump_stats}" = 1 ] && dump_stats
652}
653
654chk_join_nr()
655{
656	local msg="$1"
657	local syn_nr=$2
658	local syn_ack_nr=$3
659	local ack_nr=$4
660	local count
661	local dump_stats
662
663	printf "%02u %-36s %s" "$TEST_COUNT" "$msg" "syn"
664	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinSynRx | awk '{print $2}'`
665	[ -z "$count" ] && count=0
666	if [ "$count" != "$syn_nr" ]; then
667		echo "[fail] got $count JOIN[s] syn expected $syn_nr"
668		ret=1
669		dump_stats=1
670	else
671		echo -n "[ ok ]"
672	fi
673
674	echo -n " - synack"
675	count=`ip netns exec $ns2 nstat -as | grep MPTcpExtMPJoinSynAckRx | awk '{print $2}'`
676	[ -z "$count" ] && count=0
677	if [ "$count" != "$syn_ack_nr" ]; then
678		echo "[fail] got $count JOIN[s] synack expected $syn_ack_nr"
679		ret=1
680		dump_stats=1
681	else
682		echo -n "[ ok ]"
683	fi
684
685	echo -n " - ack"
686	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinAckRx | awk '{print $2}'`
687	[ -z "$count" ] && count=0
688	if [ "$count" != "$ack_nr" ]; then
689		echo "[fail] got $count JOIN[s] ack expected $ack_nr"
690		ret=1
691		dump_stats=1
692	else
693		echo "[ ok ]"
694	fi
695	[ "${dump_stats}" = 1 ] && dump_stats
696	if [ $checksum -eq 1 ]; then
697		chk_csum_nr
698		chk_fail_nr 0 0
699	fi
700}
701
702# a negative value for 'stale_max' means no upper bound:
703# for bidirectional transfer, if one peer sleep for a while
704# - as these tests do - we can have a quite high number of
705# stale/recover conversions, proportional to
706# sleep duration/ MPTCP-level RTX interval.
707chk_stale_nr()
708{
709	local ns=$1
710	local stale_min=$2
711	local stale_max=$3
712	local stale_delta=$4
713	local dump_stats
714	local stale_nr
715	local recover_nr
716
717	printf "%-39s %-18s" " " "stale"
718	stale_nr=`ip netns exec $ns nstat -as | grep MPTcpExtSubflowStale | awk '{print $2}'`
719	[ -z "$stale_nr" ] && stale_nr=0
720	recover_nr=`ip netns exec $ns nstat -as | grep MPTcpExtSubflowRecover | awk '{print $2}'`
721	[ -z "$recover_nr" ] && recover_nr=0
722
723	if [ $stale_nr -lt $stale_min ] ||
724	   [ $stale_max -gt 0 -a $stale_nr -gt $stale_max ] ||
725	   [ $((stale_nr - $recover_nr)) -ne $stale_delta ]; then
726		echo "[fail] got $stale_nr stale[s] $recover_nr recover[s], " \
727		     " expected stale in range [$stale_min..$stale_max]," \
728		     " stale-recover delta $stale_delta "
729		ret=1
730		dump_stats=1
731	else
732		echo "[ ok ]"
733	fi
734
735	if [ "${dump_stats}" = 1 ]; then
736		echo $ns stats
737		ip netns exec $ns ip -s link show
738		ip netns exec $ns nstat -as | grep MPTcp
739	fi
740}
741
742chk_add_nr()
743{
744	local add_nr=$1
745	local echo_nr=$2
746	local port_nr=${3:-0}
747	local syn_nr=${4:-$port_nr}
748	local syn_ack_nr=${5:-$port_nr}
749	local ack_nr=${6:-$port_nr}
750	local mis_syn_nr=${7:-0}
751	local mis_ack_nr=${8:-0}
752	local count
753	local dump_stats
754
755	printf "%-39s %s" " " "add"
756	count=`ip netns exec $ns2 nstat -as | grep MPTcpExtAddAddr | awk '{print $2}'`
757	[ -z "$count" ] && count=0
758	if [ "$count" != "$add_nr" ]; then
759		echo "[fail] got $count ADD_ADDR[s] expected $add_nr"
760		ret=1
761		dump_stats=1
762	else
763		echo -n "[ ok ]"
764	fi
765
766	echo -n " - echo  "
767	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtEchoAdd | awk '{print $2}'`
768	[ -z "$count" ] && count=0
769	if [ "$count" != "$echo_nr" ]; then
770		echo "[fail] got $count ADD_ADDR echo[s] expected $echo_nr"
771		ret=1
772		dump_stats=1
773	else
774		echo -n "[ ok ]"
775	fi
776
777	if [ $port_nr -gt 0 ]; then
778		echo -n " - pt "
779		count=`ip netns exec $ns2 nstat -as | grep MPTcpExtPortAdd | awk '{print $2}'`
780		[ -z "$count" ] && count=0
781		if [ "$count" != "$port_nr" ]; then
782			echo "[fail] got $count ADD_ADDR[s] with a port-number expected $port_nr"
783			ret=1
784			dump_stats=1
785		else
786			echo "[ ok ]"
787		fi
788
789		printf "%-39s %s" " " "syn"
790		count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinPortSynRx |
791			awk '{print $2}'`
792		[ -z "$count" ] && count=0
793		if [ "$count" != "$syn_nr" ]; then
794			echo "[fail] got $count JOIN[s] syn with a different \
795				port-number expected $syn_nr"
796			ret=1
797			dump_stats=1
798		else
799			echo -n "[ ok ]"
800		fi
801
802		echo -n " - synack"
803		count=`ip netns exec $ns2 nstat -as | grep MPTcpExtMPJoinPortSynAckRx |
804			awk '{print $2}'`
805		[ -z "$count" ] && count=0
806		if [ "$count" != "$syn_ack_nr" ]; then
807			echo "[fail] got $count JOIN[s] synack with a different \
808				port-number expected $syn_ack_nr"
809			ret=1
810			dump_stats=1
811		else
812			echo -n "[ ok ]"
813		fi
814
815		echo -n " - ack"
816		count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPJoinPortAckRx |
817			awk '{print $2}'`
818		[ -z "$count" ] && count=0
819		if [ "$count" != "$ack_nr" ]; then
820			echo "[fail] got $count JOIN[s] ack with a different \
821				port-number expected $ack_nr"
822			ret=1
823			dump_stats=1
824		else
825			echo "[ ok ]"
826		fi
827
828		printf "%-39s %s" " " "syn"
829		count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMismatchPortSynRx |
830			awk '{print $2}'`
831		[ -z "$count" ] && count=0
832		if [ "$count" != "$mis_syn_nr" ]; then
833			echo "[fail] got $count JOIN[s] syn with a mismatched \
834				port-number expected $mis_syn_nr"
835			ret=1
836			dump_stats=1
837		else
838			echo -n "[ ok ]"
839		fi
840
841		echo -n " - ack   "
842		count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMismatchPortAckRx |
843			awk '{print $2}'`
844		[ -z "$count" ] && count=0
845		if [ "$count" != "$mis_ack_nr" ]; then
846			echo "[fail] got $count JOIN[s] ack with a mismatched \
847				port-number expected $mis_ack_nr"
848			ret=1
849			dump_stats=1
850		else
851			echo "[ ok ]"
852		fi
853	else
854		echo ""
855	fi
856
857	[ "${dump_stats}" = 1 ] && dump_stats
858}
859
860chk_rm_nr()
861{
862	local rm_addr_nr=$1
863	local rm_subflow_nr=$2
864	local invert=${3:-""}
865	local count
866	local dump_stats
867	local addr_ns
868	local subflow_ns
869
870	if [ -z $invert ]; then
871		addr_ns=$ns1
872		subflow_ns=$ns2
873	elif [ $invert = "invert" ]; then
874		addr_ns=$ns2
875		subflow_ns=$ns1
876	fi
877
878	printf "%-39s %s" " " "rm "
879	count=`ip netns exec $addr_ns nstat -as | grep MPTcpExtRmAddr | awk '{print $2}'`
880	[ -z "$count" ] && count=0
881	if [ "$count" != "$rm_addr_nr" ]; then
882		echo "[fail] got $count RM_ADDR[s] expected $rm_addr_nr"
883		ret=1
884		dump_stats=1
885	else
886		echo -n "[ ok ]"
887	fi
888
889	echo -n " - sf    "
890	count=`ip netns exec $subflow_ns nstat -as | grep MPTcpExtRmSubflow | awk '{print $2}'`
891	[ -z "$count" ] && count=0
892	if [ "$count" != "$rm_subflow_nr" ]; then
893		echo "[fail] got $count RM_SUBFLOW[s] expected $rm_subflow_nr"
894		ret=1
895		dump_stats=1
896	else
897		echo "[ ok ]"
898	fi
899
900	[ "${dump_stats}" = 1 ] && dump_stats
901}
902
903chk_prio_nr()
904{
905	local mp_prio_nr_tx=$1
906	local mp_prio_nr_rx=$2
907	local count
908	local dump_stats
909
910	printf "%-39s %s" " " "ptx"
911	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPPrioTx | awk '{print $2}'`
912	[ -z "$count" ] && count=0
913	if [ "$count" != "$mp_prio_nr_tx" ]; then
914		echo "[fail] got $count MP_PRIO[s] TX expected $mp_prio_nr_tx"
915		ret=1
916		dump_stats=1
917	else
918		echo -n "[ ok ]"
919	fi
920
921	echo -n " - prx   "
922	count=`ip netns exec $ns1 nstat -as | grep MPTcpExtMPPrioRx | awk '{print $2}'`
923	[ -z "$count" ] && count=0
924	if [ "$count" != "$mp_prio_nr_rx" ]; then
925		echo "[fail] got $count MP_PRIO[s] RX expected $mp_prio_nr_rx"
926		ret=1
927		dump_stats=1
928	else
929		echo "[ ok ]"
930	fi
931
932	[ "${dump_stats}" = 1 ] && dump_stats
933}
934
935chk_link_usage()
936{
937	local ns=$1
938	local link=$2
939	local out=$3
940	local expected_rate=$4
941	local tx_link=`ip netns exec $ns cat /sys/class/net/$link/statistics/tx_bytes`
942	local tx_total=`ls -l $out | awk '{print $5}'`
943	local tx_rate=$((tx_link * 100 / $tx_total))
944	local tolerance=5
945
946	printf "%-39s %-18s" " " "link usage"
947	if [ $tx_rate -lt $((expected_rate - $tolerance)) -o \
948	     $tx_rate -gt $((expected_rate + $tolerance)) ]; then
949		echo "[fail] got $tx_rate% usage, expected $expected_rate%"
950		ret=1
951	else
952		echo "[ ok ]"
953	fi
954}
955
956wait_for_tw()
957{
958	local timeout_ms=$((timeout_poll * 1000))
959	local time=0
960	local ns=$1
961
962	while [ $time -lt $timeout_ms ]; do
963		local cnt=$(ip netns exec $ns ss -t state time-wait |wc -l)
964
965		[ "$cnt" = 1 ] && return 1
966		time=$((time + 100))
967		sleep 0.1
968	done
969	return 1
970}
971
972subflows_tests()
973{
974	reset
975	run_tests $ns1 $ns2 10.0.1.1
976	chk_join_nr "no JOIN" "0" "0" "0"
977
978	# subflow limited by client
979	reset
980	ip netns exec $ns1 ./pm_nl_ctl limits 0 0
981	ip netns exec $ns2 ./pm_nl_ctl limits 0 0
982	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
983	run_tests $ns1 $ns2 10.0.1.1
984	chk_join_nr "single subflow, limited by client" 0 0 0
985
986	# subflow limited by server
987	reset
988	ip netns exec $ns1 ./pm_nl_ctl limits 0 0
989	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
990	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
991	run_tests $ns1 $ns2 10.0.1.1
992	chk_join_nr "single subflow, limited by server" 1 1 0
993
994	# subflow
995	reset
996	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
997	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
998	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
999	run_tests $ns1 $ns2 10.0.1.1
1000	chk_join_nr "single subflow" 1 1 1
1001
1002	# multiple subflows
1003	reset
1004	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1005	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1006	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1007	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1008	run_tests $ns1 $ns2 10.0.1.1
1009	chk_join_nr "multiple subflows" 2 2 2
1010
1011	# multiple subflows limited by server
1012	reset
1013	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1014	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1015	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1016	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1017	run_tests $ns1 $ns2 10.0.1.1
1018	chk_join_nr "multiple subflows, limited by server" 2 2 1
1019
1020	# single subflow, dev
1021	reset
1022	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1023	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1024	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow dev ns2eth3
1025	run_tests $ns1 $ns2 10.0.1.1
1026	chk_join_nr "single subflow, dev" 1 1 1
1027}
1028
1029subflows_error_tests()
1030{
1031	# If a single subflow is configured, and matches the MPC src
1032	# address, no additional subflow should be created
1033	reset
1034	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1035	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1036	ip netns exec $ns2 ./pm_nl_ctl add 10.0.1.2 flags subflow
1037	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1038	chk_join_nr "no MPC reuse with single endpoint" 0 0 0
1039
1040	# multiple subflows, with subflow creation error
1041	reset
1042	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1043	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1044	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1045	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1046	ip netns exec $ns1 iptables -A INPUT -s 10.0.3.2 -p tcp -j REJECT
1047	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1048	chk_join_nr "multi subflows, with failing subflow" 1 1 1
1049
1050	# multiple subflows, with subflow timeout on MPJ
1051	reset
1052	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1053	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1054	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1055	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1056	ip netns exec $ns1 iptables -A INPUT -s 10.0.3.2 -p tcp -j DROP
1057	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1058	chk_join_nr "multi subflows, with subflow timeout" 1 1 1
1059
1060	# multiple subflows, check that the endpoint corresponding to
1061	# closed subflow (due to reset) is not reused if additional
1062	# subflows are added later
1063	reset
1064	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1065	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1066	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1067	ip netns exec $ns1 iptables -A INPUT -s 10.0.3.2 -p tcp -j REJECT
1068	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow &
1069
1070	# updates in the child shell do not have any effect here, we
1071	# need to bump the test counter for the above case
1072	TEST_COUNT=$((TEST_COUNT+1))
1073
1074	# mpj subflow will be in TW after the reset
1075	wait_for_tw $ns2
1076	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1077	wait
1078
1079	# additional subflow could be created only if the PM select
1080	# the later endpoint, skipping the already used one
1081	chk_join_nr "multi subflows, fair usage on close" 1 1 1
1082}
1083
1084signal_address_tests()
1085{
1086	# add_address, unused
1087	reset
1088	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1089	run_tests $ns1 $ns2 10.0.1.1
1090	chk_join_nr "unused signal address" 0 0 0
1091	chk_add_nr 1 1
1092
1093	# accept and use add_addr
1094	reset
1095	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1096	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1097	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1098	run_tests $ns1 $ns2 10.0.1.1
1099	chk_join_nr "signal address" 1 1 1
1100	chk_add_nr 1 1
1101
1102	# accept and use add_addr with an additional subflow
1103	# note: signal address in server ns and local addresses in client ns must
1104	# belong to different subnets or one of the listed local address could be
1105	# used for 'add_addr' subflow
1106	reset
1107	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1108	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1109	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1110	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1111	run_tests $ns1 $ns2 10.0.1.1
1112	chk_join_nr "subflow and signal" 2 2 2
1113	chk_add_nr 1 1
1114
1115	# accept and use add_addr with additional subflows
1116	reset
1117	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1118	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1119	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1120	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1121	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1122	run_tests $ns1 $ns2 10.0.1.1
1123	chk_join_nr "multiple subflows and signal" 3 3 3
1124	chk_add_nr 1 1
1125
1126	# signal addresses
1127	reset
1128	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1129	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1130	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1131	ip netns exec $ns1 ./pm_nl_ctl add 10.0.4.1 flags signal
1132	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1133	run_tests $ns1 $ns2 10.0.1.1
1134	chk_join_nr "signal addresses" 3 3 3
1135	chk_add_nr 3 3
1136
1137	# signal invalid addresses
1138	reset
1139	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1140	ip netns exec $ns1 ./pm_nl_ctl add 10.0.12.1 flags signal
1141	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1142	ip netns exec $ns1 ./pm_nl_ctl add 10.0.14.1 flags signal
1143	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1144	run_tests $ns1 $ns2 10.0.1.1
1145	chk_join_nr "signal invalid addresses" 1 1 1
1146	chk_add_nr 3 3
1147
1148	# signal addresses race test
1149	reset
1150	ip netns exec $ns1 ./pm_nl_ctl limits 4 4
1151	ip netns exec $ns2 ./pm_nl_ctl limits 4 4
1152	ip netns exec $ns1 ./pm_nl_ctl add 10.0.1.1 flags signal
1153	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1154	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1155	ip netns exec $ns1 ./pm_nl_ctl add 10.0.4.1 flags signal
1156	ip netns exec $ns2 ./pm_nl_ctl add 10.0.1.2 flags signal
1157	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags signal
1158	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags signal
1159	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags signal
1160	run_tests $ns1 $ns2 10.0.1.1
1161
1162	# the server will not signal the address terminating
1163	# the MPC subflow
1164	chk_add_nr 3 3
1165}
1166
1167link_failure_tests()
1168{
1169	# accept and use add_addr with additional subflows and link loss
1170	reset
1171
1172	# without any b/w limit each veth could spool the packets and get
1173	# them acked at xmit time, so that the corresponding subflow will
1174	# have almost always no outstanding pkts, the scheduler will pick
1175	# always the first subflow and we will have hard time testing
1176	# active backup and link switch-over.
1177	# Let's set some arbitrary (low) virtual link limits.
1178	init_shapers
1179	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1180	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 dev ns1eth2 flags signal
1181	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1182	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 dev ns2eth3 flags subflow
1183	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 dev ns2eth4 flags subflow
1184	run_tests $ns1 $ns2 10.0.1.1 1
1185	chk_join_nr "multiple flows, signal, link failure" 3 3 3
1186	chk_add_nr 1 1
1187	chk_stale_nr $ns2 1 5 1
1188
1189	# accept and use add_addr with additional subflows and link loss
1190	# for bidirectional transfer
1191	reset
1192	init_shapers
1193	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1194	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 dev ns1eth2 flags signal
1195	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1196	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 dev ns2eth3 flags subflow
1197	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 dev ns2eth4 flags subflow
1198	run_tests $ns1 $ns2 10.0.1.1 2
1199	chk_join_nr "multi flows, signal, bidi, link fail" 3 3 3
1200	chk_add_nr 1 1
1201	chk_stale_nr $ns2 1 -1 1
1202
1203	# 2 subflows plus 1 backup subflow with a lossy link, backup
1204	# will never be used
1205	reset
1206	init_shapers
1207	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1208	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 dev ns1eth2 flags signal
1209	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1210	export FAILING_LINKS="1"
1211	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 dev ns2eth3 flags subflow,backup
1212	run_tests $ns1 $ns2 10.0.1.1 1
1213	chk_join_nr "backup subflow unused, link failure" 2 2 2
1214	chk_add_nr 1 1
1215	chk_link_usage $ns2 ns2eth3 $cinsent 0
1216
1217	# 2 lossy links after half transfer, backup will get half of
1218	# the traffic
1219	reset
1220	init_shapers
1221	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1222	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 dev ns1eth2 flags signal
1223	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1224	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 dev ns2eth3 flags subflow,backup
1225	export FAILING_LINKS="1 2"
1226	run_tests $ns1 $ns2 10.0.1.1 1
1227	chk_join_nr "backup flow used, multi links fail" 2 2 2
1228	chk_add_nr 1 1
1229	chk_stale_nr $ns2 2 4 2
1230	chk_link_usage $ns2 ns2eth3 $cinsent 50
1231
1232	# use a backup subflow with the first subflow on a lossy link
1233	# for bidirectional transfer
1234	reset
1235	init_shapers
1236	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1237	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 dev ns1eth2 flags signal
1238	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1239	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 dev ns2eth3 flags subflow,backup
1240	run_tests $ns1 $ns2 10.0.1.1 2
1241	chk_join_nr "backup flow used, bidi, link failure" 2 2 2
1242	chk_add_nr 1 1
1243	chk_stale_nr $ns2 1 -1 2
1244	chk_link_usage $ns2 ns2eth3 $cinsent 50
1245}
1246
1247add_addr_timeout_tests()
1248{
1249	# add_addr timeout
1250	reset_with_add_addr_timeout
1251	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1252	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1253	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1254	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow
1255	chk_join_nr "signal address, ADD_ADDR timeout" 1 1 1
1256	chk_add_nr 4 0
1257
1258	# add_addr timeout IPv6
1259	reset_with_add_addr_timeout 6
1260	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1261	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1262	ip netns exec $ns1 ./pm_nl_ctl add dead:beef:2::1 flags signal
1263	run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
1264	chk_join_nr "signal address, ADD_ADDR6 timeout" 1 1 1
1265	chk_add_nr 4 0
1266
1267	# signal addresses timeout
1268	reset_with_add_addr_timeout
1269	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1270	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1271	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1272	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1273	run_tests $ns1 $ns2 10.0.1.1 0 0 0 least
1274	chk_join_nr "signal addresses, ADD_ADDR timeout" 2 2 2
1275	chk_add_nr 8 0
1276
1277	# signal invalid addresses timeout
1278	reset_with_add_addr_timeout
1279	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1280	ip netns exec $ns1 ./pm_nl_ctl add 10.0.12.1 flags signal
1281	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1282	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1283	run_tests $ns1 $ns2 10.0.1.1 0 0 0 least
1284	chk_join_nr "invalid address, ADD_ADDR timeout" 1 1 1
1285	chk_add_nr 8 0
1286}
1287
1288remove_tests()
1289{
1290	# single subflow, remove
1291	reset
1292	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1293	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1294	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1295	run_tests $ns1 $ns2 10.0.1.1 0 0 -1 slow
1296	chk_join_nr "remove single subflow" 1 1 1
1297	chk_rm_nr 1 1
1298
1299	# multiple subflows, remove
1300	reset
1301	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1302	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1303	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1304	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1305	run_tests $ns1 $ns2 10.0.1.1 0 0 -2 slow
1306	chk_join_nr "remove multiple subflows" 2 2 2
1307	chk_rm_nr 2 2
1308
1309	# single address, remove
1310	reset
1311	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1312	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1313	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1314	run_tests $ns1 $ns2 10.0.1.1 0 -1 0 slow
1315	chk_join_nr "remove single address" 1 1 1
1316	chk_add_nr 1 1
1317	chk_rm_nr 1 1 invert
1318
1319	# subflow and signal, remove
1320	reset
1321	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1322	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1323	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1324	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1325	run_tests $ns1 $ns2 10.0.1.1 0 -1 -1 slow
1326	chk_join_nr "remove subflow and signal" 2 2 2
1327	chk_add_nr 1 1
1328	chk_rm_nr 1 1
1329
1330	# subflows and signal, remove
1331	reset
1332	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1333	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1334	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1335	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1336	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1337	run_tests $ns1 $ns2 10.0.1.1 0 -1 -2 slow
1338	chk_join_nr "remove subflows and signal" 3 3 3
1339	chk_add_nr 1 1
1340	chk_rm_nr 2 2
1341
1342	# addresses remove
1343	reset
1344	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1345	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal id 250
1346	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1347	ip netns exec $ns1 ./pm_nl_ctl add 10.0.4.1 flags signal
1348	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1349	run_tests $ns1 $ns2 10.0.1.1 0 -3 0 slow
1350	chk_join_nr "remove addresses" 3 3 3
1351	chk_add_nr 3 3
1352	chk_rm_nr 3 3 invert
1353
1354	# invalid addresses remove
1355	reset
1356	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1357	ip netns exec $ns1 ./pm_nl_ctl add 10.0.12.1 flags signal
1358	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1359	ip netns exec $ns1 ./pm_nl_ctl add 10.0.14.1 flags signal
1360	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1361	run_tests $ns1 $ns2 10.0.1.1 0 -3 0 slow
1362	chk_join_nr "remove invalid addresses" 1 1 1
1363	chk_add_nr 3 3
1364	chk_rm_nr 3 1 invert
1365
1366	# subflows and signal, flush
1367	reset
1368	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1369	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1370	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1371	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1372	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1373	run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
1374	chk_join_nr "flush subflows and signal" 3 3 3
1375	chk_add_nr 1 1
1376	chk_rm_nr 2 2
1377
1378	# subflows flush
1379	reset
1380	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1381	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1382	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow id 150
1383	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1384	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1385	run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
1386	chk_join_nr "flush subflows" 3 3 3
1387	chk_rm_nr 3 3
1388
1389	# addresses flush
1390	reset
1391	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1392	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal id 250
1393	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1394	ip netns exec $ns1 ./pm_nl_ctl add 10.0.4.1 flags signal
1395	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1396	run_tests $ns1 $ns2 10.0.1.1 0 -8 -8 slow
1397	chk_join_nr "flush addresses" 3 3 3
1398	chk_add_nr 3 3
1399	chk_rm_nr 3 3 invert
1400
1401	# invalid addresses flush
1402	reset
1403	ip netns exec $ns1 ./pm_nl_ctl limits 3 3
1404	ip netns exec $ns1 ./pm_nl_ctl add 10.0.12.1 flags signal
1405	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal
1406	ip netns exec $ns1 ./pm_nl_ctl add 10.0.14.1 flags signal
1407	ip netns exec $ns2 ./pm_nl_ctl limits 3 3
1408	run_tests $ns1 $ns2 10.0.1.1 0 -8 0 slow
1409	chk_join_nr "flush invalid addresses" 1 1 1
1410	chk_add_nr 3 3
1411	chk_rm_nr 3 1 invert
1412
1413	# remove id 0 subflow
1414	reset
1415	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1416	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1417	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1418	run_tests $ns1 $ns2 10.0.1.1 0 0 -9 slow
1419	chk_join_nr "remove id 0 subflow" 1 1 1
1420	chk_rm_nr 1 1
1421
1422	# remove id 0 address
1423	reset
1424	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1425	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1426	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1427	run_tests $ns1 $ns2 10.0.1.1 0 -9 0 slow
1428	chk_join_nr "remove id 0 address" 1 1 1
1429	chk_add_nr 1 1
1430	chk_rm_nr 1 1 invert
1431}
1432
1433add_tests()
1434{
1435	# add single subflow
1436	reset
1437	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1438	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1439	run_tests $ns1 $ns2 10.0.1.1 0 0 1 slow
1440	chk_join_nr "add single subflow" 1 1 1
1441
1442	# add signal address
1443	reset
1444	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1445	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1446	run_tests $ns1 $ns2 10.0.1.1 0 1 0 slow
1447	chk_join_nr "add signal address" 1 1 1
1448	chk_add_nr 1 1
1449
1450	# add multiple subflows
1451	reset
1452	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1453	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1454	run_tests $ns1 $ns2 10.0.1.1 0 0 2 slow
1455	chk_join_nr "add multiple subflows" 2 2 2
1456
1457	# add multiple subflows IPv6
1458	reset
1459	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1460	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1461	run_tests $ns1 $ns2 dead:beef:1::1 0 0 2 slow
1462	chk_join_nr "add multiple subflows IPv6" 2 2 2
1463
1464	# add multiple addresses IPv6
1465	reset
1466	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1467	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1468	run_tests $ns1 $ns2 dead:beef:1::1 0 2 0 slow
1469	chk_join_nr "add multiple addresses IPv6" 2 2 2
1470	chk_add_nr 2 2
1471}
1472
1473ipv6_tests()
1474{
1475	# subflow IPv6
1476	reset
1477	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1478	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1479	ip netns exec $ns2 ./pm_nl_ctl add dead:beef:3::2 flags subflow
1480	run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
1481	chk_join_nr "single subflow IPv6" 1 1 1
1482
1483	# add_address, unused IPv6
1484	reset
1485	ip netns exec $ns1 ./pm_nl_ctl add dead:beef:2::1 flags signal
1486	run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
1487	chk_join_nr "unused signal address IPv6" 0 0 0
1488	chk_add_nr 1 1
1489
1490	# signal address IPv6
1491	reset
1492	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1493	ip netns exec $ns1 ./pm_nl_ctl add dead:beef:2::1 flags signal
1494	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1495	run_tests $ns1 $ns2 dead:beef:1::1 0 0 0 slow
1496	chk_join_nr "single address IPv6" 1 1 1
1497	chk_add_nr 1 1
1498
1499	# single address IPv6, remove
1500	reset
1501	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1502	ip netns exec $ns1 ./pm_nl_ctl add dead:beef:2::1 flags signal
1503	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1504	run_tests $ns1 $ns2 dead:beef:1::1 0 -1 0 slow
1505	chk_join_nr "remove single address IPv6" 1 1 1
1506	chk_add_nr 1 1
1507	chk_rm_nr 1 1 invert
1508
1509	# subflow and signal IPv6, remove
1510	reset
1511	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1512	ip netns exec $ns1 ./pm_nl_ctl add dead:beef:2::1 flags signal
1513	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1514	ip netns exec $ns2 ./pm_nl_ctl add dead:beef:3::2 flags subflow
1515	run_tests $ns1 $ns2 dead:beef:1::1 0 -1 -1 slow
1516	chk_join_nr "remove subflow and signal IPv6" 2 2 2
1517	chk_add_nr 1 1
1518	chk_rm_nr 1 1
1519}
1520
1521v4mapped_tests()
1522{
1523	# subflow IPv4-mapped to IPv4-mapped
1524	reset
1525	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1526	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1527	ip netns exec $ns2 ./pm_nl_ctl add "::ffff:10.0.3.2" flags subflow
1528	run_tests $ns1 $ns2 "::ffff:10.0.1.1"
1529	chk_join_nr "single subflow IPv4-mapped" 1 1 1
1530
1531	# signal address IPv4-mapped with IPv4-mapped sk
1532	reset
1533	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1534	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1535	ip netns exec $ns1 ./pm_nl_ctl add "::ffff:10.0.2.1" flags signal
1536	run_tests $ns1 $ns2 "::ffff:10.0.1.1"
1537	chk_join_nr "signal address IPv4-mapped" 1 1 1
1538	chk_add_nr 1 1
1539
1540	# subflow v4-map-v6
1541	reset
1542	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1543	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1544	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1545	run_tests $ns1 $ns2 "::ffff:10.0.1.1"
1546	chk_join_nr "single subflow v4-map-v6" 1 1 1
1547
1548	# signal address v4-map-v6
1549	reset
1550	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1551	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1552	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1553	run_tests $ns1 $ns2 "::ffff:10.0.1.1"
1554	chk_join_nr "signal address v4-map-v6" 1 1 1
1555	chk_add_nr 1 1
1556
1557	# subflow v6-map-v4
1558	reset
1559	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1560	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1561	ip netns exec $ns2 ./pm_nl_ctl add "::ffff:10.0.3.2" flags subflow
1562	run_tests $ns1 $ns2 10.0.1.1
1563	chk_join_nr "single subflow v6-map-v4" 1 1 1
1564
1565	# signal address v6-map-v4
1566	reset
1567	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1568	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1569	ip netns exec $ns1 ./pm_nl_ctl add "::ffff:10.0.2.1" flags signal
1570	run_tests $ns1 $ns2 10.0.1.1
1571	chk_join_nr "signal address v6-map-v4" 1 1 1
1572	chk_add_nr 1 1
1573
1574	# no subflow IPv6 to v4 address
1575	reset
1576	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1577	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1578	ip netns exec $ns2 ./pm_nl_ctl add dead:beef:2::2 flags subflow
1579	run_tests $ns1 $ns2 10.0.1.1
1580	chk_join_nr "no JOIN with diff families v4-v6" 0 0 0
1581
1582	# no subflow IPv6 to v4 address even if v6 has a valid v4 at the end
1583	reset
1584	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1585	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1586	ip netns exec $ns2 ./pm_nl_ctl add dead:beef:2::10.0.3.2 flags subflow
1587	run_tests $ns1 $ns2 10.0.1.1
1588	chk_join_nr "no JOIN with diff families v4-v6-2" 0 0 0
1589
1590	# no subflow IPv4 to v6 address, no need to slow down too then
1591	reset
1592	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1593	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1594	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1595	run_tests $ns1 $ns2 dead:beef:1::1
1596	chk_join_nr "no JOIN with diff families v6-v4" 0 0 0
1597}
1598
1599backup_tests()
1600{
1601	# single subflow, backup
1602	reset
1603	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1604	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1605	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow,backup
1606	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow nobackup
1607	chk_join_nr "single subflow, backup" 1 1 1
1608	chk_prio_nr 0 1
1609
1610	# single address, backup
1611	reset
1612	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1613	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1614	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1615	run_tests $ns1 $ns2 10.0.1.1 0 0 0 slow backup
1616	chk_join_nr "single address, backup" 1 1 1
1617	chk_add_nr 1 1
1618	chk_prio_nr 1 0
1619}
1620
1621add_addr_ports_tests()
1622{
1623	# signal address with port
1624	reset
1625	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1626	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1627	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1628	run_tests $ns1 $ns2 10.0.1.1
1629	chk_join_nr "signal address with port" 1 1 1
1630	chk_add_nr 1 1 1
1631
1632	# subflow and signal with port
1633	reset
1634	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1635	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1636	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1637	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1638	run_tests $ns1 $ns2 10.0.1.1
1639	chk_join_nr "subflow and signal with port" 2 2 2
1640	chk_add_nr 1 1 1
1641
1642	# single address with port, remove
1643	reset
1644	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1645	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1646	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1647	run_tests $ns1 $ns2 10.0.1.1 0 -1 0 slow
1648	chk_join_nr "remove single address with port" 1 1 1
1649	chk_add_nr 1 1 1
1650	chk_rm_nr 1 1 invert
1651
1652	# subflow and signal with port, remove
1653	reset
1654	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1655	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1656	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1657	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1658	run_tests $ns1 $ns2 10.0.1.1 0 -1 -1 slow
1659	chk_join_nr "remove subflow and signal with port" 2 2 2
1660	chk_add_nr 1 1 1
1661	chk_rm_nr 1 1
1662
1663	# subflows and signal with port, flush
1664	reset
1665	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1666	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1667	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1668	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1669	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1670	run_tests $ns1 $ns2 10.0.1.1 0 -8 -2 slow
1671	chk_join_nr "flush subflows and signal with port" 3 3 3
1672	chk_add_nr 1 1
1673	chk_rm_nr 2 2
1674
1675	# multiple addresses with port
1676	reset
1677	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1678	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1679	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal port 10100
1680	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1681	run_tests $ns1 $ns2 10.0.1.1
1682	chk_join_nr "multiple addresses with port" 2 2 2
1683	chk_add_nr 2 2 2
1684
1685	# multiple addresses with ports
1686	reset
1687	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1688	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal port 10100
1689	ip netns exec $ns1 ./pm_nl_ctl add 10.0.3.1 flags signal port 10101
1690	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1691	run_tests $ns1 $ns2 10.0.1.1
1692	chk_join_nr "multiple addresses with ports" 2 2 2
1693	chk_add_nr 2 2 2
1694}
1695
1696syncookies_tests()
1697{
1698	# single subflow, syncookies
1699	reset_with_cookies
1700	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1701	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1702	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1703	run_tests $ns1 $ns2 10.0.1.1
1704	chk_join_nr "single subflow with syn cookies" 1 1 1
1705
1706	# multiple subflows with syn cookies
1707	reset_with_cookies
1708	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1709	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1710	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1711	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1712	run_tests $ns1 $ns2 10.0.1.1
1713	chk_join_nr "multiple subflows with syn cookies" 2 2 2
1714
1715	# multiple subflows limited by server
1716	reset_with_cookies
1717	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1718	ip netns exec $ns2 ./pm_nl_ctl limits 0 2
1719	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1720	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow
1721	run_tests $ns1 $ns2 10.0.1.1
1722	chk_join_nr "subflows limited by server w cookies" 2 1 1
1723
1724	# test signal address with cookies
1725	reset_with_cookies
1726	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1727	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1728	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1729	run_tests $ns1 $ns2 10.0.1.1
1730	chk_join_nr "signal address with syn cookies" 1 1 1
1731	chk_add_nr 1 1
1732
1733	# test cookie with subflow and signal
1734	reset_with_cookies
1735	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1736	ip netns exec $ns1 ./pm_nl_ctl limits 0 2
1737	ip netns exec $ns2 ./pm_nl_ctl limits 1 2
1738	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1739	run_tests $ns1 $ns2 10.0.1.1
1740	chk_join_nr "subflow and signal w cookies" 2 2 2
1741	chk_add_nr 1 1
1742
1743	# accept and use add_addr with additional subflows
1744	reset_with_cookies
1745	ip netns exec $ns1 ./pm_nl_ctl limits 0 3
1746	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1747	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1748	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1749	ip netns exec $ns2 ./pm_nl_ctl add 10.0.4.2 flags subflow
1750	run_tests $ns1 $ns2 10.0.1.1
1751	chk_join_nr "subflows and signal w. cookies" 3 3 3
1752	chk_add_nr 1 1
1753}
1754
1755checksum_tests()
1756{
1757	# checksum test 0 0
1758	reset_with_checksum 0 0
1759	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1760	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1761	run_tests $ns1 $ns2 10.0.1.1
1762	chk_csum_nr "checksum test 0 0"
1763
1764	# checksum test 1 1
1765	reset_with_checksum 1 1
1766	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1767	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1768	run_tests $ns1 $ns2 10.0.1.1
1769	chk_csum_nr "checksum test 1 1"
1770
1771	# checksum test 0 1
1772	reset_with_checksum 0 1
1773	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1774	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1775	run_tests $ns1 $ns2 10.0.1.1
1776	chk_csum_nr "checksum test 0 1"
1777
1778	# checksum test 1 0
1779	reset_with_checksum 1 0
1780	ip netns exec $ns1 ./pm_nl_ctl limits 0 1
1781	ip netns exec $ns2 ./pm_nl_ctl limits 0 1
1782	run_tests $ns1 $ns2 10.0.1.1
1783	chk_csum_nr "checksum test 1 0"
1784}
1785
1786deny_join_id0_tests()
1787{
1788	# subflow allow join id0 ns1
1789	reset_with_allow_join_id0 1 0
1790	ip netns exec $ns1 ./pm_nl_ctl limits 1 1
1791	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1792	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1793	run_tests $ns1 $ns2 10.0.1.1
1794	chk_join_nr "single subflow allow join id0 ns1" 1 1 1
1795
1796	# subflow allow join id0 ns2
1797	reset_with_allow_join_id0 0 1
1798	ip netns exec $ns1 ./pm_nl_ctl limits 1 1
1799	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1800	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1801	run_tests $ns1 $ns2 10.0.1.1
1802	chk_join_nr "single subflow allow join id0 ns2" 0 0 0
1803
1804	# signal address allow join id0 ns1
1805	# ADD_ADDRs are not affected by allow_join_id0 value.
1806	reset_with_allow_join_id0 1 0
1807	ip netns exec $ns1 ./pm_nl_ctl limits 1 1
1808	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1809	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1810	run_tests $ns1 $ns2 10.0.1.1
1811	chk_join_nr "signal address allow join id0 ns1" 1 1 1
1812	chk_add_nr 1 1
1813
1814	# signal address allow join id0 ns2
1815	# ADD_ADDRs are not affected by allow_join_id0 value.
1816	reset_with_allow_join_id0 0 1
1817	ip netns exec $ns1 ./pm_nl_ctl limits 1 1
1818	ip netns exec $ns2 ./pm_nl_ctl limits 1 1
1819	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1820	run_tests $ns1 $ns2 10.0.1.1
1821	chk_join_nr "signal address allow join id0 ns2" 1 1 1
1822	chk_add_nr 1 1
1823
1824	# subflow and address allow join id0 ns1
1825	reset_with_allow_join_id0 1 0
1826	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1827	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1828	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1829	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1830	run_tests $ns1 $ns2 10.0.1.1
1831	chk_join_nr "subflow and address allow join id0 1" 2 2 2
1832
1833	# subflow and address allow join id0 ns2
1834	reset_with_allow_join_id0 0 1
1835	ip netns exec $ns1 ./pm_nl_ctl limits 2 2
1836	ip netns exec $ns2 ./pm_nl_ctl limits 2 2
1837	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1838	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow
1839	run_tests $ns1 $ns2 10.0.1.1
1840	chk_join_nr "subflow and address allow join id0 2" 1 1 1
1841}
1842
1843fullmesh_tests()
1844{
1845	# fullmesh 1
1846	# 2 fullmesh addrs in ns2, added before the connection,
1847	# 1 non-fullmesh addr in ns1, added during the connection.
1848	reset
1849	ip netns exec $ns1 ./pm_nl_ctl limits 0 4
1850	ip netns exec $ns2 ./pm_nl_ctl limits 1 4
1851	ip netns exec $ns2 ./pm_nl_ctl add 10.0.2.2 flags subflow,fullmesh
1852	ip netns exec $ns2 ./pm_nl_ctl add 10.0.3.2 flags subflow,fullmesh
1853	run_tests $ns1 $ns2 10.0.1.1 0 1 0 slow
1854	chk_join_nr "fullmesh test 2x1" 4 4 4
1855	chk_add_nr 1 1
1856
1857	# fullmesh 2
1858	# 1 non-fullmesh addr in ns1, added before the connection,
1859	# 1 fullmesh addr in ns2, added during the connection.
1860	reset
1861	ip netns exec $ns1 ./pm_nl_ctl limits 1 3
1862	ip netns exec $ns2 ./pm_nl_ctl limits 1 3
1863	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1864	run_tests $ns1 $ns2 10.0.1.1 0 0 fullmesh_1 slow
1865	chk_join_nr "fullmesh test 1x1" 3 3 3
1866	chk_add_nr 1 1
1867
1868	# fullmesh 3
1869	# 1 non-fullmesh addr in ns1, added before the connection,
1870	# 2 fullmesh addrs in ns2, added during the connection.
1871	reset
1872	ip netns exec $ns1 ./pm_nl_ctl limits 2 5
1873	ip netns exec $ns2 ./pm_nl_ctl limits 1 5
1874	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1875	run_tests $ns1 $ns2 10.0.1.1 0 0 fullmesh_2 slow
1876	chk_join_nr "fullmesh test 1x2" 5 5 5
1877	chk_add_nr 1 1
1878
1879	# fullmesh 4
1880	# 1 non-fullmesh addr in ns1, added before the connection,
1881	# 2 fullmesh addrs in ns2, added during the connection,
1882	# limit max_subflows to 4.
1883	reset
1884	ip netns exec $ns1 ./pm_nl_ctl limits 2 4
1885	ip netns exec $ns2 ./pm_nl_ctl limits 1 4
1886	ip netns exec $ns1 ./pm_nl_ctl add 10.0.2.1 flags signal
1887	run_tests $ns1 $ns2 10.0.1.1 0 0 fullmesh_2 slow
1888	chk_join_nr "fullmesh test 1x2, limited" 4 4 4
1889	chk_add_nr 1 1
1890}
1891
1892all_tests()
1893{
1894	subflows_tests
1895	subflows_error_tests
1896	signal_address_tests
1897	link_failure_tests
1898	add_addr_timeout_tests
1899	remove_tests
1900	add_tests
1901	ipv6_tests
1902	v4mapped_tests
1903	backup_tests
1904	add_addr_ports_tests
1905	syncookies_tests
1906	checksum_tests
1907	deny_join_id0_tests
1908	fullmesh_tests
1909}
1910
1911usage()
1912{
1913	echo "mptcp_join usage:"
1914	echo "  -f subflows_tests"
1915	echo "  -e subflows_error_tests"
1916	echo "  -s signal_address_tests"
1917	echo "  -l link_failure_tests"
1918	echo "  -t add_addr_timeout_tests"
1919	echo "  -r remove_tests"
1920	echo "  -a add_tests"
1921	echo "  -6 ipv6_tests"
1922	echo "  -4 v4mapped_tests"
1923	echo "  -b backup_tests"
1924	echo "  -p add_addr_ports_tests"
1925	echo "  -k syncookies_tests"
1926	echo "  -S checksum_tests"
1927	echo "  -d deny_join_id0_tests"
1928	echo "  -m fullmesh_tests"
1929	echo "  -c capture pcap files"
1930	echo "  -C enable data checksum"
1931	echo "  -h help"
1932}
1933
1934sin=$(mktemp)
1935sout=$(mktemp)
1936cin=$(mktemp)
1937cinsent=$(mktemp)
1938cout=$(mktemp)
1939init
1940make_file "$cin" "client" 1
1941make_file "$sin" "server" 1
1942trap cleanup EXIT
1943
1944for arg in "$@"; do
1945	# check for "capture/checksum" args before launching tests
1946	if [[ "${arg}" =~ ^"-"[0-9a-zA-Z]*"c"[0-9a-zA-Z]*$ ]]; then
1947		capture=1
1948	fi
1949	if [[ "${arg}" =~ ^"-"[0-9a-zA-Z]*"C"[0-9a-zA-Z]*$ ]]; then
1950		checksum=1
1951	fi
1952
1953	# exception for the capture/checksum options, the rest means: a part of the tests
1954	if [ "${arg}" != "-c" ] && [ "${arg}" != "-C" ]; then
1955		do_all_tests=0
1956	fi
1957done
1958
1959if [ $do_all_tests -eq 1 ]; then
1960	all_tests
1961	exit $ret
1962fi
1963
1964while getopts 'fesltra64bpkdmchCS' opt; do
1965	case $opt in
1966		f)
1967			subflows_tests
1968			;;
1969		e)
1970			subflows_error_tests
1971			;;
1972		s)
1973			signal_address_tests
1974			;;
1975		l)
1976			link_failure_tests
1977			;;
1978		t)
1979			add_addr_timeout_tests
1980			;;
1981		r)
1982			remove_tests
1983			;;
1984		a)
1985			add_tests
1986			;;
1987		6)
1988			ipv6_tests
1989			;;
1990		4)
1991			v4mapped_tests
1992			;;
1993		b)
1994			backup_tests
1995			;;
1996		p)
1997			add_addr_ports_tests
1998			;;
1999		k)
2000			syncookies_tests
2001			;;
2002		S)
2003			checksum_tests
2004			;;
2005		d)
2006			deny_join_id0_tests
2007			;;
2008		m)
2009			fullmesh_tests
2010			;;
2011		c)
2012			;;
2013		C)
2014			;;
2015		h | *)
2016			usage
2017			;;
2018	esac
2019done
2020
2021exit $ret
2022