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 16
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	((-5 <= pct && pct <= 5))
224	check_err $? "Expected rate 1000 pps, got $rate pps, which is $pct% off. Required accuracy is +-5%"
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 id
276
277	for id in $(devlink_trap_policer_ids_get); do
278		echo
279		log_info "Running rate test for policer $id"
280		__rate_test $id
281	done
282}
283
284__burst_test()
285{
286	local t0_rx t0_drop t1_rx t1_drop rx drop
287	local id=$1; shift
288
289	RET=0
290
291	devlink trap policer set $DEVLINK_DEV policer $id rate 1000 burst 32
292	devlink trap group set $DEVLINK_DEV group l3_drops policer $id
293
294	# Send a burst of 64 packets and make sure that about 32 are received
295	# and the rest are dropped by the policer
296	log_info "=== Tx burst size: 64, Policer burst size: 32 pps ==="
297
298	t0_rx=$(devlink_trap_rx_packets_get blackhole_route)
299	t0_drop=$(devlink_trap_policer_rx_dropped_get $id)
300
301	start_traffic $h1 192.0.2.1 198.51.100.100 $rp1_mac -c 64
302
303	t1_rx=$(devlink_trap_rx_packets_get blackhole_route)
304	t1_drop=$(devlink_trap_policer_rx_dropped_get $id)
305
306	rx=$((t1_rx - t0_rx))
307	pct=$((100 * (rx - 32) / 32))
308	((-20 <= pct && pct <= 20))
309	check_err $? "Expected burst size of 32 packets, got $rx packets, which is $pct% off. Required accuracy is +-20%"
310	log_info "Expected burst size of 32 packets, measured burst size of $rx packets"
311
312	drop=$((t1_drop - t0_drop))
313	(( drop > 0 ))
314	check_err $? "Expected non-zero policer drops, got 0"
315	log_info "Measured policer drops of $drop packets"
316
317	# Send a burst of 16 packets and make sure that 16 are received
318	# and that none are dropped by the policer
319	log_info "=== Tx burst size: 16, Policer burst size: 32 pps ==="
320
321	t0_rx=$(devlink_trap_rx_packets_get blackhole_route)
322	t0_drop=$(devlink_trap_policer_rx_dropped_get $id)
323
324	start_traffic $h1 192.0.2.1 198.51.100.100 $rp1_mac -c 16
325
326	t1_rx=$(devlink_trap_rx_packets_get blackhole_route)
327	t1_drop=$(devlink_trap_policer_rx_dropped_get $id)
328
329	rx=$((t1_rx - t0_rx))
330	(( rx == 16 ))
331	check_err $? "Expected burst size of 16 packets, got $rx packets"
332	log_info "Expected burst size of 16 packets, measured burst size of $rx packets"
333
334	drop=$((t1_drop - t0_drop))
335	(( drop == 0 ))
336	check_err $? "Expected zero policer drops, got $drop"
337	log_info "Measured policer drops of $drop packets"
338
339	# Unbind the policer and send a burst of 64 packets. Make sure that
340	# 64 packets are received and that none are dropped by the policer
341	log_info "=== Tx burst size: 64, Policer burst size: No policer ==="
342
343	devlink trap group set $DEVLINK_DEV group l3_drops nopolicer
344
345	t0_rx=$(devlink_trap_rx_packets_get blackhole_route)
346	t0_drop=$(devlink_trap_policer_rx_dropped_get $id)
347
348	start_traffic $h1 192.0.2.1 198.51.100.100 $rp1_mac -c 64
349
350	t1_rx=$(devlink_trap_rx_packets_get blackhole_route)
351	t1_drop=$(devlink_trap_policer_rx_dropped_get $id)
352
353	rx=$((t1_rx - t0_rx))
354	(( rx == 64 ))
355	check_err $? "Expected burst size of 64 packets, got $rx packets"
356	log_info "Expected burst size of 64 packets, measured burst size of $rx packets"
357
358	drop=$((t1_drop - t0_drop))
359	(( drop == 0 ))
360	check_err $? "Expected zero policer drops, got $drop"
361	log_info "Measured policer drops of $drop packets"
362
363	log_test "Trap policer burst size"
364}
365
366burst_test()
367{
368	local id
369
370	for id in $(devlink_trap_policer_ids_get); do
371		echo
372		log_info "Running burst size test for policer $id"
373		__burst_test $id
374	done
375}
376
377trap cleanup EXIT
378
379setup_prepare
380setup_wait
381
382tests_run
383
384exit $EXIT_STATUS
385