Wednesday, June 03, 2009

To limit your network traffic bandwidth

If you need to limit your network traffic bandwidth.
You need to install iproute-2.6.27-2.fc10.i386 in Fedora .
The original script is from
http://www.torrentflux.com/forum/index.php?topic=2743.0

To use it you need to have root privileged.
E.g.
$ sudo sh tc.sh upload

=======
#!/bin/bash
#
# Created by Discovery for PSA
# Modified by Mige Harimurti
#
# Name of the traffic control command.
TC=/sbin/tc

# The network interface we're planning on limiting bandwidth.
IF=eth0 # Interface

# Download limit (in mega bits)
DNLD=1000mbps # DOWNLOAD Limit
#DNLD=10kbps # DOWNLOAD Limit

# IP address of the machine we are limiting the uploads on.
# IP=192.168.1.101 # Host IP

# Filter options for limiting the intended interface.
# U32="$TC filter add dev $IF protocol ip parent 1:0 prio 1 u32"

start() {

# We'll use Hierarchical Token Bucket (HTB) to shape bandwidth.
# For detailed configuration options, please consult Linux man
# page.

$TC qdisc add dev $IF root handle 1: htb default 10
$TC class add dev $IF parent 1: classid 1:1 htb rate $DNLD burst 15k
$TC class add dev $IF parent 1:1 classid 1:10 htb rate $DNLD ceil $DNLD burst 15k
$TC class add dev $IF parent 1:1 classid 1:20 htb rate $UPLD

$TC qdisc add dev $IF parent 1:10 handle 10: sfq perturb 10
$TC qdisc add dev $IF parent 1:20 handle 20: sfq perturb 10

$TC filter add dev $IF protocol ip parent 1:0 prio 1 handle 10 fw flowid 1:10
$TC filter add dev $IF protocol ip parent 1:0 prio 1 handle 20 fw flowid 1:20


#iptables -t mangle -A OUTPUT -p tcp --sport 1024:65535 -j MARK --set-mark 0x14
iptables -t mangle -A OUTPUT -p tcp --sport 9001 -j MARK --set-mark 0x14


# The first line creates the root qdisc, and the next two lines
# create three child qdisc that are to be used to shape download
# and upload bandwidth.

}

stop() {

# Stop the bandwidth shaping.
$TC qdisc del dev $IF root
iptables -t mangle -F
}

upload() {

read -p 'Enter Upload Speed [e.g.50kbps]: ' UPLD
}

restart() {

# Self-explanatory.
stop
sleep 1
start

}

show() {

# Display status of traffic control status.
$TC -s qdisc ls dev $IF

}

case "$1" in

start)

upload
echo -n "Starting bandwidth shaping: "
start
echo "done"
;;

upload)
#read -p 'Enter Upload Speed in kbps: ' UPLD
upload
echo "Upload is now set to: $UPLD"
echo -n "Restarting bandwidth shaping: "
restart
echo "done"
;;

stop)

echo -n "Stopping bandwidth shaping: "
stop
echo "done"
;;

restart)

echo -n "Restarting bandwidth shaping: "
restart
echo "done"
;;

show)

echo "Bandwidth shaping status for $IF:"
show
echo ""
;;

*)

pwd=$(pwd)
echo "Usage: tc.bash {start|upload|stop|restart|show}"
;;

esac

exit
==============
Post a Comment