2 * refclock_shm - clock driver for utc via shared memory
3 * - under construction -
4 * To add new modes: Extend or union the shmTime-struct. Do not
5 * extend/shrink size, because otherwise existing implementations
6 * will specify wrong size of shared memory-segment
14 #include "ntp_types.h"
16 #if defined(REFCLOCK) && defined(CLOCK_SHM)
22 #include "ntp_refclock.h"
24 #include "timespecops.h"
26 #include "ntp_stdlib.h"
41 * This driver supports a reference clock attached thru shared memory
45 * SHM interface definitions
47 #define PRECISION (-1) /* precision assumed (0.5 s) */
48 #define REFID "SHM" /* reference ID */
49 #define DESCRIPTION "SHM/Shared memory interface"
51 #define NSAMPLES 3 /* stages of median filter */
56 #define SHM_MODE_PRIVATE 0x0001
61 static int shm_start (int unit, struct peer *peer);
62 static void shm_shutdown (int unit, struct peer *peer);
63 static void shm_poll (int unit, struct peer *peer);
64 static void shm_timer (int unit, struct peer *peer);
65 static void shm_clockstats (int unit, struct peer *peer);
66 static void shm_control (int unit, const struct refclockstat * in_st,
67 struct refclockstat * out_st, struct peer *peer);
72 struct refclock refclock_shm = {
73 shm_start, /* start up driver */
74 shm_shutdown, /* shut down driver */
75 shm_poll, /* transmit poll message */
76 shm_control, /* control settings */
77 noentry, /* not used: init */
78 noentry, /* not used: buginfo */
79 shm_timer, /* once per second */
83 int mode; /* 0 - if valid is set:
86 * 1 - if valid is set:
87 * if count before and after read of values is equal,
92 time_t clockTimeStampSec;
93 int clockTimeStampUSec;
94 time_t receiveTimeStampSec;
95 int receiveTimeStampUSec;
100 unsigned clockTimeStampNSec; /* Unsigned ns timestamps */
101 unsigned receiveTimeStampNSec; /* Unsigned ns timestamps */
106 struct shmTime *shm; /* pointer to shared memory segment */
107 int forall; /* access for all UIDs? */
109 /* debugging/monitoring counters - reset when printed */
110 int ticks; /* number of attempts to read data*/
111 int good; /* number of valid samples */
112 int notready; /* number of peeks without data ready */
113 int bad; /* number of invalid samples */
114 int clash; /* number of access clashes while reading */
116 time_t max_delta; /* difference limit */
117 time_t max_delay; /* age/stale limit */
120 static struct shmTime*
126 struct shmTime *p = NULL;
132 /* 0x4e545030 is NTP0.
133 * Big units will give non-ascii but that's OK
134 * as long as everybody does it the same way.
136 shmid=shmget(0x4e545030 + unit, sizeof (struct shmTime),
137 IPC_CREAT | (forall ? 0666 : 0600));
138 if (shmid == -1) { /* error */
139 msyslog(LOG_ERR, "SHM shmget (unit %d): %m", unit);
142 p = (struct shmTime *)shmat (shmid, 0, 0);
143 if (p == (struct shmTime *)-1) { /* error */
144 msyslog(LOG_ERR, "SHM shmat (unit %d): %m", unit);
151 static const char * nspref[2] = { "Local", "Global" };
153 LPSECURITY_ATTRIBUTES psec = 0;
155 SECURITY_DESCRIPTOR sd;
156 SECURITY_ATTRIBUTES sa;
159 numch = snprintf(buf, sizeof(buf), "%s\\NTP%d",
160 nspref[forall != 0], (unit & 0xFF));
161 if (numch >= sizeof(buf)) {
162 msyslog(LOG_ERR, "SHM name too long (unit %d)", unit);
165 if (forall) { /* world access */
166 if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) {
167 msyslog(LOG_ERR,"SHM InitializeSecurityDescriptor (unit %d): %m", unit);
170 if (!SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE)) {
171 msyslog(LOG_ERR, "SHM SetSecurityDescriptorDacl (unit %d): %m", unit);
174 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
175 sa.lpSecurityDescriptor = &sd;
176 sa.bInheritHandle = FALSE;
179 shmid = CreateFileMapping ((HANDLE)0xffffffff, psec, PAGE_READWRITE,
180 0, sizeof (struct shmTime), buf);
181 if (shmid == NULL) { /*error*/
183 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
184 0, GetLastError (), 0, buf, sizeof (buf), 0);
185 msyslog(LOG_ERR, "SHM CreateFileMapping (unit %d): %s", unit, buf);
188 p = (struct shmTime *)MapViewOfFile(shmid, FILE_MAP_WRITE, 0, 0,
189 sizeof (struct shmTime));
190 if (p == NULL) { /*error*/
192 FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
193 0, GetLastError (), 0, buf, sizeof (buf), 0);
194 msyslog(LOG_ERR,"SHM MapViewOfFile (unit %d): %s", unit, buf);
203 * shm_start - attach to shared memory
211 struct refclockproc * const pp = peer->procptr;
212 struct shmunit * const up = emalloc_zero(sizeof(*up));
214 pp->io.clock_recv = noentry;
215 pp->io.srcclock = peer;
219 up->forall = (unit >= 2) && !(peer->ttl & SHM_MODE_PRIVATE);
221 up->shm = getShmTime(unit, up->forall);
224 * Initialize miscellaneous peer variables
226 memcpy((char *)&pp->refid, REFID, 4);
229 up->shm->precision = PRECISION;
230 peer->precision = up->shm->precision;
232 up->shm->nsamples = NSAMPLES;
233 pp->clockdesc = DESCRIPTION;
234 /* items to be changed later in 'shm_control()': */
236 up->max_delta = 4*3600;
247 * shm_control - configure flag1/time2 params
249 * These are not yet available during 'shm_start', so we have to do any
250 * pre-computations we want to avoid during regular poll/timer callbacks
256 const struct refclockstat * in_st,
257 struct refclockstat * out_st,
261 struct refclockproc * const pp = peer->procptr;
262 struct shmunit * const up = pp->unitptr;
269 if (pp->sloppyclockflag & CLK_FLAG1)
271 else if (pp->fudgetime2 < 1. || pp->fudgetime2 > 86400.)
272 up->max_delta = 4*3600;
274 up->max_delta = (time_t)floor(pp->fudgetime2 + 0.5);
279 * shm_shutdown - shut down the clock
287 struct refclockproc * const pp = peer->procptr;
288 struct shmunit * const up = pp->unitptr;
295 /* HMS: shmdt() wants char* or const void * */
296 (void)shmdt((char *)up->shm);
300 UnmapViewOfFile(up->shm);
308 * shm_poll - called by the transmit procedure
316 struct refclockproc * const pp = peer->procptr;
317 struct shmunit * const up = pp->unitptr;
322 /* get dominant reason if we have no samples at all */
323 major_error = max(up->notready, up->bad);
324 major_error = max(major_error, up->clash);
327 * Process median filter samples. If none received, see what
328 * happened, tell the core and keep going.
330 if (pp->coderecv != pp->codeproc) {
331 /* have some samples, everything OK */
332 pp->lastref = pp->lastrec;
333 refclock_receive(peer);
334 } else if (NULL == up->shm) { /* is this possible at all? */
335 /* we're out of business without SHM access */
336 refclock_report(peer, CEVNT_FAULT);
337 } else if (major_error == up->clash) {
338 /* too many collisions is like a bad signal */
339 refclock_report(peer, CEVNT_PROP);
340 } else if (major_error == up->bad) {
341 /* too much stale/bad/garbled data */
342 refclock_report(peer, CEVNT_BADREPLY);
344 /* in any other case assume it's just a timeout */
345 refclock_report(peer, CEVNT_TIMEOUT);
347 /* shm_clockstats() clears the tallies, so it must be last... */
348 shm_clockstats(unit, peer);
352 * shm_timer - called onece every second.
354 * This tries to grab a sample from the SHM segment
362 struct refclockproc * const pp = peer->procptr;
363 struct shmunit * const up = pp->unitptr;
365 /* access order is important for lock-free SHM access; we
366 ** enforce order by treating the whole structure volatile.
368 ** IMPORTANT NOTE: This does not protect from reordering on CPU
369 ** level, and it does nothing for cache consistency and
370 ** visibility of changes by other cores. We need atomic and/or
371 ** fence instructions for that.
373 volatile struct shmTime *shm;
380 unsigned int cns_new, rns_new;
383 /* for formatting 'a_lastcode': */
389 * This is the main routine. It snatches the time from the shm
390 * board and tacks on a local timestamp.
393 if ((shm = up->shm) == NULL) {
394 /* try to map again - this may succeed if meanwhile some-
395 body has ipcrm'ed the old (unaccessible) shared mem segment */
396 shm = up->shm = getShmTime(unit, up->forall);
398 DPRINTF(1, ("%s: no SHM segment\n",
399 refnumtoa(&peer->srcadr)));
404 DPRINTF(1, ("%s: SHM not ready\n",
405 refnumtoa(&peer->srcadr)));
412 tvr.tv_sec = shm->receiveTimeStampSec;
413 tvr.tv_nsec = shm->receiveTimeStampUSec * 1000;
414 rns_new = shm->receiveTimeStampNSec;
415 tvt.tv_sec = shm->clockTimeStampSec;
416 tvt.tv_nsec = shm->clockTimeStampUSec * 1000;
417 cns_new = shm->clockTimeStampNSec;
419 /* Since the following comparisons are between unsigned
420 ** variables they are always well defined, and any
421 ** (signed) underflow will turn into very large unsigned
422 ** values, well above the 1000 cutoff.
424 ** Note: The usecs *must* be a *truncated*
425 ** representation of the nsecs. This code will fail for
426 ** *rounded* usecs, and the logic to deal with
427 ** wrap-arounds in the presence of rounded values is
428 ** much more convoluted.
430 if ( ((cns_new - (unsigned)tvt.tv_nsec) < 1000)
431 && ((rns_new - (unsigned)tvr.tv_nsec) < 1000)) {
432 tvt.tv_nsec = cns_new;
433 tvr.tv_nsec = rns_new;
435 /* At this point tvr and tvt contains valid ns-level
436 ** timestamps, possibly generated by extending the old
437 ** us-level timestamps
439 DPRINTF(2, ("%s: SHM type 0 sample\n",
440 refnumtoa(&peer->srcadr)));
446 tvr.tv_sec = shm->receiveTimeStampSec;
447 tvr.tv_nsec = shm->receiveTimeStampUSec * 1000;
448 rns_new = shm->receiveTimeStampNSec;
449 tvt.tv_sec = shm->clockTimeStampSec;
450 tvt.tv_nsec = shm->clockTimeStampUSec * 1000;
451 cns_new = shm->clockTimeStampNSec;
452 if (cnt != shm->count) {
453 DPRINTF(1, ("%s: type 1 access clash\n",
454 refnumtoa(&peer->srcadr)));
455 msyslog (LOG_NOTICE, "SHM: access clash in shared memory");
460 /* See the case above for an explanation of the
463 if ( ((cns_new - (unsigned)tvt.tv_nsec) < 1000)
464 && ((rns_new - (unsigned)tvr.tv_nsec) < 1000)) {
465 tvt.tv_nsec = cns_new;
466 tvr.tv_nsec = rns_new;
468 /* At this point tvr and tvt contains valid ns-level
469 ** timestamps, possibly generated by extending the old
470 ** us-level timestamps
472 DPRINTF(2, ("%s: SHM type 1 sample\n",
473 refnumtoa(&peer->srcadr)));
477 DPRINTF(1, ("%s: SHM type blooper, mode=%d\n",
478 refnumtoa(&peer->srcadr), shm->mode));
480 msyslog (LOG_ERR, "SHM: bad mode found in shared memory: %d",
486 /* format the last time code in human-readable form into
487 * 'pp->a_lastcode'. Someone claimed: "NetBSD has incompatible
488 * tv_sec". I can't find a base for this claim, but we can work
489 * around that potential problem. BTW, simply casting a pointer
490 * is a receipe for disaster on some architectures.
492 tt = (time_t)tvt.tv_sec;
493 ts = time_to_vint64(&tt);
494 ntpcal_time_to_date(&cd, &ts);
496 /* add ntpq -c cv timecode in ISO 8601 format */
497 c = snprintf(pp->a_lastcode, sizeof(pp->a_lastcode),
498 "%04u-%02u-%02uT%02u:%02u:%02u.%09ldZ",
499 cd.year, cd.month, cd.monthday,
500 cd.hour, cd.minute, cd.second,
502 pp->lencode = (c < sizeof(pp->a_lastcode)) ? c : 0;
504 /* check 1: age control of local time stamp */
506 tt = now - tvr.tv_sec;
507 if (tt < 0 || tt > up->max_delay) {
508 DPRINTF(1, ("%s:SHM stale/bad receive time, delay=%llds\n",
509 refnumtoa(&peer->srcadr), (long long)tt));
511 msyslog (LOG_ERR, "SHM: stale/bad receive time, delay=%llds",
516 /* check 2: delta check */
517 tt = tvr.tv_sec - tvt.tv_sec - (tvr.tv_nsec < tvt.tv_nsec);
520 if (up->max_delta > 0 && tt > up->max_delta) {
521 DPRINTF(1, ("%s: SHM diff limit exceeded, delta=%llds\n",
522 refnumtoa(&peer->srcadr), (long long)tt));
524 msyslog (LOG_ERR, "SHM: difference limit exceeded, delta=%llds\n",
529 /* if we really made it to this point... we're winners! */
530 DPRINTF(2, ("%s: SHM feeding data\n",
531 refnumtoa(&peer->srcadr)));
532 tsrcv = tspec_stamp_to_lfp(tvr);
533 tsref = tspec_stamp_to_lfp(tvt);
534 pp->leap = shm->leap;
535 peer->precision = shm->precision;
536 refclock_process_offset(pp, tsref, tsrcv, pp->fudgetime1);
541 * shm_clockstats - dump and reset counters
543 static void shm_clockstats(
548 struct refclockproc * const pp = peer->procptr;
549 struct shmunit * const up = pp->unitptr;
552 if (pp->sloppyclockflag & CLK_FLAG4) {
554 &peer->srcadr, "%3d %3d %3d %3d %3d",
555 up->ticks, up->good, up->notready,
558 up->ticks = up->good = up->notready = up->bad = up->clash = 0;
562 NONEMPTY_TRANSLATION_UNIT
563 #endif /* REFCLOCK */