1#!/bin/bash 2# SPDX-License-Identifier: GPL-2.0 3 4lib_dir=$(dirname $0)/../../../net/forwarding 5 6ALL_TESTS="fw_flash_test params_test regions_test reload_test \ 7 netns_reload_test resource_test dev_info_test \ 8 empty_reporter_test dummy_reporter_test" 9NUM_NETIFS=0 10source $lib_dir/lib.sh 11 12BUS_ADDR=10 13PORT_COUNT=4 14DEV_NAME=netdevsim$BUS_ADDR 15SYSFS_NET_DIR=/sys/bus/netdevsim/devices/$DEV_NAME/net/ 16DEBUGFS_DIR=/sys/kernel/debug/netdevsim/$DEV_NAME/ 17DL_HANDLE=netdevsim/$DEV_NAME 18 19wait_for_devlink() 20{ 21 "$@" | grep -q $DL_HANDLE 22} 23 24devlink_wait() 25{ 26 local timeout=$1 27 28 busywait "$timeout" wait_for_devlink devlink dev 29} 30 31fw_flash_test() 32{ 33 RET=0 34 35 devlink dev flash $DL_HANDLE file dummy 36 check_err $? "Failed to flash with status updates on" 37 38 devlink dev flash $DL_HANDLE file dummy component fw.mgmt 39 check_err $? "Failed to flash with component attribute" 40 41 devlink dev flash $DL_HANDLE file dummy overwrite settings 42 check_fail $? "Flash with overwrite settings should be rejected" 43 44 echo "1"> $DEBUGFS_DIR/fw_update_overwrite_mask 45 check_err $? "Failed to change allowed overwrite mask" 46 47 devlink dev flash $DL_HANDLE file dummy overwrite settings 48 check_err $? "Failed to flash with settings overwrite enabled" 49 50 devlink dev flash $DL_HANDLE file dummy overwrite identifiers 51 check_fail $? "Flash with overwrite settings should be identifiers" 52 53 echo "3"> $DEBUGFS_DIR/fw_update_overwrite_mask 54 check_err $? "Failed to change allowed overwrite mask" 55 56 devlink dev flash $DL_HANDLE file dummy overwrite identifiers overwrite settings 57 check_err $? "Failed to flash with settings and identifiers overwrite enabled" 58 59 echo "n"> $DEBUGFS_DIR/fw_update_status 60 check_err $? "Failed to disable status updates" 61 62 devlink dev flash $DL_HANDLE file dummy 63 check_err $? "Failed to flash with status updates off" 64 65 log_test "fw flash test" 66} 67 68param_get() 69{ 70 local name=$1 71 72 cmd_jq "devlink dev param show $DL_HANDLE name $name -j" \ 73 '.[][][].values[] | select(.cmode == "driverinit").value' 74} 75 76param_set() 77{ 78 local name=$1 79 local value=$2 80 81 devlink dev param set $DL_HANDLE name $name cmode driverinit value $value 82} 83 84check_value() 85{ 86 local name=$1 87 local phase_name=$2 88 local expected_param_value=$3 89 local expected_debugfs_value=$4 90 local value 91 92 value=$(param_get $name) 93 check_err $? "Failed to get $name param value" 94 [ "$value" == "$expected_param_value" ] 95 check_err $? "Unexpected $phase_name $name param value" 96 value=$(<$DEBUGFS_DIR/$name) 97 check_err $? "Failed to get $name debugfs value" 98 [ "$value" == "$expected_debugfs_value" ] 99 check_err $? "Unexpected $phase_name $name debugfs value" 100} 101 102params_test() 103{ 104 RET=0 105 106 local max_macs 107 local test1 108 109 check_value max_macs initial 32 32 110 check_value test1 initial true Y 111 112 param_set max_macs 16 113 check_err $? "Failed to set max_macs param value" 114 param_set test1 false 115 check_err $? "Failed to set test1 param value" 116 117 check_value max_macs post-set 16 32 118 check_value test1 post-set false Y 119 120 devlink dev reload $DL_HANDLE 121 122 check_value max_macs post-reload 16 16 123 check_value test1 post-reload false N 124 125 log_test "params test" 126} 127 128check_region_size() 129{ 130 local name=$1 131 local size 132 133 size=$(devlink region show $DL_HANDLE/$name -j | jq -e -r '.[][].size') 134 check_err $? "Failed to get $name region size" 135 [ $size -eq 32768 ] 136 check_err $? "Invalid $name region size" 137} 138 139check_region_snapshot_count() 140{ 141 local name=$1 142 local phase_name=$2 143 local expected_count=$3 144 local count 145 146 count=$(devlink region show $DL_HANDLE/$name -j | jq -e -r '.[][].snapshot | length') 147 [ $count -eq $expected_count ] 148 check_err $? "Unexpected $phase_name snapshot count" 149} 150 151regions_test() 152{ 153 RET=0 154 155 local count 156 157 check_region_size dummy 158 check_region_snapshot_count dummy initial 0 159 160 echo ""> $DEBUGFS_DIR/take_snapshot 161 check_err $? "Failed to take first dummy region snapshot" 162 check_region_snapshot_count dummy post-first-snapshot 1 163 164 echo ""> $DEBUGFS_DIR/take_snapshot 165 check_err $? "Failed to take second dummy region snapshot" 166 check_region_snapshot_count dummy post-second-snapshot 2 167 168 echo ""> $DEBUGFS_DIR/take_snapshot 169 check_err $? "Failed to take third dummy region snapshot" 170 check_region_snapshot_count dummy post-third-snapshot 3 171 172 devlink region del $DL_HANDLE/dummy snapshot 1 173 check_err $? "Failed to delete first dummy region snapshot" 174 175 check_region_snapshot_count dummy post-first-delete 2 176 177 devlink region new $DL_HANDLE/dummy snapshot 25 178 check_err $? "Failed to create a new snapshot with id 25" 179 180 check_region_snapshot_count dummy post-first-request 3 181 182 devlink region dump $DL_HANDLE/dummy snapshot 25 >> /dev/null 183 check_err $? "Failed to dump snapshot with id 25" 184 185 devlink region read $DL_HANDLE/dummy snapshot 25 addr 0 len 1 >> /dev/null 186 check_err $? "Failed to read snapshot with id 25 (1 byte)" 187 188 devlink region read $DL_HANDLE/dummy snapshot 25 addr 128 len 128 >> /dev/null 189 check_err $? "Failed to read snapshot with id 25 (128 bytes)" 190 191 devlink region read $DL_HANDLE/dummy snapshot 25 addr 128 len $((1<<32)) >> /dev/null 192 check_err $? "Failed to read snapshot with id 25 (oversized)" 193 194 devlink region read $DL_HANDLE/dummy snapshot 25 addr $((1<<32)) len 128 >> /dev/null 2>&1 195 check_fail $? "Bad read of snapshot with id 25 did not fail" 196 197 devlink region del $DL_HANDLE/dummy snapshot 25 198 check_err $? "Failed to delete snapshot with id 25" 199 200 check_region_snapshot_count dummy post-second-delete 2 201 202 sid=$(devlink -j region new $DL_HANDLE/dummy | jq '.[][][][]') 203 check_err $? "Failed to create a new snapshot with id allocated by the kernel" 204 205 check_region_snapshot_count dummy post-first-request 3 206 207 devlink region dump $DL_HANDLE/dummy snapshot $sid >> /dev/null 208 check_err $? "Failed to dump a snapshot with id allocated by the kernel" 209 210 devlink region del $DL_HANDLE/dummy snapshot $sid 211 check_err $? "Failed to delete snapshot with id allocated by the kernel" 212 213 check_region_snapshot_count dummy post-first-request 2 214 215 log_test "regions test" 216} 217 218reload_test() 219{ 220 RET=0 221 222 devlink dev reload $DL_HANDLE 223 check_err $? "Failed to reload" 224 225 echo "y"> $DEBUGFS_DIR/fail_reload 226 check_err $? "Failed to setup devlink reload to fail" 227 228 devlink dev reload $DL_HANDLE 229 check_fail $? "Unexpected success of devlink reload" 230 231 echo "n"> $DEBUGFS_DIR/fail_reload 232 check_err $? "Failed to setup devlink reload not to fail" 233 234 devlink dev reload $DL_HANDLE 235 check_err $? "Failed to reload after set not to fail" 236 237 echo "y"> $DEBUGFS_DIR/dont_allow_reload 238 check_err $? "Failed to forbid devlink reload" 239 240 devlink dev reload $DL_HANDLE 241 check_fail $? "Unexpected success of devlink reload" 242 243 echo "n"> $DEBUGFS_DIR/dont_allow_reload 244 check_err $? "Failed to re-enable devlink reload" 245 246 devlink dev reload $DL_HANDLE 247 check_err $? "Failed to reload after re-enable" 248 249 log_test "reload test" 250} 251 252netns_reload_test() 253{ 254 RET=0 255 256 ip netns add testns1 257 check_err $? "Failed add netns \"testns1\"" 258 ip netns add testns2 259 check_err $? "Failed add netns \"testns2\"" 260 261 devlink dev reload $DL_HANDLE netns testns1 262 check_err $? "Failed to reload into netns \"testns1\"" 263 264 devlink -N testns1 dev reload $DL_HANDLE netns testns2 265 check_err $? "Failed to reload from netns \"testns1\" into netns \"testns2\"" 266 267 ip netns del testns2 268 ip netns del testns1 269 270 # Wait until netns async cleanup is done. 271 devlink_wait 2000 272 273 log_test "netns reload test" 274} 275 276DUMMYDEV="dummytest" 277 278res_val_get() 279{ 280 local netns=$1 281 local parentname=$2 282 local name=$3 283 local type=$4 284 285 cmd_jq "devlink -N $netns resource show $DL_HANDLE -j" \ 286 ".[][][] | select(.name == \"$parentname\").resources[] \ 287 | select(.name == \"$name\").$type" 288} 289 290resource_test() 291{ 292 RET=0 293 294 ip netns add testns1 295 check_err $? "Failed add netns \"testns1\"" 296 ip netns add testns2 297 check_err $? "Failed add netns \"testns2\"" 298 299 devlink dev reload $DL_HANDLE netns testns1 300 check_err $? "Failed to reload into netns \"testns1\"" 301 302 # Create dummy dev to add the address and routes on. 303 304 ip -n testns1 link add name $DUMMYDEV type dummy 305 check_err $? "Failed create dummy device" 306 ip -n testns1 link set $DUMMYDEV up 307 check_err $? "Failed bring up dummy device" 308 ip -n testns1 a a 192.0.1.1/24 dev $DUMMYDEV 309 check_err $? "Failed add an IP address to dummy device" 310 311 local occ=$(res_val_get testns1 IPv4 fib occ) 312 local limit=$((occ+1)) 313 314 # Set fib size limit to handle one another route only. 315 316 devlink -N testns1 resource set $DL_HANDLE path IPv4/fib size $limit 317 check_err $? "Failed to set IPv4/fib resource size" 318 local size_new=$(res_val_get testns1 IPv4 fib size_new) 319 [ "$size_new" -eq "$limit" ] 320 check_err $? "Unexpected \"size_new\" value (got $size_new, expected $limit)" 321 322 devlink -N testns1 dev reload $DL_HANDLE 323 check_err $? "Failed to reload" 324 local size=$(res_val_get testns1 IPv4 fib size) 325 [ "$size" -eq "$limit" ] 326 check_err $? "Unexpected \"size\" value (got $size, expected $limit)" 327 328 # Insert 2 routes, the first is going to be inserted, 329 # the second is expected to fail to be inserted. 330 331 ip -n testns1 r a 192.0.2.0/24 via 192.0.1.2 332 check_err $? "Failed to add route" 333 334 ip -n testns1 r a 192.0.3.0/24 via 192.0.1.2 335 check_fail $? "Unexpected successful route add over limit" 336 337 # Now create another dummy in second network namespace and 338 # insert two routes. That is over the limit of the netdevsim 339 # instance in the first namespace. Move the netdevsim instance 340 # into the second namespace and expect it to fail. 341 342 ip -n testns2 link add name $DUMMYDEV type dummy 343 check_err $? "Failed create dummy device" 344 ip -n testns2 link set $DUMMYDEV up 345 check_err $? "Failed bring up dummy device" 346 ip -n testns2 a a 192.0.1.1/24 dev $DUMMYDEV 347 check_err $? "Failed add an IP address to dummy device" 348 ip -n testns2 r a 192.0.2.0/24 via 192.0.1.2 349 check_err $? "Failed to add route" 350 ip -n testns2 r a 192.0.3.0/24 via 192.0.1.2 351 check_err $? "Failed to add route" 352 353 devlink -N testns1 dev reload $DL_HANDLE netns testns2 354 check_fail $? "Unexpected successful reload from netns \"testns1\" into netns \"testns2\"" 355 356 devlink -N testns2 resource set $DL_HANDLE path IPv4/fib size ' -1' 357 check_err $? "Failed to reset IPv4/fib resource size" 358 359 devlink -N testns2 dev reload $DL_HANDLE netns 1 360 check_err $? "Failed to reload devlink back" 361 362 ip netns del testns2 363 ip netns del testns1 364 365 # Wait until netns async cleanup is done. 366 devlink_wait 2000 367 368 log_test "resource test" 369} 370 371info_get() 372{ 373 local name=$1 374 375 cmd_jq "devlink dev info $DL_HANDLE -j" ".[][][\"$name\"]" "-e" 376} 377 378dev_info_test() 379{ 380 RET=0 381 382 driver=$(info_get "driver") 383 check_err $? "Failed to get driver name" 384 [ "$driver" == "netdevsim" ] 385 check_err $? "Unexpected driver name $driver" 386 387 log_test "dev_info test" 388} 389 390empty_reporter_test() 391{ 392 RET=0 393 394 devlink health show $DL_HANDLE reporter empty >/dev/null 395 check_err $? "Failed show empty reporter" 396 397 devlink health dump show $DL_HANDLE reporter empty >/dev/null 398 check_err $? "Failed show dump of empty reporter" 399 400 devlink health diagnose $DL_HANDLE reporter empty >/dev/null 401 check_err $? "Failed diagnose empty reporter" 402 403 devlink health recover $DL_HANDLE reporter empty 404 check_err $? "Failed recover empty reporter" 405 406 log_test "empty reporter test" 407} 408 409check_reporter_info() 410{ 411 local name=$1 412 local expected_state=$2 413 local expected_error=$3 414 local expected_recover=$4 415 local expected_grace_period=$5 416 local expected_auto_recover=$6 417 418 local show=$(devlink health show $DL_HANDLE reporter $name -j | jq -e -r ".[][][]") 419 check_err $? "Failed show $name reporter" 420 421 local state=$(echo $show | jq -r ".state") 422 [ "$state" == "$expected_state" ] 423 check_err $? "Unexpected \"state\" value (got $state, expected $expected_state)" 424 425 local error=$(echo $show | jq -r ".error") 426 [ "$error" == "$expected_error" ] 427 check_err $? "Unexpected \"error\" value (got $error, expected $expected_error)" 428 429 local recover=`echo $show | jq -r ".recover"` 430 [ "$recover" == "$expected_recover" ] 431 check_err $? "Unexpected \"recover\" value (got $recover, expected $expected_recover)" 432 433 local grace_period=$(echo $show | jq -r ".grace_period") 434 check_err $? "Failed get $name reporter grace_period" 435 [ "$grace_period" == "$expected_grace_period" ] 436 check_err $? "Unexpected \"grace_period\" value (got $grace_period, expected $expected_grace_period)" 437 438 local auto_recover=$(echo $show | jq -r ".auto_recover") 439 [ "$auto_recover" == "$expected_auto_recover" ] 440 check_err $? "Unexpected \"auto_recover\" value (got $auto_recover, expected $expected_auto_recover)" 441} 442 443dummy_reporter_test() 444{ 445 RET=0 446 447 check_reporter_info dummy healthy 0 0 0 true 448 449 devlink health set $DL_HANDLE reporter dummy auto_recover false 450 check_err $? "Failed to dummy reporter auto_recover option" 451 452 check_reporter_info dummy healthy 0 0 0 false 453 454 local BREAK_MSG="foo bar" 455 echo "$BREAK_MSG"> $DEBUGFS_DIR/health/break_health 456 check_err $? "Failed to break dummy reporter" 457 458 check_reporter_info dummy error 1 0 0 false 459 460 local dump=$(devlink health dump show $DL_HANDLE reporter dummy -j) 461 check_err $? "Failed show dump of dummy reporter" 462 463 local dump_break_msg=$(echo $dump | jq -r ".break_message") 464 [ "$dump_break_msg" == "$BREAK_MSG" ] 465 check_err $? "Unexpected dump break message value (got $dump_break_msg, expected $BREAK_MSG)" 466 467 devlink health dump clear $DL_HANDLE reporter dummy 468 check_err $? "Failed clear dump of dummy reporter" 469 470 devlink health recover $DL_HANDLE reporter dummy 471 check_err $? "Failed recover dummy reporter" 472 473 check_reporter_info dummy healthy 1 1 0 false 474 475 devlink health set $DL_HANDLE reporter dummy auto_recover true 476 check_err $? "Failed to dummy reporter auto_recover option" 477 478 check_reporter_info dummy healthy 1 1 0 true 479 480 echo "$BREAK_MSG"> $DEBUGFS_DIR/health/break_health 481 check_err $? "Failed to break dummy reporter" 482 483 check_reporter_info dummy healthy 2 2 0 true 484 485 local diagnose=$(devlink health diagnose $DL_HANDLE reporter dummy -j -p) 486 check_err $? "Failed show diagnose of dummy reporter" 487 488 local rcvrd_break_msg=$(echo $diagnose | jq -r ".recovered_break_message") 489 [ "$rcvrd_break_msg" == "$BREAK_MSG" ] 490 check_err $? "Unexpected recovered break message value (got $rcvrd_break_msg, expected $BREAK_MSG)" 491 492 devlink health set $DL_HANDLE reporter dummy grace_period 10 493 check_err $? "Failed to dummy reporter grace_period option" 494 495 check_reporter_info dummy healthy 2 2 10 true 496 497 echo "Y"> $DEBUGFS_DIR/health/fail_recover 498 check_err $? "Failed set dummy reporter recovery to fail" 499 500 echo "$BREAK_MSG"> $DEBUGFS_DIR/health/break_health 501 check_fail $? "Unexpected success of dummy reporter break" 502 503 check_reporter_info dummy error 3 2 10 true 504 505 devlink health recover $DL_HANDLE reporter dummy 506 check_fail $? "Unexpected success of dummy reporter recover" 507 508 echo "N"> $DEBUGFS_DIR/health/fail_recover 509 check_err $? "Failed set dummy reporter recovery to be successful" 510 511 devlink health recover $DL_HANDLE reporter dummy 512 check_err $? "Failed recover dummy reporter" 513 514 check_reporter_info dummy healthy 3 3 10 true 515 516 echo 8192 > $DEBUGFS_DIR/health/binary_len 517 check_err $? "Failed set dummy reporter binary len to 8192" 518 519 local dump=$(devlink health dump show $DL_HANDLE reporter dummy -j) 520 check_err $? "Failed show dump of dummy reporter" 521 522 devlink health dump clear $DL_HANDLE reporter dummy 523 check_err $? "Failed clear dump of dummy reporter" 524 525 log_test "dummy reporter test" 526} 527 528setup_prepare() 529{ 530 modprobe netdevsim 531 echo "$BUS_ADDR $PORT_COUNT" > /sys/bus/netdevsim/new_device 532 while [ ! -d $SYSFS_NET_DIR ] ; do :; done 533} 534 535cleanup() 536{ 537 pre_cleanup 538 echo "$BUS_ADDR" > /sys/bus/netdevsim/del_device 539 modprobe -r netdevsim 540} 541 542trap cleanup EXIT 543 544setup_prepare 545 546tests_run 547 548exit $EXIT_STATUS 549