1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3 4############################################################################## 5# Defines 6 7if [[ ! -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 13 fi 14 if [[ "$(echo $DEVLINK_DEV | grep -c pci)" -eq 0 ]]; then 15 echo "SKIP: devlink device's bus is not PCI" 16 exit 1 17 fi 18 19 DEVLINK_VIDDID=$(lspci -s $(echo $DEVLINK_DEV | cut -d"/" -f2) \ 20 -n | cut -d" " -f3) 21else 22 devlink dev show $DEVLINK_DEV &> /dev/null 23 if [ $? -ne 0 ]; then 24 echo "SKIP: devlink device \"$DEVLINK_DEV\" not found" 25 exit 1 26 fi 27fi 28 29############################################################################## 30# Sanity checks 31 32devlink help 2>&1 | grep resource &> /dev/null 33if [ $? -ne 0 ]; then 34 echo "SKIP: iproute2 too old, missing devlink resource support" 35 exit 1 36fi 37 38devlink help 2>&1 | grep trap &> /dev/null 39if [ $? -ne 0 ]; then 40 echo "SKIP: iproute2 too old, missing devlink trap support" 41 exit 1 42fi 43 44devlink dev help 2>&1 | grep info &> /dev/null 45if [ $? -ne 0 ]; then 46 echo "SKIP: iproute2 too old, missing devlink dev info support" 47 exit 1 48fi 49 50############################################################################## 51# Devlink helpers 52 53devlink_resource_names_to_path() 54{ 55 local resource 56 local path="" 57 58 for resource in "${@}"; do 59 if [ "$path" == "" ]; then 60 path="$resource" 61 else 62 path="${path}/$resource" 63 fi 64 done 65 66 echo "$path" 67} 68 69devlink_resource_get() 70{ 71 local name=$1 72 local resource_name=.[][\"$DEVLINK_DEV\"] 73 74 resource_name="$resource_name | .[] | select (.name == \"$name\")" 75 76 shift 77 for resource in "${@}"; do 78 resource_name="${resource_name} | .[\"resources\"][] | \ 79 select (.name == \"$resource\")" 80 done 81 82 devlink -j resource show "$DEVLINK_DEV" | jq "$resource_name" 83} 84 85devlink_resource_size_get() 86{ 87 local size=$(devlink_resource_get "$@" | jq '.["size_new"]') 88 89 if [ "$size" == "null" ]; then 90 devlink_resource_get "$@" | jq '.["size"]' 91 else 92 echo "$size" 93 fi 94} 95 96devlink_resource_size_set() 97{ 98 local new_size=$1 99 local path 100 101 shift 102 path=$(devlink_resource_names_to_path "$@") 103 devlink resource set "$DEVLINK_DEV" path "$path" size "$new_size" 104 check_err $? "Failed setting path $path to size $size" 105} 106 107devlink_resource_occ_get() 108{ 109 devlink_resource_get "$@" | jq '.["occ"]' 110} 111 112devlink_reload() 113{ 114 local still_pending 115 116 devlink dev reload "$DEVLINK_DEV" &> /dev/null 117 check_err $? "Failed reload" 118 119 still_pending=$(devlink resource show "$DEVLINK_DEV" | \ 120 grep -c "size_new") 121 check_err $still_pending "Failed reload - There are still unset sizes" 122} 123 124declare -A DEVLINK_ORIG 125 126# Changing pool type from static to dynamic causes reinterpretation of threshold 127# values. They therefore need to be saved before pool type is changed, then the 128# pool type can be changed, and then the new values need to be set up. Therefore 129# instead of saving the current state implicitly in the _set call, provide 130# functions for all three primitives: save, set, and restore. 131 132devlink_port_pool_threshold() 133{ 134 local port=$1; shift 135 local pool=$1; shift 136 137 devlink sb port pool show $port pool $pool -j \ 138 | jq '.port_pool."'"$port"'"[].threshold' 139} 140 141devlink_port_pool_th_save() 142{ 143 local port=$1; shift 144 local pool=$1; shift 145 local key="port_pool($port,$pool).threshold" 146 147 DEVLINK_ORIG[$key]=$(devlink_port_pool_threshold $port $pool) 148} 149 150devlink_port_pool_th_set() 151{ 152 local port=$1; shift 153 local pool=$1; shift 154 local th=$1; shift 155 156 devlink sb port pool set $port pool $pool th $th 157} 158 159devlink_port_pool_th_restore() 160{ 161 local port=$1; shift 162 local pool=$1; shift 163 local key="port_pool($port,$pool).threshold" 164 local -a orig=(${DEVLINK_ORIG[$key]}) 165 166 if [[ -z $orig ]]; then 167 echo "WARNING: Mismatched devlink_port_pool_th_restore" 168 else 169 devlink sb port pool set $port pool $pool th $orig 170 fi 171} 172 173devlink_pool_size_thtype() 174{ 175 local pool=$1; shift 176 177 devlink sb pool show "$DEVLINK_DEV" pool $pool -j \ 178 | jq -r '.pool[][] | (.size, .thtype)' 179} 180 181devlink_pool_size_thtype_save() 182{ 183 local pool=$1; shift 184 local key="pool($pool).size_thtype" 185 186 DEVLINK_ORIG[$key]=$(devlink_pool_size_thtype $pool) 187} 188 189devlink_pool_size_thtype_set() 190{ 191 local pool=$1; shift 192 local thtype=$1; shift 193 local size=$1; shift 194 195 devlink sb pool set "$DEVLINK_DEV" pool $pool size $size thtype $thtype 196} 197 198devlink_pool_size_thtype_restore() 199{ 200 local pool=$1; shift 201 local key="pool($pool).size_thtype" 202 local -a orig=(${DEVLINK_ORIG[$key]}) 203 204 if [[ -z ${orig[0]} ]]; then 205 echo "WARNING: Mismatched devlink_pool_size_thtype_restore" 206 else 207 devlink sb pool set "$DEVLINK_DEV" pool $pool \ 208 size ${orig[0]} thtype ${orig[1]} 209 fi 210} 211 212devlink_tc_bind_pool_th() 213{ 214 local port=$1; shift 215 local tc=$1; shift 216 local dir=$1; shift 217 218 devlink sb tc bind show $port tc $tc type $dir -j \ 219 | jq -r '.tc_bind[][] | (.pool, .threshold)' 220} 221 222devlink_tc_bind_pool_th_save() 223{ 224 local port=$1; shift 225 local tc=$1; shift 226 local dir=$1; shift 227 local key="tc_bind($port,$dir,$tc).pool_th" 228 229 DEVLINK_ORIG[$key]=$(devlink_tc_bind_pool_th $port $tc $dir) 230} 231 232devlink_tc_bind_pool_th_set() 233{ 234 local port=$1; shift 235 local tc=$1; shift 236 local dir=$1; shift 237 local pool=$1; shift 238 local th=$1; shift 239 240 devlink sb tc bind set $port tc $tc type $dir pool $pool th $th 241} 242 243devlink_tc_bind_pool_th_restore() 244{ 245 local port=$1; shift 246 local tc=$1; shift 247 local dir=$1; shift 248 local key="tc_bind($port,$dir,$tc).pool_th" 249 local -a orig=(${DEVLINK_ORIG[$key]}) 250 251 if [[ -z ${orig[0]} ]]; then 252 echo "WARNING: Mismatched devlink_tc_bind_pool_th_restore" 253 else 254 devlink sb tc bind set $port tc $tc type $dir \ 255 pool ${orig[0]} th ${orig[1]} 256 fi 257} 258 259devlink_traps_num_get() 260{ 261 devlink -j trap | jq '.[]["'$DEVLINK_DEV'"] | length' 262} 263 264devlink_traps_get() 265{ 266 devlink -j trap | jq -r '.[]["'$DEVLINK_DEV'"][].name' 267} 268 269devlink_trap_type_get() 270{ 271 local trap_name=$1; shift 272 273 devlink -j trap show $DEVLINK_DEV trap $trap_name \ 274 | jq -r '.[][][].type' 275} 276 277devlink_trap_action_set() 278{ 279 local trap_name=$1; shift 280 local action=$1; shift 281 282 # Pipe output to /dev/null to avoid expected warnings. 283 devlink trap set $DEVLINK_DEV trap $trap_name \ 284 action $action &> /dev/null 285} 286 287devlink_trap_action_get() 288{ 289 local trap_name=$1; shift 290 291 devlink -j trap show $DEVLINK_DEV trap $trap_name \ 292 | jq -r '.[][][].action' 293} 294 295devlink_trap_group_get() 296{ 297 devlink -j trap show $DEVLINK_DEV trap $trap_name \ 298 | jq -r '.[][][].group' 299} 300 301devlink_trap_metadata_test() 302{ 303 local trap_name=$1; shift 304 local metadata=$1; shift 305 306 devlink -jv trap show $DEVLINK_DEV trap $trap_name \ 307 | jq -e '.[][][].metadata | contains(["'$metadata'"])' \ 308 &> /dev/null 309} 310 311devlink_trap_rx_packets_get() 312{ 313 local trap_name=$1; shift 314 315 devlink -js trap show $DEVLINK_DEV trap $trap_name \ 316 | jq '.[][][]["stats"]["rx"]["packets"]' 317} 318 319devlink_trap_rx_bytes_get() 320{ 321 local trap_name=$1; shift 322 323 devlink -js trap show $DEVLINK_DEV trap $trap_name \ 324 | jq '.[][][]["stats"]["rx"]["bytes"]' 325} 326 327devlink_trap_stats_idle_test() 328{ 329 local trap_name=$1; shift 330 local t0_packets t0_bytes 331 local t1_packets t1_bytes 332 333 t0_packets=$(devlink_trap_rx_packets_get $trap_name) 334 t0_bytes=$(devlink_trap_rx_bytes_get $trap_name) 335 336 sleep 1 337 338 t1_packets=$(devlink_trap_rx_packets_get $trap_name) 339 t1_bytes=$(devlink_trap_rx_bytes_get $trap_name) 340 341 if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then 342 return 0 343 else 344 return 1 345 fi 346} 347 348devlink_traps_enable_all() 349{ 350 local trap_name 351 352 for trap_name in $(devlink_traps_get); do 353 devlink_trap_action_set $trap_name "trap" 354 done 355} 356 357devlink_traps_disable_all() 358{ 359 for trap_name in $(devlink_traps_get); do 360 devlink_trap_action_set $trap_name "drop" 361 done 362} 363 364devlink_trap_groups_get() 365{ 366 devlink -j trap group | jq -r '.[]["'$DEVLINK_DEV'"][].name' 367} 368 369devlink_trap_group_action_set() 370{ 371 local group_name=$1; shift 372 local action=$1; shift 373 374 # Pipe output to /dev/null to avoid expected warnings. 375 devlink trap group set $DEVLINK_DEV group $group_name action $action \ 376 &> /dev/null 377} 378 379devlink_trap_group_rx_packets_get() 380{ 381 local group_name=$1; shift 382 383 devlink -js trap group show $DEVLINK_DEV group $group_name \ 384 | jq '.[][][]["stats"]["rx"]["packets"]' 385} 386 387devlink_trap_group_rx_bytes_get() 388{ 389 local group_name=$1; shift 390 391 devlink -js trap group show $DEVLINK_DEV group $group_name \ 392 | jq '.[][][]["stats"]["rx"]["bytes"]' 393} 394 395devlink_trap_group_stats_idle_test() 396{ 397 local group_name=$1; shift 398 local t0_packets t0_bytes 399 local t1_packets t1_bytes 400 401 t0_packets=$(devlink_trap_group_rx_packets_get $group_name) 402 t0_bytes=$(devlink_trap_group_rx_bytes_get $group_name) 403 404 sleep 1 405 406 t1_packets=$(devlink_trap_group_rx_packets_get $group_name) 407 t1_bytes=$(devlink_trap_group_rx_bytes_get $group_name) 408 409 if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then 410 return 0 411 else 412 return 1 413 fi 414} 415 416devlink_trap_exception_test() 417{ 418 local trap_name=$1; shift 419 local group_name 420 421 group_name=$(devlink_trap_group_get $trap_name) 422 423 devlink_trap_stats_idle_test $trap_name 424 check_fail $? "Trap stats idle when packets should have been trapped" 425 426 devlink_trap_group_stats_idle_test $group_name 427 check_fail $? "Trap group idle when packets should have been trapped" 428} 429 430devlink_trap_drop_test() 431{ 432 local trap_name=$1; shift 433 local dev=$1; shift 434 local handle=$1; shift 435 local group_name 436 437 group_name=$(devlink_trap_group_get $trap_name) 438 439 # This is the common part of all the tests. It checks that stats are 440 # initially idle, then non-idle after changing the trap action and 441 # finally idle again. It also makes sure the packets are dropped and 442 # never forwarded. 443 devlink_trap_stats_idle_test $trap_name 444 check_err $? "Trap stats not idle with initial drop action" 445 devlink_trap_group_stats_idle_test $group_name 446 check_err $? "Trap group stats not idle with initial drop action" 447 448 devlink_trap_action_set $trap_name "trap" 449 devlink_trap_stats_idle_test $trap_name 450 check_fail $? "Trap stats idle after setting action to trap" 451 devlink_trap_group_stats_idle_test $group_name 452 check_fail $? "Trap group stats idle after setting action to trap" 453 454 devlink_trap_action_set $trap_name "drop" 455 456 devlink_trap_stats_idle_test $trap_name 457 check_err $? "Trap stats not idle after setting action to drop" 458 devlink_trap_group_stats_idle_test $group_name 459 check_err $? "Trap group stats not idle after setting action to drop" 460 461 tc_check_packets "dev $dev egress" $handle 0 462 check_err $? "Packets were not dropped" 463} 464 465devlink_trap_drop_cleanup() 466{ 467 local mz_pid=$1; shift 468 local dev=$1; shift 469 local proto=$1; shift 470 local pref=$1; shift 471 local handle=$1; shift 472 473 kill $mz_pid && wait $mz_pid &> /dev/null 474 tc filter del dev $dev egress protocol $proto pref $pref handle $handle flower 475} 476 477devlink_trap_stats_test() 478{ 479 local test_name=$1; shift 480 local trap_name=$1; shift 481 local send_one="$@" 482 local t0_packets 483 local t1_packets 484 485 RET=0 486 487 t0_packets=$(devlink_trap_rx_packets_get $trap_name) 488 489 $send_one && sleep 1 490 491 t1_packets=$(devlink_trap_rx_packets_get $trap_name) 492 493 if [[ $t1_packets -eq $t0_packets ]]; then 494 check_err 1 "Trap stats did not increase" 495 fi 496 497 log_test "$test_name" 498} 499 500devlink_trap_policers_num_get() 501{ 502 devlink -j -p trap policer show | jq '.[]["'$DEVLINK_DEV'"] | length' 503} 504 505devlink_trap_policer_rate_get() 506{ 507 local policer_id=$1; shift 508 509 devlink -j -p trap policer show $DEVLINK_DEV policer $policer_id \ 510 | jq '.[][][]["rate"]' 511} 512 513devlink_trap_policer_burst_get() 514{ 515 local policer_id=$1; shift 516 517 devlink -j -p trap policer show $DEVLINK_DEV policer $policer_id \ 518 | jq '.[][][]["burst"]' 519} 520 521devlink_trap_policer_rx_dropped_get() 522{ 523 local policer_id=$1; shift 524 525 devlink -j -p -s trap policer show $DEVLINK_DEV policer $policer_id \ 526 | jq '.[][][]["stats"]["rx"]["dropped"]' 527} 528 529devlink_trap_group_policer_get() 530{ 531 local group_name=$1; shift 532 533 devlink -j -p trap group show $DEVLINK_DEV group $group_name \ 534 | jq '.[][][]["policer"]' 535} 536 537devlink_trap_policer_ids_get() 538{ 539 devlink -j -p trap policer show \ 540 | jq '.[]["'$DEVLINK_DEV'"][]["policer"]' 541} 542 543devlink_port_by_netdev() 544{ 545 local if_name=$1 546 547 devlink -j port show $if_name | jq -e '.[] | keys' | jq -r '.[]' 548} 549 550devlink_cpu_port_get() 551{ 552 local cpu_dl_port_num=$(devlink port list | grep "$DEVLINK_DEV" | 553 grep cpu | cut -d/ -f3 | cut -d: -f1 | 554 sed -n '1p') 555 556 echo "$DEVLINK_DEV/$cpu_dl_port_num" 557} 558 559devlink_cell_size_get() 560{ 561 devlink sb pool show "$DEVLINK_DEV" pool 0 -j \ 562 | jq '.pool[][].cell_size' 563} 564