Skip to contentSkip to conversation

use helpers in lib.sh and net_helpers.sh

PATCH Superseded Matthieu Baerts (NGI0) 2 participants 11 messages May 24, 2024

Subsystems (3)

v5 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Matthieu Baerts (NGI0)

author maintainer
#0 lore raw

v5 cover use helpers in lib.sh and net_helpers.sh

(This is a new version for the series created by Geliang. @Geliang: I
hope that's OK for you like that, it was easier to describing the modifications by email. I validated my changes by running MPTCP selftests only)

This set uses the public helpers defined in net selftests to replace helpers in mptcp_lib.sh.

v5: (Matt)
- New patches: 1-6: some unrelated fixes, some modifications to avoid adding workaround on our side (changing variable names, trap, etc.)
- "rename ns in setup_ns": drop the modifications in cleanup_ns, exit in case of error, and updated commit message.
- Drop patches 2, 4, 5/6 from v4.

v4:
- two more fixes added
- exit when setup_ns fails in mptcp_lib_ns_init

v3:
- address Matt's comments in v2 (thanks, it's very useful.)
- don't rename ns in diag.sh, but in lib.sh
- ns in get_counter needs to be renamed too
- add a ns_name valid check in setup_ns
- drop $NS_LIST in mptcp_lib_ns_init
- don't drop mptcp_lib_ns_init, use cleanup_ns in it
- don't use cleanup_all_ns, still use mptcp_lib_ns_init

v2:
- only patch 3 updated, mptcp_join.sh, move cleanup_all_ns from cleanup_partial to cleanup, setup_ns will delete existing namespaces automatically.
- update commit logs.

---
Geliang Tang (3):
selftests: net: lib: rename ns in setup_ns
selftests: mptcp: lib: use setup/cleanup_ns helpers
selftests: mptcp: lib: use wait_local_port_listen helper

Matthieu Baerts (NGI0) (6):
selftests: net: lib: set 'i' as local
selftests: net: lib: support errexit with busywait
selftests: net: lib: avoid error removing empty netns name
selftests: net: lib: ignore possible error
selftests: net: lib: remove ns from list after clean-up
selftests: net: lib: do not set ns var as readonly

 tools/testing/selftests/net/lib.sh             | 73 +++++++++++++++-----------
 tools/testing/selftests/net/mptcp/mptcp_lib.sh | 33 ++++--------
 2 files changed, 52 insertions(+), 54 deletions(-)

---
base-commit: 958cd231864867eb931c4ed55db7945fb4e5af43
change-id: 20240524-selftests-net-lib-fixes-5a70b8d3b828

Best regards,

Signature · 1 line
Matthieu Baerts (NGI0) <[email protected]>
author maintainer
#1 lore raw

PATCH v5 1/9 selftests: net: lib: set 'i' as local

Without this, the 'i' variable declared before could be overridden by accident, e.g.

  for i in "${@}"; do
      __ksft_status_merge "${i}"  ## 'i' has been modified
      foo "${i}"                  ## using 'i' with an unexpected value
  done

After a quick look, it looks like 'i' is currently not used after having been modified in __ksft_status_merge(), but still, better be safe than sorry. I saw this while modifying the same file, not because I suspected an issue somewhere.

Fixes 596c8819cb78 ("selftests: forwarding: Have RET track kselftest framework constants")
---
 tools/testing/selftests/net/lib.sh | 1 +
 1 file changed, 1 insertion(+)
tools/testing/selftests/net/lib.sh +1 -0
index edc030e81a46..be80f7b1f9d7 100644
@@ -27,6 +27,7 @@ __ksft_status_merge()
 	local -A weights
 	local weight=0
 
+	local i
 	for i in "$@"; do
 		weights[$i]=$((weight++))
 	done
Signature · 1 line
2.43.0
author maintainer
#2 lore raw

PATCH v5 2/9 selftests: net: lib: support errexit with busywait

In reply to Matthieu Baerts (NGI0)

If errexit is enabled ('set -e'), loopy_wait -- or busywait and others using it -- will stop after the first failure.

Note that if the returned status of loopy_wait is checked, and even if errexit is enabled, Bash will not stop at the first error.

Fixes 25ae948b4478 ("selftests/net: add lib.sh")
---
 tools/testing/selftests/net/lib.sh | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
tools/testing/selftests/net/lib.sh +1 -3
index be80f7b1f9d7..2fbc01003b0f 100644
@@ -68,9 +68,7 @@ loopy_wait()
 	while true
 	do
 		local out
-		out=$("$@")
-		local ret=$?
-		if ((!ret)); then
+		if out=$("$@"); then
 			echo -n "$out"
 			return 0
 		fi
Signature · 1 line
2.43.0
author maintainer
#3 lore raw

PATCH v5 3/9 selftests: net: lib: avoid error removing empty netns name

In reply to Matthieu Baerts (NGI0)

If there is an error to create the first netns with 'setup_ns()', 'cleanup_ns()' will be called with an empty string as first parameter.

The consequences is that 'cleanup_ns()' will try to delete an invalid netns, and wait 20 seconds if the netns list is empty.

Instead of just checking if the name is not empty, convert the string separated by spaces to an array. Manipulating the array is cleaner, and calling 'cleanup_ns()' with an empty array will be a no-op.

Fixes 25ae948b4478 ("selftests/net: add lib.sh")
---
 tools/testing/selftests/net/lib.sh | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)
tools/testing/selftests/net/lib.sh +7 -6
index 2fbc01003b0f..9155c914c064 100644
@@ -15,7 +15,7 @@ ksft_xfail=2
 ksft_skip=4
 
 # namespace list created by setup_ns
-NS_LIST=""
+NS_LIST=()
 
 ##############################################################################
 # Helpers
@@ -138,6 +138,7 @@ cleanup_ns()
 	fi
 
 	for ns in "$@"; do
+		[ -z "${ns}" ] && continue
 		ip netns delete "${ns}" &> /dev/null
 		if ! busywait $BUSYWAIT_TIMEOUT ip netns list \| grep -vq "^$ns$" &> /dev/null; then
 			echo "Warn: Failed to remove namespace $ns"
@@ -151,7 +152,7 @@ cleanup_ns()
 
 cleanup_all_ns()
 {
-	cleanup_ns $NS_LIST
+	cleanup_ns "${NS_LIST[@]}"
 }
 
 # setup netns with given names as prefix. e.g
@@ -160,7 +161,7 @@ setup_ns()
 {
 	local ns=""
 	local ns_name=""
-	local ns_list=""
+	local ns_list=()
 	local ns_exist=
 	for ns_name in "$@"; do
 		# Some test may setup/remove same netns multi times
@@ -176,13 +177,13 @@ setup_ns()
 
 		if ! ip netns add "$ns"; then
 			echo "Failed to create namespace $ns_name"
-			cleanup_ns "$ns_list"
+			cleanup_ns "${ns_list[@]}"
 			return $ksft_skip
 		fi
 		ip -n "$ns" link set lo up
-		! $ns_exist && ns_list="$ns_list $ns"
+		! $ns_exist && ns_list+=("$ns")
 	done
-	NS_LIST="$NS_LIST $ns_list"
+	NS_LIST+=("${ns_list[@]}")
 }
 
 tc_rule_stats_get()
Signature · 1 line
2.43.0
author maintainer
#4 lore raw

PATCH v5 4/9 selftests: net: lib: ignore possible error

In reply to Matthieu Baerts (NGI0)

No need to disable errexit temporary, simply ignore the possible error.

---
 tools/testing/selftests/net/lib.sh | 10 +---------
 1 file changed, 1 insertion(+), 9 deletions(-)
tools/testing/selftests/net/lib.sh +1 -9
index 9155c914c064..b2572aff6286 100644
@@ -128,25 +128,17 @@ slowwait_for_counter()
 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
 		fi
 	done
 
-	[ $errexit -eq 1 ] && set -e
 	return $ret
 }
 
Signature · 1 line
2.43.0
author maintainer
#5 lore raw

PATCH v5 5/9 selftests: net: lib: remove ns from list after clean-up

In reply to Matthieu Baerts (NGI0)

Instead of only appending items to the list, remove them when the netns has been deleted.

By doing that, we can make sure 'cleanup_all_ns()' is not trying to remove already deleted netns.

---
 tools/testing/selftests/net/lib.sh | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)
tools/testing/selftests/net/lib.sh +17 -4
index b2572aff6286..c7a8cfb477cc 100644
@@ -125,6 +125,20 @@ 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=""
@@ -136,6 +150,8 @@ cleanup_ns()
 		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
 
@@ -154,17 +170,14 @@ setup_ns()
 	local ns=""
 	local ns_name=""
 	local ns_list=()
-	local ns_exist=
 	for ns_name in "$@"; do
 		# 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
 		else
 			eval ns='$'${ns_name}
 			cleanup_ns "$ns"
-			ns_exist=true
 		fi
 
 		if ! ip netns add "$ns"; then
@@ -173,7 +186,7 @@ setup_ns()
 			return $ksft_skip
 		fi
 		ip -n "$ns" link set lo up
-		! $ns_exist && ns_list+=("$ns")
+		ns_list+=("$ns")
 	done
 	NS_LIST+=("${ns_list[@]}")
 }
Signature · 1 line
2.43.0
author maintainer
#6 lore raw

PATCH v5 6/9 selftests: net: lib: do not set ns var as readonly

In reply to Matthieu Baerts (NGI0)

It sounds good to mark the global netns variable as 'readonly', but Bash doesn't allow the creation of local variable with the same name.

Because it looks like 'readonly' is mainly used here to check if a netns with that name has already been set, it sounds fine to check if the variable with this name has already been set instead. By doing that, we avoid having to modify helpers from MPTCP selftests using the same variable name as the one used to store the created netns name.

While at it, also avoid an unnecessary call to 'eval' to set a local variable.

---
 tools/testing/selftests/net/lib.sh | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
tools/testing/selftests/net/lib.sh +3 -3
index c7a8cfb477cc..114b927fee25 100644
@@ -172,11 +172,11 @@ setup_ns()
 	local ns_list=()
 	for ns_name in "$@"; do
 		# 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 readonly ${ns_name}="$ns"
+			eval "${ns_name}=${ns}"
 		else
-			eval ns='$'${ns_name}
+			ns="${!ns_name}"
 			cleanup_ns "$ns"
 		fi
 
Signature · 1 line
2.43.0
author maintainer
#7 lore raw

PATCH v5 7/9 selftests: net: lib: rename ns in setup_ns

In reply to Matthieu Baerts (NGI0)

From: Geliang Tang <[email protected]>

The helpers setup_ns() doesn't work when a net namespace named "ns" is passed to them.

For example, in net/mptcp/diag.sh, the name of the namespace is "ns". If "setup_ns ns" is used in it, diag.sh fails with errors:

Invalid netns name "./mptcp_connect"
Cannot open network namespace "10000": No such file or directory Cannot open network namespace "10000": No such file or directory

That is because "ns" is also a local variable in setup_ns, and it will not set the value for the global variable that has been giving in argument. To solve this, this patch renames the local variable "ns" as "_ns", which is more unlikely to conflict with existing variables. "_ns" name appears to be unused in the net selftests.

Also a check has been added to setup_ns: if "_ns" is passed, setup_ns() helper will exit with an error.

---
 tools/testing/selftests/net/lib.sh | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)
tools/testing/selftests/net/lib.sh +17 -11
index 114b927fee25..b883289ec4a1 100644
@@ -167,26 +167,32 @@ cleanup_all_ns()
 # setup_ns local remote
 setup_ns()
 {
-	local ns=""
+	local _ns=""
 	local ns_name=""
 	local ns_list=()
 	for ns_name in "$@"; do
-		# Some test may setup/remove same netns multi times
-		if [ -z "${!ns_name}" ]; then
-			ns="${ns_name,,}-$(mktemp -u XXXXXX)"
-			eval "${ns_name}=${ns}"
-		else
-			ns="${!ns_name}"
-			cleanup_ns "$ns"
+		if [ "${ns_name}" = "_ns" ]; then
+			echo "Failed to setup namespace '${ns_name}': invalid name"
+			cleanup_ns "${ns_list[@]}"
+			exit $ksft_fail
 		fi
 
-		if ! ip netns add "$ns"; then
+		# Some test may setup/remove same netns multi times
+		if [ -z "${!ns_name}" ]; then
+			_ns="${ns_name,,}-$(mktemp -u XXXXXX)"
+			eval "${ns_name}=${_ns}"
+		else
+			_ns="${!ns_name}"
+			cleanup_ns "${_ns}"
+		fi
+
+		if ! ip netns add "${_ns}"; then
 			echo "Failed to create namespace $ns_name"
 			cleanup_ns "${ns_list[@]}"
 			return $ksft_skip
 		fi
-		ip -n "$ns" link set lo up
-		ns_list+=("$ns")
+		ip -n "${_ns}" link set lo up
+		ns_list+=("${_ns}")
 	done
 	NS_LIST+=("${ns_list[@]}")
 }
Signature · 1 line
2.43.0
author maintainer
#8 lore raw

PATCH v5 8/9 selftests: mptcp: lib: use setup/cleanup_ns helpers

In reply to Matthieu Baerts (NGI0)

From: Geliang Tang <[email protected]>

This patch includes lib.sh into mptcp_lib.sh, uses setup_ns helper defined in lib.sh to set up namespaces in mptcp_lib_ns_init(), and uses cleanup_ns to delete namespaces in mptcp_lib_ns_exit().

---
 tools/testing/selftests/net/mptcp/mptcp_lib.sh | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_lib.sh +8 -9
index 6ffa9b7a3260..d9e30516dc72 100644
@@ -1,6 +1,8 @@
 #! /bin/bash
 # SPDX-License-Identifier: GPL-2.0
 
+. "$(dirname "${0}")/../lib.sh"
+
 readonly KSFT_PASS=0
 readonly KSFT_FAIL=1
 readonly KSFT_SKIP=4
@@ -438,17 +440,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 +454,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
 }
Signature · 1 line
2.43.0
author maintainer
#9 lore raw

PATCH v5 9/9 selftests: mptcp: lib: use wait_local_port_listen helper

In reply to Matthieu Baerts (NGI0)

From: Geliang Tang <[email protected]>

This patch includes net_helper.sh into mptcp_lib.sh, uses the helper wait_local_port_listen() defined in it to implement the similar mptcp helper. This can drop some duplicate code.

---
 tools/testing/selftests/net/mptcp/mptcp_lib.sh | 16 ++--------------
 1 file changed, 2 insertions(+), 14 deletions(-)
tools/testing/selftests/net/mptcp/mptcp_lib.sh +2 -14
index d9e30516dc72..194c8fc2e55a 100644
@@ -2,6 +2,7 @@
 # SPDX-License-Identifier: GPL-2.0
 
 . "$(dirname "${0}")/../lib.sh"
+. "$(dirname "${0}")/../net_helper.sh"
 
 readonly KSFT_PASS=0
 readonly KSFT_FAIL=1
@@ -363,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() {
Signature · 1 line
2.43.0
#10 lore raw
In reply to Matthieu Baerts (NGI0)

Hi Geliang,

Thank you for your modifications, that's great!

Our CI did some validations and here is its report:

- KVM Validation: normal: Success! ✅
- KVM Validation: debug: Unstable: 1 failed test(s): packetdrill_fastopen 🔴
- KVM Validation: btf (only bpftest_all): Success! ✅
- Task: https://github.com/multipath-tcp/mptcp_net-next/actions/runs/9226376652

Initiator: Patchew Applier
Commits: https://github.com/multipath-tcp/mptcp_net-next/commits/079d74fb7b0d
Patchwork: https://patchwork.kernel.org/project/mptcp/list/?series=855731

If there are some issues, you can reproduce them using the same environment as the one used by the CI thanks to a docker image, e.g.:

    $ cd [kernel source code]
    $ docker run -v "${PWD}:${PWD}:rw" -w "${PWD}" --privileged --rm -it \
        --pull always mptcp/mptcp-upstream-virtme-docker:latest \
        auto-normal

For more details:

https://github.com/multipath-tcp/mptcp-upstream-virtme-docker

Please note that despite all the efforts that have been already done to have a stable tests suite when executed on a public CI like here, it is possible some reported issues are not due to your modifications. Still, do not hesitate to help us improve that ;-)

Cheers,
MPTCP GH Action bot
Bot operated by Matthieu Baerts (NGI0 Core)