2 # SPDX-License-Identifier: GPL-2.0
4 ##############################################################################
7 if [[ ! -v DEVLINK_DEV
]]; then
8 DEVLINK_DEV
=$
(devlink port show
"${NETIFS[p1]:-$NETIF_NO_CABLE}" -j \
9 | jq
-r '.port | keys[]' | cut
-d/ -f-2)
10 if [ -z "$DEVLINK_DEV" ]; then
11 echo "SKIP: ${NETIFS[p1]} has no devlink device registered for it"
14 if [[ "$(echo $DEVLINK_DEV | grep -c pci)" -eq 0 ]]; then
15 echo "SKIP: devlink device's bus is not PCI"
19 DEVLINK_VIDDID
=$
(lspci
-s $
(echo $DEVLINK_DEV | cut
-d"/" -f2) \
23 ##############################################################################
26 devlink
help 2>&1 |
grep resource
&> /dev
/null
28 echo "SKIP: iproute2 too old, missing devlink resource support"
32 devlink
help 2>&1 |
grep trap &> /dev
/null
34 echo "SKIP: iproute2 too old, missing devlink trap support"
38 devlink dev
help 2>&1 |
grep info
&> /dev
/null
40 echo "SKIP: iproute2 too old, missing devlink dev info support"
44 ##############################################################################
47 devlink_resource_names_to_path
()
52 for resource
in "${@}"; do
53 if [ "$path" == "" ]; then
56 path
="${path}/$resource"
63 devlink_resource_get
()
66 local resource_name
=.
[][\"$DEVLINK_DEV\"]
68 resource_name
="$resource_name | .[] | select (.name == \"$name\")"
71 for resource
in "${@}"; do
72 resource_name
="${resource_name} | .[\"resources\"][] | \
73 select (.name == \"$resource\")"
76 devlink
-j resource show
"$DEVLINK_DEV" | jq
"$resource_name"
79 devlink_resource_size_get
()
81 local size
=$
(devlink_resource_get
"$@" | jq
'.["size_new"]')
83 if [ "$size" == "null" ]; then
84 devlink_resource_get
"$@" | jq
'.["size"]'
90 devlink_resource_size_set
()
96 path
=$
(devlink_resource_names_to_path
"$@")
97 devlink resource
set "$DEVLINK_DEV" path
"$path" size
"$new_size"
98 check_err $?
"Failed setting path $path to size $size"
101 devlink_resource_occ_get
()
103 devlink_resource_get
"$@" | jq
'.["occ"]'
110 devlink dev reload
"$DEVLINK_DEV" &> /dev
/null
111 check_err $?
"Failed reload"
113 still_pending
=$
(devlink resource show
"$DEVLINK_DEV" | \
115 check_err
$still_pending "Failed reload - There are still unset sizes"
118 declare -A DEVLINK_ORIG
120 # Changing pool type from static to dynamic causes reinterpretation of threshold
121 # values. They therefore need to be saved before pool type is changed, then the
122 # pool type can be changed, and then the new values need to be set up. Therefore
123 # instead of saving the current state implicitly in the _set call, provide
124 # functions for all three primitives: save, set, and restore.
126 devlink_port_pool_threshold
()
131 devlink sb port pool show
$port pool
$pool -j \
132 | jq
'.port_pool."'"$port"'"[].threshold'
135 devlink_port_pool_th_save
()
139 local key
="port_pool($port,$pool).threshold"
141 DEVLINK_ORIG
[$key]=$
(devlink_port_pool_threshold
$port $pool)
144 devlink_port_pool_th_set
()
150 devlink sb port pool
set $port pool
$pool th
$th
153 devlink_port_pool_th_restore
()
157 local key
="port_pool($port,$pool).threshold"
158 local -a orig
=(${DEVLINK_ORIG[$key]})
160 if [[ -z $orig ]]; then
161 echo "WARNING: Mismatched devlink_port_pool_th_restore"
163 devlink sb port pool
set $port pool
$pool th
$orig
167 devlink_pool_size_thtype
()
171 devlink sb pool show
"$DEVLINK_DEV" pool
$pool -j \
172 | jq
-r '.pool[][] | (.size, .thtype)'
175 devlink_pool_size_thtype_save
()
178 local key
="pool($pool).size_thtype"
180 DEVLINK_ORIG
[$key]=$
(devlink_pool_size_thtype
$pool)
183 devlink_pool_size_thtype_set
()
186 local thtype
=$1; shift
189 devlink sb pool
set "$DEVLINK_DEV" pool
$pool size
$size thtype
$thtype
192 devlink_pool_size_thtype_restore
()
195 local key
="pool($pool).size_thtype"
196 local -a orig
=(${DEVLINK_ORIG[$key]})
198 if [[ -z ${orig[0]} ]]; then
199 echo "WARNING: Mismatched devlink_pool_size_thtype_restore"
201 devlink sb pool
set "$DEVLINK_DEV" pool
$pool \
202 size
${orig[0]} thtype
${orig[1]}
206 devlink_tc_bind_pool_th
()
212 devlink sb tc
bind show
$port tc
$tc type $dir -j \
213 | jq
-r '.tc_bind[][] | (.pool, .threshold)'
216 devlink_tc_bind_pool_th_save
()
221 local key
="tc_bind($port,$dir,$tc).pool_th"
223 DEVLINK_ORIG
[$key]=$
(devlink_tc_bind_pool_th
$port $tc $dir)
226 devlink_tc_bind_pool_th_set
()
234 devlink sb tc
bind set $port tc
$tc type $dir pool
$pool th
$th
237 devlink_tc_bind_pool_th_restore
()
242 local key
="tc_bind($port,$dir,$tc).pool_th"
243 local -a orig
=(${DEVLINK_ORIG[$key]})
245 if [[ -z ${orig[0]} ]]; then
246 echo "WARNING: Mismatched devlink_tc_bind_pool_th_restore"
248 devlink sb tc
bind set $port tc
$tc type $dir \
249 pool
${orig[0]} th
${orig[1]}
253 devlink_traps_num_get
()
255 devlink
-j trap | jq
'.[]["'$DEVLINK_DEV'"] | length'
260 devlink
-j trap | jq
-r '.[]["'$DEVLINK_DEV'"][].name'
263 devlink_trap_type_get
()
265 local trap_name
=$1; shift
267 devlink
-j trap show
$DEVLINK_DEV trap $trap_name \
268 | jq
-r '.[][][].type'
271 devlink_trap_action_set
()
273 local trap_name
=$1; shift
274 local action
=$1; shift
276 # Pipe output to /dev/null to avoid expected warnings.
277 devlink
trap set $DEVLINK_DEV trap $trap_name \
278 action
$action &> /dev
/null
281 devlink_trap_action_get
()
283 local trap_name
=$1; shift
285 devlink
-j trap show
$DEVLINK_DEV trap $trap_name \
286 | jq
-r '.[][][].action'
289 devlink_trap_group_get
()
291 devlink
-j trap show
$DEVLINK_DEV trap $trap_name \
292 | jq
-r '.[][][].group'
295 devlink_trap_metadata_test
()
297 local trap_name
=$1; shift
298 local metadata
=$1; shift
300 devlink
-jv trap show
$DEVLINK_DEV trap $trap_name \
301 | jq
-e '.[][][].metadata | contains(["'$metadata'"])' \
305 devlink_trap_rx_packets_get
()
307 local trap_name
=$1; shift
309 devlink
-js trap show
$DEVLINK_DEV trap $trap_name \
310 | jq
'.[][][]["stats"]["rx"]["packets"]'
313 devlink_trap_rx_bytes_get
()
315 local trap_name
=$1; shift
317 devlink
-js trap show
$DEVLINK_DEV trap $trap_name \
318 | jq
'.[][][]["stats"]["rx"]["bytes"]'
321 devlink_trap_stats_idle_test
()
323 local trap_name
=$1; shift
324 local t0_packets t0_bytes
325 local t1_packets t1_bytes
327 t0_packets
=$
(devlink_trap_rx_packets_get
$trap_name)
328 t0_bytes
=$
(devlink_trap_rx_bytes_get
$trap_name)
332 t1_packets
=$
(devlink_trap_rx_packets_get
$trap_name)
333 t1_bytes
=$
(devlink_trap_rx_bytes_get
$trap_name)
335 if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then
342 devlink_traps_enable_all
()
346 for trap_name
in $
(devlink_traps_get
); do
347 devlink_trap_action_set
$trap_name "trap"
351 devlink_traps_disable_all
()
353 for trap_name
in $
(devlink_traps_get
); do
354 devlink_trap_action_set
$trap_name "drop"
358 devlink_trap_groups_get
()
360 devlink
-j trap group | jq
-r '.[]["'$DEVLINK_DEV'"][].name'
363 devlink_trap_group_action_set
()
365 local group_name
=$1; shift
366 local action
=$1; shift
368 # Pipe output to /dev/null to avoid expected warnings.
369 devlink
trap group
set $DEVLINK_DEV group
$group_name action
$action \
373 devlink_trap_group_rx_packets_get
()
375 local group_name
=$1; shift
377 devlink
-js trap group show
$DEVLINK_DEV group
$group_name \
378 | jq
'.[][][]["stats"]["rx"]["packets"]'
381 devlink_trap_group_rx_bytes_get
()
383 local group_name
=$1; shift
385 devlink
-js trap group show
$DEVLINK_DEV group
$group_name \
386 | jq
'.[][][]["stats"]["rx"]["bytes"]'
389 devlink_trap_group_stats_idle_test
()
391 local group_name
=$1; shift
392 local t0_packets t0_bytes
393 local t1_packets t1_bytes
395 t0_packets
=$
(devlink_trap_group_rx_packets_get
$group_name)
396 t0_bytes
=$
(devlink_trap_group_rx_bytes_get
$group_name)
400 t1_packets
=$
(devlink_trap_group_rx_packets_get
$group_name)
401 t1_bytes
=$
(devlink_trap_group_rx_bytes_get
$group_name)
403 if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then
410 devlink_trap_exception_test
()
412 local trap_name
=$1; shift
415 group_name
=$
(devlink_trap_group_get
$trap_name)
417 devlink_trap_stats_idle_test
$trap_name
418 check_fail $?
"Trap stats idle when packets should have been trapped"
420 devlink_trap_group_stats_idle_test
$group_name
421 check_fail $?
"Trap group idle when packets should have been trapped"
424 devlink_trap_drop_test
()
426 local trap_name
=$1; shift
428 local handle
=$1; shift
431 group_name
=$
(devlink_trap_group_get
$trap_name)
433 # This is the common part of all the tests. It checks that stats are
434 # initially idle, then non-idle after changing the trap action and
435 # finally idle again. It also makes sure the packets are dropped and
437 devlink_trap_stats_idle_test
$trap_name
438 check_err $?
"Trap stats not idle with initial drop action"
439 devlink_trap_group_stats_idle_test
$group_name
440 check_err $?
"Trap group stats not idle with initial drop action"
442 devlink_trap_action_set
$trap_name "trap"
443 devlink_trap_stats_idle_test
$trap_name
444 check_fail $?
"Trap stats idle after setting action to trap"
445 devlink_trap_group_stats_idle_test
$group_name
446 check_fail $?
"Trap group stats idle after setting action to trap"
448 devlink_trap_action_set
$trap_name "drop"
450 devlink_trap_stats_idle_test
$trap_name
451 check_err $?
"Trap stats not idle after setting action to drop"
452 devlink_trap_group_stats_idle_test
$group_name
453 check_err $?
"Trap group stats not idle after setting action to drop"
455 tc_check_packets
"dev $dev egress" $handle 0
456 check_err $?
"Packets were not dropped"
459 devlink_trap_drop_cleanup
()
461 local mz_pid
=$1; shift
463 local proto
=$1; shift
465 local handle
=$1; shift
467 kill $mz_pid && wait $mz_pid &> /dev
/null
468 tc filter del dev
$dev egress protocol
$proto pref
$pref handle
$handle flower
471 devlink_trap_stats_test
()
473 local test_name
=$1; shift
474 local trap_name
=$1; shift
481 t0_packets
=$
(devlink_trap_rx_packets_get
$trap_name)
485 t1_packets
=$
(devlink_trap_rx_packets_get
$trap_name)
487 if [[ $t1_packets -eq $t0_packets ]]; then
488 check_err
1 "Trap stats did not increase"
491 log_test
"$test_name"
494 devlink_trap_policers_num_get
()
496 devlink
-j -p trap policer show | jq
'.[]["'$DEVLINK_DEV'"] | length'
499 devlink_trap_policer_rate_get
()
501 local policer_id
=$1; shift
503 devlink
-j -p trap policer show
$DEVLINK_DEV policer
$policer_id \
504 | jq
'.[][][]["rate"]'
507 devlink_trap_policer_burst_get
()
509 local policer_id
=$1; shift
511 devlink
-j -p trap policer show
$DEVLINK_DEV policer
$policer_id \
512 | jq
'.[][][]["burst"]'
515 devlink_trap_policer_rx_dropped_get
()
517 local policer_id
=$1; shift
519 devlink
-j -p -s trap policer show
$DEVLINK_DEV policer
$policer_id \
520 | jq
'.[][][]["stats"]["rx"]["dropped"]'
523 devlink_trap_group_policer_get
()
525 local group_name
=$1; shift
527 devlink
-j -p trap group show
$DEVLINK_DEV group
$group_name \
528 | jq
'.[][][]["policer"]'
531 devlink_trap_policer_ids_get
()
533 devlink
-j -p trap policer show \
534 | jq
'.[]["'$DEVLINK_DEV'"][]["policer"]'
537 devlink_port_by_netdev
()
541 devlink
-j port show
$if_name | jq
-e '.[] | keys' | jq
-r '.[]'
544 devlink_cpu_port_get
()
546 local cpu_dl_port_num
=$
(devlink port list |
grep "$DEVLINK_DEV" |
547 grep cpu | cut
-d/ -f3 | cut
-d: -f1 |
550 echo "$DEVLINK_DEV/$cpu_dl_port_num"
553 devlink_cell_size_get
()
555 devlink sb pool show
"$DEVLINK_DEV" pool
0 -j \
556 | jq
'.pool[][].cell_size'