forked from microchip-ung/ptp4l
-
Notifications
You must be signed in to change notification settings - Fork 0
/
relay.c
515 lines (463 loc) · 12.3 KB
/
relay.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
/**
* @file relay.c
* @note Copyright (C) 2018 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-1335 USA.
*/
#include <errno.h>
#include "port.h"
#include "port_private.h"
#include "print.h"
#include "rtnl.h"
#include "tc.h"
static int relay_forward(struct port *q, struct ptp_message *msg)
{
uint16_t steps_removed;
struct port *p;
int cnt;
if (msg_type(msg) == ANNOUNCE) {
steps_removed = ntohs(msg->announce.stepsRemoved);
if (steps_removed + 1 <= clock_max_steps_removed(q->clock))
msg->announce.stepsRemoved = htons(1 + steps_removed);
}
for (p = clock_first_port(q->clock); p; p = LIST_NEXT(p, list)) {
if (tc_blocked(q, p, msg)) {
continue;
}
msg->header.sourcePortIdentity.clockIdentity = p->portIdentity.clockIdentity;
msg->header.sourcePortIdentity.portNumber = htons(p->portIdentity.portNumber);
cnt = transport_send(p->trp, &p->fda, TRANS_GENERAL, msg);
if (cnt <= 0) {
pr_err("tc failed to forward message on port %d",
portnum(p));
port_dispatch(p, EV_FAULT_DETECTED, 0);
}
}
return 0;
}
static void relay_complete_syfup(struct port *q, struct port *p,
struct ptp_message *msg, tmv_t residence)
{
enum tc_match type = TC_MISMATCH;
struct ptp_message *dup;
struct ptp_message *fup;
struct tc_txd *txd;
Integer64 c1, c2;
int cnt;
TAILQ_FOREACH(txd, &p->tc_transmitted, list) {
type = tc_match_syfup(portnum(q), msg, txd);
switch (type) {
case TC_MISMATCH:
break;
case TC_SYNC_FUP:
fup = msg;
residence = txd->residence;
break;
case TC_FUP_SYNC:
fup = txd->msg;
break;
case TC_DELAY_REQRESP:
pr_err("tc: unexpected match of delay request - sync!");
return;
}
if (type != TC_MISMATCH) {
break;
}
}
if (type == TC_MISMATCH) {
txd = tc_allocate();
if (!txd) {
port_dispatch(p, EV_FAULT_DETECTED, 0);
return;
}
msg_get(msg);
txd->msg = msg;
txd->residence = residence;
txd->ingress_port = portnum(q);
TAILQ_INSERT_TAIL(&p->tc_transmitted, txd, list);
return;
}
c1 = net2host64(fup->header.correction);
c2 = c1 + tmv_to_TimeInterval(residence);
c2 += tmv_to_TimeInterval(q->peer_delay);
c2 += q->asymmetry;
fup->header.correction = host2net64(c2);
dup = msg_allocate();
if (!dup)
return;
memcpy(dup, fup, sizeof(*dup));
dup->refcnt = 1;
dup->header.sourcePortIdentity.clockIdentity = p->portIdentity.clockIdentity;
dup->header.sourcePortIdentity.portNumber = htons(p->portIdentity.portNumber);
if (p->follow_up_info)
clock_follow_up_info_update(p->clock, dup);
cnt = transport_send(p->trp, &p->fda, TRANS_GENERAL, dup);
if (cnt <= 0) {
pr_err("tc failed to forward follow up on port %d", portnum(p));
port_dispatch(p, EV_FAULT_DETECTED, 0);
}
/* Restore original correction value for next egress port. */
fup->header.correction = host2net64(c1);
TAILQ_REMOVE(&p->tc_transmitted, txd, list);
msg_put(txd->msg);
msg_put(dup);
tc_recycle(txd);
}
static void relay_complete(struct port *q, struct port *p,
struct ptp_message *msg, tmv_t residence)
{
switch (msg_type(msg)) {
case SYNC:
case FOLLOW_UP:
relay_complete_syfup(q, p, msg, residence);
break;
}
}
static int relay_fwd_folup(struct port *q, struct ptp_message *msg)
{
struct port *p;
clock_gettime(CLOCK_MONOTONIC, &msg->ts.host);
for (p = clock_first_port(q->clock); p; p = LIST_NEXT(p, list)) {
if (tc_blocked(q, p, msg)) {
continue;
}
relay_complete(q, p, msg, tmv_zero());
}
return 0;
}
static int relay_fwd_event(struct port *q, struct ptp_message *msg)
{
tmv_t egress, ingress = msg->hwts.ts, residence;
struct ptp_message *dup;
struct port *p;
int cnt, err;
double rr;
clock_gettime(CLOCK_MONOTONIC, &msg->ts.host);
dup = msg_allocate();
if (!dup)
return 1;
memcpy(dup, msg, sizeof(*dup));
dup->refcnt = 1;
/* First send the event message out. */
for (p = clock_first_port(q->clock); p; p = LIST_NEXT(p, list)) {
if (tc_blocked(q, p, msg)) {
continue;
}
dup->header.sourcePortIdentity.clockIdentity = p->portIdentity.clockIdentity;
dup->header.sourcePortIdentity.portNumber = htons(p->portIdentity.portNumber);
cnt = transport_send(p->trp, &p->fda, TRANS_DEFER_EVENT, dup);
if (cnt <= 0) {
pr_err("failed to forward event from port %hd to %hd",
portnum(q), portnum(p));
port_dispatch(p, EV_FAULT_DETECTED, 0);
}
}
msg_put(dup);
/* Go back and gather the transmit time stamps. */
for (p = clock_first_port(q->clock); p; p = LIST_NEXT(p, list)) {
if (tc_blocked(q, p, msg)) {
continue;
}
err = transport_txts(&p->fda, msg);
if (err || !msg_sots_valid(msg)) {
pr_err("failed to fetch txts on port %hd to %hd event",
portnum(q), portnum(p));
port_dispatch(p, EV_FAULT_DETECTED, 0);
continue;
}
ts_add(&msg->hwts.ts, p->tx_timestamp_offset);
egress = msg->hwts.ts;
residence = tmv_sub(egress, ingress);
residence = tmv_add(residence, clock_get_path_delay(q->clock));
rr = clock_rate_ratio(q->clock);
if (rr != 1.0) {
residence = dbl_tmv(tmv_dbl(residence) * rr);
}
relay_complete(q, p, msg, residence);
}
return 0;
}
static int relay_delay_request(struct port *p)
{
switch (p->state) {
case PS_INITIALIZING:
case PS_FAULTY:
case PS_DISABLED:
return 0;
case PS_LISTENING:
case PS_PRE_MASTER:
case PS_MASTER:
case PS_PASSIVE:
case PS_UNCALIBRATED:
case PS_SLAVE:
case PS_GRAND_MASTER:
break;
}
return port_delay_request(p);
}
void relay_dispatch(struct port *p, enum fsm_event event, int mdiff)
{
if (!port_state_update(p, event, mdiff)) {
if (clock_best_local(p->clock)) {
if (event == EV_RS_GRAND_MASTER) {
set_tmo_log(p->fda.fd[FD_MANNO_TIMER], 1, -10); /*~1ms*/
port_set_sync_tx_tmo(p);
}
return;
}
switch (p->state) {
case PS_PRE_MASTER:
port_clr_tmo(p->fda.fd[FD_ANNOUNCE_TIMER]);
port_clr_tmo(p->fda.fd[FD_SYNC_RX_TIMER]);
/* Leave FD_DELAY_TIMER running. */
port_clr_tmo(p->fda.fd[FD_MANNO_TIMER]);
port_clr_tmo(p->fda.fd[FD_SYNC_TX_TIMER]);
break;
case PS_MASTER:
case PS_GRAND_MASTER:
port_clr_tmo(p->fda.fd[FD_ANNOUNCE_TIMER]);
port_clr_tmo(p->fda.fd[FD_SYNC_RX_TIMER]);
/* Leave FD_DELAY_TIMER running. */
port_clr_tmo(p->fda.fd[FD_QUALIFICATION_TIMER]);
port_clr_tmo(p->fda.fd[FD_MANNO_TIMER]);
port_clr_tmo(p->fda.fd[FD_SYNC_TX_TIMER]);
break;
default:
;
};
return;
}
if (!portnum(p)) {
/* UDS needs no timers. */
return;
}
port_clr_tmo(p->fda.fd[FD_ANNOUNCE_TIMER]);
port_clr_tmo(p->fda.fd[FD_SYNC_RX_TIMER]);
/* Leave FD_DELAY_TIMER running. */
port_clr_tmo(p->fda.fd[FD_QUALIFICATION_TIMER]);
port_clr_tmo(p->fda.fd[FD_MANNO_TIMER]);
port_clr_tmo(p->fda.fd[FD_SYNC_TX_TIMER]);
/*
* Handle the side effects of the state transition.
*/
switch (p->state) {
case PS_INITIALIZING:
break;
case PS_FAULTY:
case PS_DISABLED:
port_disable(p);
break;
case PS_LISTENING:
port_set_announce_tmo(p);
port_set_delay_tmo(p);
break;
case PS_PRE_MASTER:
port_set_qualification_tmo(p);
break;
case PS_MASTER:
case PS_GRAND_MASTER:
set_tmo_log(p->fda.fd[FD_MANNO_TIMER], 1, -10); /*~1ms*/
port_set_sync_tx_tmo(p);
break;
case PS_PASSIVE:
port_set_announce_tmo(p);
break;
case PS_UNCALIBRATED:
case PS_SLAVE:
port_set_announce_tmo(p);
break;
};
}
static int path_trace_insert(struct port *p, struct ptp_message *m)
{
struct path_trace_tlv *ptt;
uint16_t length;
uint8_t *ptr;
ptr = m->announce.suffix;
if (!ptr) {
pr_err("TLV on %s not allowed", msg_type_string(msg_type(m)));
return -1;
}
/* Check that the message buffer has enough room for the new TLV. */
if ((unsigned long)(ptr + sizeof(struct ClockIdentity)) >
(unsigned long)(&m->tail_room)) {
pr_debug("cannot fit TLV of length into message");
return -1;
}
ptt = (struct path_trace_tlv *) ptr;
if (ntohs(m->header.messageLength) +
sizeof(struct ClockIdentity) > 1500) {
uint16_t ptt_len = ntohs(ptt->length);
length = ntohs(m->header.messageLength);
length -= ptt_len;
length -= 4;
m->header.messageLength = htons(length);
return -1;
}
length = ntohs(ptt->length);
length += sizeof(struct ClockIdentity);
ptt->length = htons(length);
ptt->cid[length / sizeof(struct ClockIdentity) - 1] =
clock_identity(p->clock);
length = ntohs(m->header.messageLength);
length += sizeof(struct ClockIdentity);
m->header.messageLength = htons(length);
return 0;
}
enum fsm_event relay_event(struct port *p, int fd_index)
{
int cnt, fd = p->fda.fd[fd_index];
enum fsm_event event = EV_NONE;
struct ptp_message *msg, *dup;
int err = 0;
switch (fd_index) {
case FD_ANNOUNCE_TIMER:
case FD_SYNC_RX_TIMER:
pr_debug("port %hu: %s timeout", portnum(p),
fd_index == FD_SYNC_RX_TIMER ? "rx sync" : "announce");
if (p->best) {
fc_clear(p->best);
}
port_set_announce_tmo(p);
return EV_ANNOUNCE_RECEIPT_TIMEOUT_EXPIRES;
case FD_DELAY_TIMER:
pr_debug("port %hu: delay timeout", portnum(p));
port_set_delay_tmo(p);
tc_prune(p);
return relay_delay_request(p) ? EV_FAULT_DETECTED : EV_NONE;
case FD_QUALIFICATION_TIMER:
pr_debug("port %hu: qualification timeout", portnum(p));
return EV_QUALIFICATION_TIMEOUT_EXPIRES;
case FD_MANNO_TIMER:
if (clock_best_local(p->clock)) {
pr_debug("port %hu: master tx announce timeout", portnum(p));
port_set_manno_tmo(p);
return port_tx_announce(p, NULL) ? EV_FAULT_DETECTED : EV_NONE;
}
return EV_NONE;
case FD_SYNC_TX_TIMER:
if (clock_best_local(p->clock)) {
pr_debug("port %hu: master sync timeout", portnum(p));
port_set_sync_tx_tmo(p);
return port_tx_sync(p, NULL) ? EV_FAULT_DETECTED : EV_NONE;
}
break;
case FD_UNICAST_REQ_TIMER:
case FD_UNICAST_SRV_TIMER:
pr_err("unexpected timer expiration");
return EV_NONE;
case FD_RTNL:
pr_debug("port %hu: received link status notification", portnum(p));
rtnl_link_status(fd, p->name, port_link_status, p);
if (p->link_status == (LINK_UP|LINK_STATE_CHANGED)) {
return EV_FAULT_CLEARED;
} else if ((p->link_status == (LINK_DOWN|LINK_STATE_CHANGED)) ||
(p->link_status & TS_LABEL_CHANGED)) {
return EV_FAULT_DETECTED;
} else {
return EV_NONE;
}
}
msg = msg_allocate();
if (!msg) {
return EV_FAULT_DETECTED;
}
msg->hwts.type = p->timestamping;
cnt = transport_recv(p->trp, fd, msg);
if (cnt <= 0) {
pr_err("port %hu: recv message failed", portnum(p));
msg_put(msg);
return EV_FAULT_DETECTED;
}
if (msg_sots_valid(msg)) {
ts_add(&msg->hwts.ts, -p->rx_timestamp_offset);
}
if (msg_unicast(msg)) {
pl_warning(600, "cannot switch unicast messages!");
msg_put(msg);
return EV_NONE;
}
dup = msg_duplicate(msg, cnt);
if (!dup) {
msg_put(msg);
return EV_NONE;
}
if (tc_ignore(p, dup)) {
msg_put(dup);
dup = NULL;
}
switch (msg_type(msg)) {
case SYNC:
if (dup) {
err = process_sync(p, dup);
}
if (!err && relay_fwd_event(p, msg)) {
event = EV_FAULT_DETECTED;
break;
}
break;
case DELAY_REQ:
break;
case PDELAY_REQ:
if (dup && process_pdelay_req(p, dup)) {
event = EV_FAULT_DETECTED;
}
break;
case PDELAY_RESP:
if (dup && process_pdelay_resp(p, dup)) {
event = EV_FAULT_DETECTED;
}
break;
case FOLLOW_UP:
if (dup) {
err = process_follow_up(p, dup);
}
if (!err && relay_fwd_folup(p, msg)) {
event = EV_FAULT_DETECTED;
break;
}
break;
case DELAY_RESP:
break;
case PDELAY_RESP_FOLLOW_UP:
if (dup) {
process_pdelay_resp_fup(p, dup);
}
break;
case ANNOUNCE:
if (p->path_trace_enabled && path_trace_insert(p, msg)) {
pr_err("port %hu: append path trace failed", portnum(p));
}
if (relay_forward(p, msg)) {
event = EV_FAULT_DETECTED;
break;
}
if (dup && process_announce(p, dup)) {
event = EV_STATE_DECISION_EVENT;
}
break;
case SIGNALING:
case MANAGEMENT:
if (relay_forward(p, msg)) {
event = EV_FAULT_DETECTED;
}
break;
}
msg_put(msg);
if (dup) {
msg_put(dup);
}
return event;
}