#!/bin/sh
#
# chkconfig: 2345 11 89
# description:	Brings up and configures the ethernet bridge
# processname: bridge

# Source function library.
. /etc/init.d/functions

# Check that networking is up.
if [ "$NETWORKING" = "no" ]
then
	exit 0
fi

RETVAL=0

[ -f /etc/rsbridgeinit.conf ] && . /etc/rsbridgeinit.conf

###### Sample of what /etc/rsbridgeinit.conf should look like
#   bridgeprefix="gbr"
#   #UPDATE_STRING=-b eth2 eth3
#   BRIDGES="0"
#   CSIF[0]="eth2"
#   SSIF[0]="eth3"
##########################

#Enable RSTP if we have /sbin/rstpd
RSTPD=/sbin/rstpd
RSTPCTL=/sbin/rstpctl
RSTP=0
[ -x $RSTPD -a -x $RSTPCTL ] && RSTP=1

slaves () {
 cat /proc/net/bonding/$1 | grep 'Slave Interface' | cut -d: -f2
}

# set interrupt affinity to first cpu
setsmpaffinity() {
    if [[ $1 == bond* ]] ; then
        for sl in `slaves $1`; do
            irq=`grep $sl /proc/interrupts | cut -d: -f1`
            echo 1 > /proc/irq/$irq/smp_affinity
        done
    else
        irq=`grep $1 /proc/interrupts | cut -d: -f1`
        echo 1 > /proc/irq/$irq/smp_affinity
    fi
}

start () {
        [ $RSTP == 1 ] && echo Starting rstpd && daemon $RSTPD ">>" /var/log/rstpd.log "2>&1"
        for b in $BRIDGES ; do
            echo Starting service bridge $bridgeprefix$b
            brctl addbr $bridgeprefix$b  ||  RETVAL=1
            if [ $RSTP == 0 ] ; then
              brctl stp $bridgeprefix$b on 
              brctl setbridgeprio $bridgeprefix$b 65000
            fi

            for br in ${CSIF[$b]} ; do
                echo Adding CSIF $br on $bridgeprefix$b
                ifup $br
                brctl addif $bridgeprefix$b $br  ||  RETVAL=1
            done

            if [ "$1" != "client" ]; then
                for br in ${SSIF[$b]} ; do
                    echo Adding SSIF $br on $bridgeprefix$b
                    ifup $br
                    if [[ $br == bond* ]] ; then
                      for sl in `slaves $br`; do
                        ifconfig $sl down
                      done
                    else
                      ifconfig $br down
                    fi
                    brctl addif $bridgeprefix$b $br  ||  RETVAL=1
                done
            fi
            ifup $bridgeprefix$b
            if [ $RSTP == 1 ]; then 
              rstpctl rstp $bridgeprefix$b on
              rstpctl setbridgeprio $bridgeprefix$b 61440
            fi
        done

        for b in $BRIDGES ; do

            . /etc/sysconfig/network-scripts/ifcfg-$bridgeprefix$b
# We will always have the subnet route entry. If there is a default gateway
# on that subnet, we will have an entry for that as well
            if [ -n "$GATEWAY" ] ; then rttarget=2 ; else rttarget=1 ; fi
            rtcount=x
            
            count=1
            while true ; do
                new_rtcount=`grep -c $bridgeprefix$b /proc/net/route`;
                if [ $new_rtcount != $rtcount ]; then
#DEBUG              echo Number of route entries for $bridgeprefix$b is $new_rtcount
                rtcount=$new_rtcount
                fi
                if [ $rtcount == $rttarget ]; then
#DEBUG              echo Reached target for $bridgeprefix$b
                    break;
                fi
                count=`expr $count + 1`
                if [ $count -gt 12 ]; then
                    echo Incomplete IP configuration for $bridgeprefix$b. Check network config. Aborting.
                    break;
                fi
                echo Incomplete IP configuration for $bridgeprefix$b. Waiting 5 seconds.
                sleep 5
            done
        done
}

stop () {
    for b in $BRIDGES ; do
        echo "Shutting down service bridge $bridgeprefix$b"
        for br in ${SSIF[$b]} ; do
            echo Removing SSIF $br on $bridgeprefix$b
            brctl delif $bridgeprefix$b $br  ||  RETVAL=1
        done
        for br in ${CSIF[$b]} ; do
            echo Removing CSIF $br on $bridgeprefix$b
            brctl delif $bridgeprefix$b $br  ||  RETVAL=1
        done
        ifconfig $bridgeprefix$b down || RETVAL=1
        brctl delbr $bridgeprefix$b  ||  RETVAL=1
     done
     [ $RSTP == 1 ] && killproc rstpd

}

serverif () {
    case "$1" in
      up)
        for b in $BRIDGES ; do
            for br in ${SSIF[$b]} ; do
                echo Enabling $br on $bridgeprefix$b
                if [[ $br == bond* ]] ; then
                  for sl in `slaves $br`; do
                    echo ' ' Enabling slave $sl of $br
                    ifconfig $sl up
                  done
                else
                  ifconfig $br up
                fi
            done
        done
        ;;
      down)
        for b in $BRIDGES ; do
            for br in ${SSIF[$b]} ; do
                echo Disabling $br on $bridgeprefix$b
                if [[ $br == bond* ]]; then
                  for sl in `slaves $br`; do
                    echo ' ' Disabling slave $sl of $br
                    ifconfig $sl down
                  done
                else 
                  ifconfig $br down
                fi
            done
        done
        ;;
      *)
        exit 1
    esac
}

# See how we were called.
case "$1" in
  start)
	start $2
	;;
  stop)
	stop $2
	;;
  status)
    for b in $BRIDGES ; do
        ifconfig $bridgeprefix$b
        brctl showstp $bridgeprefix$b
    done
    ;;
  serverif)
    serverif $2
    ;;
  restart|reload)
	stop
	start
	;;
  *)
	echo $"Usage: $0 {start|stop|status|restart|reload}"
	exit 1
esac

exit $RETVAL
