xref: /freebsd-src/tests/sys/common/vnet.subr (revision ae8d58814089308028046ac80aeeb9cbb784bd0a)
180fc2502SGleb Smirnoff# VNET/jail utility functions
206aac31aSKristof Provost##
306aac31aSKristof Provost
480fc2502SGleb Smirnofflist_interface()
580fc2502SGleb Smirnoff{
680fc2502SGleb Smirnoff	echo $1 >> created_interfaces.lst
780fc2502SGleb Smirnoff}
880fc2502SGleb Smirnoff
980fc2502SGleb Smirnoffunlist_interface()
1080fc2502SGleb Smirnoff{
1180fc2502SGleb Smirnoff	sed -i "" /^$1\$/d created_interfaces.lst
1280fc2502SGleb Smirnoff}
1380fc2502SGleb Smirnoff
14*ae8d5881SKristof Provost_vnet_check_req()
15*ae8d5881SKristof Provost{
16*ae8d5881SKristof Provost	type=$1
17*ae8d5881SKristof Provost
18*ae8d5881SKristof Provost	if kldstat -q -n if_${type}.ko; then
19*ae8d5881SKristof Provost		return
20*ae8d5881SKristof Provost	fi
21*ae8d5881SKristof Provost
22*ae8d5881SKristof Provost	if ! kldload -n -q if_${type}; then
23*ae8d5881SKristof Provost		atf_skip "if_${type}.ko is required to run this test."
24*ae8d5881SKristof Provost		return
25*ae8d5881SKristof Provost	fi
26*ae8d5881SKristof Provost}
27*ae8d5881SKristof Provost
2806aac31aSKristof Provostvnet_init()
2906aac31aSKristof Provost{
3006aac31aSKristof Provost	if [ "`sysctl -i -n kern.features.vimage`" != 1 ]; then
3106aac31aSKristof Provost		atf_skip "This test requires VIMAGE"
3206aac31aSKristof Provost	fi
33*ae8d5881SKristof Provost
34*ae8d5881SKristof Provost	# Check if we can create if_epair or if_bridge interfaces.
35*ae8d5881SKristof Provost	# We may be running in a jail already, unable to load modules.
36*ae8d5881SKristof Provost	# If so, skip this test because it very likely (but not certainly)
37*ae8d5881SKristof Provost	# wants at least one of those
38*ae8d5881SKristof Provost	_vnet_check_req epair
39*ae8d5881SKristof Provost	_vnet_check_req bridge
4006aac31aSKristof Provost}
4106aac31aSKristof Provost
4206aac31aSKristof Provostvnet_mkepair()
4306aac31aSKristof Provost{
4406aac31aSKristof Provost	ifname=$(ifconfig epair create)
4580fc2502SGleb Smirnoff	list_interface $ifname
4680fc2502SGleb Smirnoff	list_interface ${ifname%a}b
4706aac31aSKristof Provost	echo ${ifname%a}
4806aac31aSKristof Provost}
4906aac31aSKristof Provost
50480ad405SKristof Provostvnet_init_bridge()
51480ad405SKristof Provost{
52480ad405SKristof Provost	if ! kldstat -q -m if_bridge; then
53480ad405SKristof Provost		atf_skip "This test requires if_bridge"
54480ad405SKristof Provost	fi
55480ad405SKristof Provost}
56480ad405SKristof Provost
57095aabf7SKristof Provostvnet_mkbridge()
58095aabf7SKristof Provost{
59095aabf7SKristof Provost	ifname=$(ifconfig bridge create)
6080fc2502SGleb Smirnoff	list_interface $ifname
61095aabf7SKristof Provost	echo ${ifname}
62095aabf7SKristof Provost}
63095aabf7SKristof Provost
64e62175dfSKristof Provostvnet_mkvlan()
65e62175dfSKristof Provost{
66e62175dfSKristof Provost	ifname=$(ifconfig vlan create)
6780fc2502SGleb Smirnoff	list_interface $ifname
68e62175dfSKristof Provost	echo ${ifname}
69e62175dfSKristof Provost}
70e62175dfSKristof Provost
71b4a4a3b8SAlexander V. Chernikovvnet_mkloopback()
72b4a4a3b8SAlexander V. Chernikov{
73b4a4a3b8SAlexander V. Chernikov	ifname=$(ifconfig lo create)
7480fc2502SGleb Smirnoff	list_interface $ifname
75b4a4a3b8SAlexander V. Chernikov	echo ${ifname}
76b4a4a3b8SAlexander V. Chernikov}
77b4a4a3b8SAlexander V. Chernikov
7806aac31aSKristof Provostvnet_mkjail()
7906aac31aSKristof Provost{
8006aac31aSKristof Provost	jailname=$1
8106aac31aSKristof Provost	shift
8206aac31aSKristof Provost
8306aac31aSKristof Provost	vnet_interfaces=
8406aac31aSKristof Provost	for ifname in $@
8506aac31aSKristof Provost	do
8606aac31aSKristof Provost		vnet_interfaces="${vnet_interfaces} vnet.interface=${ifname}"
8780fc2502SGleb Smirnoff		unlist_interface $ifname
8806aac31aSKristof Provost	done
8906aac31aSKristof Provost	jail -c name=${jailname} persist vnet ${vnet_interfaces}
9006aac31aSKristof Provost
9180fc2502SGleb Smirnoff	echo $jailname $@ >> created_jails.lst
9280fc2502SGleb Smirnoff}
9380fc2502SGleb Smirnoff
9480fc2502SGleb Smirnoffvnet_ifmove()
9580fc2502SGleb Smirnoff{
9680fc2502SGleb Smirnoff	ifname=$1
9780fc2502SGleb Smirnoff	jailname=$2
9880fc2502SGleb Smirnoff
9980fc2502SGleb Smirnoff	ifconfig ${ifname} vnet ${jailname}
10080fc2502SGleb Smirnoff	unlist_interface $ifname
10180fc2502SGleb Smirnoff	sed -i "" "/^${jailname}/s/\$/ ${ifname}/" created_jails.lst
10206aac31aSKristof Provost}
10306aac31aSKristof Provost
104ddcdb534SKajetan Staszkiewiczvnet_ifrename_jail()
105ddcdb534SKajetan Staszkiewicz{
106ddcdb534SKajetan Staszkiewicz	jailname=$1
107ddcdb534SKajetan Staszkiewicz	ifname=$2
108ddcdb534SKajetan Staszkiewicz	ifnewname=$3
109ddcdb534SKajetan Staszkiewicz
110c5e57222SDag-Erling Smørgrav	ifconfig -j ${jailname} $ifname name $ifnewname
111ddcdb534SKajetan Staszkiewicz	sed -i "" "/^${jailname}/s/${ifname}/${ifnewname}/" created_jails.lst
112ddcdb534SKajetan Staszkiewicz}
113ddcdb534SKajetan Staszkiewicz
11406aac31aSKristof Provostvnet_cleanup()
11506aac31aSKristof Provost{
11606aac31aSKristof Provost	if [ -f created_jails.lst ]; then
11780fc2502SGleb Smirnoff		while read jailname ifnames; do
11880fc2502SGleb Smirnoff			for ifname in ${ifnames}; do
119c5e57222SDag-Erling Smørgrav				ifconfig -j ${jailname} ${ifname} destroy
12006aac31aSKristof Provost			done
12180fc2502SGleb Smirnoff			jail -r ${jailname}
12280fc2502SGleb Smirnoff		done < created_jails.lst
12306aac31aSKristof Provost		rm created_jails.lst
12406aac31aSKristof Provost	fi
12506aac31aSKristof Provost
12606aac31aSKristof Provost	if [ -f created_interfaces.lst ]; then
127c5e57222SDag-Erling Smørgrav		while read ifname; do
12806aac31aSKristof Provost			ifconfig ${ifname} destroy
129c5e57222SDag-Erling Smørgrav		done < created_interfaces.lst
13006aac31aSKristof Provost		rm created_interfaces.lst
13106aac31aSKristof Provost	fi
13206aac31aSKristof Provost}
133