opnsense-src/tests/sys/common/vnet.subr
Kajetan Staszkiewicz ddcdb534b7 pf tests: properly destroy renamed interfaces
The pfsync:pbr tests leaves  lot of interfaces when they finish, making
other tests slower due to long time of loading scapy from pft_ping.py
when more interfaces are present. When both sides of epair are assigned
to jails, they are both removed from created_interfaces.lst and thus
won't be removed during cleanup from this file. An interface assigned to
jail is stored in created_jails.lst but if it is renamed, it won't be
cleaned up either. Furthermore this test uses identical names for
multiple interfaces across multiple jails which after destroying those
jails adds to overall confusion.

To address this issue a new function is provided for renaming interfaces
in jails and storing their new names in created_jails.lst for deletion
during cleanup.

MFC after:	1 week
Sponsored by:	InnoGames GmbH
Differential Revision:	https://reviews.freebsd.org/D38024
2023-01-16 07:23:07 +01:00

105 lines
1.7 KiB
Text

# VNET/jail utility functions
##
list_interface()
{
echo $1 >> created_interfaces.lst
}
unlist_interface()
{
sed -i "" /^$1\$/d created_interfaces.lst
}
vnet_init()
{
if [ "`sysctl -i -n kern.features.vimage`" != 1 ]; then
atf_skip "This test requires VIMAGE"
fi
}
vnet_mkepair()
{
ifname=$(ifconfig epair create)
list_interface $ifname
list_interface ${ifname%a}b
echo ${ifname%a}
}
vnet_mkbridge()
{
ifname=$(ifconfig bridge create)
list_interface $ifname
echo ${ifname}
}
vnet_mkvlan()
{
ifname=$(ifconfig vlan create)
list_interface $ifname
echo ${ifname}
}
vnet_mkloopback()
{
ifname=$(ifconfig lo create)
list_interface $ifname
echo ${ifname}
}
vnet_mkjail()
{
jailname=$1
shift
vnet_interfaces=
for ifname in $@
do
vnet_interfaces="${vnet_interfaces} vnet.interface=${ifname}"
unlist_interface $ifname
done
jail -c name=${jailname} persist vnet ${vnet_interfaces}
echo $jailname $@ >> created_jails.lst
}
vnet_ifmove()
{
ifname=$1
jailname=$2
ifconfig ${ifname} vnet ${jailname}
unlist_interface $ifname
sed -i "" "/^${jailname}/s/\$/ ${ifname}/" created_jails.lst
}
vnet_ifrename_jail()
{
jailname=$1
ifname=$2
ifnewname=$3
jexec ${jailname} ifconfig $ifname name $ifnewname
sed -i "" "/^${jailname}/s/${ifname}/${ifnewname}/" created_jails.lst
}
vnet_cleanup()
{
if [ -f created_jails.lst ]; then
while read jailname ifnames; do
for ifname in ${ifnames}; do
jexec ${jailname} ifconfig ${ifname} destroy
done
jail -r ${jailname}
done < created_jails.lst
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
}