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) 21elif [[ ! -z "$DEVLINK_DEV" ]]; then 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_drop_packets_get() 328{ 329 local trap_name=$1; shift 330 331 devlink -js trap show $DEVLINK_DEV trap $trap_name \ 332 | jq '.[][][]["stats"]["rx"]["dropped"]' 333} 334 335devlink_trap_stats_idle_test() 336{ 337 local trap_name=$1; shift 338 local t0_packets t0_bytes 339 local t1_packets t1_bytes 340 341 t0_packets=$(devlink_trap_rx_packets_get $trap_name) 342 t0_bytes=$(devlink_trap_rx_bytes_get $trap_name) 343 344 sleep 1 345 346 t1_packets=$(devlink_trap_rx_packets_get $trap_name) 347 t1_bytes=$(devlink_trap_rx_bytes_get $trap_name) 348 349 if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then 350 return 0 351 else 352 return 1 353 fi 354} 355 356devlink_trap_drop_stats_idle_test() 357{ 358 local trap_name=$1; shift 359 local t0_packets t0_bytes 360 361 t0_packets=$(devlink_trap_drop_packets_get $trap_name) 362 363 sleep 1 364 365 t1_packets=$(devlink_trap_drop_packets_get $trap_name) 366 367 if [[ $t0_packets -eq $t1_packets ]]; then 368 return 0 369 else 370 return 1 371 fi 372} 373 374devlink_traps_enable_all() 375{ 376 local trap_name 377 378 for trap_name in $(devlink_traps_get); do 379 devlink_trap_action_set $trap_name "trap" 380 done 381} 382 383devlink_traps_disable_all() 384{ 385 for trap_name in $(devlink_traps_get); do 386 devlink_trap_action_set $trap_name "drop" 387 done 388} 389 390devlink_trap_groups_get() 391{ 392 devlink -j trap group | jq -r '.[]["'$DEVLINK_DEV'"][].name' 393} 394 395devlink_trap_group_action_set() 396{ 397 local group_name=$1; shift 398 local action=$1; shift 399 400 # Pipe output to /dev/null to avoid expected warnings. 401 devlink trap group set $DEVLINK_DEV group $group_name action $action \ 402 &> /dev/null 403} 404 405devlink_trap_group_rx_packets_get() 406{ 407 local group_name=$1; shift 408 409 devlink -js trap group show $DEVLINK_DEV group $group_name \ 410 | jq '.[][][]["stats"]["rx"]["packets"]' 411} 412 413devlink_trap_group_rx_bytes_get() 414{ 415 local group_name=$1; shift 416 417 devlink -js trap group show $DEVLINK_DEV group $group_name \ 418 | jq '.[][][]["stats"]["rx"]["bytes"]' 419} 420 421devlink_trap_group_stats_idle_test() 422{ 423 local group_name=$1; shift 424 local t0_packets t0_bytes 425 local t1_packets t1_bytes 426 427 t0_packets=$(devlink_trap_group_rx_packets_get $group_name) 428 t0_bytes=$(devlink_trap_group_rx_bytes_get $group_name) 429 430 sleep 1 431 432 t1_packets=$(devlink_trap_group_rx_packets_get $group_name) 433 t1_bytes=$(devlink_trap_group_rx_bytes_get $group_name) 434 435 if [[ $t0_packets -eq $t1_packets && $t0_bytes -eq $t1_bytes ]]; then 436 return 0 437 else 438 return 1 439 fi 440} 441 442devlink_trap_exception_test() 443{ 444 local trap_name=$1; shift 445 local group_name 446 447 group_name=$(devlink_trap_group_get $trap_name) 448 449 devlink_trap_stats_idle_test $trap_name 450 check_fail $? "Trap stats idle when packets should have been trapped" 451 452 devlink_trap_group_stats_idle_test $group_name 453 check_fail $? "Trap group idle when packets should have been trapped" 454} 455 456devlink_trap_drop_test() 457{ 458 local trap_name=$1; shift 459 local dev=$1; shift 460 local handle=$1; shift 461 local group_name 462 463 group_name=$(devlink_trap_group_get $trap_name) 464 465 # This is the common part of all the tests. It checks that stats are 466 # initially idle, then non-idle after changing the trap action and 467 # finally idle again. It also makes sure the packets are dropped and 468 # never forwarded. 469 devlink_trap_stats_idle_test $trap_name 470 check_err $? "Trap stats not idle with initial drop action" 471 devlink_trap_group_stats_idle_test $group_name 472 check_err $? "Trap group stats not idle with initial drop action" 473 474 devlink_trap_action_set $trap_name "trap" 475 devlink_trap_stats_idle_test $trap_name 476 check_fail $? "Trap stats idle after setting action to trap" 477 devlink_trap_group_stats_idle_test $group_name 478 check_fail $? "Trap group stats idle after setting action to trap" 479 480 devlink_trap_action_set $trap_name "drop" 481 482 devlink_trap_stats_idle_test $trap_name 483 check_err $? "Trap stats not idle after setting action to drop" 484 devlink_trap_group_stats_idle_test $group_name 485 check_err $? "Trap group stats not idle after setting action to drop" 486 487 tc_check_packets "dev $dev egress" $handle 0 488 check_err $? "Packets were not dropped" 489} 490 491devlink_trap_drop_cleanup() 492{ 493 local mz_pid=$1; shift 494 local dev=$1; shift 495 local proto=$1; shift 496 local pref=$1; shift 497 local handle=$1; shift 498 499 kill $mz_pid && wait $mz_pid &> /dev/null 500 tc filter del dev $dev egress protocol $proto pref $pref handle $handle flower 501} 502 503devlink_trap_stats_test() 504{ 505 local test_name=$1; shift 506 local trap_name=$1; shift 507 local send_one="$@" 508 local t0_packets 509 local t1_packets 510 511 RET=0 512 513 t0_packets=$(devlink_trap_rx_packets_get $trap_name) 514 515 $send_one && sleep 1 516 517 t1_packets=$(devlink_trap_rx_packets_get $trap_name) 518 519 if [[ $t1_packets -eq $t0_packets ]]; then 520 check_err 1 "Trap stats did not increase" 521 fi 522 523 log_test "$test_name" 524} 525 526devlink_trap_policers_num_get() 527{ 528 devlink -j -p trap policer show | jq '.[]["'$DEVLINK_DEV'"] | length' 529} 530 531devlink_trap_policer_rate_get() 532{ 533 local policer_id=$1; shift 534 535 devlink -j -p trap policer show $DEVLINK_DEV policer $policer_id \ 536 | jq '.[][][]["rate"]' 537} 538 539devlink_trap_policer_burst_get() 540{ 541 local policer_id=$1; shift 542 543 devlink -j -p trap policer show $DEVLINK_DEV policer $policer_id \ 544 | jq '.[][][]["burst"]' 545} 546 547devlink_trap_policer_rx_dropped_get() 548{ 549 local policer_id=$1; shift 550 551 devlink -j -p -s trap policer show $DEVLINK_DEV policer $policer_id \ 552 | jq '.[][][]["stats"]["rx"]["dropped"]' 553} 554 555devlink_trap_group_policer_get() 556{ 557 local group_name=$1; shift 558 559 devlink -j -p trap group show $DEVLINK_DEV group $group_name \ 560 | jq '.[][][]["policer"]' 561} 562 563devlink_trap_policer_ids_get() 564{ 565 devlink -j -p trap policer show \ 566 | jq '.[]["'$DEVLINK_DEV'"][]["policer"]' 567} 568 569devlink_port_by_netdev() 570{ 571 local if_name=$1 572 573 devlink -j port show $if_name | jq -e '.[] | keys' | jq -r '.[]' 574} 575 576devlink_cpu_port_get() 577{ 578 local cpu_dl_port_num=$(devlink port list | grep "$DEVLINK_DEV" | 579 grep cpu | cut -d/ -f3 | cut -d: -f1 | 580 sed -n '1p') 581 582 echo "$DEVLINK_DEV/$cpu_dl_port_num" 583} 584 585devlink_cell_size_get() 586{ 587 devlink sb pool show "$DEVLINK_DEV" pool 0 -j \ 588 | jq '.pool[][].cell_size' 589} 590