xref: /freebsd/tests/sys/common/vnet.subr (revision bd8296e7173aa625f6d4d766aac2df6be639e49a)
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	# When transmit checksum offloading is enabled, if_epair does not
46	# compute checksums, it just marks packets that this computation still
47	# needs to be done. However, some test cases verify the checksum.
48	# Therefore disable this for IPv4 and IPv6.
49	ifconfig ${ifname} -txcsum -txcsum6
50	list_interface $ifname
51	list_interface ${ifname%a}b
52	echo ${ifname%a}
53}
54
55vnet_init_bridge()
56{
57	if ! kldstat -q -m if_bridge; then
58		atf_skip "This test requires if_bridge"
59	fi
60}
61
62vnet_mkbridge()
63{
64	ifname=$(ifconfig bridge create)
65	list_interface $ifname
66	echo ${ifname}
67}
68
69vnet_mkvlan()
70{
71	ifname=$(ifconfig vlan create)
72	list_interface $ifname
73	echo ${ifname}
74}
75
76vnet_mkloopback()
77{
78	ifname=$(ifconfig lo create)
79	list_interface $ifname
80	echo ${ifname}
81}
82
83vnet_mkjail()
84{
85	jailname=$1
86	shift
87
88	vnet_interfaces=
89	for ifname in $@
90	do
91		vnet_interfaces="${vnet_interfaces} vnet.interface=${ifname}"
92		unlist_interface $ifname
93	done
94	jail -c name=${jailname} persist vnet ${vnet_interfaces}
95
96	echo $jailname $@ >> created_jails.lst
97}
98
99vnet_ifmove()
100{
101	ifname=$1
102	jailname=$2
103
104	ifconfig ${ifname} vnet ${jailname}
105	unlist_interface $ifname
106	sed -i "" "/^${jailname}/s/\$/ ${ifname}/" created_jails.lst
107}
108
109vnet_ifrename_jail()
110{
111	jailname=$1
112	ifname=$2
113	ifnewname=$3
114
115	ifconfig -j ${jailname} $ifname name $ifnewname
116	sed -i "" "/^${jailname}/s/${ifname}/${ifnewname}/" created_jails.lst
117}
118
119vnet_cleanup()
120{
121	if [ -f created_jails.lst ]; then
122		while read jailname ifnames; do
123			for ifname in ${ifnames}; do
124				ifconfig -j ${jailname} ${ifname} destroy
125			done
126			jail -r ${jailname}
127		done < created_jails.lst
128		rm created_jails.lst
129	fi
130
131	if [ -f created_interfaces.lst ]; then
132		while read ifname; do
133			ifconfig ${ifname} destroy
134		done < created_interfaces.lst
135		rm created_interfaces.lst
136	fi
137}
138