Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1 | /* |
| 2 | * NTP client/server, based on OpenNTPD 3.9p1 |
| 3 | * |
| 4 | * Author: Adam Tkac <vonsch@gmail.com> |
| 5 | * |
| 6 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
| 7 | * |
| 8 | * Parts of OpenNTPD clock syncronization code is replaced by |
Denys Vlasenko | bfc2a32 | 2010-01-01 18:12:06 +0100 | [diff] [blame] | 9 | * code which is based on ntp-4.2.6, whuch carries the following |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 10 | * copyright notice: |
| 11 | * |
| 12 | *********************************************************************** |
| 13 | * * |
| 14 | * Copyright (c) University of Delaware 1992-2009 * |
| 15 | * * |
| 16 | * Permission to use, copy, modify, and distribute this software and * |
| 17 | * its documentation for any purpose with or without fee is hereby * |
| 18 | * granted, provided that the above copyright notice appears in all * |
| 19 | * copies and that both the copyright notice and this permission * |
| 20 | * notice appear in supporting documentation, and that the name * |
| 21 | * University of Delaware not be used in advertising or publicity * |
| 22 | * pertaining to distribution of the software without specific, * |
| 23 | * written prior permission. The University of Delaware makes no * |
| 24 | * representations about the suitability this software for any * |
| 25 | * purpose. It is provided "as is" without express or implied * |
| 26 | * warranty. * |
| 27 | * * |
| 28 | *********************************************************************** |
| 29 | */ |
| 30 | #include "libbb.h" |
| 31 | #include <math.h> |
| 32 | #include <netinet/ip.h> /* For IPTOS_LOWDELAY definition */ |
| 33 | #include <sys/timex.h> |
| 34 | #ifndef IPTOS_LOWDELAY |
| 35 | # define IPTOS_LOWDELAY 0x10 |
| 36 | #endif |
| 37 | #ifndef IP_PKTINFO |
| 38 | # error "Sorry, your kernel has to support IP_PKTINFO" |
| 39 | #endif |
| 40 | |
| 41 | |
Denys Vlasenko | bfc2a32 | 2010-01-01 18:12:06 +0100 | [diff] [blame] | 42 | /* Verbosity control (max level of -dddd options accepted). |
| 43 | * max 5 is very talkative (and bloated). 2 is non-bloated, |
| 44 | * production level setting. |
| 45 | */ |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 46 | #define MAX_VERBOSE 2 |
Denys Vlasenko | bfc2a32 | 2010-01-01 18:12:06 +0100 | [diff] [blame] | 47 | |
| 48 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 49 | #define RETRY_INTERVAL 5 /* on error, retry in N secs */ |
| 50 | #define QUERYTIME_MAX 15 /* wait for reply up to N secs */ |
| 51 | |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 52 | #define FREQ_TOLERANCE 0.000015 /* % frequency tolerance (15 PPM) */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 53 | #define MINPOLL 4 /* % minimum poll interval (6: 64 s) */ |
| 54 | #define MAXPOLL 12 /* % maximum poll interval (12: 1.1h, 17: 36.4h) (was 17) */ |
| 55 | #define MINDISP 0.01 /* % minimum dispersion (s) */ |
| 56 | #define MAXDISP 16 /* maximum dispersion (s) */ |
| 57 | #define MAXSTRAT 16 /* maximum stratum (infinity metric) */ |
| 58 | #define MAXDIST 1 /* % distance threshold (s) */ |
| 59 | #define MIN_SELECTED 1 /* % minimum intersection survivors */ |
| 60 | #define MIN_CLUSTERED 3 /* % minimum cluster survivors */ |
| 61 | |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 62 | #define MAXDRIFT 0.000500 /* frequency drift we can correct (500 PPM) */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 63 | |
| 64 | /* Clock discipline parameters and constants */ |
| 65 | #define STEP_THRESHOLD 0.128 /* step threshold (s) */ |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 66 | #define WATCH_THRESHOLD 150 /* stepout threshold (s). std ntpd uses 900 (11 mins (!)) */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 67 | /* NB: set WATCH_THRESHOLD to ~60 when debugging to save time) */ |
| 68 | #define PANIC_THRESHOLD 1000 /* panic threshold (s) */ |
| 69 | |
| 70 | /* Poll-adjust threshold. |
| 71 | * When we see that offset is small enough compared to discipline jitter, |
Denys Vlasenko | bfc2a32 | 2010-01-01 18:12:06 +0100 | [diff] [blame] | 72 | * we grow a counter: += MINPOLL. When it goes over POLLADJ_LIMIT, |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 73 | * we poll_exp++. If offset isn't small, counter -= poll_exp*2, |
| 74 | * and when it goes below -POLLADJ_LIMIT, we poll_exp-- |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 75 | */ |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 76 | #define POLLADJ_LIMIT 30 |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 77 | /* If offset < POLLADJ_GATE * discipline_jitter, then we can increase |
| 78 | * poll interval (we think we can't improve timekeeping |
| 79 | * by staying at smaller poll). |
| 80 | */ |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 81 | #define POLLADJ_GATE 4 |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 82 | /* Compromise Allan intercept (s). doc uses 1500, std ntpd uses 512 */ |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 83 | #define ALLAN 512 |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 84 | /* PLL loop gain */ |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 85 | #define PLL 65536 |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 86 | /* FLL loop gain [why it depends on MAXPOLL??] */ |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 87 | #define FLL (MAXPOLL + 1) |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 88 | /* Parameter averaging constant */ |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 89 | #define AVG 4 |
| 90 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 91 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 92 | enum { |
| 93 | NTP_VERSION = 4, |
| 94 | NTP_MAXSTRATUM = 15, |
| 95 | |
| 96 | NTP_DIGESTSIZE = 16, |
| 97 | NTP_MSGSIZE_NOAUTH = 48, |
| 98 | NTP_MSGSIZE = (NTP_MSGSIZE_NOAUTH + 4 + NTP_DIGESTSIZE), |
| 99 | |
| 100 | /* Status Masks */ |
| 101 | MODE_MASK = (7 << 0), |
| 102 | VERSION_MASK = (7 << 3), |
| 103 | VERSION_SHIFT = 3, |
| 104 | LI_MASK = (3 << 6), |
| 105 | |
| 106 | /* Leap Second Codes (high order two bits of m_status) */ |
| 107 | LI_NOWARNING = (0 << 6), /* no warning */ |
| 108 | LI_PLUSSEC = (1 << 6), /* add a second (61 seconds) */ |
| 109 | LI_MINUSSEC = (2 << 6), /* minus a second (59 seconds) */ |
| 110 | LI_ALARM = (3 << 6), /* alarm condition */ |
| 111 | |
| 112 | /* Mode values */ |
| 113 | MODE_RES0 = 0, /* reserved */ |
| 114 | MODE_SYM_ACT = 1, /* symmetric active */ |
| 115 | MODE_SYM_PAS = 2, /* symmetric passive */ |
| 116 | MODE_CLIENT = 3, /* client */ |
| 117 | MODE_SERVER = 4, /* server */ |
| 118 | MODE_BROADCAST = 5, /* broadcast */ |
| 119 | MODE_RES1 = 6, /* reserved for NTP control message */ |
| 120 | MODE_RES2 = 7, /* reserved for private use */ |
| 121 | }; |
| 122 | |
| 123 | //TODO: better base selection |
| 124 | #define OFFSET_1900_1970 2208988800UL /* 1970 - 1900 in seconds */ |
| 125 | |
| 126 | #define NUM_DATAPOINTS 8 |
| 127 | |
| 128 | typedef struct { |
| 129 | uint32_t int_partl; |
| 130 | uint32_t fractionl; |
| 131 | } l_fixedpt_t; |
| 132 | |
| 133 | typedef struct { |
| 134 | uint16_t int_parts; |
| 135 | uint16_t fractions; |
| 136 | } s_fixedpt_t; |
| 137 | |
| 138 | typedef struct { |
| 139 | uint8_t m_status; /* status of local clock and leap info */ |
| 140 | uint8_t m_stratum; |
| 141 | uint8_t m_ppoll; /* poll value */ |
| 142 | int8_t m_precision_exp; |
| 143 | s_fixedpt_t m_rootdelay; |
| 144 | s_fixedpt_t m_rootdisp; |
| 145 | uint32_t m_refid; |
| 146 | l_fixedpt_t m_reftime; |
| 147 | l_fixedpt_t m_orgtime; |
| 148 | l_fixedpt_t m_rectime; |
| 149 | l_fixedpt_t m_xmttime; |
| 150 | uint32_t m_keyid; |
| 151 | uint8_t m_digest[NTP_DIGESTSIZE]; |
| 152 | } msg_t; |
| 153 | |
| 154 | typedef struct { |
| 155 | double d_recv_time; |
| 156 | double d_offset; |
| 157 | double d_dispersion; |
| 158 | } datapoint_t; |
| 159 | |
| 160 | typedef struct { |
| 161 | len_and_sockaddr *p_lsa; |
| 162 | char *p_dotted; |
| 163 | /* when to send new query (if p_fd == -1) |
| 164 | * or when receive times out (if p_fd >= 0): */ |
| 165 | time_t next_action_time; |
| 166 | int p_fd; |
| 167 | int datapoint_idx; |
| 168 | uint32_t lastpkt_refid; |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 169 | uint8_t lastpkt_status; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 170 | uint8_t lastpkt_stratum; |
| 171 | uint8_t p_reachable_bits; |
| 172 | double p_xmttime; |
| 173 | double lastpkt_recv_time; |
| 174 | double lastpkt_delay; |
| 175 | double lastpkt_rootdelay; |
| 176 | double lastpkt_rootdisp; |
| 177 | /* produced by filter algorithm: */ |
| 178 | double filter_offset; |
| 179 | double filter_dispersion; |
| 180 | double filter_jitter; |
| 181 | datapoint_t filter_datapoint[NUM_DATAPOINTS]; |
| 182 | /* last sent packet: */ |
| 183 | msg_t p_xmt_msg; |
| 184 | } peer_t; |
| 185 | |
| 186 | |
| 187 | enum { |
| 188 | OPT_n = (1 << 0), |
| 189 | OPT_q = (1 << 1), |
| 190 | OPT_N = (1 << 2), |
| 191 | OPT_x = (1 << 3), |
| 192 | /* Insert new options above this line. */ |
| 193 | /* Non-compat options: */ |
| 194 | OPT_p = (1 << 4), |
| 195 | OPT_l = (1 << 5) * ENABLE_FEATURE_NTPD_SERVER, |
| 196 | }; |
| 197 | |
| 198 | struct globals { |
| 199 | /* total round trip delay to currently selected reference clock */ |
| 200 | double rootdelay; |
| 201 | /* reference timestamp: time when the system clock was last set or corrected */ |
| 202 | double reftime; |
| 203 | /* total dispersion to currently selected reference clock */ |
| 204 | double rootdisp; |
| 205 | llist_t *ntp_peers; |
| 206 | #if ENABLE_FEATURE_NTPD_SERVER |
| 207 | int listen_fd; |
| 208 | #endif |
| 209 | unsigned verbose; |
| 210 | unsigned peer_cnt; |
| 211 | /* refid: 32-bit code identifying the particular server or reference clock |
| 212 | * in stratum 0 packets this is a four-character ASCII string, |
| 213 | * called the kiss code, used for debugging and monitoring |
| 214 | * in stratum 1 packets this is a four-character ASCII string |
| 215 | * assigned to the reference clock by IANA. Example: "GPS " |
| 216 | * in stratum 2+ packets, it's IPv4 address or 4 first bytes of MD5 hash of IPv6 |
| 217 | */ |
| 218 | uint32_t refid; |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 219 | uint8_t ntp_status; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 220 | /* precision is defined as the larger of the resolution and time to |
| 221 | * read the clock, in log2 units. For instance, the precision of a |
| 222 | * mains-frequency clock incrementing at 60 Hz is 16 ms, even when the |
| 223 | * system clock hardware representation is to the nanosecond. |
| 224 | * |
| 225 | * Delays, jitters of various kinds are clamper down to precision. |
| 226 | * |
| 227 | * If precision_sec is too large, discipline_jitter gets clamped to it |
| 228 | * and if offset is much smaller than discipline_jitter, poll interval |
| 229 | * grows even though we really can benefit from staying at smaller one, |
| 230 | * collecting non-lagged datapoits and correcting the offset. |
| 231 | * (Lagged datapoits exist when poll_exp is large but we still have |
| 232 | * systematic offset error - the time distance between datapoints |
| 233 | * is significat and older datapoints have smaller offsets. |
| 234 | * This makes our offset estimation a bit smaller than reality) |
| 235 | * Due to this effect, setting G_precision_sec close to |
| 236 | * STEP_THRESHOLD isn't such a good idea - offsets may grow |
| 237 | * too big and we will step. I observed it with -6. |
| 238 | * |
| 239 | * OTOH, setting precision too small would result in futile attempts |
| 240 | * to syncronize to the unachievable precision. |
| 241 | * |
| 242 | * -6 is 1/64 sec, -7 is 1/128 sec and so on. |
| 243 | */ |
| 244 | #define G_precision_exp -8 |
| 245 | #define G_precision_sec (1.0 / (1 << (- G_precision_exp))) |
| 246 | uint8_t stratum; |
| 247 | /* Bool. After set to 1, never goes back to 0: */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 248 | uint8_t adjtimex_was_done; |
| 249 | |
| 250 | uint8_t discipline_state; // doc calls it c.state |
| 251 | uint8_t poll_exp; // s.poll |
| 252 | int polladj_count; // c.count |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 253 | long kernel_freq_drift; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 254 | double last_update_offset; // c.last |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 255 | double last_update_recv_time; // s.t |
| 256 | double discipline_jitter; // c.jitter |
| 257 | //TODO: add s.jitter - grep for it here and see clock_combine() in doc |
| 258 | #define USING_KERNEL_PLL_LOOP 1 |
| 259 | #if !USING_KERNEL_PLL_LOOP |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 260 | double discipline_freq_drift; // c.freq |
| 261 | //TODO: conditionally calculate wander? it's used only for logging |
| 262 | double discipline_wander; // c.wander |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 263 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 264 | }; |
| 265 | #define G (*ptr_to_globals) |
| 266 | |
| 267 | static const int const_IPTOS_LOWDELAY = IPTOS_LOWDELAY; |
| 268 | |
| 269 | |
Denys Vlasenko | bfc2a32 | 2010-01-01 18:12:06 +0100 | [diff] [blame] | 270 | #define VERB1 if (MAX_VERBOSE && G.verbose) |
| 271 | #define VERB2 if (MAX_VERBOSE >= 2 && G.verbose >= 2) |
| 272 | #define VERB3 if (MAX_VERBOSE >= 3 && G.verbose >= 3) |
| 273 | #define VERB4 if (MAX_VERBOSE >= 4 && G.verbose >= 4) |
| 274 | #define VERB5 if (MAX_VERBOSE >= 5 && G.verbose >= 5) |
| 275 | |
| 276 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 277 | static double LOG2D(int a) |
| 278 | { |
| 279 | if (a < 0) |
| 280 | return 1.0 / (1UL << -a); |
| 281 | return 1UL << a; |
| 282 | } |
| 283 | static ALWAYS_INLINE double SQUARE(double x) |
| 284 | { |
| 285 | return x * x; |
| 286 | } |
| 287 | static ALWAYS_INLINE double MAXD(double a, double b) |
| 288 | { |
| 289 | if (a > b) |
| 290 | return a; |
| 291 | return b; |
| 292 | } |
| 293 | static ALWAYS_INLINE double MIND(double a, double b) |
| 294 | { |
| 295 | if (a < b) |
| 296 | return a; |
| 297 | return b; |
| 298 | } |
| 299 | #define SQRT(x) (sqrt(x)) |
| 300 | |
| 301 | static double |
| 302 | gettime1900d(void) |
| 303 | { |
| 304 | struct timeval tv; |
| 305 | gettimeofday(&tv, NULL); /* never fails */ |
| 306 | return (tv.tv_sec + 1.0e-6 * tv.tv_usec + OFFSET_1900_1970); |
| 307 | } |
| 308 | |
| 309 | static void |
| 310 | d_to_tv(double d, struct timeval *tv) |
| 311 | { |
| 312 | tv->tv_sec = (long)d; |
| 313 | tv->tv_usec = (d - tv->tv_sec) * 1000000; |
| 314 | } |
| 315 | |
| 316 | static double |
| 317 | lfp_to_d(l_fixedpt_t lfp) |
| 318 | { |
| 319 | double ret; |
| 320 | lfp.int_partl = ntohl(lfp.int_partl); |
| 321 | lfp.fractionl = ntohl(lfp.fractionl); |
| 322 | ret = (double)lfp.int_partl + ((double)lfp.fractionl / UINT_MAX); |
| 323 | return ret; |
| 324 | } |
| 325 | static double |
| 326 | sfp_to_d(s_fixedpt_t sfp) |
| 327 | { |
| 328 | double ret; |
| 329 | sfp.int_parts = ntohs(sfp.int_parts); |
| 330 | sfp.fractions = ntohs(sfp.fractions); |
| 331 | ret = (double)sfp.int_parts + ((double)sfp.fractions / USHRT_MAX); |
| 332 | return ret; |
| 333 | } |
| 334 | #if ENABLE_FEATURE_NTPD_SERVER |
| 335 | static l_fixedpt_t |
| 336 | d_to_lfp(double d) |
| 337 | { |
| 338 | l_fixedpt_t lfp; |
| 339 | lfp.int_partl = (uint32_t)d; |
| 340 | lfp.fractionl = (uint32_t)((d - lfp.int_partl) * UINT_MAX); |
| 341 | lfp.int_partl = htonl(lfp.int_partl); |
| 342 | lfp.fractionl = htonl(lfp.fractionl); |
| 343 | return lfp; |
| 344 | } |
| 345 | static s_fixedpt_t |
| 346 | d_to_sfp(double d) |
| 347 | { |
| 348 | s_fixedpt_t sfp; |
| 349 | sfp.int_parts = (uint16_t)d; |
| 350 | sfp.fractions = (uint16_t)((d - sfp.int_parts) * USHRT_MAX); |
| 351 | sfp.int_parts = htons(sfp.int_parts); |
| 352 | sfp.fractions = htons(sfp.fractions); |
| 353 | return sfp; |
| 354 | } |
| 355 | #endif |
| 356 | |
| 357 | static double |
| 358 | dispersion(const datapoint_t *dp, double t) |
| 359 | { |
| 360 | return dp->d_dispersion + FREQ_TOLERANCE * (t - dp->d_recv_time); |
| 361 | } |
| 362 | |
| 363 | static double |
| 364 | root_distance(peer_t *p, double t) |
| 365 | { |
| 366 | /* The root synchronization distance is the maximum error due to |
| 367 | * all causes of the local clock relative to the primary server. |
| 368 | * It is defined as half the total delay plus total dispersion |
| 369 | * plus peer jitter. |
| 370 | */ |
| 371 | return MAXD(MINDISP, p->lastpkt_rootdelay + p->lastpkt_delay) / 2 |
| 372 | + p->lastpkt_rootdisp |
| 373 | + p->filter_dispersion |
| 374 | + FREQ_TOLERANCE * (t - p->lastpkt_recv_time) |
| 375 | + p->filter_jitter; |
| 376 | } |
| 377 | |
| 378 | static void |
| 379 | set_next(peer_t *p, unsigned t) |
| 380 | { |
| 381 | p->next_action_time = time(NULL) + t; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Peer clock filter and its helpers |
| 386 | */ |
| 387 | static void |
| 388 | filter_datapoints(peer_t *p, double t) |
| 389 | { |
| 390 | int i, idx; |
Denys Vlasenko | d9109e3 | 2010-01-02 00:36:43 +0100 | [diff] [blame] | 391 | int got_newest; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 392 | double minoff, maxoff, wavg, sum, w; |
Denys Vlasenko | d9109e3 | 2010-01-02 00:36:43 +0100 | [diff] [blame] | 393 | double x = x; /* for compiler */ |
| 394 | double oldest_off = oldest_off; |
| 395 | double oldest_age = oldest_age; |
| 396 | double newest_off = newest_off; |
| 397 | double newest_age = newest_age; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 398 | |
| 399 | minoff = maxoff = p->filter_datapoint[0].d_offset; |
| 400 | for (i = 1; i < NUM_DATAPOINTS; i++) { |
| 401 | if (minoff > p->filter_datapoint[i].d_offset) |
| 402 | minoff = p->filter_datapoint[i].d_offset; |
| 403 | if (maxoff < p->filter_datapoint[i].d_offset) |
| 404 | maxoff = p->filter_datapoint[i].d_offset; |
| 405 | } |
| 406 | |
| 407 | idx = p->datapoint_idx; /* most recent datapoint */ |
| 408 | /* Average offset: |
| 409 | * Drop two outliers and take weighted average of the rest: |
| 410 | * most_recent/2 + older1/4 + older2/8 ... + older5/32 + older6/32 |
| 411 | * we use older6/32, not older6/64 since sum of weights should be 1: |
| 412 | * 1/2 + 1/4 + 1/8 + 1/16 + 1/32 + 1/32 = 1 |
| 413 | */ |
| 414 | wavg = 0; |
| 415 | w = 0.5; |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 416 | /* n-1 |
| 417 | * --- dispersion(i) |
| 418 | * filter_dispersion = \ ------------- |
| 419 | * / (i+1) |
| 420 | * --- 2 |
| 421 | * i=0 |
| 422 | */ |
Denys Vlasenko | d9109e3 | 2010-01-02 00:36:43 +0100 | [diff] [blame] | 423 | got_newest = 0; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 424 | sum = 0; |
| 425 | for (i = 0; i < NUM_DATAPOINTS; i++) { |
| 426 | VERB4 { |
| 427 | bb_error_msg("datapoint[%d]: off:%f disp:%f(%f) age:%f%s", |
| 428 | i, |
| 429 | p->filter_datapoint[idx].d_offset, |
| 430 | p->filter_datapoint[idx].d_dispersion, dispersion(&p->filter_datapoint[idx], t), |
| 431 | t - p->filter_datapoint[idx].d_recv_time, |
| 432 | (minoff == p->filter_datapoint[idx].d_offset || maxoff == p->filter_datapoint[idx].d_offset) |
| 433 | ? " (outlier by offset)" : "" |
| 434 | ); |
| 435 | } |
| 436 | |
| 437 | sum += dispersion(&p->filter_datapoint[idx], t) / (2 << i); |
| 438 | |
| 439 | if (minoff == p->filter_datapoint[idx].d_offset) { |
Denys Vlasenko | e4844b8 | 2010-01-01 21:59:49 +0100 | [diff] [blame] | 440 | minoff -= 1; /* so that we don't match it ever again */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 441 | } else |
| 442 | if (maxoff == p->filter_datapoint[idx].d_offset) { |
| 443 | maxoff += 1; |
| 444 | } else { |
Denys Vlasenko | d9109e3 | 2010-01-02 00:36:43 +0100 | [diff] [blame] | 445 | oldest_off = p->filter_datapoint[idx].d_offset; |
| 446 | oldest_age = t - p->filter_datapoint[idx].d_recv_time; |
| 447 | if (!got_newest) { |
| 448 | got_newest = 1; |
| 449 | newest_off = oldest_off; |
| 450 | newest_age = oldest_age; |
| 451 | } |
| 452 | x = oldest_off * w; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 453 | wavg += x; |
| 454 | w /= 2; |
| 455 | } |
| 456 | |
| 457 | idx = (idx - 1) & (NUM_DATAPOINTS - 1); |
| 458 | } |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 459 | p->filter_dispersion = sum; |
Denys Vlasenko | d9109e3 | 2010-01-02 00:36:43 +0100 | [diff] [blame] | 460 | wavg += x; /* add another older6/64 to form older6/32 */ |
| 461 | /* Fix systematic underestimation with large poll intervals. |
| 462 | * Imagine that we still have a bit of uncorrected drift, |
| 463 | * and poll interval is big (say, 100 sec). Offsets form a progression: |
| 464 | * 0.0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 - 0.7 is most recent. |
| 465 | * The algorithm above drops 0.0 and 0.7 as outliers, |
| 466 | * and then we have this estimation, ~25% off from 0.7: |
| 467 | * 0.1/32 + 0.2/32 + 0.3/16 + 0.4/8 + 0.5/4 + 0.6/2 = 0.503125 |
| 468 | */ |
| 469 | x = newest_age / (oldest_age - newest_age); /* in above example, 100 / (600 - 100) */ |
| 470 | if (x < 1) { |
| 471 | x = (newest_off - oldest_off) * x; /* 0.5 * 100/500 = 0.1 */ |
| 472 | wavg += x; |
| 473 | } |
| 474 | p->filter_offset = wavg; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 475 | |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 476 | /* +----- -----+ ^ 1/2 |
| 477 | * | n-1 | |
| 478 | * | --- | |
| 479 | * | 1 \ 2 | |
| 480 | * filter_jitter = | --- * / (avg-offset_j) | |
| 481 | * | n --- | |
| 482 | * | j=0 | |
| 483 | * +----- -----+ |
| 484 | * where n is the number of valid datapoints in the filter (n > 1); |
| 485 | * if filter_jitter < precision then filter_jitter = precision |
| 486 | */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 487 | sum = 0; |
| 488 | for (i = 0; i < NUM_DATAPOINTS; i++) { |
| 489 | sum += SQUARE(wavg - p->filter_datapoint[i].d_offset); |
| 490 | } |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 491 | sum = SQRT(sum / NUM_DATAPOINTS); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 492 | p->filter_jitter = sum > G_precision_sec ? sum : G_precision_sec; |
| 493 | |
Denys Vlasenko | d9109e3 | 2010-01-02 00:36:43 +0100 | [diff] [blame] | 494 | VERB3 bb_error_msg("filter offset:%f(corr:%e) disp:%f jitter:%f", |
| 495 | p->filter_offset, x, |
| 496 | p->filter_dispersion, |
| 497 | p->filter_jitter); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 498 | |
| 499 | } |
| 500 | |
| 501 | static void |
| 502 | reset_peer_stats(peer_t *p, double t, double offset) |
| 503 | { |
| 504 | int i; |
| 505 | for (i = 0; i < NUM_DATAPOINTS; i++) { |
| 506 | if (offset < 16 * STEP_THRESHOLD) { |
| 507 | p->filter_datapoint[i].d_recv_time -= offset; |
| 508 | if (p->filter_datapoint[i].d_offset != 0) { |
| 509 | p->filter_datapoint[i].d_offset -= offset; |
| 510 | } |
| 511 | } else { |
| 512 | p->filter_datapoint[i].d_recv_time = t; |
| 513 | p->filter_datapoint[i].d_offset = 0; |
| 514 | p->filter_datapoint[i].d_dispersion = MAXDISP; |
| 515 | } |
| 516 | } |
| 517 | if (offset < 16 * STEP_THRESHOLD) { |
| 518 | p->lastpkt_recv_time -= offset; |
| 519 | } else { |
| 520 | p->p_reachable_bits = 0; |
| 521 | p->lastpkt_recv_time = t; |
| 522 | } |
| 523 | filter_datapoints(p, t); /* recalc p->filter_xxx */ |
| 524 | p->next_action_time -= (time_t)offset; |
| 525 | VERB5 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time); |
| 526 | } |
| 527 | |
| 528 | static void |
| 529 | add_peers(char *s) |
| 530 | { |
| 531 | peer_t *p; |
| 532 | |
| 533 | p = xzalloc(sizeof(*p)); |
| 534 | p->p_lsa = xhost2sockaddr(s, 123); |
| 535 | p->p_dotted = xmalloc_sockaddr2dotted_noport(&p->p_lsa->u.sa); |
| 536 | p->p_fd = -1; |
| 537 | p->p_xmt_msg.m_status = MODE_CLIENT | (NTP_VERSION << 3); |
| 538 | p->next_action_time = time(NULL); /* = set_next(p, 0); */ |
| 539 | reset_peer_stats(p, gettime1900d(), 16 * STEP_THRESHOLD); |
| 540 | /* Speed up initial sync: with small offsets from peers, |
| 541 | * 3 samples will sync |
| 542 | */ |
| 543 | p->filter_datapoint[6].d_dispersion = 0; |
| 544 | p->filter_datapoint[7].d_dispersion = 0; |
| 545 | |
| 546 | llist_add_to(&G.ntp_peers, p); |
| 547 | G.peer_cnt++; |
| 548 | } |
| 549 | |
| 550 | static int |
| 551 | do_sendto(int fd, |
| 552 | const struct sockaddr *from, const struct sockaddr *to, socklen_t addrlen, |
| 553 | msg_t *msg, ssize_t len) |
| 554 | { |
| 555 | ssize_t ret; |
| 556 | |
| 557 | errno = 0; |
| 558 | if (!from) { |
| 559 | ret = sendto(fd, msg, len, MSG_DONTWAIT, to, addrlen); |
| 560 | } else { |
| 561 | ret = send_to_from(fd, msg, len, MSG_DONTWAIT, to, from, addrlen); |
| 562 | } |
| 563 | if (ret != len) { |
| 564 | bb_perror_msg("send failed"); |
| 565 | return -1; |
| 566 | } |
| 567 | return 0; |
| 568 | } |
| 569 | |
| 570 | static int |
| 571 | send_query_to_peer(peer_t *p) |
| 572 | { |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 573 | /* Why do we need to bind()? |
| 574 | * See what happens when we don't bind: |
| 575 | * |
| 576 | * socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 3 |
| 577 | * setsockopt(3, SOL_IP, IP_TOS, [16], 4) = 0 |
| 578 | * gettimeofday({1259071266, 327885}, NULL) = 0 |
| 579 | * sendto(3, "xxx", 48, MSG_DONTWAIT, {sa_family=AF_INET, sin_port=htons(123), sin_addr=inet_addr("10.34.32.125")}, 16) = 48 |
| 580 | * ^^^ we sent it from some source port picked by kernel. |
| 581 | * time(NULL) = 1259071266 |
| 582 | * write(2, "ntpd: entering poll 15 secs\n", 28) = 28 |
| 583 | * poll([{fd=3, events=POLLIN}], 1, 15000) = 1 ([{fd=3, revents=POLLIN}]) |
| 584 | * recv(3, "yyy", 68, MSG_DONTWAIT) = 48 |
| 585 | * ^^^ this recv will receive packets to any local port! |
| 586 | * |
| 587 | * Uncomment this and use strace to see it in action: |
| 588 | */ |
| 589 | #define PROBE_LOCAL_ADDR /* { len_and_sockaddr lsa; lsa.len = LSA_SIZEOF_SA; getsockname(p->query.fd, &lsa.u.sa, &lsa.len); } */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 590 | |
| 591 | if (p->p_fd == -1) { |
| 592 | int fd, family; |
| 593 | len_and_sockaddr *local_lsa; |
| 594 | |
| 595 | family = p->p_lsa->u.sa.sa_family; |
| 596 | p->p_fd = fd = xsocket_type(&local_lsa, family, SOCK_DGRAM); |
| 597 | /* local_lsa has "null" address and port 0 now. |
| 598 | * bind() ensures we have a *particular port* selected by kernel |
| 599 | * and remembered in p->p_fd, thus later recv(p->p_fd) |
| 600 | * receives only packets sent to this port. |
| 601 | */ |
| 602 | PROBE_LOCAL_ADDR |
| 603 | xbind(fd, &local_lsa->u.sa, local_lsa->len); |
| 604 | PROBE_LOCAL_ADDR |
| 605 | #if ENABLE_FEATURE_IPV6 |
| 606 | if (family == AF_INET) |
| 607 | #endif |
| 608 | setsockopt(fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY)); |
| 609 | free(local_lsa); |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | * Send out a random 64-bit number as our transmit time. The NTP |
| 614 | * server will copy said number into the originate field on the |
| 615 | * response that it sends us. This is totally legal per the SNTP spec. |
| 616 | * |
| 617 | * The impact of this is two fold: we no longer send out the current |
| 618 | * system time for the world to see (which may aid an attacker), and |
| 619 | * it gives us a (not very secure) way of knowing that we're not |
| 620 | * getting spoofed by an attacker that can't capture our traffic |
| 621 | * but can spoof packets from the NTP server we're communicating with. |
| 622 | * |
| 623 | * Save the real transmit timestamp locally. |
| 624 | */ |
| 625 | p->p_xmt_msg.m_xmttime.int_partl = random(); |
| 626 | p->p_xmt_msg.m_xmttime.fractionl = random(); |
| 627 | p->p_xmttime = gettime1900d(); |
| 628 | |
| 629 | if (do_sendto(p->p_fd, /*from:*/ NULL, /*to:*/ &p->p_lsa->u.sa, /*addrlen:*/ p->p_lsa->len, |
| 630 | &p->p_xmt_msg, NTP_MSGSIZE_NOAUTH) == -1 |
| 631 | ) { |
| 632 | close(p->p_fd); |
| 633 | p->p_fd = -1; |
| 634 | set_next(p, RETRY_INTERVAL); |
| 635 | return -1; |
| 636 | } |
| 637 | |
| 638 | p->p_reachable_bits <<= 1; |
| 639 | VERB1 bb_error_msg("sent query to %s", p->p_dotted); |
| 640 | set_next(p, QUERYTIME_MAX); |
| 641 | |
| 642 | return 0; |
| 643 | } |
| 644 | |
| 645 | |
| 646 | static void |
| 647 | step_time(double offset) |
| 648 | { |
| 649 | double dtime; |
| 650 | struct timeval tv; |
| 651 | char buf[80]; |
| 652 | time_t tval; |
| 653 | |
| 654 | gettimeofday(&tv, NULL); /* never fails */ |
| 655 | dtime = offset + tv.tv_sec; |
| 656 | dtime += 1.0e-6 * tv.tv_usec; |
| 657 | d_to_tv(dtime, &tv); |
| 658 | |
| 659 | if (settimeofday(&tv, NULL) == -1) |
| 660 | bb_perror_msg_and_die("settimeofday"); |
| 661 | |
| 662 | tval = tv.tv_sec; |
| 663 | strftime(buf, sizeof(buf), "%a %b %e %H:%M:%S %Z %Y", localtime(&tval)); |
| 664 | |
| 665 | bb_error_msg("setting clock to %s (offset %fs)", buf, offset); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | |
| 669 | /* |
| 670 | * Selection and clustering, and their helpers |
| 671 | */ |
| 672 | typedef struct { |
| 673 | peer_t *p; |
| 674 | int type; |
| 675 | double edge; |
| 676 | } point_t; |
| 677 | static int |
| 678 | compare_point_edge(const void *aa, const void *bb) |
| 679 | { |
| 680 | const point_t *a = aa; |
| 681 | const point_t *b = bb; |
| 682 | if (a->edge < b->edge) { |
| 683 | return -1; |
| 684 | } |
| 685 | return (a->edge > b->edge); |
| 686 | } |
| 687 | typedef struct { |
| 688 | peer_t *p; |
| 689 | double metric; |
| 690 | } survivor_t; |
| 691 | static int |
| 692 | compare_survivor_metric(const void *aa, const void *bb) |
| 693 | { |
| 694 | const survivor_t *a = aa; |
| 695 | const survivor_t *b = bb; |
| 696 | if (a->metric < b->metric) |
| 697 | return -1; |
| 698 | return (a->metric > b->metric); |
| 699 | } |
| 700 | static int |
| 701 | fit(peer_t *p, double rd) |
| 702 | { |
| 703 | if (p->p_reachable_bits == 0) { |
| 704 | VERB3 bb_error_msg("peer %s unfit for selection: unreachable", p->p_dotted); |
| 705 | return 0; |
| 706 | } |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 707 | #if 0 /* we filter out such packets earlier */ |
| 708 | if ((p->lastpkt_status & LI_ALARM) == LI_ALARM |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 709 | || p->lastpkt_stratum >= MAXSTRAT |
| 710 | ) { |
| 711 | VERB3 bb_error_msg("peer %s unfit for selection: bad status/stratum", p->p_dotted); |
| 712 | return 0; |
| 713 | } |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 714 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 715 | /* rd is root_distance(p, t) */ |
| 716 | if (rd > MAXDIST + FREQ_TOLERANCE * (1 << G.poll_exp)) { |
| 717 | VERB3 bb_error_msg("peer %s unfit for selection: root distance too high", p->p_dotted); |
| 718 | return 0; |
| 719 | } |
| 720 | //TODO |
| 721 | // /* Do we have a loop? */ |
| 722 | // if (p->refid == p->dstaddr || p->refid == s.refid) |
| 723 | // return 0; |
| 724 | return 1; |
| 725 | } |
| 726 | static peer_t* |
| 727 | select_and_cluster(double t) |
| 728 | { |
| 729 | llist_t *item; |
| 730 | int i, j; |
| 731 | int size = 3 * G.peer_cnt; |
| 732 | /* for selection algorithm */ |
| 733 | point_t point[size]; |
| 734 | unsigned num_points, num_candidates; |
| 735 | double low, high; |
| 736 | unsigned num_falsetickers; |
| 737 | /* for cluster algorithm */ |
| 738 | survivor_t survivor[size]; |
| 739 | unsigned num_survivors; |
| 740 | |
| 741 | /* Selection */ |
| 742 | |
| 743 | num_points = 0; |
| 744 | item = G.ntp_peers; |
| 745 | while (item != NULL) { |
| 746 | peer_t *p = (peer_t *) item->data; |
| 747 | double rd = root_distance(p, t); |
| 748 | double offset = p->filter_offset; |
| 749 | |
| 750 | if (!fit(p, rd)) { |
| 751 | item = item->link; |
| 752 | continue; |
| 753 | } |
| 754 | |
| 755 | VERB4 bb_error_msg("interval: [%f %f %f] %s", |
| 756 | offset - rd, |
| 757 | offset, |
| 758 | offset + rd, |
| 759 | p->p_dotted |
| 760 | ); |
| 761 | point[num_points].p = p; |
| 762 | point[num_points].type = -1; |
| 763 | point[num_points].edge = offset - rd; |
| 764 | num_points++; |
| 765 | point[num_points].p = p; |
| 766 | point[num_points].type = 0; |
| 767 | point[num_points].edge = offset; |
| 768 | num_points++; |
| 769 | point[num_points].p = p; |
| 770 | point[num_points].type = 1; |
| 771 | point[num_points].edge = offset + rd; |
| 772 | num_points++; |
| 773 | item = item->link; |
| 774 | } |
| 775 | num_candidates = num_points / 3; |
| 776 | if (num_candidates == 0) { |
| 777 | VERB3 bb_error_msg("no valid datapoints, no peer selected"); |
| 778 | return NULL; /* never happers? */ |
| 779 | } |
| 780 | //TODO: sorting does not seem to be done in reference code |
| 781 | qsort(point, num_points, sizeof(point[0]), compare_point_edge); |
| 782 | |
| 783 | /* Start with the assumption that there are no falsetickers. |
| 784 | * Attempt to find a nonempty intersection interval containing |
| 785 | * the midpoints of all truechimers. |
| 786 | * If a nonempty interval cannot be found, increase the number |
| 787 | * of assumed falsetickers by one and try again. |
| 788 | * If a nonempty interval is found and the number of falsetickers |
| 789 | * is less than the number of truechimers, a majority has been found |
| 790 | * and the midpoint of each truechimer represents |
| 791 | * the candidates available to the cluster algorithm. |
| 792 | */ |
| 793 | num_falsetickers = 0; |
| 794 | while (1) { |
| 795 | int c; |
| 796 | unsigned num_midpoints = 0; |
| 797 | |
| 798 | low = 1 << 9; |
| 799 | high = - (1 << 9); |
| 800 | c = 0; |
| 801 | for (i = 0; i < num_points; i++) { |
| 802 | /* We want to do: |
| 803 | * if (point[i].type == -1) c++; |
| 804 | * if (point[i].type == 1) c--; |
| 805 | * and it's simpler to do it this way: |
| 806 | */ |
| 807 | c -= point[i].type; |
| 808 | if (c >= num_candidates - num_falsetickers) { |
| 809 | /* If it was c++ and it got big enough... */ |
| 810 | low = point[i].edge; |
| 811 | break; |
| 812 | } |
| 813 | if (point[i].type == 0) |
| 814 | num_midpoints++; |
| 815 | } |
| 816 | c = 0; |
| 817 | for (i = num_points-1; i >= 0; i--) { |
| 818 | c += point[i].type; |
| 819 | if (c >= num_candidates - num_falsetickers) { |
| 820 | high = point[i].edge; |
| 821 | break; |
| 822 | } |
| 823 | if (point[i].type == 0) |
| 824 | num_midpoints++; |
| 825 | } |
| 826 | /* If the number of midpoints is greater than the number |
| 827 | * of allowed falsetickers, the intersection contains at |
| 828 | * least one truechimer with no midpoint - bad. |
| 829 | * Also, interval should be nonempty. |
| 830 | */ |
| 831 | if (num_midpoints <= num_falsetickers && low < high) |
| 832 | break; |
| 833 | num_falsetickers++; |
| 834 | if (num_falsetickers * 2 >= num_candidates) { |
| 835 | VERB3 bb_error_msg("too many falsetickers:%d (candidates:%d), no peer selected", |
| 836 | num_falsetickers, num_candidates); |
| 837 | return NULL; |
| 838 | } |
| 839 | } |
| 840 | VERB3 bb_error_msg("selected interval: [%f, %f]; candidates:%d falsetickers:%d", |
| 841 | low, high, num_candidates, num_falsetickers); |
| 842 | |
| 843 | /* Clustering */ |
| 844 | |
| 845 | /* Construct a list of survivors (p, metric) |
| 846 | * from the chime list, where metric is dominated |
| 847 | * first by stratum and then by root distance. |
| 848 | * All other things being equal, this is the order of preference. |
| 849 | */ |
| 850 | num_survivors = 0; |
| 851 | for (i = 0; i < num_points; i++) { |
| 852 | peer_t *p; |
| 853 | |
| 854 | if (point[i].edge < low || point[i].edge > high) |
| 855 | continue; |
| 856 | p = point[i].p; |
| 857 | survivor[num_survivors].p = p; |
| 858 | //TODO: save root_distance in point_t and reuse here? |
| 859 | survivor[num_survivors].metric = MAXDIST * p->lastpkt_stratum + root_distance(p, t); |
| 860 | VERB4 bb_error_msg("survivor[%d] metric:%f peer:%s", |
| 861 | num_survivors, survivor[num_survivors].metric, p->p_dotted); |
| 862 | num_survivors++; |
| 863 | } |
| 864 | /* There must be at least MIN_SELECTED survivors to satisfy the |
| 865 | * correctness assertions. Ordinarily, the Byzantine criteria |
| 866 | * require four survivors, but for the demonstration here, one |
| 867 | * is acceptable. |
| 868 | */ |
| 869 | if (num_survivors < MIN_SELECTED) { |
| 870 | VERB3 bb_error_msg("num_survivors %d < %d, no peer selected", |
| 871 | num_survivors, MIN_SELECTED); |
| 872 | return NULL; |
| 873 | } |
| 874 | |
| 875 | //looks like this is ONLY used by the fact that later we pick survivor[0]. |
| 876 | //we can avoid sorting then, just find the minimum once! |
| 877 | qsort(survivor, num_survivors, sizeof(survivor[0]), compare_survivor_metric); |
| 878 | |
| 879 | /* For each association p in turn, calculate the selection |
| 880 | * jitter p->sjitter as the square root of the sum of squares |
| 881 | * (p->offset - q->offset) over all q associations. The idea is |
| 882 | * to repeatedly discard the survivor with maximum selection |
| 883 | * jitter until a termination condition is met. |
| 884 | */ |
| 885 | while (1) { |
| 886 | unsigned max_idx = max_idx; |
| 887 | double max_selection_jitter = max_selection_jitter; |
| 888 | double min_jitter = min_jitter; |
| 889 | |
| 890 | if (num_survivors <= MIN_CLUSTERED) { |
| 891 | bb_error_msg("num_survivors %d <= %d, not discarding more", |
| 892 | num_survivors, MIN_CLUSTERED); |
| 893 | break; |
| 894 | } |
| 895 | |
| 896 | /* To make sure a few survivors are left |
| 897 | * for the clustering algorithm to chew on, |
| 898 | * we stop if the number of survivors |
| 899 | * is less than or equal to MIN_CLUSTERED (3). |
| 900 | */ |
| 901 | for (i = 0; i < num_survivors; i++) { |
| 902 | double selection_jitter_sq; |
| 903 | peer_t *p = survivor[i].p; |
| 904 | |
| 905 | if (i == 0 || p->filter_jitter < min_jitter) |
| 906 | min_jitter = p->filter_jitter; |
| 907 | |
| 908 | selection_jitter_sq = 0; |
| 909 | for (j = 0; j < num_survivors; j++) { |
| 910 | peer_t *q = survivor[j].p; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 911 | selection_jitter_sq += SQUARE(p->filter_offset - q->filter_offset); |
| 912 | } |
| 913 | if (i == 0 || selection_jitter_sq > max_selection_jitter) { |
| 914 | max_selection_jitter = selection_jitter_sq; |
| 915 | max_idx = i; |
| 916 | } |
| 917 | VERB5 bb_error_msg("survivor %d selection_jitter^2:%f", |
| 918 | i, selection_jitter_sq); |
| 919 | } |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 920 | max_selection_jitter = SQRT(max_selection_jitter / num_survivors); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 921 | VERB4 bb_error_msg("max_selection_jitter (at %d):%f min_jitter:%f", |
| 922 | max_idx, max_selection_jitter, min_jitter); |
| 923 | |
| 924 | /* If the maximum selection jitter is less than the |
| 925 | * minimum peer jitter, then tossing out more survivors |
| 926 | * will not lower the minimum peer jitter, so we might |
| 927 | * as well stop. |
| 928 | */ |
| 929 | if (max_selection_jitter < min_jitter) { |
| 930 | VERB3 bb_error_msg("max_selection_jitter:%f < min_jitter:%f, num_survivors:%d, not discarding more", |
| 931 | max_selection_jitter, min_jitter, num_survivors); |
| 932 | break; |
| 933 | } |
| 934 | |
| 935 | /* Delete survivor[max_idx] from the list |
| 936 | * and go around again. |
| 937 | */ |
| 938 | VERB5 bb_error_msg("dropping survivor %d", max_idx); |
| 939 | num_survivors--; |
| 940 | while (max_idx < num_survivors) { |
| 941 | survivor[max_idx] = survivor[max_idx + 1]; |
| 942 | max_idx++; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | /* Pick the best clock. If the old system peer is on the list |
| 947 | * and at the same stratum as the first survivor on the list, |
| 948 | * then don't do a clock hop. Otherwise, select the first |
| 949 | * survivor on the list as the new system peer. |
| 950 | */ |
| 951 | //TODO - see clock_combine() |
| 952 | VERB3 bb_error_msg("selected peer %s filter_offset:%f age:%f", |
| 953 | survivor[0].p->p_dotted, |
| 954 | survivor[0].p->filter_offset, |
| 955 | t - survivor[0].p->lastpkt_recv_time |
| 956 | ); |
| 957 | return survivor[0].p; |
| 958 | } |
| 959 | |
| 960 | |
| 961 | /* |
| 962 | * Local clock discipline and its helpers |
| 963 | */ |
| 964 | static void |
| 965 | set_new_values(int disc_state, double offset, double recv_time) |
| 966 | { |
| 967 | /* Enter new state and set state variables. Note we use the time |
| 968 | * of the last clock filter sample, which must be earlier than |
| 969 | * the current time. |
| 970 | */ |
Denys Vlasenko | d9109e3 | 2010-01-02 00:36:43 +0100 | [diff] [blame] | 971 | VERB3 bb_error_msg("disc_state=%d last update offset=%f recv_time=%f", |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 972 | disc_state, offset, recv_time); |
| 973 | G.discipline_state = disc_state; |
| 974 | G.last_update_offset = offset; |
| 975 | G.last_update_recv_time = recv_time; |
| 976 | } |
| 977 | /* Clock state definitions */ |
| 978 | #define STATE_NSET 0 /* initial state, "nothing is set" */ |
| 979 | #define STATE_FSET 1 /* frequency set from file */ |
| 980 | #define STATE_SPIK 2 /* spike detected */ |
| 981 | #define STATE_FREQ 3 /* initial frequency */ |
| 982 | #define STATE_SYNC 4 /* clock synchronized (normal operation) */ |
| 983 | /* Return: -1: decrease poll interval, 0: leave as is, 1: increase */ |
| 984 | static int |
| 985 | update_local_clock(peer_t *p, double t) |
| 986 | { |
| 987 | int rc; |
| 988 | long old_tmx_offset; |
| 989 | struct timex tmx; |
| 990 | double offset = p->filter_offset; |
| 991 | double recv_time = p->lastpkt_recv_time; |
| 992 | double abs_offset; |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 993 | #if !USING_KERNEL_PLL_LOOP |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 994 | double freq_drift; |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 995 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 996 | double since_last_update; |
| 997 | double etemp, dtemp; |
| 998 | |
| 999 | abs_offset = fabs(offset); |
| 1000 | |
| 1001 | /* If the offset is too large, give up and go home */ |
| 1002 | if (abs_offset > PANIC_THRESHOLD) { |
| 1003 | bb_error_msg_and_die("offset %f far too big, exiting", offset); |
| 1004 | } |
| 1005 | |
| 1006 | /* If this is an old update, for instance as the result |
| 1007 | * of a system peer change, avoid it. We never use |
| 1008 | * an old sample or the same sample twice. |
| 1009 | */ |
| 1010 | if (recv_time <= G.last_update_recv_time) { |
| 1011 | VERB3 bb_error_msg("same or older datapoint: %f >= %f, not using it", |
| 1012 | G.last_update_recv_time, recv_time); |
| 1013 | return 0; /* "leave poll interval as is" */ |
| 1014 | } |
| 1015 | |
| 1016 | /* Clock state machine transition function. This is where the |
| 1017 | * action is and defines how the system reacts to large time |
| 1018 | * and frequency errors. |
| 1019 | */ |
| 1020 | since_last_update = recv_time - G.reftime; |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 1021 | #if !USING_KERNEL_PLL_LOOP |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1022 | freq_drift = 0; |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 1023 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1024 | if (G.discipline_state == STATE_FREQ) { |
| 1025 | /* Ignore updates until the stepout threshold */ |
| 1026 | if (since_last_update < WATCH_THRESHOLD) { |
| 1027 | VERB3 bb_error_msg("measuring drift, datapoint ignored, %f sec remains", |
| 1028 | WATCH_THRESHOLD - since_last_update); |
| 1029 | return 0; /* "leave poll interval as is" */ |
| 1030 | } |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 1031 | #if !USING_KERNEL_PLL_LOOP |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1032 | freq_drift = (offset - G.last_update_offset) / since_last_update; |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 1033 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1034 | } |
| 1035 | |
| 1036 | /* There are two main regimes: when the |
| 1037 | * offset exceeds the step threshold and when it does not. |
| 1038 | */ |
| 1039 | if (abs_offset > STEP_THRESHOLD) { |
| 1040 | llist_t *item; |
| 1041 | |
| 1042 | switch (G.discipline_state) { |
| 1043 | case STATE_SYNC: |
| 1044 | /* The first outlyer: ignore it, switch to SPIK state */ |
| 1045 | VERB3 bb_error_msg("offset:%f - spike detected", offset); |
| 1046 | G.discipline_state = STATE_SPIK; |
| 1047 | return -1; /* "decrease poll interval" */ |
| 1048 | |
| 1049 | case STATE_SPIK: |
| 1050 | /* Ignore succeeding outlyers until either an inlyer |
| 1051 | * is found or the stepout threshold is exceeded. |
| 1052 | */ |
| 1053 | if (since_last_update < WATCH_THRESHOLD) { |
| 1054 | VERB3 bb_error_msg("spike detected, datapoint ignored, %f sec remains", |
| 1055 | WATCH_THRESHOLD - since_last_update); |
| 1056 | return -1; /* "decrease poll interval" */ |
| 1057 | } |
| 1058 | /* fall through: we need to step */ |
| 1059 | } /* switch */ |
| 1060 | |
| 1061 | /* Step the time and clamp down the poll interval. |
| 1062 | * |
| 1063 | * In NSET state an initial frequency correction is |
| 1064 | * not available, usually because the frequency file has |
| 1065 | * not yet been written. Since the time is outside the |
| 1066 | * capture range, the clock is stepped. The frequency |
| 1067 | * will be set directly following the stepout interval. |
| 1068 | * |
| 1069 | * In FSET state the initial frequency has been set |
| 1070 | * from the frequency file. Since the time is outside |
| 1071 | * the capture range, the clock is stepped immediately, |
| 1072 | * rather than after the stepout interval. Guys get |
| 1073 | * nervous if it takes 17 minutes to set the clock for |
| 1074 | * the first time. |
| 1075 | * |
| 1076 | * In SPIK state the stepout threshold has expired and |
| 1077 | * the phase is still above the step threshold. Note |
| 1078 | * that a single spike greater than the step threshold |
| 1079 | * is always suppressed, even at the longer poll |
| 1080 | * intervals. |
| 1081 | */ |
| 1082 | VERB3 bb_error_msg("stepping time by %f; poll_exp=MINPOLL", offset); |
| 1083 | step_time(offset); |
| 1084 | if (option_mask32 & OPT_q) { |
| 1085 | /* We were only asked to set time once. Done. */ |
| 1086 | exit(0); |
| 1087 | } |
| 1088 | |
| 1089 | G.polladj_count = 0; |
| 1090 | G.poll_exp = MINPOLL; |
| 1091 | G.stratum = MAXSTRAT; |
| 1092 | for (item = G.ntp_peers; item != NULL; item = item->link) { |
| 1093 | peer_t *pp = (peer_t *) item->data; |
| 1094 | reset_peer_stats(pp, t, offset); |
| 1095 | } |
| 1096 | if (G.discipline_state == STATE_NSET) { |
| 1097 | set_new_values(STATE_FREQ, /*offset:*/ 0, recv_time); |
| 1098 | return 1; /* "ok to increase poll interval" */ |
| 1099 | } |
| 1100 | set_new_values(STATE_SYNC, /*offset:*/ 0, recv_time); |
| 1101 | |
| 1102 | } else { /* abs_offset <= STEP_THRESHOLD */ |
| 1103 | |
| 1104 | if (G.poll_exp < MINPOLL) { |
| 1105 | VERB3 bb_error_msg("saw small offset %f, disabling burst mode", offset); |
| 1106 | G.poll_exp = MINPOLL; |
| 1107 | } |
| 1108 | |
| 1109 | /* Compute the clock jitter as the RMS of exponentially |
| 1110 | * weighted offset differences. Used by the poll adjust code. |
| 1111 | */ |
| 1112 | etemp = SQUARE(G.discipline_jitter); |
| 1113 | dtemp = SQUARE(MAXD(fabs(offset - G.last_update_offset), G_precision_sec)); |
| 1114 | G.discipline_jitter = SQRT(etemp + (dtemp - etemp) / AVG); |
| 1115 | VERB3 bb_error_msg("discipline jitter=%f", G.discipline_jitter); |
| 1116 | |
| 1117 | switch (G.discipline_state) { |
| 1118 | case STATE_NSET: |
| 1119 | if (option_mask32 & OPT_q) { |
| 1120 | /* We were only asked to set time once. |
| 1121 | * The clock is precise enough, no need to step. |
| 1122 | */ |
| 1123 | exit(0); |
| 1124 | } |
| 1125 | /* This is the first update received and the frequency |
| 1126 | * has not been initialized. The first thing to do |
| 1127 | * is directly measure the oscillator frequency. |
| 1128 | */ |
| 1129 | set_new_values(STATE_FREQ, offset, recv_time); |
| 1130 | VERB3 bb_error_msg("transitioning to FREQ, datapoint ignored"); |
| 1131 | return -1; /* "decrease poll interval" */ |
| 1132 | |
| 1133 | #if 0 /* this is dead code for now */ |
| 1134 | case STATE_FSET: |
| 1135 | /* This is the first update and the frequency |
| 1136 | * has been initialized. Adjust the phase, but |
| 1137 | * don't adjust the frequency until the next update. |
| 1138 | */ |
| 1139 | set_new_values(STATE_SYNC, offset, recv_time); |
| 1140 | /* freq_drift remains 0 */ |
| 1141 | break; |
| 1142 | #endif |
| 1143 | |
| 1144 | case STATE_FREQ: |
| 1145 | /* since_last_update >= WATCH_THRESHOLD, we waited enough. |
| 1146 | * Correct the phase and frequency and switch to SYNC state. |
| 1147 | * freq_drift was already estimated (see code above) |
| 1148 | */ |
| 1149 | set_new_values(STATE_SYNC, offset, recv_time); |
| 1150 | break; |
| 1151 | |
| 1152 | default: |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 1153 | #if !USING_KERNEL_PLL_LOOP |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1154 | /* Compute freq_drift due to PLL and FLL contributions. |
| 1155 | * |
| 1156 | * The FLL and PLL frequency gain constants |
| 1157 | * depend on the poll interval and Allan |
| 1158 | * intercept. The FLL is not used below one-half |
| 1159 | * the Allan intercept. Above that the loop gain |
| 1160 | * increases in steps to 1 / AVG. |
| 1161 | */ |
| 1162 | if ((1 << G.poll_exp) > ALLAN / 2) { |
| 1163 | etemp = FLL - G.poll_exp; |
| 1164 | if (etemp < AVG) |
| 1165 | etemp = AVG; |
| 1166 | freq_drift += (offset - G.last_update_offset) / (MAXD(since_last_update, ALLAN) * etemp); |
| 1167 | } |
| 1168 | /* For the PLL the integration interval |
| 1169 | * (numerator) is the minimum of the update |
| 1170 | * interval and poll interval. This allows |
| 1171 | * oversampling, but not undersampling. |
| 1172 | */ |
| 1173 | etemp = MIND(since_last_update, (1 << G.poll_exp)); |
| 1174 | dtemp = (4 * PLL) << G.poll_exp; |
| 1175 | freq_drift += offset * etemp / SQUARE(dtemp); |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 1176 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1177 | set_new_values(STATE_SYNC, offset, recv_time); |
| 1178 | break; |
| 1179 | } |
| 1180 | G.stratum = p->lastpkt_stratum + 1; |
| 1181 | } |
| 1182 | |
| 1183 | G.reftime = t; |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 1184 | G.ntp_status = p->lastpkt_status; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1185 | G.refid = p->lastpkt_refid; |
| 1186 | G.rootdelay = p->lastpkt_rootdelay + p->lastpkt_delay; |
| 1187 | dtemp = p->filter_jitter; // SQRT(SQUARE(p->filter_jitter) + SQUARE(s.jitter)); |
| 1188 | dtemp += MAXD(p->filter_dispersion + FREQ_TOLERANCE * (t - p->lastpkt_recv_time) + abs_offset, MINDISP); |
| 1189 | G.rootdisp = p->lastpkt_rootdisp + dtemp; |
| 1190 | VERB3 bb_error_msg("updating leap/refid/reftime/rootdisp from peer %s", p->p_dotted); |
| 1191 | |
| 1192 | /* We are in STATE_SYNC now, but did not do adjtimex yet. |
| 1193 | * (Any other state does not reach this, they all return earlier) |
| 1194 | * By this time, freq_drift and G.last_update_offset are set |
| 1195 | * to values suitable for adjtimex. |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 1196 | */ |
| 1197 | #if !USING_KERNEL_PLL_LOOP |
| 1198 | /* Calculate the new frequency drift and frequency stability (wander). |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1199 | * Compute the clock wander as the RMS of exponentially weighted |
| 1200 | * frequency differences. This is not used directly, but can, |
| 1201 | * along with the jitter, be a highly useful monitoring and |
| 1202 | * debugging tool. |
| 1203 | */ |
| 1204 | dtemp = G.discipline_freq_drift + freq_drift; |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 1205 | G.discipline_freq_drift = MAXD(MIND(MAXDRIFT, dtemp), -MAXDRIFT); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1206 | etemp = SQUARE(G.discipline_wander); |
| 1207 | dtemp = SQUARE(dtemp); |
| 1208 | G.discipline_wander = SQRT(etemp + (dtemp - etemp) / AVG); |
| 1209 | |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 1210 | VERB3 bb_error_msg("discipline freq_drift=%.9f(int:%ld corr:%e) wander=%f", |
| 1211 | G.discipline_freq_drift, |
| 1212 | (long)(G.discipline_freq_drift * 65536e6), |
| 1213 | freq_drift, |
| 1214 | G.discipline_wander); |
| 1215 | #endif |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1216 | VERB3 { |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1217 | memset(&tmx, 0, sizeof(tmx)); |
| 1218 | if (adjtimex(&tmx) < 0) |
| 1219 | bb_perror_msg_and_die("adjtimex"); |
| 1220 | VERB3 bb_error_msg("p adjtimex freq:%ld offset:%ld constant:%ld status:0x%x", |
| 1221 | tmx.freq, tmx.offset, tmx.constant, tmx.status); |
| 1222 | } |
| 1223 | |
| 1224 | old_tmx_offset = 0; |
| 1225 | if (!G.adjtimex_was_done) { |
| 1226 | G.adjtimex_was_done = 1; |
| 1227 | /* When we use adjtimex for the very first time, |
| 1228 | * we need to ADD to pre-existing tmx.offset - it may be !0 |
| 1229 | */ |
| 1230 | memset(&tmx, 0, sizeof(tmx)); |
| 1231 | if (adjtimex(&tmx) < 0) |
| 1232 | bb_perror_msg_and_die("adjtimex"); |
| 1233 | old_tmx_offset = tmx.offset; |
| 1234 | } |
| 1235 | memset(&tmx, 0, sizeof(tmx)); |
| 1236 | #if 0 |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 1237 | //doesn't work, offset remains 0 (!) in kernel: |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1238 | //ntpd: set adjtimex freq:1786097 tmx.offset:77487 |
| 1239 | //ntpd: prev adjtimex freq:1786097 tmx.offset:0 |
| 1240 | //ntpd: cur adjtimex freq:1786097 tmx.offset:0 |
| 1241 | tmx.modes = ADJ_FREQUENCY | ADJ_OFFSET; |
| 1242 | /* 65536 is one ppm */ |
| 1243 | tmx.freq = G.discipline_freq_drift * 65536e6; |
| 1244 | tmx.offset = G.last_update_offset * 1000000; /* usec */ |
| 1245 | #endif |
| 1246 | tmx.modes = ADJ_OFFSET | ADJ_STATUS | ADJ_TIMECONST;// | ADJ_MAXERROR | ADJ_ESTERROR; |
| 1247 | tmx.offset = (G.last_update_offset * 1000000) /* usec */ |
| 1248 | /* + (G.last_update_offset < 0 ? -0.5 : 0.5) - too small to bother */ |
| 1249 | + old_tmx_offset; /* almost always 0 */ |
| 1250 | tmx.status = STA_PLL; |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 1251 | if (G.ntp_status & LI_PLUSSEC) |
| 1252 | tmx.status |= STA_INS; |
| 1253 | if (G.ntp_status & LI_MINUSSEC) |
| 1254 | tmx.status |= STA_DEL; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1255 | tmx.constant = G.poll_exp - 4; |
| 1256 | //tmx.esterror = (u_int32)(clock_jitter * 1e6); |
| 1257 | //tmx.maxerror = (u_int32)((sys_rootdelay / 2 + sys_rootdisp) * 1e6); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1258 | rc = adjtimex(&tmx); |
| 1259 | if (rc < 0) |
| 1260 | bb_perror_msg_and_die("adjtimex"); |
Denys Vlasenko | d9109e3 | 2010-01-02 00:36:43 +0100 | [diff] [blame] | 1261 | /* NB: here kernel returns constant == G.poll_exp, not == G.poll_exp - 4. |
| 1262 | * Not sure why. Perhaps it is normal. |
| 1263 | */ |
| 1264 | VERB3 bb_error_msg("adjtimex:%d freq:%ld offset:%ld constant:%ld status:0x%x", |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1265 | rc, tmx.freq, tmx.offset, tmx.constant, tmx.status); |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 1266 | #if 0 |
Denys Vlasenko | d9109e3 | 2010-01-02 00:36:43 +0100 | [diff] [blame] | 1267 | VERB3 { |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 1268 | /* always gives the same output as above msg */ |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1269 | memset(&tmx, 0, sizeof(tmx)); |
| 1270 | if (adjtimex(&tmx) < 0) |
| 1271 | bb_perror_msg_and_die("adjtimex"); |
| 1272 | VERB3 bb_error_msg("c adjtimex freq:%ld offset:%ld constant:%ld status:0x%x", |
| 1273 | tmx.freq, tmx.offset, tmx.constant, tmx.status); |
Denys Vlasenko | d9109e3 | 2010-01-02 00:36:43 +0100 | [diff] [blame] | 1274 | } |
Denys Vlasenko | 6131311 | 2010-01-01 19:56:16 +0100 | [diff] [blame] | 1275 | #endif |
Denys Vlasenko | d9109e3 | 2010-01-02 00:36:43 +0100 | [diff] [blame] | 1276 | if (G.kernel_freq_drift != tmx.freq / 65536) { |
| 1277 | G.kernel_freq_drift = tmx.freq / 65536; |
| 1278 | VERB2 bb_error_msg("kernel clock drift: %ld ppm", G.kernel_freq_drift); |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1279 | } |
| 1280 | // #define STA_MODE 0x4000 /* mode (0 = PLL, 1 = FLL) (ro) */ - ? |
| 1281 | // it appeared after a while: |
| 1282 | //ntpd: p adjtimex freq:-14545653 offset:-5396 constant:10 status:0x41 |
| 1283 | //ntpd: c adjtimex freq:-14547835 offset:-8307 constant:10 status:0x1 |
| 1284 | //ntpd: p adjtimex freq:-14547835 offset:-6398 constant:10 status:0x41 |
| 1285 | //ntpd: c adjtimex freq:-14550486 offset:-10158 constant:10 status:0x1 |
| 1286 | //ntpd: p adjtimex freq:-14550486 offset:-6132 constant:10 status:0x41 |
| 1287 | //ntpd: c adjtimex freq:-14636129 offset:-10158 constant:10 status:0x4001 |
| 1288 | //ntpd: p adjtimex freq:-14636129 offset:-10002 constant:10 status:0x4041 |
| 1289 | //ntpd: c adjtimex freq:-14636245 offset:-7497 constant:10 status:0x1 |
| 1290 | //ntpd: p adjtimex freq:-14636245 offset:-4573 constant:10 status:0x41 |
| 1291 | //ntpd: c adjtimex freq:-14642034 offset:-11715 constant:10 status:0x1 |
| 1292 | //ntpd: p adjtimex freq:-14642034 offset:-4098 constant:10 status:0x41 |
| 1293 | //ntpd: c adjtimex freq:-14699112 offset:-11746 constant:10 status:0x4001 |
| 1294 | //ntpd: p adjtimex freq:-14699112 offset:-4239 constant:10 status:0x4041 |
| 1295 | //ntpd: c adjtimex freq:-14762330 offset:-12786 constant:10 status:0x4001 |
| 1296 | //ntpd: p adjtimex freq:-14762330 offset:-4434 constant:10 status:0x4041 |
| 1297 | //ntpd: b adjtimex freq:0 offset:-9669 constant:8 status:0x1 |
| 1298 | //ntpd: adjtimex:0 freq:-14809095 offset:-9669 constant:10 status:0x4001 |
| 1299 | //ntpd: c adjtimex freq:-14809095 offset:-9669 constant:10 status:0x4001 |
| 1300 | |
| 1301 | return 1; /* "ok to increase poll interval" */ |
| 1302 | } |
| 1303 | |
| 1304 | |
| 1305 | /* |
| 1306 | * We've got a new reply packet from a peer, process it |
| 1307 | * (helpers first) |
| 1308 | */ |
| 1309 | static unsigned |
| 1310 | retry_interval(void) |
| 1311 | { |
| 1312 | /* Local problem, want to retry soon */ |
| 1313 | unsigned interval, r; |
| 1314 | interval = RETRY_INTERVAL; |
| 1315 | r = random(); |
| 1316 | interval += r % (unsigned)(RETRY_INTERVAL / 4); |
| 1317 | VERB3 bb_error_msg("chose retry interval:%u", interval); |
| 1318 | return interval; |
| 1319 | } |
| 1320 | static unsigned |
| 1321 | poll_interval(int exponent) /* exp is always -1 or 0 */ |
| 1322 | { |
| 1323 | /* Want to send next packet at (1 << G.poll_exp) + small random value */ |
| 1324 | unsigned interval, r; |
| 1325 | exponent += G.poll_exp; /* G.poll_exp is always > 0 */ |
| 1326 | /* never true: if (exp < 0) exp = 0; */ |
| 1327 | interval = 1 << exponent; |
| 1328 | r = random(); |
| 1329 | interval += ((r & (interval-1)) >> 4) + ((r >> 8) & 1); /* + 1/16 of interval, max */ |
| 1330 | VERB3 bb_error_msg("chose poll interval:%u (poll_exp:%d exp:%d)", interval, G.poll_exp, exponent); |
| 1331 | return interval; |
| 1332 | } |
| 1333 | static void |
| 1334 | recv_and_process_peer_pkt(peer_t *p) |
| 1335 | { |
| 1336 | int rc; |
| 1337 | ssize_t size; |
| 1338 | msg_t msg; |
| 1339 | double T1, T2, T3, T4; |
| 1340 | unsigned interval; |
| 1341 | datapoint_t *datapoint; |
| 1342 | peer_t *q; |
| 1343 | |
| 1344 | /* We can recvfrom here and check from.IP, but some multihomed |
| 1345 | * ntp servers reply from their *other IP*. |
| 1346 | * TODO: maybe we should check at least what we can: from.port == 123? |
| 1347 | */ |
| 1348 | size = recv(p->p_fd, &msg, sizeof(msg), MSG_DONTWAIT); |
| 1349 | if (size == -1) { |
| 1350 | bb_perror_msg("recv(%s) error", p->p_dotted); |
| 1351 | if (errno == EHOSTUNREACH || errno == EHOSTDOWN |
| 1352 | || errno == ENETUNREACH || errno == ENETDOWN |
| 1353 | || errno == ECONNREFUSED || errno == EADDRNOTAVAIL |
| 1354 | || errno == EAGAIN |
| 1355 | ) { |
| 1356 | //TODO: always do this? |
| 1357 | set_next(p, retry_interval()); |
| 1358 | goto close_sock; |
| 1359 | } |
| 1360 | xfunc_die(); |
| 1361 | } |
| 1362 | |
| 1363 | if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) { |
| 1364 | bb_error_msg("malformed packet received from %s", p->p_dotted); |
| 1365 | goto bail; |
| 1366 | } |
| 1367 | |
| 1368 | if (msg.m_orgtime.int_partl != p->p_xmt_msg.m_xmttime.int_partl |
| 1369 | || msg.m_orgtime.fractionl != p->p_xmt_msg.m_xmttime.fractionl |
| 1370 | ) { |
| 1371 | goto bail; |
| 1372 | } |
| 1373 | |
| 1374 | if ((msg.m_status & LI_ALARM) == LI_ALARM |
| 1375 | || msg.m_stratum == 0 |
| 1376 | || msg.m_stratum > NTP_MAXSTRATUM |
| 1377 | ) { |
| 1378 | // TODO: stratum 0 responses may have commands in 32-bit m_refid field: |
| 1379 | // "DENY", "RSTR" - peer does not like us at all |
| 1380 | // "RATE" - peer is overloaded, reduce polling freq |
| 1381 | interval = poll_interval(0); |
| 1382 | bb_error_msg("reply from %s: not synced, next query in %us", p->p_dotted, interval); |
| 1383 | goto close_sock; |
| 1384 | } |
| 1385 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1386 | // /* Verify valid root distance */ |
| 1387 | // if (msg.m_rootdelay / 2 + msg.m_rootdisp >= MAXDISP || p->lastpkt_reftime > msg.m_xmt) |
| 1388 | // return; /* invalid header values */ |
| 1389 | |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 1390 | p->lastpkt_status = msg.m_status; |
| 1391 | p->lastpkt_stratum = msg.m_stratum; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1392 | p->lastpkt_rootdelay = sfp_to_d(msg.m_rootdelay); |
| 1393 | p->lastpkt_rootdisp = sfp_to_d(msg.m_rootdisp); |
| 1394 | p->lastpkt_refid = msg.m_refid; |
| 1395 | |
| 1396 | /* |
| 1397 | * From RFC 2030 (with a correction to the delay math): |
| 1398 | * |
| 1399 | * Timestamp Name ID When Generated |
| 1400 | * ------------------------------------------------------------ |
| 1401 | * Originate Timestamp T1 time request sent by client |
| 1402 | * Receive Timestamp T2 time request received by server |
| 1403 | * Transmit Timestamp T3 time reply sent by server |
| 1404 | * Destination Timestamp T4 time reply received by client |
| 1405 | * |
| 1406 | * The roundtrip delay and local clock offset are defined as |
| 1407 | * |
| 1408 | * delay = (T4 - T1) - (T3 - T2); offset = ((T2 - T1) + (T3 - T4)) / 2 |
| 1409 | */ |
| 1410 | T1 = p->p_xmttime; |
| 1411 | T2 = lfp_to_d(msg.m_rectime); |
| 1412 | T3 = lfp_to_d(msg.m_xmttime); |
| 1413 | T4 = gettime1900d(); |
| 1414 | |
| 1415 | p->lastpkt_recv_time = T4; |
| 1416 | |
| 1417 | VERB5 bb_error_msg("%s->lastpkt_recv_time=%f", p->p_dotted, p->lastpkt_recv_time); |
| 1418 | p->datapoint_idx = p->p_reachable_bits ? (p->datapoint_idx + 1) % NUM_DATAPOINTS : 0; |
| 1419 | datapoint = &p->filter_datapoint[p->datapoint_idx]; |
| 1420 | datapoint->d_recv_time = T4; |
| 1421 | datapoint->d_offset = ((T2 - T1) + (T3 - T4)) / 2; |
| 1422 | /* The delay calculation is a special case. In cases where the |
| 1423 | * server and client clocks are running at different rates and |
| 1424 | * with very fast networks, the delay can appear negative. In |
| 1425 | * order to avoid violating the Principle of Least Astonishment, |
| 1426 | * the delay is clamped not less than the system precision. |
| 1427 | */ |
| 1428 | p->lastpkt_delay = (T4 - T1) - (T3 - T2); |
Denys Vlasenko | a9aaeda | 2010-01-01 22:23:27 +0100 | [diff] [blame] | 1429 | if (p->lastpkt_delay < G_precision_sec) |
| 1430 | p->lastpkt_delay = G_precision_sec; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1431 | datapoint->d_dispersion = LOG2D(msg.m_precision_exp) + G_precision_sec; |
| 1432 | if (!p->p_reachable_bits) { |
| 1433 | /* 1st datapoint ever - replicate offset in every element */ |
| 1434 | int i; |
| 1435 | for (i = 1; i < NUM_DATAPOINTS; i++) { |
| 1436 | p->filter_datapoint[i].d_offset = datapoint->d_offset; |
| 1437 | } |
| 1438 | } |
| 1439 | |
| 1440 | p->p_reachable_bits |= 1; |
| 1441 | VERB1 { |
| 1442 | bb_error_msg("reply from %s: reach 0x%02x offset %f delay %f", |
| 1443 | p->p_dotted, |
| 1444 | p->p_reachable_bits, |
| 1445 | datapoint->d_offset, p->lastpkt_delay); |
| 1446 | } |
| 1447 | |
| 1448 | /* Muck with statictics and update the clock */ |
| 1449 | filter_datapoints(p, T4); |
| 1450 | q = select_and_cluster(T4); |
| 1451 | rc = -1; |
| 1452 | if (q) |
| 1453 | rc = update_local_clock(q, T4); |
| 1454 | |
| 1455 | if (rc != 0) { |
| 1456 | /* Adjust the poll interval by comparing the current offset |
| 1457 | * with the clock jitter. If the offset is less than |
| 1458 | * the clock jitter times a constant, then the averaging interval |
| 1459 | * is increased, otherwise it is decreased. A bit of hysteresis |
| 1460 | * helps calm the dance. Works best using burst mode. |
| 1461 | */ |
| 1462 | VERB4 if (rc > 0) { |
| 1463 | bb_error_msg("offset:%f POLLADJ_GATE*discipline_jitter:%f poll:%s", |
| 1464 | q->filter_offset, POLLADJ_GATE * G.discipline_jitter, |
| 1465 | fabs(q->filter_offset) < POLLADJ_GATE * G.discipline_jitter |
| 1466 | ? "grows" : "falls" |
| 1467 | ); |
| 1468 | } |
| 1469 | if (rc > 0 && fabs(q->filter_offset) < POLLADJ_GATE * G.discipline_jitter) { |
Denys Vlasenko | bfc2a32 | 2010-01-01 18:12:06 +0100 | [diff] [blame] | 1470 | /* was += G.poll_exp but it is a bit |
| 1471 | * too optimistic for my taste at high poll_exp's */ |
| 1472 | G.polladj_count += MINPOLL; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1473 | if (G.polladj_count > POLLADJ_LIMIT) { |
| 1474 | G.polladj_count = 0; |
| 1475 | if (G.poll_exp < MAXPOLL) { |
| 1476 | G.poll_exp++; |
| 1477 | VERB3 bb_error_msg("polladj: discipline_jitter:%f ++poll_exp=%d", |
| 1478 | G.discipline_jitter, G.poll_exp); |
| 1479 | } |
| 1480 | } else { |
| 1481 | VERB3 bb_error_msg("polladj: incr:%d", G.polladj_count); |
| 1482 | } |
| 1483 | } else { |
| 1484 | G.polladj_count -= G.poll_exp * 2; |
| 1485 | if (G.polladj_count < -POLLADJ_LIMIT) { |
| 1486 | G.polladj_count = 0; |
| 1487 | if (G.poll_exp > MINPOLL) { |
Denys Vlasenko | 2e36eb8 | 2010-01-02 01:50:16 +0100 | [diff] [blame] | 1488 | llist_t *item; |
| 1489 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1490 | G.poll_exp--; |
Denys Vlasenko | 2e36eb8 | 2010-01-02 01:50:16 +0100 | [diff] [blame] | 1491 | /* Correct p->next_action_time in each peer |
| 1492 | * which waits for sending, so that they send earlier. |
| 1493 | * Old pp->next_action_time are on the order |
| 1494 | * of t + (1 << old_poll_exp) + small_random, |
| 1495 | * we simply need to subtract ~half of that. |
| 1496 | */ |
| 1497 | for (item = G.ntp_peers; item != NULL; item = item->link) { |
| 1498 | peer_t *pp = (peer_t *) item->data; |
| 1499 | if (pp->p_fd < 0) |
| 1500 | pp->next_action_time -= (1 << G.poll_exp); |
| 1501 | } |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1502 | VERB3 bb_error_msg("polladj: discipline_jitter:%f --poll_exp=%d", |
| 1503 | G.discipline_jitter, G.poll_exp); |
| 1504 | } |
| 1505 | } else { |
| 1506 | VERB3 bb_error_msg("polladj: decr:%d", G.polladj_count); |
| 1507 | } |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | /* Decide when to send new query for this peer */ |
| 1512 | interval = poll_interval(0); |
| 1513 | set_next(p, interval); |
| 1514 | |
| 1515 | close_sock: |
| 1516 | /* We do not expect any more packets from this peer for now. |
| 1517 | * Closing the socket informs kernel about it. |
| 1518 | * We open a new socket when we send a new query. |
| 1519 | */ |
| 1520 | close(p->p_fd); |
| 1521 | p->p_fd = -1; |
| 1522 | bail: |
| 1523 | return; |
| 1524 | } |
| 1525 | |
| 1526 | #if ENABLE_FEATURE_NTPD_SERVER |
| 1527 | static void |
| 1528 | recv_and_process_client_pkt(void /*int fd*/) |
| 1529 | { |
| 1530 | ssize_t size; |
| 1531 | uint8_t version; |
| 1532 | double rectime; |
| 1533 | len_and_sockaddr *to; |
| 1534 | struct sockaddr *from; |
| 1535 | msg_t msg; |
| 1536 | uint8_t query_status; |
| 1537 | l_fixedpt_t query_xmttime; |
| 1538 | |
| 1539 | to = get_sock_lsa(G.listen_fd); |
| 1540 | from = xzalloc(to->len); |
| 1541 | |
| 1542 | size = recv_from_to(G.listen_fd, &msg, sizeof(msg), MSG_DONTWAIT, from, &to->u.sa, to->len); |
| 1543 | if (size != NTP_MSGSIZE_NOAUTH && size != NTP_MSGSIZE) { |
| 1544 | char *addr; |
| 1545 | if (size < 0) { |
| 1546 | if (errno == EAGAIN) |
| 1547 | goto bail; |
| 1548 | bb_perror_msg_and_die("recv"); |
| 1549 | } |
| 1550 | addr = xmalloc_sockaddr2dotted_noport(from); |
| 1551 | bb_error_msg("malformed packet received from %s: size %u", addr, (int)size); |
| 1552 | free(addr); |
| 1553 | goto bail; |
| 1554 | } |
| 1555 | |
| 1556 | query_status = msg.m_status; |
| 1557 | query_xmttime = msg.m_xmttime; |
| 1558 | |
| 1559 | /* Build a reply packet */ |
| 1560 | memset(&msg, 0, sizeof(msg)); |
Denys Vlasenko | 1ee5afd | 2010-01-02 15:57:07 +0100 | [diff] [blame^] | 1561 | msg.m_status = G.stratum < MAXSTRAT ? G.ntp_status : LI_ALARM; |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1562 | msg.m_status |= (query_status & VERSION_MASK); |
| 1563 | msg.m_status |= ((query_status & MODE_MASK) == MODE_CLIENT) ? |
| 1564 | MODE_SERVER : MODE_SYM_PAS; |
| 1565 | msg.m_stratum = G.stratum; |
| 1566 | msg.m_ppoll = G.poll_exp; |
| 1567 | msg.m_precision_exp = G_precision_exp; |
| 1568 | rectime = gettime1900d(); |
| 1569 | msg.m_xmttime = msg.m_rectime = d_to_lfp(rectime); |
| 1570 | msg.m_reftime = d_to_lfp(G.reftime); |
| 1571 | msg.m_orgtime = query_xmttime; |
| 1572 | msg.m_rootdelay = d_to_sfp(G.rootdelay); |
| 1573 | //simple code does not do this, fix simple code! |
| 1574 | msg.m_rootdisp = d_to_sfp(G.rootdisp); |
| 1575 | version = (query_status & VERSION_MASK); /* ... >> VERSION_SHIFT - done below instead */ |
| 1576 | msg.m_refid = G.refid; // (version > (3 << VERSION_SHIFT)) ? G.refid : G.refid3; |
| 1577 | |
| 1578 | /* We reply from the local address packet was sent to, |
| 1579 | * this makes to/from look swapped here: */ |
| 1580 | do_sendto(G.listen_fd, |
| 1581 | /*from:*/ &to->u.sa, /*to:*/ from, /*addrlen:*/ to->len, |
| 1582 | &msg, size); |
| 1583 | |
| 1584 | bail: |
| 1585 | free(to); |
| 1586 | free(from); |
| 1587 | } |
| 1588 | #endif |
| 1589 | |
| 1590 | /* Upstream ntpd's options: |
| 1591 | * |
| 1592 | * -4 Force DNS resolution of host names to the IPv4 namespace. |
| 1593 | * -6 Force DNS resolution of host names to the IPv6 namespace. |
| 1594 | * -a Require cryptographic authentication for broadcast client, |
| 1595 | * multicast client and symmetric passive associations. |
| 1596 | * This is the default. |
| 1597 | * -A Do not require cryptographic authentication for broadcast client, |
| 1598 | * multicast client and symmetric passive associations. |
| 1599 | * This is almost never a good idea. |
| 1600 | * -b Enable the client to synchronize to broadcast servers. |
| 1601 | * -c conffile |
| 1602 | * Specify the name and path of the configuration file, |
| 1603 | * default /etc/ntp.conf |
| 1604 | * -d Specify debugging mode. This option may occur more than once, |
| 1605 | * with each occurrence indicating greater detail of display. |
| 1606 | * -D level |
| 1607 | * Specify debugging level directly. |
| 1608 | * -f driftfile |
| 1609 | * Specify the name and path of the frequency file. |
| 1610 | * This is the same operation as the "driftfile FILE" |
| 1611 | * configuration command. |
| 1612 | * -g Normally, ntpd exits with a message to the system log |
| 1613 | * if the offset exceeds the panic threshold, which is 1000 s |
| 1614 | * by default. This option allows the time to be set to any value |
| 1615 | * without restriction; however, this can happen only once. |
| 1616 | * If the threshold is exceeded after that, ntpd will exit |
| 1617 | * with a message to the system log. This option can be used |
| 1618 | * with the -q and -x options. See the tinker command for other options. |
| 1619 | * -i jaildir |
| 1620 | * Chroot the server to the directory jaildir. This option also implies |
| 1621 | * that the server attempts to drop root privileges at startup |
| 1622 | * (otherwise, chroot gives very little additional security). |
| 1623 | * You may need to also specify a -u option. |
| 1624 | * -k keyfile |
| 1625 | * Specify the name and path of the symmetric key file, |
| 1626 | * default /etc/ntp/keys. This is the same operation |
| 1627 | * as the "keys FILE" configuration command. |
| 1628 | * -l logfile |
| 1629 | * Specify the name and path of the log file. The default |
| 1630 | * is the system log file. This is the same operation as |
| 1631 | * the "logfile FILE" configuration command. |
| 1632 | * -L Do not listen to virtual IPs. The default is to listen. |
| 1633 | * -n Don't fork. |
| 1634 | * -N To the extent permitted by the operating system, |
| 1635 | * run the ntpd at the highest priority. |
| 1636 | * -p pidfile |
| 1637 | * Specify the name and path of the file used to record the ntpd |
| 1638 | * process ID. This is the same operation as the "pidfile FILE" |
| 1639 | * configuration command. |
| 1640 | * -P priority |
| 1641 | * To the extent permitted by the operating system, |
| 1642 | * run the ntpd at the specified priority. |
| 1643 | * -q Exit the ntpd just after the first time the clock is set. |
| 1644 | * This behavior mimics that of the ntpdate program, which is |
| 1645 | * to be retired. The -g and -x options can be used with this option. |
| 1646 | * Note: The kernel time discipline is disabled with this option. |
| 1647 | * -r broadcastdelay |
| 1648 | * Specify the default propagation delay from the broadcast/multicast |
| 1649 | * server to this client. This is necessary only if the delay |
| 1650 | * cannot be computed automatically by the protocol. |
| 1651 | * -s statsdir |
| 1652 | * Specify the directory path for files created by the statistics |
| 1653 | * facility. This is the same operation as the "statsdir DIR" |
| 1654 | * configuration command. |
| 1655 | * -t key |
| 1656 | * Add a key number to the trusted key list. This option can occur |
| 1657 | * more than once. |
| 1658 | * -u user[:group] |
| 1659 | * Specify a user, and optionally a group, to switch to. |
| 1660 | * -v variable |
| 1661 | * -V variable |
| 1662 | * Add a system variable listed by default. |
| 1663 | * -x Normally, the time is slewed if the offset is less than the step |
| 1664 | * threshold, which is 128 ms by default, and stepped if above |
| 1665 | * the threshold. This option sets the threshold to 600 s, which is |
| 1666 | * well within the accuracy window to set the clock manually. |
| 1667 | * Note: since the slew rate of typical Unix kernels is limited |
| 1668 | * to 0.5 ms/s, each second of adjustment requires an amortization |
| 1669 | * interval of 2000 s. Thus, an adjustment as much as 600 s |
| 1670 | * will take almost 14 days to complete. This option can be used |
| 1671 | * with the -g and -q options. See the tinker command for other options. |
| 1672 | * Note: The kernel time discipline is disabled with this option. |
| 1673 | */ |
| 1674 | |
| 1675 | /* By doing init in a separate function we decrease stack usage |
| 1676 | * in main loop. |
| 1677 | */ |
| 1678 | static NOINLINE void ntp_init(char **argv) |
| 1679 | { |
| 1680 | unsigned opts; |
| 1681 | llist_t *peers; |
| 1682 | |
| 1683 | srandom(getpid()); |
| 1684 | |
| 1685 | if (getuid()) |
| 1686 | bb_error_msg_and_die(bb_msg_you_must_be_root); |
| 1687 | |
| 1688 | /* Set some globals */ |
| 1689 | #if 0 |
| 1690 | /* With constant b = 100, G.precision_exp is also constant -6. |
| 1691 | * Uncomment this to verify. |
| 1692 | */ |
| 1693 | { |
| 1694 | int prec = 0; |
| 1695 | int b; |
| 1696 | # if 0 |
| 1697 | struct timespec tp; |
| 1698 | /* We can use sys_clock_getres but assuming 10ms tick should be fine */ |
| 1699 | clock_getres(CLOCK_REALTIME, &tp); |
| 1700 | tp.tv_sec = 0; |
| 1701 | tp.tv_nsec = 10000000; |
| 1702 | b = 1000000000 / tp.tv_nsec; /* convert to Hz */ |
| 1703 | # else |
| 1704 | b = 100; /* b = 1000000000/10000000 = 100 */ |
| 1705 | # endif |
| 1706 | while (b > 1) |
| 1707 | prec--, b >>= 1; |
| 1708 | /*G.precision_exp = prec;*/ |
| 1709 | /*G.precision_sec = (1.0 / (1 << (- prec)));*/ |
| 1710 | bb_error_msg("G.precision_exp:%d sec:%f", prec, G_precision_sec); /* -6 */ |
| 1711 | } |
| 1712 | #endif |
| 1713 | G.stratum = MAXSTRAT; |
| 1714 | G.poll_exp = 1; /* should use MINPOLL, but 1 speeds up initial sync */ |
| 1715 | G.reftime = G.last_update_recv_time = gettime1900d(); |
| 1716 | |
| 1717 | /* Parse options */ |
| 1718 | peers = NULL; |
| 1719 | opt_complementary = "dd:p::"; /* d: counter, p: list */ |
| 1720 | opts = getopt32(argv, |
| 1721 | "nqNx" /* compat */ |
| 1722 | "p:"IF_FEATURE_NTPD_SERVER("l") /* NOT compat */ |
| 1723 | "d" /* compat */ |
| 1724 | "46aAbgL", /* compat, ignored */ |
| 1725 | &peers, &G.verbose); |
| 1726 | if (!(opts & (OPT_p|OPT_l))) |
| 1727 | bb_show_usage(); |
| 1728 | // if (opts & OPT_x) /* disable stepping, only slew is allowed */ |
| 1729 | // G.time_was_stepped = 1; |
| 1730 | while (peers) |
| 1731 | add_peers(llist_pop(&peers)); |
| 1732 | if (!(opts & OPT_n)) { |
| 1733 | bb_daemonize_or_rexec(DAEMON_DEVNULL_STDIO, argv); |
| 1734 | logmode = LOGMODE_NONE; |
| 1735 | } |
| 1736 | #if ENABLE_FEATURE_NTPD_SERVER |
| 1737 | G.listen_fd = -1; |
| 1738 | if (opts & OPT_l) { |
| 1739 | G.listen_fd = create_and_bind_dgram_or_die(NULL, 123); |
| 1740 | socket_want_pktinfo(G.listen_fd); |
| 1741 | setsockopt(G.listen_fd, IPPROTO_IP, IP_TOS, &const_IPTOS_LOWDELAY, sizeof(const_IPTOS_LOWDELAY)); |
| 1742 | } |
| 1743 | #endif |
| 1744 | /* I hesitate to set -20 prio. -15 should be high enough for timekeeping */ |
| 1745 | if (opts & OPT_N) |
| 1746 | setpriority(PRIO_PROCESS, 0, -15); |
| 1747 | |
| 1748 | bb_signals((1 << SIGTERM) | (1 << SIGINT), record_signo); |
| 1749 | bb_signals((1 << SIGPIPE) | (1 << SIGHUP), SIG_IGN); |
| 1750 | } |
| 1751 | |
| 1752 | int ntpd_main(int argc UNUSED_PARAM, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 1753 | int ntpd_main(int argc UNUSED_PARAM, char **argv) |
| 1754 | { |
| 1755 | struct globals g; |
| 1756 | struct pollfd *pfd; |
| 1757 | peer_t **idx2peer; |
| 1758 | |
| 1759 | memset(&g, 0, sizeof(g)); |
| 1760 | SET_PTR_TO_GLOBALS(&g); |
| 1761 | |
| 1762 | ntp_init(argv); |
| 1763 | |
| 1764 | { |
| 1765 | /* if ENABLE_FEATURE_NTPD_SERVER, + 1 for listen_fd: */ |
| 1766 | unsigned cnt = g.peer_cnt + ENABLE_FEATURE_NTPD_SERVER; |
| 1767 | idx2peer = xzalloc(sizeof(idx2peer[0]) * cnt); |
| 1768 | pfd = xzalloc(sizeof(pfd[0]) * cnt); |
| 1769 | } |
| 1770 | |
| 1771 | while (!bb_got_signal) { |
| 1772 | llist_t *item; |
| 1773 | unsigned i, j; |
| 1774 | unsigned sent_cnt, trial_cnt; |
| 1775 | int nfds, timeout; |
| 1776 | time_t cur_time, nextaction; |
| 1777 | |
| 1778 | /* Nothing between here and poll() blocks for any significant time */ |
| 1779 | |
| 1780 | cur_time = time(NULL); |
| 1781 | nextaction = cur_time + 3600; |
| 1782 | |
| 1783 | i = 0; |
| 1784 | #if ENABLE_FEATURE_NTPD_SERVER |
| 1785 | if (g.listen_fd != -1) { |
| 1786 | pfd[0].fd = g.listen_fd; |
| 1787 | pfd[0].events = POLLIN; |
| 1788 | i++; |
| 1789 | } |
| 1790 | #endif |
| 1791 | /* Pass over peer list, send requests, time out on receives */ |
| 1792 | sent_cnt = trial_cnt = 0; |
| 1793 | for (item = g.ntp_peers; item != NULL; item = item->link) { |
| 1794 | peer_t *p = (peer_t *) item->data; |
| 1795 | |
| 1796 | /* Overflow-safe "if (p->next_action_time <= cur_time) ..." */ |
| 1797 | if ((int)(cur_time - p->next_action_time) >= 0) { |
| 1798 | if (p->p_fd == -1) { |
| 1799 | /* Time to send new req */ |
| 1800 | trial_cnt++; |
| 1801 | if (send_query_to_peer(p) == 0) |
| 1802 | sent_cnt++; |
| 1803 | } else { |
| 1804 | /* Timed out waiting for reply */ |
| 1805 | close(p->p_fd); |
| 1806 | p->p_fd = -1; |
| 1807 | timeout = poll_interval(-1); /* try a bit faster */ |
| 1808 | bb_error_msg("timed out waiting for %s, reach 0x%02x, next query in %us", |
| 1809 | p->p_dotted, p->p_reachable_bits, timeout); |
| 1810 | set_next(p, timeout); |
| 1811 | } |
| 1812 | } |
| 1813 | |
| 1814 | if (p->next_action_time < nextaction) |
| 1815 | nextaction = p->next_action_time; |
| 1816 | |
| 1817 | if (p->p_fd >= 0) { |
| 1818 | /* Wait for reply from this peer */ |
| 1819 | pfd[i].fd = p->p_fd; |
| 1820 | pfd[i].events = POLLIN; |
| 1821 | idx2peer[i] = p; |
| 1822 | i++; |
| 1823 | } |
| 1824 | } |
| 1825 | |
Denys Vlasenko | dd6673b | 2010-01-01 16:46:17 +0100 | [diff] [blame] | 1826 | timeout = nextaction - cur_time; |
| 1827 | if (timeout < 1) |
| 1828 | timeout = 1; |
| 1829 | |
| 1830 | /* Here we may block */ |
| 1831 | VERB2 bb_error_msg("poll %us, sockets:%u", timeout, i); |
| 1832 | nfds = poll(pfd, i, timeout * 1000); |
| 1833 | if (nfds <= 0) |
| 1834 | continue; |
| 1835 | |
| 1836 | /* Process any received packets */ |
| 1837 | j = 0; |
| 1838 | #if ENABLE_FEATURE_NTPD_SERVER |
| 1839 | if (g.listen_fd != -1) { |
| 1840 | if (pfd[0].revents /* & (POLLIN|POLLERR)*/) { |
| 1841 | nfds--; |
| 1842 | recv_and_process_client_pkt(/*g.listen_fd*/); |
| 1843 | } |
| 1844 | j = 1; |
| 1845 | } |
| 1846 | #endif |
| 1847 | for (; nfds != 0 && j < i; j++) { |
| 1848 | if (pfd[j].revents /* & (POLLIN|POLLERR)*/) { |
| 1849 | nfds--; |
| 1850 | recv_and_process_peer_pkt(idx2peer[j]); |
| 1851 | } |
| 1852 | } |
| 1853 | } /* while (!bb_got_signal) */ |
| 1854 | |
| 1855 | kill_myself_with_sig(bb_got_signal); |
| 1856 | } |
| 1857 | |
| 1858 | |
| 1859 | |
| 1860 | |
| 1861 | |
| 1862 | |
| 1863 | /*** openntpd-4.6 uses only adjtime, not adjtimex ***/ |
| 1864 | |
| 1865 | /*** ntp-4.2.6/ntpd/ntp_loopfilter.c - adjtimex usage ***/ |
| 1866 | |
| 1867 | #if 0 |
| 1868 | static double |
| 1869 | direct_freq(double fp_offset) |
| 1870 | { |
| 1871 | |
| 1872 | #ifdef KERNEL_PLL |
| 1873 | /* |
| 1874 | * If the kernel is enabled, we need the residual offset to |
| 1875 | * calculate the frequency correction. |
| 1876 | */ |
| 1877 | if (pll_control && kern_enable) { |
| 1878 | memset(&ntv, 0, sizeof(ntv)); |
| 1879 | ntp_adjtime(&ntv); |
| 1880 | #ifdef STA_NANO |
| 1881 | clock_offset = ntv.offset / 1e9; |
| 1882 | #else /* STA_NANO */ |
| 1883 | clock_offset = ntv.offset / 1e6; |
| 1884 | #endif /* STA_NANO */ |
| 1885 | drift_comp = FREQTOD(ntv.freq); |
| 1886 | } |
| 1887 | #endif /* KERNEL_PLL */ |
| 1888 | set_freq((fp_offset - clock_offset) / (current_time - clock_epoch) + drift_comp); |
| 1889 | wander_resid = 0; |
| 1890 | return drift_comp; |
| 1891 | } |
| 1892 | |
| 1893 | static void |
| 1894 | set_freq(double freq) /* frequency update */ |
| 1895 | { |
| 1896 | char tbuf[80]; |
| 1897 | |
| 1898 | drift_comp = freq; |
| 1899 | |
| 1900 | #ifdef KERNEL_PLL |
| 1901 | /* |
| 1902 | * If the kernel is enabled, update the kernel frequency. |
| 1903 | */ |
| 1904 | if (pll_control && kern_enable) { |
| 1905 | memset(&ntv, 0, sizeof(ntv)); |
| 1906 | ntv.modes = MOD_FREQUENCY; |
| 1907 | ntv.freq = DTOFREQ(drift_comp); |
| 1908 | ntp_adjtime(&ntv); |
| 1909 | snprintf(tbuf, sizeof(tbuf), "kernel %.3f PPM", drift_comp * 1e6); |
| 1910 | report_event(EVNT_FSET, NULL, tbuf); |
| 1911 | } else { |
| 1912 | snprintf(tbuf, sizeof(tbuf), "ntpd %.3f PPM", drift_comp * 1e6); |
| 1913 | report_event(EVNT_FSET, NULL, tbuf); |
| 1914 | } |
| 1915 | #else /* KERNEL_PLL */ |
| 1916 | snprintf(tbuf, sizeof(tbuf), "ntpd %.3f PPM", drift_comp * 1e6); |
| 1917 | report_event(EVNT_FSET, NULL, tbuf); |
| 1918 | #endif /* KERNEL_PLL */ |
| 1919 | } |
| 1920 | |
| 1921 | ... |
| 1922 | ... |
| 1923 | ... |
| 1924 | |
| 1925 | #ifdef KERNEL_PLL |
| 1926 | /* |
| 1927 | * This code segment works when clock adjustments are made using |
| 1928 | * precision time kernel support and the ntp_adjtime() system |
| 1929 | * call. This support is available in Solaris 2.6 and later, |
| 1930 | * Digital Unix 4.0 and later, FreeBSD, Linux and specially |
| 1931 | * modified kernels for HP-UX 9 and Ultrix 4. In the case of the |
| 1932 | * DECstation 5000/240 and Alpha AXP, additional kernel |
| 1933 | * modifications provide a true microsecond clock and nanosecond |
| 1934 | * clock, respectively. |
| 1935 | * |
| 1936 | * Important note: The kernel discipline is used only if the |
| 1937 | * step threshold is less than 0.5 s, as anything higher can |
| 1938 | * lead to overflow problems. This might occur if some misguided |
| 1939 | * lad set the step threshold to something ridiculous. |
| 1940 | */ |
| 1941 | if (pll_control && kern_enable) { |
| 1942 | |
| 1943 | #define MOD_BITS (MOD_OFFSET | MOD_MAXERROR | MOD_ESTERROR | MOD_STATUS | MOD_TIMECONST) |
| 1944 | |
| 1945 | /* |
| 1946 | * We initialize the structure for the ntp_adjtime() |
| 1947 | * system call. We have to convert everything to |
| 1948 | * microseconds or nanoseconds first. Do not update the |
| 1949 | * system variables if the ext_enable flag is set. In |
| 1950 | * this case, the external clock driver will update the |
| 1951 | * variables, which will be read later by the local |
| 1952 | * clock driver. Afterwards, remember the time and |
| 1953 | * frequency offsets for jitter and stability values and |
| 1954 | * to update the frequency file. |
| 1955 | */ |
| 1956 | memset(&ntv, 0, sizeof(ntv)); |
| 1957 | if (ext_enable) { |
| 1958 | ntv.modes = MOD_STATUS; |
| 1959 | } else { |
| 1960 | #ifdef STA_NANO |
| 1961 | ntv.modes = MOD_BITS | MOD_NANO; |
| 1962 | #else /* STA_NANO */ |
| 1963 | ntv.modes = MOD_BITS; |
| 1964 | #endif /* STA_NANO */ |
| 1965 | if (clock_offset < 0) |
| 1966 | dtemp = -.5; |
| 1967 | else |
| 1968 | dtemp = .5; |
| 1969 | #ifdef STA_NANO |
| 1970 | ntv.offset = (int32)(clock_offset * 1e9 + dtemp); |
| 1971 | ntv.constant = sys_poll; |
| 1972 | #else /* STA_NANO */ |
| 1973 | ntv.offset = (int32)(clock_offset * 1e6 + dtemp); |
| 1974 | ntv.constant = sys_poll - 4; |
| 1975 | #endif /* STA_NANO */ |
| 1976 | ntv.esterror = (u_int32)(clock_jitter * 1e6); |
| 1977 | ntv.maxerror = (u_int32)((sys_rootdelay / 2 + sys_rootdisp) * 1e6); |
| 1978 | ntv.status = STA_PLL; |
| 1979 | |
| 1980 | /* |
| 1981 | * Enable/disable the PPS if requested. |
| 1982 | */ |
| 1983 | if (pps_enable) { |
| 1984 | if (!(pll_status & STA_PPSTIME)) |
| 1985 | report_event(EVNT_KERN, |
| 1986 | NULL, "PPS enabled"); |
| 1987 | ntv.status |= STA_PPSTIME | STA_PPSFREQ; |
| 1988 | } else { |
| 1989 | if (pll_status & STA_PPSTIME) |
| 1990 | report_event(EVNT_KERN, |
| 1991 | NULL, "PPS disabled"); |
| 1992 | ntv.status &= ~(STA_PPSTIME | |
| 1993 | STA_PPSFREQ); |
| 1994 | } |
| 1995 | if (sys_leap == LEAP_ADDSECOND) |
| 1996 | ntv.status |= STA_INS; |
| 1997 | else if (sys_leap == LEAP_DELSECOND) |
| 1998 | ntv.status |= STA_DEL; |
| 1999 | } |
| 2000 | |
| 2001 | /* |
| 2002 | * Pass the stuff to the kernel. If it squeals, turn off |
| 2003 | * the pps. In any case, fetch the kernel offset, |
| 2004 | * frequency and jitter. |
| 2005 | */ |
| 2006 | if (ntp_adjtime(&ntv) == TIME_ERROR) { |
| 2007 | if (!(ntv.status & STA_PPSSIGNAL)) |
| 2008 | report_event(EVNT_KERN, NULL, |
| 2009 | "PPS no signal"); |
| 2010 | } |
| 2011 | pll_status = ntv.status; |
| 2012 | #ifdef STA_NANO |
| 2013 | clock_offset = ntv.offset / 1e9; |
| 2014 | #else /* STA_NANO */ |
| 2015 | clock_offset = ntv.offset / 1e6; |
| 2016 | #endif /* STA_NANO */ |
| 2017 | clock_frequency = FREQTOD(ntv.freq); |
| 2018 | |
| 2019 | /* |
| 2020 | * If the kernel PPS is lit, monitor its performance. |
| 2021 | */ |
| 2022 | if (ntv.status & STA_PPSTIME) { |
| 2023 | #ifdef STA_NANO |
| 2024 | clock_jitter = ntv.jitter / 1e9; |
| 2025 | #else /* STA_NANO */ |
| 2026 | clock_jitter = ntv.jitter / 1e6; |
| 2027 | #endif /* STA_NANO */ |
| 2028 | } |
| 2029 | |
| 2030 | #if defined(STA_NANO) && NTP_API == 4 |
| 2031 | /* |
| 2032 | * If the TAI changes, update the kernel TAI. |
| 2033 | */ |
| 2034 | if (loop_tai != sys_tai) { |
| 2035 | loop_tai = sys_tai; |
| 2036 | ntv.modes = MOD_TAI; |
| 2037 | ntv.constant = sys_tai; |
| 2038 | ntp_adjtime(&ntv); |
| 2039 | } |
| 2040 | #endif /* STA_NANO */ |
| 2041 | } |
| 2042 | #endif /* KERNEL_PLL */ |
| 2043 | #endif |