xref: /freebsd/tests/sys/common/vnet.subr (revision e9ac41698b2f322d55ccf9da50a3596edb2c1800)
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_init_bridge()
30{
31	if ! kldstat -q -m if_bridge; then
32		atf_skip "This test requires if_bridge"
33	fi
34}
35
36vnet_mkbridge()
37{
38	ifname=$(ifconfig bridge create)
39	list_interface $ifname
40	echo ${ifname}
41}
42
43vnet_mkvlan()
44{
45	ifname=$(ifconfig vlan create)
46	list_interface $ifname
47	echo ${ifname}
48}
49
50vnet_mkloopback()
51{
52	ifname=$(ifconfig lo create)
53	list_interface $ifname
54	echo ${ifname}
55}
56
57vnet_mkjail()
58{
59	jailname=$1
60	shift
61
62	vnet_interfaces=
63	for ifname in $@
64	do
65		vnet_interfaces="${vnet_interfaces} vnet.interface=${ifname}"
66		unlist_interface $ifname
67	done
68	jail -c name=${jailname} persist vnet ${vnet_interfaces}
69
70	echo $jailname $@ >> created_jails.lst
71}
72
73vnet_ifmove()
74{
75	ifname=$1
76	jailname=$2
77
78	ifconfig ${ifname} vnet ${jailname}
79	unlist_interface $ifname
80	sed -i "" "/^${jailname}/s/\$/ ${ifname}/" created_jails.lst
81}
82
83vnet_ifrename_jail()
84{
85	jailname=$1
86	ifname=$2
87	ifnewname=$3
88
89	ifconfig -j ${jailname} $ifname name $ifnewname
90	sed -i "" "/^${jailname}/s/${ifname}/${ifnewname}/" created_jails.lst
91}
92
93vnet_cleanup()
94{
95	if [ -f created_jails.lst ]; then
96		while read jailname ifnames; do
97			for ifname in ${ifnames}; do
98				ifconfig -j ${jailname} ${ifname} destroy
99			done
100			jail -r ${jailname}
101		done < created_jails.lst
102		rm created_jails.lst
103	fi
104
105	if [ -f created_interfaces.lst ]; then
106		while read ifname; do
107			ifconfig ${ifname} destroy
108		done < created_interfaces.lst
109		rm created_interfaces.lst
110	fi
111}
112