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