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