NTP

提供: Wikinote
移動: 案内検索

Network Time Protocol

やってることは単に時刻を合わせて、時計の進み方を正確にするだけなのに、異様に奥が深い NTP について。 ちなみに、ntp 付属文書の 日本語訳 やってます。いつまでかかることやら。 これ Expert Network Time Protocol: An Experience in Time with NTP (Expert) 誰か翻訳して…。

参考文献

うるう秒関連

メモ

  • ntpd はサーバでもクライアントでも UDP 123 番ポートを用いる。

ntpd の動作について

処理の流れ

主に、ntp_proto.c および ntp_loopfilter.c まわり。 <toggledisplay>

--- init_proto
--- default_get_precision
--- proto_clr_stats
--- init_loopfilter
--- rstclock
--- loop_config
--- peer_clear
--- loop_config
--- rstclock
--- adj_host_clock
--- transmit
--- clock_filter
--- peer_xmit
--- poll_update
--- receive
--- process_packet
--- poll_update
--- clock_filter
--- clock_select
--- peer_unfit
--- root_distance 7.959930
--- adj_host_clock
--- adj_host_clock ★ 1 秒間隔
:
--- adj_host_clock
--- transmit
--- peer_xmit
--- poll_update
--- receive
--- process_packet
--- poll_update
--- clock_filter
--- clock_select
--- peer_unfit
--- root_distance 3.961535
--- adj_host_clock
 :
--- adj_host_clock
--- adj_host_clock
--- transmit
--- peer_xmit
--- poll_update
--- receive
--- process_packet
--- poll_update
--- clock_filter
--- clock_select
--- peer_unfit
--- root_distance 0.965042
--- root_distance 0.965042
--- root_distance 0.965042
--- clock_combine
--- root_distance 0.965042
--- clock_update ★ここまでいくと同期成功
--- poll_update
--- local_clock
--- rstclock
--- adj_host_clock
--- adj_host_clock

</toggledisplay>

ツール等

ntpq コマンド

NTP の情報を得るのに最もポピュラーなコマンド。

$ ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
-202.224.32.4    210.171.225.76   2 u  975  256  370    5.759   -1.264   0.162
-133.243.232.68  210.171.226.40   2 u   30  256  377    9.018   -1.209   0.386
+210.173.160.27  210.173.160.86   2 u  209  256  377    5.452    0.021   0.123
+210.173.160.57  133.243.236.18   2 u  215  256  377    5.741    0.272   0.111
*210.173.160.87  210.173.176.4    2 u   38  256  377    5.817   -0.100   0.222
$ ntpq -c as

ind assID status  conf reach auth condition  last_event cnt
===========================================================
  1 59668  9314   yes   yes  none   outlyer   reachable  1
  2 59669  9314   yes   yes  none   outlyer   reachable  1
  3 59670  9414   yes   yes  none  candidat   reachable  1
  4 59671  9414   yes   yes  none  candidat   reachable  1
  5 59672  9614   yes   yes  none  sys.peer   reachable  1
$ ntpq -c rl
assID=0 status=06c4 leap_none, sync_ntp, 12 events, event_peer/strat_chg,
version="ntpd 4.2.2p1@1.1570-o Tue Jun 10 00:07:18 UTC 2008 (1)",
processor="i686", system="Linux/2.6.18-194.el5PAE", leap=00, stratum=3,
precision=-20, rootdelay=6.168, rootdispersion=25.945, peer=59672,
refid=210.173.160.87,
reftime=cff96bee.64899eac  Tue, Jul 27 2010 23:30:06.392, poll=8,
clock=cff96dc6.30334e8f  Tue, Jul 27 2010 23:37:58.188, state=4,
offset=0.057, frequency=44.983, jitter=0.321, noise=0.254,
stability=0.060, tai=0
$ ntpq -c "pstatus 59670"
assID=59670 status=9414 reach, conf, sel_candidat, 1 event, event_reach,
srcadr=ntp1.jst.mfeed.ad.jp, srcport=123, dstadr=118.243.85.16,
dstport=123, leap=00, stratum=2, precision=-19, rootdelay=0.427,
rootdispersion=12.009, refid=210.173.160.86, reach=377, unreach=0,
hmode=3, pmode=4, hpoll=10, ppoll=10, flash=00 ok, keyid=0, ttl=0,
offset=0.040, delay=6.133, dispersion=15.059, jitter=0.120,
reftime=cff981d4.b4092641  Wed, Jul 28 2010  1:03:32.703,
org=cff98349.637b8c15  Wed, Jul 28 2010  1:09:45.388,
rec=cff98349.6442e24e  Wed, Jul 28 2010  1:09:45.391,
xmt=cff98349.629de6de  Wed, Jul 28 2010  1:09:45.385,
filtdelay=     6.40    6.13    6.04    6.70    6.37    8.75    6.04    6.54,
filtoffset=    0.16    0.04   -0.18    0.13    0.03   -1.28   -0.29   -0.09,
filtdisp=      0.00   15.36   23.03   30.69   38.39   46.08   53.79   61.47

ntptime コマンド

カーネルの時計関連の情報を表示・設定することができる。

$ ntptime
ntp_gettime() returns code 0 (OK)
  time cd8dcff1.8d374000  Mon, Apr 13 2009 23:51:29.551, (.551624),
  maximum error 20085 us, estimated error 0 us
ntp_adjtime() returns code 0 (OK)
  modes 0x0 (),
  offset 0.000 us, frequency 77.335 ppm, interval 1 s,
  maximum error 20085 us, estimated error 0 us,
  status 0x1 (PLL),
  time constant 6, precision 1.000 us, tolerance 512 ppm,

それぞれの値の意味は…難しいんだこれが。誰か解説本書いてくれないかなあ。

ntp_adjtime() から返っている status 値は、/usr/include/linux/timex.h で定義されている以下の値を取る。

/*     
 * Status codes (timex.status) 
 */    
#define STA_PLL                0x0001  /* enable PLL updates (rw) */
#define STA_PPSFREQ    0x0002  /* enable PPS freq discipline (rw) */
#define STA_PPSTIME    0x0004  /* enable PPS time discipline (rw) */
#define STA_FLL                0x0008  /* select frequency-lock mode (rw) */

#define STA_INS                0x0010  /* insert leap (rw) */
#define STA_DEL                0x0020  /* delete leap (rw) */
#define STA_UNSYNC     0x0040  /* clock unsynchronized (rw) */ ★いわゆる 11 分モードのとき、0 になる
#define STA_FREQHOLD   0x0080  /* hold frequency (rw) */

#define STA_PPSSIGNAL  0x0100  /* PPS signal present (ro) */
#define STA_PPSJITTER  0x0200  /* PPS signal jitter exceeded (ro) */
#define STA_PPSWANDER  0x0400  /* PPS signal wander exceeded (ro) */
#define STA_PPSERROR   0x0800  /* PPS signal calibration error (ro) */

#define STA_CLOCKERR   0x1000  /* clock hardware fault (ro) */

#define STA_RONLY (STA_PPSSIGNAL | STA_PPSJITTER | STA_PPSWANDER | \
    STA_PPSERROR | STA_CLOCKERR) /* read-only bits */

ntpdc コマンド

ちょっとマイナーだけど様々な情報が得られる。

$ ntpdc -pn
     remote           local      st poll reach  delay   offset    disp
=======================================================================
*210.173.160.87  118.243.85.16    2  256  377 0.00552 -0.000084 0.07770
=202.224.32.4    118.243.85.16    2  256  360 0.00575 -0.001264 0.45509
=133.243.232.68  118.243.85.16    2  256  377 0.00900 -0.001209 0.08156
=210.173.160.27  118.243.85.16    2  256  377 0.00545  0.000021 0.12018
=210.173.160.57  118.243.85.16    2  256  377 0.00574  0.000272 0.09195
$ ntpdc -l
client    ntp3.jst.mfeed.ad.jp
client    orion.asahi-net.or.jp
client    udon.nict.go.jp
client    ntp1.jst.mfeed.ad.jp
client    ntp2.jst.mfeed.ad.jp
$ ntpdc -c "showpeer 210.173.160.27"
remote 210.173.160.27, local 118.243.85.16
hmode client, pmode unspec, stratum 2, precision -19
leap 00, refid [210.173.160.86], rootdistance 0.03963, rootdispersion 0.00789
ppoll 10, hpoll 10, keyid 0, version 4, association 59670
reach 377, unreach 0, flash 0x0000, boffset 0.00400, ttl/mode 0
timer 0s, flags config, bclient
reference time:      cff97e87.bdfef6dd  Wed, Jul 28 2010  0:49:27.742
originate timestamp: cff97f49.6383079f  Wed, Jul 28 2010  0:52:41.388
receive timestamp:   cff97f49.64496620  Wed, Jul 28 2010  0:52:41.391
transmit timestamp:  cff97f49.62b5a175  Wed, Jul 28 2010  0:52:41.385
filter delay:  0.00612  0.00604  0.00668  0.00636 
               0.00874  0.00603  0.00653  0.00629 
filter offset: 0.000040 -0.00017 0.000134 0.000033
               -0.00128 -0.00029 -0.00009 0.000085
filter order:  1        0        2        3       
               4        5        6        7       
offset -0.000177, delay 0.00604, error bound 0.09654, filter error 0.01637
$ ntpdc -c "pstats 210.173.160.27"
remote host:          ntp1.jst.mfeed.ad.jp
local interface:      118.243.85.16
time last received:   550s
time until next send: 474s
reachability change:  11597s
packets sent:         73
packets received:     73
bad authentication:   0
bogus origin:         0
duplicate:            0
bad dispersion:       0
bad reference time:   14
candidate order:      4
flags:     config, bclient, iburst
$ ntpdc -c kerninfo
pll offset:           5.2e-05 s
pll frequency:        44.985 ppm
maximum error:        0.138157 s
estimated error:      0.000237 s
status:               0001  pll
pll time constant:    4
precision:            1e-06 s
frequency tolerance:  512 ppm
$ ntpdc -c loopinfo
offset:               0.000065 s
frequency:            44.985 ppm
poll adjust:          0
watchdog timer:       235 s
$ ntpdc -c sysinfo
system peer:          ntp3.jst.mfeed.ad.jp
system peer mode:     client
leap indicator:       00
stratum:              3
precision:            -20
root distance:        0.00620 s
root dispersion:      0.02278 s
reference ID:         [210.173.160.87]
reference time:       cff96dec.641c9286  Tue, Jul 27 2010 23:38:36.391
system flags:         auth monitor ntp kernel stats 
jitter:               0.000275 s
stability:            0.000 ppm
broadcastdelay:       0.003998 s
authdelay:            0.000000 s
$ ntpdc -c sysstats
time since restart:     6980
time since reset:       6980
packets received:       1415
packets processed:      319
current version:        532
previous version:       0
bad version:            0
access denied:          0
bad length or format:   0
bad authentication:     0
rate exceeded:          0
$ ntpdc -c memstats
time since reset:     7007
total peer memory:    15
free peer memory:     10
calls to findpeer:    533
new peer allocations: 0
peer demobilizations: 0
hash table counts:      0   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        2   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   1   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   0   0   1   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   0   0   0   0   0   0
                        0   0   1   0   0   0   0   0
                        0   0   0   0   0   0   0   0
$ ntpdc -c iostats
time since reset:     7116
receive buffers:      10
free receive buffers: 9
used receive buffers: 0
low water refills:    1
dropped packets:      0
ignored packets:      0
received packets:     1432
packets sent:         2151
packets not sent:     0
interrupts handled:   1414
received by int:      1414
$ ntpdc -c timerstats
time since reset:  7133
alarms handled:    0
alarm overruns:    0
calls to transmit: 0
$ ntpdc -c reslist
   address          mask            count        flags
=====================================================================
0.0.0.0         0.0.0.0               320  noquery, nomodify, nopeer, notrap, kod
y085016.dynamic 255.255.255.255         0  ntpport, interface, ignore
localhost.local 255.255.255.255       911  none
localhost.local 255.255.255.255         0  ntpport, interface, ignore
192.168.1.0     255.255.255.0          44  nomodify, notrap
lab.hagio.org   255.255.255.255         0  ntpport, interface, ignore
192.168.8.0     255.255.255.0         167  nomodify, notrap
192.168.8.1     255.255.255.255         0  ntpport, interface, ignore
192.168.10.1    255.255.255.255         0  ntpport, interface, ignore
::              ::                      0  none
localhost6.loca ffff:ffff:ffff:         0  ntpport, interface, ignore
fe80::20a:79ff: ffff:ffff:ffff:         0  ntpport, interface, ignore
fe80::21f:d0ff: ffff:ffff:ffff:         0  ntpport, interface, ignore
fe80::250:56ff: ffff:ffff:ffff:         0  ntpport, interface, ignore
fe80::250:56ff: ffff:ffff:ffff:         0  ntpport, interface, ignore
$ ntpdc -c monlist
remote address          port local address      count m ver code avgint  lstint
===============================================================================
localhost.localdomain  40435 127.0.0.1            923 7 2      0      6       0
192.168.8.60             123 192.168.8.1           22 3 4    180    290      32
192.168.8.47             123 192.168.8.1          146 3 4    180     96     111
192.168.1.10             123 192.168.1.1           44 3 4    180    230     170
orion.asahi-net.or.jp    123 118.243.85.16         62 4 4    5d0    317     284
ntp1.jst.mfeed.ad.jp     123 118.243.85.16         65 4 4    5d0    225     293
ntp2.jst.mfeed.ad.jp     123 118.243.85.16         64 4 4    5d0    227     299
udon.nict.go.jp          123 118.243.85.16         65 4 4    5d0    225     371
ntp3.jst.mfeed.ad.jp     123 118.243.85.16         64 4 4    5d0    225     380

その他

  • 最近の携帯電話に搭載されている自動時刻合わせ機能は、基地局からの制御情報に含まれる時刻情報を利用している。したがって、i-mode などを用いた NTP でもなければ、標準電波を利用した、いわゆる電波時計でもない。電波っちゃ電波だけど。