Traffic control

From Ilianko
#! /bin/bash
NETCARD=eth2
NET2=ppp0

MAXBANDWIDTH=100

# reinit
iptables -t mangle -F
echo "iptables -t mangle -F"
#tc qdisc del dev $NETCARD root handle 3
#tc qdisc del dev $NET2 root handle 4

echo "tc qdisc del dev $NETCARD root handle 1"

tc qdisc add dev $NETCARD root handle 3: htb default 9999
echo "tc qdisc add dev $NETCARD root handle 1: htb default 9999"

tc qdisc add dev $NET2 root handle 4: htb default 9999



# create the default class
tc class add dev $NETCARD parent 3:0 classid 3:9999 htb rate $(( $MAXBANDWIDTH ))kbit ceil $(( $MAXBANDWIDTH ))kbit burst 5k prio 9999
tc class add dev $NET2 parent 4:0 classid 4:9999 htb rate $(( $MAXBANDWIDTH ))kbit ceil $(( $MAXBANDWIDTH ))kbit burst 5k prio 9999



# control bandwidth per w

declare -A ipctrl
# define list of IP and bandwidth (in kilo bits per seconds) below
ipctrl[172.16.20.2]="1000"
ipctrl[172.16.20.1]="1000"


mark=0
for ((i=1;i<=253;i++))
do
    ip="192.168.24.$i"
    mark=$(( mark + 1 ))
    bandwidth=700

#echo $ip

    # traffic shaping rule
#echo "tc class add dev $NETCARD parent 1:0 classid 1:$mark htb rate $(( $bandwidth ))kbit ceil "
tc class add dev $NETCARD parent 3:0 classid 3:$mark htb rate $(( $bandwidth ))kbit ceil $(( $bandwidth ))kbit burst 5k prio $mark
tc class add dev $NET2 parent 4:0 classid 4:$mark htb rate $(( $bandwidth ))kbit ceil $(( $bandwidth ))kbit burst 5k prio $mark


    # netfilter packet marking rule
    iptables -t mangle -A POSTROUTING -s $ip -j CLASSIFY --set-class 4:$mark
    iptables -t mangle -A POSTROUTING -d $ip -j CLASSIFY --set-class 3:$mark


    # filter that bind the two
    tc filter add dev $NETCARD parent 3:0 protocol ip prio $mark handle $mark fw flowid 1:$mark
    tc filter add dev $NET2 parent 4:0 protocol ip prio $mark handle $mark fw flowid 1:$mark

 #   echo "IP $ip is attached to mark $mark and limited to $bandwidth kbps"
done