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