Skip to content

Commit f8a2d2f

Browse files
Geliang Tangkuba-moo
authored andcommitted
selftests: net: lib: remove 'ns' var in setup_ns
The helper setup_ns() doesn't work when a net namespace named "ns" is passed to it. 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, we could rename the variable, but it sounds better to drop it, as we can resolve the name using the variable passed in argument instead. The other local variables -- "ns_list" and "ns_name" -- are more unlikely to conflict with existing global variables. They don't seem to be currently used in any other net selftests. Co-developed-by: Matthieu Baerts (NGI0) <[email protected]> Signed-off-by: Matthieu Baerts (NGI0) <[email protected]> Signed-off-by: Geliang Tang <[email protected]> Signed-off-by: Matthieu Baerts (NGI0) <[email protected]> Link: https://lore.kernel.org/r/20240607-upstream-net-next-20240607-selftests-mptcp-net-lib-v1-4-e36986faac94@kernel.org Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 577db6b commit f8a2d2f

File tree

1 file changed

+12
-8
lines changed
  • tools/testing/selftests/net

1 file changed

+12
-8
lines changed

tools/testing/selftests/net/lib.sh

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,26 +167,30 @@ cleanup_all_ns()
167167
# setup_ns local remote
168168
setup_ns()
169169
{
170-
local ns=""
171170
local ns_name=""
172171
local ns_list=()
173172
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+
174180
# Some test may setup/remove same netns multi times
175181
if [ -z "${!ns_name}" ]; then
176-
ns="${ns_name,,}-$(mktemp -u XXXXXX)"
177-
eval "${ns_name}=${ns}"
182+
eval "${ns_name}=${ns_name,,}-$(mktemp -u XXXXXX)"
178183
else
179-
ns="${!ns_name}"
180-
cleanup_ns "$ns"
184+
cleanup_ns "${!ns_name}"
181185
fi
182186

183-
if ! ip netns add "$ns"; then
187+
if ! ip netns add "${!ns_name}"; then
184188
echo "Failed to create namespace $ns_name"
185189
cleanup_ns "${ns_list[@]}"
186190
return $ksft_skip
187191
fi
188-
ip -n "$ns" link set lo up
189-
ns_list+=("$ns")
192+
ip -n "${!ns_name}" link set lo up
193+
ns_list+=("${!ns_name}")
190194
done
191195
NS_LIST+=("${ns_list[@]}")
192196
}

0 commit comments

Comments
 (0)