3 * refclock_local - local pseudo-clock driver
5 * wjm 17-aug-1995: add a hook for special treatment of VMS_LOCALUNIT
14 #include "ntp_refclock.h"
15 #include "ntp_stdlib.h"
21 #include "ntp_syscall.h"
25 * This is a hack to allow a machine to use its own system clock as a
26 * reference clock, i.e., to free-run using no outside clock discipline
27 * source. This is useful if you want to use NTP in an isolated
28 * environment with no radio clock or NIST modem available. Pick a
29 * machine that you figure has a good clock oscillator and configure it
30 * with this driver. Set the clock using the best means available, like
31 * eyeball-and-wristwatch. Then, point all the other machines at this
32 * one or use broadcast (not multicast) mode to distribute time.
34 * Another application for this driver is if you want to use a
35 * particular server's clock as the clock of last resort when all other
36 * normal synchronization sources have gone away. This is especially
37 * useful if that server has an ovenized oscillator. For this you would
38 * configure this driver at a higher stratum (say 5) to prevent the
39 * server's stratum from falling below that.
41 * A third application for this driver is when an external discipline
42 * source is available, such as the NIST "lockclock" program, which
43 * synchronizes the local clock via a telephone modem and the NIST
44 * Automated Computer Time Service (ACTS), or the Digital Time
45 * Synchronization Service (DTSS), which runs on DCE machines. In this
46 * case the stratum should be set at zero, indicating a bona fide
47 * stratum-1 source. Exercise some caution with this, since there is no
48 * easy way to telegraph via NTP that something might be wrong in the
49 * discipline source itself. In the case of DTSS, the local clock can
50 * have a rather large jitter, depending on the interval between
51 * corrections and the intrinsic frequency error of the clock
52 * oscillator. In extreme cases, this can cause clients to exceed the
53 * 128-ms slew window and drop off the NTP subnet.
55 * THis driver includes provisions to telegraph synchronization state
56 * and related variables by means of kernel variables with specially
57 * modified kernels. This is done using the ntp_adjtime() syscall.
58 * In the cases where another protocol or device synchronizes the local
59 * host, the data given to the kernel can be slurped up by this driver
60 * and distributed to clients by ordinary NTP messaging.
62 * In the default mode the behavior of the clock selection algorithm is
63 * modified when this driver is in use. The algorithm is designed so
64 * that this driver will never be selected unless no other discipline
65 * source is available. This can be overriden with the prefer keyword of
66 * the server configuration command, in which case only this driver will
67 * be selected for synchronization and all other discipline sources will
68 * be ignored. This behavior is intended for use when an external
69 * discipline source controls the system clock.
73 * The stratum for this driver set at 5 by default, but it can be
74 * changed by the fudge command and/or the ntpdc utility. The reference
75 * ID is 127.0.0.1 by default, but can be changed using the same mechanism.
76 * *NEVER* configure this driver to operate at a stratum which might
77 * possibly disrupt a client with access to a bona fide primary server,
78 * unless the local clock oscillator is reliably disciplined by another
79 * source. *NEVER NEVER* configure a server which might devolve to an
80 * undisciplined local clock to use multicast mode. Always remember that
81 * an improperly configured local clock driver let loose in the Internet
82 * can cause very serious disruption. This is why most of us who care
83 * about good time use cryptographic authentication.
85 * This driver provides a mechanism to trim the local clock in both time
86 * and frequency, as well as a way to manipulate the leap bits. The
87 * fudge time1 parameter adjusts the time, in seconds, and the fudge
88 * time2 parameter adjusts the frequency, in ppm. The fudge time1
89 * parameter is additive; that is, it adds an increment to the current
90 * time. The fudge time2 parameter directly sets the frequency.
93 * Local interface definitions
95 #define PRECISION (-7) /* about 10 ms precision */
96 #define DESCRIPTION "Undisciplined local clock" /* WRU */
97 #define STRATUM 5 /* default stratum */
98 #define DISPERSION .01 /* default dispersion (10 ms) */
101 * Imported from the timer module
103 extern u_long current_time;
106 * Imported from ntp_proto
108 extern s_char sys_precision;
112 * Imported from ntp_loopfilter
114 extern int pll_control; /* kernel pll control */
115 extern int kern_enable; /* kernel pll enabled */
116 extern int ext_enable; /* external clock enable */
117 #endif /* KERNEL_PLL */
120 * Function prototypes
122 static int local_start P((int, struct peer *));
123 static void local_poll P((int, struct peer *));
128 static u_long poll_time; /* last time polled */
133 struct refclock refclock_local = {
134 local_start, /* start up driver */
135 noentry, /* shut down driver (not used) */
136 local_poll, /* transmit poll message */
137 noentry, /* not used (old lcl_control) */
138 noentry, /* initialize driver (not used) */
139 noentry, /* not used (old lcl_buginfo) */
140 NOFLAGS /* not used */
145 * local_start - start up the clock
153 struct refclockproc *pp;
158 * Initialize miscellaneous variables
160 peer->precision = sys_precision;
161 pp->leap = LEAP_NOTINSYNC;
162 peer->stratum = STRATUM;
163 pp->stratum = STRATUM;
164 pp->clockdesc = DESCRIPTION;
165 memcpy(&pp->refid, "LOCL", 4);
166 poll_time = current_time;
172 * local_poll - called by the transmit procedure
174 * LOCKCLOCK: If the kernel supports the nanokernel or microkernel
175 * system calls, the leap bits are extracted from the kernel. If there
176 * is a kernel error or the kernel leap bits are set to 11, the NTP leap
177 * bits are set to 11 and the stratum is set to infinity. Otherwise, the
178 * NTP leap bits are set to the kernel leap bits and the stratum is set
179 * as fudged. This behavior does not faithfully follow the
180 * specification, but is probably more appropriate in a multiple-server
181 * national laboratory network.
189 #if defined(KERNEL_PLL) && defined(LOCKCLOCK)
191 #endif /* KERNEL_PLL LOCKCLOCK */
192 struct refclockproc *pp;
194 #if defined(VMS) && defined(VMS_LOCALUNIT)
195 if (unit == VMS_LOCALUNIT) {
196 extern void vms_local_poll(struct peer *);
198 vms_local_poll(peer);
201 #endif /* VMS && VMS_LOCALUNIT */
206 * Ramble through the usual filtering and grooming code, which
207 * is essentially a no-op and included mostly for pretty
208 * billboards. We allow a one-time time adjustment using fudge
209 * time1 (s) and a continuous frequency adjustment using fudge
212 get_systime(&pp->lastrec);
213 pp->fudgetime1 += pp->fudgetime2 * 1e-6 * (current_time -
215 poll_time = current_time;
216 refclock_process_offset(pp, pp->lastrec, pp->lastrec,
220 * If another process is disciplining the system clock, we set
221 * the leap bits and quality indicators from the kernel.
223 #if defined(KERNEL_PLL) && defined(LOCKCLOCK)
224 memset(&ntv, 0, sizeof ntv);
225 switch (ntp_adjtime(&ntv)) {
227 pp->leap = LEAP_NOWARNING;
228 peer->stratum = pp->stratum;
232 pp->leap = LEAP_ADDSECOND;
233 peer->stratum = pp->stratum;
237 pp->leap = LEAP_DELSECOND;
238 peer->stratum = pp->stratum;
242 pp->leap = LEAP_NOTINSYNC;
243 peer->stratum = STRATUM_UNSPEC;
247 #else /* KERNEL_PLL LOCKCLOCK */
248 pp->leap = LEAP_NOWARNING;
249 pp->disp = DISPERSION;
251 #endif /* KERNEL_PLL LOCKCLOCK */
252 pp->lastref = pp->lastrec;
253 refclock_receive(peer);
257 int refclock_local_bs;
258 #endif /* REFCLOCK */