Merge tag 'xfs-5.13-merge-5' of git://git.kernel.org/pub/scm/fs/xfs/xfs-linux
[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 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
75 trap cleanup EXIT
76 ip netns add $ns
77 ip -n $ns link set dev lo up
78
79 echo "a" | \
80         timeout ${timeout_test} \
81                 ip netns exec $ns \
82                         ./mptcp_connect -p 10000 -l -t ${timeout_poll} \
83                                 0.0.0.0 >/dev/null &
84 sleep 0.1
85 chk_msk_nr 0 "no msk on netns creation"
86
87 echo "b" | \
88         timeout ${timeout_test} \
89                 ip netns exec $ns \
90                         ./mptcp_connect -p 10000 -j -t ${timeout_poll} \
91                                 127.0.0.1 >/dev/null &
92 sleep 0.1
93 chk_msk_nr 2 "after MPC handshake "
94 chk_msk_remote_key_nr 2 "....chk remote_key"
95 chk_msk_fallback_nr 0 "....chk no fallback"
96 flush_pids
97
98
99 echo "a" | \
100         timeout ${timeout_test} \
101                 ip netns exec $ns \
102                         ./mptcp_connect -p 10001 -l -s TCP -t ${timeout_poll} \
103                                 0.0.0.0 >/dev/null &
104 sleep 0.1
105 echo "b" | \
106         timeout ${timeout_test} \
107                 ip netns exec $ns \
108                         ./mptcp_connect -p 10001 -j -t ${timeout_poll} \
109                                 127.0.0.1 >/dev/null &
110 sleep 0.1
111 chk_msk_fallback_nr 1 "check fallback"
112 flush_pids
113
114 NR_CLIENTS=100
115 for I in `seq 1 $NR_CLIENTS`; do
116         echo "a" | \
117                 timeout ${timeout_test} \
118                         ip netns exec $ns \
119                                 ./mptcp_connect -p $((I+10001)) -l -w 10 \
120                                         -t ${timeout_poll} 0.0.0.0 >/dev/null &
121 done
122 sleep 0.1
123
124 for I in `seq 1 $NR_CLIENTS`; do
125         echo "b" | \
126                 timeout ${timeout_test} \
127                         ip netns exec $ns \
128                                 ./mptcp_connect -p $((I+10001)) -w 10 \
129                                         -t ${timeout_poll} 127.0.0.1 >/dev/null &
130 done
131 sleep 1.5
132
133 chk_msk_nr $((NR_CLIENTS*2)) "many msk socket present"
134 flush_pids
135
136 exit $ret