
                        ADSL Bandwidth Management HOWTO

@:Dan Singletary dvsing@sonicspike.net
Ķ:ӼC expns@yahoo.com
     _________________________________________________________________

   oɴyzpN Linux ]w֦ae޲z\઺Ѿ,Ħa޲zADSL
   M䥦bandwidth ](cable modem, ISDN, ) 
     _________________________________________________________________

1. 

     * 1.1 ɪ̷s
     * 1.2 lC
     * 1.3 n
     * 1.4 z]vM\i
     * 1.5 XPץ

2. I

     * 2.1 n
     * 2.2 G
     * 2.3 Packet Queues(ƾڥ]C)

3. u@z

     * 3.1 Throttling Outbound Traffic with Linux HTB(ϥHTBXq
       T)
     * 3.2 Priority Queuing with HTB(ϥ HTB ]wCuv)
     * 3.3 ϥiptables XqT
     * 3.4 ٥iHA@U
     * 3.5 Attempting to Throttle Inbound Traffic(JqT)
     * 3.6 JqTݰ_Ӥ

4. 

     * 4.1 Caveats
     * 4.2 Script: myshaper

5. 

6. OK It Works!! Now What?
     _________________________________________________________________

1. 

   ɪئaOѤ@ӥi檺k޲zADSL(cable mode)XqH.

1.1 ɪ̷s

   ziHb [1]http://www.tldp.org oɪ̷s.

1.2 lC

   ADSL Bandwidth Manage DMHЭq\: [2]jared.sonicspike.net

1.3 n

   pGĥΤFoHOWTOkӹ]Ʃγyk,L׬O@, G
   ιoHOWTO^mHNڵӾd.

1.4 z]vM\i

   HOWTOz]vDan SingletaryҦ:

   This document is copyright 2002 by Dan Singletary, and is released
   under the terms of the GNU Free Documentation License, which is hereby
   incorporated by reference.

1.5 XPץ

   pGzHOWTODάݪk,ЦbŪɭԵ@
   e-mail:dvsing@sonicspike.net

2. I

2.1 n

   nI: oǤkިSb䥦o檩L,ڷQ`u@jS
   D.UOB檺:
     * Red Hat Linux 7.3
     * 2.4.18-5  QoS ֤ߪ (Ҳդ]iH) ]tHUpatches (
       ɤB)(i|̲ץ[J̷s֤߷):
     * HTB queue - [3]http://luxik.cdi.cz/~devik/qos/htb/ `N: Mandrake(
       ҼwܧJ8.1, 8.2)֤ߦ 2.4.18-3 _NFHTB  patches.
     * IMQ device - [4]IMQ device - http://luxik.cdi.cz/~patrick/imq/
     * iptables v1.2.6a Χs(version of iptables distributed with
       Red Hat 7.3 is missing the length module)

   Note: Previous versions of this document specified a method of
   bandwidth control that involved patching the existing sch_prio queue.
   It was found later that this patch was entirely unnecessary.
   Regardless, the newer methods outlined in this document will give you
   better results (although at the writing of this document 2 kernel
   patches are now necessary. :) Happy patching.)

2.2 G

   c²,Ҧ]w̷ӤUoӧGi:
       ______________________________________________________________

  <-- 128kbit/s      --------------     <-- 10Mbit -->
  Internet <--------------------> | ADSL Modem | <--------------------
                1.5Mbit/s -->     --------------                     |
                                                                     | eth0
                                                                     V
                                                         -----------------
                                                         |               |
                                                         | Linux Router  |
                                                         |               |
                                                         -----------------
                                                          | .. | eth1..ethN
                                                          |    |
                                                          V    V

                                                       Local Network
       ______________________________________________________________

2.3 Packet Queues(ƾڥ]C)

   Packet QueuesO@Ӯe, ƾڤQ]ƥ߬Jeɭ, Packet
   Queues tdȮɦd. DQ]wt~@ؤ覡,_hƾڥ]O FIFO
   (first in, first out ̦iJQueuesƾڱNQֵ̧oe) iƶ.

  The Upstream(VWǿ)

   ADSLaeѤ٪ 1.5Mbit/s downstream(VUǿ)M128kbit/sec
   upstream(VWǿ)զ. Linux Ѿ(D)PADSL modemstv
   b10Mbits/sk.pG Linux ѾP Local Network(a) stv
   ]b10Mbits/sk,ѾMLocal Network(a)Queues(C)N|
   sb.H10Mbits/sFADSL modemƾڥ]onH128kbit/sec ǿ
   Internet.]ƾڥ]NbADSL modemΦQueues,ADSL modemNIӲ
   ͼƾڥ]ᥢ{H. TCPNOΨӱo˪p,վǿ鵡fjpH
   FQαaęήĪG.

   TCPQueues(C)HQαae. jFIFO QueuesNƾڥ]ǰe
   .

   t@ئPFIFOIۦQueues(C)O n-band priority queue, NFIFO
   u@ӶCk, ƾڥ]ŧOƥXhFIFO Queues(C), C@
   QueuesuŧO]w, `OqŧOQueuesNƾdequeued(XC).
   ϥγoؤk,FTPMtelnetPɤWƾڥ]ɭ, telnetƾڥ]No
   uŧO.Wtelnetƾڥ]NQ߬Joe.

   Linux ϥΤ@طsQueues: Hierarchical Token Bucket (HTB Ķū
   Ce). In-band priority queue, n-band priority queueb
   CӯŧOuƾڳqTO. HTB@[i\:bwŧO
   Wإߤ@ӷsŧOqT.hTаѷ: [5]
   http://www.lartc.org/

  The Downstream(VUǿ)

   qInternetoeADSL modemƾڥ]JMƾڥ]XQueuesjܬۦP. 
   ޫ, queue |bzISP. ]o˱zjઽƾڥ]p
   ƶΥHاΦtuv. u@ؤkYuo̪ɶ:Vz
   oeƾڥ]ɭԤnӧ. O,zLkƾڥ]Ft. o
   ̦@ǤkNoe̪t״C:
     * GNNJƾڥ]. TCP is designed to take full advantage of
       the available bandwidth while also avoiding congestion of the
       link. This means that during a bulk data transfer TCP will send
       more and more data until eventually a packet is dropped. TCP
       detects this and reduces it's transmission window. This cycle
       continues throughout the transfer and assures data is moved as
       quickly as possible.
     * aadvertised receive window(si)- During a TCP transfer,
       the receiver sends back a continuous stream of acknowledgment
       (ACK) packets. Included in the ACK packets is a window size
       advertisement which states the maximum amount of unacknowledged
       data the receiver should send. By manipulating the window size of
       outbound ACK packets we can intentionally slow down the sender. At
       the moment there is no (free) implementation for this type of
       flow-control on Linux (however I may be working on one!).

3. u@z

   XӨBJiHuupstream bandwidth(VWǿ骺ae).Ĥ@ONLinux
   ADSL modemǿaeC ADSL modemInternetaeHU.b Linux
   ѾΦƾڥ]C.

   ĤG,bѾ]wCuvM´k.

   ڭ̱Nqtelnet , hHsuCHΥ椬nӦҬdCuv.

   ϥ HTB C,ڭ̥iHPɳ]waeMCuv,åBuŧO
   |ۤ.

   ĤT,]wϥfwmarkϤƾڥ].

3.1 Throttling Outbound Traffic with Linux HTB(ϥHTBXqT)

   ڭ̱NϥHTPƾڥ]F ADSL modem tv, FYuɶ,ڭ̥
   ݫOҤb ADSL modem ΦȬOu@Ӽƾڥ]C.

   Note: previous claims in this section (originally named N-band
   priority queuing) were later found to be incorrect. It actually WAS
   possible to classify packets into the individual bands of the priority
   queue by only using the fwmark field, however it was poorly documented
   at the writing of version 0.1 of this document

3.2 Priority Queuing with HTB(ϥ HTB ]wCuv)

   {b,ڭ̤p󧹵ʯ, ڭ̥uONCqADSL modem ಾLinux
   ѾWӤx. pG{b100 ƾڥ]qCX{be]w,ڱN
   QG, ouO@ɪMӤx.

   HTBCӬ۾FCiHt@uv.bPŧO]wP
   .۱qڭ̥iHCӯŧO]w@ӳ̤pOҭ, ڭ̴N֦Fƾڥ]
   XCMoeǯO. HTBiHܦnaoIåB|uŬۤ..

   ]wFŧOH,ڭ̨ϥιLoNqHiŧO.XؤkiH{,
   ڭ̥uб`Ϊiptables/ipchains. ڭ̱Nϥiptables]w@ǳWhN
   PqHJ줣PŧO.

3.3 ϥiptables XqT

   Note: originally this document used ipchains to classify packets. The
   newer iptables is now used.

   o̦@²檺yz,Xƾڥ]pq0x00Ŷ}l,J4ӤP
   ŷ:
     * NҦƾڥ]ŧO]0x03,oO̧CŧO.
     * NICMPƾڥ]ŧO]0x00, Qping,Nݱo̰ŧO
       uv.
     * NҦoؼкݤf25ƾڥ]ŧO]w0x03,pGֵoee-mail
       a@ӫܤj, ڭ̪qTN|JhA@ˤoB, M,
       ̨äQ.
     * NҦoCAȾƾڥ]ŧO]w0x02,oNC@ӾA
       ɶ. but will keep them from swamping out the system
       applications that require low latency.
     * NҦoؼкݤf1024ΧCƾڥ]ŧO]w0x01,
       telnet,SSHtΪAȴuv. Ftpݤf]boӽd򤧤.

   N"p"ƾڥ]ŧO]w0x02,Outbound ACK packets from inbound
   downloads should be sent promptly to assure efficient downloads. This
   is possible using the iptables length module.

   M,٥iH̾ڱzݨDӳ]w.

3.4 ٥iHA@U

   n[֤zܤ֭nHWƱ. , N̤jǿ椸(MTU)]w
   b1500bytesHU, CoӭȴN|Yuݮɶ, o|t(
   _FڥiΪ]Rq),]CӼƾڥ]40bytesIPMTCPT. t~[
   kONCYu100HU,oiH٥hADSL10۷MŤ@
   1500byteMTUɶ.

3.5 Attempting to Throttle Inbound Traffic(JqT)

   qLϥ Intermediate Queuing Device (IMQ)C, ڭ̥iHBzX
   ƾڥ]@˱NJƾڥ]eJC. oӮרҤƾڥ]uvD`²
   . NݩTCPd򤺪qTŧO]w 0x00, ݩTCPd򤺪qTŧO]
   w 0x01, ]iHN"p"TCPƾڥ]qTŧO]w 0x00,ڭ̱Nз
   FIFOCŧO]w 0x00 , ڭ̧Random Early Drop (RED) CŧO]w
   0x01 REDNbƾڥ]ݰ_ӥhɭ(CNnX), CǿαN
   ڥ]. ڭ̱N̤jƤJtv(tvpگF쪺).We'll also
   rate-limit both classes to some maximum inbound rate which is less
   than your true inbound speed over the ADSL modem.

3.6 JqTݰ_Ӥ

   ڭ̥ݭJqT,HISPCM, oˬ۷w5ƾ,
   DO{bߤ@~|ONƾڥ].oǼƾڥ]HgqADSL modedm
   oF@Ǳae. OoǼƾڥ]oQF,oǳQ󪺼ƾڥ]̲׷|Y
   hae. ڭ̭qTɭ, ڭ̭FӦۥaƾڥ]ǰe
   v. ]]ڭ̥󪺨Ǽƾڥ]ҥHڤJǰevbW. ڭ̹
   ڤWJADSL modemگF쪺v٭nC. bڷ, ڱNۤv
   1.5mbit/s downstream ADSL b700kbit/sec ,ϥõo5ӤUs
   . TCP|ܶVh,Obƾڥ]aeNVh,åBƲvz٭nC.

   n~|ӱTCPqTOާ@ TCP window, OoӦnDF(ڪD
   @...)

4. 

4.1 Caveats

   oeDSL modemƾڳtvݰ_². jh DSL modems
   HguabzISPhDM linux box إߤFǿƾڪHӺ. j
   hƪ DSL modems ϥATM@oeƾڪsh. ATM `OH53bytes/椸
   Φoeƾ.oǼƾڷ 5bytes OHY ,lU48bytes~Oǿ骺
   ƾ.Jϱzoe1byteƾ,]N]ATM `OH 53bytes/椸 Φoe
   ƾڦӮ53bytesae. oܱzNoe@ӥ 0 bytes ƾ + 20 bytes
   TCP Y + 20 bytes IP Y + 18 bytes HӺY զTCP ACKƾڥ].
   ڤW,JϱzoeHӺƾڥ]u40bytesĭt (TCP and IP
   header), ̤pHӺƾڥ]ĭtƾڬO46bytes,ҥHt~6bytesO
   t. oN۹ڥHӺƾڥ][WYO 18 + 46 = 64 bytes. bATM
   Wh,pGoe64bytesƾ,zNoe`@106bytesaeATM
   cells(椸). oܨCoe@TCP ACK ƾڥ], z|O42bytesae.
   pG Linux p DSL modem ϥΪʸ˴NSDF, O, Linux up
   TCP header, IP header, M 14 bytes  MAC a}. (Linux p 4 bytes
    CRC ]oOΨӱwh). Linux |NHӺƾڥ]̤pȭp
    46 bytes, ]|hpTw ATM 椸jp.

   oǩҦܱzXaeڤWnC@I.zݧ̾AXz
   ۤv. OzU@ӤjɺɶN|ɺ3HW.
   ]Linuxbaeӭp⪺~t, ҥHoܥio.

   I have been working on a solution to this problem for a few months and
   have almost settled on a solution that I will soon release to the
   public for further testing. The solution involves using a user-space
   queue instead of linux's QoS to rate-limit packets. I've basically
   implemented a simple HTB queue using linux user-space queues. This
   solution (so far) has been able to regulate outbound traffic SO WELL
   that even during a massive bulk download (several streams) and bulk
   upload (gnutella, several streams) the latency PEAKS at 400ms over my
   nominal no-traffic latency of about 15ms. For more information on this
   QoS method, subscribe to the email list for updates or check back on
   updates to this HOWTO.

4.2 Script: myshaper

   UOڥΨӱۤvѾscript. XqT̾J7ӶC
   . JqTJܨӻPTCPƾ(pGJƾڶWXtv,TCPƾڥ]NQ
   )C(lowest priority). script XtvݤWhu@o
   n,oOAXڦۤv]w,zӻGiणjۦP.

   o script Ob ADSL WonderShaper ¦WgXӪ,аѷ: [6]LARTC
   website.
       ______________________________________________________________

#!/bin/bash
#
# myshaper - DSL/Cable modem outbound traffic shaper and prioritizer.
#            Based on the ADSL/Cable wondershaper (www.lartc.org)
#
# Written by Dan Singletary (8/7/02)
#
# NOTE!! - This script assumes your kernel has been patched with the
#          appropriate HTB queue and IMQ patches available here:
#          (subnote: future kernels may not require patching)
#
#       http://luxik.cdi.cz/~devik/qos/htb/
#       http://luxik.cdi.cz/~patrick/imq/
#
# Configuration options for myshaper:
#  DEV    - set to ethX that connects to DSL/Cable Modem
#  RATEUP - set this to slightly lower than your
#           outbound bandwidth on the DSL/Cable Modem.
#           I have a 1500/128 DSL line and setting
#           RATEUP=90 works well for my 128kbps upstream.
#           However, your mileage may vary.
#  RATEDN - set this to slightly lower than your
#           inbound bandwidth on the DSL/Cable Modem.
#
#
#  Theory on using imq to "shape" inbound traffic:
#
#     It's impossible to directly limit the rate of data that will
#  be sent to you by other hosts on the internet.  In order to shape
#  the inbound traffic rate, we have to rely on the congestion avoidance
#  algorithms in TCP.  Because of this, WE CAN ONLY ATTEMPT TO SHAPE
#  INBOUND TRAFFIC ON TCP CONNECTIONS.  This means that any traffic that
#  is not tcp should be placed in the high-prio class, since dropping
#  a non-tcp packet will most likely result in a retransmit which will
#  do nothing but unnecessarily consume bandwidth.
#     We attempt to shape inbound TCP traffic by dropping tcp packets
#  when they overflow the HTB queue which will only pass them on at
#  a certain rate (RATEDN) which is slightly lower than the actual
#  capability of the inbound device.  By dropping TCP packets that
#  are over-rate, we are simulating the same packets getting dropped
#  due to a queue-overflow on our ISP's side.  The advantage of this
#  is that our ISP's queue will never fill because TCP will slow it's
#  transmission rate in response to the dropped packets in the assumption
#  that it has filled the ISP's queue, when in reality it has not.
#     The advantage of using a priority-based queuing discipline is
#  that we can specifically choose NOT to drop certain types of packets
#  that we place in the higher priority buckets (ssh, telnet, etc).  This
#  is because packets will always be dequeued from the lowest priority class
#  with the stipulation that packets will still be dequeued from every
#  class fairly at a minimum rate (in this script, each bucket will deliver
#  at least it's fair share of 1/7 of the bandwidth).
#
#  Reiterating main points:
#   * Dropping a tcp packet on a connection will lead to a slower rate
#     of reception for that connection due to the congestion avoidance algorith
m.
#   * We gain nothing from dropping non-TCP packets.  In fact, if they
#     were important they would probably be retransmitted anyways so we want to
#     try to never drop these packets.  This means that saturated TCP connectio
ns
#     will not negatively effect protocols that don't have a built-in retransmi
t like TCP.
#   * Slowing down incoming TCP connections such that the total inbound rate is
 less
#     than the true capability of the device (ADSL/Cable Modem) SHOULD result i
n little
#     to no packets being queued on the ISP's side (DSLAM, cable concentrator,
etc).  Since
#     these ISP queues have been observed to queue 4 seconds of data at 1500Kbp
s or 6 megabits
#     of data, having no packets queued there will mean lower latency.
#
#  Caveats (questions posed before testing):
#   * Will limiting inbound traffic in this fashion result in poor bulk TCP per
formance?
#     - Preliminary answer is no!  Seems that by prioritizing ACK packets (smal
l <64b)
#       we maximize throughput by not wasting bandwidth on retransmitted packet
s
#       that we already have.
#

# NOTE: The following configuration works well for my
# setup: 1.5M/128K ADSL via Pacific Bell Internet (SBC Global Services)

DEV=eth0
RATEUP=90
RATEDN=700  # Note that this is significantly lower than the capacity of 1500.
            # Because of this, you may not want to bother limiting inbound traf
fic
            # until a better implementation such as TCP window manipulation can
 be used.

#
# End Configuration Options
#

if [ "$1" = "status" ]
then
        echo "[qdisc]"
        tc -s qdisc show dev $DEV
        tc -s qdisc show dev imq0
        echo "[class]"
        tc -s class show dev $DEV
        tc -s class show dev imq0
        echo "[filter]"
        tc -s filter show dev $DEV
        tc -s filter show dev imq0
        echo "[iptables]"
        iptables -t mangle -L MYSHAPER-OUT -v -x 2> /dev/null
        iptables -t mangle -L MYSHAPER-IN -v -x 2> /dev/null
        exit
fi

# Reset everything to a known state (cleared)
tc qdisc del dev $DEV root    2> /dev/null > /dev/null
tc qdisc del dev imq0 root 2> /dev/null > /dev/null
iptables -t mangle -D POSTROUTING -o $DEV -j MYSHAPER-OUT 2> /dev/null > /dev/n
ull
iptables -t mangle -F MYSHAPER-OUT 2> /dev/null > /dev/null
iptables -t mangle -X MYSHAPER-OUT 2> /dev/null > /dev/null
iptables -t mangle -D PREROUTING -i $DEV -j MYSHAPER-IN 2> /dev/null > /dev/nul
l
iptables -t mangle -F MYSHAPER-IN 2> /dev/null > /dev/null
iptables -t mangle -X MYSHAPER-IN 2> /dev/null > /dev/null
ip link set imq0 down 2> /dev/null > /dev/null
rmmod imq 2> /dev/null > /dev/null

if [ "$1" = "stop" ]
then
        echo "Shaping removed on $DEV."
        exit
fi

###########################################################
#
# Outbound Shaping (limits total bandwidth to RATEUP)

# set queue size to give latency of about 2 seconds on low-prio packets
ip link set dev $DEV qlen 30

# changes mtu on the outbound device.  Lowering the mtu will result
# in lower latency but will also cause slightly lower throughput due
# to IP and TCP protocol overhead.
ip link set dev $DEV mtu 1000

# add HTB root qdisc
tc qdisc add dev $DEV root handle 1: htb default 26

# add main rate limit classes
tc class add dev $DEV parent 1: classid 1:1 htb rate ${RATEUP}kbit

# add leaf classes - We grant each class at LEAST it's "fair share" of bandwidt
h.
#                    this way no class will ever be starved by another class.
Each
#                    class is also permitted to consume all of the available ba
ndwidth
#                    if no other classes are in use.
tc class add dev $DEV parent 1:1 classid 1:20 htb rate $[$RATEUP/7]kbit ceil ${
RATEUP}kbit prio 0
tc class add dev $DEV parent 1:1 classid 1:21 htb rate $[$RATEUP/7]kbit ceil ${
RATEUP}kbit prio 1
tc class add dev $DEV parent 1:1 classid 1:22 htb rate $[$RATEUP/7]kbit ceil ${
RATEUP}kbit prio 2
tc class add dev $DEV parent 1:1 classid 1:23 htb rate $[$RATEUP/7]kbit ceil ${
RATEUP}kbit prio 3
tc class add dev $DEV parent 1:1 classid 1:24 htb rate $[$RATEUP/7]kbit ceil ${
RATEUP}kbit prio 4
tc class add dev $DEV parent 1:1 classid 1:25 htb rate $[$RATEUP/7]kbit ceil ${
RATEUP}kbit prio 5
tc class add dev $DEV parent 1:1 classid 1:26 htb rate $[$RATEUP/7]kbit ceil ${
RATEUP}kbit prio 6

# attach qdisc to leaf classes - here we at SFQ to each priority class.  SFQ in
sures that
#                                within each class connections will be treated
(almost) fairly.
tc qdisc add dev $DEV parent 1:20 handle 20: sfq perturb 10
tc qdisc add dev $DEV parent 1:21 handle 21: sfq perturb 10
tc qdisc add dev $DEV parent 1:22 handle 22: sfq perturb 10
tc qdisc add dev $DEV parent 1:23 handle 23: sfq perturb 10
tc qdisc add dev $DEV parent 1:24 handle 24: sfq perturb 10
tc qdisc add dev $DEV parent 1:25 handle 25: sfq perturb 10
tc qdisc add dev $DEV parent 1:26 handle 26: sfq perturb 10

# filter traffic into classes by fwmark - here we direct traffic into priority
class according to
#                                         the fwmark set on the packet (we set
fwmark with iptables
#                                         later).  Note that above we've set th
e default priority
#                                         class to 1:26 so unmarked packets (or
 packets marked with
#                                         unfamiliar IDs) will be defaulted to
the lowest priority
#                                         class.
tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 20 fw flowid 1:20
tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 21 fw flowid 1:21
tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 22 fw flowid 1:22
tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 23 fw flowid 1:23
tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 24 fw flowid 1:24
tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 25 fw flowid 1:25
tc filter add dev $DEV parent 1:0 prio 0 protocol ip handle 26 fw flowid 1:26

# add MYSHAPER-OUT chain to the mangle table in iptables - this sets up the tab
le we'll use
#                                                      to filter and mark packe
ts.
iptables -t mangle -N MYSHAPER-OUT
iptables -t mangle -I POSTROUTING -o $DEV -j MYSHAPER-OUT

# add fwmark entries to classify different types of traffic - Set fwmark from 2
0-26 according to
#                                                             desired class. 20
 is highest prio.
iptables -t mangle -A MYSHAPER-OUT -p tcp --sport 0:1024 -j MARK --set-mark 23
# Default for low port traffic
iptables -t mangle -A MYSHAPER-OUT -p tcp --dport 0:1024 -j MARK --set-mark 23
# ""
iptables -t mangle -A MYSHAPER-OUT -p tcp --dport 20 -j MARK --set-mark 26
# ftp-data port, low prio
iptables -t mangle -A MYSHAPER-OUT -p tcp --dport 5190 -j MARK --set-mark 23
# aol instant messenger
iptables -t mangle -A MYSHAPER-OUT -p icmp -j MARK --set-mark 20
# ICMP (ping) - high prio, impress friends
iptables -t mangle -A MYSHAPER-OUT -p udp -j MARK --set-mark 21
# DNS name resolution (small packets)
iptables -t mangle -A MYSHAPER-OUT -p tcp --dport ssh -j MARK --set-mark 22
# secure shell
iptables -t mangle -A MYSHAPER-OUT -p tcp --sport ssh -j MARK --set-mark 22
# secure shell
iptables -t mangle -A MYSHAPER-OUT -p tcp --dport telnet -j MARK --set-mark 22
# telnet (ew...)
iptables -t mangle -A MYSHAPER-OUT -p tcp --sport telnet -j MARK --set-mark 22
# telnet (ew...)
iptables -t mangle -A MYSHAPER-OUT -p ipv6-crypt -j MARK --set-mark 24
# IPSec - we don't know what the payload is though...
iptables -t mangle -A MYSHAPER-OUT -p tcp --sport http -j MARK --set-mark 25
# Local web server
iptables -t mangle -A MYSHAPER-OUT -p tcp -m length --length :64 -j MARK --set-
mark 21 # small packets (probably just ACKs)
iptables -t mangle -A MYSHAPER-OUT -m mark --mark 0 -j MARK --set-mark 26
# redundant- mark any unmarked packets as 26 (low prio)

# Done with outbound shaping
#
####################################################

echo "Outbound shaping added to $DEV.  Rate: ${RATEUP}Kbit/sec."

# uncomment following line if you only want upstream shaping.
# exit

####################################################
#
# Inbound Shaping (limits total bandwidth to RATEDN)

# make sure imq module is loaded

modprobe imq numdevs=1

ip link set imq0 up

# add qdisc - default low-prio class 1:21

tc qdisc add dev imq0 handle 1: root htb default 21

# add main rate limit classes
tc class add dev imq0 parent 1: classid 1:1 htb rate ${RATEDN}kbit

# add leaf classes - TCP traffic in 21, non TCP traffic in 20
#
tc class add dev imq0 parent 1:1 classid 1:20 htb rate $[$RATEDN/2]kbit ceil ${
RATEDN}kbit prio 0
tc class add dev imq0 parent 1:1 classid 1:21 htb rate $[$RATEDN/2]kbit ceil ${
RATEDN}kbit prio 1

# attach qdisc to leaf classes - here we at SFQ to each priority class.  SFQ in
sures that
#                                within each class connections will be treated
(almost) fairly.
tc qdisc add dev imq0 parent 1:20 handle 20: sfq perturb 10
tc qdisc add dev imq0 parent 1:21 handle 21: red limit 1000000 min 5000 max 100
000 avpkt 1000 burst 50

# filter traffic into classes by fwmark - here we direct traffic into priority
class according to
#                                         the fwmark set on the packet (we set
fwmark with iptables
#                                         later).  Note that above we've set th
e default priority
#                                         class to 1:26 so unmarked packets (or
 packets marked with
#                                         unfamiliar IDs) will be defaulted to
the lowest priority
#                                         class.
tc filter add dev imq0 parent 1:0 prio 0 protocol ip handle 20 fw flowid 1:20
tc filter add dev imq0 parent 1:0 prio 0 protocol ip handle 21 fw flowid 1:21

# add MYSHAPER-IN chain to the mangle table in iptables - this sets up the tabl
e we'll use
#                                                         to filter and mark pa
ckets.
iptables -t mangle -N MYSHAPER-IN
iptables -t mangle -I PREROUTING -i $DEV -j MYSHAPER-IN

# add fwmark entries to classify different types of traffic - Set fwmark from 2
0-26 according to
#                                                             desired class. 20
 is highest prio.
iptables -t mangle -A MYSHAPER-IN -p ! tcp -j MARK --set-mark 20              #
 Set non-tcp packets to highest priority
iptables -t mangle -A MYSHAPER-IN -p tcp -m length --length :64 -j MARK --set-m
ark 20 # short TCP packets are probably ACKs
iptables -t mangle -A MYSHAPER-IN -p tcp --dport ssh -j MARK --set-mark 20    #
 secure shell
iptables -t mangle -A MYSHAPER-IN -p tcp --sport ssh -j MARK --set-mark 20    #
 secure shell
iptables -t mangle -A MYSHAPER-IN -p tcp --dport telnet -j MARK --set-mark 20 #
 telnet (ew...)
iptables -t mangle -A MYSHAPER-IN -p tcp --sport telnet -j MARK --set-mark 20 #
 telnet (ew...)
iptables -t mangle -A MYSHAPER-IN -m mark --mark 0 -j MARK --set-mark 21
       # redundant- mark any unmarked packets as 26 (low prio)

# finally, instruct these packets to go through the imq0 we set up above
iptables -t mangle -A MYSHAPER-IN -j IMQ

# Done with inbound shaping
#
####################################################

echo "Inbound shaping added to $DEV.  Rate: ${RATEDN}Kbit/sec."
       ______________________________________________________________

5. 

   ²檺kO low-priority qTupstreamM.o̾ڱzŧO]w.
   p,NpingMtelnetqT]wuŧO(lower fwmark). pGzFTPW
   M upstream ae, zunpinghDɶ(on the other side of
   the DSL line) W[@ǼƶqPSCpۤ.Ping b 100ms
   HU(̾ڱz]w). pGhX1,2 ,ܦǦa褣l.

6. OK It Works!! Now What?

   U, UӴNϥXzQo쪺Uتۨ"ɨ"aӪnBa!

   Now that you've successfully started to manage your bandwidth, you
   should start thinking of ways to use it. After all, you're probably
   paying for it!
     * Use a Gnutella client and SHARE YOUR FILES without adversely
       affecting your network performance
     * Run a web server without having web page hits slow you down in
       Quake

References

   1. http://www.tldp.org/
   2. http://jared.sonicspike.net/mailman/listinfo/adsl-qos
   3. http://luxik.cdi.cz/~devik/qos/htb/
   4. http://luxik.cdi.cz/~patrick/imq/
   5. http://www.lartc.org/
   6. http://www.lartc.org/
