2 * Copyright (c) 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 int fd
; /* udp descriptor */
37 int server_state
; /* -1 (no dns), 0 (dns good), or 1 (pkt good) */
38 int server_insane
; /* insanity check */
39 int poll_sleep
; /* countdown for poll (in seconds) */
40 int poll_mode
; /* mode of operation */
41 int poll_count
; /* number of polls in current mode */
42 int poll_failed
; /* count of NTP failures */
43 struct sockaddr
*sam
; /* udp connection info */
44 struct sockaddr_storage sam_st
; /* udp connection info (stor) */
45 char *target
; /* target hostname or IP (string) */
46 char *ipstr
; /* IP string */
49 * A second linear regression playing hopskip with the first. This
50 * is maintained by the check function.
52 struct server_info
*altinfo
;
55 * Linear regression accumulator
57 * note: the starting base time is where all corrections get applied
58 * to eventually. The linear regression makes a relative microseconds
59 * calculation between the current base time and the starting base
60 * time to figure out what corrections the system has made to the
63 struct timeval lin_tv
; /* starting real time */
64 struct timeval lin_btv
; /* starting base time */
65 double lin_count
; /* samples */
66 double lin_sumx
; /* sum(x) */
67 double lin_sumy
; /* sum(y) */
68 double lin_sumxy
; /* sum(x*y) */
69 double lin_sumx2
; /* sum(x^2) */
70 double lin_sumy2
; /* sum(y^2) */
73 * Offsets are accumulated for a straight average. When a
74 * correction is made we have to reset the averaging code
75 * or follow-up corrections will oscillate wildly because
76 * the new offsets simply cannot compete with the dozens
77 * of previously polls in the sum.
79 double lin_sumoffset
; /* sum of compensated offsets */
80 double lin_sumoffset2
; /* sum of compensated offsets^2 */
81 double lin_countoffset
; /* count is reset after a correction is made */
86 double lin_cache_slope
; /* (freq calculations) */
87 double lin_cache_yint
; /* (freq calculations) */
88 double lin_cache_corr
; /* (freq calculations) */
89 double lin_cache_stddev
; /* (offset calculations) */
91 double lin_cache_offset
; /* last sampled offset (NOT an average) */
92 double lin_cache_freq
; /* last frequency correction (s/s) */
96 * Polling modes and max polls for specific modes. Note that the polling
97 * mode basically just effects the polling rate. It does not effect the
100 #define POLL_FIXED 0 /* fixed internal (nom_sleep_opt seconds) */
101 #define POLL_STARTUP 1 /* startup poll for offset adjust (min) */
102 #define POLL_ACQUIRE 2 /* acquisition for frequency adjust (nom) */
103 #define POLL_MAINTAIN 3 /* maintainance mode (max) */
104 #define POLL_FAILED 4 /* failure state (nom) */
106 #define POLL_STARTUP_MAX 6 /* max polls in this mode */
107 #define POLL_ACQUIRE_MAX 16 /* max polls in this mode */
108 #define POLL_FAIL_RESET 3 /* reset the regression after 3 fails */
109 #define POLL_RECOVERY_RESTART 10 /* ->ACQ vs ->STARTUP after recovery */
112 * We start a second linear regression a LIN_RESTART / 2 and it
113 * replaces the first one (and we start a third) at LIN_RESTART.
115 #define LIN_RESTART 30
118 * A course correction is made if the time gets more then 2 minutes
121 #define COURSE_OFFSET_CORRECTION_LIMIT 120.0
123 typedef struct server_info
*server_info_t
;
125 void client_init(void);
126 void client_main(struct server_info
**info_ary
, int count
) __dead2
;
127 void client_poll(server_info_t info
, int poll_interval
,
128 int calc_offset_correction
);
129 void client_check(struct server_info
**check
,
130 struct server_info
**best_off
,
131 struct server_info
**best_freq
);
132 void client_check_duplicate_ips(struct server_info
**info_ary
, int count
);
133 void client_manage_polling_mode(struct server_info
*info
, int *didreconnect
);
134 void client_setserverstate(server_info_t info
, int state
, const char *str
);
136 void lin_regress(server_info_t info
,
137 struct timeval
*ltv
, struct timeval
*lbtv
,
138 double offset
, int calc_offset_correction
);
139 void lin_reset(server_info_t info
);
140 void lin_resetalloffsets(struct server_info
**info_ary
, int count
);
141 void lin_resetoffsets(server_info_t info
);