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