xref: /freebsd/contrib/ntp/ntpd/refclock_shm.c (revision 7847e04111f2c2b06b36f6d19a46d78814d7836d)
1c0b746e5SOllivier Robert /*
2c0b746e5SOllivier Robert  * refclock_shm - clock driver for utc via shared memory
3c0b746e5SOllivier Robert  * - under construction -
4c0b746e5SOllivier Robert  * To add new modes: Extend or union the shmTime-struct. Do not
5c0b746e5SOllivier Robert  * extend/shrink size, because otherwise existing implementations
6c0b746e5SOllivier Robert  * will specify wrong size of shared memory-segment
7c0b746e5SOllivier Robert  * PB 18.3.97
8c0b746e5SOllivier Robert  */
9c0b746e5SOllivier Robert 
10c0b746e5SOllivier Robert #ifdef HAVE_CONFIG_H
11c0b746e5SOllivier Robert # include <config.h>
12c0b746e5SOllivier Robert #endif
13c0b746e5SOllivier Robert 
142b15cb3dSCy Schubert #include "ntp_types.h"
152b15cb3dSCy Schubert 
16c0b746e5SOllivier Robert #if defined(REFCLOCK) && defined(CLOCK_SHM)
17c0b746e5SOllivier Robert 
18c0b746e5SOllivier Robert #include "ntpd.h"
19c0b746e5SOllivier Robert #undef fileno
20c0b746e5SOllivier Robert #include "ntp_io.h"
21c0b746e5SOllivier Robert #undef fileno
22c0b746e5SOllivier Robert #include "ntp_refclock.h"
23c0b746e5SOllivier Robert #undef fileno
242b15cb3dSCy Schubert #include "timespecops.h"
25c0b746e5SOllivier Robert #undef fileno
26c0b746e5SOllivier Robert #include "ntp_stdlib.h"
27276da39aSCy Schubert #include "ntp_assert.h"
28c0b746e5SOllivier Robert 
29224ba2bdSOllivier Robert #undef fileno
30224ba2bdSOllivier Robert #include <ctype.h>
31224ba2bdSOllivier Robert #undef fileno
32224ba2bdSOllivier Robert 
33c0b746e5SOllivier Robert #ifndef SYS_WINNT
34c0b746e5SOllivier Robert # include <sys/ipc.h>
35c0b746e5SOllivier Robert # include <sys/shm.h>
36c0b746e5SOllivier Robert # include <assert.h>
37c0b746e5SOllivier Robert # include <unistd.h>
38c0b746e5SOllivier Robert # include <stdio.h>
39c0b746e5SOllivier Robert #endif
40c0b746e5SOllivier Robert 
41276da39aSCy Schubert #ifdef HAVE_STDATOMIC_H
42276da39aSCy Schubert # include <stdatomic.h>
43276da39aSCy Schubert #endif /* HAVE_STDATOMIC_H */
44276da39aSCy Schubert 
45c0b746e5SOllivier Robert /*
46c0b746e5SOllivier Robert  * This driver supports a reference clock attached thru shared memory
47c0b746e5SOllivier Robert  */
48c0b746e5SOllivier Robert 
49c0b746e5SOllivier Robert /*
50c0b746e5SOllivier Robert  * SHM interface definitions
51c0b746e5SOllivier Robert  */
52c0b746e5SOllivier Robert #define PRECISION       (-1)    /* precision assumed (0.5 s) */
53c0b746e5SOllivier Robert #define REFID           "SHM"   /* reference ID */
54c0b746e5SOllivier Robert #define DESCRIPTION     "SHM/Shared memory interface"
55c0b746e5SOllivier Robert 
56c0b746e5SOllivier Robert #define NSAMPLES        3       /* stages of median filter */
57c0b746e5SOllivier Robert 
58c0b746e5SOllivier Robert /*
592b15cb3dSCy Schubert  * Mode flags
602b15cb3dSCy Schubert  */
612b15cb3dSCy Schubert #define SHM_MODE_PRIVATE 0x0001
622b15cb3dSCy Schubert 
632b15cb3dSCy Schubert /*
64c0b746e5SOllivier Robert  * Function prototypes
65c0b746e5SOllivier Robert  */
662b15cb3dSCy Schubert static  int     shm_start       (int unit, struct peer *peer);
672b15cb3dSCy Schubert static  void    shm_shutdown    (int unit, struct peer *peer);
682b15cb3dSCy Schubert static  void    shm_poll        (int unit, struct peer *peer);
692b15cb3dSCy Schubert static  void    shm_timer       (int unit, struct peer *peer);
702b15cb3dSCy Schubert static	void	shm_clockstats  (int unit, struct peer *peer);
712b15cb3dSCy Schubert static	void	shm_control	(int unit, const struct refclockstat * in_st,
722b15cb3dSCy Schubert 				 struct refclockstat * out_st, struct peer *peer);
73c0b746e5SOllivier Robert 
74c0b746e5SOllivier Robert /*
75c0b746e5SOllivier Robert  * Transfer vector
76c0b746e5SOllivier Robert  */
77c0b746e5SOllivier Robert struct  refclock refclock_shm = {
78c0b746e5SOllivier Robert 	shm_start,              /* start up driver */
79c0b746e5SOllivier Robert 	shm_shutdown,           /* shut down driver */
80c0b746e5SOllivier Robert 	shm_poll,		/* transmit poll message */
812b15cb3dSCy Schubert 	shm_control,		/* control settings */
822b15cb3dSCy Schubert 	noentry,		/* not used: init */
832b15cb3dSCy Schubert 	noentry,		/* not used: buginfo */
842b15cb3dSCy Schubert 	shm_timer,              /* once per second */
85c0b746e5SOllivier Robert };
862b15cb3dSCy Schubert 
87c0b746e5SOllivier Robert struct shmTime {
882b15cb3dSCy Schubert 	int    mode; /* 0 - if valid is set:
89c0b746e5SOllivier Robert 		      *       use values,
90c0b746e5SOllivier Robert 		      *       clear valid
912b15cb3dSCy Schubert 		      * 1 - if valid is set:
92c0b746e5SOllivier Robert 		      *       if count before and after read of values is equal,
93c0b746e5SOllivier Robert 		      *         use values
94c0b746e5SOllivier Robert 		      *       clear valid
95c0b746e5SOllivier Robert 		      */
962b15cb3dSCy Schubert 	volatile int    count;
97c0b746e5SOllivier Robert 	time_t		clockTimeStampSec;
98c0b746e5SOllivier Robert 	int		clockTimeStampUSec;
99c0b746e5SOllivier Robert 	time_t		receiveTimeStampSec;
100c0b746e5SOllivier Robert 	int		receiveTimeStampUSec;
101c0b746e5SOllivier Robert 	int		leap;
102c0b746e5SOllivier Robert 	int		precision;
103c0b746e5SOllivier Robert 	int		nsamples;
1042b15cb3dSCy Schubert 	volatile int    valid;
1052b15cb3dSCy Schubert 	unsigned	clockTimeStampNSec;	/* Unsigned ns timestamps */
1062b15cb3dSCy Schubert 	unsigned	receiveTimeStampNSec;	/* Unsigned ns timestamps */
1072b15cb3dSCy Schubert 	int		dummy[8];
108c0b746e5SOllivier Robert };
1099c2daa00SOllivier Robert 
1102b15cb3dSCy Schubert struct shmunit {
1112b15cb3dSCy Schubert 	struct shmTime *shm;	/* pointer to shared memory segment */
1122b15cb3dSCy Schubert 	int forall;		/* access for all UIDs?	*/
1139c2daa00SOllivier Robert 
1142b15cb3dSCy Schubert 	/* debugging/monitoring counters - reset when printed */
1152b15cb3dSCy Schubert 	int ticks;		/* number of attempts to read data*/
1162b15cb3dSCy Schubert 	int good;		/* number of valid samples */
1172b15cb3dSCy Schubert 	int notready;		/* number of peeks without data ready */
1182b15cb3dSCy Schubert 	int bad;		/* number of invalid samples */
1192b15cb3dSCy Schubert 	int clash;		/* number of access clashes while reading */
1202b15cb3dSCy Schubert 
1212b15cb3dSCy Schubert 	time_t max_delta;	/* difference limit */
1222b15cb3dSCy Schubert 	time_t max_delay;	/* age/stale limit */
1232b15cb3dSCy Schubert };
1242b15cb3dSCy Schubert 
125276da39aSCy Schubert 
1262b15cb3dSCy Schubert static struct shmTime*
getShmTime(int unit,int forall)1272b15cb3dSCy Schubert getShmTime(
1282b15cb3dSCy Schubert 	int unit,
1292b15cb3dSCy Schubert 	int/*BOOL*/ forall
1302b15cb3dSCy Schubert 	)
1312b15cb3dSCy Schubert {
1322b15cb3dSCy Schubert 	struct shmTime *p = NULL;
1332b15cb3dSCy Schubert 
134c0b746e5SOllivier Robert #ifndef SYS_WINNT
135c0b746e5SOllivier Robert 
1362b15cb3dSCy Schubert 	int shmid;
1372b15cb3dSCy Schubert 
1382b15cb3dSCy Schubert 	/* 0x4e545030 is NTP0.
1392b15cb3dSCy Schubert 	 * Big units will give non-ascii but that's OK
1402b15cb3dSCy Schubert 	 * as long as everybody does it the same way.
1412b15cb3dSCy Schubert 	 */
142c0b746e5SOllivier Robert 	shmid=shmget(0x4e545030 + unit, sizeof (struct shmTime),
1432b15cb3dSCy Schubert 		      IPC_CREAT | (forall ? 0666 : 0600));
144c0b746e5SOllivier Robert 	if (shmid == -1) { /* error */
1452b15cb3dSCy Schubert 		msyslog(LOG_ERR, "SHM shmget (unit %d): %m", unit);
1462b15cb3dSCy Schubert 		return NULL;
147c0b746e5SOllivier Robert 	}
1482b15cb3dSCy Schubert 	p = (struct shmTime *)shmat (shmid, 0, 0);
1492b15cb3dSCy Schubert 	if (p == (struct shmTime *)-1) { /* error */
1502b15cb3dSCy Schubert 		msyslog(LOG_ERR, "SHM shmat (unit %d): %m", unit);
1512b15cb3dSCy Schubert 		return NULL;
152c0b746e5SOllivier Robert 	}
1532b15cb3dSCy Schubert 
154276da39aSCy Schubert 	return p;
155c0b746e5SOllivier Robert #else
1562b15cb3dSCy Schubert 
1572b15cb3dSCy Schubert 	static const char * nspref[2] = { "Local", "Global" };
1582b15cb3dSCy Schubert 	char buf[20];
159c0b746e5SOllivier Robert 	LPSECURITY_ATTRIBUTES psec = 0;
160c0b746e5SOllivier Robert 	HANDLE shmid = 0;
161c0b746e5SOllivier Robert 	SECURITY_DESCRIPTOR sd;
162c0b746e5SOllivier Robert 	SECURITY_ATTRIBUTES sa;
1632b15cb3dSCy Schubert 	unsigned int numch;
1642b15cb3dSCy Schubert 
1652b15cb3dSCy Schubert 	numch = snprintf(buf, sizeof(buf), "%s\\NTP%d",
1662b15cb3dSCy Schubert 			 nspref[forall != 0], (unit & 0xFF));
1672b15cb3dSCy Schubert 	if (numch >= sizeof(buf)) {
1682b15cb3dSCy Schubert 		msyslog(LOG_ERR, "SHM name too long (unit %d)", unit);
1692b15cb3dSCy Schubert 		return NULL;
1702b15cb3dSCy Schubert 	}
1712b15cb3dSCy Schubert 	if (forall) { /* world access */
172c0b746e5SOllivier Robert 		if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION)) {
173c0b746e5SOllivier Robert 			msyslog(LOG_ERR,"SHM InitializeSecurityDescriptor (unit %d): %m", unit);
1742b15cb3dSCy Schubert 			return NULL;
175c0b746e5SOllivier Robert 		}
1762b15cb3dSCy Schubert 		if (!SetSecurityDescriptorDacl(&sd, TRUE, NULL, FALSE)) {
177c0b746e5SOllivier Robert 			msyslog(LOG_ERR, "SHM SetSecurityDescriptorDacl (unit %d): %m", unit);
1782b15cb3dSCy Schubert 			return NULL;
179c0b746e5SOllivier Robert 		}
180c0b746e5SOllivier Robert 		sa.nLength = sizeof(SECURITY_ATTRIBUTES);
181c0b746e5SOllivier Robert 		sa.lpSecurityDescriptor = &sd;
1822b15cb3dSCy Schubert 		sa.bInheritHandle = FALSE;
183c0b746e5SOllivier Robert 		psec = &sa;
184c0b746e5SOllivier Robert 	}
185c0b746e5SOllivier Robert 	shmid = CreateFileMapping ((HANDLE)0xffffffff, psec, PAGE_READWRITE,
186c0b746e5SOllivier Robert 				   0, sizeof (struct shmTime), buf);
1872b15cb3dSCy Schubert 	if (shmid == NULL) { /*error*/
188c0b746e5SOllivier Robert 		char buf[1000];
189c0b746e5SOllivier Robert 		FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
190c0b746e5SOllivier Robert 			       0, GetLastError (), 0, buf, sizeof (buf), 0);
191c0b746e5SOllivier Robert 		msyslog(LOG_ERR, "SHM CreateFileMapping (unit %d): %s", unit, buf);
1922b15cb3dSCy Schubert 		return NULL;
193c0b746e5SOllivier Robert 	}
1942b15cb3dSCy Schubert 	p = (struct shmTime *)MapViewOfFile(shmid, FILE_MAP_WRITE, 0, 0,
1952b15cb3dSCy Schubert 					    sizeof (struct shmTime));
1962b15cb3dSCy Schubert 	if (p == NULL) { /*error*/
197c0b746e5SOllivier Robert 		char buf[1000];
198c0b746e5SOllivier Robert 		FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM,
199c0b746e5SOllivier Robert 			       0, GetLastError (), 0, buf, sizeof (buf), 0);
200c0b746e5SOllivier Robert 		msyslog(LOG_ERR,"SHM MapViewOfFile (unit %d): %s", unit, buf);
2012b15cb3dSCy Schubert 		return NULL;
202c0b746e5SOllivier Robert 	}
2032b15cb3dSCy Schubert 
204276da39aSCy Schubert 	return p;
205c0b746e5SOllivier Robert #endif
2062b15cb3dSCy Schubert 
207276da39aSCy Schubert 	/* NOTREACHED */
208276da39aSCy Schubert 	ENSURE(!"getShmTime(): Not reached.");
209c0b746e5SOllivier Robert }
210276da39aSCy Schubert 
211276da39aSCy Schubert 
212c0b746e5SOllivier Robert /*
213c0b746e5SOllivier Robert  * shm_start - attach to shared memory
214c0b746e5SOllivier Robert  */
215c0b746e5SOllivier Robert static int
shm_start(int unit,struct peer * peer)216c0b746e5SOllivier Robert shm_start(
217c0b746e5SOllivier Robert 	int unit,
218c0b746e5SOllivier Robert 	struct peer *peer
219c0b746e5SOllivier Robert 	)
220c0b746e5SOllivier Robert {
2212b15cb3dSCy Schubert 	struct refclockproc * const pp = peer->procptr;
2222b15cb3dSCy Schubert 	struct shmunit *      const up = emalloc_zero(sizeof(*up));
2232b15cb3dSCy Schubert 
224c0b746e5SOllivier Robert 	pp->io.clock_recv = noentry;
2252b15cb3dSCy Schubert 	pp->io.srcclock = peer;
226c0b746e5SOllivier Robert 	pp->io.datalen = 0;
227c0b746e5SOllivier Robert 	pp->io.fd = -1;
2282b15cb3dSCy Schubert 
2292b15cb3dSCy Schubert 	up->forall = (unit >= 2) && !(peer->ttl & SHM_MODE_PRIVATE);
2302b15cb3dSCy Schubert 
2312b15cb3dSCy Schubert 	up->shm = getShmTime(unit, up->forall);
232c0b746e5SOllivier Robert 
233c0b746e5SOllivier Robert 	/*
234c0b746e5SOllivier Robert 	 * Initialize miscellaneous peer variables
235c0b746e5SOllivier Robert 	 */
236c0b746e5SOllivier Robert 	memcpy((char *)&pp->refid, REFID, 4);
2372b15cb3dSCy Schubert 	if (up->shm != 0) {
2382b15cb3dSCy Schubert 		pp->unitptr = up;
2392b15cb3dSCy Schubert 		up->shm->precision = PRECISION;
2402b15cb3dSCy Schubert 		peer->precision = up->shm->precision;
2412b15cb3dSCy Schubert 		up->shm->valid = 0;
2422b15cb3dSCy Schubert 		up->shm->nsamples = NSAMPLES;
243c0b746e5SOllivier Robert 		pp->clockdesc = DESCRIPTION;
2442b15cb3dSCy Schubert 		/* items to be changed later in 'shm_control()': */
2452b15cb3dSCy Schubert 		up->max_delay = 5;
2462b15cb3dSCy Schubert 		up->max_delta = 4*3600;
2472b15cb3dSCy Schubert 		return 1;
2482b15cb3dSCy Schubert 	} else {
2492b15cb3dSCy Schubert 		free(up);
2502b15cb3dSCy Schubert 		pp->unitptr = NULL;
251c0b746e5SOllivier Robert 		return 0;
252c0b746e5SOllivier Robert 	}
253c0b746e5SOllivier Robert }
254c0b746e5SOllivier Robert 
255c0b746e5SOllivier Robert 
256c0b746e5SOllivier Robert /*
2572b15cb3dSCy Schubert  * shm_control - configure flag1/time2 params
2582b15cb3dSCy Schubert  *
2592b15cb3dSCy Schubert  * These are not yet available during 'shm_start', so we have to do any
2602b15cb3dSCy Schubert  * pre-computations we want to avoid during regular poll/timer callbacks
2612b15cb3dSCy Schubert  * in this callback.
2622b15cb3dSCy Schubert  */
2632b15cb3dSCy Schubert static void
shm_control(int unit,const struct refclockstat * in_st,struct refclockstat * out_st,struct peer * peer)2642b15cb3dSCy Schubert shm_control(
2652b15cb3dSCy Schubert 	int                         unit,
2662b15cb3dSCy Schubert 	const struct refclockstat * in_st,
2672b15cb3dSCy Schubert 	struct refclockstat       * out_st,
2682b15cb3dSCy Schubert 	struct peer               * peer
2692b15cb3dSCy Schubert 	)
2702b15cb3dSCy Schubert {
2712b15cb3dSCy Schubert 	struct refclockproc * const pp = peer->procptr;
2722b15cb3dSCy Schubert 	struct shmunit *      const up = pp->unitptr;
2732b15cb3dSCy Schubert 
2742b15cb3dSCy Schubert 	UNUSED_ARG(unit);
2752b15cb3dSCy Schubert 	UNUSED_ARG(in_st);
2762b15cb3dSCy Schubert 	UNUSED_ARG(out_st);
2772b15cb3dSCy Schubert 	if (NULL == up)
2782b15cb3dSCy Schubert 		return;
2792b15cb3dSCy Schubert 	if (pp->sloppyclockflag & CLK_FLAG1)
2802b15cb3dSCy Schubert 		up->max_delta = 0;
2812b15cb3dSCy Schubert 	else if (pp->fudgetime2 < 1. || pp->fudgetime2 > 86400.)
2822b15cb3dSCy Schubert 		up->max_delta = 4*3600;
2832b15cb3dSCy Schubert 	else
2842b15cb3dSCy Schubert 		up->max_delta = (time_t)floor(pp->fudgetime2 + 0.5);
2852b15cb3dSCy Schubert }
2862b15cb3dSCy Schubert 
2872b15cb3dSCy Schubert 
2882b15cb3dSCy Schubert /*
289c0b746e5SOllivier Robert  * shm_shutdown - shut down the clock
290c0b746e5SOllivier Robert  */
291c0b746e5SOllivier Robert static void
shm_shutdown(int unit,struct peer * peer)292c0b746e5SOllivier Robert shm_shutdown(
293c0b746e5SOllivier Robert 	int unit,
294c0b746e5SOllivier Robert 	struct peer *peer
295c0b746e5SOllivier Robert 	)
296c0b746e5SOllivier Robert {
2972b15cb3dSCy Schubert 	struct refclockproc * const pp = peer->procptr;
2982b15cb3dSCy Schubert 	struct shmunit *      const up = pp->unitptr;
299c0b746e5SOllivier Robert 
3002b15cb3dSCy Schubert 	UNUSED_ARG(unit);
3012b15cb3dSCy Schubert 	if (NULL == up)
3022b15cb3dSCy Schubert 		return;
303c0b746e5SOllivier Robert #ifndef SYS_WINNT
3042b15cb3dSCy Schubert 
3059c2daa00SOllivier Robert 	/* HMS: shmdt() wants char* or const void * */
3062b15cb3dSCy Schubert 	(void)shmdt((char *)up->shm);
3072b15cb3dSCy Schubert 
308c0b746e5SOllivier Robert #else
3092b15cb3dSCy Schubert 
3102b15cb3dSCy Schubert 	UnmapViewOfFile(up->shm);
3112b15cb3dSCy Schubert 
312c0b746e5SOllivier Robert #endif
3132b15cb3dSCy Schubert 	free(up);
314c0b746e5SOllivier Robert }
315c0b746e5SOllivier Robert 
316c0b746e5SOllivier Robert 
317c0b746e5SOllivier Robert /*
318c0b746e5SOllivier Robert  * shm_poll - called by the transmit procedure
319c0b746e5SOllivier Robert  */
320c0b746e5SOllivier Robert static void
shm_poll(int unit,struct peer * peer)321c0b746e5SOllivier Robert shm_poll(
322c0b746e5SOllivier Robert 	int unit,
323c0b746e5SOllivier Robert 	struct peer *peer
324c0b746e5SOllivier Robert 	)
325c0b746e5SOllivier Robert {
3262b15cb3dSCy Schubert 	struct refclockproc * const pp = peer->procptr;
3272b15cb3dSCy Schubert 	struct shmunit *      const up = pp->unitptr;
3282b15cb3dSCy Schubert 	int major_error;
3292b15cb3dSCy Schubert 
3302b15cb3dSCy Schubert 	pp->polls++;
3312b15cb3dSCy Schubert 
3322b15cb3dSCy Schubert 	/* get dominant reason if we have no samples at all */
3332b15cb3dSCy Schubert 	major_error = max(up->notready, up->bad);
3342b15cb3dSCy Schubert 	major_error = max(major_error, up->clash);
3352b15cb3dSCy Schubert 
3362b15cb3dSCy Schubert         /*
3372b15cb3dSCy Schubert          * Process median filter samples. If none received, see what
3382b15cb3dSCy Schubert          * happened, tell the core and keep going.
3392b15cb3dSCy Schubert          */
3402b15cb3dSCy Schubert         if (pp->coderecv != pp->codeproc) {
3412b15cb3dSCy Schubert 		/* have some samples, everything OK */
3422b15cb3dSCy Schubert 		pp->lastref = pp->lastrec;
343*4e1ef62aSXin LI 		refclock_report(peer, CEVNT_NOMINAL);
3442b15cb3dSCy Schubert 		refclock_receive(peer);
3452b15cb3dSCy Schubert 	} else if (NULL == up->shm) { /* is this possible at all? */
3462b15cb3dSCy Schubert 		/* we're out of business without SHM access */
3472b15cb3dSCy Schubert 		refclock_report(peer, CEVNT_FAULT);
3482b15cb3dSCy Schubert 	} else if (major_error == up->clash) {
3492b15cb3dSCy Schubert 		/* too many collisions is like a bad signal */
3502b15cb3dSCy Schubert                 refclock_report(peer, CEVNT_PROP);
3512b15cb3dSCy Schubert 	} else if (major_error == up->bad) {
3522b15cb3dSCy Schubert 		/* too much stale/bad/garbled data */
3532b15cb3dSCy Schubert                 refclock_report(peer, CEVNT_BADREPLY);
3542b15cb3dSCy Schubert 	} else {
3552b15cb3dSCy Schubert 		/* in any other case assume it's just a timeout */
3562b15cb3dSCy Schubert                 refclock_report(peer, CEVNT_TIMEOUT);
3572b15cb3dSCy Schubert         }
3582b15cb3dSCy Schubert 	/* shm_clockstats() clears the tallies, so it must be last... */
3592b15cb3dSCy Schubert 	shm_clockstats(unit, peer);
3602b15cb3dSCy Schubert }
3612b15cb3dSCy Schubert 
362276da39aSCy Schubert 
363276da39aSCy Schubert enum segstat_t {
364276da39aSCy Schubert     OK, NO_SEGMENT, NOT_READY, BAD_MODE, CLASH
365276da39aSCy Schubert };
366276da39aSCy Schubert 
367276da39aSCy Schubert struct shm_stat_t {
368276da39aSCy Schubert     int status;
369276da39aSCy Schubert     int mode;
370276da39aSCy Schubert     struct timespec tvc, tvr, tvt;
371276da39aSCy Schubert     int precision;
372276da39aSCy Schubert     int leap;
373276da39aSCy Schubert };
374276da39aSCy Schubert 
memory_barrier(void)375276da39aSCy Schubert static inline void memory_barrier(void)
3762b15cb3dSCy Schubert {
377276da39aSCy Schubert #ifdef HAVE_ATOMIC_THREAD_FENCE
378276da39aSCy Schubert     atomic_thread_fence(memory_order_seq_cst);
379276da39aSCy Schubert #endif /* HAVE_ATOMIC_THREAD_FENCE */
380276da39aSCy Schubert }
3812b15cb3dSCy Schubert 
shm_query(volatile struct shmTime * shm_in,struct shm_stat_t * shm_stat)382276da39aSCy Schubert static enum segstat_t shm_query(volatile struct shmTime *shm_in, struct shm_stat_t *shm_stat)
383276da39aSCy Schubert /* try to grab a sample from the specified SHM segment */
384276da39aSCy Schubert {
3853311ff84SXin LI     struct shmTime shmcopy;
3863311ff84SXin LI     volatile struct shmTime *shm = shm_in;
387276da39aSCy Schubert     volatile int cnt;
3882b15cb3dSCy Schubert 
3892b15cb3dSCy Schubert     unsigned int cns_new, rns_new;
390c0b746e5SOllivier Robert 
391c0b746e5SOllivier Robert     /*
392c0b746e5SOllivier Robert      * This is the main routine. It snatches the time from the shm
393c0b746e5SOllivier Robert      * board and tacks on a local timestamp.
394c0b746e5SOllivier Robert      */
3952b15cb3dSCy Schubert     if (shm == NULL) {
396276da39aSCy Schubert 	shm_stat->status = NO_SEGMENT;
397276da39aSCy Schubert 	return NO_SEGMENT;
398c0b746e5SOllivier Robert     }
399ea906c41SOllivier Robert 
400276da39aSCy Schubert     /*@-type@*//* splint is confused about struct timespec */
401276da39aSCy Schubert     shm_stat->tvc.tv_sec = shm_stat->tvc.tv_nsec = 0;
402276da39aSCy Schubert     {
403276da39aSCy Schubert 	time_t now;
404276da39aSCy Schubert 
405276da39aSCy Schubert 	time(&now);
406276da39aSCy Schubert 	shm_stat->tvc.tv_sec = now;
407276da39aSCy Schubert     }
408276da39aSCy Schubert 
409276da39aSCy Schubert     /* relying on word access to be atomic here */
410276da39aSCy Schubert     if (shm->valid == 0) {
411276da39aSCy Schubert 	shm_stat->status = NOT_READY;
412276da39aSCy Schubert 	return NOT_READY;
413276da39aSCy Schubert     }
414276da39aSCy Schubert 
415276da39aSCy Schubert     cnt = shm->count;
416276da39aSCy Schubert 
417276da39aSCy Schubert     /*
418276da39aSCy Schubert      * This is proof against concurrency issues if either
419276da39aSCy Schubert      * (a) the memory_barrier() call works on this host, or
420276da39aSCy Schubert      * (b) memset compiles to an uninterruptible single-instruction bitblt.
421276da39aSCy Schubert      */
422276da39aSCy Schubert     memory_barrier();
4233311ff84SXin LI     memcpy(&shmcopy, (void*)(uintptr_t)shm, sizeof(struct shmTime));
424276da39aSCy Schubert     shm->valid = 0;
425276da39aSCy Schubert     memory_barrier();
426276da39aSCy Schubert 
427276da39aSCy Schubert     /*
428276da39aSCy Schubert      * Clash detection in case neither (a) nor (b) was true.
429276da39aSCy Schubert      * Not supported in mode 0, and word access to the count field
430276da39aSCy Schubert      * must be atomic for this to work.
431276da39aSCy Schubert      */
432276da39aSCy Schubert     if (shmcopy.mode > 0 && cnt != shm->count) {
433276da39aSCy Schubert 	shm_stat->status = CLASH;
434276da39aSCy Schubert 	return shm_stat->status;
435276da39aSCy Schubert     }
436276da39aSCy Schubert 
437276da39aSCy Schubert     shm_stat->status = OK;
438276da39aSCy Schubert     shm_stat->mode = shmcopy.mode;
439276da39aSCy Schubert 
440276da39aSCy Schubert     switch (shmcopy.mode) {
4412b15cb3dSCy Schubert     case 0:
442276da39aSCy Schubert 	shm_stat->tvr.tv_sec	= shmcopy.receiveTimeStampSec;
443276da39aSCy Schubert 	shm_stat->tvr.tv_nsec	= shmcopy.receiveTimeStampUSec * 1000;
444276da39aSCy Schubert 	rns_new		= shmcopy.receiveTimeStampNSec;
445276da39aSCy Schubert 	shm_stat->tvt.tv_sec	= shmcopy.clockTimeStampSec;
446276da39aSCy Schubert 	shm_stat->tvt.tv_nsec	= shmcopy.clockTimeStampUSec * 1000;
447276da39aSCy Schubert 	cns_new		= shmcopy.clockTimeStampNSec;
4482b15cb3dSCy Schubert 
4492b15cb3dSCy Schubert 	/* Since the following comparisons are between unsigned
4502b15cb3dSCy Schubert 	** variables they are always well defined, and any
4512b15cb3dSCy Schubert 	** (signed) underflow will turn into very large unsigned
4522b15cb3dSCy Schubert 	** values, well above the 1000 cutoff.
4532b15cb3dSCy Schubert 	**
4542b15cb3dSCy Schubert 	** Note: The usecs *must* be a *truncated*
4552b15cb3dSCy Schubert 	** representation of the nsecs. This code will fail for
4562b15cb3dSCy Schubert 	** *rounded* usecs, and the logic to deal with
4572b15cb3dSCy Schubert 	** wrap-arounds in the presence of rounded values is
4582b15cb3dSCy Schubert 	** much more convoluted.
4599c2daa00SOllivier Robert 	*/
460276da39aSCy Schubert 	if (   ((cns_new - (unsigned)shm_stat->tvt.tv_nsec) < 1000)
461276da39aSCy Schubert 	       && ((rns_new - (unsigned)shm_stat->tvr.tv_nsec) < 1000)) {
462276da39aSCy Schubert 	    shm_stat->tvt.tv_nsec = cns_new;
463276da39aSCy Schubert 	    shm_stat->tvr.tv_nsec = rns_new;
4642b15cb3dSCy Schubert 	}
465276da39aSCy Schubert 	/* At this point shm_stat->tvr and shm_stat->tvt contain valid ns-level
4662b15cb3dSCy Schubert 	** timestamps, possibly generated by extending the old
4672b15cb3dSCy Schubert 	** us-level timestamps
4682b15cb3dSCy Schubert 	*/
4692b15cb3dSCy Schubert 	break;
4702b15cb3dSCy Schubert 
4712b15cb3dSCy Schubert     case 1:
4722b15cb3dSCy Schubert 
473276da39aSCy Schubert 	shm_stat->tvr.tv_sec	= shmcopy.receiveTimeStampSec;
474276da39aSCy Schubert 	shm_stat->tvr.tv_nsec	= shmcopy.receiveTimeStampUSec * 1000;
475276da39aSCy Schubert 	rns_new		= shmcopy.receiveTimeStampNSec;
476276da39aSCy Schubert 	shm_stat->tvt.tv_sec	= shmcopy.clockTimeStampSec;
477276da39aSCy Schubert 	shm_stat->tvt.tv_nsec	= shmcopy.clockTimeStampUSec * 1000;
478276da39aSCy Schubert 	cns_new		= shmcopy.clockTimeStampNSec;
4792b15cb3dSCy Schubert 
4802b15cb3dSCy Schubert 	/* See the case above for an explanation of the
4812b15cb3dSCy Schubert 	** following test.
4822b15cb3dSCy Schubert 	*/
483276da39aSCy Schubert 	if (   ((cns_new - (unsigned)shm_stat->tvt.tv_nsec) < 1000)
484276da39aSCy Schubert 	       && ((rns_new - (unsigned)shm_stat->tvr.tv_nsec) < 1000)) {
485276da39aSCy Schubert 	    shm_stat->tvt.tv_nsec = cns_new;
486276da39aSCy Schubert 	    shm_stat->tvr.tv_nsec = rns_new;
4872b15cb3dSCy Schubert 	}
488276da39aSCy Schubert 	/* At this point shm_stat->tvr and shm_stat->tvt contains valid ns-level
4892b15cb3dSCy Schubert 	** timestamps, possibly generated by extending the old
4902b15cb3dSCy Schubert 	** us-level timestamps
4912b15cb3dSCy Schubert 	*/
4922b15cb3dSCy Schubert 	break;
4932b15cb3dSCy Schubert 
4942b15cb3dSCy Schubert     default:
495276da39aSCy Schubert 	shm_stat->status = BAD_MODE;
496276da39aSCy Schubert 	break;
497276da39aSCy Schubert     }
498276da39aSCy Schubert     /*@-type@*/
499276da39aSCy Schubert 
500276da39aSCy Schubert     /*
501276da39aSCy Schubert      * leap field is not a leap offset but a leap notification code.
502276da39aSCy Schubert      * The values are magic numbers used by NTP and set by GPSD, if at all, in
503276da39aSCy Schubert      * the subframe code.
504276da39aSCy Schubert      */
505276da39aSCy Schubert     shm_stat->leap = shmcopy.leap;
506276da39aSCy Schubert     shm_stat->precision = shmcopy.precision;
507276da39aSCy Schubert 
508276da39aSCy Schubert     return shm_stat->status;
509276da39aSCy Schubert }
510276da39aSCy Schubert 
511276da39aSCy Schubert /*
512276da39aSCy Schubert  * shm_timer - called once every second.
513276da39aSCy Schubert  *
514276da39aSCy Schubert  * This tries to grab a sample from the SHM segment, filtering bad ones
515276da39aSCy Schubert  */
516276da39aSCy Schubert static void
shm_timer(int unit,struct peer * peer)517276da39aSCy Schubert shm_timer(
518276da39aSCy Schubert 	int unit,
519276da39aSCy Schubert 	struct peer *peer
520276da39aSCy Schubert 	)
521276da39aSCy Schubert {
522276da39aSCy Schubert 	struct refclockproc * const pp = peer->procptr;
523276da39aSCy Schubert 	struct shmunit *      const up = pp->unitptr;
524276da39aSCy Schubert 
525276da39aSCy Schubert 	volatile struct shmTime *shm;
526276da39aSCy Schubert 
527276da39aSCy Schubert 	l_fp tsrcv;
528276da39aSCy Schubert 	l_fp tsref;
529276da39aSCy Schubert 	int c;
530276da39aSCy Schubert 
531276da39aSCy Schubert 	/* for formatting 'a_lastcode': */
532276da39aSCy Schubert 	struct calendar cd;
533276da39aSCy Schubert 	time_t tt;
534276da39aSCy Schubert 	vint64 ts;
535276da39aSCy Schubert 
536276da39aSCy Schubert 	enum segstat_t status;
537276da39aSCy Schubert 	struct shm_stat_t shm_stat;
538276da39aSCy Schubert 
539276da39aSCy Schubert 	up->ticks++;
540276da39aSCy Schubert 	if ((shm = up->shm) == NULL) {
541276da39aSCy Schubert 		/* try to map again - this may succeed if meanwhile some-
542276da39aSCy Schubert 		body has ipcrm'ed the old (unaccessible) shared mem segment */
543276da39aSCy Schubert 		shm = up->shm = getShmTime(unit, up->forall);
544276da39aSCy Schubert 		if (shm == NULL) {
545276da39aSCy Schubert 			DPRINTF(1, ("%s: no SHM segment\n",
546276da39aSCy Schubert 				    refnumtoa(&peer->srcadr)));
547276da39aSCy Schubert 			return;
548276da39aSCy Schubert 		}
549276da39aSCy Schubert 	}
550276da39aSCy Schubert 
551276da39aSCy Schubert 	/* query the segment, atomically */
552276da39aSCy Schubert 	status = shm_query(shm, &shm_stat);
553276da39aSCy Schubert 
554276da39aSCy Schubert 	switch (status) {
555276da39aSCy Schubert 	case OK:
556276da39aSCy Schubert 	    DPRINTF(2, ("%s: SHM type %d sample\n",
557276da39aSCy Schubert 			refnumtoa(&peer->srcadr), shm_stat.mode));
558276da39aSCy Schubert 	    break;
559276da39aSCy Schubert 	case NO_SEGMENT:
560276da39aSCy Schubert 	    /* should never happen, but is harmless */
561276da39aSCy Schubert 	    return;
562276da39aSCy Schubert 	case NOT_READY:
563276da39aSCy Schubert 	    DPRINTF(1, ("%s: SHM not ready\n",refnumtoa(&peer->srcadr)));
564276da39aSCy Schubert 	    up->notready++;
565276da39aSCy Schubert 	    return;
566276da39aSCy Schubert 	case BAD_MODE:
5672b15cb3dSCy Schubert 	    DPRINTF(1, ("%s: SHM type blooper, mode=%d\n",
5682b15cb3dSCy Schubert 			refnumtoa(&peer->srcadr), shm->mode));
5692b15cb3dSCy Schubert 	    up->bad++;
5702b15cb3dSCy Schubert 	    msyslog (LOG_ERR, "SHM: bad mode found in shared memory: %d",
5712b15cb3dSCy Schubert 		     shm->mode);
572c0b746e5SOllivier Robert 	    return;
573276da39aSCy Schubert 	case CLASH:
574276da39aSCy Schubert 	    DPRINTF(1, ("%s: type 1 access clash\n",
575276da39aSCy Schubert 			refnumtoa(&peer->srcadr)));
576276da39aSCy Schubert 	    msyslog (LOG_NOTICE, "SHM: access clash in shared memory");
577276da39aSCy Schubert 	    up->clash++;
578276da39aSCy Schubert 	    return;
579276da39aSCy Schubert 	default:
580276da39aSCy Schubert 	    DPRINTF(1, ("%s: internal error, unknown SHM fetch status\n",
581276da39aSCy Schubert 			refnumtoa(&peer->srcadr)));
582276da39aSCy Schubert 	    msyslog (LOG_NOTICE, "internal error, unknown SHM fetch status");
583276da39aSCy Schubert 	    up->bad++;
584276da39aSCy Schubert 	    return;
585c0b746e5SOllivier Robert 	}
586276da39aSCy Schubert 
5872b15cb3dSCy Schubert 
5882b15cb3dSCy Schubert 	/* format the last time code in human-readable form into
5892b15cb3dSCy Schubert 	 * 'pp->a_lastcode'. Someone claimed: "NetBSD has incompatible
5902b15cb3dSCy Schubert 	 * tv_sec". I can't find a base for this claim, but we can work
5912b15cb3dSCy Schubert 	 * around that potential problem. BTW, simply casting a pointer
5922b15cb3dSCy Schubert 	 * is a receipe for disaster on some architectures.
5932b15cb3dSCy Schubert 	 */
594276da39aSCy Schubert 	tt = (time_t)shm_stat.tvt.tv_sec;
5952b15cb3dSCy Schubert 	ts = time_to_vint64(&tt);
5962b15cb3dSCy Schubert 	ntpcal_time_to_date(&cd, &ts);
5972b15cb3dSCy Schubert 
5982b15cb3dSCy Schubert 	/* add ntpq -c cv timecode in ISO 8601 format */
5992b15cb3dSCy Schubert 	c = snprintf(pp->a_lastcode, sizeof(pp->a_lastcode),
6002b15cb3dSCy Schubert 		     "%04u-%02u-%02uT%02u:%02u:%02u.%09ldZ",
6012b15cb3dSCy Schubert 		     cd.year, cd.month, cd.monthday,
6022b15cb3dSCy Schubert 		     cd.hour, cd.minute, cd.second,
603276da39aSCy Schubert 		     (long)shm_stat.tvt.tv_nsec);
60468ba7e87SXin LI 	pp->lencode = (c > 0 && (size_t)c < sizeof(pp->a_lastcode)) ? c : 0;
6052b15cb3dSCy Schubert 
6062b15cb3dSCy Schubert 	/* check 1: age control of local time stamp */
607276da39aSCy Schubert 	tt = shm_stat.tvc.tv_sec - shm_stat.tvr.tv_sec;
6082b15cb3dSCy Schubert 	if (tt < 0 || tt > up->max_delay) {
6092b15cb3dSCy Schubert 		DPRINTF(1, ("%s:SHM stale/bad receive time, delay=%llds\n",
6102b15cb3dSCy Schubert 			    refnumtoa(&peer->srcadr), (long long)tt));
6112b15cb3dSCy Schubert 		up->bad++;
6122b15cb3dSCy Schubert 		msyslog (LOG_ERR, "SHM: stale/bad receive time, delay=%llds",
6132b15cb3dSCy Schubert 			 (long long)tt);
6142b15cb3dSCy Schubert 		return;
6152b15cb3dSCy Schubert 	}
6162b15cb3dSCy Schubert 
6172b15cb3dSCy Schubert 	/* check 2: delta check */
618276da39aSCy Schubert 	tt = shm_stat.tvr.tv_sec - shm_stat.tvt.tv_sec - (shm_stat.tvr.tv_nsec < shm_stat.tvt.tv_nsec);
6192b15cb3dSCy Schubert 	if (tt < 0)
6202b15cb3dSCy Schubert 		tt = -tt;
6212b15cb3dSCy Schubert 	if (up->max_delta > 0 && tt > up->max_delta) {
6222b15cb3dSCy Schubert 		DPRINTF(1, ("%s: SHM diff limit exceeded, delta=%llds\n",
6232b15cb3dSCy Schubert 			    refnumtoa(&peer->srcadr), (long long)tt));
6242b15cb3dSCy Schubert 		up->bad++;
6252b15cb3dSCy Schubert 		msyslog (LOG_ERR, "SHM: difference limit exceeded, delta=%llds\n",
6262b15cb3dSCy Schubert 			 (long long)tt);
6272b15cb3dSCy Schubert 		return;
6282b15cb3dSCy Schubert 	}
6292b15cb3dSCy Schubert 
6302b15cb3dSCy Schubert 	/* if we really made it to this point... we're winners! */
6312b15cb3dSCy Schubert 	DPRINTF(2, ("%s: SHM feeding data\n",
6322b15cb3dSCy Schubert 		    refnumtoa(&peer->srcadr)));
633276da39aSCy Schubert 	tsrcv = tspec_stamp_to_lfp(shm_stat.tvr);
634276da39aSCy Schubert 	tsref = tspec_stamp_to_lfp(shm_stat.tvt);
635276da39aSCy Schubert 	pp->leap = shm_stat.leap;
636276da39aSCy Schubert 	peer->precision = shm_stat.precision;
6372b15cb3dSCy Schubert 	refclock_process_offset(pp, tsref, tsrcv, pp->fudgetime1);
6382b15cb3dSCy Schubert 	up->good++;
6392b15cb3dSCy Schubert }
6402b15cb3dSCy Schubert 
6412b15cb3dSCy Schubert /*
6422b15cb3dSCy Schubert  * shm_clockstats - dump and reset counters
6432b15cb3dSCy Schubert  */
shm_clockstats(int unit,struct peer * peer)6442b15cb3dSCy Schubert static void shm_clockstats(
6452b15cb3dSCy Schubert 	int unit,
6462b15cb3dSCy Schubert 	struct peer *peer
6472b15cb3dSCy Schubert 	)
6482b15cb3dSCy Schubert {
6492b15cb3dSCy Schubert 	struct refclockproc * const pp = peer->procptr;
6502b15cb3dSCy Schubert 	struct shmunit *      const up = pp->unitptr;
6512b15cb3dSCy Schubert 
6522b15cb3dSCy Schubert 	UNUSED_ARG(unit);
6532b15cb3dSCy Schubert 	if (pp->sloppyclockflag & CLK_FLAG4) {
6542b15cb3dSCy Schubert 		mprintf_clock_stats(
6552b15cb3dSCy Schubert 			&peer->srcadr, "%3d %3d %3d %3d %3d",
6562b15cb3dSCy Schubert 			up->ticks, up->good, up->notready,
6572b15cb3dSCy Schubert 			up->bad, up->clash);
6582b15cb3dSCy Schubert 	}
6592b15cb3dSCy Schubert 	up->ticks = up->good = up->notready = up->bad = up->clash = 0;
660c0b746e5SOllivier Robert }
661c0b746e5SOllivier Robert 
662c0b746e5SOllivier Robert #else
6632b15cb3dSCy Schubert NONEMPTY_TRANSLATION_UNIT
664c0b746e5SOllivier Robert #endif /* REFCLOCK */
665