Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / ntp / ntpd / refclock_local.c
blob2dcabe54752798d66cf2a6848cf217e5032b0370
1 /* $NetBSD: refclock_local.c,v 1.2 2003/12/04 16:23:37 drochner Exp $ */
4 /*
5 * refclock_local - local pseudo-clock driver
7 * wjm 17-aug-1995: add a hook for special treatment of VMS_LOCALUNIT
8 */
9 #ifdef HAVE_CONFIG_H
10 #include <config.h>
11 #endif
13 #ifdef REFCLOCK
15 #include "ntpd.h"
16 #include "ntp_refclock.h"
17 #include "ntp_stdlib.h"
19 #include <stdio.h>
20 #include <ctype.h>
22 #ifdef KERNEL_PLL
23 #include "ntp_syscall.h"
24 #endif
27 * This is a hack to allow a machine to use its own system clock as a
28 * reference clock, i.e., to free-run using no outside clock discipline
29 * source. This is useful if you want to use NTP in an isolated
30 * environment with no radio clock or NIST modem available. Pick a
31 * machine that you figure has a good clock oscillator and configure it
32 * with this driver. Set the clock using the best means available, like
33 * eyeball-and-wristwatch. Then, point all the other machines at this
34 * one or use broadcast (not multicast) mode to distribute time.
36 * Another application for this driver is if you want to use a
37 * particular server's clock as the clock of last resort when all other
38 * normal synchronization sources have gone away. This is especially
39 * useful if that server has an ovenized oscillator. For this you would
40 * configure this driver at a higher stratum (say 5) to prevent the
41 * server's stratum from falling below that.
43 * A third application for this driver is when an external discipline
44 * source is available, such as the NIST "lockclock" program, which
45 * synchronizes the local clock via a telephone modem and the NIST
46 * Automated Computer Time Service (ACTS), or the Digital Time
47 * Synchronization Service (DTSS), which runs on DCE machines. In this
48 * case the stratum should be set at zero, indicating a bona fide
49 * stratum-1 source. Exercise some caution with this, since there is no
50 * easy way to telegraph via NTP that something might be wrong in the
51 * discipline source itself. In the case of DTSS, the local clock can
52 * have a rather large jitter, depending on the interval between
53 * corrections and the intrinsic frequency error of the clock
54 * oscillator. In extreme cases, this can cause clients to exceed the
55 * 128-ms slew window and drop off the NTP subnet.
57 * THis driver includes provisions to telegraph synchronization state
58 * and related variables by means of kernel variables with specially
59 * modified kernels. This is done using the ntp_adjtime() syscall.
60 * In the cases where another protocol or device synchronizes the local
61 * host, the data given to the kernel can be slurped up by this driver
62 * and distributed to clients by ordinary NTP messaging.
64 * In the default mode the behavior of the clock selection algorithm is
65 * modified when this driver is in use. The algorithm is designed so
66 * that this driver will never be selected unless no other discipline
67 * source is available. This can be overriden with the prefer keyword of
68 * the server configuration command, in which case only this driver will
69 * be selected for synchronization and all other discipline sources will
70 * be ignored. This behavior is intended for use when an external
71 * discipline source controls the system clock.
73 * Fudge Factors
75 * The stratum for this driver set at 5 by default, but it can be
76 * changed by the fudge command and/or the ntpdc utility. The reference
77 * ID is 127.0.0.1 by default, but can be changed using the same mechanism.
78 * *NEVER* configure this driver to operate at a stratum which might
79 * possibly disrupt a client with access to a bona fide primary server,
80 * unless the local clock oscillator is reliably disciplined by another
81 * source. *NEVER NEVER* configure a server which might devolve to an
82 * undisciplined local clock to use multicast mode. Always remember that
83 * an improperly configured local clock driver let loose in the Internet
84 * can cause very serious disruption. This is why most of us who care
85 * about good time use cryptographic authentication.
87 * This driver provides a mechanism to trim the local clock in both time
88 * and frequency, as well as a way to manipulate the leap bits. The
89 * fudge time1 parameter adjusts the time, in seconds, and the fudge
90 * time2 parameter adjusts the frequency, in ppm. The fudge time1
91 * parameter is additive; that is, it adds an increment to the current
92 * time. The fudge time2 parameter directly sets the frequency.
95 * Local interface definitions
97 #define PRECISION (-7) /* about 10 ms precision */
98 #define DESCRIPTION "Undisciplined local clock" /* WRU */
99 #define STRATUM 5 /* default stratum */
100 #define DISPERSION .01 /* default dispersion (10 ms) */
103 * Imported from the timer module
105 extern u_long current_time;
108 * Imported from ntp_proto
110 extern s_char sys_precision;
112 #ifdef KERNEL_PLL
114 * Imported from ntp_loopfilter
116 extern int pll_control; /* kernel pll control */
117 extern int kern_enable; /* kernel pll enabled */
118 extern int ext_enable; /* external clock enable */
119 #endif /* KERNEL_PLL */
122 * Function prototypes
124 static int local_start P((int, struct peer *));
125 static void local_poll P((int, struct peer *));
128 * Local variables
130 static u_long poll_time; /* last time polled */
133 * Transfer vector
135 struct refclock refclock_local = {
136 local_start, /* start up driver */
137 noentry, /* shut down driver (not used) */
138 local_poll, /* transmit poll message */
139 noentry, /* not used (old lcl_control) */
140 noentry, /* initialize driver (not used) */
141 noentry, /* not used (old lcl_buginfo) */
142 NOFLAGS /* not used */
147 * local_start - start up the clock
149 static int
150 local_start(
151 int unit,
152 struct peer *peer
155 struct refclockproc *pp;
157 pp = peer->procptr;
160 * Initialize miscellaneous variables
162 peer->precision = sys_precision;
163 pp->leap = LEAP_NOTINSYNC;
164 peer->stratum = STRATUM;
165 pp->stratum = STRATUM;
166 pp->clockdesc = DESCRIPTION;
167 memcpy(&pp->refid, "LOCL", 4);
168 poll_time = current_time;
169 return (1);
174 * local_poll - called by the transmit procedure
176 * LOCKCLOCK: If the kernel supports the nanokernel or microkernel
177 * system calls, the leap bits are extracted from the kernel. If there
178 * is a kernel error or the kernel leap bits are set to 11, the NTP leap
179 * bits are set to 11 and the stratum is set to infinity. Otherwise, the
180 * NTP leap bits are set to the kernel leap bits and the stratum is set
181 * as fudged. This behavior does not faithfully follow the
182 * specification, but is probably more appropriate in a multiple-server
183 * national laboratory network.
185 static void
186 local_poll(
187 int unit,
188 struct peer *peer
191 #if defined(KERNEL_PLL) && defined(LOCKCLOCK)
192 struct timex ntv;
193 #endif /* KERNEL_PLL LOCKCLOCK */
194 struct refclockproc *pp;
196 #if defined(VMS) && defined(VMS_LOCALUNIT)
197 if (unit == VMS_LOCALUNIT) {
198 extern void vms_local_poll(struct peer *);
200 vms_local_poll(peer);
201 return;
203 #endif /* VMS && VMS_LOCALUNIT */
204 pp = peer->procptr;
205 pp->polls++;
208 * Ramble through the usual filtering and grooming code, which
209 * is essentially a no-op and included mostly for pretty
210 * billboards. We allow a one-time time adjustment using fudge
211 * time1 (s) and a continuous frequency adjustment using fudge
212 * time 2 (ppm).
214 get_systime(&pp->lastrec);
215 pp->fudgetime1 += pp->fudgetime2 * 1e-6 * (current_time -
216 poll_time);
217 poll_time = current_time;
218 refclock_process_offset(pp, pp->lastrec, pp->lastrec,
219 pp->fudgetime1);
222 * If another process is disciplining the system clock, we set
223 * the leap bits and quality indicators from the kernel.
225 #if defined(KERNEL_PLL) && defined(LOCKCLOCK)
226 memset(&ntv, 0, sizeof ntv);
227 switch (ntp_adjtime(&ntv)) {
228 case TIME_OK:
229 pp->leap = LEAP_NOWARNING;
230 peer->stratum = pp->stratum;
231 break;
233 case TIME_INS:
234 pp->leap = LEAP_ADDSECOND;
235 peer->stratum = pp->stratum;
236 break;
238 case TIME_DEL:
239 pp->leap = LEAP_DELSECOND;
240 peer->stratum = pp->stratum;
241 break;
243 default:
244 pp->leap = LEAP_NOTINSYNC;
245 peer->stratum = STRATUM_UNSPEC;
247 pp->disp = 0;
248 pp->jitter = 0;
249 #else /* KERNEL_PLL LOCKCLOCK */
250 pp->leap = LEAP_NOWARNING;
251 pp->disp = DISPERSION;
252 pp->jitter = 0;
253 #endif /* KERNEL_PLL LOCKCLOCK */
254 pp->lastref = pp->lastrec;
255 refclock_receive(peer);
256 pp->fudgetime1 = 0;
258 #else
259 int refclock_local_bs;
260 #endif /* REFCLOCK */