refclock_local.c revision 285612
1
2/*
3 * refclock_local - local pseudo-clock driver
4 *
5 * wjm 17-aug-1995: add a hook for special treatment of VMS_LOCALUNIT
6 */
7#ifdef HAVE_CONFIG_H
8#include <config.h>
9#endif
10
11#ifdef REFCLOCK
12
13#include "ntpd.h"
14#include "ntp_refclock.h"
15#include "ntp_stdlib.h"
16
17#include <stdio.h>
18#include <ctype.h>
19
20#ifdef KERNEL_PLL
21#include "ntp_syscall.h"
22#endif
23
24/*
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. Note that the clock selection algorithm will not select this
28 * driver unless all other sources of synchronization have been lost.
29 * This is useful if you want to use NTP in an isolated environment
30 * with no radio clock or NIST modem available. Pick a machine that you
31 * figure has a good clock oscillator and configure it with this
32 * 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.
35 *
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. However, the
40 * preferred was to do this is using orphan mode. See the documentation.
41 *
42 * A third application for this driver is when an external discipline
43 * source is available, such as the NIST "lockclock" program, which
44 * synchronizes the local clock via a telephone modem and the NIST
45 * Automated Computer Time Service (ACTS), or the Digital Time
46 * Synchronization Service (DTSS), which runs on DCE machines. In this
47 * case the stratum should be set at zero, indicating a bona fide
48 * stratum-1 source. Exercise some caution with this, since there is no
49 * easy way to telegraph via NTP that something might be wrong in the
50 * discipline source itself. In the case of DTSS, the local clock can
51 * have a rather large jitter, depending on the interval between
52 * corrections and the intrinsic frequency error of the clock
53 * oscillator. In extreme cases, this can cause clients to exceed the
54 * 128-ms slew window and drop off the NTP subnet.
55 *
56 * Fudge Factors
57 *
58 * If fudge flag1 is lit, the leap second bit is set in the peer
59 * status word. It should be set early in the day of a leap second
60 * event and set dark on the day after the event.
61 *
62 * Note the fudge time1 and time2 have been deprecated. The fudge time1
63 * was intended to apply a bias offset. This can be done using the Unix
64 * date command. The fudge time2 was intended to apply a bias frequency.
65 * This can be done using the frequency file and/or the freq
66 * configuration command.
67 */
68/*
69 * Local interface definitions
70 */
71#define PRECISION	(-7)	/* about 10 ms precision */
72#define DESCRIPTION "Undisciplined local clock" /* WRU */
73#define STRATUM 	5	/* default stratum */
74#define DISPERSION	.01	/* default dispersion (10 ms) */
75
76/*
77 * Imported from the timer module
78 */
79extern u_long current_time;
80
81/*
82 * Imported from ntp_proto
83 */
84extern s_char sys_precision;
85
86/*
87 * Function prototypes
88 */
89static	int local_start (int, struct peer *);
90static	void	local_poll	(int, struct peer *);
91
92/*
93 * Local variables
94 */
95static	u_long poll_time;	/* last time polled */
96
97/*
98 * Transfer vector
99 */
100struct	refclock refclock_local = {
101	local_start,		/* start up driver */
102	noentry,		/* shut down driver (not used) */
103	local_poll,	 	/* transmit poll message */
104	noentry,		/* not used (old lcl_control) */
105	noentry,		/* initialize driver (not used) */
106	noentry,		/* not used (old lcl_buginfo) */
107	NOFLAGS 		/* not used */
108};
109
110
111/*
112 * local_start - start up the clock
113 */
114static int
115local_start(
116	int unit,
117	struct peer *peer
118	)
119{
120	struct refclockproc *pp;
121
122	pp = peer->procptr;
123
124	/*
125	 * Initialize miscellaneous variables
126	 */
127	peer->precision = sys_precision;
128	pp->leap = LEAP_NOTINSYNC;
129	peer->stratum = STRATUM;
130	pp->stratum = STRATUM;
131	pp->clockdesc = DESCRIPTION;
132	memcpy(&pp->refid, "LOCL", 4);
133	poll_time = current_time;
134	return (1);
135}
136
137
138/*
139 * local_poll - called by the transmit procedure
140 *
141 * LOCKCLOCK: If the kernel supports the nanokernel or microkernel
142 * system calls, the leap bits are extracted from the kernel. If there
143 * is a kernel error or the kernel leap bits are set to 11, the NTP leap
144 * bits are set to 11 and the stratum is set to infinity. Otherwise, the
145 * NTP leap bits are set to the kernel leap bits and the stratum is set
146 * as fudged. This behavior does not faithfully follow the
147 * specification, but is probably more appropriate in a multiple-server
148 * national laboratory network.
149 */
150static void
151local_poll(
152	int unit,
153	struct peer *peer
154	)
155{
156#if defined(KERNEL_PLL) && defined(LOCKCLOCK)
157	struct timex ntv;
158#endif /* KERNEL_PLL LOCKCLOCK */
159	struct refclockproc *pp;
160
161	/*
162	 * Do no evil unless the house is dark or lit with our own lamp.
163	 */
164	if (!(sys_peer == NULL || sys_peer == peer))
165		return;
166
167#if defined(VMS) && defined(VMS_LOCALUNIT)
168	if (unit == VMS_LOCALUNIT) {
169		extern void vms_local_poll(struct peer *);
170
171		vms_local_poll(peer);
172		return;
173	}
174#endif /* VMS && VMS_LOCALUNIT */
175
176	pp = peer->procptr;
177	pp->polls++;
178
179	/*
180	 * Ramble through the usual filtering and grooming code, which
181	 * is essentially a no-op and included mostly for pretty
182	 * billboards. We allow a one-time time adjustment using fudge
183	 * time1 (s) and a continuous frequency adjustment using fudge
184	 * time 2 (ppm).
185	 */
186	poll_time = current_time;
187	refclock_process_offset(pp, pp->lastrec, pp->lastrec, 0);
188
189	/*
190	 * If another process is disciplining the system clock, we set
191	 * the leap bits and quality indicators from the kernel.
192	 */
193#if defined(KERNEL_PLL) && defined(LOCKCLOCK)
194	memset(&ntv,  0, sizeof ntv);
195	switch (ntp_adjtime(&ntv)) {
196	case TIME_OK:
197		pp->leap = LEAP_NOWARNING;
198		peer->stratum = pp->stratum;
199		break;
200
201	case TIME_INS:
202		pp->leap = LEAP_ADDSECOND;
203		peer->stratum = pp->stratum;
204		break;
205
206	case TIME_DEL:
207		pp->leap = LEAP_DELSECOND;
208		peer->stratum = pp->stratum;
209		break;
210
211	default:
212		pp->leap = LEAP_NOTINSYNC;
213		peer->stratum = STRATUM_UNSPEC;
214	}
215	pp->disp = 0;
216	pp->jitter = 0;
217#else /* KERNEL_PLL LOCKCLOCK */
218	if (pp->sloppyclockflag & CLK_FLAG1)
219		pp->leap = LEAP_ADDSECOND;
220	else
221		pp->leap = LEAP_NOWARNING;
222	pp->disp = DISPERSION;
223	pp->jitter = 0;
224#endif /* KERNEL_PLL LOCKCLOCK */
225	pp->lastref = pp->lastrec;
226	refclock_receive(peer);
227}
228#else
229int refclock_local_bs;
230#endif /* REFCLOCK */
231