gpsd-time-service-howto.adoc 82 KB

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