Commit d4cea2ca authored by Jakub Kicinski's avatar Jakub Kicinski

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.orgSigned-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parents bfc65070 1af3bc91
...@@ -125,28 +125,36 @@ slowwait_for_counter() ...@@ -125,28 +125,36 @@ slowwait_for_counter()
slowwait "$timeout" until_counter_is ">= $((base + delta))" "$@" slowwait "$timeout" until_counter_is ">= $((base + delta))" "$@"
} }
remove_ns_list()
{
local item=$1
local ns
local ns_list=("${NS_LIST[@]}")
NS_LIST=()
for ns in "${ns_list[@]}"; do
if [ "${ns}" != "${item}" ]; then
NS_LIST+=("${ns}")
fi
done
}
cleanup_ns() cleanup_ns()
{ {
local ns="" local ns=""
local errexit=0
local ret=0 local ret=0
# disable errexit temporary
if [[ $- =~ "e" ]]; then
errexit=1
set +e
fi
for ns in "$@"; do for ns in "$@"; do
[ -z "${ns}" ] && continue [ -z "${ns}" ] && continue
ip netns delete "${ns}" &> /dev/null ip netns delete "${ns}" &> /dev/null || true
if ! busywait $BUSYWAIT_TIMEOUT ip netns list \| grep -vq "^$ns$" &> /dev/null; then if ! busywait $BUSYWAIT_TIMEOUT ip netns list \| grep -vq "^$ns$" &> /dev/null; then
echo "Warn: Failed to remove namespace $ns" echo "Warn: Failed to remove namespace $ns"
ret=1 ret=1
else
remove_ns_list "${ns}"
fi fi
done done
[ $errexit -eq 1 ] && set -e
return $ret return $ret
} }
...@@ -159,29 +167,30 @@ cleanup_all_ns() ...@@ -159,29 +167,30 @@ cleanup_all_ns()
# setup_ns local remote # setup_ns local remote
setup_ns() setup_ns()
{ {
local ns=""
local ns_name="" local ns_name=""
local ns_list=() local ns_list=()
local ns_exist=
for ns_name in "$@"; do for ns_name in "$@"; do
# avoid conflicts with local var: internal error
if [ "${ns_name}" = "ns_name" ]; then
echo "Failed to setup namespace '${ns_name}': invalid name"
cleanup_ns "${ns_list[@]}"
exit $ksft_fail
fi
# Some test may setup/remove same netns multi times # Some test may setup/remove same netns multi times
if unset ${ns_name} 2> /dev/null; then if [ -z "${!ns_name}" ]; then
ns="${ns_name,,}-$(mktemp -u XXXXXX)" eval "${ns_name}=${ns_name,,}-$(mktemp -u XXXXXX)"
eval readonly ${ns_name}="$ns"
ns_exist=false
else else
eval ns='$'${ns_name} cleanup_ns "${!ns_name}"
cleanup_ns "$ns"
ns_exist=true
fi fi
if ! ip netns add "$ns"; then if ! ip netns add "${!ns_name}"; then
echo "Failed to create namespace $ns_name" echo "Failed to create namespace $ns_name"
cleanup_ns "${ns_list[@]}" cleanup_ns "${ns_list[@]}"
return $ksft_skip return $ksft_skip
fi fi
ip -n "$ns" link set lo up ip -n "${!ns_name}" link set lo up
! $ns_exist && ns_list+=("$ns") ns_list+=("${!ns_name}")
done done
NS_LIST+=("${ns_list[@]}") NS_LIST+=("${ns_list[@]}")
} }
......
#! /bin/bash #! /bin/bash
# SPDX-License-Identifier: GPL-2.0 # SPDX-License-Identifier: GPL-2.0
. "$(dirname "${0}")/../lib.sh"
. "$(dirname "${0}")/../net_helper.sh"
readonly KSFT_PASS=0 readonly KSFT_PASS=0
readonly KSFT_FAIL=1 readonly KSFT_FAIL=1
readonly KSFT_SKIP=4 readonly KSFT_SKIP=4
...@@ -361,20 +364,7 @@ mptcp_lib_check_transfer() { ...@@ -361,20 +364,7 @@ mptcp_lib_check_transfer() {
# $1: ns, $2: port # $1: ns, $2: port
mptcp_lib_wait_local_port_listen() { mptcp_lib_wait_local_port_listen() {
local listener_ns="${1}" wait_local_port_listen "${@}" "tcp"
local port="${2}"
local port_hex
port_hex="$(printf "%04X" "${port}")"
local _
for _ in $(seq 10); do
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}" &&
break
sleep 0.1
done
} }
mptcp_lib_check_output() { mptcp_lib_check_output() {
...@@ -438,17 +428,13 @@ mptcp_lib_check_tools() { ...@@ -438,17 +428,13 @@ mptcp_lib_check_tools() {
} }
mptcp_lib_ns_init() { mptcp_lib_ns_init() {
local sec rndh if ! setup_ns ${@}; then
mptcp_lib_pr_fail "Failed to setup namespace ${@}"
sec=$(date +%s) exit ${KSFT_FAIL}
rndh=$(printf %x "${sec}")-$(mktemp -u XXXXXX) fi
local netns local netns
for netns in "${@}"; do for netns in "${@}"; do
eval "${netns}=${netns}-${rndh}"
ip netns add "${!netns}" || exit ${KSFT_SKIP}
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
ip netns exec "${!netns}" sysctl -q net.ipv4.conf.all.rp_filter=0 ip netns exec "${!netns}" sysctl -q net.ipv4.conf.all.rp_filter=0
ip netns exec "${!netns}" sysctl -q net.ipv4.conf.default.rp_filter=0 ip netns exec "${!netns}" sysctl -q net.ipv4.conf.default.rp_filter=0
...@@ -456,9 +442,10 @@ mptcp_lib_ns_init() { ...@@ -456,9 +442,10 @@ mptcp_lib_ns_init() {
} }
mptcp_lib_ns_exit() { mptcp_lib_ns_exit() {
cleanup_ns "${@}"
local netns local netns
for netns in "${@}"; do for netns in "${@}"; do
ip netns del "${netns}"
rm -f /tmp/"${netns}".{nstat,out} rm -f /tmp/"${netns}".{nstat,out}
done done
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment