selftests: mptcp: join: clarify local/global vars

Some vars are redefined in different places. Best to avoid this
classical Bash pitfall where variables are accidentally overridden by
other functions because the proper scope has not been defined.

Most issues are with loops: typically 'i' is used in for-loops but if it
is not global, calling a function from a for-loop also doing a for-loop
with the same non local 'i' variable causes troubles because the first
'i' will be assigned to another value. To prevent such issues, the
iterator variable is now declared as local just before the loop. If it
is always done like this, issues are avoided.

To distinct between local and non local variables, all non local ones
are defined at the beginning of the script. The others are now defined
with the "local" keyword.

Signed-off-by: Matthieu Baerts <matthieu.baerts@tessares.net>
Signed-off-by: Mat Martineau <mathew.j.martineau@linux.intel.com>
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
Matthieu Baerts 2022-03-09 11:16:34 -08:00 committed by Jakub Kicinski
parent 3469d72f13
commit 1e777bd818

View File

@ -9,6 +9,9 @@ cin=""
cinfail="" cinfail=""
cinsent="" cinsent=""
cout="" cout=""
capout=""
ns1=""
ns2=""
ksft_skip=4 ksft_skip=4
timeout_poll=30 timeout_poll=30
timeout_test=$((timeout_poll * 2 + 1)) timeout_test=$((timeout_poll * 2 + 1))
@ -51,12 +54,14 @@ init_partial()
{ {
capout=$(mktemp) capout=$(mktemp)
local rndh
rndh=$(mktemp -u XXXXXX) rndh=$(mktemp -u XXXXXX)
ns1="ns1-$rndh" ns1="ns1-$rndh"
ns2="ns2-$rndh" ns2="ns2-$rndh"
for netns in "$ns1" "$ns2";do local netns
for netns in "$ns1" "$ns2"; do
ip netns add $netns || exit $ksft_skip ip netns add $netns || exit $ksft_skip
ip -net $netns link set lo up ip -net $netns link set lo up
ip netns exec $netns sysctl -q net.mptcp.enabled=1 ip netns exec $netns sysctl -q net.mptcp.enabled=1
@ -77,6 +82,7 @@ init_partial()
# ns1eth3 ns2eth3 # ns1eth3 ns2eth3
# ns1eth4 ns2eth4 # ns1eth4 ns2eth4
local i
for i in `seq 1 4`; do for i in `seq 1 4`; do
ip link add ns1eth$i netns "$ns1" type veth peer name ns2eth$i netns "$ns2" ip link add ns1eth$i netns "$ns1" type veth peer name ns2eth$i netns "$ns2"
ip -net "$ns1" addr add 10.0.$i.1/24 dev ns1eth$i ip -net "$ns1" addr add 10.0.$i.1/24 dev ns1eth$i
@ -95,6 +101,7 @@ init_partial()
init_shapers() init_shapers()
{ {
local i
for i in `seq 1 4`; do for i in `seq 1 4`; do
tc -n $ns1 qdisc add dev ns1eth$i root netem rate 20mbit delay 1 tc -n $ns1 qdisc add dev ns1eth$i root netem rate 20mbit delay 1
tc -n $ns2 qdisc add dev ns2eth$i root netem rate 20mbit delay 1 tc -n $ns2 qdisc add dev ns2eth$i root netem rate 20mbit delay 1
@ -105,6 +112,7 @@ cleanup_partial()
{ {
rm -f "$capout" rm -f "$capout"
local netns
for netns in "$ns1" "$ns2"; do for netns in "$ns1" "$ns2"; do
ip netns del $netns ip netns del $netns
rm -f /tmp/$netns.{nstat,out} rm -f /tmp/$netns.{nstat,out}
@ -201,7 +209,8 @@ reset_with_cookies()
{ {
reset "${1}" || return 1 reset "${1}" || return 1
for netns in "$ns1" "$ns2";do local netns
for netns in "$ns1" "$ns2"; do
ip netns exec $netns sysctl -q net.ipv4.tcp_syncookies=2 ip netns exec $netns sysctl -q net.ipv4.tcp_syncookies=2
done done
} }
@ -276,10 +285,11 @@ print_file_err()
check_transfer() check_transfer()
{ {
in=$1 local in=$1
out=$2 local out=$2
what=$3 local what=$3
local line
cmp -l "$in" "$out" | while read line; do cmp -l "$in" "$out" | while read line; do
local arr=($line) local arr=($line)
@ -301,9 +311,9 @@ check_transfer()
do_ping() do_ping()
{ {
listener_ns="$1" local listener_ns="$1"
connector_ns="$2" local connector_ns="$2"
connect_addr="$3" local connect_addr="$3"
ip netns exec ${connector_ns} ping -q -c 1 $connect_addr >/dev/null ip netns exec ${connector_ns} ping -q -c 1 $connect_addr >/dev/null
if [ $? -ne 0 ] ; then if [ $? -ne 0 ] ; then
@ -314,15 +324,16 @@ do_ping()
link_failure() link_failure()
{ {
ns="$1" local ns="$1"
if [ -z "$FAILING_LINKS" ]; then if [ -z "$FAILING_LINKS" ]; then
l=$((RANDOM%4)) l=$((RANDOM%4))
FAILING_LINKS=$((l+1)) FAILING_LINKS=$((l+1))
fi fi
local l
for l in $FAILING_LINKS; do for l in $FAILING_LINKS; do
veth="ns1eth$l" local veth="ns1eth$l"
ip -net "$ns" link set "$veth" down ip -net "$ns" link set "$veth" down
done done
} }
@ -339,9 +350,10 @@ wait_local_port_listen()
local listener_ns="${1}" local listener_ns="${1}"
local port="${2}" local port="${2}"
local port_hex i local port_hex
port_hex="$(printf "%04X" "${port}")" port_hex="$(printf "%04X" "${port}")"
local i
for i in $(seq 10); do for i in $(seq 10); do
ip netns exec "${listener_ns}" cat /proc/net/tcp* | \ ip netns exec "${listener_ns}" cat /proc/net/tcp* | \
awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" && awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) {rc=0; exit}} END {exit rc}" &&
@ -352,7 +364,7 @@ wait_local_port_listen()
rm_addr_count() rm_addr_count()
{ {
ns=${1} local ns=${1}
ip netns exec ${ns} nstat -as | grep MPTcpExtRmAddr | awk '{print $2}' ip netns exec ${ns} nstat -as | grep MPTcpExtRmAddr | awk '{print $2}'
} }
@ -363,8 +375,8 @@ wait_rm_addr()
local ns="${1}" local ns="${1}"
local old_cnt="${2}" local old_cnt="${2}"
local cnt local cnt
local i
local i
for i in $(seq 10); do for i in $(seq 10); do
cnt=$(rm_addr_count ${ns}) cnt=$(rm_addr_count ${ns})
[ "$cnt" = "${old_cnt}" ] || break [ "$cnt" = "${old_cnt}" ] || break
@ -404,12 +416,13 @@ pm_nl_add_endpoint()
{ {
local ns=$1 local ns=$1
local addr=$2 local addr=$2
local flags local flags _flags
local port local port _port
local dev local dev _dev
local id local id _id
local nr=2 local nr=2
local p
for p in $@ for p in $@
do do
if [ $p = "flags" ]; then if [ $p = "flags" ]; then
@ -572,24 +585,26 @@ filter_tcp_from()
do_transfer() do_transfer()
{ {
listener_ns="$1" local listener_ns="$1"
connector_ns="$2" local connector_ns="$2"
cl_proto="$3" local cl_proto="$3"
srv_proto="$4" local srv_proto="$4"
connect_addr="$5" local connect_addr="$5"
test_link_fail="$6" local test_link_fail="$6"
addr_nr_ns1="$7" local addr_nr_ns1="$7"
addr_nr_ns2="$8" local addr_nr_ns2="$8"
speed="$9" local speed="$9"
sflags="${10}" local sflags="${10}"
port=$((10000+$TEST_COUNT-1)) local port=$((10000 + TEST_COUNT - 1))
local cappid
:> "$cout" :> "$cout"
:> "$sout" :> "$sout"
:> "$capout" :> "$capout"
if [ $capture -eq 1 ]; then if [ $capture -eq 1 ]; then
local capuser
if [ -z $SUDO_USER ] ; then if [ -z $SUDO_USER ] ; then
capuser="" capuser=""
else else
@ -643,7 +658,7 @@ do_transfer()
./mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \ ./mptcp_connect -t ${timeout_poll} -l -p $port -s ${srv_proto} \
$extra_args ${local_addr} < "$sin" > "$sout" & $extra_args ${local_addr} < "$sin" > "$sout" &
fi fi
spid=$! local spid=$!
wait_local_port_listen "${listener_ns}" "${port}" wait_local_port_listen "${listener_ns}" "${port}"
@ -666,15 +681,16 @@ do_transfer()
./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \ ./mptcp_connect -t ${timeout_poll} -p $port -s ${cl_proto} \
$extra_args $connect_addr > "$cout" & $extra_args $connect_addr > "$cout" &
fi fi
cpid=$! local cpid=$!
# let the mptcp subflow be established in background before # let the mptcp subflow be established in background before
# do endpoint manipulation # do endpoint manipulation
[ $addr_nr_ns1 = "0" -a $addr_nr_ns2 = "0" ] || sleep 1 [ $addr_nr_ns1 = "0" -a $addr_nr_ns2 = "0" ] || sleep 1
if [ $addr_nr_ns1 -gt 0 ]; then if [ $addr_nr_ns1 -gt 0 ]; then
local counter=2
local add_nr_ns1
let add_nr_ns1=addr_nr_ns1 let add_nr_ns1=addr_nr_ns1
counter=2
while [ $add_nr_ns1 -gt 0 ]; do while [ $add_nr_ns1 -gt 0 ]; do
local addr local addr
if is_v6 "${connect_addr}"; then if is_v6 "${connect_addr}"; then
@ -687,13 +703,16 @@ do_transfer()
let add_nr_ns1-=1 let add_nr_ns1-=1
done done
elif [ $addr_nr_ns1 -lt 0 ]; then elif [ $addr_nr_ns1 -lt 0 ]; then
local rm_nr_ns1
let rm_nr_ns1=-addr_nr_ns1 let rm_nr_ns1=-addr_nr_ns1
if [ $rm_nr_ns1 -lt 8 ]; then if [ $rm_nr_ns1 -lt 8 ]; then
counter=0 local counter=0
local line
pm_nl_show_endpoints ${listener_ns} | while read line; do pm_nl_show_endpoints ${listener_ns} | while read line; do
local arr=($line) local arr=($line)
local nr=0 local nr=0
local i
for i in ${arr[@]}; do for i in ${arr[@]}; do
if [ $i = "id" ]; then if [ $i = "id" ]; then
if [ $counter -eq $rm_nr_ns1 ]; then if [ $counter -eq $rm_nr_ns1 ]; then
@ -715,7 +734,7 @@ do_transfer()
fi fi
fi fi
flags="subflow" local flags="subflow"
if [[ "${addr_nr_ns2}" = "fullmesh_"* ]]; then if [[ "${addr_nr_ns2}" = "fullmesh_"* ]]; then
flags="${flags},fullmesh" flags="${flags},fullmesh"
addr_nr_ns2=${addr_nr_ns2:9} addr_nr_ns2=${addr_nr_ns2:9}
@ -726,8 +745,9 @@ do_transfer()
[ $addr_nr_ns1 -gt 0 -a $addr_nr_ns2 -lt 0 ] && sleep 1 [ $addr_nr_ns1 -gt 0 -a $addr_nr_ns2 -lt 0 ] && sleep 1
if [ $addr_nr_ns2 -gt 0 ]; then if [ $addr_nr_ns2 -gt 0 ]; then
local add_nr_ns2
let add_nr_ns2=addr_nr_ns2 let add_nr_ns2=addr_nr_ns2
counter=3 local counter=3
while [ $add_nr_ns2 -gt 0 ]; do while [ $add_nr_ns2 -gt 0 ]; do
local addr local addr
if is_v6 "${connect_addr}"; then if is_v6 "${connect_addr}"; then
@ -740,18 +760,21 @@ do_transfer()
let add_nr_ns2-=1 let add_nr_ns2-=1
done done
elif [ $addr_nr_ns2 -lt 0 ]; then elif [ $addr_nr_ns2 -lt 0 ]; then
let rm_nr_ns2=-addr_nr_ns2 local rm_nr_ns2
if [ $rm_nr_ns2 -lt 8 ]; then if [ $rm_nr_ns2 -lt 8 ]; then
counter=0 local counter=0
local line
pm_nl_show_endpoints ${connector_ns} | while read line; do pm_nl_show_endpoints ${connector_ns} | while read line; do
local arr=($line) local arr=($line)
local nr=0 local nr=0
local i
for i in ${arr[@]}; do for i in ${arr[@]}; do
if [ $i = "id" ]; then if [ $i = "id" ]; then
if [ $counter -eq $rm_nr_ns2 ]; then if [ $counter -eq $rm_nr_ns2 ]; then
break break
fi fi
local id rm_addr
# rm_addr are serialized, allow the previous one to # rm_addr are serialized, allow the previous one to
# complete # complete
id=${arr[$nr+1]} id=${arr[$nr+1]}
@ -778,12 +801,16 @@ do_transfer()
if [ ! -z $sflags ]; then if [ ! -z $sflags ]; then
sleep 1 sleep 1
local netns
for netns in "$ns1" "$ns2"; do for netns in "$ns1" "$ns2"; do
local line
pm_nl_show_endpoints $netns | while read line; do pm_nl_show_endpoints $netns | while read line; do
local arr=($line) local arr=($line)
local nr=0 local nr=0
local id local id
local i
for i in ${arr[@]}; do for i in ${arr[@]}; do
if [ $i = "id" ]; then if [ $i = "id" ]; then
id=${arr[$nr+1]} id=${arr[$nr+1]}
@ -796,9 +823,9 @@ do_transfer()
fi fi
wait $cpid wait $cpid
retc=$? local retc=$?
wait $spid wait $spid
rets=$? local rets=$?
if [ $capture -eq 1 ]; then if [ $capture -eq 1 ]; then
sleep 1 sleep 1
@ -848,9 +875,9 @@ do_transfer()
make_file() make_file()
{ {
name=$1 local name=$1
who=$2 local who=$2
size=$3 local size=$3
dd if=/dev/urandom of="$name" bs=1024 count=$size 2> /dev/null dd if=/dev/urandom of="$name" bs=1024 count=$size 2> /dev/null
echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name" echo -e "\nMPTCP_TEST_FILE_END_MARKER" >> "$name"
@ -860,14 +887,16 @@ make_file()
run_tests() run_tests()
{ {
listener_ns="$1" local listener_ns="$1"
connector_ns="$2" local connector_ns="$2"
connect_addr="$3" local connect_addr="$3"
test_linkfail="${4:-0}" local test_linkfail="${4:-0}"
addr_nr_ns1="${5:-0}" local addr_nr_ns1="${5:-0}"
addr_nr_ns2="${6:-0}" local addr_nr_ns2="${6:-0}"
speed="${7:-fast}" local speed="${7:-fast}"
sflags="${8:-""}" local sflags="${8:-""}"
local size
# The values above 2 are reused to make test files # The values above 2 are reused to make test files
# with the given sizes (KB) # with the given sizes (KB)
@ -1437,7 +1466,9 @@ wait_attempt_fail()
local ns=$1 local ns=$1
while [ $time -lt $timeout_ms ]; do while [ $time -lt $timeout_ms ]; do
local cnt=$(ip netns exec $ns nstat -as TcpAttemptFails | grep TcpAttemptFails | awk '{print $2}') local cnt
cnt=$(ip netns exec $ns nstat -as TcpAttemptFails | grep TcpAttemptFails | awk '{print $2}')
[ "$cnt" = 1 ] && return 1 [ "$cnt" = 1 ] && return 1
time=$((time + 100)) time=$((time + 100))