1 # NQPTP – Not Quite PTP
2 `nqptp` is a daemon that monitors timing data from [PTP](https://en.wikipedia.org/wiki/Precision_Time_Protocol) clocks it sees on ports 319 and 320. It maintains records for one clock, identified by its Clock ID.
4 It is a companion application to [Shairport Sync](https://github.com/mikebrady/shairport-sync) and provides timing information for AirPlay 2 operation.
8 This guide is for recent Linux and FreeBSD systems.
10 As usual, you should first ensure everything is up to date.
12 #### Please use `git`!
13 As you probably know, you can download the repository in two ways: (1) using `git` to clone it -- recommended -- or (2) downloading the repository as a ZIP archive. Please use the `git` method. The reason it that when you use `git`,
14 the build process can incorporate the `git` build information in the version string you get when you execute the command `$ nqptp -V`.
15 This will be very useful for identifying the exact build if you are making comments or bug reports. Here is an example:
17 Version with git build information:
18 Version: 1.1-dev-24-g0c00a79. Shared Memory Interface Version: 5.
20 Version without git build information:
21 Version: 1.1-dev. Shared Memory Interface Version: 5.
23 ### Remove Old Service Files
25 If you are updating from version `1.2.4` or earlier in Linux, remove the service file `nqptp.service` from the directories `/lib/systemd/system` and `/usr/local/lib/systemd/system` (you'll need superuser privileges):
27 # rm /lib/systemd/system/nqptp.service
28 # rm /usr/local/lib/systemd/system/nqptp.service
29 # systemctl daemon-reload
31 Don't worry if you get a message stating that the files doesn't exist -- no harm done.
34 At present, there is no need to remove the old startup script as (in FreeBSD only) it is always replaced during the `# make install` step.
36 The startup script is at `/usr/local/etc/rc.d/nqptp`.
40 Note that you will need superuser privileges to install, enable and start the daemon.
44 $ git clone https://github.com/mikebrady/nqptp.git
47 $ ./configure --with-systemd-startup
53 $ git clone https://github.com/mikebrady/nqptp.git
56 $ ./configure --with-freebsd-startup
60 The `make install` installs a startup script as requested. You should enable it and start it in the normal way:
62 ### First Install or Update?
65 If you are installing `nqptp` for the first time, enable it and start it:
67 # systemctl enable nqptp
68 # systemctl start nqptp
70 If Shairport Sync is already running, you should restart it after starting `nqptp`:
72 # systemctl restart shairport-sync
75 If you are updating an existing installation of `nqptp`, after installing it you should restart it. You should then also restart Shairport Sync:
77 # systemctl restart nqptp
78 # systemctl restart shairport-sync
82 If you are installing `nqptp` for the first time, add an automatic startup entry for it in `/etc/rc.local` and start it:
83 1. Edit `/etc/rc.conf` and add the following line:
87 2. When you have finished editing `/etc/rc.conf`, you can start `nqptp` from the command line:
91 If Shairport Sync is already running, you should you restart it after starting `nqptp`:
93 # service shairport_sync restart
97 If you are updating an existing installation of `nqptp`, after installing it you should restart it. You should then also restart Shairport Sync:
99 # service nqptp restart
100 # service shairport_sync restart
104 If your system runs a firewall, ensure that ports 319 and 320 are open for UDP traffic in both directions. These ports are associated with PTP service and may be referred to as "PTP" in firewall rules. For example, the following would open ports 319 and 320 for Fedora, which uses `firewalld`:
106 # firewall-cmd --add-service=ptp
107 # firewall-cmd --permanent --add-service=ptp # make it permanent across reboots
111 The `nqptp` application requires exclusive access to ports 319 and 320.
112 This means that it can not coexist with any other user of those ports, such as full PTP service daemons.
113 In Linux, `nqptp` runs as a low-priviliged user but is given special access to ports 319 and 320 using systemd `AmbientCapabilities`.
114 In FreeBSD, `nqptp` runs as `root` user.
117 Commands and status information are sent to `nqptp` over port 9000.
119 Information about the PTP clock is provided via a [POSIX shared memory](https://pubs.opengroup.org/onlinepubs/007908799/xsh/shm_open.html) interface.
122 Here are details of the interface:
125 uint64_t master_clock_id; // the current master clock
126 uint64_t local_time; // the time when the offset was calculated
127 uint64_t local_to_master_time_offset; // add this to the local time to get master clock time
128 uint64_t master_clock_start_time; // this is when the master clock became master
131 // The actual interface comprises a shared memory region of type struct shm_structure.
132 // This comprises two records of type shm_structure_set.
133 // The secondary record is written strictly after all writes to the main record are
134 // complete. This is ensured using the __sync_synchronize() construct.
135 // The reader should ensure that both copies match for a read to be valid.
136 // For safety, the secondary record should be read strictly after the first.
138 struct shm_structure {
139 uint16_t version; // check this is equal to NQPTP_SHM_STRUCTURES_VERSION
140 shm_structure_set main;
141 shm_structure_set secondary;
145 Clock records that are not updated for a period are deleted.
147 * `nqptp` has not been thoroughly checked or audited for security issues. Note that it runs in `root` mode on FreeBSD.
148 * It's probably buggy!
149 * `nqptp` does not take advantage of hardware timestamping.
152 The `nqptp` daemon is under active development and, consequently, everything here can change, possibly very radically.
155 `nqptp` uses just a part of the [IEEE 1588-2008](https://standards.ieee.org/standard/1588-2008.html) protocol. It is not a PTP clock.