xref: /freebsd/tests/sys/common/vnet.subr (revision 1323ec571215a77ddd21294f0871979d5ad6b992)
1# VNET/jail utility functions
2##
3
4list_interface()
5{
6	echo $1 >> created_interfaces.lst
7}
8
9unlist_interface()
10{
11	sed -i "" /^$1\$/d created_interfaces.lst
12}
13
14vnet_init()
15{
16	if [ "`sysctl -i -n kern.features.vimage`" != 1 ]; then
17		atf_skip "This test requires VIMAGE"
18	fi
19}
20
21vnet_mkepair()
22{
23	ifname=$(ifconfig epair create)
24	list_interface $ifname
25	list_interface ${ifname%a}b
26	echo ${ifname%a}
27}
28
29vnet_mkbridge()
30{
31	ifname=$(ifconfig bridge create)
32	list_interface $ifname
33	echo ${ifname}
34}
35
36vnet_mkvlan()
37{
38	ifname=$(ifconfig vlan create)
39	list_interface $ifname
40	echo ${ifname}
41}
42
43vnet_mkloopback()
44{
45	ifname=$(ifconfig lo create)
46	list_interface $ifname
47	echo ${ifname}
48}
49
50vnet_mkjail()
51{
52	jailname=$1
53	shift
54
55	vnet_interfaces=
56	for ifname in $@
57	do
58		vnet_interfaces="${vnet_interfaces} vnet.interface=${ifname}"
59		unlist_interface $ifname
60	done
61	jail -c name=${jailname} persist vnet ${vnet_interfaces}
62
63	echo $jailname $@ >> created_jails.lst
64}
65
66vnet_ifmove()
67{
68	ifname=$1
69	jailname=$2
70
71	ifconfig ${ifname} vnet ${jailname}
72	unlist_interface $ifname
73	sed -i "" "/^${jailname}/s/\$/ ${ifname}/" created_jails.lst
74}
75
76vnet_cleanup()
77{
78	if [ -f created_jails.lst ]; then
79		while read jailname ifnames; do
80			for ifname in ${ifnames}; do
81				jexec ${jailname} ifconfig ${ifname} destroy
82			done
83			jail -r ${jailname}
84		done < created_jails.lst
85		rm created_jails.lst
86	fi
87
88	if [ -f created_interfaces.lst ]; then
89		for ifname in `cat created_interfaces.lst`
90		do
91			ifconfig ${ifname} destroy
92		done
93		rm created_interfaces.lst
94	fi
95}
96