xref: /linux/tools/testing/selftests/net/mptcp/diag.sh (revision e7d759f31ca295d589f7420719c311870bb3166f)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4. "$(dirname "${0}")/mptcp_lib.sh"
5
6sec=$(date +%s)
7rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
8ns="ns1-$rndh"
9ksft_skip=4
10test_cnt=1
11timeout_poll=100
12timeout_test=$((timeout_poll * 2 + 1))
13ret=0
14
15flush_pids()
16{
17	# mptcp_connect in join mode will sleep a bit before completing,
18	# give it some time
19	sleep 1.1
20
21	ip netns pids "${ns}" | xargs --no-run-if-empty kill -SIGUSR1 &>/dev/null
22
23	for _ in $(seq 10); do
24		[ -z "$(ip netns pids "${ns}")" ] && break
25		sleep 0.1
26	done
27}
28
29cleanup()
30{
31	ip netns pids "${ns}" | xargs --no-run-if-empty kill -SIGKILL &>/dev/null
32
33	ip netns del $ns
34}
35
36mptcp_lib_check_mptcp
37
38ip -Version > /dev/null 2>&1
39if [ $? -ne 0 ];then
40	echo "SKIP: Could not run test without ip tool"
41	exit $ksft_skip
42fi
43ss -h | grep -q MPTCP
44if [ $? -ne 0 ];then
45	echo "SKIP: ss tool does not support MPTCP"
46	exit $ksft_skip
47fi
48
49get_msk_inuse()
50{
51	ip netns exec $ns cat /proc/net/protocols | awk '$1~/^MPTCP$/{print $3}'
52}
53
54__chk_nr()
55{
56	local command="$1"
57	local expected=$2
58	local msg="$3"
59	local skip="${4-SKIP}"
60	local nr
61
62	nr=$(eval $command)
63
64	printf "%-50s" "$msg"
65	if [ $nr != $expected ]; then
66		if [ $nr = "$skip" ] && ! mptcp_lib_expect_all_features; then
67			echo "[ skip ] Feature probably not supported"
68			mptcp_lib_result_skip "${msg}"
69		else
70			echo "[ fail ] expected $expected found $nr"
71			mptcp_lib_result_fail "${msg}"
72			ret=$test_cnt
73		fi
74	else
75		echo "[  ok  ]"
76		mptcp_lib_result_pass "${msg}"
77	fi
78	test_cnt=$((test_cnt+1))
79}
80
81__chk_msk_nr()
82{
83	local condition=$1
84	shift 1
85
86	__chk_nr "ss -inmHMN $ns | $condition" "$@"
87}
88
89chk_msk_nr()
90{
91	__chk_msk_nr "grep -c token:" "$@"
92}
93
94wait_msk_nr()
95{
96	local condition="grep -c token:"
97	local expected=$1
98	local timeout=20
99	local msg nr
100	local max=0
101	local i=0
102
103	shift 1
104	msg=$*
105
106	while [ $i -lt $timeout ]; do
107		nr=$(ss -inmHMN $ns | $condition)
108		[ $nr == $expected ] && break;
109		[ $nr -gt $max ] && max=$nr
110		i=$((i + 1))
111		sleep 1
112	done
113
114	printf "%-50s" "$msg"
115	if [ $i -ge $timeout ]; then
116		echo "[ fail ] timeout while expecting $expected max $max last $nr"
117		mptcp_lib_result_fail "${msg} # timeout"
118		ret=$test_cnt
119	elif [ $nr != $expected ]; then
120		echo "[ fail ] expected $expected found $nr"
121		mptcp_lib_result_fail "${msg} # unexpected result"
122		ret=$test_cnt
123	else
124		echo "[  ok  ]"
125		mptcp_lib_result_pass "${msg}"
126	fi
127	test_cnt=$((test_cnt+1))
128}
129
130chk_msk_fallback_nr()
131{
132	__chk_msk_nr "grep -c fallback" "$@"
133}
134
135chk_msk_remote_key_nr()
136{
137	__chk_msk_nr "grep -c remote_key" "$@"
138}
139
140__chk_listen()
141{
142	local filter="$1"
143	local expected=$2
144	local msg="$3"
145
146	__chk_nr "ss -N $ns -Ml '$filter' | grep -c LISTEN" "$expected" "$msg" 0
147}
148
149chk_msk_listen()
150{
151	lport=$1
152
153	# destination port search should always return empty list
154	__chk_listen "dport $lport" 0 "listen match for dport $lport"
155
156	# should return 'our' mptcp listen socket
157	__chk_listen "sport $lport" 1 "listen match for sport $lport"
158
159	__chk_listen "src inet:0.0.0.0:$lport" 1 "listen match for saddr and sport"
160
161	__chk_listen "" 1 "all listen sockets"
162
163	nr=$(ss -Ml $filter | wc -l)
164}
165
166chk_msk_inuse()
167{
168	local expected=$1
169	local msg="$2"
170	local listen_nr
171
172	listen_nr=$(ss -N "${ns}" -Ml | grep -c LISTEN)
173	expected=$((expected + listen_nr))
174
175	for _ in $(seq 10); do
176		if [ $(get_msk_inuse) -eq $expected ];then
177			break
178		fi
179		sleep 0.1
180	done
181
182	__chk_nr get_msk_inuse $expected "$msg" 0
183}
184
185# $1: cestab nr
186chk_msk_cestab()
187{
188	local cestab=$1
189
190	__chk_nr "mptcp_lib_get_counter ${ns} MPTcpExtMPCurrEstab" \
191		 "${cestab}" "....chk ${cestab} cestab" ""
192}
193
194wait_connected()
195{
196	local listener_ns="${1}"
197	local port="${2}"
198
199	local port_hex i
200
201	port_hex="$(printf "%04X" "${port}")"
202	for i in $(seq 10); do
203		ip netns exec ${listener_ns} grep -q " 0100007F:${port_hex} " /proc/net/tcp && break
204		sleep 0.1
205	done
206}
207
208trap cleanup EXIT
209ip netns add $ns
210ip -n $ns link set dev lo up
211
212echo "a" | \
213	timeout ${timeout_test} \
214		ip netns exec $ns \
215			./mptcp_connect -p 10000 -l -t ${timeout_poll} -w 20 \
216				0.0.0.0 >/dev/null &
217mptcp_lib_wait_local_port_listen $ns 10000
218chk_msk_nr 0 "no msk on netns creation"
219chk_msk_listen 10000
220
221echo "b" | \
222	timeout ${timeout_test} \
223		ip netns exec $ns \
224			./mptcp_connect -p 10000 -r 0 -t ${timeout_poll} -w 20 \
225				127.0.0.1 >/dev/null &
226wait_connected $ns 10000
227chk_msk_nr 2 "after MPC handshake "
228chk_msk_remote_key_nr 2 "....chk remote_key"
229chk_msk_fallback_nr 0 "....chk no fallback"
230chk_msk_inuse 2 "....chk 2 msk in use"
231chk_msk_cestab 2
232flush_pids
233
234chk_msk_inuse 0 "....chk 0 msk in use after flush"
235chk_msk_cestab 0
236
237echo "a" | \
238	timeout ${timeout_test} \
239		ip netns exec $ns \
240			./mptcp_connect -p 10001 -l -s TCP -t ${timeout_poll} -w 20 \
241				0.0.0.0 >/dev/null &
242mptcp_lib_wait_local_port_listen $ns 10001
243echo "b" | \
244	timeout ${timeout_test} \
245		ip netns exec $ns \
246			./mptcp_connect -p 10001 -r 0 -t ${timeout_poll} -w 20 \
247				127.0.0.1 >/dev/null &
248wait_connected $ns 10001
249chk_msk_fallback_nr 1 "check fallback"
250chk_msk_inuse 1 "....chk 1 msk in use"
251chk_msk_cestab 1
252flush_pids
253
254chk_msk_inuse 0 "....chk 0 msk in use after flush"
255chk_msk_cestab 0
256
257NR_CLIENTS=100
258for I in `seq 1 $NR_CLIENTS`; do
259	echo "a" | \
260		timeout ${timeout_test} \
261			ip netns exec $ns \
262				./mptcp_connect -p $((I+10001)) -l -w 20 \
263					-t ${timeout_poll} 0.0.0.0 >/dev/null &
264done
265mptcp_lib_wait_local_port_listen $ns $((NR_CLIENTS + 10001))
266
267for I in `seq 1 $NR_CLIENTS`; do
268	echo "b" | \
269		timeout ${timeout_test} \
270			ip netns exec $ns \
271				./mptcp_connect -p $((I+10001)) -w 20 \
272					-t ${timeout_poll} 127.0.0.1 >/dev/null &
273done
274
275wait_msk_nr $((NR_CLIENTS*2)) "many msk socket present"
276chk_msk_inuse $((NR_CLIENTS*2)) "....chk many msk in use"
277chk_msk_cestab $((NR_CLIENTS*2))
278flush_pids
279
280chk_msk_inuse 0 "....chk 0 msk in use after flush"
281chk_msk_cestab 0
282
283mptcp_lib_result_print_all_tap
284exit $ret
285