Sunday 24 April 2011

Bandwidthd how to..: hierarchical token bucket HTB (Bandwidth Allocatio...

Bandwidthd how to..: hierarchical token bucket HTB (Bandwidth Allocatio...: "HTB-tools is a software suite with several tools that help simplify the difficult process of bandwidth allocation, for both upload and down..."

hierarchical token bucket HTB (Bandwidth Allocation)

HTB-tools is a software suite with several tools that help simplify the difficult process of bandwidth allocation, for both upload and download traffic: generate and check configuration files, real time traffic overview for each separate client.

Features:
* bandwidth limitation using public IP addresses, using the two configuration files for upload and download
* bandwidth limitation using private IP addresses (SNAT), using a single configuration file
* match mark
* match mark in u32
* metropolitan/external limitation
* menu based management software for configuration and administration of HTB-tools

Hierarchical Token Bucket ( HTB ) successfully replaces Class Based Queueing ( CBQ ) due to the precise and easy to understand way it operates. The difference from CBQ is that the bandwidth is allocated to one (or more) classes, and when the class's allocated bandwidth is exceeded, it can (temporarily) borrow unused bandwidth from another class. Moreover, unlike CBQ, you can allocate several clients to one class. Using HTB-tools all classes and all clients can be defined in a configuration file.

To better understand how it works, let's suppose that we have a bandwidth of 256kbps shared by 4 clients (with routable IP addresses), each client having a guaranteed minimum of 48kbps and a guaranteed maximum of 64kbps upload/download.

After HTB-tools is installed, you can proceed to editing the configuration files.
Assuming that eth0 is the interface to your provider and eth1 the interface to your clients, on the eth0 interface you can only limit the upload (only if you use public IP addresses) and on eth1 only the download, because you can only control the debit of the packages that leave the router ( the information that enters the router is imposed by the external environment ).
For upload limitations/guarantees you have to edit the file /etc/htb/eth0-qos.cfg and for download limitations/guarantees you have to edit the file /etc/htb/eth1-qos.cfg


Configuration files for HTB-tools

Remarks:
- the format of the configuration files resembles the format of bind's configuration files;
- the bandwidth users are divided into classes;
- these classes can not share the bandwidth among themselves;
- the members of a class (clients) can share the same bandwidth according to the parameters specified in the configuration file;
- a class may contain one or more clients;
- a special class is the default class, that defines the bandwidth allocated to those which are not included in any class;
- the transfer rate is specified in kbit;
- the lines having # as the first character are considered comments.
- you can NOT specify the fields src and dst for a class; these fields are only defined for clients.

The class syntax:
bandwidth 192; - the guaranteed minimum for the class; represents the minimum total sum guaranteed for the clients of the class;
limit 256; - the maximum transfer rate for the class;
burst 2; - the maximum number of kbits sent at once by the class;

!!! NOTE:
If you don't set the burst value properly, the limits might not work
correctly. Eg: for the limit 10000kbit you must use a burst of 12 kbit.
THIS IS AVAILABLE ONLY IF YOU USE PUBLIC IPs !


burst 0; - ONLY for HTB-tools beta4 - if burst is set to 0 then HTB-tools will calculate the burst value automatically. This is valid for clients side too.
priority 1; - priority of the class; there are 8 priority levels: from 0 to 7. The packets are served in ascending order of priority. For example, if the priority is 0 then the packets will first be served to this class, on the other hand if the priority is 4 then the packets will first be sent to the class having priority 0 and only then to the class having priority 4;
que sfq; - we can specify the qdisc for a class, if not specified the default is pfifo limit 5.
(or esfq;)
For more details about esfq, please see [link]


As specified above, we have a bandwidth of 256kbps that we want to allocate to 4 clients, both for upload and for download. For this, we first define the class:

class class_1 {

bandwidth 192;

limit 256;

burst 2;

priority 1;

que sfq;


The clients syntax:
bandwidth 48; - guaranteed minimum for the client;
limit 64; - maximum transfer rate for the client;
burst 2; - maximum number of kbits sent to the client at once;
mark 20; - if only mark is specified, without dst/src, then fw will be used; if the source/destination is specified, then u32 will be used with the possibility to mark (match_in_u32);
dst or src can be used together in the configuration file only for clients; if you wish to limit the upload then you must use src, and if you wish to limit the download then you must use dst; in our example above we have used limitation/allocation for download and the configuration file will be eth1-qos.cfg;
priority 1; - client priority;
- in the case of a single IP, the representation looks like this: 192.168.1.10/32;
- in the case of a class, the representation looks like this: 192.168.1.0/24;
- if you wish to apply the limitation to a particular port for a single IP:
192.168.1.10/32 80; - limits the traffic to port 80 (http)
- if you wish to apply the limitation to a particular port for an IP class:
192.168.1.0/24 21; - limits the traffic to port 21 (ftp)

The next step is to define the 4 clients. For each client a minimum of 48kbit and a maximum of 64kbit will be allocated.

client client1 {


bandwidth 48;

limit 64;

burst 2;

priority 1;

mark 20;

dst {



192.168.100.4/32;


};

};


client client2{


bandwidth 48;

limit 64;

burst 2;

priority 1;

mark 20;

dst {



192.168.100.5/32;


};

};


client client3 {


bandwidth 48;

limit 64;

burst 2;

priority 1;

mark 20;

dst {



192.168.100.8/32;


};

};


client clien4 {


bandwidth 48;

limit 64;

burst 2;

priority 1;

mark 20;

dst {



192.168.100.10/32;


};

};


class default { bandwidth 8; };
};
};



Only for HTB-tools beta4
Another new feature is the "upload" function, which would assist in managing the
upload, if you use SNAT (non-routeable IP addresses). This can be done using the same configuration file that you would use to manage the download.
WARNING: this type of shaping (ingress) drops the packets that are over the
limit, which generates additional traffic when entering the interface. For this reason it is
recommended to have the shaping machine in the same LAN with the shaped machines.
The "upload" option uses dst but don't work with mark.

Configuration examples (client side):
client client {


bandwidth 350;

limit 1200;

burst 0; # new

priority 1;

upload 300; #( in kbits) <- new

dst {

192.168.100.5/32;

};
};



client client {


bandwidth 350;

limit 1200;

burst 0; # new

priority 1;

upload 300; #( in kbits) <- new

dst {

192.168.100.30/32;

};
};

UPLOAD option available from 0.3.0-beta4 works only with dst.
You can specify mark or src but this options will NOT
affect outgoing trafic.


The next step is checking the configuration file using the q_checkcfg command:
q_checkcfg /etc/htb/eth1-qos.cfg

Default bandwidth: 8
Class class_1, CIR: 192, MIR: 256
** 4 clients, CIR2: 192, MIR2: 256
1 classes; CIR / MIR = 192 / 256; CIR2 / MIR2 = 192 / 256

- the configuratin files are syntactically correct.

The rc.htb init script
------------------------
With this script you can start/stop/monitor/generate/restart the limitations/guarantees for upload/download or for both together.


To start at boot the bandwidth policies for upload and download you must add to /etc/rc.d/rc.local the line:
/etc/rc.d/rc.htb start

If you wish to limit/guarantee the bandwidth only for download then you must use the command:(this example is works on Slackware linux)
/etc/rc.d/rc.htb start_eth1


If you wish to limit/guarantee the bandwidth only for upload then you must use the command:
/etc/rc.d/rc.htb start_eth0


If you wish to generate the traffic rules script you can use:
for eth0
[center]/etc/rc.d/rc.htb gen_eth0[/center]


for eth1

[center]/etc/rc.d/rc.htb gen_eth1[/center]




q_show allows you to see in real-time the traffic and bandwidth usage for each client (download). In order to see realtime trafic run:
/etc/rc.d/rc.htb show_eth1

...and the result is:

class_1 224.802192256
client_1 62.2514864
client_2 51.0514864
client_348.2514864
client_463.2514864
_default_0000


If you like to pass some options to q_show , please see the q_show(8) man page.

rc.htb complete options and usage
----------------------------------------
/etc/rc.d/rc.htb start | stop | restart |
start_eth0 | stop_eth0 | restart_eth0 |
start_eth1 | stop_eth1 | restart_eth1 |
start_eth2 | stop_eth2 | restart_eth2 |
show_eth0 | show_eth1 | show_eth2 |
gen_eth0 | gen_eth1 |

Web q_show
---------------
Web q_show is a tool that displays in a webpage the traffic status and allocated bandwidth for each class/client according to the configuration file.
A cron job collects the traffic data in a .log file, namely q_show.log. From here on it is the job of .php script (q_show.php) that parses the file and displays the content in a webpage.

Configuration
----------------
Before starting anything, you must have php installed and a working
web server (apache). Let's assume that you have domain.ro and the
default directory for the webpages is /var/www/htdocs/. The default
directory specified at the install time (with "make install_web") will
have a folder called webhtb with q_show.php script.

Before adding the line to crontab, set:
- the time interval (*/1 every minute or */5 every 5 minutes etc) at which to generate the traffic logs in the file;
- the correct path to the configuration file
- the interface, ethx where x = 0, 1, 2 ... etc, the interface you want to monitor;
- ethx-qos.cfg the configuration file for the monitored interface;
- the path to the webhtb directory;

Add to crontab (crontab -e):

*/1 * * * * /sbin/q_show -i eth1 -f /etc/htb/eth1-qos.cfg -1 > /var/www/htdocs/webhtb/q_show.log

You can see the traffic at the address [link] .

Web HTB-tools configuration files generator
---------------------------------------------------
The web q_show install will get you whtbcfg in the same directory as webhtb (i.e.: /var/www/htdocs/whtbcfg). You will need a functional httpd (apache) with php support to be able to use it.

php.ini settings
------------------
Please set register_globals to ON in php.ini and disable error_reporting like
this:


register_globals = on
#error_reporting = E_ALL & ~E_NOTICE


After modifying php.ini, please restart the httpd server and point your browser to [link] to be able to generate configuration files.

Possible configurations in HTB-tools beta 4
--------------------------------------------------
Please see the file cfg/possible_configs

INSTALL

Requirements:
------------------
- GNU/Linux distribution;
- GCC compiler;
- Iproute2 (the latest version is recommended from here);
- Linux Kernel 2.4.32 or 2.6.15.1 ([link]);
- dialog for HTB-tools 0.3.0 (the latest version from here)
- Apache and php for web q_show and web HTB-tools config file generator.
- flex version 2.5.4a.

And now, on with the show ...


Linux distribution
--------------------
You must have a functional GNU/Linux distribution.
I received feedback from users who successfully run HTB-tools on:
Slackware, Gentoo, Fedora Core, Red Hat, Debian, Suse.
If you have tested and successfully run HTB-tools on other distributions then what I mentioned, please send me an e-mail to update this section.

GCC compiler
----------------
All Linux distributions have a GCC compiler included.
If you do not have GCC installed, please see your distribution manual or documentation for how to install GCC. You need this to compile HTB-tools.

Linux Kernel 2.4.32 - [link]
If you compile the Kernel from sources, you will need to select the
following options for HTB-tools-0.2.7 and HTB-tools 0.3.0:
#
# QoS and/or fair queueing
#
CONFIG_NET_SCHED=y
CONFIG_NET_SCH_CBQ=m
CONFIG_NET_SCH_HTB=m
CONFIG_NET_SCH_CSZ=m
CONFIG_NET_SCH_HFSC=m
CONFIG_NET_SCH_PRIO=m
CONFIG_NET_SCH_RED=m
CONFIG_NET_SCH_SFQ=m
CONFIG_NET_SCH_TEQL=m
CONFIG_NET_SCH_TBF=m
CONFIG_NET_SCH_GRED=m
CONFIG_NET_SCH_NETEM=m
CONFIG_NET_SCH_DSMARK=m
CONFIG_NET_SCH_INGRESS=m
CONFIG_NET_QOS=y
CONFIG_NET_ESTIMATOR=y
CONFIG_NET_CLS=y
CONFIG_NET_CLS_TCINDEX=m
CONFIG_NET_CLS_ROUTE4=m
CONFIG_NET_CLS_ROUTE=y
CONFIG_NET_CLS_FW=m
CONFIG_NET_CLS_U32=m
CONFIG_NET_CLS_RSVP=m
CONFIG_NET_CLS_RSVP6=m
CONFIG_NET_CLS_POLICE=y

Linux Kernel 2.6.15.1 - [link]
If you compile the Kernel from sources, you will need to select the
following options for HTB-tools-0.2.7 and HTB-tools 0.3.0:
#
# QoS and/or fair queueing
#
CONFIG_NET_SCHED=y
CONFIG_NET_SCH_CLK_JIFFIES=y
# CONFIG_NET_SCH_CLK_GETTIMEOFDAY is not set
# CONFIG_NET_SCH_CLK_CPU is not set

#
# Queueing/Scheduling
#
CONFIG_NET_SCH_CBQ=m
CONFIG_NET_SCH_HTB=m
CONFIG_NET_SCH_HFSC=m
CONFIG_NET_SCH_PRIO=m
CONFIG_NET_SCH_RED=m
CONFIG_NET_SCH_SFQ=m
CONFIG_NET_SCH_TEQL=m
CONFIG_NET_SCH_TBF=m
CONFIG_NET_SCH_GRED=m
CONFIG_NET_SCH_DSMARK=m
CONFIG_NET_SCH_NETEM=m
CONFIG_NET_SCH_INGRESS=m
#
# Classification
#
CONFIG_NET_CLS=y
CONFIG_NET_CLS_BASIC=m
CONFIG_NET_CLS_TCINDEX=m
CONFIG_NET_CLS_ROUTE4=y
CONFIG_NET_CLS_ROUTE=y
CONFIG_NET_CLS_FW=m
CONFIG_NET_CLS_U32=m
CONFIG_CLS_U32_PERF=y
CONFIG_CLS_U32_MARK=y
CONFIG_NET_CLS_RSVP=m
CONFIG_NET_CLS_RSVP6=m
CONFIG_NET_EMATCH=y
CONFIG_NET_EMATCH_STACK=32
CONFIG_NET_EMATCH_CMP=m
CONFIG_NET_EMATCH_NBYTE=m
CONFIG_NET_EMATCH_U32=m
CONFIG_NET_EMATCH_META=m
CONFIG_NET_EMATCH_TEXT=m
CONFIG_NET_CLS_ACT=y
CONFIG_NET_ACT_POLICE=m
CONFIG_NET_ACT_GACT=y
CONFIG_GACT_PROB=y
CONFIG_NET_ACT_MIRRED=m
CONFIG_NET_ACT_IPT=m
CONFIG_NET_ACT_PEDIT=m
CONFIG_NET_ACT_SIMP=m
CONFIG_NET_CLS_IND=y
CONFIG_NET_ESTIMATOR=y

NOTE ========================================================
To successfully use mark_in_u32 you MUST use at least the kernel 2.6.11.
!!! IT IS BEST TO USE THE LATEST VERSION!!!
=============================================================


Iproute - [link]
----------
Before compiling HTB-tools 0.2.7 or HTB-tools 0.3.0, you need proute2-2.6.10-ss050124 or a greater version to be installed.
!!! IT IS BEST TO USE THE LATEST VERSION !!!
After downloading and extracting the sources, execute "make" followed by "make install" commands. After compilation you must copy the tc binary to /sbin directory.

HTB-tools 0.2.7 or HTB-tools 0.3.0 ( [link] )
------------------------------------------------
HTB-tools Bandwidth Management Software is a suite of tools that help simplify the difficult process of bandwidth allocation, for both upload and download traffic, using the Linux kernel's HTB facility. It can generate and check configuration files. It also provides a real time traffic overview for each separate client.


The set of HTB-tools includes:
q_parser : reads a configuration file (the file defines classes,
clients, bandwidth limits) and generates an HTB settings
script;

q_checkcfg: check configuration files;

q_show : displays in the console the status of the traffic and the
allocated bandwidth for each class/client defined
in the configuration file;

q_show.php: displays in a webpage the status of the traffic and the
allocated bandwidth for each class/client defined in the
configuration file;

wHTB-tools_cfg_gen: create and generate configuration files from
a web page (only in HTB-tools 0.3.0)

htbgen : generate configuration files from bash;

Compile and install:
Download the sources HTB-tools-0.2.7.tar.gz or HTB-tools.0.3.0.tar.gz

- extract and compile the sources:

* for HTB-tools 0.2.7:
tar -zxvf HTB-tools-0.2.7.tar.gz
cd HTB-tools-0.2.7
make


* for HTB-tools 0.3.0
tar -xvf HTB-tools.0.3.0.tar.gz
cd HTB-tools.0.3.0
make

After compilation is done and if you install HTB-tools for the first time then you must run the following command:

make full

- this is recommended if you install HTB-tools for the first time; will install the binaries q_parser, q_show, q_checkcfg, htb, htbgen in the /sbin directory, and the two default configuration files: eth0-qos.cfg for upload and eth1-qos.cfg for download in /etc/htb; also will install the rc.htb init script, web htb and wHTBcfg. For web htb please read the docs/README-web-en.

Upgrade
----------
You can upgrade HTB-tools using the fallowing command:
make install

- this is will install (upgrade) the binaries q_parser, q_show, q_checkcfg, htb, htbgen in the /sbin directory,

Friday 22 April 2011

Installation and Configuration of Bandwidthd

BANDWIDTHD...

INTRODUCTION:
It is important to know traffic usage of your client if you're a Linux network administrator. You can monitor your client in text mode, graphic mode or html exported like mrtg, cacti or bandwidthd and one of my favorite bandwidth monitor is bandwidthd.

BandwidthD tracks usage of TCP/IP network subnets and builds html files with graphs to display utilization. Charts are built by individual IPs, and by default display utilization over 2 day, 8 day, 40 day, and 400 day periods. Furthermore, each ip address's utilization can be logged out at intervals of 3.3 minutes, 10 minutes, 1 hour or 12 hours in cdf format, or to a backend database server. HTTP, TCP, UDP, ICMP, VPN, and P2P traffic are color coded.



Below are the setups to configure Bandwidthd..

Download Bandwidthd form http://sourceforge.net/projects/bandwidthd/files/

[root@arslan]# tar xvfz bandwidthd-2.0.1.tgz
[root@arslan]# cd bandwidthd
Configure and install the Bandwidthd source:
[root@arslan]# ./configure && make install

Please make sure you have:
libpcap from http://www.tcpdump.org/
libpng from http://www.libpng.org/
libgd from http://www.boutell.com/gd/

Now Edit /usr/local/bandwidthd/etc/bandwidthd.conf
to suit your network environment.

My Example is :


subnet 192.168.0.0/24
subnet 192.168.1.0/24
# Device to listen on
# Bandwidthd listens on the first device it detects
# by default.  Run "bandwidthd -l" for a list of
# devices.
dev "eth0"
#dev "eth1"


 Now Start Bandwidthd
/usr/local/bandwidthd/bandwidthd

You  can't see your graphic report until you set /usr/local/bandwidthd/htdocs/ folder to set as apache virtual directory. Add below line to your apache config file.

Alias /bandwidthd "/usr/local/bandwidthd/htdocs"
<Directory "/usr/local/bandwidthd/htdocs">
Order Allow,Deny
Allow from All
</Directory>

now go to browser and write http://your-server-ip/bandwidthd

ALLHAMDULILLAH  WE HAVE DONE IT......


Regards

Muhammad Arslan
Cell #             +923215611763