#!/bin/bash
# This script defines the network bridge to be used by containers.
# Copyright (c) 2014-2017, 2020 by Cisco Systems, Inc.
# All rights reserved.

BOOTSTRAP_FILE="/etc/init.d/calvados_bootstrap.cfg"
source $BOOTSTRAP_FILE
source /etc/init.d/spirit-functions

if [[ -f /etc/init.d/app-hosting-functions ]]; then
    source /etc/init.d/app-hosting-functions
fi

# Thie script would be executed only if VIRT_METHOD=lxc on host.
if [[ $VIRT_METHOD != "lxc" ]]; then
    exit 0
fi

vmtype=`cat /proc/cmdline | sed 's/^.*vmtype=//' | cut -d" " -f1`
if [[ "$vmtype" != "hostos" ]]; then
    exit 0
fi

RETVAL=0
prog="nwbridge"
VAR='/var/log/messages'

function define_nwb(){
    #Perform multiple software bridge creation 
    virsh -c lxc:/// net-define /etc/init/ieobc_br_network.xml
    virsh -c lxc:/// net-define /etc/init/local_br_network.xml
    virsh -c lxc:/// net-define /etc/init/xr_local_br_network.xml 2>> $VAR
    virsh -c lxc:/// net-start ieobc_br
    virsh -c lxc:/// net-start local_br
    virsh -c lxc:/// net-start xr_local_br

    #create default ns for 3rd party connectivity.
    ip netns add global-vrf &>/dev/null
    if [[ ! -f /var/run/netns/global-vrf ]]; then
        echo Failed to create globlal-vrf namespace
    fi

    #
    # Maintain backwards compatibility for now for the removed tpnns
    #
    ln -sf /var/run/netns/global-vrf /var/run/netns/tpnns &>/dev/null

    declare -F app_host_is_enabled &>/dev/null && app_host_is_enabled
    if [[ $? -eq 0 ]]; then
        # Create namespace specific resolv.conf,hosts for global-vrf
        create_global_vrf_dirs
    fi
    
    # virsh -c lxc:/// list --all
}
readonly -f define_nwb


start()
{
        # echo "Starting $prog"

        if [[ "$PLATFORM_HOST_BRIDGE" -eq 1 ]]; then
            #Define and start the network we will use for now
            define_nwb

            ifconfig local_br 10.0.2.16 up netmask 255.255.255.0
            #Setting MTUs for all interfaces under ieobc_br -- Done for GSP
            ifconfig ieobc_br mtu 9000 up
            ifconfig local_br mtu 9000 up
            echo 0 > /sys/devices/virtual/net/ieobc_br/bridge/multicast_snooping
            if [[ "$PLATFORM" == "ncs1001" ]]; then
                ifconfig eth2 mtu 9000 up
                ifconfig eth1 mtu 9000 up
            elif [[ "$PLATFORM" == "ncs1004" ]]; then
                ifconfig eth-mgmt0 mtu 9000
                ifconfig eth-mgmt1 mtu 9000
            elif [[ "$PLATFORM" == "xrv9k" ]]; then
                ifconfig mgmt-eth0 mtu 9000 up
                ifconfig ctrl-eth0 mtu 9000 up
                ifconfig host-eth0 mtu 9000 up
                # disable all filters on bridge
                for f in /proc/sys/net/bridge/bridge-nf-*; do echo 0 > $f; done

            elif [[ "$PLATFORM" != "fretta" && "$PLATFORM" != "zermatt" && "$PLATFORM" != "asr9k" && "$PLATFORM" != "ncs560" ]]; then
                ifconfig eth1 mtu 9000 up
                ifconfig eth0 mtu 9000 up
            fi


            #
            # Make sure these routes stay within local bridge
            #
            route add 10.11.12.15 local_br
            route add 10.11.12.14 local_br
            route add 10.11.12.13 local_br
            route add 10.11.12.115 local_br
        fi

        # Add eth2 to bridge ieobc_br for access to host from guests
        # Default MTU value of eth2 is 1500 but as per CDETS CSCuq56483 
        # we need to make it 9000 Since this interface is not needed 
        # for nested LXC hence Commenting below code
        # brctl addif ieobc_br eth2
        if [[ "$SIMULATION" -eq 1 && "$PLATFORM" == "asr9k" ]]; then
            brctl addif ieobc_br eth1
            echo "BRIDGE ETH1 configured"
        fi
        if [[ "$SIMULATION" -eq 1 && "$PLATFORM" == "ncs1k" ]]; then
            brctl addif ieobc_br eth1
            echo "eth1 added to ieobc_br"
        fi
        if [[ "$SIMULATION" -eq 1 && "$PLATFORM" == "ncs1004" ]]; then
            brctl addif ieobc_br eth1
            echo "eth1 added to ieobc_br"
        fi
        if [[ "$SIMULATION" -eq 1 && "$PLATFORM" == "ncs1001" ]]; then
            brctl addif ieobc_br eth1
            echo "eth1 added to ieobc_br"
            brctl addif ieobc_br eth2
            echo "eth2 added to ieobc_br"
        fi

        if [[ "$PLATFORM" != "iosxrwbd" ]]; then
            if [[ "$PLATFORM_BRIDGE_EXT_CONNECTIVITY" -eq 1 ]]; then
                # eth1 on the host is reserved for connectivity to other 
                # nodes. Hence adding this eth1 interface to the ieobc_br
                brctl addif ieobc_br eth1
                echo "eth1 added to ieobc_br"
            fi
        fi
}

stop()
{
        echo "Stoping $prog"
}

reload()
{
        stop;
        start;
}

case "$1" in
        start)
                start
                ;;
        stop)
                stop
                ;;
        restart)
                stop
                start
                ;;
        reload)
                reload
                ;;

         *)
                echo $"Usage: $0 {start|stop|restart|reload|condrestart|status}"
                RETVAL=1
esac
exit $RETVAL

