-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvnet.subr
550 lines (469 loc) · 15.2 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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
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 ! ${KLDSTAT_CMD} -qm if_bridge; then
${ECHO} "${N1_COLOR}Loading if_bridge.ko...${N0_COLOR}"
${KLDLOAD_CMD} 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 $( ${IFCONFIG_CMD} -l ); do
case "${_i}" in
${1}*${_epair})
${IFCONFIG_CMD} ${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 $( ${IFCONFIG_CMD} -l ); do
case "${_i}" in
${1}*${_epair})
_niclist="${_niclist} ${_i}"
;;
esac
done
[ -n "${_niclist}" ] && echo "${_niclist}"
return 0
}
gw_enable()
{
${SYSCTL_CMD} -n net.inet.ip.forwarding=1 > /dev/null 2>&1
${SYSCTL_CMD} -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=$( ${IFCONFIG_CMD} ${1} 2>/dev/null | ${AWK_CMD} '/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=$( ${IFCONFIG_CMD} ${1} 2>/dev/null | ${AWK_CMD} '/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, epair or vpc) , $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
local _vpc=0 _dbpath
[ -z "${1}" ] && echo "set device" && return 1
[ -z "${2}" ] && echo "No uplink" && return 1
_dev=$1
_desc=$2
if [ "${_dev}" = "vpc" ]; then
_vpc=1
_dev="bridge"
fi
for _i in $( ${IFCONFIG_CMD} -g ${_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
# set desciption first
_test=$( ${IFCONFIG_CMD} ${_firstfree} create description ${_desc} 2>&1 )
_ret=$?
if [ ${_ret} -ne 0 ]; then
cbsdlogger NOTICE ${CBSD_APP}: get_my_device: error, unable to create: ${_ret}: ${_test}
echo "${_test}"
return 1
fi
case "${_vpc}" in
0)
cbsdlogger NOTICE ${CBSD_APP}: get_my_device: ${IFCONFIG_CMD} ${_firstfree} addm ${_desc} up
_test=$( ${IFCONFIG_CMD} ${_firstfree} addm ${_desc} up 2>&1 )
_ret=$?
if [ ${_ret} -ne 0 ]; then
cbsdlogger NOTICE ${CBSD_APP}: get_my_device: error, ret: ${_ret}: ${_test}
echo "${_test}"
return 1
fi
;;
1)
_dbpath="${VPC_ROOT_DIR}/${vpc_name}.sqlite"
[ ! -r ${_dbpath} ] && err 1 "${N1_COLOR}VPC not exist: ${N2_COLOR}${_dbpath}${N0_COLOR}"
_vxlan_list=$( cbsdsqlro ${_dbpath} "SELECT vxlan_id FROM vpc_peers WHERE src_node=\"${nodename}\"" | ${XARGS_CMD} )
if [ -z "${_vxlan_list}" ]; then
echo "no vxlan_id in VPC database"
return 1
fi
for i in ${_vxlan_list}; do
${IFCONFIG_CMD} vxlan${i} > /dev/null 2>&1
_ret=$?
if [ ${_ret} -ne 0 ]; then
echo "vxlan interface not initialized: vxlan${i}"
return 1
fi
_test=$( ${IFCONFIG_CMD} ${_firstfree} addm vxlan${i} private vxlan${i} up 2>&1 )
_ret=$?
if [ ${_ret} -ne 0 ]; then
echo "ifconfig addm error: ${IFCONFIG_CMD} ${_firstfree} addm vxlan${i}"
return 1
fi
done
;;
esac
fi
echo "${_firstfree}"
}
# create epair and switch epairXa to bridge $1
# out of created epair
get_my_epair()
{
local _firstfree
local _parent_mtu
[ -z "${1}" ] && echo "No bridge" && return 1
_firstfree=$( find_first_freenic epair )
[ $? -eq 1 ] && echo "No free available epair" && return 1
${IFCONFIG_CMD} ${_firstfree} create >/dev/null 2>/dev/null
_parent_mtu=$( ${toolsdir}/nic_info --nic=${1} --mtu --quiet 2>/dev/null )
${IFCONFIG_CMD} ${_firstfree}a mtu ${_parent_mtu}
${IFCONFIG_CMD} ${_firstfree}b mtu ${_parent_mtu}
${IFCONFIG_CMD} ${1} addm ${_firstfree}a >/dev/null 2>/dev/null
# MAC MGMT
local maca=
local macb=
if [ -r ${jailsysdir}/${jname}/local.sqlite ]; then
macb=$( cbsdsqlro ${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 )
cbsdsqlrw ${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
${IFCONFIG_CMD} ${_firstfree}a up > /dev/null 2>&1
${IFCONFIG_CMD} ${_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 _parent_mtu=0 _ret
local _create=1
[ -z "${1}" ] && echo "No bridge" && return 1
_firstfree=$( find_first_freenic tap )
if [ $? -eq 1 ]; then
cbsdlogger WARNING ${CBSD_APP}: get_my_tap: no free available tap
echo "No free available tap"
return 1
fi
cbsdlogger NOTICE ${CBSD_APP}: get_my_tap: found new free device: ${_firstfree}. Will be created
${IFCONFIG_CMD} ${_firstfree} create >/dev/null 2>/dev/null
_ret=$?
if [ ${_ret} -ne 0 ]; then
cbsdlogger NOTICE ${CBSD_APP}: get_my_tap: ${IFCONFIG_CMD} ${_firstfree} create: failed
fi
# Inherits parent MTU to stop "ifconfig: BRDGADD XXX: Invalid argument" when
# parent MTU less then tap
_parent_mtu=$( ${toolsdir}/nic_info --nic=${mybridge} --mtu --quiet 2>/dev/null )
_ret=$?
if [ ${_ret} -eq 0 -a -n "${_parent_mtu}" ]; then
cbsdlogger NOTICE ${CBSD_APP}: get_my_tap: adjust parent_mtu for ${_firstfree}: ${_parent_mtu}
${IFCONFIG_CMD} ${_firstfree} mtu ${_parent_mtu}
fi
if [ "${1}" != "disable" ]; then
${IFCONFIG_CMD} ${1} addm ${_firstfree}
${IFCONFIG_CMD} ${_firstfree} up
fi
echo ${_firstfree}
return 0
}
# 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 | ${CUT_CMD} -d: -f 3` )
ip_b=$( let 0x`echo $mac | ${CUT_CMD} -d: -f 4` )
ip_c=$( let 0x`echo $mac | ${CUT_CMD} -d: -f 5` )
ip_d=$( let 0x`echo $mac | ${CUT_CMD} -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 -p <parent> must be set
# -u (update VALE ports table), for vale only
get_vm_uplink_interface()
{
local tmp_nic_parent _jname _update=0
local _get_by_route=0 # get upink by IPv4 route ?
local _sw _pos _arg_len _pref _vale_arg _vale_id _vale_port
local _vale_port_exclude
tmp_nic_parent="${nic_parent}"
while getopts "p:u" opt; do
case "${opt}" in
p) tmp_nic_parent="${OPTARG}" ;;
u) _update=1 ;;
esac
shift $(($OPTIND - 1))
done
case "${tmp_nic_parent}" in
cbsdvale*)
strpos --str="${tmp_nic_parent}" --search="_"
_pos=$?
if [ ${_pos} -eq 0 ]; then
# not vale_XXX form
else
_arg_len=$( strlen ${tmp_nic_parent} )
_pref=$(( _arg_len - _pos ))
_vale_arg=$( substr --pos=0 --len=${_pos} --str="${tmp_nic_parent}" )
_sw=$( substr --pos=$(( ${_pos} +2 )) --len=${_pref} --str="${tmp_nic_parent}" )
fi
_vale_id=$( cbsdsqlro local SELECT idx FROM vale WHERE name=\"${_sw}\" )
[ -z "${_vale_id}" ] && log_err 1 "errmsg=\"compile_nic_args: can't determine vale nic for: ${nic_parent}, please run: cbsd valecfg\""
# VALE has limits on the length of the port name.
# Also, we should avoid ID/GID conflicts with multiple working directories.
# Determine next free VALE port, without any reference to id or name.
cbsdlogger NOTICE "${CBSD_APP}: looking for next free vale port: ${miscdir}/next-vale-port -i ${_vale_id}"
_vale_port_exclude=$( cbsdsqlro local "SELECT port_id FROM vale_ports WHERE vale_id=\"${_vale_id}\"" | ${XARGS_CMD} )
_vale_port=$( ${miscdir}/next-vale-port -i ${_vale_id} -e "${_vale_port_exclude}" 2>/dev/null )
if [ $? -ne 0 -o -z "${_vale_port}" ]; then
log_err 1 "errmsg=\"compile_nic_args: can't next vale port: ${miscdir}/next-vale-port -i ${_vale_id}\""
fi
if [ ${_update} -eq 1 ]; then
cbsdlogger NOTICE "${CBSD_APP}: get_vm_uplink_interface: update/reservation next-vale-port for ${jname}, valeid: ${_vale_id}: port: ${_vale_port}"
cbsdsqlrw local "INSERT INTO vale_ports ( jname,vale_id,port_id ) VALUES ( \"${jname}\", ${_vale_id}, ${_vale_port} )"
fi
mytap="vale${_vale_id}:${_vale_port}"
net_emul="virtio-net"
cbsdlogger NOTICE "${CBSD_APP}: get_vm_uplink_interface: vale id: ${_vale_id} -> ${mytap}"
;;
vale*)
# pass vale as-is
# VALE has limits on the length of the port name.
# Also, we should avoid ID/GID conflicts with multiple working directories.
# Determine next free VALE port, without any reference to id or name.
_vale_id=$( echo ${tmp_nic_parent} | ${TR_CMD} -d [:alpha:] ) # remove alpha, get ID only
[ -z "${_vale_id}" ] && log_err 1 "errmsg=\"compile_nic_args: can't determine vale nic for: ${tmp_nic_parent}, please run: cbsd valecfg\""
_vale_port=$( ${miscdir}/next-vale-port -i ${_vale_id} 2>/dev/null )
if [ $? -ne 0 -o -z "${_vale_port}" ]; then
log_err 1 "errmsg=\"compile_nic_args: can't next vale port: ${miscdir}/next-vale-port -i ${_vale_id}\""
fi
mytap="${tmp_nic_parent}:${_vale_port}"
net_emul="virtio-net"
cbsdlogger NOTICE "${CBSD_APP}: get_vm_uplink_interface: pass vale interface as-is -> ${mytap}"
return 0
;;
*)
if [ "${tmp_nic_parent}" = "0" -o "${tmp_nic_parent}" = "auto" ]; then
case ${ip4_addr} in
[Dd][Hh][Cc][Pp])
_get_by_route=0
;;
*)
iptype ${ip4_addr}
case $? in
1)
cbsdlogger NOTICE ${CBSD_APP}: get_vm_uplink_interface for ${jname}: has valid IP address: ${ip4_addr}, parent auto: get_by_route=1
_get_by_route=1
;;
2)
# IPv6 here: WIP
_get_by_route=0
;;
*)
_get_by_route=0
;;
esac
;;
esac
else
# no auto
_get_by_route=0
fi
case ${_get_by_route} in
0)
# when ip=0 and interface=auto we must use default interface for upstream
if [ "${tmp_nic_parent}" = "0" -o "${tmp_nic_parent}" = "auto" ]; then
cbsdlogger NOTICE "${CBSD_APP}: get_vm_uplink_interface: get_by_route: ${_get_by_route}, tmp_nic_parent: ${tmp_nic_parent}, use default iface"
. ${distdir}/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
cbsdlogger NOTICE ${CBSD_APP}: get_vm_uplink_interface for ${jname}: ip4_addr: ${ip4_addr}, determine uplink method: by uplink, interface: ${interface}
;;
1)
interface=$( ${ROUTE_CMD} -n -4 get ${IWM} 2>/dev/null | ${AWK_CMD} '/interface/{print $2}' )
cbsdlogger NOTICE ${CBSD_APP}: get_vm_uplink_interface for ${jname}: ip4_addr: ${IWM}, determine uplink method: by route, interface: ${interface}
case "${interface}" in
lo*)
cbsdlogger NOTICE ${CBSD_APP}: get_vm_uplink_interface for ${jname}: ip4_addr: ${IWM}, iface: ${interface} is loopback. force to 0.0.0.0 route
interface=$( ${ROUTE_CMD} -n -4 get 0.0.0.0 2>/dev/null | ${AWK_CMD} '/interface/{print $2}' )
cbsdlogger NOTICE ${CBSD_APP}: get_vm_uplink_interface for ${jname}: ip4_addr: 0.0.0.0, determine uplink method: by route, interface: ${interface}
;;
*)
cbsdlogger NOTICE ${CBSD_APP}: get_vm_uplink_interface for ${jname}: interface by route detected: ${interface}
;;
esac
;;
esac
;;
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
cbsdsqlrw ${jailsysdir}/${jname}/local.sqlite DELETE FROM jailnic WHERE id=\"${id}\"
return 0
}
# return 0 if -n $nic exist on the host
# e.g:
# if is_nic_exist -n lo0; then
# echo "EXIST"
#fi
is_nic_exist()
{
local _nic _nic_list _i
while getopts "n:" opt; do
case "${opt}" in
n) _nic="${OPTARG}" ;;
esac
shift $(($OPTIND - 1))
done
[ -z "${_nic}" ] && return 1
_nic_list=$( ${miscdir}/nics-list | ${XARGS_CMD} )
local IFS=" "
for _i in ${_nic_list}; do
[ "${_nic}" = "${_i}" ] && return 0
done
return 1
}
# function search for nic in system by iface description
# return 0 and print to stdout nic if found
# return 1 when not
# example:
# if ttt=$( get_nic_by_descr freebsd1-nic0 ); then
# echo "Found: $ttt"
# fi
get_nic_by_descr()
{
local _iface _mytap _i _list=
_iface=$( ${IFCONFIG_CMD} -l )
_mytap=
for _i in ${_iface}; do
_desc=$( ${IFCONFIG_CMD} ${_i} 2>/dev/null | ${AWK_CMD} '/description:/{print $2}' )
[ -z "${_desc}" ] && continue
if [ "${_desc}" = "${1}" ]; then
if [ -n "${_list}" ]; then
_list="${_list} ${_i}"
else
_list="${_i}"
fi
fi
done
if [ -n "${_list}" ]; then
printf "${_list}"
return 0
else
return 1
fi
}
###
fi