0001
0002
0003
0004 rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
0005 ns="ns1-$rndh"
0006 ksft_skip=4
0007 test_cnt=1
0008 timeout_poll=100
0009 timeout_test=$((timeout_poll * 2 + 1))
0010 ret=0
0011
0012 flush_pids()
0013 {
0014
0015
0016 sleep 1.1
0017
0018 ip netns pids "${ns}" | xargs --no-run-if-empty kill -SIGUSR1 &>/dev/null
0019 }
0020
0021 cleanup()
0022 {
0023 ip netns pids "${ns}" | xargs --no-run-if-empty kill -SIGKILL &>/dev/null
0024
0025 ip netns del $ns
0026 }
0027
0028 ip -Version > /dev/null 2>&1
0029 if [ $? -ne 0 ];then
0030 echo "SKIP: Could not run test without ip tool"
0031 exit $ksft_skip
0032 fi
0033 ss -h | grep -q MPTCP
0034 if [ $? -ne 0 ];then
0035 echo "SKIP: ss tool does not support MPTCP"
0036 exit $ksft_skip
0037 fi
0038
0039 __chk_nr()
0040 {
0041 local condition="$1"
0042 local expected=$2
0043 local msg nr
0044
0045 shift 2
0046 msg=$*
0047 nr=$(ss -inmHMN $ns | $condition)
0048
0049 printf "%-50s" "$msg"
0050 if [ $nr != $expected ]; then
0051 echo "[ fail ] expected $expected found $nr"
0052 ret=$test_cnt
0053 else
0054 echo "[ ok ]"
0055 fi
0056 test_cnt=$((test_cnt+1))
0057 }
0058
0059 chk_msk_nr()
0060 {
0061 __chk_nr "grep -c token:" $*
0062 }
0063
0064 wait_msk_nr()
0065 {
0066 local condition="grep -c token:"
0067 local expected=$1
0068 local timeout=20
0069 local msg nr
0070 local max=0
0071 local i=0
0072
0073 shift 1
0074 msg=$*
0075
0076 while [ $i -lt $timeout ]; do
0077 nr=$(ss -inmHMN $ns | $condition)
0078 [ $nr == $expected ] && break;
0079 [ $nr -gt $max ] && max=$nr
0080 i=$((i + 1))
0081 sleep 1
0082 done
0083
0084 printf "%-50s" "$msg"
0085 if [ $i -ge $timeout ]; then
0086 echo "[ fail ] timeout while expecting $expected max $max last $nr"
0087 ret=$test_cnt
0088 elif [ $nr != $expected ]; then
0089 echo "[ fail ] expected $expected found $nr"
0090 ret=$test_cnt
0091 else
0092 echo "[ ok ]"
0093 fi
0094 test_cnt=$((test_cnt+1))
0095 }
0096
0097 chk_msk_fallback_nr()
0098 {
0099 __chk_nr "grep -c fallback" $*
0100 }
0101
0102 chk_msk_remote_key_nr()
0103 {
0104 __chk_nr "grep -c remote_key" $*
0105 }
0106
0107 __chk_listen()
0108 {
0109 local filter="$1"
0110 local expected=$2
0111
0112 shift 2
0113 msg=$*
0114
0115 nr=$(ss -N $ns -Ml "$filter" | grep -c LISTEN)
0116 printf "%-50s" "$msg"
0117
0118 if [ $nr != $expected ]; then
0119 echo "[ fail ] expected $expected found $nr"
0120 ret=$test_cnt
0121 else
0122 echo "[ ok ]"
0123 fi
0124 }
0125
0126 chk_msk_listen()
0127 {
0128 lport=$1
0129 local msg="check for listen socket"
0130
0131
0132 __chk_listen "dport $lport" 0 "listen match for dport $lport"
0133
0134
0135 __chk_listen "sport $lport" 1 "listen match for sport $lport"
0136
0137 __chk_listen "src inet:0.0.0.0:$lport" 1 "listen match for saddr and sport"
0138
0139 __chk_listen "" 1 "all listen sockets"
0140
0141 nr=$(ss -Ml $filter | wc -l)
0142 }
0143
0144
0145 wait_local_port_listen()
0146 {
0147 local listener_ns="${1}"
0148 local port="${2}"
0149
0150 local port_hex i
0151
0152 port_hex="$(printf "%04X" "${port}")"
0153 for i in $(seq 10); do
0154 ip netns exec "${listener_ns}" cat /proc/net/tcp | \
0155 awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
0156 break
0157 sleep 0.1
0158 done
0159 }
0160
0161 wait_connected()
0162 {
0163 local listener_ns="${1}"
0164 local port="${2}"
0165
0166 local port_hex i
0167
0168 port_hex="$(printf "%04X" "${port}")"
0169 for i in $(seq 10); do
0170 ip netns exec ${listener_ns} grep -q " 0100007F:${port_hex} " /proc/net/tcp && break
0171 sleep 0.1
0172 done
0173 }
0174
0175 trap cleanup EXIT
0176 ip netns add $ns
0177 ip -n $ns link set dev lo up
0178
0179 echo "a" | \
0180 timeout ${timeout_test} \
0181 ip netns exec $ns \
0182 ./mptcp_connect -p 10000 -l -t ${timeout_poll} -w 20 \
0183 0.0.0.0 >/dev/null &
0184 wait_local_port_listen $ns 10000
0185 chk_msk_nr 0 "no msk on netns creation"
0186 chk_msk_listen 10000
0187
0188 echo "b" | \
0189 timeout ${timeout_test} \
0190 ip netns exec $ns \
0191 ./mptcp_connect -p 10000 -r 0 -t ${timeout_poll} -w 20 \
0192 127.0.0.1 >/dev/null &
0193 wait_connected $ns 10000
0194 chk_msk_nr 2 "after MPC handshake "
0195 chk_msk_remote_key_nr 2 "....chk remote_key"
0196 chk_msk_fallback_nr 0 "....chk no fallback"
0197 flush_pids
0198
0199
0200 echo "a" | \
0201 timeout ${timeout_test} \
0202 ip netns exec $ns \
0203 ./mptcp_connect -p 10001 -l -s TCP -t ${timeout_poll} -w 20 \
0204 0.0.0.0 >/dev/null &
0205 wait_local_port_listen $ns 10001
0206 echo "b" | \
0207 timeout ${timeout_test} \
0208 ip netns exec $ns \
0209 ./mptcp_connect -p 10001 -r 0 -t ${timeout_poll} -w 20 \
0210 127.0.0.1 >/dev/null &
0211 wait_connected $ns 10001
0212 chk_msk_fallback_nr 1 "check fallback"
0213 flush_pids
0214
0215 NR_CLIENTS=100
0216 for I in `seq 1 $NR_CLIENTS`; do
0217 echo "a" | \
0218 timeout ${timeout_test} \
0219 ip netns exec $ns \
0220 ./mptcp_connect -p $((I+10001)) -l -w 20 \
0221 -t ${timeout_poll} 0.0.0.0 >/dev/null &
0222 done
0223 wait_local_port_listen $ns $((NR_CLIENTS + 10001))
0224
0225 for I in `seq 1 $NR_CLIENTS`; do
0226 echo "b" | \
0227 timeout ${timeout_test} \
0228 ip netns exec $ns \
0229 ./mptcp_connect -p $((I+10001)) -w 20 \
0230 -t ${timeout_poll} 127.0.0.1 >/dev/null &
0231 done
0232
0233 wait_msk_nr $((NR_CLIENTS*2)) "many msk socket present"
0234 flush_pids
0235
0236 exit $ret