2 # SPDX-License-Identifier: GPL-2.0
4 ##############################################################################
7 if [[ ! -v DEVLINK_DEV
]]; then
8 DEVLINK_DEV
=$
(devlink port show
"${NETIFS[p1]}" -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 ##############################################################################
41 devlink_resource_names_to_path
()
46 for resource
in "${@}"; do
47 if [ "$path" == "" ]; then
50 path
="${path}/$resource"
57 devlink_resource_get
()
60 local resource_name
=.
[][\"$DEVLINK_DEV\"]
62 resource_name
="$resource_name | .[] | select (.name == \"$name\")"
65 for resource
in "${@}"; do
66 resource_name
="${resource_name} | .[\"resources\"][] | \
67 select (.name == \"$resource\")"
70 devlink
-j resource show
"$DEVLINK_DEV" | jq
"$resource_name"
73 devlink_resource_size_get
()
75 local size
=$
(devlink_resource_get
"$@" | jq
'.["size_new"]')
77 if [ "$size" == "null" ]; then
78 devlink_resource_get
"$@" | jq
'.["size"]'
84 devlink_resource_size_set
()
90 path
=$
(devlink_resource_names_to_path
"$@")
91 devlink resource
set "$DEVLINK_DEV" path
"$path" size
"$new_size"
92 check_err $?
"Failed setting path $path to size $size"
99 devlink dev reload
"$DEVLINK_DEV" &> /dev
/null
100 check_err $?
"Failed reload"
102 still_pending
=$
(devlink resource show
"$DEVLINK_DEV" | \
104 check_err
$still_pending "Failed reload - There are still unset sizes"
107 declare -A DEVLINK_ORIG
109 devlink_port_pool_threshold
()
114 devlink sb port pool show
$port pool
$pool -j \
115 | jq
'.port_pool."'"$port"'"[].threshold'
118 devlink_port_pool_th_set
()
123 local key
="port_pool($port,$pool).threshold"
125 DEVLINK_ORIG
[$key]=$
(devlink_port_pool_threshold
$port $pool)
126 devlink sb port pool
set $port pool
$pool th
$th
129 devlink_port_pool_th_restore
()
133 local key
="port_pool($port,$pool).threshold"
135 devlink sb port pool
set $port pool
$pool th
${DEVLINK_ORIG[$key]}
138 devlink_pool_size_thtype
()
142 devlink sb pool show
"$DEVLINK_DEV" pool
$pool -j \
143 | jq
-r '.pool[][] | (.size, .thtype)'
146 devlink_pool_size_thtype_set
()
149 local thtype
=$1; shift
151 local key
="pool($pool).size_thtype"
153 DEVLINK_ORIG
[$key]=$
(devlink_pool_size_thtype
$pool)
154 devlink sb pool
set "$DEVLINK_DEV" pool
$pool size
$size thtype
$thtype
157 devlink_pool_size_thtype_restore
()
160 local key
="pool($pool).size_thtype"
161 local -a orig
=(${DEVLINK_ORIG[$key]})
163 devlink sb pool
set "$DEVLINK_DEV" pool
$pool \
164 size
${orig[0]} thtype
${orig[1]}
167 devlink_tc_bind_pool_th
()
173 devlink sb tc
bind show
$port tc
$tc type $dir -j \
174 | jq
-r '.tc_bind[][] | (.pool, .threshold)'
177 devlink_tc_bind_pool_th_set
()
184 local key
="tc_bind($port,$dir,$tc).pool_th"
186 DEVLINK_ORIG
[$key]=$
(devlink_tc_bind_pool_th
$port $tc $dir)
187 devlink sb tc
bind set $port tc
$tc type $dir pool
$pool th
$th
190 devlink_tc_bind_pool_th_restore
()
195 local key
="tc_bind($port,$dir,$tc).pool_th"
196 local -a orig
=(${DEVLINK_ORIG[$key]})
198 devlink sb tc
bind set $port tc
$tc type $dir \
199 pool
${orig[0]} th
${orig[1]}
202 devlink_traps_num_get
()
204 devlink
-j trap | jq
'.[]["'$DEVLINK_DEV'"] | length'
209 devlink
-j trap | jq
-r '.[]["'$DEVLINK_DEV'"][].name'
212 devlink_trap_type_get
()
214 local trap_name
=$1; shift
216 devlink
-j trap show
$DEVLINK_DEV trap $trap_name \
217 | jq
-r '.[][][].type'
220 devlink_trap_action_set
()
222 local trap_name
=$1; shift
223 local action
=$1; shift
225 # Pipe output to /dev/null to avoid expected warnings.
226 devlink
trap set $DEVLINK_DEV trap $trap_name \
227 action
$action &> /dev
/null
230 devlink_trap_action_get
()
232 local trap_name
=$1; shift
234 devlink
-j trap show
$DEVLINK_DEV trap $trap_name \
235 | jq
-r '.[][][].action'
238 devlink_trap_group_get
()
240 devlink
-j trap show
$DEVLINK_DEV trap $trap_name \
241 | jq
-r '.[][][].group'
244 devlink_trap_metadata_test
()
246 local trap_name
=$1; shift
247 local metadata
=$1; shift
249 devlink
-jv trap show
$DEVLINK_DEV trap $trap_name \
250 | jq
-e '.[][][].metadata | contains(["'$metadata'"])' \
254 devlink_trap_rx_packets_get
()
256 local trap_name
=$1; shift
258 devlink
-js trap show
$DEVLINK_DEV trap $trap_name \
259 | jq
'.[][][]["stats"]["rx"]["packets"]'
262 devlink_trap_rx_bytes_get
()
264 local trap_name
=$1; shift
266 devlink
-js trap show
$DEVLINK_DEV trap $trap_name \
267 | jq
'.[][][]["stats"]["rx"]["bytes"]'
270 devlink_trap_stats_idle_test
()
272 local trap_name
=$1; shift
273 local t0_packets t0_bytes
274 local t1_packets t1_bytes
276 t0_packets
=$
(devlink_trap_rx_packets_get
$trap_name)
277 t0_bytes
=$
(devlink_trap_rx_bytes_get
$trap_name)
281 t1_packets
=$
(devlink_trap_rx_packets_get
$trap_name)
282 t1_bytes
=$
(devlink_trap_rx_bytes_get
$trap_name)
284 if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then
291 devlink_traps_enable_all
()
295 for trap_name
in $
(devlink_traps_get
); do
296 devlink_trap_action_set
$trap_name "trap"
300 devlink_traps_disable_all
()
302 for trap_name
in $
(devlink_traps_get
); do
303 devlink_trap_action_set
$trap_name "drop"
307 devlink_trap_groups_get
()
309 devlink
-j trap group | jq
-r '.[]["'$DEVLINK_DEV'"][].name'
312 devlink_trap_group_action_set
()
314 local group_name
=$1; shift
315 local action
=$1; shift
317 # Pipe output to /dev/null to avoid expected warnings.
318 devlink
trap group
set $DEVLINK_DEV group
$group_name action
$action \
322 devlink_trap_group_rx_packets_get
()
324 local group_name
=$1; shift
326 devlink
-js trap group show
$DEVLINK_DEV group
$group_name \
327 | jq
'.[][][]["stats"]["rx"]["packets"]'
330 devlink_trap_group_rx_bytes_get
()
332 local group_name
=$1; shift
334 devlink
-js trap group show
$DEVLINK_DEV group
$group_name \
335 | jq
'.[][][]["stats"]["rx"]["bytes"]'
338 devlink_trap_group_stats_idle_test
()
340 local group_name
=$1; shift
341 local t0_packets t0_bytes
342 local t1_packets t1_bytes
344 t0_packets
=$
(devlink_trap_group_rx_packets_get
$group_name)
345 t0_bytes
=$
(devlink_trap_group_rx_bytes_get
$group_name)
349 t1_packets
=$
(devlink_trap_group_rx_packets_get
$group_name)
350 t1_bytes
=$
(devlink_trap_group_rx_bytes_get
$group_name)
352 if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then