Skip to content

Commit

Permalink
Merge branch 'selftests-mptcp-share-code-and-fix-shellcheck-warnings'
Browse files Browse the repository at this point in the history
Matthieu Baerts says:

====================
selftests: mptcp: share code and fix shellcheck warnings

This series cleans MPTCP selftests code.

Patch 1 stops using 'iptables-legacy' if available, but uses 'iptables',
which is likely 'iptables-nft' behind.

Patches 2, 4 and 6 move duplicated code to mptcp_lib.sh. Patch 3 is a
preparation for patch 4, and patch 5 adds generic actions at the
creation and deletion of netns.

Patches 7 to 11 disable a few shellcheck warnings, and fix the rest, so
it is easy to spot real issues later. MPTCP CI is checking that now.

Patch 12 avoids redoing some actions at init time twice, e.g. restarting
the pm events tool.

v1: https://lore.kernel.org/r/20240305-upstream-net-next-20240304-selftests-mptcp-shared-code-shellcheck-v1-0-66618ea5504e@kernel.org
====================

Link: https://lore.kernel.org/r/20240306-upstream-net-next-20240304-selftests-mptcp-shared-code-shellcheck-v2-0-bc79e6e5e6a0@kernel.org
Signed-off-by: Jakub Kicinski <kuba@kernel.org>
  • Loading branch information
Jakub Kicinski committed Mar 8, 2024
2 parents c3874bb + c66fb48 commit d3423ed
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 244 deletions.
35 changes: 14 additions & 21 deletions tools/testing/selftests/net/mptcp/diag.sh
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0

# Double quotes to prevent globbing and word splitting is recommended in new
# code but we accept it, especially because there were too many before having
# address all other issues detected by shellcheck.
#shellcheck disable=SC2086

. "$(dirname "${0}")/mptcp_lib.sh"

sec=$(date +%s)
rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
ns="ns1-$rndh"
ksft_skip=4
ns=""
test_cnt=1
timeout_poll=30
timeout_test=$((timeout_poll * 2 + 1))
Expand All @@ -26,25 +28,17 @@ flush_pids()
done
}

# This function is used in the cleanup trap
#shellcheck disable=SC2317
cleanup()
{
ip netns pids "${ns}" | xargs --no-run-if-empty kill -SIGKILL &>/dev/null

ip netns del $ns
mptcp_lib_ns_exit "${ns}"
}

mptcp_lib_check_mptcp

ip -Version > /dev/null 2>&1
if [ $? -ne 0 ];then
echo "SKIP: Could not run test without ip tool"
exit $ksft_skip
fi
ss -h | grep -q MPTCP
if [ $? -ne 0 ];then
echo "SKIP: ss tool does not support MPTCP"
exit $ksft_skip
fi
mptcp_lib_check_tools ip ss

get_msk_inuse()
{
Expand Down Expand Up @@ -186,7 +180,7 @@ chk_msk_inuse()
expected=$((expected + listen_nr))

for _ in $(seq 10); do
if [ $(get_msk_inuse) -eq $expected ];then
if [ "$(get_msk_inuse)" -eq $expected ]; then
break
fi
sleep 0.1
Expand Down Expand Up @@ -224,8 +218,7 @@ wait_connected()
}

trap cleanup EXIT
ip netns add $ns
ip -n $ns link set dev lo up
mptcp_lib_ns_init ns

echo "a" | \
timeout ${timeout_test} \
Expand Down Expand Up @@ -273,7 +266,7 @@ chk_msk_inuse 0 "1->0"
chk_msk_cestab 0 "1->0"

NR_CLIENTS=100
for I in `seq 1 $NR_CLIENTS`; do
for I in $(seq 1 $NR_CLIENTS); do
echo "a" | \
timeout ${timeout_test} \
ip netns exec $ns \
Expand All @@ -282,7 +275,7 @@ for I in `seq 1 $NR_CLIENTS`; do
done
mptcp_lib_wait_local_port_listen $ns $((NR_CLIENTS + 10001))

for I in `seq 1 $NR_CLIENTS`; do
for I in $(seq 1 $NR_CLIENTS); do
echo "b" | \
timeout ${timeout_test} \
ip netns exec $ns \
Expand Down
111 changes: 58 additions & 53 deletions tools/testing/selftests/net/mptcp/mptcp_connect.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0

# Double quotes to prevent globbing and word splitting is recommended in new
# code but we accept it, especially because there were too many before having
# address all other issues detected by shellcheck.
#shellcheck disable=SC2086

. "$(dirname "${0}")/mptcp_lib.sh"

time_start=$(date +%s)
Expand All @@ -13,7 +18,6 @@ sout=""
cin_disconnect=""
cin=""
cout=""
ksft_skip=4
capture=false
timeout_poll=30
timeout_test=$((timeout_poll * 2 + 1))
Expand Down Expand Up @@ -121,38 +125,29 @@ while getopts "$optstring" option;do
esac
done

sec=$(date +%s)
rndh=$(printf %x $sec)-$(mktemp -u XXXXXX)
ns1="ns1-$rndh"
ns2="ns2-$rndh"
ns3="ns3-$rndh"
ns4="ns4-$rndh"
ns1=""
ns2=""
ns3=""
ns4=""

TEST_COUNT=0
TEST_GROUP=""

# This function is used in the cleanup trap
#shellcheck disable=SC2317
cleanup()
{
rm -f "$cin_disconnect" "$cout_disconnect"
rm -f "$cin" "$cout"
rm -f "$sin" "$sout"
rm -f "$capout"

local netns
for netns in "$ns1" "$ns2" "$ns3" "$ns4";do
ip netns del $netns
rm -f /tmp/$netns.{nstat,out}
done
mptcp_lib_ns_exit "${ns1}" "${ns2}" "${ns3}" "${ns4}"
}

mptcp_lib_check_mptcp
mptcp_lib_check_kallsyms

ip -Version > /dev/null 2>&1
if [ $? -ne 0 ];then
echo "SKIP: Could not run test without ip tool"
exit $ksft_skip
fi
mptcp_lib_check_tools ip

sin=$(mktemp)
sout=$(mktemp)
Expand All @@ -163,10 +158,7 @@ cin_disconnect="$cin".disconnect
cout_disconnect="$cout".disconnect
trap cleanup EXIT

for i in "$ns1" "$ns2" "$ns3" "$ns4";do
ip netns add $i || exit $ksft_skip
ip -net $i link set lo up
done
mptcp_lib_ns_init ns1 ns2 ns3 ns4

# "$ns1" ns2 ns3 ns4
# ns1eth2 ns2eth1 ns2eth3 ns3eth2 ns3eth4 ns4eth3
Expand Down Expand Up @@ -225,8 +217,9 @@ set_ethtool_flags() {
local dev="$2"
local flags="$3"

ip netns exec $ns ethtool -K $dev $flags 2>/dev/null
[ $? -eq 0 ] && echo "INFO: set $ns dev $dev: ethtool -K $flags"
if ip netns exec $ns ethtool -K $dev $flags 2>/dev/null; then
echo "INFO: set $ns dev $dev: ethtool -K $flags"
fi
}

set_random_ethtool_flags() {
Expand Down Expand Up @@ -256,8 +249,8 @@ fi

check_mptcp_disabled()
{
local disabled_ns="ns_disabled-$rndh"
ip netns add ${disabled_ns} || exit $ksft_skip
local disabled_ns
mptcp_lib_ns_init disabled_ns

# net.mptcp.enabled should be enabled by default
if [ "$(ip netns exec ${disabled_ns} sysctl net.mptcp.enabled | awk '{ print $3 }')" -ne 1 ]; then
Expand All @@ -271,7 +264,7 @@ check_mptcp_disabled()
local err=0
LC_ALL=C ip netns exec ${disabled_ns} ./mptcp_connect -p 10000 -s MPTCP 127.0.0.1 < "$cin" 2>&1 | \
grep -q "^socket: Protocol not available$" && err=1
ip netns delete ${disabled_ns}
mptcp_lib_ns_exit "${disabled_ns}"

if [ ${err} -eq 0 ]; then
echo -e "New MPTCP socket cannot be blocked via sysctl\t\t[ FAIL ]"
Expand Down Expand Up @@ -321,7 +314,7 @@ do_transfer()
local extra_args="$7"

local port
port=$((10000+$TEST_COUNT))
port=$((10000+TEST_COUNT))
TEST_COUNT=$((TEST_COUNT+1))

if [ "$rcvbuf" -gt 0 ]; then
Expand Down Expand Up @@ -353,6 +346,7 @@ do_transfer()

if $capture; then
local capuser
local rndh="${connector_ns:4}"
if [ -z $SUDO_USER ] ; then
capuser=""
else
Expand All @@ -378,12 +372,18 @@ do_transfer()
nstat -n
fi

local stat_synrx_last_l=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtMPCapableSYNRX")
local stat_ackrx_last_l=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtMPCapableACKRX")
local stat_cookietx_last=$(mptcp_lib_get_counter "${listener_ns}" "TcpExtSyncookiesSent")
local stat_cookierx_last=$(mptcp_lib_get_counter "${listener_ns}" "TcpExtSyncookiesRecv")
local stat_csum_err_s=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtDataCsumErr")
local stat_csum_err_c=$(mptcp_lib_get_counter "${connector_ns}" "MPTcpExtDataCsumErr")
local stat_synrx_last_l
local stat_ackrx_last_l
local stat_cookietx_last
local stat_cookierx_last
local stat_csum_err_s
local stat_csum_err_c
stat_synrx_last_l=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtMPCapableSYNRX")
stat_ackrx_last_l=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtMPCapableACKRX")
stat_cookietx_last=$(mptcp_lib_get_counter "${listener_ns}" "TcpExtSyncookiesSent")
stat_cookierx_last=$(mptcp_lib_get_counter "${listener_ns}" "TcpExtSyncookiesRecv")
stat_csum_err_s=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtDataCsumErr")
stat_csum_err_c=$(mptcp_lib_get_counter "${connector_ns}" "MPTcpExtDataCsumErr")

timeout ${timeout_test} \
ip netns exec ${listener_ns} \
Expand Down Expand Up @@ -446,11 +446,16 @@ do_transfer()
mptcp_lib_check_transfer $cin $sout "file received by server"
rets=$?

local stat_synrx_now_l=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtMPCapableSYNRX")
local stat_ackrx_now_l=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtMPCapableACKRX")
local stat_cookietx_now=$(mptcp_lib_get_counter "${listener_ns}" "TcpExtSyncookiesSent")
local stat_cookierx_now=$(mptcp_lib_get_counter "${listener_ns}" "TcpExtSyncookiesRecv")
local stat_ooo_now=$(mptcp_lib_get_counter "${listener_ns}" "TcpExtTCPOFOQueue")
local stat_synrx_now_l
local stat_ackrx_now_l
local stat_cookietx_now
local stat_cookierx_now
local stat_ooo_now
stat_synrx_now_l=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtMPCapableSYNRX")
stat_ackrx_now_l=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtMPCapableACKRX")
stat_cookietx_now=$(mptcp_lib_get_counter "${listener_ns}" "TcpExtSyncookiesSent")
stat_cookierx_now=$(mptcp_lib_get_counter "${listener_ns}" "TcpExtSyncookiesRecv")
stat_ooo_now=$(mptcp_lib_get_counter "${listener_ns}" "TcpExtTCPOFOQueue")

expect_synrx=$((stat_synrx_last_l))
expect_ackrx=$((stat_ackrx_last_l))
Expand All @@ -459,16 +464,16 @@ do_transfer()
cookies=${cookies##*=}

if [ ${cl_proto} = "MPTCP" ] && [ ${srv_proto} = "MPTCP" ]; then
expect_synrx=$((stat_synrx_last_l+$connect_per_transfer))
expect_ackrx=$((stat_ackrx_last_l+$connect_per_transfer))
expect_synrx=$((stat_synrx_last_l+connect_per_transfer))
expect_ackrx=$((stat_ackrx_last_l+connect_per_transfer))
fi

if [ ${stat_synrx_now_l} -lt ${expect_synrx} ]; then
printf "[ FAIL ] lower MPC SYN rx (%d) than expected (%d)\n" \
"${stat_synrx_now_l}" "${expect_synrx}" 1>&2
retc=1
fi
if [ ${stat_ackrx_now_l} -lt ${expect_ackrx} -a ${stat_ooo_now} -eq 0 ]; then
if [ ${stat_ackrx_now_l} -lt ${expect_ackrx} ] && [ ${stat_ooo_now} -eq 0 ]; then
if [ ${stat_ooo_now} -eq 0 ]; then
printf "[ FAIL ] lower MPC ACK rx (%d) than expected (%d)\n" \
"${stat_ackrx_now_l}" "${expect_ackrx}" 1>&2
Expand All @@ -479,18 +484,20 @@ do_transfer()
fi

if $checksum; then
local csum_err_s=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtDataCsumErr")
local csum_err_c=$(mptcp_lib_get_counter "${connector_ns}" "MPTcpExtDataCsumErr")
local csum_err_s
local csum_err_c
csum_err_s=$(mptcp_lib_get_counter "${listener_ns}" "MPTcpExtDataCsumErr")
csum_err_c=$(mptcp_lib_get_counter "${connector_ns}" "MPTcpExtDataCsumErr")

local csum_err_s_nr=$((csum_err_s - stat_csum_err_s))
if [ $csum_err_s_nr -gt 0 ]; then
printf "[ FAIL ]\nserver got $csum_err_s_nr data checksum error[s]"
printf "[ FAIL ]\nserver got %d data checksum error[s]" ${csum_err_s_nr}
rets=1
fi

local csum_err_c_nr=$((csum_err_c - stat_csum_err_c))
if [ $csum_err_c_nr -gt 0 ]; then
printf "[ FAIL ]\nclient got $csum_err_c_nr data checksum error[s]"
printf "[ FAIL ]\nclient got %d data checksum error[s]" ${csum_err_c_nr}
retc=1
fi
fi
Expand Down Expand Up @@ -658,7 +665,7 @@ run_test_transparent()
return
fi

ip netns exec "$listener_ns" nft -f /dev/stdin <<"EOF"
if ! ip netns exec "$listener_ns" nft -f /dev/stdin <<"EOF"
flush ruleset
table inet mangle {
chain divert {
Expand All @@ -669,7 +676,7 @@ table inet mangle {
}
}
EOF
if [ $? -ne 0 ]; then
then
echo "SKIP: $msg, could not load nft ruleset"
mptcp_lib_fail_if_expected_feature "nft rules"
mptcp_lib_result_skip "${TEST_GROUP}"
Expand All @@ -684,17 +691,15 @@ EOF
local_addr="0.0.0.0"
fi

ip -net "$listener_ns" $r6flag rule add fwmark 1 lookup 100
if [ $? -ne 0 ]; then
if ! ip -net "$listener_ns" $r6flag rule add fwmark 1 lookup 100; then
ip netns exec "$listener_ns" nft flush ruleset
echo "SKIP: $msg, ip $r6flag rule failed"
mptcp_lib_fail_if_expected_feature "ip rule"
mptcp_lib_result_skip "${TEST_GROUP}"
return
fi

ip -net "$listener_ns" route add local $local_addr/0 dev lo table 100
if [ $? -ne 0 ]; then
if ! ip -net "$listener_ns" route add local $local_addr/0 dev lo table 100; then
ip netns exec "$listener_ns" nft flush ruleset
ip -net "$listener_ns" $r6flag rule del fwmark 1 lookup 100
echo "SKIP: $msg, ip route add local $local_addr failed"
Expand Down Expand Up @@ -857,7 +862,7 @@ stop_if_error "Could not even run ping tests"
echo -n "INFO: Using loss of $tc_loss "
test "$tc_delay" -gt 0 && echo -n "delay $tc_delay ms "

reorder_delay=$(($tc_delay / 4))
reorder_delay=$((tc_delay / 4))

if [ -z "${tc_reorder}" ]; then
reorder1=$((RANDOM%10))
Expand Down
Loading

0 comments on commit d3423ed

Please sign in to comment.