drm/atomic-helper: document drm_atomic_helper_check() restrictions
[drm/drm-misc.git] / tools / testing / selftests / net / forwarding / devlink_lib.sh
blob18afa89ebbcc07d34d95dab3392a8df4f742dc95
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 # Kselftest framework requirement - SKIP code is 4.
5 ksft_skip=4
7 ##############################################################################
8 # Defines
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"
15 exit $ksft_skip
17 if [[ "$(echo $DEVLINK_DEV | grep -c pci)" -eq 0 ]]; then
18 echo "SKIP: devlink device's bus is not PCI"
19 exit $ksft_skip
22 DEVLINK_VIDDID=$(lspci -s $(echo $DEVLINK_DEV | cut -d"/" -f2) \
23 -n | cut -d" " -f3)
24 elif [[ ! -z "$DEVLINK_DEV" ]]; then
25 devlink dev show $DEVLINK_DEV &> /dev/null
26 if [ $? -ne 0 ]; then
27 echo "SKIP: devlink device \"$DEVLINK_DEV\" not found"
28 exit $ksft_skip
32 ##############################################################################
33 # Sanity checks
35 devlink help 2>&1 | grep resource &> /dev/null
36 if [ $? -ne 0 ]; then
37 echo "SKIP: iproute2 too old, missing devlink resource support"
38 exit $ksft_skip
41 devlink help 2>&1 | grep trap &> /dev/null
42 if [ $? -ne 0 ]; then
43 echo "SKIP: iproute2 too old, missing devlink trap support"
44 exit $ksft_skip
47 devlink dev help 2>&1 | grep info &> /dev/null
48 if [ $? -ne 0 ]; then
49 echo "SKIP: iproute2 too old, missing devlink dev info support"
50 exit $ksft_skip
53 ##############################################################################
54 # Devlink helpers
56 devlink_resource_names_to_path()
58 local resource
59 local path=""
61 for resource in "${@}"; do
62 if [ "$path" == "" ]; then
63 path="$resource"
64 else
65 path="${path}/$resource"
67 done
69 echo "$path"
72 devlink_resource_get()
74 local name=$1
75 local resource_name=.[][\"$DEVLINK_DEV\"]
77 resource_name="$resource_name | .[] | select (.name == \"$name\")"
79 shift
80 for resource in "${@}"; do
81 resource_name="${resource_name} | .[\"resources\"][] | \
82 select (.name == \"$resource\")"
83 done
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"]'
94 else
95 echo "$size"
99 devlink_resource_size_set()
101 local new_size=$1
102 local path
104 shift
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"]'
115 devlink_reload()
117 local still_pending
119 devlink dev reload "$DEVLINK_DEV" &> /dev/null
120 check_err $? "Failed reload"
122 still_pending=$(devlink resource show "$DEVLINK_DEV" | \
123 grep -c "size_new")
124 check_err $still_pending "Failed reload - There are still unset sizes"
126 udevadm settle
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()
139 local port=$1; shift
140 local pool=$1; shift
142 devlink sb port pool show $port pool $pool -j \
143 | jq '.port_pool."'"$port"'"[].threshold'
146 devlink_port_pool_th_save()
148 local port=$1; shift
149 local pool=$1; shift
150 local key="port_pool($port,$pool).threshold"
152 DEVLINK_ORIG[$key]=$(devlink_port_pool_threshold $port $pool)
155 devlink_port_pool_th_set()
157 local port=$1; shift
158 local pool=$1; shift
159 local th=$1; shift
161 devlink sb port pool set $port pool $pool th $th
164 devlink_port_pool_th_restore()
166 local port=$1; shift
167 local pool=$1; shift
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"
173 else
174 devlink sb port pool set $port pool $pool th $orig
178 devlink_pool_size_thtype()
180 local pool=$1; shift
182 devlink sb pool show "$DEVLINK_DEV" pool $pool -j \
183 | jq -r '.pool[][] | (.size, .thtype)'
186 devlink_pool_size_thtype_save()
188 local pool=$1; shift
189 local key="pool($pool).size_thtype"
191 DEVLINK_ORIG[$key]=$(devlink_pool_size_thtype $pool)
194 devlink_pool_size_thtype_set()
196 local pool=$1; shift
197 local thtype=$1; shift
198 local size=$1; shift
200 devlink sb pool set "$DEVLINK_DEV" pool $pool size $size thtype $thtype
203 devlink_pool_size_thtype_restore()
205 local pool=$1; shift
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"
211 else
212 devlink sb pool set "$DEVLINK_DEV" pool $pool \
213 size ${orig[0]} thtype ${orig[1]}
217 devlink_tc_bind_pool_th()
219 local port=$1; shift
220 local tc=$1; shift
221 local dir=$1; shift
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()
229 local port=$1; shift
230 local tc=$1; shift
231 local dir=$1; shift
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()
239 local port=$1; shift
240 local tc=$1; shift
241 local dir=$1; shift
242 local pool=$1; shift
243 local th=$1; shift
245 devlink sb tc bind set $port tc $tc type $dir pool $pool th $th
248 devlink_tc_bind_pool_th_restore()
250 local port=$1; shift
251 local tc=$1; shift
252 local dir=$1; shift
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"
258 else
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'
269 devlink_traps_get()
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'"])' \
313 &> /dev/null
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)
349 sleep 1
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
355 return 0
356 else
357 return 1
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)
368 sleep 1
370 t1_packets=$(devlink_trap_drop_packets_get $trap_name)
372 if [[ $t0_packets -eq $t1_packets ]]; then
373 return 0
374 else
375 return 1
379 devlink_traps_enable_all()
381 local trap_name
383 for trap_name in $(devlink_traps_get); do
384 devlink_trap_action_set $trap_name "trap"
385 done
388 devlink_traps_disable_all()
390 for trap_name in $(devlink_traps_get); do
391 devlink_trap_action_set $trap_name "drop"
392 done
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 \
407 &> /dev/null
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)
435 sleep 1
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
441 return 0
442 else
443 return 1
447 devlink_trap_exception_test()
449 local trap_name=$1; shift
450 local group_name
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
464 local dev=$1; shift
465 local handle=$1; shift
466 local group_name
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
473 # never forwarded.
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
499 local dev=$1; shift
500 local proto=$1; shift
501 local pref=$1; shift
502 local handle=$1; shift
504 kill_process $mz_pid
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
511 local send_one="$@"
512 local t0_packets
513 local t1_packets
515 t0_packets=$(devlink_trap_rx_packets_get $trap_name)
517 $send_one && sleep 1
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
528 RET=0
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()
575 local if_name=$1
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 |
584 sed -n '1p')
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"]'