WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / drivers / net / mlxsw / devlink_trap_policer.sh
blob508a702f002153ba4804e5dc0d4ba3940a6daeb9
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 # Test devlink-trap policer functionality over mlxsw.
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 # +---------------------------------+
32 lib_dir=$(dirname $0)/../../../net/forwarding
34 ALL_TESTS="
35 rate_limits_test
36 burst_limits_test
37 rate_test
38 burst_test
40 NUM_NETIFS=4
41 source $lib_dir/tc_common.sh
42 source $lib_dir/lib.sh
43 source $lib_dir/devlink_lib.sh
45 h1_create()
47 simple_if_init $h1 192.0.2.1/24
48 mtu_set $h1 10000
50 ip -4 route add default vrf v$h1 nexthop via 192.0.2.2
53 h1_destroy()
55 ip -4 route del default vrf v$h1 nexthop via 192.0.2.2
57 mtu_restore $h1
58 simple_if_fini $h1 192.0.2.1/24
61 h2_create()
63 simple_if_init $h2 198.51.100.1/24
64 mtu_set $h2 10000
66 ip -4 route add default vrf v$h2 nexthop via 198.51.100.2
69 h2_destroy()
71 ip -4 route del default vrf v$h2 nexthop via 198.51.100.2
73 mtu_restore $h2
74 simple_if_fini $h2 198.51.100.1/24
77 router_create()
79 ip link set dev $rp1 up
80 ip link set dev $rp2 up
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
87 ip -4 route add blackhole 198.51.100.100
89 devlink trap set $DEVLINK_DEV trap blackhole_route action trap
92 router_destroy()
94 devlink trap set $DEVLINK_DEV trap blackhole_route action drop
96 ip -4 route del blackhole 198.51.100.100
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
103 ip link set dev $rp2 down
104 ip link set dev $rp1 down
107 setup_prepare()
109 h1=${NETIFS[p1]}
110 rp1=${NETIFS[p2]}
112 rp2=${NETIFS[p3]}
113 h2=${NETIFS[p4]}
115 rp1_mac=$(mac_get $rp1)
117 vrf_prepare
119 h1_create
120 h2_create
122 router_create
125 cleanup()
127 pre_cleanup
129 router_destroy
131 h2_destroy
132 h1_destroy
134 vrf_cleanup
136 # Reload to ensure devlink-trap settings are back to default.
137 devlink_reload
140 rate_limits_test()
142 RET=0
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"
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"
155 log_test "Trap policer rate limits"
158 burst_limits_test()
160 RET=0
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"
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"
177 log_test "Trap policer burst size limits"
180 trap_rate_get()
182 local t0 t1
184 t0=$(devlink_trap_rx_packets_get blackhole_route)
185 sleep 10
186 t1=$(devlink_trap_rx_packets_get blackhole_route)
188 echo $(((t1 - t0) / 10))
191 policer_drop_rate_get()
193 local id=$1; shift
194 local t0 t1
196 t0=$(devlink_trap_policer_rx_dropped_get $id)
197 sleep 10
198 t1=$(devlink_trap_policer_rx_dropped_get $id)
200 echo $(((t1 - t0) / 10))
203 __rate_test()
205 local rate pct drop_rate
206 local id=$1; shift
208 RET=0
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
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 ==="
217 start_traffic $h1 192.0.2.1 198.51.100.100 $rp1_mac
219 sleep 5 # Take measurements when rate is stable
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"
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"
232 stop_traffic
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 ==="
238 start_traffic $h1 192.0.2.1 198.51.100.100 $rp1_mac -d 1msec
240 sleep 5 # Take measurements when rate is stable
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"
247 stop_traffic
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 ==="
254 devlink trap group set $DEVLINK_DEV group l3_drops nopolicer
256 start_traffic $h1 192.0.2.1 198.51.100.100 $rp1_mac
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"
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"
268 stop_traffic
270 log_test "Trap policer rate"
273 rate_test()
275 local id
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
284 __burst_test()
286 local t0_rx t0_drop t1_rx t1_drop rx drop
287 local id=$1; shift
289 RET=0
291 devlink trap policer set $DEVLINK_DEV policer $id rate 1000 burst 512
292 devlink trap group set $DEVLINK_DEV group l3_drops policer $id
294 # Send a burst of 16 packets and make sure that 16 are received
295 # and that none are dropped by the policer
296 log_info "=== Tx burst size: 16, Policer burst size: 512 ==="
298 t0_rx=$(devlink_trap_rx_packets_get blackhole_route)
299 t0_drop=$(devlink_trap_policer_rx_dropped_get $id)
301 start_traffic $h1 192.0.2.1 198.51.100.100 $rp1_mac -c 16
303 t1_rx=$(devlink_trap_rx_packets_get blackhole_route)
304 t1_drop=$(devlink_trap_policer_rx_dropped_get $id)
306 rx=$((t1_rx - t0_rx))
307 (( rx == 16 ))
308 check_err $? "Expected burst size of 16 packets, got $rx packets"
309 log_info "Expected burst size of 16 packets, measured burst size of $rx packets"
311 drop=$((t1_drop - t0_drop))
312 (( drop == 0 ))
313 check_err $? "Expected zero policer drops, got $drop"
314 log_info "Measured policer drops of $drop packets"
316 # Unbind the policer and send a burst of 64 packets. Make sure that
317 # 64 packets are received and that none are dropped by the policer
318 log_info "=== Tx burst size: 64, Policer burst size: No policer ==="
320 devlink trap group set $DEVLINK_DEV group l3_drops nopolicer
322 t0_rx=$(devlink_trap_rx_packets_get blackhole_route)
323 t0_drop=$(devlink_trap_policer_rx_dropped_get $id)
325 start_traffic $h1 192.0.2.1 198.51.100.100 $rp1_mac -c 64
327 t1_rx=$(devlink_trap_rx_packets_get blackhole_route)
328 t1_drop=$(devlink_trap_policer_rx_dropped_get $id)
330 rx=$((t1_rx - t0_rx))
331 (( rx == 64 ))
332 check_err $? "Expected burst size of 64 packets, got $rx packets"
333 log_info "Expected burst size of 64 packets, measured burst size of $rx packets"
335 drop=$((t1_drop - t0_drop))
336 (( drop == 0 ))
337 check_err $? "Expected zero policer drops, got $drop"
338 log_info "Measured policer drops of $drop packets"
340 log_test "Trap policer burst size"
343 burst_test()
345 local id
347 for id in $(devlink_trap_policer_ids_get); do
348 echo
349 log_info "Running burst size test for policer $id"
350 __burst_test $id
351 done
354 trap cleanup EXIT
356 setup_prepare
357 setup_wait
359 tests_run
361 exit $EXIT_STATUS