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