Precision Time Protocol (PTP) is a protocol to synchronize clocks in a computer network, similar to Network Time Protocol (NTP).
NTP is accurate, under ten milliseconds. PTP, however, is accurate up to less than a microsecond and is measured in nanoseconds. Why would you want this? NTP is fine for network clients such as desktops, laptops, or network monitoring. PTP is useful in scenarios where very accurate timing is required. For example:
PTP can be used in these scenarios. One advantage of PTP is that if you already have Ethernet or IP, you can use your existing network for time synchronization. PTP can run directly over Ethernet or on top of IP with UDP for transport.
PTP is an IEEE/IEC standardized protocol defined in IEEE 1588, with multiple versions of PTP. The first version is from 2002, specified in IEEE 1588-2002. PTP Version 2 (PTPv2) was announced in IEEE 1588-2008. This version is not backward compatible with the 1588-2002 version. We now also have IEEE 1588-2019, known as PTPv2.1. This version is compatible with PTPv2 and adds some additional features.
Each industry has different requirements, which is why, besides the standard, there are different profiles. Here are some examples:
There are differences in how PTP is implemented between these profiles. For example:
And there are other differences, such as how often they send messages, the clock types they support, etc.
When we want to synchronize clocks accurately up to nanoseconds, we have to deal with delays. For example:
When one clock transmits the current time, it can be outdated the moment it is received by another clock. Luckily, PTP has a way to deal with this. Delay is inevitable, but we can measure, and PTP can compensate for it.
PTP works best when your hardware supports it. A NIC with PTP support has its own onboard clock, which is used to timestamp messages. You can run PTP in software without hardware support, but your OS will have to take care of timestamping. This negatively affects delay.
There are multiple options where you can timestamp. The closer to the physical layer, the more reliable and less delay you’ll see.
PTP uses the same epoch as Unix time (1 January 1970) but is based on International Atomic Time (TAI). The abbreviation is from the French name (Temps Atomique International). It’s not based on UTC. One of the reasons is that UTC is subject to leap seconds. PTP communicates the offset between TAI and UTC so that UTC can be calculated from the PTP time.
To ensure clocks are synchronized correctly, PTP must measure the delay between two clocks. We have two clock roles. The time source has the master role, and the time receiver is the slave.
Because of the insensitivity of using slavery as a technical analogy, the IEEE 1588g-2022 amendment replaces the terminology “master” and “slave”. The master will be replaced by “timeTransmitter” and the slave will be replaced by “timeReceiver”. It will take time until all network devices, manuals, and marketing material will be updated. To prevent confusion, I will use the master/slave terminology for now and will update the new terminology in the future when it is more common.
PTP synchronizes clocks by exchanging a couple of timestamped messages and storing the time we send and receive these messages. A slave clock calculates its delay and offset and updates its clock accordingly. Let’s look at how PTP synchronizes clocks. I’ll use a simple example with two clocks:
The master clock is at 9:00:00. The slave clock thinks it’s 9:00:10, so we have a 10-second offset. To keep it simple, we’ll imagine we have a one-second delay to transfer packets from the master to the slave clock.
The master periodically sends a sync message. Let’s say it sends a sync message at exactly 09:00:00. This moment is the T1 value. We now have two options:
In my example, we use the two-step message exchange.
The slave clock stores the time it received the sync message as the T2 value. Its local clock believes it is 09:00:12. The slave clock now knows there is a 10-second difference between its local clock and the master clock. However, it can’t update itself yet because it doesn’t know how long it took to get a message from the master to the slave clock.
To synchronize itself correctly, it has to calculate two values:
Here are the two formulas to calculate these two values:
delay = ((t2 - t1) + (t4 - t3)) / 2
offset = ((t2 - t1) - (t4 - t3)) / 2
We have seen T1 and T2, but we haven’t seen T3 and T4 yet.
When the slave clock receives the follow-up message, it sends a delay request message. As the name implies, this message is used to determine the delay between the master and slave clock. The slave clock stores the time it sent this message as the T3 value. From the slave clock’s perspective, at 09:00:12.
Because of our one-second delay, the master clock receives it at 09:00:03. The master clock stores this moment as T4.
The master clock replies immediately with a delay response message that includes the T4 value. The slave clock now has four timestamps:
The slave clock can now calculate the offset relative to the master clock. The offset calculation assumes that the delay in propagating the message from the master to the slave clock is the same as the other way around. This is not always the case on Ethernet networks, though.
Let’s put everything in the delay and offset formula. Everything starts with 09:00, so we’ll focus on the seconds. Let’s start with delay. Here is the formula to calculate delay:
delay = ((t2 - t1) + (t4 - t3)) / 2
Let’s add our numbers:
delay = ((11 - 0) + (3 - 12)) / 2
Let’s break it down:
(11 - 0) = 11 (3 - 12) = -9 11 + -9 = 2 2 / 2 = 1
The delay is calculated as one second. Now, we’ll calculate the offset. Here is the formula:
offset = ((t2 - t1) - (t4 - t3)) / 2
Let’s add our numbers:
offset = ((11 - 0) - (3 - 12)) / 2
Let’s break it down:
(11 - 0) = 11 (3 - 12) = -9 11 - -9 = 20 20 / 2 = 10
The calculated offset is 10 seconds. The slave clock updates itself, offsetting its clock by 10 seconds. Network delay can change over time, so the master clock will keep sending sync messages.
PTP has different clock types. Here is an overview:
There are four clock types:
Besides the clock type, each interface can have the master (M) or slave (S) role. The master sends timing information, and the slave synchronizes itself. Depending on the clock type, an interface can have the master or slave role. It’s also possible that a clock has the master role on one interface and the slave role on another interface.
Let’s look at the different clock types.
The grandmaster clock (GMC) is the primary source of time in PTP and is responsible for root timing reference. This clock is connected to a reliable time source, such as GPS or an atomic clock. All other clocks synchronize directly or indirectly with the grand master clock. The grandmaster clock always has the master role on its interface(s).
The ordinary clock (OC) runs PTP on only one of its interfaces. This interface can have the slave or master role. This is usually an end device that needs its time synchronized.
The boundary clock (BC) runs PTP on two or more interfaces. It can synchronize one network segment with another. The upstream interface that connects to the grandmaster clock has the slave role. The downstream interface that connects to other clocks has the master role.
A boundary clock sits between the grandmaster clock and other boundary or ordinary clocks. Each interface can connect to a different VLAN to synchronize time in different VLANs. Adding boundary clocks to the network also has a scalability advantage because it prevents all ordinary clocks from having to talk with the grandmaster clock directly.
Each boundary clock syncs with an upstream clock and distributes time based on its local clock. This means that each clock depends on the quality of all upstream clocks. The more boundary clocks you add, the higher the chance your clocks are not as accurate anymore. Because of this, using boundary clocks is only suitable for networks with a small number of switches.
Transparent clocks (TC) were introduced in PTPv2, and their goal is to forward PTP messages. They can’t be a source clock like a grandmaster or boundary clock. They can forward PTP messages within a VLAN but not between VLANs.
The transparent clock can sit between the grandmaster clock and an ordinary clock. When we forward a PTP message through the transparent clock, the delay increases, which must be compensated. The transparent clock measures this delay and adds it to the correction field of a PTP message. This added time is called the residence time.
There are two transparent clock types:
Let’s check them out.
The end-to-end (E2) transparent clock sits between the grandmaster and the ordinary clock and transparently forwards PTP messages. Here’s what it looks like:
We see the same messages as those discussed in the clock synchronization section. The blue arrows indicate the time it takes for the transparent clock to forward these messages. This adds delay, and it must be compensated. Transparent clocks keep track of the time it takes to forward a PTP message. This is called the residence time (RT). Transparent clocks add this time in the correction field of PTP messages so that the ordinary clock can compensate for the delay.
The idea behind the peer-to-peer transparent clock (P2P) is that it’s better to calculate the delay for each interface than end-to-end. The P2p transparent clock calculates the delay from:
Here’s what that looks like:
When a grandmaster clock sends a sync (and follow-up) message, the transparent clock forwards these messages to the ordinary clock. It also sends a peer delay request message to the grandmaster clock and receives a peer delay response message. The same peer delay message exchange occurs with the ordinary clock. This allows the transparent clock to figure out the delay on both interfaces. It also keeps track of the residence time and adds this in the correction field of PTP messages.
Why would you pick E2E transparent clocks or P2P transparent clocks? Take a look at this picture:
Above, we have a hierarchical network with PTP clocks. Imagine we have 500 ordinary clocks. If the transparent clocks were all E2E, the grandmaster clock would receive 500 delay request messages from all ordinary clocks. This doesn’t scale well.
With P2P transparent clocks, peer delay messages are only sent between one upstream or downstream clock. Peer-to-peer is more efficient because the delay is calculated on a single link, not end-to-end. Also, the ordinary clocks won’t send delay messages directly to the grandmaster clock.
How do we define which clock becomes the master? We compare announce messages to determine which clock is the most accurate. We use the following order of items to determine the best master clock:
When the BMC algorithm finishes, the master clock sends sync messages at regular intervals. When a current master clock recognizes a new best master clock, it stops sending sync messages (or announce messages when using IEEE 1588-2008), and the new clock takes over as master.
The Best Master Clock Algorithm (BMCA) will be renamed to Best TimeTransmitter Clock Algorithm (BTCA) because of the reasons explained in the master/slave terminology. I will use BMCA throughout this lesson until the new terminology is more common and used on Cisco switches.
Throughout this lesson, I explained some of the PTP messages. It’s useful to have a complete overview of all PTP messages and their function. There are two message types:
Let’s check out both.
Event messages are time-critical. These are used to measure the time delay between clocks and are timestamped.
General messages are not time-critical and don’t have timestamps. They are used to maintain PTP’s operation and manage the state of clocks.
Enough theory for now. Let’s look at PTP in action.