xref: /freebsd/tests/sys/common/vnet.subr (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
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
14_vnet_check_req()
15{
16	type=$1
17
18	if kldstat -q -n if_${type}.ko; then
19		return
20	fi
21
22	if ! kldload -n -q if_${type}; then
23		atf_skip "if_${type}.ko is required to run this test."
24		return
25	fi
26}
27
28vnet_init()
29{
30	if [ "`sysctl -i -n kern.features.vimage`" != 1 ]; then
31		atf_skip "This test requires VIMAGE"
32	fi
33
34	# Check if we can create if_epair or if_bridge interfaces.
35	# We may be running in a jail already, unable to load modules.
36	# If so, skip this test because it very likely (but not certainly)
37	# wants at least one of those
38	_vnet_check_req epair
39	_vnet_check_req bridge
40}
41
42vnet_mkepair()
43{
44	ifname=$(ifconfig epair create)
45	list_interface $ifname
46	list_interface ${ifname%a}b
47	echo ${ifname%a}
48}
49
50vnet_init_bridge()
51{
52	if ! kldstat -q -m if_bridge; then
53		atf_skip "This test requires if_bridge"
54	fi
55}
56
57vnet_mkbridge()
58{
59	ifname=$(ifconfig bridge create)
60	list_interface $ifname
61	echo ${ifname}
62}
63
64vnet_mkvlan()
65{
66	ifname=$(ifconfig vlan create)
67	list_interface $ifname
68	echo ${ifname}
69}
70
71vnet_mkloopback()
72{
73	ifname=$(ifconfig lo create)
74	list_interface $ifname
75	echo ${ifname}
76}
77
78vnet_mkjail()
79{
80	jailname=$1
81	shift
82
83	vnet_interfaces=
84	for ifname in $@
85	do
86		vnet_interfaces="${vnet_interfaces} vnet.interface=${ifname}"
87		unlist_interface $ifname
88	done
89	jail -c name=${jailname} persist vnet ${vnet_interfaces}
90
91	echo $jailname $@ >> created_jails.lst
92}
93
94vnet_ifmove()
95{
96	ifname=$1
97	jailname=$2
98
99	ifconfig ${ifname} vnet ${jailname}
100	unlist_interface $ifname
101	sed -i "" "/^${jailname}/s/\$/ ${ifname}/" created_jails.lst
102}
103
104vnet_ifrename_jail()
105{
106	jailname=$1
107	ifname=$2
108	ifnewname=$3
109
110	ifconfig -j ${jailname} $ifname name $ifnewname
111	sed -i "" "/^${jailname}/s/${ifname}/${ifnewname}/" created_jails.lst
112}
113
114vnet_cleanup()
115{
116	if [ -f created_jails.lst ]; then
117		while read jailname ifnames; do
118			for ifname in ${ifnames}; do
119				ifconfig -j ${jailname} ${ifname} destroy
120			done
121			jail -r ${jailname}
122		done < created_jails.lst
123		rm created_jails.lst
124	fi
125
126	if [ -f created_interfaces.lst ]; then
127		while read ifname; do
128			ifconfig ${ifname} destroy
129		done < created_interfaces.lst
130		rm created_interfaces.lst
131	fi
132}
133