1 TITLE: Iproute2 and traffic shaping
2 LFS VERSION: Kernel 2.4.20 and later versions
3 AUTHOR: Marcos Zapata <zeta11@hotpop.com>
7 How to compile iproute2 and some basic traffic shaping scripts for your LFS.
10 Most linux distributions are starting to provide the iproute2 package,
11 because of the new redesigned network subsystem implemented in kernels 2.2 and
12 up. The old comands 'ifconfig' and 'route' are now been deprecated because of
13 their faulty and unexpected behaviour under these kernels.
14 It was written by Alexey Kuznetsov, who also wrote the routing code of the
15 kernels 2.2 and up. This new routing and filtering code provides many advantages
16 and features that weren't available before, and ip/tc are the tools to handle
17 it. I won't be explaining traffic shaping with CBQ and HTB, just how to get them
18 for your LFS. You'll find some links in references.
21 This package requires db. If you don't have it, you can get it at
22 www.sleepycat.com. For example: db-4.1.25 (actually, I've been using db-4.0.14
23 without any problem), from:
25 http://www.sleepycat.com/update/snapshot/db-4.1.25.tar.gz
27 tar -zxvf db-4.1.25.tar.gz
29 ./configure --prefix=/usr --enable-compat185
33 now, we can continue with iproute2.
37 ftp://ftp.inr.ac.ru/ip-routing/
39 For the purposites of this hint, I'll be using:
40 iproute2-2.4.7-now-ss020116-try.tar.gz and kernel-2.4.20.
42 Starting with kernel-2.4.20 you can find HTB and CBQ packet schedulers. If you
43 plan to use an older kernel (not recommended) you'll need to apply a patch to
44 support it. Either way we'll need to get the patch for iproute to handle HTB:
46 http://luxik.cdi.cz/~devik/qos/htb/v3/htb3.6-020525.tgz
48 The compiling and instalation of these tools is very strait-forward:
50 tar -zxvf htb3.6-020525.tgz
51 tar -zxvf iproute2-2.4.7-now-ss020116-try.tar.gz
55 patch -Np1 < ../htb3.6_tc.diff
57 #if you want, edit Makefile to change some values like KERNEL_INCLUDE or
58 #SBINDIR. You shouldn't need to if you've built LFS.
63 If you didn't edit Makefile, the tools should be in /sbin, the conf. files in
64 /etc/iproute2 and the docs in /usr/doc/iproute2. Sadly, it doesn't include any
65 man pages, you'll need latex and sgmltools to read the docs. I advised you to
66 read them, to fully understand and use these powerful tools.
68 Ok, now that we have ip and tc with HTB support we need to recompile the kernel.
69 With 'make menuconfig' under 'Networking options', enable netlink and filtering
70 support, also tunneling and multicasting. To enable traffic shaping, enable all
71 the options in 'Qos and/or fair queueing' as modules. The exact options names
72 may vary for the different kernel versions. Compile the bzImage and modules,
73 install, and remember to add this new kernel to your lilo or grub conf. to start
74 with this new configuration.
76 If you built LFS 3.1 (I guess it could work with previous versions) you need to
77 change the boot scripts: localnet and network in /etc/rc.d/init.d.
81 look for 'loadproc ifconfig lo 127.0.0.1' in start), replace it with:
82 ip addr add 127.0.0.1/8 dev lo
85 look for 'loadproc ifconfig lo down' in stop), replace it with:
87 ip addr del 127.0.0.1 dev lo
89 look for 'ifconfig lo' in status), replace it with:
94 look for 'route add default gateway $GATEWAY metric 1 dev $GATEWAY_IF',...:
95 ip route add default via $GATEWAY dev $GATEWAY_IF
97 look for 'route del -net default', replace it with:
100 Also, the scripts in /etc/sysconfig/network-devices: ifdown and ifup.
104 look for 'ifconfig $1 down', replace it with:
110 look for 'ifconfig $1 $IP netmask $NETMASK broadcast $BROADCAST',....:
112 ip addr add $IP/$NETMASK broadcast $BROADCAST dev $1
114 As you can see the ip command is very simple to use, and it's very similar to
115 ifconfig and route. The only thing that changes is the NETMASK.
117 You need to change NETMASK in /etc/sysconfig/network-devices/ifconfig.eth* :
118 from 255.255.255.255 to 32
119 .. 255.255.255.0 to 24
123 so that ifconfig.eth0 (for example) looks something like this:
127 BROADCAST=192.168.100.255
130 Now, let's start with some traffic shaping scripts: cbq.init and/or htb.init
132 Both CBQ and HTB help you to control the use of the outbound bandwidth on a
133 given link. Both allow you to use one physical link to simulate several slower
134 links and to send different kinds of traffic on different simulated links.
137 You can get it at http://sourceforge.net/projects/cbqinit. From one of the
140 http://heanet.dl.sourceforge.net/sourceforge/cbqinit/cbq.init-v0.7.2
142 mv cbq.init-v0.7.2 cbq.init
144 cp cbq.init /etc/rc.d/init.d
145 mkdir /etc/sysconfig/cbq
147 Remeber to add the symlinks in /etc/rc.d/rc*.d. All the explanations of this
148 tool are in script: how it works, parameters, and a sample.
151 It's derived from cbq.init that allows for easy setup of HTB-based traffic
152 control on Linux. You can get it at http://sourceforge.net/projects/htbinit. One
155 http://keihanna.dl.sourceforge.net/sourceforge/htbinit/htb.init-v0.8.4
157 mv htb.init-v0.8.4 htb.init
159 cp htb.init /etc/rc.d/init.d
160 mkdir /etc/sysconfig/htb
162 Remember to add the symlinks in /etc/rc.d/rc*.d. Just like cbq, you can find all
163 you need inside the script.
165 You can use either one of them. CBQ is older, but it's still widely used. HTB is
166 easier and more accurate.
169 I have a very crapy dsl conection and it really help me: mantaining low latency
170 for interactive traffic and surfing while uploading.
173 http://lartc.org/wondershaper/wondershaper-1.1a.tar.gz
175 tar -zxvf wondershaper-1.1a.tar.gz
178 There are two versions of the script, for CBQ and HTB. To start, you'll need to
179 modify at the beginning: DOWNLINK, UPLINK and DEV. You'll also find a README in
180 the same directory, please read it, it will help you understand what it really
181 does and how to fine tunning it. Afterwards, to get them ready:
183 cp wshaper wshaper.htb /usr/sbin
185 That's it. You can reboot now and start using your LFS with these new tools.
186 You shouldn't have much trouble setting it up. Good luck.
190 http://lartc.org/lartc.txt Linux Advanced Routing & Traffic Control:
191 http://luxik.cdi.cz/~devik/qos/htb/ HTB Home page
195 Daniel Thaler <daniel@dthaler.de>: db headers, tc (wondershaper)
196 lfs@vs.megalink.ru: db headers
197 DJ Lucas <dj_me@swbell.net>: db headers
198 Samual Walters <saltwater@madasafish.com>: db headers
199 Diego Saravia <dsa@unsa.edu.ar>
200 ( Sorry for taking so long to correct the hint. )
204 EXTRA: Automatic generation of broadcast addresses with LFS
205 I was not sure to include this but here it goes, if it can be of any use...
206 I was tired of calculating broadcast addresses, so i decided to modify the ifup
207 script from LFS to do just that. It's not very clean and surely not the
208 paradigm of programming but it works just fine. You've been warned!
210 You need bc. Get it at:
212 ftp://ftp.gnu.org/gnu/bc/bc-1.06.tar.gz
214 tar -zxvf bc-1.06.tar.gz
216 ./configure --prefix=/usr
222 http://ftp.debian.org/debian/pool/main/r/rpncalc/rpncalc_1.33.3.tar.gz
224 tar -zxvf rpncalc_1.33.3.tar.gz
226 ./configure --prefix=/usr
228 make MKINSTALLDIRS=mkinstalldirs install
232 ---/etc/sysconfig/network-devices/ifup---
236 source /etc/sysconfig/rc
238 source $network_devices/ifconfig.$1
240 if [ -f $network_devices/ifup-$1 ]
242 $network_devices/ifup-$1
246 echo "IP variable missing for ifconfig.$1, cannot continue"
252 echo -n "NETMASK variable missing for ifconfig.$1, "
253 echo "using 255.255.255.0"
254 NETMASK=255.255.255.0
257 if [ "`echo "$NETMASK" | grep "\."`" ]; then
259 for i in `seq 1 4`; do
260 OC=`echo "$NETMASK" | cut -d'.' -f$i`
261 for j in `seq 0 7`; do
262 BIN=`echo "256-2^$j" | bc`
263 if [ $BIN == $OC ]; then
276 if [ $MASK -lt 24 ]; then
280 DIF=`echo "256-2^(8-$MASK%8)" | bc`
281 for i in `seq 1 4`; do
283 if [ $MSK -gt -1 ]; then
286 if [ $MSK -lt -8 ]; then
289 NETMASK=$NETMASK.$DIF
293 NETMASK=`echo "$NETMASK" | sed -e "s/^.//"`
296 for i in `seq 1 4`; do
297 OC=`echo "$IP" | cut -d'.' -f$i`
298 OC2=`echo "$NETMASK" | cut -d'.' -f$i`
299 BROADCAST="$BROADCAST.`echo "$OC 255 $OC2 xor or" | rpncalc | cut -d' ' -f3`"
301 BROADCAST=`echo "$BROADCAST" | sed -e "s/^.//"`
303 # Calculo automatico solo para redes C
304 CAN=`echo "2^($MASK-24)" | bc`
305 OCTIP=`echo "$IP" | cut -d'.' -f4`
306 OCT3IP=`echo "$IP" | cut -d'.' -f-3`
309 for i in `seq 1 $CAN`; do
310 RED=`echo "256/$CAN*($i-1)" | bc`
311 BROAD=`echo "$RED+256/$CAN-1" | bc`
312 if [ $OCTIP -gt $RED -a $BROAD -gt $OCTIP ]; then
313 BROADCAST="$OCT3IP.$BROAD"
321 echo "Cannot calculate broadcast for ifconfig.$1, something is wrong"
322 echo "Please check your IP=$IP and NETMASK=$MASK variables"
326 echo "Bringing up the $1 interface..."
328 ip addr add $IP/$MASK broadcast $BROADCAST dev $1
332 ---/etc/sysconfig/network-devices/ifup---
335 This script will calculate the correct broadcast address from the ip address
336 and netmask. It will work with all kinds of classes: A, B and C. Although,
337 it will only "check" for a valid broadcast address from /24 to /32 and as you
338 can see, you can now use NETMASK in ifconfig.eth* with a full address like
339 255.255.255.192 or 26. To use it just comment out BROADCAST in ifconfig.eth*.