Commit d4cea2ca authored by Jakub Kicinski's avatar Jakub Kicinski
Browse files

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: default avatarJakub Kicinski <kuba@kernel.org>
parents bfc65070 1af3bc91
Loading
Loading
Loading
Loading
+30 −21
Original line number Diff line number Diff line
@@ -125,28 +125,36 @@ slowwait_for_counter()
	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()
{
	local ns=""
	local errexit=0
	local ret=0

	# disable errexit temporary
	if [[ $- =~ "e" ]]; then
		errexit=1
		set +e
	fi

	for ns in "$@"; do
		[ -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
			echo "Warn: Failed to remove namespace $ns"
			ret=1
		else
			remove_ns_list "${ns}"
		fi
	done

	[ $errexit -eq 1 ] && set -e
	return $ret
}

@@ -159,29 +167,30 @@ cleanup_all_ns()
# setup_ns local remote
setup_ns()
{
	local ns=""
	local ns_name=""
	local ns_list=()
	local ns_exist=
	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
		if unset ${ns_name} 2> /dev/null; then
			ns="${ns_name,,}-$(mktemp -u XXXXXX)"
			eval readonly ${ns_name}="$ns"
			ns_exist=false
		if [ -z "${!ns_name}" ]; then
			eval "${ns_name}=${ns_name,,}-$(mktemp -u XXXXXX)"
		else
			eval ns='$'${ns_name}
			cleanup_ns "$ns"
			ns_exist=true
			cleanup_ns "${!ns_name}"
		fi

		if ! ip netns add "$ns"; then
		if ! ip netns add "${!ns_name}"; then
			echo "Failed to create namespace $ns_name"
			cleanup_ns "${ns_list[@]}"
			return $ksft_skip
		fi
		ip -n "$ns" link set lo up
		! $ns_exist && ns_list+=("$ns")
		ip -n "${!ns_name}" link set lo up
		ns_list+=("${!ns_name}")
	done
	NS_LIST+=("${ns_list[@]}")
}
+10 −23
Original line number Diff line number Diff line
#! /bin/bash
# SPDX-License-Identifier: GPL-2.0

. "$(dirname "${0}")/../lib.sh"
. "$(dirname "${0}")/../net_helper.sh"

readonly KSFT_PASS=0
readonly KSFT_FAIL=1
readonly KSFT_SKIP=4
@@ -361,20 +364,7 @@ mptcp_lib_check_transfer() {

# $1: ns, $2: port
mptcp_lib_wait_local_port_listen() {
	local listener_ns="${1}"
	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
	wait_local_port_listen "${@}" "tcp"
}

mptcp_lib_check_output() {
@@ -438,17 +428,13 @@ mptcp_lib_check_tools() {
}

mptcp_lib_ns_init() {
	local sec rndh

	sec=$(date +%s)
	rndh=$(printf %x "${sec}")-$(mktemp -u XXXXXX)
	if ! setup_ns ${@}; then
		mptcp_lib_pr_fail "Failed to setup namespace ${@}"
		exit ${KSFT_FAIL}
	fi

	local netns
	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.ipv4.conf.all.rp_filter=0
		ip netns exec "${!netns}" sysctl -q net.ipv4.conf.default.rp_filter=0
@@ -456,9 +442,10 @@ mptcp_lib_ns_init() {
}

mptcp_lib_ns_exit() {
	cleanup_ns "${@}"

	local netns
	for netns in "${@}"; do
		ip netns del "${netns}"
		rm -f /tmp/"${netns}".{nstat,out}
	done
}