目录
    
   
    一、前言
   
    1.1 什么是ptp4l
   
    1.2 描述
   
    1.3 官网连接
   
    1.4 目的
   
    1.5 平台
   
      二、 平台必要软件
     
      2.1 linux物理机
     
      2.2 imx6ull开发板
     
     三、检查硬件是否支持
    
     3.1 ifconfig查看当前网卡的名称
    
      
    
     3.2 ethtool检查驱动和网卡是否支持
    
为了使用 PTP ,网络接口的内核网络驱动程序必须支持软件或硬件时间戳功能。除了驱动程序中存在的硬件时间戳支持之外,NIC 还必须能够在物理硬件中支持此功能。验证特定驱动程序和 NIC 的时间戳功能的最佳方法是使用ethtool查询接口
命令:ethtool -T {网口名称}
      
    
注:针对虚拟机,只有软件时间戳。
| 软件时间戳支持 | 硬件时间戳支持 | 
| SOF_TIMESTAMPING_SOFTWARE SOF_TIMESTAMPING_TX_SOFTWARE SOF_TIMESTAMPING_RX_SOFTWARE | SOF_TIMESTAMPING_RAW_HARDWARE SOF_TIMESTAMPING_TX_HARDWARE SOF_TIMESTAMPING_RX_HARDWARE | 
     四、开启ptp4l服务
    
下面提供两种开启ptp4l服务的方式,一种为官方推荐方法,另一种为我自己摸索的方法。
     4.1 方法一:官方推荐方法开启ptp4l服务
    
参考README配置(路径:/usr/share/doc/linuxptp/README.Debian)
根据README进行一步步设置,其中ExecStart是调用ptp4l,其配置项可以使用man ptp4l或者看下面我提取的配置项进行合理配置。
1. Default configuration
The default configuration can be found in /etc/linuxptp/ptp4l.conf.
This is the default.cfg provided by the upstream source.
2. Systemd services
The service ptp4l invokes ptp4l on eth0 by default. To adjust the
parameters, follow these steps:
1. create a directory /etc/systemd/system/ptp4l.service.d
2. place a file with its name ending in .conf there
3. put these lines into the file, with the parameters adjusted to your needs:
[Service]
ExecStart=
ExecStart=/usr/sbin/ptp4l -f /etc/linuxptp/ptp4l.conf -i eth0
The service phc2sys syncs the system clock with the PTP clock. It is not
enabled and started by default. To activate this service, invoke the
following commands as root:
$ systemctl enable phc2sys
$ systemctl start phc2sys
It also uses eth0 as the default and can be customized as described above.
The service timemaster also isn't enabled and started by default
     4.2 方法二:自行摸索的配置ptp4l服务方法(虚拟机平台)
    
     
      缘由:
     
     为什么不根据官方的方式配置服务?因为我是apt安装ptp4l后,直接systemctl start ptp4l启动,然后使用systemctl status ptp4l查看错误项进行排查的。而且排查过程中,发现不同系统(虚拟机、物理机)的ptp4l服务配置文件位置有所不同,因此可以适用我下面的方法。
    
1、问题现象
开启ptp4l服务后,检查服务命令:systemctl status ptp4l。看到启动的服务为failed
      
    
2、资料查询
使用man ptp4l,查看Process一栏执行的命令是什么意思。
PTP4l(8)                                            System Manager's Manual                                            PTP4l(8)
NAME
       ptp4l - PTP Boundary/Ordinary Clock
SYNOPSIS
       ptp4l [ -AEP246HSLmqsv ] [ -f config ] [ -p phc-device ] [ -l print-level ] [ -i interface ] [ long-options ] ...
DESCRIPTION
       ptp4l is an implementation of the Precision Time Protocol (PTP) according to IEEE standard 1588 for Linux. It implements
       Boundary Clock (BC) and Ordinary Clock (OC).
OPTIONS
       -A     Select the delay mechanism automatically. Start with E2E and switch to P2P when a peer delay request is received.
       -E     Select the delay request-response (E2E) mechanism. This is the default mechanism. All clocks on single PTP commu‐
              nication  path  must  use  the same mechanism. A warning will be printed when a peer delay request is received on
              port using the E2E mechanism.
       -P     Select the peer delay (P2P) mechanism. A warning will be printed when a delay request is received on  port  using
              the P2P mechanism.
       -2     Select the IEEE 802.3 network transport.
       -4     Select the UDP IPv4 network transport. This is the default transport.
       -6     Select the UDP IPv6 network transport.
       -H     Select the hardware time stamping. All ports specified by the -i option and in the configuration file must be at‐
              tached to the same PTP hardware clock (PHC). This is the default time stamping.
       -S     Select the software time stamping.
       -L     Select the legacy hardware time stamping.
       -f config
              Read configuration from the specified file. No configuration file is read by default.
       -i interface
              Specify a PTP port, it may be used multiple times. At least one port must be specified by this option or  in  the
              configuration file.
       -p phc-device
              (This  option  is  deprecated.)   Before Linux kernel v3.5 there was no way to discover the PHC device associated
              with a network interface.  This option specifies the PHC device (e.g. /dev/ptp0)  to  be  used  when  running  on
              legacy kernels.
       -s     Enable the slaveOnly mode.
       -l print-level
              Set  the  maximum syslog level of messages which should be printed or sent to the system logger. The default is 6
              (LOG_INFO).
       -m     Print messages to the standard output.
       -q     Don't send messages to the system logger.
       -v     Prints the software version and exits.
       -h     Display a help message.3、参数分析
针对命令/usr/sbin/ptp4l -f /etc/linuxptp/ptp4l.conf -i eth0。
其中-f 用于指定配置文件。 -i 用于指定网口。
根据图片提示信息可看出,问题点是指定的网卡名称有误,通过3.1的ifconfig命令查看,当前的物理网口的接口为ens33
而且由于是虚拟机平台,并且3.2 中也查看了网卡只支持软件时间戳功能。因此需要加上-S配置项。
4、修改参数
     修改参数的第一步是找到配置文件所在位置。我经过测试虚拟机平台和物理机平台。发现ptp4l配置文件所在位置有所区别,因此各位同学也需要根据实际情况
     
      找到配置文件的位置
     
     。
    
查找配置文件位置:
cd /etc
sudo grep "ExecStart=/usr/sbin/ptp4l -f /etc/linuxptp/ptp4l.conf" -R
      
    
可以得知ptp4l配置文件的位置:/etc/systemd/system/multi-user.target.wants/ptp4l.service。修改网卡接口以及适用软时间戳。
      
    
保存后,按照如下指令重新启动ptp4l即可:
sudo systemctl daemon-reload    #修改service后需要重新加载systemd manager configuration
sudo systemctl restart ptp4l    #重启ptp4l
sudo systemctl status ptp4l     #查看是否启动成功
      
    
     4.3 验证软时间戳时间同步
    
测试平台:
使用两个虚拟机,其中一个作为master,另一个作为slave
前提:针对虚拟机,需要确保双方能够互相ping通
 
     
     
