#!/bin/sh


# Delete anything that is setup already
$PRG_TC qdisc del dev $DEV root 2> /dev/null

# Attach cbq to device:
$PRG_TC qdisc add dev $DEV root handle 8000: \
  cbq bandwidth 100Mbit allot 1514 cell 8 avpkt 1514 mpu 64

  
# Create default high-priority class for forwarded traffic:
$PRG_TC class add dev $DEV parent 8000:0 classid 8000:1 \
  cbq bandwidth 100Mbit rate $BW"Kbit" weight 25Kbit allot 1514 cell 8 mpu 64 \
    prio 8 bounded isolated maxburst 10 minburst 0 avpkt 1514
$PRG_TC filter add dev $DEV parent 8000: protocol ip pref 15 \
       u32 match ip src 0.0.0.0/0 flowid 8000:1
       
# Add WRR to the class:
$PRG_TC qdisc add dev $DEV parent 8000:1 handle 8001: \
  wrr $WRR_ADDR_FIELD $CLASSIFY $MAXCLASSES $PROXY_MAXCON

# Attach SFQ queus to WRR:
declare -i NUM=$MAXCLASSES;
while [ $NUM -ge 1 ]; do
  HNUM=$(printf %X $NUM)
  $PRG_TC qdisc add dev $DEV parent 8001:$HNUM handle $HNUM: sfq
  NUM=$NUM-1
done  

# If this is a local interface we should create a special class for LAN traffic
if [ "$ADDR" != "" ]; then
    # Create a class for LAN trafic
   $PRG_TC class add dev $DEV parent 8000:0 classid 8000:2 \
       cbq bandwidth 100Mbit rate 100Mbit weight 25Kbit allot 1514 cell 8 mpu 64 \
           prio 6 bounded isolated maxburst 10 minburst 0 avpkt 1514
	   
    # Set filter for it ...
    for x in $LAN ; do
        for y in $LAN ; do
            if [ $x != $y ]; then
	    	 $PRG_TC filter add dev $DEV parent 8000:0 protocol ip prio 6 u32 match ip src $x match ip dst $y flowid 8000:2
            fi
        done
    done
   
    for x in $LAN ; do
	for y in $LAN_ADDR ; do
		if [ $x != $y ]; then
	    	 $PRG_TC filter add dev $DEV parent 8000:0 protocol ip prio 6 u32 match ip src $x match ip dst $y flowid 8000:2
		fi
	done
    done
												    
    for x in $LAN_ADDR ; do
	for y in $LAN ; do
		if [ $x != $y ]; then
	    	 $PRG_TC filter add dev $DEV parent 8000:0 protocol ip prio 6 u32 match ip src $x match ip dst $y flowid 8000:2
		fi
	done
    done
    
    #Proxy 
    for x in $PROXY; do
        $PRG_TC filter add dev $DEV protocol ip parent 8000:0 prio 1 u32 match ip sport $x 0xffff match ip tos 8 0xff flowid 8000:1
        $PRG_TC filter add dev $DEV protocol ip parent 8000:0 prio 2 u32 match ip sport $x 0xffff flowid 8000:2
    done

    # And set the pfifo scheulder		    
    $PRG_TC qdisc add dev $DEV parent 8000:2 handle 8002: pfifo
fi     

# 100 kbits class for icmp and special ports traffic
$PRG_TC class add dev $DEV parent 8000:0 classid 8000:3 \
      cbq bandwidth 100Mbit rate $BW_PORTS"Kbit" weight 25Kbit allot 1514 cell 8 mpu 64 \
          prio 2 bounded isolated maxburst 10 minburst 0 avpkt 1514
	  
for x in $PORTS; do
    if [ "$ADDR" != "" ]; then
        $PRG_TC filter add dev $DEV parent 8000: protocol ip pref 1 \
	    u32 match ip sport $x 0xffff flowid 8000:3
    elif [ "$ADDR" = "1" ]; then
	$PRG_TC filter add dev $DEV parent 8000: protocol ip pref 1 \
	    u32 match ip dport $y 0xffff flowid 8000:3
    fi
done
#ICMP is always in this class ... 
$PRG_TC filter add dev $DEV parent 8000: protocol ip prio 2 \
    u32 match ip protocol 1 0xff flowid 8000:3 
#Added WRR support for ICMP and special PORTS
$PRG_TC qdisc add dev $DEV parent 8000:3 handle 8003: \
 wrr sour masq 256 0
