WIP FPC-III support
[linux/fpc-iii.git] / tools / testing / selftests / net / forwarding / devlink_lib.sh
blob9c12c4fd3afc942699dc17f87ef0a6fd5a6ac9b9
1 #!/bin/bash
2 # SPDX-License-Identifier: GPL-2.0
4 ##############################################################################
5 # Defines
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"
12 exit 1
14 if [[ "$(echo $DEVLINK_DEV | grep -c pci)" -eq 0 ]]; then
15 echo "SKIP: devlink device's bus is not PCI"
16 exit 1
19 DEVLINK_VIDDID=$(lspci -s $(echo $DEVLINK_DEV | cut -d"/" -f2) \
20 -n | cut -d" " -f3)
23 ##############################################################################
24 # Sanity checks
26 devlink help 2>&1 | grep resource &> /dev/null
27 if [ $? -ne 0 ]; then
28 echo "SKIP: iproute2 too old, missing devlink resource support"
29 exit 1
32 devlink help 2>&1 | grep trap &> /dev/null
33 if [ $? -ne 0 ]; then
34 echo "SKIP: iproute2 too old, missing devlink trap support"
35 exit 1
38 devlink dev help 2>&1 | grep info &> /dev/null
39 if [ $? -ne 0 ]; then
40 echo "SKIP: iproute2 too old, missing devlink dev info support"
41 exit 1
44 ##############################################################################
45 # Devlink helpers
47 devlink_resource_names_to_path()
49 local resource
50 local path=""
52 for resource in "${@}"; do
53 if [ "$path" == "" ]; then
54 path="$resource"
55 else
56 path="${path}/$resource"
58 done
60 echo "$path"
63 devlink_resource_get()
65 local name=$1
66 local resource_name=.[][\"$DEVLINK_DEV\"]
68 resource_name="$resource_name | .[] | select (.name == \"$name\")"
70 shift
71 for resource in "${@}"; do
72 resource_name="${resource_name} | .[\"resources\"][] | \
73 select (.name == \"$resource\")"
74 done
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"]'
85 else
86 echo "$size"
90 devlink_resource_size_set()
92 local new_size=$1
93 local path
95 shift
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"]'
106 devlink_reload()
108 local still_pending
110 devlink dev reload "$DEVLINK_DEV" &> /dev/null
111 check_err $? "Failed reload"
113 still_pending=$(devlink resource show "$DEVLINK_DEV" | \
114 grep -c "size_new")
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()
128 local port=$1; shift
129 local pool=$1; shift
131 devlink sb port pool show $port pool $pool -j \
132 | jq '.port_pool."'"$port"'"[].threshold'
135 devlink_port_pool_th_save()
137 local port=$1; shift
138 local pool=$1; shift
139 local key="port_pool($port,$pool).threshold"
141 DEVLINK_ORIG[$key]=$(devlink_port_pool_threshold $port $pool)
144 devlink_port_pool_th_set()
146 local port=$1; shift
147 local pool=$1; shift
148 local th=$1; shift
150 devlink sb port pool set $port pool $pool th $th
153 devlink_port_pool_th_restore()
155 local port=$1; shift
156 local pool=$1; shift
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"
162 else
163 devlink sb port pool set $port pool $pool th $orig
167 devlink_pool_size_thtype()
169 local pool=$1; shift
171 devlink sb pool show "$DEVLINK_DEV" pool $pool -j \
172 | jq -r '.pool[][] | (.size, .thtype)'
175 devlink_pool_size_thtype_save()
177 local pool=$1; shift
178 local key="pool($pool).size_thtype"
180 DEVLINK_ORIG[$key]=$(devlink_pool_size_thtype $pool)
183 devlink_pool_size_thtype_set()
185 local pool=$1; shift
186 local thtype=$1; shift
187 local size=$1; shift
189 devlink sb pool set "$DEVLINK_DEV" pool $pool size $size thtype $thtype
192 devlink_pool_size_thtype_restore()
194 local pool=$1; shift
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"
200 else
201 devlink sb pool set "$DEVLINK_DEV" pool $pool \
202 size ${orig[0]} thtype ${orig[1]}
206 devlink_tc_bind_pool_th()
208 local port=$1; shift
209 local tc=$1; shift
210 local dir=$1; shift
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()
218 local port=$1; shift
219 local tc=$1; shift
220 local dir=$1; shift
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()
228 local port=$1; shift
229 local tc=$1; shift
230 local dir=$1; shift
231 local pool=$1; shift
232 local th=$1; shift
234 devlink sb tc bind set $port tc $tc type $dir pool $pool th $th
237 devlink_tc_bind_pool_th_restore()
239 local port=$1; shift
240 local tc=$1; shift
241 local dir=$1; shift
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"
247 else
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'
258 devlink_traps_get()
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'"])' \
302 &> /dev/null
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)
330 sleep 1
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
336 return 0
337 else
338 return 1
342 devlink_traps_enable_all()
344 local trap_name
346 for trap_name in $(devlink_traps_get); do
347 devlink_trap_action_set $trap_name "trap"
348 done
351 devlink_traps_disable_all()
353 for trap_name in $(devlink_traps_get); do
354 devlink_trap_action_set $trap_name "drop"
355 done
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 \
370 &> /dev/null
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)
398 sleep 1
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
404 return 0
405 else
406 return 1
410 devlink_trap_exception_test()
412 local trap_name=$1; shift
413 local group_name
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
427 local dev=$1; shift
428 local handle=$1; shift
429 local group_name
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
436 # never forwarded.
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
462 local dev=$1; shift
463 local proto=$1; shift
464 local pref=$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
475 local send_one="$@"
476 local t0_packets
477 local t1_packets
479 RET=0
481 t0_packets=$(devlink_trap_rx_packets_get $trap_name)
483 $send_one && sleep 1
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()
539 local if_name=$1
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 |
548 sed -n '1p')
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'