#!/bin/sh
# SPDX-License-Identifier: BSD-2-Clause
# SPDX-FileCopyrightText: 2021 Benoit Chesneau (bchesneau@gmail.com)

# show the configuration details for a netgraph switch
#
# @param string _name the switch name
# @param string _format output format
#
switch::netgraph::show(){
    local _name="$1"
    local _format="$2"
    local _id

    switch::netgraph::id "_id" "${_name}"
    printf "${_format}" "${_name}" "netraph" "${_id}" "n/a" "n/a" "n/a" "n/a" "n/a"
}

# create a netgraph switch
#
# @param string _switch the name of the switch
#
switch::netgraph::create(){
    config::core::set "switch_list" "${_switch}" "1"
    config::core::set "type_${_switch}" "netgraph"
}

# remove a netgraph switch
#
switch::netgraph::remove(){ }

# add a new interface to this switch
# at the moment we require the user to manually
# set up any netgraph switches
#
# @param string _switch name of the switch
# @param string _if the interface to add
#
switch::netgraph::add_member(){
    util::err "physical interfaces must be added to the netgraph switch manually"
}

# remove an interface
#
# @param string _switch name of the switch
# @param string _if the interface to remove
#
switch::netgraph::remove_member(){
    util::err "physical interfaces must be removed from the netgraph switch manually"
}

# set vlan id
#
# @param string _switch name of switch
# @param int _vlan vlan id to set
#
switch::netgraph::vlan(){
    util::err "vlan support is not currently implemented for netgraph switches"
}
# gets a unique linkname name for a ng_bridge interface
# we need to make sure the link is unique and the last one
#
# @param string _var name of variable to put port name into
# @param string _switch the name of the switch
#
switch::netgraph::id(){
    local _var="$1"
    local _switch="$2"

    # Create a new interface to the bridge
    num=2
    while ngctl msg "${_switch}:" getstats $num > /dev/null 2>&1
    do
        num=$(( $num + 1 ))
    done
    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 ${_bus}:${_slot}:${_func},${_emulation},${_ngid}"
    [ -n "${_mac}" ] && _devices="${_devices},mac=${_mac}"

     _func=$((_func + 1))
}
