1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*
chronyd/chronyc - Programs for keeping computer clocks accurate.
**********************************************************************
* Copyright (C) Richard P. Curnow 1997-2003
* Copyright (C) Miroslav Lichvar 2009-2012, 2014-2015, 2017
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
**********************************************************************
=======================================================================
Driver for systems that implement the adjtimex()/ntp_adjtime() system call
*/
#include "config.h"
#include "sysincl.h"
#include "conf.h"
#include "privops.h"
#include "sys_generic.h"
#include "sys_timex.h"
#include "logging.h"
#ifdef PRIVOPS_ADJUSTTIMEX
#define NTP_ADJTIME PRV_AdjustTimex
#define NTP_ADJTIME_NAME "ntp_adjtime"
#else
#ifdef LINUX
#define NTP_ADJTIME adjtimex
#define NTP_ADJTIME_NAME "adjtimex"
#else
#define NTP_ADJTIME ntp_adjtime
#define NTP_ADJTIME_NAME "ntp_adjtime"
#endif
#endif
/* Maximum frequency offset accepted by the kernel (in ppm) */
#define MAX_FREQ 500.0
/* Frequency scale to convert from ppm to the timex freq */
#define FREQ_SCALE (double)(1 << 16)
/* Threshold for the timex maxerror when the kernel sets the UNSYNC flag */
#define MAX_SYNC_ERROR 16.0
/* Minimum assumed rate at which the kernel updates the clock frequency */
#define MIN_TICK_RATE 100
/* Saved timex status */
static int sys_status;
/* Saved TAI-UTC offset */
static int sys_tai_offset;
/* ================================================== */
static double
convert_timex_frequency(const struct timex *txc)
{
double freq_ppm;
freq_ppm = txc->freq / FREQ_SCALE;
return -freq_ppm;
}
/* ================================================== */
static double
read_frequency(void)
{
struct timex txc;
txc.modes = 0;
SYS_Timex_Adjust(&txc, 0);
return convert_timex_frequency(&txc);
}
/* ================================================== */
static double
set_frequency(double freq_ppm)
{
struct timex txc;
txc.modes = MOD_FREQUENCY;
txc.freq = freq_ppm * -FREQ_SCALE;
SYS_Timex_Adjust(&txc, 0);
return convert_timex_frequency(&txc);
}
/* ================================================== */
static void
set_leap(int leap, int tai_offset)
{
struct timex txc;
int applied, prev_status;
txc.modes = 0;
applied = SYS_Timex_Adjust(&txc, 0) == TIME_WAIT;
prev_status = sys_status;
sys_status &= ~(STA_INS | STA_DEL);
if (leap > 0)
sys_status |= STA_INS;
else if (leap < 0)
sys_status |= STA_DEL;
txc.modes = MOD_STATUS;
txc.status = sys_status;
#ifdef MOD_TAI
if (tai_offset) {
txc.modes |= MOD_TAI;
txc.constant = tai_offset;
if (applied && !(sys_status & (STA_INS | STA_DEL)))
sys_tai_offset += prev_status & STA_INS ? 1 : -1;
if (sys_tai_offset != tai_offset) {
sys_tai_offset = tai_offset;
LOG(LOGS_INFO, "System clock TAI offset set to %d seconds", tai_offset);
}
}
#endif
SYS_Timex_Adjust(&txc, 0);
if (prev_status != sys_status) {
LOG(LOGS_INFO, "System clock status %s leap second",
leap ? (leap > 0 ? "set to insert" : "set to delete") :
(applied ? "reset after" : "set to not insert/delete"));
}
}
/* ================================================== */
static void
set_sync_status(int synchronised, double est_error, double max_error)
{
struct timex txc;
if (synchronised) {
if (est_error > MAX_SYNC_ERROR)
est_error = MAX_SYNC_ERROR;
if (max_error >= MAX_SYNC_ERROR) {
max_error = MAX_SYNC_ERROR;
synchronised = 0;
}
} else {
est_error = max_error = MAX_SYNC_ERROR;
}
#ifdef LINUX
/* On Linux clear the UNSYNC flag only if rtcsync is enabled */
if (!CNF_GetRtcSync())
synchronised = 0;
#endif
if (synchronised)
sys_status &= ~STA_UNSYNC;
else
sys_status |= STA_UNSYNC;
txc.modes = MOD_STATUS | MOD_ESTERROR | MOD_MAXERROR;
txc.status = sys_status;
txc.esterror = est_error * 1.0e6;
txc.maxerror = max_error * 1.0e6;
if (SYS_Timex_Adjust(&txc, 1) < 0)
;
}
/* ================================================== */
static void
initialise_timex(void)
{
struct timex txc;
sys_status = STA_UNSYNC;
sys_tai_offset = 0;
/* Reset PLL offset */
txc.modes = MOD_OFFSET | MOD_STATUS;
txc.status = STA_PLL | sys_status;
txc.offset = 0;
SYS_Timex_Adjust(&txc, 0);
/* Turn PLL off */
txc.modes = MOD_STATUS;
txc.status = sys_status;
SYS_Timex_Adjust(&txc, 0);
}
/* ================================================== */
void
SYS_Timex_Initialise(void)
{
SYS_Timex_InitialiseWithFunctions(MAX_FREQ, 1.0 / MIN_TICK_RATE, NULL, NULL, NULL,
0.0, 0.0, NULL, NULL);
}
/* ================================================== */
void
SYS_Timex_InitialiseWithFunctions(double max_set_freq_ppm, double max_set_freq_delay,
lcl_ReadFrequencyDriver sys_read_freq,
lcl_SetFrequencyDriver sys_set_freq,
lcl_ApplyStepOffsetDriver sys_apply_step_offset,
double min_fastslew_offset, double max_fastslew_rate,
lcl_AccrueOffsetDriver sys_accrue_offset,
lcl_OffsetCorrectionDriver sys_get_offset_correction)
{
initialise_timex();
SYS_Generic_CompleteFreqDriver(max_set_freq_ppm, max_set_freq_delay,
sys_read_freq ? sys_read_freq : read_frequency,
sys_set_freq ? sys_set_freq : set_frequency,
sys_apply_step_offset,
min_fastslew_offset, max_fastslew_rate,
sys_accrue_offset, sys_get_offset_correction,
set_leap, set_sync_status);
}
/* ================================================== */
void
SYS_Timex_Finalise(void)
{
SYS_Generic_Finalise();
}
/* ================================================== */
int
SYS_Timex_Adjust(struct timex *txc, int ignore_error)
{
int state;
#ifdef SOLARIS
/* The kernel seems to check the constant even when it's not being set */
if (!(txc->modes & MOD_TIMECONST))
txc->constant = 10;
#endif
state = NTP_ADJTIME(txc);
if (state < 0) {
LOG(ignore_error ? LOGS_DEBUG : LOGS_FATAL,
NTP_ADJTIME_NAME"(0x%x) failed : %s", txc->modes, strerror(errno));
}
return state;
}
|