Skip to content

Commit 25ae948

Browse files
liuhangbinPaolo Abeni
authored andcommitted
selftests/net: add lib.sh
Add a lib.sh for net selftests. This file can be used to define commonly used variables and functions. Some commonly used functions can be moved from forwarding/lib.sh to this lib file. e.g. busywait(). Add function setup_ns() for user to create unique namespaces with given prefix name. Reviewed-by: Petr Machata <[email protected]> Signed-off-by: Hangbin Liu <[email protected]> Signed-off-by: Paolo Abeni <[email protected]>
1 parent 333f339 commit 25ae948

File tree

3 files changed

+87
-27
lines changed

3 files changed

+87
-27
lines changed

tools/testing/selftests/net/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ TEST_PROGS += ip_local_port_range.sh
5454
TEST_PROGS += rps_default_mask.sh
5555
TEST_PROGS += big_tcp.sh
5656
TEST_PROGS_EXTENDED := in_netns.sh setup_loopback.sh setup_veth.sh
57-
TEST_PROGS_EXTENDED += toeplitz_client.sh toeplitz.sh
57+
TEST_PROGS_EXTENDED += toeplitz_client.sh toeplitz.sh lib.sh
5858
TEST_GEN_FILES = socket nettest
5959
TEST_GEN_FILES += psock_fanout psock_tpacket msg_zerocopy reuseport_addr_any
6060
TEST_GEN_FILES += tcp_mmap tcp_inq psock_snd txring_overwrite

tools/testing/selftests/net/forwarding/lib.sh

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
##############################################################################
55
# Defines
66

7-
# Kselftest framework requirement - SKIP code is 4.
8-
ksft_skip=4
9-
107
# Can be overridden by the configuration file.
118
PING=${PING:=ping}
129
PING6=${PING6:=ping6}
@@ -41,6 +38,7 @@ if [[ -f $relative_path/forwarding.config ]]; then
4138
source "$relative_path/forwarding.config"
4239
fi
4340

41+
source ../lib.sh
4442
##############################################################################
4543
# Sanity checks
4644

@@ -395,29 +393,6 @@ log_info()
395393
echo "INFO: $msg"
396394
}
397395

398-
busywait()
399-
{
400-
local timeout=$1; shift
401-
402-
local start_time="$(date -u +%s%3N)"
403-
while true
404-
do
405-
local out
406-
out=$("$@")
407-
local ret=$?
408-
if ((!ret)); then
409-
echo -n "$out"
410-
return 0
411-
fi
412-
413-
local current_time="$(date -u +%s%3N)"
414-
if ((current_time - start_time > timeout)); then
415-
echo -n "$out"
416-
return 1
417-
fi
418-
done
419-
}
420-
421396
not()
422397
{
423398
"$@"

tools/testing/selftests/net/lib.sh

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#!/bin/bash
2+
# SPDX-License-Identifier: GPL-2.0
3+
4+
##############################################################################
5+
# Defines
6+
7+
# Kselftest framework requirement - SKIP code is 4.
8+
ksft_skip=4
9+
10+
##############################################################################
11+
# Helpers
12+
busywait()
13+
{
14+
local timeout=$1; shift
15+
16+
local start_time="$(date -u +%s%3N)"
17+
while true
18+
do
19+
local out
20+
out=$("$@")
21+
local ret=$?
22+
if ((!ret)); then
23+
echo -n "$out"
24+
return 0
25+
fi
26+
27+
local current_time="$(date -u +%s%3N)"
28+
if ((current_time - start_time > timeout)); then
29+
echo -n "$out"
30+
return 1
31+
fi
32+
done
33+
}
34+
35+
cleanup_ns()
36+
{
37+
local ns=""
38+
local errexit=0
39+
local ret=0
40+
41+
# disable errexit temporary
42+
if [[ $- =~ "e" ]]; then
43+
errexit=1
44+
set +e
45+
fi
46+
47+
for ns in "$@"; do
48+
ip netns delete "${ns}" &> /dev/null
49+
if ! busywait 2 ip netns list \| grep -vq "^$ns$" &> /dev/null; then
50+
echo "Warn: Failed to remove namespace $ns"
51+
ret=1
52+
fi
53+
done
54+
55+
[ $errexit -eq 1 ] && set -e
56+
return $ret
57+
}
58+
59+
# setup netns with given names as prefix. e.g
60+
# setup_ns local remote
61+
setup_ns()
62+
{
63+
local ns=""
64+
local ns_name=""
65+
local ns_list=""
66+
for ns_name in "$@"; do
67+
# Some test may setup/remove same netns multi times
68+
if unset ${ns_name} 2> /dev/null; then
69+
ns="${ns_name,,}-$(mktemp -u XXXXXX)"
70+
eval readonly ${ns_name}="$ns"
71+
else
72+
eval ns='$'${ns_name}
73+
cleanup_ns "$ns"
74+
75+
fi
76+
77+
if ! ip netns add "$ns"; then
78+
echo "Failed to create namespace $ns_name"
79+
cleanup_ns "$ns_list"
80+
return $ksft_skip
81+
fi
82+
ip -n "$ns" link set lo up
83+
ns_list="$ns_list $ns"
84+
done
85+
}

0 commit comments

Comments
 (0)