1 # $NetBSD: t_rtable.sh,v 1.8 2023/03/26 19:10:34 andvar Exp $ 2 # 3 # Copyright (c) 2017 Internet Initiative Japan Inc. 4 # All rights reserved. 5 # 6 # Redistribution and use in source and binary forms, with or without 7 # modification, are permitted provided that the following conditions 8 # are met: 9 # 1. Redistributions of source code must retain the above copyright 10 # notice, this list of conditions and the following disclaimer. 11 # 2. Redistributions in binary form must reproduce the above copyright 12 # notice, this list of conditions and the following disclaimer in the 13 # documentation and/or other materials provided with the distribution. 14 # 15 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 16 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 17 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 18 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 19 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 # POSSIBILITY OF SUCH DAMAGE. 26 # 27 28 SOCK1=unix://commsock1 29 SOCK2=unix://commsock2 30 SOCK3=unix://commsock3 31 IP1=10.0.0.1 32 IP2=10.0.0.2 33 34 DEBUG=${DEBUG:-false} 35 TIMEOUT=5 36 37 setup_endpoint() 38 { 39 local sock=${1} 40 local addr=${2} 41 local bus=${3} 42 local mode=${4} 43 44 rump_server_add_iface $sock shmif0 $bus 45 export RUMP_SERVER=${sock} 46 if [ $mode = "ipv6" ]; then 47 atf_check -s exit:0 rump.ifconfig shmif0 inet6 ${addr} 48 else 49 atf_check -s exit:0 rump.ifconfig shmif0 inet ${addr} netmask 0xffffff00 50 fi 51 52 atf_check -s exit:0 rump.ifconfig shmif0 up 53 $DEBUG && rump.ifconfig shmif0 54 } 55 56 setup_bridge_server() 57 { 58 59 rump_server_add_iface $SOCK2 shmif0 bus1 60 rump_server_add_iface $SOCK2 shmif1 bus2 61 export RUMP_SERVER=$SOCK2 62 atf_check -s exit:0 rump.ifconfig shmif0 up 63 atf_check -s exit:0 rump.ifconfig shmif1 up 64 } 65 66 setup() 67 { 68 69 rump_server_start $SOCK1 bridge 70 rump_server_start $SOCK2 bridge 71 rump_server_start $SOCK3 bridge 72 73 setup_endpoint $SOCK1 $IP1 bus1 ipv4 74 setup_endpoint $SOCK3 $IP2 bus2 ipv4 75 setup_bridge_server 76 } 77 78 setup_bridge() 79 { 80 81 export RUMP_SERVER=$SOCK2 82 rump_server_add_iface $SOCK2 bridge0 83 atf_check -s exit:0 rump.ifconfig bridge0 up 84 85 export LD_PRELOAD=/usr/lib/librumphijack.so 86 atf_check -s exit:0 /sbin/brconfig bridge0 add shmif0 87 atf_check -s exit:0 /sbin/brconfig bridge0 add shmif1 88 /sbin/brconfig bridge0 89 unset LD_PRELOAD 90 rump.ifconfig shmif0 91 rump.ifconfig shmif1 92 } 93 94 get_number_of_caches() 95 { 96 97 export RUMP_SERVER=$SOCK2 98 export LD_PRELOAD=/usr/lib/librumphijack.so 99 echo $(/sbin/brconfig bridge0 addr |wc -l) 100 unset LD_PRELOAD 101 } 102 103 104 atf_test_case bridge_rtable_basic cleanup 105 bridge_rtable_basic_head() 106 { 107 108 atf_set "descr" "Tests basic operations of bridge's learning table" 109 atf_set "require.progs" "rump_server" 110 } 111 112 bridge_rtable_basic_body() 113 { 114 local addr1= addr3= 115 116 setup 117 setup_bridge 118 119 # Get MAC addresses of the endpoints. 120 addr1=$(get_macaddr $SOCK1 shmif0) 121 addr3=$(get_macaddr $SOCK3 shmif0) 122 123 # Confirm there is no MAC address caches. 124 export RUMP_SERVER=$SOCK2 125 export LD_PRELOAD=/usr/lib/librumphijack.so 126 $DEBUG && /sbin/brconfig bridge0 127 atf_check -s exit:0 -o not-match:"$addr1" /sbin/brconfig bridge0 128 atf_check -s exit:0 -o not-match:"$addr3" /sbin/brconfig bridge0 129 unset LD_PRELOAD 130 131 # Make the bridge learn the MAC addresses of the endpoints. 132 export RUMP_SERVER=$SOCK1 133 atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2 134 unset RUMP_SERVER 135 136 # Tests the addresses are in the cache. 137 export RUMP_SERVER=$SOCK2 138 export LD_PRELOAD=/usr/lib/librumphijack.so 139 $DEBUG && /sbin/brconfig bridge0 140 atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0 141 atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0 142 143 # Tests brconfig deladdr 144 atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 deladdr "$addr1" 145 atf_check -s exit:0 -o not-match:"$addr1 shmif0" /sbin/brconfig bridge0 146 atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 deladdr "$addr3" 147 atf_check -s exit:0 -o not-match:"$addr3 shmif1" /sbin/brconfig bridge0 148 unset LD_PRELOAD 149 150 rump_server_destroy_ifaces 151 } 152 153 bridge_rtable_basic_cleanup() 154 { 155 156 $DEBUG && dump 157 cleanup 158 } 159 160 161 atf_test_case bridge_rtable_flush cleanup 162 bridge_rtable_flush_head() 163 { 164 165 atf_set "descr" "Tests brconfig flush" 166 atf_set "require.progs" "rump_server" 167 } 168 169 bridge_rtable_flush_body() 170 { 171 local addr1= addr3= 172 local n= 173 174 setup 175 setup_bridge 176 177 # Get MAC addresses of the endpoints. 178 addr1=$(get_macaddr $SOCK1 shmif0) 179 addr3=$(get_macaddr $SOCK3 shmif0) 180 181 # Make the bridge learn the MAC addresses of the endpoints. 182 export RUMP_SERVER=$SOCK1 183 atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2 184 unset RUMP_SERVER 185 186 # Tests the addresses are in the cache. 187 export RUMP_SERVER=$SOCK2 188 export LD_PRELOAD=/usr/lib/librumphijack.so 189 $DEBUG && /sbin/brconfig bridge0 190 atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0 191 atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0 192 193 # Tests brconfig flush. 194 atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 flush 195 atf_check -s exit:0 -o not-match:"$addr1 shmif0" /sbin/brconfig bridge0 196 atf_check -s exit:0 -o not-match:"$addr3 shmif1" /sbin/brconfig bridge0 197 unset LD_PRELOAD 198 199 # Add extra interfaces and addresses 200 export RUMP_SERVER=$SOCK1 201 rump_server_add_iface $SOCK1 shmif1 bus1 202 atf_check -s exit:0 rump.ifconfig shmif1 10.0.0.11/24 203 atf_check -s exit:0 rump.ifconfig -w 10 204 205 export RUMP_SERVER=$SOCK3 206 rump_server_add_iface $SOCK3 shmif1 bus2 207 atf_check -s exit:0 rump.ifconfig shmif1 10.0.0.12/24 208 atf_check -s exit:0 rump.ifconfig -w 10 209 210 # Let cache entries 211 export RUMP_SERVER=$SOCK1 212 atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 10.0.0.12 213 export RUMP_SERVER=$SOCK3 214 atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 10.0.0.11 215 216 export RUMP_SERVER=$SOCK2 217 export LD_PRELOAD=/usr/lib/librumphijack.so 218 $DEBUG && /sbin/brconfig bridge0 219 n=$(get_number_of_caches) 220 atf_check_equal $n 4 221 222 atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 flush 223 n=$(get_number_of_caches) 224 atf_check_equal $n 0 225 unset LD_PRELOAD 226 227 rump_server_destroy_ifaces 228 } 229 230 bridge_rtable_flush_cleanup() 231 { 232 233 $DEBUG && dump 234 cleanup 235 } 236 237 238 atf_test_case bridge_rtable_timeout cleanup 239 bridge_rtable_timeout_head() 240 { 241 242 atf_set "descr" "Tests cache timeout of bridge's learning table" 243 atf_set "require.progs" "rump_server" 244 } 245 246 bridge_rtable_timeout_body() 247 { 248 local addr1= addr3= 249 local timeout=5 250 251 setup 252 setup_bridge 253 254 # Get MAC addresses of the endpoints. 255 addr1=$(get_macaddr $SOCK1 shmif0) 256 addr3=$(get_macaddr $SOCK3 shmif0) 257 258 # Tests brconfig timeout. 259 export RUMP_SERVER=$SOCK2 260 export LD_PRELOAD=/usr/lib/librumphijack.so 261 atf_check -s exit:0 -o match:"timeout: 1200" /sbin/brconfig bridge0 262 atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 timeout $timeout 263 atf_check -s exit:0 -o match:"timeout: $timeout" /sbin/brconfig bridge0 264 unset LD_PRELOAD 265 266 # Make the bridge learn the MAC addresses of the endpoints. 267 export RUMP_SERVER=$SOCK1 268 atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2 269 unset RUMP_SERVER 270 271 # Tests the addresses are in the cache. 272 export RUMP_SERVER=$SOCK2 273 export LD_PRELOAD=/usr/lib/librumphijack.so 274 $DEBUG && /sbin/brconfig bridge0 275 atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0 276 atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0 277 278 # TODO: cache expiration 279 # The initial timeout value of a cache is changed to $timeout and 280 # after $timeout elapsed the cache is ready to be sweeped. However, 281 # the GC of rtable runs every 5 minutes and the cache remains until 282 # then. Should we have a sysctl to change the period? 283 284 #sleep $(($timeout + 2)) 285 # 286 ## Tests the addresses are not in the cache. 287 #export RUMP_SERVER=$SOCK2 288 #export LD_PRELOAD=/usr/lib/librumphijack.so 289 #$DEBUG && /sbin/brconfig bridge0 290 #atf_check -s exit:0 -o not-match:"$addr1 shmif0" /sbin/brconfig bridge0 291 #atf_check -s exit:0 -o not-match:"$addr3 shmif1" /sbin/brconfig bridge0 292 293 rump_server_destroy_ifaces 294 } 295 296 bridge_rtable_timeout_cleanup() 297 { 298 299 $DEBUG && dump 300 cleanup 301 } 302 303 304 atf_test_case bridge_rtable_maxaddr cleanup 305 bridge_rtable_maxaddr_head() 306 { 307 308 atf_set "descr" "Tests brconfig maxaddr" 309 atf_set "require.progs" "rump_server" 310 } 311 312 bridge_rtable_maxaddr_body() 313 { 314 local addr1= addr3= 315 316 setup 317 setup_bridge 318 319 # Get MAC addresses of the endpoints. 320 addr1=$(get_macaddr $SOCK1 shmif0) 321 addr3=$(get_macaddr $SOCK3 shmif0) 322 323 # Fill the MAC addresses of the endpoints. 324 export RUMP_SERVER=$SOCK1 325 atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2 326 export RUMP_SERVER=$SOCK2 327 export LD_PRELOAD=/usr/lib/librumphijack.so 328 /sbin/brconfig bridge0 329 atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0 330 atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0 331 332 # Check the default # of caches is 100 333 atf_check -s exit:0 -o match:"max cache: 100" /sbin/brconfig bridge0 334 335 # Test two MAC addresses are cached 336 n=$(get_number_of_caches) 337 atf_check_equal $n 2 338 339 # Limit # of caches to one 340 atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 maxaddr 1 341 atf_check -s exit:0 -o match:"max cache: 1" /sbin/brconfig bridge0 342 /sbin/brconfig bridge0 343 344 # Check a cache is flushed out 345 n=$(get_number_of_caches) 346 atf_check_equal $n 1 347 348 # Test a new address cache is not created 349 export RUMP_SERVER=$SOCK1 350 atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2 351 export RUMP_SERVER=$SOCK2 352 n=$(get_number_of_caches) 353 atf_check_equal $n 1 354 355 # Increase # of caches to two 356 atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 maxaddr 2 357 atf_check -s exit:0 -o match:"max cache: 2" /sbin/brconfig bridge0 358 unset LD_PRELOAD 359 360 # Test we can cache two addresses again 361 export RUMP_SERVER=$SOCK1 362 atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2 363 export RUMP_SERVER=$SOCK2 364 export LD_PRELOAD=/usr/lib/librumphijack.so 365 /sbin/brconfig bridge0 366 atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0 367 atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0 368 unset LD_PRELOAD 369 370 rump_server_destroy_ifaces 371 } 372 373 bridge_rtable_maxaddr_cleanup() 374 { 375 376 $DEBUG && dump 377 cleanup 378 } 379 380 381 atf_test_case bridge_rtable_delete_member cleanup 382 bridge_rtable_delete_member_head() 383 { 384 385 atf_set "descr" "Tests belonging rtable entries are removed on deleting an interface" 386 atf_set "require.progs" "rump_server" 387 } 388 389 bridge_rtable_delete_member_body() 390 { 391 local addr10= addr30= addr11= addr31= 392 local n= 393 394 setup 395 setup_bridge 396 397 # Add extra interfaces and addresses 398 export RUMP_SERVER=$SOCK1 399 rump_server_add_iface $SOCK1 shmif1 bus1 400 atf_check -s exit:0 rump.ifconfig shmif1 10.0.0.11/24 401 atf_check -s exit:0 rump.ifconfig -w 10 402 403 export RUMP_SERVER=$SOCK3 404 rump_server_add_iface $SOCK3 shmif1 bus2 405 atf_check -s exit:0 rump.ifconfig shmif1 10.0.0.12/24 406 atf_check -s exit:0 rump.ifconfig -w 10 407 408 # Get MAC addresses of the endpoints. 409 addr10=$(get_macaddr $SOCK1 shmif0) 410 addr30=$(get_macaddr $SOCK3 shmif0) 411 addr11=$(get_macaddr $SOCK1 shmif1) 412 addr31=$(get_macaddr $SOCK3 shmif1) 413 414 # Make the bridge learn the MAC addresses of the endpoints. 415 export RUMP_SERVER=$SOCK1 416 atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 10.0.0.12 417 export RUMP_SERVER=$SOCK3 418 atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 10.0.0.11 419 420 export RUMP_SERVER=$SOCK2 421 export LD_PRELOAD=/usr/lib/librumphijack.so 422 $DEBUG && /sbin/brconfig bridge0 423 atf_check -s exit:0 -o match:"$addr10 shmif0" /sbin/brconfig bridge0 424 atf_check -s exit:0 -o match:"$addr11 shmif0" /sbin/brconfig bridge0 425 atf_check -s exit:0 -o match:"$addr30 shmif1" /sbin/brconfig bridge0 426 atf_check -s exit:0 -o match:"$addr31 shmif1" /sbin/brconfig bridge0 427 428 atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 delete shmif0 429 atf_check -s exit:0 -o not-match:"$addr10 shmif0" /sbin/brconfig bridge0 430 atf_check -s exit:0 -o not-match:"$addr11 shmif0" /sbin/brconfig bridge0 431 atf_check -s exit:0 -o match:"$addr30 shmif1" /sbin/brconfig bridge0 432 atf_check -s exit:0 -o match:"$addr31 shmif1" /sbin/brconfig bridge0 433 434 atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 delete shmif1 435 atf_check -s exit:0 -o not-match:"$addr10 shmif0" /sbin/brconfig bridge0 436 atf_check -s exit:0 -o not-match:"$addr11 shmif0" /sbin/brconfig bridge0 437 atf_check -s exit:0 -o not-match:"$addr30 shmif1" /sbin/brconfig bridge0 438 atf_check -s exit:0 -o not-match:"$addr31 shmif1" /sbin/brconfig bridge0 439 440 rump_server_destroy_ifaces 441 } 442 443 bridge_rtable_delete_member_cleanup() 444 { 445 446 $DEBUG && dump 447 cleanup 448 } 449 450 451 atf_test_case bridge_rtable_manyaddrs cleanup 452 bridge_rtable_manyaddrs_head() 453 { 454 455 atf_set "descr" "Tests brconfig addr under many MAC addresses" 456 atf_set "require.progs" "rump_server" 457 atf_set "timeout" "1200" 458 } 459 460 bridge_rtable_manyaddrs_body() 461 { 462 local addr= 463 464 setup 465 setup_bridge 466 467 export RUMP_SERVER=$SOCK2 468 export LD_PRELOAD=/usr/lib/librumphijack.so 469 atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 maxaddr 1024 470 471 # Fill the MAC addresses 472 for i in 1 2 3 4; do 473 for j in $(seq 0 255); do 474 addr="00:11:22:33:4$i:$(printf "%02x" $j)" 475 atf_check -s exit:0 -o empty \ 476 /sbin/brconfig bridge0 static shmif0 $addr 477 done 478 479 n=$(get_number_of_caches) 480 atf_check_equal $n $((i * 256)) 481 done 482 483 484 rump_server_destroy_ifaces 485 } 486 487 bridge_rtable_manyaddrs_cleanup() 488 { 489 490 $DEBUG && dump 491 cleanup 492 } 493 494 495 atf_init_test_cases() 496 { 497 498 atf_add_test_case bridge_rtable_basic 499 atf_add_test_case bridge_rtable_flush 500 atf_add_test_case bridge_rtable_timeout 501 atf_add_test_case bridge_rtable_maxaddr 502 atf_add_test_case bridge_rtable_delete_member 503 atf_add_test_case bridge_rtable_manyaddrs 504 # TODO: brconfig static/flushall/discover/learn 505 } 506