Merge 5.17-rc6 into char-misc-next
[linux-2.6-microblaze.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 chk_msk_fallback_nr()
65 {
66                 __chk_nr "grep -c fallback" $*
67 }
68
69 chk_msk_remote_key_nr()
70 {
71                 __chk_nr "grep -c remote_key" $*
72 }
73
74 # $1: ns, $2: port
75 wait_local_port_listen()
76 {
77         local listener_ns="${1}"
78         local port="${2}"
79
80         local port_hex i
81
82         port_hex="$(printf "%04X" "${port}")"
83         for i in $(seq 10); do
84                 ip netns exec "${listener_ns}" cat /proc/net/tcp | \
85                         awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
86                         break
87                 sleep 0.1
88         done
89 }
90
91 wait_connected()
92 {
93         local listener_ns="${1}"
94         local port="${2}"
95
96         local port_hex i
97
98         port_hex="$(printf "%04X" "${port}")"
99         for i in $(seq 10); do
100                 ip netns exec ${listener_ns} grep -q " 0100007F:${port_hex} " /proc/net/tcp && break
101                 sleep 0.1
102         done
103 }
104
105 trap cleanup EXIT
106 ip netns add $ns
107 ip -n $ns link set dev lo up
108
109 echo "a" | \
110         timeout ${timeout_test} \
111                 ip netns exec $ns \
112                         ./mptcp_connect -p 10000 -l -t ${timeout_poll} \
113                                 0.0.0.0 >/dev/null &
114 wait_local_port_listen $ns 10000
115 chk_msk_nr 0 "no msk on netns creation"
116
117 echo "b" | \
118         timeout ${timeout_test} \
119                 ip netns exec $ns \
120                         ./mptcp_connect -p 10000 -r 0 -t ${timeout_poll} \
121                                 127.0.0.1 >/dev/null &
122 wait_connected $ns 10000
123 chk_msk_nr 2 "after MPC handshake "
124 chk_msk_remote_key_nr 2 "....chk remote_key"
125 chk_msk_fallback_nr 0 "....chk no fallback"
126 flush_pids
127
128
129 echo "a" | \
130         timeout ${timeout_test} \
131                 ip netns exec $ns \
132                         ./mptcp_connect -p 10001 -l -s TCP -t ${timeout_poll} \
133                                 0.0.0.0 >/dev/null &
134 wait_local_port_listen $ns 10001
135 echo "b" | \
136         timeout ${timeout_test} \
137                 ip netns exec $ns \
138                         ./mptcp_connect -p 10001 -r 0 -t ${timeout_poll} \
139                                 127.0.0.1 >/dev/null &
140 wait_connected $ns 10001
141 chk_msk_fallback_nr 1 "check fallback"
142 flush_pids
143
144 NR_CLIENTS=100
145 for I in `seq 1 $NR_CLIENTS`; do
146         echo "a" | \
147                 timeout ${timeout_test} \
148                         ip netns exec $ns \
149                                 ./mptcp_connect -p $((I+10001)) -l -w 10 \
150                                         -t ${timeout_poll} 0.0.0.0 >/dev/null &
151 done
152 wait_local_port_listen $ns $((NR_CLIENTS + 10001))
153
154 for I in `seq 1 $NR_CLIENTS`; do
155         echo "b" | \
156                 timeout ${timeout_test} \
157                         ip netns exec $ns \
158                                 ./mptcp_connect -p $((I+10001)) -w 10 \
159                                         -t ${timeout_poll} 127.0.0.1 >/dev/null &
160 done
161 sleep 1.5
162
163 chk_msk_nr $((NR_CLIENTS*2)) "many msk socket present"
164 flush_pids
165
166 exit $ret