A common scenario on Delhi roads: a minister on the move in his Ambassador car, led and followed by cops and Commando Jeeps, with traffic cops ensuring that the minister reaches his venue on time. The cops do so by blocking or delaying traffic on the road to allow the minister's crew. This is very similar to how network traffic can be prioritized on a computer network using a Linux box. This article will mention some free packages or scripts to realize QoS on your network. The scripts make use of the tc (traffic control) features of the Linux Kernel. It can be used to queue and delay the low-priority traffic to ensure the smooth flow of higher-priority traffic.
Note that the mentioned scripts should be run on a Linux router with two network interfaces: One connected to the internal network and the second connected to the external network, supposedly the Internet.
Snapshot | |
Applies to | Network administrators |
Usp | Controlling badwidth usage on network |
Links | www.lartc.org |
CBQ
CBQ (Class Based Queueing) is a queuing algorithm that can be used (as a script) on a Linux router or gateway machine. The network traffic is divided into classes based on the type of service (FTP, SMTP or HTTP), the destination network for the traffic or the destination host (network node) for the traffic. Each class is assigned a FIFO (First In First Out) based queue that is used to queue low-priority traffic. Each queue is assigned a specific amount of bandwidth as defined in the class.
To implement a CBQ-based traffic control on your network you can use a pre-written script called cbq.init. Download the latest version of this script from http://sourceforge.net/projects/cbqinit. Copy the script in the directory /etc/rc.d/init.d as
cbq.init.
Next, comes the task of defining the class for each traffic. This is done by dropping text files corresponding to each class in the directory /etc/sysconfig/cbq. Create this directory if it does not exist on your Linux machine. The following class is defined to limit smtp traffic:
DEVICE=eth0,10Mbit,1Mbit
RATE=20Kbit
WEIGHT=2Kbit
PRIO=5
RULE=:25,192.168.1.0/24
For DEVICE, the syntax is:
DEVICE=
RULE=:
This specifies the traffic flowing from anywhere on port 25 on the network 192.168.1.0. For more examples on specifying RULE read through the cbq.init file. Save this file as cbq-10.smtp-network in the directory /etc/sysconfig/cbq. Assign executable permissions to the cbq.init script (using chmod +x command) and run the cbq script as:
/etc/rc.d/init.d/cbq.init start
Define a similar class for the second network interface.
HTB
HTB (Hierarchical Token Bucket) uses hierarchical class-based queuing. It is hierarchical in the sense that it has a root class and other classes are child to the root. From the implementation point of view, we don't need to dwell much into the intricacies as using it is similar to using CBQ. The major difference between CBQ and HTB is that in CBQ when you allocate certain bandwidth to class, it cannot use more than the allocated bandwidth even if bandwidth is available. However, in HTB you can specify how much extra bandwidth (if available) can be used by a class. This is done by specifying a parameter called CEIL (as we will see later).
Download the htb.init script from http://sourceforge.net/projects/htbinit/ and copy it in the directory /etc/rc.d/init.d as htb.init. Create a directory named htb under /etc/sysconfig/. Download the example files found under the same URL. Extract the archive of the examples to /etc/sysconfig/htb. The directory will now contain the following files:
eth0
eth0-2.root
eth0-2:10.www
eth0-2:20.smtp
eth0-2:30.dfl
The eth0 file corresponds to the network interface on which HTB controls the traffic. Hence, to shape the traffic on each network interface, you must have a corresponding file. The eth0 file will contain a line as:
DEFAULT=30
In HTB you specify a default class to which the remaining, unreserved bandwidth goes to. In this case, the remaining bandwidth is allocated to class 30 (we will explain this below). As explained above HTB is based on hierarchy structure. The file eth0-2.root is the root class and specifies the maximum bandwidth. The '2' in the filename specifies the class id. Next, comes specifying the bandwidth for each class. This is done in separate files with filenames containing the network interface name (eth0), root's class id and its own class id. For example: eth0-2:20.smtp. Here the class id is 20 (separated by the : ).
Also, the name of the service can be specified with a dot prefix for better description. The content of this file is more or less same as in case of cbq. The difference is the CEIL parameter using which we can specify the extra available bandwidth that can be used by the class. The class file eth0-2:30.dfl is the default class. Open the file in a text editor to notice that it does not define a rule that means it applies to all traffic.
Assign executable permissions to the htb.init script (using chmod +x command) and run the script as:
/etc/rc.d/init.d/htb.init start
Define similar classes for the other types of services and network interfaces.
Shekhar Govindarajan
IT4Enterprise