2 .\" $FreeBSD: /repoman/r/ncvs/src/sbin/ipfw/ipfw.8,v 1.63.2.38 2003/07/28 07:15:13 luigi Exp $
9 .Nd Traffic shaper control program
29 .Brq Cm delete | list | show
44 utility is the user interface for controlling the
49 operates by first using a packet filter to classify packets and divide them into
51 using any match pattern that can be used in
54 Depending on local policies, a flow can contain packets for a single
55 TCP connection, or from/to a given host, or entire subnet, or a
58 Packets belonging to the same flow are then passed to either of two
59 different objects, which implement the traffic regulation:
60 .Bl -hang -offset XXXX
62 A pipe emulates a link with given bandwidth, propagation delay,
63 queue size and packet loss rate.
64 Packets are queued in front of the pipe as they come out from the classifier,
65 and then transferred to the pipe according to the pipe's parameters.
69 is an abstraction used to implement the WF2Q+
70 (Worst-case Fair Weighted Fair Queueing) policy, which is
71 an efficient variant of the WFQ policy.
73 The queue associates a
75 and a reference pipe to each flow, and then all backlogged (i.e.,
76 with packets queued) flows linked to the same pipe share the pipe's
77 bandwidth proportionally to their weights.
78 Note that weights are not priorities; a flow with a lower weight
79 is still guaranteed to get its fraction of the bandwidth even if a
80 flow with a higher weight is permanently backlogged.
85 can be used to set hard limits to the bandwidth that a flow can use, whereas
87 can be used to determine how different flow share the available bandwidth.
93 configuration commands are the following:
94 .Bd -ragged -offset indent
95 .Cm pipe Ar number Cm config Ar pipe-configuration
97 .Cm queue Ar number Cm config Ar queue-configuration
100 The following parameters can be configured for a pipe:
102 .Bl -tag -width indent -compact
103 .It Cm bw Ar bandwidth | device
104 Bandwidth, measured in
107 .Brq Cm bit/s | Byte/s .
110 A value of 0 (default) means unlimited bandwidth.
111 The unit must immediately follow the number, as in
113 .Dl "dnctl pipe 1 config bw 300Kbit/s"
115 If a device name is specified instead of a numeric value, as in
117 .Dl "dnctl pipe 1 config bw tun0"
119 then the transmit clock is supplied by the specified device.
124 .It Cm delay Ar ms-delay
125 Propagation delay, measured in milliseconds.
126 The value is rounded to the next multiple of the clock tick
127 (typically 10ms, but it is a good practice to run kernels
129 .Dq "options HZ=1000"
131 the granularity to 1ms or less).
132 Default value is 0, meaning no delay.
135 The following parameters can be configured for a queue:
137 .Bl -tag -width indent -compact
138 .It Cm pipe Ar pipe_nr
139 Connects a queue to the specified pipe.
140 Multiple queues (with the same or different weights) can be connected to
141 the same pipe, which specifies the aggregate rate for the set of queues.
143 .It Cm weight Ar weight
144 Specifies the weight to be used for flows matching this queue.
145 The weight must be in the range 1..100, and defaults to 1.
148 Finally, the following parameters can be configured for both
151 .Bl -tag -width XXXX -compact
152 .It Cm buckets Ar hash-table-size
153 Specifies the size of the hash table used for storing the
155 Default value is 64 controlled by the
158 .Em net.inet.ip.dummynet.hash_size ,
159 allowed range is 16 to 65536.
161 .It Cm mask Ar mask-specifier
162 Packets sent to a given pipe or queue by an
164 rule can be further classified into multiple flows, each of which is then
168 A flow identifier is constructed by masking the IP addresses,
169 ports and protocol types as specified with the
171 options in the configuration of the pipe or queue.
172 For each different flow identifier, a new pipe or queue is created
173 with the same parameters as the original object, and matching packets
178 are used, each flow will get the same bandwidth as defined by the pipe,
181 are used, each flow will share the parent's pipe bandwidth evenly
182 with other flows generated by the same queue (note that other queues
183 with different weights might be connected to the same pipe).
185 Available mask specifiers are a combination of one or more of the following:
188 .Cm dst-ip6 Ar mask ,
190 .Cm src-ip6 Ar mask ,
191 .Cm dst-port Ar mask ,
192 .Cm src-port Ar mask ,
197 where the latter means all bits in all fields are significant.
200 When a packet is dropped by a dummynet queue or pipe, the error
201 is normally reported to the caller routine in the kernel, in the
202 same way as it happens when a device queue fills up. Setting this
203 option reports the packet as successfully delivered, which can be
204 needed for some experimental setups where you want to simulate
205 loss or congestion at a remote router.
207 .It Cm plr Ar packet-loss-rate
211 is a floating-point number between 0 and 1, with 0 meaning no
212 loss, 1 meaning 100% loss.
213 The loss rate is internally represented on 31 bits.
215 .It Cm queue Brq Ar slots | size Ns Cm Kbytes
220 Default value is 50 slots, which
221 is the typical queue size for Ethernet devices.
222 Note that for slow speed links you should keep the queue
223 size short or your traffic might be affected by a significant
225 E.g., 50 max-sized ethernet packets (1500 bytes) mean 600Kbit
226 or 20s of queue on a 30Kbit/s pipe.
227 Even worse effect can result if you get packets from an
228 interface with a much larger MTU, e.g. the loopback interface
229 with its 16KB packets.
231 .It Cm red | gred Ar w_q Ns / Ns Ar min_th Ns / Ns Ar max_th Ns / Ns Ar max_p
232 Make use of the RED (Random Early Detection) queue management algorithm.
237 point numbers between 0 and 1 (0 not included), while
241 are integer numbers specifying thresholds for queue management
242 (thresholds are computed in bytes if the queue has been defined
243 in bytes, in slots otherwise).
246 also supports the gentle RED variant (gred).
250 variables can be used to control the RED behaviour:
251 .Bl -tag -width indent
252 .It Em net.inet.ip.dummynet.red_lookup_depth
253 specifies the accuracy in computing the average queue
254 when the link is idle (defaults to 256, must be greater than zero)
255 .It Em net.inet.ip.dummynet.red_avg_pkt_size
256 specifies the expected average packet size (defaults to 512, must be
258 .It Em net.inet.ip.dummynet.red_max_pkt_size
259 specifies the expected maximum packet size, only used when queue
260 thresholds are in bytes (defaults to 1500, must be greater than zero).
264 The following options are available:
265 .Bl -tag -width indent
267 While listing, show counter values.
270 command just implies this option.
272 Don't ask for confirmation for commands that can cause problems
275 If there is no tty associated with the process, this is implied.
277 Displays a short help.
279 Only check syntax of the command strings, without actually passing
288 be quiet about actions
291 This is useful for adjusting rules by executing multiple
294 or by processing a file of many
296 rules across a remote login session.
299 is performed in normal (verbose) mode (with the default kernel
300 configuration), it prints a message.
301 Because all rules are flushed, the message might not be delivered
302 to the login session, causing the remote login session to be closed
303 and the remainder of the ruleset to not be processed.
304 Access to the console would then be required to recover.
306 While listing pipes, sort according to one of the four
307 counters (total or current packets or bytes).
312 To ease configuration, rules can be put into a file which is
315 as shown in the last synopsis line.
319 The file will be read line by line and applied as arguments to the
323 Optionally, a preprocessor can be specified using
327 is to be piped through.
328 Useful preprocessors include
334 doesn't start with a slash
336 as its first character, the usual
338 name search is performed.
339 Care should be taken with this in environments where not all
340 file systems are mounted (yet) by the time
342 is being run (e.g. when they are mounted over NFS).
345 has been specified, any additional arguments as passed on to the preprocessor
347 This allows for flexible configuration files (like conditionalizing
348 them on the local hostname) and the use of macros to centralize
349 frequently required arguments like IP addresses.
352 Here are some important points to consider when designing your
356 Remember that you filter both packets going
360 Most connections need packets going in both directions.
362 Remember to test very carefully.
363 It is a good idea to be near the console when doing this.
364 If you cannot be near the console,
365 use an auto-recovery script such as the one in
366 .Pa /usr/share/examples/ipfw/change_rules.sh .
368 Don't forget the loopback interface.
373 variables controls the behaviour of the
376 These are shown below together with their default value
377 (but always check with the
379 command what value is actually in use) and meaning:
380 .Bl -tag -width indent
381 .It Em net.inet.ip.dummynet.expire : No 1
382 Lazily delete dynamic pipes/queue once they have no pending traffic.
383 You can disable this by setting the variable to 0, in which case
384 the pipes/queues will only be deleted when the threshold is reached.
385 .It Em net.inet.ip.dummynet.hash_size : No 64
386 Default size of the hash table used for dynamic pipes/queues.
387 This value is used when no
389 option is specified when configuring a pipe/queue.
390 .It Em net.inet.ip.dummynet.max_chain_len : No 16
391 Target value for the maximum number of pipes/queues in a hash bucket.
393 .Cm max_chain_len*hash_size
394 is used to determine the threshold over which empty pipes/queues
395 will be expired even when
396 .Cm net.inet.ip.dummynet.expire=0 .
397 .It Em net.inet.ip.dummynet.red_lookup_depth : No 256
398 .It Em net.inet.ip.dummynet.red_avg_pkt_size : No 512
399 .It Em net.inet.ip.dummynet.red_max_pkt_size : No 1500
400 Parameters used in the computations of the drop probability
401 for the RED algorithm.
404 The following rules show some of the applications of
406 for simulations and the like. The examples use the obsolete
408 command for illustration only as the use of
412 This rule drops random incoming packets with a probability
415 .Dl "ipfw add pipe 10 ip from any to any"
416 .Dl "dnctl pipe 10 config plr 0.05"
418 We can use pipes to artificially limit bandwidth, e.g. on a
419 machine acting as a router, if we want to limit traffic from
420 local clients on 192.168.2.0/24 we do:
422 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
423 .Dl "dnctl pipe 1 config bw 300Kbit/s queue 50KBytes"
427 modifier so that the rule is not used twice.
428 Remember in fact that
430 rules are checked both on incoming and outgoing packets.
432 Should we want to simulate a bidirectional link with bandwidth
433 limitations, the correct way is the following:
435 .Dl "ipfw add pipe 1 ip from any to any out"
436 .Dl "ipfw add pipe 2 ip from any to any in"
437 .Dl "dnctl pipe 1 config bw 64Kbit/s queue 10Kbytes"
438 .Dl "dnctl pipe 2 config bw 64Kbit/s queue 10Kbytes"
440 The above can be very useful, e.g. if you want to see how
441 your fancy Web page will look for a residential user who
442 is connected only through a slow link.
443 You should not use only one pipe for both directions, unless
444 you want to simulate a half-duplex medium (e.g. AppleTalk,
446 It is not necessary that both pipes have the same configuration,
447 so we can also simulate asymmetric links.
449 Should we want to verify network performance with the RED queue
450 management algorithm:
452 .Dl "ipfw add pipe 1 ip from any to any"
453 .Dl "dnctl pipe 1 config bw 500Kbit/s queue 100 red 0.002/30/80/0.1"
455 Another typical application of the traffic shaper is to
456 introduce some delay in the communication.
457 This can significantly affect applications which do a lot of Remote
458 Procedure Calls, and where the round-trip-time of the
459 connection often becomes a limiting factor much more than
462 .Dl "ipfw add pipe 1 ip from any to any out"
463 .Dl "ipfw add pipe 2 ip from any to any in"
464 .Dl "dnctl pipe 1 config delay 250ms bw 1Mbit/s"
465 .Dl "dnctl pipe 2 config delay 250ms bw 1Mbit/s"
467 Per-flow queueing can be useful for a variety of purposes.
468 A very simple one is counting traffic:
470 .Dl "ipfw add pipe 1 tcp from any to any"
471 .Dl "ipfw add pipe 1 udp from any to any"
472 .Dl "ipfw add pipe 1 ip from any to any"
473 .Dl "dnctl pipe 1 config mask all"
475 The above set of rules will create queues (and collect
476 statistics) for all traffic.
477 Because the pipes have no limitations, the only effect is
478 collecting statistics.
479 Note that we need 3 rules, not just the last one, because
482 tries to match IP packets it will not consider ports, so we
483 would not see connections on separate ports as different
486 A more sophisticated example is limiting the outbound traffic
487 on a net with per-host limits, rather than per-network limits:
489 .Dl "ipfw add pipe 1 ip from 192.168.2.0/24 to any out"
490 .Dl "ipfw add pipe 2 ip from any to 192.168.2.0/24 in"
491 .Dl "dnctl pipe 1 config mask src-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
492 .Dl "dnctl pipe 2 config mask dst-ip 0x000000ff bw 200Kbit/s queue 20Kbytes"
494 To add a set of rules atomically, e.g. set 18:
496 .Dl "ipfw set disable 18"
497 .Dl "ipfw add NN set 18 ... # repeat as needed"
498 .Dl "ipfw set enable 18"
500 To delete a set of rules atomically the command is simply:
502 .Dl "ipfw delete set 18"
504 To test a ruleset and disable it and regain control if something goes wrong:
506 .Dl "ipfw set disable 18"
507 .Dl "ipfw add NN set 18 ... # repeat as needed"
508 .Dl "ipfw set enable 18; echo done; sleep 30 && ipfw set disable 18"
510 Here if everything goes well, you press control-C before the "sleep"
511 terminates, and your ruleset will be left active. Otherwise, e.g. if
512 you cannot access your box, the ruleset will be disabled after
513 the sleep terminates thus restoring the previous situation.
524 .An Ugen J. S. Antsilevich ,
525 .An Poul-Henning Kamp ,
531 API based upon code written by
537 traffic shaper supported by Akamba Corp.
541 utility first appeared in
546 Stateful extensions were introduced in
549 was introduced in Summer 2002.