xref: /linux/tools/testing/selftests/net/drop_monitor_tests.sh (revision 0e59523b440c90ed1899476f32ad4e949282f43e)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3
4# This test is for checking drop monitor functionality.
5source lib.sh
6ret=0
7
8# all tests in this script. Can be overridden with -t option
9TESTS="
10	sw_drops
11	hw_drops
12"
13
14NETDEVSIM_PATH=/sys/bus/netdevsim/
15DEV_ADDR=1337
16DEV=netdevsim${DEV_ADDR}
17DEVLINK_DEV=netdevsim/${DEV}
18
19log_test()
20{
21	log_test_expected "$1" "$2" "$3"
22}
23
24setup()
25{
26	modprobe netdevsim &> /dev/null
27
28	set -e
29	setup_ns NS1
30	$IP link add dummy10 up type dummy
31
32	$NS_EXEC echo "$DEV_ADDR 1" > ${NETDEVSIM_PATH}/new_device
33	udevadm settle
34	local netdev=$($NS_EXEC ls ${NETDEVSIM_PATH}/devices/${DEV}/net/)
35	$IP link set dev $netdev up
36
37	set +e
38}
39
40cleanup()
41{
42	$NS_EXEC echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device
43	cleanup_ns ${NS1}
44}
45
46sw_drops_test()
47{
48	echo
49	echo "Software drops test"
50
51	setup
52
53	local dir=$(mktemp -d)
54
55	$TC qdisc add dev dummy10 clsact
56	$TC filter add dev dummy10 egress pref 1 handle 101 proto ip \
57		flower dst_ip 192.0.2.10 action drop
58
59	$NS_EXEC mausezahn dummy10 -a 00:11:22:33:44:55 -b 00:aa:bb:cc:dd:ee \
60		-A 192.0.2.1 -B 192.0.2.10 -t udp sp=12345,dp=54321 -c 0 -q \
61		-d 100msec &
62	timeout 5 dwdump -o sw -w ${dir}/packets.pcap
63	(( $(tshark -r ${dir}/packets.pcap \
64		-Y 'ip.dst == 192.0.2.10' 2> /dev/null | wc -l) != 0))
65	log_test $? 0 "Capturing active software drops"
66
67	rm ${dir}/packets.pcap
68
69	kill_process %%
70	timeout 5 dwdump -o sw -w ${dir}/packets.pcap
71	(( $(tshark -r ${dir}/packets.pcap \
72		-Y 'ip.dst == 192.0.2.10' 2> /dev/null | wc -l) == 0))
73	log_test $? 0 "Capturing inactive software drops"
74
75	rm -r $dir
76
77	cleanup
78}
79
80hw_drops_test()
81{
82	echo
83	echo "Hardware drops test"
84
85	setup
86
87	local dir=$(mktemp -d)
88
89	$DEVLINK trap set $DEVLINK_DEV trap blackhole_route action trap
90	timeout 5 dwdump -o hw -w ${dir}/packets.pcap
91	(( $(tshark -r ${dir}/packets.pcap \
92		-Y 'net_dm.hw_trap_name== blackhole_route' 2> /dev/null \
93		| wc -l) != 0))
94	log_test $? 0 "Capturing active hardware drops"
95
96	rm ${dir}/packets.pcap
97
98	$DEVLINK trap set $DEVLINK_DEV trap blackhole_route action drop
99	timeout 5 dwdump -o hw -w ${dir}/packets.pcap
100	(( $(tshark -r ${dir}/packets.pcap \
101		-Y 'net_dm.hw_trap_name== blackhole_route' 2> /dev/null \
102		| wc -l) == 0))
103	log_test $? 0 "Capturing inactive hardware drops"
104
105	rm -r $dir
106
107	cleanup
108}
109
110################################################################################
111# usage
112
113usage()
114{
115	cat <<EOF
116usage: ${0##*/} OPTS
117
118        -t <test>   Test(s) to run (default: all)
119                    (options: $TESTS)
120EOF
121}
122
123################################################################################
124# main
125
126while getopts ":t:h" opt; do
127	case $opt in
128		t) TESTS=$OPTARG;;
129		h) usage; exit 0;;
130		*) usage; exit 1;;
131	esac
132done
133
134if [ "$(id -u)" -ne 0 ];then
135	echo "SKIP: Need root privileges"
136	exit $ksft_skip;
137fi
138
139if [ ! -x "$(command -v ip)" ]; then
140	echo "SKIP: Could not run test without ip tool"
141	exit $ksft_skip
142fi
143
144if [ ! -x "$(command -v devlink)" ]; then
145	echo "SKIP: Could not run test without devlink tool"
146	exit $ksft_skip
147fi
148
149if [ ! -x "$(command -v tshark)" ]; then
150	echo "SKIP: Could not run test without tshark tool"
151	exit $ksft_skip
152fi
153
154if [ ! -x "$(command -v dwdump)" ]; then
155	echo "SKIP: Could not run test without dwdump tool"
156	exit $ksft_skip
157fi
158
159if [ ! -x "$(command -v udevadm)" ]; then
160	echo "SKIP: Could not run test without udevadm tool"
161	exit $ksft_skip
162fi
163
164if [ ! -x "$(command -v timeout)" ]; then
165	echo "SKIP: Could not run test without timeout tool"
166	exit $ksft_skip
167fi
168
169if [ ! -x "$(command -v mausezahn)" ]; then
170	echo "SKIP: Could not run test without mausezahn tool"
171	exit $ksft_skip
172fi
173
174tshark -G fields 2> /dev/null | grep -q net_dm
175if [ $? -ne 0 ]; then
176	echo "SKIP: tshark too old, missing net_dm dissector"
177	exit $ksft_skip
178fi
179
180# create netns first so we can get the namespace name
181setup_ns NS1
182cleanup &> /dev/null
183trap cleanup EXIT
184
185IP="ip -netns ${NS1}"
186TC="tc -netns ${NS1}"
187DEVLINK="devlink -N ${NS1}"
188NS_EXEC="ip netns exec ${NS1}"
189
190for t in $TESTS
191do
192	case $t in
193	sw_drops|sw)			sw_drops_test;;
194	hw_drops|hw)			hw_drops_test;;
195
196	help) echo "Test names: $TESTS"; exit 0;;
197	esac
198done
199
200if [ "$TESTS" != "none" ]; then
201	printf "\nTests passed: %3d\n" ${nsuccess}
202	printf "Tests failed: %3d\n"   ${nfail}
203fi
204
205exit $ret
206