Skip to content

Commit

Permalink
Tidy up tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hack3ric committed Jul 19, 2024
1 parent 01eb09c commit 013aa98
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 227 deletions.
40 changes: 21 additions & 19 deletions tests/bench.bash
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,55 @@ setup() {
switch_env_setup --wg --wg-v6 --wg-mtu=1408

if [ "$1" = "mimic" ]; then
for _i in `seq 0 $max`; do
for _i in {0..1}; do
ip netns exec ${netns[$_i]} "$_curdir/../out/mimic" run ${veth[$_i]} \
"-flocal=[`strip_ip_cidr ${veth_ipv6[$_i]}`]:${wg_port[$_i]}" & _mimic[$_i]=$!
"-flocal=[$(strip_ip_cidr ${veth_ipv6[$_i]})]:${wg_port[$_i]}" &
_mimic[$_i]=$!
done
sleep 1
fi
}

bench() {
local _type="$1"; shift
local _type="$1"
shift

local _dest
if [ "$_type" = "veth" ]; then
_dest=`strip_ip_cidr ${veth_ipv6[0]}`
_dest=$(strip_ip_cidr ${veth_ipv6[0]})
else
_dest=`strip_ip_cidr ${wg_ipv6[0]}`
_dest=$(strip_ip_cidr ${wg_ipv6[0]})
fi

_iperf3_pid_tmp=`mktemp`
_iperf3_pid_tmp=$(mktemp)
ip netns exec ${netns[0]} iperf3 -s -D -I "$_iperf3_pid_tmp"
ip netns exec ${netns[1]} iperf3 -c $_dest $@
}

cleanup() (
set +e
for _i in `seq 0 $max`; do
for _i in {0..1}; do
[ -z ${_mimic[$_i]} ] || kill ${_mimic[$_i]}
done
wait
if [ -n "$_iperf3_pid_tmp" ]; then
kill `cat $_iperf3_pid_tmp`
kill $(cat $_iperf3_pid_tmp)
fi
switch_env_cleanup
)

case "$1" in
clean | cleanup)
cleanup
exit
;;
veth | wg | mimic)
_type=$1
shift
;;
*)
_type=mimic
;;
clean | cleanup)
cleanup
exit
;;
veth | wg | mimic)
_type=$1
shift
;;
*)
_type=mimic
;;
esac

if [ "$UID" -ne 0 ]; then
Expand Down
98 changes: 51 additions & 47 deletions tests/env/router.bash
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,30 @@
# \----------/ \-----------------/ \----------/

br=(brmimicrt{1..2})
netns=(mimicrt{1..3})
netns_internal=(mimicrt{1..3})

veth=(vethmimicrt{1..2}{1..2})
veth_internal=(vethmimicrt{1..2}{1..2})
veth_ipv4_range=(169.254.101.{0,128}/25)
veth_ipv4=(169.254.101.{1,2,129,130}/25)
veth_ipv4_internal=(169.254.101.{1,2,129,130}/25)
veth_ipv6_range=(fc11:{1..2}::/64)
veth_ipv6=(fc11:{1..2}::{1..2}/64)
veth_ipv6_internal=(fc11:{1..2}::{1..2}/64)

netns=(${netns_internal[0]} ${netns_internal[2]})
veth=(${veth_internal[0]} ${veth_internal[3]})
veth_ipv4=(${veth_ipv4_internal[0]} ${veth_ipv4_internal[3]})
veth_ipv6=(${veth_ipv6_internal[0]} ${veth_ipv6_internal[3]})

wg_port=(1101{1..2})
wg_ipv4_range=169.254.201.0/24
wg_ipv4=(169.254.201.{1..2}/24)
wg_ipv6_range=fc21::/64
wg_ipv6=(fc21::{1..2}/64)

strip_ip_cidr() {
sed 's/\/[0-9]\+$//' <(echo "$1")
}
_curdir=$(dirname $(realpath "${BASH_SOURCE[0]}"))
source "$_curdir/../util.bash"

router_env_setup() {
for _netns in ${netns[@]}; do
for _netns in ${netns_internal[@]}; do
ip netns add $_netns
ip netns exec $_netns sysctl -w net.ipv4.conf.all.forwarding=1
ip netns exec $_netns sysctl -w net.ipv6.conf.all.forwarding=1
Expand All @@ -40,44 +44,44 @@ router_env_setup() {
ip link set ${br[$_i]} up

for _j in {0..1}; do
local _n=$(( _i * 2 + _j )) # veth index
local _netns=${netns[$(( _i + _j ))]} # netns where the veth should be in
local _n=$((_i * 2 + _j)) # veth index
local _netns=${netns_internal[$((_i + _j))]} # netns where the veth should be in

ip link add ${veth[$_n]} type veth peer ${veth[$_n]} netns $_netns
ip link set ${veth[$_n]} master ${br[$_i]}
ip link add ${veth_internal[$_n]} type veth peer ${veth_internal[$_n]} netns $_netns
ip link set ${veth_internal[$_n]} master ${br[$_i]}

ip -n $_netns addr add dev ${veth[$_n]} ${veth_ipv4[$_n]}
ip -n $_netns addr add dev ${veth[$_n]} ${veth_ipv6[$_n]}
ip -n $_netns addr add dev ${veth_internal[$_n]} ${veth_ipv4_internal[$_n]}
ip -n $_netns addr add dev ${veth_internal[$_n]} ${veth_ipv6_internal[$_n]}

ip link set ${veth[$_n]} up
ip link set ${veth_internal[$_n]} up
ip -n $_netns link set lo up
ip -n $_netns link set ${veth[$_n]} up
ip -n $_netns link set ${veth_internal[$_n]} up
done
done

sleep 2 # IPv6 addresses seem to need some warmup
sleep 2 # IPv6 addresses seem to need some warmup

# This is a bit messy, but it basically sets correct route of the other side
# through the "router" netns
ip -n ${netns[0]} route add ${veth_ipv4_range[1]} \
via `strip_ip_cidr ${veth_ipv4[1]}` \
src `strip_ip_cidr ${veth_ipv4[0]}` \
dev ${veth[0]}
ip -n ${netns[2]} route add ${veth_ipv4_range[0]} \
via `strip_ip_cidr ${veth_ipv4[2]}` \
src `strip_ip_cidr ${veth_ipv4[3]}` \
dev ${veth[3]}
ip -6 -n ${netns[0]} route add ${veth_ipv6_range[1]} \
via `strip_ip_cidr ${veth_ipv6[1]}` \
src `strip_ip_cidr ${veth_ipv6[0]}` \
dev ${veth[0]}
ip -6 -n ${netns[2]} route add ${veth_ipv6_range[0]} \
via `strip_ip_cidr ${veth_ipv6[2]}` \
src `strip_ip_cidr ${veth_ipv6[3]}` \
dev ${veth[3]}
ip -n ${netns_internal[0]} route add ${veth_ipv4_range[1]} \
via $(strip_ip_cidr ${veth_ipv4_internal[1]}) \
src $(strip_ip_cidr ${veth_ipv4_internal[0]}) \
dev ${veth_internal[0]}
ip -n ${netns_internal[2]} route add ${veth_ipv4_range[0]} \
via $(strip_ip_cidr ${veth_ipv4_internal[2]}) \
src $(strip_ip_cidr ${veth_ipv4_internal[3]}) \
dev ${veth_internal[3]}
ip -6 -n ${netns_internal[0]} route add ${veth_ipv6_range[1]} \
via $(strip_ip_cidr ${veth_ipv6_internal[1]}) \
src $(strip_ip_cidr ${veth_ipv6_internal[0]}) \
dev ${veth_internal[0]}
ip -6 -n ${netns_internal[2]} route add ${veth_ipv6_range[0]} \
via $(strip_ip_cidr ${veth_ipv6_internal[2]}) \
src $(strip_ip_cidr ${veth_ipv6_internal[3]}) \
dev ${veth_internal[3]}

# Enable conntrack in router
ip netns exec ${netns[1]} nft -f - <<EOF
ip netns exec ${netns_internal[1]} nft -f - <<EOF
table inet filter {
chain input {
type filter hook input priority 0; policy accept;
Expand All @@ -94,25 +98,25 @@ router_env_cleanup() {
for _br in ${br[@]}; do
ip link del $_br
done
for _netns in ${netns[@]}; do
for _netns in ${netns_internal[@]}; do
ip netns del $_netns
done
}

if [ "${BASH_SOURCE[0]}" = "$0" ]; then
set -e
case "$1" in
setup)
shift
router_env_setup $@
;;
clean)
shift
router_env_cleanup $@
;;
*)
>&2 echo "expected 'setup' or 'clean'"
exit 1
;;
setup)
shift
router_env_setup $@
;;
clean)
shift
router_env_cleanup $@
;;
*)
>&2 echo "expected 'setup' or 'clean'"
exit 1
;;
esac
fi
65 changes: 31 additions & 34 deletions tests/env/switch.bash
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,27 @@ wg_ipv4=(169.254.200.{1..2}/24)
wg_ipv6_range=fc20::/64
wg_ipv6=(fc20::{1..2}/64)

max=$((${#netns[@]} - 1))

strip_ip_cidr() {
sed 's/\/[0-9]\+$//' <(echo "$1")
}
_curdir=$(dirname $(realpath "${BASH_SOURCE[0]}"))
source "$_curdir/../util.bash"

switch_env_setup() {
for _i in "$@"; do
case "$_i" in
--mtu=*) local mtu="${_i#*=}";;
--no-offload) local no_offload=true;;
--wg) local wg=1;;
--wg-v4) local wg_ip_kind=v4;;
--wg-v6) local wg_ip_kind=v6;;
--wg-mtu=*) local wg_mtu="${_i#*=}";;
*) ;;
--mtu=*) local mtu="${_i#*=}" ;;
--no-offload) local no_offload=true ;;
--wg) local wg=1 ;;
--wg-v4) local wg_ip_kind=v4 ;;
--wg-v6) local wg_ip_kind=v6 ;;
--wg-mtu=*) local wg_mtu="${_i#*=}" ;;
*) ;;
esac
done

ip link add $br type bridge
[ -z "$mtu" ] || ip link set $br mtu "$mtu"
ip link set $br up

for _i in `seq 0 $max`; do
for _i in {0..1}; do
ip netns add ${netns[$_i]}
ip link add ${veth[$_i]} type veth peer ${veth[$_i]} netns ${netns[$_i]}
ip link set ${veth[$_i]} master $br
Expand All @@ -58,26 +55,26 @@ switch_env_setup() {
done

if [ -n "$wg" ]; then
local _priv_key=(`wg genkey` `wg genkey`)
for _i in `seq 0 $max`; do
local _priv_key=($(wg genkey) $(wg genkey))
for _i in {0..1}; do
ip -n ${netns[$_i]} link add wg-${veth[$_i]} type wireguard
ip -n ${netns[$_i]} addr add dev wg-${veth[$_i]} ${wg_ipv4[$_i]}
ip -n ${netns[$_i]} addr add dev wg-${veth[$_i]} ${wg_ipv6[$_i]}

ip netns exec ${netns[$_i]} wg set wg-${veth[$_i]} \
listen-port ${wg_port[$_i]} \
private-key <(echo ${_priv_key[$_i]})
listen-port ${wg_port[$_i]} \
private-key <(echo ${_priv_key[$_i]})

for _j in `seq 0 $max`; do
for _j in {0..1}; do
if [ $_i -eq $_j ]; then continue; fi
local _endpoint
if [ "$wg_ip_kind" = v6 ]; then
_endpoint=\[`strip_ip_cidr ${veth_ipv6[$_j]}`\]
_endpoint=\[$(strip_ip_cidr ${veth_ipv6[$_j]})\]
else
_endpoint=`strip_ip_cidr ${veth_ipv4[$_j]}`
_endpoint=$(strip_ip_cidr ${veth_ipv4[$_j]})
fi
ip netns exec ${netns[$_i]} wg set wg-${veth[$_i]} \
peer `echo ${_priv_key[$_j]} | wg pubkey` \
peer $(echo ${_priv_key[$_j]} | wg pubkey) \
allowed-ips ${wg_ipv4_range},${wg_ipv6_range} \
endpoint $_endpoint:${wg_port[$_j]}
done
Expand All @@ -91,7 +88,7 @@ switch_env_setup() {
switch_env_cleanup() (
set +e
ip link del $br
for _i in `seq 0 $max`; do
for _i in {0..1}; do
ip netns del ${netns[$_i]}
ip link del ${veth[$_i]} 2>/dev/null
done
Expand All @@ -100,17 +97,17 @@ switch_env_cleanup() (
if [ "${BASH_SOURCE[0]}" = "$0" ]; then
set -e
case "$1" in
setup)
shift
switch_env_setup $@
;;
clean)
shift
switch_env_cleanup $@
;;
*)
>&2 echo "expected 'setup' or 'clean'"
exit 1
;;
setup)
shift
switch_env_setup $@
;;
clean)
shift
switch_env_cleanup $@
;;
*)
>&2 echo "expected 'setup' or 'clean'"
exit 1
;;
esac
fi
Loading

0 comments on commit 013aa98

Please sign in to comment.