tests: Move common (vnet) test functions into a common file

The netipsec and pf tests have a number of common test functions. These
used to be duplicated, but it makes more sense for them to re-use the
common functions.

PR:		236223
This commit is contained in:
Kristof Provost 2019-03-04 18:15:06 +00:00
parent 01c27978f5
commit 06aac31aec
17 changed files with 126 additions and 139 deletions

View File

@ -39,4 +39,6 @@ _cddl= cddl
# Items not integrated into kyua runs by default
SUBDIR+= pjdfstest
SUBDIR+= common
.include <bsd.test.mk>

View File

@ -0,0 +1,7 @@
# $FreeBSD$
PACKAGE= common
TESTSDIR= ${TESTSBASE}/sys/common
${PACKAGE}FILES+= vnet.subr
.include <bsd.test.mk>

View File

@ -0,0 +1,51 @@
# $FreeBSD$
# VNAT/jail utility functions
##
vnet_init()
{
if [ "`sysctl -i -n kern.features.vimage`" != 1 ]; then
atf_skip "This test requires VIMAGE"
fi
}
vnet_mkepair()
{
ifname=$(ifconfig epair create)
echo $ifname >> created_interfaces.lst
echo ${ifname%a}
}
vnet_mkjail()
{
jailname=$1
shift
vnet_interfaces=
for ifname in $@
do
vnet_interfaces="${vnet_interfaces} vnet.interface=${ifname}"
done
jail -c name=${jailname} persist vnet ${vnet_interfaces}
echo $jailname >> created_jails.lst
}
vnet_cleanup()
{
if [ -f created_jails.lst ]; then
for jailname in `cat created_jails.lst`
do
jail -r ${jailname}
done
rm created_jails.lst
fi
if [ -f created_interfaces.lst ]; then
for ifname in `cat created_interfaces.lst`
do
ifconfig ${ifname} destroy
done
rm created_interfaces.lst
fi
}

View File

@ -4,51 +4,29 @@
: ${TMPDIR=/tmp}
. $(atf_get_srcdir)/../../common/vnet.subr
ist_init()
{
if [ "$(sysctl -i -n kern.features.vimage)" != 1 ]; then
atf_skip "This test requires VIMAGE"
fi
}
pft_mkepair()
{
ifname=$(ifconfig epair create)
echo $ifname >> created_interfaces.lst
echo ${ifname%a}
}
pft_mkjail()
{
jailname=$1
shift
vnet_interfaces=
for ifname in $@
do
vnet_interfaces="${vnet_interfaces} vnet.interface=${ifname}"
done
jail -c name=${jailname} persist vnet ${vnet_interfaces}
echo $jailname >> created_jails.lst
vnet_init
}
ist_labsetup ()
{
epair_LAN_A=$(pft_mkepair)
epair_LAN_A=$(vnet_mkepair)
ifconfig ${epair_LAN_A}a up
epair_PUB_A=$(pft_mkepair)
epair_PUB_A=$(vnet_mkepair)
ifconfig ${epair_PUB_A}a up
epair_LAN_B=$(pft_mkepair)
epair_LAN_B=$(vnet_mkepair)
ifconfig ${epair_LAN_B}a up
epair_PUB_B=$(pft_mkepair)
epair_PUB_B=$(vnet_mkepair)
ifconfig ${epair_PUB_B}a up
pft_mkjail hostA ${epair_LAN_A}a
pft_mkjail ipsecA ${epair_LAN_A}b ${epair_PUB_A}a
pft_mkjail router ${epair_PUB_A}b ${epair_PUB_B}b
pft_mkjail ipsecB ${epair_LAN_B}b ${epair_PUB_B}a
pft_mkjail hostB ${epair_LAN_B}a
vnet_mkjail hostA ${epair_LAN_A}a
vnet_mkjail ipsecA ${epair_LAN_A}b ${epair_PUB_A}a
vnet_mkjail router ${epair_PUB_A}b ${epair_PUB_B}b
vnet_mkjail ipsecB ${epair_LAN_B}b ${epair_PUB_B}a
vnet_mkjail hostB ${epair_LAN_B}a
}
ist_v4_setup ()
@ -147,20 +125,5 @@ ist_test()
}
ist_cleanup()
{
if [ -f created_jails.lst ]; then
for jailname in $(cat created_jails.lst)
do
jail -r ${jailname}
rm -f ${TMPDIR}/ipsec.${jailname}.conf
done
rm created_jails.lst
fi
if [ -f created_interfaces.lst ]; then
for ifname in $(cat created_interfaces.lst)
do
ifconfig ${ifname} destroy
done
rm created_interfaces.lst
fi
vnet_cleanup
}

View File

@ -13,8 +13,8 @@ pr183198_body()
{
pft_init
epair=$(pft_mkepair)
pft_mkjail alcatraz ${epair}b
epair=$(vnet_mkepair)
vnet_mkjail alcatraz ${epair}b
jexec alcatraz pfctl -e
# Forward with pf enabled

View File

@ -16,13 +16,13 @@ v4_body()
{
pft_init
epair_send=$(pft_mkepair)
epair_send=$(vnet_mkepair)
ifconfig ${epair_send}a 192.0.2.1/24 up
epair_recv=$(pft_mkepair)
epair_recv=$(vnet_mkepair)
ifconfig ${epair_recv}a up
pft_mkjail alcatraz ${epair_send}b ${epair_recv}b
vnet_mkjail alcatraz ${epair_send}b ${epair_recv}b
jexec alcatraz ifconfig ${epair_send}b 192.0.2.2/24 up
jexec alcatraz ifconfig ${epair_recv}b 198.51.100.2/24 up
jexec alcatraz sysctl net.inet.ip.forwarding=1
@ -75,13 +75,13 @@ v6_body()
{
pft_init
epair_send=$(pft_mkepair)
epair_recv=$(pft_mkepair)
epair_send=$(vnet_mkepair)
epair_recv=$(vnet_mkepair)
ifconfig ${epair_send}a inet6 2001:db8:42::1/64 up no_dad -ifdisabled
ifconfig ${epair_recv}a up
pft_mkjail alcatraz ${epair_send}b ${epair_recv}b
vnet_mkjail alcatraz ${epair_send}b ${epair_recv}b
jexec alcatraz ifconfig ${epair_send}b inet6 2001:db8:42::2/64 up no_dad
jexec alcatraz ifconfig ${epair_recv}b inet6 2001:db8:43::2/64 up no_dad

View File

@ -14,8 +14,8 @@ too_many_fragments_body()
{
pft_init
epair=$(pft_mkepair)
pft_mkjail alcatraz ${epair}a
epair=$(vnet_mkepair)
vnet_mkjail alcatraz ${epair}a
ifconfig ${epair}b inet 192.0.2.1/24 up
jexec alcatraz ifconfig ${epair}a 192.0.2.2/24 up
@ -57,11 +57,11 @@ v6_body()
{
pft_init
epair_send=$(pft_mkepair)
epair_link=$(pft_mkepair)
epair_send=$(vnet_mkepair)
epair_link=$(vnet_mkepair)
pft_mkjail alcatraz ${epair_send}b ${epair_link}a
pft_mkjail singsing ${epair_link}b
vnet_mkjail alcatraz ${epair_send}b ${epair_link}a
vnet_mkjail singsing ${epair_link}b
ifconfig ${epair_send}a inet6 2001:db8:42::1/64 no_dad up

View File

@ -13,9 +13,9 @@ names_body()
{
pft_init
epair=$(pft_mkepair)
epair=$(vnet_mkepair)
pft_mkjail alcatraz ${epair}b
vnet_mkjail alcatraz ${epair}b
ifconfig ${epair}a name foo
jexec alcatraz ifconfig ${epair}b name foo

View File

@ -13,11 +13,11 @@ exhaust_body()
{
pft_init
epair_nat=$(pft_mkepair)
epair_echo=$(pft_mkepair)
epair_nat=$(vnet_mkepair)
epair_echo=$(vnet_mkepair)
pft_mkjail nat ${epair_nat}b ${epair_echo}a
pft_mkjail echo ${epair_echo}b
vnet_mkjail nat ${epair_nat}b ${epair_echo}a
vnet_mkjail echo ${epair_echo}b
ifconfig ${epair_nat}a 192.0.2.2/24 up
route add -net 198.51.100.0/24 192.0.2.1

View File

@ -13,11 +13,11 @@ v4_body()
{
pft_init
epair=$(pft_mkepair)
epair=$(vnet_mkepair)
ifconfig ${epair}a 192.0.2.1/24 up
# Set up a simple jail with one interface
pft_mkjail alcatraz ${epair}b
vnet_mkjail alcatraz ${epair}b
jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
# Trivial ping to the jail, without pf
@ -52,11 +52,11 @@ v6_body()
{
pft_init
epair=$(pft_mkepair)
epair=$(vnet_mkepair)
ifconfig ${epair}a inet6 2001:db8:42::1/64 up no_dad
# Set up a simple jail with one interface
pft_mkjail alcatraz ${epair}b
vnet_mkjail alcatraz ${epair}b
jexec alcatraz ifconfig ${epair}b inet6 2001:db8:42::2/64 up no_dad
# Trivial ping to the jail, without pf
@ -95,10 +95,10 @@ noalias_body()
{
pft_init
epair=$(pft_mkepair)
epair=$(vnet_mkepair)
ifconfig ${epair}a inet6 2001:db8:42::1/64 up no_dad
pft_mkjail alcatraz ${epair}b
vnet_mkjail alcatraz ${epair}b
jexec alcatraz ifconfig ${epair}b inet6 2001:db8:42::2/64 up no_dad
linklocaladdr=$(jexec alcatraz ifconfig ${epair}b inet6 \

View File

@ -19,12 +19,12 @@ common_body()
defer=$1
pfsynct_init
epair_sync=$(pft_mkepair)
epair_one=$(pft_mkepair)
epair_two=$(pft_mkepair)
epair_sync=$(vnet_mkepair)
epair_one=$(vnet_mkepair)
epair_two=$(vnet_mkepair)
pft_mkjail one ${epair_one}a ${epair_sync}a
pft_mkjail two ${epair_two}a ${epair_sync}b
vnet_mkjail one ${epair_one}a ${epair_sync}a
vnet_mkjail two ${epair_two}a ${epair_sync}b
# pfsync interface
jexec one ifconfig ${epair_sync}a 192.0.2.1/24 up

View File

@ -13,9 +13,9 @@ basic_body()
{
pft_init
epair=$(pft_mkepair)
epair=$(vnet_mkepair)
pft_mkjail alcatraz ${epair}b
vnet_mkjail alcatraz ${epair}b
ifconfig ${epair}a 192.0.2.2/24 up
route add -net 198.51.100.0/24 192.0.2.1

View File

@ -13,12 +13,12 @@ v4_body()
{
pft_init
epair_send=$(pft_mkepair)
epair_send=$(vnet_mkepair)
ifconfig ${epair_send}a 192.0.2.1/24 up
epair_route=$(pft_mkepair)
epair_route=$(vnet_mkepair)
ifconfig ${epair_route}a 203.0.113.1/24 up
pft_mkjail alcatraz ${epair_send}b ${epair_route}b
vnet_mkjail alcatraz ${epair_send}b ${epair_route}b
jexec alcatraz ifconfig ${epair_send}b 192.0.2.2/24 up
jexec alcatraz ifconfig ${epair_route}b 203.0.113.2/24 up
jexec alcatraz route add -net 198.51.100.0/24 192.0.2.1
@ -49,12 +49,12 @@ v6_body()
{
pft_init
epair_send=$(pft_mkepair)
epair_send=$(vnet_mkepair)
ifconfig ${epair_send}a inet6 2001:db8:42::1/64 up no_dad -ifdisabled
epair_route=$(pft_mkepair)
epair_route=$(vnet_mkepair)
ifconfig ${epair_route}a inet6 2001:db8:43::1/64 up no_dad -ifdisabled
pft_mkjail alcatraz ${epair_send}b ${epair_route}b
vnet_mkjail alcatraz ${epair_send}b ${epair_route}b
jexec alcatraz ifconfig ${epair_send}b inet6 2001:db8:42::2/64 up no_dad
jexec alcatraz ifconfig ${epair_route}b inet6 2001:db8:43::2/64 up no_dad
jexec alcatraz route add -6 2001:db8:666::/64 2001:db8:42::2

View File

@ -14,7 +14,7 @@ set_skip_group_body()
# See PR 229241
pft_init
pft_mkjail alcatraz
vnet_mkjail alcatraz
jexec alcatraz ifconfig lo0 127.0.0.1/8 up
jexec alcatraz ifconfig lo0 group foo
jexec alcatraz pfctl -e
@ -42,7 +42,7 @@ set_skip_group_lo_body()
# See PR 229241
pft_init
pft_mkjail alcatraz
vnet_mkjail alcatraz
jexec alcatraz ifconfig lo0 127.0.0.1/8 up
jexec alcatraz pfctl -e
pft_set_rules alcatraz "set skip on lo" \

View File

@ -16,13 +16,13 @@ v4_body()
{
pft_init
epair_send=$(pft_mkepair)
epair_send=$(vnet_mkepair)
ifconfig ${epair_send}a 192.0.2.1/24 up
epair_recv=$(pft_mkepair)
epair_recv=$(vnet_mkepair)
ifconfig ${epair_recv}a up
pft_mkjail alcatraz ${epair_send}b ${epair_recv}b
vnet_mkjail alcatraz ${epair_send}b ${epair_recv}b
jexec alcatraz ifconfig ${epair_send}b 192.0.2.2/24 up
jexec alcatraz ifconfig ${epair_recv}b 198.51.100.2/24 up
jexec alcatraz sysctl net.inet.ip.forwarding=1

View File

@ -13,18 +13,18 @@ synproxy_body()
{
pft_init
epair=$(pft_mkepair)
epair=$(vnet_mkepair)
ifconfig ${epair}a 192.0.2.1/24 up
route add -net 198.51.100.0/24 192.0.2.2
link=$(pft_mkepair)
link=$(vnet_mkepair)
pft_mkjail alcatraz ${epair}b ${link}a
vnet_mkjail alcatraz ${epair}b ${link}a
jexec alcatraz ifconfig ${epair}b 192.0.2.2/24 up
jexec alcatraz ifconfig ${link}a 198.51.100.1/24 up
jexec alcatraz sysctl net.inet.ip.forwarding=1
pft_mkjail singsing ${link}b
vnet_mkjail singsing ${link}b
jexec singsing ifconfig ${link}b 198.51.100.2/24 up
jexec singsing route add default 198.51.100.1

View File

@ -2,15 +2,15 @@
# Utility functions
##
. $(atf_get_srcdir)/../../common/vnet.subr
pft_init()
{
vnet_init
if [ ! -c /dev/pf ]; then
atf_skip "This test requires pf"
fi
if [ "`sysctl -i -n kern.features.vimage`" != 1 ]; then
atf_skip "This test requires VIMAGE"
fi
}
pfsynct_init()
@ -22,28 +22,6 @@ pfsynct_init()
fi
}
pft_mkepair()
{
ifname=$(ifconfig epair create)
echo $ifname >> created_interfaces.lst
echo ${ifname%a}
}
pft_mkjail()
{
jailname=$1
shift
vnet_interfaces=
for ifname in $@
do
vnet_interfaces="${vnet_interfaces} vnet.interface=${ifname}"
done
jail -c name=${jailname} persist vnet ${vnet_interfaces}
echo $jailname >> created_jails.lst
}
pft_set_rules()
{
jname=$1
@ -66,21 +44,7 @@ pft_set_rules()
pft_cleanup()
{
if [ -f created_jails.lst ]; then
for jailname in `cat created_jails.lst`
do
jail -r ${jailname}
done
rm created_jails.lst
fi
if [ -f created_interfaces.lst ]; then
for ifname in `cat created_interfaces.lst`
do
ifconfig ${ifname} destroy
done
rm created_interfaces.lst
fi
vnet_cleanup
}
pfsynct_cleanup()