From 0cc494b6d55c7d4cdd7567f20c58c6b9cb8792b4 Mon Sep 17 00:00:00 2001 From: Geliang Tang Date: Fri, 24 May 2024 16:42:30 +0800 Subject: [PATCH] use setup_ns Signed-off-by: Geliang Tang --- tools/testing/selftests/net/big_tcp.sh | 16 +++++++-------- .../selftests/net/io_uring_zerocopy_tx.sh | 14 ++++++------- .../testing/selftests/net/rps_default_mask.sh | 9 +++++---- tools/testing/selftests/net/so_txtime.sh | 18 ++++++----------- .../selftests/net/srv6_end_flavors_test.sh | 20 +++++++++++-------- .../net/srv6_end_x_next_csid_l3vpn_test.sh | 11 ++++++---- 6 files changed, 43 insertions(+), 45 deletions(-) diff --git a/tools/testing/selftests/net/big_tcp.sh b/tools/testing/selftests/net/big_tcp.sh index 2db9d15cd45fe..6a73c7a039a33 100755 --- a/tools/testing/selftests/net/big_tcp.sh +++ b/tools/testing/selftests/net/big_tcp.sh @@ -4,15 +4,17 @@ # Testing For IPv4 and IPv6 BIG TCP. # TOPO: CLIENT_NS (link0)<--->(link1) ROUTER_NS (link2)<--->(link3) SERVER_NS -CLIENT_NS=$(mktemp -u client-XXXXXXXX) +source lib.sh + +CLIENT_NS="" CLIENT_IP4="198.51.100.1" CLIENT_IP6="2001:db8:1::1" -SERVER_NS=$(mktemp -u server-XXXXXXXX) +SERVER_NS="" SERVER_IP4="203.0.113.1" SERVER_IP6="2001:db8:2::1" -ROUTER_NS=$(mktemp -u router-XXXXXXXX) +ROUTER_NS="" SERVER_GW4="203.0.113.2" CLIENT_GW4="198.51.100.2" SERVER_GW6="2001:db8:2::2" @@ -25,9 +27,7 @@ CHK_SIZE=65535 ksft_skip=4 setup() { - ip netns add $CLIENT_NS - ip netns add $SERVER_NS - ip netns add $ROUTER_NS + setup_ns CLIENT_NS SERVER_NS ROUTER_NS ip -net $ROUTER_NS link add link1 type veth peer name link0 netns $CLIENT_NS ip -net $ROUTER_NS link add link2 type veth peer name link3 netns $SERVER_NS @@ -83,9 +83,7 @@ cleanup() { ip net exec $SERVER_NS pkill netserver ip -net $ROUTER_NS link del link1 ip -net $ROUTER_NS link del link2 - ip netns del "$CLIENT_NS" - ip netns del "$SERVER_NS" - ip netns del "$ROUTER_NS" + cleanup_all_ns } start_counter() { diff --git a/tools/testing/selftests/net/io_uring_zerocopy_tx.sh b/tools/testing/selftests/net/io_uring_zerocopy_tx.sh index 123439545013d..050fb8df6c2ab 100755 --- a/tools/testing/selftests/net/io_uring_zerocopy_tx.sh +++ b/tools/testing/selftests/net/io_uring_zerocopy_tx.sh @@ -3,6 +3,8 @@ # Send data between two processes across namespaces # Run twice: once without and once with zerocopy +source lib.sh + set -e readonly DEV="veth0" @@ -10,10 +12,8 @@ readonly DEV_MTU=65535 readonly BIN_TX="./io_uring_zerocopy_tx" readonly BIN_RX="./msg_zerocopy" -readonly RAND="$(mktemp -u XXXXXX)" -readonly NSPREFIX="ns-${RAND}" -readonly NS1="${NSPREFIX}1" -readonly NS2="${NSPREFIX}2" +NS1="" +NS2="" readonly SADDR4='192.168.1.1' readonly DADDR4='192.168.1.2' @@ -78,15 +78,13 @@ esac # Start of state changes: install cleanup handler cleanup() { - ip netns del "${NS2}" - ip netns del "${NS1}" + cleanup_ns "${NS1}" "${NS2}" } trap cleanup EXIT # Create virtual ethernet pair between network namespaces -ip netns add "${NS1}" -ip netns add "${NS2}" +setup_ns NS1 NS2 # Configure system settings ip netns exec "${NS1}" sysctl -w -q "${path_sysctl_mem}=1000000" diff --git a/tools/testing/selftests/net/rps_default_mask.sh b/tools/testing/selftests/net/rps_default_mask.sh index 4287a85298907..69a8024192cf2 100755 --- a/tools/testing/selftests/net/rps_default_mask.sh +++ b/tools/testing/selftests/net/rps_default_mask.sh @@ -1,6 +1,8 @@ #!/bin/bash # SPDX-License-Identifier: GPL-2.0 +source lib.sh + readonly ksft_skip=4 readonly cpus=$(nproc) ret=0 @@ -10,16 +12,15 @@ ret=0 readonly INITIAL_RPS_DEFAULT_MASK=$(cat /proc/sys/net/core/rps_default_mask) readonly TAG="$(mktemp -u XXXXXX)" readonly VETH="veth${TAG}" -readonly NETNS="ns-${TAG}" +NETNS="" setup() { - ip netns add "${NETNS}" - ip -netns "${NETNS}" link set lo up + setup_ns NETNS } cleanup() { echo $INITIAL_RPS_DEFAULT_MASK > /proc/sys/net/core/rps_default_mask - ip netns del $NETNS + cleanup_all_ns } chk_rps() { diff --git a/tools/testing/selftests/net/so_txtime.sh b/tools/testing/selftests/net/so_txtime.sh index 5e861ad32a42e..b1fc8179fa050 100755 --- a/tools/testing/selftests/net/so_txtime.sh +++ b/tools/testing/selftests/net/so_txtime.sh @@ -3,32 +3,26 @@ # # Regression tests for the SO_TXTIME interface +source lib.sh + set -e readonly ksft_skip=4 readonly DEV="veth0" readonly BIN="./so_txtime" -readonly RAND="$(mktemp -u XXXXXX)" -readonly NSPREFIX="ns-${RAND}" -readonly NS1="${NSPREFIX}1" -readonly NS2="${NSPREFIX}2" +NS1="" +NS2="" readonly SADDR4='192.168.1.1' readonly DADDR4='192.168.1.2' readonly SADDR6='fd::1' readonly DADDR6='fd::2' -cleanup() { - ip netns del "${NS2}" - ip netns del "${NS1}" -} - -trap cleanup EXIT +trap cleanup_all_ns EXIT # Create virtual ethernet pair between network namespaces -ip netns add "${NS1}" -ip netns add "${NS2}" +setup_ns NS1 NS2 ip link add "${DEV}" netns "${NS1}" type veth \ peer name "${DEV}" netns "${NS2}" diff --git a/tools/testing/selftests/net/srv6_end_flavors_test.sh b/tools/testing/selftests/net/srv6_end_flavors_test.sh index 50563443a4adc..6e6f9ab039968 100755 --- a/tools/testing/selftests/net/srv6_end_flavors_test.sh +++ b/tools/testing/selftests/net/srv6_end_flavors_test.sh @@ -194,6 +194,8 @@ # after the IPv6 header. At this point, the packet with IPv6 DA=cafe::1 is sent # to the destination, i.e. hs-1. +source lib.sh + # Kselftest framework requirement - SKIP code is 4. readonly ksft_skip=4 @@ -778,14 +780,15 @@ test_kernel_supp_or_ksft_skip() { local flavor="$1" local test_netns + local kflv - test_netns="kflv-$(mktemp -u XXXXXXXX)" - - if ! ip netns add "${test_netns}"; then + if ! setup_ns kflv; then echo "SKIP: Cannot set up netns to test kernel support for flavors" exit "${ksft_skip}" fi + test_netns=${kflv} + if ! ip -netns "${test_netns}" link \ add "${DUMMY_DEVNAME}" type dummy; then echo "SKIP: Cannot set up dummy dev to test kernel support for flavors" @@ -811,20 +814,21 @@ test_kernel_supp_or_ksft_skip() exit "${ksft_skip}" fi - ip netns del "${test_netns}" + cleanup_ns "${test_netns}" } test_dummy_dev_or_ksft_skip() { local test_netns + local dummy - test_netns="dummy-$(mktemp -u XXXXXXXX)" - - if ! ip netns add "${test_netns}"; then + if ! setup_ns dummy; then echo "SKIP: Cannot set up netns for testing dummy dev support" exit "${ksft_skip}" fi + test_netns=${dummy} + modprobe dummy &>/dev/null || true if ! ip -netns "${test_netns}" link \ add "${DUMMY_DEVNAME}" type dummy; then @@ -834,7 +838,7 @@ test_dummy_dev_or_ksft_skip() exit "${ksft_skip}" fi - ip netns del "${test_netns}" + cleanup_ns "${test_netns}" } if [ "$(id -u)" -ne 0 ]; then diff --git a/tools/testing/selftests/net/srv6_end_x_next_csid_l3vpn_test.sh b/tools/testing/selftests/net/srv6_end_x_next_csid_l3vpn_test.sh index c79cb8ede17f4..1e753bdb3d129 100755 --- a/tools/testing/selftests/net/srv6_end_x_next_csid_l3vpn_test.sh +++ b/tools/testing/selftests/net/srv6_end_x_next_csid_l3vpn_test.sh @@ -287,6 +287,8 @@ # packet using the SRv6 End.DT46 behavior (associated with the SID fcff:1::d46) # and sends it to the host hs-1. +source lib.sh + # Kselftest framework requirement - SKIP code is 4. readonly ksft_skip=4 @@ -1153,14 +1155,15 @@ test_iproute2_supp_or_ksft_skip() test_dummy_dev_or_ksft_skip() { local test_netns + local dummy="" - test_netns="dummy-$(mktemp -u XXXXXXXX)" - - if ! ip netns add "${test_netns}"; then + if ! setup_ns dummy; then echo "SKIP: Cannot set up netns for testing dummy dev support" exit "${ksft_skip}" fi + test_netns=${dummy} + modprobe dummy &>/dev/null || true if ! ip -netns "${test_netns}" link \ add "${DUMMY_DEVNAME}" type dummy; then @@ -1170,7 +1173,7 @@ test_dummy_dev_or_ksft_skip() exit "${ksft_skip}" fi - ip netns del "${test_netns}" + cleanup_ns "${test_netns}" } test_vrf_or_ksft_skip()