2 # SPDX-License-Identifier: GPL-2.0
4 # Kselftest framework requirement - SKIP code is 4.
7 ##############################################################################
10 if [[ ! -v DEVLINK_DEV ]]; then
11 DEVLINK_DEV=$(devlink port show "${NETIFS[p1]:-$NETIF_NO_CABLE}" -j \
12 | jq -r '.port | keys[]' | cut -d/ -f-2)
13 if [ -z "$DEVLINK_DEV" ]; then
14 echo "SKIP: ${NETIFS[p1]} has no devlink device registered for it"
17 if [[ "$(echo $DEVLINK_DEV | grep -c pci)" -eq 0 ]]; then
18 echo "SKIP: devlink device's bus is not PCI"
22 DEVLINK_VIDDID=$(lspci -s $(echo $DEVLINK_DEV | cut -d"/" -f2) \
24 elif [[ ! -z "$DEVLINK_DEV" ]]; then
25 devlink dev show $DEVLINK_DEV &> /dev/null
27 echo "SKIP: devlink device \"$DEVLINK_DEV\" not found"
32 ##############################################################################
35 devlink help 2>&1 | grep resource &> /dev/null
37 echo "SKIP: iproute2 too old, missing devlink resource support"
41 devlink help 2>&1 | grep trap &> /dev/null
43 echo "SKIP: iproute2 too old, missing devlink trap support"
47 devlink dev help 2>&1 | grep info &> /dev/null
49 echo "SKIP: iproute2 too old, missing devlink dev info support"
53 ##############################################################################
56 devlink_resource_names_to_path()
61 for resource in "${@}"; do
62 if [ "$path" == "" ]; then
65 path="${path}/$resource"
72 devlink_resource_get()
75 local resource_name=.[][\"$DEVLINK_DEV\"]
77 resource_name="$resource_name | .[] | select (.name == \"$name\")"
80 for resource in "${@}"; do
81 resource_name="${resource_name} | .[\"resources\"][] | \
82 select (.name == \"$resource\")"
85 devlink -j resource show "$DEVLINK_DEV" | jq "$resource_name"
88 devlink_resource_size_get()
90 local size=$(devlink_resource_get "$@" | jq '.["size_new"]')
92 if [ "$size" == "null" ]; then
93 devlink_resource_get "$@" | jq '.["size"]'
99 devlink_resource_size_set()
105 path=$(devlink_resource_names_to_path "$@")
106 devlink resource set "$DEVLINK_DEV" path "$path" size "$new_size"
107 check_err $? "Failed setting path $path to size $size"
110 devlink_resource_occ_get()
112 devlink_resource_get "$@" | jq '.["occ"]'
119 devlink dev reload "$DEVLINK_DEV" &> /dev/null
120 check_err $? "Failed reload"
122 still_pending=$(devlink resource show "$DEVLINK_DEV" | \
124 check_err $still_pending "Failed reload - There are still unset sizes"
129 declare -A DEVLINK_ORIG
131 # Changing pool type from static to dynamic causes reinterpretation of threshold
132 # values. They therefore need to be saved before pool type is changed, then the
133 # pool type can be changed, and then the new values need to be set up. Therefore
134 # instead of saving the current state implicitly in the _set call, provide
135 # functions for all three primitives: save, set, and restore.
137 devlink_port_pool_threshold()
142 devlink sb port pool show $port pool $pool -j \
143 | jq '.port_pool."'"$port"'"[].threshold'
146 devlink_port_pool_th_save()
150 local key="port_pool($port,$pool).threshold"
152 DEVLINK_ORIG[$key]=$(devlink_port_pool_threshold $port $pool)
155 devlink_port_pool_th_set()
161 devlink sb port pool set $port pool $pool th $th
164 devlink_port_pool_th_restore()
168 local key="port_pool($port,$pool).threshold"
169 local -a orig=(${DEVLINK_ORIG[$key]})
171 if [[ -z $orig ]]; then
172 echo "WARNING: Mismatched devlink_port_pool_th_restore"
174 devlink sb port pool set $port pool $pool th $orig
178 devlink_pool_size_thtype()
182 devlink sb pool show "$DEVLINK_DEV" pool $pool -j \
183 | jq -r '.pool[][] | (.size, .thtype)'
186 devlink_pool_size_thtype_save()
189 local key="pool($pool).size_thtype"
191 DEVLINK_ORIG[$key]=$(devlink_pool_size_thtype $pool)
194 devlink_pool_size_thtype_set()
197 local thtype=$1; shift
200 devlink sb pool set "$DEVLINK_DEV" pool $pool size $size thtype $thtype
203 devlink_pool_size_thtype_restore()
206 local key="pool($pool).size_thtype"
207 local -a orig=(${DEVLINK_ORIG[$key]})
209 if [[ -z ${orig[0]} ]]; then
210 echo "WARNING: Mismatched devlink_pool_size_thtype_restore"
212 devlink sb pool set "$DEVLINK_DEV" pool $pool \
213 size ${orig[0]} thtype ${orig[1]}
217 devlink_tc_bind_pool_th()
223 devlink sb tc bind show $port tc $tc type $dir -j \
224 | jq -r '.tc_bind[][] | (.pool, .threshold)'
227 devlink_tc_bind_pool_th_save()
232 local key="tc_bind($port,$dir,$tc).pool_th"
234 DEVLINK_ORIG[$key]=$(devlink_tc_bind_pool_th $port $tc $dir)
237 devlink_tc_bind_pool_th_set()
245 devlink sb tc bind set $port tc $tc type $dir pool $pool th $th
248 devlink_tc_bind_pool_th_restore()
253 local key="tc_bind($port,$dir,$tc).pool_th"
254 local -a orig=(${DEVLINK_ORIG[$key]})
256 if [[ -z ${orig[0]} ]]; then
257 echo "WARNING: Mismatched devlink_tc_bind_pool_th_restore"
259 devlink sb tc bind set $port tc $tc type $dir \
260 pool ${orig[0]} th ${orig[1]}
264 devlink_traps_num_get()
266 devlink -j trap | jq '.[]["'$DEVLINK_DEV'"] | length'
271 devlink -j trap | jq -r '.[]["'$DEVLINK_DEV'"][].name'
274 devlink_trap_type_get()
276 local trap_name=$1; shift
278 devlink -j trap show $DEVLINK_DEV trap $trap_name \
279 | jq -r '.[][][].type'
282 devlink_trap_action_set()
284 local trap_name=$1; shift
285 local action=$1; shift
287 # Pipe output to /dev/null to avoid expected warnings.
288 devlink trap set $DEVLINK_DEV trap $trap_name \
289 action $action &> /dev/null
292 devlink_trap_action_get()
294 local trap_name=$1; shift
296 devlink -j trap show $DEVLINK_DEV trap $trap_name \
297 | jq -r '.[][][].action'
300 devlink_trap_group_get()
302 devlink -j trap show $DEVLINK_DEV trap $trap_name \
303 | jq -r '.[][][].group'
306 devlink_trap_metadata_test()
308 local trap_name=$1; shift
309 local metadata=$1; shift
311 devlink -jv trap show $DEVLINK_DEV trap $trap_name \
312 | jq -e '.[][][].metadata | contains(["'$metadata'"])' \
316 devlink_trap_rx_packets_get()
318 local trap_name=$1; shift
320 devlink -js trap show $DEVLINK_DEV trap $trap_name \
321 | jq '.[][][]["stats"]["rx"]["packets"]'
324 devlink_trap_rx_bytes_get()
326 local trap_name=$1; shift
328 devlink -js trap show $DEVLINK_DEV trap $trap_name \
329 | jq '.[][][]["stats"]["rx"]["bytes"]'
332 devlink_trap_drop_packets_get()
334 local trap_name=$1; shift
336 devlink -js trap show $DEVLINK_DEV trap $trap_name \
337 | jq '.[][][]["stats"]["rx"]["dropped"]'
340 devlink_trap_stats_idle_test()
342 local trap_name=$1; shift
343 local t0_packets t0_bytes
344 local t1_packets t1_bytes
346 t0_packets=$(devlink_trap_rx_packets_get $trap_name)
347 t0_bytes=$(devlink_trap_rx_bytes_get $trap_name)
351 t1_packets=$(devlink_trap_rx_packets_get $trap_name)
352 t1_bytes=$(devlink_trap_rx_bytes_get $trap_name)
354 if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then
361 devlink_trap_drop_stats_idle_test()
363 local trap_name=$1; shift
364 local t0_packets t0_bytes
366 t0_packets=$(devlink_trap_drop_packets_get $trap_name)
370 t1_packets=$(devlink_trap_drop_packets_get $trap_name)
372 if [[ $t0_packets -eq $t1_packets ]]; then
379 devlink_traps_enable_all()
383 for trap_name in $(devlink_traps_get); do
384 devlink_trap_action_set $trap_name "trap"
388 devlink_traps_disable_all()
390 for trap_name in $(devlink_traps_get); do
391 devlink_trap_action_set $trap_name "drop"
395 devlink_trap_groups_get()
397 devlink -j trap group | jq -r '.[]["'$DEVLINK_DEV'"][].name'
400 devlink_trap_group_action_set()
402 local group_name=$1; shift
403 local action=$1; shift
405 # Pipe output to /dev/null to avoid expected warnings.
406 devlink trap group set $DEVLINK_DEV group $group_name action $action \
410 devlink_trap_group_rx_packets_get()
412 local group_name=$1; shift
414 devlink -js trap group show $DEVLINK_DEV group $group_name \
415 | jq '.[][][]["stats"]["rx"]["packets"]'
418 devlink_trap_group_rx_bytes_get()
420 local group_name=$1; shift
422 devlink -js trap group show $DEVLINK_DEV group $group_name \
423 | jq '.[][][]["stats"]["rx"]["bytes"]'
426 devlink_trap_group_stats_idle_test()
428 local group_name=$1; shift
429 local t0_packets t0_bytes
430 local t1_packets t1_bytes
432 t0_packets=$(devlink_trap_group_rx_packets_get $group_name)
433 t0_bytes=$(devlink_trap_group_rx_bytes_get $group_name)
437 t1_packets=$(devlink_trap_group_rx_packets_get $group_name)
438 t1_bytes=$(devlink_trap_group_rx_bytes_get $group_name)
440 if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then
447 devlink_trap_exception_test()
449 local trap_name=$1; shift
452 group_name=$(devlink_trap_group_get $trap_name)
454 devlink_trap_stats_idle_test $trap_name
455 check_fail $? "Trap stats idle when packets should have been trapped"
457 devlink_trap_group_stats_idle_test $group_name
458 check_fail $? "Trap group idle when packets should have been trapped"
461 devlink_trap_drop_test()
463 local trap_name=$1; shift
465 local handle=$1; shift
468 group_name=$(devlink_trap_group_get $trap_name)
470 # This is the common part of all the tests. It checks that stats are
471 # initially idle, then non-idle after changing the trap action and
472 # finally idle again. It also makes sure the packets are dropped and
474 devlink_trap_stats_idle_test $trap_name
475 check_err $? "Trap stats not idle with initial drop action"
476 devlink_trap_group_stats_idle_test $group_name
477 check_err $? "Trap group stats not idle with initial drop action"
479 devlink_trap_action_set $trap_name "trap"
480 devlink_trap_stats_idle_test $trap_name
481 check_fail $? "Trap stats idle after setting action to trap"
482 devlink_trap_group_stats_idle_test $group_name
483 check_fail $? "Trap group stats idle after setting action to trap"
485 devlink_trap_action_set $trap_name "drop"
487 devlink_trap_stats_idle_test $trap_name
488 check_err $? "Trap stats not idle after setting action to drop"
489 devlink_trap_group_stats_idle_test $group_name
490 check_err $? "Trap group stats not idle after setting action to drop"
492 tc_check_packets "dev $dev egress" $handle 0
493 check_err $? "Packets were not dropped"
496 devlink_trap_drop_cleanup()
498 local mz_pid=$1; shift
500 local proto=$1; shift
502 local handle=$1; shift
505 tc filter del dev $dev egress protocol $proto pref $pref handle $handle flower
508 devlink_trap_stats_check()
510 local trap_name=$1; shift
515 t0_packets=$(devlink_trap_rx_packets_get $trap_name)
519 t1_packets=$(devlink_trap_rx_packets_get $trap_name)
521 [[ $t1_packets -ne $t0_packets ]]
524 devlink_trap_stats_test()
526 local test_name=$1; shift
530 devlink_trap_stats_check "$@"
531 check_err $? "Trap stats did not increase"
533 log_test "$test_name"
536 devlink_trap_policers_num_get()
538 devlink -j -p trap policer show | jq '.[]["'$DEVLINK_DEV'"] | length'
541 devlink_trap_policer_rate_get()
543 local policer_id=$1; shift
545 devlink -j -p trap policer show $DEVLINK_DEV policer $policer_id \
546 | jq '.[][][]["rate"]'
549 devlink_trap_policer_burst_get()
551 local policer_id=$1; shift
553 devlink -j -p trap policer show $DEVLINK_DEV policer $policer_id \
554 | jq '.[][][]["burst"]'
557 devlink_trap_policer_rx_dropped_get()
559 local policer_id=$1; shift
561 devlink -j -p -s trap policer show $DEVLINK_DEV policer $policer_id \
562 | jq '.[][][]["stats"]["rx"]["dropped"]'
565 devlink_trap_group_policer_get()
567 local group_name=$1; shift
569 devlink -j -p trap group show $DEVLINK_DEV group $group_name \
570 | jq '.[][][]["policer"]'
573 devlink_port_by_netdev()
577 devlink -j port show $if_name | jq -e '.[] | keys' | jq -r '.[]'
580 devlink_cpu_port_get()
582 local cpu_dl_port_num=$(devlink port list | grep "$DEVLINK_DEV" |
583 grep cpu | cut -d/ -f3 | cut -d: -f1 |
586 echo "$DEVLINK_DEV/$cpu_dl_port_num"
589 devlink_cell_size_get()
591 devlink sb pool show "$DEVLINK_DEV" pool 0 -j \
592 | jq '.pool[][].cell_size'
595 devlink_pool_size_get()
597 devlink sb show "$DEVLINK_DEV" -j | jq '.[][][]["size"]'