Fix some acpi manpage references that do not exist on our current
[dragonfly.git] / en / books / handbook / config / chapter.sgml
1 <!--
2      The FreeBSD Documentation Project
3
4      $FreeBSD: doc/en_US.ISO8859-1/books/handbook/config/chapter.sgml,v 1.154 2004/06/08 11:41:49 den Exp $
5      $DragonFly: doc/en/books/handbook/config/chapter.sgml,v 1.3 2004/08/25 20:42:15 justin Exp $
6 -->
7
8 <chapter id="config-tuning">
9   <chapterinfo>
10     <authorgroup>
11       <author>
12         <firstname>Chern</firstname>
13         <surname>Lee</surname>
14         <contrib>Written by </contrib>
15       </author>
16     </authorgroup>
17     <authorgroup>
18       <author>
19         <firstname>Mike</firstname>
20         <surname>Smith</surname>
21         <contrib>Based on a tutorial written by </contrib>
22       </author>
23     </authorgroup>
24     <authorgroup>
25       <author>
26         <firstname>Matt</firstname>
27         <surname>Dillon</surname>
28         <contrib>Also based on tuning(7) written by </contrib>
29       </author>
30     </authorgroup>
31   </chapterinfo>
32
33   <title>Configuration and Tuning</title>
34
35   <sect1 id="config-synopsis">
36     <title>Synopsis</title>
37
38     <indexterm><primary>system configuration</primary></indexterm>
39     <indexterm><primary>system optimization</primary></indexterm>
40
41     <para>One of the important aspects of &os; is system configuration.
42       Correct system configuration will help prevent headaches during future upgrades.
43       This chapter will explain much of the &os; configuration process,
44       including some of the parameters which
45       can be set to tune a &os; system.
46       </para>
47
48     <para>After reading this chapter, you will know:</para>
49
50     <itemizedlist>
51       <listitem>
52         <para>How to efficiently work with
53           file systems and swap partitions.</para>
54       </listitem>
55       <listitem>
56         <para>The basics of <filename>rc.conf</filename> configuration and
57           <filename>/usr/local/etc/rc.d</filename> startup systems.</para>
58       </listitem>
59       <listitem>
60         <para>How to configure and test a network card.</para>
61       </listitem>
62       <listitem>
63         <para>How to configure virtual hosts on your network devices.</para>
64       </listitem>
65       <listitem>
66         <para>How to use the various configuration files in
67           <filename>/etc</filename>.</para>
68       </listitem>
69       <listitem>
70         <para>How to tune &os; using <command>sysctl</command>
71           variables.</para>
72       </listitem>
73       <listitem>
74         <para>How to tune disk performance and modify kernel
75           limitations.</para>
76       </listitem>
77     </itemizedlist>
78
79     <para>Before reading this chapter, you should:</para>
80
81     <itemizedlist>
82       <listitem>
83         <para>Understand &unix; and &os; basics (<xref
84             linkend="basics">).</para>
85       </listitem>
86       <listitem>
87         <para>Be familiar with keeping &os; sources up to date
88           (<xref linkend="updating-setup">), and
89           the basics of kernel configuration/compilation
90           (<xref linkend="kernelconfig">).</para>
91       </listitem>
92     </itemizedlist>
93   </sect1>
94
95   <sect1 id="configtuning-initial">
96     <title>Initial Configuration</title>
97
98     <sect2>
99       <title>Partition Layout</title>
100
101       <indexterm><primary>partition layout</primary></indexterm>
102       <indexterm>
103         <primary><filename>/etc</filename></primary>
104       </indexterm>
105       <indexterm>
106         <primary><filename>/var</filename></primary>
107       </indexterm>
108       <indexterm>
109         <primary><filename>/usr</filename></primary>
110       </indexterm>
111
112       <sect3>
113         <title>Base Partitions</title>
114
115         <para>When laying out file systems with &man.disklabel.8;
116           or &man.sysinstall.8;, remember that hard
117           drives transfer data faster from the outer
118           tracks to the inner.
119           Thus smaller and heavier-accessed file systems
120           should be closer to the outside of the drive, while
121           larger partitions like <filename>/usr</filename> should be placed
122           toward the inner.  It is a good idea to create
123           partitions in a similar order to: root, swap,
124           <filename>/var</filename>, <filename>/usr</filename>.</para>
125
126         <para>The size of <filename>/var</filename>
127           reflects the intended machine usage.
128           <filename>/var</filename> is used to hold
129           mailboxes, log files, and printer spools.  Mailboxes and log
130           files can grow to unexpected sizes depending
131           on how many users exist and how long log
132           files are kept.  Most users would never require a gigabyte,
133           but remember that <filename>/var/tmp</filename>
134           must be large enough to contain packages.
135           </para>
136
137         <para>The <filename>/usr</filename> partition holds much
138           of the files required to support the system, the &man.ports.7;
139           collection (recommended) and the source code (optional).  Both
140           of which are optional at install time.
141           At least 2 gigabytes would be recommended for this partition.</para>
142
143         <para>When selecting partition sizes, keep the space
144           requirements in mind.  Running out of space in
145           one partition while barely using another can be a
146           hassle.</para>
147
148         <note><para>Some users have found that &man.sysinstall.8;'s
149             <literal>Auto-defaults</literal> partition sizer will
150             sometimes select smaller than adequate <filename>/var</filename>
151             and <filename>/</filename> partitions.  Partition wisely and
152             generously.</para></note>
153
154       </sect3>
155
156       <sect3 id="swap-design">
157         <title>Swap Partition</title>
158
159         <indexterm><primary>swap sizing</primary></indexterm>
160         <indexterm><primary>swap partition</primary></indexterm>
161
162         <para>As a rule of thumb, the swap partition should be
163           about double the size of system memory (RAM).  For example,
164           if the machine has 128&nbsp;megabytes of memory,
165           the swap file should be 256&nbsp;megabytes.  Systems with
166           less memory may perform better with more swap.
167           Less than 256&nbsp;megabytes of swap is not recommended and
168           memory expansion should be considered.
169           The kernel's VM paging algorithms are tuned to
170           perform best when the swap partition is at least two times the
171           size of main memory.  Configuring too little swap can lead to
172           inefficiencies in the VM page scanning code and might create
173           issues later if more memory is added.</para>
174
175         <para>On larger systems with multiple SCSI disks (or
176           multiple IDE disks operating on different controllers), it is
177           recommend that a swap is configured on each drive (up
178           to four drives).  The swap partitions should be
179           approximately the same size.  The kernel can handle arbitrary
180           sizes but internal data structures scale to 4 times the
181           largest swap partition.  Keeping the swap partitions near the
182           same size will allow the kernel to optimally stripe swap space
183           across disks.
184           Large swap sizes are fine, even if swap is not
185           used much.  It might be easier to recover
186           from a runaway program before being forced to reboot.</para>
187       </sect3>
188
189       <sect3>
190         <title>Why Partition?</title>
191
192         <para>Several users think a single large partition will be fine,
193           but there are several reasons why this is a bad idea.
194           First, each partition has different operational
195           characteristics and separating them allows the file system to
196           tune accordingly.  For example, the root
197           and <filename>/usr</filename> partitions are read-mostly, without
198           much writing.  While a lot of reading and writing could
199           occur in <filename>/var</filename> and
200           <filename>/var/tmp</filename>.</para>
201
202         <para>By properly partitioning a system, fragmentation
203           introduced in the smaller write heavy partitions
204           will not bleed over into the mostly-read partitions.
205           Keeping the write-loaded partitions closer to
206           the disk's edge,
207           will
208           increase I/O performance in the partitions where it occurs
209           the most.  Now while I/O
210           performance in the larger partitions may be needed,
211           shifting them more toward the edge of the disk will not
212           lead to a significant performance improvement over moving
213           <filename>/var</filename> to the edge.
214           Finally, there are safety concerns.  A smaller, neater root
215           partition which is mostly read-only has a greater
216           chance of surviving a bad crash.</para>
217       </sect3>
218     </sect2>
219
220   </sect1>
221
222   <sect1 id="configtuning-core-configuration">
223     <title>Core Configuration</title>
224
225     <indexterm>
226       <primary>rc files</primary>
227       <secondary><filename>rc.conf</filename></secondary>
228     </indexterm>
229
230     <para>The principal location for system configuration information
231       is within <filename>/etc/rc.conf</filename>.  This file
232       contains a wide range of configuration information, principally
233       used at system startup to configure the system.  Its name
234       directly implies this; it is configuration information for the
235       <filename>rc*</filename> files.</para>
236
237     <para>An administrator should make entries in the
238       <filename>rc.conf</filename> file to
239       override the default settings from
240       <filename>/etc/defaults/rc.conf</filename>.  The defaults file
241       should not be copied verbatim to <filename>/etc</filename> - it
242       contains default values, not examples.  All system-specific
243       changes should be made in the <filename>rc.conf</filename>
244       file itself.</para>
245
246     <para>A number of strategies may be applied in clustered
247       applications to separate site-wide configuration from
248       system-specific configuration in order to keep administration
249       overhead down.  The recommended approach is to place site-wide
250       configuration into another file,
251       such as <filename>/etc/rc.conf.site</filename>, and then include
252       this file into <filename>/etc/rc.conf</filename>, which will
253       contain only system-specific information.</para>
254
255     <para>As <filename>rc.conf</filename> is read by &man.sh.1; it is
256       trivial to achieve this.  For example:</para>
257
258     <itemizedlist>
259       <listitem><para>rc.conf:</para>
260 <programlisting>        . rc.conf.site
261         hostname="node15.example.com"
262         network_interfaces="fxp0 lo0"
263         ifconfig_fxp0="inet 10.1.1.1"</programlisting></listitem>
264       <listitem><para>rc.conf.site:</para>
265 <programlisting>        defaultrouter="10.1.1.254"
266         saver="daemon"
267         blanktime="100"</programlisting></listitem>
268     </itemizedlist>
269
270     <para>The <filename>rc.conf.site</filename> file can then be
271       distributed to every system using <command>rsync</command> or a
272       similar program, while the <filename>rc.conf</filename> file
273       remains unique.</para>
274
275     <para>Upgrading the system using &man.sysinstall.8;
276       or <command>make world</command> will not overwrite the
277       <filename>rc.conf</filename>
278       file, so system configuration information will not be lost.</para>
279
280   </sect1>
281
282   <sect1 id="configtuning-appconfig">
283     <title>Application Configuration</title>
284
285     <para>Typically, installed applications have their own
286       configuration files, with their own syntax, etc.  It is
287       important that these files be kept separate from the base
288       system, so that they may be easily located and managed by the
289       package management tools.</para>
290
291     <indexterm><primary>/usr/local/etc</primary></indexterm>
292
293     <para>Typically, these files are installed in
294       <filename>/usr/local/etc</filename>.  In the case where an
295       application has a large number of configuration files, a
296       subdirectory will be created to hold them.</para>
297
298     <para>Normally, when a port or package is installed, sample
299       configuration files are also installed.  These are usually
300       identified with a <filename>.default</filename> suffix.  If there
301       are no existing
302       configuration files for the application, they will be created by
303       copying the <filename>.default</filename> files.</para>
304
305     <para>For example, consider the contents of the directory
306     <filename>/usr/local/etc/apache</filename>:</para>
307
308 <literallayout class="monospaced">-rw-r--r--  1 root  wheel   2184 May 20  1998 access.conf
309 -rw-r--r--  1 root  wheel   2184 May 20  1998 access.conf.default
310 -rw-r--r--  1 root  wheel   9555 May 20  1998 httpd.conf
311 -rw-r--r--  1 root  wheel   9555 May 20  1998 httpd.conf.default
312 -rw-r--r--  1 root  wheel  12205 May 20  1998 magic
313 -rw-r--r--  1 root  wheel  12205 May 20  1998 magic.default
314 -rw-r--r--  1 root  wheel   2700 May 20  1998 mime.types
315 -rw-r--r--  1 root  wheel   2700 May 20  1998 mime.types.default
316 -rw-r--r--  1 root  wheel   7980 May 20  1998 srm.conf
317 -rw-r--r--  1 root  wheel   7933 May 20  1998 srm.conf.default</literallayout>
318
319     <para>The file sizes show that only the <filename>srm.conf</filename>
320       file has been changed.  A later update of the <application>Apache</application> port would not
321       overwrite this changed file.</para>
322
323   </sect1>
324
325   <sect1 id="configtuning-starting-services">
326     <title>Starting Services</title>
327
328     <indexterm><primary>services</primary></indexterm>
329
330     <para>It is common for a system to host a number of services.
331       These may be started in several different fashions, each having
332       different advantages.</para>
333
334     <indexterm><primary>/usr/local/etc/rc.d</primary></indexterm>
335
336     <para>Software installed from a port or the packages collection
337       will often place a script in
338       <filename>/usr/local/etc/rc.d</filename> which is invoked at
339       system startup with a <option>start</option> argument, and at
340       system shutdown with a <option>stop</option> argument.
341       This is the recommended way for
342       starting system-wide services that are to be run as
343       <username>root</username>, or that
344       expect to be started as <username>root</username>.
345       These scripts are registered as
346       part of the installation of the package, and will be removed
347       when the package is removed.</para>
348
349     <para>A generic startup script in
350       <filename>/usr/local/etc/rc.d</filename> looks like:</para>
351
352     <programlisting>#!/bin/sh
353 echo -n ' FooBar'
354
355 case "$1" in
356 start)
357         /usr/local/bin/foobar
358         ;;
359 stop)
360         kill -9 `cat /var/run/foobar.pid`
361         ;;
362 *)
363         echo "Usage: `basename $0` {start|stop}" >&2
364         exit 64
365         ;;
366 esac
367
368 exit 0
369     </programlisting>
370
371     <para>The startup scripts of &os; will look in
372       <filename>/usr/local/etc/rc.d</filename> for scripts that have an
373       <literal>.sh</literal> extension and are executable by
374       <username>root</username>.  Those scripts that are found are called with
375       an option <option>start</option> at startup, and <option>stop</option>
376       at shutdown to allow them to carry out their purpose.  So if you wanted
377       the above sample script to be picked up and run at the proper time during
378       system startup, you should save it to a file called
379       <filename>FooBar.sh</filename> in
380       <filename>/usr/local/etc/rc.d</filename> and make sure it is
381       executable.  You can make a shell script executable with &man.chmod.1;
382       as shown below:</para>
383
384     <screen>&prompt.root; <userinput>chmod 755 <replaceable>FooBar.sh</replaceable></userinput></screen>
385
386     <para>Some services expect to be invoked by &man.inetd.8; when a
387       connection is received on a suitable port.  This is common for
388       mail reader servers (POP and IMAP, etc.).  These services are
389       enabled by editing the file <filename>/etc/inetd.conf</filename>.
390       See &man.inetd.8; for details on editing this file.</para>
391
392     <para>Some additional system services may not be covered by the
393       toggles in <filename>/etc/rc.conf</filename>.  These are
394       traditionally enabled by placing the command(s) to invoke them
395       in <filename>/etc/rc.local</filename>.  As of &os;&nbsp;3.1 there
396       is no default <filename>/etc/rc.local</filename>; if it is
397       created by the administrator it will however be honored in the
398       normal fashion.  Note that <filename>rc.local</filename> is
399       generally regarded as the location of last resort; if there is a
400       better place to start a service, do it there.</para>
401
402     <note><para>Do <emphasis>not</emphasis> place any commands in
403       <filename>/etc/rc.conf</filename>.  To start daemons, or
404       run any commands at boot time, place a script in
405       <filename>/usr/local/etc/rc.d</filename> instead.</para>
406     </note>
407
408     <para>It is also possible to use the &man.cron.8; daemon to start
409       system services.  This approach has a number of advantages, not
410       least being that because &man.cron.8; runs these processes as the
411       owner of the <command>crontab</command>, services may be started
412       and maintained by non-<username>root</username> users.</para>
413
414     <para>This takes advantage of a feature of &man.cron.8;: the
415       time specification may be replaced by <literal>@reboot</literal>,
416       which will
417       cause the job to be run when &man.cron.8; is started shortly after
418       system boot.</para>
419   </sect1>
420
421   <sect1 id="configtuning-cron">
422     <sect1info>
423       <authorgroup>
424         <author>
425         <firstname>Tom</firstname>
426         <surname>Rhodes</surname>
427         <contrib>Contributed by </contrib>
428         <!-- 20 May 2003 -->
429         </author>
430       </authorgroup>
431     </sect1info>
432     <title>Configuring the <command>cron</command> Utility</title>
433
434     <indexterm><primary>cron</primary>
435       <secondary>configuration</secondary></indexterm>
436
437     <para>One of the most useful utilities in &os; is &man.cron.8;.  The
438       <command>cron</command> utility runs in the background and constantly
439       checks the <filename>/etc/crontab</filename> file.  The <command>cron</command>
440       utility also checks the <filename>/var/cron/tabs</filename> directory, in
441       search of new <filename>crontab</filename> files.  These
442       <filename>crontab</filename> files store information about specific
443       functions which <command>cron</command> is supposed to perform at
444       certain times.</para>
445
446     <para>The <command>cron</command> utility uses two different
447       types of configuration files, the system crontab and user crontabs. The
448       only difference between these two formats is the sixth field.  In the
449       system crontab, the sixth field is the name of a user for the command
450       to run as. This gives the system crontab the ability to run commands
451       as any user. In a user crontab, the sixth field is the command to run,
452       and all commands run as the user who created the crontab; this is an
453       important security feature.</para>
454
455     <note>
456       <para>User crontabs allow individual users to schedule tasks without the 
457         need for root privileges. Commands in a user's crontab run with the 
458         permissions of the user who owns the crontab.</para>
459
460       <para>The <username>root</username> user can have a user crontab just like
461         any other user. This one is different from
462         <filename>/etc/crontab</filename> (the system crontab). Because of the
463         system crontab, there's usually no need to create a user crontab
464         for <username>root</username>.</para>
465     </note>
466
467     <para>Let us take a look at the <filename>/etc/crontab</filename> file
468       (the system crontab):</para>
469
470
471     <programlisting># /etc/crontab - root's crontab for &os;
472 #
473 # &dollar;&os;: src/etc/crontab,v 1.32 2002/11/22 16:13:39 tom Exp &dollar;
474 # <co id="co-comments">
475 #
476 SHELL=/bin/sh
477 PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin <co id="co-env">
478 HOME=/var/log
479 #
480 #
481 #minute hour    mday    month   wday    who     command <co id="co-field-descr">
482 #
483 #
484 */5     *       *       *       *       root    /usr/libexec/atrun <co id="co-main">
485 </programlisting>
486
487     <calloutlist>
488       <callout arearefs="co-comments">
489         <para>Like most &os; configuration files, the <literal>#</literal>
490           character represents a comment.  A comment can be placed in
491           the file as a reminder of what and why a desired action is performed.
492           Comments cannot be on the same line as a command or else they will
493           be interpreted as part of the command; they must be on a new line.
494           Blank lines are ignored.</para>
495       </callout>
496
497       <callout arearefs="co-env">
498         <para>First, the environment must be defined.  The equals
499           (<literal>=</literal>) character is used to define any environment
500           settings, as with this example where it is used for the <envar>SHELL</envar>,
501           <envar>PATH</envar>, and <envar>HOME</envar> options.  If the shell line is
502           omitted, <command>cron</command> will use the default, which is
503           <command>sh</command>.  If the <envar>PATH</envar> variable is
504           omitted, no default will be used and file locations will need to
505           be absolute.  If <envar>HOME</envar> is omitted, <command>cron</command>
506           will use the invoking users home directory.</para>
507       </callout>
508
509       <callout arearefs="co-field-descr">
510         <para>This line defines a total of seven fields.  Listed here are the
511           values <literal>minute</literal>, <literal>hour</literal>,
512           <literal>mday</literal>, <literal>month</literal>, <literal>wday</literal>,
513           <literal>who</literal>, and <literal>command</literal>.  These
514           are almost all self explanatory.  <literal>minute</literal> is the time in minutes the
515           command will be run.  <literal>hour</literal> is similar to the <literal>minute</literal> option, just in
516           hours.  <literal>mday</literal> stands for day of the month.  <literal>month</literal> is similar to <literal>hour</literal>
517           and <literal>minute</literal>, as it designates the month.  The <literal>wday</literal> option stands for
518           day of the week.  All these fields must be numeric values, and follow
519           the twenty-four hour clock.  The <literal>who</literal> field is special,
520           and only exists in the <filename>/etc/crontab</filename> file.
521           This field specifies which user the command should be run as.
522           When a user installs his or her <filename>crontab</filename> file, they
523           will not have this option. Finally, the <literal>command</literal> option is listed.
524           This is the last field, so naturally it should designate the command
525           to be executed.</para>
526       </callout>
527
528       <callout arearefs="co-main">
529         <para>This last line will define the values discussed above.  Notice here
530           we have a <literal>*/5</literal> listing, followed by several more
531           <literal>*</literal> characters.  These <literal>*</literal> characters
532           mean <quote>first-last</quote>, and can be interpreted as
533           <emphasis>every</emphasis> time.  So, judging by this line,
534           it is apparent that the <command>atrun</command> command is to be invoked by
535           <username>root</username> every five minutes regardless of what
536           day or month it is.  For more information on the <command>atrun</command> command,
537           see the &man.atrun.8; manual page.</para>
538
539         <para>Commands can have any number of flags passed to them; however,
540           commands which extend to multiple lines need to be broken with the backslash
541           <quote>\</quote> continuation character.</para>
542       </callout>
543     </calloutlist>
544
545     <para>This is the basic set up for every
546       <filename>crontab</filename> file, although there is one thing
547       different about this one.  Field number six, where we specified
548       the username, only exists in the system
549       <filename>/etc/crontab</filename> file.  This field should be
550       omitted for individual user <filename>crontab</filename>
551       files.</para>
552
553
554     <sect2 id="configtuning-installcrontab">
555       <title>Installing a Crontab</title>
556
557       <important>
558       <para>You must not use the procedure described here to
559         edit/install the system crontab. Simply use your favorite
560         editor: the <command>cron</command> utility will notice that the file
561         has changed and immediately begin using the updated version. 
562         See
563         <ulink url="http://www.freebsd.org/doc/en_US.ISO8859-1/books/faq/admin.html#ROOT-NOT-FOUND-CRON-ERRORS">
564         this FAQ entry </ulink> for more information.</para>
565       </important>
566
567       <para>To install a freshly written user
568         <filename>crontab</filename>, first use your favorite editor to create
569         a file in the proper format, and then use the
570         <command>crontab</command> utility.  The most common usage
571         is:</para>
572
573       <screen>&prompt.user; <userinput>crontab crontab-file</userinput></screen>
574       
575       <para>In this example, <filename>crontab-file</filename> is the filename
576     of a <filename>crontab</filename> that was previously created.</para>
577
578       <para>There is also an option to list installed
579         <filename>crontab</filename> files: just pass the
580         <option>-l</option> option to <command>crontab</command> and look
581         over the output.</para>
582
583       <para>For users who wish to begin their own crontab file from scratch,
584         without the use of a template, the <command>crontab -e</command>
585         option is available.  This will invoke the selected editor
586         with an empty file.  When the file is saved, it will be
587         automatically installed by the <command>crontab</command> command.
588       </para>
589       
590       <para>If you later want to remove your user <filename>crontab</filename>
591         completely, use <command>crontab</command> with the <option>-r</option>
592         option.
593       </para>
594
595     </sect2>
596   </sect1>
597
598   <sect1 id="configtuning-rcNG">
599     <sect1info>
600       <authorgroup>
601         <author>
602          <firstname>Tom</firstname>
603          <surname>Rhodes</surname>
604          <contrib>Contributed by </contrib>
605          <!-- 16 May 2003 -->
606         </author>
607       </authorgroup>
608     </sect1info>
609
610     <title>Using rc under &os;</title>
611
612     <indexterm><primary>rcNG</primary></indexterm>
613
614     <para>&os; uses the NetBSD
615       <filename>rc.d</filename> system for system initialization.
616       Users should notice the files listed in the
617       <filename>/etc/rc.d</filename> directory.  Many of these files
618       are for basic services which can be controlled with the
619       <option>start</option>, <option>stop</option>,
620       and <option>restart</option> options.
621       For instance, &man.sshd.8; can be restarted with the following
622       command:</para>
623
624     <screen>&prompt.root; <userinput>/etc/rc.d/sshd restart</userinput></screen>
625
626     <para>This procedure is similar for other services.  Of course,
627       services are usually started automatically as specified in
628       &man.rc.conf.5;.  For example, enabling the Network Address
629       Translation daemon at startup is as simple as adding the
630       following line to <filename>/etc/rc.conf</filename>:</para>
631
632     <programlisting>natd_enable="YES"</programlisting>
633
634     <para>If a <option>natd_enable="NO"</option> line is already
635        present, then simply change the <option>NO</option> to
636        <option>YES</option>.  The rc scripts will automatically load
637        any other dependent services during the next reboot, as
638        described below.</para>
639
640     <para>Since the <filename>rc.d</filename> system is primarily
641       intended to start/stop services at system startup/shutdown time,
642       the standard <option>start</option>,
643       <option>stop</option> and <option>restart</option> options will only
644       perform their action if the appropriate
645       <filename>/etc/rc.conf</filename> variables are set.  For
646       instance the above <command>sshd restart</command> command will
647       only work if <varname>sshd_enable</varname> is set to
648       <option>YES</option> in <filename>/etc/rc.conf</filename>.  To
649       <option>start</option>, <option>stop</option> or
650       <option>restart</option> a service regardless of the settings in
651       <filename>/etc/rc.conf</filename>, the commands should be
652       prefixed with <quote>force</quote>.  For instance to restart
653       <command>sshd</command> regardless of the current
654       <filename>/etc/rc.conf</filename> setting, execute the following
655       command:</para>
656
657     <screen>&prompt.root; <userinput>/etc/rc.d/sshd forcerestart</userinput></screen>
658
659     <para>It is easy to check if a service is enabled in
660       <filename>/etc/rc.conf</filename> by running the appropriate
661       <filename>rc.d</filename> script with the option
662       <option>rcvar</option>.  Thus, an administrator can check that
663       <command>sshd</command> is in fact enabled in
664       <filename>/etc/rc.conf</filename> by running:</para>
665
666     <screen>&prompt.root; <userinput>/etc/rc.d/sshd rcvar</userinput>
667 # sshd
668 $sshd_enable=YES</screen>
669
670     <note>
671       <para>The second line (<literal># sshd</literal>) is the output
672         from the <command>sshd</command> command, not a <username>root</username>
673         console.</para>
674     </note>
675
676     <para>To determine if a service is running, a
677       <option>status</option> option is available.  For instance to
678       verify that <command>sshd</command> is actually started:</para>
679
680     <screen>&prompt.root; <userinput>/etc/rc.d/sshd status</userinput>
681 sshd is running as pid 433.</screen>
682
683     <para>It is also possible to <option>reload</option> a service.
684       This will attempt to send a signal to an individual service, forcing the
685       service to reload its configuration files.  In most cases this
686       means sending the service a <literal>SIGHUP</literal>
687       signal.</para>
688
689     <para>The <application>rcNG</application> structure is used both 
690       for network services and system initialization.  Some services are run 
691       only at boot; and the RCNG system is what triggers them.
692
693     <para>Many system services depend on other services to function
694       properly.  For example, NIS and other RPC-based services may
695       fail to start until after the <command>rpcbind</command>
696       (portmapper) service has started.  To resolve this issue,
697       information about dependencies and other meta-data is included
698       in the comments at the top of each startup script.  The
699       &man.rcorder.8; program is then used to parse these comments
700       during system initialization to determine the order in which
701       system services should be invoked to satisfy the dependencies.
702       The following words may be included at the top of each startup
703       file:</para>
704
705     <itemizedlist>
706       <listitem>
707         <para><literal>PROVIDE</literal>: Specifies the services this file provides.</para>
708       </listitem>
709
710       <listitem>
711         <para><literal>REQUIRE</literal>: Lists services which are required for this
712           service.  This file will run <emphasis>after</emphasis>
713           the specified services.</para>
714       </listitem>
715
716       <listitem>
717         <para><literal>BEFORE</literal>: Lists services which depend on this service.
718           This file will run <emphasis>before</emphasis>
719           the specified services.</para>
720       </listitem>
721
722       <listitem>
723         <para>KEYWORD: &os; or NetBSD.  This is used for *BSD dependent features.</para>
724       </listitem>
725     </itemizedlist>
726
727     <para>By using this method, an administrator can easily control system
728       services without the hassle of <quote>runlevels</quote> like
729       some other &unix; operating systems.</para>
730
731     <para>Additional information about the &os; 
732       <filename>rc.d</filename> system can be found in the &man.rc.8;
733       and &man.rc.subr.8; manual pages.</para>
734   </sect1>
735
736   <sect1 id="config-network-setup">
737     <sect1info>
738       <authorgroup>
739         <author>
740          <firstname>Marc</firstname>
741          <surname>Fonvieille</surname>
742          <contrib>Contributed by </contrib>
743          <!-- 6 October 2002 -->
744         </author>
745       </authorgroup>
746     </sect1info>
747
748     <title>Setting Up Network Interface Cards</title>
749
750     <indexterm><primary>network card configuration</primary></indexterm>
751
752     <para>Nowadays we can not think about a computer without thinking
753       about a network connection.  Adding and configuring a network
754       card is a common task for any &os; administrator.</para>
755
756     <sect2>
757       <title>Locating the Correct Driver</title>
758
759       <indexterm>
760         <primary>network card configuration</primary>
761         <secondary>locating the driver</secondary>
762       </indexterm>
763
764       <para>Before you begin, you should know the model of the card
765         you have, the chip it uses, and whether it is a PCI or ISA card.
766         &os; supports a wide variety of both PCI and ISA cards.
767         Check the Hardware Compatibility List for your release to see
768         if your card is supported.</para>
769
770       <para>Once you are sure your card is supported, you need
771         to determine the proper driver for the card.  The file
772         <filename>/usr/src/sys/i386/conf/LINT</filename> will give you
773         the list of network interfaces drivers with some information
774         about the supported chipsets/cards.  If you have doubts about
775         which driver is the correct one, read the manual page of the
776         driver.  The manual page will give you more information about
777         the supported hardware and even the possible problems that
778         could occur.</para>
779
780       <para>If you own a common card, most of the time you will not
781         have to look very hard for a driver.  Drivers for common
782         network cards are present in the <filename>GENERIC</filename>
783         kernel, so your card should show up during boot, like so:</para>
784
785 <screen>dc0: &lt;82c169 PNIC 10/100BaseTX&gt; port 0xa000-0xa0ff mem 0xd3800000-0xd38
786 000ff irq 15 at device 11.0 on pci0
787 dc0: Ethernet address: 00:a0:cc:da:da:da
788 miibus0: &lt;MII bus&gt; on dc0
789 ukphy0: &lt;Generic IEEE 802.3u media interface&gt; on miibus0
790 ukphy0:  10baseT, 10baseT-FDX, 100baseTX, 100baseTX-FDX, auto
791 dc1: &lt;82c169 PNIC 10/100BaseTX&gt; port 0x9800-0x98ff mem 0xd3000000-0xd30
792 000ff irq 11 at device 12.0 on pci0
793 dc1: Ethernet address: 00:a0:cc:da:da:db
794 miibus1: &lt;MII bus&gt; on dc1
795 ukphy1: &lt;Generic IEEE 802.3u media interface&gt; on miibus1
796 ukphy1:  10baseT, 10baseT-FDX, 100baseTX, 100baseTX-FDX, auto</screen>
797
798       <para>In this example, we see that two cards using the &man.dc.4;
799         driver are present on the system.</para>
800
801       <para>To use your network card, you will need to load the proper
802         driver.  This may be accomplished in one of two ways.  The
803         easiest way is to simply load a kernel module for your network
804         card with &man.kldload.8;.  A module is not available for all
805         network card drivers (ISA cards and cards using the &man.ed.4;
806         driver, for example).  Alternatively, you may statically compile
807         the support for your card into your kernel.  Check
808         <filename>/usr/src/sys/i386/conf/LINT</filename> and the
809         manual page of the driver to know what to add in your kernel
810         configuration file.  For more information about recompiling your
811         kernel, please see <xref linkend="kernelconfig">.  If your card
812         was detected at boot by your kernel (<filename>GENERIC</filename>)
813         you do not have to build a new kernel.</para>
814     </sect2>
815
816     <sect2>
817       <title>Configuring the Network Card</title>
818
819       <indexterm>
820         <primary>Network card configuration</primary>
821         <secondary>configuration</secondary>
822       </indexterm>
823
824       <para>Once the right driver is loaded for the network card, the
825         card needs to be configured.  As with many other things, the
826         network card may have been configured at installation time by
827         <application>sysinstall</application>.</para>
828
829       <para>To display the configuration for the network interfaces on
830         your system, enter the following command:</para>
831
832 <screen>&prompt.user; <userinput>ifconfig</userinput>
833 dc0: flags=8843&lt;UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST&gt; mtu 1500
834         inet 192.168.1.3 netmask 0xffffff00 broadcast 192.168.1.255
835         ether 00:a0:cc:da:da:da
836         media: Ethernet autoselect (100baseTX &lt;full-duplex&gt;)
837         status: active
838 dc1: flags=8843&lt;UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST&gt; mtu 1500
839         inet 10.0.0.1 netmask 0xffffff00 broadcast 10.0.0.255
840         ether 00:a0:cc:da:da:db
841         media: Ethernet 10baseT/UTP
842         status: no carrier
843 lp0: flags=8810&lt;POINTOPOINT,SIMPLEX,MULTICAST&gt; mtu 1500
844 lo0: flags=8049&lt;UP,LOOPBACK,RUNNING,MULTICAST&gt; mtu 16384
845         inet 127.0.0.1 netmask 0xff000000
846 tun0: flags=8010&lt;POINTOPOINT,MULTICAST&gt; mtu 1500</screen>
847
848       <note>
849         <para>Note that entries concerning IPv6
850           (<literal>inet6</literal> etc.) were omitted in this
851           example.</para>
852       </note>
853
854       <para>In this example, the following devices were
855         displayed:</para>
856
857       <itemizedlist>
858         <listitem>
859           <para><devicename>dc0</devicename>: The first Ethernet
860             interface</para>
861         </listitem>
862
863         <listitem>
864           <para><devicename>dc1</devicename>: The second Ethernet
865             interface</para>
866         </listitem>
867
868         <listitem>
869           <para><devicename>lp0</devicename>: The parallel port
870             interface</para>
871         </listitem>
872
873         <listitem>
874           <para><devicename>lo0</devicename>: The loopback device</para>
875         </listitem>
876
877         <listitem>
878           <para><devicename>tun0</devicename>: The tunnel device used by
879             <application>ppp</application></para>
880         </listitem>
881       </itemizedlist>
882
883       <para>&os; uses the driver name followed by the order in
884         which one the card is detected at the kernel boot to name the
885         network card, starting the count at zero.  For example, 
886         <devicename>sis2</devicename> would be the third network card 
887         on the system using the &man.sis.4; driver.</para>
888
889       <para>In this example, the <devicename>dc0</devicename> device is
890         up and running.  The key indicators are:</para>
891
892       <orderedlist>
893         <listitem>
894           <para><literal>UP</literal> means that the card is configured
895             and ready.</para>
896         </listitem>
897
898         <listitem>
899           <para>The card has an Internet (<literal>inet</literal>)
900             address (in this case
901             <hostid role="ipaddr">192.168.1.3</hostid>).</para>
902         </listitem>
903
904         <listitem>
905           <para>It has a valid subnet mask (<literal>netmask</literal>;
906             <hostid role="netmask">0xffffff00</hostid> is the same as
907             <hostid role="netmask">255.255.255.0</hostid>).</para>
908         </listitem>
909
910         <listitem>
911           <para>It has a valid broadcast address (in this case,
912             <hostid role="ipaddr">192.168.1.255</hostid>).</para>
913         </listitem>
914
915         <listitem>
916           <para>The MAC address of the card (<literal>ether</literal>)
917             is <hostid role="mac">00:a0:cc:da:da:da</hostid></para>
918         </listitem>
919
920         <listitem>
921           <para>The physical media selection is on autoselection mode
922             (<literal>media: Ethernet autoselect (100baseTX
923             &lt;full-duplex&gt;)</literal>).  We see that
924             <devicename>dc1</devicename> was configured to run with
925             <literal>10baseT/UTP</literal> media.  For more
926             information on available media types for a driver, please
927             refer to its manual page.</para>
928         </listitem>
929
930         <listitem>
931           <para>The status of the link (<literal>status</literal>)
932             is <literal>active</literal>, i.e. the carrier is detected.
933             For <devicename>dc1</devicename>, we see
934             <literal>status: no carrier</literal>.  This is normal when
935             an ethernet cable is not plugged into the card.</para>
936         </listitem>
937       </orderedlist>
938
939       <para>If the &man.ifconfig.8; output had shown something similar
940         to:</para>
941
942 <screen>dc0: flags=8843&lt;BROADCAST,SIMPLEX,MULTICAST&gt; mtu 1500
943                 ether 00:a0:cc:da:da:da</screen>
944
945       <para>it would indicate the card has not been configured.</para>
946
947       <para>To configure your card, you need <username>root</username>
948         privileges.  The network card configuration can be done from the
949         command line with &man.ifconfig.8; as root.</para>
950         
951 <screen>
952 &prompt.root; <userinput>ifconfig dc0 inet 192.168.1.3 netmask 255.255.255.0</userinput>
953 </screen>
954       
955       
956       <para>Manually configuring the care has the disadvantage that you 
957         would have to do it after each reboot of the system.  The file
958         <filename>/etc/rc.conf</filename> is where to add the network
959         card's configuration.</para>
960
961       <para>Open <filename>/etc/rc.conf</filename> in your favorite
962         editor.  You need to add a line for each network card present on
963         the system, for example in our case, we added these lines:</para>
964
965 <programlisting>ifconfig_dc0="inet 192.168.1.3 netmask 255.255.255.0"
966 ifconfig_dc1="inet 10.0.0.1 netmask 255.255.255.0 media 10baseT/UTP"</programlisting>
967
968       <para>You have to replace <devicename>dc0</devicename>,
969         <devicename>dc1</devicename>, and so on, with
970         the correct device for your cards, and the addresses with the
971         proper ones.  You should read the card driver and
972         &man.ifconfig.8; manual pages for more details about the allowed
973         options and also &man.rc.conf.5; manual page for more
974         information on the syntax of
975         <filename>/etc/rc.conf</filename>.</para>
976
977       <para>If you configured the network during installation, some
978         lines about the network card(s) may be already present.  Double
979         check <filename>/etc/rc.conf</filename> before adding any
980         lines.</para>
981
982       <para>You will also have to edit the file
983         <filename>/etc/hosts</filename> to add the names and the IP
984         addresses of various machines of the LAN, if they are not already
985         there.  For more information please refer to &man.hosts.5;
986         and to <filename>/usr/share/examples/etc/hosts</filename>.</para>
987     </sect2>
988
989     <sect2>
990       <title>Testing and Troubleshooting</title>
991
992       <para>Once you have made the necessary changes in
993         <filename>/etc/rc.conf</filename>, you should reboot your
994         system.  This will allow the change(s) to the interface(s) to
995         be applied, and verify that the system restarts without any
996         configuration errors.</para>
997
998       <para>Once the system has been rebooted, you should test the
999         network interfaces.</para>
1000
1001       <sect3>
1002         <title>Testing the Ethernet Card</title>
1003
1004         <indexterm>
1005           <primary>network card configuration</primary>
1006           <secondary>testing the card</secondary>
1007         </indexterm>
1008
1009         <para>To verify that an Ethernet card is configured correctly,
1010           you have to try two things.  First, ping the interface itself,
1011           and then ping another machine on the LAN.</para>
1012
1013         <para>First test the local interface:</para>
1014
1015 <screen>&prompt.user; <userinput>ping -c5 192.168.1.3</userinput>
1016 PING 192.168.1.3 (192.168.1.3): 56 data bytes
1017 64 bytes from 192.168.1.3: icmp_seq=0 ttl=64 time=0.082 ms
1018 64 bytes from 192.168.1.3: icmp_seq=1 ttl=64 time=0.074 ms
1019 64 bytes from 192.168.1.3: icmp_seq=2 ttl=64 time=0.076 ms
1020 64 bytes from 192.168.1.3: icmp_seq=3 ttl=64 time=0.108 ms
1021 64 bytes from 192.168.1.3: icmp_seq=4 ttl=64 time=0.076 ms
1022
1023 --- 192.168.1.3 ping statistics ---
1024 5 packets transmitted, 5 packets received, 0% packet loss
1025 round-trip min/avg/max/stddev = 0.074/0.083/0.108/0.013 ms</screen>
1026
1027         <para>Now we have to ping another machine on the LAN:</para>
1028
1029 <screen>&prompt.user; <userinput>ping -c5 192.168.1.2</userinput>
1030 PING 192.168.1.2 (192.168.1.2): 56 data bytes
1031 64 bytes from 192.168.1.2: icmp_seq=0 ttl=64 time=0.726 ms
1032 64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=0.766 ms
1033 64 bytes from 192.168.1.2: icmp_seq=2 ttl=64 time=0.700 ms
1034 64 bytes from 192.168.1.2: icmp_seq=3 ttl=64 time=0.747 ms
1035 64 bytes from 192.168.1.2: icmp_seq=4 ttl=64 time=0.704 ms
1036
1037 --- 192.168.1.2 ping statistics ---
1038 5 packets transmitted, 5 packets received, 0% packet loss
1039 round-trip min/avg/max/stddev = 0.700/0.729/0.766/0.025 ms</screen>
1040
1041         <para>You could also use the machine name instead of
1042           <hostid role="ipaddr">192.168.1.2</hostid> if you have set up the
1043           <filename>/etc/hosts</filename> file.</para>
1044       </sect3>
1045
1046       <sect3>
1047         <title>Troubleshooting</title>
1048
1049       <indexterm>
1050         <primary>network card configuration</primary>
1051         <secondary>troubleshooting</secondary>
1052       </indexterm>
1053
1054       <para>Troubleshooting hardware and software configurations is always
1055         a pain, and a pain which can be alleviated by checking the simple
1056         things first.  Is your network cable plugged in?  Have you properly
1057         configured the network services?  Did you configure the firewall
1058         correctly?  Is the card you are using supported by &os;?  Always
1059         check the hardware notes before sending off a bug report.  Update
1060         your version of &os; to the latest STABLE version.  Check the
1061         mailing list archives, or perhaps search the Internet.</para>
1062
1063       <para>If the card works, yet performance is poor, it would be
1064         worthwhile to read over the &man.tuning.7; manual page.  You
1065         can also check the network configuration as incorrect network
1066         settings can cause slow connections.</para>
1067
1068       <para>Some users experience one or two <quote>device
1069         timeouts</quote>, which is normal for some cards.  If they
1070         continue, or are bothersome, you may wish to be sure the
1071         device is not conflicting with another device.  Double check
1072         the cable connections.  Perhaps you may just need to get
1073         another card.</para>
1074
1075       <para>At times, users see a few <errorname>watchdog timeout</errorname>
1076         errors.  The first thing to do here is to check your network
1077         cable. Many cards require a PCI slot which supports Bus
1078         Mastering. On some old motherboards, only one PCI slot allows
1079         it (usually slot 0). Check the network card and the
1080         motherboard documentation to determine if that may be the
1081         problem.</para>
1082
1083       <para><errorname>No route to host</errorname> messages occur if the
1084         system is unable to route a packet to the destination host.
1085         This can happen if no default route is specified, or if a
1086         cable is unplugged.  Check the output of <command>netstat
1087         -rn</command> and make sure there is a valid route to the host
1088         you are trying to reach.  If there is not, read on to <xref
1089         linkend="advanced-networking">.</para>
1090
1091       <para><errorname>ping: sendto: Permission denied</errorname> error
1092         messages are often caused by a misconfigured firewall.  If
1093         <command>ipfw</command> is enabled in the kernel but no rules
1094         have been defined, then the default policy is to deny all
1095         traffic, even ping requests!  Read on to <xref
1096         linkend="firewalls"> for more information.</para>
1097
1098       <para>Sometimes performance of the card is poor, or below average.
1099         In these cases it is best to set the media selection mode
1100         from <literal>autoselect</literal> to the correct media selection.
1101         While this usually works for most hardware, it may not resolve
1102         this issue for everyone.  Again, check all the network settings,
1103         and read over the &man.tuning.7; manual page.</para>
1104
1105       </sect3>
1106     </sect2>
1107   </sect1>
1108
1109   <sect1 id="configtuning-virtual-hosts">
1110     <title>Virtual Hosts</title>
1111
1112     <indexterm><primary>virtual hosts</primary></indexterm>
1113     <indexterm><primary>IP aliases</primary></indexterm>
1114
1115     <para>A very common use of &os; is virtual site hosting, where
1116       one server appears to the network as many servers.  This is
1117       achieved by assigning multiple network addresses to a single
1118       interface.</para>
1119
1120     <para>A given network interface has one <quote>real</quote> address,
1121       and may have any number of <quote>alias</quote> addresses.
1122       These aliases are
1123       normally added by placing alias entries in
1124       <filename>/etc/rc.conf</filename>.</para>
1125
1126     <para>An alias entry for the interface <devicename>fxp0</devicename>
1127       looks like:</para>
1128
1129 <programlisting>ifconfig_fxp0_alias0="inet xxx.xxx.xxx.xxx netmask xxx.xxx.xxx.xxx"</programlisting>
1130
1131     <para>Note that alias entries must start with alias0 and proceed
1132       upwards in order, (for example, _alias1, _alias2, and so on).
1133       The configuration process will stop at the first missing number.
1134     </para>
1135
1136     <para>The calculation of alias netmasks is important, but
1137       fortunately quite simple.  For a given interface, there must be
1138       one address which correctly represents the network's netmask.
1139       Any other addresses which fall within this network must have a
1140       netmask of all <literal>1</literal>s.</para>
1141
1142     <para>For example, consider the case where the
1143       <devicename>fxp0</devicename> interface is
1144       connected to two networks, the <hostid role="ipaddr">10.1.1.0</hostid>
1145       network with a netmask of <hostid role="netmask">255.255.255.0</hostid>
1146       and the <hostid role="ipaddr">202.0.75.16</hostid> network with
1147       a netmask of <hostid role="netmask">255.255.255.240</hostid>.
1148       We want the system to appear at <hostid role="ipaddr">10.1.1.1</hostid>
1149       through <hostid role="ipaddr">10.1.1.5</hostid> and at
1150       <hostid role="ipaddr">202.0.75.17</hostid> through
1151       <hostid role="ipaddr">202.0.75.20</hostid>.</para>
1152
1153     <para>The following entries configure the adapter correctly for
1154       this arrangement:</para>
1155
1156 <programlisting> ifconfig_fxp0="inet 10.1.1.1 netmask 255.255.255.0"
1157  ifconfig_fxp0_alias0="inet 10.1.1.2 netmask 255.255.255.255"
1158  ifconfig_fxp0_alias1="inet 10.1.1.3 netmask 255.255.255.255"
1159  ifconfig_fxp0_alias2="inet 10.1.1.4 netmask 255.255.255.255"
1160  ifconfig_fxp0_alias3="inet 10.1.1.5 netmask 255.255.255.255"
1161  ifconfig_fxp0_alias4="inet 202.0.75.17 netmask 255.255.255.240"
1162  ifconfig_fxp0_alias5="inet 202.0.75.18 netmask 255.255.255.255"
1163  ifconfig_fxp0_alias6="inet 202.0.75.19 netmask 255.255.255.255"
1164  ifconfig_fxp0_alias7="inet 202.0.75.20 netmask 255.255.255.255"</programlisting>
1165
1166   </sect1>
1167
1168   <sect1 id="configtuning-configfiles">
1169     <title>Configuration Files</title>
1170
1171     <sect2>
1172       <title><filename>/etc</filename> Layout</title>
1173       <para>There are a number of directories in which configuration
1174         information is kept.  These include:</para>
1175
1176       <informaltable frame="none">
1177         <tgroup cols="2">
1178           <tbody>
1179             <row>
1180               <entry><filename>/etc</filename></entry>
1181               <entry>Generic system configuration information; data here is
1182                 system-specific.</entry>
1183             </row>
1184             <row>
1185               <entry><filename>/etc/defaults</filename></entry>
1186               <entry>Default versions of system configuration files.</entry>
1187             </row>
1188             <row>
1189               <entry><filename>/etc/mail</filename></entry>
1190               <entry>Extra &man.sendmail.8; configuration, other
1191                 MTA configuration files.
1192               </entry>
1193             </row>
1194             <row>
1195               <entry><filename>/etc/ppp</filename></entry>
1196               <entry>Configuration for both user- and kernel-ppp programs.
1197               </entry>
1198             </row>
1199             <row>
1200               <entry><filename>/etc/namedb</filename></entry>
1201               <entry>Default location for &man.named.8; data.  Normally
1202                 <filename>named.conf</filename> and zone files are stored
1203                 here.</entry>
1204             </row>
1205             <row>
1206               <entry><filename>/usr/local/etc</filename></entry>
1207               <entry>Configuration files for installed applications.
1208                 May contain per-application subdirectories.</entry>
1209             </row>
1210             <row>
1211               <entry><filename>/usr/local/etc/rc.d</filename></entry>
1212               <entry>Start/stop scripts for installed applications.</entry>
1213             </row>
1214             <row>
1215               <entry><filename>/var/db</filename></entry>
1216               <entry>Automatically generated system-specific database files,
1217                  such as the package database, the locate database, and so
1218                  on</entry>
1219             </row>
1220           </tbody>
1221         </tgroup>
1222       </informaltable>
1223     </sect2>
1224
1225     <sect2>
1226       <title>Hostnames</title>
1227
1228       <indexterm><primary>hostname</primary></indexterm>
1229       <indexterm><primary>DNS</primary></indexterm>
1230
1231       <sect3>
1232         <title><filename>/etc/resolv.conf</filename></title>
1233
1234         <indexterm>
1235           <primary><filename>resolv.conf</filename></primary>
1236         </indexterm>
1237
1238         <para><filename>/etc/resolv.conf</filename> dictates how &os;'s
1239           resolver accesses the Internet Domain Name System (DNS).</para>
1240
1241         <para>The most common entries to <filename>resolv.conf</filename> are:
1242         </para>
1243
1244         <informaltable frame="none">
1245           <tgroup cols="2">
1246             <tbody>
1247               <row>
1248                 <entry><literal>nameserver</literal></entry>
1249                 <entry>The IP address of a name server the resolver
1250                   should query.  The servers are queried in the order
1251                   listed with a maximum of three.</entry>
1252               </row>
1253               <row>
1254                 <entry><literal>search</literal></entry>
1255                 <entry>Search list for hostname lookup.  This is normally
1256                   determined by the domain of the local hostname.</entry>
1257               </row>
1258               <row>
1259                 <entry><literal>domain</literal></entry>
1260                 <entry>The local domain name.</entry>
1261               </row>
1262             </tbody>
1263           </tgroup>
1264         </informaltable>
1265
1266         <para>A typical <filename>resolv.conf</filename>:</para>
1267
1268         <programlisting>search example.com
1269 nameserver 147.11.1.11
1270 nameserver 147.11.100.30</programlisting>
1271
1272         <note><para>Only one of the <literal>search</literal> and
1273           <literal>domain</literal> options should be used.</para></note>
1274
1275         <para>If you are using DHCP, &man.dhclient.8; usually rewrites
1276           <filename>resolv.conf</filename> with information received from the
1277           DHCP server.</para>
1278       </sect3>
1279
1280       <sect3>
1281         <title><filename>/etc/hosts</filename></title>
1282
1283         <indexterm><primary>hosts</primary></indexterm>
1284
1285         <para><filename>/etc/hosts</filename> is a simple text
1286           database reminiscent of the old Internet.  It works in
1287           conjunction with DNS and NIS providing name to IP address
1288           mappings.  Local computers connected via a LAN can be placed
1289           in here for simplistic naming purposes instead of setting up
1290           a &man.named.8; server.  Additionally,
1291           <filename>/etc/hosts</filename> can be used to provide a
1292           local record of Internet names, reducing the need to query
1293           externally for commonly accessed names.</para>
1294
1295         <programlisting># &dollar;&os;&dollar;
1296 #
1297 # Host Database
1298 # This file should contain the addresses and aliases
1299 # for local hosts that share this file.
1300 # In the presence of the domain name service or NIS, this file may
1301 # not be consulted at all; see /etc/nsswitch.conf for the resolution order.
1302 #
1303 #
1304 ::1                     localhost localhost.my.domain myname.my.domain
1305 127.0.0.1               localhost localhost.my.domain myname.my.domain
1306
1307 #
1308 # Imaginary network.
1309 #10.0.0.2               myname.my.domain myname
1310 #10.0.0.3               myfriend.my.domain myfriend
1311 #
1312 # According to RFC 1918, you can use the following IP networks for
1313 # private nets which will never be connected to the Internet:
1314 #
1315 #       10.0.0.0        -   10.255.255.255
1316 #       172.16.0.0      -   172.31.255.255
1317 #       192.168.0.0     -   192.168.255.255
1318 #
1319 # In case you want to be able to connect to the Internet, you need
1320 # real official assigned numbers.  PLEASE PLEASE PLEASE do not try
1321 # to invent your own network numbers but instead get one from your
1322 # network provider (if any) or from the Internet Registry (ftp to
1323 # rs.internic.net, directory `/templates').
1324 #</programlisting>
1325
1326         <para><filename>/etc/hosts</filename> takes on the simple format
1327           of:</para>
1328
1329         <programlisting>[Internet address] [official hostname] [alias1] [alias2] ...</programlisting>
1330
1331         <para>For example:</para>
1332
1333         <programlisting>10.0.0.1 myRealHostname.example.com myRealHostname foobar1 foobar2</programlisting>
1334
1335         <para>Consult &man.hosts.5; for more information.</para>
1336       </sect3>
1337     </sect2>
1338
1339     <sect2>
1340       <title>Log File Configuration</title>
1341
1342       <indexterm><primary>log files</primary></indexterm>
1343
1344       <sect3>
1345         <title><filename>syslog.conf</filename></title>
1346
1347         <indexterm><primary>syslog.conf</primary></indexterm>
1348
1349         <para><filename>syslog.conf</filename> is the configuration file
1350           for the &man.syslogd.8; program.  It indicates which types
1351           of <command>syslog</command> messages are logged to particular
1352           log files.</para>
1353
1354         <programlisting># &dollar;&os;&dollar;
1355 #
1356 #       Spaces ARE valid field separators in this file. However,
1357 #       other *nix-like systems still insist on using tabs as field
1358 #       separators. If you are sharing this file between systems, you
1359 #       may want to use only tabs as field separators here.
1360 #       Consult the syslog.conf(5) manual page.
1361 *.err;kern.debug;auth.notice;mail.crit          /dev/console
1362 *.notice;kern.debug;lpr.info;mail.crit;news.err /var/log/messages
1363 security.*                                      /var/log/security
1364 mail.info                                       /var/log/maillog
1365 lpr.info                                        /var/log/lpd-errs
1366 cron.*                                          /var/log/cron
1367 *.err                                           root
1368 *.notice;news.err                               root
1369 *.alert                                         root
1370 *.emerg                                         *
1371 # uncomment this to log all writes to /dev/console to /var/log/console.log
1372 #console.info                                   /var/log/console.log
1373 # uncomment this to enable logging of all log messages to /var/log/all.log
1374 #*.*                                            /var/log/all.log
1375 # uncomment this to enable logging to a remote log host named loghost
1376 #*.*                                            @loghost
1377 # uncomment these if you're running inn
1378 # news.crit                                     /var/log/news/news.crit
1379 # news.err                                      /var/log/news/news.err
1380 # news.notice                                   /var/log/news/news.notice
1381 !startslip
1382 *.*                                             /var/log/slip.log
1383 !ppp
1384 *.*                                             /var/log/ppp.log</programlisting>
1385
1386         <para>Consult the &man.syslog.conf.5; manual page for more
1387           information.</para>
1388       </sect3>
1389
1390       <sect3>
1391         <title><filename>newsyslog.conf</filename></title>
1392
1393         <indexterm><primary>newsyslog.conf</primary></indexterm>
1394
1395         <para><filename>newsyslog.conf</filename> is the configuration
1396           file for &man.newsyslog.8;, a program that is normally scheduled
1397           to run by &man.cron.8;.  &man.newsyslog.8; determines when log
1398           files require archiving or rearranging.
1399           <filename>logfile</filename> is moved to
1400           <filename>logfile.0</filename>, <filename>logfile.0</filename>
1401           is moved to <filename>logfile.1</filename>, and so on.
1402           Alternatively, the log files may be archived in &man.gzip.1; format
1403           causing them to be named: <filename>logfile.0.gz</filename>,
1404           <filename>logfile.1.gz</filename>, and so on.</para>
1405
1406         <para><filename>newsyslog.conf</filename> indicates which log
1407           files are to be managed, how many are to be kept, and when
1408           they are to be touched.  Log files can be rearranged and/or
1409           archived when they have either reached a certain size, or at a
1410           certain periodic time/date.</para>
1411
1412         <programlisting># configuration file for newsyslog
1413 # &dollar;&os;&dollar;
1414 #
1415 # filename          [owner:group]    mode count size when [ZB] [/pid_file] [sig_num]
1416 /var/log/cron                           600  3     100  *     Z
1417 /var/log/amd.log                        644  7     100  *     Z
1418 /var/log/kerberos.log                   644  7     100  *     Z
1419 /var/log/lpd-errs                       644  7     100  *     Z
1420 /var/log/maillog                        644  7     *    @T00  Z
1421 /var/log/sendmail.st                    644  10    *    168   B
1422 /var/log/messages                       644  5     100  *     Z
1423 /var/log/all.log                        600  7     *    @T00  Z
1424 /var/log/slip.log                       600  3     100  *     Z
1425 /var/log/ppp.log                        600  3     100  *     Z
1426 /var/log/security                       600  10    100  *     Z
1427 /var/log/wtmp                           644  3     *    @01T05 B
1428 /var/log/daily.log                      640  7     *    @T00  Z
1429 /var/log/weekly.log                     640  5     1    $W6D0 Z
1430 /var/log/monthly.log                    640  12    *    $M1D0 Z
1431 /var/log/console.log                    640  5     100  *     Z</programlisting>
1432
1433         <para>Consult the &man.newsyslog.8; manual page for more
1434           information.</para>
1435       </sect3>
1436     </sect2>
1437
1438     <sect2>
1439       <title><filename>sysctl.conf</filename></title>
1440
1441       <indexterm><primary>sysctl.conf</primary></indexterm>
1442       <indexterm><primary>sysctl</primary></indexterm>
1443
1444       <para><filename>sysctl.conf</filename> looks much like
1445         <filename>rc.conf</filename>.  Values are set in a
1446         <literal>variable=value</literal>
1447         form.  The specified values are set after the system goes into
1448         multi-user mode.  Not all variables are settable in this mode.</para>
1449
1450       <para>A sample <filename>sysctl.conf</filename> turning off logging
1451         of fatal signal exits and letting Linux programs know they are really
1452         running under &os;:</para>
1453
1454       <programlisting>kern.logsigexit=0       # Do not log fatal signal exits (e.g. sig 11)
1455 compat.linux.osname=&os;
1456 compat.linux.osrelease=4.3-STABLE</programlisting>
1457     </sect2>
1458   </sect1>
1459
1460   <sect1 id="configtuning-sysctl">
1461     <title>Tuning with sysctl</title>
1462
1463     <indexterm><primary>sysctl</primary></indexterm>
1464     <indexterm>
1465       <primary>tuning</primary>
1466       <secondary>with sysctl</secondary>
1467     </indexterm>
1468
1469     <para>&man.sysctl.8; is an interface that allows you to make changes
1470       to a running &os; system.  This includes many advanced
1471       options of the TCP/IP stack and virtual memory system that can
1472       dramatically improve performance for an experienced system
1473       administrator.  Over five hundred system variables can be read
1474       and set using &man.sysctl.8;.</para>
1475
1476     <para>At its core, &man.sysctl.8; serves two functions: to read and
1477       to modify system settings.</para>
1478
1479     <para>To view all readable variables:</para>
1480
1481     <screen>&prompt.user; <userinput>sysctl -a</userinput></screen>
1482
1483     <para>To read a particular variable, for example,
1484       <varname>kern.maxproc</varname>:</para>
1485
1486     <screen>&prompt.user; <userinput>sysctl kern.maxproc</userinput>
1487 kern.maxproc: 1044</screen>
1488
1489     <para>To set a particular variable, use the intuitive
1490       <replaceable>variable</replaceable>=<replaceable>value</replaceable>
1491       syntax:</para>
1492
1493     <screen>&prompt.root; <userinput>sysctl kern.maxfiles=5000</userinput>
1494 kern.maxfiles: 2088 -> 5000</screen>
1495
1496     <para>Settings of sysctl variables are usually either strings,
1497       numbers, or booleans (a  boolean being <literal>1</literal> for yes
1498       or a <literal>0</literal> for no).</para>
1499
1500   <sect2 id="sysctl-readonly">
1501     <sect2info>
1502       <authorgroup>
1503         <author>
1504          <firstname>Tom</firstname>
1505          <surname>Rhodes</surname>
1506          <contrib>Contributed by </contrib>
1507          <!-- 31 January 2003 -->
1508         </author>
1509       </authorgroup>
1510     </sect2info>
1511     <title>&man.sysctl.8; Read-only</title>
1512
1513     <para>In some cases it may be desirable to modify read-only &man.sysctl.8;
1514       values.  While this is not recommended, it is also sometimes unavoidable.</para>
1515
1516     <para>For instance on some laptop models the &man.cardbus.4; device will
1517       not probe memory ranges, and fail with errors which look similar to:</para>
1518
1519     <screen>cbb0: Could not map register memory
1520 device_probe_and_attach: cbb0 attach returned 12</screen>
1521
1522     <para>Cases like the one above usually require the modification of some
1523       default &man.sysctl.8; settings which are set read only.  To overcome
1524       these situations a user can put &man.sysctl.8; <quote>OIDs</quote>
1525       in their local <filename>/boot/loader.conf</filename>.  Default
1526       settings are located in the <filename>/boot/defaults/loader.conf</filename>
1527       file.</para>
1528
1529     <para>Fixing the problem mentioned above would require a user to set
1530       <option>hw.pci.allow_unsupported_io_range=1</option> in the aforementioned
1531       file.  Now &man.cardbus.4; will work properly.</para>
1532
1533     </sect2>
1534   </sect1>
1535
1536   <sect1 id="configtuning-disk">
1537     <title>Tuning Disks</title>
1538
1539     <sect2>
1540       <title>Sysctl Variables</title>
1541
1542       <sect3>
1543         <title><varname>vfs.vmiodirenable</varname></title>
1544
1545         <indexterm>
1546           <primary><varname>vfs.vmiodirenable</varname></primary>
1547         </indexterm>
1548
1549         <para>The <varname>vfs.vmiodirenable</varname> sysctl variable
1550           may be set to either 0 (off) or 1 (on); it is 1 by default.
1551           This variable controls how directories are cached by the
1552           system.  Most directories are small, using just a single
1553           fragment (typically 1&nbsp;K) in the file system and less
1554           (typically 512&nbsp;bytes) in the buffer cache.
1555           However, when operating in the default mode the buffer
1556           cache will only cache a fixed number of directories even if
1557           you have a huge amount of memory.  Turning on this sysctl
1558           allows the buffer cache to use the VM Page Cache to cache the
1559           directories, making all the memory available for caching
1560           directories.  However,
1561           the minimum in-core memory used to cache a directory is the
1562           physical page size (typically 4&nbsp;K) rather than 512&nbsp;
1563           bytes.  We recommend turning this option on if you are running
1564           any services which manipulate large numbers of files.  Such
1565           services can include web caches, large mail systems, and news
1566           systems.  Turning on this option will generally not reduce
1567           performance even with the wasted memory but you should
1568           experiment to find out.</para>
1569       </sect3>
1570
1571      <sect3>
1572         <title><varname>vfs.write_behind</varname></title>
1573
1574         <indexterm>
1575           <primary><varname>vfs.write_behind</varname></primary>
1576         </indexterm>
1577
1578         <para>The <varname>vfs.write_behind</varname> sysctl variable
1579           defaults to <literal>1</literal> (on).  This tells the file system
1580           to issue media writes as full clusters are collected, which
1581           typically occurs when writing large sequential files.  The idea
1582           is to avoid saturating the buffer cache with dirty buffers when
1583           it would not benefit I/O performance.  However, this may stall
1584           processes and under certain circumstances you may wish to turn it
1585           off.</para>
1586        </sect3>
1587
1588        <sect3>
1589         <title><varname>vfs.hirunningspace</varname></title>
1590
1591         <indexterm>
1592           <primary><varname>vfs.hirunningspace</varname></primary>
1593         </indexterm>
1594
1595         <para>The <varname>vfs.hirunningspace</varname> sysctl variable
1596           determines how much outstanding write I/O may be queued to disk
1597           controllers system-wide at any given instance.  The default is
1598           usually sufficient but on machines with lots of disks you may
1599           want to bump it up to four or five <emphasis>megabytes</emphasis>.
1600           Note that setting too high a value (exceeding the buffer cache's
1601           write threshold) can lead to extremely bad clustering
1602           performance.  Do not set this value arbitrarily high!  Higher
1603           write values may add latency to reads occurring at the same time.
1604         </para>
1605
1606         <para>There are various other buffer-cache and VM page cache
1607           related sysctls.  We do not recommend modifying these values.  
1608           The VM system does an extremely good job of
1609           automatically tuning itself.</para>
1610        </sect3>
1611
1612        <sect3>
1613         <title><varname>vm.swap_idle_enabled</varname></title>
1614
1615         <indexterm>
1616           <primary><varname>vm.swap_idle_enabled</varname></primary>
1617         </indexterm>
1618
1619         <para>The <varname>vm.swap_idle_enabled</varname> sysctl variable
1620           is useful in large multi-user systems where you have lots of
1621           users entering and leaving the system and lots of idle processes.
1622           Such systems tend to generate a great deal of continuous pressure
1623           on free memory reserves.  Turning this feature on and tweaking
1624           the swapout hysteresis (in idle seconds) via
1625           <varname>vm.swap_idle_threshold1</varname> and
1626           <varname>vm.swap_idle_threshold2</varname> allows you to depress
1627           the priority of memory pages associated with idle processes more
1628           quickly then the normal pageout algorithm.  This gives a helping
1629           hand to the pageout daemon.  Do not turn this option on unless
1630           you need it, because the tradeoff you are making is essentially
1631           pre-page memory sooner rather than later; thus eating more swap
1632           and disk bandwidth.  In a small system this option will have a
1633           determinable effect but in a large system that is already doing
1634           moderate paging this option allows the VM system to stage whole
1635           processes into and out of memory easily.</para>
1636        </sect3>
1637
1638       <sect3>
1639         <title><varname>hw.ata.wc</varname></title>
1640
1641         <indexterm>
1642           <primary><varname>hw.ata.wc</varname></primary>
1643         </indexterm>
1644
1645         <para>IDE drives lie about when a write completes.  With IDE write
1646           caching turned on, IDE hard drives not only write data
1647           to disk out of order, but will sometimes delay writing some
1648           blocks indefinitely when under heavy disk loads.  A crash or
1649           power failure may cause serious file system corruption. Turning 
1650           off write caching will remove the danger of this data loss, but 
1651           will also cause disk operations to proceed 
1652           <emphasis>very slowly.</emphasis>  Change this only if prepared 
1653           to suffer with the disk slowdown.</para>
1654           
1655         <para>Changing this variable must be done from the
1656           boot loader at boot time.  Attempting to do it after the
1657           kernel boots will have no effect.</para>
1658
1659         <para>For more information, please see &man.ata.4;.</para>
1660       </sect3>
1661
1662     </sect2>
1663
1664     <sect2 id="soft-updates">
1665       <title>Soft Updates</title>
1666
1667       <indexterm><primary>Soft Updates</primary></indexterm>
1668       <indexterm><primary>tunefs</primary></indexterm>
1669
1670       <para>The &man.tunefs.8; program can be used to fine-tune a
1671         file system.  This program has many different options, but for
1672         now we are only concerned with toggling Soft Updates on and
1673         off, which is done by:</para>
1674
1675       <screen>&prompt.root; <userinput>tunefs -n enable /filesystem</userinput>
1676 &prompt.root; <userinput>tunefs -n disable /filesystem</userinput></screen>
1677
1678       <para>A filesystem cannot be modified with &man.tunefs.8; while
1679         it is mounted.  A good time to enable Soft Updates is before any
1680         partitions have been mounted, in single-user mode.</para>
1681
1682       <note><para>It is possible to enable Soft Updates
1683         at filesystem creation time, through use of the <literal>-U</literal>
1684         option to &man.newfs.8;.</para></note>
1685
1686       <para>Soft Updates drastically improves meta-data performance, mainly
1687         file creation and deletion, through the use of a memory cache.  We
1688         recommend to use Soft Updates on all of your file systems.  There
1689         are two downsides to Soft Updates that you should be aware of:  First,
1690         Soft Updates guarantees filesystem consistency in the case of a crash
1691         but could very easily be several seconds (even a minute!) behind
1692         updating the physical disk.  If your system crashes you may lose more
1693         work than otherwise.  Secondly, Soft Updates delays the freeing of
1694         filesystem blocks.  If you have a filesystem (such as the root
1695         filesystem) which is almost full, performing a major update, such as
1696         <command>make installworld</command>, can cause the filesystem to run
1697         out of space and the update to fail.</para>
1698
1699       <sect3>
1700         <title>More Details about Soft Updates</title>
1701
1702         <indexterm>
1703           <primary>Soft Updates</primary>
1704           <secondary>details</secondary>
1705         </indexterm>
1706
1707         <para>There are two traditional approaches to writing a file
1708           systems meta-data back to disk.  (Meta-data updates are
1709           updates to non-content data like inodes or
1710           directories.)</para>
1711
1712         <para>Historically, the default behavior was to write out
1713           meta-data updates synchronously.  If a directory had been
1714           changed, the system waited until the change was actually
1715           written to disk.  The file data buffers (file contents) were
1716           passed through the buffer cache and backed up
1717           to disk later on asynchronously.  The advantage of this
1718           implementation is that it operates safely.  If there is
1719           a failure during an update, the meta-data are always in a
1720           consistent state.  A file is either created completely
1721           or not at all.  If the data blocks of a file did not find
1722           their way out of the buffer cache onto the disk by the time
1723           of the crash, &man.fsck.8; is able to recognize this and
1724           repair the filesystem by setting the file length to
1725           0.  Additionally, the implementation is clear and simple.
1726           The disadvantage is that meta-data changes are slow.  An
1727           <command>rm -r</command>, for instance, touches all the files
1728           in a directory sequentially, but each directory
1729           change (deletion of a file) will be written synchronously
1730           to the disk.  This includes updates to the directory itself,
1731           to the inode table, and possibly to indirect blocks
1732           allocated by the file.  Similar considerations apply for
1733           unrolling large hierarchies (<command>tar -x</command>).</para>
1734
1735         <para>The second case is asynchronous meta-data updates.  This
1736           is the default for Linux/ext2fs and
1737           <command>mount -o async</command> for *BSD ufs.  All
1738           meta-data updates are simply being passed through the buffer
1739           cache too, that is, they will be intermixed with the updates
1740           of the file content data.  The advantage of this
1741           implementation is there is no need to wait until each
1742           meta-data update has been written to disk, so all operations
1743           which cause huge amounts of meta-data updates work much
1744           faster than in the synchronous case.  Also, the
1745           implementation is still clear and simple, so there is a low
1746           risk for bugs creeping into the code.  The disadvantage is
1747           that there is no guarantee at all for a consistent state of
1748           the filesystem.  If there is a failure during an operation
1749           that updated large amounts of meta-data (like a power
1750           failure, or someone pressing the reset button),
1751           the filesystem
1752           will be left in an unpredictable state.  There is no opportunity
1753           to examine the state of the filesystem when the system
1754           comes up again; the data blocks of a file could already have
1755           been written to the disk while the updates of the inode
1756           table or the associated directory were not.  It is actually
1757           impossible to implement a <command>fsck</command> which is
1758           able to clean up the resulting chaos (because the necessary
1759           information is not available on the disk).  If the
1760           filesystem has been damaged beyond repair, the only choice
1761           is to use &man.newfs.8; on it and restore it from backup.
1762           </para>
1763
1764         <para>The usual solution for this problem was to implement
1765           <emphasis>dirty region logging</emphasis>, which is also
1766           referred to as <emphasis>journaling</emphasis>, although that
1767           term is not used consistently and is occasionally applied
1768           to other forms of transaction logging as well.  Meta-data
1769           updates are still written synchronously, but only into a
1770           small region of the disk.  Later on they will be moved
1771           to their proper location.  Because the logging
1772           area is a small, contiguous region on the disk, there
1773           are no long distances for the disk heads to move, even
1774           during heavy operations, so these operations are quicker
1775           than synchronous updates.
1776           Additionally the complexity of the implementation is fairly
1777           limited, so the risk of bugs being present is low.  A disadvantage
1778           is that all meta-data are written twice (once into the
1779           logging region and once to the proper location) so for
1780           normal work, a performance <quote>pessimization</quote>
1781           might result.  On the other hand, in case of a crash, all
1782           pending meta-data operations can be quickly either rolled-back
1783           or completed from the logging area after the system comes
1784           up again, resulting in a fast filesystem startup.</para>
1785
1786         <para>Kirk McKusick, the developer of Berkeley FFS,
1787            solved this problem with Soft Updates: all pending
1788            meta-data updates are kept in memory and written out to disk
1789            in a sorted sequence (<quote>ordered meta-data
1790            updates</quote>).  This has the effect that, in case of
1791            heavy meta-data operations, later updates to an item
1792            <quote>catch</quote> the earlier ones if the earlier ones are still in
1793            memory and have not already been written to disk.  So all
1794            operations on, say, a directory are generally performed in
1795            memory before the update is written to disk (the data
1796            blocks are sorted according to their position so
1797            that they will not be on the disk ahead of their meta-data).
1798            If the system crashes, this causes an implicit <quote>log
1799            rewind</quote>: all operations which did not find their way
1800            to the disk appear as if they had never happened.  A
1801            consistent filesystem state is maintained that appears to
1802            be the one of 30 to 60 seconds earlier.  The
1803            algorithm used guarantees that all resources in use
1804            are marked as such in their appropriate bitmaps: blocks and inodes.
1805            After a crash, the only resource allocation error
1806            that occurs is that resources are
1807            marked as <quote>used</quote> which are actually <quote>free</quote>.
1808            &man.fsck.8; recognizes this situation,
1809            and frees the resources that are no longer used.  It is safe to
1810            ignore the dirty state of the filesystem after a crash by
1811            forcibly mounting it with <command>mount -f</command>.  In
1812            order to free resources that may be unused, &man.fsck.8;
1813            needs to be run at a later time.</para>
1814
1815          <para>The advantage is that meta-data operations are nearly as
1816            fast as asynchronous updates (i.e. faster than with
1817            <emphasis>logging</emphasis>, which has to write the
1818            meta-data twice).  The disadvantages are the complexity of
1819            the code (implying a higher risk for bugs in an area that
1820            is highly sensitive regarding loss of user data), and a
1821            higher memory consumption.  Additionally there are some
1822            idiosyncrasies one has to get used to.
1823            After a crash, the state of the filesystem appears to be
1824            somewhat <quote>older</quote>.  In situations where
1825            the standard synchronous approach would have caused some
1826            zero-length files to remain after the
1827            <command>fsck</command>, these files do not exist at all
1828            with a Soft Updates filesystem because neither the meta-data
1829            nor the file contents have ever been written to disk.
1830            Disk space is not released until the updates have been
1831            written to disk, which may take place some time after
1832            running <command>rm</command>.  This may cause problems
1833            when installing large amounts of data on a filesystem
1834            that does not have enough free space to hold all the files
1835            twice.</para>
1836       </sect3>
1837     </sect2>
1838   </sect1>
1839
1840   <sect1 id="configtuning-kernel-limits">
1841     <title>Tuning Kernel Limits</title>
1842
1843     <indexterm>
1844       <primary>tuning</primary>
1845       <secondary>kernel limits</secondary>
1846     </indexterm>
1847
1848     <sect2 id="file-process-limits">
1849       <title>File/Process Limits</title>
1850
1851       <sect3 id="kern-maxfiles">
1852         <title><varname>kern.maxfiles</varname></title>
1853
1854         <indexterm>
1855           <primary><varname>kern.maxfiles</varname></primary>
1856         </indexterm>
1857
1858         <para><varname>kern.maxfiles</varname> can be raised or
1859           lowered based upon your system requirements.  This variable
1860           indicates the maximum number of file descriptors on your
1861           system.  When the file descriptor table is full,
1862           <errorname>file: table is full</errorname> will show up repeatedly
1863           in the system message buffer, which can be viewed with the
1864           <command>dmesg</command> command.</para>
1865
1866         <para>Each open file, socket, or fifo uses one file
1867           descriptor.  A large-scale production server may easily
1868           require many thousands of file descriptors, depending on the
1869           kind and number of services running concurrently.</para>
1870
1871         <para><varname>kern.maxfile</varname>'s default value is
1872           dictated by the <option>MAXUSERS</option> option in your
1873           kernel configuration file.  <varname>kern.maxfiles</varname> grows
1874           proportionally to the value of <option>MAXUSERS</option>.  When
1875           compiling a custom kernel, it is a good idea to set this kernel
1876           configuration option according to the uses of your system.  From
1877           this number, the kernel is given most of its pre-defined limits.
1878           Even though a production machine may not actually have 256 users
1879           connected at once, the resources needed may be similar to a
1880           high-scale web server.</para>
1881
1882         <note><para>Setting <option>MAXUSERS</option> to
1883           <literal>0</literal> in your kernel configuration file will choose
1884           a reasonable default value based on the amount of RAM present in
1885           your system.  It is set to 0 in the default GENERIC kernel.</para></note>
1886
1887       </sect3>
1888
1889       <sect3>
1890         <title><varname>kern.ipc.somaxconn</varname></title>
1891
1892         <indexterm>
1893           <primary><varname>kern.ipc.somaxconn</varname></primary>
1894         </indexterm>
1895
1896         <para>The <varname>kern.ipc.somaxconn</varname> sysctl variable
1897           limits the size of the listen queue for accepting new TCP
1898           connections.  The default value of <literal>128</literal> is
1899           typically too low for robust handling of new connections in a
1900           heavily loaded web server environment.  For such environments, it
1901           is recommended to increase this value to <literal>1024</literal> or
1902           higher.  The service daemon may itself limit the listen queue size
1903           (e.g. &man.sendmail.8;, or <application>Apache</application>) but
1904           will often have a directive in its configuration file to adjust
1905           the queue size.  Large listen queues also do a better job of
1906           avoiding Denial of Service (<abbrev>DoS</abbrev>) attacks.</para>
1907       </sect3>
1908
1909     </sect2>
1910     <sect2>
1911       <title>Network Limits</title>
1912
1913       <para>The <option>NMBCLUSTERS</option> kernel configuration
1914         option dictates the amount of network Mbufs available to the
1915         system.  A heavily-trafficked server with a low number of Mbufs
1916         will hinder &os;'s ability.  Each cluster represents
1917         approximately 2&nbsp;K of memory, so a value of 1024 represents 2
1918         megabytes of kernel memory reserved for network buffers.  A
1919         simple calculation can be done to figure out how many are
1920         needed.  If you have a web server which maxes out at 1000
1921         simultaneous connections, and each connection eats a 16&nbsp;K receive
1922         and 16&nbsp;K send buffer, you need approximately 32&nbsp;MB worth of
1923         network buffers to cover the web server.  A good rule of thumb is
1924         to multiply by 2, so 2x32&nbsp;MB&nbsp;/&nbsp;2&nbsp;KB&nbsp;=
1925         64&nbsp;MB&nbsp;/&nbsp;2&nbsp;kB&nbsp;= 32768.  We recommend
1926         values between 4096 and 32768 for machines with greater amounts
1927         of memory.  Under no circumstances should you specify an
1928         arbitrarily high value for this parameter as it could lead to a
1929         boot time crash.  The <option>-m</option> option to
1930         &man.netstat.1; may be used to observe network cluster
1931         use.  <varname>kern.ipc.nmbclusters</varname> loader tunable should
1932         be used to tune this at boot time.</para>
1933
1934       <para>For busy servers that make extensive use of the
1935         &man.sendfile.2; system call, it may be necessary to increase
1936         the number of &man.sendfile.2; buffers via the
1937         <option>NSFBUFS</option> kernel configuration option or by
1938         setting its value in <filename>/boot/loader.conf</filename>
1939         (see &man.loader.8; for details).  A common indicator that
1940         this parameter needs to be adjusted is when processes are seen
1941         in the <errorname>sfbufa</errorname> state.  The sysctl
1942         variable <varname>kern.ipc.nsfbufs</varname> is a read-only
1943         glimpse at the kernel configured variable.  This parameter
1944         nominally scales with <varname>kern.maxusers</varname>,
1945         however it may be necessary to tune accordingly.</para>
1946
1947       <important>
1948         <para>Even though a socket has been marked as non-blocking,
1949           calling &man.sendfile.2; on the non-blocking socket may
1950           result in the &man.sendfile.2; call blocking until enough
1951           <literal>struct sf_buf</literal>'s are made
1952           available.</para>
1953       </important>
1954
1955       <sect3>
1956         <title><varname>net.inet.ip.portrange.*</varname></title>
1957
1958         <indexterm>
1959           <primary>net.inet.ip.portrange.*</primary>
1960         </indexterm>
1961
1962         <para>The <varname>net.inet.ip.portrange.*</varname> sysctl
1963           variables control the port number ranges automatically bound to TCP
1964           and UDP sockets.  There are three ranges: a low range, a default
1965           range, and a high range.  Most network programs use the default
1966           range which is controlled by the
1967           <varname>net.inet.ip.portrange.first</varname> and
1968           <varname>net.inet.ip.portrange.last</varname>, which default to
1969           1024 and 5000, respectively.  Bound port ranges are used  for
1970           outgoing connections, and it is possible to run the system out of
1971           ports under certain circumstances.  This most commonly occurs
1972           when you are running a heavily loaded web proxy.  The port range
1973           is not an issue when running servers which handle mainly incoming
1974           connections, such as a normal web server, or has a limited number
1975           of outgoing connections, such as a mail relay.  For situations
1976           where you may run yourself out of ports, it is recommended to
1977           increase <varname>net.inet.ip.portrange.last</varname> modestly.
1978           A value of <literal>10000</literal>, <literal>20000</literal> or
1979           <literal>30000</literal> may be reasonable.  You should also
1980           consider firewall effects when changing the port range.  Some
1981           firewalls may block large ranges of ports (usually low-numbered
1982           ports) and expect systems to use higher ranges of ports for
1983           outgoing connections &mdash; for this reason it is recommended that
1984           <varname>net.inet.ip.portrange.first</varname> be lowered.</para>
1985       </sect3>
1986
1987       <sect3>
1988         <title>TCP Bandwidth Delay Product</title>
1989
1990         <indexterm>
1991           <primary>TCP Bandwidth Delay Product Limiting</primary>
1992           <secondary><varname>net.inet.tcp.inflight_enable</varname></secondary>
1993         </indexterm>
1994
1995         <para>The TCP Bandwidth Delay Product Limiting is similar to
1996           TCP/Vegas in <application>NetBSD</application>.  It can be
1997           enabled by setting <varname>net.inet.tcp.inflight_enable</varname>
1998           sysctl variable to <literal>1</literal>.  The system will attempt
1999           to calculate the bandwidth delay product for each connection and
2000           limit the amount of data queued to the network to just the amount
2001           required to maintain optimum throughput.</para>
2002
2003         <para>This feature is useful if you are serving data over modems,
2004           Gigabit Ethernet, or even high speed WAN links (or any other link
2005           with a high bandwidth delay product), especially if you are also
2006           using window scaling or have configured a large send window.  If
2007           you enable this option, you should also be sure to set
2008           <varname>net.inet.tcp.inflight_debug</varname> to
2009           <literal>0</literal> (disable debugging), and for production use
2010           setting <varname>net.inet.tcp.inflight_min</varname> to at least
2011           <literal>6144</literal> may be beneficial.  However, note that
2012           setting high minimums may effectively disable bandwidth limiting
2013           depending on the link.  The limiting feature reduces the amount of
2014           data built up in intermediate route and switch packet queues as
2015           well as reduces the amount of data built up in the local host's
2016           interface queue.  With fewer packets queued up, interactive
2017           connections, especially over slow modems, will also be able to
2018           operate with lower <emphasis>Round Trip Times</emphasis>.  However,
2019           note that this feature only effects data transmission (uploading
2020           / server side).  It has no effect on data reception (downloading).
2021         </para>
2022
2023         <para>Adjusting <varname>net.inet.tcp.inflight_stab</varname> is
2024           <emphasis>not</emphasis> recommended.  This parameter defaults to
2025           20, representing 2 maximal packets added to the bandwidth delay
2026           product window calculation.  The additional window is required to
2027           stabilize the algorithm and improve responsiveness to changing
2028           conditions, but it can also result in higher ping times over slow
2029           links (though still much lower than you would get without the
2030           inflight algorithm).  In such cases, you may wish to try reducing
2031           this parameter to 15, 10, or 5; and may also have to reduce
2032           <varname>net.inet.tcp.inflight_min</varname> (for example, to
2033           3500) to get the desired effect.  Reducing these parameters
2034           should be done as a last resort only.</para>
2035       </sect3>
2036     </sect2>
2037   </sect1>
2038
2039   <sect1 id="adding-swap-space">
2040     <title>Adding Swap Space</title>
2041
2042     <para>No matter how well you plan, sometimes a system does not run
2043       as you expect.  If you find you need more swap space, it is
2044       simple enough to add.  You have three ways to increase swap
2045       space: adding a new hard drive, enabling swap over NFS, and
2046       creating a swap file on an existing partition.</para>
2047
2048     <sect2 id="new-drive-swap">
2049       <title>Swap on a New Hard Drive</title>
2050
2051       <para>The best way to add swap, of course, is to use this as an
2052         excuse to add another hard drive.  You can always use another
2053         hard drive, after all.  If you can do this, go reread the
2054         discussion of <ulink
2055         url="configtuning-initial.html#SWAP-DESIGN">swap space
2056         </ulink> from the <ulink
2057         url="configtuning-initial.html">Initial Configuration</ulink>
2058         section of the Handbook for some suggestions on how to best
2059         arrange your swap.</para>
2060     </sect2>
2061
2062     <sect2 id="nfs-swap">
2063       <title>Swapping over NFS</title>
2064
2065       <para>Swapping over NFS is only recommended if you do not have a
2066         local hard disk to swap to.  Even though &os;  has an excellent 
2067         NFS implementation, NFS swapping will be limited
2068         by the available network bandwidth and puts an additional
2069         burden on the NFS server.</para>
2070     </sect2>
2071
2072     <sect2 id="create-swapfile">
2073       <title>Swapfiles</title>
2074
2075       <para>You can create a file of a specified size to use as a swap
2076         file.  In our example here we will use a 64MB file called
2077         <filename>/usr/swap0</filename>.  You can use any name you
2078         want, of course.</para>
2079
2080       <example>
2081         <title>Creating a Swapfile</title>
2082
2083       <orderedlist>
2084         <listitem>
2085           <para>Be certain that your kernel configuration includes
2086             the vnode driver.  It is <emphasis>not</emphasis> in recent versions of
2087             <filename>GENERIC</filename>.</para>
2088
2089           <programlisting>pseudo-device   vn 1   #Vnode driver (turns a file into a device)</programlisting>
2090         </listitem>
2091
2092         <listitem>
2093           <para>Create a vn-device:</para>
2094           <screen>&prompt.root; <userinput>cd /dev</userinput>
2095 &prompt.root; <userinput>sh MAKEDEV vn0</userinput></screen>
2096         </listitem>
2097
2098         <listitem>
2099           <para>Create a swapfile (<filename>/usr/swap0</filename>):</para>
2100
2101           <screen>&prompt.root; <userinput>dd if=/dev/zero of=/usr/swap0 bs=1024k count=64</userinput></screen>
2102         </listitem>
2103
2104         <listitem>
2105           <para>Set proper permissions on (<filename>/usr/swap0</filename>):</para>
2106
2107           <screen>&prompt.root; <userinput>chmod 0600 /usr/swap0</userinput></screen>
2108         </listitem>
2109
2110         <listitem>
2111           <para>Enable the swap file in <filename>/etc/rc.conf</filename>:</para>
2112
2113           <programlisting>swapfile="/usr/swap0"   # Set to name of swapfile if aux swapfile desired.</programlisting>
2114         </listitem>
2115
2116         <listitem>
2117
2118           <para>Reboot the machine or to enable the swap file immediately,
2119             type:</para>
2120
2121           <screen>&prompt.root; <userinput>vnconfig -e /dev/vn0b /usr/swap0 swap</userinput></screen>
2122         </listitem>
2123       </orderedlist>
2124
2125       </example>
2126     </sect2>
2127   </sect1>
2128
2129   <sect1 id="acpi-overview">
2130     <sect1info>
2131       <authorgroup>
2132         <author>
2133           <firstname>Hiten</firstname>
2134           <surname>Pandya</surname>
2135           <contrib>Written by </contrib>
2136         </author>
2137         <author>
2138           <firstname>Tom</firstname>
2139           <surname>Rhodes</surname>
2140         </author>
2141       </authorgroup>
2142     </sect1info>
2143
2144     <title>Power and Resource Management</title>
2145
2146     <para>It is very important to utilize hardware resources in an
2147       efficient manner.  Before <acronym>ACPI</acronym> was introduced,
2148       it was very difficult and inflexible for operating systems to manage
2149       the power usage and thermal properties of a system.  The hardware was
2150       controlled by some sort of <acronym>BIOS</acronym> embedded
2151       interface, such as <emphasis>Plug and Play BIOS (PNPBIOS)</emphasis>, or
2152       <emphasis>Advanced Power Management (APM)</emphasis> and so on.
2153       Power and Resource Management is one of the key components of a modern
2154       operating system.  For example, you may want an operating system to
2155       monitor system limits (and possibly alert you) in case your system
2156       temperature increased unexpectedly.</para>
2157
2158     <para>In this section, we will provide
2159       comprehensive information about <acronym>ACPI</acronym>.  References
2160       will be provided for further reading at the end.  Please be aware
2161       that <acronym>ACPI</acronym> is available on &os; systems as a 
2162       default kernel module.  </para>
2163
2164     <sect2 id="acpi-intro">
2165       <title>What Is ACPI?</title>
2166
2167       <para>Advanced Configuration and Power Interface
2168         (<acronym>ACPI</acronym>) is a standard written by
2169         an alliance of vendors to provide a standard interface for
2170         hardware resources and power management (hence the name).
2171         It is a key element in <emphasis>Operating System-directed
2172         configuration and Power Management</emphasis>, i.e.: it provides
2173         more control and flexibility to the operating system
2174         (<acronym>OS</acronym>).
2175         Modern systems <quote>stretched</quote> the limits of the
2176         current Plug and Play interfaces (such as APM), prior to the introduction of
2177         <acronym>ACPI</acronym>.  <acronym>ACPI</acronym> is the direct
2178         successor to <acronym>APM</acronym>
2179         (Advanced Power Management).</para>
2180     </sect2>
2181
2182     <sect2 id="acpi-old-spec">
2183       <title>Shortcomings of Advanced Power Management (APM)</title>
2184
2185       <para>The <emphasis>Advanced Power Management (APM)</emphasis>
2186       facility control's the power usage of a system based on its
2187       activity.  The APM BIOS is supplied by the (system) vendor and
2188       it is specific to the hardware platform.  An APM driver in the
2189       OS mediates access to the <emphasis>APM Software Interface</emphasis>,
2190       which allows management of power levels.</para>
2191
2192       <para>There are four major problems in APM.  Firstly, power
2193       management is done by the (vendor-specific) BIOS, and the OS
2194       does not have any knowledge of it.  One example of this, is when
2195       the user sets idle-time values for a hard drive in the APM BIOS,
2196       that when exceeded, it (BIOS) would spin down the hard drive,
2197       without the consent of the OS.  Secondly, the APM logic is
2198       embedded in the BIOS, and it operates outside the scope of the
2199       OS.  This means users can only fix problems in their APM BIOS by
2200       flashing a new one into the ROM; which, is a very dangerous
2201       procedure, and if it fails, it could leave the system in an
2202       unrecoverable state. Thirdly, APM is a vendor-specific
2203       technology, which, means that there is a lot or parity
2204       (duplication of efforts) and bugs found in one vendor's BIOS,
2205       may not be solved in others.  Last but not the least, the APM
2206       BIOS did not have enough room to implement a sophisticated power
2207       policy, or one that can adapt very well to the purpose of the
2208       machine.</para>
2209
2210       <para><emphasis>Plug and Play BIOS (PNPBIOS)</emphasis> was
2211       unreliable in many situations.  PNPBIOS is 16-bit technology,
2212       so the OS has to use 16-bit emulation in order to
2213       <quote>interface</quote> with PNPBIOS methods.</para>
2214
2215       <para>The &os; <acronym>APM</acronym> driver is documented in
2216       the &man.apm.4; manual page.</para>
2217     </sect2>
2218
2219     <sect2 id="acpi-config">
2220       <title>Configuring <acronym>ACPI</acronym></title>
2221
2222       <para>The <filename>acpi.ko</filename> driver is loaded by default
2223         at start up by the &man.loader.8; and should <emphasis>not</emphasis>
2224         be compiled into the kernel.  The reasoning behind this is that modules
2225         are easier to work with, say if switching to another <filename>acpi.ko</filename>
2226         without doing a kernel rebuild.  This has the advantage of making testing easier.
2227         Another reason is that starting <acronym>ACPI</acronym> after a system has been
2228         brought up is not too useful, and in some cases can be fatal.  In doubt, just
2229         disable <acronym>ACPI</acronym> all together. This driver should not and can not
2230         be unloaded because the system bus uses it for various hardware interactions.
2231         <acronym>ACPI</acronym> can be disabled with the &man.acpiconf.8; utility.
2232         In fact most of the interaction with <acronym>ACPI</acronym> can be done via
2233         &man.acpiconf.8;.  Basically this means, if anything about <acronym>ACPI</acronym>
2234         is in the &man.dmesg.8; output, then most likely it is already running.</para>
2235
2236       <note><para><acronym>ACPI</acronym> and <acronym>APM</acronym> cannot coexist and
2237         should be used separately.  The last one to load will terminate if the driver
2238         notices the other running.</para></note>
2239
2240       <para>In the simplest form, <acronym>ACPI</acronym> can be used to put the
2241         system into a sleep mode with &man.acpiconf.8;, the <option>-s</option>
2242         flag, and a <literal>1-5</literal> option.  Most users will only need
2243         <literal>1</literal>.  Option <literal>5</literal> will do a soft-off
2244         which is the same action as:</para>
2245
2246       <screen>&prompt.root; <userinput>halt -p</userinput></screen>
2247
2248       <para>The other options are available.  Check out the &man.acpiconf.8;
2249         manual page for more information.</para>
2250     </sect2>
2251   </sect1>
2252
2253   <sect1 id="ACPI-debug">
2254     <sect1info>
2255       <authorgroup>
2256         <author>
2257           <firstname>Nate</firstname>
2258           <surname>Lawson</surname>
2259           <contrib>Written by </contrib>
2260         </author>
2261       </authorgroup>
2262       <authorgroup>
2263         <author>
2264           <firstname>Peter</firstname>
2265           <surname>Schultz</surname>
2266           <contrib>With contributions from </contrib>
2267         </author>
2268         <author>
2269           <firstname>Tom</firstname>
2270           <surname>Rhodes</surname>
2271         </author>
2272       </authorgroup>
2273     </sect1info>
2274
2275     <title>Using and Debugging &os; <acronym>ACPI</acronym></title>
2276
2277     <para><acronym>ACPI</acronym> is a fundamentally new way of
2278       discovering devices, managing power usage, and providing
2279       standardized access to various hardware previously managed
2280       by the <acronym>BIOS</acronym>.  Progress is being made toward
2281       <acronym>ACPI</acronym> working on all systems, but bugs in some
2282       motherboards' <firstterm><acronym>ACPI</acronym> Machine
2283       Language</firstterm> (<acronym>AML</acronym>) bytecode,
2284       incompleteness in &os;'s kernel subsystems, and bugs in the Intel
2285       <acronym>ACPI-CA</acronym> interpreter continue to appear.</para>
2286
2287     <para>This document is intended to help you assist the &os;
2288       <acronym>ACPI</acronym> maintainers in identifying the root cause
2289       of problems you observe and debugging and developing a solution.
2290       Thanks for reading this and we hope we can solve your system's
2291       problems.</para>
2292
2293     <sect2 id="ACPI-submitdebug">
2294       <title>Submitting Debugging Information</title>
2295
2296       <note>
2297         <para>Before submitting a problem, be sure you are running the latest
2298           <acronym>BIOS</acronym> version and, if available, embedded
2299           controller firmware version.</para>
2300       </note>
2301
2302       <para>For those of you that want to submit a problem right away,
2303         please send the following information to
2304         &a.bugs.name;</para>
2305
2306       <itemizedlist>
2307         <listitem>
2308           <para>Description of the buggy behavior, including system type
2309             and model and anything that causes the bug to appear.  Also,
2310             please note as accurately as possible when the bug began
2311             occurring if it is new for you.</para>
2312         </listitem>
2313
2314         <listitem>
2315           <para>The dmesg output after <quote>boot
2316             <option>-v</option></quote>, including any error messages
2317             generated by you exercising the bug.</para>
2318         </listitem>
2319
2320         <listitem>
2321           <para>dmesg output from <quote>boot
2322             <option>-v</option></quote> with <acronym>ACPI</acronym>
2323             disabled, if disabling it helps fix the problem.</para>
2324         </listitem>
2325
2326         <listitem>
2327           <para>Output from <quote>sysctl hw.acpi</quote>.  This is also
2328             a good way of figuring out what features your system
2329             offers.</para>
2330         </listitem>
2331
2332         <listitem>
2333           <para><acronym>URL</acronym> where your
2334             <firstterm><acronym>ACPI</acronym> Source Language</firstterm>
2335             (<acronym>ASL</acronym>)
2336             can be found.  Do <emphasis>not</emphasis> send the
2337             <acronym>ASL</acronym> directly to the list as it can be
2338             very large.  Generate a copy of your <acronym>ASL</acronym>
2339             by running this command:</para>
2340
2341           <screen>&prompt.root; <userinput>acpidump -t -d &gt; <replaceable>name</replaceable>-<replaceable>system</replaceable>.asl</userinput></screen>
2342
2343           <para>(Substitute your login name for
2344             <replaceable>name</replaceable> and manufacturer/model for
2345             <replaceable>system</replaceable>.  Example:
2346             <filename>njl-FooCo6000.asl</filename>)</para>
2347         </listitem>
2348       </itemizedlist>
2349
2350     </sect2>
2351
2352     <sect2 id="ACPI-background">
2353       <title>Background</title>
2354
2355       <para><acronym>ACPI</acronym> is present in all modern computers
2356         that conform to the ia32 (x86), ia64 (Itanium), and amd64 (AMD)
2357         architectures.  The full standard has many features including
2358         <acronym>CPU</acronym> performance management, power planes
2359         control, thermal zones, various battery systems, embedded
2360         controllers, and bus enumeration.  Most systems implement less
2361         than the full standard.  For instance, a desktop system usually
2362         only implements the bus enumeration parts while a laptop might
2363         have cooling and battery management support as well.  Laptops
2364         also have suspend and resume, with their own associated
2365         complexity.</para>
2366
2367       <para>An <acronym>ACPI</acronym>-compliant system has various
2368         components.  The <acronym>BIOS</acronym> and chipset vendors
2369         provide various fixed tables (e.g., <acronym>FADT</acronym>)
2370         in memory that specify things like the <acronym>APIC</acronym>
2371         map (used for <acronym>SMP</acronym>), config registers, and
2372         simple configuration values.  Additionally, a table of bytecode
2373         (the <firstterm>Differentiated System Description Table</firstterm>
2374         <acronym>DSDT</acronym>) is provided that specifies a
2375         tree-like name space of devices and methods.</para>
2376
2377       <para>The <acronym>ACPI</acronym> driver must parse the fixed
2378         tables, implement an interpreter for the bytecode, and modify
2379         device drivers and the kernel to accept information from the
2380         <acronym>ACPI</acronym> subsystem.  For &os;, Intel has
2381         provided an interpreter (<acronym>ACPI-CA</acronym>) that is
2382         shared with Linux and NetBSD.  The path to the 
2383         <acronym>ACPI-CA</acronym> source code is 
2384         <filename role="directory">src/sys/contrib/dev/acpica-unix-YYYYMMDD</filename>, 
2385         where YYYYMMDD is the release date of the ACPI-CA source code. The 
2386         glue code that allows <acronym>ACPI-CA</acronym> to work on 
2387         &os; is in <filename>src/sys/dev/acpica5/Osd</filename>. Finally, 
2388         drivers that implement various <acronym>ACPI</acronym> devices 
2389         are found in <filename role="directory">src/sys/dev/acpica5</filename>, 
2390         and architecture-dependent code resides in 
2391         <filename role="directory">/sys/<replaceable>arch</replaceable>/acpica5</filename>.
2392       </para>
2393     </sect2>
2394
2395     <sect2 id="ACPI-comprob">
2396       <title>Common Problems</title>
2397
2398       <para>For <acronym>ACPI</acronym> to work correctly, all the parts
2399         have to work correctly.  Here are some common problems, in order
2400         of frequency of appearance, and some possible workarounds or
2401         fixes.</para>
2402
2403       <sect3>
2404         <title>Suspend/Resume</title>
2405
2406         <para><acronym>ACPI</acronym> has three suspend to
2407           <acronym>RAM</acronym> (<acronym>STR</acronym>) states,
2408           <literal>S1</literal>-<literal>S3</literal>, and one suspend
2409           to disk state (<literal>STD</literal>), called
2410           <literal>S4</literal>.  <literal>S5</literal> is
2411           <quote>soft off</quote> and is the normal state your system
2412           is in when plugged in but not powered up.
2413           <literal>S4</literal> can actually be implemented two separate
2414           ways.  <literal>S4</literal><acronym>BIOS</acronym> is a
2415           <acronym>BIOS</acronym>-assisted suspend to disk.
2416           <literal>S4</literal><acronym>OS</acronym> is implemented
2417           entirely by the operating system.</para>
2418
2419         <para>Start by checking <command>sysctl</command>
2420           <option>hw.acpi</option> for the suspend-related items.  Here
2421           are the results for my Thinkpad:</para>
2422
2423         <screen>hw.acpi.supported_sleep_state: S3 S4 S5</screen>
2424         <screen>hw.acpi.s4bios: 0</screen>
2425
2426         <para>This means that I can use <command>acpiconf -s</command>
2427           to test <literal>S3</literal>,
2428           <literal>S4</literal><acronym>OS</acronym>, and
2429           <literal>S5</literal>.  If <option>s4bios</option> was one
2430           (<literal>1</literal>), I would have
2431           <literal>S4</literal><acronym>BIOS</acronym>
2432           support instead of <literal>S4</literal>
2433           <acronym>OS</acronym>.</para>
2434
2435         <para>When testing suspend/resume, start with
2436           <literal>S1</literal>, if supported.  This state is most
2437           likely to work since it doesn't require much driver support.
2438           No one has implemented <literal>S2</literal> but if you have
2439           it, it's similar to <literal>S1</literal>.  The next thing
2440           to try is <literal>S3</literal>.  This is the deepest
2441           <acronym>STR</acronym> state and requires a lot of driver
2442           support to properly reinitialize your hardware.  If you have
2443           problems resuming, feel free to email the &a.bugs.name; list but
2444           do not expect the problem to be resolved since there are a lot
2445           of drivers/hardware that need more testing and work.</para>
2446
2447         <para>To help isolate the problem, remove as many drivers from
2448           your kernel as possible.  If it works, you can narrow down
2449           which driver is the problem by loading drivers until it fails
2450           again.  Typically binary drivers like
2451           <filename>nvidia.ko</filename>, <application>X11</application>
2452           display drivers, and <acronym>USB</acronym> will have the most
2453           problems while Ethernet interfaces usually work fine.  If you
2454           can load/unload the drivers ok, you can automate this by
2455           putting the appropriate commands in
2456           <filename>/etc/rc.suspend</filename> and
2457           <filename>/etc/rc.resume</filename>.  There is a
2458           commented-out example for unloading and loading a driver.  Try
2459           setting <option>hw.acpi.reset_video</option> to zero (0) if
2460           your display is messed up after resume.  Try setting longer or
2461           shorter values for <option>hw.acpi.sleep_delay</option> to see
2462           if that helps.</para>
2463
2464         <para>Another thing to try is load a recent Linux distribution
2465           with <acronym>ACPI</acronym> support and test their
2466           suspend/resume support on the same hardware.  If it works
2467           on Linux, it's likely a &os; driver problem and narrowing down
2468           which driver causes the problems will help us fix the problem.
2469           Note that the <acronym>ACPI</acronym> maintainers do not
2470           usually maintain other drivers (e.g sound,
2471           <acronym>ATA</acronym>, etc.) so any work done on tracking
2472           down a driver problem should probably eventually be posted
2473           to the &a.bugs.name; list and mailed to the driver
2474           maintainer.  If you are feeling adventurous, go ahead and
2475           start putting some debugging &man.printf.3;s in a problematic
2476           driver to track down where in its resume function it
2477           hangs.</para>
2478
2479         <para>Finally, try disabling <acronym>ACPI</acronym> and
2480           enabling <acronym>APM</acronym> instead.  If suspend/resume
2481           works with <acronym>APM</acronym>, you may be better off
2482           sticking with <acronym>APM</acronym>, especially on older
2483           hardware (pre-2000).  It took vendors a while to get
2484           <acronym>ACPI</acronym> support correct and older hardware is
2485           more likely to have <acronym>BIOS</acronym> problems with
2486           <acronym>ACPI</acronym>.</para>
2487       </sect3>
2488
2489       <sect3>
2490         <title>System Hangs (temporary or permanent)</title>
2491
2492         <para>Most system hangs are a result of lost interrupts or an
2493           interrupt storm.  Chipsets have a lot of problems based on how
2494           the <acronym>BIOS</acronym> configures interrupts before boot,
2495           correctness of the <acronym>APIC</acronym>
2496           (<acronym>MADT</acronym>) table, and routing of the
2497           <firstterm>System Control Interrupt</firstterm>
2498           (<acronym>SCI</acronym>).</para>
2499
2500         <para>Interrupt storms can be distinguished from lost interrupts
2501           by checking the output of <command>vmstat -i</command>
2502           and looking at the line that has
2503           <literal>acpi0</literal>.  If the counter is increasing at more
2504           than a couple per second, you have an interrupt storm.  If the
2505           system appears hung, try breaking to <acronym>DDB</acronym>
2506           (<keycombo action="simul"><keycap>CTRL</keycap>
2507           <keycap>ALT</keycap><keycap>ESC</keycap></keycombo> on
2508           console) and type <option>show interrupts</option>.</para>
2509
2510         <para>Your best hope when dealing with interrupt problems is to
2511           try disabling <acronym>APIC</acronym> support with
2512           <literal>hint.apic.0.disabled="1"</literal> in
2513           <filename>loader.conf</filename>.</para>
2514       </sect3>
2515
2516       <sect3>
2517         <title>Panics</title>
2518
2519         <para>Panics are relatively rare for <acronym>ACPI</acronym> and
2520           are the top priority to be fixed.  The first step is to
2521           isolate the steps to reproduce the panic (if possible)
2522           and get a backtrace.  Follow the advice for enabling
2523           <option>options DDB</option> and setting up a serial console
2524           (see <xref linkend="serialconsole-ddb">)
2525           or setting up a &man.dump.8; partition.  You can get a
2526           backtrace in <acronym>DDB</acronym> with
2527           <option>tr</option>.  If you have to handwrite the
2528           backtrace, be sure to at least get the lowest five (5) and top
2529           five (5) lines in the trace.</para>
2530
2531         <para>Then, try to isolate the problem by booting with
2532           <acronym>ACPI</acronym> disabled.  If that works, you can
2533           isolate the <acronym>ACPI</acronym> subsystem by using various
2534           values of <option>debug.acpi.disable</option>.  See the
2535           &man.acpi.4; manual page for some examples.</para>
2536       </sect3>
2537
2538       <sect3>
2539         <title>System Powers Up After Suspend or Shutdown</title>
2540         <para>First, try setting
2541           <option>hw.acpi.disable_on_poweroff=</option><quote>0</quote>
2542           in &man.loader.conf.5;.  This keeps <acronym>ACPI</acronym>
2543           from disabling various events during the shutdown process.
2544           Some systems need this value set to <quote>1</quote> (the
2545           default) for the same reason.  This usually fixes
2546           the problem of a system powering up spontaneously after a
2547           suspend or poweroff.</para>
2548       </sect3>
2549
2550       <sect3>
2551         <title>Other Problems</title>
2552
2553         <para>If you have other problems with <acronym>ACPI</acronym>
2554           (working with a docking station, devices not detected, etc.),
2555           please email a description to the mailing list as well;
2556           however, some of these issues may be related to unfinished
2557           parts of the <acronym>ACPI</acronym> subsystem so they might
2558           take a while to be implemented.  Please be patient and
2559           prepared to test patches we may send you.</para>
2560       </sect3>
2561     </sect2>
2562
2563     <sect2 id="ACPI-aslanddump">
2564       <title><acronym>ASL</acronym>, <command>acpidump</command>, and
2565         <acronym>IASL</acronym></title>
2566
2567       <para>The most common problem is the <acronym>BIOS</acronym>
2568         vendors providing incorrect (or outright buggy!) bytecode.  This
2569         is usually manifested by kernel console messages like
2570         this:</para>
2571
2572       <screen>ACPI-1287: *** Error: Method execution failed [\\_SB_.PCI0.LPC0.FIGD._STA] (Node 0xc3f6d160), AE_NOT_FOUND</screen>
2573
2574       <para>Often, you can resolve these problems by updating your
2575         <acronym>BIOS</acronym> to the latest revision.  Most console
2576         messages are harmless but if you have other problems like
2577         battery status not working, they're a good place to start
2578         looking for problems in the <acronym>AML</acronym>.  The
2579         bytecode, known as <acronym>AML</acronym>, is compiled from a
2580         source language called <acronym>ASL</acronym>.  The
2581         <acronym>AML</acronym> is found in the table known as the
2582         <acronym>DSDT</acronym>.  To get a copy of your
2583         <acronym>ASL</acronym>, use &man.acpidump.8;.  You should use
2584         both the <option>-t</option> (show contents of the fixed tables)
2585         and <option>-d</option> (disassemble <acronym>AML</acronym> to
2586         <acronym>ASL</acronym>) options.  See the
2587         <link linkend="ACPI-submitdebug">Submitting Debugging
2588         Information</link> section for an example syntax.</para>
2589
2590       <para>The simplest first check you can do is to recompile your
2591         <acronym>ASL</acronym> to check for errors.  Warnings can
2592         usually be ignored but errors are bugs that will usually prevent
2593         <acronym>ACPI</acronym> from working correctly.  To recompile
2594         your <acronym>ASL</acronym>, issue the following command:</para>
2595
2596       <screen>&prompt.root; <userinput>iasl your.asl</userinput></screen>
2597     </sect2>
2598
2599     <sect2 id="ACPI-fixasl">
2600       <title>Fixing Your <acronym>ASL</acronym></title>
2601
2602       <para>In the long run, our goal is for almost everyone to have
2603         <acronym>ACPI</acronym> work without any user intervention.  At
2604         this point, however, we are still developing workarounds for
2605         common mistakes made by the <acronym>BIOS</acronym> vendors.
2606         The Microsoft interpreter (<filename>acpi.sys</filename> and
2607         <filename>acpiec.sys</filename>) does not strictly check for
2608         adherence to the standard, and thus many <acronym>BIOS</acronym>
2609         vendors who only test <acronym>ACPI</acronym> under Windows
2610         never fix their <acronym>ASL</acronym>.  We hope to continue to
2611         identify and document exactly what non-standard behavior is
2612         allowed by Microsoft's interpreter and replicate it so &os; can
2613         work without forcing users to fix the <acronym>ASL</acronym>.
2614         As a workaround and to help us identify behavior, you can fix
2615         the <acronym>ASL</acronym> manually.  If this works for you,
2616         please send a &man.diff.1; of the old and new
2617         <acronym>ASL</acronym> so we can possibly work around the buggy
2618         behavior in <acronym>ACPI-CA</acronym> and thus make your fix
2619         unnecessary.</para>
2620
2621       <para>Here is a list of common error messages, their cause, and
2622         how to fix them:</para>
2623
2624       <sect3>
2625         <title>_OS dependencies</title>
2626
2627         <para>Some <acronym>AML</acronym> assumes the world consists of
2628           various Windows versions.  You can tell &os; to claim it is
2629           any <acronym>OS</acronym> to see if this fixes problems you
2630           may have.  An easy way to override this is to set
2631           <option>hw.acpi.osname</option>=<quote>Windows 2001</quote>
2632           in <filename>/boot/loader.conf</filename> or other similar
2633           strings you find in the <acronym>ASL</acronym>.</para>
2634
2635       <sect3>
2636         <title>Missing Return statements</title>
2637
2638         <para>Some methods do not explicitly return a value as the
2639           standard requires.  While <acronym>ACPI-CA</acronym>
2640           does not handle this, &os; has a workaround that allows it to
2641           return the value implicitly.  You can also add explicit
2642           Return statements where required if you know what value should
2643           be returned.  To force <command>iasl</command> to compile the
2644           <acronym>ASL</acronym>, use the <option>-f</option>
2645           flag.</para>
2646       </sect3>
2647
2648       <sect3>
2649         <title>Overriding the Default <acronym>AML</acronym></title>
2650
2651         <para>After you customize <filename>your.asl</filename>, you
2652           will want to compile it, run:</para>
2653
2654         <screen>&prompt.root; <userinput>iasl your.asl</userinput></screen>
2655
2656         <para>You can add the <option>-f</option> flag to force creation
2657           of the <acronym>AML</acronym>, even if there are errors during
2658           compilation.  Remember that some errors (e.g., missing Return
2659           statements) are automatically worked around by the
2660           interpreter.</para>
2661
2662         <para><filename>DSDT.aml</filename> is the default output
2663           filename for <command>iasl</command>.  You can load this
2664           instead of your <acronym>BIOS</acronym>'s buggy copy (which
2665           is still present in flash memory) by editing
2666           <filename>/boot/loader.conf</filename> as
2667           follows:</para>
2668
2669         <programlisting>acpi_dsdt_load="YES"
2670 acpi_dsdt_name="/boot/DSDT.aml"</programlisting>
2671
2672         <para>Be sure to copy your <filename>DSDT.aml</filename> to the
2673           <filename role="directory">/boot</filename> directory.</para>
2674       </sect3>
2675
2676     <sect2 id="ACPI-debugoutput">
2677       <title>Getting Debugging Output From
2678         <acronym>ACPI</acronym></title>
2679
2680       <para>The <acronym>ACPI</acronym> driver has a very flexible
2681         debugging facility.  It allows you to specify a set of subsystems
2682         as well as the level of verbosity.  The subsystems you wish to
2683         debug are specified as <quote>layers</quote> and are broken down
2684         into <acronym>ACPI-CA</acronym> components (ACPI_ALL_COMPONENTS)
2685         and <acronym>ACPI</acronym> hardware support (ACPI_ALL_DRIVERS).
2686         The verbosity of debugging output is specified as the
2687         <quote>level</quote> and ranges from ACPI_LV_ERROR (just report
2688         errors) to ACPI_LV_VERBOSE (everything).  The
2689         <quote>level</quote> is a bitmask so multiple options can be set
2690         at once, separated by spaces.  In practice, you will want to use
2691         a serial console to log the output if it is so long
2692         it flushes the console message buffer.  </para>
2693
2694       <para>Debugging output is not enabled by default.  To enable it,
2695         add <option>options ACPI_DEBUG</option> to your kernel config
2696         if <acronym>ACPI</acronym> is compiled into the kernel.  You can
2697         add <option>ACPI_DEBUG=1</option> to your
2698         <filename>/etc/make.conf</filename> to enable it globally.  If
2699         it is a module, you can recompile just your
2700         <filename>acpi.ko</filename> module as follows:</para>
2701
2702       <screen>&prompt.root; <userinput>cd /sys/dev/acpica5
2703 &amp;&amp; make clean &amp;&amp;
2704 make ACPI_DEBUG=1</userinput></screen>
2705
2706       <para>Install <filename>acpi.ko</filename> in
2707         <filename role="directory">/boot/kernel</filename> and add your
2708         desired level and layer to <filename>loader.conf</filename>.
2709         This example enables debug messages for all
2710         <acronym>ACPI-CA</acronym> components and all
2711         <acronym>ACPI</acronym> hardware drivers
2712         (<acronym>CPU</acronym>, <acronym>LID</acronym>, etc.)  It will
2713         only output error messages, the least verbose level.</para>
2714
2715       <programlisting>debug.acpi.layer="ACPI_ALL_COMPONENTS ACPI_ALL_DRIVERS"
2716 debug.acpi.level="ACPI_LV_ERROR"</programlisting>
2717
2718       <para>If the information you want is triggered by a specific event
2719         (say, a suspend and then resume), you can leave out changes to
2720         <filename>loader.conf</filename> and instead use
2721         <command>sysctl</command> to specify the layer and level after
2722         booting and preparing your system for the specific event.  The
2723         <command>sysctl</command>s are named the same as the tunables
2724         in <filename>loader.conf</filename>.</para>
2725     </sect2>
2726
2727     <sect2 id="ACPI-References">
2728       <title>References</title>
2729
2730       <para>More information about <acronym>ACPI</acronym> may be found
2731         in the following locations:</para>
2732
2733       <itemizedlist>
2734         <listitem>
2735           <para>The FreeBSD &a.acpi; (This is FreeBSD-specific; posting 
2736           &os; questions here may not generate much of an answer.)</para>
2737         </listitem>
2738
2739         <listitem>
2740           <para>The <acronym>ACPI</acronym> Mailing List Archives (FreeBSD)
2741             <ulink url="http://lists.freebsd.org/pipermail/freebsd-acpi/"></ulink></para>
2742         </listitem>
2743
2744         <listitem>
2745           <para>The old <acronym>ACPI</acronym> Mailing List Archives (FreeBSD)
2746             <ulink url="http://home.jp.FreeBSD.org/mail-list/acpi-jp/"></ulink></para>
2747         </listitem>
2748
2749         <listitem>
2750           <para>The <acronym>ACPI</acronym> 2.0 Specification
2751             <ulink url="http://acpi.info/spec.htm"></ulink></para>
2752         </listitem>
2753
2754         <listitem>
2755           <para>&os; Manual pages: 
2756             &man.acpidump.8;,
2757             &man.acpiconf.8;, 
2758             &man.acpidb.8;</para>
2759         </listitem>
2760
2761         <listitem>
2762           <para><ulink
2763             url="http://www.cpqlinux.com/acpi-howto.html#fix_broken_dsdt">
2764             <acronym>DSDT</acronym> debugging resource</ulink>.
2765             (Uses Compaq as an example but generally useful.)</para>
2766         </listitem>
2767       </itemizedlist>
2768     </sect2>
2769   </sect1>
2770 </chapter>
2771
2772 <!--
2773      Local Variables:
2774      mode: sgml
2775      sgml-declaration: "../chapter.decl"
2776      sgml-indent-data: t
2777      sgml-omittag: nil
2778      sgml-always-quote-attributes: t
2779      sgml-parent-document: ("../book.sgml" "part" "chapter")
2780      End:
2781 -->