Typo
[linux_from_scratch_hints.git] / OLD / iproute2.txt
bloba8c301437b3e5993fe43c61625ae03b3fb83c54f
1 TITLE:          Iproute2 and traffic shaping
2 LFS VERSION:    Kernel 2.4.20 and later versions
3 AUTHOR:         Marcos Zapata <zeta11@hotpop.com>
4 VERSION:                2003/08/20
6 SYNOPSIS:
7     How to compile iproute2 and some basic traffic shaping scripts for your LFS.
9 HINT:
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
28 cd db-4.1.25/dist
29 ./configure --prefix=/usr --enable-compat185
30 make
31 make install
33 now, we can continue with iproute2.
36 Where to download it?
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
52 cd iproute2
54 #apply the patch
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.
60 make
61 make install
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.
79 localnet:
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
83 ip link set lo up
85 look for 'loadproc ifconfig lo down' in stop), replace it with:
86 ip link set lo down
87 ip addr del 127.0.0.1 dev lo
89 look for 'ifconfig lo' in status), replace it with:
90 ip addr show lo
92 network:
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:
98 ip route del default
100 Also, the scripts in /etc/sysconfig/network-devices: ifdown and ifup.
102 ifdown:
104 look for 'ifconfig $1 down', replace it with:
105 ip addr flush dev $1
106 ip link set $1 down
108 ifup:
110 look for 'ifconfig $1 $IP netmask $NETMASK broadcast $BROADCAST',....:
111 ip link set $1 up
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
120  ..  255.255.0.0     to 16
121  ..  255.0.0.0       to 8
123 so that ifconfig.eth0 (for example) looks something like this:
124 ONBOOT=yes
125 IP=192.168.100.254
126 NETMASK=24
127 BROADCAST=192.168.100.255
130 Now, let's start with some traffic shaping scripts: cbq.init and/or htb.init
131 and wondershaper.
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.
136 cbq.init:
137 You can get it at http://sourceforge.net/projects/cbqinit. From one of the
138 mirrors at:
140 http://heanet.dl.sourceforge.net/sourceforge/cbqinit/cbq.init-v0.7.2
142 mv cbq.init-v0.7.2 cbq.init
143 chmod a+x 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.
150 htb.init:
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
153 of the mirros:
155 http://keihanna.dl.sourceforge.net/sourceforge/htbinit/htb.init-v0.8.4
157 mv htb.init-v0.8.4 htb.init
158 chmod a+x 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.
168 Wondershaper:
169 I have a very crapy dsl conection and it really help me: mantaining low latency
170 for interactive traffic and surfing while uploading.
171 We can get it at:
173 http://lartc.org/wondershaper/wondershaper-1.1a.tar.gz
175 tar -zxvf wondershaper-1.1a.tar.gz
176 cd wondershaper-1.1a
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.
189 REFERENCES:
190 http://lartc.org/lartc.txt      Linux Advanced Routing & Traffic Control:
191 http://luxik.cdi.cz/~devik/qos/htb/     HTB Home page
194 THANKS:
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
215 cd bc-1.06
216 ./configure --prefix=/usr
217 make
218 make install
220 and rpncalc at:
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
225 cd rpncalc-1.33.3
226 ./configure --prefix=/usr
227 make
228 make MKINSTALLDIRS=mkinstalldirs install
230 The script:
232 ---/etc/sysconfig/network-devices/ifup---
234 #!/bin/sh
236 source /etc/sysconfig/rc
237 source $rc_functions
238 source $network_devices/ifconfig.$1
240 if [ -f $network_devices/ifup-$1 ]
241 then
242         $network_devices/ifup-$1
243 else
244         if [ -z $IP ]
245         then
246                 echo "IP variable missing for ifconfig.$1, cannot continue"
247                 exit 1
248         fi
250         if [ -z $NETMASK ]
251         then
252                 echo -n "NETMASK variable missing for ifconfig.$1, "
253                 echo "using 255.255.255.0"
254                 NETMASK=255.255.255.0
255         fi
257         if [ "`echo "$NETMASK" | grep "\."`" ]; then
258           MASK=0
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
264                 k=`echo "8-$j" | bc`
265                 let MASK=MASK+$k
266                 break
267               fi
268             done
269           done
270         else
271           MASK=$NETMASK
272         fi
274         if [ -z $BROADCAST ]
275         then
276             if [ $MASK -lt 24 ]; then
277                 # Good luck!
278                 NETMASK=""
279                 MSK=$MASK
280                 DIF=`echo "256-2^(8-$MASK%8)" | bc`
281                 for i in `seq 1 4`; do
282                   let MSK=MSK-8
283                   if [ $MSK -gt -1 ]; then
284                     NETMASK=$NETMASK.255
285                   else
286                   if [ $MSK -lt -8 ]; then
287                     NETMASK=$NETMASK.0
288                   else
289                     NETMASK=$NETMASK.$DIF
290                   fi
291                 fi
292                 done
293                 NETMASK=`echo "$NETMASK" | sed -e "s/^.//"`
295                 BROADCAST=""
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`"
300                 done
301                 BROADCAST=`echo "$BROADCAST" | sed -e "s/^.//"`
302             else
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`
307                 BROADCAST=""
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"
314                       break
315                   fi
316                 done  
317             fi
318         fi
319         if [ -z $BROADCAST ]
320         then
321                 echo "Cannot calculate broadcast for ifconfig.$1, something is wrong"
322                 echo "Please check your IP=$IP and NETMASK=$MASK variables"
323                 exit 1
324         fi
326         echo "Bringing up the $1 interface..."
327         ip link set $1 up
328         ip addr add $IP/$MASK broadcast $BROADCAST dev $1
329         evaluate_retval
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*.
340 Good luck.
344 Zeta