refclock_shm.c revision 1.1.1.2.16.1 1 1.1.1.2.16.1 snj /* $NetBSD: refclock_shm.c,v 1.1.1.2.16.1 2014/12/25 02:13:07 snj Exp $ */
2 1.1 kardel
3 1.1 kardel /*
4 1.1.1.2.16.1 snj * refclock_shm - clock driver for utc via shared memory
5 1.1 kardel * - under construction -
6 1.1 kardel * To add new modes: Extend or union the shmTime-struct. Do not
7 1.1 kardel * extend/shrink size, because otherwise existing implementations
8 1.1 kardel * will specify wrong size of shared memory-segment
9 1.1 kardel * PB 18.3.97
10 1.1 kardel */
11 1.1 kardel
12 1.1 kardel #ifdef HAVE_CONFIG_H
13 1.1 kardel # include <config.h>
14 1.1 kardel #endif
15 1.1 kardel
16 1.1.1.2.16.1 snj #include "ntp_types.h"
17 1.1.1.2.16.1 snj
18 1.1 kardel #if defined(REFCLOCK) && defined(CLOCK_SHM)
19 1.1 kardel
20 1.1 kardel #include "ntpd.h"
21 1.1.1.2.16.1 snj #undef fileno
22 1.1 kardel #include "ntp_io.h"
23 1.1.1.2.16.1 snj #undef fileno
24 1.1 kardel #include "ntp_refclock.h"
25 1.1.1.2.16.1 snj #undef fileno
26 1.1.1.2.16.1 snj #include "timespecops.h"
27 1.1.1.2.16.1 snj #undef fileno
28 1.1 kardel #include "ntp_stdlib.h"
29 1.1 kardel
30 1.1.1.2.16.1 snj #undef fileno
31 1.1 kardel #include <ctype.h>
32 1.1.1.2.16.1 snj #undef fileno
33 1.1 kardel
34 1.1 kardel #ifndef SYS_WINNT
35 1.1 kardel # include <sys/ipc.h>
36 1.1 kardel # include <sys/shm.h>
37 1.1 kardel # include <assert.h>
38 1.1 kardel # include <unistd.h>
39 1.1 kardel # include <stdio.h>
40 1.1 kardel #endif
41 1.1 kardel
42 1.1 kardel /*
43 1.1 kardel * This driver supports a reference clock attached thru shared memory
44 1.1.1.2.16.1 snj */
45 1.1 kardel
46 1.1 kardel /*
47 1.1 kardel * SHM interface definitions
48 1.1 kardel */
49 1.1 kardel #define PRECISION (-1) /* precision assumed (0.5 s) */
50 1.1 kardel #define REFID "SHM" /* reference ID */
51 1.1 kardel #define DESCRIPTION "SHM/Shared memory interface"
52 1.1 kardel
53 1.1 kardel #define NSAMPLES 3 /* stages of median filter */
54 1.1 kardel
55 1.1 kardel /*
56 1.1 kardel * Function prototypes
57 1.1 kardel */
58 1.1 kardel static int shm_start (int unit, struct peer *peer);
59 1.1 kardel static void shm_shutdown (int unit, struct peer *peer);
60 1.1 kardel static void shm_poll (int unit, struct peer *peer);
61 1.1 kardel static void shm_timer (int unit, struct peer *peer);
62 1.1.1.2.16.1 snj static void shm_peek (int unit, struct peer *peer);
63 1.1.1.2.16.1 snj static void shm_clockstats (int unit, struct peer *peer);
64 1.1.1.2.16.1 snj static void shm_control (int unit, const struct refclockstat * in_st,
65 1.1.1.2.16.1 snj struct refclockstat * out_st, struct peer *peer);
66 1.1 kardel
67 1.1 kardel /*
68 1.1 kardel * Transfer vector
69 1.1 kardel */
70 1.1 kardel struct refclock refclock_shm = {
71 1.1 kardel shm_start, /* start up driver */
72 1.1 kardel shm_shutdown, /* shut down driver */
73 1.1 kardel shm_poll, /* transmit poll message */
74 1.1.1.2.16.1 snj shm_control, /* control settings */
75 1.1 kardel noentry, /* not used: init */
76 1.1 kardel noentry, /* not used: buginfo */
77 1.1 kardel shm_timer, /* once per second */
78 1.1 kardel };
79 1.1 kardel
80 1.1 kardel struct shmTime {
81 1.1.1.2.16.1 snj int mode; /* 0 - if valid is set:
82 1.1.1.2.16.1 snj * use values,
83 1.1 kardel * clear valid
84 1.1.1.2.16.1 snj * 1 - if valid is set:
85 1.1 kardel * if count before and after read of values is equal,
86 1.1.1.2.16.1 snj * use values
87 1.1 kardel * clear valid
88 1.1 kardel */
89 1.1.1.2.16.1 snj volatile int count;
90 1.1.1.2.16.1 snj time_t clockTimeStampSec;
91 1.1.1.2.16.1 snj int clockTimeStampUSec;
92 1.1.1.2.16.1 snj time_t receiveTimeStampSec;
93 1.1.1.2.16.1 snj int receiveTimeStampUSec;
94 1.1.1.2.16.1 snj int leap;
95 1.1.1.2.16.1 snj int precision;
96 1.1.1.2.16.1 snj int nsamples;
97 1.1.1.2.16.1 snj volatile int valid;
98 1.1.1.2.16.1 snj unsigned clockTimeStampNSec; /* Unsigned ns timestamps */
99 1.1.1.2.16.1 snj unsigned receiveTimeStampNSec; /* Unsigned ns timestamps */
100 1.1.1.2.16.1 snj int dummy[8];
101 1.1 kardel };
102 1.1 kardel
103 1.1 kardel struct shmunit {
104 1.1 kardel struct shmTime *shm; /* pointer to shared memory segment */
105 1.1 kardel
106 1.1 kardel /* debugging/monitoring counters - reset when printed */
107 1.1 kardel int ticks; /* number of attempts to read data*/
108 1.1 kardel int good; /* number of valid samples */
109 1.1 kardel int notready; /* number of peeks without data ready */
110 1.1 kardel int bad; /* number of invalid samples */
111 1.1 kardel int clash; /* number of access clashes while reading */
112 1.1.1.2.16.1 snj
113 1.1.1.2.16.1 snj time_t max_delta; /* difference limit */
114 1.1.1.2.16.1 snj time_t max_delay; /* age/stale limit */
115 1.1 kardel };
116 1.1 kardel
117 1.1 kardel
118 1.1 kardel struct shmTime *getShmTime(int);
119 1.1 kardel
120 1.1 kardel struct shmTime *getShmTime (int unit) {
121 1.1 kardel #ifndef SYS_WINNT
122 1.1 kardel int shmid=0;
123 1.1 kardel
124 1.1 kardel /* 0x4e545030 is NTP0.
125 1.1 kardel * Big units will give non-ascii but that's OK
126 1.1.1.2.16.1 snj * as long as everybody does it the same way.
127 1.1 kardel */
128 1.1.1.2.16.1 snj shmid=shmget (0x4e545030 + unit, sizeof (struct shmTime),
129 1.1.1.2.16.1 snj IPC_CREAT | ((unit < 2) ? 0600 : 0666));
130 1.1.1.2.16.1 snj if (shmid == -1) { /* error */
131 1.1.1.2.16.1 snj msyslog(LOG_ERR, "SHM shmget (unit %d): %m", unit);
132 1.1 kardel return 0;
133 1.1 kardel }
134 1.1 kardel else { /* no error */
135 1.1.1.2.16.1 snj struct shmTime *p = (struct shmTime *)shmat (shmid, 0, 0);
136 1.1.1.2.16.1 snj if (p == (struct shmTime *)-1) { /* error */
137 1.1.1.2.16.1 snj msyslog(LOG_ERR, "SHM shmat (unit %d): %m", unit);
138 1.1 kardel return 0;
139 1.1 kardel }
140 1.1 kardel return p;
141 1.1 kardel }
142 1.1 kardel #else
143 1.1 kardel char buf[10];
144 1.1.1.2.16.1 snj LPSECURITY_ATTRIBUTES psec = 0;
145 1.1.1.2.16.1 snj HANDLE shmid = 0;
146 1.1 kardel SECURITY_DESCRIPTOR sd;
147 1.1 kardel SECURITY_ATTRIBUTES sa;
148 1.1.1.2.16.1 snj
149 1.1.1.2 kardel snprintf(buf, sizeof(buf), "NTP%d", unit);
150 1.1.1.2 kardel if (unit >= 2) { /* world access */
151 1.1 kardel if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) {
152 1.1.1.2.16.1 snj msyslog(LOG_ERR,"SHM InitializeSecurityDescriptor (unit %d): %m", unit);
153 1.1 kardel return 0;
154 1.1 kardel }
155 1.1.1.2.16.1 snj if (!SetSecurityDescriptorDacl(&sd, 1, 0, 0)) {
156 1.1.1.2.16.1 snj msyslog(LOG_ERR, "SHM SetSecurityDescriptorDacl (unit %d): %m", unit);
157 1.1 kardel return 0;
158 1.1 kardel }
159 1.1 kardel sa.nLength=sizeof (SECURITY_ATTRIBUTES);
160 1.1.1.2.16.1 snj sa.lpSecurityDescriptor = &sd;
161 1.1.1.2.16.1 snj sa.bInheritHandle = 0;
162 1.1.1.2.16.1 snj psec = &sa;
163 1.1 kardel }
164 1.1.1.2.16.1 snj shmid = CreateFileMapping ((HANDLE)0xffffffff, psec, PAGE_READWRITE,
165 1.1.1.2.16.1 snj 0, sizeof (struct shmTime), buf);
166 1.1 kardel if (!shmid) { /*error*/
167 1.1 kardel char buf[1000];
168 1.1.1.2.16.1 snj
169 1.1 kardel FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
170 1.1 kardel 0, GetLastError (), 0, buf, sizeof (buf), 0);
171 1.1.1.2.16.1 snj msyslog(LOG_ERR, "SHM CreateFileMapping (unit %d): %s", unit, buf);
172 1.1 kardel return 0;
173 1.1.1.2.16.1 snj } else {
174 1.1.1.2.16.1 snj struct shmTime *p = (struct shmTime *) MapViewOfFile (shmid,
175 1.1 kardel FILE_MAP_WRITE, 0, 0, sizeof (struct shmTime));
176 1.1.1.2.16.1 snj if (p == 0) { /*error*/
177 1.1 kardel char buf[1000];
178 1.1.1.2.16.1 snj
179 1.1 kardel FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
180 1.1 kardel 0, GetLastError (), 0, buf, sizeof (buf), 0);
181 1.1.1.2.16.1 snj msyslog(LOG_ERR,"SHM MapViewOfFile (unit %d): %s", unit, buf)
182 1.1 kardel return 0;
183 1.1 kardel }
184 1.1 kardel return p;
185 1.1 kardel }
186 1.1 kardel #endif
187 1.1 kardel }
188 1.1 kardel /*
189 1.1 kardel * shm_start - attach to shared memory
190 1.1 kardel */
191 1.1 kardel static int
192 1.1 kardel shm_start(
193 1.1 kardel int unit,
194 1.1 kardel struct peer *peer
195 1.1 kardel )
196 1.1 kardel {
197 1.1 kardel struct refclockproc *pp;
198 1.1 kardel struct shmunit *up;
199 1.1 kardel
200 1.1 kardel pp = peer->procptr;
201 1.1 kardel pp->io.clock_recv = noentry;
202 1.1.1.2.16.1 snj pp->io.srcclock = peer;
203 1.1 kardel pp->io.datalen = 0;
204 1.1 kardel pp->io.fd = -1;
205 1.1 kardel
206 1.1.1.2.16.1 snj up = emalloc_zero(sizeof(*up));
207 1.1 kardel
208 1.1 kardel up->shm = getShmTime(unit);
209 1.1 kardel
210 1.1 kardel /*
211 1.1 kardel * Initialize miscellaneous peer variables
212 1.1 kardel */
213 1.1 kardel memcpy((char *)&pp->refid, REFID, 4);
214 1.1 kardel if (up->shm != 0) {
215 1.1.1.2.16.1 snj pp->unitptr = up;
216 1.1 kardel up->shm->precision = PRECISION;
217 1.1 kardel peer->precision = up->shm->precision;
218 1.1.1.2.16.1 snj up->shm->valid = 0;
219 1.1.1.2.16.1 snj up->shm->nsamples = NSAMPLES;
220 1.1 kardel pp->clockdesc = DESCRIPTION;
221 1.1.1.2.16.1 snj /* items to be changed later in 'shm_control()': */
222 1.1.1.2.16.1 snj up->max_delay = 5;
223 1.1.1.2.16.1 snj up->max_delta = 4*3600;
224 1.1.1.2.16.1 snj return 1;
225 1.1.1.2.16.1 snj } else {
226 1.1.1.2.16.1 snj free(up);
227 1.1.1.2.16.1 snj pp->unitptr = NULL;
228 1.1 kardel return 0;
229 1.1 kardel }
230 1.1 kardel }
231 1.1 kardel
232 1.1 kardel
233 1.1 kardel /*
234 1.1.1.2.16.1 snj * shm_control - configure flag1/time2 params
235 1.1.1.2.16.1 snj *
236 1.1.1.2.16.1 snj * These are not yet available during 'shm_start', so we have to do any
237 1.1.1.2.16.1 snj * pre-computations we want to avoid during regular poll/timer callbacks
238 1.1.1.2.16.1 snj * in this callback.
239 1.1.1.2.16.1 snj */
240 1.1.1.2.16.1 snj static void
241 1.1.1.2.16.1 snj shm_control(
242 1.1.1.2.16.1 snj int unit,
243 1.1.1.2.16.1 snj const struct refclockstat * in_st,
244 1.1.1.2.16.1 snj struct refclockstat * out_st,
245 1.1.1.2.16.1 snj struct peer * peer
246 1.1.1.2.16.1 snj )
247 1.1.1.2.16.1 snj {
248 1.1.1.2.16.1 snj struct refclockproc *pp;
249 1.1.1.2.16.1 snj struct shmunit *up;
250 1.1.1.2.16.1 snj
251 1.1.1.2.16.1 snj pp = peer->procptr;
252 1.1.1.2.16.1 snj up = pp->unitptr;
253 1.1.1.2.16.1 snj
254 1.1.1.2.16.1 snj if (NULL == up)
255 1.1.1.2.16.1 snj return;
256 1.1.1.2.16.1 snj if (pp->sloppyclockflag & CLK_FLAG1)
257 1.1.1.2.16.1 snj up->max_delta = 0;
258 1.1.1.2.16.1 snj else if (pp->fudgetime2 < 1. || pp->fudgetime2 > 86400.)
259 1.1.1.2.16.1 snj up->max_delta = 4*3600;
260 1.1.1.2.16.1 snj else
261 1.1.1.2.16.1 snj up->max_delta = (time_t)floor(pp->fudgetime2 + 0.5);
262 1.1.1.2.16.1 snj }
263 1.1.1.2.16.1 snj
264 1.1.1.2.16.1 snj
265 1.1.1.2.16.1 snj /*
266 1.1 kardel * shm_shutdown - shut down the clock
267 1.1 kardel */
268 1.1 kardel static void
269 1.1 kardel shm_shutdown(
270 1.1 kardel int unit,
271 1.1 kardel struct peer *peer
272 1.1 kardel )
273 1.1 kardel {
274 1.1 kardel struct refclockproc *pp;
275 1.1 kardel struct shmunit *up;
276 1.1 kardel
277 1.1 kardel pp = peer->procptr;
278 1.1.1.2.16.1 snj up = pp->unitptr;
279 1.1.1.2 kardel
280 1.1.1.2 kardel if (NULL == up)
281 1.1.1.2 kardel return;
282 1.1 kardel #ifndef SYS_WINNT
283 1.1.1.2.16.1 snj /* HMS: shmdt() wants char* or const void * */
284 1.1 kardel (void) shmdt ((char *)up->shm);
285 1.1 kardel #else
286 1.1 kardel UnmapViewOfFile (up->shm);
287 1.1 kardel #endif
288 1.1.1.2 kardel free(up);
289 1.1 kardel }
290 1.1 kardel
291 1.1 kardel
292 1.1 kardel /*
293 1.1 kardel * shm_timer - called every second
294 1.1 kardel */
295 1.1 kardel static void
296 1.1 kardel shm_timer(int unit, struct peer *peer)
297 1.1 kardel {
298 1.1 kardel shm_peek(unit, peer);
299 1.1 kardel }
300 1.1 kardel
301 1.1 kardel
302 1.1 kardel /*
303 1.1 kardel * shm_poll - called by the transmit procedure
304 1.1 kardel */
305 1.1 kardel static void
306 1.1 kardel shm_poll(
307 1.1 kardel int unit,
308 1.1 kardel struct peer *peer
309 1.1 kardel )
310 1.1 kardel {
311 1.1 kardel struct refclockproc *pp;
312 1.1.1.2.16.1 snj struct shmunit *up;
313 1.1.1.2.16.1 snj int major_error;
314 1.1 kardel
315 1.1 kardel pp = peer->procptr;
316 1.1.1.2.16.1 snj up = pp->unitptr;
317 1.1.1.2.16.1 snj
318 1.1.1.2.16.1 snj pp->polls++;
319 1.1.1.2.16.1 snj
320 1.1.1.2.16.1 snj /* get dominant reason if we have no samples at all */
321 1.1.1.2.16.1 snj major_error = max(up->notready, up->bad);
322 1.1.1.2.16.1 snj major_error = max(major_error, up->clash);
323 1.1 kardel
324 1.1 kardel /*
325 1.1.1.2.16.1 snj * Process median filter samples. If none received, see what
326 1.1.1.2.16.1 snj * happened, tell the core and keep going.
327 1.1 kardel */
328 1.1.1.2.16.1 snj if (pp->coderecv != pp->codeproc) {
329 1.1.1.2.16.1 snj /* have some samples, everything OK */
330 1.1.1.2.16.1 snj pp->lastref = pp->lastrec;
331 1.1.1.2.16.1 snj refclock_receive(peer);
332 1.1.1.2.16.1 snj } else if (NULL == up->shm) { /* is this possible at all? */
333 1.1.1.2.16.1 snj /* we're out of business without SHM access */
334 1.1.1.2.16.1 snj refclock_report(peer, CEVNT_FAULT);
335 1.1.1.2.16.1 snj } else if (major_error == up->clash) {
336 1.1.1.2.16.1 snj /* too many collisions is like a bad signal */
337 1.1.1.2.16.1 snj refclock_report(peer, CEVNT_PROP);
338 1.1.1.2.16.1 snj } else if (major_error == up->bad) {
339 1.1.1.2.16.1 snj /* too much stale/bad/garbled data */
340 1.1.1.2.16.1 snj refclock_report(peer, CEVNT_BADREPLY);
341 1.1.1.2.16.1 snj } else {
342 1.1.1.2.16.1 snj /* in any other case assume it's just a timeout */
343 1.1 kardel refclock_report(peer, CEVNT_TIMEOUT);
344 1.1 kardel }
345 1.1.1.2.16.1 snj /* shm_clockstats() clears the tallies, so it must be last... */
346 1.1 kardel shm_clockstats(unit, peer);
347 1.1 kardel }
348 1.1 kardel
349 1.1 kardel /*
350 1.1 kardel * shm_peek - try to grab a sample
351 1.1 kardel */
352 1.1.1.2.16.1 snj static void
353 1.1.1.2.16.1 snj shm_peek(
354 1.1 kardel int unit,
355 1.1 kardel struct peer *peer
356 1.1 kardel )
357 1.1 kardel {
358 1.1 kardel struct refclockproc *pp;
359 1.1 kardel struct shmunit *up;
360 1.1.1.2.16.1 snj
361 1.1.1.2.16.1 snj /* access order is important for lock-free SHM access; we
362 1.1.1.2.16.1 snj ** enforce order by treating the whole structure volatile.
363 1.1.1.2.16.1 snj **
364 1.1.1.2.16.1 snj ** IMPORTANT NOTE: This does not protect from reordering on CPU
365 1.1.1.2.16.1 snj ** level, and it does nothing for cache consistency and
366 1.1.1.2.16.1 snj ** visibility of changes by other cores. We need atomic and/or
367 1.1.1.2.16.1 snj ** fence instructions for that.
368 1.1.1.2.16.1 snj */
369 1.1.1.2.16.1 snj volatile struct shmTime *shm;
370 1.1.1.2.16.1 snj
371 1.1.1.2.16.1 snj struct timespec tvr;
372 1.1.1.2.16.1 snj struct timespec tvt;
373 1.1.1.2.16.1 snj l_fp tsrcv;
374 1.1.1.2.16.1 snj l_fp tsref;
375 1.1.1.2.16.1 snj unsigned int c;
376 1.1.1.2.16.1 snj unsigned int cns_new, rns_new;
377 1.1.1.2.16.1 snj int cnt;
378 1.1.1.2.16.1 snj
379 1.1.1.2.16.1 snj /* for formatting 'a_lastcode': */
380 1.1.1.2.16.1 snj struct calendar cd;
381 1.1.1.2.16.1 snj time_t tt, now;
382 1.1.1.2.16.1 snj vint64 ts;
383 1.1 kardel
384 1.1 kardel /*
385 1.1 kardel * This is the main routine. It snatches the time from the shm
386 1.1 kardel * board and tacks on a local timestamp.
387 1.1 kardel */
388 1.1 kardel pp = peer->procptr;
389 1.1.1.2.16.1 snj up = pp->unitptr;
390 1.1 kardel up->ticks++;
391 1.1 kardel if (up->shm == 0) {
392 1.1 kardel /* try to map again - this may succeed if meanwhile some-
393 1.1 kardel body has ipcrm'ed the old (unaccessible) shared mem segment */
394 1.1 kardel up->shm = getShmTime(unit);
395 1.1 kardel }
396 1.1 kardel shm = up->shm;
397 1.1 kardel if (shm == 0) {
398 1.1.1.2.16.1 snj DPRINTF(1, ("%s: no SHM segment\n",
399 1.1.1.2.16.1 snj refnumtoa(&peer->srcadr)));
400 1.1.1.2.16.1 snj return;
401 1.1.1.2.16.1 snj }
402 1.1.1.2.16.1 snj if ( ! shm->valid) {
403 1.1.1.2.16.1 snj DPRINTF(1, ("%s: SHM not ready\n",
404 1.1.1.2.16.1 snj refnumtoa(&peer->srcadr)));
405 1.1.1.2.16.1 snj up->notready++;
406 1.1.1.2.16.1 snj return;
407 1.1 kardel }
408 1.1.1.2.16.1 snj
409 1.1.1.2.16.1 snj switch (shm->mode) {
410 1.1.1.2.16.1 snj case 0:
411 1.1.1.2.16.1 snj tvr.tv_sec = shm->receiveTimeStampSec;
412 1.1.1.2.16.1 snj tvr.tv_nsec = shm->receiveTimeStampUSec * 1000;
413 1.1.1.2.16.1 snj rns_new = shm->receiveTimeStampNSec;
414 1.1.1.2.16.1 snj tvt.tv_sec = shm->clockTimeStampSec;
415 1.1.1.2.16.1 snj tvt.tv_nsec = shm->clockTimeStampUSec * 1000;
416 1.1.1.2.16.1 snj cns_new = shm->clockTimeStampNSec;
417 1.1.1.2.16.1 snj
418 1.1.1.2.16.1 snj /* Since the following comparisons are between unsigned
419 1.1.1.2.16.1 snj ** variables they are always well defined, and any
420 1.1.1.2.16.1 snj ** (signed) underflow will turn into very large unsigned
421 1.1.1.2.16.1 snj ** values, well above the 1000 cutoff.
422 1.1.1.2.16.1 snj **
423 1.1.1.2.16.1 snj ** Note: The usecs *must* be a *truncated*
424 1.1.1.2.16.1 snj ** representation of the nsecs. This code will fail for
425 1.1.1.2.16.1 snj ** *rounded* usecs, and the logic to deal with
426 1.1.1.2.16.1 snj ** wrap-arounds in the presence of rounded values is
427 1.1.1.2.16.1 snj ** much more convoluted.
428 1.1.1.2.16.1 snj */
429 1.1.1.2.16.1 snj if ( ((cns_new - (unsigned)tvt.tv_nsec) < 1000)
430 1.1.1.2.16.1 snj && ((rns_new - (unsigned)tvr.tv_nsec) < 1000)) {
431 1.1.1.2.16.1 snj tvt.tv_nsec = cns_new;
432 1.1.1.2.16.1 snj tvr.tv_nsec = rns_new;
433 1.1 kardel }
434 1.1.1.2.16.1 snj /* At this point tvr and tvt contains valid ns-level
435 1.1.1.2.16.1 snj ** timestamps, possibly generated by extending the old
436 1.1.1.2.16.1 snj ** us-level timestamps
437 1.1.1.2.16.1 snj */
438 1.1.1.2.16.1 snj DPRINTF(2, ("%s: SHM type 0 sample\n",
439 1.1.1.2.16.1 snj refnumtoa(&peer->srcadr)));
440 1.1.1.2.16.1 snj break;
441 1.1.1.2.16.1 snj
442 1.1.1.2.16.1 snj case 1:
443 1.1.1.2.16.1 snj cnt = shm->count;
444 1.1.1.2.16.1 snj
445 1.1.1.2.16.1 snj tvr.tv_sec = shm->receiveTimeStampSec;
446 1.1.1.2.16.1 snj tvr.tv_nsec = shm->receiveTimeStampUSec * 1000;
447 1.1.1.2.16.1 snj rns_new = shm->receiveTimeStampNSec;
448 1.1.1.2.16.1 snj tvt.tv_sec = shm->clockTimeStampSec;
449 1.1.1.2.16.1 snj tvt.tv_nsec = shm->clockTimeStampUSec * 1000;
450 1.1.1.2.16.1 snj cns_new = shm->clockTimeStampNSec;
451 1.1.1.2.16.1 snj if (cnt != shm->count) {
452 1.1.1.2.16.1 snj DPRINTF(1, ("%s: type 1 access clash\n",
453 1.1.1.2.16.1 snj refnumtoa(&peer->srcadr)));
454 1.1 kardel msyslog (LOG_NOTICE, "SHM: access clash in shared memory");
455 1.1 kardel up->clash++;
456 1.1.1.2.16.1 snj return;
457 1.1.1.2.16.1 snj }
458 1.1.1.2.16.1 snj
459 1.1.1.2.16.1 snj /* See the case above for an explanation of the
460 1.1.1.2.16.1 snj ** following test.
461 1.1.1.2.16.1 snj */
462 1.1.1.2.16.1 snj if ( ((cns_new - (unsigned)tvt.tv_nsec) < 1000)
463 1.1.1.2.16.1 snj && ((rns_new - (unsigned)tvr.tv_nsec) < 1000)) {
464 1.1.1.2.16.1 snj tvt.tv_nsec = cns_new;
465 1.1.1.2.16.1 snj tvr.tv_nsec = rns_new;
466 1.1 kardel }
467 1.1.1.2.16.1 snj /* At this point tvr and tvt contains valid ns-level
468 1.1.1.2.16.1 snj ** timestamps, possibly generated by extending the old
469 1.1.1.2.16.1 snj ** us-level timestamps
470 1.1.1.2.16.1 snj */
471 1.1.1.2.16.1 snj DPRINTF(2, ("%s: SHM type 1 sample\n",
472 1.1.1.2.16.1 snj refnumtoa(&peer->srcadr)));
473 1.1.1.2.16.1 snj break;
474 1.1.1.2.16.1 snj
475 1.1.1.2.16.1 snj default:
476 1.1.1.2.16.1 snj DPRINTF(1, ("%s: SHM type blooper, mode=%d\n",
477 1.1.1.2.16.1 snj refnumtoa(&peer->srcadr), shm->mode));
478 1.1.1.2.16.1 snj up->bad++;
479 1.1.1.2.16.1 snj msyslog (LOG_ERR, "SHM: bad mode found in shared memory: %d",
480 1.1.1.2.16.1 snj shm->mode);
481 1.1.1.2.16.1 snj return;
482 1.1 kardel }
483 1.1.1.2.16.1 snj shm->valid = 0;
484 1.1.1.2.16.1 snj
485 1.1.1.2.16.1 snj /* format the last time code in human-readable form into
486 1.1.1.2.16.1 snj * 'pp->a_lastcode'. Someone claimed: "NetBSD has incompatible
487 1.1.1.2.16.1 snj * tv_sec". I can't find a base for this claim, but we can work
488 1.1.1.2.16.1 snj * around that potential problem. BTW, simply casting a pointer
489 1.1.1.2.16.1 snj * is a receipe for disaster on some architectures.
490 1.1.1.2.16.1 snj */
491 1.1.1.2.16.1 snj tt = (time_t)tvt.tv_sec;
492 1.1.1.2.16.1 snj ts = time_to_vint64(&tt);
493 1.1.1.2.16.1 snj ntpcal_time_to_date(&cd, &ts);
494 1.1.1.2.16.1 snj
495 1.1.1.2.16.1 snj /* add ntpq -c cv timecode in ISO 8601 format */
496 1.1.1.2.16.1 snj c = snprintf(pp->a_lastcode, sizeof(pp->a_lastcode),
497 1.1.1.2.16.1 snj "%04u-%02u-%02uT%02u:%02u:%02u.%09ldZ",
498 1.1.1.2.16.1 snj cd.year, cd.month, cd.monthday,
499 1.1.1.2.16.1 snj cd.hour, cd.minute, cd.second,
500 1.1.1.2.16.1 snj (long)tvt.tv_nsec);
501 1.1.1.2.16.1 snj pp->lencode = (c < sizeof(pp->a_lastcode)) ? c : 0;
502 1.1.1.2.16.1 snj
503 1.1.1.2.16.1 snj /* check 1: age control of local time stamp */
504 1.1.1.2.16.1 snj time(&now);
505 1.1.1.2.16.1 snj tt = now - tvr.tv_sec;
506 1.1.1.2.16.1 snj if (tt < 0 || tt > up->max_delay) {
507 1.1.1.2.16.1 snj DPRINTF(1, ("%s:SHM stale/bad receive time, delay=%llds\n",
508 1.1.1.2.16.1 snj refnumtoa(&peer->srcadr), (long long)tt));
509 1.1.1.2.16.1 snj up->bad++;
510 1.1.1.2.16.1 snj msyslog (LOG_ERR, "SHM: stale/bad receive time, delay=%llds",
511 1.1.1.2.16.1 snj (long long)tt);
512 1.1.1.2.16.1 snj return;
513 1.1 kardel }
514 1.1.1.2.16.1 snj
515 1.1.1.2.16.1 snj /* check 2: delta check */
516 1.1.1.2.16.1 snj tt = tvr.tv_sec - tvt.tv_sec - (tvr.tv_nsec < tvt.tv_nsec);
517 1.1.1.2.16.1 snj if (tt < 0)
518 1.1.1.2.16.1 snj tt = -tt;
519 1.1.1.2.16.1 snj if (up->max_delta > 0 && tt > up->max_delta) {
520 1.1.1.2.16.1 snj DPRINTF(1, ("%s: SHM diff limit exceeded, delta=%llds\n",
521 1.1.1.2.16.1 snj refnumtoa(&peer->srcadr), (long long)tt));
522 1.1 kardel up->bad++;
523 1.1.1.2.16.1 snj msyslog (LOG_ERR, "SHM: difference limit exceeded, delta=%llds\n",
524 1.1.1.2.16.1 snj (long long)tt);
525 1.1.1.2.16.1 snj return;
526 1.1 kardel }
527 1.1.1.2.16.1 snj
528 1.1.1.2.16.1 snj /* if we really made it to this point... we're winners! */
529 1.1.1.2.16.1 snj DPRINTF(2, ("%s: SHM feeding data\n",
530 1.1.1.2.16.1 snj refnumtoa(&peer->srcadr)));
531 1.1.1.2.16.1 snj tsrcv = tspec_stamp_to_lfp(tvr);
532 1.1.1.2.16.1 snj tsref = tspec_stamp_to_lfp(tvt);
533 1.1.1.2.16.1 snj pp->leap = shm->leap;
534 1.1.1.2.16.1 snj peer->precision = shm->precision;
535 1.1.1.2.16.1 snj refclock_process_offset(pp, tsref, tsrcv, pp->fudgetime1);
536 1.1 kardel up->good++;
537 1.1 kardel }
538 1.1 kardel
539 1.1 kardel /*
540 1.1 kardel * shm_clockstats - dump and reset counters
541 1.1 kardel */
542 1.1.1.2.16.1 snj static void shm_clockstats(
543 1.1 kardel int unit,
544 1.1 kardel struct peer *peer
545 1.1 kardel )
546 1.1 kardel {
547 1.1 kardel struct refclockproc *pp;
548 1.1 kardel struct shmunit *up;
549 1.1.1.2.16.1 snj char logbuf[64];
550 1.1.1.2.16.1 snj unsigned int llen;
551 1.1 kardel
552 1.1 kardel pp = peer->procptr;
553 1.1.1.2.16.1 snj up = pp->unitptr;
554 1.1 kardel
555 1.1.1.2.16.1 snj if (pp->sloppyclockflag & CLK_FLAG4) {
556 1.1.1.2.16.1 snj /* if snprintf() returns a negative values on errors
557 1.1.1.2.16.1 snj ** (some older ones do) make sure we are NUL
558 1.1.1.2.16.1 snj ** terminated. Using an unsigned result does the trick.
559 1.1.1.2.16.1 snj */
560 1.1.1.2.16.1 snj llen = snprintf(logbuf, sizeof(logbuf),
561 1.1.1.2.16.1 snj "%3d %3d %3d %3d %3d",
562 1.1.1.2.16.1 snj up->ticks, up->good, up->notready,
563 1.1.1.2.16.1 snj up->bad, up->clash);
564 1.1.1.2.16.1 snj logbuf[min(llen, sizeof(logbuf)-1)] = '\0';
565 1.1.1.2.16.1 snj record_clock_stats(&peer->srcadr, logbuf);
566 1.1.1.2.16.1 snj }
567 1.1.1.2.16.1 snj up->ticks = up->good = up->notready = up->bad = up->clash = 0;
568 1.1 kardel
569 1.1 kardel }
570 1.1 kardel
571 1.1 kardel #else
572 1.1.1.2.16.1 snj NONEMPTY_TRANSLATION_UNIT
573 1.1 kardel #endif /* REFCLOCK */
574