forked from richardcochran/linuxptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
msg.c
634 lines (564 loc) · 13.9 KB
/
msg.c
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/**
* @file msg.c
* @note Copyright (C) 2011 Richard Cochran <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <arpa/inet.h>
#include <errno.h>
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "contain.h"
#include "msg.h"
#include "print.h"
#include "tlv.h"
int assume_two_step = 0;
uint8_t ptp_hdr_ver = PTP_VERSION;
/*
* Head room fits a VLAN Ethernet header, and 'msg' is 64 bit aligned.
*/
#define MSG_HEADROOM 24
struct message_storage {
unsigned char reserved[MSG_HEADROOM];
struct ptp_message msg __attribute__((aligned (8)));
};
static TAILQ_HEAD(msg_pool, ptp_message) msg_pool = TAILQ_HEAD_INITIALIZER(msg_pool);
static struct {
int total;
int count;
} pool_stats;
#ifdef DEBUG_POOL
static void pool_debug(const char *str, void *addr)
{
fprintf(stderr, "*** %p %10s total %d count %d used %d\n",
addr, str, pool_stats.total, pool_stats.count,
pool_stats.total - pool_stats.count);
}
#else
static void pool_debug(const char *str, void *addr)
{
}
#endif
static void announce_pre_send(struct announce_msg *m)
{
m->currentUtcOffset = htons(m->currentUtcOffset);
m->grandmasterClockQuality.offsetScaledLogVariance =
htons(m->grandmasterClockQuality.offsetScaledLogVariance);
m->stepsRemoved = htons(m->stepsRemoved);
}
static void announce_post_recv(struct announce_msg *m)
{
m->currentUtcOffset = ntohs(m->currentUtcOffset);
m->grandmasterClockQuality.offsetScaledLogVariance =
ntohs(m->grandmasterClockQuality.offsetScaledLogVariance);
m->stepsRemoved = ntohs(m->stepsRemoved);
}
static int hdr_post_recv(struct ptp_header *m)
{
if ((m->ver & MAJOR_VERSION_MASK) != PTP_MAJOR_VERSION)
return -EPROTO;
m->messageLength = ntohs(m->messageLength);
m->correction = net2host64(m->correction);
m->sourcePortIdentity.portNumber = ntohs(m->sourcePortIdentity.portNumber);
m->sequenceId = ntohs(m->sequenceId);
return 0;
}
static int hdr_pre_send(struct ptp_header *m)
{
m->messageLength = htons(m->messageLength);
m->correction = host2net64(m->correction);
m->sourcePortIdentity.portNumber = htons(m->sourcePortIdentity.portNumber);
m->sequenceId = htons(m->sequenceId);
return 0;
}
static uint8_t *msg_suffix(struct ptp_message *m)
{
switch (msg_type(m)) {
case SYNC:
return m->sync.suffix;
case DELAY_REQ:
return m->delay_req.suffix;
case PDELAY_REQ:
return m->pdelay_req.suffix;
case PDELAY_RESP:
return m->pdelay_resp.suffix;
case FOLLOW_UP:
return m->follow_up.suffix;
case DELAY_RESP:
return m->delay_resp.suffix;
case PDELAY_RESP_FOLLOW_UP:
return m->pdelay_resp_fup.suffix;
case ANNOUNCE:
return m->announce.suffix;
case SIGNALING:
return m->signaling.suffix;
case MANAGEMENT:
return m->management.suffix;
}
return NULL;
}
static struct tlv_extra *msg_tlv_prepare(struct ptp_message *msg, int length)
{
struct tlv_extra *extra, *tmp;
uint8_t *ptr;
/* Make sure this message type admits appended TLVs. */
ptr = msg_suffix(msg);
if (!ptr) {
pr_err("TLV on %s not allowed", msg_type_string(msg_type(msg)));
return NULL;
}
tmp = TAILQ_LAST(&msg->tlv_list, tlv_list);
if (tmp) {
ptr = (uint8_t *) tmp->tlv;
ptr += sizeof(tmp->tlv->type);
ptr += sizeof(tmp->tlv->length);
ptr += tmp->tlv->length;
}
/* Check that the message buffer has enough room for the new TLV. */
if ((unsigned long)(ptr + length) >
(unsigned long)(&msg->tail_room)) {
pr_debug("cannot fit TLV of length %d into message", length);
return NULL;
}
/* Allocate a TLV descriptor and setup the pointer. */
extra = tlv_extra_alloc();
if (!extra) {
pr_err("failed to allocate TLV descriptor");
return NULL;
}
extra->tlv = (struct TLV *) ptr;
return extra;
}
static void msg_tlv_recycle(struct ptp_message *msg)
{
struct tlv_extra *extra;
while ((extra = TAILQ_FIRST(&msg->tlv_list)) != NULL) {
TAILQ_REMOVE(&msg->tlv_list, extra, list);
tlv_extra_recycle(extra);
}
}
static void port_id_post_recv(struct PortIdentity *pid)
{
pid->portNumber = ntohs(pid->portNumber);
}
static void port_id_pre_send(struct PortIdentity *pid)
{
pid->portNumber = htons(pid->portNumber);
}
static int suffix_post_recv(struct ptp_message *msg, int len)
{
uint8_t *ptr = msg_suffix(msg);
struct tlv_extra *extra;
int err, suffix_len = 0;
if (!ptr)
return 0;
msg_tlv_recycle(msg);
while (len >= sizeof(struct TLV)) {
extra = tlv_extra_alloc();
if (!extra) {
pr_err("failed to allocate TLV descriptor");
return -ENOMEM;
}
extra->tlv = (struct TLV *) ptr;
extra->tlv->type = ntohs(extra->tlv->type);
extra->tlv->length = ntohs(extra->tlv->length);
if (extra->tlv->length % 2) {
tlv_extra_recycle(extra);
return -EBADMSG;
}
suffix_len += sizeof(struct TLV);
len -= sizeof(struct TLV);
ptr += sizeof(struct TLV);
if (extra->tlv->length > len) {
tlv_extra_recycle(extra);
return -EBADMSG;
}
suffix_len += extra->tlv->length;
len -= extra->tlv->length;
ptr += extra->tlv->length;
err = tlv_post_recv(extra);
if (err) {
tlv_extra_recycle(extra);
return err;
}
msg_tlv_attach(msg, extra);
}
return suffix_len;
}
static void suffix_pre_send(struct ptp_message *msg)
{
struct tlv_extra *extra;
struct TLV *tlv;
TAILQ_FOREACH(extra, &msg->tlv_list, list) {
tlv = extra->tlv;
tlv_pre_send(tlv, extra);
tlv->type = htons(tlv->type);
tlv->length = htons(tlv->length);
}
}
static void timestamp_post_recv(struct ptp_message *m, struct Timestamp *ts)
{
uint32_t lsb = ntohl(ts->seconds_lsb);
uint16_t msb = ntohs(ts->seconds_msb);
m->ts.pdu.sec = ((uint64_t)lsb) | (((uint64_t)msb) << 32);
m->ts.pdu.nsec = ntohl(ts->nanoseconds);
}
static void timestamp_pre_send(struct Timestamp *ts)
{
ts->seconds_lsb = htonl(ts->seconds_lsb);
ts->seconds_msb = htons(ts->seconds_msb);
ts->nanoseconds = htonl(ts->nanoseconds);
}
/* public methods */
struct ptp_message *msg_allocate(void)
{
struct message_storage *s;
struct ptp_message *m = TAILQ_FIRST(&msg_pool);
if (m) {
TAILQ_REMOVE(&msg_pool, m, list);
pool_stats.count--;
pool_debug("dequeue", m);
} else {
s = malloc(sizeof(*s));
if (s) {
m = &s->msg;
pool_stats.total++;
pool_debug("allocate", m);
}
}
if (m) {
memset(m, 0, sizeof(*m));
m->refcnt = 1;
TAILQ_INIT(&m->tlv_list);
}
return m;
}
void msg_cleanup(void)
{
struct message_storage *s;
struct ptp_message *m;
tlv_extra_cleanup();
while ((m = TAILQ_FIRST(&msg_pool)) != NULL) {
TAILQ_REMOVE(&msg_pool, m, list);
s = container_of(m, struct message_storage, msg);
free(s);
}
}
struct ptp_message *msg_duplicate(struct ptp_message *msg, int cnt)
{
struct ptp_message *dup;
int err;
dup = msg_allocate();
if (!dup) {
return NULL;
}
memcpy(dup, msg, sizeof(*dup));
dup->refcnt = 1;
TAILQ_INIT(&dup->tlv_list);
if (!cnt) {
return dup;
}
err = msg_post_recv(dup, cnt);
if (err) {
switch (err) {
case -EBADMSG:
pr_err("msg_duplicate: bad message");
break;
case -EPROTO:
pr_debug("msg_duplicate: ignoring message");
break;
}
msg_put(dup);
return NULL;
}
if (msg_sots_missing(msg)) {
pr_err("msg_duplicate: received %s without timestamp",
msg_type_string(msg_type(msg)));
msg_put(dup);
return NULL;
}
return dup;
}
void msg_get(struct ptp_message *m)
{
m->refcnt++;
}
int msg_post_recv(struct ptp_message *m, int cnt)
{
int err, pdulen, suffix_len, type;
if (cnt < sizeof(struct ptp_header))
return -EBADMSG;
err = hdr_post_recv(&m->header);
if (err)
return err;
type = msg_type(m);
switch (type) {
case SYNC:
pdulen = sizeof(struct sync_msg);
break;
case DELAY_REQ:
pdulen = sizeof(struct delay_req_msg);
break;
case PDELAY_REQ:
pdulen = sizeof(struct pdelay_req_msg);
break;
case PDELAY_RESP:
pdulen = sizeof(struct pdelay_resp_msg);
break;
case FOLLOW_UP:
pdulen = sizeof(struct follow_up_msg);
break;
case DELAY_RESP:
pdulen = sizeof(struct delay_resp_msg);
break;
case PDELAY_RESP_FOLLOW_UP:
pdulen = sizeof(struct pdelay_resp_fup_msg);
break;
case ANNOUNCE:
pdulen = sizeof(struct announce_msg);
break;
case SIGNALING:
pdulen = sizeof(struct signaling_msg);
break;
case MANAGEMENT:
pdulen = sizeof(struct management_msg);
break;
default:
return -EBADMSG;
}
if (cnt < pdulen)
return -EBADMSG;
switch (type) {
case SYNC:
timestamp_post_recv(m, &m->sync.originTimestamp);
break;
case DELAY_REQ:
break;
case PDELAY_REQ:
break;
case PDELAY_RESP:
timestamp_post_recv(m, &m->pdelay_resp.requestReceiptTimestamp);
port_id_post_recv(&m->pdelay_resp.requestingPortIdentity);
break;
case FOLLOW_UP:
timestamp_post_recv(m, &m->follow_up.preciseOriginTimestamp);
break;
case DELAY_RESP:
timestamp_post_recv(m, &m->delay_resp.receiveTimestamp);
port_id_post_recv(&m->delay_resp.requestingPortIdentity);
break;
case PDELAY_RESP_FOLLOW_UP:
timestamp_post_recv(m, &m->pdelay_resp_fup.responseOriginTimestamp);
port_id_post_recv(&m->pdelay_resp_fup.requestingPortIdentity);
break;
case ANNOUNCE:
clock_gettime(CLOCK_MONOTONIC, &m->ts.host);
timestamp_post_recv(m, &m->announce.originTimestamp);
announce_post_recv(&m->announce);
break;
case SIGNALING:
port_id_post_recv(&m->signaling.targetPortIdentity);
break;
case MANAGEMENT:
port_id_post_recv(&m->management.targetPortIdentity);
break;
}
suffix_len = suffix_post_recv(m, cnt - pdulen);
if (suffix_len < 0) {
return suffix_len;
}
if (pdulen + suffix_len != m->header.messageLength) {
return -EBADMSG;
}
return 0;
}
int msg_pre_send(struct ptp_message *m)
{
int type;
if (hdr_pre_send(&m->header))
return -1;
type = msg_type(m);
switch (type) {
case SYNC:
break;
case DELAY_REQ:
clock_gettime(CLOCK_MONOTONIC, &m->ts.host);
break;
case PDELAY_REQ:
break;
case PDELAY_RESP:
timestamp_pre_send(&m->pdelay_resp.requestReceiptTimestamp);
port_id_pre_send(&m->pdelay_resp.requestingPortIdentity);
break;
case FOLLOW_UP:
timestamp_pre_send(&m->follow_up.preciseOriginTimestamp);
break;
case DELAY_RESP:
timestamp_pre_send(&m->delay_resp.receiveTimestamp);
m->delay_resp.requestingPortIdentity.portNumber =
htons(m->delay_resp.requestingPortIdentity.portNumber);
break;
case PDELAY_RESP_FOLLOW_UP:
timestamp_pre_send(&m->pdelay_resp_fup.responseOriginTimestamp);
port_id_pre_send(&m->pdelay_resp_fup.requestingPortIdentity);
break;
case ANNOUNCE:
announce_pre_send(&m->announce);
break;
case SIGNALING:
port_id_pre_send(&m->signaling.targetPortIdentity);
break;
case MANAGEMENT:
port_id_pre_send(&m->management.targetPortIdentity);
break;
default:
return -1;
}
suffix_pre_send(m);
return 0;
}
struct tlv_extra *msg_tlv_append(struct ptp_message *msg, int length)
{
struct tlv_extra *extra;
extra = msg_tlv_prepare(msg, length);
if (extra) {
msg->header.messageLength += length;
msg_tlv_attach(msg, extra);
}
return extra;
}
void msg_tlv_attach(struct ptp_message *msg, struct tlv_extra *extra)
{
TAILQ_INSERT_TAIL(&msg->tlv_list, extra, list);
}
int msg_tlv_count(struct ptp_message *msg)
{
int count = 0;
struct tlv_extra *extra;
for (extra = TAILQ_FIRST(&msg->tlv_list);
extra != NULL;
extra = TAILQ_NEXT(extra, list))
count++;
return count;
}
int msg_tlv_copy(struct ptp_message *msg, struct ptp_message *dup) {
struct tlv_extra *extra, *dup_extra;
struct TLV *tlv;
if (msg_type(msg) != msg_type(dup)) {
return -1;
}
if (msg->header.messageLength != ntohs(dup->header.messageLength)) {
return -1;
}
TAILQ_FOREACH(extra, &msg->tlv_list, list) {
tlv = (void *) extra->tlv - (void *) msg + (void *) dup;
dup_extra = tlv_extra_alloc();
dup_extra->tlv = tlv;
msg_tlv_attach(dup, dup_extra);
}
return 0;
}
const char *msg_type_string(int type)
{
switch (type) {
case SYNC:
return "SYNC";
case DELAY_REQ:
return "DELAY_REQ";
case PDELAY_REQ:
return "PDELAY_REQ";
case PDELAY_RESP:
return "PDELAY_RESP";
case FOLLOW_UP:
return "FOLLOW_UP";
case DELAY_RESP:
return "DELAY_RESP";
case PDELAY_RESP_FOLLOW_UP:
return "PDELAY_RESP_FOLLOW_UP";
case ANNOUNCE:
return "ANNOUNCE";
case SIGNALING:
return "SIGNALING";
case MANAGEMENT:
return "MANAGEMENT";
}
return "unknown";
}
void msg_print(struct ptp_message *m, FILE *fp)
{
fprintf(fp,
"\t"
"%-10s "
// "versionPTP 0x%02X "
// "messageLength %hu "
// "domainNumber %u "
// "reserved1 0x%02X "
// "flagField 0x%02X%02X "
// "correction %lld "
// "reserved2 %u "
// "sourcePortIdentity ... "
"sequenceId %4hu "
// "control %u "
// "logMessageInterval %d "
,
msg_type_string(msg_type(m)),
// m->header.ver,
// m->header.messageLength,
// m->header.domainNumber,
// m->header.reserved1,
// m->header.flagField[0],
// m->header.flagField[1],
// m->header.correction,
// m->header.reserved2,
// m->header.sourcePortIdentity,
m->header.sequenceId
// m->header.control,
// m->header.logMessageInterval
);
fprintf(fp, "\n");
}
void msg_put(struct ptp_message *m)
{
m->refcnt--;
if (m->refcnt) {
return;
}
pool_stats.count++;
pool_debug("recycle", m);
msg_tlv_recycle(m);
TAILQ_INSERT_HEAD(&msg_pool, m, list);
}
int msg_sots_missing(struct ptp_message *m)
{
int type = msg_type(m);
switch (type) {
case SYNC:
case DELAY_REQ:
case PDELAY_REQ:
case PDELAY_RESP:
break;
case FOLLOW_UP:
case DELAY_RESP:
case PDELAY_RESP_FOLLOW_UP:
case ANNOUNCE:
case SIGNALING:
case MANAGEMENT:
default:
return 0;
}
return msg_sots_valid(m) ? 0 : 1;
}