xref: /linux/tools/testing/selftests/drivers/net/mlxsw/devlink_trap_policer.sh (revision 7f71507851fc7764b36a3221839607d3a45c2025)
1#!/bin/bash
2# SPDX-License-Identifier: GPL-2.0
3#
4# Test devlink-trap policer functionality over mlxsw.
5
6# +---------------------------------+
7# | H1 (vrf)                        |
8# |    + $h1                        |
9# |    | 192.0.2.1/24               |
10# |    |                            |
11# |    |  default via 192.0.2.2     |
12# +----|----------------------------+
13#      |
14# +----|----------------------------------------------------------------------+
15# | SW |                                                                      |
16# |    + $rp1                                                                 |
17# |        192.0.2.2/24                                                       |
18# |                                                                           |
19# |        198.51.100.2/24                                                    |
20# |    + $rp2                                                                 |
21# |    |                                                                      |
22# +----|----------------------------------------------------------------------+
23#      |
24# +----|----------------------------+
25# |    |  default via 198.51.100.2  |
26# |    |                            |
27# |    | 198.51.100.1/24            |
28# |    + $h2                        |
29# | H2 (vrf)                        |
30# +---------------------------------+
31
32lib_dir=$(dirname $0)/../../../net/forwarding
33
34ALL_TESTS="
35	rate_limits_test
36	burst_limits_test
37	rate_test
38	burst_test
39"
40NUM_NETIFS=4
41source $lib_dir/tc_common.sh
42source $lib_dir/lib.sh
43source $lib_dir/devlink_lib.sh
44
45h1_create()
46{
47	simple_if_init $h1 192.0.2.1/24
48	defer simple_if_fini $h1 192.0.2.1/24
49
50	mtu_set $h1 10000
51	defer mtu_restore $h1
52
53	ip -4 route add default vrf v$h1 nexthop via 192.0.2.2
54	defer ip -4 route del default vrf v$h1 nexthop via 192.0.2.2
55}
56
57h2_create()
58{
59	simple_if_init $h2 198.51.100.1/24
60	defer simple_if_fini $h2 198.51.100.1/24
61
62	mtu_set $h2 10000
63	defer mtu_restore $h2
64
65	ip -4 route add default vrf v$h2 nexthop via 198.51.100.2
66	defer ip -4 route del default vrf v$h2 nexthop via 198.51.100.2
67}
68
69router_create()
70{
71	ip link set dev $rp1 up
72	defer ip link set dev $rp1 down
73
74	ip link set dev $rp2 up
75	defer ip link set dev $rp2 down
76
77	__addr_add_del $rp1 add 192.0.2.2/24
78	defer __addr_add_del $rp1 del 192.0.2.2/24
79
80	__addr_add_del $rp2 add 198.51.100.2/24
81	defer __addr_add_del $rp2 del 198.51.100.2/24
82
83	mtu_set $rp1 10000
84	defer mtu_restore $rp1
85
86	mtu_set $rp2 10000
87	defer mtu_restore $rp2
88
89	ip -4 route add blackhole 198.51.100.100
90	defer ip -4 route del blackhole 198.51.100.100
91
92	devlink trap set $DEVLINK_DEV trap blackhole_route action trap
93	defer devlink trap set $DEVLINK_DEV trap blackhole_route action drop
94}
95
96setup_prepare()
97{
98	h1=${NETIFS[p1]}
99	rp1=${NETIFS[p2]}
100
101	rp2=${NETIFS[p3]}
102	h2=${NETIFS[p4]}
103
104	rp1_mac=$(mac_get $rp1)
105
106	# Reload to ensure devlink-trap settings are back to default.
107	defer devlink_reload
108
109	vrf_prepare
110	defer vrf_cleanup
111
112	h1_create
113	h2_create
114
115	router_create
116}
117
118rate_limits_test()
119{
120	RET=0
121
122	devlink trap policer set $DEVLINK_DEV policer 1 rate 0 &> /dev/null
123	check_fail $? "Policer rate was changed to rate lower than limit"
124	devlink trap policer set $DEVLINK_DEV policer 1 \
125		rate 2000000001 &> /dev/null
126	check_fail $? "Policer rate was changed to rate higher than limit"
127
128	devlink trap policer set $DEVLINK_DEV policer 1 rate 1
129	check_err $? "Failed to set policer rate to minimum"
130	devlink trap policer set $DEVLINK_DEV policer 1 rate 2000000000
131	check_err $? "Failed to set policer rate to maximum"
132
133	log_test "Trap policer rate limits"
134}
135
136burst_limits_test()
137{
138	RET=0
139
140	devlink trap policer set $DEVLINK_DEV policer 1 burst 0 &> /dev/null
141	check_fail $? "Policer burst size was changed to 0"
142	devlink trap policer set $DEVLINK_DEV policer 1 burst 17 &> /dev/null
143	check_fail $? "Policer burst size was changed to burst size that is not power of 2"
144	devlink trap policer set $DEVLINK_DEV policer 1 burst 8 &> /dev/null
145	check_fail $? "Policer burst size was changed to burst size lower than limit"
146	devlink trap policer set $DEVLINK_DEV policer 1 \
147		burst $((2**25)) &> /dev/null
148	check_fail $? "Policer burst size was changed to burst size higher than limit"
149
150	devlink trap policer set $DEVLINK_DEV policer 1 burst 16
151	check_err $? "Failed to set policer burst size to minimum"
152	devlink trap policer set $DEVLINK_DEV policer 1 burst $((2**24))
153	check_err $? "Failed to set policer burst size to maximum"
154
155	log_test "Trap policer burst size limits"
156}
157
158trap_rate_get()
159{
160	local t0 t1
161
162	t0=$(devlink_trap_rx_packets_get blackhole_route)
163	sleep 10
164	t1=$(devlink_trap_rx_packets_get blackhole_route)
165
166	echo $(((t1 - t0) / 10))
167}
168
169policer_drop_rate_get()
170{
171	local id=$1; shift
172	local t0 t1
173
174	t0=$(devlink_trap_policer_rx_dropped_get $id)
175	sleep 10
176	t1=$(devlink_trap_policer_rx_dropped_get $id)
177
178	echo $(((t1 - t0) / 10))
179}
180
181__rate_test()
182{
183	local rate pct drop_rate
184	local id=$1; shift
185
186	RET=0
187
188	devlink trap policer set $DEVLINK_DEV policer $id rate 1000 burst 512
189	devlink trap group set $DEVLINK_DEV group l3_drops policer $id
190
191	# Send packets at highest possible rate and make sure they are dropped
192	# by the policer. Make sure measured received rate is about 1000 pps
193	log_info "=== Tx rate: Highest, Policer rate: 1000 pps ==="
194
195	defer_scope_push
196
197	start_traffic $h1 192.0.2.1 198.51.100.100 $rp1_mac
198	defer stop_traffic $!
199
200	sleep 5 # Take measurements when rate is stable
201
202	rate=$(trap_rate_get)
203	pct=$((100 * (rate - 1000) / 1000))
204	((-10 <= pct && pct <= 10))
205	check_err $? "Expected rate 1000 pps, got $rate pps, which is $pct% off. Required accuracy is +-10%"
206	log_info "Expected rate 1000 pps, measured rate $rate pps"
207
208	drop_rate=$(policer_drop_rate_get $id)
209	(( drop_rate > 0 ))
210	check_err $? "Expected non-zero policer drop rate, got 0"
211	log_info "Measured policer drop rate of $drop_rate pps"
212
213	defer_scope_pop
214
215	# Send packets at a rate of 1000 pps and make sure they are not dropped
216	# by the policer
217	log_info "=== Tx rate: 1000 pps, Policer rate: 1000 pps ==="
218
219	defer_scope_push
220
221	start_traffic $h1 192.0.2.1 198.51.100.100 $rp1_mac -d 1msec
222	defer stop_traffic $!
223
224	sleep 5 # Take measurements when rate is stable
225
226	drop_rate=$(policer_drop_rate_get $id)
227	(( drop_rate == 0 ))
228	check_err $? "Expected zero policer drop rate, got a drop rate of $drop_rate pps"
229	log_info "Measured policer drop rate of $drop_rate pps"
230
231	defer_scope_pop
232
233	# Unbind the policer and send packets at highest possible rate. Make
234	# sure they are not dropped by the policer and that the measured
235	# received rate is higher than 1000 pps
236	log_info "=== Tx rate: Highest, Policer rate: No policer ==="
237
238	devlink trap group set $DEVLINK_DEV group l3_drops nopolicer
239
240	defer_scope_push
241
242	start_traffic $h1 192.0.2.1 198.51.100.100 $rp1_mac
243	defer stop_traffic $!
244
245	rate=$(trap_rate_get)
246	(( rate > 1000 ))
247	check_err $? "Expected rate higher than 1000 pps, got $rate pps"
248	log_info "Measured rate $rate pps"
249
250	drop_rate=$(policer_drop_rate_get $id)
251	(( drop_rate == 0 ))
252	check_err $? "Expected zero policer drop rate, got a drop rate of $drop_rate pps"
253	log_info "Measured policer drop rate of $drop_rate pps"
254
255	defer_scope_pop
256
257	log_test "Trap policer rate"
258}
259
260rate_test()
261{
262	local last_policer=$(devlink -j -p trap policer show |
263			     jq '[.[]["'$DEVLINK_DEV'"][].policer] | max')
264
265	log_info "Running rate test for policer 1"
266	__rate_test 1
267
268	log_info "Running rate test for policer $((last_policer / 2))"
269	__rate_test $((last_policer / 2))
270
271	log_info "Running rate test for policer $last_policer"
272	__rate_test $last_policer
273}
274
275__burst_test()
276{
277	local t0_rx t0_drop t1_rx t1_drop rx drop
278	local id=$1; shift
279
280	RET=0
281
282	devlink trap policer set $DEVLINK_DEV policer $id rate 1000 burst 512
283	devlink trap group set $DEVLINK_DEV group l3_drops policer $id
284
285	# Send a burst of 16 packets and make sure that 16 are received
286	# and that none are dropped by the policer
287	log_info "=== Tx burst size: 16, Policer burst size: 512 ==="
288
289	t0_rx=$(devlink_trap_rx_packets_get blackhole_route)
290	t0_drop=$(devlink_trap_policer_rx_dropped_get $id)
291
292	start_traffic $h1 192.0.2.1 198.51.100.100 $rp1_mac -c 16
293
294	t1_rx=$(devlink_trap_rx_packets_get blackhole_route)
295	t1_drop=$(devlink_trap_policer_rx_dropped_get $id)
296
297	rx=$((t1_rx - t0_rx))
298	(( rx == 16 ))
299	check_err $? "Expected burst size of 16 packets, got $rx packets"
300	log_info "Expected burst size of 16 packets, measured burst size of $rx packets"
301
302	drop=$((t1_drop - t0_drop))
303	(( drop == 0 ))
304	check_err $? "Expected zero policer drops, got $drop"
305	log_info "Measured policer drops of $drop packets"
306
307	# Unbind the policer and send a burst of 64 packets. Make sure that
308	# 64 packets are received and that none are dropped by the policer
309	log_info "=== Tx burst size: 64, Policer burst size: No policer ==="
310
311	devlink trap group set $DEVLINK_DEV group l3_drops nopolicer
312
313	t0_rx=$(devlink_trap_rx_packets_get blackhole_route)
314	t0_drop=$(devlink_trap_policer_rx_dropped_get $id)
315
316	start_traffic $h1 192.0.2.1 198.51.100.100 $rp1_mac -c 64
317
318	t1_rx=$(devlink_trap_rx_packets_get blackhole_route)
319	t1_drop=$(devlink_trap_policer_rx_dropped_get $id)
320
321	rx=$((t1_rx - t0_rx))
322	(( rx == 64 ))
323	check_err $? "Expected burst size of 64 packets, got $rx packets"
324	log_info "Expected burst size of 64 packets, measured burst size of $rx packets"
325
326	drop=$((t1_drop - t0_drop))
327	(( drop == 0 ))
328	check_err $? "Expected zero policer drops, got $drop"
329	log_info "Measured policer drops of $drop packets"
330
331	log_test "Trap policer burst size"
332}
333
334burst_test()
335{
336	local last_policer=$(devlink -j -p trap policer show |
337			     jq '[.[]["'$DEVLINK_DEV'"][].policer] | max')
338
339	log_info "Running burst test for policer 1"
340	__burst_test 1
341
342	log_info "Running burst test for policer $((last_policer / 2))"
343	__burst_test $((last_policer / 2))
344
345	log_info "Running burst test for policer $last_policer"
346	__burst_test $last_policer
347}
348
349trap cleanup EXIT
350
351setup_prepare
352setup_wait
353
354tests_run
355
356exit $EXIT_STATUS
357