small
[lartc.git] / lartc.db
blobe6357576fe7f5fae70dc3762884744a75644e0f7
1 <!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook V4.1//EN">
2 <!-- $Id$ -->
3 <Book id="lartc">
4 <?dbhtml banner-text="Made possible by PowerDNS">
5 <?dbhtml banner-href="http://www.powerdns.com">
7   <bookinfo>
8     <Title>Linux Advanced Routing &amp; Traffic Control HOWTO</Title>
9     <authorgroup>
10       <author>
11         <FirstName>Bert</FirstName><Surname>Hubert</Surname>
12         <affiliation>
13           <orgname>Netherlabs BV</orgname>
14           <address><email>bert.hubert@netherlabs.nl</email></address>
15         </affiliation>
16       </author>
17       
18       <collab>
19         <collabname>Gregory Maxwell (Section Author)</collabname>
20         <affiliation>
21           <address><email>remco%virtu.nl</email></address>
22         </affiliation>
23       </collab>
25       
26       <collab>
27         <collabname>Remco van Mook (Section Author)</collabname>
28         <affiliation>
29           <address><email>remco@virtu.nl</email></address>
30         </affiliation>
31      </collab>
32   
33      <collab>
34        <collabname>Martijn van Oosterhout (Section Author)</collabname>
35         <affiliation>
36           <address><email>kleptog@cupid.suninternet.com</email></address>
37         </affiliation>
38      </collab>
39   
40      <collab>
41        <collabname>Paul B Schroeder (Section Author)</collabname>
42         <affiliation>
43           <address><email>paulsch@us.ibm.com</email></address>
44         </affiliation>
45      </collab>
46   
47      <collab>
48        <collabname>Jasper Spaans (Section Author)</collabname>
49         <affiliation>
50           <address><email>jasper@spaans.ds9a.nl</email></address>
51         </affiliation>
52      </collab>
53      <collab>
54        <collabname>Pedro Larroy (Section Author)</collabname>
55         <affiliation>
56           <address><email>piotr%omega.resa.ed</email></address>
57         </affiliation>
58      </collab>
60   </authorgroup>
61   
62    <revhistory>
63      <revision>
64        <revnumber role="rcs">$Revision$</revnumber>
65        <date role="rcs">$Date$</date>
66        <revremark>DocBook Edition</revremark>
67      </revision>
68    </revhistory>
69                                    
70    <Abstract>
71      <Para>A very hands-on approach to <application>iproute2</application>,
72      traffic shaping and a bit of <application>netfilter</application>.
73      </para>
74    </Abstract>
75   
76 </bookinfo>
77 <toc></toc>
78 <chapter id="lartc.dedication">
79     <Title>Dedication</Title>
81     <Para>
82       This document is dedicated to lots of people, and is my attempt to do
83       something back. To list but a few:
84     </Para>
86     <Para>
88       <ItemizedList>
89         <ListItem>
90           <Para>
91             Rusty Russell
92           </Para>
93         </ListItem>
94         <ListItem>
96           <Para>
97             Alexey N. Kuznetsov
98           </Para>
99         </ListItem>
100         <ListItem>
101           
102           <Para>
103             The good folks from Google
104           </Para>
105         </ListItem>
106         <ListItem>
107           
108           <Para>
109             The staff of Casema Internet
110           </Para>
111         </ListItem>
113       </ItemizedList>
114         
115     </Para>
117   </chapter>
119   <chapter id="lartc.intro">
120     <Title>Introduction</Title>
122 <Para>
123 Welcome, gentle reader.
124 </Para>
126 <Para>
127       This document hopes to enlighten you on how to do more with Linux 2.2/2.4
128       routing. Unbeknownst to most users, you already run tools which allow you to
129       do spectacular things. Commands like <command>route</command> and 
130       <command>ifconfig</command> are actually
131       very thin wrappers for the very powerful iproute2 infrastructure.
132 </Para>
134     <Para>
135       I hope that this HOWTO will become as readable as the ones by Rusty Russell
136       of (amongst other things) netfilter fame.
137     </Para>
139     <Para>
140       You can always reach us by writing to the <ULink
141         URL="mailto:HOWTO@ds9a.nl"
142         >HOWTO team</ULink
143         >. However, please consider posting to the mailing
144       list (see the relevant section) if you have questions which are not directly
145       related to this HOWTO. We are no free helpdesk, but we often will answer questions
146       asked on the list.
147 </Para>
149 <Para>
150 Before losing your way in this HOWTO, if all you want to do is simple
151 traffic shaping, skip everything and head to the <citetitle><xref linkend="lartc.other"></citetitle> chapter, and read about CBQ.init.
152 </Para>
154 <Sect1 id="lartc.intro.disclaimer">
155 <Title>Disclaimer &amp; License</Title>
157 <Para>
158 This document is distributed in the hope that it will be useful,
159 but WITHOUT ANY WARRANTY; without even the implied warranty of
160 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
161 </Para>
163 <Para>
164 In short, if your STM-64 backbone breaks down and distributes pornography to
165 your most esteemed customers - it's never our fault. Sorry.
166 </Para>
168 <Para>
169 Copyright (c) 2002 by bert hubert, Gregory Maxwell, Martijn van
170 Oosterhout, Remco van Mook, Paul B. Schroeder and others. This material may
171 be distributed only subject to the terms and conditions set forth in the
172 Open Publication License, v1.0 or later (the latest version is presently
173 available at http://www.opencontent.org/openpub/).
174 </Para>
176 <Para>
177 Please freely copy and distribute (sell or give away) this document in any
178 format. It's requested that corrections and/or comments be forwarded to the
179 document maintainer. 
180 </Para>
182 <Para>
183 It is also requested that if you publish this HOWTO in hardcopy that you
184 send the authors some samples for <quote>review purposes</quote> :-) 
185 </Para>
187 </Sect1>
189 <Sect1 id="lartc.intro.prior">
190   <Title>Prior knowledge</Title>
192 <Para>
193 As the title implies, this is the <quote>Advanced</quote> HOWTO.
194 While by no means rocket science, some prior knowledge is assumed. 
195 </Para>
197 <Para>
198 Here are some other references which might help teach you more:
199 <VariableList>
200 <VarListEntry>
201   <Term>
202     <ULink URL="http://netfilter.samba.org/unreliable-guides/networking-concepts-HOWTO/index.html">
203       Rusty Russell's networking-concepts-HOWTO</ULink>
204   </Term>
205   <ListItem>
206     <Para>Very nice introduction, explaining what a network is, and how it is
207     connected to other networks.
208     </Para>
209   </ListItem>
210 </VarListEntry>
211 <VarListEntry>
212   <Term>Linux Networking-HOWTO (Previously the Net-3 HOWTO)</Term>
213   <ListItem>
214     <Para>Great stuff, although very verbose. It teaches you a lot of stuff 
215     that's already configured if you are able to connect to the Internet. 
216     Should be located in <filename>/usr/doc/HOWTO/NET3-4-HOWTO.txt</filename>
217  but can be also be found 
218     <ULink URL="http://www.linuxports.com/howto/networking">online</ULink>.
219     </Para>
220   </ListItem>
221 </VarListEntry>
222 </VariableList>
223 </Para>
225 </Sect1>
227 <Sect1 id="lartc.intro.linux">
228   <Title>What Linux can do for you</Title>
230 <Para>
231 A small list of things that are possible:
232 </Para>
234 <ItemizedList>
235 <ListItem>
236   <Para>Throttle bandwidth for certain computers
237   </Para>
238 </ListItem>
239 <ListItem>
240   <Para>Throttle bandwidth TO certain computers
241   </Para>
242 </ListItem>
243 <ListItem>
244   <Para>Help you to fairly share your bandwidth
245   </Para>
246 </ListItem>
247 <ListItem>
248   <Para>Protect your network from DoS attacks
249   </Para>
250 </ListItem>
251 <ListItem>
252   <Para>Protect the Internet from your customers
253   </Para>
254 </ListItem>
255 <ListItem>
256   <Para>Multiplex several servers as one, for load balancing or
257   enhanced availability
258   </Para>
259 </ListItem>
260 <ListItem>
261   <Para>Restrict access to your computers
262   </Para>
263 </ListItem>
264 <ListItem>
265   <Para>Limit access of your users to other hosts
266   </Para>
267 </ListItem>
268 <ListItem>
269   <Para>Do routing based on user id (yes!), MAC address, source IP
270   address, port, type of service, time of day or content
271   </Para>
272 </ListItem>
273 </ItemizedList>
275 <Para>
276 Currently, not many people are using these advanced features. This is for
277 several reasons. While the provided documentation is verbose, it is not very
278 hands-on. Traffic control is almost undocumented.
279 </Para>
281 </Sect1>
283 <Sect1 id="lartc.intro.houskeeping">
284   <Title>Housekeeping notes</Title>
286 <Para>
287 There are several things which should be noted about this document. While I
288 wrote most of it, I really don't want it to stay that way. I am a strong
289 believer in Open Source, so I encourage you to send feedback, updates,
290 patches etcetera. Do not hesitate to inform me of typos or plain old errors.
291 If my English sounds somewhat wooden, please realize that I'm not a native
292 speaker. Feel free to send suggestions.
293 </Para>
295 <Para>
296 If you feel to you are better qualified to maintain a section, or think that
297 you can author and maintain new sections, you are welcome to do so. The SGML
298 of this HOWTO is available via CVS, I very much envision more people
299 working on it.
300 </Para>
302 <Para>
303 In aid of this, you will find lots of FIXME notices. Patches are always
304 welcome! Wherever you find a FIXME, you should know that you are treading in
305 unknown territory. This is not to say that there are no errors elsewhere,
306 but be extra careful. If you have validated something, please let us know so
307 we can remove the FIXME notice.
308 </Para>
310 <Para>
311 About this HOWTO, I will take some liberties along the road. For example, I
312 postulate a 10Mbit Internet connection, while I know full well that those
313 are not very common.
314 </Para>
316 </Sect1>
318 <Sect1 id="lartc.intro.cvs">
319   <Title>Access, CVS &amp; submitting updates</Title>
321 <Para>
322 The canonical location for the HOWTO is 
323 <ULink URL="http://www.ds9a.nl/lartc">here</ULink>.
324 </Para>
326 <Para>
327 We now have anonymous CVS access available to the world at large. This is
328 good in a number of ways. You can easily upgrade to newer versions of this
329 HOWTO and submitting patches is no work at all.
330 </Para>
332 <Para>
333 Furthermore, it allows the authors to work on the source independently,
334 which is good too.
335 </Para>
337 <Screen width="80">
338 $ export CVSROOT=:pserver:anon@outpost.ds9a.nl:/var/cvsroot
339 $ cvs login
340 CVS password: [enter 'cvs' (without 's)]
341 $ cvs co 2.4routing
342 cvs server: Updating 2.4routing
343 U 2.4routing/lartc.db
344 </Screen>
346 <Para>
347 If you made changes and want to contribute them, run <userinput>
348 cvs -z3 diff -uBb</userinput>,
349 and mail the output to <email>howto@ds9a.nl</email>, we
350 can then integrate it easily. Thanks! Please make sure that you edit the
351 .db file, by the way, the other files are generated from that one. 
352 </Para>
354 <Para>
355 A Makefile is supplied which should help you create postscript, dvi, pdf,
356 html and plain text. You may need to install 
357 <application>docbook</application>, <application>docbook-utils</application>,
358 <application>ghostscript</application> and <application>tetex</application> 
359 to get all formats.
360 </Para>
362 <para>
363 Be careful not to edit 2.4routing.sgml! It contains an older version of the
364 HOWTO. The right file is lartc.db.
365 </para>
366 </Sect1>
368 <Sect1 id="lartc.intro.mlist">
369   <Title>Mailing list</Title>
371 <Para>
372 The authors receive an increasing amount of mail about this HOWTO. Because
373 of the clear interest of the community, it has been decided to start a
374 mailinglist where people can talk to each other about Advanced Routing and
375 Traffic Control. You can subscribe to the list
376 <ULink URL="http://mailman.ds9a.nl/mailman/listinfo/lartc">here</ULink>.
377 </Para>
379 <Para>
380 It should be pointed out that the authors are very hesitant of answering
381 questions not asked on the list. We would like the archive of the list to
382 become some kind of knowledge base. If you have a question, please search
383 the archive, and then post to the mailinglist.
384 </Para>
386 </Sect1>
388 <Sect1 id="lartc.intro.layout">
389   <Title>Layout of this document</Title>
391 <Para>
392 We will be doing interesting stuff almost immediately, which also means that
393 there will initially be parts that are explained incompletely or are not
394 perfect. Please gloss over these parts and assume that all will become clear.
395 </Para>
397 <Para>
398 Routing and filtering are two distinct things. Filtering is documented very
399 well by Rusty's HOWTOs, available here:
400 </Para>
402 <ItemizedList>
403 <ListItem>
404   <Para><ULink URL="http://netfilter.samba.org/unreliable-guides/">
405     Rusty's Remarkably Unreliable Guides</ULink>
406   </Para>
407 </ListItem>
408 </ItemizedList>
410 <Para>We will be focusing mostly on what is possible by combining netfilter
411 and iproute2.
412 </Para>
414 </Sect1>
416 </chapter>
418 <chapter id="lartc.iproute2">
419   <Title>Introduction to iproute2</Title>
421 <Sect1 id="lartc.iproute2.why">
422   <Title>Why iproute2?</Title>
424 <Para>
425 Most Linux distributions, and most UNIX's, currently use the 
426 venerable <command>arp</command>, <command>ifconfig</command> and 
427 <command>route</command> commands.
428 While these tools work, they show some unexpected behaviour under Linux 2.2 
429 and up.
430 For example, GRE tunnels are an integral part of routing these days, but 
431 require completely different tools.
432 </Para>
434 <Para>
435 With <application>iproute2</application>, tunnels are an integral part of 
436 the tool set.
437 </Para>
439 <Para>
440 The 2.2 and above Linux kernels include a completely redesigned network
441 subsystem. This new networking code brings Linux performance and a feature
442 set with little competition in the general OS arena. In fact, the new
443 routing, filtering, and classifying code is more featureful than the one
444 provided by many dedicated routers and firewalls and traffic shaping
445 products.
446 </Para>
448 <Para>
449 As new networking concepts have been invented, people have found ways to
450 plaster them on top of the existing framework in existing OSes. This
451 constant layering of cruft has lead to networking code that is filled with
452 strange behaviour, much like most human languages. In the past, Linux
453 emulated SunOS's handling of many of these things, which was not ideal.  
454 </Para>
456 <Para>
457 This new framework makes it possible to clearly express features
458 previously beyond Linux's reach.
459 </Para>
461 </Sect1>
463 <Sect1 id="lartc.iproute2.tour">
464   <Title>iproute2 tour</Title>
466 <Para>
467 Linux has a sophisticated system for bandwidth provisioning called Traffic
468 Control. This system supports various method for classifying, prioritizing,
469 sharing, and limiting both inbound and outbound traffic.
470 </Para>
472 <Para>
473 We'll start off with a tiny tour of iproute2 possibilities.
474 </Para>
476 </Sect1>
478 <Sect1 id="lartc.iproute2.package">
479   <Title>Prerequisites</Title>
481 <Para>
482 You should make sure that you have the userland tools installed. This
483 package is called 'iproute' on both RedHat and Debian, and may otherwise be
484 found at <filename>ftp://ftp.inr.ac.ru/ip-routing/iproute2-2.2.4-now-ss??????.tar.gz"</filename>. 
485 </Para>
487 <Para>
488 You can also try 
489 <ULink URL="ftp://ftp.inr.ac.ru/ip-routing/iproute2-current.tar.gz">here</ULink> 
490 for the latest version.
491 </Para>
493 <Para>
494 Some parts of iproute require you to have certain kernel options enabled. It
495 should also be noted that all releases of RedHat up to and including 6.2
496 come without most of the traffic control features in the default kernel. 
497 </Para>
499 <Para>
500 RedHat 7.2 has everything in by default.
501 </Para>
503 <Para>
504 Also make sure that you have netlink support, should you choose to roll your
505 own kernel. Iproute2 needs it.
506 </Para>
508 </Sect1>
510 <Sect1 id="lartc.iproute2.explore">
511   <Title>Exploring your current configuration</Title>
513 <Para>
514 This may come as a surprise, but iproute2 is already configured! The current
515 commands <command>ifconfig</command> and <command>route</command> are already using the advanced
516 syscalls, but mostly with very default (ie. boring) settings.
517 </Para>
519 <Para>
520 The <command>ip</command> tool is central, and we'll ask it to display our interfaces
521 for us.
522 </Para>
524 <Sect2>
525 <Title><command>ip</command> shows us our links</Title>
527 <Screen width="80">
528 [ahu@home ahu]$ ip link list
529 1: lo: &#60;LOOPBACK,UP&#62; mtu 3924 qdisc noqueue 
530     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
531 2: dummy: &#60;BROADCAST,NOARP&#62; mtu 1500 qdisc noop 
532     link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
533 3: eth0: &#60;BROADCAST,MULTICAST,PROMISC,UP&#62; mtu 1400 qdisc pfifo_fast qlen 100
534     link/ether 48:54:e8:2a:47:16 brd ff:ff:ff:ff:ff:ff
535 4: eth1: &#60;BROADCAST,MULTICAST,PROMISC,UP&#62; mtu 1500 qdisc pfifo_fast qlen 100
536     link/ether 00:e0:4c:39:24:78 brd ff:ff:ff:ff:ff:ff
537 3764: ppp0: &#60;POINTOPOINT,MULTICAST,NOARP,UP&#62; mtu 1492 qdisc pfifo_fast qlen 10
538     link/ppp 
540 </Screen>
542 <Para>
543 Your mileage may vary, but this is what it shows on my NAT router at
544 home. I'll only explain part of the output as not everything is directly
545 relevant.
546 </Para>
548 <Para>
549 We first see the loopback interface. While your computer may function
550 somewhat without one, I'd advise against it. The MTU size (Maximum Transfer
551 Unit) is 3924 octets, and it is not supposed to queue. Which makes sense
552 because the loopback interface is a figment of your kernel's imagination.
553 </Para>
555 <Para>
556 I'll skip the dummy interface for now, and it may not be present on your
557 computer. Then there are my two physical network interfaces, one at the side
558 of my cable modem, the other one serves my home ethernet segment.
559 Furthermore, we see a ppp0 interface.
560 </Para>
562 <Para>
563 Note the absence of IP addresses. iproute disconnects the concept of 'links'
564 and 'IP addresses'. With IP aliasing, the concept of 'the' IP address had
565 become quite irrelevant anyhow. 
566 </Para>
568 <Para>
569 It does show us the MAC addresses though, the hardware identifier of our
570 ethernet interfaces.
571 </Para>
573 </Sect2>
575 <Sect2>
576   <Title><command>ip</command> shows us our IP addresses</Title>
578 <Screen width="80">
579 [ahu@home ahu]$ ip address show        
580 1: lo: &#60;LOOPBACK,UP&#62; mtu 3924 qdisc noqueue 
581     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
582     inet 127.0.0.1/8 brd 127.255.255.255 scope host lo
583 2: dummy: &#60;BROADCAST,NOARP&#62; mtu 1500 qdisc noop 
584     link/ether 00:00:00:00:00:00 brd ff:ff:ff:ff:ff:ff
585 3: eth0: &#60;BROADCAST,MULTICAST,PROMISC,UP&#62; mtu 1400 qdisc pfifo_fast qlen 100
586     link/ether 48:54:e8:2a:47:16 brd ff:ff:ff:ff:ff:ff
587     inet 10.0.0.1/8 brd 10.255.255.255 scope global eth0
588 4: eth1: &#60;BROADCAST,MULTICAST,PROMISC,UP&#62; mtu 1500 qdisc pfifo_fast qlen 100
589     link/ether 00:e0:4c:39:24:78 brd ff:ff:ff:ff:ff:ff
590 3764: ppp0: &#60;POINTOPOINT,MULTICAST,NOARP,UP&#62; mtu 1492 qdisc pfifo_fast qlen 10
591     link/ppp 
592     inet 212.64.94.251 peer 212.64.94.1/32 scope global ppp0
593 </Screen>
595 <Para>
596 This contains more information. It shows all our addresses, and to which
597 cards they belong. 'inet' stands for Internet (IPv4). There are lots of other
598 address families, but these don't concern us right now.
599 </Para>
601 <Para>
602 Let's examine eth0 somewhat closer. It says that it is related to the inet
603 address '10.0.0.1/8'. What does this mean? The /8 stands for the number of
604 bits that are in the Network Address. There are 32 bits, so we have 24 bits
605 left that are part of our network. The first 8 bits of 10.0.0.1 correspond
606 to 10.0.0.0, our Network Address, and our netmask is 255.0.0.0.
607 </Para>
609 <Para>
610 The other bits are connected to this interface, so 10.250.3.13 is directly
611 available on eth0, as is 10.0.0.1 for example. 
612 </Para>
614 <Para>
615 With ppp0, the same concept goes, though the numbers are different. Its
616 address is 212.64.94.251, without a subnet mask. This means that we have a
617 point-to-point connection and that every address, with the exception of
618 212.64.94.251, is remote. There is more information, however. It tells us
619 that on the other side of the link there is, yet again, only one address,
620 212.64.94.1. The /32 tells us that there are no 'network bits'.
621 </Para>
623 <Para>
624 It is absolutely vital that you grasp these concepts. Refer to the
625 documentation mentioned at the beginning of this HOWTO if you have trouble.
626 </Para>
628 <Para>
629 You may also note 'qdisc', which stands for Queueing Discipline. This will
630 become vital later on. 
631 </Para>
633 </Sect2>
635 <Sect2>
636   <Title><command>ip</command> shows us our routes</Title>
638 <Para>
639 Well, we now know how to find 10.x.y.z addresses, and we are able to reach
640 212.64.94.1. This is not enough however, so we need instructions on how to
641 reach the world. The Internet is available via our ppp connection, and it
642 appears that 212.64.94.1 is willing to spread our packets around the
643 world, and deliver results back to us.
644 </Para>
646 <Screen width="80">
647 [ahu@home ahu]$ ip route show
648 212.64.94.1 dev ppp0  proto kernel  scope link  src 212.64.94.251 
649 10.0.0.0/8 dev eth0  proto kernel  scope link  src 10.0.0.1 
650 127.0.0.0/8 dev lo  scope link 
651 default via 212.64.94.1 dev ppp0 
652 </Screen>
654 <Para>
655 This is pretty much self explanatory. The first 4 lines of output explicitly
656 state what was already implied by <command>ip address show</command>, the last line
657 tells us that the rest of the world can be found via 212.64.94.1, our
658 default gateway. We can see that it is a gateway because of the word
659 via, which tells us that we need to send packets to 212.64.94.1, and that it
660 will take care of things.
661 </Para>
663 <Para>
664 For reference, this is what the old <command>route</command> utility shows us:
665 </Para>
667 <Screen width="80">
668 [ahu@home ahu]$ route -n
669 Kernel IP routing table
670 Destination     Gateway         Genmask         Flags Metric Ref    Use
671 Iface
672 212.64.94.1     0.0.0.0         255.255.255.255 UH    0      0        0 ppp0
673 10.0.0.0        0.0.0.0         255.0.0.0       U     0      0        0 eth0
674 127.0.0.0       0.0.0.0         255.0.0.0       U     0      0        0 lo
675 0.0.0.0         212.64.94.1     0.0.0.0         UG    0      0        0 ppp0
676 </Screen>
678 </Sect2>
680 </Sect1>
682 <Sect1 id="lartc.iproute2.arp">
683   <Title>ARP</Title>
685 <Para>
686 ARP is the Address Resolution Protocol as described in
687 <ULink URL="http://www.faqs.org/rfcs/rfc826.html">RFC 826</ULink>.
688 ARP is used by a networked machine to resolve the hardware location/address of
689 another machine on the same
690 local network.  Machines on the Internet are generally known by their names
691 which resolve to IP
692 addresses.  This is how a machine on the foo.com network is able to communicate
693 with another machine which is on the bar.net network.  An IP address, though,
694 cannot tell you the physical location of a machine.  This is where ARP comes
695 into the picture.
696 </Para>
698 <Para>
699 Let's take a very simple example.  Suppose I have a network composed of several
700 machines.  Two of the machines which are currently on my network are foo
701 with an IP address of 10.0.0.1 and bar with an IP address of 10.0.0.2.
702 Now foo wants to ping bar to see that he is alive, but alas, foo has no idea
703 where bar is.  So when foo decides to ping bar he will need to send
704 out an ARP request.
705 This ARP request is akin to foo shouting out on the network "Bar (10.0.0.2)!
706 Where are you?"  As a result of this every machine on the network will hear
707 foo shouting, but only bar (10.0.0.2) will respond.  Bar will then send an
708 ARP reply directly back to foo which is akin
709 bar saying,
710 "Foo (10.0.0.1) I am here at 00:60:94:E9:08:12."  After this simple transaction
711 that's used to locate his friend on the network, foo is able to communicate
712 with bar until he (his arp cache) forgets where bar is (typically after
713 15 minutes on Unix).
714 </Para>
716 <Para>
717 Now let's see how this works.
718 You can view your machines current arp/neighbor cache/table like so:
719 </Para>
721 <Screen width="80">
722 [root@espa041 /home/src/iputils]# ip neigh show
723 9.3.76.42 dev eth0 lladdr 00:60:08:3f:e9:f9 nud reachable
724 9.3.76.1 dev eth0 lladdr 00:06:29:21:73:c8 nud reachable
725 </Screen>
727 <Para>
728 As you can see my machine espa041 (9.3.76.41) knows where to find espa042 
729 (9.3.76.42) and
730 espagate (9.3.76.1).  Now let's add another machine to the arp cache.
731 </Para>
733 <Screen width="80">
734 [root@espa041 /home/paulsch/.gnome-desktop]# ping -c 1 espa043
735 PING espa043.austin.ibm.com (9.3.76.43) from 9.3.76.41 : 56(84) bytes of data.
736 64 bytes from 9.3.76.43: icmp_seq=0 ttl=255 time=0.9 ms
738 --- espa043.austin.ibm.com ping statistics ---
739 1 packets transmitted, 1 packets received, 0% packet loss
740 round-trip min/avg/max = 0.9/0.9/0.9 ms
742 [root@espa041 /home/src/iputils]# ip neigh show
743 9.3.76.43 dev eth0 lladdr 00:06:29:21:80:20 nud reachable
744 9.3.76.42 dev eth0 lladdr 00:60:08:3f:e9:f9 nud reachable
745 9.3.76.1 dev eth0 lladdr 00:06:29:21:73:c8 nud reachable
746 </Screen>
748 <Para>
749 As a result of espa041 trying to contact espa043, espa043's hardware
750 address/location has now been added to the arp/neighbor cache.
751 So until the entry for
752 espa043 times out (as a result of no communication between the two) espa041
753 knows where to find espa043 and has no need to send an ARP request.
754 </Para>
756 <Para>
757 Now let's delete espa043 from our arp cache:
758 </Para>
760 <Screen width="80">
761 [root@espa041 /home/src/iputils]# ip neigh delete 9.3.76.43 dev eth0
762 [root@espa041 /home/src/iputils]# ip neigh show
763 9.3.76.43 dev eth0  nud failed
764 9.3.76.42 dev eth0 lladdr 00:60:08:3f:e9:f9 nud reachable
765 9.3.76.1 dev eth0 lladdr 00:06:29:21:73:c8 nud stale
766 </Screen>
768 <Para>
769 Now espa041 has again forgotten where to find espa043 and will need to send
770 another ARP request the next time he needs to communicate with espa043.
771 You can also see from the above output that espagate (9.3.76.1) has been
772 changed to the "stale" state.  This means that the location shown is still
773 valid, but it will have to be confirmed at the first transaction to that
774 machine.
775 </Para>
777 </Sect1>
779 </chapter>
781 <chapter id="lartc.rpdb">
782   <Title>Rules - routing policy database</Title>
784 <Para>
785 If you have a large router, you may well cater for the needs of different
786 people, who should be served differently. The routing policy database allows
787 you to do this by having multiple sets of routing tables. 
788 </Para>
790 <Para>
791 If you want to use this feature, make sure that your kernel is compiled with
792 the "IP: advanced router" and "IP: policy routing" features.
793 </Para>
795 <Para>
796 When the kernel needs to make a routing decision, it finds out which table
797 needs to be consulted. By default, there are three tables. The old 'route'
798 tool modifies the main and local tables, as does the ip tool (by default).
799 </Para>
801 <Para>The default rules:
802 </Para>
804 <Screen width="80">
805 [ahu@home ahu]$ ip rule list
806 0:      from all lookup local 
807 32766:  from all lookup main 
808 32767:  from all lookup default
809 </Screen>
811 <Para>
812 This lists the priority of all rules. We see that all rules apply to all
813 packets ('from all'). We've seen the 'main' table before, it is output by
814 <userinput>ip route ls</userinput>, but the 'local' and 'default' table are new.
815 </Para>
817 <Para>
818 If we want to do fancy things, we generate rules which point to different
819 tables which allow us to override system wide routing rules.
820 </Para>
822 <Para>
823 For the exact semantics on what the kernel does when there are more matching
824 rules, see Alexey's ip-cref documentation. 
825 </Para>
827 <Sect1 id="lartc.rpdb.simple">
828   <Title>Simple source policy routing</Title>
830 <Para>
831 Let's take a real example once again, I have 2 (actually 3, about time I
832 returned them) cable modems, connected to a Linux NAT ('masquerading')
833 router. People living here pay me to use the Internet. Suppose one of my
834 house mates only visits hotmail and wants to pay less. This is fine with me,
835 but they'll end up using the low-end cable modem.
836 </Para>
838 <Para>
839 The 'fast' cable modem is known as 212.64.94.251 and is a PPP link to
840 212.64.94.1. The 'slow' cable modem is known by various ip addresses,
841 212.64.78.148 in this example and is a link to 195.96.98.253.
842 </Para>
844 <Para>The local table:
845 </Para>
847 <Screen width="80">
848 [ahu@home ahu]$ ip route list table local
849 broadcast 127.255.255.255 dev lo  proto kernel  scope link  src 127.0.0.1 
850 local 10.0.0.1 dev eth0  proto kernel  scope host  src 10.0.0.1 
851 broadcast 10.0.0.0 dev eth0  proto kernel  scope link  src 10.0.0.1 
852 local 212.64.94.251 dev ppp0  proto kernel  scope host  src 212.64.94.251 
853 broadcast 10.255.255.255 dev eth0  proto kernel  scope link  src 10.0.0.1 
854 broadcast 127.0.0.0 dev lo  proto kernel  scope link  src 127.0.0.1 
855 local 212.64.78.148 dev ppp2  proto kernel  scope host  src 212.64.78.148 
856 local 127.0.0.1 dev lo  proto kernel  scope host  src 127.0.0.1 
857 local 127.0.0.0/8 dev lo  proto kernel  scope host  src 127.0.0.1 
858 </Screen>
860 <Para>
861 Lots of obvious things, but things that need to be specified somewhere.
862 Well, here they are. The default table is empty.
863 </Para>
865 <Para>Let's view the 'main' table:
866 </Para>
868 <Screen width="80">
869 [ahu@home ahu]$ ip route list table main 
870 195.96.98.253 dev ppp2  proto kernel  scope link  src 212.64.78.148 
871 212.64.94.1 dev ppp0  proto kernel  scope link  src 212.64.94.251 
872 10.0.0.0/8 dev eth0  proto kernel  scope link  src 10.0.0.1 
873 127.0.0.0/8 dev lo  scope link 
874 default via 212.64.94.1 dev ppp0 
875 </Screen>
877 <Para>
878 We now generate a new rule which we call 'John', for our hypothetical
879 house mate. Although we can work with pure numbers, it's far easier if we add
880 our tables to /etc/iproute2/rt_tables.
881 </Para>
883 <Screen width="80">
884 # echo 200 John &#62;&#62; /etc/iproute2/rt_tables
885 # ip rule add from 10.0.0.10 table John
886 # ip rule ls
887 0:      from all lookup local 
888 32765:  from 10.0.0.10 lookup John
889 32766:  from all lookup main 
890 32767:  from all lookup default
891 </Screen>
893 <Para>
894 Now all that is left is to generate John's table, and flush the route cache:
895 </Para>
897 <Screen width="80">
898 # ip route add default via 195.96.98.253 dev ppp2 table John
899 # ip route flush cache
900 </Screen>
902 <Para>
903 And we are done. It is left as an exercise for the reader to implement this
904 in ip-up.
905 </Para>
907 </Sect1>
909 <sect1 id="lartc.rpdb.multiple-links">
910   <title>Routing for multiple uplinks/providers</title>
911 <para>
912 A common configuration is the following, in which there are two providers
913 that connect a local network (or even a single machine) to the big Internet.
915 <screen>
916                                                                  ________
917                                           +------------+        /
918                                           |            |       |
919                             +-------------+ Provider 1 +-------
920         __                  |             |            |     /
921     ___/  \_         +------+-------+     +------------+    |
922   _/        \__      |     if1      |                      /
923  /             \     |              |                      |
924 | Local network -----+ Linux router |                      |     Internet
925  \_           __/    |              |                      |
926    \__     __/       |     if2      |                      \
927       \___/          +------+-------+     +------------+    |
928                             |             |            |     \
929                             +-------------+ Provider 2 +-------
930                                           |            |       |
931                                           +------------+        \________
932 </screen>
933 </para>
934 <para>
935 There are usually two questions given this setup.
936 </para>
937       <sect2><title>Split access</title>
938         <para>
939           The first is how to route answers to packets coming in over a
940           particular provider, say Provider 1, back out again over that same provider.
941         </para>
942         <para>
943           Let us first set some symbolical names. Let <command>$IF1</command> be the name of the
944           first interface (if1 in the picture above) and <command>$IF2</command> the name of the
945           second interface. Then let <command>$IP1</command> be the IP address associated with
946           <command>$IF1</command> and <command>$IP2</command> the IP address associated with
947           <command>$IF2</command>. Next, let <command>$P1</command> be the IP address of the gateway at
948           Provider 1, and <command>$P2</command> the IP address of the gateway at provider 2.
949           Finally, let <command>$P1_NET</command> be the IP network <command>$P1</command> is in,
950           and <command>$P2_NET</command> the IP network <command>$P2</command> is in.
951         </para>
952         <para>
953           One creates two additional routing tables, say <command>T1</command> and <command>T2</command>.
954           These are added in /etc/iproute2/rt_tables. Then you set up routing in
955           these tables as follows:
956         </para>
957         <para>
958         <screen>
959           ip route add $P1_NET dev $IF1 src $IP1 table T1
960           ip route add default via $P1 table T1
961           ip route add $P2_NET dev $IF2 src $IP2 table T2
962           ip route add default via $P2 table T2
963         </screen>
964           
965           Nothing spectacular, just build a route to the gateway and build a
966           default route via that gateway, as you would do in the case of a single
967           upstream provider, but put the routes in a separate table per provider.
968           Note that the network route suffices, as it tells you how to find any host
969           in that network, which includes the gateway, as specified above.
970         </para>
971         <para>
972           Next you set up the main routing table. It is a good idea to route
973           things to the direct neighbour through the interface connected to that
974           neighbour. Note the `src' arguments, they make sure the right outgoing IP
975           address is chosen.
977           <screen>
978             ip route add $P1_NET dev $IF1 src $IP1
979             ip route add $P2_NET dev $IF2 src $IP2
980           </screen>
982           Then, your preference for default route:
983           
984           <screen>
985             ip route add default via $P1
986           </screen>
988           Next, you set up the routing rules. These actually choose what routing table
989           to route with. You want to make sure that you route out a given
990           interface if you already have the corresponding source address:
991           
992           <screen>
993             ip rule add from $IP1 table T1
994             ip rule add from $IP2 table T2
995           </screen>
997           This set of commands makes sure all answers to traffic coming in on a
998           particular interface get answered from that interface.
999         </para>
1000         <para>
1001         <warning><para>
1002 Reader Rod Roark notes: 'If $P0_NET is the local network and $IF0 is its interface,
1003 the following additional entries are desirable:
1004 <screen>
1005 ip route add $P0_NET     dev $IF0 table T1
1006 ip route add $P2_NET     dev $IF2 table T1
1007 ip route add 127.0.0.0/8 dev lo   table T1
1008 ip route add $P0_NET     dev $IF0 table T2
1009 ip route add $P1_NET     dev $IF1 table T2
1010 ip route add 127.0.0.0/8 dev lo   table T2                                      
1011 </screen>'
1012 </para></warning</para>
1013         <para>
1014           Now, this is just the very basic setup. It will work for all processes
1015           running on the router itself, and for the local network, if it is
1016           masqueraded. If it is not, then you either have IP space from both providers
1017           or you are going to want to masquerade to one of the two providers. In both
1018           cases you will want to add rules selecting which provider to route out from
1019           based on the IP address of the machine in the local network.
1020         </para>
1021       </sect2>
1022       <sect2><title>Load balancing</title>
1023         <para>
1024           The second question is how to balance traffic going out over the two providers.
1025           This is actually not hard if you already have set up split access as above.
1026           </para>
1027         <para>
1028           Instead of choosing one of the two providers as your default route,
1029           you now set up the default route to be a multipath route. In the default
1030           kernel this will balance routes over the two providers. It is done
1031           as follows (once more building on the example in the section on
1032           split-access):
1034           <screen>
1035             ip route add default scope global nexthop via $P1 dev $IF1 weight 1 \
1036             nexthop via $P2 dev $IF2 weight 1
1037           </screen>
1039           This will balance the routes over both providers. The <command>weight</command>
1040           parameters can be tweaked to favor one provider over the other.
1041         </para>
1042         <para>
1043           Note that balancing will not be perfect, as it is route based, and routes
1044           are cached. This means that routes to often-used sites will always
1045           be over the same provider.
1046         </para>
1047         <para>
1048           Furthermore, if you really want to do this, you probably also want to look
1049           at Julian Anastasov's patches at <ulink
1050             url="http://www.linuxvirtualserver.org/~julian/#routes">http://www.linuxvirtualserver.org/~julian/#routes
1051             </ulink>, Julian's route patch page. They will make things nicer to work with.
1052         </para>
1053       </sect2>
1054     </sect1>
1055   </chapter>
1057 <chapter id="lartc.tunnel">
1058    <Title>GRE and other tunnels</Title>
1060 <Para>
1061 There are 3 kinds of tunnels in Linux. There's IP in IP tunneling, GRE tunneling and tunnels that live outside the kernel (like, for example PPTP). 
1062 </Para>
1064 <Sect1 id="lartc.tunnel.remarks">
1065   <Title>A few general remarks about tunnels:</Title>
1067 <Para>
1068 Tunnels can be used to do some very unusual and very cool stuff. They can
1069 also make things go horribly wrong when you don't configure them right.
1070 Don't point your default route to a tunnel device unless you know
1071 <Emphasis>EXACTLY</Emphasis> what you are doing :-). Furthermore, tunneling increases
1072 overhead, because it needs an extra set of IP headers. Typically this is 20
1073 bytes per packet, so if the normal packet size (MTU) on a network is 1500
1074 bytes, a packet that is sent through a tunnel can only be 1480 bytes big.
1075 This is not necessarily a problem, but be sure to read up on IP packet
1076 fragmentation/reassembly when you plan to connect large networks with
1077 tunnels. Oh, and of course, the fastest way to dig a tunnel is to dig at
1078 both sides.
1079 </Para>
1081 </Sect1>
1083 <Sect1 id="lartc.tunnel.ip-ip">
1084   <Title>IP in IP tunneling</Title>
1086 <Para>
1087 This kind of tunneling has been available in Linux for a long time. It requires 2 kernel modules,
1088 ipip.o and new_tunnel.o.
1089 </Para>
1091 <Para>
1092 Let's say you have 3 networks: Internal networks A and B, and intermediate network C (or let's say, Internet). 
1093 So we have network A:
1094 </Para>
1096 <Screen width="80">
1097 network 10.0.1.0
1098 netmask 255.255.255.0
1099 router  10.0.1.1
1100 </Screen>
1102 <Para>The router has address 172.16.17.18 on network C.
1103 </Para>
1105 <Para>and network B:
1106 </Para>
1108 <Screen width="80">
1109 network 10.0.2.0
1110 netmask 255.255.255.0
1111 router  10.0.2.1
1112 </Screen>
1114 <Para>The router has address 172.19.20.21 on network C.
1115 </Para>
1117 <Para>
1118 As far as network C is concerned, we assume that it will pass any packet sent
1119 from A to B and vice versa. You might even use the Internet for this.
1120 </Para>
1122 <Para>Here's what you do:
1123 </Para>
1125 <Para>First, make sure the modules are installed:
1126 </Para>
1128 <Screen width="80">
1129 insmod ipip.o
1130 insmod new_tunnel.o
1131 </Screen>
1133 <Para>Then, on the router of network A, you do the following:
1134 </Para>
1136 <Screen width="80">
1137 ifconfig tunl0 10.0.1.1 pointopoint 172.19.20.21
1138 route add -net 10.0.2.0 netmask 255.255.255.0 dev tunl0
1139 </Screen>
1141 <Para>And on the router of network B:
1142 </Para>
1144 <Screen width="80">
1145 ifconfig tunl0 10.0.2.1 pointopoint 172.16.17.18
1146 route add -net 10.0.1.0 netmask 255.255.255.0 dev tunl0
1147 </Screen>
1149 <Para>And if you're finished with your tunnel:
1150 </Para>
1152 <Screen width="80">
1153 ifconfig tunl0 down
1154 </Screen>
1156 <Para>Presto, you're done. You can't forward broadcast or IPv6 traffic through
1157 an IP-in-IP tunnel, though. You just connect 2 IPv4 networks that normally wouldn't be able to talk to each other, that's all. As far as compatibility goes, this code has been around a long time, so it's compatible all the way back to 1.3 kernels. Linux IP-in-IP tunneling doesn't work with other Operating Systems or routers, as far as I know. It's simple, it works. Use it if you have to, otherwise use GRE.
1158 </Para>
1160 </Sect1>
1162 <Sect1 id="lartc.tunnel.gre">
1163   <Title>GRE tunneling</Title>
1165 <Para>
1166 GRE is a tunneling protocol that was originally developed by Cisco, and it
1167 can do a few more things than IP-in-IP tunneling. For example, you can also
1168 transport multicast traffic and IPv6 through a GRE tunnel.
1169 </Para>
1171 <Para>
1172 In Linux, you'll need the ip_gre.o module.
1173 </Para>
1175 <Sect2>
1176 <Title>IPv4 Tunneling</Title>
1178 <Para>
1179 Let's do IPv4 tunneling first:
1180 </Para>
1182 <Para>
1183 Let's say you have 3 networks: Internal networks A and B, and intermediate network C (or let's say, Internet). 
1184 </Para>
1186 <Para>
1187 So we have network A:
1189 <Screen width="80">
1190 network 10.0.1.0
1191 netmask 255.255.255.0
1192 router  10.0.1.1
1193 </Screen>
1195 The router has address 172.16.17.18 on network C.
1196 Let's call this network neta (ok, hardly original)
1197 </Para>
1199 <Para>
1200 and network B:
1202 <Screen width="80">
1203 network 10.0.2.0
1204 netmask 255.255.255.0
1205 router  10.0.2.1
1206 </Screen>
1208 The router has address 172.19.20.21 on network C.
1209 Let's call this network netb (still not original)
1210 </Para>
1212 <Para>
1213 As far as network C is concerned, we assume that it will pass any packet sent
1214 from A to B and vice versa. How and why, we do not care.
1215 </Para>
1217 <Para>On the router of network A, you do the following:
1218 </Para>
1220 <Screen width="80">
1221 ip tunnel add netb mode gre remote 172.19.20.21 local 172.16.17.18 ttl 255
1222 ip link set netb up
1223 ip addr add 10.0.1.1 dev netb
1224 ip route add 10.0.2.0/24 dev netb
1225 </Screen>
1227 <Para>
1228 Let's discuss this for a bit. In line 1, we added a tunnel device, and
1229 called it netb (which is kind of obvious because that's where we want it to
1230 go). Furthermore we told it to use the GRE protocol (mode gre), that the
1231 remote address is 172.19.20.21 (the router at the other end), that our
1232 tunneling packets should originate from 172.16.17.18 (which allows your
1233 router to have several IP addresses on network C and let you decide which
1234 one to use for tunneling) and that the TTL field of the packet should be set
1235 to 255 (ttl 255).
1236 </Para>
1238 <Para>
1239 The second line enables the device.
1240 </Para>
1242 <Para>
1243 In the third line we gave the newly born interface netb the address
1244 10.0.1.1. This is OK for smaller networks, but when you're starting up a
1245 mining expedition (LOTS of tunnels), you might want to consider using
1246 another IP range for tunneling interfaces (in this example, you could use
1247 10.0.3.0).
1248 </Para>
1250 <Para>
1251 In the fourth line we set the route for network B. Note the different notation for the netmask. If you're not familiar with this notation, here's how it works: you write out the netmask in binary form, and you count all the ones. If you don't know how to do that, just remember that 255.0.0.0 is /8, 255.255.0.0 is /16 and 255.255.255.0 is /24. Oh, and 255.255.254.0 is /23, in case you were wondering.
1252 </Para>
1254 <Para>
1255 But enough about this, let's go on with the router of network B.
1257 <Screen>
1258 ip tunnel add neta mode gre remote 172.16.17.18 local 172.19.20.21 ttl 255
1259 ip link set neta up
1260 ip addr add 10.0.2.1 dev neta
1261 ip route add 10.0.1.0/24 dev neta
1262 </Screen>
1264 And when you want to remove the tunnel on router A:
1266 <Screen>
1267 ip link set netb down
1268 ip tunnel del netb
1269 </Screen>
1271 Of course, you can replace netb with neta for router B.
1272 </Para>
1274 </Sect2>
1276 <Sect2>
1277 <Title>IPv6 Tunneling</Title>
1279 <Para>
1280 See Section 6 for a short bit about IPv6 Addresses.
1281 </Para>
1283 <Para>
1284 On with the tunnels.
1285 </Para>
1287 <Para>
1288 Let's assume that you have the following IPv6 network, and you want to connect it to 6bone, or a friend.
1289 </Para>
1291 <Para>
1293 <Screen>
1294 Network 3ffe:406:5:1:5:a:2:1/96
1295 </Screen>
1297 Your IPv4 address is 172.16.17.18, and the 6bone router has IPv4 address 172.22.23.24. 
1298 </Para>
1300 <Para>
1302 <Screen>
1303 ip tunnel add sixbone mode sit remote 172.22.23.24 local 172.16.17.18 ttl 255
1304 ip link set sixbone up
1305 ip addr add 3ffe:406:5:1:5:a:2:1/96 dev sixbone
1306 ip route add 3ffe::/15 dev sixbone 
1307 </Screen>
1309 </Para>
1311 <Para>
1312 Let's discuss this. In the first line, we created a tunnel device called sixbone. We gave it mode sit (which is IPv6 in IPv4 tunneling) and told it where to go to (remote) and where to come from (local). TTL is set to maximum, 255. Next, we made the device active (up). After that, we added our own network address, and set a route for 3ffe::/15 (which is currently all of 6bone) through the tunnel.
1313 </Para>
1315 <Para>
1316 GRE tunnels are currently the preferred type of tunneling. It's a standard that is also widely adopted outside the Linux community and therefore a Good Thing.
1317 </Para>
1319 </Sect2>
1321 </Sect1>
1323 <Sect1 id="lartc.tunnel.userland">
1324   <Title>Userland tunnels</Title>
1326 <Para>
1327 There are literally dozens of implementations of tunneling outside the kernel. Best known are of course PPP and PPTP, but there are lots more (some proprietary, some secure, some that don't even use IP) and that is really beyond the scope of this HOWTO.
1328 </Para>
1330 </Sect1>
1332 </chapter>
1334 <chapter id="lartc.ipv6-tunnel">
1335 <Title>IPv6 tunneling with Cisco and/or 6bone</Title>
1337 <Para>
1338 By Marco Davids &lt;marco@sara.nl&gt;
1339 </Para>
1341 <Para>
1342 NOTE to maintainer:
1343 </Para>
1345 <Para>
1346 As far as I am concerned, this IPv6-IPv4 tunneling is not per definition
1347 GRE tunneling. You could tunnel IPv6 over IPv4 by means of GRE tunnel devices
1348 (GRE tunnels ANY to IPv4), but the device used here ("sit") only tunnels
1349 IPv6 over IPv4 and is therefore something different.
1350 </Para>
1352 <Sect1 id="lartc.tunnel-ipv6.addressing">
1353   <Title>IPv6 Tunneling</Title>
1355 <Para>
1356 This is another application of the tunneling capabilities of Linux. It is
1357 popular among the IPv6 early adopters, or pioneers if you like.
1358 The 'hands-on' example described below is certainly not the only way
1359 to do IPv6 tunneling. However, it is the method that is often used to tunnel
1360 between Linux and a Cisco IPv6 capable router and experience tells us that
1361 this is just the thing many people are after. Ten to one this applies to
1362 you too ;-)
1363 </Para>
1365 <Para>
1366 A short bit about IPv6 addresses:
1367 </Para>
1369 <Para>
1370 IPv6 addresses are, compared to IPv4 addresses, really big: 128 bits
1371 against 32 bits. And this provides us just with the thing we need: many, many
1372 IP-addresses: 340,282,266,920,938,463,463,374,607,431,768,211,465 to be
1373 precise. Apart from this, IPv6 (or IPng, for IP Next Generation) is supposed
1374 to provide for smaller routing tables on the Internet's backbone routers,
1375 simpler configuration of equipment, better security at the IP level and
1376 better support for QoS.
1377 </Para>
1379 <Para>
1380 An example: 2002:836b:9820:0000:0000:0000:836b:9886
1381 </Para>
1383 <Para>
1384 Writing down IPv6 addresses can be quite a burden. Therefore, to make
1385 life easier there are some rules:
1386 </Para>
1388 <Para>
1390 <ItemizedList>
1391 <ListItem>
1393 <Para>
1394 Don't use leading zeroes. Same as in IPv4.
1396 </Para>
1397 </ListItem>
1398 <ListItem>
1400 <Para>
1401 Use colons to separate every 16 bits or two bytes.
1403 </Para>
1404 </ListItem>
1405 <ListItem>
1407 <Para>
1408 When you have lots of consecutive zeroes,
1409 you can write this down as ::. You can only do this once in an
1410 address and only for quantities of 16 bits, though.
1411 </Para>
1412 </ListItem>
1414 </ItemizedList>
1416 </Para>
1418 <Para>
1419 The address 2002:836b:9820:0000:0000:0000:836b:9886 can be written down
1420 as 2002:836b:9820::836b:9886, which is somewhat friendlier.
1421 </Para>
1423 <Para>
1424 Another example, the address 3ffe:0000:0000:0000:0000:0020:34A1:F32C can be
1425 written down as 3ffe::20:34A1:F32C, which is a lot shorter.
1426 </Para>
1428 <Para>
1429 IPv6 is intended to be the successor of the current IPv4. Because it
1430 is relatively new technology, there is no worldwide native IPv6 network
1431 yet. To be able to move forward swiftly, the 6bone was introduced. 
1432 </Para>
1434 <Para>
1435 Native IPv6 networks are connected to each other by encapsulating the IPv6
1436 protocol in IPv4 packets and sending them over the existing IPv4 infrastructure
1437 from one IPv6 site to another. 
1438 </Para>
1440 <Para>
1441 That is precisely where the tunnel steps in.
1442 </Para>
1444 <Para>
1445 To be able to use IPv6, we should have a kernel that supports it. There
1446 are many good documents on how to achieve this. But it all comes down to
1447 a few steps:
1449 <ItemizedList>
1450 <ListItem>
1452 <Para>
1453 Get yourself a recent Linux distribution, with suitable glibc.
1454 </Para>
1455 </ListItem>
1456 <ListItem>
1458 <Para>
1459 Then get yourself an up-to-date kernel source.
1460 </Para>
1461 </ListItem>
1463 </ItemizedList>
1465 If you are all set, then you can go ahead and compile an IPv6 capable
1466 kernel:
1468 <ItemizedList>
1469 <ListItem>
1471 <Para>
1472 Go to /usr/src/linux and type:
1473 </Para>
1474 </ListItem>
1475 <ListItem>
1477 <Para>
1478 make menuconfig
1479 </Para>
1480 </ListItem>
1481 <ListItem>
1483 <Para>
1484 Choose "Networking Options"
1485 </Para>
1486 </ListItem>
1487 <ListItem>
1489 <Para>
1490 Select "The IPv6 protocol", "IPv6: enable EUI-64 token format", "IPv6:
1491 disable provider based addresses"
1492 </Para>
1493 </ListItem>
1495 </ItemizedList>
1497 HINT: Don't go for the 'module' option. Often this won't work well.
1498 </Para>
1500 <Para>
1501 In other words, compile IPv6 as 'built-in' in your kernel.
1502 You can then save your config like usual and go ahead with compiling
1503 the kernel.
1504 </Para>
1506 <Para>
1507 HINT: Before doing so, consider editing the Makefile:
1508 EXTRAVERSION = -x ; --&#62; ; EXTRAVERSION = -x-IPv6
1509 </Para>
1511 <Para>
1512 There is a lot of good documentation about compiling and installing
1513 a kernel, however this document is about something else. If you run into
1514 problems at this stage, go and look for documentation about compiling a
1515 Linux kernel according to your own specifications.
1516 </Para>
1518 <Para>
1519 The file /usr/src/linux/README might be a good start.
1520 After you accomplished all this, and rebooted with your brand new kernel,
1521 you might want to issue an '/sbin/ifconfig -a' and notice the brand 
1522 new 'sit0-device'. SIT stands for Simple Internet Transition. You may give
1523 yourself a compliment; you are now one major step closer to IP, the Next
1524 Generation ;-)
1525 </Para>
1527 <Para>
1528 Now on to the next step. You want to connect your host, or maybe even
1529 your entire LAN to another IPv6 capable network. This might be the "6bone"
1530 that is setup especially for this particular purpose.
1531 </Para>
1533 <Para>
1534 Let's assume that you have the following IPv6 network: 3ffe:604:6:8::/64 and
1535 you want to connect it to 6bone, or a friend. Please note that the /64
1536 subnet notation works just like with regular IP addresses.
1537 </Para>
1539 <Para>
1540 Your IPv4 address is 145.100.24.181 and the 6bone router has IPv4 address
1541 145.100.1.5
1542 </Para>
1544 <Screen width="80">
1545 # ip tunnel add sixbone mode sit remote 145.100.1.5 [local 145.100.24.181 ttl 255]
1546 # ip link set sixbone up
1547 # ip addr add 3FFE:604:6:7::2/126 dev sixbone
1548 # ip route add 3ffe::0/16 dev sixbone
1549 </Screen>
1551 <Para>
1552 Let's discuss this. In the first line, we created a tunnel device called
1553 sixbone. We gave it mode sit (which is IPv6 in IPv4 tunneling) and told it
1554 where to go to (remote) and where to come from (local). TTL is set to
1555 maximum, 255. 
1556 </Para>
1558 <Para>
1559 Next, we made the device active (up). After that, we added our own network
1560 address, and set a route for 3ffe::/15 (which is currently all of 6bone)
1561 through the tunnel. If the particular machine you run this on is your IPv6
1562 gateway, then consider adding the following lines:
1563 </Para>
1565 <Screen width="80">
1566 # echo 1 &#62;/proc/sys/net/ipv6/conf/all/forwarding
1567 # /usr/local/sbin/radvd
1568 </Screen>
1570 <Para>
1571 The latter, radvd is -like zebra- a router advertisement daemon, to
1572 support IPv6's autoconfiguration features. Search for it with your favourite
1573 search-engine if you like.
1574 You can check things like this:
1575 </Para>
1577 <Screen width="80">
1578 # /sbin/ip -f inet6 addr
1579 </Screen>
1581 <Para>
1582 If you happen to have radvd running on your IPv6 gateway and boot your
1583 IPv6 capable Linux on a machine on your local LAN, you would be able to
1584 enjoy the benefits of IPv6 autoconfiguration:
1585 </Para>
1587 <Screen width="80">
1588 # /sbin/ip -f inet6 addr
1589 1: lo: &lt;LOOPBACK,UP&gt; mtu 3924 qdisc noqueue inet6 ::1/128 scope host
1591 3: eth0: &lt;BROADCAST,MULTICAST,UP&gt; mtu 1500 qdisc pfifo_fast qlen 100
1592 inet6 3ffe:604:6:8:5054:4cff:fe01:e3d6/64 scope global dynamic
1593 valid_lft forever preferred_lft 604646sec inet6 fe80::5054:4cff:fe01:e3d6/10 
1594 scope link
1595 </Screen>
1597 <Para>
1598 You could go ahead and configure your bind for IPv6 addresses. The A
1599 type has an equivalent for IPv6: AAAA. The in-addr.arpa's equivalent is:
1600 ip6.int. There's a lot of information available on this topic.
1601 </Para>
1603 <Para>
1604 There is an increasing number of IPv6-aware applications available,
1605 including secure shell, telnet, inetd, Mozilla the browser, Apache the
1606 webserver and a lot of others. But this is all outside the scope of this
1607 Routing document ;-)
1608 </Para>
1610 <Para>
1611 On the Cisco side the configuration would be something like this:
1613 <Screen>
1615 interface Tunnel1
1616 description IPv6 tunnel
1617 no ip address
1618 no ip directed-broadcast
1619 ipv6 address 3FFE:604:6:7::1/126
1620 tunnel source Serial0
1621 tunnel destination 145.100.24.181
1622 tunnel mode ipv6ip
1624 ipv6 route 3FFE:604:6:8::/64 Tunnel1
1625 </Screen>
1627 But if you don't have a Cisco at your disposal, try one of the many
1628 IPv6 tunnel brokers available on the Internet. They are willing to configure
1629 their Cisco with an extra tunnel for you. Mostly by means of a friendly
1630 web interface. Search for "ipv6 tunnel broker" on your favourite search engine.
1631 </Para>
1633 </Sect1>
1635 </chapter>
1637   <chapter id="lartc.ipsec">
1638     <Title>IPSEC: secure IP over the Internet</Title>
1639     <Para>
1641       There are two kinds of IPSEC available for Linux these days. For 2.2
1642       and 2.4, there is FreeS/WAN, which was the first major implementation. They
1644       have <ULink URL="http://www.freeswan.org/">an official site</ulink> and <ulink url="http://www.freeswan.ca">
1645         an unofficial one</ulink> that is actually maintained. FreeS/WAN has traditionally not been merged with
1646       the mainline kernel for a number of reasons. Most often mentioned are 'political' issues with Americans
1647       working on crypto tainting its exportability. Furthermore, it does not integrate too well with the Linux kernel,
1648       leading it to be a bad candidate for actual merging. 
1649     </para>
1650     <para>
1651       Additionally, <ulink
1652 url="http://www.edlug.ed.ac.uk/archive/Sep2002/msg00244.html">many</ulink> parties <ulink
1653 url="http://lists.freeswan.org/pipermail/design/2002-November/003901.html">have voiced
1654 worries</ulink> about the quality of the code. To setup FreeS/WAN, a lot of
1655 <ulink
1656 url="http://www.freeswan.ca/code/old/freeswan-Snapshot/doc/index.html">documentation</ulink>
1657 is available.
1658     </para>
1659     <para>
1660       As of Linux 2.5.47, there is a native IPSEC implementation in the kernel. It was written by Alexey Kuznetsov and
1661       Dave Miller, inspired by the work of the USAGI IPv6 group. With its merge, James Morris' CrypoAPI also became 
1662       part of the kernel - it does the actual crypting.
1663     </para>
1664     <para>
1665       This HOWTO will only document the 2.5+ version of IPSEC. FreeS/WAN is recommended for Linux 2.4 users for now, but be aware
1666       that its configuration wil differ from the native IPSEC.
1667     </para>
1668     <para>
1669         As of 2.5.49, IPSEC works without further patches.
1670         </para>
1671         <para>
1672       <note>
1673         <para>
1674           I've collected patches released by Alexey or Dave Miller <ulink
1675 url="http://ds9a.nl/ipsec">here</ulink>. Apply all of them to 2.5.48 before
1676 reporting problems! (as yet, there are none for 2.5.49). Crude userspace utilities are <ulink
1677 url="ftp://ftp.inr.ac.ru/ip-routing/iputils-ss021109-try.tar.bz2">
1678             here</ulink> (<ulink url="http://ds9a.nl/ipsec/setkey.tar.gz">pre-compiled
1679 binary &amp; manpage</ulink>). Compiling these userspace utilities requires editing the Makefiles in there to point them at your
1680           2.5.x kernel. This situation is expected to improve rapidly however.
1681         </para>
1682         <para>
1683         When compiling your kernel, be sure to turn on 'PF_KEY', 'AH', 'ESP' and
1684 everything in the CryptoAPI! TCP_MSS netfilter target is currently broken, 
1685         you need to turn it off.
1686         </para>
1687       </note>
1688       <warning>
1689         <para>
1690           The author of this chapter is a complete IPSEC nitwit! If you find the inevitable mistakes, please email
1691           bert hubert <email>ahu@ds9a.nl</email>. 
1692         </para>
1693       </warning>
1694     </Para>
1695     <para>
1696       First, we'll show how to manually setup secure communication between
1697       two hosts. A large part of this process can also be automated, but
1698 here we'll do it by hand so as to acquaint ourselves with what is going on
1699 'under the hood'. 
1700     </para>
1701     <para>
1702         Feel free to skip the following section if you are only interested
1703 in automatic keying but be aware that some understanding of manual keying is
1704 useful. 
1705     </para>
1706     <sect1 id="lartc.ipsec.intro"><title>Intro with Manual Keying</title>
1707       <para>
1708         IPSEC is a complicated subject. A lot of information is available online, this HOWTO will concentrate on getting you
1709         up and running and explaining the basic principles.
1710       </para>
1711       <para>
1712         <note>
1713           <para>
1714             Many iptables configurations drop IPSEC packets! To pass IPSEC, use: 'iptables -A xxx -p 50 -j ACCEPT' and 'iptables -A xxx -p 51 -j ACCEPT'
1715           </para>
1716         </note>
1717       </para>
1718       <para>
1719         IPSEC offers a secure version of the Internet Protocol. Security in this context means two different things: encryption and authentication. 
1720         A naive vision of security offers only encryption but it can easily be shown that is insufficient - you may be communicating encyphered,
1721         but no guarantee is offered that the remote party is the one you expect it to be.
1722       </para>
1723       <para>
1724         IPSEC supports 'Encapsulated Security Payload' (ESP) for encryption and 'Authentication Header' (AH) for authenticating the remote partner.
1725         You can configure both of them, or decided to do only either.
1726       </para>
1727       <para>
1728         Both ESP and AH rely on security associations. A security association (SA) consists of a source, a destination and an instruction. A sample 
1729         authentication SA may look like this:
1730         <screen>
1731           add 10.0.0.11 10.0.0.216 ah 15700 -A hmac-md5 "1234567890123456";
1732         </screen>
1733         This says 'traffic going from 10.0.0.11 to 10.0.0.216 that needs an AH can be signed using HMAC-MD5 using secret 1234567890123456'. This instruction
1734         is labelled with SPI ('Security Parameter Index') id '15700', more about that later.
1735         The interesting bit about SAs is that they are symmetrical. Both sides of a conversation share exactly the same SA, it is not mirrored on the
1736         other side. Do note however that there is no 'autoreverse' rule - this SA only describes a possible authentication from 10.0.0.11 to 
1737         10.0.0.216. For two-way traffic, two SAs are needed.
1738       </para>
1739       <para>
1740         A sample ESP SA:
1741         <screen>
1742 add 10.0.0.11 10.0.0.216 esp 15701 -E 3des-cbc "123456789012123456789012";
1743         </screen>
1744         This says 'traffic going from 10.0.0.11 to 10.0.0.216 that needs encryption can be encyphered using 3des-cbc with key 123456789012123456789012'. The
1745         SPI id is '15701'.
1746       </para>
1747       <para>
1748         So far, we've seen that SAs describe possible instructions, but do not in fact describe policy as to when these need to be used. In fact,
1749         there could be an arbitrary number of nearly identical SAs with only differing SPI ids. Incidentally, SPI stands for Security Parameter Index.
1750         To do actual crypto, we need to describe a policy. This policy can include things as 'use ipsec if available' or 'drop traffic unless we have ispec'.
1751       </para>
1752       <para>
1753         A typical simple Security Policy (SP) looks like this:
1754         <screen>
1755 spdadd 10.0.0.216 10.0.0.11 any -P out ipsec
1756    esp/transport//require
1757    ah/transport//require;
1758         </screen>
1759         If entered on host 10.0.0.216, this means that all traffic going out to 10.0.0.11 must be encrypted 
1760         and be wrapped in an AH authenticating header. Note that this does not describe which SA is to be used,
1761         that is left as an exercise for the kernel to determine.
1762       </para>
1763         <para>
1764         In other words, a Security Policy specifies WHAT we want; a Security
1765 Association describes HOW we want it. 
1766 </para>
1767       <para>
1768         Outgoing packets are labelled with the SA SPI ('the how') which the
1769         kernel used for encryption and authentication so the remote can
1770         lookup the corresponding verification and decryption instruction.
1771       </para>
1773       <para>
1774         What follows is a very simple configuration for talking from host 10.0.0.216 to 10.0.0.11 using 
1775         encryption and authentication. Note that the reverse path is plaintext in this first version and that
1776         this configuration should not be deployed.
1777       </para>
1778       <para>
1779         On host 10.0.0.216:
1780         <screen>
1781 #!/sbin/setkey -f
1782 add 10.0.0.216 10.0.0.11 ah 24500 -A hmac-md5 "1234567890123456";          
1783 add 10.0.0.216 10.0.0.11 esp 24501 -E 3des-cbc "123456789012123456789012";
1785 spdadd 10.0.0.216 10.0.0.11 any -P out ipsec
1786    esp/transport//require
1787    ah/transport//require;
1788         </screen>
1789       </para>
1790       <para>
1791         On host 10.0.0.11, the same Security Associations, no Security Policy:
1792         <screen>
1793 #!/sbin/setkey -f
1794 add 10.0.0.216 10.0.0.11 ah 24500 -A hmac-md5 "1234567890123456";
1795 add 10.0.0.216 10.0.0.11 esp 24501 -E 3des-cbc "123456789012123456789012";
1796         </screen>
1797       </para>
1798       <para>
1799         With the above configuration in place (these files can be executed if 'setkey' is installed in /sbin),
1800         'ping 10.0.0.11' from 10.0.0.216 looks like this using tcpdump:
1801         <screen>
1802 22:37:52 10.0.0.216 &gt; 10.0.0.11: AH(spi=0x00005fb4,seq=0xa): ESP(spi=0x00005fb5,seq=0xa) (DF)
1803 22:37:52 10.0.0.11 &gt; 10.0.0.216: icmp: echo reply
1804         </screen>
1805         Note how the ping back from 10.0.0.11 is indeed plainly visible. The forward ping cannot be read by tcpdump
1806         of course, but it does show the Security Parameter Index of AH and ESP, which tells 10.0.0.11 how to 
1807         verify the authenticity of our packet and how to decrypt it.
1808       </para>
1809       <para>
1810         A few things must be mentioned however. The configuration above is shown in a lot of IPSEC examples and it is very dangerous.
1811         The problem is that the above contains policy on how 10.0.0.216 should treat packets going to 10.0.0.11, and that it explains how 10.0.0.11
1812         should treat those packets but it does NOT instruct 10.0.0.11 to discard unauthenticated or unencrypted traffic! 
1813       </para>
1814       <para>
1815         Anybody can now insert spoofed and completely unencrypted data and 10.0.0.11 will accept it. To remedy the above, we need an incoming 
1816         Security Policy on 10.0.0.11, as follows:
1817         <screen>
1818 #!/sbin/setkey -f 
1819 spdadd 10.0.0.216 10.0.0.11 any -P IN ipsec
1820    esp/transport//require
1821    ah/transport//require;
1822         </screen>
1823         This instructs 10.0.0.11 that any traffic coming to it from 10.0.0.216 is required to have valid ESP and AH.
1824       </para>
1825       <para>
1826         Now, to complete this configuration, we need return traffic to be encrypted and authenticated as well of course. The full configuration on
1827         10.0.0.216:
1828         <screen>
1829 #!/sbin/setkey -f
1830 flush;
1831 spdflush;
1833 # AH
1834 add 10.0.0.11 10.0.0.216 ah 15700 -A hmac-md5 "1234567890123456";
1835 add 10.0.0.216 10.0.0.11 ah 24500 -A hmac-md5 "1234567890123456";
1837 # ESP
1838 add 10.0.0.11 10.0.0.216 esp 15701 -E 3des-cbc "123456789012123456789012";
1839 add 10.0.0.216 10.0.0.11 esp 24501 -E 3des-cbc "123456789012123456789012";
1841 spdadd 10.0.0.216 10.0.0.11 any -P out ipsec
1842            esp/transport//require
1843            ah/transport//require;
1845 spdadd 10.0.0.11 10.0.0.216 any -P in ipsec
1846            esp/transport//require
1847            ah/transport//require;
1848           
1849         </screen>
1850       </para>
1851       <para>
1852         And on 10.0.0.11:
1853         <screen>
1854 #!/sbin/setkey -f
1855 flush;
1856 spdflush;
1858 # AH
1859 add 10.0.0.11 10.0.0.216 ah 15700 -A hmac-md5 "1234567890123456";
1860 add 10.0.0.216 10.0.0.11 ah 24500 -A hmac-md5 "1234567890123456";
1862 # ESP
1863 add 10.0.0.11 10.0.0.216 esp 15701 -E 3des-cbc "123456789012123456789012";
1864 add 10.0.0.216 10.0.0.11 esp 24501 -E 3des-cbc "123456789012123456789012";
1867 spdadd 10.0.0.11 10.0.0.216 any -P out ipsec
1868            esp/transport//require
1869            ah/transport//require;
1871 spdadd 10.0.0.216 10.0.0.11 any -P in ipsec
1872            esp/transport//require
1873            ah/transport//require;
1875         </screen>
1876       </para>
1877       <para>
1878         Note that in this example we used identical keys for both directions of traffic. This is not in any way required however.
1879       </para>
1880       <para>
1881         To examine the configuration we just created, execute <command>setkey -D</command>, which shows the Security Associations or 
1882         <command>setkey -DP</command> which shows the configured policies.
1883       </para>
1884     </sect1>
1885     <sect1 id="lartc.ipsec.automatic.keying"><title>Automatic keying</title>
1886       <para>
1887         In the previous section, encryption was configured using simple shared secrets. In other words, to remain secure,
1888         we need to transfer our encryption configuration over a trusted channel. If we were to configure the remote host 
1889         over telnet, any third party would know our shared secret and the setup would not be secure.
1890       </para>
1891       <para>
1892         Furthermore, because the secret is shared, it is not a secret. The remote can't do a lot with our secret, but we do 
1893         need to make sure that we use a different secret for communicating with all our partners. This requires a large number of keys,
1894         if there are 10 parties, this needs at least 50 different secrets. 
1895       </para>
1896       <para>
1897         Besides the symmetric key problem, there is also the need for key rollover. If a third party manages to sniff enough traffic,
1898         it may be in a position to reverse engineer the key. This is prevented by moving to a new key every once in a while but that is
1899         a process that needs to be automated.
1900       </para>
1901       <para>
1902         Another problem is that with manual keying as described above we exactly define the algorithms and key lengths used, something
1903         that requires a lot of coordination with the remote party. It is desireable to be able to have the ability to describe a 
1904         broader key policy such as 'We can do 3DES and Blowfish with at least the following key lengths'.
1905       </para>
1906       <para>
1907         To solve these isses, IPSEC provides Internet Key Exchange to automatically exchange randomly generated keys which are
1908         transmitted using asymmetric encryption technology, according to negotiated algorithm details.
1909       </para>
1910       <para>
1911         The Linux 2.5 IPSEC implementation works with the KAME 'racoon' IKE
1912         daemon. As of 9 November, the racoon version in Alexey's iptools
1913         distribution can be compiled, although you may need to remove 
1914 #include &lt;net/route.h&gt; in two files. Alternatively, I've supplied a
1915 <ulink url="http://ds9a.nl/ipsec/racoon.bz2">precompiled version</ulink>.
1916       </para>
1917         <para>
1918         <note>
1919           <para>
1920                 IKE needs access to UDP port 500, be sure that iptables does
1921 not block it.
1922           </para>
1923         </note>
1924         </para>
1925         <sect2 id="lartc.ipsec.keying.theory"><title>Theory</title>
1926         <para>
1927                 As explained before, automatic keying does a lot of the work
1928 for us. Specifically, it creates Security Associations on the fly. It does
1929 not however set policy for us, which is as it should be.
1930         </para>
1931         <para>
1932         So, to benefit from IKE, setup a policy, but do not supply any
1933 SAs. If the kernel discovers that there is an IPSEC policy, but no Security
1934 Association, it will notify the IKE daemon, which then goes to work on
1935 trying to negotiate one.
1936         </para>
1937         <para>
1938         Reiterating, a Security Policy specifies WHAT we want; a Security
1939 Association describes HOW we want it. Using automatic keying lets us get
1940 away with only specifying what we want.
1941         </para>
1942 </sect2>
1943         <sect2 id="lartc.ipsec.automatic.keying.example"><title>Example</title>
1944         <para>
1945         Kame racoon comes with a grand host of options, most of which have
1946 very fine default values, so we don't need to touch them. As described
1947 above, the operator needs to define a Security Policy, but no Security
1948 Associations. We leave their negotiation to the IKE daemon.
1949         </para>
1950         <para>
1951         In this example, 10.0.0.11 and 10.0.0.216 are once again going to
1952 setup secure communications, but this time with help from racoon. For
1953 simplicity this configuration will be using pre-shared keys, the
1954 dreaded 'shared secrets'. X.509 certificates are discussed in a separate
1955 section, see <xref linkend="lartc.ipsec.x509">.
1956 </para>
1957 <para> We're
1958 going to stick to almost the default configuration, identical on both hosts:
1959         </para> 
1960         <para>
1961 <screen>
1962 path pre_shared_key "/usr/local/etc/racoon/psk.txt";
1964 remote anonymous
1966         exchange_mode aggressive,main;
1967         doi ipsec_doi;
1968         situation identity_only;
1970         my_identifier address;
1972         lifetime time 2 min;   # sec,min,hour
1973         initial_contact on;
1974         proposal_check obey;    # obey, strict or claim
1976         proposal {
1977                 encryption_algorithm 3des;
1978                 hash_algorithm sha1;
1979                 authentication_method pre_shared_key;
1980                 dh_group 2 ;
1981         }
1984 sainfo anonymous
1986         pfs_group 1;
1987         lifetime time 2 min;
1988         encryption_algorithm 3des ;
1989         authentication_algorithm hmac_sha1;
1990                 compression_algorithm deflate ;
1992 </screen>
1993         </para>
1994         <para>
1995         Lots of settings - I think yet more can be removed to get closer to
1996 the default configuration. A few noteworthy things. We've configured two
1997 anonymous settings which hold for all remotes, making further configuration
1998 easy. There is no need for per-host stanzas here, unless we really want
1999 them.
2000 </para>
2001         <para>
2002         Furthermore, we've set it up such that we identify ourselves based
2003 on our IP address ('my_identifier address'), and declare that we can do
2004 3des, sha1, and that we will be using a pre-shared key, located in psk.txt.
2005         </para>
2006         <para>
2007         In psk.txt, we now setup two entries, which do differ on both hosts.
2008 On 10.0.0.11:
2009 <screen>
2010 10.0.0.216      password2
2011 </screen>
2012 On 10.0.0.216:
2013 <screen>
2014 10.0.0.11       password2
2015 </screen>
2016         Make sure these files are owned by root, and set to mode 0600,
2017 racoon will not trust their contents otherwise. Note that these files are
2018 mirrors from eachother.
2019         </para>
2020         <para>
2021         Now we are ready to setup our desired policy, which is simple
2022 enough. On host 10.0.0.216:
2023 <screen>
2024 #!/sbin/setkey -f
2025 flush;
2026 spdflush;
2028 spdadd 10.0.0.216 10.0.0.11 any -P out ipsec
2029         esp/transport//require;
2031 spdadd 10.0.0.11 10.0.0.216 any -P in ipsec
2032         esp/transport//require;
2033 </screen>
2034 And on 10.0.0.11:
2035 <screen>
2036 #!/sbin/setkey -f
2037 flush;
2038 spdflush;
2040 spdadd 10.0.0.11 10.0.0.216 any -P out ipsec
2041         esp/transport//require;
2043 spdadd 10.0.0.216 10.0.0.11 any -P in ipsec
2044         esp/transport//require;
2045 </screen>
2046 Note how again these policies are mirrored.
2047         </para>
2048         <para>
2049         We are now ready to launch racoon! Once launched, the moment we try
2050 to telnet from 10.0.0.11 to 10.0.0.216, or the other way around, racoon
2051 will start negotiating:
2052 <screen>
2053 12:18:44: INFO: isakmp.c:1689:isakmp_post_acquire(): IPsec-SA
2054   request for 10.0.0.11 queued due to no phase1 found.
2055 12:18:44: INFO: isakmp.c:794:isakmp_ph1begin_i(): initiate new
2056   phase 1 negotiation: 10.0.0.216[500]<=>10.0.0.11[500]
2057 12:18:44: INFO: isakmp.c:799:isakmp_ph1begin_i(): begin Aggressive mode.
2058 12:18:44: INFO: vendorid.c:128:check_vendorid(): received Vendor ID: 
2059   KAME/racoon
2060 12:18:44: NOTIFY: oakley.c:2037:oakley_skeyid(): couldn't find
2061   the proper pskey, try to get one by the peer's address.
2062 12:18:44: INFO: isakmp.c:2417:log_ph1established(): ISAKMP-SA
2063   established 10.0.0.216[500]-10.0.0.11[500] spi:044d25dede78a4d1:ff01e5b4804f0680
2064 12:18:45: INFO: isakmp.c:938:isakmp_ph2begin_i(): initiate new phase 2 
2065   negotiation: 10.0.0.216[0]<=>10.0.0.11[0]
2066 12:18:45: INFO: pfkey.c:1106:pk_recvupdate(): IPsec-SA established: 
2067   ESP/Transport 10.0.0.11->10.0.0.216 spi=44556347(0x2a7e03b)
2068 12:18:45: INFO: pfkey.c:1318:pk_recvadd(): IPsec-SA established:
2069   ESP/Transport 10.0.0.216->10.0.0.11 spi=15863890(0xf21052)
2070 </screen>
2071 </para>
2072 <para>
2073         If we now run setkey -D, which shows the Security Associations, they
2074 are indeed there:
2075 <screen>
2076 10.0.0.216 10.0.0.11 
2077         esp mode=transport spi=224162611(0x0d5c7333) reqid=0(0x00000000)
2078         E: 3des-cbc  5d421c1b d33b2a9f 4e9055e3 857db9fc 211d9c95 ebaead04
2079         A: hmac-sha1  c5537d66 f3c5d869 bd736ae2 08d22133 27f7aa99
2080         seq=0x00000000 replay=4 flags=0x00000000 state=mature 
2081         created: Nov 11 12:28:45 2002   current: Nov 11 12:29:16 2002
2082         diff: 31(s)     hard: 600(s)    soft: 480(s)
2083         last: Nov 11 12:29:12 2002      hard: 0(s)      soft: 0(s)
2084         current: 304(bytes)     hard: 0(bytes)  soft: 0(bytes)
2085         allocated: 3    hard: 0 soft: 0
2086         sadb_seq=1 pid=17112 refcnt=0
2087 10.0.0.11 10.0.0.216 
2088         esp mode=transport spi=165123736(0x09d79698) reqid=0(0x00000000)
2089         E: 3des-cbc  d7af8466 acd4f14c 872c5443 ec45a719 d4b3fde1 8d239d6a
2090         A: hmac-sha1  41ccc388 4568ac49 19e4e024 628e240c 141ffe2f
2091         seq=0x00000000 replay=4 flags=0x00000000 state=mature 
2092         created: Nov 11 12:28:45 2002   current: Nov 11 12:29:16 2002
2093         diff: 31(s)     hard: 600(s)    soft: 480(s)
2094         last:                           hard: 0(s)      soft: 0(s)
2095         current: 231(bytes)     hard: 0(bytes)  soft: 0(bytes)
2096         allocated: 2    hard: 0 soft: 0
2097         sadb_seq=0 pid=17112 refcnt=0
2098 </screen>
2099 As are the Security Policies we configured ourselves:
2100 <screen>
2101 10.0.0.11[any] 10.0.0.216[any] tcp
2102         in ipsec
2103         esp/transport//require
2104         created:Nov 11 12:28:28 2002 lastused:Nov 11 12:29:12 2002
2105         lifetime:0(s) validtime:0(s)
2106         spid=3616 seq=5 pid=17134
2107         refcnt=3
2108 10.0.0.216[any] 10.0.0.11[any] tcp
2109         out ipsec
2110         esp/transport//require
2111         created:Nov 11 12:28:28 2002 lastused:Nov 11 12:28:44 2002
2112         lifetime:0(s) validtime:0(s)
2113         spid=3609 seq=4 pid=17134
2114         refcnt=3
2115 </screen>
2116         <sect3><title>Problems and known defects</title>
2117         <para>
2118                 If this does not work, check that all configuration files
2119 are owned by root, and can only be read by root. To start racoon on the
2120 foreground, use '-F'. To force it to read a certain configuration file,
2121 instead of at the compiled location, use '-f'. For staggering amounts of
2122 detail, add a 'log debug;' statement to racoon.conf.
2123         </para>
2124         </sect2>
2125         <sect2 id="lartc.ipsec.x509"><title>Automatic keying using X.509 certificates</title>
2126         <para>
2127         As mentioned before, the use of shared secrets is hard because they
2128 aren't easily shared and once shared, are no longer secret. Luckily, there
2129 is asymmetric encryption technology to help resolve this.
2130         </para>
2131         <para>
2132         If each IPSEC participant makes a public and a private key, secure
2133 communications can be setup by both parties publishing their public key, and
2134 configuring policy.
2135         </para>
2136         <para>
2137         Building a key is relatively easy, although it requires some work.
2138 The following is based on the 'openssl' tool. 
2139         </para>
2140         <sect3><title>Building an X.509 certificate for your host</title>
2141         <para>
2142         OpenSSL has a lot of infrastructure for keys that may or may not be
2143 signed by certificate authorities. Right now, we need to circumvent all that
2144 infrastructure and practice some good old Snake Oil security, and do without
2145 a certificate authority.
2146         </para>
2147         <para>
2148         First we issue a 'certificate request' for our host, called
2149 'laptop':
2150 <screen>
2151 $ openssl req -new -nodes -newkey rsa:1024 -sha1 -keyform PEM -keyout \
2152   laptop.private -outform PEM -out request.pem
2153 </screen>
2154 This asks us some questions:
2155 <screen>
2156 Country Name (2 letter code) [AU]:NL
2157 State or Province Name (full name) [Some-State]:.
2158 Locality Name (eg, city) []:Delft
2159 Organization Name (eg, company) [Internet Widgits Pty Ltd]:Linux Advanced
2160 Routing & Traffic Control
2161 Organizational Unit Name (eg, section) []:laptop
2162 Common Name (eg, YOUR name) []:bert hubert
2163 Email Address []:ahu@ds9a.nl
2165 Please enter the following 'extra' attributes
2166 to be sent with your certificate request
2167 A challenge password []:
2168 An optional company name []:
2169 </screen>
2170         It is left to your own discretion how completely you want to fill
2171 this out. You may or may not want to put your hostname in there, depending
2172 on your security needs. In this example, we have.
2173 </para>
2174         <para>
2175         We'll now 'self sign' this request:
2176 <screen>
2177 $ openssl x509 -req -in request.pem -signkey laptop.private -out \
2178   laptop.public
2179 Signature ok
2180 subject=/C=NL/L=Delft/O=Linux Advanced Routing &amp; Traffic \
2181   Control/OU=laptop/CN=bert hubert/Email=ahu@ds9a.nl
2182 Getting Private key
2183 </screen>       
2184         The 'request.pem' file can now be discarded. 
2185         </para>
2186         <para>
2187         Repeat this procedure for all hosts you need a key for. You can
2188 distribute the '.public' file with impunity, but keep the '.private' one
2189 private!
2190         </para>
2191         </sect3>
2192         <sect3><title>Setting up and launching</title>
2193         <para>
2194         Once we have a public and a private key for our hosts we can tell
2195         racoon to use them.
2196         </para>
2197         <para>
2198         We return to our previous configuration and the two hosts, 10.0.0.11
2199 ('upstairs') and 10.0.0.216 ('laptop').
2200         </para>
2201         <para>
2202         To the <filename>racoon.conf</filename> file on 10.0.0.11, we add:
2203 <screen>
2204 path certificate "/usr/local/etc/racoon/certs";
2206 remote 10.0.0.216
2208         exchange_mode aggressive,main;
2209         my_identifier asn1dn;
2210         peers_identifier asn1dn;
2212         certificate_type x509 "upstairs.public" "upstairs.private";
2214         peers_certfile "laptop.public";
2215         proposal {
2216                 encryption_algorithm 3des;
2217                 hash_algorithm sha1;
2218                 authentication_method rsasig;
2219                 dh_group 2 ;
2220         }
2222 </screen>
2223         This tells racoon that certificates are to be found in
2224 <filename>/usr/local/etc/racoon/certs/</filename>. Furthermore, it contains
2225 configuration items specific for remote 10.0.0.216.
2226         </para>
2227         <para>
2228         The 'asn1dn' lines tell racoon that the identifier for both the
2229 local and remote ends are to be extracted from the public keys. This is the
2230 'subject=/C=NL/L=Delft/O=Linux Advanced Routing &amp; Traffic 
2231   Control/OU=laptop/CN=bert hubert/Email=ahu@ds9a.nl' output from above.
2232         </para>
2233         <para>
2234         The <command>certificate_type</command> line configures the local
2235 public and private key. The <command>peers_certfile</command> statement
2236 configures racoon to read the public key of the remote peer from the file
2237 <filename>laptop.public</filename>.
2238         </para>
2239         <para>
2240         The <command>proposal</command> stanza is unchanged from what we've
2241 seen earlier, with the exception that the
2242 <command>authentication_method</command> is now <command>rsasig</command>,
2243 indicating the use of RSA public/private keys for authentication.
2244         </para>
2245         <para>
2246         The addition to the configuration of 10.0.0.216 is nearly identical, except for the
2247 usual mirroring:
2248 <screen>
2249 path certificate "/usr/local/etc/racoon/certs";
2251 remote 10.0.0.11
2253         exchange_mode aggressive,main;
2254         my_identifier asn1dn;
2255         peers_identifier asn1dn;
2257         certificate_type x509 "laptop.public" "laptop.private";
2259         peers_certfile "upstairs.public";
2261         proposal {
2262                 encryption_algorithm 3des;
2263                 hash_algorithm sha1;
2264                 authentication_method rsasig;
2265                 dh_group 2 ;
2266         }
2268 </screen>
2269 </para>
2270         <para>
2271         Now that we've added these statements to both hosts, we only need to
2272 move the key files in place. The 'upstairs' machine needs 
2273 <filename>upstairs.private</filename>, <filename>upstairs.public</filename>, 
2274 and <filename>laptop.public</filename> in
2275 <filename>/usr/local/etc/racoon/certs</filename>. Make sure that this
2276 directory is owned by root and has mode 0600 or racoon may refuse to read
2278 </para>
2279 <para>
2280 The 'laptop' machine needs 
2281 <filename>laptop.private</filename>, <filename>laptop.public</filename>, 
2282 and <filename>upstairs.public</filename> in
2283 <filename>/usr/local/etc/racoon/certs</filename>. In other words, each host
2284 needs its own public and private key and additionally, the public key of the
2285 remote.
2286 </para>
2287 <para>
2288         Verify that a Security Policy is in place (execute the 'spdadd' lines in
2289 <xref linkend="lartc.ipsec.automatic.keying.example">). Then launch racoon and everything should
2290 work.
2291         </sect3>
2292         <sect3><title>How to setup tunnels securely</title>
2293         <para>
2294         To setup secure communications with a remote party, we must exchange
2295 public keys. While the public key does not need to be kept a secret, on the
2296 contrary, it is very important to be sure that it is in fact the unaltered
2297 key. In other words, you need to be certain there is no 'man in the middle'.
2298         </para>
2299         <para>
2300 To make this easy, OpenSSL provides the 'digest' command:
2301 <screen>
2302 $ openssl dgst upstairs.public 
2303 MD5(upstairs.public)= 78a3bddafb4d681c1ca8ed4d23da4ff1
2304 </screen>
2305 </para>
2306         <para>
2307         Now all we need to do is verify if our remote partner sees the same
2308 digest. This might be done by meeting in real life or perhaps over the
2309 phone, making sure the number of the remote party was not in fact sent over
2310 the same email containing the key!
2311         </para>
2312         <para>
2313         Another way of doing this is the use of a Trusted Third Party which
2314 runs a Certificate Authority. This CA would then sign your key, which we've
2315 done ourselves above.
2316         </para>
2317         
2318         </sect3>
2319     </sect2>
2321     </sect1>
2322     <sect1 id="lartc.ipsec.tunnel"><title>IPSEC tunnels</title>
2323       <para>
2324         So far, we've only seen IPSEC in so called 'transport' mode where both endpoints understand IPSEC directly. As this is often not
2325         the case, it may be necessary to have only routers understand IPSEC, and have them do the work for the hosts behind them. 
2326         This is called 'tunnel mode'.
2327       </para>
2328       <para>
2329         Setting this up is a breeze. To tunnel all traffic to 130.161.0.0/16 from 10.0.0.216 via 10.0.0.11, we issue the following on
2330         10.0.0.216:
2331 <screen>
2332 #!/sbin/setkey -f
2333 flush;
2334 spdflush;
2336 add 10.0.0.216 10.0.0.11 esp 34501
2337         -m tunnel
2338         -E 3des-cbc "123456789012123456789012";
2340 spdadd 10.0.0.0/24 130.161.0.0/16 any -P out ipsec
2341            esp/tunnel/10.0.0.216-10.0.0.11/require;
2342 </screen>
2343         Note the '-m tunnel', it is vitally important! This first configures an ESP encryption SA between our tunnel endpoints,
2344         10.0.0.216 and 10.0.0.11. 
2345       </para>
2346       <para>
2347         Next the actual tunnel is configured. It instructs the kernel to encrypt all traffic it has to route from 10.0.0.0/24 to
2348         130.161.0.0. Furthermore, this traffic then has to be shipped to 10.0.0.11.
2349       </para>
2350       <para>
2351         10.0.0.11 also needs some configuration:
2352 <screen>
2353 #!/sbin/setkey -f
2354 flush;
2355 spdflush;
2357 add 10.0.0.216 10.0.0.11 esp 34501
2358         -m tunnel
2359         -E 3des-cbc "123456789012123456789012";
2361 spdadd 10.0.0.0/24 130.161.0.0/16 any -P in ipsec
2362            esp/tunnel/10.0.0.216-10.0.0.11/require;
2363 </screen>
2364         Note that this is exactly identical, except for the change from '-P out' to '-P in'. As with earlier examples,
2365         we've now only configured traffic going one way. Completing the other half of the tunnel is left as an
2366         exercise for the reader.
2367       </para>
2368       <para>
2369         Another name for this setup is 'proxy ESP', which is somewhat clearer.
2370       </para>
2371       <para>
2372         <note>
2373           <para>
2374             The IPSEC tunnel needs to have IP Forwarding enabled in the kernel!
2375           </para>
2376         </note>
2377       </para>
2378     </sect1>
2379     <sect1 id="lartc.ipsec.other"><title>Other IPSEC software</title>
2380       <para>
2381         Thomas Walpuski reports that he wrote a patch to make OpenBSD isakpmd work with Linux 2.5 IPSEC.
2382         It can be found <ulink url="http://bender.thinknerd.de/~thomas/IPsec/isakmpd-linux-2.5.diff">on his page</ulink>.
2383         He notes that isakpmd on Linux depends only on <filename>libkeynote</filename>. According to Thomas, it works quite well
2384         with Linux 2.5.59. 
2385       </para>
2386       <para>
2388         isakpmd is quite different from racoon mentioned above but many
2389         people like it. It can be found <ulink
2390 url="http://www.openbsd.org/cgi-bin/cvsweb/src/sbin/isakmpd/">here</ulink>.
2391 Read more about OpenBSD CVS <ulink
2392 url="http://www.openbsd.org/anoncvs.html">here</ulink>. Thomas also made a
2393 <ulink
2394 url="http://bender.thinknerd.de/~thomas/IPsec/isakmpd.tgz">tarball</ulink>
2395 available for those uncomfortable with CVS or patch.
2397       </para>
2398     </sect1>
2399     <sect1 id="lartc.ipsec.interop"><title>IPSEC interoperation with other systems</title>
2400       <para>
2401         FIXME: Write this
2402       </para>
2403       <sect2 id="lartc.ipsec.interop.win32"><title>Windows</title>
2404         <para>
2405           FIXME: Write this
2406         </para>
2407       </sect2>
2408     </sect1>
2410   </chapter>
2412 <chapter id="lartc.multicast">
2413 <Title>Multicast routing</Title>
2415 <Para>
2416 FIXME: Editor Vacancy!
2417 </Para>
2419 <Para>
2420 The Multicast-HOWTO is ancient (relatively-speaking) and may be inaccurate
2421 or misleading in places, for that reason.
2422 </Para>
2424 <Para>
2425 Before you can do any multicast routing, you need to configure the Linux
2426 kernel to support the type of multicast routing you want to do. This, in
2427 turn, requires you to decide what type of multicast routing you expect to
2428 be using. There are essentially four "common" types - DVMRP (the Multicast
2429 version of the RIP unicast protocol), MOSPF (the same, but for OSPF), PIM-SM
2430 ("Protocol Independent Multicasting - Sparse Mode", which assumes that users
2431 of any multicast group are spread out, rather than clumped) and PIM-DM (the
2432 same, but "Dense Mode", which assumes that there will be significant clumps
2433 of users of the same multicast group).
2434 </Para>
2436 <Para>
2437 In the Linux kernel, you will notice that these options don't appear. This is
2438 because the protocol itself is handled by a routing application, such as
2439 Zebra, mrouted, or pimd. However, you still have to have a good idea of which
2440 you're going to use, to select the right options in the kernel.
2441 </Para>
2443 <Para>
2444 For all multicast routing, you will definitely need to enable "multicasting"
2445 and "multicast routing". For DVMRP and MOSPF, this is sufficient. If you are
2446 going to use PIM, you must also enable PIMv1 or PIMv2, depending on whether
2447 the network you are connecting to uses version 1 or 2 of the PIM protocol.
2448 </Para>
2450 <Para>
2451 Once you have all that sorted out, and your new Linux kernel compiled, you
2452 will see that the IP protocols listed, at boot time, now include IGMP. This
2453 is a protocol for managing multicast groups. At the time of writing, Linux
2454 supports IGMP versions 1 and 2 only, although version 3 does exist and has
2455 been documented. This doesn't really affect us that much, as IGMPv3 is still
2456 new enough that the extra capabilities of IGMPv3 aren't going to be that
2457 much use. Because IGMP deals with groups, only the features present in the
2458 simplest version of IGMP over the entire group are going to be used. For the
2459 most part, that will be IGMPv2, although IGMPv1 is sill going to be
2460 encountered.
2461 </Para>
2463 <Para>
2464 So far, so good. We've enabled multicasting. Now, we have to tell the Linux
2465 kernel to actually do something with it, so we can start routing. This means
2466 adding the Multicast virtual network to the router table:
2467 </Para>
2469 <Para>
2470 ip route add 224.0.0.0/4 dev eth0
2471 </Para>
2473 <Para>
2474 (Assuming, of course, that you're multicasting over eth0! Substitute the
2475 device of your choice, for this.)
2476 </Para>
2478 <Para>
2479 Now, tell Linux to forward packets...
2480 </Para>
2482 <Para>
2483 echo 1 &#62; /proc/sys/net/ipv4/ip_forward
2484 </Para>
2486 <Para>
2487 At this point, you may be wondering if this is ever going to do anything. So,
2488 to test our connection, we ping the default group, 224.0.0.1, to see if anyone
2489 is alive. All machines on your LAN with multicasting enabled <Emphasis>should</Emphasis>
2490 respond, but nothing else. You'll notice that none of the machines that
2491 respond have an IP address of 224.0.0.1. What a surprise! :) This is a group
2492 address (a "broadcast" to subscribers), and all members of the group will
2493 respond with their own address, not the group address.
2494 </Para>
2496 <Para>
2497 ping -c 2 224.0.0.1
2498 </Para>
2500 <Para>
2501 At this point, you're ready to do actual multicast routing. Well, assuming
2502 that you have two networks to route between.
2503 </Para>
2505 <Para>
2506 (To Be Continued!)
2507 </Para>
2509 </chapter>
2511 <chapter id="lartc.qdisc">
2512 <Title>Queueing Disciplines for Bandwidth Management</Title>
2514 <Para>
2515 Now, when I discovered this, it <Emphasis>really</Emphasis> blew me away. Linux 2.2/2.4
2516 comes with everything to manage bandwidth in ways comparable to high-end
2517 dedicated bandwidth management systems.
2518 </Para>
2520 <Para>
2521 Linux even goes far beyond what Frame and ATM provide. 
2522 </Para>
2524 <Para>Just to prevent confusion, <command>tc</command> uses the following 
2525 rules for bandwith specification:
2527 <literallayout class='monospaced'>
2528 mbps = 1024 kbps = 1024 * 1024 bps =&#62; byte/s
2529 mbit = 1024 kbit =&#62; kilo bit/s.
2530 mb = 1024 kb = 1024 * 1024 b =&#62; byte
2531 mbit = 1024 kbit =&#62; kilo bit.
2532 </literallayout>
2534 Internally, the number is stored in bps and b.
2535 </Para>
2537 <Para>But when <command>tc</command> prints the rate, it uses following :
2538 </Para>
2540 <literallayout class='monospaced'>
2541 1Mbit = 1024 Kbit = 1024 * 1024 bps =&#62; byte/s
2542 </literallayout>
2544 <Sect1 id="lartc.qdisc.explain">
2545   <Title>Queues and Queueing Disciplines explained</Title>
2547 <Para>
2548 With queueing we determine the way in which data is <Emphasis>SENT</Emphasis>.
2549 It is important to realise that we can only shape data that we transmit.
2550 </Para>
2552 <Para>
2553 With the way the Internet works, we have no direct control of what people
2554 send us. It's a bit like your (physical!) mailbox at home. There is no way
2555 you can influence the world to modify the amount of mail they send you,
2556 short of contacting everybody.
2557 </Para>
2559 <Para>
2560 However, the Internet is mostly based on TCP/IP which has a few features
2561 that help us. TCP/IP has no way of knowing the capacity of the network
2562 between two hosts, so it just starts sending data faster and faster ('slow
2563 start') and when packets start getting lost, because there is no room to
2564 send them, it will slow down. In fact it is a bit smarter than this, but
2565 more about that later.
2566 </Para>
2568 <Para>
2569 This is the equivalent of not reading half of your mail, and hoping that
2570 people will stop sending it to you. With the difference that it works for
2571 the Internet :-)
2572 </Para>
2574 <Para>
2575 If you have a router and wish to prevent certain hosts within your network
2576 from downloading too fast, you need to do your shaping on the *inner* interface
2577 of your router, the one that sends data to your own computers.
2578 </Para>
2580 <Para>
2581 You also have to be sure you are controlling the bottleneck of the link.
2582 If you have a 100Mbit NIC and you have a router that has a 256kbit link,
2583 you have to make sure you are not sending more data than your router can
2584 handle.  Otherwise, it will be the router who is controlling the link and
2585 shaping the available bandwith. We need to 'own the queue' so to speak, and
2586 be the slowest link in the chain. Luckily this is easily possible.
2587 </Para>
2589 </Sect1>
2591 <Sect1 id="lartc.qdisc.classless">
2592   <Title>Simple, classless Queueing Disciplines</Title>
2594 <Para>
2595 As said, with queueing disciplines, we change the way data is sent.
2596 Classless queueing disciplines are those that, by and large accept data and
2597 only reschedule, delay or drop it.
2598 </Para>
2600 <Para>
2601 These can be used to shape traffic for an entire interface, without any
2602 subdivisions. It is vital that you understand this part of queueing before
2603 we go on the the classful qdisc-containing-qdiscs!
2604 </Para>
2606 <Para>
2607 By far the most widely used discipline is the pfifo_fast qdisc - this is the
2608 default. This also explains why these advanced features are so robust. They
2609 are nothing more than 'just another queue'.
2610 </Para>
2612 <Para>
2613 Each of these queues has specific strengths and weaknesses. Not all of them
2614 may be as well tested.
2615 </Para>
2617 <Sect2>
2618 <Title>pfifo_fast</Title>
2620 <Para>
2621 This queue is, as the name says, First In, First Out, which means that no
2622 packet receives special treatment. At least, not quite. This queue has 3 so
2623 called 'bands'. Within each band, FIFO rules apply. However, as long as
2624 there are packets waiting in band 0, band 1 won't be processed. Same goes
2625 for band 1 and band 2.
2626 </Para>
2628 <Para>
2629 The kernel honors the so called Type of Service flag of packets, and takes
2630 care to insert 'minimum delay' packets in band 0. 
2631 </Para>
2633 <Para>
2634 Do not confuse this classless simple qdisc with the classful PRIO one!
2635 Although they behave similarly, pfifo_fast is classless and you cannot add
2636 other qdiscs to it with the tc command.
2637 </Para>
2639 <Sect3>
2640 <Title>Parameters &amp; usage</Title>
2642 <Para>
2643 You can't configure the pfifo_fast qdisc as it is the hardwired default.
2644 This is how it is configured by default:
2645 <VariableList>
2647 <VarListEntry>
2648 <Term>priomap</Term>
2649 <ListItem>
2650 <Para>
2651 Determines how packet priorities, as assigned by the kernel, map to bands.
2652 Mapping occurs based on the TOS octet of the packet, which looks like this:
2653 </Para>
2655 <Para>
2657 <Screen>
2658    0     1     2     3     4     5     6     7
2659 +-----+-----+-----+-----+-----+-----+-----+-----+
2660 |                 |                       |     |
2661 |   PRECEDENCE    |          TOS          | MBZ |
2662 |                 |                       |     |
2663 +-----+-----+-----+-----+-----+-----+-----+-----+
2664 </Screen>
2666 </Para>
2668 <Para>
2669 The four TOS bits (the 'TOS field') are defined as:
2671 <Screen>
2672 Binary Decimcal  Meaning
2673 -----------------------------------------
2674 1000   8         Minimize delay (md)
2675 0100   4         Maximize throughput (mt)
2676 0010   2         Maximize reliability (mr)
2677 0001   1         Minimize monetary cost (mmc)
2678 0000   0         Normal Service
2679 </Screen>
2681 </Para>
2683 <Para>
2684 As there is 1 bit to the right of these four bits, the actual value of the
2685 TOS field is double the value of the TOS bits. Tcpdump -v -v shows you the
2686 value of the entire TOS field, not just the four bits. It is the value you
2687 see in the first column of this table:
2688 </Para>
2690 <Para>
2692 <Screen>
2693 TOS     Bits  Means                    Linux Priority    Band
2694 ------------------------------------------------------------
2695 0x0     0     Normal Service           0 Best Effort     1
2696 0x2     1     Minimize Monetary Cost   1 Filler          2
2697 0x4     2     Maximize Reliability     0 Best Effort     1
2698 0x6     3     mmc+mr                   0 Best Effort     1
2699 0x8     4     Maximize Throughput      2 Bulk            2
2700 0xa     5     mmc+mt                   2 Bulk            2
2701 0xc     6     mr+mt                    2 Bulk            2
2702 0xe     7     mmc+mr+mt                2 Bulk            2
2703 0x10    8     Minimize Delay           6 Interactive     0
2704 0x12    9     mmc+md                   6 Interactive     0
2705 0x14    10    mr+md                    6 Interactive     0
2706 0x16    11    mmc+mr+md                6 Interactive     0
2707 0x18    12    mt+md                    4 Int. Bulk       1
2708 0x1a    13    mmc+mt+md                4 Int. Bulk       1
2709 0x1c    14    mr+mt+md                 4 Int. Bulk       1
2710 0x1e    15    mmc+mr+mt+md             4 Int. Bulk       1
2711 </Screen>
2713 </Para>
2715 <Para>
2716 Lots of numbers. The second column contains the value of the relevant four
2717 TOS bits, followed by their translated meaning. For example, 15 stands for a
2718 packet wanting Minimal Monetary Cost, Maximum Reliability, Maximum
2719 Throughput AND Minimum Delay. I would call this a 'Dutch Packet'.
2720 </Para>
2722 <Para>
2723 The fourth column lists the way the Linux kernel interprets the TOS bits, by
2724 showing to which Priority they are mapped. 
2725 </Para>
2727 <Para>
2728 The last column shows the result of the default priomap. On the command line,
2729 the default priomap looks like this:
2731 <Screen>
2732 1, 2, 2, 2, 1, 2, 0, 0 , 1, 1, 1, 1, 1, 1, 1, 1
2733 </Screen>
2735 </Para>
2737 <Para>
2738 This means that priority 4, for example, gets mapped to band number 1. The
2739 priomap also allows you to list higher priorities (&gt; 7) which do not
2740 correspond to TOS mappings, but which are set by other means.
2741 </Para>
2743 <Para>
2744 This table from RFC 1349 (read it for more details) tells you how
2745 applications might very well set their TOS bits:
2747 <Screen>
2748 TELNET                   1000           (minimize delay)
2750         Control          1000           (minimize delay)
2751         Data             0100           (maximize throughput)
2753 TFTP                     1000           (minimize delay)
2755 SMTP 
2756         Command phase    1000           (minimize delay)
2757         DATA phase       0100           (maximize throughput)
2759 Domain Name Service
2760         UDP Query        1000           (minimize delay)
2761         TCP Query        0000
2762         Zone Transfer    0100           (maximize throughput)
2764 NNTP                     0001           (minimize monetary cost)
2766 ICMP
2767         Errors           0000
2768         Requests         0000 (mostly)
2769         Responses        &#60;same as request&#62; (mostly)
2770 </Screen>
2772 </Para></ListItem>
2773 </VarListEntry>
2774 <VarListEntry>
2775 <Term>txqueuelen</Term>
2776 <ListItem>
2777 <Para>
2778 The length of this queue is gleaned from the interface configuration, which
2779 you can see and set with ifconfig and ip. To set the queue length to 10,
2780 execute: ifconfig eth0 txqueuelen 10
2781 </Para>
2783 <Para>
2784 You can't set this parameter with tc!
2785 </Para></ListItem>
2786 </VarListEntry>
2787 </VariableList>
2788 </Para>
2790 </Sect3>
2792 </Sect2>
2794 <Sect2>
2795 <Title>Token Bucket Filter</Title>
2797 <Para>
2798 The Token Bucket Filter (TBF) is a simple qdisc that only passes packets
2799 arriving at a rate which is not exceeding some administratively set rate, but
2800 with the possibility to allow short bursts in excess of this rate.
2801 </Para>
2803 <Para>
2804 TBF is very precise, network- and processor friendly. It should be your
2805 first choice if you simply want to slow an interface down!
2806 </Para>
2808 <Para>
2809 The TBF implementation consists of a buffer (bucket), constantly filled by
2810 some virtual pieces of information called tokens, at a specific rate (token
2811 rate). The most important parameter of the bucket is its size, that is the
2812 number of tokens it can store.
2813 </Para>
2815 <Para>
2816 Each arriving token collects one incoming data packet from the data queue
2817 and is then deleted from the bucket. Associating this algorithm
2818 with the two flows -- token and data, gives us three possible scenarios:
2819 </Para>
2821 <Para>
2823 <ItemizedList>
2824 <ListItem>
2826 <Para>
2827  The data arrives in TBF at a rate that's <Emphasis>equal</Emphasis> to the rate
2828 of incoming tokens. In this case each incoming packet has its matching token
2829 and passes the queue without delay.
2831 </Para>
2832 </ListItem>
2833 <ListItem>
2835 <Para>
2836  The data arrives in TBF at a rate that's <Emphasis>smaller</Emphasis> than the
2837 token rate. Only a part of the tokens are deleted at output of each data packet
2838 that's sent out the queue, so the tokens accumulate, up to the bucket size.
2839 The unused tokens can then be used to send data a a speed that's exceeding the
2840 standard token rate, in case short data bursts occur.
2842 </Para>
2843 </ListItem>
2844 <ListItem>
2846 <Para>
2847  The data arrives in TBF at a rate <Emphasis>bigger</Emphasis> than the token rate.
2848 This means that the bucket will soon be devoid of tokens, which causes the
2849 TBF to throttle itself for a while. This is called an 'overlimit situation'.
2850 If packets keep coming in, packets will start to get dropped.
2851 </Para>
2852 </ListItem>
2854 </ItemizedList>
2856 </Para>
2858 <Para>
2859 The last scenario is very important, because it allows to
2860 administratively shape the bandwidth available to data that's passing
2861 the filter.
2862 </Para>
2864 <Para>
2865 The accumulation of tokens allows a short burst of overlimit data to be
2866 still passed without loss, but any lasting overload will cause packets to be
2867 constantly delayed, and then dropped.
2868 </Para>
2870 <Para>
2871 Please note that in the actual implementation, tokens correspond to bytes,
2872 not packets.
2873 </Para>
2875 <Sect3>
2876 <Title>Parameters &amp; usage</Title>
2878 <Para>
2879 Even though you will probably not need to change them, tbf has some knobs
2880 available. First the parameters that are always available:
2881 <VariableList>
2883 <VarListEntry>
2884 <Term>limit or latency</Term>
2885 <ListItem>
2886 <Para>
2887 Limit is the number of bytes that can be queued waiting for tokens to become
2888 available. You can also specify this the other way around by setting the
2889 latency parameter, which specifies the maximum amount of time a packet can
2890 sit in the TBF. The latter calculation takes into account the size of the
2891 bucket, the rate and possibly the peakrate (if set).
2892 </Para></ListItem>
2893 </VarListEntry>
2894 <VarListEntry>
2895 <Term>burst/buffer/maxburst</Term>
2896 <ListItem>
2897 <Para>
2898 Size of the bucket, in bytes. This is the maximum amount of bytes that
2899 tokens can be available for instantaneously. In general, larger shaping
2900 rates require a larger buffer. For 10mbit/s on Intel, you need at least
2901 10kbyte buffer if you want to reach your configured rate!
2902 </Para>
2904 <Para>
2905 If your buffer is too small, packets may be dropped because more tokens
2906 arrive per timer tick than fit in your bucket.
2907 </Para></ListItem>
2908 </VarListEntry>
2909 <VarListEntry>
2910 <Term>mpu</Term>
2911 <ListItem>
2912 <Para>
2913 A zero-sized packet does not use zero bandwidth. For ethernet, no packet
2914 uses less than 64 bytes. The Minimum Packet Unit determines the minimal
2915 token usage for a packet.
2916 </Para></ListItem>
2917 </VarListEntry>
2918 <VarListEntry>
2919 <Term>rate</Term>
2920 <ListItem>
2921 <Para>
2922 The speedknob. See remarks above about limits!
2923 </Para></ListItem>
2924 </VarListEntry>
2925 </VariableList>
2926 </Para>
2928 <Para>
2929 If the bucket contains tokens and is allowed to empty, by default it does so
2930 at infinite speed. If this is unacceptable, use the following parameters:
2931 </Para>
2933 <Para>
2934 <VariableList>
2936 <VarListEntry>
2937 <Term>peakrate</Term>
2938 <ListItem>
2939 <Para>
2940 If tokens are available, and packets arrive, they are sent out immediately
2941 by default, at 'lightspeed' so to speak. That may not be what you want,
2942 especially if you have a large bucket. 
2943 </Para>
2945 <Para>
2946 The peakrate can be used to specify how quickly the bucket is allowed to be
2947 depleted. If doing everything by the book, this is achieved by releasing a
2948 packet, and then wait just long enough, and release the next. We calculated
2949 our waits so we send just at peakrate.
2950 </Para>
2952 <Para>
2953 However, due to de default 10ms timer resolution of Unix, with 10.000 bits
2954 average packets, we are limited to 1mbit/s of peakrate!
2955 </Para></ListItem>
2956 </VarListEntry>
2957 <VarListEntry>
2958 <Term>mtu/minburst</Term>
2959 <ListItem>
2960 <Para>
2961 The 1mbit/s peakrate is not very useful if your regular rate is more than
2962 that. A higher peakrate is possible by sending out more packets per
2963 timertick, which effectively means that we create a second bucket!
2964 </Para>
2966 <Para>
2967 This second bucket defaults to a single packet, which is not a bucket at
2968 all.
2969 </Para>
2971 <Para>
2972 To calculate the maximum possible peakrate, multiply the configured mtu by
2973 100 (or more correctly, HZ, which is 100 on Intel, 1024 on Alpha).
2974 </Para></ListItem>
2975 </VarListEntry>
2976 </VariableList>
2977 </Para>
2979 </Sect3>
2981 <Sect3>
2982 <Title>Sample configuration</Title>
2984 <Para>
2985 A simple but *very* useful configuration is this:
2987 <Screen>
2988 # tc qdisc add dev ppp0 root tbf rate 220kbit latency 50ms burst 1540
2989 </Screen>
2991 </Para>
2993 <Para>
2994 Ok, why is this useful? If you have a networking device with a large queue,
2995 like a DSL modem or a cable modem, and you talk to it over a fast device,
2996 like over an ethernet interface, you will find that uploading absolutely
2997 destroys interactivity.
2998 </Para>
3000 <Para>
3001 This is because uploading will fill the queue in the modem, which is
3002 probably *huge* because this helps actually achieving good data throughput
3003 uploading. But this is not what you want, you want to have the queue not too
3004 big so interactivity remains and you can still do other stuff while sending
3005 data.
3006 </Para>
3008 <Para>
3009 The line above slows down sending to a rate that does not lead to a queue in
3010 the modem - the queue will be in Linux, where we can control it to a limited
3011 size.
3012 </Para>
3014 <Para>
3015 Change 220kbit to your uplink's *actual* speed, minus a few percent. If you
3016 have a really fast modem, raise 'burst' a bit. 
3017 </Para>
3019 </Sect3>
3021 </Sect2>
3023 <Sect2 id="lartc.sfq">
3024 <Title>Stochastic Fairness Queueing</Title>
3026 <Para>
3027 Stochastic Fairness Queueing (SFQ) is a simple implementation of the fair
3028 queueing algorithms family. It's less accurate than others, but it also
3029 requires less calculations while being almost perfectly fair.
3030 </Para>
3032 <Para>
3033 The key word in SFQ is conversation (or flow), which mostly corresponds to a
3034 TCP session or a UDP stream. Traffic is divided into a pretty large number
3035 of FIFO queues, one for each conversation. Traffic is then sent in a round
3036 robin fashion, giving each session the chance to send data in turn.
3037 </Para>
3039 <Para>
3040 This leads to very fair behaviour and disallows any single conversation from
3041 drowning out the rest. SFQ is called 'Stochastic' because it doesn't really
3042 allocate a queue for each session, it has an algorithm which divides traffic
3043 over a limited number of queues using a hashing algorithm. 
3044 </Para>
3046 <Para>
3047 Because of the hash, multiple sessions might end up in the same bucket, which
3048 would halve each session's chance of sending a packet, thus halving the
3049 effective speed available. To prevent this situation from becoming
3050 noticeable, SFQ changes its hashing algorithm quite often so that any two
3051 colliding sessions will only do so for a small number of seconds.
3052 </Para>
3054 <Para>
3055 It is important to note that SFQ is only useful in case your actual outgoing
3056 interface is really full! If it isn't then there will be no queue on your
3057 linux machine and hence no effect. Later on we will describe how to combine
3058 SFQ with other qdiscs to get a best-of-both worlds situation.
3059 </Para>
3061 <Para>
3062 Specifically, setting SFQ on the ethernet interface heading to your
3063 cable modem or DSL router is pointless without further shaping!
3064 </Para>
3066 <Sect3>
3067 <Title>Parameters &amp; usage</Title>
3069 <Para>
3070 The SFQ is pretty much self tuning:
3071 <VariableList>
3073 <VarListEntry>
3074 <Term>perturb</Term>
3075 <ListItem>
3076 <Para>
3077 Reconfigure hashing once this many seconds. If unset, hash will never be
3078 reconfigured. Not recommended. 10 seconds is probably a good value.
3079 </Para></ListItem>
3080 </VarListEntry>
3081 <VarListEntry>
3082 <Term>quantum</Term>
3083 <ListItem>
3084 <Para>
3085 Amount of bytes a stream is allowed to dequeue before the next queue gets a
3086 turn. Defaults to 1 maximum sized packet (MTU-sized). Do not set below the
3087 MTU!
3088 </Para></ListItem>
3089 </VarListEntry>
3090 </VariableList>
3091 </Para>
3093 </Sect3>
3095 <Sect3>
3096 <Title>Sample configuration</Title>
3098 <Para>
3099 If you have a device which has identical link speed and actual available
3100 rate, like a phone modem, this configuration will help promote fairness:
3102 <Screen>
3103 # tc qdisc add dev ppp0 root sfq perturb 10
3104 # tc -s -d qdisc ls
3105 qdisc sfq 800c: dev ppp0 quantum 1514b limit 128p flows 128/1024 perturb 10sec 
3106  Sent 4812 bytes 62 pkts (dropped 0, overlimits 0) 
3107 </Screen>
3109 </Para>
3111 <Para>
3112 The number 800c: is the automatically assigned handle number, limit means
3113 that 128 packets can wait in this queue. There are 1024 hashbuckets
3114 available for accounting, of which 128 can be active at a time (no more
3115 packets fit in the queue!) Once every 10 seconds, the hashes are
3116 reconfigured.
3117 </Para>
3119 </Sect3>
3121 </Sect2>
3123 </Sect1>
3125 <Sect1 id="lartc.qdisc.advice">
3126   <Title>Advice for when to use which queue</Title>
3128 <Para>
3129 Summarizing, these are the simple queues that actually manage traffic by
3130 reordering, slowing or dropping packets.
3131 </Para>
3133 <Para>
3134 The following tips may help in choosing which queue to use. It mentions some
3135 qdiscs described in the
3136 <citetitle><xref linkend="lartc.adv-qdisc"></citetitle> chapter.
3137 </Para>
3139 <ItemizedList>
3140 <ListItem>
3141 <Para>
3142 To purely slow down outgoing traffic, use the Token Bucket Filter. Works up
3143 to huge bandwidths, if you scale the bucket.
3144 </Para>
3145 </ListItem>
3146 <ListItem>
3148 <Para>
3149 If your link is truly full and you want to make sure that no single session
3150 can dominate your outgoing bandwidth, use Stochastical Fairness Queueing.
3151 </Para>
3152 </ListItem>
3153 <ListItem>
3155 <Para>
3156 If you have a big backbone and know what you are doing, consider Random
3157 Early Drop (see Advanced chapter).
3158 </Para>
3159 </ListItem>
3160 <ListItem>
3162 <Para>
3163 To 'shape' incoming traffic which you are not forwarding, use the Ingress
3164 Policer. Incoming shaping is called 'policing', by the way, not 'shaping'.  
3165 </Para>
3166 </ListItem>
3167 <ListItem>
3169 <Para>
3170 If you *are* forwarding it, use a TBF on the interface you are forwarding
3171 the data to. Unless you want to shape traffic that may go out over several
3172 interfaces, in which case the only common factor is the incoming interface.
3173 In that case use the Ingress Policer.
3174 </Para>
3175 </ListItem>
3176 <ListItem>
3178 <Para>
3179 If you don't want to shape, but only want to see if your interface is so
3180 loaded that it has to queue, use the pfifo queue (not pfifo_fast). It lacks
3181 internal bands but does account the size of its backlog.
3182 </Para>
3183 </ListItem>
3184 <ListItem>
3185 <Para>
3186 Finally - you can also do <quote>social shaping</quote>.
3187 You may not always be able to use technology to achieve what you want.
3188 Users experience technical constraints as hostile.
3189 A kind word may also help with getting your bandwidth to be divided right!
3190 </Para>
3191 </ListItem>
3192 </ItemizedList>
3194 </Sect1>
3196 <Sect1 id="lartc.qdisc.terminology">
3197   <Title>Terminology</Title>
3199 <Para>
3200 To properly understand more complicated configurations it is necessary to
3201 explain a few concepts first. Because of the complexity and the relative
3202 youth of the subject, a lot of different words are used when people in fact
3203 mean the same thing.
3204 </Para>
3206 <Para>
3207 The following is loosely based on 
3208 <filename>draft-ietf-diffserv-model-06.txt</filename>,
3209 <citetitle>An Informal Management Model for Diffserv Routers</citetitle>.
3210 It can currently be found at 
3211 <ulink url="http://www.ietf.org/internet-drafts/draft-ietf-diffserv-model-06.txt">
3212   http://www.ietf.org/internet-drafts/draft-ietf-diffserv-model-06.txt
3213 </ulink>.
3214 </Para>
3216 <Para>
3217 Read it for the strict definitions of the terms used.
3218 <VariableList>
3220 <VarListEntry>
3221 <Term>Queueing Discipline (qdisc)</Term>
3222 <ListItem>
3223 <Para>
3224 An algorithm that manages the queue of a device, either incoming (ingress)
3225 or outgoing (egress).
3226 </Para></ListItem>
3227 </VarListEntry>
3228 <VarListEntry>
3229 <Term>root qdisc</Term>
3230 <ListItem>
3231 <Para>
3232 The root qdisc is the qdisc attached to the device.
3233 </Para></ListItem>
3234 </VarListEntry>
3235 <VarListEntry>
3236 <Term>Classless qdisc</Term>
3237 <ListItem>
3238 <Para>
3239 A qdisc with no configurable internal subdivisions. 
3240 </Para></ListItem>
3241 </VarListEntry>
3242 <VarListEntry>
3243 <Term>Classful qdisc</Term>
3244 <ListItem>
3245 <Para>
3246 A classful qdisc contains multiple classes. Some of these classes contains a
3247 further qdisc, which may again be classful, but need not be. According to
3248 the strict definition, pfifo_fast *is* classful, because it contains three
3249 bands which are, in fact, classes. However, from the user's configuration
3250 perspective, it is classless as the classes can't be touched with the tc
3251 tool. 
3252 </Para></ListItem>
3253 </VarListEntry>
3254 <VarListEntry>
3255 <Term>Classes</Term>
3256 <ListItem>
3257 <Para>
3258 A classful qdisc may have many classes, which each are internal to the
3259 qdisc.  A class, in turn, may have several classes added to it.  So a class
3260 can have a qdisc as parent or an other class.
3262 A leaf class is a class with no child classes.  This class has 1 qdisc attached
3263 to it.  This qdisc is responsible to send the data from that class.  When
3264 you create a class, a fifo qdisc is attached to it.  When you add a child class,
3265 this qdisc is removed.
3266 For a leaf class, this fifo qdisc can be replaced with
3267 an other more suitable qdisc.  You can even replace this fifo qdisc with a
3268 classful qdisc so you can add extra classes.
3269 </Para></ListItem>
3270 </VarListEntry>
3271 <VarListEntry>
3272 <Term>Classifier</Term>
3273 <ListItem>
3274 <Para>
3275 Each classful qdisc needs to determine to which class it needs to send a
3276 packet. This is done using the classifier.
3277 </Para></ListItem>
3278 </VarListEntry>
3279 <VarListEntry>
3280 <Term>Filter</Term>
3281 <ListItem>
3282 <Para>
3283 Classification can be performed using filters. A filter contains a number of
3284 conditions which if matched, make the filter match.
3285 </Para></ListItem>
3286 </VarListEntry>
3287 <VarListEntry>
3288 <Term>Scheduling</Term>
3289 <ListItem>
3290 <Para>
3291 A qdisc may, with the help of a classifier, decide that some packets need to
3292 go out earlier than others. This process is called Scheduling, and is
3293 performed for example by the pfifo_fast qdisc mentioned earlier. Scheduling
3294 is also called 'reordering', but this is confusing.
3295 </Para></ListItem>
3296 </VarListEntry>
3297 <VarListEntry>
3298 <Term>Shaping</Term>
3299 <ListItem>
3300 <Para>
3301 The process of delaying packets before they go out to make traffic confirm
3302 to a configured maximum rate. Shaping is performed on egress. Colloquially, 
3303 dropping packets to slow traffic down is also often called Shaping.
3304 </Para></ListItem>
3305 </VarListEntry>
3306 <VarListEntry>
3307 <Term>Policing</Term>
3308 <ListItem>
3309 <Para>
3310 Delaying or dropping packets in order to make traffic stay below a
3311 configured bandwidth. In Linux, policing can only drop a packet and not
3312 delay it - there is no 'ingress queue'.
3313 </Para></ListItem>
3314 </VarListEntry>
3315 <VarListEntry>
3316 <Term>Work-Conserving</Term>
3317 <ListItem>
3318 <Para>
3319 A work-conserving qdisc always delivers a packet if one is available. In
3320 other words, it never delays a packet if the network adaptor is ready to
3321 send one (in the case of an egress qdisc).
3322 </Para></ListItem>
3323 </VarListEntry>
3324 <VarListEntry>
3325 <Term>non-Work-Conserving</Term>
3326 <ListItem>
3327 <Para>
3328 Some queues, like for example the Token Bucket Filter, may need to hold on
3329 to a packet for a certain time in order to limit the bandwidth. This means
3330 that they sometimes refuse to give up a packet, even though they have one
3331 available.
3332 </Para></ListItem>
3333 </VarListEntry>
3334 </VariableList>
3335 </Para>
3337 <Para>
3338 Now that we have our terminology straight, let's see where all these things
3339 are.
3340 </Para>
3342 <Para>
3344 <Screen width="80">
3345                 Userspace programs
3346                      ^
3347                      |
3348      +---------------+-----------------------------------------+
3349      |               Y                                         |
3350      |    -------&#62; IP Stack                                    |
3351      |   |              |                                      |
3352      |   |              Y                                      |
3353      |   |              Y                                      |
3354      |   ^              |                                      |
3355      |   |  / ----------&#62; Forwarding -&#62;                        |
3356      |   ^ /                           |                       |
3357      |   |/                            Y                       |
3358      |   |                             |                       |
3359      |   ^                             Y          /-qdisc1-\   |
3360      |   |                            Egress     /--qdisc2--\  |
3361   ---&#62;-&#62;Ingress                       Classifier ---qdisc3---- | -&#62;
3362      |   Qdisc                                   \__qdisc4__/  |
3363      |                                            \-qdiscN_/   |
3364      |                                                         |
3365      +----------------------------------------------------------+
3366 </Screen>
3368 Thanks to Jamal Hadi Salim for this ASCII representation.
3369 </Para>
3371 <Para>
3372 The big block represents the kernel. The leftmost arrow represents traffic
3373 entering your machine from the network. It is then fed to the Ingress
3374 Qdisc which may apply Filters to a packet, and decide to drop it. This
3375 is called 'Policing'.
3376 </Para>
3378 <Para>
3379 This happens at a very early stage, before it has seen a lot of the kernel.
3380 It is therefore a very good place to drop traffic very early, without
3381 consuming a lot of CPU power.
3382 </Para>
3384 <Para>
3385 If the packet is allowed to continue, it may be destined for a local
3386 application, in which case it enters the IP stack in order to be processed,
3387 and handed over to a userspace program. The packet may also be forwarded
3388 without entering an application, in which case it is destined for egress.
3389 Userspace programs may also deliver data, which is then examined and
3390 forwarded to the Egress Classifier.
3391 </Para>
3393 <Para>
3394 There it is investigated and enqueued to any of a number of qdiscs. In the
3395 unconfigured default case, there is only one egress qdisc installed, the
3396 pfifo_fast, which always receives the packet. This is called 'enqueueing'.
3397 </Para>
3399 <Para>
3400 The packet now sits in the qdisc, waiting for the kernel to ask for
3401 it for transmission over the network interface. This is called 'dequeueing'.
3402 </Para>
3404 <Para>
3405 This picture also holds in case there is only one network adaptor - the
3406 arrows entering and leaving the kernel should not be taken too literally.
3407 Each network adaptor has both ingress and egress hooks.
3408 </Para>
3410 </Sect1>
3412 <Sect1 id="lartc.qdisc.classful">
3413   <Title>Classful Queueing Disciplines</Title>
3415 <Para>
3416 Classful qdiscs are very useful if you have different kinds of traffic which
3417 should have differing treatment. One of the classful qdiscs is called 'CBQ',
3418 'Class Based Queueing' and it is so widely mentioned that people identify
3419 queueing with classes solely with CBQ, but this is not the case.
3420 </Para>
3422 <Para>
3423 CBQ is merely the oldest kid on the block - and also the most complex one.
3424 It may not always do what you want.  This may come as something of a shock
3425 to many who fell for the 'sendmail effect', which teaches us that any
3426 complex technology which doesn't come with documentation must be the best
3427 available.
3428 </Para>
3430 <Para>
3431 More about CBQ and its alternatives shortly.
3432 </Para>
3434 <Sect2>
3435 <Title>Flow within classful qdiscs &amp; classes</Title>
3437 <Para>
3438 When traffic enters a classful qdisc, it needs to be sent to any of the
3439 classes within - it needs to be 'classified'. To determine what to do with a
3440 packet, the so called 'filters' are consulted. It is important to know that
3441 the filters are called from within a qdisc, and not the other way around!
3442 </Para>
3444 <Para>
3445 The filters attached to that qdisc then return with a decision, and the
3446 qdisc uses this to enqueue the packet into one of the classes. Each subclass
3447 may try other filters to see if further instructions apply. If not, the
3448 class enqueues the packet to the qdisc it contains.
3449 </Para>
3451 <Para>
3452 Besides containing other qdiscs, most classful qdiscs also perform shaping.
3453 This is useful to perform both packet scheduling (with SFQ, for example) and
3454 rate control. You need this in cases where you have a high speed
3455 interface (for example, ethernet) to a slower device (a cable modem).
3456 </Para>
3458 <Para>
3459 If you were only to run SFQ, nothing would happen, as packets enter &amp;
3460 leave your router without delay: the output interface is far faster than
3461 your actual link speed. There is no queue to schedule then.
3462 </Para>
3464 </Sect2>
3466 <Sect2>
3467 <Title>The qdisc family: roots, handles, siblings and parents</Title>
3469 <Para>
3470 Each interface has one egress 'root qdisc', by default the earlier mentioned
3471 classless pfifo_fast queueing discipline. Each qdisc and class is assigned a
3472 handle, which can be used by later configuration statements to refer to that
3473 qdisc. Besides an egress qdisc, an interface may also have an ingress qdisc ,
3474 which polices traffic coming in.
3475 </Para>
3477 <Para>
3478 The handles of these qdiscs consist of two parts, a major number and a minor
3479 number : &lt;major&gt;:&lt;minor&gt;. It is customary to name the root qdisc '1:', which
3480 is equal to '1:0'.  The minor number of a qdisc is always 0. 
3481 </Para>
3483 <Para>
3484 Classes need to have the same major number as their parent.  This major number
3485 must be unique within a egress or ingress setup.  The minor number must be
3486 unique within a qdisc and his classes.
3487 </Para>
3489 <Sect3>
3490 <Title>How filters are used to classify traffic </Title>
3492 <Para>
3493 Recapping, a typical hierarchy might look like this:
3495 <Screen>
3496                      1:   root qdisc
3497                       |
3498                      1:1    chils class
3499                    /  |  \
3500                   /   |   \
3501                  /    |    \
3502                  /    |    \
3503               1:10  1:11  1:12   child classes
3504                |      |     | 
3505                |     11:    |    leaf class
3506                |            | 
3507                10:         12:   qdisc
3508               /   \       /   \
3509            10:1  10:2   12:1  12:2   leaf classes
3510 </Screen>
3512 </Para>
3514 <Para>
3515 But don't let this tree fool you! You should *not* imagine the kernel to be
3516 at the apex of the tree and the network below, that is just not the case.
3517 Packets get enqueued and dequeued at the root qdisc, which is the only thing
3518 the kernel talks to. 
3519 </Para>
3521 <Para>
3522 A packet might get classified in a chain like this:
3523 </Para>
3525 <Para>
3526 1: -&#62; 1:1 -&#62; 1:12 -&#62; 12: -&#62; 12:2
3527 </Para>
3529 <Para>
3530 The packet now resides in a queue in a qdisc attached to class 12:2. In this
3531 example, a filter was attached to each 'node' in the tree, each choosing a
3532 branch to take next. This can make sense. However, this is also possible:
3533 </Para>
3535 <Para>
3536 1: -&#62; 12:2
3537 </Para>
3539 <Para>
3540 In this case, a filter attached to the root decided to send the packet
3541 directly to 12:2.
3542 </Para>
3544 </Sect3>
3546 <Sect3>
3547 <Title>How packets are dequeued to the hardware</Title>
3549 <Para>
3550 When the kernel decides that it needs to extract packets to send to the
3551 interface, the root qdisc 1: gets a dequeue request, which is passed to
3552 1:1, which is in turn passed to 10:, 11: and 12:, which each query their
3553 siblings, and try to dequeue() from them. In this case, the kernel needs to
3554 walk the entire tree, because only 12:2 contains a packet. 
3555 </Para>
3557 <Para>
3558 In short, nested classes ONLY talk to their parent qdiscs, never to an
3559 interface. Only the root qdisc gets dequeued by the kernel!
3560 </Para>
3562 <Para>
3563 The upshot of this is that classes never get dequeued faster than their
3564 parents allow. And this is exactly what we want: this way we can have SFQ in
3565 an inner class, which doesn't do any shaping, only scheduling, and have a
3566 shaping outer qdisc, which does the shaping.
3567 </Para>
3569 </Sect3>
3571 </Sect2>
3573 <Sect2>
3574 <Title>The PRIO qdisc</Title>
3576 <Para>
3577 The PRIO qdisc doesn't actually shape, it only subdivides traffic based on
3578 how you configured your filters. You can consider the PRIO qdisc a kind
3579 of pfifo_fast on steroids, whereby each band is a separate class instead of
3580 a simple FIFO.
3581 </Para>
3583 <Para>
3584 When a packet is enqueued to the PRIO qdisc, a class is chosen based on the
3585 filter commands you gave. By default, three classes are created. These
3586 classes by default contain pure FIFO qdiscs with no internal
3587 structure, but you can replace these by any qdisc you have available.
3588 </Para>
3590 <Para>
3591 Whenever a packet needs to be dequeued, class :1 is tried first. Higher
3592 classes are only used if lower bands all did not give up a packet.
3593 </Para>
3595 <Para>
3596 This qdisc is very useful in case you want to prioritize certain kinds of
3597 traffic without using only TOS-flags but using all the power of the tc
3598 filters. You can also add an other qdisc to the 3 predefined classes,
3599 whereas pfifo_fast is limited to simple fifo qdiscs.
3600 </Para>
3602 <Para>
3603 Because it doesn't actually shape, the same warning as for SFQ holds: either
3604 use it only if your physical link is really full or wrap it inside a
3605 classful qdisc that does shape. The last holds for almost all cable modems
3606 and DSL devices.
3607 </Para>
3609 <Para>
3610 In formal words, the PRIO qdisc is a Work-Conserving scheduler.
3611 </Para>
3613 <Sect3>
3614 <Title>PRIO parameters &amp; usage</Title>
3616 <Para>
3617 The following parameters are recognized by tc:
3618 <VariableList>
3620 <VarListEntry>
3621 <Term>bands</Term>
3622 <ListItem>
3623 <Para>
3624 Number of bands to create. Each band is in fact a class. If you change this
3625 number, you must also change:
3626 </Para></ListItem>
3627 </VarListEntry>
3628 <VarListEntry>
3629 <Term>priomap</Term>
3630 <ListItem>
3631 <Para>
3632 If you do not provide tc filters to classify traffic, the PRIO qdisc looks
3633 at the TC_PRIO priority to decide how to enqueue traffic. 
3634 </Para>
3636 <Para>
3637 This works just like with the pfifo_fast qdisc mentioned earlier, see there
3638 for lots of detail.
3639 </Para></ListItem>
3640 </VarListEntry>
3641 </VariableList>
3642 The bands are classes, and are called major:1 to major:3 by default, so if
3643 your PRIO qdisc is called 12:, tc filter traffic to 12:1 to grant it more
3644 priority.
3645 </Para>
3647 <Para>
3648 Reiterating, band 0 goes to minor number 1! Band 1 to minor number 2, etc.
3649 </Para>
3651 </Sect3>
3653 <Sect3>
3654 <Title>Sample configuration</Title>
3656 <Para>
3657 We will create this tree:
3659 <Screen>
3660           1:   root qdisc
3661          / | \ 
3662        /   |   \
3663        /   |   \
3664      1:1  1:2  1:3    classes
3665       |    |    |
3666      10:  20:  30:    qdiscs    qdiscs
3667      sfq  tbf  sfq
3668 band  0    1    2
3669 </Screen>
3671 </Para>
3673 <Para>
3674 Bulk traffic will go to 30:, interactive traffic to 20: or 10:.
3675 </Para>
3677 <Para>
3678 Command lines:
3680 <Screen>
3681 # tc qdisc add dev eth0 root handle 1: prio 
3682 ## This *instantly* creates classes 1:1, 1:2, 1:3
3683   
3684 # tc qdisc add dev eth0 parent 1:1 handle 10: sfq
3685 # tc qdisc add dev eth0 parent 1:2 handle 20: tbf rate 20kbit buffer 1600 limit 3000
3686 # tc qdisc add dev eth0 parent 1:3 handle 30: sfq                                
3687 </Screen>
3689 </Para>
3691 <Para>
3692 Now let's see what we created:
3694 <Screen>
3695 # tc -s qdisc ls dev eth0 
3696 qdisc sfq 30: quantum 1514b 
3697  Sent 0 bytes 0 pkts (dropped 0, overlimits 0) 
3699  qdisc tbf 20: rate 20Kbit burst 1599b lat 667.6ms 
3700  Sent 0 bytes 0 pkts (dropped 0, overlimits 0) 
3702  qdisc sfq 10: quantum 1514b 
3703  Sent 132 bytes 2 pkts (dropped 0, overlimits 0) 
3705  qdisc prio 1: bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
3706  Sent 174 bytes 3 pkts (dropped 0, overlimits 0) 
3707 </Screen>
3709 As you can see, band 0 has already had some traffic, and one packet was sent
3710 while running this command!
3711 </Para>
3713 <Para>
3714 We now do some bulk data transfer with a tool that properly sets TOS flags,
3715 and take another look:
3717 <Screen>
3718 # scp tc ahu@10.0.0.11:./
3719 ahu@10.0.0.11's password: 
3720 tc                   100% |*****************************|   353 KB    00:00    
3721 # tc -s qdisc ls dev eth0
3722 qdisc sfq 30: quantum 1514b 
3723  Sent 384228 bytes 274 pkts (dropped 0, overlimits 0) 
3725  qdisc tbf 20: rate 20Kbit burst 1599b lat 667.6ms 
3726  Sent 2640 bytes 20 pkts (dropped 0, overlimits 0) 
3728  qdisc sfq 10: quantum 1514b 
3729  Sent 2230 bytes 31 pkts (dropped 0, overlimits 0) 
3731  qdisc prio 1: bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
3732  Sent 389140 bytes 326 pkts (dropped 0, overlimits 0) 
3733 </Screen>
3735 As you can see, all traffic went to handle 30:, which is the lowest priority
3736 band, just as intended. Now to verify that interactive traffic goes to
3737 higher bands, we create some interactive traffic:
3738 </Para>
3740 <Para>
3742 <Screen>
3743 # tc -s qdisc ls dev eth0
3744 qdisc sfq 30: quantum 1514b 
3745  Sent 384228 bytes 274 pkts (dropped 0, overlimits 0) 
3747  qdisc tbf 20: rate 20Kbit burst 1599b lat 667.6ms 
3748  Sent 2640 bytes 20 pkts (dropped 0, overlimits 0) 
3750  qdisc sfq 10: quantum 1514b 
3751  Sent 14926 bytes 193 pkts (dropped 0, overlimits 0) 
3753  qdisc prio 1: bands 3 priomap  1 2 2 2 1 2 0 0 1 1 1 1 1 1 1 1
3754  Sent 401836 bytes 488 pkts (dropped 0, overlimits 0) 
3755 </Screen>
3757 </Para>
3759 <Para>
3760 It worked - all additional traffic has gone to 10:, which is our highest
3761 priority qdisc. No traffic was sent to the lowest priority, which previously
3762 received our entire scp.
3763 </Para>
3765 </Sect3>
3767 </Sect2>
3769 <Sect2>
3770 <Title>The famous CBQ qdisc</Title>
3772 <Para>
3773 As said before, CBQ is the most complex qdisc available, the most hyped, the
3774 least understood, and probably the trickiest one to get right. This is not
3775 because the authors are evil or incompetent, far from it, it's just that the
3776 CBQ algorithm isn't all that precise and doesn't really match the way Linux
3777 works.
3778 </Para>
3780 <Para>
3781 Besides being classful, CBQ is also a shaper and it is in that aspect that
3782 it really doesn't work very well. It should work like this. If you try to
3783 shape a 10mbit/s connection to 1mbit/s, the link should be idle 90&percnt; of the
3784 time. If it isn't, we need to throttle so that it IS idle 90&percnt; of the time.
3785 </Para>
3787 <Para>
3788 This is pretty hard to measure, so CBQ  instead derives the idle time from
3789 the number of microseconds that elapse between requests from the hardware
3790 layer for more data. Combined, this can be used to approximate how full or
3791 empty the link is.
3792 </Para>
3794 <Para>
3795 This is rather circumspect and doesn't always arrive at proper results. For
3796 example, what if the actual link speed of an interface that is not really
3797 able to transmit the full 100mbit/s of data, perhaps because of a badly
3798 implemented driver? A PCMCIA network card will also never achieve 100mbit/s
3799 because of the way the bus is designed - again, how do we calculate the idle
3800 time?
3801 </Para>
3803 <Para>
3804 It gets even worse if we consider not-quite-real network devices like PPP
3805 over Ethernet or PPTP over TCP/IP. The effective bandwidth in that case is
3806 probably determined by the efficiency of pipes to userspace - which is huge.
3807 </Para>
3809 <Para>
3810 People who have done measurements discover that CBQ is not always very
3811 accurate and sometimes completely misses the mark.
3812 </Para>
3814 <Para>
3815 In many circumstances however it works well. With the documentation provided
3816 here, you should be able to configure it to work well in most cases.
3817 </Para>
3819 <Sect3>
3820 <Title>CBQ shaping in detail</Title>
3822 <Para>
3823 As said before, CBQ works by making sure that the link is idle just long
3824 enough to bring down the real bandwidth to the configured rate. To do so, it
3825 calculates the time that should pass between average packets. 
3826 </Para>
3828 <Para>
3829 During operations, the effective idletime is measured using an exponential
3830 weighted moving average (EWMA), which considers recent packets to be
3831 exponentially more important than past ones. The UNIX loadaverage is
3832 calculated in the same way.
3833 </Para>
3835 <Para>
3836 The calculated idle time is subtracted from the EWMA measured one, the
3837 resulting number is called 'avgidle'. A perfectly loaded link has an avgidle
3838 of zero: packets arrive exactly once every calculated interval.  
3839 </Para>
3841 <Para>
3842 An overloaded link has a negative avgidle and if it gets too negative, CBQ
3843 shuts down for a while and is then 'overlimit'.
3844 </Para>
3846 <Para>
3847 Conversely, an idle link might amass a huge avgidle, which would then allow
3848 infinite bandwidths after a few hours of silence. To prevent this, avgidle is
3849 capped at maxidle.
3850 </Para>
3852 <Para>
3853 If overlimit, in theory, the CBQ could throttle itself for exactly the
3854 amount of time that was calculated to pass between packets, and then pass
3855 one packet, and throttle again. But see the 'minburst' parameter below.
3856 </Para>
3858 <Para>
3859 These are parameters you can specify in order to configure shaping:
3860 <VariableList>
3862 <VarListEntry>
3863 <Term>avpkt</Term>
3864 <ListItem>
3865 <Para>
3866 Average size of a packet, measured in bytes. Needed for calculating maxidle,
3867 which is derived from maxburst, which is specified in packets.
3868 </Para></ListItem>
3869 </VarListEntry>
3870 <VarListEntry>
3871 <Term>bandwidth</Term>
3872 <ListItem>
3873 <Para>
3874 The physical bandwidth of your device, needed for idle time
3875 calculations.
3876 </Para></ListItem>
3877 </VarListEntry>
3878 <VarListEntry>
3879 <Term>cell</Term>
3880 <ListItem>
3881 <Para>
3882 The time a packet takes to be transmitted over a device may grow in steps,
3883 based on the packet size. An 800 and an 806 size packet may take just as long
3884 to send, for example - this sets the granularity. Most often set to '8'.
3885 Must be an integral power of two.
3886 </Para></ListItem>
3887 </VarListEntry>
3888 <VarListEntry>
3889 <Term>maxburst</Term>
3890 <ListItem>
3891 <Para>
3892 This number of packets is used to calculate maxidle so that when avgidle is
3893 at maxidle, this number of average packets can be burst before avgidle drops
3894 to 0. Set it higher to be more tolerant of bursts. You can't set maxidle
3895 directly, only via this parameter.
3896 </Para></ListItem>
3897 </VarListEntry>
3898 <VarListEntry>
3899 <Term>minburst</Term>
3900 <ListItem>
3901 <Para>
3902 As mentioned before, CBQ needs to throttle in case of overlimit. The ideal
3903 solution is to do so for exactly the calculated idle time, and pass 1
3904 packet. However, Unix kernels generally have a hard time scheduling events
3905 shorter than 10ms, so it is better to throttle for a longer period, and then
3906 pass minburst packets in one go, and then sleep minburst times longer.
3907 </Para>
3909 <Para>
3910 The time to wait is called the offtime. Higher values of minburst lead to
3911 more accurate shaping in the long term, but to bigger bursts at millisecond
3912 timescales.
3913 </Para></ListItem>
3914 </VarListEntry>
3915 <VarListEntry>
3916 <Term>minidle</Term>
3917 <ListItem>
3918 <Para>
3919 If avgidle is below 0, we are overlimits and need to wait until avgidle will
3920 be big enough to send one packet. To prevent a sudden burst from shutting
3921 down the link for a prolonged period of time, avgidle is reset to minidle if
3922 it gets too low.
3923 </Para>
3925 <Para>
3926 Minidle is specified in negative microseconds, so 10 means that avgidle is
3927 capped at -10us.
3928 </Para></ListItem>
3929 </VarListEntry>
3930 <VarListEntry>
3931 <Term>mpu</Term>
3932 <ListItem>
3933 <Para>
3934 Minimum packet size - needed because even a zero size packet is padded
3935 to 64 bytes on ethernet, and so takes a certain time to transmit. CBQ needs
3936 to know this to accurately calculate the idle time.
3937 </Para></ListItem>
3938 </VarListEntry>
3939 <VarListEntry>
3940 <Term>rate</Term>
3941 <ListItem>
3942 <Para>
3943 Desired rate of traffic leaving this qdisc - this is the 'speed knob'!
3944 </Para></ListItem>
3945 </VarListEntry>
3946 </VariableList>
3947 </Para>
3949 <Para>
3950 Internally, CBQ has a lot of fine tuning. For example, classes which are
3951 known not to have data enqueued to them aren't queried. Overlimit classes
3952 are penalized by lowering their effective priority. All very smart &amp;
3953 complicated.
3954 </Para>
3956 </Sect3>
3958 <Sect3>
3959 <Title>CBQ classful behaviour</Title>
3961 <Para>
3962 Besides shaping, using the aforementioned idletime approximations, CBQ also
3963 acts like the PRIO queue in the sense that classes can have differing
3964 priorities and that lower priority numbers will be polled before the higher
3965 priority ones.
3966 </Para>
3968 <Para>
3969 Each time a packet is requested by the hardware layer to be sent out to the
3970 network, a weighted round robin process ('WRR') starts, beginning with the
3971 lower-numbered priority classes.
3972 </Para>
3974 <Para>
3975 These are then grouped and queried if they have data available. If so, it is
3976 returned. After a class has been allowed to dequeue a number of bytes, the
3977 next class within that priority is tried.
3978 </Para>
3980 <Para>
3981 The following parameters control the WRR process:
3982 <VariableList>
3984 <VarListEntry>
3985 <Term>allot</Term>
3986 <ListItem>
3987 <Para>
3988 When the outer CBQ is asked for a packet to send out on the interface, it
3989 will try all inner qdiscs (in the classes) in turn, in order of 
3990 the 'priority' parameter. Each time a class gets its turn, it can only send out
3991 a limited amount of data. 'Allot' is the base unit of this amount. See 
3992 the 'weight' parameter for more information.
3993 </Para></ListItem>
3994 </VarListEntry>
3995 <VarListEntry>
3996 <Term>prio</Term>
3997 <ListItem>
3998 <Para>
3999 The CBQ can also act like the PRIO device. Inner classes with higher priority
4000 are tried first and as long as they have traffic, other classes are not
4001 polled for traffic.
4002 </Para></ListItem>
4003 </VarListEntry>
4004 <VarListEntry>
4005 <Term>weight</Term>
4006 <ListItem>
4007 <Para>
4008 Weight helps in the Weighted Round Robin process. Each class gets a chance
4009 to send in turn. If you have classes with significantly more bandwidth than
4010 other classes, it makes sense to allow them to send more data in one round
4011 than the others.
4012 </Para>
4014 <Para>
4015 A CBQ adds up all weights under a class, and normalizes them, so you can use
4016 arbitrary numbers: only the ratios are important. People have been 
4017 using 'rate/10' as a rule of thumb and it appears to work well. The renormalized
4018 weight is multiplied by the 'allot' parameter to determine how much data can
4019 be sent in one round. 
4020 </Para></ListItem>
4021 </VarListEntry>
4022 </VariableList>
4023 </Para>
4025 <Para>
4026 Please note that all classes within an CBQ hierarchy need to share the same
4027 major number!
4028 </Para>
4030 </Sect3>
4032 <Sect3>
4033 <Title>CBQ parameters that determine link sharing &amp; borrowing</Title>
4035 <Para>
4036 Besides purely limiting certain kinds of traffic, it is also possible to
4037 specify which classes can borrow capacity from other classes or, conversely,
4038 lend out bandwidth.
4039 </Para>
4041 <Para>
4042 <VariableList>
4044 <VarListEntry>
4045 <Term>Isolated/sharing</Term>
4046 <ListItem>
4047 <Para>
4048 A class that is configured with 'isolated' will not lend out bandwidth to
4049 sibling classes. Use this if you have competing or mutually-unfriendly
4050 agencies on your link who do want to give each other freebies.
4051 </Para>
4053 <Para>
4054 The control program tc also knows about 'sharing', which is the reverse 
4055 of 'isolated'.
4056 </Para></ListItem>
4057 </VarListEntry>
4058 <VarListEntry>
4059 <Term>bounded/borrow</Term>
4060 <ListItem>
4061 <Para>
4062 A class can also be 'bounded', which means that it will not try to borrow
4063 bandwidth from sibling classes. tc also knows about 'borrow', which is the
4064 reverse of 'bounded'.
4065 </Para></ListItem>
4066 </VarListEntry>
4067 </VariableList>
4068 A typical situation might be where you have two agencies on your link which
4069 are both 'isolated' and 'bounded', which means that they are really limited
4070 to their assigned rate, and also won't allow each other to borrow.
4071 </Para>
4073 <Para>
4074 Within such an agency class, there might be other classes which are allowed
4075 to swap bandwidth.
4076 </Para>
4078 </Sect3>
4080 <Sect3>
4081 <Title>Sample configuration</Title>
4082 <Screen>
4084                1:           root qdisc
4085                |
4086               1:1           child class
4087              /   \
4088             /     \
4089           1:3     1:4       leaf classes
4090            |       |
4091           30:     40:       qdiscs
4092          (sfq)   (sfq)
4093 </Screen>
4095 <Para>
4096 This configuration limits webserver traffic to 5mbit and SMTP traffic to 3
4097 mbit. Together, they may not get more than 6mbit. We have a 100mbit NIC and
4098 the classes may borrow bandwidth from each other.
4100 <Screen>
4101 # tc qdisc add dev eth0 root handle 1:0 cbq bandwidth 100Mbit         \
4102   avpkt 1000 cell 8
4103 # tc class add dev eth0 parent 1:0 classid 1:1 cbq bandwidth 100Mbit  \
4104   rate 6Mbit weight 0.6Mbit prio 8 allot 1514 cell 8 maxburst 20      \
4105   avpkt 1000 bounded
4106 </Screen>
4108 This part installs the root and the customary 1:1 class. The 1:1 class is
4109 bounded, so the total bandwidth can't exceed 6mbit.
4110 </Para>
4112 <Para>
4113 As said before, CBQ requires a *lot* of knobs. All parameters are explained
4114 above, however. The corresponding HTB configuration is lots simpler.
4115 </Para>
4117 <Para>
4119 <Screen>
4120 # tc class add dev eth0 parent 1:1 classid 1:3 cbq bandwidth 100Mbit  \
4121   rate 5Mbit weight 0.5Mbit prio 5 allot 1514 cell 8 maxburst 20      \
4122   avpkt 1000                       
4123 # tc class add dev eth0 parent 1:1 classid 1:4 cbq bandwidth 100Mbit  \
4124   rate 3Mbit weight 0.3Mbit prio 5 allot 1514 cell 8 maxburst 20      \
4125   avpkt 1000
4126 </Screen>
4128 </Para>
4130 <Para>
4131 These are our two leaf classes. Note how we scale the weight with the configured
4132 rate. Both classes are not bounded, but they are connected to class 1:1
4133 which is bounded.  So the sum of bandwith of the 2 classes will never be
4134 more than 6mbit. The classids need to be within the same major number as
4135 the parent qdisc, by the way!
4136 </Para>
4138 <Para>
4140 <Screen>
4141 # tc qdisc add dev eth0 parent 1:3 handle 30: sfq
4142 # tc qdisc add dev eth0 parent 1:4 handle 40: sfq
4143 </Screen>
4145 </Para>
4147 <Para>
4148 Both classes have a FIFO qdisc by default.  But we replaced these with an SFQ
4149 queue so each flow of data is treated equally.
4151 <Screen>
4152 # tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 match ip \
4153   sport 80 0xffff flowid 1:3
4154 # tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 match ip \
4155   sport 25 0xffff flowid 1:4
4156 </Screen>
4158 </Para>
4160 <Para>
4161 These commands, attached directly to the root, send traffic to the right
4162 qdiscs.
4163 </Para>
4165 <Para>
4166 Note that we use 'tc class add' to CREATE classes within a qdisc, but that
4167 we use 'tc qdisc add' to actually add qdiscs to these classes.
4168 </Para>
4170 <Para>
4171 You may wonder what happens to traffic that is not classified by any of the
4172 two rules. It appears that in this case, data will then be processed within
4173 1:0, and be unlimited. 
4174 </Para>
4176 <Para>
4177 If SMTP+web together try to exceed the set limit of 6mbit/s, bandwidth will
4178 be divided according to the weight parameter, giving 5/8 of traffic to  the
4179 webserver and 3/8 to the mail server.
4180 </Para>
4182 <Para>
4183 With this configuration you can also say that webserver traffic will always
4184 get at minimum 5/8 * 6 mbit = 3.75 mbit.
4185 </Para>
4187 </Sect3>
4189 <Sect3>
4190 <Title>Other CBQ parameters: split &amp; defmap</Title>
4192 <Para>
4193 As said before, a classful qdisc needs to call filters to determine
4194 which class a packet will be enqueued to. 
4195 </Para>
4197 <Para>
4198 Besides calling the filter, CBQ offers other options, defmap &amp; split.
4199 This is pretty complicated to understand, and it is not vital. But as this
4200 is the only known place where defmap &amp; split are properly explained, I'm
4201 doing my best. 
4202 </Para>
4204 <Para>
4205 As you will often want to filter on the Type of Service field only, a special
4206 syntax is provided. Whenever the CBQ needs to figure out where a packet
4207 needs to be enqueued, it checks if this node is a 'split node'. If so, one
4208 of the sub-qdiscs has indicated that it wishes to receive all packets with
4209 a certain configured priority, as might be derived from the TOS field, or
4210 socket options set by applications.
4211 </Para>
4213 <Para>
4214 The packets' priority bits are or-ed with the defmap field to see if a match
4215 exists. In other words, this is a short-hand way of creating a very fast
4216 filter, which only matches certain priorities. A defmap of ff (hex) will
4217 match everything, a map of 0 nothing. A sample configuration may help make
4218 things clearer:
4219 </Para>
4221 <Para>
4223 <Screen>
4224 # tc qdisc add dev eth1 root handle 1: cbq bandwidth 10Mbit allot 1514 \
4225   cell 8 avpkt 1000 mpu 64
4227 # tc class add dev eth1 parent 1:0 classid 1:1 cbq bandwidth 10Mbit    \
4228   rate 10Mbit allot 1514 cell 8 weight 1Mbit prio 8 maxburst 20        \
4229   avpkt 1000
4230 </Screen>
4232 Standard CBQ preamble. I never get used to the sheer amount of numbers
4233 required!
4234 </Para>
4236 <Para>
4237 Defmap refers to TC_PRIO bits, which are defined as follows:
4238 </Para>
4240 <Para>
4242 <Screen>
4243 TC_PRIO..          Num  Corresponds to TOS
4244 -------------------------------------------------
4245 BESTEFFORT         0    Maximize Reliablity        
4246 FILLER             1    Minimize Cost              
4247 BULK               2    Maximize Throughput (0x8)  
4248 INTERACTIVE_BULK   4                               
4249 INTERACTIVE        6    Minimize Delay (0x10)      
4250 CONTROL            7                               
4251 </Screen>
4253 </Para>
4255 <Para>
4256 The TC_PRIO.. number corresponds to bits, counted from the right. See the
4257 pfifo_fast section for more details how TOS bits are converted to
4258 priorities.
4259 </Para>
4261 <Para>
4262 Now the interactive and the bulk classes:
4263 </Para>
4265 <Para>
4267 <Screen>
4268 # tc class add dev eth1 parent 1:1 classid 1:2 cbq bandwidth 10Mbit     \
4269   rate 1Mbit allot 1514 cell 8 weight 100Kbit prio 3 maxburst 20        \
4270   avpkt 1000 split 1:0 defmap c0
4272 # tc class add dev eth1 parent 1:1 classid 1:3 cbq bandwidth 10Mbit     \
4273   rate 8Mbit allot 1514 cell 8 weight 800Kbit prio 7 maxburst 20        \
4274   avpkt 1000 split 1:0 defmap 3f
4275 </Screen>
4277 </Para>
4279 <Para>
4280 The 'split qdisc' is 1:0, which is where the choice will be made. C0 is
4281 binary for 11000000, 3F for 00111111, so these two together will match
4282 everything. The first class matches bits 7 &#38; 6, and thus corresponds 
4283 to 'interactive' and 'control' traffic. The second class matches the rest.
4284 </Para>
4286 <Para>
4287 Node 1:0 now has a table like this:
4289 <Screen>
4290 priority        send to
4291 0               1:3
4292 1               1:3
4293 2               1:3
4294 3               1:3
4295 4               1:3
4296 5               1:3
4297 6               1:2
4298 7               1:2
4299 </Screen>
4301 </Para>
4303 <Para>
4304 For additional fun, you can also pass a 'change mask', which indicates
4305 exactly which priorities you wish to change. You only need to use this if you
4306 are running 'tc class change'. For example, to add best effort traffic to
4307 1:2, we could run this:
4308 </Para>
4310 <Para>
4312 <Screen>
4313 # tc class change dev eth1 classid 1:2 cbq defmap 01/01
4314 </Screen>
4316 </Para>
4318 <Para>
4319 The priority map over at 1:0 now looks like this:
4320 </Para>
4322 <Para>
4324 <Screen>
4325 priority        send to
4326 0               1:2
4327 1               1:3
4328 2               1:3
4329 3               1:3
4330 4               1:3
4331 5               1:3
4332 6               1:2
4333 7               1:2
4334 </Screen>
4336 </Para>
4338 <Para>
4339 FIXME: did not test 'tc class change', only looked at the source.
4340 </Para>
4342 </Sect3>
4344 </Sect2>
4346 <Sect2>
4347 <Title>Hierarchical Token Bucket </Title>
4349 <Para>
4350 Martin Devera (&lt;devik&gt;) rightly realised that CBQ is complex and does
4351 not seem optimized for many typical situations. His Hierarchical approach is
4352 well suited for setups where you have a fixed amount of bandwidth which you
4353 want to divide for different purposes, giving each purpose a guaranteed
4354 bandwidth, with the possibility of specifying how much bandwidth can be
4355 borrowed.
4356 </Para>
4358 <Para>
4359 HTB works just like CBQ but does not resort to idle time calculations to
4360 shape. Instead, it is a classful Token Bucket Filter - hence the name. It
4361 has only a few parameters, which are well documented on his 
4362 <ULink
4363 URL="http://luxik.cdi.cz/~devik/qos/htb/"
4364 >site</ULink
4366 </Para>
4368 <Para>
4369 As your HTB configuration gets more complex, your configuration scales
4370 well. With CBQ it is already complex even in simple cases! HTB3 (check
4371 <ulink url="http://luxik.cdi.cz/~devik/qos/htb/">its homepage</ulink> for
4372 details on HTB versions) is now part of the official kernel sources 
4373 (from 2.4.20-pre1 and 2.5.31 onwards). However, maybe you still need to
4374 get a HTB3 patched version of 'tc': HTB kernel and userspace parts must
4375 be the same major version, or 'tc' will not work with HTB.
4377 </Para>
4379 <Para>
4380 If you already have a modern kernel, or are in a position to patch your 
4381 kernel, by all means consider HTB.
4382 </para>
4385 <Sect3>
4386 <Title>Sample configuration</Title>
4388 <Para>
4389 Functionally almost identical to the CBQ sample configuration above:
4390 </Para>
4392 <Para>
4394 <Screen>
4395 # tc qdisc add dev eth0 root handle 1: htb default 30
4397 # tc class add dev eth0 parent 1: classid 1:1 htb rate 6mbit burst 15k
4399 # tc class add dev eth0 parent 1:1 classid 1:10 htb rate 5mbit burst 15k
4400 # tc class add dev eth0 parent 1:1 classid 1:20 htb rate 3mbit ceil 6mbit burst 15k
4401 # tc class add dev eth0 parent 1:1 classid 1:30 htb rate 1kbit ceil 6mbit burst 15k
4402 </Screen>
4404 </Para>
4406 <Para>
4407 The author then recommends SFQ for beneath these classes:
4409 <Screen>
4410 # tc qdisc add dev eth0 parent 1:10 handle 10: sfq perturb 10
4411 # tc qdisc add dev eth0 parent 1:20 handle 20: sfq perturb 10
4412 # tc qdisc add dev eth0 parent 1:30 handle 30: sfq perturb 10
4413 </Screen>
4415 </Para>
4417 <Para>
4418 Add the filters which direct traffic to the right classes:
4420 <Screen>
4421 # U32="tc filter add dev eth0 protocol ip parent 1:0 prio 1 u32"
4422 # $U32 match ip dport 80 0xffff flowid 1:10
4423 # $U32 match ip sport 25 0xffff flowid 1:20
4424 </Screen>
4426 And that's it - no unsightly unexplained numbers, no undocumented
4427 parameters. 
4428 </Para>
4430 <Para>
4431 HTB certainly looks wonderful - if 10: and 20: both have their guaranteed
4432 bandwidth, and more is left to divide, they borrow in a 5:3 ratio, just as
4433 you would expect.
4434 </Para>
4436 <Para>
4437 Unclassified traffic gets routed to 30:, which has little bandwidth of its
4438 own but can borrow everything that is left over. Because we chose SFQ
4439 internally, we get fairness thrown in for free!
4440 </Para>
4442 </Sect3>
4444 </Sect2>
4446 </Sect1>
4448 <Sect1 id="lartc.qdisc.filters">
4449   <Title>Classifying packets with filters</Title>
4451 <Para>
4452 To determine which class shall process a packet, the so-called 'classifier
4453 chain' is called each time a choice needs to be made. This chain consists of
4454 all filters attached to the classful qdisc that needs to decide.
4455 </Para>
4457 <Para>To reiterate the tree, which is not a tree:
4458 </Para>
4460 <Screen width="80">
4461                     root 1:
4462                       |
4463                     _1:1_
4464                    /  |  \
4465                   /   |   \
4466                  /    |    \
4467                10:   11:   12:
4468               /   \       /   \
4469            10:1  10:2   12:1  12:2
4470 </Screen>
4472 <Para>
4473 When enqueueing a packet, at each branch the filter chain is consulted for a
4474 relevant instruction. A typical setup might be to have a filter in 1:1 that
4475 directs a packet to 12: and a filter on 12: that sends the packet to 12:2.
4476 </Para>
4478 <Para>
4479 You might also attach this latter rule to 1:1, but you can make efficiency
4480 gains by having more specific tests lower in the chain.
4481 </Para>
4483 <Para>
4484 You can't filter a packet 'upwards', by the way. Also, with HTB, you should
4485 attach all filters to the root!
4486 </Para>
4488 <Para>
4489 And again - packets are only enqueued downwards! When they are dequeued,
4490 they go up again, where the interface lives. They do NOT fall off the end of
4491 the tree to the network adaptor!
4492 </Para>
4494 <Sect2>
4495 <Title>Some simple filtering examples</Title>
4497 <Para>
4498 As explained in the Classifier chapter, you can match on literally anything,
4499 using a very complicated syntax. To start, we will show how to do the
4500 obvious things, which luckily are quite easy.
4501 </Para>
4503 <Para>
4504 Let's say we have a PRIO qdisc called '10:' which contains three classes, and
4505 we want to assign all traffic from and to port 22 to the highest priority
4506 band, the filters would be:
4507 </Para>
4509 <Para>
4511 <Screen>
4512 # tc filter add dev eth0 protocol ip parent 10: prio 1 u32 match \ 
4513   ip dport 22 0xffff flowid 10:1
4514 # tc filter add dev eth0 protocol ip parent 10: prio 1 u32 match \
4515   ip sport 80 0xffff flowid 10:1
4516 # tc filter add dev eth0 protocol ip parent 10: prio 2 flowid 10:2
4517 </Screen>
4519 </Para>
4521 <Para>
4522 What does this say? It says: attach to eth0, node 10: a  priority 1 u32
4523 filter that matches on IP destination port 22 *exactly* and send it to band
4524 10:1. And it then repeats the same for source port 80. The last command says
4525 that anything unmatched so far should go to band 10:2, the next-highest
4526 priority.
4527 </Para>
4529 <Para>
4530 You need to add 'eth0', or whatever your interface is called, because each
4531 interface has a unique namespace of handles.
4532 </Para>
4534 <Para>
4535 To select on an IP address, use this:
4537 <Screen>
4538 # tc filter add dev eth0 parent 10:0 protocol ip prio 1 u32 \ 
4539   match ip dst 4.3.2.1/32 flowid 10:1
4540 # tc filter add dev eth0 parent 10:0 protocol ip prio 1 u32 \
4541   match ip src 1.2.3.4/32 flowid 10:1
4542 # tc filter add dev eth0 protocol ip parent 10: prio 2      \
4543   flowid 10:2
4544 </Screen>
4546 </Para>
4548 <Para>
4549 This assigns traffic to 4.3.2.1 and traffic from 1.2.3.4 to the highest
4550 priority queue, and the rest to the next-highest one.
4551 </Para>
4553 <Para>
4554 You can concatenate matches, to match on traffic from 1.2.3.4 and from port
4555 80, do this:
4557 <Screen>
4558 # tc filter add dev eth0 parent 10:0 protocol ip prio 1 u32 match ip src 4.3.2.1/32
4559   match ip sport 80 0xffff flowid 10:1
4560 </Screen>
4562 </Para>
4564 </Sect2>
4566 <Sect2 id="lartc.filtering.simple">
4567 <Title>All the filtering commands you will normally need</Title>
4569 <Para>
4570 Most shaping commands presented here start with this preamble:
4572 <Screen>
4573 # tc filter add dev eth0 parent 1:0 protocol ip prio 1 u32 ..
4574 </Screen>
4576 These are the so called 'u32' matches, which can match on ANY part of a
4577 packet.
4578 <VariableList>
4580 <VarListEntry>
4581 <Term>On source/destination address</Term>
4582 <ListItem>
4583 <Para>
4584 Source mask 'match ip src 1.2.3.0/24', destination mask 'match ip dst
4585 4.3.2.0/24'. To match a single host, use /32, or omit the mask.
4586 </Para></ListItem>
4587 </VarListEntry>
4588 <VarListEntry>
4589 <Term>On source/destination port, all IP protocols</Term>
4590 <ListItem>
4591 <Para>
4592 Source: 'match ip sport 80 0xffff', 'match ip dport 0xffff'
4593 </Para></ListItem>
4594 </VarListEntry>
4595 <VarListEntry>
4596 <Term>On ip protocol (tcp, udp, icmp, gre, ipsec)</Term>
4597 <ListItem>
4598 <Para>
4599 Use the numbers from /etc/protocols, for example, icmp is 1: 'match ip
4600 protocol 1 0xff'. 
4601 </Para></ListItem>
4602 </VarListEntry>
4603 <VarListEntry>
4604 <Term>On fwmark</Term>
4605 <ListItem>
4606 <Para>
4607 You can mark packets with either ipchains or iptables and have that mark
4608 survive routing across interfaces. This is really useful to for example only
4609 shape traffic on eth1 that came in on eth0. Syntax:
4610 <screen>
4611 # tc filter add dev eth1 protocol ip parent 1:0 prio 1 handle 6 fw flowid 1:1
4612 </screen>
4613 Note that this is not a u32 match!
4614 </Para>
4616 <Para>
4617 You can place a mark like this:
4619 <Screen>
4620 # iptables -A PREROUTING -t mangle -i eth0 -j MARK --set-mark 6
4621 </Screen>
4623 The number 6 is arbitrary.
4624 </Para>
4626 <Para>
4627 If you don't want to understand the full tc filter syntax, just use
4628 iptables, and only learn to select on fwmark.
4629 </Para></ListItem>
4630 </VarListEntry>
4631 <VarListEntry>
4632 <Term>On the TOS field</Term>
4633 <ListItem>
4634 <Para>
4635 To select interactive, minimum delay traffic:
4637 <Screen>
4638 # tc filter add dev ppp0 parent 1:0 protocol ip prio 10 u32 \
4639       match ip tos 0x10 0xff \
4640      flowid 1:4
4641 </Screen>
4643 Use 0x08 0xff for bulk traffic.
4644 </Para></ListItem>
4645 </VarListEntry>
4646 </VariableList>
4647 </Para>
4649 <Para>
4650 For more filtering commands, see the Advanced Filters chapter.
4651 </Para>
4653 </Sect2>
4655 </Sect1>
4656 <Sect1 id="lartc.imq">
4657 <Title>The Intermediate queueing device (IMQ)</Title>
4659 <Para>
4660 The Intermediate queueing device is not a qdisc but its usage is tightly bound
4661 to qdiscs. Within linux, qdiscs are attached to network devices and everything
4662 that is queued to the device is first queued to the qdisc. From this concept,
4663 two limitations arise:
4664 </Para>
4666 <Para>
4667 1. Only egress shaping is possible (an ingress qdisc exists, but its
4668 possibilities are very limited compared to classful qdiscs).
4669 </Para>
4671 <Para>
4672 2. A qdisc can only see traffic of one interface, global limitations can't be
4673 placed.
4674 </Para>
4676 <Para>
4677 IMQ is there to help solve those two limitations. In short, you can put 
4678 everything you choose in a qdisc. Specially marked packets get intercepted
4679 in netfilter NF_IP_PRE_ROUTING and NF_IP_POST_ROUTING hooks and pass through
4680 the qdisc attached to an imq device. An iptables target is used for marking
4681 the packets.
4682 </Para>
4684 <Para>
4685 This enables you to do ingress shaping as you can just mark packets coming in from somewhere and/or treat interfaces as classes to set global limits.
4686 You can also do lots of other stuff like just putting your http traffic in a
4687 qdisc, put new connection requests in a qdisc, ...
4688 </Para>
4690 <Sect2>
4691 <Title>Sample configuration</Title>
4693 <Para>
4694 The first thing that might come to mind is use ingress shaping to give yourself
4695 a high guaranteed bandwidth. ;)
4696 Configuration is just like with any other interface:
4698 <Screen>
4699 tc qdisc add dev imq0 root handle 1: htb default 20
4701 tc class add dev imq0 parent 1: classid 1:1 htb rate 2mbit burst 15k
4703 tc class add dev imq0 parent 1:1 classid 1:10 htb rate 1mbit
4704 tc class add dev imq0 parent 1:1 classid 1:20 htb rate 1mbit
4706 tc qdisc add dev imq0 parent 1:10 handle 10: pfifo
4707 tc qdisc add dev imq0 parent 1:20 handle 20: sfq
4709 tc filter add dev imq0 parent 10:0 protocol ip prio 1 u32 match \
4710                 ip dst 10.0.0.230/32 flowid 1:10
4711 </Screen>
4713 In this example u32 is used for classification. Other classifiers should work as
4714 expected.
4715 Next traffic has to be selected and marked to be enqueued to imq0.
4717 <Screen>
4718 iptables -t mangle -A PREROUTING -i eth0 -j IMQ --todev 0
4720 ip link set imq0 up
4721 </Screen>
4723 </Para>
4725 <Para>
4726 The IMQ iptables targets is valid in the PREROUTING and POSTROUTING chains of
4727 the mangle table. It's syntax is 
4729 <Screen>
4730 IMQ [ --todev n ]       n : number of imq device
4731 </Screen>
4733 An ip6tables target is also provided.
4734 </Para>
4736 <Para>
4737 Please note traffic is not enqueued when the target is hit but afterwards.
4738 The exact location where traffic enters the imq device depends on the
4739 direction of the traffic (in/out).
4740 These are the predefined netfilter hooks used by iptables:
4742 <Screen>
4743 enum nf_ip_hook_priorities {
4744         NF_IP_PRI_FIRST = INT_MIN,
4745         NF_IP_PRI_CONNTRACK = -200,
4746         NF_IP_PRI_MANGLE = -150,
4747         NF_IP_PRI_NAT_DST = -100,
4748         NF_IP_PRI_FILTER = 0,
4749         NF_IP_PRI_NAT_SRC = 100,
4750         NF_IP_PRI_LAST = INT_MAX,
4752 </Screen>
4754 </Para>
4756 <Para>
4757 For ingress traffic, imq registers itself with NF_IP_PRI_MANGLE + 1 priority
4758 which means packets enter the imq device directly after the mangle PREROUTING
4759 chain has been passed.
4760 </Para>
4762 <Para>
4763 For egress imq uses NF_IP_PRI_LAST which honours the fact that packets dropped
4764 by the filter table won't occupy bandwidth.
4765 </Para>
4767 <Para>
4768 The patches and some more information can be found at the
4769 <ULink
4770 URL="http://luxik.cdi.cz/~patrick/imq/"
4771 >imq site</ULink>.
4772 </Para>
4774 </Sect2>
4776 </Sect1>
4778 </chapter>
4780 <chapter id="lartc.loadshare">
4781 <Title>Load sharing over multiple interfaces</Title>
4783 <Para>
4784 There are several ways of doing this. One of the easiest and straightforward
4785 ways is 'TEQL' - "True" (or "trivial") link equalizer. Like most things
4786 having to do with queueing, load sharing goes both ways. Both ends of a link
4787 may need to participate for full effect.
4788 </Para>
4790 <Para>
4791 Imagine this situation:
4792 </Para>
4794 <Para>
4796 <Screen>
4797                  +-------+   eth1   +-------+
4798                  |       |==========|       |
4799  'network 1' ----|   A   |          |   B   |---- 'network 2'
4800                  |       |==========|       |
4801                  +-------+   eth2   +-------+
4802 </Screen>
4804 </Para>
4806 <Para>
4807 A and B are routers, and for the moment we'll assume both run Linux. If
4808 traffic is going from network 1 to network 2, router A needs to distribute
4809 the packets over both links to B. Router B needs to be configured to accept
4810 this. Same goes the other way around, when packets go from network 2 to
4811 network 1, router B needs to send the packets over both eth1 and eth2.
4812 </Para>
4814 <Para>
4815 The distributing part is done by a 'TEQL' device, like this (it couldn't be
4816 easier):
4817 </Para>
4819 <Para>
4821 <Screen>
4822 # tc qdisc add dev eth1 root teql0
4823 # tc qdisc add dev eth2 root teql0
4824 # ip link set dev teql0 up
4825 </Screen>
4827 </Para>
4829 <Para>
4830 Don't forget the 'ip link set up' command!
4831 </Para>
4833 <Para>
4834 This needs to be done on both hosts. The device teql0 is basically a
4835 roundrobbin distributor over eth1 and eth2, for sending packets. No data
4836 ever comes in over an teql device, that just appears on the 'raw' eth1 and
4837 eth2.
4838 </Para>
4840 <Para>
4841 But now we just have devices, we also need proper routing. One way to do
4842 this is to assign a /31 network to both links, and a /31 to the teql0 device
4843 as well:
4844 </Para>
4846 <Para>
4847 On router A:
4849 <Screen>
4850 # ip addr add dev eth1 10.0.0.0/31
4851 # ip addr add dev eth2 10.0.0.2/31
4852 # ip addr add dev teql0 10.0.0.4/31
4853 </Screen>
4855 </Para>
4857 <Para>
4858 On router B:
4860 <Screen>
4861 # ip addr add dev eth1 10.0.0.1/31
4862 # ip addr add dev eth2 10.0.0.3/31
4863 # ip addr add dev teql0 10.0.0.5/31
4864 </Screen>
4866 </Para>
4868 <Para>
4869 Router A should now be able to ping 10.0.0.1, 10.0.0.3 and 10.0.0.5 over the
4870 2 real links and the 1 equalized device. Router B should be able to ping
4871 10.0.0.0, 10.0.0.2 and 10.0.0.4 over the links.
4872 </Para>
4874 <Para>
4875 If this works, Router A should make 10.0.0.5 its route for reaching network
4876 2, and Router B should make 10.0.0.4 its route for reaching network 1. For
4877 the special case where network 1 is your network at home, and network 2 is
4878 the Internet, Router A should make 10.0.0.5 its default gateway.
4879 </Para>
4881 <Sect1 id="lartc.loadshare.caveats">
4882   <Title>Caveats</Title>
4884 <Para>
4885 Nothing is as easy as it seems. eth1 and eth2 on both router A and B need to
4886 have return path filtering turned off, because they will otherwise drop
4887 packets destined for ip addresses other than their own:
4888 </Para>
4890 <Para>
4892 <Screen>
4893 # echo 0 &#62; /proc/sys/net/ipv4/conf/eth1/rp_filter
4894 # echo 0 &#62; /proc/sys/net/ipv4/conf/eth2/rp_filter
4895 </Screen>
4897 </Para>
4899 <Para>
4900 Then there is the nasty problem of packet reordering. Let's say 6 packets
4901 need to be sent from A to B - eth1 might get 1, 3 and 5. eth2 would then do
4902 2, 4 and 6. In an ideal world, router B would receive this in order, 1, 2,
4903 3, 4, 5, 6. But the possibility is very real that the kernel gets it like
4904 this: 2, 1, 4, 3, 6, 5. The problem is that this confuses TCP/IP. While not
4905 a problem for links carrying many different TCP/IP sessions, you won't be
4906 able to to a bundle multiple links and get to ftp a single file lots faster,
4907 except when your receiving or sending OS is Linux, which is not easily
4908 shaken by some simple reordering.
4909 </Para>
4911 <Para>
4912 However, for lots of applications, link load balancing is a great idea.
4913 </Para>
4915 </Sect1>
4916 <Sect1 id="lartc.loadshare.other">
4917   <Title>Other possibilities</Title>
4918 <para>
4919 William Stearns has used an advanced tunneling setup to achieve good use of
4920 multiple, unrelated, internet connections together. It can be found on
4921 <ULink
4922 URL="http://www.stearns.org/tunnel/">his tunneling page</ULink>.
4923 </para>
4924 <para>
4925 The HOWTO may feature more about this in the future.
4926 </para>
4927 </Sect1>
4928 </chapter>
4930 <chapter id="lartc.netfilter">
4931 <Title>Netfilter &amp; iproute - marking packets</Title>
4933 <Para>
4934 So far we've seen how iproute works, and netfilter was mentioned a few
4935 times. This would be a good time to browse through <ULink
4936 URL="http://netfilter.samba.org/unreliable-guides/"
4937 >Rusty's Remarkably Unreliable Guides</ULink
4938 >. Netfilter itself
4939 can be found <ULink
4940 URL="http://netfilter.filewatcher.org/"
4941 >here</ULink
4943 </Para>
4945 <Para>
4946 Netfilter allows us to filter packets, or mangle their headers. One special
4947 feature is that we can mark a packet with a number. This is done with the
4948 --set-mark facility. 
4949 </Para>
4951 <Para>
4952 As an example, this command marks all packets destined for port 25, outgoing
4953 mail:
4954 </Para>
4956 <Para>
4958 <Screen>
4959 # iptables -A PREROUTING -i eth0 -t mangle -p tcp --dport 25 \
4960  -j MARK --set-mark 1
4961 </Screen>
4963 </Para>
4965 <Para>
4966 Let's say that we have multiple connections, one that is fast (and
4967 expensive, per megabyte) and one that is slower, but flat fee. We would most
4968 certainly like outgoing mail to go via the cheap route.
4969 </Para>
4971 <Para>
4972 We've already marked the packets with a '1', we now instruct the routing
4973 policy database to act on this:
4974 </Para>
4976 <Para>
4978 <Screen>
4979 # echo 201 mail.out &#62;&#62; /etc/iproute2/rt_tables
4980 # ip rule add fwmark 1 table mail.out
4981 # ip rule ls
4982 0:      from all lookup local 
4983 32764:  from all fwmark        1 lookup mail.out 
4984 32766:  from all lookup main 
4985 32767:  from all lookup default 
4986 </Screen>
4988 </Para>
4990 <Para>
4991 Now we generate the mail.out table with a route to the slow but cheap link:
4993 <Screen>
4994 # /sbin/ip route add default via 195.96.98.253 dev ppp0 table mail.out
4995 </Screen>
4997 </Para>
4999 <Para>
5000 And we are done. Should we want to make exceptions, there are lots of ways
5001 to achieve this. We can modify the netfilter statement to exclude certain
5002 hosts, or we can insert a rule with a lower priority that points to the main
5003 table for our excepted hosts.
5004 </Para>
5006 <Para>
5007 We can also use this feature to honour TOS bits by marking packets with a
5008 different type of service with different numbers, and creating rules to act
5009 on that. This way you can even dedicate, say, an ISDN line to interactive
5010 sessions.
5011 </Para>
5013 <Para>
5014 Needless to say, this also works fine on a host that's doing NAT
5015 ('masquerading').
5016 </Para>
5018 <Para>
5019 IMPORTANT: We received a report that MASQ and SNAT at least collide
5020 with marking packets. Rusty Russell explains it in
5021 <ULink
5022 URL="http://lists.samba.org/pipermail/netfilter/2000-November/006089.html"
5023 >this posting</ULink
5024 >. Turn off the reverse path filter to make it work
5025 properly.
5026 </Para>
5028 <Para>
5029 Note: to mark packets, you need to have some options enabled in your
5030 kernel:
5031 </Para>
5033 <Para>
5035 <Screen>
5036 IP: advanced router (CONFIG_IP_ADVANCED_ROUTER) [Y/n/?]
5037 IP: policy routing (CONFIG_IP_MULTIPLE_TABLES) [Y/n/?]
5038 IP: use netfilter MARK value as routing key (CONFIG_IP_ROUTE_FWMARK) [Y/n/?]
5039 </Screen>
5041 </Para>
5043 <Para>
5044 See also the <xref linkend="lartc.cookbook.squid"> in the
5045 <citetitle><xref linkend="lartc.cookbook"></citetitle>.
5046 </Para>
5048 </chapter>
5050 <chapter id="lartc.adv-filter"
5051   xreflabel="Advanced filters for (re-)classifying packets">
5052   <Title>Advanced filters for (re-)classifying packets</Title>
5054 <Para>
5055 As explained in the section on classful queueing disciplines, filters are
5056 needed to classify packets into any of the sub-queues. These filters are
5057 called from within the classful qdisc.
5058 </Para>
5060 <Para>
5061 Here is an incomplete list of classifiers available:
5062 <VariableList>
5064 <VarListEntry>
5065 <Term>fw</Term>
5066 <ListItem>
5067 <Para>
5068 Bases the decision on how the firewall has marked the packet. This can be
5069 the easy way out if you don't want to learn tc filter syntax. See the
5070 Queueing chapter for details.
5071 </Para></ListItem>
5072 </VarListEntry>
5073 <VarListEntry>
5074 <Term>u32</Term>
5075 <ListItem>
5076 <Para>
5077 Bases the decision on fields within the packet (i.e. source IP address, etc)
5078 </Para></ListItem>
5079 </VarListEntry>
5080 <VarListEntry>
5081 <Term>route</Term>
5082 <ListItem>
5083 <Para>
5084 Bases the decision on which route the packet will be routed by
5085 </Para></ListItem>
5086 </VarListEntry>
5087 <VarListEntry>
5088 <Term>rsvp, rsvp6</Term>
5089 <ListItem>
5090 <Para>
5091 Routes packets based on <ULink
5092 URL="http://www.isi.edu/div7/rsvp/overview.html"
5093 >RSVP </ULink
5094 >. Only useful
5095 on networks you control - the Internet does not respect RSVP.
5096 </Para></ListItem>
5097 </VarListEntry>
5098 <VarListEntry>
5099 <Term>tcindex</Term>
5100 <ListItem>
5101 <Para>
5102 Used in the DSMARK qdisc, see the relevant section.
5103 </Para></ListItem>
5104 </VarListEntry>
5105 </VariableList>
5106 </Para>
5108 <Para>
5109 Note that in general there are many ways in which you can classify packet
5110 and that it generally comes down to preference as to which system you wish
5111 to use.
5112 </Para>
5114 <Para>
5115 Classifiers in general accept a few arguments in common. They are listed
5116 here for convenience:
5117 </Para>
5119 <Para>
5120 <VariableList>
5122 <VarListEntry>
5123 <Term>protocol</Term>
5124 <ListItem>
5125 <Para>
5126 The protocol this classifier will accept. Generally you will only be
5127 accepting only IP traffic. Required.
5128 </Para></ListItem>
5129 </VarListEntry>
5130 <VarListEntry>
5131 <Term>parent</Term>
5132 <ListItem>
5133 <Para>
5134 The handle this classifier is to be attached to. This handle must be
5135 an already existing class. Required.
5136 </Para></ListItem>
5137 </VarListEntry>
5138 <VarListEntry>
5139 <Term>prio</Term>
5140 <ListItem>
5141 <Para>
5142 The priority of this classifier. Lower numbers get tested first.
5143 </Para></ListItem>
5144 </VarListEntry>
5145 <VarListEntry>
5146 <Term>handle</Term>
5147 <ListItem>
5148 <Para>
5149 This handle means different things to different filters.
5150 </Para></ListItem>
5151 </VarListEntry>
5152 </VariableList>
5153 </Para>
5155 <Para>
5156 All the following sections will assume you are trying to shape the traffic
5157 going to <Literal remap="tt">HostA</Literal>. They will assume that the root class has been
5158 configured on 1: and that the class you want to send the selected traffic to
5159 is 1:1.
5160 </Para>
5162 <Sect1 id="lartc.adv-filter.u32">
5163   <Title>The <option>u32</option> classifier</Title>
5165 <Para>
5166 The U32 filter is the most advanced filter available in the current
5167 implementation. It entirely based on hashing tables, which make it
5168 robust when there are many filter rules.
5169 </Para>
5171 <Para>
5172 In its simplest form the U32 filter is a list of records, each
5173 consisting of two fields: a selector and an action. The selectors,
5174 described below, are compared with the currently processed IP packet
5175 until the first match occurs, and then the associated action is performed.
5176 The simplest type of action would be directing the packet into defined
5177 CBQ class.
5178 </Para>
5180 <Para>
5181 The command line of <Literal remap="tt">tc filter</Literal> program, used to configure the filter,
5182 consists of three parts: filter specification, a selector and an action.
5183 The filter specification can be defined as:
5184 </Para>
5186 <Para>
5188 <Screen>
5189 tc filter add dev IF [ protocol PROTO ]
5190                      [ (preference|priority) PRIO ]
5191                      [ parent CBQ ]
5192 </Screen>
5194 </Para>
5196 <Para>
5197 The <Literal remap="tt">protocol</Literal> field describes protocol that the filter will be
5198 applied to. We will only discuss case of <Literal remap="tt">ip</Literal> protocol. The
5199 <Literal remap="tt">preference</Literal> field (<Literal remap="tt">priority</Literal> can be used alternatively)
5200 sets the priority of currently defined filter. This is important, since
5201 you can have several filters (lists of rules) with different priorities.
5202 Each list will be passed in the order the rules were added, then list with
5203 lower priority (higher preference number) will be processed. The <Literal remap="tt">parent</Literal>
5204 field defines the CBQ tree top (e.g. 1:0), the filter should be attached
5206 </Para>
5208 <Para>
5209 The options described above apply to all filters, not only U32.
5210 </Para>
5212 <Sect2>
5213 <Title>U32 selector </Title>
5215 <Para>
5216 The U32 selector contains definition of the pattern, that will be matched
5217 to the currently processed packet. Precisely, it defines which bits are
5218 to be matched in the packet header and nothing more, but this simple
5219 method is very powerful. Let's take a look at the following examples,
5220 taken directly from a pretty complex, real-world filter:
5221 </Para>
5223 <Para>
5225 <Screen>
5226 # tc filter add dev eth0 protocol ip parent 1:0 pref 10 u32 \
5227   match u32 00100000 00ff0000 at 0 flowid 1:10
5228 </Screen>
5230 </Para>
5232 <Para>
5233 For now, leave the first line alone - all these parameters describe
5234 the filter's hash tables. Focus on the selector line, containing
5235 <Literal remap="tt">match</Literal> keyword. This selector will match to IP headers, whose
5236 second byte will be 0x10 (0010). As you can guess, the 00ff number is
5237 the match mask, telling the filter exactly which bits to match. Here
5238 it's 0xff, so the byte will match if it's exactly 0x10. The <Literal remap="tt">at</Literal>
5239 keyword means that the match is to be started at specified offset (in
5240 bytes) -- in this case it's beginning of the packet.  Translating all
5241 that to human language, the packet will match if its Type of Service
5242 field will have `low delay' bits set. Let's analyze another rule:
5243 </Para>
5245 <Para>
5247 <Screen>
5248 # tc filter add dev eth0 protocol ip parent 1:0 pref 10 u32 \
5249   match u32 00000016 0000ffff at nexthdr+0 flowid 1:10
5250 </Screen>
5252 </Para>
5254 <Para>
5255 The <Literal remap="tt">nexthdr</Literal> option means next header encapsulated in the IP packet,
5256 i.e. header of upper-layer protocol. The match will also start here
5257 at the beginning of the next header. The match should occur in the
5258 second, 32-bit word of the header. In TCP and UDP protocols this field
5259 contains packet's destination port. The number is given in big-endian
5260 format, i.e. older bits first, so we simply read 0x0016 as 22 decimal,
5261 which stands for SSH service if this was TCP. As you guess, this match
5262 is ambiguous without a context, and we will discuss this later.
5263 </Para>
5265 <Para>
5266 Having understood all the above, we will find the following selector
5267 quite easy to read: <Literal remap="tt">match c0a80100 ffffff00 at 16</Literal>. What we
5268 got here is a three byte match at 17-th byte, counting from the IP
5269 header start. This will match for packets with destination address
5270 anywhere in 192.168.1/24 network. After analyzing the examples, we
5271 can summarize what we have learned.
5272 </Para>
5274 </Sect2>
5276 <Sect2>
5277 <Title>General selectors</Title>
5279 <Para>
5280 General selectors define the pattern, mask and offset the pattern
5281 will be matched to the packet contents. Using the general selectors
5282 you can match virtually any single bit in the IP (or upper layer)
5283 header. They are more difficult to write and read, though, than
5284 specific selectors that described below. The general selector syntax
5286 </Para>
5288 <Para>
5290 <Screen>
5291 match [ u32 | u16 | u8 ] PATTERN MASK [ at OFFSET | nexthdr+OFFSET]
5292 </Screen>
5294 </Para>
5296 <Para>
5297 One of the keywords <Literal remap="tt">u32</Literal>, <Literal remap="tt">u16</Literal> or <Literal remap="tt">u8</Literal> specifies
5298 length of the pattern in bits. PATTERN and MASK should follow, of length
5299 defined by the previous keyword. The OFFSET parameter is the offset,
5300 in bytes, to start matching. If <Literal remap="tt">nexthdr+</Literal> keyword is given,
5301 the offset is relative to start of the upper layer header.
5302 </Para>
5304 <Para>
5305 Some examples:
5306 </Para>
5308 <Para>
5310 <Screen>
5311 # tc filter add dev ppp14 parent 1:0 prio 10 u32 \
5312      match u8 64 0xff at 8 \
5313      flowid 1:4
5314 </Screen>
5316 </Para>
5318 <Para>
5319 Packet will match to this rule, if its time to live (TTL) is 64.
5320 TTL is the field starting just after 8-th byte of the IP header.
5321 </Para>
5324 <Para>
5325 Matches all TCP packets which have the ACK bit set:
5326 </Para>
5328 <Para>
5330 <Screen>
5331 # tc filter add dev ppp14 parent 1:0 prio 10 u32 \
5332      match ip protocol 6 0xff \
5333      match u8 0x10 0xff at nexthdr+13 \
5334      flowid 1:3 
5335 </Screen>
5337 </Para>
5339 <Para>
5340 Use this to match ACKs on packets smaller than 64 bytes:
5341 </Para>
5343 <Para>
5345 <Screen>
5346 ## match acks the hard way,
5347 ## IP protocol 6,
5348 ## IP header length 0x5(32 bit words),
5349 ## IP Total length 0x34 (ACK + 12 bytes of TCP options)
5350 ## TCP ack set (bit 5, offset 33)
5351 # tc filter add dev ppp14 parent 1:0 protocol ip prio 10 u32 \
5352             match ip protocol 6 0xff \
5353             match u8 0x05 0x0f at 0 \
5354             match u16 0x0000 0xffc0 at 2 \
5355             match u8 0x10 0xff at 33 \
5356             flowid 1:3
5357 </Screen>
5359 </Para>
5361 <Para>
5362 This rule will only match TCP packets with ACK bit set, and no further
5363 payload. Here we can see an example of using two selectors, the final result
5364 will be logical AND of their results. If we take a look at TCP header
5365 diagram, we can see that the ACK bit is second older bit (0x10) in the 14-th
5366 byte of the TCP header (<Literal remap="tt">at nexthdr+13</Literal>).  As for the second
5367 selector, if we'd like to make our life harder, we could write <Literal remap="tt">match u8
5368 0x06 0xff at 9</Literal> instead of using the specific selector <Literal remap="tt">protocol
5369 tcp</Literal>, because 6 is the number of TCP protocol, present in 10-th byte of
5370 the IP header. On the other hand, in this example we couldn't use any
5371 specific selector for the first match - simply because there's no specific
5372 selector to match TCP ACK bits.
5373 </Para>
5375 <Para>
5376 The filter below is a modified version of the filter above. The difference is, that it
5377 doesn't check the ip header length. Why? Because the filter above does only work on 32
5378 bit systems.
5379 </Para>
5381 <Para>
5383 <Screen>
5384 tc filter add dev ppp14 parent 1:0 protocol ip prio 10 u32 \
5385      match ip protocol 6 0xff \
5386      match u8 0x10 0xff at nexthdr+13 \
5387      match u16 0x0000 0xffc0 at 2 \
5388      flowid 1:3
5389 </Screen>
5391 </Para>
5394 </Sect2>
5396 <Sect2>
5397 <Title>Specific selectors</Title>
5399 <Para>
5400 The following table contains a list of all specific selectors 
5401 the author of this section has found in the <Literal remap="tt">tc</Literal> program
5402 source code. They simply make your life easier and increase readability
5403 of your filter's configuration.
5404 </Para>
5406 <Para>
5407 FIXME: table placeholder - the table is in separate file ,,selector.html''
5408 </Para>
5410 <Para>
5411 FIXME: it's also still in Polish :-(
5412 </Para>
5414 <Para>
5415 FIXME: must be sgml'ized
5416 </Para>
5418 <Para>
5419 Some examples:
5420 </Para>
5422 <Para>
5424 <Screen>
5425 # tc filter add dev ppp0 parent 1:0 prio 10 u32 \
5426      match ip tos 0x10 0xff \
5427      flowid 1:4
5428 </Screen>
5430 </Para>
5432 <Para>
5433 FIXME: tcp dport match does not work as described below:
5434 </Para>
5436 <Para>
5437 The above rule will match packets which have the TOS field set to 0x10.
5438 The TOS field starts at second byte of the packet and is one byte big,
5439 so we could write an equivalent general selector: <Literal remap="tt">match u8 0x10 0xff
5440 at 1</Literal>. This gives us hint to the internals of U32 filter -- the
5441 specific rules are always translated to general ones, and in this
5442 form they are stored in the kernel memory. This leads to another conclusion
5443 -- the <Literal remap="tt">tcp</Literal> and <Literal remap="tt">udp</Literal> selectors are exactly the same
5444 and this is why you can't use single <Literal remap="tt">match tcp dport 53 0xffff</Literal>
5445 selector to match TCP packets sent to given port -- they will also
5446 match UDP packets sent to this port. You must remember to also specify
5447 the protocol and end up with the following rule:
5448 </Para>
5450 <Para>
5452 <Screen>
5453 # tc filter add dev ppp0 parent 1:0 prio 10 u32 \
5454         match tcp dport 53 0xffff \
5455         match ip protocol 0x6 0xff \
5456         flowid 1:2
5457 </Screen>
5459 </Para>
5461 </Sect2>
5463 </Sect1>
5465 <Sect1 id="lartc.adv-filter.route">
5466   <Title>The <option>route</option> classifier</Title>
5468 <Para>
5469 This classifier filters based on the results of the routing tables. When a
5470 packet that is traversing through the classes reaches one that is marked
5471 with the "route" filter, it splits the packets up based on information in
5472 the routing table.
5473 </Para>
5475 <Para>
5477 <Screen>
5478 # tc filter add dev eth1 parent 1:0 protocol ip prio 100 route
5479 </Screen>
5481 </Para>
5483 <Para>
5484 Here we add a route classifier onto the parent node 1:0 with priority 100. 
5485 When a packet reaches this node (which, since it is the root, will happen
5486 immediately) it will consult the routing table and if one matches will
5487 send it to the given class and give it a priority of 100. Then, to finally
5488 kick it into action, you add the appropriate routing entry:
5489 </Para>
5491 <Para>
5492 The trick here is to define 'realm' based on either destination or source. 
5493 The way to do it is like this:
5494 </Para>
5496 <Para>
5498 <Screen>
5499 # ip route add Host/Network via Gateway dev Device realm RealmNumber
5500 </Screen>
5502 </Para>
5504 <Para>
5505 For instance, we can define our destination network 192.168.10.0 with a realm
5506 number 10:
5507 </Para>
5509 <Para>
5511 <Screen>
5512 # ip route add 192.168.10.0/24 via 192.168.10.1 dev eth1 realm 10
5513 </Screen>
5514   
5515 </Para>
5517 <Para>
5518 When adding route filters, we can use realm numbers to represent the
5519 networks or hosts and specify how the routes match the filters.
5520 </Para>
5522 <Para>
5524 <Screen>
5525 # tc filter add dev eth1 parent 1:0 protocol ip prio 100 \
5526   route to 10 classid 1:10
5527 </Screen>
5528   
5529 </Para>
5531 <Para>
5532 The above rule says packets going to the network 192.168.10.0 match class id
5533 1:10.
5534 </Para>
5536 <Para>
5537 Route filter can also be used to match source routes. For example, there is 
5538 a subnetwork attached to the Linux router on eth2.
5539 </Para>
5541 <Para>
5543 <Screen>
5544 # ip route add 192.168.2.0/24 dev eth2 realm 2
5545 # tc filter add dev eth1 parent 1:0 protocol ip prio 100 \
5546   route from 2 classid 1:2
5547 </Screen>
5549 </Para>
5551 <Para>
5552 Here the filter specifies that packets from the subnetwork 192.168.2.0
5553 (realm 2) will match class id 1:2.
5554 </Para>
5556 </Sect1>
5558 <Sect1 id="lartc.adv-filter.policing">
5559   <Title>Policing filters</Title>
5561 <Para>
5562 To make even more complicated setups possible, you can have filters that
5563 only match up to a certain bandwidth. You can declare a filter to entirely
5564 cease matching above a certain rate, or only to not match only the bandwidth
5565 exceeding a certain rate.
5566 </Para>
5568 <Para>
5569 So if you decided to police at 4mbit/s, but 5mbit/s of traffic is present,
5570 you can stop matching either the entire 5mbit/s, or only not match 1mbit/s,
5571 and do send 4mbit/s to the configured class.
5572 </Para>
5574 <Para>
5575 If bandwidth exceeds the configured rate, you can drop a packet, reclassify
5576 it, or see if another filter will match it.
5577 </Para>
5579 <Sect2>
5580 <Title>Ways to police</Title>
5582 <Para>
5583 There are basically two ways to police. If you compiled the kernel 
5584 with 'Estimators', the kernel can measure for each filter how much traffic
5585 it is passing, more or less. These estimators are very easy on the CPU, as
5586 they simply count 25 times per second how many data has been passed, and
5587 calculate the bitrate from that.
5588 </Para>
5590 <Para>
5591 The other way works again via a Token Bucket Filter, this time living within
5592 your filter. The TBF only matches traffic UP TO your configured bandwidth,
5593 if more is offered, only the excess is subject to the configured overlimit
5594 action.
5595 </Para>
5597 <Sect3>
5598 <Title>With the kernel estimator</Title>
5600 <Para>
5601 This is very simple and has only one parameter: avrate. Either the flow
5602 remains below avrate, and the filter classifies the traffic to the classid
5603 configured, or your rate exceeds it in which case the specified action is
5604 taken, which is 'reclassify' by default.
5605 </Para>
5607 <Para>
5608 The kernel uses an Exponential Weighted Moving Average for your bandwidth
5609 which makes it less sensitive to short bursts.
5610 </Para>
5612 </Sect3>
5614 <Sect3>
5615 <Title>With Token Bucket Filter</Title>
5617 <Para>
5618 Uses the following parameters:
5620 <ItemizedList>
5621 <ListItem>
5623 <Para>
5624 buffer/maxburst
5625 </Para>
5626 </ListItem>
5627 <ListItem>
5629 <Para>
5630 mtu/minburst
5631 </Para>
5632 </ListItem>
5633 <ListItem>
5635 <Para>
5637 </Para>
5638 </ListItem>
5639 <ListItem>
5641 <Para>
5642 rate
5643 </Para>
5644 </ListItem>
5646 </ItemizedList>
5648 </Para>
5650 <Para>
5651 Which behave mostly identical to those described in the Token Bucket Filter
5652 section. Please note however that if you set the mtu of a TBF policer too
5653 low, *no* packets will pass, whereas the egress TBF qdisc will just pass
5654 them slower.
5655 </Para>
5657 <Para>
5658 Another difference is that a policer can only let a packet pass, or drop it.
5659 It cannot delay hold on to it in order to delay it.
5660 </Para>
5662 </Sect3>
5664 </Sect2>
5666 <Sect2>
5667 <Title>Overlimit actions</Title>
5669 <Para>
5670 If your filter decides that it is overlimit, it can take 'actions'.
5671 Currently, three actions are available:
5672 <VariableList>
5674 <VarListEntry>
5675 <Term>continue</Term>
5676 <ListItem>
5677 <Para>
5678 Causes this filter not to match, but perhaps other filters will.
5679 </Para></ListItem>
5680 </VarListEntry>
5681 <VarListEntry>
5682 <Term>drop</Term>
5683 <ListItem>
5684 <Para>
5685 This is a very fierce option which simply discards traffic exceeding a
5686 certain rate. It is often used in the ingress policer and has limited uses.
5687 For example, you may have a name server that falls over if offered more than
5688 5mbit/s of packets, in which case an ingress filter could be used to make
5689 sure no more is ever offered.
5690 </Para></ListItem>
5691 </VarListEntry>
5692 <VarListEntry>
5693 <Term>Pass/OK</Term>
5694 <ListItem>
5695 <Para>
5696 Pass on traffic ok. Might be used to disable a complicated filter, but leave
5697 it in place.
5698 </Para></ListItem>
5699 </VarListEntry>
5700 <VarListEntry>
5701 <Term>reclassify</Term>
5702 <ListItem>
5703 <Para>
5704 Most often comes down to reclassification to Best Effort. This is the
5705 default action.
5706 </Para></ListItem>
5707 </VarListEntry>
5708 </VariableList>
5709 </Para>
5711 </Sect2>
5713 <Sect2>
5714 <Title>Examples</Title>
5716 <Para>
5717 The only real example known is mentioned in the 'Protecting your host 
5718 from SYN floods' section. 
5719 </Para>
5721 <Para>
5722 FIXME: if you have used this, please share your experience with us
5723 </Para>
5725 </Sect2>
5727 </Sect1>
5729 <Sect1 id="lartc.adv-filter.hashing">
5730 <Title>Hashing filters for very fast massive filtering</Title>
5732 <Para>
5733 If you have a need for thousands of rules, for example if you have a lot of
5734 clients or computers, all with different QoS specifications, you may find
5735 that the kernel spends a lot of time matching all those rules.
5736 </Para>
5738 <Para>
5739 By default, all filters reside in one big chain which is matched in
5740 descending order of priority. If you have 1000 rules, 1000 checks may be
5741 needed to determine what to do with a packet.
5742 </Para>
5744 <Para>
5745 Matching would go much quicker if you would have 256 chains with each four
5746 rules - if you could divide packets over those 256 chains, so that the right
5747 rule will be there.
5748 </Para>
5750 <Para>
5751 Hashing makes this possible. Let's say you have 1024 cable modem customers in
5752 your network, with IP addresses ranging from 1.2.0.0 to 1.2.3.255, and each
5753 has to go in another bin, for example 'lite', 'regular' and 'premium'. You
5754 would then have 1024 rules like this:
5755 </Para>
5757 <Para>
5759 <Screen>
5760 # tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src \
5761   1.2.0.0 classid 1:1
5762 # tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src \
5763   1.2.0.1 classid 1:1
5765 # tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src \
5766   1.2.3.254 classid 1:3
5767 # tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src \
5768   1.2.3.255 classid 1:2
5769 </Screen>
5771 </Para>
5773 <Para>
5774 To speed this up, we can use the last part of the IP address as a 'hash
5775 key'. We then get 256 tables, the first of which looks like this:
5777 <Screen>
5778 # tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src \
5779   1.2.0.0 classid 1:1
5780 # tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src \
5781   1.2.1.0 classid 1:1
5782 # tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src \
5783   1.2.2.0 classid 1:3
5784 # tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src \
5785   1.2.3.0 classid 1:2
5786 </Screen>
5788 </Para>
5790 <Para>
5791 The next one starts like this:
5793 <Screen>
5794 # tc filter add dev eth1 parent 1:0 protocol ip prio 100 match ip src \
5795   1.2.0.1 classid 1:1
5797 </Screen>
5799 </Para>
5801 <Para>
5802 This way, only four checks are needed at most, two on average. 
5803 </Para>
5805 <Para>
5806 Configuration is pretty complicated, but very worth it by the time you have
5807 this many rules. First we make a filter root, then we create a table with
5808 256 entries:
5810 <Screen>
5811 # tc filter add dev eth1 parent 1:0 prio 5 protocol ip u32
5812 # tc filter add dev eth1 parent 1:0 prio 5 handle 2: protocol ip u32 divisor 256
5813 </Screen>
5815 </Para>
5817 <Para>
5818 Now we add some rules to entries in the created table:
5819 </Para>
5821 <Para>
5823 <Screen>
5824 # tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 ht 2:7b: \
5825         match ip src 1.2.0.123 flowid 1:1
5826 # tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 ht 2:7b: \
5827         match ip src 1.2.1.123 flowid 1:2
5828 # tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 ht 2:7b: \
5829         match ip src 1.2.3.123 flowid 1:3
5830 # tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 ht 2:7b: \
5831         match ip src 1.2.4.123 flowid 1:2
5832 </Screen>
5834 This is entry 123, which contains matches for 1.2.0.123, 1.2.1.123,
5835 1.2.2.123, 1.2.3.123, and sends them to 1:1, 1:2, 1:3 and 1:2 respectively.
5836 Note that we need to specify our hash bucket in hex, 0x7b is 123.
5837 </Para>
5839 <Para>
5840 Next create a 'hashing filter' that directs traffic to the right entry in
5841 the hashing table:
5843 <Screen>
5844 # tc filter add dev eth1 protocol ip parent 1:0 prio 5 u32 ht 800:: \
5845         match ip src 1.2.0.0/16 \
5846         hashkey mask 0x000000ff at 12 \
5847         link 2:
5848 </Screen>
5850 Ok, some numbers need explaining. The default hash table is called 800:: and
5851 all filtering starts there. Then we select the source address, which lives
5852 as position 12, 13, 14 and 15 in the IP header, and indicate that we are
5853 only interested in the last part. This we send to hash table 2:, which we
5854 created earlier.
5855 </Para>
5857 <Para>
5858 It is quite complicated, but it does work in practice and performance will
5859 be staggering. Note that this example could be improved to the ideal case
5860 where each chain contains 1 filter!
5861 </Para>
5863 </Sect1>
5865 </chapter>
5867 <chapter id="lartc.kernel">
5868 <Title>Kernel network parameters </Title>
5870 <Para>
5872 The kernel has lots of parameters which
5873 can be tuned for different circumstances. While, as usual, the default
5874 parameters serve 99&percnt; of installations very well, we don't call this the
5875 Advanced HOWTO for the fun of it!
5876 </Para>
5878 <Para>
5879 The interesting bits are in /proc/sys/net, take a look there. Not everything
5880 will be documented here initially, but we're working on it.
5881 </Para>
5883 <Para>
5884 In the meantime you may want to have a look at the Linux-Kernel sources;
5885 read the file Documentation/filesystems/proc.txt. Most of the
5886 features are explained there.
5887 </Para>
5889 <Para>
5890 (FIXME)
5891 </Para>
5893 <Sect1 id="lartc.kernel.rpf"
5894   xreflabel="Reverse Path Filtering">
5895   <Title>Reverse Path Filtering</Title>
5897 <Para>
5898 By default, routers route everything, even packets which 'obviously' don't
5899 belong on your network. A common example is private IP space escaping onto
5900 the Internet. If you have an interface with a route of 195.96.96.0/24 to it,
5901 you do not expect packets from 212.64.94.1 to arrive there.
5902 </Para>
5904 <Para>
5905 Lots of people will want to turn this feature off, so the kernel hackers
5906 have made it easy. There are files in /proc where you can tell
5907 the kernel to do this for you. The method is called "Reverse Path
5908 Filtering". Basically, if the reply to this packet wouldn't go out the
5909 interface this packet came in, then this is a bogus packet and should be
5910 ignored.
5911 </Para>
5913 <Para>
5914 The following fragment will turn this on for all current and future
5915 interfaces.
5916 </Para>
5918 <Para>
5920 <Screen>
5921 # for i in /proc/sys/net/ipv4/conf/*/rp_filter ; do
5922 &gt;  echo 2 &#62; $i 
5923 &gt; done
5924 </Screen>
5926 </Para>
5928 <Para>
5929 Going by the example above, if a packet arrived on the Linux router on eth1
5930 claiming to come from the Office+ISP subnet, it would be dropped. Similarly,
5931 if a packet came from the Office subnet, claiming to be from somewhere
5932 outside your firewall, it would be dropped also.
5933 </Para>
5935 <Para>
5936 The above is full reverse path filtering. The default is to only filter
5937 based on IPs that are on directly connected networks. This is because the
5938 full filtering breaks in the case of asymmetric routing (where packets come
5939 in one way and go out another, like satellite traffic, or if you have
5940 dynamic (bgp, ospf, rip) routes in your network. The data comes down
5941 through the satellite dish and replies go back through normal land-lines).
5942 </Para>
5944 <Para>
5945 If this exception applies to you (and you'll probably know if it does) you
5946 can simply turn off the rp_filter on the interface where the
5947 satellite data comes in. If you want to see if any packets are being
5948 dropped, the log_martians file in the same directory will tell
5949 the kernel to log them to your syslog.
5950 </Para>
5952 <Para>
5954 <Screen>
5955 # echo 1 &#62;/proc/sys/net/ipv4/conf/&#60;interfacename&#62;/log_martians
5956 </Screen>
5958 </Para>
5960 <Para>
5961 FIXME: is setting the conf/&lcub;default,all&rcub;/* files enough? - martijn
5962 </Para>
5964 </Sect1>
5966 <Sect1 id="lartc.kernel.obscure">
5967   <Title>Obscure settings</Title>
5969 <Para>
5970 Ok, there are a lot of parameters which can be modified. We try to list them
5971 all. Also documented (partly) in Documentation/ip-sysctl.txt.
5972 </Para>
5974 <Para>
5975 Some of these settings have different defaults based on whether you 
5976 answered 'Yes' to 'Configure as router and not host' while compiling your
5977 kernel.
5978 </Para>
5980 <para>
5981 Oskar Andreasson also has a page on all these flags and it appears to be
5982 better than ours, so also check 
5983 <ulink url="http://ipsysctl-tutorial.frozentux.net/">
5984 http://ipsysctl-tutorial.frozentux.net/</ulink>.
5985 </para>
5987 <Sect2>
5988 <Title>Generic ipv4</Title>
5990 <Para>
5991 As a generic note, most rate limiting features don't work on loopback, so
5992 don't test them locally. The limits are supplied in 'jiffies', and are
5993 enforced using the earlier mentioned token bucket filter.
5994 </Para>
5996 <Para>
5997 The kernel has an internal clock which runs at 'HZ' ticks (or 'jiffies') per
5998 second. On Intel, 'HZ' is mostly 100. So setting a *_rate file to, say 50,
5999 would allow for 2 packets per second. The token bucket filter is also
6000 configured to allow for a burst of at most 6 packets, if enough tokens have
6001 been earned.
6002 </Para>
6004 <Para>
6005 Several entries in the following list have been copied from
6006 /usr/src/linux/Documentation/networking/ip-sysctl.txt, written by Alexey
6007 Kuznetsov &lt;kuznet@ms2.inr.ac.ru&gt; and Andi Kleen &lt;ak@muc.de&gt;
6008 <VariableList>
6010 <VarListEntry>
6011 <Term>/proc/sys/net/ipv4/icmp_destunreach_rate</Term>
6012 <ListItem>
6013 <Para>
6014 If the kernel decides that it can't deliver a packet, it will drop it, and
6015 send the source of the packet an ICMP notice to this effect.
6016 </Para></ListItem>
6017 </VarListEntry>
6018 <VarListEntry>
6019 <Term>/proc/sys/net/ipv4/icmp_echo_ignore_all</Term>
6020 <ListItem>
6021 <Para>
6022 Don't act on echo packets at all. Please don't set this by default, but if
6023 you are used as a relay in a DoS attack, it may be useful.
6024 </Para></ListItem>
6025 </VarListEntry>
6026 <VarListEntry>
6027 <Term>/proc/sys/net/ipv4/icmp_echo_ignore_broadcasts &lsqb;Useful&rsqb;</Term>
6028 <ListItem>
6029 <Para>
6030 If you ping the broadcast address of a network, all hosts are supposed to
6031 respond. This makes for a dandy denial-of-service tool. Set this to 1 to
6032 ignore these broadcast messages.
6033 </Para></ListItem>
6034 </VarListEntry>
6035 <VarListEntry>
6036 <Term>/proc/sys/net/ipv4/icmp_echoreply_rate</Term>
6037 <ListItem>
6038 <Para>
6039 The rate at which echo replies are sent to any one destination.
6040 </Para></ListItem>
6041 </VarListEntry>
6042 <VarListEntry>
6043 <Term>/proc/sys/net/ipv4/icmp_ignore_bogus_error_responses</Term>
6044 <ListItem>
6045 <Para>
6046 Set this to ignore ICMP errors caused by hosts in the network reacting badly
6047 to frames sent to what they perceive to be the broadcast address.
6048 </Para></ListItem>
6049 </VarListEntry>
6050 <VarListEntry>
6051 <Term>/proc/sys/net/ipv4/icmp_paramprob_rate</Term>
6052 <ListItem>
6053 <Para>
6054 A relatively unknown ICMP message, which is sent in response to incorrect
6055 packets with broken IP or TCP headers. With this file you can control the
6056 rate at which it is sent.
6057 </Para></ListItem>
6058 </VarListEntry>
6059 <VarListEntry>
6060 <Term>/proc/sys/net/ipv4/icmp_timeexceed_rate</Term>
6061 <ListItem>
6062 <Para>
6063 This the famous cause of the 'Solaris middle star' in traceroutes. Limits
6064 number of ICMP Time Exceeded messages sent. 
6065 </Para></ListItem>
6066 </VarListEntry>
6067 <VarListEntry>
6068 <Term>/proc/sys/net/ipv4/igmp_max_memberships</Term>
6069 <ListItem>
6070 <Para>
6071 Maximum number of listening igmp (multicast) sockets on the host.
6072 FIXME: Is this true?
6073 </Para></ListItem>
6074 </VarListEntry>
6075 <VarListEntry>
6076 <Term>/proc/sys/net/ipv4/inet_peer_gc_maxtime</Term>
6077 <ListItem>
6078 <Para>
6079 FIXME: Add a little explanation about the inet peer storage?
6081 Minimum interval between garbage collection passes.  This interval is in
6082 effect under low (or absent) memory pressure on the pool. Measured in
6083 jiffies.
6084 </Para></ListItem>
6085 </VarListEntry>
6086 <VarListEntry>
6087 <Term>/proc/sys/net/ipv4/inet_peer_gc_mintime</Term>
6088 <ListItem>
6089 <Para>
6090 Minimum interval between garbage collection passes.  This interval is in
6091 effect under high memory pressure on the pool. Measured in jiffies.
6092 </Para></ListItem>
6093 </VarListEntry>
6094 <VarListEntry>
6095 <Term>/proc/sys/net/ipv4/inet_peer_maxttl</Term>
6096 <ListItem>
6097 <Para>
6098 Maximum time-to-live of entries.  Unused entries will expire after this
6099 period of time if there is no memory pressure on the pool (i.e. when the
6100 number of entries in the pool is very small). Measured in jiffies.
6101 </Para></ListItem>
6102 </VarListEntry>
6103 <VarListEntry>
6104 <Term>/proc/sys/net/ipv4/inet_peer_minttl</Term>
6105 <ListItem>
6106 <Para>
6107 Minimum time-to-live of entries.  Should be enough to cover fragment
6108 time-to-live on the reassembling side.  This minimum time-to-live
6109 is guaranteed if the pool size is less than inet_peer_threshold.
6110 Measured in jiffies.
6111 </Para></ListItem>
6112 </VarListEntry>
6113 <VarListEntry>
6114 <Term>/proc/sys/net/ipv4/inet_peer_threshold</Term>
6115 <ListItem>
6116 <Para>
6117 The approximate size of the INET peer storage.  Starting from this threshold
6118 entries will be thrown aggressively.  This threshold also determines
6119 entries' time-to-live and time intervals between garbage collection passes. 
6120 More entries, less time-to-live, less GC interval.
6121 </Para></ListItem>
6122 </VarListEntry>
6123 <VarListEntry>
6124 <Term>/proc/sys/net/ipv4/ip_autoconfig</Term>
6125 <ListItem>
6126 <Para>
6127 This file contains the number one if the host received its IP configuration by
6128 RARP, BOOTP, DHCP or a similar mechanism. Otherwise it is zero.
6129 </Para></ListItem>
6130 </VarListEntry>
6131 <VarListEntry>
6132 <Term>/proc/sys/net/ipv4/ip_default_ttl</Term>
6133 <ListItem>
6134 <Para>
6135 Time To Live of packets. Set to a safe 64. Raise it if you have a huge
6136 network. Don't do so for fun - routing loops cause much more damage that
6137 way. You might even consider lowering it in some circumstances.
6138 </Para></ListItem>
6139 </VarListEntry>
6140 <VarListEntry>
6141 <Term>/proc/sys/net/ipv4/ip_dynaddr</Term>
6142 <ListItem>
6143 <Para>
6144 You need to set this if you use dial-on-demand with a dynamic interface
6145 address. Once your demand interface comes up, any local TCP sockets which haven't seen replies will be rebound to have the right address. This solves the problem that the
6146 connection that brings up your interface itself does not work, but the
6147 second try does.
6148 </Para></ListItem>
6149 </VarListEntry>
6150 <VarListEntry>
6151 <Term>/proc/sys/net/ipv4/ip_forward</Term>
6152 <ListItem>
6153 <Para>
6154 If the kernel should attempt to forward packets. Off by default.
6155 </Para></ListItem>
6156 </VarListEntry>
6157 <VarListEntry>
6158 <Term>/proc/sys/net/ipv4/ip_local_port_range</Term>
6159 <ListItem>
6160 <Para>
6161 Range of local ports for outgoing connections. Actually quite small by
6162 default, 1024 to 4999.
6163 </Para></ListItem>
6164 </VarListEntry>
6165 <VarListEntry>
6166 <Term>/proc/sys/net/ipv4/ip_no_pmtu_disc</Term>
6167 <ListItem>
6168 <Para>
6169 Set this if you want to disable Path MTU discovery - a technique to
6170 determine the largest Maximum Transfer Unit possible on your path. See also
6171 the section on Path MTU discovery in the 
6172 <citetitle><xref linkend="lartc.cookbook"></citetitle> chapter.
6173 </Para></ListItem>
6174 </VarListEntry>
6175 <VarListEntry>
6176 <Term>/proc/sys/net/ipv4/ipfrag_high_thresh</Term>
6177 <ListItem>
6178 <Para>
6179 Maximum memory used to reassemble IP fragments. When 
6180 ipfrag_high_thresh bytes of memory is allocated for this purpose,
6181 the fragment handler will toss packets until ipfrag_low_thresh
6182 is reached.
6183 </Para></ListItem>
6184 </VarListEntry>
6185 <VarListEntry>
6186 <Term>/proc/sys/net/ipv4/ip_nonlocal_bind</Term>
6187 <ListItem>
6188 <Para>
6189 Set this if you want your applications to be able to bind to an address
6190 which doesn't belong to a device on your system. This can be useful when
6191 your machine is on a non-permanent (or even dynamic) link, so your services
6192 are able to start up and bind to a specific address when your link is down.
6193 </Para></ListItem>
6194 </VarListEntry>
6195 <VarListEntry>
6196 <Term>/proc/sys/net/ipv4/ipfrag_low_thresh</Term>
6197 <ListItem>
6198 <Para>
6199 Minimum memory used to reassemble IP fragments.
6200 </Para></ListItem>
6201 </VarListEntry>
6202 <VarListEntry>
6203 <Term>/proc/sys/net/ipv4/ipfrag_time</Term>
6204 <ListItem>
6205 <Para>
6206 Time in seconds to keep an IP fragment in memory.
6207 </Para></ListItem>
6208 </VarListEntry>
6209 <VarListEntry>
6210 <Term>/proc/sys/net/ipv4/tcp_abort_on_overflow</Term>
6211 <ListItem>
6212 <Para>
6213 A boolean flag controlling the behaviour under lots of incoming connections.
6214 When enabled, this causes the kernel to actively send RST packets when a
6215 service is overloaded.
6216 </Para></ListItem>
6217 </VarListEntry>
6218 <VarListEntry>
6219 <Term>/proc/sys/net/ipv4/tcp_fin_timeout</Term>
6220 <ListItem>
6221 <Para>
6222 Time to hold socket in state FIN-WAIT-2, if it was closed by our side. Peer
6223 can be broken and never close its side, or even died unexpectedly. Default
6224 value is 60sec. Usual value used in 2.2 was 180 seconds, you may restore it,
6225 but remember that if your machine is even underloaded WEB server, you risk
6226 to overflow memory with kilotons of dead sockets, FIN-WAIT-2 sockets are
6227 less dangerous than FIN-WAIT-1, because they eat maximum 1.5K of memory, but
6228 they tend to live longer. Cf. tcp_max_orphans.
6229 </Para></ListItem>
6230 </VarListEntry>
6231 <VarListEntry>
6232 <Term>/proc/sys/net/ipv4/tcp_keepalive_time</Term>
6233 <ListItem>
6234 <Para>
6235 How often TCP sends out keepalive messages when keepalive is enabled. 
6237 Default: 2hours.
6238 </Para></ListItem>
6239 </VarListEntry>
6240 <VarListEntry>
6241 <Term>/proc/sys/net/ipv4/tcp_keepalive_intvl</Term>
6242 <ListItem>
6243 <Para>
6244 How frequent probes are retransmitted, when a probe isn't acknowledged. 
6246 Default: 75 seconds.
6247 </Para></ListItem>
6248 </VarListEntry>
6249 <VarListEntry>
6250 <Term>/proc/sys/net/ipv4/tcp_keepalive_probes</Term>
6251 <ListItem>
6252 <Para>
6253 How many keepalive probes TCP will send, until it decides that the
6254 connection is broken. 
6256 Default value: 9. 
6258 Multiplied with tcp_keepalive_intvl, this gives the time a link can be
6259 non-responsive after a keepalive has been sent.
6260 </Para></ListItem>
6261 </VarListEntry>
6262 <VarListEntry>
6263 <Term>/proc/sys/net/ipv4/tcp_max_orphans</Term>
6264 <ListItem>
6265 <Para>
6266 Maximal number of TCP sockets not attached to any user file handle, held by
6267 system. If this number is exceeded orphaned connections are reset
6268 immediately and warning is printed. This limit exists only to prevent simple
6269 DoS attacks, you _must_ not rely on this or lower the limit artificially,
6270 but rather increase it (probably, after increasing installed memory), if
6271 network conditions require more than default value, and tune network
6272 services to linger and kill such states more aggressively. Let me remind you
6273 again: each orphan eats up to &nbsp;64K of unswappable memory.
6274 </Para></ListItem>
6275 </VarListEntry>
6276 <VarListEntry>
6277 <Term>/proc/sys/net/ipv4/tcp_orphan_retries</Term>
6278 <ListItem>
6279 <Para>
6280 How may times to retry before killing TCP connection, closed by our side.
6281 Default value 7 corresponds to &nbsp;50sec-16min depending on RTO. If your machine
6282 is a loaded WEB server, you should think about lowering this value, such
6283 sockets may consume significant resources. Cf. tcp_max_orphans.
6284 </Para></ListItem>
6285 </VarListEntry>
6286 <VarListEntry>
6287 <Term>/proc/sys/net/ipv4/tcp_max_syn_backlog</Term>
6288 <ListItem>
6289 <Para>
6290 Maximal number of remembered connection requests, which still did not
6291 receive an acknowledgment from connecting client. Default value is 1024 for
6292 systems with more than 128Mb of memory, and 128 for low memory machines. If
6293 server suffers of overload, try to increase this number. Warning! If you
6294 make it greater than 1024, it would be better to change TCP_SYNQ_HSIZE in
6295 include/net/tcp.h to keep TCP_SYNQ_HSIZE*16&#60;=tcp_max_syn_backlog and to
6296 recompile kernel.
6297 </Para></ListItem>
6298 </VarListEntry>
6299 <VarListEntry>
6300 <Term>/proc/sys/net/ipv4/tcp_max_tw_buckets</Term>
6301 <ListItem>
6302 <Para>
6303 Maximal number of timewait sockets held by system simultaneously. If this
6304 number is exceeded time-wait socket is immediately destroyed and warning is
6305 printed. This limit exists only to prevent simple DoS attacks, you _must_
6306 not lower the limit artificially, but rather increase it (probably, after
6307 increasing installed memory), if network conditions require more than
6308 default value.
6309 </Para></ListItem>
6310 </VarListEntry>
6311 <VarListEntry>
6312 <Term>/proc/sys/net/ipv4/tcp_retrans_collapse</Term>
6313 <ListItem>
6314 <Para>
6315 Bug-to-bug compatibility with some broken printers.
6316 On retransmit try to send bigger packets to work around bugs in
6317 certain TCP stacks.
6318 </Para></ListItem>
6319 </VarListEntry>
6320 <VarListEntry>
6321 <Term>/proc/sys/net/ipv4/tcp_retries1</Term>
6322 <ListItem>
6323 <Para>
6324 How many times to retry before deciding that something is wrong
6325 and it is necessary to report this suspicion to network layer.
6326 Minimal RFC value is 3, it is default, which corresponds
6327 to &nbsp;3sec-8min depending on RTO.
6328 </Para></ListItem>
6329 </VarListEntry>
6330 <VarListEntry>
6331 <Term>/proc/sys/net/ipv4/tcp_retries2</Term>
6332 <ListItem>
6333 <Para>
6334 How may times to retry before killing alive TCP connection.
6335 <ULink
6336 URL="http://www.ietf.org/rfc/rfc1122.txt"
6337 >RFC 1122</ULink
6339 says that the limit should be longer than 100 sec.
6340 It is too small number. Default value 15 corresponds to &nbsp;13-30min
6341 depending on RTO.
6342 </Para></ListItem>
6343 </VarListEntry>
6344 <VarListEntry>
6345 <Term>/proc/sys/net/ipv4/tcp_rfc1337</Term>
6346 <ListItem>
6347 <Para>
6348 This boolean enables a fix for 'time-wait assassination hazards in tcp', described
6349 in RFC 1337. If enabled, this causes the kernel to drop RST packets for
6350 sockets in the time-wait state.
6352 Default: 0
6353 </Para></ListItem>
6354 </VarListEntry>
6355 <VarListEntry>
6356 <Term>/proc/sys/net/ipv4/tcp_sack</Term>
6357 <ListItem>
6358 <Para>
6359 Use Selective ACK which can be used to signify that specific packets are
6360 missing - therefore helping fast recovery.
6361 </Para></ListItem>
6362 </VarListEntry>
6363 <VarListEntry>
6364 <Term>/proc/sys/net/ipv4/tcp_stdurg</Term>
6365 <ListItem>
6366 <Para>
6367 Use the Host requirements interpretation of the TCP urg pointer
6368 field. 
6370 Most hosts use the older BSD interpretation, so if you turn this on
6371 Linux might not communicate correctly with them. 
6373 Default: FALSE 
6374 </Para></ListItem>
6375 </VarListEntry>
6376 <VarListEntry>
6377 <Term>/proc/sys/net/ipv4/tcp_syn_retries</Term>
6378 <ListItem>
6379 <Para>
6380 Number of SYN packets the kernel will send before giving up on the new
6381 connection.
6382 </Para></ListItem>
6383 </VarListEntry>
6384 <VarListEntry>
6385 <Term>/proc/sys/net/ipv4/tcp_synack_retries</Term>
6386 <ListItem>
6387 <Para>
6388 To open the other side of the connection, the kernel sends a SYN with a
6389 piggybacked ACK on it, to acknowledge the earlier received SYN. This is part
6390 2 of the threeway handshake. This setting determines the number of SYN+ACK
6391 packets sent before the kernel gives up on the connection.
6392 </Para></ListItem>
6393 </VarListEntry>
6394 <VarListEntry>
6395 <Term>/proc/sys/net/ipv4/tcp_timestamps</Term>
6396 <ListItem>
6397 <Para>
6398 Timestamps are used, amongst other things, to protect against wrapping
6399 sequence numbers. A 1 gigabit link might conceivably re-encounter a previous
6400 sequence number with an out-of-line value, because it was of a previous
6401 generation. The timestamp will let it recognize this 'ancient packet'.
6402 </Para></ListItem>
6403 </VarListEntry>
6404 <VarListEntry>
6405 <Term>/proc/sys/net/ipv4/tcp_tw_recycle</Term>
6406 <ListItem>
6407 <Para>
6408 Enable fast recycling TIME-WAIT sockets. Default value is 1.
6409 It should not be changed without advice/request of technical experts.
6410 </Para></ListItem>
6411 </VarListEntry>
6412 <VarListEntry>
6413 <Term>/proc/sys/net/ipv4/tcp_window_scaling</Term>
6414 <ListItem>
6415 <Para>
6416 TCP/IP normally allows windows up to 65535 bytes big. For really fast
6417 networks, this may not be enough. The window scaling options allows for
6418 almost gigabyte windows, which is good for high bandwidth*delay products.
6419 </Para></ListItem>
6420 </VarListEntry>
6421 </VariableList>
6422 </Para>
6424 </Sect2>
6426 <Sect2>
6427 <Title>Per device settings</Title>
6429 <Para>
6430 DEV can either stand for a real interface, or for 'all' or 'default'.
6431 Default also changes settings for interfaces yet to be created.
6432 <VariableList>
6434 <VarListEntry>
6435 <Term>/proc/sys/net/ipv4/conf/DEV/accept_redirects</Term>
6436 <ListItem>
6437 <Para>
6438 If a router decides that you are using it for a wrong purpose (ie, it needs
6439 to resend your packet on the same interface), it will send us a ICMP
6440 Redirect. This is a slight security risk however, so you may want to turn it
6441 off, or use secure redirects.
6442 </Para></ListItem>
6443 </VarListEntry>
6444 <VarListEntry>
6445 <Term>/proc/sys/net/ipv4/conf/DEV/accept_source_route</Term>
6446 <ListItem>
6447 <Para>
6448 Not used very much anymore. You used to be able to give a packet a list of
6449 IP addresses it should visit on its way. Linux can be made to honor this IP
6450 option.
6451 </Para></ListItem>
6452 </VarListEntry>
6453 <VarListEntry>
6454 <Term>/proc/sys/net/ipv4/conf/DEV/bootp_relay</Term>
6455 <ListItem>
6456 <Para>
6457 Accept packets  with source address 0.b.c.d with destinations not to this host
6458 as local ones. It is supposed that a BOOTP relay daemon will catch and forward
6459 such packets.
6460 </Para>
6462 <Para>
6463 The default  is  0,  since this feature is not implemented yet (kernel version
6464 2.2.12).
6465 </Para></ListItem>
6466 </VarListEntry>
6467 <VarListEntry>
6468 <Term>/proc/sys/net/ipv4/conf/DEV/forwarding</Term>
6469 <ListItem>
6470 <Para>
6471 Enable or disable IP forwarding on this interface.
6472 </Para></ListItem>
6473 </VarListEntry>
6474 <VarListEntry>
6475 <Term>/proc/sys/net/ipv4/conf/DEV/log_martians</Term>
6476 <ListItem>
6477 <Para>
6478 See the section on 
6479 <citetitle><xref linkend="lartc.kernel.rpf"></citetitle>.
6480 </Para></ListItem>
6481 </VarListEntry>
6482 <VarListEntry>
6483 <Term>/proc/sys/net/ipv4/conf/DEV/mc_forwarding</Term>
6484 <ListItem>
6485 <Para>
6486 If we do multicast forwarding on this interface
6487 </Para></ListItem>
6488 </VarListEntry>
6489 <VarListEntry>
6490 <Term>/proc/sys/net/ipv4/conf/DEV/proxy_arp</Term>
6491 <ListItem>
6492 <Para>
6493 If you set this to 1, this interface will respond to ARP requests for
6494 addresses the kernel has routes to. Can be very useful when building 'ip
6495 pseudo bridges'. Do take care that your netmasks are very correct before
6496 enabling this! Also be aware that the rp_filter, mentioned elsewhere, also
6497 operates on ARP queries!
6498 </Para></ListItem>
6499 </VarListEntry>
6500 <VarListEntry>
6501 <Term>/proc/sys/net/ipv4/conf/DEV/rp_filter</Term>
6502 <ListItem>
6503 <Para>
6504 See the section on 
6505 <citetitle><xref linkend="lartc.kernel.rpf"></citetitle>.
6506 </Para></ListItem>
6507 </VarListEntry>
6508 <VarListEntry>
6509 <Term>/proc/sys/net/ipv4/conf/DEV/secure_redirects</Term>
6510 <ListItem>
6511 <Para>
6512 Accept ICMP  redirect  messages  only  for gateways, listed in default gateway
6513 list. Enabled by default.
6514 </Para></ListItem>
6515 </VarListEntry>
6516 <VarListEntry>
6517 <Term>/proc/sys/net/ipv4/conf/DEV/send_redirects</Term>
6518 <ListItem>
6519 <Para>
6520 If we send the above mentioned redirects.
6521 </Para></ListItem>
6522 </VarListEntry>
6523 <VarListEntry>
6524 <Term>/proc/sys/net/ipv4/conf/DEV/shared_media</Term>
6525 <ListItem>
6526 <Para>
6527 If it  is  not  set  the kernel does not assume that different subnets on this
6528 device can communicate directly. Default setting is 'yes'.
6529 </Para></ListItem>
6530 </VarListEntry>
6531 <VarListEntry>
6532 <Term>/proc/sys/net/ipv4/conf/DEV/tag</Term>
6533 <ListItem>
6534 <Para>
6535 FIXME: fill this in
6536 </Para></ListItem>
6537 </VarListEntry>
6538 </VariableList>
6539 </Para>
6541 </Sect2>
6543 <Sect2>
6544 <Title>Neighbor policy</Title>
6546 <Para>
6547 Dev can either stand for a real interface, or for 'all' or 'default'.
6548 Default also changes settings for interfaces yet to be created.
6549 <VariableList>
6551 <VarListEntry>
6552 <Term>/proc/sys/net/ipv4/neigh/DEV/anycast_delay</Term>
6553 <ListItem>
6554 <Para>
6555 Maximum for  random  delay  of  answers  to  neighbor solicitation messages in
6556 jiffies (1/100  sec). Not yet implemented (Linux does not have anycast support
6557 yet).
6558 </Para></ListItem>
6559 </VarListEntry>
6560 <VarListEntry>
6561 <Term>/proc/sys/net/ipv4/neigh/DEV/app_solicit</Term>
6562 <ListItem>
6563 <Para>
6564 Determines the  number of requests to send to the user level ARP daemon. Use 0
6565 to turn off.
6566 </Para></ListItem>
6567 </VarListEntry>
6568 <VarListEntry>
6569 <Term>/proc/sys/net/ipv4/neigh/DEV/base_reachable_time</Term>
6570 <ListItem>
6571 <Para>
6572 A base  value  used for computing the random reachable time value as specified
6573 in RFC2461.
6574 </Para></ListItem>
6575 </VarListEntry>
6576 <VarListEntry>
6577 <Term>/proc/sys/net/ipv4/neigh/DEV/delay_first_probe_time</Term>
6578 <ListItem>
6579 <Para>
6580 Delay for  the  first  time  probe  if  the  neighbor  is  reachable.  (see
6581 gc_stale_time)
6582 </Para></ListItem>
6583 </VarListEntry>
6584 <VarListEntry>
6585 <Term>/proc/sys/net/ipv4/neigh/DEV/gc_stale_time</Term>
6586 <ListItem>
6587 <Para>
6588 Determines how  often  to  check  for stale ARP entries. After an ARP entry is
6589 stale it  will  be resolved again (which is useful when an IP address migrates
6590 to another  machine).  When  ucast_solicit is greater than 0 it first tries to
6591 send an  ARP  packet  directly  to  the  known  host  When  that  fails  and
6592 mcast_solicit is greater than 0, an ARP request is broadcast.
6593 </Para></ListItem>
6594 </VarListEntry>
6595 <VarListEntry>
6596 <Term>/proc/sys/net/ipv4/neigh/DEV/locktime</Term>
6597 <ListItem>
6598 <Para>
6599 An ARP/neighbor  entry  is only replaced with a new one if the old is at least
6600 locktime old. This prevents ARP cache thrashing.
6601 </Para></ListItem>
6602 </VarListEntry>
6603 <VarListEntry>
6604 <Term>/proc/sys/net/ipv4/neigh/DEV/mcast_solicit</Term>
6605 <ListItem>
6606 <Para>
6607 Maximum number of retries for multicast solicitation.
6608 </Para></ListItem>
6609 </VarListEntry>
6610 <VarListEntry>
6611 <Term>/proc/sys/net/ipv4/neigh/DEV/proxy_delay</Term>
6612 <ListItem>
6613 <Para>
6614 Maximum time  (real  time is random &lsqb;0..proxytime]) before answering to an ARP
6615 request for  which  we have an proxy ARP entry. In some cases, this is used to
6616 prevent network flooding.
6617 </Para></ListItem>
6618 </VarListEntry>
6619 <VarListEntry>
6620 <Term>/proc/sys/net/ipv4/neigh/DEV/proxy_qlen</Term>
6621 <ListItem>
6622 <Para>
6623 Maximum queue length of the delayed proxy arp timer. (see proxy_delay).
6624 </Para></ListItem>
6625 </VarListEntry>
6626 <VarListEntry>
6627 <Term>/proc/sys/net/ipv4/neigh/DEV/retrans_time</Term>
6628 <ListItem>
6629 <Para>
6630 The time,  expressed  in  jiffies  (1/100 sec), between retransmitted Neighbor
6631 Solicitation messages.  Used  for  address  resolution  and  to determine if a
6632 neighbor is unreachable.
6633 </Para></ListItem>
6634 </VarListEntry>
6635 <VarListEntry>
6636 <Term>/proc/sys/net/ipv4/neigh/DEV/ucast_solicit</Term>
6637 <ListItem>
6638 <Para>
6639 Maximum number of retries for unicast solicitation.
6640 </Para></ListItem>
6641 </VarListEntry>
6642 <VarListEntry>
6643 <Term>/proc/sys/net/ipv4/neigh/DEV/unres_qlen</Term>
6644 <ListItem>
6645 <Para>
6646 Maximum queue  length  for a pending arp request - the number of packets which
6647 are accepted from other layers while the ARP address is still resolved.
6648 </Para></ListItem>
6649 </VarListEntry>
6650 <VarListEntry>
6651 <Term>Internet QoS: Architectures and Mechanisms for Quality of Service,
6652 Zheng Wang, ISBN 1-55860-608-4</Term>
6653 <ListItem>
6654 <Para>
6655 Hardcover textbook covering topics
6656 related to Quality of Service. Good for understanding basic concepts.
6657 </Para></ListItem>
6658 </VarListEntry>
6659 </VariableList>
6660 </Para>
6662 </Sect2>
6664 <Sect2>
6665 <Title>Routing settings</Title>
6667 <Para>
6668 <VariableList>
6670 <VarListEntry>
6671 <Term>/proc/sys/net/ipv4/route/error_burst</Term>
6672 <ListItem>
6673 <Para>
6674 These parameters  are used to limit the warning messages written to the kernel
6675 log from  the  routing  code.  The  higher the error_cost factor is, the fewer
6676 messages will  be written. Error_burst controls when messages will be dropped.
6677 The default settings limit warning messages to one every five seconds.
6678 </Para></ListItem>
6679 </VarListEntry>
6680 <VarListEntry>
6681 <Term>/proc/sys/net/ipv4/route/error_cost</Term>
6682 <ListItem>
6683 <Para>
6684 These parameters  are used to limit the warning messages written to the kernel
6685 log from  the  routing  code.  The  higher the error_cost factor is, the fewer
6686 messages will  be written. Error_burst controls when messages will be dropped.
6687 The default settings limit warning messages to one every five seconds.
6688 </Para></ListItem>
6689 </VarListEntry>
6690 <VarListEntry>
6691 <Term>/proc/sys/net/ipv4/route/flush</Term>
6692 <ListItem>
6693 <Para>
6694 Writing to this file results in a flush of the routing cache.
6695 </Para></ListItem>
6696 </VarListEntry>
6697 <VarListEntry>
6698 <Term>/proc/sys/net/ipv4/route/gc_elasticity</Term>
6699 <ListItem>
6700 <Para>
6701 Values to  control  the  frequency  and  behavior  of  the  garbage collection
6702 algorithm for the routing cache. This can be important for when doing
6703 fail over. At least gc_timeout seconds will elapse before Linux will skip
6704 to another route because the previous one has died. By default set to 300,
6705 you may want to lower it if you want to have a speedy fail over.
6706 </Para>
6708 <Para>
6709 Also see <ULink
6710 URL="http://mailman.ds9a.nl/pipermail/lartc/2002q1/002667.html"
6711 >this post</ULink
6712 > by Ard van Breemen.
6713 </Para></ListItem>
6714 </VarListEntry>
6715 <VarListEntry>
6716 <Term>/proc/sys/net/ipv4/route/gc_interval</Term>
6717 <ListItem>
6718 <Para>
6719 See /proc/sys/net/ipv4/route/gc_elasticity.
6720 </Para></ListItem>
6721 </VarListEntry>
6722 <VarListEntry>
6723 <Term>/proc/sys/net/ipv4/route/gc_min_interval</Term>
6724 <ListItem>
6725 <Para>
6726 See /proc/sys/net/ipv4/route/gc_elasticity.
6727 </Para></ListItem>
6728 </VarListEntry>
6729 <VarListEntry>
6730 <Term>/proc/sys/net/ipv4/route/gc_thresh</Term>
6731 <ListItem>
6732 <Para>
6733 See /proc/sys/net/ipv4/route/gc_elasticity.
6734 </Para></ListItem>
6735 </VarListEntry>
6736 <VarListEntry>
6737 <Term>/proc/sys/net/ipv4/route/gc_timeout</Term>
6738 <ListItem>
6739 <Para>
6740 See /proc/sys/net/ipv4/route/gc_elasticity.
6741 </Para></ListItem>
6742 </VarListEntry>
6743 <VarListEntry>
6744 <Term>/proc/sys/net/ipv4/route/max_delay</Term>
6745 <ListItem>
6746 <Para>
6747 Delays for flushing the routing cache.
6748 </Para></ListItem>
6749 </VarListEntry>
6750 <VarListEntry>
6751 <Term>/proc/sys/net/ipv4/route/max_size</Term>
6752 <ListItem>
6753 <Para>
6754 Maximum size  of  the routing cache. Old entries will be purged once the cache
6755 reached has this size.
6756 </Para></ListItem>
6757 </VarListEntry>
6758 <VarListEntry>
6759 <Term>/proc/sys/net/ipv4/route/min_adv_mss</Term>
6760 <ListItem>
6761 <Para>
6762 FIXME: fill this in
6763 </Para></ListItem>
6764 </VarListEntry>
6765 <VarListEntry>
6766 <Term>/proc/sys/net/ipv4/route/min_delay</Term>
6767 <ListItem>
6768 <Para>
6769 Delays for flushing the routing cache.
6770 </Para></ListItem>
6771 </VarListEntry>
6772 <VarListEntry>
6773 <Term>/proc/sys/net/ipv4/route/min_pmtu</Term>
6774 <ListItem>
6775 <Para>
6776 FIXME: fill this in
6777 </Para></ListItem>
6778 </VarListEntry>
6779 <VarListEntry>
6780 <Term>/proc/sys/net/ipv4/route/mtu_expires</Term>
6781 <ListItem>
6782 <Para>
6783 FIXME: fill this in
6784 </Para></ListItem>
6785 </VarListEntry>
6786 <VarListEntry>
6787 <Term>/proc/sys/net/ipv4/route/redirect_load</Term>
6788 <ListItem>
6789 <Para>
6790 Factors which  determine  if  more ICMP redirects should be sent to a specific
6791 host. No  redirects  will be sent once the load limit or the maximum number of
6792 redirects has been reached.
6793 </Para></ListItem>
6794 </VarListEntry>
6795 <VarListEntry>
6796 <Term>/proc/sys/net/ipv4/route/redirect_number</Term>
6797 <ListItem>
6798 <Para>
6799 See /proc/sys/net/ipv4/route/redirect_load.
6800 </Para></ListItem>
6801 </VarListEntry>
6802 <VarListEntry>
6803 <Term>/proc/sys/net/ipv4/route/redirect_silence</Term>
6804 <ListItem>
6805 <Para>
6806 Timeout for redirects. After this period redirects will be sent again, even if
6807 this has been stopped, because the load or number limit has been reached.
6808 </Para></ListItem>
6809 </VarListEntry>
6810 </VariableList>
6811 </Para>
6813 </Sect2>
6815 </Sect1>
6817 </chapter>
6819 <chapter id="lartc.adv-qdisc">
6820   <Title>Advanced &amp; less common queueing disciplines</Title>
6822 <Para>
6823 Should you find that you have needs not addressed by the queues mentioned
6824 earlier, the kernel contains some other more specialized queues mentioned here.
6825 </Para>
6827 <Sect1 id="lartc.adv-qdisc.bfifo-pfifo">
6828   <Title><literal>bfifo</literal>/<literal>pfifo</literal></Title>
6830 <Para>
6831 These classless queues are even simpler than pfifo_fast in that they lack
6832 the internal bands - all traffic is really equal. They have one important
6833 benefit though, they have some statistics. So even if you don't need shaping
6834 or prioritizing, you can use this qdisc to determine the backlog on your
6835 interface.
6836 </Para>
6838 <Para>
6839 pfifo has a length measured in packets, bfifo in bytes. 
6840 </Para>
6842 <Sect2>
6843 <Title>Parameters &amp; usage</Title>
6845 <Para>
6846 <VariableList>
6848 <VarListEntry>
6849 <Term>limit</Term>
6850 <ListItem>
6851 <Para>
6852 Specifies the length of the queue. Measured in bytes for bfifo, in packets
6853 for pfifo. Defaults to the interface txqueuelen (see pfifo_fast chapter)
6854 packets long or txqueuelen*mtu bytes for bfifo.
6855 </Para></ListItem>
6856 </VarListEntry>
6857 </VariableList>
6858 </Para>
6860 </Sect2>
6862 </Sect1>
6864 <Sect1 id="lartc.adv-qdisc.csz">
6865   <Title>Clark-Shenker-Zhang algorithm (CSZ)</Title>
6867 <Para>
6868 This is so theoretical that not even Alexey (the main CBQ author) claims to
6869 understand it. From his source:
6870 </Para>
6872 <blockquote>
6873 <Para>
6874 David D. Clark, Scott Shenker and Lixia Zhang
6875 <citetitle>Supporting Real-Time Applications in an Integrated Services Packet
6876 Network: Architecture and Mechanism</citetitle>.
6877 </Para>
6879 <Para>
6880 As I understand it, the main idea is to create WFQ flows for each guaranteed
6881 service and to allocate the rest of bandwith to dummy flow-0. Flow-0
6882 comprises the predictive services and the best effort traffic; it is handled
6883 by a priority scheduler with the highest priority band allocated for
6884 predictive services, and the rest --- to the best effort packets.
6885 </Para>
6887 <Para>
6888 Note that in CSZ flows are NOT limited to their bandwidth.  It is supposed
6889 that the flow passed admission control at the edge of the QoS network and it
6890 doesn't need further shaping. Any attempt to improve the flow or to shape it
6891 to a token bucket at intermediate hops will introduce undesired delays and
6892 raise jitter.
6893 </Para>
6895 <Para>
6896 At the moment CSZ is the only scheduler that provides true guaranteed
6897 service. Another schemes (including CBQ) do not provide guaranteed delay and
6898 randomize jitter."
6899 </Para>
6901 <Para>
6902 Does not currently seem like a good candidate to use, unless you've read and
6903 understand the article mentioned.
6904 </Para>
6905 </blockquote>
6907 </Sect1>
6909 <Sect1 id="lartc.adv-qdisc.dsmark"
6910   xreflabel="DSMARK">
6911   <Title>DSMARK</Title>
6913   <abstract>
6914     <Para>
6915       <author><firstname>Esteve</firstname><surname>Camps</surname></author>
6916       <address><email>marvin@grn.es</email></address>
6917       This text is an extract from my thesis on
6918       <citetitle>QoS Support in Linux</citetitle>, September 2000.
6919     </Para>
6920   </abstract>
6922 <Para>Source documents:
6923 </para>
6925 <ItemizedList>
6926 <ListItem>
6927   <Para>
6928     <ULink URL="ftp://icaftp.epfl.ch/pub/linux/diffserv/misc/dsid-01.txt.gz">
6929       Draft-almesberger-wajhak-diffserv-linux-01.txt</ULink>.
6930   </Para>
6931 </ListItem>
6932 <ListItem>
6933   <Para>Examples in iproute2 distribution.
6934   </Para>
6935 </ListItem>
6936 <ListItem>
6937   <Para>
6938     <ULink URL="http://www.qosforum.com/white-papers/qosprot_v3.pdf">
6939       White Paper-QoS protocols and architectures</ULink> and
6940     <ULink URL="http://www.qosforum.com/docs/faq">
6941       IP QoS Frequently Asked Questions</ULink> both by 
6942     <citetitle>Quality of Service Forum</citetitle>.
6943   </Para>
6944 </ListItem>
6945 </ItemizedList>
6947 <Para>
6948 This chapter was written by Esteve Camps &lt;esteve@hades.udg.es&gt;.
6949 </Para>
6951 <Sect2>
6952 <Title>Introduction</Title>
6954 <Para>
6955 First of all, first of all, it would be a great idea for you to read RFCs
6956 written about this (RFC2474, RFC2475, RFC2597 and RFC2598) at 
6957 <ULink URL="http://www.ietf.org/html.charters/diffserv-charter.html">
6958   IETF DiffServ working Group web site</ULink> and
6959 <ULink URL="http://diffserv.sf.net/">
6960   Werner Almesberger web site</ULink>
6961 (he wrote the code to support Differentiated Services on Linux).
6962 </Para>
6964 </Sect2>
6966 <Sect2>
6967 <Title>What is Dsmark related to?</Title>
6969 <Para>
6970 Dsmark is a queueing discipline that offers the capabilities needed in
6971 Differentiated Services (also called DiffServ or, simply, DS). DiffServ is
6972 one of two actual QoS architectures (the other one is called Integrated
6973 Services) that is based on a value carried by packets in the DS field of the
6974 IP header.
6975 </Para>
6977 <Para>
6978 One of the first solutions in IP designed to offer some QoS level was
6979 the Type of Service field (TOS byte) in IP header. By changing that value,
6980 we could choose a high/low level of throughput, delay or reliability.
6981 But this didn't provide sufficient flexibility to the needs of new
6982 services (such as real-time applications, interactive applications and
6983 others). After this, new architectures appeared. One of these was DiffServ
6984 which kept TOS bits and renamed DS field.
6985 </Para>
6987 </Sect2>
6989 <Sect2>
6990 <Title>Differentiated Services guidelines</Title>
6992 <Para>
6993 Differentiated Services is group-oriented. I mean, we don't know anything
6994 about flows (this will be the Integrated Services purpose); we know about
6995 flow aggregations and we will apply different behaviours depending on which
6996 aggregation a packet belongs to.
6997 </Para>
6999 <Para>
7000 When a packet arrives to an edge node (entry node to a DiffServ domain)
7001 entering to a DiffServ Domain we'll have to policy, shape and/or mark those
7002 packets (marking refers to assigning a value to the DS field. It's just like the
7003 cows :-) ). This will be the mark/value that the internal/core nodes on our
7004 DiffServ Domain will look at to determine which behaviour or QoS level
7005 apply.
7006 </Para>
7008 <Para>
7009 As you can deduce, Differentiated Services involves a domain on which
7010 all DS rules will have to be applied. In fact you can think I
7011 will classify all the packets entering my domain. Once they enter my
7012 domain they will be subjected to the rules that my classification dictates
7013 and every traversed node will apply that QoS level.
7014 </Para>
7016 <Para>
7017 In fact, you can apply your own policies into your local domains, but some
7018 <Emphasis>Service Level Agreements</Emphasis> should be considered when connecting to
7019 other DS domains.
7020 </Para>
7022 <Para>
7023 At this point, you maybe have a lot of questions. DiffServ is more than I've
7024 explained. In fact, you can understand that I can not resume more than 3
7025 RFCs in just 50 lines :-).
7026 </Para>
7028 </Sect2>
7030 <Sect2>
7031 <Title>Working with Dsmark</Title>
7033 <Para>
7034 As the DiffServ bibliography specifies, we differentiate boundary nodes and
7035 interior nodes. These are two important points in the traffic path. Both
7036 types perform a classification when the packets arrive. Its result may be
7037 used in different places along the DS process before the packet is released
7038 to the network. It's just because of this that the diffserv code supplies an
7039 structure called sk_buff, including a new field called skb-&gt;tc_index
7040 where we'll store the result of initial classification that may be used in
7041 several points in DS treatment.
7042 </Para>
7044 <Para>
7045 The skb-&gt;tc_index value will be initially set by the DSMARK qdisc,
7046 retrieving it from the DS field in IP header of every received packet.
7047 Besides, cls_tcindex classifier will read all or part of skb-&gt;tcindex
7048 value and use it to select classes.
7049 </Para>
7051 <Para>
7052 But, first of all, take a look at DSMARK qdisc command and its parameters:
7054 <Screen>
7055 ... dsmark indices INDICES [ default_index DEFAULT_INDEX ] [ set_tc_index ]
7056 </Screen>
7058 What do these parameters mean?
7060 <ItemizedList>
7061 <ListItem>
7063 <Para>
7064 <Emphasis remap="bf">indices</Emphasis>: size of table of (mask,value) pairs. Maximum value is 2&circ;n, where n&gt;=0.
7065 </Para>
7066 </ListItem>
7067 <ListItem>
7069 <Para>
7070 <Emphasis remap="bf">Default_index</Emphasis>: the default table entry index if classifier finds no match.
7071 </Para>
7072 </ListItem>
7073 <ListItem>
7075 <Para>
7076 <Emphasis remap="bf">Set_tc_index</Emphasis>: instructs dsmark discipline to retrieve the DS field and store it onto skb-&gt;tc_index.
7077 </Para>
7078 </ListItem>
7080 </ItemizedList>
7082 Let's see the DSMARK process.
7083 </Para>
7085 </Sect2>
7087 <Sect2>
7088 <Title>How SCH_DSMARK works.</Title>
7090 <Para>
7091 This qdisc will apply the next steps:
7093 <ItemizedList>
7094 <ListItem>
7096 <Para>
7097 If we have declared set_tc_index option in qdisc command, DS field is retrieved and stored onto
7098 skb-&gt;tc_index variable.
7099 </Para>
7100 </ListItem>
7101 <ListItem>
7103 <Para>
7104 Classifier is invoked. The classifier will be executed and it will return a class ID that will be stored in
7105 skb-&gt;tc_index variable.If no filter matches are found, we consider the default_index option to be the
7106 classId to store. If neither set_tc_index nor default_index has been declared results may be
7107 unpredictable.
7108 </Para>
7109 </ListItem>
7110 <ListItem>
7112 <Para>
7113 After been sent to internal qdiscs where you can reuse the result of the filter, the classid returned by
7114 the internal qdisc is stored into skb-&gt;tc_index. We will use this value in the future to index a mask-
7115 value table. The final result to assign to the packet will be that resulting from next operation:
7117 <Screen>
7118 New_Ds_field = ( Old_DS_field &#38; mask ) | value
7119 </Screen>
7122 </Para>
7123 </ListItem>
7124 <ListItem>
7126 <Para>
7127 Thus, new value will result from "anding" ds_field and mask values and next, this result "ORed" with
7128 value parameter. See next diagram to understand all this process:
7129 </Para>
7130 </ListItem>
7132 </ItemizedList>
7135 <Screen>
7136                          skb-&gt;ihp-&gt;tos
7137 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - &gt;
7138      |                                                       |     ^
7139      | -- If you declare set_tc_index, we set DS             |     |  &lt;-----May change
7140      |    value into skb-&gt;tc_index variable                  |     |O       DS field
7141      |                                                      A|     |R
7142    +-|-+      +------+    +---+-+    Internal   +-+     +---N|-----|----+
7143    | | |      | tc   |---&gt;|   | |--&gt;  . . .  --&gt;| |     |   D|     |    |
7144    | | |-----&gt;|index |---&gt;|   | |     Qdisc     | |----&gt;|    v     |    |
7145    | | |      |filter|---&gt;| | | +---------------+ |   ----&gt;(mask,value) |
7146 --&gt;| O |      +------+    +-|-+--------------^----+  /  |  (.  ,  .)    |
7147    | | |          ^         |                |       |  |  (.  ,  .)    |
7148    | | +----------|---------|----------------|-------|--+  (.  ,  .)    |
7149    | | sch_dsmark |         |                |       |                  |
7150    +-|------------|---------|----------------|-------|------------------+
7151      |            |         | &lt;- tc_index -&gt; |       |
7152      |            |(read)   |    may change  |       |  &lt;--------------Index to the
7153      |            |         |                |       |                    (mask,value)
7154      v            |         v                v       |                    pairs table
7155 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -&gt;
7156                          skb-&gt;tc_index
7157 </Screen>
7159 </Para>
7161 <Para>
7162 How to do marking? Just change the mask and value of the class you want to remark. See next line of code:
7164 <Screen>
7165 tc class change dev eth0 classid 1:1 dsmark mask 0x3 value 0xb8
7166 </Screen>
7168 This changes the (mask,value) pair in hash table, to remark packets belonging to class 1:1.You have to "change" this values
7169 because of default values that (mask,value) gets initially (see table below).
7170 </Para>
7172 <Para>
7173 Now, we'll explain how TC_INDEX filter works and how fits into this. Besides, TCINDEX filter can be
7174 used in other configurations rather than those including DS services.
7175 </Para>
7177 </Sect2>
7179 <Sect2>
7180 <Title>TC_INDEX Filter</Title>
7182 <Para>
7183 This is the basic command to declare a TC_INDEX filter:
7185 <Screen>
7186 ... tcindex [ hash SIZE ] [ mask MASK ] [ shift SHIFT ]
7187             [ pass_on | fall_through ]
7188             [ classid CLASSID ] [ police POLICE_SPEC ]
7189 </Screen>
7191 Next, we show the example used to explain TC_INDEX operation mode. Pay attention to bolded words:
7194 tc qdisc add dev eth0 handle 1:0 root dsmark indices 64 <Emphasis remap="bf">set_tc_index</Emphasis>
7196 tc filter add dev eth0 parent 1:0 protocol ip prio 1 tcindex <Emphasis remap="bf">mask 0xfc  shift 2</Emphasis>
7198 tc qdisc add dev eth0 parent 1:0 handle 2:0 cbq bandwidth 10Mbit cell 8 avpkt 1000 mpu 64
7200 &num; EF traffic class
7202 tc class add dev eth0 parent 2:0 classid 2:1 cbq bandwidth 10Mbit rate 1500Kbit avpkt 1000 prio 1 bounded isolated allot 1514 weight 1 maxburst 10
7204 &num; Packet fifo qdisc for EF traffic
7206 tc qdisc add dev eth0 parent 2:1 pfifo limit 5
7208 tc filter add dev eth0 parent 2:0 protocol ip prio 1 <Emphasis remap="bf">handle 0x2e</Emphasis> tcindex <Emphasis remap="bf">classid 2:1 pass_on</Emphasis>
7212 (This code is not complete. It's just an extract from EFCBQ example included in iproute2 distribution).
7213 </Para>
7215 <Para>
7216 First of all, suppose we receive a packet marked as EF .  If you read RFC2598, you'll see that DSCP
7217 recommended value for EF traffic is 101110. This means that DS field will be 10111000 (remember that
7218 less significant bits in TOS byte are not used in DS) or 0xb8 in hexadecimal codification.
7219 </Para>
7221 <Para>
7223 <Screen>
7224               TC INDEX
7225               FILTER
7226    +---+      +-------+    +---+-+    +------+                +-+    +-------+
7227    |   |      |       |    |   | |    |FILTER|  +-+    +-+    | |    |       |
7228    |   |-----&gt;| MASK  | -&gt; |   | | -&gt; |HANDLE|-&gt;| |    | | -&gt; | | -&gt; |       |
7229    |   |  .   | =0xfc |    |   | |    |0x2E  |  | +----+ |    | |    |       |
7230    |   |  .   |       |    |   | |    +------+  +--------+    | |    |       |
7231    |   |  .   |       |    |   | |                            | |    |       |
7232 --&gt;|   |  .   | SHIFT |    |   | |                            | |    |       |--&gt;
7233    |   |  .   | =2    |    |   | +----------------------------+ |    |       |
7234    |   |      |       |    |   |       CBQ 2:0                  |    |       |
7235    |   |      +-------+    +---+--------------------------------+    |       |
7236    |   |                                                             |       |
7237    |   +-------------------------------------------------------------+       |
7238    |                          DSMARK 1:0                                     |
7239    +-------------------------------------------------------------------------+
7241 </Screen>
7243 </Para>
7245 <Para>
7246 The packet arrives, then, set with 0xb8 value at DS field. As we explained before, dsmark qdisc identified
7247 by 1:0 id in the example, retrieves DS field and store it in skb-&gt;tc_index variable.
7248 Next step in the example will correspond to the filter associated to this qdisc (second line in the example).
7249 This will perform next operations:
7251 <Screen>
7252 Value1 = skb-&#62;tc_index &#38; MASK
7253 Key = Value1 &#62;&#62; SHIFT
7254 </Screen>
7256 </Para>
7258 <Para>
7259 In the example, MASK=0xFC i SHIFT=2.
7261 <Screen>
7262 Value1 = 10111000 &#38; 11111100 = 10111000
7263 Key = 10111000 &#62;&#62; 2 = 00101110 -&#62; 0x2E in hexadecimal
7264 </Screen>
7266 </Para>
7268 <Para>
7269 The returned value will correspond to a qdisc internal filter handle (in the example, identifier 2:0). If a
7270 filter with this id exists, policing and metering conditions will be verified (in case that filter includes this)
7271 and the classid will be returned (in our example, classid 2:1) and stored in skb-&gt;tc_index variable.
7272 </Para>
7274 <Para>
7275 But if any filter with that identifier is found, the result will depend on fall_through flag declaration. If so,
7276 value key is returned as classid. If not, an error is returned and process continues with the rest filters. Be
7277 careful if you use fall_through flag; this can be done if a simple relation exists between values
7279 of skb-&gt;tc_index variable and class id's.
7280 </Para>
7282 <Para>
7283 The latest parameters to comment on are hash and pass_on. The first one
7284 relates to hash table size. Pass_on will be used to indicate that if no classid
7285 equal to the result of this filter is found, try next filter.
7286 The default action is fall_through (look at next table).
7287 </Para>
7289 <Para>
7290 Finally, let's see which possible values can be set to all this TCINDEX parameters:
7292 <Screen>
7293 TC Name                 Value           Default
7294 -----------------------------------------------------------------
7295 Hash                    1...0x10000     Implementation dependent
7296 Mask                    0...0xffff      0xffff
7297 Shift                   0...15          0
7298 Fall through / Pass_on  Flag            Fall_through
7299 Classid                 Major:minor     None
7300 Police                  .....           None
7301 </Screen>
7303 </Para>
7305 <Para>
7306 This kind of filter is very powerful. It's necessary to explore all possibilities. Besides, this filter is not only used in DiffServ configurations.
7307 You can use it as any other kind of filter.
7308 </Para>
7310 <Para>
7311 I recommend you to look at all DiffServ examples included in iproute2 distribution. I promise I will try to
7312 complement this text as soon as I can. Besides, all I have explained is the result of a lot of tests.
7313 I would thank you tell me if I'm wrong in any point.
7314 </Para>
7316 </Sect2>
7318 </Sect1>
7320 <Sect1 id="lartc.adv-qdisc.ingress">
7321   <Title>Ingress qdisc</Title>
7323 <Para>
7324 All qdiscs discussed so far are egress qdiscs. Each interface however can
7325 also have an ingress qdisc which is not used to send packets
7326 out to the network adaptor. Instead, it allows you to apply tc filters to
7327 packets coming in over the interface, regardless of whether they have a local
7328 destination or are to be forwarded.
7329 </Para>
7331 <Para>
7332 As the tc filters contain a full Token Bucket Filter implementation, and are
7333 also able to match on the kernel flow estimator, there is a lot of
7334 functionality available. This effectively allows you to police incoming
7335 traffic, before it even enters the IP stack.
7336 </Para>
7338 <Sect2>
7339 <Title>Parameters &amp; usage</Title>
7341 <Para>
7342 The ingress qdisc itself does not require any parameters. It differs from
7343 other qdiscs in that it does not occupy the root of a device. Attach it like
7344 this:
7346 <Screen>
7347 # tc qdisc add dev eth0 ingress
7348 </Screen>
7350 This allows you to have other, sending, qdiscs on your device besides the
7351 ingress qdisc. 
7352 </Para>
7354 <Para>
7355 For a contrived example how the ingress qdisc could be used, see the
7356 Cookbook.
7357 </Para>
7359 </Sect2>
7361 </Sect1>
7363 <Sect1 id="lartc.adv-qdisc.red">
7364   <Title>Random Early Detection (RED)</Title>
7366 <Para>
7367 This section is meant as an introduction to backbone routing, which often
7368 involves &lt;100 megabit bandwidths, which requires a different approach than
7369 your ADSL modem at home.
7370 </Para>
7372 <Para>
7373 The normal behaviour of router queues on the Internet is called tail-drop.
7374 Tail-drop works by queueing up to a certain amount, then dropping all traffic
7375 that 'spills over'. This is very unfair, and also leads to retransmit 
7376 synchronization. When retransmit synchronization occurs, the sudden burst
7377 of drops from a router that has reached its fill will cause a delayed burst
7378 of retransmits, which will over fill the congested router again. 
7379 </Para>
7381 <Para>
7382 In order to cope with transient congestion on links, backbone routers will
7383 often implement large queues. Unfortunately, while these queues are good for
7384 throughput, they can substantially increase latency and cause TCP
7385 connections to behave very burstily during congestion.
7386 </Para>
7388 <Para>
7389 These issues with tail-drop are becoming increasingly troublesome on the
7390 Internet because the use of network unfriendly applications is increasing.
7391 The Linux kernel offers us RED, short for Random Early Detect, also called
7392 Random Early Drop, as that is how it works.
7393 </Para>
7395 <Para>
7396 RED isn't a cure-all for this, applications which inappropriately fail to 
7397 implement exponential backoff still get an unfair share of the bandwidth,
7398 however, with RED they do not cause as much harm to the throughput and
7399 latency of other connections.
7400 </Para>
7402 <Para>
7403 RED statistically drops packets from flows before it reaches its hard
7404 limit. This causes a congested backbone link to slow more gracefully, and
7405 prevents retransmit synchronization. This also helps TCP find its 'fair'
7406 speed faster by allowing some packets to get dropped sooner keeping queue
7407 sizes low and latency under control. The probability of a packet being
7408 dropped from a particular connection is proportional to its bandwidth usage
7409 rather than the number of packets it transmits. 
7410 </Para>
7412 <Para>
7413 RED is a good queue for backbones, where you can't afford the 
7414 complexity of per-session state tracking needed by fairness queueing.
7415 </Para>
7417 <Para>
7418 In order to use RED, you must decide on three parameters: Min, Max, and
7419 burst. Min sets the minimum queue size in bytes before dropping will begin,
7420 Max is a soft maximum that the algorithm will attempt to stay under, and
7421 burst sets the maximum number of packets that can 'burst through'.
7422 </Para>
7424 <Para>
7425 You should set the min by calculating that highest acceptable base queueing 
7426 latency you wish, and multiply it by your bandwidth. For instance, on my 
7427 64kbit/s ISDN link, I might want a base queueing latency of 200ms so I set
7428 min to 1600 bytes. Setting min too small will degrade throughput and too
7429 large will degrade latency. Setting a small min is not a replacement for
7430 reducing the MTU on a slow link to improve interactive response.
7431 </Para>
7433 <Para>
7434 You should make max at least twice min to prevent synchronization. On slow
7435 links with small Min's it might be wise to make max perhaps four or
7436 more times large then min.
7437 </Para>
7439 <Para>
7440 Burst controls how the RED algorithm responds to bursts. Burst must be set
7441 larger then min/avpkt. Experimentally, I've found (min+min+max)/(3*avpkt) to
7442 work ok.
7443 </Para>
7445 <Para>
7446 Additionally, you need to set limit and avpkt. Limit is a safety value, after
7447 there are limit bytes in the queue, RED 'turns into' tail-drop. I typical set
7448 limit to eight times max. Avpkt should be your average packet size. 1000
7449 works OK on high speed Internet links with a 1500byte MTU. 
7450 </Para>
7452 <Para>
7453 Read <ULink
7454 URL="http://www.aciri.org/floyd/papers/red/red.html"
7455 >the paper on RED queueing</ULink
7456 > by Sally Floyd and Van Jacobson for technical
7457 information.
7458 </Para>
7460 </Sect1>
7462 <Sect1 id="lartc.adv-qdisc.gred">
7463 <Title>Generic Random Early Detection</Title>
7465 <Para>
7466 Not a lot is known about GRED. It looks like GRED with several internal
7467 queues, whereby the internal queue is chosen based on the Diffserv tcindex
7468 field. According to a slide found
7469 <ULink URL="http://www.davin.ottawa.on.ca/ols/img22.htm">here</ULink>,
7470 it contains the capabilities of Cisco's 'Distributed Weighted RED', as well 
7471 as Dave Clark's RIO.
7472 </Para>
7474 <Para>
7475 Each virtual queue can have its own Drop Parameters specified.
7476 </Para>
7478 <Para>
7479 FIXME: get Jamal or Werner to tell us more
7480 </Para>
7482 </Sect1>
7484 <Sect1 id="lartc.adv-qdisc.vc-atm">
7485   <Title>VC/ATM emulation</Title>
7487 <Para>
7488 This is quite a major effort by Werner Almesberger to allow you to build
7489 Virtual Circuits over TCP/IP sockets. A Virtual Circuit is a concept from
7490 ATM network theory. 
7491 </Para>
7493 <Para>
7494 For more information, see the <ULink
7495 URL="http://linux-atm.sourceforge.net/"
7496 >ATM on Linux homepage</ULink
7497 >. 
7498 </Para>
7500 </Sect1>
7502 <Sect1 id="lartc.adv-qdisc.wrr">
7503   <Title>Weighted Round Robin (WRR)</Title>
7505 <Para>
7506 This qdisc is not included in the standard kernels but can be downloaded from 
7507 <ULink URL="http://wipl-wrr.dkik.dk/wrr/">here</ULink>. 
7508 Currently the qdisc is only tested with Linux 2.2 kernels but it will 
7509 probably work with 2.4/2.5 kernels too.
7510 </Para>
7512 <Para>
7513 The WRR qdisc distributes bandwidth between its classes using the weighted 
7514 round robin scheme. That is, like the CBQ qdisc it contains classes 
7515 into which arbitrary qdiscs can be plugged. All classes which have sufficient 
7516 demand will get bandwidth proportional to the weights associated with the classes.
7517 The weights can be set manually using the <Literal remap="tt">tc</Literal> program. But they
7518 can also be made automatically decreasing for classes transferring much data.
7519 </Para>
7521 <Para>
7522 The qdisc has a built-in classifier which assigns packets coming from or 
7523 sent to different machines to different classes. Either the MAC or IP and 
7524 either source or destination addresses can be used. The MAC address can only 
7525 be used when the Linux box is acting as an ethernet bridge, however. The 
7526 classes are automatically assigned to machines based on the packets seen.
7527 </Para>
7529 <Para>
7530 The qdisc can be very useful at sites such as dorms where a lot of unrelated 
7531 individuals share an Internet connection. A set of scripts setting up a 
7532 relevant behavior for such a site is a central part of the WRR distribution.
7533 </Para>
7535 </Sect1>
7537 </chapter>
7539 <chapter id="lartc.cookbook"
7540   xreflabel="Cookbook">
7541   <Title>Cookbook</Title>
7543 <Para>
7544 This section contains 'cookbook' entries which may help you solve problems.
7545 A cookbook is no replacement for understanding however, so try and comprehend
7546 what is going on. 
7547 </Para>
7549 <Sect1 id="lartc.cookbook.sla">
7550 <Title>Running multiple sites with different SLAs</Title>
7552 <Para>
7553 You can do this in several ways. Apache has some support for this with a
7554 module, but we'll show how Linux can do this for you, and do so for other
7555 services as well. These commands are stolen from a presentation by Jamal
7556 Hadi that's referenced below.
7557 </Para>
7559 <Para>
7560 Let's say we have two customers, with http, ftp and streaming audio, and we
7561 want to sell them a limited amount of bandwidth. We do so on the server itself.
7562 </Para>
7564 <Para>
7565 Customer A should have at most 2 megabits, customer B has paid for 5
7566 megabits. We separate our customers by creating virtual IP addresses on our
7567 server.
7568 </Para>
7570 <Para>
7572 <Screen>
7573 # ip address add 188.177.166.1 dev eth0
7574 # ip address add 188.177.166.2 dev eth0
7575 </Screen>
7577 </Para>
7579 <Para>
7580 It is up to you to attach the different servers to the right IP address. All
7581 popular daemons have support for this.
7582 </Para>
7584 <Para>
7585 We first attach a CBQ qdisc to eth0:
7587 <Screen>
7588 # tc qdisc add dev eth0 root handle 1: cbq bandwidth 10Mbit cell 8 avpkt 1000 \
7589   mpu 64
7590 </Screen>
7592 </Para>
7594 <Para>
7595 We then create classes for our customers:
7596 </Para>
7598 <Para>
7600 <Screen>
7601 # tc class add dev eth0 parent 1:0 classid 1:1 cbq bandwidth 10Mbit rate \
7602   2MBit avpkt 1000 prio 5 bounded isolated allot 1514 weight 1 maxburst 21
7603 # tc class add dev eth0 parent 1:0 classid 1:2 cbq bandwidth 10Mbit rate \
7604   5Mbit avpkt 1000 prio 5 bounded isolated allot 1514 weight 1 maxburst 21
7605 </Screen>
7607 </Para>
7609 <Para>
7610 Then we add filters for our two classes:
7612 <Screen>
7613 ##FIXME: Why this line, what does it do?, what is a divisor?:
7614 ##FIXME: A divisor has something to do with a hash table, and the number of
7615 ##       buckets - ahu
7616 # tc filter add dev eth0 parent 1:0 protocol ip prio 5 handle 1: u32 divisor 1
7617 # tc filter add dev eth0 parent 1:0 prio 5 u32 match ip src 188.177.166.1
7618   flowid 1:1
7619 # tc filter add dev eth0 parent 1:0 prio 5 u32 match ip src 188.177.166.2
7620   flowid 1:2
7621 </Screen>
7623 </Para>
7625 <Para>
7626 And we're done.
7627 </Para>
7629 <Para>
7630 FIXME: why no token bucket filter? is there a default pfifo_fast fallback
7631 somewhere?
7632 </Para>
7634 </Sect1>
7636 <Sect1 id="lartc.cookbook.synflood-protect"
7637   xreflabel="Protecting your host from SYN floods">
7638   <Title>Protecting your host from SYN floods</Title>
7640 <Para>
7641 From Alexey's iproute documentation, adapted to netfilter and with more
7642 plausible paths. If you use this, take care to adjust the numbers to
7643 reasonable values for your system.
7644 </Para>
7646 <Para>
7647 If you want to protect an entire network, skip this script, which is best
7648 suited for a single host.
7649 </Para>
7651 <Para>
7652 It appears that you need the very latest version of the iproute2 tools to
7653 get this to work with 2.4.0.
7654 </Para>
7656 <Para>
7658 <Screen>
7659 #! /bin/sh -x
7661 # sample script on using the ingress capabilities
7662 # this script shows how one can rate limit incoming SYNs
7663 # Useful for TCP-SYN attack protection. You can use
7664 # IPchains to have more powerful additions to the SYN (eg 
7665 # in addition the subnet)
7667 #path to various utilities;
7668 #change to reflect yours.
7670 TC=/sbin/tc
7671 IP=/sbin/ip
7672 IPTABLES=/sbin/iptables
7673 INDEV=eth2
7675 # tag all incoming SYN packets through $INDEV as mark value 1
7676 ############################################################ 
7677 $iptables -A PREROUTING -i $INDEV -t mangle -p tcp --syn \
7678   -j MARK --set-mark 1
7679 ############################################################ 
7681 # install the ingress qdisc on the ingress interface
7682 ############################################################ 
7683 $TC qdisc add dev $INDEV handle ffff: ingress
7684 ############################################################ 
7688 # SYN packets are 40 bytes (320 bits) so three SYNs equals
7689 # 960 bits (approximately 1kbit); so we rate limit below
7690 # the incoming SYNs to 3/sec (not very useful really; but
7691 #serves to show the point - JHS
7692 ############################################################ 
7693 $TC filter add dev $INDEV parent ffff: protocol ip prio 50 handle 1 fw \
7694 police rate 1kbit burst 40 mtu 9k drop flowid :1
7695 ############################################################ 
7699 echo "---- qdisc parameters Ingress  ----------"
7700 $TC qdisc ls dev $INDEV
7701 echo "---- Class parameters Ingress  ----------"
7702 $TC class ls dev $INDEV
7703 echo "---- filter parameters Ingress ----------"
7704 $TC filter ls dev $INDEV parent ffff:
7706 #deleting the ingress qdisc
7707 #$TC qdisc del $INDEV ingress
7708 </Screen>
7710 </Para>
7712 </Sect1>
7714 <Sect1 id="lartc.cookbook.icmp-ratelimit">
7715   <Title>Rate limit ICMP to prevent dDoS</Title>
7717 <Para>
7718 Recently, distributed denial of service attacks have become a major nuisance
7719 on the Internet. By properly filtering and rate limiting your network, you can
7720 both prevent becoming a casualty or the cause of these attacks.
7721 </Para>
7723 <Para>
7724 You should filter your networks so that you do not allow non-local IP source
7725 addressed packets to leave your network. This stops people from anonymously
7726 sending junk to the Internet. 
7727 </Para>
7729 <Para>
7730 Rate limiting goes much as shown earlier. To refresh your memory, our
7731 ASCIIgram again:
7732 </Para>
7734 <Para>
7736 <Screen>
7737 [The Internet] ---&#60;E3, T3, whatever&#62;--- [Linux router] --- [Office+ISP]
7738                                       eth1          eth0
7739 </Screen>
7741 </Para>
7743 <Para>
7744 We first set up the prerequisite parts:
7745 </Para>
7747 <Para>
7749 <Screen>
7750 # tc qdisc add dev eth0 root handle 10: cbq bandwidth 10Mbit avpkt 1000
7751 # tc class add dev eth0 parent 10:0 classid 10:1 cbq bandwidth 10Mbit rate \
7752   10Mbit allot 1514 prio 5 maxburst 20 avpkt 1000
7753 </Screen>
7755 </Para>
7757 <Para>
7758 If you have 100Mbit, or more, interfaces, adjust these numbers. Now you need
7759 to determine how much ICMP traffic you want to allow. You can perform
7760 measurements with tcpdump, by having it write to a file for a while, and
7761 seeing how much ICMP passes your network. Do not forget to raise the
7762 snapshot length!
7763 </Para>
7765 <Para>
7766 If measurement is impractical, you might want to choose 5&percnt; of your available
7767 bandwidth. Let's set up our class:
7769 <Screen>
7770 # tc class add dev eth0 parent 10:1 classid 10:100 cbq bandwidth 10Mbit rate \
7771   100Kbit allot 1514 weight 800Kbit prio 5 maxburst 20 avpkt 250 \
7772   bounded
7773 </Screen>
7775 </Para>
7777 <Para>
7778 This limits at 100Kbit. Now we need a filter to assign ICMP traffic to this
7779 class:
7781 <Screen>
7782 # tc filter add dev eth0 parent 10:0 protocol ip prio 100 u32 match ip
7783   protocol 1 0xFF flowid 10:100
7785 </Screen>
7787 </Para>
7789 </Sect1>
7791 <Sect1 id="lartc.cookbook.interactive-prio">
7792 <Title>Prioritizing interactive traffic</Title>
7794 <Para>
7795 If lots of data is coming down your link, or going up for that matter, and
7796 you are trying to do some maintenance via telnet or ssh, this may not go too
7797 well. Other packets are blocking your keystrokes. Wouldn't it be great if
7798 there were a way for your interactive packets to sneak past the bulk
7799 traffic? Linux can do this for you!
7800 </Para>
7802 <Para>
7803 As before, we need to handle traffic going both ways. Evidently, this works
7804 best if there are Linux boxes on both ends of your link, although other
7805 UNIX's are able to do this. Consult your local Solaris/BSD guru for this.
7806 </Para>
7808 <Para>
7809 The standard pfifo_fast scheduler has 3 different 'bands'. Traffic in band 0
7810 is transmitted first, after which traffic in band 1 and 2 gets considered.
7811 It is vital that our interactive traffic be in band 0!
7812 </Para>
7814 <Para>
7815 We blatantly adapt from the (soon to be obsolete) ipchains HOWTO:
7816 </Para>
7818 <Para>
7819 There are four seldom-used bits in the IP header, called the Type of Service
7820 (TOS) bits. They effect the way packets are treated; the four bits are
7821 "Minimum Delay", "Maximum Throughput", "Maximum Reliability" and "Minimum
7822 Cost". Only one of these bits is allowed to be set. Rob van Nieuwkerk, the
7823 author of the ipchains TOS-mangling code, puts it as follows:
7824 </Para>
7826 <Para>
7828 <Screen>
7829 Especially the "Minimum Delay" is important for me. I switch it on for
7830 "interactive" packets in my upstream (Linux) router. I'm
7831 behind a 33k6 modem link. Linux prioritizes packets in 3 queues. This
7832 way I get acceptable interactive performance while doing bulk
7833 downloads at the same time. 
7834 </Screen>
7836 </Para>
7838 <Para>
7839 The most common use is to set telnet &#38; ftp control connections to "Minimum
7840 Delay" and FTP data to "Maximum Throughput". This would be
7841 done as follows, on your upstream router:
7842 </Para>
7844 <Para>
7846 <Screen>
7847 # iptables -A PREROUTING -t mangle -p tcp --sport telnet \
7848   -j TOS --set-tos Minimize-Delay
7849 # iptables -A PREROUTING -t mangle -p tcp --sport ftp \
7850   -j TOS --set-tos Minimize-Delay
7851 # iptables -A PREROUTING -t mangle -p tcp --sport ftp-data \
7852   -j TOS --set-tos Maximize-Throughput
7853 </Screen>
7855 </Para>
7857 <Para>
7858 Now, this only works for data going from your telnet foreign host to your
7859 local computer. The other way around appears to be done for you, ie, telnet,
7860 ssh &#38; friends all set the TOS field on outgoing packets automatically.
7861 </Para>
7863 <Para>
7864 Should you have an application that does not do this, you can always do it 
7865 with netfilter. On your local box:
7866 </Para>
7868 <Para>
7870 <Screen>
7871 # iptables -A OUTPUT -t mangle -p tcp --dport telnet \
7872   -j TOS --set-tos Minimize-Delay
7873 # iptables -A OUTPUT -t mangle -p tcp --dport ftp \
7874   -j TOS --set-tos Minimize-Delay
7875 # iptables -A OUTPUT -t mangle -p tcp --dport ftp-data \
7876   -j TOS --set-tos Maximize-Throughput
7877 </Screen>
7879 </Para>
7881     </Sect1>
7883     <Sect1 id="lartc.cookbook.squid">
7884       <Title>Transparent web-caching using <application>netfilter</application>,
7885         <application>iproute2</application>, <application>ipchains</application> and 
7886         <application>squid</application></Title>
7887       
7888       <Para>
7889         This section was sent in by reader Ram Narula from Internet for Education
7890         (Thailand).
7891       </Para>
7892       
7893       <Para>
7894         The regular technique in accomplishing this in Linux
7895         is probably with use of ipchains AFTER making sure
7896         that the "outgoing" port 80(web) traffic gets routed through
7897         the server running squid.
7898       </Para>
7900       <Para>
7901         There are 3 common methods to make sure "outgoing"
7902         port 80 traffic gets routed to the server running squid
7903         and 4th one is being introduced here.
7904       </Para>
7905       
7906       <Para>
7907         <VariableList>
7908           <VarListEntry>
7909             <Term>Making the gateway router do it.</Term>
7910             <ListItem>
7911               <Para>
7912                 If you can tell your gateway router to 
7913                 match packets that has outgoing destination port
7914                 of 80 to be sent to the IP address of squid server.
7915               </Para>
7916               
7917               <Para>
7918                 BUT
7919               </Para>
7921               <Para>
7922                 This would put additional load on the router and
7923                 some commercial routers might not even support this.
7924               </Para>
7925             </ListItem>
7926           </VarListEntry>
7927           <VarListEntry>
7928             <Term>Using a Layer 4 switch.</Term>
7929             <ListItem>
7930               <Para>
7931                 Layer 4 switches can handle this without any problem.
7932               </Para>
7933               
7934               <Para>
7935                 BUT
7936               </Para>
7937               
7938               <Para>
7939                 The cost for this equipment is usually very high. Typical
7940                 layer 4 switch would normally cost more than
7941                 a typical router+good linux server.
7942               </Para>
7943             </ListItem>
7944           </VarListEntry>
7945           <VarListEntry>
7946             <Term>Using cache server as network's gateway.</Term>
7947             <ListItem>
7948               <Para>
7949                 You can force ALL traffic through cache server.
7950               </Para>
7951               <Para>
7952                 BUT
7953               </Para>
7954               <Para>
7955                 This is quite risky because Squid does utilize lots of CPU power which might
7956                 result in slower over-all network performance or the server itself might crash and no one on the
7957                 network will be able to access the Internet if that occurs.
7958               </Para>
7959             </ListItem>
7960           </VarListEntry>
7961           <VarListEntry>
7962             <Term>Linux+NetFilter router.</Term>
7963             <ListItem>
7964               <Para>
7965                 By using NetFilter another technique can be implemented
7966                 which is using NetFilter for "mark"ing the packets
7967                 with destination port 80 and using iproute2 to
7968                 route the "mark"ed packets to the Squid server.
7969               </Para>
7970             </ListItem>
7971           </VarListEntry>
7972         </VariableList>
7973         <Screen>
7974 |----------------|
7975 | Implementation |
7976 |----------------|
7978  Addresses used
7979  10.0.0.1 naret (NetFilter server)
7980  10.0.0.2 silom (Squid server)
7981  10.0.0.3 donmuang (Router connected to the Internet)
7982  10.0.0.4 kaosarn (other server on network)
7983  10.0.0.5 RAS
7984  10.0.0.0/24 main network
7985  10.0.0.0/19 total network
7987 |---------------|
7988 |Network diagram|
7989 |---------------|
7991 Internet
7993 donmuang
7995 ------------hub/switch----------
7996 |        |             |       |
7997 naret   silom        kaosarn  RAS etc.
7998         </Screen>
8000         First, make all traffic pass through naret by making sure it is the default gateway except for silom.
8001         Silom's default gateway has to be donmuang (10.0.0.3) or this would create web traffic loop.
8002       </Para>
8003       <Para>
8004         (all servers on my network had 10.0.0.1 as the default gateway  which was the former IP address of donmuang router so what I did
8005         was changed the IP address of donmuang to 10.0.0.3 and gave naret ip address of 10.0.0.1)
8006       </Para>
8007       <Para>
8008         <Screen>
8009 Silom
8010 -----
8011 -setup squid and ipchains 
8012         </Screen>
8013       </Para>
8014       <Para>
8015         Setup Squid server on silom, make sure it does support  transparent caching/proxying, the default port is usually
8016         3128, so all traffic for port 80 has to be redirected to port 3128 locally. This can be done by using ipchains with the following:
8017       </Para>
8018       <Para>
8019         <Screen>
8020 silom# ipchains -N allow1
8021 silom# ipchains -A allow1 -p TCP -s 10.0.0.0/19 -d 0/0 80 -j REDIRECT 3128
8022 silom# ipchains -I input -j allow1
8023         </Screen>
8024       </Para>
8025       <Para>
8026 Or, in netfilter lingo:
8027         <Screen>
8028 silom# iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-port 3128
8029         </Screen>
8030       </Para>
8031       <Para>
8032         (note: you might have other entries as well)
8033       </Para>
8034       <Para>
8035         For more information on setting Squid server please refer to Squid FAQ page on <ULink
8036           URL="http://squid.nlanr.net">http://squid.nlanr.net</ULink>).
8037       </Para>
8038       <Para>
8039         Make sure ip forwarding is enabled on this server and the default gateway for this server is donmuang router (NOT naret).
8040       </Para>
8041       <Para>
8042         <Screen>
8043 Naret
8044 -----
8045 -setup iptables and iproute2
8046 -disable icmp REDIRECT messages (if needed)
8047         </Screen>
8048       </Para>
8049       <Para>
8050         <OrderedList>
8051           <ListItem>
8052             <Para>
8053               "Mark" packets of destination port 80 with value 2
8054               <Screen>
8055 naret# iptables -A PREROUTING -i eth0 -t mangle -p tcp --dport 80 \
8056  -j MARK --set-mark 2
8057               </Screen>
8058             </Para>
8059           </ListItem>
8060           <ListItem>
8061             <Para>
8062               Setup iproute2 so it will route packets with "mark" 2 to silom
8063               <Screen>
8064 naret# echo 202 www.out &#62;&#62; /etc/iproute2/rt_tables
8065 naret# ip rule add fwmark 2 table www.out
8066 naret# ip route add default via 10.0.0.2 dev eth0 table www.out
8067 naret# ip route flush cache
8068               </Screen>
8069             </para>
8070             <Para>
8071               If donmuang and naret is on the same subnet then naret should not send out icmp REDIRECT messages.
8072               In this case it is, so icmp REDIRECTs has to be disabled by:
8073               <Screen>
8074 naret# echo 0 &#62; /proc/sys/net/ipv4/conf/all/send_redirects
8075 naret# echo 0 &#62; /proc/sys/net/ipv4/conf/default/send_redirects
8076 naret# echo 0 &#62; /proc/sys/net/ipv4/conf/eth0/send_redirects
8077               </Screen>
8078             </Para>
8079           </ListItem>
8080         </OrderedList>
8081       </Para>
8082       <Para>
8083         The setup is complete, check the configuration
8084       </Para>
8085       <Para>
8086         <Screen>
8087 On naret:
8089 naret# iptables -t mangle -L
8090 Chain PREROUTING (policy ACCEPT)
8091 target     prot opt source               destination         
8092 MARK       tcp  --  anywhere             anywhere           tcp dpt:www MARK set 0x2 
8094 Chain OUTPUT (policy ACCEPT)
8095 target     prot opt source               destination         
8097 naret# ip rule ls
8098 0:      from all lookup local 
8099 32765:  from all fwmark        2 lookup www.out 
8100 32766:  from all lookup main 
8101 32767:  from all lookup default 
8103 naret# ip route list table www.out
8104 default via 203.114.224.8 dev eth0 
8106 naret# ip route   
8107 10.0.0.1 dev eth0  scope link 
8108 10.0.0.0/24 dev eth0  proto kernel  scope link  src 10.0.0.1
8109 127.0.0.0/8 dev lo  scope link 
8110 default via 10.0.0.3 dev eth0 
8112 (make sure silom belongs to one of the above lines, in this case
8113 it's the line with 10.0.0.0/24)
8115 |------|
8116 |-DONE-|
8117 |------|
8118         </Screen>
8119       </Para>
8120       <Sect2>
8121 <Title>Traffic flow diagram after implementation</Title>
8123 <Screen width="80">
8125 |-----------------------------------------|
8126 |Traffic flow diagram after implementation|
8127 |-----------------------------------------|
8129 INTERNET
8133 -----------------donmuang router---------------------
8134 /\                                      /\         ||
8135 ||                                      ||         ||
8136 ||                                      \/         ||
8137 naret                                  silom       ||
8138 *destination port 80 traffic=========&#62;(cache)      ||
8139 /\                                      ||         ||
8140 ||                                      \/         \/
8141 \\===================================kaosarn, RAS, etc.
8143 </Screen>
8145 <Para>
8146 Note that the network is asymmetric as there is one extra hop on 
8147 general outgoing path.
8148 </Para>
8150 <Para>
8152 <Screen>
8153 Here is run down for packet traversing the network from kaosarn
8154 to and from the Internet.
8156 For web/http traffic:
8157 kaosarn http request-&#62;naret-&#62;silom-&#62;donmuang-&#62;internet
8158 http replies from Internet-&#62;donmuang-&#62;silom-&#62;kaosarn
8160 For non-web/http requests(eg. telnet):
8161 kaosarn outgoing data-&#62;naret-&#62;donmuang-&#62;internet
8162 incoming data from Internet-&#62;donmuang-&#62;kaosarn
8163 </Screen>
8165 </Para>
8167 </Sect2>
8169 </Sect1>
8171 <Sect1 id="lartc.cookbook.mtu-discovery">
8172 <Title>Circumventing Path MTU Discovery issues with per route MTU settings</Title>
8174 <Para>
8175 For sending bulk data, the Internet generally works better when using larger
8176 packets. Each packet implies a routing decision, when sending a 1 megabyte
8177 file, this can either mean around 700 packets when using packets that are as
8178 large as possible, or 4000 if using the smallest default.
8179 </Para>
8181 <Para>
8182 However, not all parts of the Internet support full 1460 bytes of payload
8183 per packet. It is therefore necessary to try and find the largest packet
8184 that will 'fit', in order to optimize a connection.
8185 </Para>
8187 <Para>
8188 This process is called 'Path MTU Discovery', where MTU stands for 'Maximum
8189 Transfer Unit.' 
8190 </Para>
8192 <Para>
8193 When a router encounters a packet that's too big too send in one piece, AND
8194 it has been flagged with the "Don't Fragment" bit, it returns an ICMP
8195 message stating that it was forced to drop a packet because of this. The
8196 sending host acts on this hint by sending smaller packets, and by iterating
8197 it can find the optimum packet size for a connection over a certain path.
8198 </Para>
8200 <Para>
8201 This used to work well until the Internet was discovered by hooligans who do
8202 their best to disrupt communications. This in turn lead administrators to
8203 either block or shape ICMP traffic in a misguided attempt to improve
8204 security or robustness of their Internet service.
8205 </Para>
8207 <Para>
8208 What has happened now is that Path MTU Discovery is working less and less
8209 well and fails for certain routes, which leads to strange TCP/IP sessions
8210 which die after a while.
8211 </Para>
8213 <Para>
8214 Although I have no proof for this, two sites who I used to have this problem
8215 with both run Alteon Acedirectors before the affected systems - perhaps
8216 somebody more knowledgeable can provide clues as to why this happens.
8217 </Para>
8219 <Sect2>
8220 <Title>Solution</Title>
8222 <Para>
8223 When you encounter sites that suffer from this problem, you can disable Path
8224 MTU discovery by setting it manually. Koos van den Hout, slightly edited,
8225 writes:
8226 </Para>
8228 <blockquote>
8229 <Para>
8230 The following problem: I set the mtu/mru of my leased line running ppp to
8231 296 because it's only 33k6 and I cannot influence the queueing on the
8232 other side. At 296, the response to a key press is within a reasonable
8233 time frame.
8234 </Para>
8236 <Para>
8237 And, on my side I have a masqrouter running (of course) Linux.
8238 </Para>
8240 <Para>
8241 Recently I split 'server' and 'router' so most applications are run on a
8242 different machine than the routing happens on.
8243 </Para>
8245 <Para>
8246 I then had trouble logging into irc. Big panic! Some digging did find
8247 out that I got connected to irc, even showed up as 'connected' on irc
8248 but I did not receive the motd from irc. I checked what could be wrong
8249 and noted that I already had some previous trouble reaching certain
8250 websites related to the MTU, since I had no trouble reaching them when
8251 the MTU was 1500, the problem just showed when the MTU was set to 296.
8252 Since irc servers block about every kind of traffic not needed for their
8253 immediate operation, they also block icmp. 
8254 </Para>
8256 <Para>
8257 I managed to convince the operators of a webserver that this was the cause
8258 of a problem, but the irc server operators were not going to fix this.
8259 </Para>
8261 <Para>
8262 So, I had to make sure outgoing masqueraded traffic started with the lower
8263 mtu of the outside link. But I want local ethernet traffic to have the
8264 normal mtu (for things like nfs traffic).
8265 </Para>
8267 <Para>Solution:
8268 </Para>
8270 <Screen width="80">
8271 ip route add default via 10.0.0.1 mtu 296
8272 </Screen>
8274 <Para>
8275 (10.0.0.1 being the default gateway, the inside address of the
8276 masquerading router)
8277 </Para>
8278 </blockquote>
8280 <Para>
8281 In general, it is possible to override PMTU Discovery by setting specific
8282 routes. For example, if only a certain subnet is giving problems, this
8283 should help:
8284 </Para>
8286 <Screen width="80">
8287 ip route add 195.96.96.0/24 via 10.0.0.1 mtu 1000
8288 </Screen>
8290 </Sect2>
8292 </Sect1>
8294 <Sect1 id="lartc.cookbook.mtu-mss">
8295   <Title>Circumventing Path MTU Discovery issues with MSS Clamping
8296   (for ADSL, cable, PPPoE &amp; PPtP users)</Title>
8298 <Para>
8299 As explained above, Path MTU Discovery doesn't work as well as it should
8300 anymore. If you know for a fact that a hop somewhere in your network has a
8301 limited (&lt;1500) MTU, you cannot rely on PMTU Discovery finding this out.
8302 </Para>
8304 <Para>
8305 Besides MTU, there is yet another way to set the maximum packet size, the so
8306 called Maximum Segment Size. This is a field in the TCP Options part of a
8307 SYN packet.
8308 </Para>
8310 <Para>
8311 Recent Linux kernels, and a few PPPoE drivers (notably, the excellent
8312 Roaring Penguin one), feature the possibility to 'clamp the MSS'. 
8313 </Para>
8315 <Para>
8316 The good thing about this is that by setting the MSS value, you are telling
8317 the remote side unequivocally 'do not ever try to send me packets bigger
8318 than this value'. No ICMP traffic is needed to get this to work.
8319 </Para>
8321 <Para>
8322 The bad thing is that it's an obvious hack - it breaks 'end to end' by
8323 modifying packets. Having said that, we use this trick in many places and it
8324 works like a charm.
8325 </Para>
8327 <Para>
8328 In order for this to work you need at least iptables-1.2.1a and Linux 2.4.3
8329 or higher. The basic command line is:
8331 <Screen>
8332 # iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS  --clamp-mss-to-pmtu
8333 </Screen>
8335 </Para>
8337 <Para>
8338 This calculates the proper MSS for your link. If you are feeling brave, or
8339 think that you know best, you can also do something like this:
8340 </Para>
8342 <Para>
8344 <Screen>
8345 # iptables -A FORWARD -p tcp --tcp-flags SYN,RST SYN -j TCPMSS --set-mss 128
8346 </Screen>
8348 </Para>
8350 <Para>
8351 This sets the MSS of passing SYN packets to 128. Use this if you have VoIP
8352 with tiny packets, and huge http packets which are causing chopping in your
8353 voice calls.
8354 </Para>
8356 </Sect1>
8358 <Sect1 id="lartc.cookbook.ultimate-tc">
8359 <Title>The Ultimate Traffic Conditioner: Low Latency, Fast Up &amp; Downloads</Title>
8361 <Para>
8362 Note: This script has recently been upgraded and previously only worked for
8363 Linux clients in your network! So you might want to update if you have
8364 Windows machines or Macs in your network and noticed that they were not able
8365 to download faster while others were uploading.
8366 </Para>
8368 <Para>
8369 I attempted to create the holy grail:
8370 <VariableList>
8372 <VarListEntry>
8373 <Term>Maintain low latency for interactive traffic at all times</Term>
8374 <ListItem>
8375 <Para>
8376 This means that downloading or uploading files should not disturb SSH or
8377 even telnet. These are the most important things, even 200ms latency is
8378 sluggish to work over.
8379 </Para></ListItem>
8380 </VarListEntry>
8381 <VarListEntry>
8382 <Term>Allow 'surfing' at reasonable speeds while up or downloading</Term>
8383 <ListItem>
8384 <Para>
8385 Even though http is 'bulk' traffic, other traffic should not drown it out 
8386 too much.
8387 </Para></ListItem>
8388 </VarListEntry>
8389 <VarListEntry>
8390 <Term>Make sure uploads don't harm downloads, and the other way around</Term>
8391 <ListItem>
8392 <Para>
8393 This is a much observed phenomenon where upstream traffic simply destroys
8394 download speed.
8395 </Para></ListItem>
8396 </VarListEntry>
8397 </VariableList>
8398 It turns out that all this is possible, at the cost of a tiny bit of
8399 bandwidth. The reason that uploads, downloads and ssh hurt each other is the
8400 presence of large queues in many domestic access devices like cable or DSL
8401 modems.
8402 </Para>
8404 <Para>
8405 The next section explains in depth what causes the delays, and how we can
8406 fix them. You can safely skip it and head straight for the script if you
8407 don't care how the magic is performed.
8408 </Para>
8410 <Sect2>
8411 <Title>Why it doesn't work well by default</Title>
8413 <Para>
8414 ISPs know that they are benchmarked solely on how fast people can download.
8415 Besides available bandwidth, download speed is influenced heavily by packet
8416 loss, which seriously hampers TCP/IP performance. Large queues can help
8417 prevent packet loss, and speed up downloads. So ISPs configure large queues.
8418 </Para>
8420 <Para>
8421 These large queues however damage interactivity. A keystroke must first
8422 travel the upstream queue, which may be seconds (!) long and go to your
8423 remote host. It is then displayed, which leads to a packet coming back, which
8424 must then traverse the downstream queue, located at your ISP, before it
8425 appears on your screen.
8426 </Para>
8428 <Para>
8429 This HOWTO teaches you how to mangle and process the queue in many ways, but
8430 sadly, not all queues are accessible to us. The queue over at the ISP is
8431 completely off-limits, whereas the upstream queue probably lives inside your
8432 cable modem or DSL device. You may or may not be able to configure it. Most
8433 probably not.
8434 </Para>
8436 <Para>
8437 So, what next? As we can't control either of those queues, they must be
8438 eliminated, and moved to your Linux router. Luckily this is possible.
8439 </Para>
8441 <Para>
8442 <VariableList>
8444 <VarListEntry>
8445 <Term>Limit upload speed</Term>
8446 <ListItem>
8447 <Para>
8448 By limiting our upload speed to slightly less than the truly available rate,
8449 no queues are built up in our modem. The queue is now moved to Linux.
8450 </Para></ListItem>
8451 </VarListEntry>
8452 <VarListEntry>
8453 <Term>Limit download speed</Term>
8454 <ListItem>
8455 <Para>
8456 This is slightly trickier as we can't really influence how fast the internet
8457 ships us data. We can however drop packets that are coming in too fast,
8458 which causes TCP/IP to slow down to just the rate we want. Because we don't 
8459 want to drop traffic unnecessarily, we configure a 'burst' size we allow at
8460 higher speed.
8461 </Para></ListItem>
8462 </VarListEntry>
8463 </VariableList>
8464 </Para>
8466 <Para>
8467 Now, once we have done this, we have eliminated the downstream queue totally
8468 (except for short bursts), and gain the ability to manage the upstream queue
8469 with all the power Linux offers.
8470 </Para>
8472 <Para>
8473 What remains to be done is to make sure interactive traffic jumps to the
8474 front of the upstream queue. To make sure that uploads don't hurt downloads,
8475 we also move ACK packets to the front of the queue. This is what normally
8476 causes the huge slowdown observed when generating bulk traffic both ways.
8477 The ACKnowledgements for downstream traffic must compete with upstream
8478 traffic, and get delayed in the process.
8479 </Para>
8481 <Para>
8482 If we do all this we get the following measurements using an excellent ADSL
8483 connection from xs4all in the Netherlands:
8484 </Para>
8486 <Para>
8488 <Screen>
8489 Baseline latency:
8490 round-trip min/avg/max = 14.4/17.1/21.7 ms
8492 Without traffic conditioner, while downloading:
8493 round-trip min/avg/max = 560.9/573.6/586.4 ms
8495 Without traffic conditioner, while uploading:
8496 round-trip min/avg/max = 2041.4/2332.1/2427.6 ms
8498 With conditioner, during 220kbit/s upload:
8499 round-trip min/avg/max = 15.7/51.8/79.9 ms
8501 With conditioner, during 850kbit/s download:
8502 round-trip min/avg/max = 20.4/46.9/74.0 ms
8504 When uploading, downloads proceed at ~80% of the available speed. Uploads
8505 at around 90%. Latency then jumps to 850 ms, still figuring out why.
8506 </Screen>
8508 </Para>
8510 <Para>
8511 What you can expect from this script depends a lot on your actual uplink
8512 speed. When uploading at full speed, there will always be a single packet
8513 ahead of your keystroke. That is the lower limit to the latency you can
8514 achieve - divide your MTU by your upstream speed to calculate. Typical
8515 values will be somewhat higher than that. Lower your MTU for better effects!
8516 </Para>
8518 <Para>
8519 Next, two versions of this script, one with Devik's excellent HTB, the other
8520 with CBQ which is in each Linux kernel, unlike HTB. Both are tested and work
8521 well.
8522 </Para>
8524 </Sect2>
8526 <Sect2>
8527 <Title>The actual script (CBQ)</Title>
8529 <Para>
8530 Works on all kernels. Within the CBQ
8531 qdisc we place two Stochastic Fairness Queues that make sure that multiple
8532 bulk streams don't drown each other out.
8533 </Para>
8535 <Para>
8536 Downstream traffic is policed using a tc filter containing a Token Bucket
8537 Filter.
8538 </Para>
8540 <Para>
8541 You might improve on this script by adding 'bounded' to the line that starts
8542 with 'tc class add .. classid 1:20'. If you lowered your MTU, also lower the
8543 allot &amp; avpkt numbers!
8544 </Para>
8546 <Para>
8548 <Screen>
8549 #!/bin/bash 
8551 # The Ultimate Setup For Your Internet Connection At Home
8554 # Set the following values to somewhat less than your actual download
8555 # and uplink speed. In kilobits
8556 DOWNLINK=800
8557 UPLINK=220
8558 DEV=ppp0
8560 # clean existing down- and uplink qdiscs, hide errors
8561 tc qdisc del dev $DEV root    2&#62; /dev/null &#62; /dev/null
8562 tc qdisc del dev $DEV ingress 2&#62; /dev/null &#62; /dev/null
8564 ###### uplink
8566 # install root CBQ
8568 tc qdisc add dev $DEV root handle 1: cbq avpkt 1000 bandwidth 10mbit 
8570 # shape everything at $UPLINK speed - this prevents huge queues in your
8571 # DSL modem which destroy latency:
8572 # main class
8574 tc class add dev $DEV parent 1: classid 1:1 cbq rate ${UPLINK}kbit \
8575 allot 1500 prio 5 bounded isolated 
8577 # high prio class 1:10:
8579 tc class add dev $DEV parent 1:1 classid 1:10 cbq rate ${UPLINK}kbit \
8580    allot 1600 prio 1 avpkt 1000
8582 # bulk and default class 1:20 - gets slightly less traffic, 
8583 #  and a lower priority:
8585 tc class add dev $DEV parent 1:1 classid 1:20 cbq rate $[9*$UPLINK/10]kbit \
8586    allot 1600 prio 2 avpkt 1000
8588 # both get Stochastic Fairness:
8589 tc qdisc add dev $DEV parent 1:10 handle 10: sfq perturb 10
8590 tc qdisc add dev $DEV parent 1:20 handle 20: sfq perturb 10
8592 # start filters
8593 # TOS Minimum Delay (ssh, NOT scp) in 1:10:
8594 tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \
8595       match ip tos 0x10 0xff  flowid 1:10
8597 # ICMP (ip protocol 1) in the interactive class 1:10 so we 
8598 # can do measurements &#38; impress our friends:
8599 tc filter add dev $DEV parent 1:0 protocol ip prio 11 u32 \
8600         match ip protocol 1 0xff flowid 1:10
8602 # To speed up downloads while an upload is going on, put ACK packets in
8603 # the interactive class:
8605 tc filter add dev $DEV parent 1: protocol ip prio 12 u32 \
8606    match ip protocol 6 0xff \
8607    match u8 0x05 0x0f at 0 \
8608    match u16 0x0000 0xffc0 at 2 \
8609    match u8 0x10 0xff at 33 \
8610    flowid 1:10
8612 # rest is 'non-interactive' ie 'bulk' and ends up in 1:20
8614 tc filter add dev $DEV parent 1: protocol ip prio 13 u32 \
8615    match ip dst 0.0.0.0/0 flowid 1:20
8617 ########## downlink #############
8618 # slow downloads down to somewhat less than the real speed  to prevent 
8619 # queuing at our ISP. Tune to see how high you can set it.
8620 # ISPs tend to have *huge* queues to make sure big downloads are fast
8622 # attach ingress policer:
8624 tc qdisc add dev $DEV handle ffff: ingress
8626 # filter *everything* to it (0.0.0.0/0), drop everything that's
8627 # coming in too fast:
8629 tc filter add dev $DEV parent ffff: protocol ip prio 50 u32 match ip src \
8630    0.0.0.0/0 police rate ${DOWNLINK}kbit burst 10k drop flowid :1
8631 </Screen>
8633 If you want this script to be run by ppp on connect, copy it to
8634 /etc/ppp/ip-up.d.
8635 </Para>
8637 <Para>
8638 If the last two lines give an error, update your tc tool to a newer version!
8639 </Para>
8641 </Sect2>
8643 <Sect2>
8644 <Title>The actual script (HTB)</Title>
8646 <Para>
8647 The following script achieves all goals using the wonderful HTB queue, see
8648 the relevant chapter. Well worth patching your kernel for! 
8650 <Screen>
8651 #!/bin/bash
8653 # The Ultimate Setup For Your Internet Connection At Home
8656 # Set the following values to somewhat less than your actual download
8657 # and uplink speed. In kilobits
8658 DOWNLINK=800
8659 UPLINK=220
8660 DEV=ppp0
8662 # clean existing down- and uplink qdiscs, hide errors
8663 tc qdisc del dev $DEV root    2&#62; /dev/null &#62; /dev/null
8664 tc qdisc del dev $DEV ingress 2&#62; /dev/null &#62; /dev/null
8666 ###### uplink
8668 # install root HTB, point default traffic to 1:20:
8670 tc qdisc add dev $DEV root handle 1: htb default 20
8672 # shape everything at $UPLINK speed - this prevents huge queues in your
8673 # DSL modem which destroy latency:
8675 tc class add dev $DEV parent 1: classid 1:1 htb rate ${UPLINK}kbit burst 6k
8677 # high prio class 1:10:
8679 tc class add dev $DEV parent 1:1 classid 1:10 htb rate ${UPLINK}kbit \
8680    burst 6k prio 1
8682 # bulk &#38; default class 1:20 - gets slightly less traffic, 
8683 # and a lower priority:
8685 tc class add dev $DEV parent 1:1 classid 1:20 htb rate $[9*$UPLINK/10]kbit \
8686    burst 6k prio 2
8688 # both get Stochastic Fairness:
8689 tc qdisc add dev $DEV parent 1:10 handle 10: sfq perturb 10
8690 tc qdisc add dev $DEV parent 1:20 handle 20: sfq perturb 10
8692 # TOS Minimum Delay (ssh, NOT scp) in 1:10:
8693 tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \
8694       match ip tos 0x10 0xff  flowid 1:10
8696 # ICMP (ip protocol 1) in the interactive class 1:10 so we 
8697 # can do measurements &#38; impress our friends:
8698 tc filter add dev $DEV parent 1:0 protocol ip prio 10 u32 \
8699         match ip protocol 1 0xff flowid 1:10
8701 # To speed up downloads while an upload is going on, put ACK packets in
8702 # the interactive class:
8704 tc filter add dev $DEV parent 1: protocol ip prio 10 u32 \
8705    match ip protocol 6 0xff \
8706    match u8 0x05 0x0f at 0 \
8707    match u16 0x0000 0xffc0 at 2 \
8708    match u8 0x10 0xff at 33 \
8709    flowid 1:10
8711 # rest is 'non-interactive' ie 'bulk' and ends up in 1:20
8714 ########## downlink #############
8715 # slow downloads down to somewhat less than the real speed  to prevent 
8716 # queuing at our ISP. Tune to see how high you can set it.
8717 # ISPs tend to have *huge* queues to make sure big downloads are fast
8719 # attach ingress policer:
8721 tc qdisc add dev $DEV handle ffff: ingress
8723 # filter *everything* to it (0.0.0.0/0), drop everything that's
8724 # coming in too fast:
8726 tc filter add dev $DEV parent ffff: protocol ip prio 50 u32 match ip src \
8727    0.0.0.0/0 police rate ${DOWNLINK}kbit burst 10k drop flowid :1
8728 </Screen>
8730 </Para>
8732 <Para>
8733 If you want this script to be run by ppp on connect, copy it to
8734 /etc/ppp/ip-up.d.
8735 </Para>
8737 <Para>
8738 If the last two lines give an error, update your tc tool to a newer version!
8739 </Para>
8741 </Sect2>
8743     </Sect1>
8744     <sect1 id="lartc.ratelimit.single"><title>Rate limiting a single host or netmask</title>
8745       <para>
8746         Although this is described in stupendous details elsewhere and in our manpages, this question gets asked a lot and 
8747         happily there is a simple answer that does not need full comprehension of traffic control.
8748       </para>
8749       <para>
8750         This three line script does the trick:
8751       </para>
8752       <para>
8753         <screen>
8754           tc qdisc add dev $DEV root handle 1: cbq avpkt 1000 bandwidth 10mbit 
8756           tc class add dev $DEV parent 1: classid 1:1 cbq rate 512kbit \
8757           allot 1500 prio 5 bounded isolated 
8759           tc filter add dev $DEV parent 1: protocol ip prio 16 u32 \
8760           match ip dst 195.96.96.97 flowid 1:1
8761         </screen>
8762       </para>
8763       <para>
8764         The first line installs a class based queue on your interface, and tells the kernel that for calculations,
8765         it can be assumed to be a 10mbit interface. If you get this wrong, no real harm is done. But getting it right will 
8766         make everything more precise.
8767       </para>
8768       <para>
8769         The second line creates a 512kbit class with some reasonable defaults. For details, see the cbq manpages and
8770         <xref linkend="lartc.qdisc">.
8771       </para>
8772       <para>
8773         The last line tells which traffic should go to the shaped class. Traffic not matched by this rule is NOT shaped. To make more 
8774         complicated matches (subnets, source ports, destination ports), see <xref linkend="lartc.filtering.simple">.
8775       </para>
8776       <para>
8777         If you changed anything and want to reload the script, execute 'tc qdisc del dev $DEV root' to clean up your existing
8778         configuration.
8779       </para>
8780       <para>
8781         The script can further be improved by adding a last optional line 'tc qdisc add dev $DEV parent 1:1 sfq perturb 10'. See 
8782         <xref linkend="lartc.sfq"> for details on what this does.
8783       </para>
8784     </sect1>
8785         
8786         <sect1 id="lartc.cookbook.fullnat.intro"><title>Example of a full nat solution with QoS</title>
8787                 <para>
8788                         I'm Pedro Larroy <address><email>piotr@omega.resa.es</email></address>. Here I'm describing a common set up where we have lots of users in a private network connected to the Internet trough a Linux router with a public ip address that is doing network address translation (NAT). I use this QoS setup to give access to the Internet to 198 users in a university dorm, in which I live and I'm netadmin of. The users here do heavy use of peer to peer programs, so proper traffic control is a must. I hope this serves as a practical example for all interested lartc readers.
8789                 </para>
8791                 <para>
8792                         At first I make a practical approach with step by step configuration, and in the end I explain how to make the process automatic at bootime. The network to which this example applies is a private LAN connected to the Internet through a Linux router which has one public ip address. Extending it to several public ip address should be very easy, a couple of iptables rules should be added.
8793                         In order to get things working we need:
8794                         <VariableList>
8795                                 <VarListEntry>
8796                                         <Term>Linux 2.4.18 or higher kernel version installed</Term>
8797                                         <ListItem>
8798                                         <Para>
8799                                         If you use 2.4.18 you will have to apply HTB patch available here.
8800                                         </Para></ListItem>
8801                                 </VarListEntry>
8803                                 <VarListEntry>
8804                                         <Term>iproute</Term>
8805                                         <ListItem>
8806                                         <Para>
8807                                         Also ensure the "tc" binary is HTB ready, a precompiled binary is distributed with HTB.
8808                                         </Para></ListItem>
8809                                 </VarListEntry>
8811                                 <VarListEntry>
8812                                         <Term>iptables</Term>
8813                                         <ListItem>
8814                                         <Para>
8815                                         </Para></ListItem>
8816                                 </VarListEntry>
8817                         </VariableList>
8819                 </para>
8822         <sect2>
8823         <Title>Let's begin optimizing that scarce bandwidth</Title>
8824                 <para>
8825                         First we set up some qdiscs in which we will classify the traffic. We create a htb qdisc with 6 classes with ascending priority. Then we have classes that will always get allocated rate, but can use the unused bandwidth that other classes don't need. Recall that classes with higher priority ( i.e with a lower prio number ) will get excess of bandwith allocated first. Our connection is 2Mb down 300kbits/s up Adsl. I use 240kbit/s as ceil rate just because it's the higher I can set it before latency starts to grow, due to buffer filling in whatever place between us and remote hosts. This parameter should be timed experimentally, raising it and lowering while observing latency between some near hosts.
8826                 </para>
8827                 <para>
8828                         Adjust CEIL to 75% of your upstream bandwith limit by now, and where I use eth0, you should use the interface which has a public Internet address. To begin our example execute the following in a root shell:
8829                         <Screen>
8830                         CEIL=240
8831                         tc qdisc add dev eth0 root handle 1: htb default 15
8832                         tc class add dev eth0 parent 1: classid 1:1 htb rate ${CEIL}kbit ceil ${CEIL}kbit
8833                         tc class add dev eth0 parent 1:1 classid 1:10 htb rate 80kbit ceil 80kbit prio 0
8834                         tc class add dev eth0 parent 1:1 classid 1:11 htb rate 80kbit ceil ${CEIL}kbit prio 1
8835                         tc class add dev eth0 parent 1:1 classid 1:12 htb rate 20kbit ceil ${CEIL}kbit prio 2
8836                         tc class add dev eth0 parent 1:1 classid 1:13 htb rate 20kbit ceil ${CEIL}kbit prio 2
8837                         tc class add dev eth0 parent 1:1 classid 1:14 htb rate 10kbit ceil ${CEIL}kbit prio 3
8838                         tc class add dev eth0 parent 1:1 classid 1:15 htb rate 30kbit ceil ${CEIL}kbit prio 3
8839                         tc qdisc add dev eth0 parent 1:12 handle 120: sfq perturb 10
8840                         tc qdisc add dev eth0 parent 1:13 handle 130: sfq perturb 10
8841                         tc qdisc add dev eth0 parent 1:14 handle 140: sfq perturb 10
8842                         tc qdisc add dev eth0 parent 1:15 handle 150: sfq perturb 10
8843                         </Screen>
8844                         We have just created a htb tree with one level depth. Something like this:
8846                         <Screen>
8847                         +---------+
8848                         | root 1: |
8849                         +---------+
8850                              |
8851                         +---------------------------------------+
8852                         | class 1:1                             |
8853                         +---------------------------------------+
8854                           |      |      |      |      |      |      
8855                         +----+ +----+ +----+ +----+ +----+ +----+
8856                         |1:10| |1:11| |1:12| |1:13| |1:14| |1:15| 
8857                         +----+ +----+ +----+ +----+ +----+ +----+ 
8858                         </Screen>
8859                         
8860                         <VariableList>
8861                                 <VarListEntry>
8862                                         <Term>classid 1:10 htb rate 80kbit ceil 80kbit prio 0</Term>
8863                                         <ListItem>
8864                                         <Para>
8865                                         This is the higher priority class. The packets in this class will have the lowest delay and would get the excess of bandwith first so it's a good idea to limit the ceil rate to this class. We will send through this class the following packets that benefit from low delay, such as interactive traffic: <emphasis>ssh, telnet, dns, quake3, irc, and packets with the SYN flag</emphasis>.
8866                                         </Para>
8867                                         </ListItem>
8868                                 </VarListEntry>
8870                                 <VarListEntry>
8871                                         <Term>classid 1:11 htb rate 80kbit ceil ${CEIL}kbit prio 1</Term>
8872                                         <ListItem>
8873                                         <Para>
8874                                         Here we have the first class in which we can start to put bulk traffic. In my example I have traffic from the local web server and requests for web pages: source port 80, and destination port 80 respectively.
8875                                         </Para></ListItem>
8876                                 </VarListEntry>
8878                                 <VarListEntry>
8879                                         <Term>classid 1:12 htb rate 20kbit ceil ${CEIL}kbit prio 2</Term>
8880                                         <ListItem>
8881                                         <Para>
8882                                         In this class I will put traffic with Maximize-Throughput TOS bit set and the rest of the traffic that goes from <emphasis>local processes</emphasis> on the router to the Internet. So the following classes will only have traffic that is <quote>routed through</quote> the box.
8883                                         </Para></ListItem>
8884                                 </VarListEntry>
8885                                 
8886                                 <VarListEntry>
8887                                         <Term>classid 1:13 htb rate 20kbit ceil ${CEIL}kbit prio 2</Term>
8888                                         <ListItem>
8889                                         <Para>
8890                                         This class is for the traffic of other NATed machines that need higher priority in their bulk traffic.
8891                                         </Para></ListItem>
8892                                 </VarListEntry>
8895                                 <VarListEntry>
8896                                         <Term>classid 1:14 htb rate 10kbit ceil ${CEIL}kbit prio 3</Term>
8897                                         <ListItem>
8898                                         <Para>
8899                                         Here goes mail traffic (SMTP,pop3...) and packets with Minimize-Cost TOS bit set.
8900                                         </Para></ListItem>
8901                                 </VarListEntry>
8903                                 <VarListEntry>
8904                                         <Term>classid 1:15 htb rate 30kbit ceil ${CEIL}kbit prio 3</Term>
8905                                         <ListItem>
8906                                         <Para>
8907                                         And finally here we have bulk traffic from the NATed machines behind the router. All kazaa, edonkey, and others will go here, in order to not interfere with other services.
8908                                         </Para></ListItem>
8909                                 </VarListEntry>
8911                         </VariableList>
8916                 </para>
8917         </sect2>
8918         
8919         <sect2>
8920         <Title>Classifying packets</Title>
8921                 <para>
8922                         We have created the qdisc setup but no packet classification has been made, so now all outgoing packets are going out in class 1:15 ( because we used: tc qdisc add dev eth0 root handle 1: htb <emphasis>default 15</emphasis> ). Now we need to tell which packets go where. This is the most important part.
8923                 </para>
8925                 <para>
8926                         Now we set the filters so we can classify the packets with iptables. I really prefer to do it with iptables, because they are very flexible and you have packet count for each rule. Also with the RETURN target packets don't need to traverse all rules. We execute the following commands:
8927                         <Screen>
8928                         tc filter add dev eth0 parent 1:0 protocol ip prio 1 handle 1 fw classid 1:10
8929                         tc filter add dev eth0 parent 1:0 protocol ip prio 2 handle 2 fw classid 1:11
8930                         tc filter add dev eth0 parent 1:0 protocol ip prio 3 handle 3 fw classid 1:12
8931                         tc filter add dev eth0 parent 1:0 protocol ip prio 4 handle 4 fw classid 1:13
8932                         tc filter add dev eth0 parent 1:0 protocol ip prio 5 handle 5 fw classid 1:14
8933                         tc filter add dev eth0 parent 1:0 protocol ip prio 6 handle 6 fw classid 1:15
8934                         </Screen>
8935                         We have just told the kernel that packets that has a specific FWMARK value ( hanlde x fw ) go in the specified class ( classid x:x). Next you will see how to mark packets with iptables. 
8936                 </para>
8937                 
8938                 <para>
8939                         First you have to understand how packet traverse the filters with iptables:
8940                         <Screen>
8941                                 +------------+                +---------+               +-------------+
8942                         Packet -| PREROUTING |--- routing-----| FORWARD |-------+-------| POSTROUTING |- Packets
8943                         input   +------------+    decision    +-­-------+       |       +-------------+    out
8944                                                      |                          |
8945                                                 +-------+                    +--------+   
8946                                                 | INPUT |---- Local process -| OUTPUT |
8947                                                 +-------+                    +--------+
8948                         
8949                         </Screen>
8950                         I assume you have all your tables creak and with default policy ACCEPT ( -P ACCEPT ) if you haven't poked with iptables yet, It should be ok by default. Ours private network is a class B with address 172.17.0.0/16 and public ip is 212.170.21.172
8951                 </para>
8953                 <para>
8954                         Next we instruct the kernel to <emphasis>actually do NAT</emphasis>, so clients in the private network can start talking to the outside. 
8955                         <Screen>
8956                         echo 1 > /proc/sys/net/ipv4/ip_forward
8957                         iptables -t nat -A POSTROUTING -s 172.17.0.0/255.255.0.0 -o eth0 -j SNAT --to-source 212.170.21.172
8958                         </Screen>
8959                         Now check that packets are flowing through 1:15:
8960                         <Screen>
8961                         tc -s class show dev eth0
8962                         </Screen>
8963                 </para>
8965                 <para>
8966                         You can start marking packets adding rules to the PREROUTING chain in the mangle table.
8967                         <Screen>
8968                         iptables -t mangle -A PREROUTING -p icmp -j MARK --set-mark 0x1
8969                         iptables -t mangle -A PREROUTING -p icmp -j RETURN
8970                         </Screen>
8971                         Now you should be able to see packet count increasing when pinging from machines within the private network to some site on the Internet. Check packet count increasing in 1:10
8972                         <Screen>
8973                         tc -s class show dev eth0
8974                         </Screen>
8975                         We have done a -j RETURN so packets don't traverse all rules. Icmp packets won't match other rules below RETURN. Keep that in mind.
8976                         Now we can start adding more rules, lets do proper TOS handling:
8977                         <Screen>
8978                         iptables -t mangle -A PREROUTING -m tos --tos Minimize-Delay -j MARK --set-mark 0x1
8979                         iptables -t mangle -A PREROUTING -m tos --tos Minimize-Delay -j RETURN
8980                         iptables -t mangle -A PREROUTING -m tos --tos Minimize-Cost -j MARK --set-mark 0x5
8981                         iptables -t mangle -A PREROUTING -m tos --tos Minimize-Cost -j RETURN
8982                         iptables -t mangle -A PREROUTING -m tos --tos Maximize-Throughput -j MARK --set-mark 0x6
8983                         iptables -t mangle -A PREROUTING -m tos --tos Maximize-Throughput -j RETURN
8984                         </Screen>
8986                         Now prioritize ssh packets:
8987                         <Screen>
8988                         iptables -t mangle -A PREROUTING -p tcp -m tcp --sport 22 -j MARK --set-mark 0x1
8989                         iptables -t mangle -A PREROUTING -p tcp -m tcp --sport 22 -j RETURN
8990                         </Screen>
8991                         A good idea is to prioritize packets to begin tcp connections, those with SYN flag set:
8992                         <Screen>
8993                         iptables -t mangle -I PREROUTING -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -j MARK --set-mark 0x1
8994                         iptables -t mangle -I PREROUTING -p tcp -m tcp --tcp-flags SYN,RST,ACK SYN -j RETURN
8995                         </Screen>
8996                         And so on.
8997                         
8998                         When we are done adding rules to PREROUTING in mangle, we terminate the PREROUTING table with:
8999                         <Screen>
9000                         iptables -t mangle -A PREROUTING -j MARK --set-mark 0x6
9001                         </Screen>
9002                         So previously unmarked traffic goes in 1:15. In fact this last step is unnecessary since default class was 1:15, but I will mark them in order to be consistent with the whole setup, and furthermore it's useful to see the counter in that rule.
9003                 </para>
9004                 
9005                 <para>
9006                         It will be a good idea to do the same in the OUTPUT rule, so repeat those commands with -A OUTPUT instead of PREROUTING. ( s/PREROUTING/OUTPUT/ ). Then traffic generated locally (on the Linux router) will also be classified. I finish OUTPUT chain with -j MARK --set-mark 0x3 so local traffic has higher priority. 
9007                 </para>
9009         </sect2>
9010                 
9011         <sect2>
9012         <Title>Improving our setup</Title>
9013                 <para>
9014                         Now we have all our setup working. Take time looking at the graphs, and watching where your bandwith is spent and how do you want it. Doing that for lots of hours, I finally got the Internet connection working really well. Otherwise continuous timeouts and nearly zero allotment of bandwith to newly created tcp connections will occur.
9015                 </para>
9016                 <para>
9017                         If you find that some classes are full most of the time it would be a good idea to attach another queueing discipline to them so bandwith sharing is more fair:
9018                         <Screen>
9019                         tc qdisc add dev eth0 parent 1:13 handle 130: sfq perturb 10
9020                         tc qdisc add dev eth0 parent 1:14 handle 140: sfq perturb 10
9021                         tc qdisc add dev eth0 parent 1:15 handle 150: sfq perturb 10
9022                         </Screen>
9023                 </para>
9025         <sect2>
9026         <Title>Making all of the above start at boot</Title>
9027                 <para>
9028                         It sure can be done in many ways. In mine, I have a shell script in /etc/init.d/packetfilter that accepts [start | stop | stop-tables | start-tables | reload-tables] it configures qdiscs and loads needed kernel modules, so it behaves much like a daemon. The same script loads iptables rules from /etc/network/iptables-rules. I will beautify it a little and will make it available on my web page <ULink URL="http://omega.resa.es/piotr/files/packetfilter.tar.bz2">here</ULink> 
9029                         
9030                 </para>
9031         
9032         </sect1>
9034 </chapter>
9038 <chapter id="lartc.bridging">
9039   <Title>Building bridges, and pseudo-bridges with Proxy ARP</Title>
9041 <Para>
9042 Bridges are devices which can be installed in a network without any
9043 reconfiguration. A network switch is basically a many-port bridge. A bridge
9044 is often a 2-port switch. Linux does however support multiple interfaces in
9045 a bridge, making it a true switch.
9046 </Para>
9048 <Para>
9049 Bridges are often deployed when confronted with a broken network that needs
9050 to be fixed without any alterations. Because the bridge is a layer-2 device,
9051 one layer below IP, routers and servers are not aware of its existence.
9052 This means that you can transparently block or modify certain packets, or do
9053 shaping.
9054 </Para>
9056 <Para>
9057 Another good thing is that a bridge can often be replaced by a cross cable
9058 or a hub, should it break down.
9059 </Para>
9061 <Para>
9062 The bad news is that a bridge can cause great confusion unless it is very
9063 well documented. It does not appear in traceroutes, but somehow packets
9064 disappear or get changed from point A to point B ('this network is
9065 HAUNTED!'). You should also wonder if an organization that 'does not want to
9066 change anything' is doing the right thing.
9067 </Para>
9069 <Para>
9070 The Linux 2.4/2.5 bridge is documented on
9071 <ULink URL=" http://bridge.sourceforge.net/">this page</ULink>.
9072 </Para>
9074 <Sect1 id="lartc.bridging.iptables">
9075   <Title>State of bridging and iptables</Title>
9077 <Para>
9078 As of Linux 2.4.20, bridging and iptables do not 'see' each other without
9079 help. If you bridge packets from eth0 to eth1, they do not 'pass' by
9080 iptables. This means that you cannot do filtering, or NAT or mangling or
9081 whatever. In Linux 2.5.45 and higher, this is fixed.
9082 </Para>
9083 <para>
9084 You may also see 'ebtables' mentioned which is yet another project - it
9085 allows you to do wild things as MACNAT and 'brouting'. It is truly scary.
9086 </Sect1>
9088 <Sect1 id="lartc.bridging.shaping">
9089   <Title>Bridging and shaping</Title>
9091 <Para>
9092 This does work as advertised. Be sure to figure out which side each
9093 interface is on, otherwise you might be shaping outbound traffic in your
9094 internal interface, which won't work. Use tcpdump if needed.
9095 </Para>
9097 </Sect1>
9099 <Sect1 id="lartc.bridging.proxy-arp">
9100   <Title>Pseudo-bridges with Proxy-ARP</Title>
9102 <Para>
9103 If you just want to implement a Pseudo-bridge, skip down a few sections 
9104 to 'Implementing it', but it is wise to read a bit about how it works in
9105 practice.
9106 </Para>
9108 <Para>
9109 A Pseudo-bridge works a bit differently. By default, a bridge passes packets
9110 unaltered from one interface to the other. It only looks at the hardware
9111 address of packets to determine what goes where. This in turn means that you
9112 can bridge traffic that Linux does not understand, as long as it has an
9113 hardware address it does.
9114 </Para>
9116 <Para>
9117 A 'Pseudo-bridge' works differently and looks more like a hidden router than
9118 a bridge, but like a bridge, it has little impact on network design.
9119 </Para>
9121 <Para>
9122 An advantage of the fact that it is not a bridge lies in the fact that
9123 packets really pass through the kernel, and can be filtered, changed,
9124 redirected or rerouted. 
9125 </Para>
9127 <Para>
9128 A real bridge can also be made to perform these feats, but it needs special
9129 code, like the Ethernet Frame Diverter, or the above mentioned patch.
9130 </Para>
9132 <Para>
9133 Another advantage of a pseudo-bridge is that it does not pass packets it
9134 does not understand - thus cleaning your network of a lot of cruft. In cases
9135 where you need this cruft (like SAP packets, or Netbeui), use a real bridge.
9136 </Para>
9138 <Sect2>
9139 <Title>ARP &amp; Proxy-ARP</Title>
9141 <Para>
9142 When a host wants to talk to another host on the same physical network
9143 segment, it sends out an Address Resolution Protocol packet, which, somewhat
9144 simplified, reads like this 'who has 10.0.0.1, tell 10.0.0.7'. In response
9145 to this, 10.0.0.1 replies with a short 'here' packet.
9146 </Para>
9148 <Para>
9149 10.0.0.7 then sends packets to the hardware address mentioned in the 'here' 
9150 packet. It caches this hardware address for a relatively long time, and
9151 after the cache expires, it re-asks the question.
9152 </Para>
9154 <Para>
9155 When building a Pseudo-bridge, we instruct the bridge to reply to these ARP
9156 packets, which causes the hosts in the network to send its packets to the
9157 bridge. The bridge then processes these packets, and sends them to the
9158 relevant interface.
9159 </Para>
9161 <Para>
9162 So, in short, whenever a host on one side of the bridge asks for the
9163 hardware address of a host on the other, the bridge replies with a packet
9164 that says 'hand it to me'.
9165 </Para>
9167 <Para>
9168 This way, all data traffic gets transmitted to the right place, and always
9169 passes through the bridge.
9170 </Para>
9172 </Sect2>
9174 <Sect2>
9175 <Title>Implementing it</Title>
9177 <Para>
9178 In the bad old days, it used to be possible to instruct the Linux Kernel to
9179 perform 'proxy-ARP' for just any subnet. So, to configure a pseudo-bridge,
9180 you would have to specify both the proper routes to both sides of the bridge
9181 AND create matching proxy-ARP rules. This is bad in that it requires a lot
9182 of typing, but also because it easily allows you to make mistakes which make
9183 your bridge respond to ARP queries for networks it does not know how to
9184 route.
9185 </Para>
9187 <Para>
9188 With Linux 2.4/2.5 (and possibly 2.2), this possibility has been withdrawn and
9189 has been replaced by a flag in the /proc directory, called 'proxy_arp'. The
9190 procedure for building a pseudo-bridge is then:
9191 </Para>
9193 <Para>
9195 <OrderedList>
9196 <ListItem>
9198 <Para>
9199 Assign an IP address to both interfaces, the 'left' and the 'right'
9201 </Para>
9202 </ListItem>
9203 <ListItem>
9205 <Para>
9206 Create routes so your machine knows which hosts reside on the left,
9207 and which on the right
9208 </Para>
9209 </ListItem>
9210 <ListItem>
9212 <Para>
9213 Turn on proxy-ARP on both interfaces, echo 1 &#62;
9214 /proc/sys/net/ipv4/conf/ethL/proxy_arp, echo 1 &#62;
9215 /proc/sys/net/ipv4/conf/ethR/proxy_arp, where L and R stand for the numbers
9216 of your interfaces on the left and on the right side
9217 </Para>
9218 </ListItem>
9220 </OrderedList>
9222 </Para>
9224 <Para>
9225 Also, do not forget to turn on the ip_forwarding flag! When converting from
9226 a true bridge, you may find that this flag was turned off as it is not
9227 needed when bridging.
9228 </Para>
9230 <Para>
9231 Another thing you might note when converting is that you need to clear the
9232 arp cache of computers in the network - the arp cache might contain old
9233 pre-bridge hardware addresses which are no longer correct.
9234 </Para>
9236 <Para>
9237 On a Cisco, this is done using the command 'clear arp-cache', under
9238 Linux, use 'arp -d ip.address'. You can also wait for the cache to expire
9239 manually, which can take rather long.
9240 </Para>
9241 <para>
9242 You can speed this up using the wonderful 'arping' tool, which on many
9243 distributions is part of the 'iputils' package. Using 'arping' you can send
9244 out unsolicited ARP messages so as to update remote arp caches. 
9245 </para>
9246 <para>
9247 This is a very powerful technique that is also used by 'black hats' to
9248 subvert your routing!
9249 </para>
9250 <note>
9251 <para>
9252 On Linux 2.4, you may need to execute 
9253 'echo 1 &gt; /proc/sys/net/ipv4/ip_nonlocal_bind' before being able to send
9254 out unsolicited ARP messages!
9255 </para>
9256 </note>
9257 <Para>
9258 You may also discover that your network was misconfigured if you are/were of
9259 the habit of specifying routes without netmasks. To explain, some versions
9260 of route may have guessed your netmask right in the past, or guessed wrong
9261 without you noticing. When doing surgical routing like described above, it
9262 is *vital* that you check your netmasks! 
9263 </Para>
9265 </Sect2>
9267 </Sect1>
9269 </chapter>
9271 <chapter id="lartc.dynamic-routing">
9272 <Title>Dynamic routing - OSPF and BGP</Title>
9274 <Para>
9275 Once your network starts to get really big, or you start to consider 'the
9276 internet' as your network, you need tools which dynamically route your data.
9277 Sites are often connected to each other with multiple links, and more are
9278 popping up all the time. 
9279 </Para>
9281 <Para>
9282 The Internet has mostly standardized on OSPF and BGP4 (rfc1771).
9283 Linux supports both, by way of <application>gated</application> and 
9284 <application>zebra</application>
9285 </Para>
9287 <Para>
9288 While currently not within the scope of this document, we would like to
9289 point you to the definitive works:
9290 </Para>
9292 <Para>
9293 Overview:
9294 </Para>
9296 <Para>
9297 Cisco Systems
9298 <ULink
9299 URL="http://www.cisco.com/univercd/cc/td/doc/cisintwk/idg4/nd2003.htm"
9300 >Designing large-scale IP Internetworks</ULink
9302 </Para>
9304 <Para>
9305 For OSPF:
9306 </Para>
9308 <Para>
9309 Moy, John T.
9310 "OSPF.  The anatomy of an Internet routing protocol"
9311 Addison Wesley. Reading, MA. 1998.
9312 </Para>
9314 <Para>
9315 Halabi has also written a good guide to OSPF routing design, but this
9316 appears to have been dropped from the Cisco web site.
9317 </Para>
9319 <Para>
9320 For BGP:
9321 </Para>
9323 <Para>
9324 Halabi, Bassam
9325 "Internet routing architectures"
9326 Cisco Press (New Riders Publishing). Indianapolis, IN. 1997.
9327 </Para>
9329 <Para>
9330 also
9331 </Para>
9333 <Para>
9334 Cisco Systems
9335 </Para>
9337 <Para>
9338 <ULink
9339 URL="http://www.cisco.com/univercd/cc/td/doc/cisintwk/ics/icsbgp4.htm"
9340 >Using the Border Gateway Protocol for interdomain routing</ULink
9342 </Para>
9344 <Para>
9345 Although the examples are Cisco-specific, they are remarkably similar
9346 to the configuration language in Zebra :-)
9347 </Para>
9349 <Sect1 id="lartc.dynamic-routing.ospf">
9350         <sect1info>
9351                 <author>
9352                 <FirstName>Pedro</FirstName><Surname>Larroy Tovar</Surname>
9353                 <affiliation>
9354                 <address>
9355                 <email>piotr@omega.resa.es</email>
9356                 </address>
9357                 </affiliation>
9358                 </author>
9359         </sect1info><Title>Setting up OSPF with Zebra</Title>
9361         <para>
9362                 Please, let <ulink url="mailto:piotr@omega.resa.es">me</ulink> know if any of the following information is not accurate or if you have any suggestions.
9363                 <ulink url="http://www.zebra.org">Zebra</ulink> is a great dynamic routing software written by Kunihiro Ishiguro, Toshiaki Takada and Yasuhiro Ohara. With Zebra, setting up OSPF is fast an simple, but in practice there's a lot of parameters to tune if you have very specific needs. OSPF stands for Open Shortest Path First, and some of its principal features are:
9364                 <VariableList>
9365                         <VarListEntry>
9366                                 <Term>Hierachical</Term>
9367                                 <ListItem>
9368                                 <Para>
9369                                 Networks are grouped by <emphasis>areas</emphasis>, which are interconnected by a <emphasis>backbone area</emphasis> which will be designated as <emphasis>area 0</emphasis>. All traffic goes through area 0, and all the routers in area 0 have routing information about all the other areas.
9370                                 </Para>
9371                                 </ListItem>
9372                         </VarListEntry>
9374                         <VarListEntry>
9375                                 <Term>Short convergence</Term>
9376                                 <ListItem>
9377                                 <Para>
9378                                 Routes are propagated very fast, compared with RIP, for example.
9379                                 </Para></ListItem>
9380                         </VarListEntry>
9381                         
9382                         <VarListEntry>
9383                                 <Term>Bandwith efficient</Term>
9384                                 <ListItem>
9385                                 <Para>
9386                                 Uses multicasting instead of broadcasting, so it doesn't flood other hosts with routing information that may not be of interest for them, thus reducing network overhead. Also, <emphasis>Internal Routers</emphasis> (those which only have interfaces in one area) Don't have routing information about other areas. Routers with interfaces in more than one area are called <emphasis>Area Border Routers</emphasis>, and hold topological information about the areas they are connected to.
9387                                 </Para></ListItem>
9388                         </VarListEntry>
9390                         <VarListEntry>
9391                                 <Term>Cpu intensive</Term>
9392                                 <ListItem>
9393                                 <Para>
9394                                 OSPF is based on Dijkstra's <ulink url="http://www.soi.wide.ad.jp/class/99007/slides/13/07.html">Shortest Path First algorithm</ulink>, which is expensive compared to other routing algorithms. But really is not that bad, since the Shortest Path is only calculed for each area, also for small to medium sized networks this won't be an issue, and you won't even notice.
9395                                 </Para></ListItem>
9396                         </VarListEntry>
9397                         
9398                         <VarListEntry>
9399                                 <Term>Link state</Term>
9400                                 <ListItem>
9401                                 <Para>
9402                                 OSPF counts with the special characteristics of networks and interfaces, such as bandwith, link failures, and monetary cost.
9403                                 </Para></ListItem>
9404                         </VarListEntry>
9405                         
9406                         <VarListEntry>
9407                                 <Term>Open protocol and GPLed software</Term>
9408                                 <ListItem>
9409                                 <Para>
9410                                 OSPF is an open protocol, and Zebra is GPL software, which has obvious advantages over propietary software and protocols.
9411                                 </Para></ListItem>
9412                         </VarListEntry>
9414                 </VariableList>
9415   </para>
9416   
9417         <Sect2 id="lartc.dynamic-routing.ospf.prereq">
9418         <Title>Prerequisites</Title>
9419                 <para>
9421                         <VariableList>
9422                                 
9423                                 <VarListEntry>
9424                                         <Term>Linux Kernel:</Term>
9425                                         <ListItem>
9426                                         <Para>
9427                                         Compiled with CONFIG_NETLINK_DEV and CONFIG_IP_MULTICAST (I am not sure if anything more is also needed).
9428                                         </Para></ListItem>
9429                                 </VarListEntry>
9430                                 
9431                                 <VarListEntry>
9432                                         <Term>Iproute</Term>
9433                                         <ListItem>
9434                                         <Para>
9435                                         </Para></ListItem>
9436                                 </VarListEntry>
9437                                 
9438                                 <VarListEntry>
9439                                         <Term>Zebra</Term>
9440                                         <ListItem>
9441                                         <Para>
9442                                         Get it with your favorite package manager or from <ulink url="http://www.zebra.org">http://www.zebra.org</ulink>.
9443                                         </Para></ListItem>
9444                                 </VarListEntry>
9445                         </VariableList>
9446                 </para>
9447         </sect2>  
9448         <Sect2 id="lartc.dynamic-routing.ospf.zebracfg">
9449         <Title>Configuring Zebra</Title>
9450                 <para>
9451                         Let's take this network as an example:
9452                         <screen>
9453                         ----------------------------------------------------
9454                         | 192.168.0.0/24                                   |
9455                         |                                                  |
9456                         |      Area 0    100BaseTX Switched                |
9457                         |     Backbone     Ethernet                        |
9458                         ----------------------------------------------------
9459                           |           |                |              |
9460                           |           |                |              |
9461                           |eth1       |eth1            |eth0          |
9462                           |100BaseTX  |100BaseTX       |100BaseTX     |100BaseTX
9463                           |.1         |.2              |.253          |
9464                          ---------   ------------   -----------      ----------------
9465                          |R Omega|   |R Atlantis|   |R Legolas|      |R Frodo       |
9466                          ---------   ------------   -----------      ----------------
9467                           |eth0         |eth0             |             |          |
9468                           |             |                 |             |          |
9469                           |2MbDSL/ATM   |100BaseTX        |10BaseT      |10BaseT   |10BaseT
9470                         ------------   ------------------------------------       -------------------------------
9471                         | Internet |   | 172.17.0.0/16        Area 1      |       |  192.168.1.0/24 wlan  Area 2|
9472                         ------------   |         Student network (dorm)   |       |       barcelonawireless     |
9473                                        ------------------------------------       -------------------------------
9474                         </screen>
9475                         Don't be afraid by this diagram, zebra does most of the work automatically, so it won't take any work to put all the routes up with zebra. It would be painful to mantain all those routes by hand in a day to day basis. The most important thing you must have clear, is the network topology. And take special care with Area 0, since it's the most important.
9476                         First configure zebra, editing zebra.conf and adapt it to your needs:
9477                         <screen>
9478                         hostname omega
9479                         password xxx 
9480                         enable password xxx
9481                         !
9482                         ! Interface's description.
9483                         !
9484                         !interface lo
9485                         ! description test of desc.
9486                         !
9487                         interface eth1
9488                         multicast
9489                         !
9490                         ! Static default route
9491                         !
9492                         ip route 0.0.0.0/0 212.170.21.129
9493                         !
9494                         log file /var/log/zebra/zebra.log
9495                         </screen>
9496                         In Debian, I will also had to edit /etc/zebra/daemons so they start at boot:
9497                         <screen>
9498                         zebra=yes
9499                         ospfd=yes
9500                         </screen>
9501                         Now we have to edit ospfd.conf if you are still runnig IPV4 or ospf6d.conf if you run IPV6. My ospfd.conf looks like:
9502                         <screen>
9503                         hostname omega
9504                         password xxx
9505                         enable password xxx
9506                         !
9507                         router ospf
9508                           network 192.168.0.0/24 area 0
9509                           network 172.17.0.0/16 area 1
9510                         !
9511                         ! log stdout
9512                         log file /var/log/zebra/ospfd.log
9513                         </screen>
9514                         Here we instruct ospf about our network topology.
9515                 </para>
9516         </sect2>
9517         
9518         <Sect2 id="lartc.dynamic-routing.ospf.running">
9519         <Title>Running Zebra</Title>
9520                 <para>
9521                         Now, we have to start Zebra; either by hand by typing "zebra -d" or with some script like "/etc/init.d/zebra start". Then carefully watching the ospdfd logs we should see something like:
9522                         <screen>
9523                         2002/12/13 22:46:24 OSPF: interface 192.168.0.1 join AllSPFRouters Multicast group.
9524                         2002/12/13 22:46:34 OSPF: SMUX_CLOSE with reason: 5   
9525                         2002/12/13 22:46:44 OSPF: SMUX_CLOSE with reason: 5
9526                         2002/12/13 22:46:54 OSPF: SMUX_CLOSE with reason: 5   
9527                         2002/12/13 22:47:04 OSPF: SMUX_CLOSE with reason: 5   
9528                         2002/12/13 22:47:04 OSPF: DR-Election[1st]: Backup 192.168.0.1
9529                         2002/12/13 22:47:04 OSPF: DR-Election[1st]: DR     192.168.0.1
9530                         2002/12/13 22:47:04 OSPF: DR-Election[2nd]: Backup 0.0.0.0
9531                         2002/12/13 22:47:04 OSPF: DR-Election[2nd]: DR     192.168.0.1
9532                         2002/12/13 22:47:04 OSPF: interface 192.168.0.1 join AllDRouters Multicast group.
9533                         2002/12/13 22:47:06 OSPF: DR-Election[1st]: Backup 192.168.0.2
9534                         2002/12/13 22:47:06 OSPF: DR-Election[1st]: DR     192.168.0.1
9535                         2002/12/13 22:47:06 OSPF: Packet[DD]: Negotiation done (Slave).
9536                         2002/12/13 22:47:06 OSPF: nsm_change_status(): scheduling new router-LSA origination
9537                         2002/12/13 22:47:11 OSPF: ospf_intra_add_router: Start
9538                         </screen>
9539                         Ignore the SMUX_CLOSE message by now, since it's about SNMP. We can see that 192.168.0.1 is the <emphasis>Designated Router</emphasis> and 192.168.0.2 is the <emphasis>Backup Designated Router</emphasis>
9540                 </para>
9542                 <para>
9543                         We can also interact with the zebra or the ospfd interface by executing:
9544                         <screen>
9545                         <prompt>$ </prompt>telnet localhost zebra
9546                         <prompt>$ </prompt>telnet localhost ospfd
9547                         </screen>
9549                         Let's see how to view if the routes are propagating, log into zebra and type:
9550                         
9551                         <screen>
9552                         root@atlantis:~# telnet localhost zebra
9553                         Trying 127.0.0.1...
9554                         Connected to atlantis.
9555                         Escape character is '^]'.
9557                         Hello, this is zebra (version 0.92a).
9558                         Copyright 1996-2001 Kunihiro Ishiguro.
9560                         User Access Verification
9562                         Password: 
9563                         atlantis&gt; show ip route
9564                         Codes: K - kernel route, C - connected, S - static, R - RIP, O - OSPF,
9565                                B - BGP, &gt; - selected route, * - FIB route
9567                         K>* 0.0.0.0/0 via 192.168.0.1, eth1
9568                         C>* 127.0.0.0/8 is directly connected, lo
9569                         O   172.17.0.0/16 [110/10] is directly connected, eth0, 06:21:53
9570                         C>* 172.17.0.0/16 is directly connected, eth0
9571                         O   192.168.0.0/24 [110/10] is directly connected, eth1, 06:21:53
9572                         C>* 192.168.0.0/24 is directly connected, eth1
9573                         atlantis&gt; show ip ospf border-routers
9574                         ============ OSPF router routing table =============
9575                         R    192.168.0.253         [10] area: (0.0.0.0), ABR
9576                                                    via 192.168.0.253, eth1
9577                                                          [10] area: (0.0.0.1), ABR
9578                                                    via 172.17.0.2, eth0
9579                         </screen>
9580                         Or with iproute directly:
9581                         <screen>
9582                         root@omega:~# ip route
9583                         212.170.21.128/26 dev eth0  proto kernel  scope link  src 212.170.21.172 
9584                         192.168.0.0/24 dev eth1  proto kernel  scope link  src 192.168.0.1 
9585                         172.17.0.0/16 via 192.168.0.2 dev eth1  proto zebra  metric 20 
9586                         default via 212.170.21.129 dev eth0  proto zebra 
9587                         root@omega:~# 
9588                         </screen>
9589                         We can see the zebra routes, that weren't there before. It's really nice to see routes appearing just a few seconds after you start zebra and ospfd. You can check connectivity to other hosts with ping. Zebra routes are automatic, you can just add another router to the network, configure zebra, and voila!
9590                 </para>
9591                 
9592                 <para>
9593                         Hint: You can use:
9594                         <screen>
9595                         tcpdump -i eth1 ip[9] == 89
9596                         </screen>
9597                         To campture OSPF packets for analisys. OSPF ip protocol number is 89, and the protocol field is the 9nth octet on the ip header.
9598                 </para>
9599                 
9600                 <para>
9601                         OSPF has a lot of tunable parameters, specially for large networks. In further ampliations of the howto we will show some methodologies for fine tunning OSPF.
9602                 </para>
9603         </sect2>
9604 </Sect1>  
9605 </chapter>
9608 <chapter id="lartc.other"
9609   xreflabel="Other possibilities">
9610   <Title>Other possibilities</Title>
9612 <Para>
9613 This chapter is a list of projects having to do with advanced Linux routing
9614 &amp; traffic shaping. Some of these links may deserve chapters of their
9615 own, some are documented very well of themselves, and don't need more HOWTO.
9616 </Para>
9618 <Para>
9619 <VariableList>
9621 <VarListEntry>
9622 <Term>802.1Q VLAN Implementation for Linux <ULink
9623 URL="http://scry.wanfear.com/~greear/vlan.html"
9624 >(site)</ULink
9625 ></Term>
9626 <ListItem>
9627 <Para>
9628 VLANs are a very cool way to segregate your
9629 networks in a more virtual than physical way. Good information on VLANs can
9630 be found <ULink
9631 URL="ftp://ftp.netlab.ohio-state.edu/pub/jain/courses/cis788-97/virtual_lans/index.htm"
9632 >here</ULink
9633 >. With this implementation, you can have your Linux box talk
9634 VLANs with machines like Cisco Catalyst, 3Com: &lcub;Corebuilder, Netbuilder II,
9635 SuperStack II switch 630&rcub;, Extreme Ntwks Summit 48, Foundry: &lcub;ServerIronXL,
9636 FastIron&rcub;.
9637 </Para>
9639 <Para>
9640 A great HOWTO about VLANs can be found <ULink
9641 URL="http://scry.wanfear.com/~greear/vlan/cisco_howto.html"
9642 >here</ULink
9644 </Para>
9646 <Para>
9647 Update: has been included in the kernel as of 2.4.14 (perhaps 13).
9648 </Para></ListItem>
9649 </VarListEntry>
9650 <VarListEntry>
9651 <Term>Alternate 802.1Q VLAN Implementation for Linux <ULink
9652 URL="http://vlan.sourceforge.net "
9653 >(site)</ULink
9654 ></Term>
9655 <ListItem>
9656 <Para>
9657 Alternative VLAN implementation for linux. This project was started out of
9658 disagreement with the 'established' VLAN project's architecture and coding
9659 style, resulting in a cleaner overall design.
9660 </Para></ListItem>
9661 </VarListEntry>
9662 <VarListEntry>
9663 <Term>Linux Virtual Server <ULink
9664 URL="http://www.LinuxVirtualServer.org/"
9665 >(site)</ULink
9666 ></Term>
9667 <ListItem>
9668 <Para>
9669 These people are brilliant. The Linux Virtual Server is a highly scalable and
9670 highly available server built on a cluster of real servers, with the load
9671 balancer running on the Linux operating system. The architecture of the
9672 cluster is transparent to end users. End users only see a single virtual
9673 server.
9674 </Para>
9676 <Para>
9677 In short whatever you need to load balance, at whatever level of traffic, LVS
9678 will have a way of doing it. Some of their techniques are positively evil!
9679 For example, they let several machines have the same IP address on a
9680 segment, but turn off ARP on them. Only the LVS machine does ARP - it then
9681 decides which of the backend hosts should handle an incoming packet, and
9682 sends it directly to the right MAC address of the backend server. Outgoing
9683 traffic will flow directly to the router, and not via the LVS machine, which
9684 does therefor not need to see your 5Gbit/s of content flowing to the world,
9685 and cannot be a bottleneck.
9686 </Para>
9688 <Para>
9689 The LVS is implemented as a kernel patch in Linux 2.0 and 2.2, but as a
9690 Netfilter module in 2.4/2.5, so it does not need kernel patches! Their 2.4
9691 support is still in early development, so beat on it and give feedback or
9692 send patches.
9693 </Para></ListItem>
9694 </VarListEntry>
9695 <VarListEntry>
9696 <Term>CBQ.init <ULink
9697 URL="ftp://ftp.equinox.gu.net/pub/linux/cbq/"
9698 >(site)</ULink
9699 ></Term>
9700 <ListItem>
9701 <Para>
9702 Configuring CBQ can be a bit daunting, especially if all you want to do is
9703 shape some computers behind a router. CBQ.init can help you configure Linux
9704 with a simplified syntax.
9705 </Para>
9707 <Para>
9708 For example, if you want all computers in your 192.168.1.0/24 subnet
9709 (on 10mbit eth1) to be limited to 28kbit/s download speed, put
9710 this in the CBQ.init configuration file:
9711 </Para>
9713 <Para>
9715 <Screen>
9716 DEVICE=eth1,10Mbit,1Mbit
9717 RATE=28Kbit
9718 WEIGHT=2Kbit
9719 PRIO=5
9720 RULE=192.168.1.0/24
9721 </Screen>
9723 </Para>
9725 <Para>
9726 By all means use this program if the 'how and why' don't interest you.
9727 We're using CBQ.init in production and it works very well. It can even do
9728 some more advanced things, like time dependent shaping. The documentation is
9729 embedded in the script, which explains why you can't find a README.
9730 </Para></ListItem>
9731 </VarListEntry>
9732 <VarListEntry>
9733 <Term>Chronox easy shaping scripts <ULink
9734 URL="http://www.chronox.de"
9735 >(site)</ULink
9736 ></Term>
9737 <ListItem>
9738 <Para>
9739 Stephan Mueller (smueller@chronox.de) wrote two useful scripts, 'limit.conn'
9740 and 'shaper'. The first one allows you to easily throttle a single download
9741 session, like this:
9742 </Para>
9744 <Para>
9746 <Screen>
9747 # limit.conn -s SERVERIP -p SERVERPORT -l LIMIT
9748 </Screen>
9750 </Para>
9752 <Para>
9753 It works on Linux 2.2 and 2.4/2.5.
9754 </Para>
9756 <Para>
9757 The second script is more complicated, and can be used to make lots of
9758 different queues based on iptables rules, which are used to mark packets
9759 which are then shaped.
9760 </Para></ListItem>
9761 </VarListEntry>
9762 <VarListEntry>
9763 <Term>Virtual Router
9764 Redundancy Protocol implementation <ULink
9765 URL="http://w3.arobas.net/~jetienne/vrrpd/index.html"
9766 >(site)</ULink
9767 ></Term>
9768 <ListItem>
9769 <Para>
9770 This is purely for redundancy. Two machines with their own IP address and
9771 MAC Address together create a third IP Address and MAC Address, which is
9772 virtual. Originally intended purely for routers, which need constant MAC
9773 addresses, it also works for other servers.
9774 </Para>
9776 <Para>
9777 The beauty of this approach is the incredibly easy configuration. No kernel
9778 compiling or patching required, all userspace.
9779 </Para>
9781 <Para>
9782 Just run this on all machines participating in a service:
9784 <Screen>
9785 # vrrpd -i eth0 -v 50 10.0.0.22
9786 </Screen>
9788 </Para>
9790 <Para>
9791 And you are in business! 10.0.0.22 is now carried by one of your servers,
9792 probably the first one to run the vrrp daemon. Now disconnect that computer
9793 from the network and very rapidly one of the other computers will assume the
9794 10.0.0.22 address, as well as the MAC address.
9795 </Para>
9797 <Para>
9798 I tried this over here and had it up and running in 1 minute. For some
9799 strange reason it decided to drop my default gateway, but the -n flag
9800 prevented that.
9801 </Para>
9803 <Para>
9804 This is a 'live' fail over:
9805 </Para>
9807 <Para>
9809 <Screen>
9810 64 bytes from 10.0.0.22: icmp_seq=3 ttl=255 time=0.2 ms
9811 64 bytes from 10.0.0.22: icmp_seq=4 ttl=255 time=0.2 ms
9812 64 bytes from 10.0.0.22: icmp_seq=5 ttl=255 time=16.8 ms
9813 64 bytes from 10.0.0.22: icmp_seq=6 ttl=255 time=1.8 ms
9814 64 bytes from 10.0.0.22: icmp_seq=7 ttl=255 time=1.7 ms
9815 </Screen>
9817 </Para>
9819 <Para>
9820 Not *one* ping packet was lost! Just after packet 4, I disconnected my P200
9821 from the network, and my 486 took over, which you can see from the higher
9822 latency.
9823 </Para></ListItem>
9824 </VarListEntry>
9825 </VariableList>
9826 </Para>
9828 </chapter>
9830 <chapter id="lartc.further">
9831   <Title>Further reading</Title>
9833 <Para>
9834 <VariableList>
9836 <VarListEntry>
9837 <Term><ULink
9838 URL="http://snafu.freedom.org/linux2.2/iproute-notes.html"
9839 >http://snafu.freedom.org/linux2.2/iproute-notes.html</ULink
9840 ></Term>
9841 <ListItem>
9842 <Para>
9843 Contains lots of technical information, comments from the kernel
9844 </Para></ListItem>
9845 </VarListEntry>
9846 <VarListEntry>
9847 <Term><ULink
9848 URL="http://www.davin.ottawa.on.ca/ols/"
9849 >http://www.davin.ottawa.on.ca/ols/</ULink
9850 ></Term>
9851 <ListItem>
9852 <Para>
9853 Slides by Jamal Hadi Salim, one of the authors of Linux traffic control
9854 </Para></ListItem>
9855 </VarListEntry>
9856 <VarListEntry>
9857 <Term><ULink
9858 URL="http://defiant.coinet.com/iproute2/ip-cref/"
9859 >http://defiant.coinet.com/iproute2/ip-cref/</ULink
9860 ></Term>
9861 <ListItem>
9862 <Para>
9863 HTML version of Alexeys LaTeX documentation - explains part of iproute2 in
9864 great detail
9865 </Para></ListItem>
9866 </VarListEntry>
9867 <VarListEntry>
9868 <Term><ULink
9869 URL="http://www.aciri.org/floyd/cbq.html"
9870 >http://www.aciri.org/floyd/cbq.html</ULink
9871 ></Term>
9872 <ListItem>
9873 <Para>
9874 Sally Floyd has a good page on CBQ, including her original papers. None of
9875 it is Linux specific, but it does a fair job discussing the theory and uses
9876 of CBQ.
9877 Very technical stuff, but good reading for those so inclined. 
9878 </Para></ListItem>
9879 </VarListEntry>
9880 <VarListEntry>
9881 <Term>Differentiated Services on Linux</Term>
9882 <ListItem>
9883 <Para>
9884 This <ULink
9885 URL="ftp://icaftp.epfl.ch/pub/linux/diffserv/misc/dsid-01.txt.gz"
9886 >document</ULink
9887 > by Werner Almesberger, Jamal Hadi Salim and Alexey
9888 Kuznetsov describes DiffServ facilities in the Linux kernel, amongst which
9889 are TBF, GRED, the DSMARK qdisc and the tcindex classifier.
9890 </Para></ListItem>
9891 </VarListEntry>
9892 <VarListEntry>
9893 <Term><ULink
9894 URL="http://ceti.pl/~kravietz/cbq/NET4_tc.html"
9895 >http://ceti.pl/~kravietz/cbq/NET4_tc.html</ULink
9896 ></Term>
9897 <ListItem>
9898 <Para>
9899 Yet another HOWTO, this time in Polish! You can copy/paste command lines
9900 however, they work just the same in every language. The author is
9901 cooperating with us and may soon author sections of this HOWTO.
9902 </Para></ListItem>
9903 </VarListEntry>
9904 <VarListEntry>
9905 <Term><ULink
9906 URL="http://www.cisco.com/univercd/cc/td/doc/product/software/ios111/cc111/car.htm"
9907 >IOS Committed Access Rate</ULink
9908 ></Term>
9909 <ListItem>
9910 <Para>
9911 <Anchor id="CAR">
9912 From the helpful folks of Cisco who have the laudable habit of putting
9913 their documentation online. Cisco syntax is different but the concepts are
9914 the same, except that we can do more and do it without routers the price of
9915 cars :-)
9916 </Para></ListItem>
9917 </VarListEntry>
9918 <VarListEntry>
9919 <Term>Docum experimental site<ULink
9920 URL="http://www.docum.org"
9921 >(site)</ULink
9922 ></Term>
9923 <ListItem>
9924 <Para>
9925 Stef Coene is busy convincing his boss to sell Linux support, and so he is
9926 experimenting a lot, especially with managing bandwidth. His site has a lot
9927 of practical information, examples, tests and also points out some CBQ/tc bugs. 
9928 </Para>
9929 </ListItem>
9930 </VarListEntry>
9931 <VarListEntry>
9932 <Term>TCP/IP Illustrated, volume 1, W. Richard Stevens, ISBN 0-201-63346-9</Term>
9933 <ListItem>
9934 <Para>
9935 Required reading if you truly want to understand TCP/IP. Entertaining as
9936 well.
9937 </Para>
9938 </ListItem>
9939 </VarListEntry>
9940 </VariableList> 
9941 </Para>
9943 </chapter>
9945 <chapter id="lartc.ack">
9946 <Title>Acknowledgements </Title>
9948 <Para>
9950 It is our goal to list everybody who has contributed to this HOWTO, or
9951 helped us demystify how things work. While there are currently no plans
9952 for a Netfilter type scoreboard, we do like to recognize the people who are
9953 helping.
9954 </Para>
9956 <Para>
9958 <ItemizedList spacing="compact">
9959 <ListItem>
9961    <Para>
9962    <author><firstname>Junk</firstname><surname>Alins</surname></author>
9963    <address><email>juanjo@mat.upc.es</email></address>
9964    </Para>
9965   </ListItem>
9966   <ListItem>
9967    <Para>
9968    <author><firstname>Joe</firstname><surname>Van Andel</surname></author>
9969    </Para>
9970   </ListItem>
9972   <ListItem>
9973    <Para>
9974    <author><firstname>Michael</firstname><othername>T.</othername>
9975      <surname>Babcock</surname></author>
9976    <address><email>mbabcock@fibrespeed.net</email></address>
9977    </Para>
9978   </ListItem>
9981   <ListItem>
9982    <Para>
9983    <author><firstname>Christopher</firstname>
9984      <surname>Barton</surname></author>
9985    <address><email>cpbarton%uiuc.edu</email></address>
9986    </Para>
9987   </ListItem>
9990   <ListItem>
9991    <Para>
9992    <author><firstname>Ard</firstname><surname>van Breemen</surname></author>
9993    <address><email>ard&percnt;kwaak.net</email></address>
9994   </Para>
9995   </ListItem>
9996   <ListItem>
9997    <Para>
9998    <author><firstname>Ron</firstname><surname>Brinker</surname></author>
9999    <address><email>service&percnt;emcis.com</email></address>
10000    </Para>
10001   </ListItem>
10002   <ListItem>
10003    <Para>
10004    <author><firstname>?ukasz</firstname><surname>Bromirski</surname></author>
10005    <address><email>l.bromirski@mr0vka.eu.org</email></address>
10006    </Para>
10007   </ListItem>
10008   <ListItem>
10009    <Para>
10010    <author><firstname>Lennert</firstname><surname>Buytenhek</surname></author>
10011    <address><email>buytenh@gnu.org</email></address>
10012    </Para>
10013   </ListItem>
10014   <ListItem>
10015    <Para>
10016    <author><firstname>Esteve</firstname><surname>Camps</surname></author>
10017    <address><email>esteve@hades.udg.es</email></address>
10018    </Para>
10019   </ListItem>
10020   <ListItem>
10021    <Para>
10022    <author><firstname>Stef</firstname><surname>Coene</surname></author>
10023    <address><email>stef.coene@docum.org</email></address>
10024    </Para>
10025   </ListItem>
10026   <ListItem>
10027    <Para>
10028    <author><firstname>Don</firstname><surname>Cohen</surname></author>
10029    <address><email>don-lartc&percnt;isis.cs3-inc.com</email></address>
10030    </Para>
10031   </ListItem>
10032   <ListItem>
10033    <Para>
10034    <author><firstname>Jonathan</firstname><surname>Corbet</surname></author>
10035    <address><email>lwn&percnt;lwn.net</email></address>
10036    </Para>
10037   </ListItem>
10038   <ListItem>
10039    <Para>
10040    <author><firstname>Gerry</firstname><surname>Creager</surname>
10041      <othername>N5JXS</othername></author>
10042    <address><email>gerry&percnt;cs.tamu.edu</email></address>
10043    </Para>
10044   </ListItem>
10045   <ListItem>
10046    <Para>
10047    <author><firstname>Marco</firstname><surname>Davids</surname></author>
10048    <address><email>marco@sara.nl</email></address>
10049    </Para>
10050   </ListItem>
10051   <ListItem>
10052    <Para>
10053    <author><firstname>Jonathan</firstname><surname>Day</surname></author>
10054    <address><email>jd9812@my-deja.com</email></address>
10055    </Para>
10056   </ListItem>
10057   <ListItem>
10058    <Para>
10059    <author><firstname>Martin</firstname><surname>Devera</surname>
10060      <othername>aka devik</othername></author>
10061    <address><email>devik@cdi.cz</email></address>
10062   </Para>
10063   </ListItem>
10065   <ListItem>
10066    <Para>
10067    <author><firstname>Hannes</firstname><surname>Ebner</surname>
10068      </author>
10069    <address><email>he%fli4l.de</email></address>
10070   </Para>
10071   </ListItem>
10073   <ListItem>
10074    <Para>
10075    <author><firstname>Derek</firstname><surname>Fawcus</surname>
10076      </author>
10077    <address><email>dfawcus%cisco.com</email></address>
10078   </Para>
10079   </ListItem>
10082   <ListItem>
10083    <Para>
10084    <author><firstname>Stephan</firstname><othername>"Kobold"</othername>
10085      <surname>Gehring</surname></author>
10086    <address><email>Stephan.Gehring@bechtle.de</email></address>
10087   </Para>
10088   </ListItem>
10089   <ListItem>
10090    <Para>
10091    <author><firstname>Jacek</firstname><surname>Glinkowski</surname></author>
10092    <address><email>jglinkow&percnt;hns.com</email></address>
10093   </Para>
10094   </ListItem>
10095   <ListItem>
10096    <Para>
10097    <author><firstname>Andrea</firstname><surname>Glorioso</surname></author>
10098    <address><email>sama&percnt;perchetopi.org</email></address>
10099   </Para>
10100   </ListItem>
10101   <ListItem>
10102    <Para>
10103    <author><firstname>Nadeem</firstname><surname>Hasan</surname></author>
10104    <address><email>nhasan@usa.net</email></address>
10105    </Para>
10106   </ListItem>
10107   <ListItem>
10108    <Para>
10109    <author><firstname>Erik</firstname><surname>Hensema</surname></author>
10110    <address><email>erik&percnt;hensema.xs4all.nl</email></address>
10111    </Para>
10112   </ListItem>
10113   <ListItem>
10114    <Para>
10115    <author><firstname>Vik</firstname><surname>Heyndrickx</surname></author>
10116    <address><email>vik.heyndrickx@edchq.com</email></address>
10117    </Para>
10118   </ListItem>
10119   <ListItem>
10120    <Para>
10121    <author><firstname>Spauldo</firstname><surname>Da Hippie</surname></author>
10122    <address><email>spauldo&percnt;usa.net</email></address>
10123    </Para>
10124   </ListItem>
10125   <ListItem>
10126    <Para>
10127    <author><firstname>Koos</firstname><surname>van den Hout</surname></author>
10128    <address><email>koos@kzdoos.xs4all.nl</email></address>
10129    </Para>
10130   </ListItem>
10131   <ListItem>
10133 <Para>
10134 Stefan Huelbrock &lt;shuelbrock&percnt;datasystems.de&gt;
10135 </Para>
10136 </ListItem>
10137 <ListItem>
10139 <Para>
10140 Alexander W. Janssen &lt;yalla&percnt;ynfonatic.de&gt;
10141 </Para>
10142 </ListItem>
10143 <ListItem>
10145 <Para>
10146 Gareth John &lt;gdjohn&percnt;zepler.org&gt;
10147 </Para>
10148 </ListItem>
10150   <ListItem>
10151    <Para>
10152    <author><firstname>Dave</firstname><surname>Johnson</surname></author>
10153    <address><email>dj@www.uk.linux.org</email></address>
10154    </Para>
10155   </ListItem>
10158 <ListItem>
10160 <Para>
10161 Martin Josefsson &lt;gandalf&percnt;wlug.westbo.se&gt;
10162 </Para>
10163 </ListItem>
10164 <ListItem>
10166 <Para>
10167 Andi Kleen &lt;ak&percnt;suse.de&gt;
10168 </Para>
10169 </ListItem>
10170 <ListItem>
10172 <Para>
10173 Andreas J. Koenig &lt;andreas.koenig&percnt;anima.de&gt;
10174 </Para>
10175 </ListItem>
10176 <ListItem>
10178 <Para>
10179 Pawel Krawczyk &lt;kravietz&percnt;alfa.ceti.pl&gt;
10180 </Para>
10181 </ListItem>
10182 <ListItem>
10184 <Para>
10185 Amit Kucheria &lt;amitk@ittc.ku.edu&gt;
10186 </Para>
10187 </ListItem>
10188 <ListItem>
10190 <Para>
10191 Edmund Lau &lt;edlau&percnt;ucf.ics.uci.edu&gt;
10192 </Para>
10193 </ListItem>
10194 <ListItem>
10196 <Para>
10197 Philippe Latu &lt;philippe.latu&percnt;linux-france.org&gt;
10198 </Para>
10199 </ListItem>
10200 <ListItem>
10202 <Para>
10203 Arthur van Leeuwen &lt;arthurvl&percnt;sci.kun.nl&gt;
10204 </Para>
10205 </ListItem>
10206   <ListItem>
10207    <Para>
10208    <author><firstname>Jose Luis Domingo</firstname><surname>Lopez</surname>
10209      </author>
10210    <address><email>jdomingo@24x7linux.com</email></address>
10211   </Para>
10212   </ListItem>
10213 <ListItem>
10215 <Para>
10216 Jason Lunz &lt;j@cc.gatech.edu&gt;
10217 </Para>
10218 </ListItem>
10219 <ListItem>
10221 <Para>
10222 Stuart Lynne &lt;sl@fireplug.net&gt;
10223 </Para>
10224 </ListItem>
10225 <ListItem>
10227 <Para>
10228 Alexey Mahotkin &lt;alexm@formulabez.ru&gt;
10229 </Para>
10230 </ListItem>
10231 <ListItem>
10233 <Para>
10234 Predrag Malicevic &lt;pmalic@ieee.org&gt;
10235 </Para>
10236 </ListItem>
10237 <listitem>
10238 <para>
10239 Patrick McHardy &lt;kaber@trash.net&gt;
10240 </para>
10241 </ListItem>
10243 <ListItem>
10246 <Para>
10247 Andreas Mohr &lt;andi&percnt;lisas.de&gt;
10248 </Para>
10249 </ListItem>
10251 <listitem>
10252 <para>James Morris &lt;jmorris@intercode.com.au&gt;
10253 </para></listitem>
10256 <ListItem>
10257 <Para>
10258 Andrew Morton &lt;akpm%zip.com.au&gt;
10259 </Para>
10260 </ListItem>
10262 <ListItem>
10263 <Para>
10264 Wim van der Most 
10265 </Para>
10266 </ListItem>
10267 <ListItem>
10269 <Para>
10270 Stephan Mueller &lt;smueller@chronox.de&gt;
10271 </Para>
10272 </ListItem>
10273 <ListItem>
10275 <Para>
10276 Togan Muftuoglu &lt;toganm&percnt;yahoo.com&gt;
10277 </Para>
10278 </ListItem>
10279 <ListItem>
10282 <Para>
10283 Chris Murray &lt;cmurray@stargate.ca&gt;
10284 </Para>
10285 </ListItem>
10286 <ListItem>
10288 <Para>
10289 Patrick Nagelschmidt &lt;dto&percnt;gmx.net&gt;
10290 </Para>
10291 </ListItem>
10292 <ListItem>
10294 <Para>
10295 Ram Narula &lt;ram@princess1.net&gt;
10296 </Para>
10297 </ListItem>
10298 <ListItem>
10300 <Para>
10301 Jorge Novo &lt;jnovo@educanet.net&gt;
10302 </Para>
10303 </ListItem>
10304 <ListItem>
10306 <Para>
10307 Patrik &lt;ph@kurd.nu&gt;
10308 </Para>
10309 </ListItem>
10311 <listitem><para>P?l Osgy?ny &lt;oplab%westel900.net&gt;</para></listitem>
10313 <ListItem>
10315 <Para>
10316 Lutz Pre&szlig;ler &lt;Lutz.Pressler&percnt;SerNet.DE&gt;
10317 </Para>
10318 </ListItem>
10319 <ListItem>
10321 <Para>
10322 Jason Pyeron &lt;jason&percnt;pyeron.com&gt;
10323 </Para>
10324 </ListItem>
10326 <ListItem>
10327 <Para>
10328 Rod Roark &lt;rod%sunsetsystems.com&gt;
10329 </Para>
10330 </ListItem>
10333 <ListItem>
10334 <Para>
10335 Rusty Russell &lt;rusty&percnt;rustcorp.com.au&gt;
10336 </Para>
10337 </ListItem>
10338 <ListItem>
10340 <Para>
10341 Mihai RUSU &lt;dizzy&percnt;roedu.net&gt;
10342 </Para>
10343 </ListItem>
10344 <ListItem>
10346 <Para>
10347 Jamal Hadi Salim &lt;hadi&percnt;cyberus.ca&gt;
10348 </Para>
10349 </ListItem>
10351 <ListItem>
10352 <Para>
10353 Ren? Serral &lt;rserral%ac.upc.es&gt; 
10354 </Para>
10355 </ListItem>
10358 <ListItem>
10359 <Para>
10360 David Sauer &lt;davids&percnt;penguin.cz&gt;
10361 </Para>
10362 </ListItem>
10363 <ListItem>
10365 <Para>
10366 Sheharyar Suleman Shaikh &lt;sss23@drexel.edu&gt;
10367 </Para>
10368 </ListItem>
10369 <ListItem>
10371 <Para>
10372 Stewart Shields &lt;MourningBlade&percnt;bigfoot.com&gt;
10373 </Para>
10374 </ListItem>
10375 <ListItem>
10377 <Para>
10378 Nick Silberstein &lt;nhsilber&percnt;yahoo.com&gt;
10379 </Para>
10380 </ListItem>
10381 <ListItem>
10383 <Para>
10384 Konrads Smelkov &lt;konrads@interbaltika.com&gt;
10385 </Para>
10386 </ListItem>
10387 <ListItem>
10389 <Para>
10390 <author><firstname>William</firstname><surname>Stearns</surname></author>
10391 <address><email>wstearns@pobox.com</email></address>
10392 </Para>
10393 </ListItem>
10395 <ListItem>
10396 <Para>
10397 Andreas Steinmetz &lt;ast&percnt;domdv.de&gt;
10398 </Para>
10399 </ListItem>
10400 <ListItem>
10402 <Para>
10403 Jason Tackaberry &lt;tack@linux.com&gt;
10404 </Para>
10405 </ListItem>
10406 <ListItem>
10408 <Para>
10409 Charles Tassell &lt;ctassell&percnt;isn.net&gt;
10410 </Para>
10411 </ListItem>
10412 <ListItem>
10414 <Para>
10415 Glen Turner &lt;glen.turner&percnt;aarnet.edu.au&gt;
10416 </Para>
10417 </ListItem>
10419 <ListItem>
10420 <Para>
10421 Tea Sponsor: Eric Veldhuyzen &lt;eric&percnt;terra.nu&gt; 
10422 </Para>
10423 </ListItem>
10425 <listitem>
10426 <para>
10427 Thomas Walpuski &lt;thomas%bender.thinknerd.de&gt;
10428 </para>
10429 </listitem>
10432 <ListItem>
10433 <Para>
10434 Song Wang &lt;wsong@ece.uci.edu&gt;
10435 </Para>
10436 </ListItem>
10438   <ListItem>
10439    <Para>
10440    <author><firstname>Chris</firstname><surname>Wilson</surname></author>
10441    <address><email>chris@netservers.co.uk</email></address>
10442    </Para>
10443   </ListItem>
10445   <ListItem>
10446    <Para>
10447    <author><firstname>Lazar</firstname><surname>Yanackiev</surname></author>
10448    <address><email>Lyanackiev%gmx.net</email></address>
10449    </Para>
10450   </ListItem>
10451   
10452   <ListItem>
10453    <Para>
10454    <author><firstname>Pedro</firstname><surname>Larroy</surname></author>
10455    <address><email>piotr%omega.resa.es</email></address>
10456    <ItemizedList>
10457         <ListItem>
10458         <para>
10459         Chapter 15, section 10: Example of a full nat solution with QoS
10460         </para>
10461         </ListItem>
10463         <ListItem>
10464         <para>
10465         Chapter 17,, section 1: Setting up OSPF with Zebra
10466         </para>
10467         </ListItem>
10468    </ItemizedList>
10469    </Para>
10470   </ListItem>
10473 </ItemizedList>
10475 </Para>
10477 </chapter>
10479 </Book>