PTPd Source Code Documentation

This is a brief overview of PTPd's source code intended to aid developers looking through PTPd.

This documentation is incomplete, and it will be worked on as time permits. More information, particularly on PTPd's clock servo, is available in this paper written for the 2005 Conference on IEEE 1588.

The IEEE 1588 Precision Time Protocol

The PTP daemon (PTPd) implements the Precision Time protocol (PTP) as defined by the IEEE 1588 standard. PTP was developed to provide very precise time coordination of LAN connected computers. The IEEE 1558 specification dictates most of PTPd's operation, so one should obtain a copy of the spec before going through PTPd's source code. PTPd keeps all of the names used in the spec, so it should be easy to match up PTPd's source code with the spec. Descriptions herein will assume knowledge of the spec.

Playing with PTPd

The best way to become familiar with a program is to use it. One should be able to run PTPd on standard Linux systems without much trouble. To better observe the operation of PTPd, compile it with the PTPD_DBG flag to have the program produce debugging output, and run PTPd with the '-c' argument to prevent it from backgrounding itself.

For PTPd to operate, it must be run on at least two computers connected through an Ethernet hub or switch. It is possible to have PTPd coordinate two (and only two) computers through a routed connection by running PTPd on each computer with the '-u' argument followed by the IP address of the other computer.

Right out of the box, PTPd should easily be able to coordinate the clocks of your computers with microsecond-level precision. The default distribution achieved coordination within ten microseconds running on fairly busy embedded systems with 66MHz m68k processors. While it should be easy to get PTPd to provide a precisely coordinated time-base on Linux systems, it is more difficult to use the coordinated time-base. This is because the Linux API currently (as of early 2.6) provides no mechanism to generate high-precision timed events. Jiffy timers are currently Linux's standard timing mechanism. Jiffy timers have a maximum precision dictated by the kernel's HZ value, which is typically 100 on 2.4 kernels (ten millisecond resolution) or 1000 on 2.6 kernels (one millisecond resolution).

There are ways to generate high-precision timed events with Linux, but timing in Linux, or any software system, is a tricky issue. It is also typically platform and application specific. PTPd's maintainers would be a good source of knowledge on precise timing in software systems; however, they can only do so much for free.

PTPd's Source Code Organization

PTPd's source is grouped into a few components. The component delineations are based on the functionality defined by the spec, but the delineations are not specifically defined by the spec.

The following is a block diagram of PTPd's major components, in which occlusion indicates interfaces between components.

[organization block diagram]

In general, PTPd's source is divided into platform-independent and platform-dependent code. Platform-independent code is located in the top level of the PTPd source tree, and platform-dependent code is located the dep/ sub-directory.

PTPd's major source code components are:

Protocol Engine, protocol.c
The main protocol state machine defined in the spec. The state machine is implemented as a forever loop with cases for each state. It is called with protocol() after start-up, and only returns on an error. Normal execution is expected to be halted externally and asynchronously. The loop sleeps upon after each iteration if there are no pending operations. The primary states, handled by doState(), are master, slave, inactive, and faulty. State transitions, handled by toState(), occur primarily due to the results of the BMC. The primary events are message receipts, checked for in handle(), and timer expiration. The primary actions are message sends, in issue(), timer resets, regular runs of the BMC, foreign master data updates after sync message receipts, and system clock updates after syncs.
BMC, bmc.c
The Best Master Clock algorithm defined by the spec. It is called with bmc(), and it returns the proper state, master or slave, based on the reported clock statistics of other PTP clocks that have sent Sync messages (foreign masters).
Clock Servo, ptpd_dep/servo.c
The clock servo computes the offset-from-master from the master-to-slave delay and slave-to-master delays. It uses the offset-from-master to compute clock tick rate adjustments to minimize the offset-from-master. The clock servo also performs filtering to remove jitter from its input.
Message Packer, ptpd_dep/msg.c
Gathers data into and extracts data from PTP messages, the format of which is defined by the spec.
Network Layer, ptpd_dep/net.c
Initializes connections, and sends and receives data between PTP clocks. The network layer also retrieves time stamps for Event messages from the Time Stamp component.
Time Stamp
Records message send and receive times. The send and receive times are used to compute the master-to-slave and slave-to-master delay. The delays are then used to coordinate slave clocks with master clock time. Time stamps should be recorded as close to the networking hardware as possible. This minimizes jitter in the time stamps, which is the most significant detriment to PTPd's clock coordination.
Timer, timer.c
Low resolution interval timers used in the protocol engine. The timers control Sync periodic sends by masters, Delay Request sends by slaves, periodic runs of the BMC (state change events), and Sync receive timeouts.
Start-up, ptpd_dep/startup.c
Sets the program's execution state, and retrieves run-time options from the user.
Other code
The ptpd.c file contains the execution entry point. The ptpd header files contain function prototype declarations. The datatypes header files contain structure and data type declarations. Finally, the constants header files contain constant declarations.

PTPd's Clock Servo

The IEEE 1588 spec does not define how the offset from master produced by a PTP slave is used to bring the slave clock into coordination with master clock time. This procedure, called clock discipline, is not trivial, and there are many possible design approaches and trade-offs. For these reasons, this documentation devotes a section exclusively to the clock servo component.

The following shows the message send and receive paths in a typical system running PTPd, along with the associated time stamps that form the basis of the master-to-slave and slave-to-master delay measurements.

[data path diagram]

The following is a system diagram of PTPd's clock servo. The FIR filtering of the offset from master input is a simple, two-sample average. The IIR filtering of the input one-way delay is described below. The PI controller that mediates the tick rate adjustment output has the difference equation: y[n] = e[n]/Ap + a[n], a[n] = e[n]/Ai + a[n-1].

[clock servo system diagram]

The following are plots of the frequency response of the one-way delay filter. It is a variable cutoff/delay low-pass, infinite impulse response (IIR) filter. The one-way delay filter has the difference equation: s*y[n] - (s-1)*y[n-1] = x[n]/2 + x[n-1]/2, where increasing the stiffness (s) lowers the cutoff and increases the delay.

[offset filter plot]