# $NetBSD: t_rtable.sh,v 1.8 2023/03/26 19:10:34 andvar Exp $
#
# Copyright (c) 2017 Internet Initiative Japan Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# Get MAC addresses of the endpoints.
addr1=$(get_macaddr $SOCK1 shmif0)
addr3=$(get_macaddr $SOCK3 shmif0)
# Confirm there is no MAC address caches.
export RUMP_SERVER=$SOCK2
export LD_PRELOAD=/usr/lib/librumphijack.so
$DEBUG && /sbin/brconfig bridge0
atf_check -s exit:0 -o not-match:"$addr1" /sbin/brconfig bridge0
atf_check -s exit:0 -o not-match:"$addr3" /sbin/brconfig bridge0
unset LD_PRELOAD
# Make the bridge learn the MAC addresses of the endpoints.
export RUMP_SERVER=$SOCK1
atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2
unset RUMP_SERVER
# Tests the addresses are in the cache.
export RUMP_SERVER=$SOCK2
export LD_PRELOAD=/usr/lib/librumphijack.so
$DEBUG && /sbin/brconfig bridge0
atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0
atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0
bridge_rtable_flush_body()
{
local addr1= addr3=
local n=
setup
setup_bridge
# Get MAC addresses of the endpoints.
addr1=$(get_macaddr $SOCK1 shmif0)
addr3=$(get_macaddr $SOCK3 shmif0)
# Make the bridge learn the MAC addresses of the endpoints.
export RUMP_SERVER=$SOCK1
atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2
unset RUMP_SERVER
# Tests the addresses are in the cache.
export RUMP_SERVER=$SOCK2
export LD_PRELOAD=/usr/lib/librumphijack.so
$DEBUG && /sbin/brconfig bridge0
atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0
atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0
# Make the bridge learn the MAC addresses of the endpoints.
export RUMP_SERVER=$SOCK1
atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2
unset RUMP_SERVER
# Tests the addresses are in the cache.
export RUMP_SERVER=$SOCK2
export LD_PRELOAD=/usr/lib/librumphijack.so
$DEBUG && /sbin/brconfig bridge0
atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0
atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0
# TODO: cache expiration
# The initial timeout value of a cache is changed to $timeout and
# after $timeout elapsed the cache is ready to be sweeped. However,
# the GC of rtable runs every 5 minutes and the cache remains until
# then. Should we have a sysctl to change the period?
#sleep $(($timeout + 2))
#
## Tests the addresses are not in the cache.
#export RUMP_SERVER=$SOCK2
#export LD_PRELOAD=/usr/lib/librumphijack.so
#$DEBUG && /sbin/brconfig bridge0
#atf_check -s exit:0 -o not-match:"$addr1 shmif0" /sbin/brconfig bridge0
#atf_check -s exit:0 -o not-match:"$addr3 shmif1" /sbin/brconfig bridge0
bridge_rtable_maxaddr_body()
{
local addr1= addr3=
setup
setup_bridge
# Get MAC addresses of the endpoints.
addr1=$(get_macaddr $SOCK1 shmif0)
addr3=$(get_macaddr $SOCK3 shmif0)
# Fill the MAC addresses of the endpoints.
export RUMP_SERVER=$SOCK1
atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2
export RUMP_SERVER=$SOCK2
export LD_PRELOAD=/usr/lib/librumphijack.so
/sbin/brconfig bridge0
atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0
atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0
# Check the default # of caches is 100
atf_check -s exit:0 -o match:"max cache: 100" /sbin/brconfig bridge0
# Test two MAC addresses are cached
n=$(get_number_of_caches)
atf_check_equal $n 2
# Limit # of caches to one
atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 maxaddr 1
atf_check -s exit:0 -o match:"max cache: 1" /sbin/brconfig bridge0
/sbin/brconfig bridge0
# Check a cache is flushed out
n=$(get_number_of_caches)
atf_check_equal $n 1
# Test a new address cache is not created
export RUMP_SERVER=$SOCK1
atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2
export RUMP_SERVER=$SOCK2
n=$(get_number_of_caches)
atf_check_equal $n 1
# Increase # of caches to two
atf_check -s exit:0 -o ignore /sbin/brconfig bridge0 maxaddr 2
atf_check -s exit:0 -o match:"max cache: 2" /sbin/brconfig bridge0
unset LD_PRELOAD
# Test we can cache two addresses again
export RUMP_SERVER=$SOCK1
atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 $IP2
export RUMP_SERVER=$SOCK2
export LD_PRELOAD=/usr/lib/librumphijack.so
/sbin/brconfig bridge0
atf_check -s exit:0 -o match:"$addr1 shmif0" /sbin/brconfig bridge0
atf_check -s exit:0 -o match:"$addr3 shmif1" /sbin/brconfig bridge0
unset LD_PRELOAD
# Get MAC addresses of the endpoints.
addr10=$(get_macaddr $SOCK1 shmif0)
addr30=$(get_macaddr $SOCK3 shmif0)
addr11=$(get_macaddr $SOCK1 shmif1)
addr31=$(get_macaddr $SOCK3 shmif1)
# Make the bridge learn the MAC addresses of the endpoints.
export RUMP_SERVER=$SOCK1
atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 10.0.0.12
export RUMP_SERVER=$SOCK3
atf_check -s exit:0 -o ignore rump.ping -n -w $TIMEOUT -c 1 10.0.0.11
# Fill the MAC addresses
for i in 1 2 3 4; do
for j in $(seq 0 255); do
addr="00:11:22:33:4$i:$(printf "%02x" $j)"
atf_check -s exit:0 -o empty \
/sbin/brconfig bridge0 static shmif0 $addr
done