Skip to content

Commit ea63ac1

Browse files
liuhangbinPaolo Abeni
authored andcommitted
selftests/net: use tc rule to filter the na packet
Test arp_ndisc_untracked_subnets use tcpdump to filter the unsolicited and untracked na messages. It set -e before calling tcpdump. But if tcpdump filters 0 packet, it will return none zero, and cause the script to exit. Instead of using slow tcpdump to capture packets, let's using tc rule to filter out the na message. At the same time, fix function setup_v6 which only needs one parameter. Move all the related helpers from forwarding lib.sh to net lib.sh. Fixes: 0ea7b0a ("selftests: net: arp_ndisc_untracked_subnets: test for arp_accept and accept_untracked_na") Signed-off-by: Hangbin Liu <[email protected]> Reviewed-by: Simon Horman <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Paolo Abeni <[email protected]>
1 parent efb9f4f commit ea63ac1

File tree

3 files changed

+75
-94
lines changed

3 files changed

+75
-94
lines changed

tools/testing/selftests/net/arp_ndisc_untracked_subnets.sh

Lines changed: 17 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -73,32 +73,33 @@ setup_v6() {
7373
# namespaces. veth0 is veth-router, veth1 is veth-host.
7474
# first, set up the inteface's link to the namespace
7575
# then, set the interface "up"
76-
ip -6 -netns ${ROUTER_NS_V6} link add name ${ROUTER_INTF} \
77-
type veth peer name ${HOST_INTF}
78-
79-
ip -6 -netns ${ROUTER_NS_V6} link set dev ${ROUTER_INTF} up
80-
ip -6 -netns ${ROUTER_NS_V6} link set dev ${HOST_INTF} netns \
81-
${HOST_NS_V6}
76+
ip -n ${ROUTER_NS_V6} link add name ${ROUTER_INTF} \
77+
type veth peer name ${HOST_INTF} netns ${HOST_NS_V6}
8278

83-
ip -6 -netns ${HOST_NS_V6} link set dev ${HOST_INTF} up
84-
ip -6 -netns ${ROUTER_NS_V6} addr add \
85-
${ROUTER_ADDR_V6}/${PREFIX_WIDTH_V6} dev ${ROUTER_INTF} nodad
79+
# Add tc rule to filter out host na message
80+
tc -n ${ROUTER_NS_V6} qdisc add dev ${ROUTER_INTF} clsact
81+
tc -n ${ROUTER_NS_V6} filter add dev ${ROUTER_INTF} \
82+
ingress protocol ipv6 pref 1 handle 101 \
83+
flower src_ip ${HOST_ADDR_V6} ip_proto icmpv6 type 136 skip_hw action pass
8684

8785
HOST_CONF=net.ipv6.conf.${HOST_INTF}
8886
ip netns exec ${HOST_NS_V6} sysctl -qw ${HOST_CONF}.ndisc_notify=1
8987
ip netns exec ${HOST_NS_V6} sysctl -qw ${HOST_CONF}.disable_ipv6=0
90-
ip -6 -netns ${HOST_NS_V6} addr add ${HOST_ADDR_V6}/${PREFIX_WIDTH_V6} \
91-
dev ${HOST_INTF}
92-
9388
ROUTER_CONF=net.ipv6.conf.${ROUTER_INTF}
94-
9589
ip netns exec ${ROUTER_NS_V6} sysctl -w \
9690
${ROUTER_CONF}.forwarding=1 >/dev/null 2>&1
9791
ip netns exec ${ROUTER_NS_V6} sysctl -w \
9892
${ROUTER_CONF}.drop_unsolicited_na=0 >/dev/null 2>&1
9993
ip netns exec ${ROUTER_NS_V6} sysctl -w \
10094
${ROUTER_CONF}.accept_untracked_na=${accept_untracked_na} \
10195
>/dev/null 2>&1
96+
97+
ip -n ${ROUTER_NS_V6} link set dev ${ROUTER_INTF} up
98+
ip -n ${HOST_NS_V6} link set dev ${HOST_INTF} up
99+
ip -n ${ROUTER_NS_V6} addr add ${ROUTER_ADDR_V6}/${PREFIX_WIDTH_V6} \
100+
dev ${ROUTER_INTF} nodad
101+
ip -n ${HOST_NS_V6} addr add ${HOST_ADDR_V6}/${PREFIX_WIDTH_V6} \
102+
dev ${HOST_INTF}
102103
set +e
103104
}
104105

@@ -162,26 +163,6 @@ arp_test_gratuitous_combinations() {
162163
arp_test_gratuitous 2 1
163164
}
164165

165-
cleanup_tcpdump() {
166-
set -e
167-
[[ ! -z ${tcpdump_stdout} ]] && rm -f ${tcpdump_stdout}
168-
[[ ! -z ${tcpdump_stderr} ]] && rm -f ${tcpdump_stderr}
169-
tcpdump_stdout=
170-
tcpdump_stderr=
171-
set +e
172-
}
173-
174-
start_tcpdump() {
175-
set -e
176-
tcpdump_stdout=`mktemp`
177-
tcpdump_stderr=`mktemp`
178-
ip netns exec ${ROUTER_NS_V6} timeout 15s \
179-
tcpdump --immediate-mode -tpni ${ROUTER_INTF} -c 1 \
180-
"icmp6 && icmp6[0] == 136 && src ${HOST_ADDR_V6}" \
181-
> ${tcpdump_stdout} 2> /dev/null
182-
set +e
183-
}
184-
185166
verify_ndisc() {
186167
local accept_untracked_na=$1
187168
local same_subnet=$2
@@ -222,16 +203,16 @@ ndisc_test_untracked_advertisements() {
222203
HOST_ADDR_V6=2001:db8:abcd:0012::3
223204
fi
224205
fi
225-
setup_v6 $1 $2
226-
start_tcpdump
206+
setup_v6 $1
207+
slowwait_for_counter 15 1 \
208+
tc_rule_handle_stats_get "dev ${ROUTER_INTF} ingress" 101 ".packets" "-n ${ROUTER_NS_V6}"
227209

228210
if verify_ndisc $1 $2; then
229211
printf " TEST: %-60s [ OK ]\n" "${test_msg[*]}"
230212
else
231213
printf " TEST: %-60s [FAIL]\n" "${test_msg[*]}"
232214
fi
233215

234-
cleanup_tcpdump
235216
cleanup_v6
236217
set +e
237218
}

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

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -129,14 +129,6 @@ fi
129129

130130
source "$net_forwarding_dir/../lib.sh"
131131

132-
# timeout in seconds
133-
slowwait()
134-
{
135-
local timeout_sec=$1; shift
136-
137-
loopy_wait "sleep 0.1" "$((timeout_sec * 1000))" "$@"
138-
}
139-
140132
##############################################################################
141133
# Sanity checks
142134

@@ -678,33 +670,6 @@ wait_for_trap()
678670
"$@" | grep -q trap
679671
}
680672

681-
until_counter_is()
682-
{
683-
local expr=$1; shift
684-
local current=$("$@")
685-
686-
echo $((current))
687-
((current $expr))
688-
}
689-
690-
busywait_for_counter()
691-
{
692-
local timeout=$1; shift
693-
local delta=$1; shift
694-
695-
local base=$("$@")
696-
busywait "$timeout" until_counter_is ">= $((base + delta))" "$@"
697-
}
698-
699-
slowwait_for_counter()
700-
{
701-
local timeout=$1; shift
702-
local delta=$1; shift
703-
704-
local base=$("$@")
705-
slowwait "$timeout" until_counter_is ">= $((base + delta))" "$@"
706-
}
707-
708673
setup_wait_dev()
709674
{
710675
local dev=$1; shift
@@ -1023,29 +988,6 @@ link_stats_rx_errors_get()
1023988
link_stats_get $1 rx errors
1024989
}
1025990

1026-
tc_rule_stats_get()
1027-
{
1028-
local dev=$1; shift
1029-
local pref=$1; shift
1030-
local dir=$1; shift
1031-
local selector=${1:-.packets}; shift
1032-
1033-
tc -j -s filter show dev $dev ${dir:-ingress} pref $pref \
1034-
| jq ".[1].options.actions[].stats$selector"
1035-
}
1036-
1037-
tc_rule_handle_stats_get()
1038-
{
1039-
local id=$1; shift
1040-
local handle=$1; shift
1041-
local selector=${1:-.packets}; shift
1042-
local netns=${1:-""}; shift
1043-
1044-
tc $netns -j -s filter show $id \
1045-
| jq ".[] | select(.options.handle == $handle) | \
1046-
.options.actions[0].stats$selector"
1047-
}
1048-
1049991
ethtool_stats_get()
1050992
{
1051993
local dev=$1; shift

tools/testing/selftests/net/lib.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,41 @@ busywait()
9191
loopy_wait : "$timeout_ms" "$@"
9292
}
9393

94+
# timeout in seconds
95+
slowwait()
96+
{
97+
local timeout_sec=$1; shift
98+
99+
loopy_wait "sleep 0.1" "$((timeout_sec * 1000))" "$@"
100+
}
101+
102+
until_counter_is()
103+
{
104+
local expr=$1; shift
105+
local current=$("$@")
106+
107+
echo $((current))
108+
((current $expr))
109+
}
110+
111+
busywait_for_counter()
112+
{
113+
local timeout=$1; shift
114+
local delta=$1; shift
115+
116+
local base=$("$@")
117+
busywait "$timeout" until_counter_is ">= $((base + delta))" "$@"
118+
}
119+
120+
slowwait_for_counter()
121+
{
122+
local timeout=$1; shift
123+
local delta=$1; shift
124+
125+
local base=$("$@")
126+
slowwait "$timeout" until_counter_is ">= $((base + delta))" "$@"
127+
}
128+
94129
cleanup_ns()
95130
{
96131
local ns=""
@@ -150,3 +185,26 @@ setup_ns()
150185
done
151186
NS_LIST="$NS_LIST $ns_list"
152187
}
188+
189+
tc_rule_stats_get()
190+
{
191+
local dev=$1; shift
192+
local pref=$1; shift
193+
local dir=$1; shift
194+
local selector=${1:-.packets}; shift
195+
196+
tc -j -s filter show dev $dev ${dir:-ingress} pref $pref \
197+
| jq ".[1].options.actions[].stats$selector"
198+
}
199+
200+
tc_rule_handle_stats_get()
201+
{
202+
local id=$1; shift
203+
local handle=$1; shift
204+
local selector=${1:-.packets}; shift
205+
local netns=${1:-""}; shift
206+
207+
tc $netns -j -s filter show $id \
208+
| jq ".[] | select(.options.handle == $handle) | \
209+
.options.actions[0].stats$selector"
210+
}

0 commit comments

Comments
 (0)