Skip to content

Commit d4cea2c

Browse files
committed
Merge branch 'selftests-mptcp-use-net-lib-sh-to-manage-netns'
Matthieu Baerts says: ==================== selftests: mptcp: use net/lib.sh to manage netns The goal of this series is to use helpers from net/lib.sh with MPTCP selftests. - Patches 1 to 4 are some clean-ups and preparation in net/lib.sh: - Patch 1 simplifies the code handling errexit by ignoring possible errors instead of disabling errexit temporary. - Patch 2 removes the netns from the list after having cleaned it, not to try to clean it twice. - Patch 3 removes the 'readonly' attribute for the netns variable, to allow using the same name in local variables. - Patch 4 removes the local 'ns' var, not to conflict with the global one it needs to setup. - Patch 5 uses helpers from net/lib.sh to create and delete netns in MPTCP selftests. - Patch 6 uses wait_local_port_listen helper from net/net_helper.sh. ==================== Link: https://lore.kernel.org/r/20240607-upstream-net-next-20240607-selftests-mptcp-net-lib-v1-0-e36986faac94@kernel.org Signed-off-by: Jakub Kicinski <[email protected]>
2 parents bfc6507 + 1af3bc9 commit d4cea2c

File tree

2 files changed

+40
-44
lines changed

2 files changed

+40
-44
lines changed

tools/testing/selftests/net/lib.sh

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -125,28 +125,36 @@ slowwait_for_counter()
125125
slowwait "$timeout" until_counter_is ">= $((base + delta))" "$@"
126126
}
127127

128+
remove_ns_list()
129+
{
130+
local item=$1
131+
local ns
132+
local ns_list=("${NS_LIST[@]}")
133+
NS_LIST=()
134+
135+
for ns in "${ns_list[@]}"; do
136+
if [ "${ns}" != "${item}" ]; then
137+
NS_LIST+=("${ns}")
138+
fi
139+
done
140+
}
141+
128142
cleanup_ns()
129143
{
130144
local ns=""
131-
local errexit=0
132145
local ret=0
133146

134-
# disable errexit temporary
135-
if [[ $- =~ "e" ]]; then
136-
errexit=1
137-
set +e
138-
fi
139-
140147
for ns in "$@"; do
141148
[ -z "${ns}" ] && continue
142-
ip netns delete "${ns}" &> /dev/null
149+
ip netns delete "${ns}" &> /dev/null || true
143150
if ! busywait $BUSYWAIT_TIMEOUT ip netns list \| grep -vq "^$ns$" &> /dev/null; then
144151
echo "Warn: Failed to remove namespace $ns"
145152
ret=1
153+
else
154+
remove_ns_list "${ns}"
146155
fi
147156
done
148157

149-
[ $errexit -eq 1 ] && set -e
150158
return $ret
151159
}
152160

@@ -159,29 +167,30 @@ cleanup_all_ns()
159167
# setup_ns local remote
160168
setup_ns()
161169
{
162-
local ns=""
163170
local ns_name=""
164171
local ns_list=()
165-
local ns_exist=
166172
for ns_name in "$@"; do
173+
# avoid conflicts with local var: internal error
174+
if [ "${ns_name}" = "ns_name" ]; then
175+
echo "Failed to setup namespace '${ns_name}': invalid name"
176+
cleanup_ns "${ns_list[@]}"
177+
exit $ksft_fail
178+
fi
179+
167180
# Some test may setup/remove same netns multi times
168-
if unset ${ns_name} 2> /dev/null; then
169-
ns="${ns_name,,}-$(mktemp -u XXXXXX)"
170-
eval readonly ${ns_name}="$ns"
171-
ns_exist=false
181+
if [ -z "${!ns_name}" ]; then
182+
eval "${ns_name}=${ns_name,,}-$(mktemp -u XXXXXX)"
172183
else
173-
eval ns='$'${ns_name}
174-
cleanup_ns "$ns"
175-
ns_exist=true
184+
cleanup_ns "${!ns_name}"
176185
fi
177186

178-
if ! ip netns add "$ns"; then
187+
if ! ip netns add "${!ns_name}"; then
179188
echo "Failed to create namespace $ns_name"
180189
cleanup_ns "${ns_list[@]}"
181190
return $ksft_skip
182191
fi
183-
ip -n "$ns" link set lo up
184-
! $ns_exist && ns_list+=("$ns")
192+
ip -n "${!ns_name}" link set lo up
193+
ns_list+=("${!ns_name}")
185194
done
186195
NS_LIST+=("${ns_list[@]}")
187196
}

tools/testing/selftests/net/mptcp/mptcp_lib.sh

Lines changed: 10 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
#! /bin/bash
22
# SPDX-License-Identifier: GPL-2.0
33

4+
. "$(dirname "${0}")/../lib.sh"
5+
. "$(dirname "${0}")/../net_helper.sh"
6+
47
readonly KSFT_PASS=0
58
readonly KSFT_FAIL=1
69
readonly KSFT_SKIP=4
@@ -361,20 +364,7 @@ mptcp_lib_check_transfer() {
361364

362365
# $1: ns, $2: port
363366
mptcp_lib_wait_local_port_listen() {
364-
local listener_ns="${1}"
365-
local port="${2}"
366-
367-
local port_hex
368-
port_hex="$(printf "%04X" "${port}")"
369-
370-
local _
371-
for _ in $(seq 10); do
372-
ip netns exec "${listener_ns}" cat /proc/net/tcp* | \
373-
awk "BEGIN {rc=1} {if (\$2 ~ /:${port_hex}\$/ && \$4 ~ /0A/) \
374-
{rc=0; exit}} END {exit rc}" &&
375-
break
376-
sleep 0.1
377-
done
367+
wait_local_port_listen "${@}" "tcp"
378368
}
379369

380370
mptcp_lib_check_output() {
@@ -438,27 +428,24 @@ mptcp_lib_check_tools() {
438428
}
439429

440430
mptcp_lib_ns_init() {
441-
local sec rndh
442-
443-
sec=$(date +%s)
444-
rndh=$(printf %x "${sec}")-$(mktemp -u XXXXXX)
431+
if ! setup_ns ${@}; then
432+
mptcp_lib_pr_fail "Failed to setup namespace ${@}"
433+
exit ${KSFT_FAIL}
434+
fi
445435

446436
local netns
447437
for netns in "${@}"; do
448-
eval "${netns}=${netns}-${rndh}"
449-
450-
ip netns add "${!netns}" || exit ${KSFT_SKIP}
451-
ip -net "${!netns}" link set lo up
452438
ip netns exec "${!netns}" sysctl -q net.mptcp.enabled=1
453439
ip netns exec "${!netns}" sysctl -q net.ipv4.conf.all.rp_filter=0
454440
ip netns exec "${!netns}" sysctl -q net.ipv4.conf.default.rp_filter=0
455441
done
456442
}
457443

458444
mptcp_lib_ns_exit() {
445+
cleanup_ns "${@}"
446+
459447
local netns
460448
for netns in "${@}"; do
461-
ip netns del "${netns}"
462449
rm -f /tmp/"${netns}".{nstat,out}
463450
done
464451
}

0 commit comments

Comments
 (0)