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