xref: /linux/tools/testing/selftests/net/mptcp/diag.sh (revision da1d9caf95def6f0320819cf941c9fd1069ba9e1)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
5ns="ns1-$rndh"
6ksft_skip=4
7test_cnt=1
8timeout_poll=100
9timeout_test=$((timeout_poll * 2 + 1))
10ret=0
11
12flush_pids()
13{
14	# mptcp_connect in join mode will sleep a bit before completing,
15	# give it some time
16	sleep 1.1
17
18	ip netns pids "${ns}" | xargs --no-run-if-empty kill -SIGUSR1 &>/dev/null
19}
20
21cleanup()
22{
23	ip netns pids "${ns}" | xargs --no-run-if-empty kill -SIGKILL &>/dev/null
24
25	ip netns del $ns
26}
27
28ip -Version > /dev/null 2>&1
29if [ $? -ne 0 ];then
30	echo "SKIP: Could not run test without ip tool"
31	exit $ksft_skip
32fi
33ss -h | grep -q MPTCP
34if [ $? -ne 0 ];then
35	echo "SKIP: ss tool does not support MPTCP"
36	exit $ksft_skip
37fi
38
39__chk_nr()
40{
41	local condition="$1"
42	local expected=$2
43	local msg nr
44
45	shift 2
46	msg=$*
47	nr=$(ss -inmHMN $ns | $condition)
48
49	printf "%-50s" "$msg"
50	if [ $nr != $expected ]; then
51		echo "[ fail ] expected $expected found $nr"
52		ret=$test_cnt
53	else
54		echo "[  ok  ]"
55	fi
56	test_cnt=$((test_cnt+1))
57}
58
59chk_msk_nr()
60{
61	__chk_nr "grep -c token:" $*
62}
63
64chk_msk_fallback_nr()
65{
66		__chk_nr "grep -c fallback" $*
67}
68
69chk_msk_remote_key_nr()
70{
71		__chk_nr "grep -c remote_key" $*
72}
73
74__chk_listen()
75{
76	local filter="$1"
77	local expected=$2
78
79	shift 2
80	msg=$*
81
82	nr=$(ss -N $ns -Ml "$filter" | grep -c LISTEN)
83	printf "%-50s" "$msg"
84
85	if [ $nr != $expected ]; then
86		echo "[ fail ] expected $expected found $nr"
87		ret=$test_cnt
88	else
89		echo "[  ok  ]"
90	fi
91}
92
93chk_msk_listen()
94{
95	lport=$1
96	local msg="check for listen socket"
97
98	# destination port search should always return empty list
99	__chk_listen "dport $lport" 0 "listen match for dport $lport"
100
101	# should return 'our' mptcp listen socket
102	__chk_listen "sport $lport" 1 "listen match for sport $lport"
103
104	__chk_listen "src inet:0.0.0.0:$lport" 1 "listen match for saddr and sport"
105
106	__chk_listen "" 1 "all listen sockets"
107
108	nr=$(ss -Ml $filter | wc -l)
109}
110
111# $1: ns, $2: port
112wait_local_port_listen()
113{
114	local listener_ns="${1}"
115	local port="${2}"
116
117	local port_hex i
118
119	port_hex="$(printf "%04X" "${port}")"
120	for i in $(seq 10); do
121		ip netns exec "${listener_ns}" cat /proc/net/tcp | \
122			awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
123			break
124		sleep 0.1
125	done
126}
127
128wait_connected()
129{
130	local listener_ns="${1}"
131	local port="${2}"
132
133	local port_hex i
134
135	port_hex="$(printf "%04X" "${port}")"
136	for i in $(seq 10); do
137		ip netns exec ${listener_ns} grep -q " 0100007F:${port_hex} " /proc/net/tcp && break
138		sleep 0.1
139	done
140}
141
142trap cleanup EXIT
143ip netns add $ns
144ip -n $ns link set dev lo up
145
146echo "a" | \
147	timeout ${timeout_test} \
148		ip netns exec $ns \
149			./mptcp_connect -p 10000 -l -t ${timeout_poll} \
150				0.0.0.0 >/dev/null &
151wait_local_port_listen $ns 10000
152chk_msk_nr 0 "no msk on netns creation"
153chk_msk_listen 10000
154
155echo "b" | \
156	timeout ${timeout_test} \
157		ip netns exec $ns \
158			./mptcp_connect -p 10000 -r 0 -t ${timeout_poll} \
159				127.0.0.1 >/dev/null &
160wait_connected $ns 10000
161chk_msk_nr 2 "after MPC handshake "
162chk_msk_remote_key_nr 2 "....chk remote_key"
163chk_msk_fallback_nr 0 "....chk no fallback"
164flush_pids
165
166
167echo "a" | \
168	timeout ${timeout_test} \
169		ip netns exec $ns \
170			./mptcp_connect -p 10001 -l -s TCP -t ${timeout_poll} \
171				0.0.0.0 >/dev/null &
172wait_local_port_listen $ns 10001
173echo "b" | \
174	timeout ${timeout_test} \
175		ip netns exec $ns \
176			./mptcp_connect -p 10001 -r 0 -t ${timeout_poll} \
177				127.0.0.1 >/dev/null &
178wait_connected $ns 10001
179chk_msk_fallback_nr 1 "check fallback"
180flush_pids
181
182NR_CLIENTS=100
183for I in `seq 1 $NR_CLIENTS`; do
184	echo "a" | \
185		timeout ${timeout_test} \
186			ip netns exec $ns \
187				./mptcp_connect -p $((I+10001)) -l -w 10 \
188					-t ${timeout_poll} 0.0.0.0 >/dev/null &
189done
190wait_local_port_listen $ns $((NR_CLIENTS + 10001))
191
192for I in `seq 1 $NR_CLIENTS`; do
193	echo "b" | \
194		timeout ${timeout_test} \
195			ip netns exec $ns \
196				./mptcp_connect -p $((I+10001)) -w 10 \
197					-t ${timeout_poll} 127.0.0.1 >/dev/null &
198done
199sleep 1.5
200
201chk_msk_nr $((NR_CLIENTS*2)) "many msk socket present"
202flush_pids
203
204exit $ret
205