【问题标题】:Why is NTP's root_delay so different to my measured roundtrip?为什么 NTP 的 root_delay 与我测量的往返时间如此不同?
【发布时间】:2018-06-04 21:00:48
【问题描述】:

获取网络时间协议数据包时(NTP 版本 4,请参阅here):

from contextlib import closing
from socket import socket, AF_INET, SOCK_DGRAM
import struct, time

start = time.time()
with closing(socket(AF_INET, SOCK_DGRAM)) as s:
    s.sendto('\x23' + 47 * '\0', ('pool.ntp.org', 123))       # NTP v4, see RFC 5905
    msg, address = s.recvfrom(1024)
now = time.time()

我通常会得到一个往返时间 now - start 大约 40 毫秒。

然而,与

format = "!4b4h9I"
unpacked = struct.unpack(format, msg[0:struct.calcsize(format)]) 
livnmode, stratum, poll, precision = unpacked[0:4]
print 'root_delay',  unpacked[4] + float(unpacked[5]) / 2**16                             # https://www.rfc-editor.org/rfc/rfc5905#page-13
print 'root_dispersion', unpacked[6] + float(unpacked[7]) / 2**16
print 'ref_id', unpacked[8]
print 'ref_timestamp  %.3f' % (unpacked[9] + float(unpacked[10]) / 2**32 - 2208988800L)
print 'orig_timestamp %.3f' % (unpacked[11] + float(unpacked[12]) / 2**32)
print 'recv_timestamp %.3f' % (unpacked[13] + float(unpacked[14]) / 2**32 - 2208988800L)
print 'tx_timestamp %.3f' % (unpacked[15] + float(unpacked[16]) / 2**32 - 2208988800L 

我得到一个 0.00056 秒的 root_delay,这似乎不太可能是真的! (我不认为我对时间服务器有 0.5 毫秒的 ping,往返时间......这真的太小了)

问题:root_delay 在 NTP 协议中究竟是如何测量的?

注意:

  • RFC 5905 声明:

      Root Delay (rootdelay): Total round-trip delay to the reference
      clock, in NTP short format.
    
  • 我重新启动脚本的次数越多,root_delay 似乎就会减少(即使我的本地计算机 RTC 时间没有被我的 Python 脚本更新......这很奇怪......)

  • 我对@9​​87654333@的解析似乎是正确的,见https://www.rfc-editor.org/rfc/rfc5905#page-19:

      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |LI | VN  |Mode |    Stratum     |     Poll      |  Precision   |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                         Root Delay                            |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |                         Root Dispersion                       |
      ...
    

    和https://www.rfc-editor.org/rfc/rfc5905#page-13:

      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
      |          Seconds              |           Fraction            |
      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
    
                           NTP Short Format
    
  • 我不使用ntplib 似乎有不同的root_delay 解析(但不符合https://www.rfc-editor.org/rfc/rfc5905#page-13?)

【问题讨论】:

    标签: python time ntp


    【解决方案1】:

    您只是将远程“服务器”作为“客户端”查询,它发送的 root_delay 和 root_dispersion 是服务器相对于 Stratum-0 源的值。

    如果你想计算出你自己的 root_delay 和 root_dispersion,你必须自己做数学。

    您可以使用您拥有的时间戳数据来计算数据包的往返行程,加上服务器发送的值作为它的 root_delay,现在您就有了您的 root_delay。

    【讨论】:

      猜你喜欢
      • 2023-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      • 2012-01-10
      • 1970-01-01
      相关资源
      最近更新 更多