RFC

http://www.ietf.org/rfc/rfc5905.txt

https://www.eecis.udel.edu/~mills/ntp/html/select.html

https://wenku.baidu.com/view/1539c86a71fe910ef02df83f.html###

https://wenku.baidu.com/view/d48bf96df111f18583d05a88.html

https://wenku.baidu.com/view/214946669b6648d7c1c7469e.html

settimeofday() && adjtime()

The implementation model includes some means to set and adjust the

system clock. The operating system is assumed to provide two

functions: one to set the time directly, for example, the Unix

settimeofday() function, and another to adjust the time in small

increments advancing or retarding the time by a designated amount,

for example, the Unix adjtime() function. In this and following

references, parentheses following a name indicate reference to a

function rather than a simple variable. In the intended design the

clock discipline process uses the adjtime() function if the

adjustment is less than a designated threshold, and the

settimeofday() function if above the threshold. The manner in which

this is done and the value of the threshold as described in

Section 10.

如果超过:threshold 则,settimeofday(): 快速调整。否则,adjtime() : 缓慢调整。

NTP报文

               +-----------+------------+-----------------------+
| Name | Formula | Description |
+-----------+------------+-----------------------+
| leap | leap | leap indicator (LI) |
| version | version | version number (VN) |
| mode | mode | mode |
| stratum | stratum | stratum |
| poll | poll | poll exponent |
| precision | rho | precision exponent |
| rootdelay | delta_r | root delay |
| rootdisp | epsilon_r | root dispersion |
| refid | refid | reference ID |
| reftime | reftime | reference timestamp |
| org | T1 | origin timestamp |
| rec | T2 | receive timestamp |
| xmt | T3 | transmit timestamp |
| dst | T4 | destination timestamp |
| keyid | keyid | key ID |
| dgst | dgst | message digest |
+-----------+------------+-----------------------+ Figure 7: Packet Header Variables
       0                   1                   2                   3
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
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|LI | VN |Mode | Stratum | Poll | Precision |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Root Delay |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Root Dispersion |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Reference ID |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ Reference Timestamp (64) +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ Origin Timestamp (64) +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ Receive Timestamp (64) +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
+ Transmit Timestamp (64) +
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
. .
. Extension Field 1 (variable) .
. .
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
. .
. Extension Field 2 (variable) .
. .
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Key Identifier |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| |
| dgst (128) |
| |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Figure 8: Packet Header Format

其中第一个字段: LI Leap Indicator (leap)

           +-------+----------------------------------------+
| Value | Meaning |
+-------+----------------------------------------+
| 0 | no warning |
| 1 | last minute of the day has 61 seconds |
| 2 | last minute of the day has 59 seconds |
| 3 | unknown (clock unsynchronized) |
+-------+----------------------------------------+ Figure 9: Leap Indicator

verion字段:

                      +-------+--------------------------+
| Value | Meaning |
+-------+--------------------------+
| 0 | reserved |
| 1 | symmetric active |
| 2 | symmetric passive |
| 3 | client |
| 4 | server |
| 5 | broadcast |
| 6 | NTP control message |
| 7 | reserved for private use |
+-------+--------------------------+ Figure 10: Association Modes

Stratum (stratum)字段

        +--------+-----------------------------------------------------+
| Value | Meaning |
+--------+-----------------------------------------------------+
| 0 | unspecified or invalid |
| 1 | primary server (e.g., equipped with a GPS receiver) |
| 2-15 | secondary server (via NTP) |
| 16 | unsynchronized |
| 17-255 | reserved |
+--------+-----------------------------------------------------+ Figure 11: Packet Stratum

Root Delay (rootdelay)

Root Delay (rootdelay): Total round-trip delay to the reference

clock, in NTP short format.

Root Dispersion (rootdisp)

Root Dispersion (rootdisp): Total dispersion to the reference clock,

in NTP short format.

Reference ID (refid)

     +------+----------------------------------------------------------+
| ID | Clock Source |
+------+----------------------------------------------------------+
| GOES | Geosynchronous Orbit Environment Satellite |
| GPS | Global Position System |
| GAL | Galileo Positioning System |
| PPS | Generic pulse-per-second |
| IRIG | Inter-Range Instrumentation Group |
| WWVB | LF Radio WWVB Ft. Collins, CO 60 kHz |
| DCF | LF Radio DCF77 Mainflingen, DE 77.5 kHz |
| HBG | LF Radio HBG Prangins, HB 75 kHz |
| MSF | LF Radio MSF Anthorn, UK 60 kHz |
| JJY | LF Radio JJY Fukushima, JP 40 kHz, Saga, JP 60 kHz |
| LORC | MF Radio LORAN C station, 100 kHz |
| TDF | MF Radio Allouis, FR 162 kHz |
| CHU | HF Radio CHU Ottawa, Ontario |
| WWV | HF Radio WWV Ft. Collins, CO |
| WWVH | HF Radio WWVH Kauai, HI |
| NIST | NIST telephone modem |
| ACTS | NIST telephone modem |
| USNO | USNO telephone modem |
| PTB | European telephone modem |
+------+----------------------------------------------------------+

Reference Timestamp

Reference Timestamp: Time when the system clock was last set or

corrected, in NTP timestamp format.

Origin Timestamp (org) ---> T1

Origin Timestamp (org): Time at the client when the request departed

for the server, in NTP timestamp format.

Receive Timestamp (rec) ---> T2

Receive Timestamp (rec): Time at the server when the request arrived

from the client, in NTP timestamp format.

Transmit Timestamp (xmt) ---> T3

Transmit Timestamp (xmt): Time at the server when the response left

for the client, in NTP timestamp format.

Destination Timestamp (dst) --->T4

Destination Timestamp (dst): Time at the client when the reply

arrived from the server, in NTP timestamp format.

Extension Field

Extension Field n: See Section 7.5 for a description of the format of

this field.

In NTPv4, one or more extension fields can be inserted after the

header and before the MAC, which is always present when an extension

field is present. Other than defining the field format, this

document makes no use of the field contents. An extension field

contains a request or response message in the format shown in

Figure 14.

       0                   1                   2                   3
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
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Field Type | Length |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
. .
. Value .
. .
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Padding (as needed) |
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ Figure 14: Extension Field Format

Key Identifier (keyid)

Key Identifier (keyid): 32-bit unsigned integer used by the client

and server to designate a secret 128-bit MD5 key.

Message Digest (digest)

Message Digest (digest): 128-bit MD5 hash computed over the key

followed by the NTP packet header and extensions fields (but not the

Key Identifier or Message Digest fields).

计算时间方法

大致过程: 机器A同步机器B

A: 先把当前时间copy到T1

B: B收到后,立刻把自己时间copy到T2

B: B发送时,将发送时间copy到T3

A:A收到时,把接受时间copy到T4

In the figure, the first packet transmitted by A contains only the

origin timestamp t1, which is then copied to T1. B receives the

packet at t2 and copies t1 to T1 and the receive timestamp t2 to T2.

At this time or some time later at t3, B sends a packet to A

containing t1 and t2 and the transmit timestamp t3. All three

timestamps are copied to the corresponding state variables. A

receives the packet at t4 containing the three timestamps t1, t2, and

t3 and the destination timestamp t4. These four timestamps are used

to compute the offset and delay of B relative to A, as described

below.

offset

theta = T(B) - T(A) = 1/2 * [(T2-T1) + (T3-T4)]

round-trip delay 延迟

delta = T(ABA) = (T4-T1) - (T3-T2).

NTP源码函数和这些概念对应关系

要想看懂ntp源码,如下列表概念很重要


+---------+----------+-----------------------+
| Name | Formula | Description |
+---------+----------+-----------------------+
| srcaddr | srcaddr | source address |
| srcport | srcport | source port |
| dstaddr | dstaddr | destination address |
| dstport | destport | destination port |
| keyid | keyid | key identifier key ID |
+---------+----------+-----------------------+ Figure 16: Peer Process Configuration Variables +-----------+------------+---------------------+
| Name | Formula | Description |
+-----------+------------+---------------------+
| leap | leap | leap indicator |
| version | version | version number |
| mode | mode | mode |
| stratum | stratum | stratum |
| ppoll | ppoll | peer poll exponent |
| rootdelay | delta_r | root delay |
| rootdisp | epsilon_r | root dispersion |
| refid | refid | reference ID |
| reftime | reftime | reference timestamp |
+-----------+------------+---------------------+ Figure 17: Peer Process Packet Variables +------+---------+--------------------+
| Name | Formula | Description |
+------+---------+--------------------+
| org | T1 | origin timestamp |
| rec | T2 | receive timestamp |
| xmt | T3 | transmit timestamp |
| t | t | packet time |
+------+---------+--------------------+ Figure 18: Peer Process Timestamp Variables Mills, et al. Standards Track [Page 31] RFC 5905 NTPv4 Specification June 2010 +--------+---------+-----------------+
| Name | Formula | Description |
+--------+---------+-----------------+
| offset | theta | clock offset |
| delay | delta | round-trip delay|
| disp | epsilon | dispersion |
| jitter | psi | jitter |
| filter | filter | clock filter |
| tp | t_p | filter time |
+--------+---------+-----------------+ Figure 19: Peer Process Statistics Variables

几个关键数据

dispersion

The dispersion statistic epsilon(t) represents the maximum error due

to the frequency tolerance and time since the last packet was sent.

It is initialized

epsilon(t_0) = r.rho + s.rho + PHI * (T4-T1)

when the measurement is made at t_0 according to the seconds counter.

Here, r.rho is the packet precision described in Section 7.3 and

s.rho is the system precision described in Section 11.1, both

expressed in seconds. These terms are necessary to account for the

uncertainty in reading the system clock in both the server and the

client.

The dispersion then grows at constant rate PHI; in other words, at

time t, epsilon(t) = epsilon(t_0) + PHI * (t-t_0). With the default

value PHI = 15 ppm, this amounts to about 1.3 s per day. With this

understanding, the argument t will be dropped and the dispersion

represented simply as epsilon. The remaining statistics are computed

by the clock filter algorithm described in the next section.

NTP相关算法

1. Clock Filter Algorithm (时间滤波算法)

The clock filter algorithm is part of the peer process. It grooms

the stream of on-wire data to select the samples most likely to

represent accurate time. The algorithm produces the variables shown

in Figure 19, including the offset (theta), delay (delta), dispersion

(epsilon), jitter (psi), and time of arrival (t). These data are

used by the mitigation algorithms to determine the best and final

offset used to discipline the system clock. They are also used to

determine the server health and whether it is suitable for

synchronization.

在Clock Filter Algorithm (时间滤波算法) 中,会校验完整唯一的包,并且,计算出每一个包的三个核心指标: offset (theta), delay (delta), dispersion

(epsilon)。

其中, dispersion (epsilon) 比较难以理解,这个称之为,离散。

The clock filter algorithm saves the most recent sample tuples

(theta, delta, epsilon, t) in the filter structure

2. Clock Select Algorithm (时间选择算法)



3. Cluster Algorithm (聚类算法)

4. Clock discipline Algorithm (聚类算法)

NTP同步底层实现的更多相关文章

  1. CentOS7时间设置及ntp同步配置(转)

    出处:http://www.centoscn.com/CentOS/config/2015/1105/6385.html http://www.centoscn.com/CentOS/config/2 ...

  2. 搭建NTP时间服务器~使用NTP同步时间~构建主机间时间自动同步关系

    NTP是一个时间服务器,同时它也是一个时间客户端. 我们可以使用它构建主机与主机之间的时间自动同步环境,保证所有服务器时间一致性. 常用的公共NTP时间服务器有: cn.ntp.org.cn 中国 n ...

  3. Centos 修改时间地区及NTP同步北京时间

    在我们使用CentOS系统的时候,也许时区经常会出现问题,有时候改完之后还是会出错,下面我们就来学习一种方法来改变这个状况.如果没有安装,而你使用的是 CentOS系统 那使用命令 yum insta ...

  4. 从ruby实现时间服务器ntp同步功能也谈“逆向工程”

    本猫以前写asm和C的时候常常不忘"逆向"一把,后来写驱动的时候也用VM之类的搭建"双机"调试环境进行调试:也对于一些小的软件crack cd-key神马的不亦 ...

  5. CentOS7安装chrony替代ntp同步时间

    Chrony是一个开源的自由软件,它能保持系统时钟与时钟服务器(NTP)同步,让时间保持精确.它由两个程序组成:chronyd和chronyc:chronyd是一个后台运行的守护进程,用于调整内核中运 ...

  6. CentOS ntp同步

    新装的CentOS系统服务器可能设置了错误的,需要调整时区并调整时间. 如下是CentOS系统使用NTP来从一个时间服务器同步 把当前时区调整为上海就是+8区,想改其他时区也可以去看看/usr/sha ...

  7. NTP同步网络时间

    为什么要同步网络时间呢,这是由于树莓派没有RTC和后备电池,不能像PC机那样关机之后仍可以走时. NTP对时步骤: 1 安装ntpdate sudo apt-get install ntpdate s ...

  8. Linux安装ntp同步时间

    1.安装 yum install  ntp 安装下就可以了. 2.寻找一个网络时间服务器,比如一些国家授时中心 微软公司授时主机(美国) time.windows.com 台警大授时中心(台湾) as ...

  9. ntp同步报错解决

    服务端:192.168.1.204 主机名: www.test.com 客户端:192.168.1.206 主机名: www.test3.com 客户端同步服务端报错如下: [root@www etc ...

随机推荐

  1. 【JavaScript框架封装】实现一个类似于JQuery的动画框架的封装

    // 动画框架 (function (xframe) { // 需要参与链式访问的(必须使用prototype的方式来给对象扩充方法) xframe.extend({}); // 不需要参与链式访问的 ...

  2. SpProcPool阅读笔记--1

    公司产品用了一个开源的框架,最近出了点问题,细看了这个框架. SpProcPool:  https://github.com/spsoft/spprocpool.git 我们的线程池用的是传递文件描述 ...

  3. ElasticSearch[v6.2] 在实际项目中的应用

    摘要:本文所讲述的内容,为ElasticSearch(以下简称ES)全文搜索引擎在实际大数据项目的应用:ES的底层是开源库 Lucene.但是,你没法直接用 Lucene,必须自己写代码去调用它的接口 ...

  4. (3)Spring Boot热部署【从零开始学Spring Boot】

    在编写代码的时候,你会发现我们只是简单把打印信息改变了下,就需要重新部署,如果是这样的编码方式,那么我们估计一天下来之后就真的是打几个Hello World之后就下班了.那么如何解决热部署的问题呢?那 ...

  5. BZOJ 3674 可持久化并查集加强版(按秩合并版本)

    /* bzoj 3674: 可持久化并查集加强版 http://www.lydsy.com/JudgeOnline/problem.php?id=3674 用可持久化线段树维护可持久化数组从而实现可持 ...

  6. BA-siemens-symaro传感器简介

    1 传感器的原理 传感器.控制器.执行机构是构成控制系统 3 个要素,传感器的作 用一般用来测量工艺参数,提供给控制器或显示仪表,实现工艺过程的 监测或控制.传感器的类型是按测量参数不同分类的,主要分 ...

  7. [Windows Server]新机子上装老系统·

    硬盘模式改了也得用U大师,然后再PE里装 1.U大师做启动盘 2.拷贝解压后的系统进去 3.用PE自带安装工具

  8. rails 修改数据库之后注意修改controller

    rails 修改数据库之后注意修改controller 在view中进行修改之后,注意修改controller中的内容: 这样才可以进行参数的传递:

  9. DAC0832、led、蜂鸣器

    52控制DAC0832芯片输出电流,让发光二极管D12由灭均匀变到最亮.再有亮变灭.在最亮和最灭的时候蜂鸣器发出报警声,完毕整个周期的时间是控制在5s左右. #include<reg52.h&g ...

  10. Hibernate Tools-代码生成

    建立好数据库后,点击一个键,代码就生成了,这实在是份十分满意的事情.前面有介绍Hibernate Tools就能够生成代码,那么我们怎么利用它来生成代码呢. 以下就把具体步骤贴出来,相同,有图有真相. ...