Linux PTP学习

前言

本文是对Linux PTP的学习记录,不足之处请指出。Linux PTP用于在Linux系统的精确时钟同步,支持IEEE 1588 Precision Time Protocol(PTP)标准,目的是实现在网络中,设备之间的高精度时间同步。它是一个工具的集合,核心工具有ptp4l,phc2sys等。ptp4l作为最核心的工具,用于网络中的PTP设备通信,可以将本地PTP 硬件时钟与PTP grandmaster同步,而phc2sy可以将本地 PTP 硬件时钟同步到系统时钟(反之也可),从而实现了本地系统时钟对PTP grandmaster的同步。

前提条件

首先要检查本地的PTP 硬件设备例如网卡是否支持软硬件时间戳,可以用ifconfig查看网卡名称,然后 使用命令:

ethtool -T enp23s0f0

对于软件时间戳的支持应该包括:

  • SOF_TIMESTAMPING_SOFTWARE
  • SOF_TIMESTAMPING_TX_SOFTWARE
  • SOF_TIMESTAMPING_RX_SOFTWARE

对于硬件时间戳的支持应该包括:

  • SOF_TIMESTAMPING_RAW_HARDWARE
  • SOF_TIMESTAMPING_TX_HARDWARE
  • SOF_TIMESTAMPING_RX_HARDWARE

安装Linux PTP

下载源码:

git clone http://git.code.sf.net/p/linuxptp/code linuxptp

build and install:

cd linuxptp
git checkout v2.0
make
sudo make install

ptp4l使用范例(软件时间戳)

用网线连接两台主机的网卡,eno1和enp0s25。eno1作为主时钟,enp0s25作为从时钟。
首先可以用ptp4l -h查看运行选项:

 [options]

 Delay Mechanism

 -A        Auto, starting with E2E # 自动选择延迟机制。,从E2E开始,当收到对等延迟请求时切换到P2P
 -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'                   # 从指定文件file中读取配置。 默认情况下不读取配置文件
 -i [dev]  interface device to use, for example 'eth0'      # 选择PTP接口设备,例如eno1
           (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

master 主机使用命令和日志:

sudo ptp4l -i eno1 -m -S

slave 主机使用命令和日志:

sudo ptp4l -i enp0s25 -m -S -s
  • master offset值表示从主设备测量的偏移量(以纳秒为单位)
  • s0/s1/s2表示PTP设备的不同状态,s0表示未锁定,s1表示正在同步,s2表示锁定,即SERVO_LOCKED,同步到了稳定状态,之后会根据时间戳做细微调整,继续保持和 master 时钟的同步
  • 当端口状态从UNCALIBRATED改变到SLAVE时,表示已经成功与一个PTP主时钟同步
  • freq表示时钟频率的调整,以十亿分之一(ppb)为单位
  • path delay表示从主时钟发送的同步消息的估算延迟(以纳秒为单位)

ptp4l使用范例(硬件时间戳)

master主机使用命令:

sudo ptp4l -i eno1 -m -H

slave主机使用命令:

sudo ptp4l -i enp0s25 -m -H -s

phc2sys使用范例

首先可以用phc2sys -h查看运行选项:

usage: phc2sys [options]

 automatic configuration:
 -a             turn on autoconfiguration                         # 自动寻找当前运行的ptp4l程序,利用它的时钟,同步给操作系统时钟,操作系统时钟是slave
 -r             synchronize system (realtime) clock
                repeat -r to consider it also as a time source
 manual configuration:
 -c [dev|name]  slave clock (CLOCK_REALTIME)                      # 指定slave clock
 -d [dev]       master PPS device                                 
 -s [dev|name]  master clock                                      # 指定master clock
 -O [offset]    slave-master time offset (0)
 -w             wait for ptp4l                                    # 等待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

例如将上面slave主机的系统时钟与网卡同步命令与日志:

sudo phc2sys -s enp0s25 -w -m -R 8
  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李71~李先森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值