ntp_refclock.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* Copyright (c) 1996 by Sun Microsystems, Inc.
* All Rights Reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* ntp_refclock - processing support for reference clocks
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <errno.h>
#ifdef HAVE_SYS_IOCTL_H
#endif /* HAVE_SYS_IOCTL_H */
#include "ntpd.h"
#include "ntp_io.h"
#include "ntp_unixtime.h"
#include "ntp_refclock.h"
#include "ntp_stdlib.h"
#ifdef REFCLOCK
#ifdef TTYCLK
#endif /* TTYCLK */
#ifdef CHUCLK
#endif /* CHUCLK */
#if defined(PPS) && !defined(SYS_SOLARIS)
#include <sys/ppsclock.h>
#endif /* PPS */
/*
* Reference clock support is provided here by maintaining the fiction
* that the clock is actually a peer. As no packets are exchanged with a
* reference clock, however, we replace the transmit, receive and packet
* procedures with separate code to simulate them. Routines
* refclock_transmit() and refclock_receive() maintain the peer
* variables in a state analogous to an actual peer and pass reference
* clock data on through the filters. Routines refclock_peer() and
* refclock_unpeer() are called to initialize and terminate reference
* clock associations. A set of utility routines is included to open
* serial devices, process sample data, edit input lines to extract
* embedded timestamps and to peform various debugging functions.
*
* The main interface used by these routines is the refclockproc
* structure, which contains for most drivers the decimal equivalants of
* the year, day, month, hour, second and millisecond/microsecond
* decoded from the ASCII timecode. Additional information includes the
* receive timestamp, exception report, statistics tallies, etc. In
* addition, there may be a driver-specific unit structure used for
* local control of the device.
*
* The support routines are passed a pointer to the peer structure,
* which is used for all peer-specific processing and contains a pointer
* to the refclockproc structure, which in turn containes a pointer to
* the unit structure, if used. In addition, some routines expect an
* address in the dotted quad form 127.127.t.u, where t is the clock
* type and u the unit. A table typeunit[type][unit] contains the peer
* structure pointer for each configured clock type and unit.
*
* Most drivers support the 1-pps signal provided by some radios and
* connected via a level converted described in the gadget directory.
* The signal is captured using a separate, dedicated serial port and
* the tty_clk line discipline/streams modules described in the kernel
* directory. For the highest precision, the signal is captured using
* the carrier-detect line of the same serial port using the ppsclock
* streams module described in the ppsclock directory.
*/
#ifndef CLKLDISC
#endif
#ifndef CHULDISC
#endif
/*
* The refclock configuration table. Imported from refclock_conf
*/
extern struct refclock *refclock_conf[];
extern u_char num_refclock_conf;
/*
* Imported from the I/O module
*/
extern struct interface *any_interface;
extern struct interface *loopback_interface;
/*
* Imported from ntp_loopfilter module
*/
extern int fdpps; /* pps file descriptor */
#ifdef PPS
extern int pps_enable; /* pps enabled indicator (from ntp_loopfilter.c) */
#endif /* PPS */
/*
* Imported from the timer module
*/
extern u_long current_time;
extern struct event timerqueue[];
/*
* Imported from the main and peer modules. We use the same algorithm
* for spacing out timers at configuration time that the peer module
* does.
*/
extern u_long init_peer_starttime;
extern int initializing;
extern int debug;
/*
* debugging. When all clock drivers have been converted to new style,
* this dissapears.
*/
/*
* Forward declarations
*/
static int refclock_cmpl_fp P((const void *, const void *));
/*
* refclock_report - note the occurance of an event
*
* This routine presently just remembers the report and logs it, but
* does nothing heroic for the trap handler. It tries to be a good
* citizen and bothers the system log only if things change.
*/
void
{
struct refclockproc *pp;
return;
if (code == CEVNT_BADREPLY)
if (code == CEVNT_BADTIME)
if (code == CEVNT_TIMEOUT)
if (code == CEVNT_FAULT)
"clock %s fault '%s' (0x%02x)",
else {
"clock %s event '%s' (0x%02x)",
}
}
}
/*
* init_refclock - initialize the reference clock drivers
*
* This routine calls each of the drivers in turn to initialize internal
* variables, if necessary. Most drivers have nothing to say at this
* point.
*/
void
{
int i, j;
for (i = 0; i < (int)num_refclock_conf; i++) {
(refclock_conf[i]->clock_init)();
for (j = 0; j < MAXUNIT; j++)
typeunit[i][j] = 0;
}
}
/*
* refclock_newpeer - initialize and start a reference clock
*
* This routine allocates and initializes the interface structure which
* supports a reference clock in the form of an ordinary NTP peer. A
* driver-specific support routine completes the initialization, if
* used. Default peer variables which identify the clock and establish
* its reference ID and stratum are set here. It returns one if success
* and zero if the clock address is invalid or already running,
* insufficient resources are available or the driver declares a bum
* rap.
*/
int
{
struct refclockproc *pp;
int unit;
/*
* Check for valid clock address. If already running, shut it
* down first.
*/
"refclock_newpeer: clock address %s invalid",
return (0);
}
"refclock_newpeer: clock type %d invalid\n",
clktype);
return (0);
}
/*
* Allocate and initialize interface structure
*/
if (!(pp = (struct refclockproc *)
emalloc(sizeof(struct refclockproc))))
return (0);
/*
* Initialize structures
*/
/*
* Do driver dependent initialization
*/
return (0);
}
else
/*
* Set up the timeout for polling and reachability determination
*/
if (initializing) {
} else {
}
return (1);
}
/*
* refclock_unpeer - shut down a clock
*/
void
{
int unit;
/*
* Wiggle the driver to release its resources, then give back
* the interface structure.
*/
return;
}
/*
* refclock_transmit - simulate the transmit procedure
*
* This routine implements the NTP transmit procedure for a reference
* clock. This provides a mechanism to call the driver at the NTP poll
* interval, as well as provides a reachability mechanism to detect a
* broken radio or other madness.
*/
void
{
struct refclockproc *pp;
int unit;
/*
* The transmit procedure is supposed to freeze a timestamp.
* Get one just for fun, and to tell when we last were here.
*/
/*
* Fiddle reachability.
*/
/*
* Clear this one out. No need to redo selection since
* this fellow will definitely be suffering from
* dispersion madness.
*/
if (opeer_reach != 0) {
}
/*
* Update reachability and poll variables
*/
} else if ((opeer_reach & 3) == 0) {
clock_select();
/*
* If he wants to be polled, do it. New style drivers do not use
* the unit argument, since the fudge stuff is in the
* refclockproc structure.
*/
/*
* Finally, reset the timer
*/
}
/*
* Compare two l_fp's - used with qsort()
*/
static int
{
return (-1);
return (0);
return (1);
}
/*
* refclock_process - process a pile of samples from the clock
*
* This routine converts the timecode in the form days, hours, miinutes,
* seconds, milliseconds/microseconds to internal timestamp format.
* Further processing is then delegated to refclock sample
*/
int
int nstart; /* stages of median filter */
int nskeep; /* stages after outlyer trim */
{
/*
* Compute the timecode timestamp from the days, hours, minutes,
* seconds and milliseconds/microseconds of the timecode. Use
* the fraction, when present. Note that this code relies on the
* filesystem time for the years and does not use the years of
* the timecode.
*/
return (0);
} else {
}
/*
* Include the configured fudgetime1 adjustment.
*/
}
/*
* refclock_sample - process a pile of samples from the clock
*
* This routine converts the timecode in the form days, hours, miinutes,
* seconds, milliseconds/microseconds to internal timestamp format. It
* then calculates the difference from the receive timestamp and
* assembles the samples in a shift register. It implements a recursive
* median filter to suppress spikes in the data, as well as determine a
* rough dispersion estimate. A configuration constant time adjustment
* fudgetime1 can be added to the final offset to compensate for various
* systematic errors. The routine returns one if success and zero if
* failure due to invalid timecode data or very noisy offsets.
*
* This interface is needed to allow for clocks (e. g. parse) that can
* provide the correct offset including year information (though NTP
* usually gives up on offsets greater than 1000 seconds).
*/
int
for filter machine */
int nstart; /* stages of median filter */
int nskeep; /* stages after outlyer trim */
{
int i, n;
/*
* Subtract the receive timestamp from the timecode timestamp
* to form the raw offset. Insert in the median filter shift
* register.
*/
offset = *sample_offset;
/*
* Copy the raw offsets and sort into ascending order
*/
/*
* Reject the furthest from the median of nstages samples until
* nskeep samples remain.
*/
i = 0;
while ((n - i) > nskeep) {
/* reject low end */
i++;
} else {
/* reject high end */
n--;
}
}
/*
* Compute the dispersion based on the difference between the
* extremes of the remaining offsets. Add to this the time since
* the last clock update, which represents the dispersion
* increase with time. We know that NTP_MAXSKEW is 16. If the
* sum is greater than the allowed sample dispersion, bail out.
* If the loop is unlocked, return the most recent offset;
* otherwise, return the median offset.
*/
if (disp > REFCLOCKMAXDISPERSE)
return (0);
return (1);
}
/*
* refclock_receive - simulate the receive and packet procedures
*
* This routine simulates the NTP receive and packet procedures for a
* reference clock. This provides a mechanism in which the ordinary NTP
* filter, selection and combining algorithms can be used to suppress
* misbehaving radios and to mitigate between them when more than one is
* available for backup.
*/
void
int leap; /* synchronization/leap code */
{
int restrict_mask;
int trustable;
int refclock_own_states;
#ifdef DEBUG
if (debug)
printf("refclock_receive: %s %s %s %s)\n",
#endif
/*
* some refclock implementations do a complete state and
* event handling. reporting events must be disabled for
* these critters (namely parse)
*/
leap &= ~REFCLOCK_OWN_STATES;
/*
* The authentication and access-control machinery works, but
* its utility may be questionable.
*/
return;
if (restrict_mask & RES_DONTTRUST)
trustable = 0;
else
trustable = 1;
if (trustable)
else
}
/*
* Set the timestamps. rec and org are in local time, while ref
* is in timecode time.
*/
/*
* If the interface has been set to any_interface, set it to the
* loopback address if we have one. This is so that peers which
* are unreachable are easy to see in the peer display.
*/
/*
* Set peer.pmode based on the hmode. For appearances only.
*/
case MODE_ACTIVE:
break;
default:
break;
}
/*
* Abandon ship if the radio came bum. We only got this far
* in order to make pretty billboards, even if bum.
*/
if (leap == LEAP_NOTINSYNC)
return;
/*
* If this guy was previously unreachable, report him
* reachable.
*/
/*
* Give the data to the clock filter and update the clock. Note
* the clock reading precision initialized by the driver is
* added at this point.
*/
if (precision == 0)
precision = 1;
if (!refclock_own_states)
}
/*
* refclock_gtlin - groom next input line and extract timestamp
*
* This routine processes the timecode received from the clock and
* removes the parity bit and control characters. If a timestamp is
* present in the timecode, as produced by the tty_clk line
* discipline/streams module, it returns that as the timestamp;
* otherwise, it returns the buffer timestamp. The routine return code
* is the number of characters in the line.
*/
int
char *lineptr; /* current line pointer */
int bmax; /* remaining characters in line */
{
int i;
char c;
#ifdef TIOCDCDTIMESTAMP
#endif
/*
* Check for the presence of a timestamp left by the tty_clock
* line discipline/streams module and, if present, use that
* instead of the buffer timestamp captured by the I/O routines.
* We recognize a timestamp by noting its value is earlier than
* the buffer timestamp, but not more than one second earlier.
*/
#ifdef TIOCDCDTIMESTAMP
#ifdef DEBUG
if (debug) {
"refclock_gtlin: fd %d DCDTIMESTAMP %s",
printf(" sigio %s\n",
}
#endif
} else
}
else
/* XXX fallback to old method if kernel refuses TIOCDCDTIMESTAMP */
#endif /* TIOCDCDTIMESTAMP */
#ifdef DEBUG
if (debug) {
"refclock_gtlin: fd %d ldisc %s",
get_systime(&trtmp);
printf(" sigio %s\n",
}
#endif
dpend -= 8;
} else
}
}
/*
* Edit timecode to remove control chars. Don't monkey with the
* line buffer if the input buffer contains no ASCII printing
* characters.
*/
c = *dpt & 0x7f;
if (c >= ' ')
*dp++ = c;
}
if (i > 0)
*dp = '\0';
#ifdef DEBUG
if (debug)
printf("refclock_gtlin: fd %d time %s timecode %d %s\n",
#endif
return (i);
}
/*
* The following code does not apply to WINNT & VMS ...
*/
#ifndef SYS_VXWORKS
/*
* refclock_open - open serial port for reference clock
*
* This routine opens a serial port for I/O and sets default options. It
* returns the file descriptor if success and zero if failure.
*/
int
char *dev; /* device name pointer */
int speed; /* serial port speed (code) */
int flags; /* line discipline flags */
{
int fd;
#ifdef HAVE_TERMIOS
#endif /* HAVE_TERMIOS */
#ifdef HAVE_SYSV_TTYS
#endif /* HAVE_SYSV_TTYS */
#ifdef HAVE_BSD_TTYS
#endif /* HAVE_BSD_TTYS */
#ifdef TIOCMGET
#endif /* TIOCMGET */
/*
* Open serial port and set default options
*/
#ifdef O_NONBLOCK
#endif
, 0777);
if (fd == -1) {
return (0);
}
/*
* The following sections initialize the serial line port in
* canonical (line-oriented) mode and set the specified line
* speed, 8 bits and no parity. The modem control, break, erase
* and kill functions are normally disabled. There is a
* different section for each terminal interface, as selected at
* compile time.
*/
#ifdef HAVE_TERMIOS
/*
* POSIX serial line parameters (termios interface)
*/
"refclock_open: fd %d tcgetattr %m", fd);
return (0);
}
/*
* Set canonical mode and local connection; set specified speed,
* 8 bits and no parity; map CR to NL; ignore break.
*/
#ifdef TIOCMGET
/*
* If we have modem control, check to see if modem leads are
* active; if so, set remote connection. This is necessary for
* the kernel pps mods to work.
*/
ltemp = 0;
"refclock_open: fd %d TIOCMGET failed: %m", fd);
#if DEBUG
if (debug)
printf("refclock_open: fd %d modem status %lx\n",
#endif
#endif /* TIOCMGET */
"refclock_open: fd %d TCSANOW failed: %m", fd);
return (0);
}
"refclock_open: fd %d TCIOFLUSH failed: %m", fd);
return (0);
}
#endif /* HAVE_TERMIOS */
#ifdef HAVE_SYSV_TTYS
/*
* System V serial line parameters (termio interface)
*
*/
"refclock_open: fd %d TCGETA failed: %m", fd);
return (0);
}
/*
* Set canonical mode and local connection; set specified speed,
* 8 bits and no parity; map CR to NL; ignore break.
*/
#ifdef TIOCMGET
/*
* If we have modem control, check to see if modem leads are
* active; if so, set remote connection. This is necessary for
* the kernel pps mods to work.
*/
ltemp = 0;
"refclock_open: fd %d TIOCMGET failed: %m", fd);
#if DEBUG
if (debug)
printf("refclock_open: fd %d modem status %lx\n",
#endif
#endif /* TIOCMGET */
"refclock_open: fd %d TCSETA failed: %m", fd);
return (0);
}
#endif /* HAVE_SYSV_TTYS */
#ifdef HAVE_BSD_TTYS
/*
* 4.3bsd serial line parameters (sgttyb interface)
*/
"refclock_open: fd %d TIOCGETP %m", fd);
return (0);
}
"refclock_open: TIOCSETP failed: %m");
return (0);
}
#endif /* HAVE_BSD_TTYS */
fd);
return (0);
}
return (fd);
}
#endif /* HAVE_TERMIOS || HAVE_SYSV_TTYS || HAVE_BSD_TTYS */
#endif /* SYS_VXWORKS */
/*
* refclock_ioctl - set serial port control functions
*
* This routine attempts to hide the internal, system-specific details
* of serial ports. It can handle POSIX (termios), SYSV (termio) and BSD
* (sgtty) interfaces with varying degrees of success. The routine sets
* if compiled in the daemon and requested in the call. The routine
* returns one if success and zero if failure.
*/
int
int fd; /* file descriptor */
int flags; /* line discipline flags */
{
/* simply return 1 if no UNIX line discipline is supported */
#ifndef SYS_VXWORKS
#ifdef HAVE_TERMIOS
#endif /* HAVE_TERMIOS */
#ifdef HAVE_SYSV_TTYS
#endif /* HAVE_SYSV_TTYS */
#ifdef HAVE_BSD_TTYS
#endif /* HAVE_BSD_TTYS */
#ifdef DEBUG
if (debug)
printf("refclock_ioctl: fd %d flags %x\n",
#endif
/*
* The following sections select optional features, such as
* modem control, line discipline and so forth. Some require
* specific operating system support in the form of streams
* modules, which can be loaded and unloaded at run time without
* rebooting the kernel, or line discipline modules, which must
* be compiled in the kernel. The streams modules require System
* V STREAMS support, while the line discipline modules require
* 4.3bsd or later. The checking frenzy is attenuated here,
* since the device is already open.
*
* Note that both the clk and ppsclock modules are optional; the
* dang thing still works, but the accuracy improvement using
* them will not be available. The ppsclock module is associated
* with a specific, declared line and should be used only once.
* If requested, the chu module is mandatory, since the driver
* will not work without it.
*
* Use the LDISC_PPS option ONLY with Sun baseboard ttya or
* ttyb. Using it with the SPIF multipexor crashes the kernel.
*/
if (flags == 0)
return (1);
#if !(defined(HAVE_TERMIOS) || defined(HAVE_BSD_TTYS))
"refclock_ioctl: unsupported terminal interface");
return (0);
#endif /* HAVE_TERMIOS HAVE_BSD_TTYS */
#ifdef STREAM
#ifdef TTYCLK
/*
* The TTYCLK option provides timestamping at the driver level.
* It requires the tty_clk streams module and System V STREAMS
* support.
*/
{
{
"refclock_ioctl: optional clk streams module unavailable: %m");
}
else
{
char *str;
str = "\377";
else if (flags & LDISC_ACTS)
str = "*";
else
str = "\n";
"refclock_ioctl: CLK_SETSTR failed: %m");
}
}
/*
* The ACTS line discipline requires additional line-ending
* character '*'.
*/
if (flags & LDISC_ACTS) {
}
#else
"refclock_ioctl: optional clk streams module unsupported");
#endif /* TTYCLK */
#ifdef CHUCLK
/*
* The CHUCLK option provides timestamping and decoding for the CHU
* timecode. It requires the tty_chu streams module and System V
* STREAMS support.
*/
{
{
"refclock_ioctl: required chu streams module unavailable");
return (0);
}
}
#else
{
"refclock_ioctl: required chu streams module unsupported");
return (0);
}
#endif /* CHUCLK */
#ifdef PPS
/*
* The PPS option provides timestamping at the driver level.
* It uses a 1-pps signal and level converter (gadget box) and
* requires the ppsclock streams module and System V STREAMS
* support.
*/
{
if (fdpps != -1)
{
"refclock_ioctl: ppsclock already configured");
return (0);
}
if (
#ifdef SYS_SOLARIS
#else
#endif /* SYS_SOLARIS */
)
{
"refclock_ioctl: optional ppsclock streams module unavailable");
}
else
{
}
}
#else
"refclock_ioctl: optional ppsclock streams module unsupported");
#endif /* PPS */
#else /* not STREAM */
#ifdef HAVE_TERMIOS
#ifdef TTYCLK
/*
* The TTYCLK option provides timestamping at the driver level. It
* requires the tty_clk line discipline and 4.3bsd or later.
*/
if (flags & LDISC_CLKPPS)
else if (flags & LDISC_ACTS) {
} else
}
#else
"refclock_ioctl: optional clk line discipline unsupported");
#endif /* TTYCLK */
#ifdef CHUCLK
/*
* The CHUCLK option provides timestamping and decoding for the CHU
* timecode. It requires the tty_chu line disciplne and 4.3bsd
* or later.
*/
}
#else
"refclock_ioctl: required chu line discipline unsupported");
return (0);
}
#endif /* CHUCLK */
#endif /* HAVE_TERMIOS */
#ifdef HAVE_BSD_TTYS
#ifdef TTYCLK
/*
* The TTYCLK option provides timestamping at the driver level. It
* requires the tty_clk line discipline and 4.3bsd or later.
*/
if (flags & LDISC_CLKPPS)
else if (flags & LDISC_ACTS) {
} else
"refclock_ioctl: optional clk line discipline unavailable");
}
#else
"refclock_ioctl: optional clk line discipline unsupported");
#endif /* TTYCLK */
#ifdef CHUCLK
/*
* The CHUCLK option provides timestamping and decoding for the CHU
* timecode. It requires the tty_chu line disciplne and 4.3bsd
* or later.
*/
"refclock_ioctl: required chu line discipline unavailable");
return (0);
}
}
#else
"refclock_ioctl: required chu line discipline unsupported");
return (0);
}
#endif /* CHUCLK */
#endif /* HAVE_BSD_TTYS */
#endif /* STREAM */
#endif /* HAVE_TERMIOS || HAVE_SYSV_TTYS || HAVE_BSD_TTYS */
#endif /* SYS_VXWORKS */
return (1);
}
/*
*
* This routine is used mainly for debugging. It returns designated
* values from the interface structure that can be displayed using
* xntpdc and the clockstat command. It can also be used to initialize
* configuration variables, such as fudgetimes, fudgevalues, reference
* ID and stratum.
*/
void
struct sockaddr_in *srcadr;
struct refclockstat *in;
struct refclockstat *out;
{
struct refclockproc *pp;
int unit;
/*
* Check for valid address and running peer
*/
if (!ISREFCLOCKADR(srcadr))
return;
return;
return;
/*
* Initialize requested data
*/
if (in != 0) {
else
}
}
}
}
}
/*
* Readback requested data
*/
if (out != 0) {
}
/*
* Give the stuff to the clock
*/
}
/*
* refclock_buginfo - return debugging info
*
* This routine is used mainly for debugging. It returns designated
* values from the interface structure that can be displayed using
* xntpdc and the clkbug command.
*/
void
{
struct refclockproc *pp;
int unit;
int i;
/*
* Check for valid address and peer structure
*/
if (!ISREFCLOCKADR(srcadr))
return;
return;
return;
/*
* Copy structure values
*/
/*
* Give the stuff to the clock
*/
}
#endif /* REFCLOCK */