selftests: mptcp: more stable diag tests
[platform/kernel/linux-rpi.git] / tools / testing / selftests / net / mptcp / diag.sh
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
3
4 rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
5 ns="ns1-$rndh"
6 ksft_skip=4
7 test_cnt=1
8 timeout_poll=100
9 timeout_test=$((timeout_poll * 2 + 1))
10 ret=0
11
12 flush_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
21 cleanup()
22 {
23         ip netns pids "${ns}" | xargs --no-run-if-empty kill -SIGKILL &>/dev/null
24
25         ip netns del $ns
26 }
27
28 ip -Version > /dev/null 2>&1
29 if [ $? -ne 0 ];then
30         echo "SKIP: Could not run test without ip tool"
31         exit $ksft_skip
32 fi
33 ss -h | grep -q MPTCP
34 if [ $? -ne 0 ];then
35         echo "SKIP: ss tool does not support MPTCP"
36         exit $ksft_skip
37 fi
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
59 chk_msk_nr()
60 {
61         __chk_nr "grep -c token:" $*
62 }
63
64 wait_msk_nr()
65 {
66         local condition="grep -c token:"
67         local expected=$1
68         local timeout=20
69         local msg nr
70         local max=0
71         local i=0
72
73         shift 1
74         msg=$*
75
76         while [ $i -lt $timeout ]; do
77                 nr=$(ss -inmHMN $ns | $condition)
78                 [ $nr == $expected ] && break;
79                 [ $nr -gt $max ] && max=$nr
80                 i=$((i + 1))
81                 sleep 1
82         done
83
84         printf "%-50s" "$msg"
85         if [ $i -ge $timeout ]; then
86                 echo "[ fail ] timeout while expecting $expected max $max last $nr"
87                 ret=$test_cnt
88         elif [ $nr != $expected ]; then
89                 echo "[ fail ] expected $expected found $nr"
90                 ret=$test_cnt
91         else
92                 echo "[  ok  ]"
93         fi
94         test_cnt=$((test_cnt+1))
95 }
96
97 chk_msk_fallback_nr()
98 {
99                 __chk_nr "grep -c fallback" $*
100 }
101
102 chk_msk_remote_key_nr()
103 {
104                 __chk_nr "grep -c remote_key" $*
105 }
106
107 # $1: ns, $2: port
108 wait_local_port_listen()
109 {
110         local listener_ns="${1}"
111         local port="${2}"
112
113         local port_hex i
114
115         port_hex="$(printf "%04X" "${port}")"
116         for i in $(seq 10); do
117                 ip netns exec "${listener_ns}" cat /proc/net/tcp | \
118                         awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
119                         break
120                 sleep 0.1
121         done
122 }
123
124 wait_connected()
125 {
126         local listener_ns="${1}"
127         local port="${2}"
128
129         local port_hex i
130
131         port_hex="$(printf "%04X" "${port}")"
132         for i in $(seq 10); do
133                 ip netns exec ${listener_ns} grep -q " 0100007F:${port_hex} " /proc/net/tcp && break
134                 sleep 0.1
135         done
136 }
137
138 trap cleanup EXIT
139 ip netns add $ns
140 ip -n $ns link set dev lo up
141
142 echo "a" | \
143         timeout ${timeout_test} \
144                 ip netns exec $ns \
145                         ./mptcp_connect -p 10000 -l -t ${timeout_poll} -w 20 \
146                                 0.0.0.0 >/dev/null &
147 wait_local_port_listen $ns 10000
148 chk_msk_nr 0 "no msk on netns creation"
149
150 echo "b" | \
151         timeout ${timeout_test} \
152                 ip netns exec $ns \
153                         ./mptcp_connect -p 10000 -r 0 -t ${timeout_poll} -w 20 \
154                                 127.0.0.1 >/dev/null &
155 wait_connected $ns 10000
156 chk_msk_nr 2 "after MPC handshake "
157 chk_msk_remote_key_nr 2 "....chk remote_key"
158 chk_msk_fallback_nr 0 "....chk no fallback"
159 flush_pids
160
161
162 echo "a" | \
163         timeout ${timeout_test} \
164                 ip netns exec $ns \
165                         ./mptcp_connect -p 10001 -l -s TCP -t ${timeout_poll} -w 20 \
166                                 0.0.0.0 >/dev/null &
167 wait_local_port_listen $ns 10001
168 echo "b" | \
169         timeout ${timeout_test} \
170                 ip netns exec $ns \
171                         ./mptcp_connect -p 10001 -r 0 -t ${timeout_poll} -w 20 \
172                                 127.0.0.1 >/dev/null &
173 wait_connected $ns 10001
174 chk_msk_fallback_nr 1 "check fallback"
175 flush_pids
176
177 NR_CLIENTS=100
178 for I in `seq 1 $NR_CLIENTS`; do
179         echo "a" | \
180                 timeout ${timeout_test} \
181                         ip netns exec $ns \
182                                 ./mptcp_connect -p $((I+10001)) -l -w 20 \
183                                         -t ${timeout_poll} 0.0.0.0 >/dev/null &
184 done
185 wait_local_port_listen $ns $((NR_CLIENTS + 10001))
186
187 for I in `seq 1 $NR_CLIENTS`; do
188         echo "b" | \
189                 timeout ${timeout_test} \
190                         ip netns exec $ns \
191                                 ./mptcp_connect -p $((I+10001)) -w 20 \
192                                         -t ${timeout_poll} 127.0.0.1 >/dev/null &
193 done
194
195 wait_msk_nr $((NR_CLIENTS*2)) "many msk socket present"
196 flush_pids
197
198 exit $ret