raychenv / blog

my simple blog
1 stars 0 forks source link

Video: Precision Time Protocol (PTP) and Packet Timestamping in Linux #272

Open raychenv opened 2 years ago

raychenv commented 2 years ago

https://thebroadcastknowledge.com/tag/timing/ Antoine Tenart Bootlin

Understand PTP and its mode packet timestamping and how the kernel supports it Why HW timestamp Device driver support of PTP

Background

Event ordering is essential to some applications: Transactions, Logs, Debugging and performance analysis Ordering is based on TS collected from large ranges of machines Need for clocks sync on local network: frequency, phase, time

NTP Network time protocol is a protocol for clock sync Provides accuracy within a few ms Not precise Need higher PTP

Overview PTP

sub ms accuracy on local network IEEE 1588-2002 2008 2019

Leader clock: Time src for PTP sync it with external src GNSS ordinary clock

Bounday clock: optional multiple PTP newtowk connections and relay accurate time sync its clk against leader acts as clock src for the followers May become the leader if the current leader disappear

Follower clock: Synchronize its clock to a leader May become the leader if the leader disappear Is an "ordinary clock"

Time offset is computed based on TS of packet sent and received. Done by the follower. Timestamps made on the leader side are sent to follower by follow up packets(follow_up, delay_resp)

The trip time include the transmit time and the delta between the two clocks d if = t1-t0 + deltat d fi = t3-t2 + deltat We assume the two trip times are equal, hence we have: deltat = 1/2(t1-t0+t2-t3)

PTP offloading support in Linux

Read the full doc at: Documentation/networking/timestamping.rst

Hardware timestamping must be inited for each device driver expected used Configuration passed using the SIOCSHWTSTAMP ioctl, Must choose a tx_type and an rx_filter Possible values of tx_type Possible values for rx_filter see include/uapi/linux/net_tstamp.h

ethtool -T eth0 image

In networking PHY or dedicted engines driver: impl struct mii_timestamper callbacks in struct phy_device ts_info hwtstamp rxtstamp txtstamp and calling skb_complete_tx_timestamp()

Both interfaces allow us to: 1 report ts capabilities: ts_info and get_ts_info 2 Config the mode to use hwtstamp and ndo_do_ioctl 3 Report Rx timestamps (rxtstamp and hwtstamps) 4 Report Tx timestamps (txtstamp/skb_complete_tx_timestamp() and skb_tstamp_tx)

max_adj---- Maximum frequency adjustment in parts per billion

network switch: drivers/net/ethernet/mscc/ PHY drivers/net/phy/mscc/mscc_ptp.c

User-space PTP impl

Linux PTP

IEEE-1588 Richard Cochran PHC and timestamping ptp4l and phc2sys

ptp4l

Impl of PTP for odinary and boundary clocks can use SW or HW timestamping can perform PTP operation on top of UDP can optionally use a config file ptp4l -i eth0 -H -2 -m

phc2sys

Sync two or more clocks keep the system clock in sync with the PHC when using hardware timestamping, ptp4l adjust the PHC and then phc2sys adjust the system clock When using software timestamping, phc2sys isn't used, system clk is directly adjusted by ptp4l

raychenv commented 2 years ago

https://docs.huihoo.com/doxygen/linux/kernel/3.7/include_2linux_2clocksource_8h.html#af582fda8852e22be87a1c85d0a11fc64 https://docs.huihoo.com/doxygen/linux/kernel/3.7/clocksource_8c_source.html https://elixir.bootlin.com/linux/latest/source/include/linux/timecounter.h#L84