WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / drivers / net / netdevsim / devlink_trap.sh
blobda49ad2761b5510f13f81f9289d139072eb4d5c9
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 # This test is for checking devlink-trap functionality. It makes use of
5 # netdevsim which implements the required callbacks.
7 lib_dir=$(dirname $0)/../../../net/forwarding
9 ALL_TESTS="
10 init_test
11 trap_action_test
12 trap_metadata_test
13 bad_trap_test
14 bad_trap_action_test
15 trap_stats_test
16 trap_group_action_test
17 bad_trap_group_test
18 trap_group_stats_test
19 trap_policer_test
20 trap_policer_bind_test
21 port_del_test
22 dev_del_test
24 NETDEVSIM_PATH=/sys/bus/netdevsim/
25 DEV_ADDR=1337
26 DEV=netdevsim${DEV_ADDR}
27 DEVLINK_DEV=netdevsim/${DEV}
28 DEBUGFS_DIR=/sys/kernel/debug/netdevsim/$DEV/
29 SLEEP_TIME=1
30 NETDEV=""
31 NUM_NETIFS=0
32 source $lib_dir/lib.sh
33 source $lib_dir/devlink_lib.sh
35 require_command udevadm
37 modprobe netdevsim &> /dev/null
38 if [ ! -d "$NETDEVSIM_PATH" ]; then
39 echo "SKIP: No netdevsim support"
40 exit 1
43 if [ -d "${NETDEVSIM_PATH}/devices/netdevsim${DEV_ADDR}" ]; then
44 echo "SKIP: Device netdevsim${DEV_ADDR} already exists"
45 exit 1
48 init_test()
50 RET=0
52 test $(devlink_traps_num_get) -ne 0
53 check_err $? "No traps were registered"
55 log_test "Initialization"
58 trap_action_test()
60 local orig_action
61 local trap_name
62 local action
64 RET=0
66 for trap_name in $(devlink_traps_get); do
67 # The action of non-drop traps cannot be changed.
68 if [ $(devlink_trap_type_get $trap_name) = "drop" ]; then
69 devlink_trap_action_set $trap_name "trap"
70 action=$(devlink_trap_action_get $trap_name)
71 if [ $action != "trap" ]; then
72 check_err 1 "Trap $trap_name did not change action to trap"
75 devlink_trap_action_set $trap_name "drop"
76 action=$(devlink_trap_action_get $trap_name)
77 if [ $action != "drop" ]; then
78 check_err 1 "Trap $trap_name did not change action to drop"
80 else
81 orig_action=$(devlink_trap_action_get $trap_name)
83 devlink_trap_action_set $trap_name "trap"
84 action=$(devlink_trap_action_get $trap_name)
85 if [ $action != $orig_action ]; then
86 check_err 1 "Trap $trap_name changed action when should not"
89 devlink_trap_action_set $trap_name "drop"
90 action=$(devlink_trap_action_get $trap_name)
91 if [ $action != $orig_action ]; then
92 check_err 1 "Trap $trap_name changed action when should not"
95 done
97 log_test "Trap action"
100 trap_metadata_test()
102 local trap_name
104 RET=0
106 for trap_name in $(devlink_traps_get); do
107 devlink_trap_metadata_test $trap_name "input_port"
108 check_err $? "Input port not reported as metadata of trap $trap_name"
109 if [ $trap_name == "ingress_flow_action_drop" ] ||
110 [ $trap_name == "egress_flow_action_drop" ]; then
111 devlink_trap_metadata_test $trap_name "flow_action_cookie"
112 check_err $? "Flow action cookie not reported as metadata of trap $trap_name"
114 done
116 log_test "Trap metadata"
119 bad_trap_test()
121 RET=0
123 devlink_trap_action_set "made_up_trap" "drop"
124 check_fail $? "Did not get an error for non-existing trap"
126 log_test "Non-existing trap"
129 bad_trap_action_test()
131 local traps_arr
132 local trap_name
134 RET=0
136 # Pick first trap.
137 traps_arr=($(devlink_traps_get))
138 trap_name=${traps_arr[0]}
140 devlink_trap_action_set $trap_name "made_up_action"
141 check_fail $? "Did not get an error for non-existing trap action"
143 log_test "Non-existing trap action"
146 trap_stats_test()
148 local trap_name
150 RET=0
152 for trap_name in $(devlink_traps_get); do
153 devlink_trap_stats_idle_test $trap_name
154 check_err $? "Stats of trap $trap_name not idle when netdev down"
156 ip link set dev $NETDEV up
158 if [ $(devlink_trap_type_get $trap_name) = "drop" ]; then
159 devlink_trap_action_set $trap_name "trap"
160 devlink_trap_stats_idle_test $trap_name
161 check_fail $? "Stats of trap $trap_name idle when action is trap"
163 devlink_trap_action_set $trap_name "drop"
164 devlink_trap_stats_idle_test $trap_name
165 check_err $? "Stats of trap $trap_name not idle when action is drop"
166 else
167 devlink_trap_stats_idle_test $trap_name
168 check_fail $? "Stats of non-drop trap $trap_name idle when should not"
171 ip link set dev $NETDEV down
172 done
174 log_test "Trap statistics"
177 trap_group_action_test()
179 local curr_group group_name
180 local trap_name
181 local trap_type
182 local action
184 RET=0
186 for group_name in $(devlink_trap_groups_get); do
187 devlink_trap_group_action_set $group_name "trap"
189 for trap_name in $(devlink_traps_get); do
190 curr_group=$(devlink_trap_group_get $trap_name)
191 if [ $curr_group != $group_name ]; then
192 continue
195 trap_type=$(devlink_trap_type_get $trap_name)
196 if [ $trap_type != "drop" ]; then
197 continue
200 action=$(devlink_trap_action_get $trap_name)
201 if [ $action != "trap" ]; then
202 check_err 1 "Trap $trap_name did not change action to trap"
204 done
206 devlink_trap_group_action_set $group_name "drop"
208 for trap_name in $(devlink_traps_get); do
209 curr_group=$(devlink_trap_group_get $trap_name)
210 if [ $curr_group != $group_name ]; then
211 continue
214 trap_type=$(devlink_trap_type_get $trap_name)
215 if [ $trap_type != "drop" ]; then
216 continue
219 action=$(devlink_trap_action_get $trap_name)
220 if [ $action != "drop" ]; then
221 check_err 1 "Trap $trap_name did not change action to drop"
223 done
224 done
226 log_test "Trap group action"
229 bad_trap_group_test()
231 RET=0
233 devlink_trap_group_action_set "made_up_trap_group" "drop"
234 check_fail $? "Did not get an error for non-existing trap group"
236 log_test "Non-existing trap group"
239 trap_group_stats_test()
241 local group_name
243 RET=0
245 for group_name in $(devlink_trap_groups_get); do
246 devlink_trap_group_stats_idle_test $group_name
247 check_err $? "Stats of trap group $group_name not idle when netdev down"
249 ip link set dev $NETDEV up
251 devlink_trap_group_action_set $group_name "trap"
252 devlink_trap_group_stats_idle_test $group_name
253 check_fail $? "Stats of trap group $group_name idle when action is trap"
255 devlink_trap_group_action_set $group_name "drop"
256 ip link set dev $NETDEV down
257 done
259 log_test "Trap group statistics"
262 trap_policer_test()
264 local packets_t0
265 local packets_t1
267 RET=0
269 if [ $(devlink_trap_policers_num_get) -eq 0 ]; then
270 check_err 1 "Failed to dump policers"
273 devlink trap policer set $DEVLINK_DEV policer 1337 &> /dev/null
274 check_fail $? "Did not get an error for setting a non-existing policer"
275 devlink trap policer show $DEVLINK_DEV policer 1337 &> /dev/null
276 check_fail $? "Did not get an error for getting a non-existing policer"
278 devlink trap policer set $DEVLINK_DEV policer 1 rate 2000 burst 16
279 check_err $? "Failed to set valid parameters for a valid policer"
280 if [ $(devlink_trap_policer_rate_get 1) -ne 2000 ]; then
281 check_err 1 "Policer rate was not changed"
283 if [ $(devlink_trap_policer_burst_get 1) -ne 16 ]; then
284 check_err 1 "Policer burst size was not changed"
287 devlink trap policer set $DEVLINK_DEV policer 1 rate 0 &> /dev/null
288 check_fail $? "Policer rate was changed to rate lower than limit"
289 devlink trap policer set $DEVLINK_DEV policer 1 rate 9000 &> /dev/null
290 check_fail $? "Policer rate was changed to rate higher than limit"
291 devlink trap policer set $DEVLINK_DEV policer 1 burst 2 &> /dev/null
292 check_fail $? "Policer burst size was changed to burst size lower than limit"
293 devlink trap policer set $DEVLINK_DEV policer 1 rate 65537 &> /dev/null
294 check_fail $? "Policer burst size was changed to burst size higher than limit"
295 echo "y" > $DEBUGFS_DIR/fail_trap_policer_set
296 devlink trap policer set $DEVLINK_DEV policer 1 rate 3000 &> /dev/null
297 check_fail $? "Managed to set policer rate when should not"
298 echo "n" > $DEBUGFS_DIR/fail_trap_policer_set
299 if [ $(devlink_trap_policer_rate_get 1) -ne 2000 ]; then
300 check_err 1 "Policer rate was changed to an invalid value"
302 if [ $(devlink_trap_policer_burst_get 1) -ne 16 ]; then
303 check_err 1 "Policer burst size was changed to an invalid value"
306 packets_t0=$(devlink_trap_policer_rx_dropped_get 1)
307 sleep .5
308 packets_t1=$(devlink_trap_policer_rx_dropped_get 1)
309 if [ ! $packets_t1 -gt $packets_t0 ]; then
310 check_err 1 "Policer drop counter was not incremented"
313 echo "y"> $DEBUGFS_DIR/fail_trap_policer_counter_get
314 devlink -s trap policer show $DEVLINK_DEV policer 1 &> /dev/null
315 check_fail $? "Managed to read policer drop counter when should not"
316 echo "n"> $DEBUGFS_DIR/fail_trap_policer_counter_get
317 devlink -s trap policer show $DEVLINK_DEV policer 1 &> /dev/null
318 check_err $? "Did not manage to read policer drop counter when should"
320 log_test "Trap policer"
323 trap_group_check_policer()
325 local group_name=$1; shift
327 devlink -j -p trap group show $DEVLINK_DEV group $group_name \
328 | jq -e '.[][][]["policer"]' &> /dev/null
331 trap_policer_bind_test()
333 RET=0
335 devlink trap group set $DEVLINK_DEV group l2_drops policer 1
336 check_err $? "Failed to bind a valid policer"
337 if [ $(devlink_trap_group_policer_get "l2_drops") -ne 1 ]; then
338 check_err 1 "Bound policer was not changed"
341 devlink trap group set $DEVLINK_DEV group l2_drops policer 1337 \
342 &> /dev/null
343 check_fail $? "Did not get an error for binding a non-existing policer"
344 if [ $(devlink_trap_group_policer_get "l2_drops") -ne 1 ]; then
345 check_err 1 "Bound policer was changed when should not"
348 devlink trap group set $DEVLINK_DEV group l2_drops policer 0
349 check_err $? "Failed to unbind a policer when using ID 0"
350 trap_group_check_policer "l2_drops"
351 check_fail $? "Trap group has a policer after unbinding with ID 0"
353 devlink trap group set $DEVLINK_DEV group l2_drops policer 1
354 check_err $? "Failed to bind a valid policer"
356 devlink trap group set $DEVLINK_DEV group l2_drops nopolicer
357 check_err $? "Failed to unbind a policer when using 'nopolicer' keyword"
358 trap_group_check_policer "l2_drops"
359 check_fail $? "Trap group has a policer after unbinding with 'nopolicer' keyword"
361 devlink trap group set $DEVLINK_DEV group l2_drops policer 1
362 check_err $? "Failed to bind a valid policer"
364 echo "y"> $DEBUGFS_DIR/fail_trap_group_set
365 devlink trap group set $DEVLINK_DEV group l2_drops policer 2 \
366 &> /dev/null
367 check_fail $? "Managed to bind a policer when should not"
368 echo "n"> $DEBUGFS_DIR/fail_trap_group_set
369 devlink trap group set $DEVLINK_DEV group l2_drops policer 2
370 check_err $? "Did not manage to bind a policer when should"
372 devlink trap group set $DEVLINK_DEV group l2_drops action drop \
373 policer 1337 &> /dev/null
374 check_fail $? "Did not get an error for partially modified trap group"
376 log_test "Trap policer binding"
379 port_del_test()
381 local group_name
382 local i
384 # The test never fails. It is meant to exercise different code paths
385 # and make sure we properly dismantle a port while packets are
386 # in-flight.
387 RET=0
389 devlink_traps_enable_all
391 for i in $(seq 1 10); do
392 ip link set dev $NETDEV up
394 sleep $SLEEP_TIME
396 netdevsim_port_destroy
397 netdevsim_port_create
398 udevadm settle
399 done
401 devlink_traps_disable_all
403 log_test "Port delete"
406 dev_del_test()
408 local group_name
409 local i
411 # The test never fails. It is meant to exercise different code paths
412 # and make sure we properly unregister traps while packets are
413 # in-flight.
414 RET=0
416 devlink_traps_enable_all
418 for i in $(seq 1 10); do
419 ip link set dev $NETDEV up
421 sleep $SLEEP_TIME
423 cleanup
424 setup_prepare
425 done
427 devlink_traps_disable_all
429 log_test "Device delete"
432 netdevsim_dev_create()
434 echo "$DEV_ADDR 0" > ${NETDEVSIM_PATH}/new_device
437 netdevsim_dev_destroy()
439 echo "$DEV_ADDR" > ${NETDEVSIM_PATH}/del_device
442 netdevsim_port_create()
444 echo 1 > ${NETDEVSIM_PATH}/devices/${DEV}/new_port
447 netdevsim_port_destroy()
449 echo 1 > ${NETDEVSIM_PATH}/devices/${DEV}/del_port
452 setup_prepare()
454 local netdev
456 netdevsim_dev_create
458 if [ ! -d "${NETDEVSIM_PATH}/devices/${DEV}" ]; then
459 echo "Failed to create netdevsim device"
460 exit 1
463 netdevsim_port_create
465 if [ ! -d "${NETDEVSIM_PATH}/devices/${DEV}/net/" ]; then
466 echo "Failed to create netdevsim port"
467 exit 1
470 # Wait for udev to rename newly created netdev.
471 udevadm settle
473 NETDEV=$(ls ${NETDEVSIM_PATH}/devices/${DEV}/net/)
476 cleanup()
478 pre_cleanup
479 netdevsim_port_destroy
480 netdevsim_dev_destroy
483 trap cleanup EXIT
485 setup_prepare
487 tests_run
489 exit $EXIT_STATUS