Introduction, installation and use of clock synchronization software ptp4l

1. Introduction to ptp4l

ptp4l is an open source software under Linux and one of the implementations of IEEE 1588 Precision Time Protocol (PTP). Its function is to synchronize the clocks of multiple systems over the network and provide high-precision time stamps. The following explains its concept, implementation functions, advantages and disadvantages, and application scenarios.

concept:

PTP is a network protocol used to achieve clock synchronization in distributed systems. ptp4l (PTPv2 Linux implementation) is a PTP protocol implementation that supports PTPv2 protocol, clock precision level (clock precision) selection, delay compensation and other functions. It can synchronize the clocks of multiple PTP nodes through the network and provide high-precision time stamps.

Realize the function:

ptp4l is often used to achieve clock synchronization and high-precision time stamping, and can be used in audio and video synchronization, data acquisition and processing, financial transactions, power system synchronization, industrial control and other fields. It provides the following main functions:

  1. Realize clock synchronization: ptp4l can synchronize the clocks of multiple nodes through the protocol to achieve high-precision time synchronization.

  2. Provide high-precision timestamps: ptp4l can timestamp received data and provide sub-nanosecond-level time precision.

  3. Support different precision levels: ptp4l can support different clock precision levels to achieve higher precision clock synchronization.

  4. Support for delay compensation: ptp4l can diagnose network delay and compensate the clock according to the delay.

advantage:

  1. High precision: ptp4l provides time precision at the sub-nanosecond level, which can meet scenarios that require high time precision.

  2. Scalability: ptp4l can synchronize the clocks of multiple nodes through the protocol, and can be extended to distributed systems.

  3. Flexibility: ptp4l can adapt to different application scenarios by configuring different clock precision levels, delay compensation and other parameters.

shortcoming:

  1. Limited by hardware support: The time synchronization accuracy of ptp4l is limited by hardware support. If the hardware does not support high-precision clock synchronization, the accuracy will be limited.

  2. Depends on the network: ptp4l implements clock synchronization through the network, so it is affected by factors such as network delay and bandwidth.

2. Application scenarios:

ptp4l is mainly used in scenarios that require high-precision clock synchronization, such as audio and video synchronization, data acquisition and processing, financial transactions, power system synchronization, industrial control and other fields. In these scenarios, it is necessary to synchronize the clocks in different systems to achieve high precision and consistency, so as to ensure the stability and normal operation of the system.

Here are the brief steps to install and use LinuxPTP on CentOS 7:

  1. Open a terminal and install LinuxPTP with the following command:

    sudo yum install linuxptp
    

  2. Verify that LinuxPTP was installed successfully. Enter the following command in the terminal:

    ptp4l -v
    

    If the LinuxPTP version number is displayed, the installation was successful.

  3. Configure LinuxPTP. Enter the following command in the terminal:

    sudo nano /etc/ptp4l.conf
    

    Open the configuration file and make changes as needed. For example, you can change clock settings, physical network interfaces, PTP network interfaces, and more.

  4. Start LinuxPTP. Enter the following command in the terminal:

    [root@localhost ~]# ptp4l -i enp1s0 -f /etc/ptp4l.conf -m
    ptp4l[519569.670]: selected /dev/ptp0 as PTP clock
    ptp4l[519569.672]: port 1: INITIALIZING to LISTENING on INIT_COMPLETE
    ptp4l[519569.672]: port 0: INITIALIZING to LISTENING on INIT_COMPLETE
    ptp4l[519577.354]: port 1: LISTENING to MASTER on ANNOUNCE_RECEIPT_TIMEOUT_EXPIRES
    ptp4l[519577.354]: selected local clock 001b21.fffe.c4522a as best master
    ptp4l[519577.354]: assuming the grand master role
    
    

    This will start the PTP daemon and configure it to use the eth0 interface, running with the settings specified in /etc/linuxptp/ptp4l.conf.

  5. Check clock synchronization. Enter the following command in the terminal:

    [root@localhost ~]# phc2sys -s enp1s0 -w CLOCK_REALTIME -m
    phc2sys[519870.160]: CLOCK_REALTIME phc offset      -172 s0 freq  +44777 delay    749
    phc2sys[519871.161]: CLOCK_REALTIME phc offset      -177 s2 freq  +44772 delay    758
    phc2sys[519872.161]: CLOCK_REALTIME phc offset      -173 s2 freq  +44599 delay    758
    phc2sys[519873.161]: CLOCK_REALTIME phc offset       -25 s2 freq  +44695 delay    743
    phc2sys[519874.161]: CLOCK_REALTIME phc offset        44 s2 freq  +44757 delay    745
    phc2sys[519875.161]: CLOCK_REALTIME phc offset        34 s2 freq  +44760 delay    751
    phc2sys[519876.162]: CLOCK_REALTIME phc offset        61 s2 freq  +44797 delay    745
    phc2sys[519877.162]: CLOCK_REALTIME phc offset     42355 s2 freq  +87109 delay    749
    phc2sys[519878.162]: CLOCK_REALTIME phc offset       122 s2 freq  +57583 delay    757
    phc2sys[519879.162]: CLOCK_REALTIME phc offset    -12775 s2 freq  +44722 delay    745
    phc2sys[519880.162]: CLOCK_REALTIME phc offset    -12753 s2 freq  +40912 delay    754
    phc2sys[519881.163]: CLOCK_REALTIME phc offset     -8903 s2 freq  +40936 delay    744
    
    

    This will synchronize the PHC (Ptp Hardware Clock) with the real clock, thus ensuring that the clocks are in sync.

The above are the basic steps to install and use LinuxPTP on CentOS 7. In fact, you need to know more about the detailed configuration and usage of LinuxPTP to ensure the normal operation of the system.

Common parameters

The common parameters of ptp4l have the following meanings:

usage: ptp4l [OPTION]... [INTERFACE]...

ptp4l is an implementation of the Precision Time Protocol (PTP) according
to IEEE standard 1588 for Linux.  PTP is used to synchronize the clocks of
devices over a network. ptp4l, in conjunction with phc2sys, can be used to
synchronize the system clock to an external PTP clock.

OPTION list:
  -C, --config-file     load configuration from file
  -g, --global-override enable global override of settings in configuration files
  -i, --interface       specify an interface to use
  -m, --management      enable the PTP management messages
  -p, --priority        select the priority1 and priority2 for PTP port
  -s, --step-seconds    step the clock if offset is greater than given seconds
  -A, --announce-interval
                        interval in seconds between sending announce messages
  -D, --drift-threshold drift threshold for the clock in seconds per second (default 1e-6)
  -G, --domain          specify PTP domain number (default 0)
  -H, --pdelay-req-hops number of hops for pdelay-req messages (default 1)
  -I, --init-delay      initial delay before sending first announce message (seconds)
  -M, --max-adjustable-offset
                        the maximum offset the clock can be adjusted in seconds
  -O, --outlier-threshold
                        specify outlier threshold for the clock discipline
  -P, --phc2sys         enable phc2sys compatibility mode
  -Q, --ptpengine-debug output debug information for ptpengine (use multiple times for more detail)
  -R, --hw-timestamps   use hardware timestamps instead of software
  -S, --src-addr        specify source IPv4 or IPv6 address (receiver mode only)
  -T, --unicast-address specify unicast destination address instead of multicast
  -U, --user-description
                        specify user description for PTP port
  -V, --version         print version number and exit
  -W, --override-offset-allowed
                        override the offset if the difference is outside acceptable limits (default false)
  -X, --set-timeout     specify timeout in milliseconds for message receptions
  -Y, --sync-rate-limit
                        limit the number of sync messages propagated per second
  -Z, --pid-file        write the PID of ptp4l to a file
  -d, --debug-level     output debugging information (use multiple times for more detail)
  -f, --foreground      stay in foreground and output messages to stdout
  -h, --help            display this help and exit
  -l, --lock-file       lock file used for PID file (default /var/run/ptp4l.lock)
  -o, --one-step        enable the one-step clock mode
  -r, --priority1       specify PTP priority1 value (default 128)
  -t, --priority2       specify PTP priority2 value (default 0)
  -u, --unicast-dest    specify unicast destination for PTP messages
  -v, --verbose         output verbose messages

See the ptp4l(8) man page for more information.

Some of the commonly used parameters are used as follows:

  • -C, --config-file: Specifies the location of the configuration file, eg -C /etc/ptp4l.conf.
  • -i, --interface: Specifies the interface name to use, eg -i eth0.
  • -m, --management: Enable PTP management messages, eg -m.
  • -s, --step-seconds: Steps the clock if the clock offset is greater than the given number of seconds, eg -s 1.
  • -A, --announce-interval: Specifies the interval for sending announce messages, eg -A 30.
  • -M, --max-adjustable-offset: Specifies the maximum offset by which the clock can be adjusted, eg -M 0.1.
  • -Q, --ptpengine-debug: Output the debugging information of ptpengine. You can use -Qthe option multiple times to get more debugging information, eg -Q -Q -Q.
  • -d, --debug-level: Output debugging information, use multiple -doptions to get more detailed debugging information, eg -d -d -d.
  • -f, --foreground: Run as a foreground process, output messages to standard output, eg -f.
  • -h, --help: Display help information, eg -h.
  • -v, --verbose: Print verbose messages, eg -v.

The method of using these parameters can be determined in combination with specific application scenarios.

[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

Attachment: error: failed to create a clock

ptp4l[519360.082]: interface 'enp4s0' does not support requested timestamping mode
failed to create a clock

This error usually appears on Linux systems and indicates that ptp4l was unable to enable PTP timestamp mode on the specified network interface. Possible reasons are:

  1. The network interface does not support PTP timestamp mode. Some network interfaces may not support hardware or software timestamps and therefore cannot be used for PTP synchronization.

  2. Insufficient privileges to enable PTP timestamp mode. On some systems, running ptp4l as an administrator or privileged user is required to access necessary system resources.

To resolve this issue, try the following steps:

  1. Checks whether the network interface supports PTP timestamp mode. You can use ethtoolthe command to see if a network interface supports hardware or software time stamping. For example, use the following command to view enp4s0the timestamp capabilities of a network interface:

    sudo ethtool -T enp4s0
    

    If included in the output PTPv2(IEEE 1588), the network interface supports PTP timestamp mode. Without this option, it means that the network interface does not support PTP timestamp mode.

  2. Confirm whether the current user has sufficient authority to enable PTP timestamp mode. You can try sudorunning ptp4l as an administrator or privileged user with the command.

  3. If you confirm that the network interface supports PTP timestamp mode and the current user has sufficient privileges, but you still encounter this error, try upgrading your system kernel or drivers to ensure that the network interface driver supports PTP timestamp mode.

Hope the above steps can help you solve this problem.

The interface does not support PTP timestamp mode:

 The interface supports PTP timestamp mode:

Guess you like

Origin blog.csdn.net/songpeiying/article/details/131826141