1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3 4# Double quotes to prevent globbing and word splitting is recommended in new 5# code but we accept it, especially because there were too many before having 6# address all other issues detected by shellcheck. 7#shellcheck disable=SC2086 8 9. "$(dirname "${0}")/mptcp_lib.sh" 10 11ns="" 12timeout_poll=30 13timeout_test=$((timeout_poll * 2 + 1)) 14ret=0 15 16flush_pids() 17{ 18 # mptcp_connect in join mode will sleep a bit before completing, 19 # give it some time 20 sleep 1.1 21 22 ip netns pids "${ns}" | xargs --no-run-if-empty kill -SIGUSR1 &>/dev/null 23 24 for _ in $(seq $((timeout_poll * 10))); do 25 [ -z "$(ip netns pids "${ns}")" ] && break 26 sleep 0.1 27 done 28} 29 30# This function is used in the cleanup trap 31#shellcheck disable=SC2317 32cleanup() 33{ 34 ip netns pids "${ns}" | xargs --no-run-if-empty kill -SIGKILL &>/dev/null 35 36 mptcp_lib_ns_exit "${ns}" 37} 38 39mptcp_lib_check_mptcp 40mptcp_lib_check_tools ip ss 41 42get_msk_inuse() 43{ 44 ip netns exec $ns cat /proc/net/protocols | awk '$1~/^MPTCP$/{print $3}' 45} 46 47__chk_nr() 48{ 49 local command="$1" 50 local expected=$2 51 local msg="$3" 52 local skip="${4-SKIP}" 53 local nr 54 55 nr=$(eval $command) 56 57 mptcp_lib_print_title "$msg" 58 if [ "$nr" != "$expected" ]; then 59 if [ "$nr" = "$skip" ] && ! mptcp_lib_expect_all_features; then 60 mptcp_lib_pr_skip "Feature probably not supported" 61 mptcp_lib_result_skip "${msg}" 62 else 63 mptcp_lib_pr_fail "expected $expected found $nr" 64 mptcp_lib_result_fail "${msg}" 65 ret=${KSFT_FAIL} 66 fi 67 else 68 mptcp_lib_pr_ok 69 mptcp_lib_result_pass "${msg}" 70 fi 71} 72 73__chk_msk_nr() 74{ 75 local condition=$1 76 shift 1 77 78 __chk_nr "ss -inmHMN $ns | $condition" "$@" 79} 80 81chk_msk_nr() 82{ 83 __chk_msk_nr "grep -c token:" "$@" 84} 85 86chk_listener_nr() 87{ 88 local expected=$1 89 local msg="$2" 90 91 __chk_nr "ss -nlHMON $ns | wc -l" "$expected" "$msg - mptcp" 0 92 __chk_nr "ss -nlHtON $ns | wc -l" "$expected" "$msg - subflows" 93} 94 95wait_msk_nr() 96{ 97 local condition="grep -c token:" 98 local expected=$1 99 local timeout=20 100 local msg nr 101 local max=0 102 local i=0 103 104 shift 1 105 msg=$* 106 107 while [ $i -lt $timeout ]; do 108 nr=$(ss -inmHMN $ns | $condition) 109 [ $nr == $expected ] && break; 110 [ $nr -gt $max ] && max=$nr 111 i=$((i + 1)) 112 sleep 1 113 done 114 115 mptcp_lib_print_title "$msg" 116 if [ $i -ge $timeout ]; then 117 mptcp_lib_pr_fail "timeout while expecting $expected max $max last $nr" 118 mptcp_lib_result_fail "${msg} # timeout" 119 ret=${KSFT_FAIL} 120 elif [ $nr != $expected ]; then 121 mptcp_lib_pr_fail "expected $expected found $nr" 122 mptcp_lib_result_fail "${msg} # unexpected result" 123 ret=${KSFT_FAIL} 124 else 125 mptcp_lib_pr_ok 126 mptcp_lib_result_pass "${msg}" 127 fi 128} 129 130chk_msk_fallback_nr() 131{ 132 __chk_msk_nr "grep -c fallback" "$@" 133} 134 135chk_msk_remote_key_nr() 136{ 137 __chk_msk_nr "grep -c remote_key" "$@" 138} 139 140__chk_listen() 141{ 142 local filter="$1" 143 local expected=$2 144 local msg="$3" 145 146 __chk_nr "ss -N $ns -Ml '$filter' | grep -c LISTEN" "$expected" "$msg" 0 147} 148 149chk_msk_listen() 150{ 151 lport=$1 152 153 # destination port search should always return empty list 154 __chk_listen "dport $lport" 0 "listen match for dport $lport" 155 156 # should return 'our' mptcp listen socket 157 __chk_listen "sport $lport" 1 "listen match for sport $lport" 158 159 __chk_listen "src inet:0.0.0.0:$lport" 1 "listen match for saddr and sport" 160 161 __chk_listen "" 1 "all listen sockets" 162 163 nr=$(ss -Ml $filter | wc -l) 164} 165 166chk_msk_inuse() 167{ 168 local expected=$1 169 local msg="....chk ${2:-${expected}} msk in use" 170 local listen_nr 171 172 if [ "${expected}" -eq 0 ]; then 173 msg+=" after flush" 174 fi 175 176 listen_nr=$(ss -N "${ns}" -Ml | grep -c LISTEN) 177 expected=$((expected + listen_nr)) 178 179 for _ in $(seq 10); do 180 if [ "$(get_msk_inuse)" -eq $expected ]; then 181 break 182 fi 183 sleep 0.1 184 done 185 186 __chk_nr get_msk_inuse $expected "${msg}" 0 187} 188 189# $1: cestab nr 190chk_msk_cestab() 191{ 192 local expected=$1 193 local msg="....chk ${2:-${expected}} cestab" 194 195 if [ "${expected}" -eq 0 ]; then 196 msg+=" after flush" 197 fi 198 199 __chk_nr "mptcp_lib_get_counter ${ns} MPTcpExtMPCurrEstab" \ 200 "${expected}" "${msg}" "" 201} 202 203msk_info_get_value() 204{ 205 local port="${1}" 206 local info="${2}" 207 208 ss -N "${ns}" -inHM dport "${port}" | \ 209 mptcp_lib_get_info_value "${info}" "${info}" 210} 211 212chk_msk_info() 213{ 214 local port="${1}" 215 local info="${2}" 216 local cnt="${3}" 217 local msg="....chk ${info}" 218 local delta_ms=250 # half what we waited before, just to be sure 219 local now 220 221 now=$(msk_info_get_value "${port}" "${info}") 222 223 mptcp_lib_print_title "${msg}" 224 if { [ -z "${cnt}" ] || [ -z "${now}" ]; } && 225 ! mptcp_lib_expect_all_features; then 226 mptcp_lib_pr_skip "Feature probably not supported" 227 mptcp_lib_result_skip "${msg}" 228 elif [ "$((cnt + delta_ms))" -lt "${now}" ]; then 229 mptcp_lib_pr_ok 230 mptcp_lib_result_pass "${msg}" 231 else 232 mptcp_lib_pr_fail "value of ${info} changed by $((now - cnt))ms," \ 233 "expected at least ${delta_ms}ms" 234 mptcp_lib_result_fail "${msg}" 235 ret=${KSFT_FAIL} 236 fi 237} 238 239chk_last_time_info() 240{ 241 local port="${1}" 242 local data_sent data_recv ack_recv 243 244 data_sent=$(msk_info_get_value "${port}" "last_data_sent") 245 data_recv=$(msk_info_get_value "${port}" "last_data_recv") 246 ack_recv=$(msk_info_get_value "${port}" "last_ack_recv") 247 248 sleep 0.5 # wait to check after if the timestamps difference 249 250 chk_msk_info "${port}" "last_data_sent" "${data_sent}" 251 chk_msk_info "${port}" "last_data_recv" "${data_recv}" 252 chk_msk_info "${port}" "last_ack_recv" "${ack_recv}" 253} 254 255wait_connected() 256{ 257 local listener_ns="${1}" 258 local port="${2}" 259 260 local port_hex i 261 262 port_hex="$(printf "%04X" "${port}")" 263 for i in $(seq 10); do 264 ip netns exec ${listener_ns} grep -q " 0100007F:${port_hex} " /proc/net/tcp && break 265 sleep 0.1 266 done 267} 268 269trap cleanup EXIT 270mptcp_lib_ns_init ns 271 272echo "a" | \ 273 timeout ${timeout_test} \ 274 ip netns exec $ns \ 275 ./mptcp_connect -p 10000 -l -t ${timeout_poll} -w 20 \ 276 0.0.0.0 >/dev/null & 277mptcp_lib_wait_local_port_listen $ns 10000 278chk_msk_nr 0 "no msk on netns creation" 279chk_msk_listen 10000 280 281echo "b" | \ 282 timeout ${timeout_test} \ 283 ip netns exec $ns \ 284 ./mptcp_connect -p 10000 -r 0 -t ${timeout_poll} -w 20 \ 285 127.0.0.1 >/dev/null & 286wait_connected $ns 10000 287chk_msk_nr 2 "after MPC handshake " 288chk_last_time_info 10000 289chk_msk_remote_key_nr 2 "....chk remote_key" 290chk_msk_fallback_nr 0 "....chk no fallback" 291chk_msk_inuse 2 292chk_msk_cestab 2 293flush_pids 294 295chk_msk_inuse 0 "2->0" 296chk_msk_cestab 0 "2->0" 297 298echo "a" | \ 299 timeout ${timeout_test} \ 300 ip netns exec $ns \ 301 ./mptcp_connect -p 10001 -l -s TCP -t ${timeout_poll} -w 20 \ 302 0.0.0.0 >/dev/null & 303mptcp_lib_wait_local_port_listen $ns 10001 304echo "b" | \ 305 timeout ${timeout_test} \ 306 ip netns exec $ns \ 307 ./mptcp_connect -p 10001 -r 0 -t ${timeout_poll} -w 20 \ 308 127.0.0.1 >/dev/null & 309wait_connected $ns 10001 310chk_msk_fallback_nr 1 "check fallback" 311chk_msk_inuse 1 312chk_msk_cestab 1 313flush_pids 314 315chk_msk_inuse 0 "1->0" 316chk_msk_cestab 0 "1->0" 317 318NR_CLIENTS=100 319for I in $(seq 1 $NR_CLIENTS); do 320 echo "a" | \ 321 timeout ${timeout_test} \ 322 ip netns exec $ns \ 323 ./mptcp_connect -p $((I+10001)) -l -w 20 \ 324 -t ${timeout_poll} 0.0.0.0 >/dev/null & 325done 326mptcp_lib_wait_local_port_listen $ns $((NR_CLIENTS + 10001)) 327 328for I in $(seq 1 $NR_CLIENTS); do 329 echo "b" | \ 330 timeout ${timeout_test} \ 331 ip netns exec $ns \ 332 ./mptcp_connect -p $((I+10001)) -w 20 \ 333 -t ${timeout_poll} 127.0.0.1 >/dev/null & 334done 335 336wait_msk_nr $((NR_CLIENTS*2)) "many msk socket present" 337chk_msk_inuse $((NR_CLIENTS*2)) "many" 338chk_msk_cestab $((NR_CLIENTS*2)) "many" 339flush_pids 340 341chk_msk_inuse 0 "many->0" 342chk_msk_cestab 0 "many->0" 343 344chk_listener_nr 0 "no listener sockets" 345NR_SERVERS=100 346for I in $(seq 1 $NR_SERVERS); do 347 ip netns exec $ns ./mptcp_connect -p $((I + 20001)) \ 348 -t ${timeout_poll} -l 0.0.0.0 >/dev/null 2>&1 & 349done 350mptcp_lib_wait_local_port_listen $ns $((NR_SERVERS + 20001)) 351 352chk_listener_nr $NR_SERVERS "many listener sockets" 353 354# graceful termination 355for I in $(seq 1 $NR_SERVERS); do 356 echo a | ip netns exec $ns ./mptcp_connect -p $((I + 20001)) 127.0.0.1 >/dev/null 2>&1 & 357done 358flush_pids 359 360mptcp_lib_result_print_all_tap 361exit $ret 362