Changeset View
Changeset View
Standalone View
Standalone View
tools/test/ng_macfilter/ng_macfilter_test.sh
- This file was added.
Property | Old Value | New Value |
---|---|---|
svn:eol-style | null | native \ No newline at end of property |
svn:executable | null | * \ No newline at end of property |
svn:keywords | null | FreeBSD=%H \ No newline at end of property |
svn:mime-type | null | text/plain \ No newline at end of property |
#!/bin/sh | |||||
################################################################################ | |||||
echo "===> Setting up system... <===============================================" | |||||
for kmod in netgraph ng_socket ng_ether ng_macfilter ng_one2many; do | |||||
kldstat -q -m $kmod \ | |||||
|| kldload $kmod | |||||
done | |||||
# Figure out the first ethernet interface | |||||
eth=$(ifconfig -u -l ether | awk '{print $1}') | |||||
echo "---> Using $eth..." | |||||
progname="$(basename $0 .sh)" | |||||
entries_lst="/tmp/$progname.entries.lst" | |||||
entries2_lst="/tmp/$progname.entries2.lst" | |||||
EDGES=3 | |||||
ITERATIONS=7 | |||||
SUBITERATIONS=71 | |||||
################################################################################ | |||||
echo "===> Configuring netgraph nodes... <======================================" | |||||
# We insert macfilter->one2many between lower and upper hooks in $eth: | |||||
ngctl mkpeer $eth: macfilter lower ether # Connect the lower hook of the ether instance $eth to the ether hook of a new macfilter instance | |||||
ngctl name $eth:lower MF # Give the macfilter instance a name | |||||
ngctl mkpeer MF: one2many default many0 # Then connect macfilter:default to the many0 hook of a one2many instance | |||||
ngctl name MF:default O2M # Give the one2many instance a name | |||||
ngctl msg O2M: setconfig "{ xmitAlg=3 failAlg=1 enabledLinks=[ 1 1 ] }" # XMIT_FAILOVER -> send replies always out many0 | |||||
ngctl connect O2M: $eth: one upper # Finally connect O2M:one to the upper hook of ether instance $eth | |||||
# Additionally conenct multiple outputs from macfilter to one2many | |||||
for i in $(seq 1 1 $EDGES); do | |||||
ngctl connect MF: O2M: out$i many$i | |||||
done | |||||
rc=0 | |||||
################################################################################ | |||||
cleanup () { | |||||
echo "===> Deconfiguring netgraph nodes... <====================================" | |||||
ngctl shutdown MF: | |||||
ngctl shutdown O2M: | |||||
rm -f $entries_lst $entries2_lst | |||||
} | |||||
trap 'exit 99' 1 2 3 13 14 15 | |||||
trap 'cleanup' EXIT | |||||
countmacs () { | |||||
local hookname=${1:-'[^"]*'} | |||||
ngctl msg MF: gethooks \ | |||||
| perl -ne 'sub BEGIN {$c=0} $c += $1 while s/hookname="'$hookname'" hookid=\d+ maccnt=(\d+)//; sub END {print "$c\n"}' | |||||
} | |||||
randomedge () { | |||||
edge="out$(seq 0 1 $EDGES | sort -R | head -1)" | |||||
test $edge = 'out0' && edge="default" | |||||
echo $edge | |||||
} | |||||
genmac () { | |||||
echo "00:00:00:00:$(printf "%02x" $1):$(printf "%02x" $2)" | |||||
} | |||||
validate_counts () { | |||||
if [ $1 = $2 ]; then | |||||
echo "---> $1 MACs $3" | |||||
return 0 | |||||
else | |||||
echo "**** $1 vs $n2 MACs $3" 1>&2 | |||||
return 1 | |||||
fi | |||||
} | |||||
################################################################################ | |||||
echo "===> Test: Adding many MACs... <==========================================" | |||||
I=1 | |||||
for i in $(seq $ITERATIONS | sort -R); do | |||||
echo "---> Iteration $I/$iterations..." | |||||
for j in $(seq 0 1 $SUBITERATIONS); do | |||||
test $i = 2 && edge='out2' || edge='out1' | |||||
ether=$(genmac $j $i) | |||||
ngctl msg MF: direct "{ hookname=\"$edge\" ether=$ether }" \ | |||||
|| echo "**** add failed for $ether:$edge ($(date '+%H:%M:%S'))" 1>&2 | |||||
done | |||||
I=$(($I + 1)) | |||||
done | |||||
n=$(countmacs out1) | |||||
n2=$(( ( $ITERATIONS - 1 ) * ( $SUBITERATIONS + 1 ) )) | |||||
validate_counts $n $n2 'in table for out1' | |||||
rc=$? | |||||
n=$(countmacs out2) | |||||
n2=$(( 1 * ( $SUBITERATIONS + 1 ) )) | |||||
validate_counts $n $n2 'in table for out2' | |||||
rc=$? | |||||
n=$(countmacs out3) | |||||
n2=0 | |||||
validate_counts $n $n2 'in table for out3' | |||||
rc=$? | |||||
test $rc = 0 \ | |||||
|| exit 1 | |||||
################################################################################ | |||||
echo "===> Test: Changing EDGES for MACs... <===================================" | |||||
for i in $(seq $ITERATIONS); do | |||||
edge='out3' | |||||
ether=$(genmac 0 $i) | |||||
ngctl msg MF: direct "{ hookname=\"$edge\" ether=$ether }" \ | |||||
|| echo "**** add failed for $ether:$edge ($(date '+%H:%M:%S'))" 1>&2 | |||||
done | |||||
n=$(countmacs out1) | |||||
n2=$(( ( $ITERATIONS - 1 ) * ( $SUBITERATIONS + 1 - 1 ) )) | |||||
validate_counts $n $n2 'in table for out1' | |||||
rc=$? | |||||
n=$(countmacs out2) | |||||
n2=$(( 1 * ( $SUBITERATIONS + 1 - 1 ) )) | |||||
validate_counts $n $n2 'in table for out2' | |||||
rc=$? | |||||
n=$(countmacs out3) | |||||
n2=$ITERATIONS | |||||
validate_counts $n $n2 'in table for out3' | |||||
rc=$? | |||||
test $rc = 0 \ | |||||
|| exit 1 | |||||
################################################################################ | |||||
echo "===> Test: Removing all MACs one by one... <==============================" | |||||
I=1 | |||||
for i in $(seq $ITERATIONS | sort -R); do | |||||
echo "---> Iteration $I/$iterations..." | |||||
for j in $(seq 0 1 $SUBITERATIONS | sort -R); do | |||||
edge="default" | |||||
ether=$(genmac $j $i) | |||||
ngctl msg MF: direct "{ hookname=\"$edge\" ether=$ether }" \ | |||||
|| echo "**** remove failed for $ether:$edge ($(date '+%H:%M:%S'))" 1>&2 | |||||
done | |||||
I=$(($I + 1)) | |||||
done | |||||
n=$(countmacs) | |||||
n2=0 | |||||
validate_counts $n $n2 'in table' | |||||
rc=$? | |||||
################################################################################ | |||||
rm -f $entries_lst | |||||
echo "===> Test: Randomly adding MACs on random edges... <======================" | |||||
for i in $(seq $ITERATIONS); do | |||||
echo "---> Iteration $i/$iterations..." | |||||
for j in $(seq 0 1 $SUBITERATIONS | sort -R); do | |||||
edge=$(randomedge) | |||||
ether=$(genmac $j $i) | |||||
ngctl msg MF: direct "{ hookname=\"$edge\" ether=$ether }" \ | |||||
|| echo "**** add failed for $ether:$edge" 1>&2 | |||||
echo $ether $edge >> $entries_lst | |||||
done | |||||
n=$(countmacs) | |||||
echo "---> $n MACs in table" | |||||
done | |||||
n=$(countmacs out1) | |||||
n2=$(grep -c ' out1' $entries_lst) | |||||
validate_counts $n $n2 'in table for out1' | |||||
rc=$? | |||||
n=$(countmacs out2) | |||||
n2=$(grep -c ' out2' $entries_lst) | |||||
validate_counts $n $n2 'in table for out2' | |||||
rc=$? | |||||
n=$(countmacs out3) | |||||
n2=$(grep -c ' out3' $entries_lst) | |||||
validate_counts $n $n2 'in table for out3' | |||||
rc=$? | |||||
test $rc = 0 \ | |||||
|| exit 1 | |||||
################################################################################ | |||||
rm -f $entries2_lst | |||||
echo "===> Test: Randomly changing MAC assignments... <=========================" | |||||
for i in $(seq $ITERATIONS); do | |||||
echo "---> Iteration $i/$iterations..." | |||||
cat $entries_lst | while read ether edge; do | |||||
edge2=$(randomedge) | |||||
ngctl msg MF: direct "{ hookname=\"$edge2\" ether=$ether }" \ | |||||
|| echo "**** change failed for $ether:$edge to $ether:$edge2" 1>&2 | |||||
echo $ether $edge2 >> $entries2_lst | |||||
done | |||||
n=$(countmacs out1) | |||||
n2=$(grep -c ' out1' $entries2_lst) | |||||
validate_counts $n $n2 'in table for out1' | |||||
rc=$? | |||||
n=$(countmacs out2) | |||||
n2=$(grep -c ' out2' $entries2_lst) | |||||
validate_counts $n $n2 'in table for out2' | |||||
rc=$? | |||||
n=$(countmacs out3) | |||||
n2=$(grep -c ' out3' $entries2_lst) | |||||
validate_counts $n $n2 'in table for out3' | |||||
rc=$? | |||||
test $rc = 0 \ | |||||
|| exit 1 | |||||
mv $entries2_lst $entries_lst | |||||
done | |||||
################################################################################ | |||||
echo "===> Test: Resetting macfilter... <=======================================" | |||||
ngctl msg MF: reset \ | |||||
|| echo "**** reset failed" 1>&2 | |||||
validate_counts $(countmacs) 0 'in table' | |||||
rc=$? | |||||
test $rc = 0 \ | |||||
|| exit 1 | |||||
################################################################################ | |||||
echo "===> Test: Complete! <====================================================" | |||||
exit 0 |