Traffic Control TCNG HTB HOWTO
Traffic Control TCNG HTB HOWTO
Version 1.0.1
Martin A. Brown
linuxip.net Network Administration <[email protected]> April 2006 Revision History Revision 1.0.1 20061028 Updating contact information Revision 1.0 20030416 Initial Release, reviewed by LDP Revision 0.5 20020401 submit to tldp, rename/retitle with HOWTO Revision 0.4 20020331 new example, bucket crash course Revision 0.3 20020316 corrections and notes from Jacob Teplitsky, raptor and Joshua Heling Revision 0.2 20020315 links, cleanup, publish Revision 0.1 20020314 initial revision 2006, Martin A. Brown
Revised by: MAB Revised by: tab Revised by: MAB Revised by: MAB Revised by: MAB Revised by: MAB Revised by: MAB
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with no invariant sections, with no FrontCover Texts, with no BackCover Text. A copy of the license is located at www.gnu.org/copyleft/fdl.html.
Table of Contents
1. Introduction.....................................................................................................................................................1 1.1. What is traffic control and how does it work?..................................................................................1 1.2. What is htb?......................................................................................................................................2 1.3. What is tcng? .....................................................................................................................................2 2. Requirements ...................................................................................................................................................4 2.1. kernel requirements ...........................................................................................................................4 2.2. tc requirements..................................................................................................................................4 2.3. tcng requirements..............................................................................................................................4 3. Configuration examples ..................................................................................................................................5 3.1. Using tcng to shape download only..................................................................................................5 3.2. Using a tworate threecolor meter.................................................................................................7 4. Miscellaneous Notes........................................................................................................................................9 5. Links and Further documentation..............................................................................................................10
1. Introduction
This is a brief tutorial on using tcng (Traffic Control Next Generation) with HTB (Hierarchical Token Bucket) to perform traffic shaping on a Linux machine. This tutorial is intended for systems administrators who have AT LEAST, a basic understanding of traffic control EITHER the capability to compile iproute2 and tcng from source OR the capability of building RPMS from provided SRPMs EITHER a modular kernel with support for htb and dsmark OR capability to compile a kernel with support for htb and dsmark This article is neither comprehensive nor authoritative. The author solicits positive and negative feedback at <[email protected]>. Corrections, additions, and further examples are always welcome.
1. Introduction
Traffic Control using tcng and HTB HOWTO tcng. The tcsim tool is a traffic control simulator which accepts tcng configuration files and reads a control language to simulate the behaviour of a kernel sending and receiving packets with the specified control structures. Although tcsim is a significant portion of the tcng project, tcsim will not be covered here at all.
1. Introduction
2. Requirements
There are a few requirements in order for the kernel to support HTB and DSMARK, tc to support HTB and DSMARK, and tcng itself. Specifically, support for HTB in the kernel and tc is absolutely required in order for this tutorial to be remotely useful (refer to the title if htere is any doubt in your mind). DSMARK support is, strictly speaking, optional, although some examples (class selection path, in particular, but maybe others) may not operate without dsmark support.
2.2. tc requirements
The tc command is a part of the iproute2 utility suite. For general documentation on iproute2, see http://linuxip.net/ and the iproute2 manual. The software itself is available directly from Alexey Kuznetsov'z FTP archive but commonly also via packages supplied with your linux distribution. If your distribution can make use of RPMS, you can download this SRPM and compile it on your own system. If you need to compile iproute2 yourself, use the patch to tc from this tarball at Martin Devera's HTB site in order to provide support for HTB in tc. Your tc will also need to support dsmark, the diffserv marking mechanism. Fortunately, this is a simple change to the Config file from the iproute2 source package. Simply change TC_CONFIG_DIFFSERV=n to TC_CONFIG_DIFFSERV=y and recompile. The SRPM creates a tc binary with support for dsmark and for HTB, both of which are required for this example.
2. Requirements
3. Configuration examples
Examples shown here will be modified examples of downloadable configurations available in this directory. These examples can be used as standalone configuration files to be fed into a tcc parser, or they can be used in conjunction with the example SysV startup script. The startup script is a modification of a script posted on the LARTC mailing list by raptor. If you are going to use the above startup script, take a look at this example /etc/sysconfig/tcng:
Example 1. /etc/sysconfig/tcng
# tcng metaconfiguration file # (I never metaconfiguration file I didn't like) # # 20030315 created; MAB # 20030331 modified to allow ENVAR override; MAB # # this directory will hold all of the tcng configurations # used on this host # TCCONFBASEDIR=${TCCONFBASEDIR:/etc/sysconfig/tcngconfigs} # this is the active, desired tcng configuration # note, that, because tcng provides the #include construct, # the modularity of configuration can be built into the # configuration files in $TCCONFBASEDIR # TCCONF=${TCCONF:$TCCONFBASEDIR/global.tcc} tcstats=${tcstats:no} tcstats=${tcstats:yes} # will suppress statistical output # will throw the "s" option to tc
tcdebug=${tcdebug:0} # for typical startup script usage tcdebug=${tcdebug:1} # for a bit of information about what's happening tcdebug=${tcdebug:2} # for debugging information # # # an additional measure to take, you can override the default tc and tcc # command line utilities by specifying their pathnames here, for example: # # tc=/usr/local/bin/tc # tcc=/usr/local/tcng/bin/tcc # #
if tcp_sport == 22 && ip_tos_delay == 1 ; if tcp_sport == 554 || tcp_dport == 7070 ; PORT_SSH || tcp_dport == PORT_HTTP ; if 1 ;
/* section in which we configure the qdiscs and classes */ htb () { class ( rate 600kbps, ceil 600kbps ) { $ssh = class ( rate 64kbps, ceil 128kbps ) { sfq; } ; $audio = class ( rate 128kbps, ceil 128kbps ) { sfq; } ; $bulk = class ( rate 256kbps, ceil 512kbps ) { sfq; } ; $other = class ( rate 128kbps, ceil 384kbps ) { sfq; } ; } } } }
The tcng language provides support for Cstyle include directives which can include any file. Files are included relative to the current directory or the tcng library (normally /usr/lib/tcng/include). Strictly speaking, it is not necessary to #include ports.tc and fields.tc, because tcc will include these by default. The use of #include can allow for flexible definition of variables and inclusion of common traffic control elements. See also the tcng manual on includes. These are CPP directives. The #define can be used to create macros or constants. For more on their use, you should see the tcng manual on variables. The egress keyword is synonymous with the dsmark keyword. The example here uses class selection path. It is the use of the egress keyword in this configuration which requires dsmark support in the kernel and tc. Class selection path is one approach to traffic shaping. In class selection path, the packet is marked 3. Configuration examples 6
Traffic Control using tcng and HTB HOWTO (DiffServ mark) upon entry into the router. The router may take any number of actions or apply any number of policing, scheduling or shaping actions on the packet as a result of this initial classification. Consult the tcng manual on class selection path for further details. This example shows the use of names for the ports instead of numbers. This is one of the conveniences of tcng afforded by the automatic inclusion of ports.tc. The ports are named in accordance with IANA port names. See IANA's registered ports for these names or examine the file ports.tc. Names and numbers are equally acceptable and valid. Note this peculiar construct which classifies any packet which have not yet been classified. Any packet which has not been classified by the above classifiers is put into the class "$other" here. The if 1 construct can be used to classify the remainder of unclassified traffic. This is the creation of the root qdisc which is attached to device, eth0 in this case. Consult the reference material in the tcng appendix on queuing discipline parameters for valid parameters to each qdisc. Any qdisc parameters can be inserted into the parentheses in the same fashion as the class parameters further below in the example. If no parameters need be specified, the parentheses are optional. The top level class in this example sets the maximum bandwidth allowed through this class. Let's assume that eth0 is the inside network interface of a machine. This limits the total bandwidth to 600 kilobits per second transmitted to the internal network. The parameters rate and ceil should be familiar to anybody who has used HTB. These are HTB specific parameters and are translated properly by the tcc utility. See the table on tcng rate and speed specification. This is the assignment of a class to a variable. This is commonly done as part of class selection path. As suggested by Martin Devera on the HTB homepage, an embedded SFQ gives each class a fair queuing algorithm for distribution of resources to the contenders passing packets through that class. Note the absence of any parameters to the embedded queuing discipline. If no queuing discipline is specified for leaf classes, they contain the default, a pfifo_fast qdisc. The inclusion of a stochastic fair queuing qdisc in the leaf classes inhibits the ability of a single connection to dominate in a given class.
3. Configuration examples
This is the declaration of the meter to be used for classifying traffic. The underlying technology used to implement this meter is policing. See the tcng manual on meters for the different types of meters. This meter is a tworate threecolor meter, the most complex meter available in the tcng language. This meter returns the colors green, yellow and red, based on the rates offered in the committed and peak buckets. If the metered rate exceeds the committed rate, this meter will turn yellow, and if the metered rate exceeds the peak rate, this meter will turn red. The variable $meter can be operated on by functions applicable to the meter type. In this case, there are three functions available for testing $meter's state, trTCM_green, trTCM_yellow, and trTCM_red. For efficiency, consider also the accelerated counterparts. In this example, the IP 192.168.137.50 is specifically excluded from the policing control applied to traffic departing on eth0. Up to the committed information rate (cir), packets will pass through this class. Tokens will be removed from the cir/cbs bucket. The meter is green. Traffic flow exceeding the cir/cbs bucket will be classified here. The pir/pbs bucket (pir is peak information rate, pbs is peak burst size). This allows a particular flow to be guaranteed one class of service up to a given rate, and then be reclassified above that rate. The meter is yellow. Traffic flow exceeding the pir/pbs bucket will be classified here. A common configuration causes traffic to be dropped above peak rate, although traffic could be reclassified into a besteffort class from a guaranteed class. The meter is red.
3. Configuration examples
4. Miscellaneous Notes
Thankfully, tcng does away with one of the minor annoyances of tc. The following table maps the syntax and convention of these tools with English equivalents.
Table 1. Speed/Rate syntax: tcng vs. tc tcng English tc bps bit(s) per second bit Bps byte(s) per second bps (argh!) kbps kilobit(s) per second kbit kBps kilobyte(s) per second kbps Mbps megabit(s) per second mbit or Mbit MBps megabyte(s) per second mbps or Mbps pps packet per second ?? Note that this means a slight adjustment for longtime users of tc, but a much better choice for intuitive usablity for English speakers. For example, we can use conventional expressions of rate in tcng configurations: 100Mbps, 128kbps, and even 2Gpps. See also the tcng manual on units. In order for traffic control to be effective, it is important to understand where the bottlenecks are. In most cases, you'll want to perform the traffic control at or near the bottleneck.
4. Miscellaneous Notes
10