xref: /freebsd/tests/sys/common/vnet.subr (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
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_ifrename_jail()
77{
78	jailname=$1
79	ifname=$2
80	ifnewname=$3
81
82	jexec ${jailname} ifconfig $ifname name $ifnewname
83	sed -i "" "/^${jailname}/s/${ifname}/${ifnewname}/" created_jails.lst
84}
85
86vnet_cleanup()
87{
88	if [ -f created_jails.lst ]; then
89		while read jailname ifnames; do
90			for ifname in ${ifnames}; do
91				jexec ${jailname} ifconfig ${ifname} destroy
92			done
93			jail -r ${jailname}
94		done < created_jails.lst
95		rm created_jails.lst
96	fi
97
98	if [ -f created_interfaces.lst ]; then
99		for ifname in `cat created_interfaces.lst`
100		do
101			ifconfig ${ifname} destroy
102		done
103		rm created_interfaces.lst
104	fi
105}
106