gpsd-time-service-howto.adoc 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956
  1. = GPSD Time Service HOWTO
  2. Gary E. Miller <gem@rellim.com>, Eric S. Raymond <esr@thyrsus.com>
  3. :date: 17 November 2021
  4. :description: How to set up an NTP Stratum 1 server using GPSD.
  5. :keywords: time, GPSD, NTP, time, precision, 1PPS, PPS, stratum, jitter
  6. :robots: index,follow
  7. :sectlinks:
  8. :toc: macro
  9. include::inc-menu.adoc[]
  10. This document is mastered in asciidoc format. If you are reading it in HTML,
  11. you can find the original at the GPSD project website.
  12. == Introduction
  13. GPSD, NTP and a GPS receiver supplying 1PPS (one pulse-per-second)
  14. output can be used to set up a high-quality NTP time server. This
  15. HOWTO explains the method and various options you have in setting it
  16. up.
  17. Here is the quick-start sequence. The rest of this document goes
  18. into more detail about the steps.
  19. . Ensure that gpsd and either ntpd or chronyd are installed on your
  20. system. (Both gpsd and ntpd are pre-installed in many stock Linux
  21. distributions; chronyd is normally not.) You don't have to choose
  22. which to use yet if you have easy access to both, but knowing which
  23. alternatives are readily available to you is a good place to start.
  24. . Verify that your gpsd version is at least 3.22. Many problems are
  25. caused by the use of old versions. When in doubt, reinstall
  26. gpsd from the upstream source. Many distributions ship old
  27. and/or broken versions of gpsd.
  28. . Connect a PPS-capable GPS receiver to one of your serial or USB
  29. ports. A random cheap consumer-grade GPS receiver won't do; you
  30. may have to do some hunting to find a usable one.
  31. . Check that it actually emits PPS by pointing GPSD's gpsmon utility
  32. at the port. If it has a good (3D-mode) fix, lines marked "PPS"
  33. should scroll by in the packet-logging window. A new device out of
  34. the box may take up to 30 minutes for the first 3D fix. If gpsmon
  35. shows a 3D fix, but does not show PPS lines, try running ppscheck.
  36. . If you persistently fail to get live PPS, (a) you may have a
  37. skyview problem, (b) you may have a cabling problem, (c) your GPS
  38. may not support PPS, (d) you may have a gpsd or kernel configuration
  39. problem, (e) you may have a device problem, (e) there may be a bug
  40. in the core GPSD code used by gpsmon. These are listed in roughly
  41. decreasing probability. Troubleshoot appropriately.
  42. . Edit your ntpd or chronyd configuration to tell your NTP daemon to
  43. listen for time hints. (This step is somewhat tricky.)
  44. . Start up gpsd. If you are using ntpd, you can use ipcrm(1) to check that
  45. verify that the shared-memory segment that gpsd and ntpd want to
  46. use to communicate has been attached; or you can impatiently skip
  47. to the next step and look for the segment only if that fails.
  48. . Use cgps or xgps to verify that your GNSS receiver has a good 3D fix.
  49. . Use ntpshmmon to verify that gpsd is sending time corrections to SHM
  50. memory.
  51. . Use ntpq or the chronyc sources command to verify that your device
  52. is feeding time corrections to your NTP daemon.
  53. . (Optional and challenging.) Hand-tune your installation for the
  54. best possible performance.
  55. This document does not attempt to explain all the intricacies of time
  56. service; it is focused on practical advice for one specific deployment
  57. case. There is an introduction <<TIME-INTRO>> to basic concepts and
  58. terminology for those new to time service. An overview of the NTP
  59. protocols can be found at <<WIKI-NTP>>, and the official NTP FAQ
  60. <<NTP-FAQ>> is probably as gentle an introduction to the NTP reference
  61. implementation as presently exists.
  62. We encourage others to contribute additions and corrections.
  63. .Units table
  64. |====================================================
  65. | nSec | nanoSecond | 1/1,000,000,000 of a second
  66. | uSec | microSecond | 1/1,000,000 of a second
  67. | mSec | milliSecond | 1/1,000 of a second
  68. |====================================================
  69. There are a few important terms we need to define up front. *Latency*
  70. is delay from a time measurement until a report on it arrives where it
  71. is needed. *Jitter* is short-term variation in latency. *Wobble* is a
  72. jitter-like variation that is long compared to typical measurement
  73. periods. *Accuracy* is the traceable offset from 'true' time as
  74. defined by a national standard institute.
  75. A good analogy to jitter vs wobble is changes in sea level on a beach.
  76. Jitter is caused by wave action, wobble is the daily effect of tides.
  77. For a time server, the most common causes of wobble are varying GPS
  78. satellite geometries and the effect of daily temperature variations on
  79. the oscillators in your equipment.
  80. == NTP with GPSD
  81. See <<TIME-INTRO>> for a technical description of how NTP corrects
  82. your computer's clock against wobble. For purposes of this how-to, the
  83. important concepts to take way are those of time strata, servers, and
  84. reference clocks.
  85. Ordinary NTP client computers are normally configured to get time from
  86. one or more Stratum 2 (or less commonly Stratum 3) NTP
  87. servers. However, with GPSD and a suitable GPS receiver, you can easily
  88. condition your clock to higher accuracy than what you get from typical
  89. Stratum 2; with a little effort, you can do better than you can get
  90. from most public Stratum 1 servers.
  91. You can then make your high-quality time available to other systems on
  92. your network, or even run a public NTP server. Anyone can do this;
  93. there is no official authority, and any NTP client may choose to use
  94. your host as a server by requesting time from it. The time-service
  95. network is self-regulating, with NTP daemons constantly pruning
  96. statistical outliers so the timebase cannot be accidentally or
  97. deliberately compromised.
  98. In fact many public and widely-trusted Stratum 1 servers use GPS
  99. receivers as their reference clocks, and a significant fraction of
  100. those use GPSD in the way we will describe here.
  101. == GPS time
  102. The way time is shipped from GPS satellites causes problems to
  103. beware of in certain edge cases.
  104. Date and time in GPS is represented as number of weeks from the start
  105. of zero second of 6 January 1980, plus number of seconds into the
  106. week. GPS time is *not* leap-second corrected, though satellites also
  107. broadcast a current leap-second correction which is updated on
  108. six-month boundaries according to rotational bulletins issued by the
  109. International Earth Rotation and Reference Systems Service (IERS).
  110. The leap-second correction is only included in the multiplexed satellite
  111. subframe broadcast, once every 12.5 minutes. While the satellites do
  112. notify GPSes of upcoming leap-seconds, this notification is not
  113. necessarily processed correctly on consumer-grade devices, and may not
  114. be available at all when a GPS receiver has just cold-booted. Thus,
  115. reported UTC time may be slightly inaccurate between a cold boot or leap
  116. second and the following subframe broadcast.
  117. GPS date and time are subject to a rollover problem in the 10-bit week
  118. number counter, which will re-zero every 1024 weeks (roughly every 19.6
  119. years). The first rollover since GPS went live in 1980 was in Aug-1999,
  120. followed by Apr-2019, the next will be in Nov-2038 (the 32-bit and POSIX
  121. issues will probably be more important by then). The new "CNAV" data
  122. format extends the week number to 13 bits, with the first rollover
  123. occurring in Jan-2137, but this is only used with some newly added GPS
  124. signals, and is unlikely to be usable in most consumer-grade receivers
  125. currently.
  126. For accurate time reporting, therefore, a GPS requires a supplemental
  127. time references sufficient to identify the current rollover period,
  128. e.g. accurate to within 512 weeks. Many GPSes have a wired-in (and
  129. undocumented) assumption about the UTC time of the last rollover and
  130. will thus report incorrect times outside the rollover period they were
  131. designed in.
  132. For accurate time service via GPSD, you require three things:
  133. * A GPS made since the last rollover, so its hidden assumption about
  134. the epoch will be correct.
  135. * Enough time elapsed since a cold boot or IERS leap-second adjustment
  136. for the current leap-second to get update.
  137. * A GPS that properly handles leap-second adjustments. Anything
  138. based on a u-blox from v6 onward should be good; the status of
  139. SiRFs is unknown and doubtful.
  140. == 1PPS quality issues
  141. GPSD is useful for precision time service because it can use the 1PPS
  142. pulse delivered by some GPS receivers to discipline (correct) a local
  143. NTP instance.
  144. It's tempting to think one could use a GPS receiver for time service
  145. just by timestamping the arrival of the first character in the report
  146. on each fix and correcting for a relatively small fixed latency
  147. composed of fix-processing and RS232 transmission time.
  148. At one character per ten bits (counting framing and stopbits) a
  149. 9600-bps serial link introduces about a mSec of latency *per
  150. character*; furthermore, your kernel will normally delay delivery
  151. of characters to your application until the next timer tick, about
  152. every 4 mSec in modern kernels. Both USB and RS232 will incur that
  153. approximately 5mSec-per-char latency overhead. You'll have to deal
  154. with this latency even on chips like the Venus 6 that claim the
  155. beginning of their reporting burst is synced to PPS. (Such claims are
  156. not always reliable, in any case.)
  157. Unfortunately, fix reports are also delayed in the receiver and on
  158. the link by as much as several hundred mSec, and this delay is not
  159. constant. This latency varies (wobbles) throughout the day. It may be
  160. stable to 10 mSec for hours and then jump by 200mSec. Under these
  161. circumstances you can't expect accuracy to UTC much better than 1
  162. second from this method.
  163. For example: SiRF receivers, the make currently most popular in
  164. consumer-grade GPS receivers, exhibit a wobble of about 170mSec in the
  165. offset between actual top-of-second and the transmission of the first
  166. sentence in each reporting cycle.
  167. To get accurate time, then, the in-band fix report from the GPS
  168. receiver needs to be supplemented with an out-of-band signal that has
  169. a low and constant or near-constant latency with respect to the time
  170. of the fix. GPS satellites deliver a top-of-GPS-second
  171. notification that is nominally accurate to 50nSec; in capable GPS
  172. receivers that becomes the 1PPS signal.
  173. 1PPS-capable GPS receivers use an RS-232 control line to ship the 1PPS
  174. edge of second to the host system (usually Carrier Detect or Ring
  175. Indicator; GPSD will quietly accept either). Satellite top-of-second
  176. loses some accuracy on the way down due mainly to variable delays in
  177. the ionosphere; processing overhead in the GPS receiver itself adds a
  178. bit more latency, and your local host detecting that pulse adds still
  179. more latency and jitter. But it's still often accurate to on the
  180. order of 1 uSec.
  181. Under most Unixes there are two ways to watch 1PPS; Kernel PPS (KPPS)
  182. and plain PPS latching. KPPS is an implementation of RFC 2783 <<RFC-2783>>.
  183. Plain PPS just references the pulse to the system clock as
  184. measured in user space. These have different error budgets.
  185. Kernel PPS uses a kernel function to accurately timestamp the status
  186. change on the PPS line. Plain PPS has the kernel wake up the GPSD PPS
  187. thread and then the PPS thread reads the current system clock. As
  188. noted in the GPSD code, having the kernel do the time stamp yields
  189. lower latency and less jitter. Both methods have accuracy degraded by
  190. interrupt-processing latency in the kernel serial layer, but plain
  191. PPS incurs additional context-switching overhead that KPPS does not.
  192. With KPPS it is very doable to get the system clock stable to &plusmn;1
  193. uSec. Otherwise, you are lucky to get &plusmn;5 uSec, and there will be
  194. about 20uSec of jitter. All these figures were observed on
  195. plain-vanilla x86 PCs with clock speeds in the 2GHz range.
  196. All the previous figures assume you're using PPS delivered over RS232.
  197. USB GPS receivers that deliver 1PPS are rare, but do exist. Notably,
  198. there's the Navisys GR-601W/GR-701W/GR-801W <<MACX-1>>. In case these devices go
  199. out of production it's worth noting that they are a trivial
  200. modification of the stock two-chip-on-a-miniboard
  201. commodity-GPS-receiver design of engine plus USB-to-serial adapter;
  202. the GR-[678]01W wires a u-blox 6/7/8 to a Prolific Logic PL23203. To
  203. get 1PPS out, this design just wires the 1PPS pin from the GPS engine
  204. to the Carrier Detect pin on the USB adapter. (This is known as the
  205. "Macx-1 mod".)
  206. With this design, 1PPS from the engine will turn into a USB event that
  207. becomes visible to the host system (and GPSD) the next time the USB
  208. device is polled. USB 1.1 polls 1024 slots every second. Each slot is
  209. polled in the same order every second. When a device is added it is
  210. assigned to one of those 1024 polling slots. It should then be clear
  211. that the accuracy of a USB 1.1 connected GPS receiver would be about 1
  212. mSec.
  213. As of mid-2016 no USB GPS receiver we know of implements the higher
  214. polling-rate options in USB 2 and 3 or the interrupt capability in USB
  215. 3. When one does, and if it has the Macx-1 mod, higher USB accuracy
  216. will ensue.
  217. .Summary of typical accuracy
  218. |=====================================================
  219. | GPS atomic clock | &plusmn;50nSec
  220. | KPPS | &plusmn;1uSec
  221. | PPS | &plusmn;5uSec
  222. | USB 1.1 poll interval | &plusmn;1mSec
  223. | USB 2.0 poll interval | &plusmn;100&mu;Sec (100000 nSec)
  224. | Network NTP time | ~&plusmn;30mSec footnote:[RFC5905 says "a few tens of milliseconds", but asymmetric routing can produce 100mSec offset]
  225. |=====================================================
  226. Observed variations from the typical figure increase towards the bottom
  227. of the table. Notably, a heavily loaded host system can reduce PPS
  228. accuracy further, though not KPPS accuracy except in the most extreme
  229. cases. The USB poll interval tends to be very stable (relative to its
  230. 1mSec or 100&mu;Sec base).
  231. Network NTP time accuracy can be degraded below RFC5905's "a few tens
  232. of milliseconds" by a number of factors. Almost all have more to do
  233. with the quality of your Internet connection to your servers than with
  234. the time accuracy of the servers themselves. Some negatives:
  235. * Having a cable modem. That is, as opposed to DSL or optical fiber, which
  236. tend to have less variable latencies.
  237. * Path delay asymmetries due to peering policy. These can confuse
  238. NTP's reconciliation algorithms.
  239. With these factors in play, worst-case error can reach up to
  240. &plusmn;100mSec. Fortunately, errors of over &plusmn;100mSec are
  241. unusual and should occur only if all your network routes to servers
  242. have serious problems.
  243. == Software Prerequisites
  244. If your kernel provides the RFC 2783 KPPS (kernel PPS) API, gpsd will
  245. use that for extra accuracy. Many Linux distributions have a package
  246. called "pps-tools" that will install KPPS support and the timepps.h
  247. header file. We recommend you do that. If your kernel is built in
  248. the normal modular way, this package installation will suffice.
  249. === Building gpsd
  250. A normal gpsd build includes support for interpreting 1PPS pulses that
  251. is mostly autoconfiguring and requires no special setup. If the
  252. current system, and GNSS receiver, supports pps.
  253. You can build a version stripped to the minimum configuration required
  254. for time service. This reduces the size of the binary and may be
  255. helpful on embedded systems or for SBCs like the Raspberry Pi, Odroid,
  256. or BeagleBone. Only do this if you have serious size constraints, much
  257. functionality will be lost.
  258. When gpsd is built with timeservice=yes:
  259. 1. The -n (nowait) option is forced: gpsd opens its command-line devices
  260. immediately on startup. Assuming you do not start gpsd with systemd.
  261. If you are using systemd ensure it is starting gpsd, not just the gpsd
  262. service, on startup.
  263. 2. Forces the building of ntpshmmon and cgps. Those programs would
  264. be built by default anyway, unless gpsdclients=no.
  265. 3. The configure will fail if pps is not available.
  266. 4. Most drivers will not be built. You must specify the ones you need
  267. when configuring. The NMEA 0183 driver is always built, no matter what.
  268. To configure the minimal timeservice build:
  269. -----------------------------------------------------------------------------
  270. $ scons -c
  271. $ scons timeservice=yes
  272. -----------------------------------------------------------------------------
  273. You may add GNSS reciver protocol (e.g. "ublox=yes" or "sirf=yes").
  274. Besides the daemon, this also builds cgps and ntpshmmon.
  275. If you do not use timeservice=yes, then make sure the build is with
  276. pps=yes and ntpshm=yes (the defaults). Like this:
  277. -----------------------------------------------------------------------------
  278. $ scons -c
  279. $ scons pps=yes ntpshm=yes
  280. -----------------------------------------------------------------------------
  281. More complete, and distro specific, build instructions can be found in
  282. the files INSTALL.adoc and build.adoc in the source distribution.
  283. === Kernel support
  284. If you are scratch-building a Linux kernel, the configuration
  285. must include either these two lines, or the same with "y" replaced
  286. by "m" to enable the drivers as modules:
  287. -----------------------------------------------------------------------------
  288. CONFIG_PPS=y
  289. CONFIG_PPS_CLIENT_LDISC=y
  290. -----------------------------------------------------------------------------
  291. Some embedded systems, like the Raspberry Pi, detect PPS on a GPIO
  292. line instead of on a serial port line. For those systems you will
  293. also need these two lines:
  294. -----------------------------------------------------------------------------
  295. CONFIG_PPS_CLIENT_GPIO=y
  296. CONFIG_GPIO_SYSFS=y
  297. -----------------------------------------------------------------------------
  298. Your Linux distribution may ship a file /boot/config-XXX (where XXX is
  299. the name of a kernel) or one called /proc/config.gz (for the running
  300. kernel). This will have a list of the configuration options that were
  301. used to build the kernel. You can check if the above options are
  302. set. Usually they will be set to "m", which is sufficient.
  303. NetBSD has included the RFC2783 Pulse Per Second API for real serial
  304. ports by default since 1998, and it works with ntpd. NetBSD 7
  305. (forthcoming) includes RFC2783 support for USB-serial devices, and
  306. this works (with ntpd) with the GR-601W/GR-701W/GR-801W. However,
  307. gpsd's code interacts badly with the NetBSD implementation, and gpsd's
  308. support for RFC2783 PPS does not yet work on NetBSD (for serial or
  309. USB).
  310. Other OSes have different ways to enable KPPS in their kernels.
  311. When we learn what those are, we'll document them or point
  312. at references.
  313. === Time service daemon
  314. You will need to have either ntpd or chrony installed. If you are
  315. running a Unix variant with a package system, the packages will
  316. probably be named 'ntp' (or 'ntpsec') and either 'chrony' or 'chronyd'.
  317. Between ntpd and chrony, ntpd is the older and more popular choice --
  318. thus, the one with the best-established peer community if you need
  319. help in unusual situations. On the other hand, chrony has a
  320. reputation for being easier to set up and configure, and is better in
  321. situations where your machine has to be disconnected from the Internet
  322. for long enough periods of time for the clock to drift significantly.
  323. ntpd and chrony have differing philosophies, with ntpd more interested
  324. in deriving consensus time from multiple sources while chrony tries to
  325. identify a single best source and track it closely.
  326. A feature comparison, part of the chrony documentation, is at
  327. <<CHRONY-COMPARE>>. An informative email thread about the differences
  328. is <<CHRONYDEFAULT>>. If you don't already know enough about time
  329. service to have a preference, the functional differences between them
  330. are unlikely to be significant to you; flip a coin.
  331. === NTPSec
  332. If you choose the ntpd option, it's best to go with the NTPsec version
  333. rather than legacy ntpd. NTPsec shares some maintainers with GPSD,
  334. and has some significant improvements in security and performance.
  335. As of June 2020 2019, NTPsec is available as a package in:
  336. * Alpine
  337. * archlinux
  338. * Debian (and variants like Ubuntu and Raspbian)
  339. * Gentoo
  340. * OpenSUSE
  341. If it is not available as a package, you can build it from source,
  342. <<GITLAB-SOURCE>>, it is not especially difficult.
  343. == Choice of Hardware
  344. To get 1PPS to your NTP daemon, you first need to get it from a
  345. PPS-capable GPS receiver. As of early 2015 this means either the
  346. previously mentioned GR devices or a serial GPS receiver with 1PPS.
  347. You can find 1PPS-capable devices supported by GPSD at <<HARDWARE>>.
  348. Note that the most popular consumer-grade GPS receivers do not usually
  349. deliver 1PPS through USB or even RS232. The usual run of cheap GPS
  350. mice won't do. In general, you can't use a USB device for time
  351. service unless you know it has the Macx-1 mod.
  352. In the past, the RS232 variant of the Garmin GPS-18 has been very
  353. commonly used for time service (see <<LVC>> for a typical setup very
  354. well described). While it is still a respectable choice, newer
  355. devices have better sensitivity and signal discrimination. This makes
  356. them superior for indoor use as time sources.
  357. In general, use a GPS receiver with an RS232 interface for time
  358. service if you can. The GR-601W was designed (by one of the authors,
  359. as it happens) for deployment with commodity TCP/IP routers that only
  360. have USB ports. RS232 is more fiddly to set up (with older devices
  361. like the GPS-18 you may even have to make your own cables) but it can
  362. deliver three orders of magnitude better accuracy and repeatability --
  363. enough to meet prevailing standards for a public Stratum 1 server.
  364. Among newer receiver designs the authors found the u-blox line of
  365. receivers used in the GR-[678]01W to be particularly good. Very
  366. detailed information on its timing performance can be found at
  367. <<UBLOX-TIMING>>. One of us (Raymond) has recent experience with an
  368. eval kit, the EVK 6H-0-001, that would make an excellent Stratum 0
  369. device.
  370. Both the EVK 6H and GR-601W are built around the LEA-6H module, which
  371. is a relatively inexpensive but high-quality navigation GPS
  372. receiver. We make a note of this because u-blox also has a specialized
  373. timing variant, the LEA 6T, which would probably be overkill for an
  374. NTP server. (The 6T does have the virtue that you could probably get a
  375. good fix from one satellite in view once it knows its location, but
  376. the part is expensive and difficult to find.)
  377. Unfortunately as of early 2015 the LEA-6H is still hard to find in a
  378. packaged RS232 version, as opposed to a bare OEM module exporting TTL
  379. levels or an eval kit like the EVK 6H-0-001 costing upwards of
  380. US$300. Search the web; you may find a here-today-gone-tomorrow offer
  381. on alibaba.com or somewhere similar.
  382. The LEA-6T, and some other higher-end GPS receivers (but not the
  383. LEA-6H) have a stationary mode which, after you initialize it with the
  384. device's location, can deliver time service with only one good
  385. satellite lock (as opposed to the three required for a fix in its
  386. normal mode). For most reliable service we recommend using stationary
  387. mode if your device has it. GPSD tools don't yet directly support
  388. this, but that capability may be added in a future release.
  389. The design of your host system can also affect time quality. The
  390. &plusmn;5uSec error bound quoted above is for a dual-core or better
  391. system with clock in the 2GHz range on which the OS can schedule the
  392. long-running PPS thread in GPSD on an otherwise mostly unused
  393. processor (the Linux scheduler, in particular, will do this). On a
  394. single-core system, contention with other processes can pile
  395. on several additional microseconds of error.
  396. If you are super-serious about your time-nuttery, you may want to look
  397. into the newest generation of dedicated Stratum 1 microservers being
  398. built out of open-source SBCs like the Raspberry Pi and Beaglebone, or
  399. sometimes with fully custom designs. A representative build is well
  400. described at <<RPI>>.
  401. These microserver designs avoid load-induced jitter by being fully
  402. dedicated to NTP service. They are small, low-powered devices and
  403. often surprisingly inexpensive, as in costing less than US$100. They
  404. tend to favor the LEA-6H, and many of them use preinstalled GPSD on
  405. board.
  406. == Enabling PPS
  407. You can determine whether your GPS receiver emits 1PPS, and gpsd is
  408. detecting it, by running the gpsmon utility (giving it the GPS
  409. receiver's serial-device path as argument). Watch for lines of dashes
  410. marked 'PPS' in the packet-logging window; for most GPS receiver types
  411. there will also be a "PPS offset:" field in the data panels above
  412. showing the delta between PPS and your local clock.
  413. If you don't have gpsmon available, or you don't see PPS lines in it,
  414. you can run ppscheck. As a last resort you can gpsd at -D 5 and watch
  415. for PPS state change messages in the logfile.
  416. If you don't see evidence of incoming PPS, here are some trouble
  417. sources to check:
  418. 1. The skyview of your GPS receiver may be poor. Suspect this if,
  419. when you watch it with cgps, it wanders in and out of having a
  420. good 3D fix. Unfortunately, the only fix for this is to re-site
  421. your GPS where it can see more sky; fortunately, this is not as
  422. common a problem as it used to be, because modern receivers are
  423. often capable of getting a solid fix indoors.
  424. 2. If you are using an RS232 cable, examine it suspiciously, ideally
  425. with an RS232 breakout box. Cheap DB9 to DB9 cables such as those
  426. issued with UPSs often carry TXD/RXD/SG only, omitting handshake
  427. lines such as DCD, RI, and DSR that are used to carry 1PPS.
  428. Suspect this especially if the cable jacket looks too skinny to
  429. hold more than three leads!
  430. 3. Verify that your gpsd and kernel were both built with PPS support,
  431. as previously described in the section on software prerequisites.
  432. 4. Verify that the USB or RS232 device driver is accepting the ioctl
  433. that tells it to wait on a PPS state change from the device. The
  434. messages you hope *not* to see look like "KPPS cannot set PPS line
  435. discipline" and "PPS ioctl(TIOCMIWAIT) failed". The former
  436. can probably be corrected by running as root; the latter (which
  437. should never happen with an RS232 device) probably means your USB
  438. device driver lacks this wait capability entirely and cannot be
  439. used for time service.
  440. 5. If you have a solid 3D fix, a known-good cable, your software is
  441. properly configured, the wait ioctl succeeded, but you still get no
  442. PPS, then you might have a GPS receiver that fails to deliver PPS
  443. off the chip to the RS232 or USB interface. You get to become
  444. intimate with datasheets and pinouts, and might need to acquire a
  445. different GPS receiver.
  446. == Running GPSD
  447. If you're going to use gpsd for time service, you must run in -n mode
  448. so the clock will be updated even when no clients are active. This option
  449. is forced if you built GPSD with timeservice=yes as an option.
  450. Note that gpsd assumes that after each fix the GPS receiver will
  451. assert 1PPS first and ship sentences reporting time of fix
  452. second (and the sentence burst will end before the next 1PPS). Every
  453. GPS we know of does things in this order. (However, on some very old
  454. GPSes that defaulted to 4800 baud, long sentence bursts -- notably
  455. those containing a skyview -- could slop over into the next second.)
  456. If you ever encounter an exception, it should manifest as reported
  457. times that look like they're from the future and require a negative
  458. fudge. If this ever happens, please report the device make and model
  459. to the GPSD maintainers, so we can flag it in our GPS hardware
  460. database.
  461. There is another possible cause of small negative offsets which
  462. shows up on the GR-601W: implementation bugs in your USB driver,
  463. combining with quantization by the USB poll interval. This
  464. doesn't mean the u-blox 6 inside it is actually emitting PPS
  465. after the GPS timestamp is shipped.
  466. In order to present the smallest possible attack surface to
  467. privilege-escalation attempts, gpsd, if run as root, drops its root
  468. privileges very soon after startup - just after it has opened any
  469. serial device paths passed on the command line.
  470. Thus, KPPS can only be used with devices passed that way, not with
  471. GPSes that are later presented to gpsd by the hotplug system. Those
  472. hotplug devices may, however, be able to use plain, non-kernel
  473. PPS. gpsd tries to automatically fall back to this when absence of
  474. root permissions makes KPPS unavailable.
  475. In general, if you start gpsd as other than root, the following things
  476. will happen that degrade the accuracy of reported time:
  477. 1. Devices passed on the command line will be unable to use KPPS and
  478. will fall back to the same plain PPS that all hotplug devices must
  479. use, increasing the associated error from ~1 uSec to about ~5 uSec.
  480. 2. gpsd will be unable to renice itself to a higher priority. This
  481. action helps protect it against jitter induced by variable system
  482. load. It's particularly important if your NTP server is a general-use
  483. computer that's also handling mail or web service or development.
  484. 3. The way you have to configure ntpd and chrony will change away
  485. from what we show you here; ntpd will need to be told different
  486. shared-memory segment numbers, and chronyd will need a different
  487. socket location.
  488. 4. gpsd will be unable to change to user nobody. This means gpsd will
  489. paradoxically run with higher privileges than if it was started as root.
  490. This increases the attack surface and decreases your security.
  491. You may also find gpsd can't open serial devices at all if your
  492. OS distribution has done "secure" things with the permissions.
  493. == Feeding NTPD from GPSD
  494. Most Unix systems get their time service through ntpd, a very old and
  495. stable open-source software suite which is the reference
  496. implementation of NTP. The project home page is <<NTP.ORG>>. We
  497. recommend using NTPsec, a recent fork that is improved and
  498. security-hardened <<NTPSEC.ORG>>.
  499. When gpsd receives a sentence with a timestamp, it packages the
  500. received timestamp with current local time and sends it to a
  501. shared-memory segment with an ID known to ntpd, the network time
  502. synchronization daemon. If ntpd has been properly configured to
  503. receive this message, it will be used to correct the system clock.
  504. When in doubt, the preferred method to start your timekeeping is:
  505. -----------------------------------------------------------------------------
  506. $ su -
  507. # killall -9 gpsd ntpd
  508. # gpsd -n /dev/ttyXX
  509. # sleep 2
  510. # ntpd -gN
  511. # sleep 2
  512. # cgps
  513. -----------------------------------------------------------------------------
  514. where /dev/ttyXX is whatever 1PPS-capable device you have. In a
  515. binary-package-based Linux distribution it is probable that ntpd
  516. will already have been launched at boot time.
  517. It's best to have gpsd start first. That way when ntpd restarts it has
  518. a good local time handy. If ntpd starts first, it will set the local
  519. clock using a remote, probably pool, server. Then ntpd has to spend a
  520. whole day slowly resyncing the clock.
  521. If you're using dhcp3-client to configure your system, make sure
  522. you disable /etc/dhcp3/dhclient-exit-hooks.d/ntp, as dhclient would
  523. restart ntpd with an automatically created ntp.conf otherwise -- and
  524. gpsd would not be able to talk with ntpd anymore.
  525. While gpsd may be runnable as non-root, you will get significantly
  526. better accuracy of time reporting in root mode; the difference, while
  527. almost certainly insignificant for feeding Stratum 1 time to clients
  528. over the Internet, may matter for PTP service over a LAN. Typically
  529. only root can access kernel PPS, whereas in non-root mode you're limited to
  530. plain PPS (if that feature is available). As noted in the previous
  531. section on 1PPS quality issues, this difference has performance
  532. implications.
  533. The rest of these setup instructions will assume that you are starting
  534. gpsd as root, with occasional glances at the non-root case.
  535. Now check to see if gpsd has correctly attached the shared-memory
  536. segments in needs to communicate with ntpd. ntpd's rules for the
  537. creation of these segments are:
  538. Segments 0 and 1::
  539. Permissions are 0600 -- other programs can only read and
  540. write this segment when running as root.
  541. Segments 2, 3 and above::
  542. Permissions are 0666 -- other programs can read
  543. and write as any user. If ntpd has been
  544. configured to use these segments, any
  545. unprivileged user is allowed to provide data
  546. for synchronization.
  547. Because gpsd can be started either as root or non-root, it checks and
  548. attaches the more privileged segment pair it can -- either 0 and 1 or 2
  549. and 3.
  550. For each GPS receiver that gpsd controls, it will use the attached ntpshm
  551. segments in pairs (for coarse clock and pps source, respectively)
  552. starting from the first found segments.
  553. To debug, try looking at the live segments this way:
  554. -----------------------------------------------------------------------------
  555. # ipcs -m
  556. -----------------------------------------------------------------------------
  557. If gpsd was started as root, the results should look like this:
  558. -----------------------------------------------------------------------------
  559. ------ Shared Memory Segments --------
  560. key shmid owner perms bytes nattch status
  561. 0x4e545030 0 root 700 96 2
  562. 0x4e545031 32769 root 700 96 2
  563. 0x4e545032 163842 root 666 96 1
  564. 0x4e545033 196611 root 666 96 1
  565. -----------------------------------------------------------------------------
  566. For a bit more data try this:
  567. -----------------------------------------------------------------------------
  568. cat /proc/sysvipc/shm
  569. -----------------------------------------------------------------------------
  570. If gpsd cannot open the segments, check that you are not running SELinux
  571. or apparmor. Either may require you to configure a security exception.
  572. If you see the shared segments (keys 1314148400 -- 1314148403), and
  573. no gpsd or ntpd is running then try removing them like this:
  574. -----------------------------------------------------------------------------
  575. # ipcrm -M 0x4e545030
  576. # ipcrm -M 0x4e545031
  577. # ipcrm -M 0x4e545032
  578. # ipcrm -M 0x4e545033
  579. -----------------------------------------------------------------------------
  580. Here is a minimal sample ntp.conf configuration to work with GPSD run
  581. as root, telling ntpd how to read the GPS notifications
  582. -----------------------------------------------------------------------------
  583. pool us.pool.ntp.org iburst
  584. driftfile /var/lib/ntp/ntp.drift
  585. logfile /var/log/ntp.log
  586. restrict default kod nomodify notrap nopeer noquery
  587. restrict -6 default kod nomodify notrap nopeer noquery
  588. restrict 127.0.0.1 mask 255.255.255.0
  589. restrict -6 ::1
  590. # GPS Serial data reference (NTP0)
  591. server 127.127.28.0
  592. fudge 127.127.28.0 time1 0.9999 refid GPS
  593. # GPS PPS reference (NTP1)
  594. server 127.127.28.1 prefer
  595. fudge 127.127.28.1 refid PPS
  596. -----------------------------------------------------------------------------
  597. The number "0.9999" is a placeholder, to be explained shortly. It
  598. is *not a number to be used in production* - it's too large. If you
  599. can't replace it with a real value, it would be best to leave out the
  600. clause entirely so the entry looks like:
  601. -----------------------------------------------------------------------------
  602. fudge 127.127.28.0 refid GPS
  603. -----------------------------------------------------------------------------
  604. This is equivalent to declaring a time1 of 0.
  605. The pool statement adds a variable number of servers (often 10) as
  606. additional time references needed by ntpd for redundancy and to give you
  607. a reference to see how well your local GPS receiver is performing. If
  608. you are outside of the USA replace the pool servers with one in your
  609. local area. See <<USE-POOL>> for further information.
  610. The pool statement, and the driftfile and logfile declarations after it,
  611. will not be strictly necessary if the default ntp.conf that your
  612. distribution supplies gives you a working setup. The two pairs of
  613. server and fudge declarations are the key.
  614. ntpd can be used in Denial of Service (DoS) attacks. To prevent that,
  615. but still allow clients to request the local time, be sure the 'restrict'
  616. statements are in your ntpd config file. For more information see
  617. <<CVE-2009-3563>>.
  618. Users of ntpd versions older than revision ntp-4.2.5p138 should instead use
  619. this ntp.conf, when gpsd is started as root:
  620. -----------------------------------------------------------------------------
  621. pool us.pool.ntp.org iburst
  622. driftfile /var/lib/ntp/ntp.drift
  623. logfile /var/log/ntp.log
  624. restrict default kod nomodify notrap nopeer noquery
  625. restrict -6 default kod nomodify notrap nopeer noquery
  626. restrict 127.0.0.1 mask 255.255.255.0
  627. restrict -6 ::1
  628. # GPS Serial data reference (NTP0)
  629. server 127.127.28.0 minpoll 4 maxpoll 4
  630. fudge 127.127.28.0 time1 0.9999 refid GPS
  631. # GPS PPS reference (NTP1)
  632. server 127.127.28.1 minpoll 4 maxpoll 4 prefer
  633. fudge 127.127.28.1 refid PPS
  634. -----------------------------------------------------------------------------
  635. Users of ntpd versions prior to ntp-4.2.5 do not have the "pool" option.
  636. Alternative configurations exist, but it is recommended that you upgrade
  637. ntpd, if feasible.
  638. The magic pseudo-IP address 127.127.28.0 identifies unit 0 of the ntpd
  639. shared-memory driver (NTP0); 127.127.28.1 identifies unit 1 (NTP1).
  640. Unit 0 is used for in-band message timestamps and unit 1 for the (more
  641. accurate, when available) time derived from combining in-band message
  642. timestamps with the out-of-band PPS synchronization pulse. Splitting
  643. these notifications allows ntpd to use its normal heuristics to weight
  644. them.
  645. Different units -- 2 (NTP2) and 3 (NTP3), respectively -- must be used
  646. when gpsd is not started as root. Some GPS HATs put PPS time on a GPIO
  647. pin and will also use unit 2 (NTP2) for the PPS time correction.
  648. With this configuration, ntpd will read the timestamp posted by gpsd
  649. every 64 seconds (16 if non-root) and send it to unit 0.
  650. The number after the parameter time1 (0.9999 in the example above) is a
  651. "fudge", offset in seconds. It's an estimate of the latency between
  652. the time source and the 'real' time. You can use it to compensate out
  653. some of the fixed delays in the system. An 0.9999 fudge would be
  654. ridiculously large.
  655. You may be able to find a value for the fudge by looking at the entry
  656. for your GPS receiver type on <<HARDWARE>>. Later in this document
  657. we'll explain methods for estimating a fudge factor on unknown
  658. hardware.
  659. There is nothing magic about the refid fields; they are just labels
  660. used for generating reports. You can name them anything you like.
  661. When you start gpsd, it will wait for a few good fixes before attempting
  662. to process PPS. You should run cgps to verify your GPS receiver has a
  663. 3D lock before worrying about timekeeping.
  664. After starting (as root) ntpd, then gpsd, a listing similar to the one
  665. below should appear as the output of the command "ntpq -p" (after
  666. allowing the GPS receiver to acquire a 3D fix). This may take up to
  667. 30 minutes if your GPS receiver has to cold-start or has a poor
  668. skyview.
  669. -----------------------------------------------------------------------------
  670. remote refid st t when poll reach delay offset jitter
  671. ==============================================================================
  672. xtime-a.timefreq .ACTS. 1 u 40 64 377 59.228 -8.503 0.516
  673. -bonehed.lcs.mit 18.26.4.106 2 u 44 64 377 84.259 4.194 0.503
  674. +clock.sjc.he.ne .CDMA. 1 u 41 64 377 23.634 -0.518 0.465
  675. +SHM(0) .GPS. 0 l 50 64 377 0.000 6.631 5.331
  676. -----------------------------------------------------------------------------
  677. The line with refid ".GPS." represents the in-band time reports from
  678. your GPS receiver. When you are getting PPS then it may look like
  679. this:
  680. -----------------------------------------------------------------------------
  681. remote refid st t when poll reach delay offset jitter
  682. ==============================================================================
  683. xtime-a.timefreq .ACTS. 1 u 40 64 377 59.228 -8.503 0.516
  684. -bonehed.lcs.mit 18.26.4.106 2 u 44 64 377 84.259 4.194 0.503
  685. +clock.sjc.he.ne .CDMA. 1 u 41 64 377 23.634 -0.518 0.465
  686. +SHM(0) .GPS. 0 l 50 64 377 0.000 6.631 5.331
  687. *SHM(1) .PPS. 0 l 49 64 377 0.000 0.222 0.310
  688. -----------------------------------------------------------------------------
  689. Note the additional ".PPS." line.
  690. If the value under "reach" for the SHM lines remains zero, check that
  691. gpsd is running; cgps reports a 3D fix; and the '-n' option was used.
  692. Some GNSS receivers specialized for time service can report time with signal
  693. lock on only one satellite, but with most devices a 3D fix is
  694. required.
  695. When the SHM(0) line does not appear at all, check your ntp.conf and
  696. the system logs for error messages from ntpd.
  697. Notice the 1st and 3rd servers, stratum 1 servers, disagree by more than
  698. 8 mSec. The 1st and 2nd servers disagree by over 12 mSec. Our local
  699. PPS reference agrees to the clock.sjc.he.net server within the expected
  700. jitter of the GR-601W in use.
  701. When no other servers or local reference clocks appear in the NTP
  702. configuration, the system clock will lock onto the GPS clock, but this
  703. is a fragile setup -- you can lose your only time reference if the GPS
  704. receiver is temporarily unable to get satellite lock.
  705. You should always have at least two (preferably four) fallback servers
  706. in your ntpd.conf for proper ntpd operation, in case your GPS receiver
  707. fails to report time. The 'pool' command makes this happen. And
  708. you'll need to adjust the offsets (fudges) in your ntp.conf so the
  709. SHM(0) time is consistent with your other servers (and other local
  710. reference clocks, if you have any). We'll describe how to diagnose and
  711. tune your server configuration in a later section.
  712. NTP is a cluster protocol. It sorts time sources into clusters, picks
  713. the best cluster, then the best source in that cluster, for its time
  714. source. By default, NTPsec requires 3 "sane" time sources before
  715. serving time. This is set with the *minsane* option. Not at all
  716. recommended, but you can force *ntpd* to rely sole on your PPS source by
  717. adjusting *minsane* like this in your ntp.conf:
  718. ----
  719. tos minclock 1 minsane 1
  720. ----
  721. Also note that after cold-starting ntpd it may calibrate for up to 15
  722. minutes before it starts adjusting the clock. Because the frequency
  723. error estimate ("drift") that NTP uses isn't right when you first
  724. start NTP, there will be a phase error that persists while the
  725. frequency is estimated. So if your clock is a little slow, then it
  726. will keep getting behind, and the positive offset will cause NTP to
  727. adjust the phase forward and also increase the frequency offset error.
  728. After a day or so or maybe less the frequency estimate will be very
  729. close and there won't be a persistent offset.
  730. The GPSD developers would like to receive information about the
  731. offsets (fudges) observed by users for each type of receiver. If your
  732. GPS receiver is not present in <<HARDWARE>>, or doesn't have a
  733. recommended fudge, or you see a fudge value very different from what's
  734. there, send us the output of the "ntpq -p" command and the make and
  735. type of receiver.
  736. == Feeding chrony from GPSD
  737. chrony is an alternative open-source implementation of NTP service,
  738. originally designed for systems with low-bandwidth or intermittent
  739. TCP/IP service. It interoperates with ntpd using the same NTP
  740. protocols. Unlike ntpd which is designed to always be connected to
  741. multiple internet time sources, chrony is designed for long periods
  742. of offline use. Like ntpd, it can either operate purely as a client
  743. or provide time service. The chrony project has a home page at
  744. <<CHRONY>>. Its documentation includes an instructive feature comparison
  745. with ntpd at <<CHRONY-COMPARE>>.
  746. gpsd, when run as root, may feed time information to chronyd using
  747. sockets. The sockets are named /run/chrony.XXXX.sock. Where XXXX is
  748. replaced by the basenames of the device names gpsd is using. If your
  749. receiver outputs serial data on /dev/ttyS0, then the corresponding
  750. socket is /run/chrony.ttyS0.sock. If your PPS is on /dev/pps0, then the
  751. corresponding socket is /run/chrony.pps0.sock.
  752. Older systems may use the /var/run directory instead of /run. If gpsd
  753. can not open the sockets there, it falls back to try /tmp.
  754. No gpsd configuration is required to talk to chronyd. chronyd is
  755. configured using the file /etc/chrony.conf or /etc/chrony/chrony.conf.
  756. Check your distributions documentation for the correct location.
  757. To get chronyd to connect to gpsd using the socket method add the
  758. following lines your chrony.conf file. Except, replace XXXX with
  759. the basename of your device's serial port, often ttyS0, ttyACM0, or
  760. ttyAMA0. Replace YYYY with the basename of your PPS device, usually
  761. pps0.
  762. When running as root:
  763. -----------------------------------------------------------------------------
  764. server 0.us.pool.ntp.org
  765. server 1.us.pool.ntp.org
  766. server 2.us.pool.ntp.org
  767. server 3.us.pool.ntp.org
  768. driftfile /var/lib/chrony/drift
  769. allow
  770. # set larger delay to allow the NMEA source to overlap with
  771. # the other sources and avoid the falseticker status
  772. refclock SOCK /run/chrony.XXXX.sock refid GPS precision 1e-1 offset 0.9999
  773. refclock SOCK /run/chrony.YYYY.sock refid PPS precision 1e-7
  774. -----------------------------------------------------------------------------
  775. Older systems may use the /var/run directory for the socket file instead
  776. of /run. /run is compliant with FHS 3.0. If that file can not be opened
  777. then gpsd falls back to trying in /tmp:
  778. -----------------------------------------------------------------------------
  779. refclock SOCK /tmp/chrony.XXXX.sock refid GPS precision 1e-1 offset 0.9999
  780. refclock SOCK /tmp/chrony.YYYY.sock refid PPS precision 1e-7
  781. -----------------------------------------------------------------------------
  782. You can also get gpsd to connect to chronyd using the basic ntpd
  783. compatible SHM method. To use that instead of sockets, add these lines
  784. to the basic chrony.conf file:
  785. -----------------------------------------------------------------------------
  786. server 0.us.pool.ntp.org
  787. server 1.us.pool.ntp.org
  788. server 2.us.pool.ntp.org
  789. server 3.us.pool.ntp.org
  790. driftfile /var/lib/chrony/drift
  791. allow
  792. # set larger delay to allow the NMEA source to overlap with
  793. # the other sources and avoid the falseticker status
  794. refclock SHM 0 refid GPS precision 1e-1 offset 0.9999 delay 0.2
  795. refclock SHM 1 refid PPS precision 1e-7
  796. -----------------------------------------------------------------------------
  797. You need to add the "precision 1e-7" on the SHM 1 line as chronyd fails
  798. to read the precision from the SHM structure. Without knowing the high
  799. precision of the PPS on SHM 1 it may not place enough importance on its
  800. data.
  801. If you are outside of the USA replace the pool servers with one in your
  802. local area. See <<USE-POOL>> for further information.
  803. The offset option is functionally like ntpd's time1 option, used to
  804. correct known and constant latency.
  805. The 'allow' option allows anyone on the internet to query your server's
  806. time.
  807. See the chrony man page for more detail on the configuration options
  808. <<CHRONY-MAN>>.
  809. Finally note that chronyd needs to be started before gpsd so the
  810. sockets are ready before gpsd starts up.
  811. If running as root, the preferred starting procedure is:
  812. -----------------------------------------------------------------------------
  813. $ su -
  814. # killall -9 gpsd chronyd
  815. # chronyd -f /etc/chrony/chrony.conf
  816. # sleep 2
  817. # gpsd -n /dev/ttyXX
  818. # sleep 2
  819. # cgps
  820. -----------------------------------------------------------------------------
  821. After you have verified with cgps that your GPS receiver has a good 3D
  822. lock you can check that gpsd is outputting good time by running ntpshmmon.
  823. -----------------------------------------------------------------------------
  824. # ntpshmmon
  825. ntpshmmon version 1
  826. # Name Seen@ Clock Real L Prec
  827. sample NTP0 1461537438.593729271 1461537438.593633306 1461537438.703999996 0 -1
  828. sample NTP1 1461537439.000421494 1461537439.000007374 1461537439.000000000 0 -20
  829. sample NTP0 1461537439.093844900 1461537438.593633306 1461537438.703999996 0 -1
  830. sample NTP0 1461537439.621309382 1461537439.620958240 1461537439.703999996 0 -1
  831. sample NTP1 1461537440.000615395 1461537439.999994105 1461537440.000000000 0 -20
  832. sample NTP0 1461537440.122079148 1461537439.620958240 1461537439.703999996 0 -1
  833. ^C
  834. -----------------------------------------------------------------------------
  835. If you see only "NTP2", instead, you forgot to go root before starting gpsd.
  836. Once ntpshmmon shows good time data you can see how chrony is doing by
  837. running 'chronyc sources'. Your output will look like this:
  838. -----------------------------------------------------------------------------
  839. # chronyc sources
  840. 210 Number of sources = 7
  841. MS Name/IP address Stratum Poll Reach LastRx Last sample
  842. ===============================================================================
  843. #- GPS 0 4 377 12 +3580us[+3580us] +/- 101ms
  844. #* PPS 0 4 377 10 -86ns[ -157ns] +/- 181ns
  845. ^? vimo.dorui.net 3 6 377 23 -123ms[ -125ms] +/- 71ms
  846. ^? time.gac.edu 2 6 377 24 -127ms[ -128ms] +/- 55ms
  847. ^? 2001:470:1:24f::2:3 2 6 377 24 -122ms[ -124ms] +/- 44ms
  848. ^? 142.54.181.202 2 6 377 22 -126ms[ -128ms] +/- 73ms
  849. -----------------------------------------------------------------------------
  850. The stratum is as in ntpq. The Poll is how many seconds elapse between samples.
  851. The Reach is as in ntpq. LastRx is the time since the last successful
  852. sample. Last sample is the offset and jitter of the source.
  853. To keep chronyd from preferring NMEA time over PPS time, you can add an
  854. overlarge fudge to the NMEA time. Or add the suffix 'noselect' so it
  855. is never used, just monitored.
  856. == Performance Tuning
  857. This section is general and can be used with either ntpd or chronyd.
  858. We'll have more to say about tuning techniques for the specific
  859. implementations in later sections.
  860. The clock crystals used in consumer electronics have two properties we
  861. are interested in: accuracy and stability. *Accuracy* is how well the
  862. measured frequency matches the number printed on the can. *Stability*
  863. is how well the frequency stays the same even if it isn't accurate.
  864. (Long term aging is a third property that is interesting, but ntpd and
  865. chrony both a use a drift history that is relatively short; thus,
  866. this is not a significant cause of error.)
  867. Typical specs for oscillator packages are 20, 50, 100 ppm. That includes
  868. everything; initial accuracy, temperature, supply voltage, aging, etc.
  869. With a bit of software, you can correct for the inaccuracy. ntpd and
  870. chrony both call it *drift*. It just takes some extra low order bits
  871. on the arithmetic doing the time calculations. In the simplest case,
  872. if you thought you had a 100 MHz crystal, you need to change that to
  873. something like 100.000324. The use of a PPS signal from gpsd
  874. contributes directly to this measurement.
  875. Note that a low drift contributes to stability, not necessarily accuracy.
  876. The major source of instability is temperature. Ballpark is the drift
  877. changes by 1 PPM per degree C. This means that the drift does not stay
  878. constant, it may vary with a daily and yearly pattern. This is why the
  879. value of drift the ntpd uses is calculated over a (relatively) short time.
  880. So how do we calculate the drift? The general idea is simple.
  881. Measure the time offset every N seconds over a longer window of time
  882. T, plot the graph, and fit a straight line. The slope of that line is
  883. the drift. The units cancel out. Parts-per-million is a handy scale.
  884. How do you turn that hand waving description into code? One easy way
  885. is to set N=2 and pick the right T, then run the answer through a
  886. low pass filter. In that context, there are two competing sources of
  887. error. If T is too small, the errors on each individual measurement
  888. of the offset time will dominate. If T is too big, the actual drift
  889. will change while you are measuring it. In the middle is a sweet
  890. spot. (For an example, see <<ADEV-PLOT>>.)
  891. Both ntpd and chrony use this technique; ntpd also uses a more
  892. esoteric form of estimation called a "PLL/FLL hybrid loop". How T and N are
  893. chosen is beyond the scope of this HOWTO and varies by implementation
  894. and tuning parameters.
  895. If you turn on the right logging level ("statistics loopstats peerstats"
  896. for ntpd, "log measurements tracking" for chronyd), that will record
  897. both offset, drift, and the polling interval. The ntpd stats are easy to
  898. feed to gnuplot, see the example script in the GPSD contrib directory.
  899. The most important value is the offset reported in the 3rd field in
  900. loopstats and the last field in tracking.log. With gnuplot you can
  901. compare them (after concatenating the rotated logs):
  902. -----------------------------------------------------------------------------
  903. plot "tracking.log" using 7 with lines, "loopstats" using 3 with lines
  904. -----------------------------------------------------------------------------
  905. While your NTP daemon (ntpd or chrony) is adjusting the polling
  906. interval, it is assuming that the drift is not changing. It gets
  907. confused if your drift changes abruptly, say because you started some
  908. big chunk of work on a machine that's usually idle and that raises the
  909. temperature.
  910. Your NTP daemon writes out the drift every hour or so. (Less often if
  911. it hasn't changed much to reduce the workload on flash file systems.)
  912. On startup, it reloads the old value.
  913. If you restart the daemon, it should start with a close old drift
  914. value and quickly converge to the newer slightly different value. If
  915. you reboot, expect it to converge to a new/different drift value and
  916. that may take a while depending on how different the basic calibration
  917. factors are.
  918. === ARP is the sound of your server choking
  919. By default, ntpd and chronyd poll remote servers every 64 seconds. This
  920. is an unfortunate choice. Linux by default only keeps an ARP table
  921. entry for 60 seconds, anytime thereafter it may be flushed.
  922. If the ARP table has flushed the entry for a remote peer or server then
  923. when the NTP server sends a request to the remote server an entire ARP
  924. cycle will be added to the NTP packet round trip time (RTT). This will
  925. throw off the time measurements to servers on the local lan.
  926. On a Raspberry Pi ARP has been shown to impact the remote offset by up to
  927. 600 uSec in some rare cases.
  928. The solution is the same for both ntpd and chronyd, add the "maxpoll 5"
  929. command to any 'server" or "peer directive. This will cause the maximum
  930. polling period to be 32 seconds, well under the 60 second ARP timeout.
  931. === Watch your temperatures
  932. The stability of the system clock is very temperature dependent. A one
  933. degree change in room temperature can create 0.1 ppm of clock frequency
  934. change. One simple way to see the effect is to place your running
  935. NTP server inside bubble wrap. The time will take a quick and noticeable
  936. jump.
  937. If you leave your NTP server in the bubble wrap you will notice some
  938. improved local and remote offsets.
  939. === Power saving is not your friend
  940. Normally enabling power saving features is a good thing: it saves you power.
  941. But when your CPU changes power saving modes (cstates for Intel CPUs) the
  942. impact on PPS timing is noticeable. For some reason the NO_HZ kernel
  943. mode has a similar bad effect on timekeeping.
  944. To improve your timekeeping, turn off both features on Intel CPUs by
  945. adding this to your boot command line:
  946. -----------------------------------------------------------------------------
  947. nohz=off intel_idle.max_cstate=0
  948. -----------------------------------------------------------------------------
  949. For ARM, be sure NO_HZ is off:
  950. -----------------------------------------------------------------------------
  951. nohz=off
  952. -----------------------------------------------------------------------------
  953. You will also need to select the 'performance' CPU governor to keep your
  954. CPU set to the maximum speed for continuous usage. How you see and set
  955. your governor will be distribution specific. The easiest way it to
  956. recompile your kernel to only provide the performance governor.
  957. == NTP tuning and performance details
  958. This section deals specifically with ntpd. It discusses algorithms
  959. used by the ntpd suite to measure and correct the system time. It is not
  960. directly applicable to chronyd, although some design considerations
  961. may be similar.
  962. You can't optimize what you can't visualize. The easiest way to
  963. visualize ntpd performance is with ntpviz from <<NTPSEC.ORG>>. Once you
  964. are regularly graphing your server performance it is much easier to see
  965. the results of changes.
  966. === NTP performance tuning
  967. For good time stability, you should always have at least four other
  968. servers in your ntpd or chrony configuration besides your GPS receiver
  969. -- in case, for example, your GPS receiver is temporarily unable to achieve
  970. satellite lock, or has an attack of temporary insanity. You can find
  971. public NTP servers to add to your configuration at <<USE-POOL>>.
  972. To minimize latency variations, use the national and regional
  973. pool domains for your country and/or nearby ones. Your ntp.conf
  974. configuration line should probably look like this:
  975. -----------------------------------------------------------------------------
  976. pool us.pool.ntp.org iburst
  977. -----------------------------------------------------------------------------
  978. Where "us" may be replaced by one of the zone/country codes the Pool
  979. project supports (list behind the "Global" link at <<ZONES>>). The
  980. "pool" tag expands to four randomly chosen servers by default. "iburst"
  981. implements a fast start algorithm that also weeds out bad servers.
  982. Note that a server can be a poor performer (what the NTP documentation
  983. colorfully calls a "falseticker") for any of three reasons. It may be
  984. shipping bad time, or the best routes between you and it has large
  985. latency variations (jitter), or it may have a time-asymmetric route,
  986. to you (that is, B-to-A time is on average very different from A-to-B
  987. time). Asymmetric routing is the most common cause of serious
  988. problems.
  989. The standard tool for tuning ntpd is "ntpq" ("NTP query program"). To
  990. show a list of all servers declared in ntp.conf and their statistics,
  991. invoke it with the "-p" option. On a sample system configured with 7
  992. servers from the NTP pool project and one PPS GPS receiver attached
  993. via RS232, this is the output:
  994. ------------------------------------------------------------------------
  995. $ ntpq -p
  996. remote refid st t when poll reach delay offset jitter
  997. ========================================================================
  998. -arthur.testserv 162.23.41.56 2 u 62 64 377 5.835 -1.129 8.921
  999. -ntppublic.uzh.c 130.60.159.7 3 u 62 64 377 6.121 -4.102 6.336
  1000. -smtp.irtech.ch 162.23.41.56 2 u 35 64 377 15.521 -1.677 8.678
  1001. +time2.ethz.ch .PPS. 1 u 27 64 377 5.938 -1.713 16.404
  1002. -callisto.mysnip 192.53.103.104 2 u 53 64 377 49.357 -0.274 5.125
  1003. -shore.naturalne 122.135.113.81 3 u 22 64 377 14.676 -0.528 2.601
  1004. -ntp.univ-angers 195.220.94.163 2 u 41 64 377 40.678 -1.847 13.391
  1005. +SHM(0) .GPS. 0 l 4 64 377 0.000 34.682 7.952
  1006. *SHM(1) .PPS. 0 l 3 64 377 0.000 -2.664 0.457
  1007. ------------------------------------------------------------------------
  1008. The interesting columns are "remote", "st", "reach" and "offset".
  1009. "remote" is the name of the remote NTP server. The character in its
  1010. first column shows its current state: "-" or "x" for out-of-tolerance
  1011. servers, "+" for good servers ("truechimers"), and "*" for the one good
  1012. server currently used as the primary reference. The calculations used to
  1013. determine a server's state are outside the scope of this document;
  1014. details are available in NTPv4 RFC 5905.
  1015. "st" shows the remote server's stratum.
  1016. "reach" is the octal representation of the remote server's reachability.
  1017. A bit is set if the corresponding poll of the server was successful,
  1018. i.e. the server returned a reply. New poll results are shifted in from
  1019. the least significant bit; results older than 8 polls are discarded. In
  1020. the absence of network problems, this should show "377".
  1021. "offset" shows the mean offset in the times reported between this local
  1022. host and the remote server in milliseconds. This is the value that can
  1023. be fudged with the "time1" parameter of the GPS server line in ntp.conf.
  1024. If the offset is positive, reduce the time1 value and vice versa.
  1025. The asterisk in this example indicates that ntpd has correctly
  1026. preferred '.PPS.' over '.GPS.', as it should. If for some reason it
  1027. locks on to GPS time as a preferred source, you can add an overlarge
  1028. fudge to the NMEA time to discourage it. Or add the suffix 'noselect'
  1029. so GPS time is never used, just monitored.
  1030. A more detailed description of the output is available at
  1031. <<NTPQ-OUTPUT>>.
  1032. In order to determine the correct GPS offset, do one of the following:
  1033. ==== Peerstats-based procedure
  1034. [start=1]
  1035. . Add these lines to ntp.conf:
  1036. -----------------------------------------------------------------------------
  1037. statsdir /var/log/ntpstats/
  1038. statistics peerstats
  1039. filegen peerstats file peerstats type day enable
  1040. -----------------------------------------------------------------------------
  1041. This enables logging of the peer server statistics.
  1042. . Make sure the directory exists properly. For ntpd as root do:
  1043. -----------------------------------------------------------------------------
  1044. # mkdir -p /var/log/ntpstats
  1045. # chown ntp:ntp /var/log/ntpstats
  1046. -----------------------------------------------------------------------------
  1047. . Start ntpd and let it run for at least four hours.
  1048. Periodically check progress with "ntpq -p" and wait
  1049. until change has settled out.
  1050. . Calculate the average GPS offset using this script (a copy is
  1051. included as contrib/ntpoffset in the GPSD distribution):
  1052. -----------------------------------------------------------------------------
  1053. awk '
  1054. /127\.127\.28\.0/ { sum += $5 * 1000; cnt++; }
  1055. END { print sum / cnt; }
  1056. ' </var/log/ntpstats/peerstats
  1057. -----------------------------------------------------------------------------
  1058. This prints the average offset.
  1059. . Adjust the "time1" value for unit 0 of your ntp.conf (the non-PPS
  1060. channel) by subtracting the average offset from step 4.
  1061. . Restart ntpd.
  1062. ==== Loopstats-based procedure
  1063. Recall that magic pseudo-IP address 127.127.28.0 identifies unit 0 of
  1064. the ntpd shared-memory driver (NTP0); 127.127.28.1 identifies unit
  1065. 1 (NTP1). Unit 0 is used for in-band message timestamps (IMT) and unit
  1066. 1 for the (more accurate, when available) time derived from combining
  1067. IMT with the out-of-band PPS synchronization pulse. Splitting these
  1068. notifications allows ntpd to use its normal heuristics to weight them.
  1069. We assume that the 1PPS signal, being just one bit long, and directly
  1070. triggering an interrupt, is always on time (sic). Correcting for latency
  1071. in the 1PPS signal is beyond the scope of this document. The IMT,
  1072. however, may be delayed, due to it being emitted, copied to shared
  1073. memory, etc.
  1074. Based on advice and script fragments on the GPSD list, the following
  1075. may help to calculate the 'time1' factor. You may need to modify
  1076. these scripts for your particular setup.
  1077. These scripts are for the combination of GPSD and ntpd. If you use
  1078. chronyd, you *will* need to modify these, at the least.
  1079. ==== ntpviz procedure
  1080. If all this calculating and graphing looks painful, then grab a copy
  1081. of ntpviz from <<NTPSEC.ORG>>. ntpviz generates lots of pretty graphs
  1082. and html pages. It even calculates the correct IMT offset, and other
  1083. performance metrics for you.
  1084. ===== Format of the loopstats and peerstats files
  1085. The following is incorporated from the ntpd website, see <<NTP-MONOPT>>
  1086. .loopstats
  1087. Record clock discipline loop statistics. Each system clock update
  1088. appends one line to the loopstats file set:
  1089. Example: 50935 75440.031 0.000006019 13.778 0.000351733 0.013380 6
  1090. |===========================================================
  1091. |Item |Units |Description
  1092. |50935 |MJD |date
  1093. |75440.031 |s |time past midnight (UTC)
  1094. |0.000006019 |s |clock offset
  1095. |13.778 |PPM |frequency offset
  1096. |0.000351733 |s |RMS jitter
  1097. |0.013380 |PPM |RMS frequency jitter (aka wander)
  1098. |6 |log2 s |clock discipline loop time constant
  1099. |===========================================================
  1100. .peerstats
  1101. Record peer statistics. Each NTP packet or reference clock update
  1102. received appends one line to the peerstats file set:
  1103. Example: 48773 10847.650 127.127.4.1 9714 -0.001605376 0.000000000 0.001424877 0.000958674
  1104. |================================================
  1105. |Item |Units |Description
  1106. |48773 |MJD |date
  1107. |10847.650 |s |time past midnight (UTC)
  1108. |127.127.4.1 |IP |source address
  1109. |9714 |hex |status word
  1110. |-0.001605376 |s |clock offset
  1111. |0.000000000 |s |roundtrip delay
  1112. |0.001424877 |s |dispersion
  1113. |0.000958674 |s |RMS jitter
  1114. |================================================
  1115. ===== Measurement of delay
  1116. There are three parts to measuring and correcting for the delay in
  1117. processing the 1PPS signal.
  1118. 1. Running ntpd without using the IMT (but using the 1PPS time).
  1119. 2. Measuring the delay between the two messages.
  1120. 3. Applying the correction factor.
  1121. We assume that you have successfully integrated GPSD with ntpd already.
  1122. You should also have a decent set of NTP servers you are syncing to.
  1123. [start=1]
  1124. . Running ntpd without IMT
  1125. Locate the line in your ntp.conf that refers to the SHM0 segment and
  1126. append 'noselect' to it. As an example, the first two lines in the sample
  1127. above will become:
  1128. --------------------
  1129. server 127.127.28.0 minpoll 4 maxpoll 4 noselect
  1130. fudge 127.127.28.0 time1 0.420 refid GPS
  1131. --------------------
  1132. ntpd will now continue to monitor the IMT from GPSD, but not use it
  1133. for its clock selection algorithm. It will still write out statistics to
  1134. the peerstats file. Once ntpd is stable (a few hours or so), we can
  1135. process the peerstats file.
  1136. . Measuring the delay between the two messages.
  1137. From the 'peerstats' file, extract the lines corresponding to
  1138. 127.127.28.0
  1139. -----------
  1140. grep 127.127.28.0 peerstats > peerstats.shm
  1141. -----------
  1142. You can now examine the offset and jitter of the IMT. <<ANDY-POST>>
  1143. suggests the following gnuplot fragment (you will need to set output
  1144. options before plotting).
  1145. ----------------
  1146. set term gif
  1147. set output "fudge.gif"
  1148. ----------------
  1149. If your gnuplot has X11 support, and you do not wish to save the plot,
  1150. the above may not be required. Use:
  1151. ---------------
  1152. set term x11
  1153. ---------------
  1154. Now plot the GPSD shared memory clock deviations from the system
  1155. clock. (You will get the GPSD shared memory clock fudge value
  1156. estimate from this data when NTP has converged to your
  1157. satisfaction.)
  1158. ------------------
  1159. gnuplot> plot "peerstats.shm" using ($2):($5):($8) with yerrorbars
  1160. gnuplot> replot "peerstats.shm" using ($2):($5) with lines
  1161. ------------------
  1162. . Applying the correction factor.
  1163. By examining the plot generated above, you should be able to estimate
  1164. the offset between the 1PPS time and the GPS time.
  1165. If, for example, your estimate of the offset is -0.32s, your time1 fudge
  1166. value will be '0.32'. Note the change of sign.
  1167. === Polling Interval
  1168. ntpd seems to better use a PPS refclock when the polling interval is
  1169. as small as possible. The ntpd default minpoll is 6, and can be set to
  1170. as low as 4. NTPsec versions 0.9.5 and above of ntpd allow you to
  1171. set minpoll and maxpoll as low as 0. Changing minpoll from 4 to 3, 2, 1
  1172. or maybe even as low as 0, may reduce your PPS jitter by over a factor of 4.
  1173. Any change will require several hours for ntpd to converge with the new
  1174. settings. Use ntpviz to find the best poll interval for your system.
  1175. The value that yields the lowest jitter may not be the one that yields
  1176. the best Local Clock Frequency Offset.
  1177. -----------------------------------------------------------------------------
  1178. server 127.127.28.1 minpoll 0 maxpoll 0 prefer
  1179. -----------------------------------------------------------------------------
  1180. == Chrony performance tuning
  1181. The easiest way to determine the offset with chronyd is probably to
  1182. configure the source whose offset should be measured with the noselect
  1183. option and a long poll, let chronyd run for at least 4 hours and
  1184. observe the offset reported in the chronyc sourcestats output. If the
  1185. offset is unstable, wait longer. For example:
  1186. SHM 0 configured as:
  1187. refclock SHM 0 poll 8 filter 1000 noselect
  1188. -----------------------------------------------------------------------------
  1189. # chronyc sourcestats
  1190. 210 Number of sources = 6
  1191. Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
  1192. ==============================================================================
  1193. SHM0 21 9 85m 4.278 4.713 +495ms 8896us
  1194. SHM1 20 8 307 0.000 0.002 +0ns 202ns
  1195. mort.cihar.com 21 8 72m 0.148 0.798 +668us 490us
  1196. vps2.martinpoljak.net 6 4 17m -53.200 141.596 -24ms 15ms
  1197. ntp1.kajot.cz 25 16 91m -0.774 1.494 -11ms 1859us
  1198. ntp1.karneval.cz 17 10 89m 0.127 0.539 -4131us 574us
  1199. -----------------------------------------------------------------------------
  1200. In this case (Garmin 18x) the offset specified in the config for the
  1201. SHM 0 source should be around 0.495.
  1202. //FIXME: What more can we say about chronyd tuning?
  1203. == Providing local NTP service using PTP
  1204. By now if you have a good serial PPS signal your local clock should
  1205. have jitter on the order of 1 uSec. You do not want the hassle of
  1206. placing a GPS receiver on each of your local computers. So you
  1207. install chrony or ntp on your other hosts and configure them to use
  1208. your NTP PPS server as their local server.
  1209. With your best setup on a lightly loaded GigE network you find that your
  1210. NTP clients have jitter on the order of 150 uSec, or 150 times worse
  1211. than your master. Bummer, you want to do much better, so you look to
  1212. the Precision Time Protocol <<PTP>> for help. PTP is also known as IEEE
  1213. 1588.
  1214. With PTP you can easily synchronize NTP hosts to 5 uSec with some
  1215. generic NIC hardware and newer Linux kernels. Some of the Ethernet
  1216. drivers have been modified to time stamp network packets when sending and
  1217. receiving. This is done with the new SO_TIMESTAMPING socket option. No
  1218. hardware support is required.
  1219. A more recent addition is PTP Hardware Clock (PHC) support. This requires
  1220. hardware support in the NIC.
  1221. Software timestamping is more mature, available on more NICs, and almost
  1222. as accurate as hardware timestamping. Try it first. This HOWTO will
  1223. build on those results.
  1224. One final wrinkle before proceeding with PTP. Ethernet ports have
  1225. something called <<EEE>> (IEEE 802.3az). Percentage wise EEE can save
  1226. 50% of the Ethernet energy needs. Sadly this is 50% of an already small
  1227. energy usage. Only important in large data centers. EEE can be very
  1228. disruptive to timekeeping. Up to almost 1 Sec of errors in offset,
  1229. wander and jitter. To see if you have EEE enabled, and then turn it
  1230. off:
  1231. -----------------------------------------------------------------------------
  1232. # ethtool --show-eee eth0
  1233. EEE Settings for eth0:
  1234. EEE status: enabled - inactive
  1235. Tx LPI: 0 (us)
  1236. Supported EEE link modes: 100baseT/Full
  1237. 1000baseT/Full
  1238. Advertised EEE link modes: 100baseT/Full
  1239. 1000baseT/Full
  1240. Link partner advertised EEE link modes: Not reported
  1241. # ethtool --set-eee eth0 eee off
  1242. # ethtool --show-eee eth0
  1243. EEE Settings for eth1:
  1244. EEE status: disabled
  1245. Tx LPI: disabled
  1246. Supported EEE link modes: 100baseT/Full
  1247. 1000baseT/Full
  1248. Advertised EEE link modes: Not reported
  1249. Link partner advertised EEE link modes: Not reported
  1250. -----------------------------------------------------------------------------
  1251. === PTP with software timestamping
  1252. To start you need to verify that your running Linux kernel configuration
  1253. includes these two lines, or the same with "y" replaced by "m" to enable
  1254. the drivers as modules:
  1255. -----------------------------------------------------------------------------
  1256. CONFIG_NETWORK_PHY_TIMESTAMPING=y
  1257. PTP_1588_CLOCK=y
  1258. -----------------------------------------------------------------------------
  1259. Then you need to verify that your Ethernet driver supports PTP
  1260. by running this command as root:
  1261. -----------------------------------------------------------------------------
  1262. # ethtool -T eth0 | fgrep SOFTWARE
  1263. software-transmit (SOF_TIMESTAMPING_TX_SOFTWARE)
  1264. software-receive (SOF_TIMESTAMPING_RX_SOFTWARE)
  1265. software-system-clock (SOF_TIMESTAMPING_SOFTWARE)
  1266. -----------------------------------------------------------------------------
  1267. If the result includes those three lines then you have support for
  1268. software PTP timestamping. We will leave hardware timestamping
  1269. for later.
  1270. Next you will need the <<LINUX-PTP>> package, just follow the simple
  1271. instructions on their web page to download, compile and install on your
  1272. NTP server and its slaves. Be sure to also follow their instructions on
  1273. how to configure your Linux kernel.
  1274. In this setup we will just use the ptp4l program. This program measures
  1275. the delay and offset between a master and slaves and shares that information
  1276. with chronyd or ntpd using an SHM. Since gpsd also uses SHM be very careful
  1277. not to have the two SHM servers stepping on the same shmid.
  1278. If you are using ntpd, then add the last three lines below to your
  1279. master ntp.conf file to configure the SHM.
  1280. -----------------------------------------------------------------------------
  1281. # GPS Serial data reference (NTP0)
  1282. server 127.127.28.0
  1283. fudge 127.127.28.0 time1 0.9999 refid GPS
  1284. # GPS PPS reference (NTP1)
  1285. server 127.127.28.1 prefer
  1286. fudge 127.127.28.1 refid PPS
  1287. # local PTP reference (NTP2)
  1288. server 127.127.28.2
  1289. fudge 127.127.28.2 refid PTP
  1290. -----------------------------------------------------------------------------
  1291. If you are using chronyd, then add the last one line below to your
  1292. master chronyd.conf file to configure the SHM.
  1293. -----------------------------------------------------------------------------
  1294. refclock SHM 0 refid GPS precision 1e-1 offset 0.9999 delay 0.2
  1295. refclock SHM 1 refid PPS precision 1e-7
  1296. refclock SHM 2 refid PTP precision 1e-7
  1297. -----------------------------------------------------------------------------
  1298. To configure the master ptp4l, create a new file
  1299. /usr/local/etc/ptp4l.conf with these contents:
  1300. -----------------------------------------------------------------------------
  1301. [global]
  1302. # only syslog every 1024 seconds
  1303. summary_interval 10
  1304. # send to chronyd/ntpd using SHM 2
  1305. clock_servo ntpshm
  1306. ntpshm_segment 2
  1307. # set our priority high since we have PPS
  1308. priority1 10
  1309. priority2 10
  1310. [eth0]
  1311. -----------------------------------------------------------------------------
  1312. Now as root on the master, start the ptp4l daemon:
  1313. -----------------------------------------------------------------------------
  1314. # ethtool --set-eee eth0 eee off
  1315. # ptp4l -S -f /usr/local/etc/ptp4l.conf &
  1316. -----------------------------------------------------------------------------
  1317. Configuration of the master server is now complete. Now to configure
  1318. the slaves. If the slaves also have PPS then configure them as masters.
  1319. Otherwise you will stomp on your SHMs.
  1320. If you are using ntpd, then add the last three lines below to your
  1321. master ntp.conf file to configure your one and only SHM.
  1322. -----------------------------------------------------------------------------
  1323. # local PTP reference (NTP0)
  1324. server 127.127.28.0
  1325. fudge 127.127.28.0 refid PTP
  1326. -----------------------------------------------------------------------------
  1327. If you are using chronyd, then add the one line below to your master
  1328. chronyd.conf file to configure your one and only SHM.
  1329. -----------------------------------------------------------------------------
  1330. refclock SHM 0 refid PTP precision 1e-7
  1331. -----------------------------------------------------------------------------
  1332. To configure the slave ptp4l, create a new file
  1333. /usr/local/etc/ptp4l.conf with these contents:
  1334. -----------------------------------------------------------------------------
  1335. [global]
  1336. # only syslog every 1024 seconds
  1337. summary_interval 10
  1338. # send to chronyd/ntpd using SHM 0
  1339. clock_servo ntpshm
  1340. ntpshm_segment 0
  1341. [eth0]
  1342. -----------------------------------------------------------------------------
  1343. Now as root on the slave, as with the master, turn off EEE and start the
  1344. ptp4l daemon:
  1345. -----------------------------------------------------------------------------
  1346. # ethtool --set-eee eth0 eee off
  1347. # ptp4l -S -f /usr/local/etc/ptp4l.conf &
  1348. -----------------------------------------------------------------------------
  1349. Configuration of the slave server is now complete. Follow the earlier
  1350. procedures for checking the jitter on the SHM on the slaves. Give it
  1351. a few hours to settle and your hosts will now be synced to around 5 uSec.
  1352. === PTP with hardware timestamping
  1353. Some NICs requires two additional kernel options. Just in case, verify
  1354. that your running Linux kernel configuration includes these lines, or
  1355. the same with "y" replaced by "m" to enable the drivers as modules:
  1356. -----------------------------------------------------------------------------
  1357. CONFIG_DP83640_PHY=y
  1358. CONFIG_PTP_1588_CLOCK_PCH=y
  1359. -----------------------------------------------------------------------------
  1360. Then you need to verify that your Ethernet driver supports PTP
  1361. by running ethtool as root and verify at least the following lines are
  1362. present in the output:
  1363. -----------------------------------------------------------------------------
  1364. # ethtool -T eth0
  1365. hardware-transmit (SOF_TIMESTAMPING_TX_HARDWARE)
  1366. hardware-receive (SOF_TIMESTAMPING_RX_HARDWARE)
  1367. all (HWTSTAMP_FILTER_ALL)
  1368. -----------------------------------------------------------------------------
  1369. Your NIC may have more features, and your driver may support them for
  1370. better results.
  1371. In the software timestamping above the ptp4l program took care of all steps to
  1372. determine the slave offset from the master and feeding that to a SHM for
  1373. ntpd or chronyd to use.
  1374. In hardware timestamping mode ptp4l will continue to perform most of
  1375. the work. An additional program, phc2sys, will take over the duties of
  1376. reading the hardware timestamps from the NIC, computing the offset, and
  1377. feeding that to the SHM.
  1378. phc2sys will use the SHM exactly as ptp4l did previously so no
  1379. change is required to your ntpd or chronyd configuration.
  1380. To keep things simple, for now, we will not touch the already configured
  1381. and working software timestamping master server. We will proceed to
  1382. configure a slave.
  1383. To configure the slave ptp4l, edit your /usr/local/etc/ptp4l.conf
  1384. to remove the ntpshm options:
  1385. -----------------------------------------------------------------------------
  1386. [global]
  1387. # only syslog every 1024 seconds
  1388. summary_interval 10
  1389. clock_servo linreg
  1390. [eth0]
  1391. -----------------------------------------------------------------------------
  1392. Now as root on the slave, as with the master, turn off EEE and start the
  1393. ptp4l daemon:
  1394. -----------------------------------------------------------------------------
  1395. # ethtool --set-eee eth0 eee off
  1396. # ptp4l -H -f /usr/local/etc/ptp4l.conf &
  1397. # sleep 3
  1398. # phc2sys -a -r -E ntpshm -m -M 0 &
  1399. -----------------------------------------------------------------------------
  1400. Configuration of the slave server is now complete. Follow the earlier
  1401. procedures for checking the jitter on the SHM on the slaves.
  1402. Sadly, theory and practice diverge here. I have never succeeded in
  1403. making hardware timestamping work. I have successfully trashed my
  1404. host system clock. Tread carefully. If you make progress please
  1405. pass on some clue.
  1406. == Providing public NTP service
  1407. <<NTP-FAQ>> has good advice on things to be sure you have done -- and
  1408. are ready to do -- before becoming a public server. One detail it
  1409. doesn't mention is that you'll need to un-firewall UDP port 123. The
  1410. NTP protocol does not use TCP, so no need to unblock TCP port 123.
  1411. If and when you are ready to go public, see <<JOIN-POOL>>.
  1412. == Acknowledgments
  1413. Beat Bolli <bbolli@ewanet.ch> wrote much of the section on NTP
  1414. performance tuning. Hal Murray <hmurray@megapathdsl.net> wrote
  1415. much of the section on NTP working and performance details.
  1416. Sanjeev Gupta <ghane0@gmail.com> assisted with editing.
  1417. Shawn Kohlsmith <skohlsmith@gmail.com> tweaked the Bibliography.
  1418. Jaap Winius <jwinius@rjsystems.nl> cleaned up some terminology.
  1419. The loopstats-based tuning procedure for ntpd was drafted by Sanjeev
  1420. Gupta <ghane0@gmail.com>, based on discussions on the GPSD list
  1421. <<GPSD-LIST>> in Oct and Nov 2013. Code examples are based on work by
  1422. Andy Walls <andy@silverblocksystems.net>. A copy of the original
  1423. email can be found at <<ANDY-POST>>. A thorough review was contributed
  1424. by Jaap Winius <jwinius@rjsystems.nl>.
  1425. == References
  1426. [bibliography]
  1427. - [[[TIME-INTRO]]] link:time-service-intro.html[Introduction to Time Service]
  1428. - [[[WIKI-NTP]]] https://en.wikipedia.org/wiki/Network_Time_Protocol[Network Time Protocol]
  1429. - [[[NTP-FAQ]]] http://www.ntp.org/ntpfaq/[NTP FAQ]
  1430. - [[[RFC-2783]]] https://tools.ietf.org/html/rfc2783[RFC 2783]
  1431. - [[[RFC-5905]]] https://tools.ietf.org/html/rfc5905[RFC 5905]
  1432. - [[[MACX-1]]] https://www.etsy.com/listing/280336400/navisys-gr-601w-u-blox-6-macx-1-usb-gnss[Navisys GR-601W u-blox-6 "Macx-1" USB GPS receiver]
  1433. - [[[CHRONY-COMPARE]]] https://chrony.tuxfamily.org/manual.html#Comparison-with-ntpd[ntpd (comparison with chrony)]
  1434. - [[[CHRONYDEFAULT]]] https://lists.fedoraproject.org/pipermail/devel/2010-May/135679.html
  1435. - [[[HARDWARE]]] https://gpsd.gitlab.io/gpsd/hardware.html[Compatible Hardware]
  1436. - [[[UBLOX-TIMING]]] https://www.u-blox.com/sites/default/files/products/documents/Timing_AppNote_(GPS.G6-X-11007).pdf[GPS-based timing considerations with u-blox 6 receivers]
  1437. - [[[RPI]]] https://www.satsignal.eu/ntp/Raspberry-Pi-NTP.html[The Raspberry Pi as a Stratum-1 NTP Server]
  1438. - [[[NTP.ORG]]] http://www.ntp.org/[Home of the Network Time Protocol project]
  1439. - [[[NTPSEC.ORG]]] https://www.ntpsec.org/[Wecome to NTPsec]
  1440. - [[[USE-POOL]]] https://www.pool.ntp.org/en/use.html[How do I use pool.ntp.org?]
  1441. - [[[CVE-2009-3563]]] https://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2009-3563
  1442. - [[[CHRONY]]] https://chrony.tuxfamily.org/[Chrony Home]
  1443. - [[[CHRONY-MAN]]] https://chrony.tuxfamily.org/manual.html
  1444. - [[[ADEV-PLOT]]] http://www.leapsecond.com/pages/adev-avg/[Allan deviation and Averaging]
  1445. - [[[ZONES]]] https://www.pool.ntp.org/zone
  1446. - [[[NTPQ-OUTPUT]]] https://nlug.ml1.co.uk/2012/01/ntpq-p-output/831[ntpq output description]
  1447. - [[[JOIN-POOL]]] https://www.pool.ntp.org/en/join.html[How do I join pool.ntp.org?]
  1448. - [[[ANDY-POST]]] https://lists.gnu.org/archive/html/gpsd-dev/2013-10/msg00152.html[Clarifications needed for the time-service HOWTO]
  1449. - [[[NTP-MONOPT]]] https://www.eecis.udel.edu/~mills/ntp/html/monopt.html[NTP Monitoring]
  1450. - [[[GPSD-LIST]]]https://lists.gnu.org/archive/html/gpsd-dev/[gpsd-dev Archives]
  1451. - [[[PTP]]] https://www.nist.gov/el/isd/ieee/ieee1588[PTP]
  1452. - [[[LINUX-PTP]]] http://linuxptp.sourceforge.net/[Linux PTP]
  1453. - [[[EEE]]] https://en.wikipedia.org/wiki/Energy-Efficient_Ethernet[Energy-Efficient Ethernet]
  1454. - [[[LVC]]] https://www.rjsystems.nl/en/2100-ntpd-garmin-gps-18-lvc-gpsd.php
  1455. - [[[GITLAB-SOURCE]]] https://gitlab.com/NTPsec/ntpsec[NTPSec source on Gitlab]
  1456. == Changelog
  1457. 1.1, Nov 2013::
  1458. Initial release.
  1459. 1.2, Aug 2014::
  1460. Note that NetBSD now has PPS support.
  1461. 1.3, Aug 2014::
  1462. Add a note about the GR-601W.
  1463. 1.4, Dec 2014::
  1464. Cleaned up Bibliography
  1465. 2.0, Feb 2015::
  1466. More about troubleshooting PPS delivery. Folded in Sanjeev
  1467. Gupta's Calibration Howto describing the loopstats-based
  1468. procedure. Added preliminary information on PTP.
  1469. 2.1 Mar 2015::
  1470. More on PTP. Added link to Jaap Winius's page on GPS-18 setup.
  1471. 2.2 Mar 2015::
  1472. Detailed explanation of NTP has moved to a new page,
  1473. link:time-service-intro.html[Introduction to Time Service].
  1474. 2.3 Mar 2015::
  1475. Use the NTP accuracy estimate from RFC 5905.
  1476. 2.4 Mar 2015::
  1477. Removed some typos, corrected formatting, and minor changes.
  1478. A bit more specificity about root vs. non-root.
  1479. 2.5 Apr 2016::
  1480. Note the existence of the GR-701W.
  1481. 2.6 May 2016::
  1482. New section on GPS time. Note the existence of the GR-801W.
  1483. Describe the special timeserver build of GPSD. Recommend NTPsec.
  1484. Add Macx-1 link.
  1485. Add sections on ARP and temperature problems
  1486. 2.7 June 2016::
  1487. Add section on avoiding power saving.
  1488. 2.8 July 2016::
  1489. Mention required version of gpsd
  1490. Fix Typos.
  1491. 2.9 August 2016::
  1492. Fix typos.
  1493. 2.10 September 2016::
  1494. Mention ntpviz
  1495. Recommend minpoll=maxpoll=0 for PPS refclocks
  1496. Recommend NTPsec.
  1497. 2.11 June 2019::
  1498. Check all links, and update to https where possible
  1499. 2.12 January 2021::
  1500. Add Table of Contents. Markup cleanup.
  1501. November 2021:: Minor cleanup around timeservice=yes.
  1502. == COPYING
  1503. This file is Copyright 2013 by the GPSD project +
  1504. SPDX-License-Identifier: BSD-2-clause
  1505. // end