Skip to contentSkip to conversation

tc-htb traffic shaping script

LARTC Arman 3 participants 3 messages May 24, 2007

lore
Share & tools
lore
#1 lore raw

I can send you mine, it's a modified version of one I found somewhere on the net to be able to limit bandwith on a linux router. I did no cleaning up or anything

#!/bin/bash

# tc uses the following units when passed as a parameter.

# kbps: Kilobytes per second

# mbps: Megabytes per second

# kbit: Kilobits per second

# mbit: Megabits per second

# bps: Bytes per second

# Amounts of data can be specified in:

# kb or k: Kilobytes

# mb or m: Megabytes

# mbit: Megabits

# kbit: Kilobits

# To get the byte figure from bits, divide the number by 8 bit

#

#

# Name of the traffic control command.

TC=/sbin/tc

IPTABLES=/sbin/iptables

# The network interface we're planning on limiting bandwidth.

IF1=eth1.106 # Interface

IF2=eth0 # Interface

# Download limit (in mega bits)

DNLD=100mbit # DOWNLOAD Limit

# Upload limit (in mega bits)

UPLD=100mbit # UPLOAD Limit

# IP address of the machine we are controlling

#IP=81.18.0.0/24 #Host IP

#IP=0.0.0.0/0 #Host IP

# Filter options for limiting the intended interface.

IN="$TC filter add dev $IF2 protocol ip parent 1:0 prio 1"

OUT="$TC filter add dev $IF1 protocol ip parent 2:0 prio 1"

start() {

# All traffic originating from IF1 gets marked

    $IPTABLES -t mangle -D PREROUTING -i $IF1 -j MARK --set-mark 106
Quoted 1 line

/dev/null 2>&1

    $IPTABLES -t mangle -A PREROUTING -i $IF1 -j MARK --set-mark 106

# INBOUND matches on fwmark 106 and gets shaped when it leaves the IF2 interface

$TC qdisc add dev $IF2 root handle 1: htb default 30

$TC class add dev $IF2 parent 1: classid 1:1 htb rate $DNLD

$IN handle 106 fw flowid 1:1

printf "\n"

printf "Shaping traffic incoming on $IF1 ==> $IF2 to max. $DNLD"

# OUTBOUND matches all traffic heading out IF1 gets shaped, no filter needed

$TC qdisc add dev $IF1 root handle 2: htb default 1

$TC class add dev $IF1 parent 2: classid 2:1 htb rate $UPLD

# $OUT u32 match ip src $IP flowid 2:1

    printf "\n"

    printf "Shaping traffic incoming on $IF2 ==> $IF1 to max. $UPLD\n"

# The first line creates the root qdisc, and the next line

# creates a child qdiscs that respectively are used to shape download
# and upload bandwidth. The third line defines a filter if required.

}

stop() {

# Stop the bandwidth shaping.

$TC qdisc del dev $IF1 root

$TC qdisc del dev $IF2 root

$IPTABLES -t mangle -D PREROUTING -i $IF1 -j MARK --set-mark 106

}

restart() {

# Self-explanatory.

stop

sleep 1

start

}

show() {

# Display status of traffic control status.

# $TC -s qdisc ls dev $IF1

$TC -s qdisc ls dev $IF2

}

case "$1" in

start)

echo -n "Starting bandwidth shaping: "

start

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 $IF2:"

    show

    echo ""

    ;;

*)

    pwd=$(pwd)

    echo "Usage: tc.bash {start|stop|restart|show}"

    ;;

esac

exit 0

From: [email protected] [mailto:[email protected]] On Behalf Of Arman
Sent: donderdag 24 mei 2007 12:46
To: [email protected]
Subject: [LARTC] tc-htb traffic shaping script

Hi,

Is there any tested good HTB script for traffic shaping available
like as that of CBQ available at.

http://freshmeat.net/projects/cbq.init

I am n new bie and need to work on htb.

Signature · 2 lines
Regards,
M Arman
#2 lore raw
In reply to Arman

http://lartc.org/wondershaper/

Quoted 314 lines · On 5/24/07, [email protected] <[email protected]> wrote:

I can send you mine, it's a modified version of one I found somewhere on the net to be able to limit bandwith on a linux router. I did no cleaning up or anything

#!/bin/bash

# tc uses the following units when passed as a parameter.

# kbps: Kilobytes per second

# mbps: Megabytes per second

# kbit: Kilobits per second

# mbit: Megabits per second

# bps: Bytes per second

# Amounts of data can be specified in:

# kb or k: Kilobytes

# mb or m: Megabytes

# mbit: Megabits

# kbit: Kilobits

# To get the byte figure from bits, divide the number by 8 bit

#

#

# Name of the traffic control command.

TC=/sbin/tc

IPTABLES=/sbin/iptables

# The network interface we're planning on limiting bandwidth.

IF1=eth1.106 # Interface

IF2=eth0 # Interface

# Download limit (in mega bits)

DNLD0mbit # DOWNLOAD Limit

# Upload limit (in mega bits)

UPLD0mbit # UPLOAD Limit

# IP address of the machine we are controlling

#IP�.18.0.0/24 #Host IP

#IP=0.0.0.0/0 #Host IP

# Filter options for limiting the intended interface.

IN="$TC filter add dev $IF2 protocol ip parent 1:0 prio 1"

OUT="$TC filter add dev $IF1 protocol ip parent 2:0 prio 1"

start() {

# All traffic originating from IF1 gets marked

    $IPTABLES -t mangle -D PREROUTING -i $IF1 -j MARK --set-mark 106
Quoted 1 line

/dev/null 2>&1

    $IPTABLES -t mangle -A PREROUTING -i $IF1 -j MARK --set-mark 106

# INBOUND matches on fwmark 106 and gets shaped when it leaves the IF2 interface

$TC qdisc add dev $IF2 root handle 1: htb default 30

$TC class add dev $IF2 parent 1: classid 1:1 htb rate $DNLD

$IN handle 106 fw flowid 1:1

printf "\n"

printf "Shaping traffic incoming on $IF1 => $IF2 to max. $DNLD"

# OUTBOUND matches all traffic heading out IF1 gets shaped, no filter needed

$TC qdisc add dev $IF1 root handle 2: htb default 1

$TC class add dev $IF1 parent 2: classid 2:1 htb rate $UPLD

# $OUT u32 match ip src $IP flowid 2:1

printf "\n"

printf "Shaping traffic incoming on $IF2 => $IF1 to max. $UPLD\n"

# The first line creates the root qdisc, and the next line

# creates a child qdiscs that respectively are used to shape download
# and upload bandwidth. The third line defines a filter if required.

}

stop() {

# Stop the bandwidth shaping.

$TC qdisc del dev $IF1 root

$TC qdisc del dev $IF2 root

$IPTABLES -t mangle -D PREROUTING -i $IF1 -j MARK --set-mark 106

}

restart() {

# Self-explanatory.

stop

sleep 1

start

}

show() {

# Display status of traffic control status.

# $TC -s qdisc ls dev $IF1

$TC -s qdisc ls dev $IF2

}

case "$1" in

start)

echo -n "Starting bandwidth shaping: "

start

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 $IF2:"

    show

    echo ""

    ;;

*)

    pwd=$(pwd)

    echo "Usage: tc.bash {start|stop|restart|show}"

    ;;

esac

exit 0

From: [email protected] [mailto:[email protected]] On Behalf Of Arman
Sent: donderdag 24 mei 2007 12:46
To: [email protected]
Subject: [LARTC] tc-htb traffic shaping script

Hi,

Is there any tested good HTB script for traffic shaping available like
as that of CBQ available at.

http://freshmeat.net/projects/cbq.init

I am n new bie and need to work on htb.

--
Regards,
M Arman
_______________________________________________
LARTC mailing list
[email protected]
http://mailman.ds9a.nl/cgi-bin/mailman/listinfo/lartc

Signature · 5 lines
Marco
_______________________________________________
LARTC mailing list
[email protected]
http://mailman.ds9a.nl/cgi-bin/mailman/listinfo/lartc