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
16 trap_group_action_test
20 trap_policer_bind_test
24 NETDEVSIM_PATH
=/sys
/bus
/netdevsim
/
26 DEV
=netdevsim
${DEV_ADDR}
27 DEVLINK_DEV
=netdevsim
/${DEV}
28 DEBUGFS_DIR
=/sys
/kernel
/debug
/netdevsim
/$DEV/
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"
43 if [ -d "${NETDEVSIM_PATH}/devices/netdevsim${DEV_ADDR}" ]; then
44 echo "SKIP: Device netdevsim${DEV_ADDR} already exists"
52 test $
(devlink_traps_num_get
) -ne 0
53 check_err $?
"No traps were registered"
55 log_test
"Initialization"
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"
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"
97 log_test
"Trap action"
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"
116 log_test
"Trap metadata"
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
()
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"
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"
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
174 log_test
"Trap statistics"
177 trap_group_action_test
()
179 local curr_group group_name
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
195 trap_type
=$
(devlink_trap_type_get
$trap_name)
196 if [ $trap_type != "drop" ]; then
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"
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
214 trap_type
=$
(devlink_trap_type_get
$trap_name)
215 if [ $trap_type != "drop" ]; then
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"
226 log_test
"Trap group action"
229 bad_trap_group_test
()
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
()
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
259 log_test
"Trap group statistics"
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)
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
()
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 \
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 \
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"
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
389 devlink_traps_enable_all
391 for i
in $
(seq 1 10); do
392 ip link
set dev
$NETDEV up
396 netdevsim_port_destroy
397 netdevsim_port_create
401 devlink_traps_disable_all
403 log_test
"Port delete"
411 # The test never fails. It is meant to exercise different code paths
412 # and make sure we properly unregister traps while packets are
416 devlink_traps_enable_all
418 for i
in $
(seq 1 10); do
419 ip link
set dev
$NETDEV up
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
458 if [ ! -d "${NETDEVSIM_PATH}/devices/${DEV}" ]; then
459 echo "Failed to create netdevsim device"
463 netdevsim_port_create
465 if [ ! -d "${NETDEVSIM_PATH}/devices/${DEV}/net/" ]; then
466 echo "Failed to create netdevsim port"
470 # Wait for udev to rename newly created netdev.
473 NETDEV
=$
(ls ${NETDEVSIM_PATH}/devices
/${DEV}/net
/)
479 netdevsim_port_destroy
480 netdevsim_dev_destroy