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