PTP(Precision Time Protocol)精确时间协议,由IEEE1588规定,用于同步网络中多台设备。
PTP时钟可以实现主时钟和从时钟功能,但是一个PTP通信子网内只能有一个主时钟。在系统的同步过程中,PTP主时钟提供时间同步及时间信息,从时钟接收主时钟发来的时间戳信息,系统根据此信息计算出主从线路时间延迟及主从时间差,并利用该时间差调整本地时间,从而使设备时间保持与主设备时间一致的频率和相位,实现频率同步和时间同步。
PTP作用的对象是时钟,主要分为全局主时钟(GrandMaster),主时钟(MasterClock),透明时钟(TransparentClock),边界时钟(BoundaryClock)和普通时钟(OrdinaryClock)。
| 全局主时钟(GM) | 全局唯一,为PTP域中的所有设备提供时间基准。 |
| 主时钟(MC) | OC和BC的基准时钟称为主时钟。 |
| 边界时钟 (BC) | 有多个端口连接到网络的时钟,一个slave端口连接到上游设备的PTP端口,其他的端口作为master连接到下游设备的slave端口。下游slave端口同构PTP协议直接同步于边界时钟,而不是直接和grandmaster通信。 |
| 普通时钟(OC) | 只有一个端口(port)连接到网络的时钟,可以作为master或slave之一; 作为master时候,可以是 grandmaster,作为slave时候,一般作为时钟分发网络的端点设备(end,最后一级)。 |
| 透明时钟(TC) | 可以担负同步系统中信号分配器的角色。它拥有多个PTP端口,不需要像普通时钟和边界时钟一样与其他节点进行时间同步,仅负责在端口之间转发PTP报文时,对其进行转发延时校正(在PTP报文中增加时间校正信息)。 |

在服务器上安装部署linuxptp就不做过多说明了,联网安装可执行下面的指令
yum install -y linuxptp
Linuxptp主要包含两个软件,一个是ptp4l,一个是phc2sys。作用分别如下
- ptp4l:遵循IEEE 1588-2008标准文档规范,实现了BC(Boundary Clock)、OC(Ordinary Clock)和TC(Transparent Clock);
- phc2sys:phc2sys是一个同步系统中两个或多个时钟的程序。通常,它用于将系统时钟与PTP硬件时钟(PHC)同步,该硬件时钟本身由ptp4l(8)程序同步;
- pcm:在ptp4l运行期间对其进行配置。
- [root@localhost ~]# ptp4l -h
-
- usage: ptp4l [options]
-
- Delay Mechanism
-
- -A Auto, starting with E2E
- -E E2E, delay request-response (default)
- -P P2P, peer delay mechanism
-
- Network Transport
-
- -2 IEEE 802.3
- -4 UDP IPV4 (default)
- -6 UDP IPV6
-
- Time Stamping
-
- -H HARDWARE (default)
- -S SOFTWARE
- -L LEGACY HW
-
- Other Options
-
- -f [file] read configuration from 'file'
- -i [dev] interface device to use, for example 'eth0'
- (may be specified multiple times)
- -p [dev] PTP hardware clock device to use, default auto
- (ignored for SOFTWARE/LEGACY HW time stamping)
- -s slave only mode (overrides configuration file)
- -t transparent clock
- -l [num] set the logging level to 'num'
- -m print messages to stdout
- -q do not print messages to the syslog
- -v prints the software version and exits
- -h prints this message and exits
- [root@localhost ~]# phc2sys
- autoconfiguration or valid source clock must be selected.
-
- usage: phc2sys [options]
-
-
- automatic configuration:
- -a turn on autoconfiguration
- -r synchronize system (realtime) clock
- repeat -r to consider it also as a time source
- manual configuration:
- -c [dev|name] slave clock (CLOCK_REALTIME)
- -d [dev] master PPS device
- -s [dev|name] master clock
- -O [offset] slave-master time offset (0)
- -w wait for ptp4l
- common options:
- -f [file] configuration file
- -E [pi|linreg] clock servo (pi)
- -P [kp] proportional constant (0.7)
- -I [ki] integration constant (0.3)
- -S [step] step threshold (disabled)
- -F [step] step threshold only on start (0.00002)
- -R [rate] slave clock update rate in HZ (1.0)
- -N [num] number of master clock readings per update (5)
- -L [limit] sanity frequency limit in ppb (200000000)
- -M [num] NTP SHM segment number (0)
- -u [num] number of clock updates in summary stats (0)
- -n [num] domain number (0)
- -x apply leap seconds by servo instead of kernel
- -z [path] server address for UDS (/var/run/ptp4l)
- -l [num] set the logging level to 'num' (6)
- -t [tag] add tag to log messages
- -m print messages to stdout
- -q do not print messages to the syslog
- -v prints the software version and exits
- -h prints this message and exits
命令
ethtool -T p2p1

可以看到p2p1占用的端口为/dev/ptp5
这时候server充当的角色是OC(slave角色),只接收ptp信号,Switch充当的角色是TC

/usr/sbin/ptp4l -2 -A -q -i p2p1 -f /etc/ptp4l.conf -s -m
-2: IEEE 802.3协议
-A: 自动选择延迟测量机制,运行时使用E2E,但接收到peer delay request报文后自动切换到P2P。
-q: 不将信息发送到系统logger
-i: 指定网口(PTP port),可以多次使用指定多个网口,但该选项或配置文件中至少要指定一个。
-f: 指定配置文件,若没有指定,则使用默认配置。
-s: slaveOnly模式
-m: 将信息打印到标准输出
/usr/sbin/phc2sys -s /dev/ptp5 -c CLOCK_REALTIME -w -r -n 24 -m
-s: 按设备(如/dev/ptp0)或接口(如eth0)或名称(如系统时钟的clock_REALTIME)指定主时钟(即ptp4l绑定的接口)
-c: 按设备(如/dev/ptp1)或接口(如eth1)或名称指定从时钟。默认值CLOCK_REALTIME(系统时钟)
-w: 等待直到ptp4l处于同步状态
-r: 指示phc2sys也同步系统时钟(clock_REALTIME)
-n: 指定ptp4l使用的域号。默认值为0
-m: 将信息打印到标准输出
这时候server充当的角色是BC(既是slave又是master),网络中没有Switch。
说明:
p2p1从GM获取PTP,然后将PHC同步到系统时钟,充当的是slave角色;
p3p2将p2p1中的信号从p3p2下发给RU,充当的是master的角色。

/usr/sbin/ptp4l -2 -A -q -i p2p1 -f /etc/ptp4l.conf -s -m
/usr/sbin/phc2sys -s /dev/ptp5 -c CLOCK_REALTIME -w -r -n 24 -m
获取p2p1跟p3p2设备

启动进程
/usr/sbin/ptp4l -2 -A -q -i p3p2 -f /etc/ptp4l_m.conf -m
/usr/sbin/phc2sys -q -m -s /dev/ptp5 -c /dev/ptp3 -w -r -n 24
例如如下报错:
在配置文件中修改tx_timestamp_timeout参数即可,将timeout值设置的大一点,单位是ms
tx_timestamp_timeout: 等待发送时间戳的超时时间,单位ms

配置文件参数含义
参考:转载:linuxptp/ptp4l PTP时钟同步配置选项_小学生Yi的博客-CSDN博客_ptp4l
| 配置选项 | 描述 | 默认值 |
|---|---|---|
| twoStepFlag | 两步时间戳模式,单步时间戳模式选择 | 1 |
| slaveOnly | 绕过BMCA,使clock工作在slave模式 | 0 |
| socket_priority | 报文优先级,0~15,只在L2协议下生效 | 0 |
| gmCapable | 是否具有master能力,只对gPTP模式生效 | 1 |
| priority1 | 时钟priority1属性,0~255 | 128 |
| priority2 | 时钟priority2属性,0~255 | 128 |
| clockClass | 时钟clockClass属性 | 248 |
| clockAccuracy | 时钟clockAccuracy属性 | 0xFE |
| clockIdentity | 时钟clockIdentity属性,使用默认值则会在运行时自动生成 | 000000.0000.000000 |
| offsetScaledLogVariance | 时钟offsetScaledLogVariance属性 | 0xFFFF |
| G.8275.defaultDS.localPriority | ITU-T G.8275.1 and G.8275.2使用 | 128 |
| maxStepsRemoved | Announce报文的stepsRemoved值大于等于该值时,则不参与BMCA | 255 |
| domainNumber | PTP域 | 0 |
| utc_offset | TAI和UTC时间差 | 37 |
| free_running | 不调整物理时钟 | 0 |
| freq_est_interval | 评估本地时钟和对端时钟频率比值的时间间隔,时间间隔值为该值的2的次方。 | 1(2秒) |
| assume_two_step | 按两步报文的方式,处理单步报文 | 0 |
| tc_spanning_tree | 透明时钟生成树模式,在PTP网络中,实现所有透明时钟内增加Announce报文的stepsRemoved值,和相应BMCA,可以避免环形PTP报文循环。 | 0 |
| tx_timestamp_timeout | 等待发送时间戳的超时时间,单位ms | 1 |
| check_fup_sync | 检查sync,follow_up报文顺序 | 0 |
| clock_servo | 时钟伺服器,可选择pi,ntpshm,nullf | pi |
| clock_type | 时钟类型,OC,BC,E2E_TC,P2P_TC | OC |
| pi_proportional_const | pi算法选项 | 0.0 |
| pi_integral_const | pi算法选项 | 0.0 |
| pi_proportional_scale | pi算法选项 | 0.0 |
| pi_proportional_exponent | pi算法选项 | -0.3 |
| pi_proportional_norm_max | pi算法选项 | 0.7 |
| pi_integral_scale | pi算法选项 | 0.0 |
| pi_integral_exponent | pi算法选项 | 0.4 |
| pi_integral_norm_max | pi算法选项 | 0.3 |
| step_threshold | 步进时钟的阈值,超过阈值,直接调整时钟时间,单位秒 | 0.0 |
| first_step_threshold | 第一次步进时钟的阈值,超过阈值,直接调整时钟时间,单位秒 | 0.00002 |
| max_frequency | 最大频率调整值,单位ppb | 900000000 (90%) |
| sanity_freq_limit | PTP时钟和系统monotonic时钟频率偏差限制,单位ppb | 200000000 (20%) |
| initial_delay | 链路延迟初始值,单位ns。设置为0的话,程序会先测量 | 0 |
| ntpshm_segment | ntpshm私服使用的SHM段的数目 | 0 |
| udp6_scope | IPv6多播报文的范围,默认为全局范围 | 0x0E |
| uds_address | UNIX domain socket地址 | /var/run/ptp4l |
| dscp_event | PTP事件报文使用的Differentiated Services Codepoint (DSCP),0~63 | 0 |
| dscp_general | PTP通用报文使用的Differentiated Services Codepoint (DSCP),0~63 | 0 |
| dataset_comparison | BMCA比较dataset的方法,可选择"ieee1588"或 “G.8275.x” | ieee1588 |
| logging_level | log级别 | 6 (LOG_INFO) |
| message_tag | 可以添加到所有打印信息中 | 空字符 |
| verbose | 打印消息到标准输出 | 0 |
| use_syslog | 打印消息到系统日志 | 1 |
| summary_interval | 打印统计信息时间间隔,时间间隔值为该值的2的次方。 | 0(1秒) |
| time_stamping | 时间戳类型,可选择hardware, software, legacy, onestep, 或p2p1step | hardware |
| productDescription | 时钟描述 | ;; |
| revisionData | 时钟描述 | ;; |
| userDescription | 时钟描述 | 空字符 |
| manufacturerIdentity | 时钟描述 | 00:00:00 |
| kernel_leap | linux内核闰秒 | 1 |
| timeSource | 时钟类型 | 0xA0 |
| hwts_filter | 硬件时间戳过滤器设置模式,可选择normal, check, full | normal |
| asCapable | 如果设置为true,就认为是gPTP设备,不再检查。 | auto |
| BMCA | 最优时钟算法,可选择masterOnly,slaveOnly或ptp | ptp |
| inhibit_announce | 禁止发送Annouce报文,需要使能ignore_source_id | 0 |
| ignore_source_id | 忽略sync和follow_up报文的source port id检查 | 0 |
| msg_interval_request | 是否使能请求调整sync和peer delay request报文的发送间隔 | 0 |
| servo_num_offset_values | 时间偏差值连续小于servo_offset_threshold的数目,达到这个数目伺服器进入stable状态 | 10 |
| servo_offset_threshold | 时间偏差阈值 | 0 |
| slave_event_monitor | slave事件监测的UNIX domain socket地址 | 空字符 |
| write_phase_mode | 使用PTP硬件写相位功能,代替调整频率偏差 | 0 |
| 配置选项 | 描述 |
|---|---|
| table_id | table id,正数值 |
| logQueryInterval | 单播协商时间间隔,时间间隔值为该值的2的次方。 |
| peer_address | P2P单播地址 |
| L2|UDPv4|UDPv6 | 传输协议 |