Merge branch 'dmi-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jdelvar...
[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 ret=0
9 pids=()
10
11 flush_pids()
12 {
13         # mptcp_connect in join mode will sleep a bit before completing,
14         # give it some time
15         sleep 1.1
16
17         for pid in ${pids[@]}; do
18                 [ -d /proc/$pid ] && kill -SIGUSR1 $pid >/dev/null 2>&1
19         done
20         pids=()
21 }
22
23 cleanup()
24 {
25         ip netns del $ns
26         for pid in ${pids[@]}; do
27                 [ -d /proc/$pid ] && kill -9 $pid >/dev/null 2>&1
28         done
29 }
30
31 ip -Version > /dev/null 2>&1
32 if [ $? -ne 0 ];then
33         echo "SKIP: Could not run test without ip tool"
34         exit $ksft_skip
35 fi
36 ss -h | grep -q MPTCP
37 if [ $? -ne 0 ];then
38         echo "SKIP: ss tool does not support MPTCP"
39         exit $ksft_skip
40 fi
41
42 __chk_nr()
43 {
44         local condition="$1"
45         local expected=$2
46         local msg nr
47
48         shift 2
49         msg=$*
50         nr=$(ss -inmHMN $ns | $condition)
51
52         printf "%-50s" "$msg"
53         if [ $nr != $expected ]; then
54                 echo "[ fail ] expected $expected found $nr"
55                 ret=$test_cnt
56         else
57                 echo "[  ok  ]"
58         fi
59         test_cnt=$((test_cnt+1))
60 }
61
62 chk_msk_nr()
63 {
64         __chk_nr "grep -c token:" $*
65 }
66
67 chk_msk_fallback_nr()
68 {
69                 __chk_nr "grep -c fallback" $*
70 }
71
72 chk_msk_remote_key_nr()
73 {
74                 __chk_nr "grep -c remote_key" $*
75 }
76
77
78 trap cleanup EXIT
79 ip netns add $ns
80 ip -n $ns link set dev lo up
81
82 echo "a" | ip netns exec $ns ./mptcp_connect -p 10000 -l 0.0.0.0 -t 100 >/dev/null &
83 sleep 0.1
84 pids[0]=$!
85 chk_msk_nr 0 "no msk on netns creation"
86
87 echo "b" | ip netns exec $ns ./mptcp_connect -p 10000 127.0.0.1 -j -t 100 >/dev/null &
88 sleep 0.1
89 pids[1]=$!
90 chk_msk_nr 2 "after MPC handshake "
91 chk_msk_remote_key_nr 2 "....chk remote_key"
92 chk_msk_fallback_nr 0 "....chk no fallback"
93 flush_pids
94
95
96 echo "a" | ip netns exec $ns ./mptcp_connect -p 10001 -s TCP -l 0.0.0.0 -t 100 >/dev/null &
97 pids[0]=$!
98 sleep 0.1
99 echo "b" | ip netns exec $ns ./mptcp_connect -p 10001 127.0.0.1 -j -t 100 >/dev/null &
100 pids[1]=$!
101 sleep 0.1
102 chk_msk_fallback_nr 1 "check fallback"
103 flush_pids
104
105 NR_CLIENTS=100
106 for I in `seq 1 $NR_CLIENTS`; do
107         echo "a" | ip netns exec $ns ./mptcp_connect -p $((I+10001)) -l 0.0.0.0 -t 100 -w 10 >/dev/null  &
108         pids[$((I*2))]=$!
109 done
110 sleep 0.1
111
112 for I in `seq 1 $NR_CLIENTS`; do
113         echo "b" | ip netns exec $ns ./mptcp_connect -p $((I+10001)) 127.0.0.1 -t 100 -w 10 >/dev/null &
114         pids[$((I*2 + 1))]=$!
115 done
116 sleep 1.5
117
118 chk_msk_nr $((NR_CLIENTS*2)) "many msk socket present"
119 flush_pids
120
121 exit $ret