-
Notifications
You must be signed in to change notification settings - Fork 0
/
vnet.subr
316 lines (258 loc) · 7.44 KB
/
vnet.subr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
if [ ! "$_CBSD_VNET_SUBR" ]; then
_CBSD_VNET_SUBR=1
###
# Network interface=related funcion, for vnet feature
#
# default MAC_PREFIX
MAC_PREFIX="02:00:c0"
init_vnet()
{
if ! /sbin/kldstat -qm if_bridge; then
${ECHO} "${MAGENTA}Loading if_bridge.ko...${NORMAL}"
/sbin/kldload if_bridge
fi
gw_enable
}
# $1 - nicname (eg: bridge)
# if nicname=epair we search as epairXa
# show first available nic by type
# example:
# ttt=$( find_first_freenic bridge )
# return 1 when error
find_first_freenic()
{
local _i _epair _A
local _num=1 # begin from 1, due to 0 often used for system usage ( e.g: virbr0 on Xen )
[ -z $1 ] && return 1
[ "$1" = "epair" ] && _epair="a"
for _i in $( /sbin/ifconfig -l ); do
case "${_i}" in
${1}*${_epair})
/sbin/ifconfig ${1}${_num}${_epair} >/dev/null 2>&1
[ $? -eq 1 ] && echo "${1}${_num}" && return 0
_num=$(( _num + 1 ))
[ ${_num} -gt 1000 ] && return 1
;;
esac
done
echo "${1}${_num}"
}
# $1 - nicname (eg: bridge)
# show nicX if exist
# example:
# for i in $( show_all_nic_by_name bridge ); do
# echo ${i}
# done
show_all_nic_by_name()
{
local _i _mynic _A _epair
local _niclist
[ -z $1 ] && return 1
[ "${1}" = "epair" ] && _epair="a" # we check only one of pair
for _i in $( /sbin/ifconfig -l ); do
case "${_i}" in
${1}*${_epair})
_niclist="${_niclist} ${_i}"
;;
esac
done
[ -n "${_niclist}" ] && echo "${_niclist}"
return 0
}
gw_enable()
{
/sbin/sysctl -n net.inet.ip.forwarding=1 > /dev/null 2>&1
/sbin/sysctl -n net.inet6.ip6.forwarding=1 > /dev/null 2>&1
}
# cbsd store uplink interface in description area ;)
# this func extract and show this
# $1 - iface (eg: bridge0)
# $2 - (optional) for bridge: try to search not only by desc - search for $2 as addm member
# example:
# ttt=$( get_device_uplink bridge0 )
# example2:
# ttt=$( get_device_uplink bridge0 em0 )
get_device_uplink()
{
local _desc1 _desc2
local _is_bridge
local _addm
local _addm_list
local _i
[ -z "${1}" ] && return 1
[ -n "${2}" ] && _addm="${2}"
_desc1=$( /sbin/ifconfig ${1} 2>/dev/null | /usr/bin/awk '/description:/{print $2}' )
# extra check for bridge for cases where bridge created not by CBSD but member of
# right uplink
# required $2 for member
# Also, inforamtion from bridge addm pasring preferred to desc
if [ -z "${_addm}" ]; then
[ -n "${_desc1}" ] && printf "${_desc1}"
return 0
fi
_is_bridge=$( substr --pos=0 --len=6 --str=${1} )
if [ "${_is_bridge}" != "bridge" ]; then
[ -n "${_desc1}" ] && printf "${_desc1}"
return 0
fi
_addm_list=$( /sbin/ifconfig ${1} 2>/dev/null |/usr/bin/awk '/member: /{printf $2" "}' )
for _i in ${_addm_list}; do
if [ "${_addm}" = "${_i}" ]; then
printf "${_addm}"
return 0
fi
done
[ -n "${_desc1}" ] && printf "${_desc1}"
return 0
}
# function search for available bridges in system who have uplink to ${interface}
# when not - create one
# out bridge name when it exist
# $1 - type (bridge or epair) , $2 - uplink interface
# return 1 when error
# example:
# if ! ttt=$( get_my_device bridge nfe0 ); then
# echo "Error: $ttt"
# fi
get_my_device()
{
local _i _uplink _firstfree _ret _test _dev _desc
[ -z "${1}" ] && echo "set device" && return 1
[ -z "${2}" ] && echo "No uplink" && return 1
_dev=$1
_desc=$2
for _i in $( show_all_nic_by_name ${_dev} ); do
_uplink=$( get_device_uplink ${_i} ${_desc} )
[ "${_uplink}" = "${_desc}" ] && echo "${_i}" && return 0
done
# we need for new bridge with ${_dev} uplink
_firstfree=$( find_first_freenic ${_dev} )
[ -z "${_firstfree}" ] && echo "Cant find first available ${_dev}" && return 1
if [ "${_dev}" = "bridge" ]; then
_test=$( /sbin/ifconfig ${_firstfree} create addm ${_desc} up description ${_desc} 2>&1 )
_ret=$?
[ ${_ret} -ne 0 ] && echo "${_test}" && return 1
fi
echo "${_firstfree}"
}
# create epair and switch epairXa to bridge $1
# out of created epair
get_my_epair()
{
local _firstfree
[ -z "${1}" ] && echo "No bridge" && return 1
_firstfree=$( find_first_freenic epair )
[ $? -eq 1 ] && echo "No free available epair" && return 1
/sbin/ifconfig ${_firstfree} create >/dev/null 2>/dev/null
/sbin/ifconfig ${1} addm ${_firstfree}a >/dev/null 2>/dev/null
# MAC MGMT
local maca=
local macb=
if [ -r ${jailsysdir}/${jname}/local.sqlite ]; then
macb=$( cbsdsql ${jailsysdir}/${jname}/local.sqlite SELECT nic_hwaddr FROM jailnic )
else
/usr/local/bin/cbsd ${miscdir}/updatesql ${jailsysdir}/${jname}/local.sqlite ${distdir}/share/local-jailnic.schema jailnic
fi
if [ -z "${macb}" -o "${macb}" = "0" ]; then
macb=$( mac_gen 02:ff:f0 )
cbsdsql ${jailsysdir}/${jname}/local.sqlite "INSERT INTO jailnic ( name,nic_order,nic_slot,nic_parent,nic_hwaddr ) VALUES ( \"epairb\",\"0\",\"0\",\"auto\",\"${macb}\" )"
fi
# flush stdout
/sbin/ifconfig ${_firstfree}a up > /dev/null 2>&1
/sbin/ifconfig ${_firstfree}b ether ${macb} up > /dev/null 2>&1
echo ${_firstfree}
}
# create tap and attach to bridge $1 if $1 is not "disable"
get_my_tap()
{
local _firstfree
[ -z "${1}" ] && echo "No bridge" && return 1
_firstfree=$( find_first_freenic tap )
[ $? -eq 1 ] && echo "No free available tap" && return 1
/sbin/ifconfig ${_firstfree} create >/dev/null 2>/dev/null
if [ "${1}" != "disable" ]; then
/sbin/ifconfig ${1} addm ${_firstfree}
/sbin/ifconfig ${_firstfree} up
fi
echo ${_firstfree}
}
# convert mac to ip
# mac2ip 5c:f9:dd:76:d5:c4
mac2ip() {
local mac=$1
[ -z "${mac}" ] && return 0
ip_a=$( let 0x`echo $mac | /usr/bin/cut -d: -f 3` )
ip_b=$( let 0x`echo $mac | /usr/bin/cut -d: -f 4` )
ip_c=$( let 0x`echo $mac | /usr/bin/cut -d: -f 5` )
ip_d=$( let 0x`echo $mac | /usr/bin/cut -d: -f 6` )
echo "$ip_a.$ip_b.$ip_c.$ip_d"
}
# convert ip to mac
# ip2mac 221.118.213.196
ip2mac() {
local ip=$1
[ -z "${ip}" ] && return 0
local IFS="."
local macpart=4
eval $( for i in ${ip}; do
unset macval
unset len
macval=$( printf "%x" ${i} )
# append zero if len=1
len=$( strlen ${macval} )
[ "${len}" = "1" ] && macval="0${macval}"
echo "export mac_${macpart}=\"${macval}\""
macpart=$(( macpart + 1 ))
done )
echo "${MAC_PREFIX}:${mac_5}:${mac_6}:${mac_7}"
}
# return $interface as parent interface and $mytap for vale
# $nic_parent or $1 must be set
get_vm_uplink_interface()
{
local tmp_nic_parent
if [ -n "${1}" ]; then
tmp_nic_parent="${1}"
else
tmp_nic_parent="${nic_parent}"
fi
case ${tmp_nic_parent} in
vale_*)
# valeid=$( cbsdsql local SELECT idx FROM vale WHERE name=\"${nic_parent}\" )
valeid=$( cbsdsql local SELECT idx FROM vale WHERE name=\"psw1\" )
[ -z "${valeid}" ] && err 1 "errmsg=\"compile_nic_args: can't determine vale nic for: ${nic_parent}\""
mytap="vale${valeid}:${jname}"
# net_emul="e1000"
net_emul="virtio-net"
;;
*)
# when ip=0 and interface=auto we must use default interface for upstream
if [ "${tmp_nic_parent}" = "0" -o "${tmp_nic_parent}" = "auto" ]; then
. ${workdir}/initenv.subr
update_netinfo
if [ "${node_ip6_active}" = "1" ]; then
interface="${CBSD_UPLINK_IFACE6}"
else
interface="${CBSD_UPLINK_IFACE4}"
fi
else
interface="${tmp_nic_parent}"
fi
;;
esac
[ -z "${interface}" ] && return 1
return 0
}
# detach and remove NIC from vnet-based jail
# jname must be set
# $1 is id of nic in SQL table
# require: /usr/local/bin/cbsd as shell
jail_remove_nic()
{
local id="${1}"; shift
[ -z "${jname}" -o -z "${id}" ] && return 0
cbsdsql ${jailsysdir}/${jname}/local.sqlite DELETE FROM jailnic WHERE id=\"${id}\"
return 0
}
###
fi