-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathptp.cpp
More file actions
425 lines (339 loc) · 14.1 KB
/
Copy pathptp.cpp
File metadata and controls
425 lines (339 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
* @file utils/ptp.cpp
* @author Martin Piatka <piatka@cesnet.cz>
*/
/*
* Copyright (c) 2025-2026 CESNET z.s.p.o.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, is permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of CESNET nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ptp.hpp"
#include <chrono>
#include <cstddef>
#include <algorithm>
#include "rtp/net_udp.h"
#include "utils/thread.h"
#include "compat/platform_sched.h"
#include "debug.h"
#include "host.h"
#define MOD_NAME "[PTP] "
#define PTP_PORT_EVENT 319
#define PTP_PORT_GENERAL 320
#define MAX_PACKET_LEN 128
#define PTP_ADDRESS "224.0.1.129"
#define PTP_FLAG_TWOSTEP 0x200
#define PTP_MSG_ANNOUNCE 0xb
#define PTP_MSG_SYNC 0x0
#define PTP_MSG_FOLLOWUP 0x8
#define PTP_MSG_DELAY_REQ 0x1
#define PTP_MSG_DELAY_RESP 0x9
#define PTP_MSG_MANAGEMENT 0xd
#define LOCK_THRESH_uS_LOW 200
#define LOCK_THRESH_uS_HIGH 400
using clk = std::chrono::steady_clock;
namespace {
struct Timestamped_pkt{
uint64_t local_ts;
uint8_t buf[MAX_PACKET_LEN]; //Event packets should not be larger than 54B
unsigned buflen = 0;
};
template<typename T, unsigned n>
T read_val(uint8_t *ptr){
T ret{};
for(unsigned i = 0; i < n; ++i){
ret <<= 8;
ret |= ptr[i];
}
return ret;
}
template<unsigned n, typename T>
void write_val(uint8_t *ptr, T val){
for(unsigned i = 0; i < n; ++i){
ptr[i] = (val >> ((n - 1 - i) * 8)) & 0xFF;
}
}
struct Ptp_hdr{
bool valid = false;
uint8_t msg_type;
uint16_t msg_len;
uint16_t flags;
uint64_t correction_field;
uint64_t clock_identity;
uint16_t port_number;
uint16_t seq;
uint8_t log_msg_interval;
static constexpr size_t header_length = 34;
std::vector<unsigned char> to_bytes() const{
std::vector<unsigned char> ret;
ret.resize(header_length);
write_val<1>(&ret[0], msg_type);
write_val<1>(&ret[1], 0x2); //Version
write_val<2>(&ret[2], msg_len);
write_val<1>(&ret[4], 0); //Domain number
write_val<1>(&ret[5], 0); //Reserved
write_val<2>(&ret[6], flags);
write_val<4>(&ret[8], correction_field);
write_val<4>(&ret[16], 0); //Reserved
write_val<8>(&ret[20], clock_identity);
write_val<2>(&ret[28], port_number);
write_val<2>(&ret[30], seq);
uint8_t control_field;
switch(msg_type){
case PTP_MSG_SYNC: control_field = 0x00; break;
case PTP_MSG_DELAY_REQ: control_field = 0x01; break;
case PTP_MSG_FOLLOWUP: control_field = 0x02; break;
case PTP_MSG_DELAY_RESP: control_field = 0x03; break;
case PTP_MSG_MANAGEMENT: control_field = 0x04; break;
default: control_field = 0x05; break;
}
write_val<1>(&ret[32], control_field);
write_val<1>(&ret[33], log_msg_interval);
return ret;
}
};
const char *clock_id_to_str(uint64_t id){
static char buf[16 + 7 + 1] = {};
snprintf(buf, sizeof(buf), "%02X-%02X-%02X-%02X-%02X-%02X-%02X-%02X",
(int)(id >> 56) & 0xFF,
(int)(id >> 48) & 0xFF,
(int)(id >> 40) & 0xFF,
(int)(id >> 32) & 0xFF,
(int)(id >> 24) & 0xFF,
(int)(id >> 16) & 0xFF,
(int)(id >> 8) & 0xFF,
(int)id & 0xFF);
return buf;
}
Ptp_hdr parse_ptp_header(uint8_t *buf, size_t len){
Ptp_hdr ret{};
if(len < Ptp_hdr::header_length){
return ret;
}
int version = buf[1] & 0x0F;
if(version != 2){
return ret;
}
ret.msg_type = buf[0] & 0x0F;
ret.msg_len = read_val<uint16_t, 2>(&buf[2]);
ret.flags = read_val<uint16_t, 2>(&buf[6]);
ret.correction_field = read_val<uint64_t, 8>(&buf[8]);
ret.clock_identity = read_val<uint64_t, 8>(&buf[20]);
ret.port_number = read_val<uint16_t, 2>(&buf[28]);
ret.seq = read_val<uint16_t, 2>(&buf[30]);
ret.log_msg_interval = buf[33];
if(ret.msg_len >= Ptp_hdr::header_length)
ret.valid = true;
return ret;
}
} //anon namespace
void Ptp_clock::update_clock(uint64_t new_local_ts, uint64_t new_ptp_ts){
if(synth_ptp_ts == 0) synth_ptp_ts = new_ptp_ts;
auto delta_local = new_local_ts - local_ts;
if(local_ts == 0){
ptp_ts = new_ptp_ts;
local_ts = new_local_ts;
return;
}
ptp_ts = new_ptp_ts;
local_ts = new_local_ts;
synth_ptp_ts += delta_local * spa_corr;
int64_t error_ns = (int64_t) synth_ptp_ts - (int64_t) new_ptp_ts;
spa_corr = spa_dll_update(&dll, error_ns);
avg.push(std::abs(error_ns));
double avg_error_us = avg.get() / 1000;
if(avg.size() >= 10 && avg_error_us < LOCK_THRESH_uS_LOW){
if(!locked){
std::lock_guard<std::mutex> l(mut);
locked = true;
log_msg(LOG_LEVEL_NOTICE, MOD_NAME "Clock locked\n");
cv.notify_all();
}
} else if(avg_error_us > LOCK_THRESH_uS_HIGH && locked){
std::lock_guard<std::mutex> l(mut);
locked = false;
log_msg(LOG_LEVEL_WARNING, MOD_NAME "Clock unlocked (avg err: %f)\n", avg_error_us);
cv.notify_all();
}
update_count.fetch_add(1, std::memory_order_seq_cst);
local_snapshot.store(new_local_ts, std::memory_order_seq_cst);
ptp_snapshot.store(synth_ptp_ts, std::memory_order_seq_cst);
corr_snapshot.store(spa_corr, std::memory_order_seq_cst);
update_count.fetch_add(1, std::memory_order_seq_cst);
}
void Ptp_clock::drop_sync_pkts_older_than(uint16_t seq){
auto new_end = std::remove_if(sync_pkts.begin(), sync_pkts.end(), [seq](const detail::Sync_pkt_data& pkt){ return pkt.seq <= seq; });
sync_pkts.erase(new_end, sync_pkts.end());
}
void Ptp_clock::processPtpPkt(uint8_t *buf, size_t len, uint64_t pkt_ts){
Ptp_hdr header = parse_ptp_header(buf, len);
if(!header.valid){
return;
}
if(header.clock_identity != clock_identity){
if(clock_identity == 0){
set_clock_identity(header.clock_identity);
} else {
return;
}
}
if(header.msg_type == PTP_MSG_SYNC && (header.flags & PTP_FLAG_TWOSTEP)){
uint64_t sec = read_val<uint64_t, 6>(&buf[34]);
uint32_t nsec = read_val<uint32_t, 4>(&buf[40]);
uint64_t new_ptp_ts = sec * 1'000'000'000 + nsec;
sync_pkts.push_back({header.seq, new_ptp_ts, pkt_ts});
return;
}
if(header.msg_type == PTP_MSG_FOLLOWUP){
uint64_t sec = read_val<uint64_t, 6>(&buf[34]);
uint32_t nsec = read_val<uint32_t, 4>(&buf[40]);
uint64_t new_ptp_ts = sec * 1'000'000'000 + nsec;
auto it = std::find_if(sync_pkts.begin(), sync_pkts.end(), [=](const detail::Sync_pkt_data& pkt){ return pkt.seq == header.seq; });
if(it == sync_pkts.end()){
log_msg(LOG_LEVEL_WARNING, MOD_NAME "Sync pkt for followup not found\n");
return;
}
update_clock(it->local_ts, new_ptp_ts);
drop_sync_pkts_older_than(header.seq);
return;
}
}
bool Ptp_clock::wait_for_lock(){
std::unique_lock<std::mutex> l(mut);
using namespace std::chrono_literals;
while(!cv.wait_for(l, 1s, [&]{ return locked; })){
if(!should_run) return false;
}
return true;
}
uint64_t Ptp_clock::get_time(){
uint32_t seq0;
uint32_t seq1;
uint64_t l;
uint64_t p;
double c;
do{
seq0 = update_count.load(std::memory_order_seq_cst);
l = local_snapshot.load(std::memory_order_seq_cst);
p = ptp_snapshot.load(std::memory_order_seq_cst);
c = corr_snapshot.load(std::memory_order_seq_cst);
seq1 = update_count.load(std::memory_order_seq_cst);
}while(seq0 != seq1);
uint64_t now_ts = std::chrono::nanoseconds(clk::now().time_since_epoch()).count();
auto delta_local = now_ts - l;
return p + delta_local * c;
}
void Ptp_clock::ptp_worker_event(){
set_thread_name(__func__);
log_msg(LOG_LEVEL_NOTICE, MOD_NAME "Init sock %s on %s port %d\n", PTP_ADDRESS, network_interface.c_str(), PTP_PORT_EVENT);
auto ptp_sock = socket_udp_uniq(udp_init_if(PTP_ADDRESS, network_interface.c_str(), PTP_PORT_EVENT, 0, 255, 4, false));
if(!ptp_sock){
log_msg(LOG_LEVEL_ERROR, MOD_NAME "Failed to create sock\n");
exit_uv(-1);
should_run = false;
return;
}
set_realtime_sched_this_thread();
while(should_run){
Timestamped_pkt pkt;
timeval timeout {1, 0};
pkt.buflen = udp_recv_timeout(ptp_sock.get(), (char *)pkt.buf, MAX_PACKET_LEN, &timeout);
pkt.local_ts = std::chrono::nanoseconds(clk::now().time_since_epoch()).count();
if(pkt.buflen == 0)
continue;
Ptp_hdr hdr = parse_ptp_header(pkt.buf, pkt.buflen);
if(!hdr.valid){
continue;
}
ring_buffer_write(event_pkt_ring.get(), reinterpret_cast<const char*>(&pkt), sizeof(pkt));
}
}
void Ptp_clock::ptp_worker_general(){
log_msg(LOG_LEVEL_NOTICE, MOD_NAME "Init sock %s on %s port %d\n", PTP_ADDRESS, network_interface.c_str(), PTP_PORT_GENERAL);
auto ptp_sock = socket_udp_uniq(udp_init_if(PTP_ADDRESS, network_interface.c_str(), PTP_PORT_GENERAL, 0, 255, 4, false));
if(!ptp_sock){
log_msg(LOG_LEVEL_ERROR, MOD_NAME "Failed to create sock\n");
exit_uv(-1);
should_run = false;
return;
}
spa_dll_init(&dll);
spa_dll_set_bw(&dll, 0.05, 250'000'000, 1'000'000'000); //TODO
auto last_report = std::chrono::steady_clock::now();
while(should_run){
int buflen = 0;
uint8_t buffer[MAX_PACKET_LEN];
timeval timeout {0, 33'000'000};
buflen = udp_recv_timeout(ptp_sock.get(), (char *)buffer, MAX_PACKET_LEN, &timeout);
auto ring_avail = ring_get_current_size(event_pkt_ring.get());
while(ring_avail >= (int) sizeof(Timestamped_pkt)){
Timestamped_pkt pkt;
ring_buffer_read(event_pkt_ring.get(), reinterpret_cast<char *>(&pkt), sizeof(pkt));
processPtpPkt(pkt.buf, pkt.buflen, pkt.local_ts);
ring_avail -= sizeof(Timestamped_pkt);
}
if(buflen == 0)
continue;
processPtpPkt(buffer, buflen, 0);
if(std::chrono::steady_clock::now() - last_report > std::chrono::seconds(5)){
last_report = std::chrono::steady_clock::now();
double avg_err_us = avg.get() / 1000;
log_msg(LOG_LEVEL_INFO, MOD_NAME "Average absoulute err %.3f usec\n", avg_err_us);
}
}
}
const char* Ptp_clock::get_clock_id_str(){
std::lock_guard<std::mutex> l(mut);
return clock_id_to_str(clock_identity);
}
void Ptp_clock::set_clock_identity(uint64_t id){
std::lock_guard<std::mutex> l(mut);
clock_identity = id;
log_msg(LOG_LEVEL_NOTICE, MOD_NAME "Selecting clock %s\n", clock_id_to_str(clock_identity));
}
bool Ptp_clock::is_locked() const{
std::lock_guard<std::mutex> l(mut);
return locked;
}
void Ptp_clock::start(std::string_view interface_name){
network_interface = interface_name;
constexpr size_t ring_size = sizeof(Timestamped_pkt) * 1000;
event_pkt_ring.reset(ring_buffer_init(ring_size));
worker_general = std::thread(&Ptp_clock::ptp_worker_general, this);
worker_event = std::thread(&Ptp_clock::ptp_worker_event, this);
}
void Ptp_clock::stop(){
should_run = false;
if(worker_event.joinable()){
worker_event.join();
}
if(worker_general.joinable()){
worker_general.join();
}
}