]> git.saurik.com Git - apple/network_cmds.git/blob - dnctl/dnctl.8
19c09d4d69fab7c66d76cd8bc52e119d3dc23c93
[apple/network_cmds.git] / dnctl / dnctl.8
1 .\"
2 .\" $FreeBSD: /repoman/r/ncvs/src/sbin/ipfw/ipfw.8,v 1.63.2.38 2003/07/28 07:15:13 luigi Exp $
3 .\"
4 .Dd August 13, 2002
5 .Dt DNCTL 8
6 .Os Darwin
7 .Sh NAME
8 .Nm dnctl
9 .Nd Traffic shaper control program
10 .Sh SYNOPSIS
11 .Nm
12 .Op Fl anqs
13 .Brq Cm list | show
14 .Nm
15 .Op Fl f | q
16 .Cm flush
17 .Nm
18 .Op Fl q
19 .Brq Cm delete
20 .Op Ar number ...
21 .Nm
22 .Brq Cm pipe | queue
23 .Ar number
24 .Cm config
25 .Ar config-options
26 .Nm
27 .Op Fl s Op Ar field
28 .Brq Cm pipe | queue
29 .Brq Cm delete | list | show
30 .Op Ar number ...
31 .Nm
32 .Op Fl nq
33 .Oo
34 .Fl p Ar preproc
35 .Oo
36 .Ar preproc-flags
37 .Oc
38 .Oc
39 .Ar pathname
40 .Sh DESCRIPTION
41 .Pp
42 The
43 .Nm
44 utility is the user interface for controlling the
45 .Xr dummynet 4
46 traffic shaper.
47 .Pp
48 .Nm dummynet
49 operates by first using a packet filter to classify packets and divide them into
50 .Em flows ,
51 using any match pattern that can be used in
52 .Nm
53 rules.
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
56 protocol type, etc.
57 .Pp
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
61 .It Em pipe
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.
66 .Pp
67 .It Em queue
68 A queue
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.
72 .br
73 The queue associates a
74 .Em weight
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.
81 .Pp
82 .El
83 In practice,
84 .Em pipes
85 can be used to set hard limits to the bandwidth that a flow can use, whereas
86 .Em queues
87 can be used to determine how different flow share the available bandwidth.
88 .Pp
89 The
90 .Em pipe
91 and
92 .Em queue
93 configuration commands are the following:
94 .Bd -ragged -offset indent
95 .Cm pipe Ar number Cm config Ar pipe-configuration
96 .Pp
97 .Cm queue Ar number Cm config Ar queue-configuration
98 .Ed
99 .Pp
100 The following parameters can be configured for a pipe:
101 .Pp
102 .Bl -tag -width indent -compact
103 .It Cm bw Ar bandwidth | device
104 Bandwidth, measured in
105 .Sm off
106 .Op Cm K | M
107 .Brq Cm bit/s | Byte/s .
108 .Sm on
109 .Pp
110 A value of 0 (default) means unlimited bandwidth.
111 The unit must immediately follow the number, as in
112 .Pp
113 .Dl "dnctl pipe 1 config bw 300Kbit/s"
114 .Pp
115 If a device name is specified instead of a numeric value, as in
116 .Pp
117 .Dl "dnctl pipe 1 config bw tun0"
118 .Pp
119 then the transmit clock is supplied by the specified device.
120 At the moment no
121 device supports this
122 functionality.
123 .Pp
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
128 with
129 .Dq "options HZ=1000"
130 to reduce
131 the granularity to 1ms or less).
132 Default value is 0, meaning no delay.
133 .El
134 .Pp
135 The following parameters can be configured for a queue:
136 .Pp
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.
142 .Pp
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.
146 .El
147 .Pp
148 Finally, the following parameters can be configured for both
149 pipes and queues:
150 .Pp
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
154 various queues.
155 Default value is 64 controlled by the
156 .Xr sysctl 8
157 variable
158 .Em net.inet.ip.dummynet.hash_size ,
159 allowed range is 16 to 65536.
160 .Pp
161 .It Cm mask Ar mask-specifier
162 Packets sent to a given pipe or queue by an
163 .Nm
164 rule can be further classified into multiple flows, each of which is then
165 sent to a different
166 .Em dynamic
167 pipe or queue.
168 A flow identifier is constructed by masking the IP addresses,
169 ports and protocol types as specified with the
170 .Cm mask
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
174 are sent to it.
175 .Pp
176 Thus, when
177 .Em dynamic pipes
178 are used, each flow will get the same bandwidth as defined by the pipe,
179 whereas when
180 .Em dynamic queues
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).
184 .br
185 Available mask specifiers are a combination of one or more of the following:
186 .Pp
187 .Cm dst-ip Ar mask ,
188 .Cm dst-ip6 Ar mask ,
189 .Cm src-ip Ar mask ,
190 .Cm src-ip6 Ar mask ,
191 .Cm dst-port Ar mask ,
192 .Cm src-port Ar mask ,
193 .Cm proto Ar mask
194 or
195 .Cm all ,
196 .Pp
197 where the latter means all bits in all fields are significant.
198 .Pp
199 .It Cm noerror
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.
206 .Pp
207 .It Cm plr Ar packet-loss-rate
208 Packet loss rate.
209 Argument
210 .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.
214 .Pp
215 .It Cm queue Brq Ar slots | size Ns Cm Kbytes
216 Queue size, in
217 .Ar slots
218 or
219 .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
224 queueing delay.
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.
230 .Pp
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.
233 .Ar w_q
234 and
235 .Ar max_p
236 are floating
237 point numbers between 0 and 1 (0 not included), while
238 .Ar min_th
239 and
240 .Ar max_th
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).
244 The
245 .Xr dummynet 4
246 also supports the gentle RED variant (gred).
247 .Pp
248 Three
249 .Xr sysctl 8
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
257 greater than zero)
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).
261 .El
262 .El
263 .Pp
264 The following options are available:
265 .Bl -tag -width indent
266 .It Fl a
267 While listing, show counter values.
268 The
269 .Cm show
270 command just implies this option.
271 .It Fl f
272 Don't ask for confirmation for commands that can cause problems
273 if misused,
274 .No i.e. Cm flush .
275 If there is no tty associated with the process, this is implied.
276 .It Fl h
277 Displays a short help.
278 .It Fl n
279 Only check syntax of the command strings, without actually passing
280 them to the kernel.
281 .It Fl q
282 While
283 .Cm add Ns ing ,
284 .Cm zero Ns ing ,
285 .Cm resetlog Ns ging
286 or
287 .Cm flush Ns ing ,
288 be quiet about actions
289 (implies
290 .Fl f ) .
291 This is useful for adjusting rules by executing multiple
292 .Nm
293 commands in a script
294 or by processing a file of many
295 .Nm
296 rules across a remote login session.
297 If a
298 .Cm flush
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.
305 .It Fl s Op Ar field
306 While listing pipes, sort according to one of the four
307 counters (total or current packets or bytes).
308 .It Fl v
309 Be verbose.
310 .El
311 .Pp
312 To ease configuration, rules can be put into a file which is
313 processed using
314 .Nm
315 as shown in the last synopsis line.
316 An absolute
317 .Ar pathname
318 must be used.
319 The file will be read line by line and applied as arguments to the
320 .Nm
321 utility.
322 .Pp
323 Optionally, a preprocessor can be specified using
324 .Fl p Ar preproc
325 where
326 .Ar pathname
327 is to be piped through.
328 Useful preprocessors include
329 .Xr cpp 1
330 and
331 .Xr m4 1 .
332 If
333 .Ar preproc
334 doesn't start with a slash
335 .Pq Ql /
336 as its first character, the usual
337 .Ev PATH
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
341 .Nm
342 is being run (e.g. when they are mounted over NFS).
343 Once
344 .Fl p
345 has been specified, any additional arguments as passed on to the preprocessor
346 for interpretation.
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.
350 .El
351 .Sh CHECKLIST
352 Here are some important points to consider when designing your
353 rules:
354 .Bl -bullet
355 .It
356 Remember that you filter both packets going
357 .Cm in
358 and
359 .Cm out .
360 Most connections need packets going in both directions.
361 .It
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 .
367 .It
368 Don't forget the loopback interface.
369 .El
370 .Sh SYSCTL VARIABLES
371 A set of
372 .Xr sysctl 8
373 variables controls the behaviour of the
374 .Nm dummynet
375 module.
376 These are shown below together with their default value
377 (but always check with the
378 .Xr sysctl 8
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
388 .Cm buckets
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.
392 The product
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.
402 .El
403 .Sh EXAMPLES
404 The following rules show some of the applications of
405 .Xr dummynet 4
406 for simulations and the like. The examples use the obsolete
407 .Nm ipfw
408 command for illustration only as the use of
409 .Nm ipfw
410 is discouraged.
411 .Pp
412 This rule drops random incoming packets with a probability
413 of 5%:
414 .Pp
415 .Dl "ipfw add pipe 10 ip from any to any"
416 .Dl "dnctl pipe 10 config plr 0.05"
417 .Pp
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:
421 .Pp
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"
424 .Pp
425 note that we use the
426 .Cm out
427 modifier so that the rule is not used twice.
428 Remember in fact that
429 .Nm
430 rules are checked both on incoming and outgoing packets.
431 .Pp
432 Should we want to simulate a bidirectional link with bandwidth
433 limitations, the correct way is the following:
434 .Pp
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"
439 .Pp
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,
445 Ethernet, IRDA).
446 It is not necessary that both pipes have the same configuration,
447 so we can also simulate asymmetric links.
448 .Pp
449 Should we want to verify network performance with the RED queue
450 management algorithm:
451 .Pp
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"
454 .Pp
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
460 bandwidth:
461 .Pp
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"
466 .Pp
467 Per-flow queueing can be useful for a variety of purposes.
468 A very simple one is counting traffic:
469 .Pp
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"
474 .Pp
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
480 when
481 .Nm
482 tries to match IP packets it will not consider ports, so we
483 would not see connections on separate ports as different
484 ones.
485 .Pp
486 A more sophisticated example is limiting the outbound traffic
487 on a net with per-host limits, rather than per-network limits:
488 .Pp
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"
493 .Ss SETS OF RULES
494 To add a set of rules atomically, e.g. set 18:
495 .Pp
496 .Dl "ipfw set disable 18"
497 .Dl "ipfw add NN set 18 ... # repeat as needed"
498 .Dl "ipfw set enable 18"
499 .Pp
500 To delete a set of rules atomically the command is simply:
501 .Pp
502 .Dl "ipfw delete set 18"
503 .Pp
504 To test a ruleset and disable it and regain control if something goes wrong:
505 .Pp
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"
509 .Pp
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.
514 .Sh SEE ALSO
515 .Xr cpp 1 ,
516 .Xr m4 1 ,
517 .Xr dummynet 4 ,
518 .Xr ip 4 ,
519 .Xr ipfirewall 4 ,
520 .Xr protocols 5 ,
521 .Xr services 5 ,
522 .Xr sysctl 8
523 .Sh AUTHORS
524 .An Ugen J. S. Antsilevich ,
525 .An Poul-Henning Kamp ,
526 .An Alex Nash ,
527 .An Archie Cobbs ,
528 .An Luigi Rizzo .
529 .Pp
530 .An -nosplit
531 API based upon code written by
532 .An Daniel Boulet
533 for BSDI.
534 .Pp
535 Work on
536 .Xr dummynet 4
537 traffic shaper supported by Akamba Corp.
538 .Sh HISTORY
539 The
540 .Nm
541 utility first appeared in
542 .Fx 2.0 .
543 .Xr dummynet 4
544 was introduced in
545 .Fx 2.2.8 .
546 Stateful extensions were introduced in
547 .Fx 4.0 .
548 .Nm ipfw2
549 was introduced in Summer 2002.