Move interface init code into emulation specific switch modules

This commit is contained in:
Matt Churchyard
2022-07-22 11:50:07 +01:00
parent e323640b88
commit 07013f7d25
6 changed files with 145 additions and 100 deletions

View File

@@ -24,8 +24,8 @@
# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
VERSION=1.5
VERSION_INT=105102
VERSION=1.6-devel
VERSION_INT=106001
VERSION_BSD=$(uname -K)
PATH=${PATH}:/bin:/usr/bin:/usr/local/bin:/sbin:/usr/sbin:/usr/local/sbin

View File

@@ -551,23 +551,14 @@ vm::bhyve_device_disks(){
# we add each tap to __taplist from vm::run which it will
# use to desstroy them all on shutdown
#
# @modifies _devices _slot _taplist
# @modifies _devices _slot _taplist _func
#
vm::bhyve_device_networking(){
local _type _switch _mac _custom_tap _tap _sid _mtu
local _num=0 _member_type _sw_type _iname
local _emulation _num=0
while true; do
config::get "_type" "network${_num}_type"
[ -z "${_type}" ] && break
config::get "_switch" "network${_num}_switch"
config::get "_mac" "network${_num}_mac"
config::get "_custom_tap" "network${_num}_device"
config::get "_iname" "network${_num}_name"
# set a static mac if we don't have one
[ -z "${_mac}" ] && vm::generate_static_mac
config::get "_emulation" "network${_num}_type"
[ -z "${_emulation}" ] && break
# move slot if we've hit function 8
if [ ${_func} -ge 8 ]; then
@@ -575,77 +566,7 @@ vm::bhyve_device_networking(){
_slot=$((_slot + 1))
fi
# get type of switch
switch::type "_sw_type" "${_switch}"
if [ "${_sw_type}" = "vale" ]; then
# create a vale port id
switch::vale::id "_tap" "${_switch}" "${_mac}"
util::log "guest" "${_name}" "adding vale interface ${_tap} (${_switch})"
_devices="${_devices} -s ${_slot}:${_func},${_type},${_tap}"
[ -n "${_mac}" ] && _devices="${_devices},mac=${_mac}"
_func=$((_func + 1))
elif [ "${_sw_type}" = "netgraph" ]; then
# create a netgraph peer
switch::netgraph::id "_path" "${_switch}"
util::log "guest" "${_name}" "adding netgraph interface ${_path} (${_switch})"
_devices="${_devices} -s ${_slot}:${_func},${_type},${_path}"
[ -n "${_mac}" ] && _devices="${_devices},mac=${_mac}"
_func=$((_func + 1))
else
# create interface
if [ -n "${_custom_tap}" ]; then
_tap="${_custom_tap}"
elif [ -n "${_iname}" ]; then
_tap=$(ifconfig tap create name "${_iname}")
else
_tap=$(ifconfig tap create)
fi
# should this be a span member?
_member_type="addm"
config::yesno "network${_num}_span" && _member_type="span"
if [ -n "${_tap}" ]; then
util::log "guest" "${_name}" "initialising network device ${_tap}"
ifconfig "${_tap}" descr "vmnet/${_name}/${_num}/${_switch:-custom}" group vm-port >/dev/null 2>&1
if [ -n "${_switch}" ]; then
switch::id "_sid" "${_switch}"
if [ -n "${_sid}" ]; then
_mtu=$(ifconfig "${_sid}" | head -n1 | awk '{print $NF}')
if [ "${_mtu}" != "1500" ]; then
util::log "guest" "${_name}" "setting mtu of ${_tap} to ${_mtu}"
ifconfig "${_tap}" mtu "${_mtu}" >/dev/null 2>&1
fi
util::log "guest" "${_name}" "adding ${_tap} -> ${_sid} (${_switch} ${_member_type})"
ifconfig "${_sid}" "${_member_type}" "${_tap}" >/dev/null 2>&1 || util::log "guest" "${_name}" "failed to add ${_tap} to ${_sid}"
util::log "guest" "${_name}" "bring up ${_tap} -> ${_sid} (${_switch} ${_member_type})"
ifconfig "${_tap}" up >/dev/null 2>&1 || util::log "guest" "${_name}" "failed to bring up ${_tap} in ${_sid}"
# set private if configured
switch::is_private "${_switch}" && ifconfig "${_sid}" "private" "${_tap}" >/dev/null 2>&1
else
util::log "guest" "${_name}" "failed to find virtual switch '${_switch}'"
fi
fi
_devices="${_devices} -s ${_slot}:${_func},${_type},${_tap}"
[ -n "${_mac}" ] && _devices="${_devices},mac=${_mac}"
_func=$((_func + 1))
[ -z "${_custom_tap}" ] && _taplist="${_taplist}${_taplist:+ }${_tap}"
fi
fi
switch::provision
_num=$((_num + 1))
done

View File

@@ -48,11 +48,11 @@ switch::init(){
config::core::get "_type" "type_${_switch}"
case "${_type}" in
vxlan) switch::vxlan::init "${_switch}" ;;
manual) switch::manual::init "${_switch}" ;;
vale) ;;
netgraph) ;;
*) switch::standard::init "${_switch}" ;;
vxlan) switch::vxlan::init "${_switch}" ;;
manual) switch::manual::init "${_switch}" ;;
vale) ;;
netgraph) ;;
*) switch::standard::init "${_switch}" ;;
esac
done
fi
@@ -75,11 +75,11 @@ switch::list(){
config::core::get "_type" "type_${_switch}"
case "${_type}" in
netgraph) switch::netgraph::show "${_switch}" "${_format}" ;;
vale) switch::vale::show "${_switch}" "${_format}" ;;
vxlan) switch::vxlan::show "${_switch}" "${_format}" ;;
manual) switch::manual::show "${_switch}" "${_format}" ;;
*) switch::standard::show "${_switch}" "${_format}" ;;
netgraph) switch::netgraph::show "${_switch}" "${_format}" ;;
vale) switch::vale::show "${_switch}" "${_format}" ;;
vxlan) switch::vxlan::show "${_switch}" "${_format}" ;;
manual) switch::manual::show "${_switch}" "${_format}" ;;
*) switch::standard::show "${_switch}" "${_format}" ;;
esac
done
fi
@@ -391,10 +391,10 @@ switch::id(){
config::core::get "_type" "type_${_switch}"
case "${_type}" in
vale) switch::vale::id "${_var}" "${_switch}" ;;
netgraph) switch::netgraph::id "${_var}" "${_switch}" ;;
manual) switch::manual::id "${_var}" "${_switch}" ;;
*) switch::standard::id "${_var}" "${_switch}" ;;
vale) switch::vale::id "${_var}" "${_switch}" ;;
netgraph) switch::netgraph::id "${_var}" "${_switch}" ;;
manual) switch::manual::id "${_var}" "${_switch}" ;;
*) switch::standard::id "${_var}" "${_switch}" ;;
esac
}
@@ -439,3 +439,29 @@ switch::set_viid(){
switch::__viid "_viid" "${_switch}"
ifconfig "${_iface}" group "${_viid}" >/dev/null 2>&1
}
# create a network interface for a guest
# relies heavily on variables set in the main vm::run function
#
# @modifies _func _devices
#
switch::provision(){
local _switch _mac _type
config::get "_switch" "network${_num}_switch"
config::get "_mac" "network${_num}_mac"
# set a static mac if we don't have one
[ -z "${_mac}" ] && vm::generate_static_mac
switch::type "_type" "${_switch}"
case "${_type}" in
vale) switch::vale::provision ;;
netgraph) switch::netgraph::provision ;;
standard) ;&
manual) ;&
vxlan) switch::standard::provision ;;
*) util::warn "unable to configure interface ${_num}" ;;
esac
}

View File

@@ -99,3 +99,21 @@ switch::netgraph::id(){
setvar "${_var}" "netgraph,path=${_switch}:,peerhook=link$num"
}
# create a netgraph interface for a guest
# relies heavily on variables set in the main vm::run function
#
# @modifies _func _devices
# @return 1 if we don't get a tap device
#
switch::netgraph::provision(){
local _ngid
# create a netgraph peer
switch::netgraph::id "_ngid" "${_switch}"
util::log "guest" "${_name}" "adding netgraph interface ${_ngid} (${_switch})"
_devices="${_devices} -s ${_slot}:${_func},${_emulation},${_ngid}"
[ -n "${_mac}" ] && _devices="${_devices},mac=${_mac}"
_func=$((_func + 1))
}

View File

@@ -342,3 +342,64 @@ switch::standard::__unconfigure_port(){
switch::standard::id(){
switch::find "$1" "$2"
}
# creates a standard tap interface for a guest
# relies heavily on variables set in the main vm::run function
#
# @modifies _func _devices
# @return 1 if we don't get a tap device
#
switch::standard::provision(){
local _tap _custom_tap _sid _mtu _member_type _iname
config::get "_custom_tap" "network${_num}_device"
config::get "_iname" "network${_num}_name"
# create interface
if [ -n "${_custom_tap}" ]; then
_tap="${_custom_tap}"
elif [ -n "${_iname}" ]; then
_tap=$(ifconfig tap create name "${_iname}")
else
_tap=$(ifconfig tap create)
fi
[ -z "${_tap}" ] && return 1;
util::log "guest" "${_name}" "initialising network device ${_tap}"
ifconfig "${_tap}" descr "vmnet/${_name}/${_num}/${_switch:-custom}" group vm-port >/dev/null 2>&1
if [ -n "${_switch}" ]; then
switch::id "_sid" "${_switch}"
# should this be a span member?
_member_type="addm"
config::yesno "network${_num}_span" && _member_type="span"
if [ -n "${_sid}" ]; then
_mtu=$(ifconfig "${_sid}" | head -n1 | awk '{print $NF}')
if [ "${_mtu}" != "1500" ]; then
util::log "guest" "${_name}" "setting mtu of ${_tap} to ${_mtu}"
ifconfig "${_tap}" mtu "${_mtu}" >/dev/null 2>&1
fi
util::log "guest" "${_name}" "adding ${_tap} -> ${_sid} (${_switch} ${_member_type})"
ifconfig "${_sid}" "${_member_type}" "${_tap}" >/dev/null 2>&1 || util::log "guest" "${_name}" "failed to add ${_tap} to ${_sid}"
util::log "guest" "${_name}" "bring up ${_tap} -> ${_sid} (${_switch} ${_member_type})"
ifconfig "${_tap}" up >/dev/null 2>&1 || util::log "guest" "${_name}" "failed to bring up ${_tap} in ${_sid}"
# set private if configured
switch::is_private "${_switch}" && ifconfig "${_sid}" "private" "${_tap}" >/dev/null 2>&1
else
util::log "guest" "${_name}" "failed to find virtual switch '${_switch}'"
fi
fi
_devices="${_devices} -s ${_slot}:${_func},${_emulation},${_tap}"
[ -n "${_mac}" ] && _devices="${_devices},mac=${_mac}"
_func=$((_func + 1))
[ -z "${_custom_tap}" ] && _taplist="${_taplist}${_taplist:+ }${_tap}"
}

View File

@@ -107,3 +107,22 @@ switch::vale::id(){
setvar "${_var}" "vale${_id_s}"
fi
}
# create a vale interface for a guest
# relies heavily on variables set in the main vm::run function
#
# @modifies _func _devices
# @return 1 if we don't get a tap device
#
switch::vale::provision(){
local _vale_id
# create a vale port id
switch::vale::id "_vale_id" "${_switch}" "${_mac}"
util::log "guest" "${_name}" "adding vale interface ${_tap} (${_switch})"
_devices="${_devices} -s ${_slot}:${_func},${_emulation},${_vale_id}"
[ -n "${_mac}" ] && _devices="${_devices},mac=${_mac}"
_func=$((_func + 1))
}