Title: NTP Architecture, Protocol and Algorithms
1Server 1
Peer/Poll 1
Selection and Clustering Algorithms
SystemProcess
Clock Discipline Process
Combining Algorithm
Server 2
Peer/Poll 2
Loop Filter
Server 3
Peer/Poll 3
VFO
RemoteServers
Peer/PollProcesses
Clock Adjust Process
Figure 1. Implementation Model
20
15
16
31
Seconds
Fraction
NTP Short Format
0
31
32
63
Timestamp
Fraction
NTP Timestamp Format
0
63
64
127
31
32
Timestamp
Fraction
Era
NTP Date Format
Figure 2. NTP Time Formats
3t2
t3
t6
t7
0
t3
t5.
t1
PacketVariables Peer B StateVariables
T1
0
t4
t2
t6
T2
t5.
t1
t3 clock
t7 clock
T3
t2 clock
t6 clock
T4
t5
T3 ? t1?
org
t1
t1
t6
t6
rec
t2
t2
t7
T1 t3?
xmt
t3
0
t3
t2
t6
t7
t4
t1
t5
t8
t1
t4
t8
t5
t5
t3.
0
t1
PacketVariables Peer A StateVariables
T1
t6
t4
0
t2
T2
t7
t3.
t1 clock
t5 clock
T3
t4 clock
t8 clock
T4
0.
org
t3
T3 ? t3?
T3 ? 0?
0
rec
t4
t8
t4
t1
xmt
t5
T1 t5?
T1 t1?
Figure 3. On-Wire Protocol
4receive()
format OK?
no
format error
yes
access OK?
no
access deny
yes
mode 3?
yes
client_packet
no
auth OK?
no
auth error
yes
match_assoc
Figure 4. Receive Processing
5client_packet
Packet Variable Variablex.leap ?
s.leapx.version ? r.versionx.mode ?
4x.stratum ? s.stratumx.poll ?
r.pollx.precision ? s.precisionx.rootdelay
? s.rootdelay x.rootdisp ? s.rootdisp
x.refid ? s.refidx.reftime ?
s.reftimex.org ? r.xmt x.rec ?
r.dstx.xmt ? clockx.keyid ?
r.keyidx.digest ? md5 digest
copy header
copy T1,T2
T3 clock
auth OK?
yes
no
MD5 digest
NAK digest
fast_xmit()
xmt T3
return
Figure 5. Client Packet Processing
6match_assoc
T3 0?
yes
format error
no
yes
T3 xmt?
duplicate
no
T1 0? orT2 0?
mode 5?
no
yes
yes
no
T1 xmt?
yes
no
yes
auth error
auth NAK?
no
org T3rec T4
org T3rec T4
return
packet
Figure 6. Timestamp Processing
7packet
copy header
header?
bad
header error
ok
reach 1
Peer PacketVariables Variablesp.leap ?
r.leapp.mode ? r.modep.stratum ?
r.stratump.ppoll ? r.ppoll p.rootdelay ?
r.rootdelayp.rootdisp ? r.rootdispp.refid
? r.refidp.reftime ? r.reftime
poll_update()
clock_filter
Figure 7. Packet Processing
8clock_filter
Shift sample q, d, e, t into filter shift register
Copy filter to a temporary list. Sort the list by
increasing d. Let qi, di, ei, ti be the ith entry
on the sorted list.
no
t0 gt t
yes
clock_select()
return
Figure 8. Clock Filter Algorithm
9clock_select()
scan candidates
accept()?
no
yes
add peer
selection algorthm
find majority clique
survivors?
no
yes
clustering algorithm
n lt CMIN?
yes
no
s.p NULL
s.p v0.p
return(UNSYNC)
return(SYNC)
Figure 9. clock_select() Routine
10For each of m acceptable associations construct
a correctness interval q l, q l
Select the lowpoint, midpoint and highpoint of
these intervals. Sort these values in a list from
lowest to highest. Set the number of falsetickers
f 0.
Set the number of midpoints d 0. Set c 0.
Scan from lowest endpoint to highest. Add one to
c for every lowpoint, subtract one for every
highpoint, add one to d for every midpoint. If c
m - f, stop set l current lowpoint
Set c 0. Scan from highest endpoint to lowest.
Add one to c for every highpoint, subtract one
for every lowpoint, add one to d for every
midpoint. If c m - f, stop set u current
highpoint.
If d f and l lt u?
no
yes
Add one to f. Is f lt m / 2?
Success the intersection interval is l, u.
yes
no
Failure a majority clique could not be found..
Figure 10. Selection Algorithm
11accept()
leap 11?stratum gt MAXSTRAT?
any yes
server not synchronized
all no
reach 0?
yes
server not reachable
no
root_dist() gt MAXDIST?
yes
root distance exceeded
no
yes
refid addr?
server/client sync loop
no
return(NO)
return(YES)
Figure 11. accept() Routine
12Let (q, j, L) represent a candidate peer with
offset q, jitter j and a weight factor L
stratum MAXDIST l.
Save the candidates in the v structure sorted by
increasing L. Let n be the number of candidates.
For each candidate s, compute the selection
jitter js (RMS peer offset differences between s
and all other candidates).
Select jmax as the candidate with maximum js.
Select jmin as the candidate with minimum j.
yes
jmax lt jmin or n NMIN?
no
Delete the outlyer candidate with jmax reduce n
by one.
Done. The remaining cluster survivors are the
pick of the litter. The survivors are in the v
structure sorted by L.
Figure 12. Clustering Algorithm
13clock_combine()
y z w 0
Variable Process DescriptionQ system combined
clock offsetJp system combined
jitterq0 survivor list first survivor
offsetqi survivor list ith survivor offsetx, y,
z, w temporaries
scancluster survivors
x rootdist()
done
Variables and Parameters
return
Figure 13. clock_combine() Routine
14clock_update()
System System PeerVariables Variablesleap ?
leapstratum ? stratum 1refid ?
refidreftime ? reftimeD ? DR dE ? ER e
Fm j Q
no
yes
local_clock()
IGNOR
ADJ
PANIC
STEP
panic exit
clear all associations
Update System Variables
update system variables
leap 3stratum MAXSTRAT
return
Figure 14. clock_update() Routine
15Peer Variables
System Variables
Client
S
S
S
Server
Figure 15. System Variables Processing
16qr
Vd
Vs
NTP
Clock Filter
Phase Detector
qc -
VFO
Loop Filter
x
Vc
Phase/freqPrediction
ClockAdjust
y
Figure 16. Clock Discipline Algorithm
17PhaseCorrect
x
yFLL
FLLPredict
Vs
S
y
yPLL
PLLPredict
Figure 17. PLL/FLL Prediction Functions
18local_ clock()
yes
Q gt PANICT?
return(PANIC)
no
freq 0rval IGNOR
SPIK
NSET
yes
no
SYNC
Q gt STEPT?
FREQ
FSET
state SPIK
SYNC
m lt WATCH
FREQ
SPIK
NSET
FSET
yes
no
FREQ?
m lt WATCH
QR Q
no
yes
no
yes
QR Q
rst(FREQ, 0)
Calculate new freq adjustment from Q, t and m
using hybrid PLL and FLL.
return(rval)
step_time (Q)rval STEP
rval ADJ
state NSET?
rst(SYNC, 0)
no
yes
rst(new, off)
tc
rst(FREQ, 0)
state newQB off - QRQR off
return(rval)
return
Figure 18. local_clock() Routine (1 of 2)
19tc
state SYNC
yes
QR gt PGATE j ?
no
count t
count - 2t
count ³ LIMIT?
count -LIMIT?
yes
yes
no
no
count 0
count 0
t lt MAXPOLL
t gt MINPOLL
no
no
yes
yes
t
t--
f freq
return(rval)
Figure 19. local_clock() Routine (2 of 2)
20clock_adjust()
E F
QR - tmp
adjust_time (f tmp)
next lt timer?
no
yes
poll()
return
Figure 20. clock_adjust() Routine
21poll()
mode 5?
yes
no
yes
no
burst 0?
burst--
reach ltlt 1last timer
yes
reach 0x7 0?
clock_filter (0, 0, 8, t)
mode 6?
yes
no
peer_xmit()
no
yes
unreach lt UNREACH?
reach 0?
no
poll_update()
unreach 0hpoll t
no
yes
IBURST unreach 0?
no
return
BURST accept()?
yes
no
unreach
yes
hpoll
burst BCOUNT
Figure 21. poll() Routine
22Packet Variable Variablex.leap ?
s.leapx.version ? VERSIONx.mode ?
p.modex.stratum ? s.stratumx.poll ?
p.hpollx.precision ? s.precx.rootdelay ?
s.rootdelay x.rootdisp ? s.rootdisp
x.refid ? s.refidx.reftime ?
s.reftimex.org ? p.org x.rec ? p.rec
x.xmt ? clockx.keyid ?
p.keyidx.digest ? md5()
peer_xmit()
copy header
copy T1,T2
T3 get_clock()
MD5()
xmit_packet()
xmt T3
return
Figure 22. transmit() Routine
23poll_update()
hpoll minMAXPOLL, max(MINPOLL, hpoll)
burst gt 0
yes
no
poll minhpoll, max(MINPOLL, ppoll)
next last?
yes
no
next last (1 ltlt poll)
next 2
return
Figure 23. poll_update Routine
240
2
5
8
16
24
31
Stratum
Poll
LI
Mode
VN
Precision
Root Delay
Root Dispersion
Reference Identifier
Reference Timestamp (64)
Originate Timestamp (64)
Cryptosum
Receive Timestamp (64)
Transmit Timestamp (64)
Extension Field 1 (optional)
Extension Field 2 (optional)
Key Identifier
MAC (Optional)
Message Digest (128)
Figure 24. NTP Header Format
250
2
8
16
31
Code
Length
RE
VN
Association ID
Timestamp
Filestamp
Value Length
Value (optional)
Padding (zeros)
Signature Length
Signature (optional)
Padding (zeros)
Value Fields (optional)
Figure 25. NTP Extension Field Format
26Association Mode Assoc. Mode Packet
Mode Symmetric Active 1 1 or 2Symmetric
Passive 2 1 Client 3 4 Server 4 3 Broadcast
Server 5 5 5Broadcast Client 6 na
Table 1. Association and Packet Modes
27Year MJD NTP Date NTP Era NTP Timestamp Epoch 1
Jan -4712 -2,400,001 -208,657,814,400 -49 1,795,58
3,104 First day Julian Era 1 Jan
-1 -679,306 -59,989,766,400 -14 139,775,744 2
BCE 1 Jan 0 -678,941 -59,958,230,400 -14 171,311,7
44 1 BCE 1 Jan 1 -678,575 -59,926,608,000 -14 202,
934,144 1 CE 4 Oct 1582 -100,851 -10,011,254,400 -
3 2,873,647,488 Last day of Julian Calendar 15
Oct 1582 -100,840 -10,010,304,000 -3 2,874,597,888
First day Gregorian Calendar 31 Dec
1899 15,019 -86,400 -1 4,294,880,896 Last day
NTP Era -1 1 Jan 1900 15,020 0 0 0 First day NTP
Era 0 1 Jan 1970 40,587 2,208,988,800 0 2,208,988
,800 First day Unix 1 Jan 1972 41,317 2,272,060,80
0 0 2,272,060,800 First day UTC 31 Dec
1999 51,543 3,155,587,200 0 3,155,587,200 Last
day 20th century 1 Jan 2000 51,544 3,155,673,600
0 3,155,673,600 First day 21st century 7 Feb
2036 64,730 4,294,944,000 0 4,294,944,000 Last
day NTP Era 0 8 Feb 2036 64,731 4,295,030,400 1 6
3,104 First day NTP Era 1 16 Mar
2172 114,441 8,589,974,400 2 39,808 First day
NTP Era 2 1 Jan 2500 234,166 18,934,214,400 4 1,75
4,345,216 2500 CE 1 Jan 3000 416,787 34,712,668,80
0 8 352,930,432 3000 CE
Table 2. Interesting Historic NTP Dates
28Name Description r. receive packet header
variablex. transmit packet header
variablep. peer/poll variables. system
variablec. clock discipline variable
Table 3. Name Prefix Conventions
29Name Value Description PORT 123 NTP port
numberVERSION 4 version numberTOLERANCE 15e-6 fr
equency tolerance (F) (s/s)MINPOLL 4 minimum
poll exponent (16 s)MAXPOLL 17 maximum poll
exponent (36 h)MAXDISP 16 maximum dispersion
(s)MINDISP .005 minimum dispersion increment
(s)MAXDIST 1 distance threshold
(s)MAXSTRAT 16 maximum stratum number
Table 4. Global Parameters
30Name Formula Description leap leap leap
indicator (LI)version version version number
(VN)mode mode modestratum stratum stratumpoll p
oll poll exponentprecision rR precision
exponentrootdelay DR root delayrootdisp ER root
dispersionrefid refid reference
IDreftime reftime reference timestamporg T1 orig
in timestamp rec T2 receive timestamp
xmt T3 transmit timestampdst T4 destination
timestampkeyid keyid key IDdigest digest message
digest
Table 5. Packet Header Variables
31Name Formula Description Configuration
Variables srcaddr srcaddr source
address srcport srcport source portdstaddr dstadd
r destination addressdstport destport destination
port keyid keyid key identifier key ID Packet
Variablesleap leap leap indicatorversion version
version number mode mode mode stratum stratum str
atum ppoll ppoll peer poll exponentrootdelay DR r
oot delay rootdisp ER root dispersion refid refid
reference ID reftime reftime reference
timestampTimestamp Variables t t epochorg T1 ori
gin timestamp rec T2 receive timestamp xmt T3 tran
smit timestampStatistics Variables offset q clock
offset delay d roundtrip delay disp e dispersion
jitter j jitter
Table 6. Peer Variables
32Table 7. Packet Error Checks
33Name Formula Description t t epochleap leap leap
indicator stratum stratum stratumprecision r prec
isionp p system peer pointeroffset Q combined
offsetjitter J combined jitterrootdelay D root
delay rootdisp E root dispersionrefid refid refer
ence ID reftime reftime reference
timeNMIN 3 minimum survivorsCMIN 1 minimum
candidates
Table 8. System Process Variables and Parameters
34Name Formula Descriptiont timer seconds counter
offset Q combined offsetresid QR residual
offsetfreq f clock frequencyjitter j clock
jitterwander Y frequency wander tc t time
constant (log2)state state stateadj adj frequenc
y adjustmentcount count hysteresis
counterSTEPT 125 step thresh (.125
s)WATCH 900 stepout thresh (s)PANICT 1000 panic
thresh. (1000 s) LIMIT 30 hysteresis
limitPGATE 4 hysteresis gate TC 16 time
constant scaleAVG 8 averaging constant
Table 9. Clock Discipline Variables and Parameters
35State Q lt STEP Q ³ STEP CommentsNSET FREQ,
adj. time FREQ, step time Frequency never
set. FSET SYNC, adj. time SYNC, step
time Frequency set from file. SPIK SYNC, adj.
freq, adj. time if m lt WATCH, SPIK Outlyer
found. else step time FREQ if m lt WATCH,
FREQ if m lt WATCH, FREQ Frequency
measurement. else step freq, adj. time else
SYNC, step freq, step time SYNC SYNC, adj
freq, adj. time SPIK Normal operation.
Table 10. Clock Discipline State Transition Table
36Name Formula Description hpoll hpoll host poll
exponentlast last last poll timenext next next
poll timereach reach reach registerunreach unrea
ch unreach counterUNREACH 24 unreach limit
BCOUNT 8 burst countBURST flag burst
enableIBURST flag iburst enable
Table 11. Poll Process Variables and Parameters