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
|
/**
* @file tc.c
* @note Copyright (C) 2018 Richard Cochran <richardcochran@gmail.com>
*
* 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 <stdlib.h>
#include "port.h"
#include "print.h"
#include "tc.h"
#include "tmv.h"
enum tc_match {
TC_MISMATCH,
TC_SYNC_FUP,
TC_FUP_SYNC,
TC_DELAY_REQRESP,
};
static TAILQ_HEAD(tc_pool, tc_txd) tc_pool = TAILQ_HEAD_INITIALIZER(tc_pool);
static int tc_match_delay(int ingress_port, struct ptp_message *resp,
struct tc_txd *txd);
static int tc_match_syfup(int ingress_port, struct ptp_message *msg,
struct tc_txd *txd);
static void tc_recycle(struct tc_txd *txd);
static struct tc_txd *tc_allocate(void)
{
struct tc_txd *txd = TAILQ_FIRST(&tc_pool);
if (txd) {
TAILQ_REMOVE(&tc_pool, txd, list);
memset(txd, 0, sizeof(*txd));
return txd;
}
txd = calloc(1, sizeof(*txd));
return txd;
}
static int tc_blocked(struct port *q, struct port *p, struct ptp_message *m)
{
enum port_state s;
if (q == p) {
return 1;
}
if (portnum(p) == 0) {
return 1;
}
if (!q->tc_spanning_tree) {
return 0;
}
/* Forward frames in the wrong domain unconditionally. */
if (m->header.domainNumber != clock_domain_number(p->clock)) {
return 0;
}
/* Ingress state */
s = port_state(q);
switch (s) {
case PS_INITIALIZING:
case PS_FAULTY:
case PS_DISABLED:
case PS_LISTENING:
case PS_PRE_MASTER:
case PS_PASSIVE:
return 1;
case PS_MASTER:
case PS_GRAND_MASTER:
/* Delay_Req swims against the stream. */
if (msg_type(m) != DELAY_REQ) {
return 1;
}
break;
case PS_UNCALIBRATED:
case PS_SLAVE:
break;
}
/* Egress state */
s = port_state(p);
switch (s) {
case PS_INITIALIZING:
case PS_FAULTY:
case PS_DISABLED:
case PS_LISTENING:
case PS_PRE_MASTER:
case PS_PASSIVE:
return 1;
case PS_UNCALIBRATED:
case PS_SLAVE:
/* Delay_Req swims against the stream. */
if (msg_type(m) != DELAY_REQ) {
return 1;
}
break;
case PS_MASTER:
case PS_GRAND_MASTER:
/* No use forwarding Delay_Req out the wrong port. */
if (msg_type(m) == DELAY_REQ) {
return 1;
}
break;
}
return 0;
}
static void tc_complete_request(struct port *q, struct port *p,
struct ptp_message *req, tmv_t residence)
{
struct tc_txd *txd = tc_allocate();
if (!txd) {
port_dispatch(p, EV_FAULT_DETECTED, 0);
return;
}
#ifdef DEBUG
pr_err("stash delay request from %s to %s seqid %hu residence %lu",
q->log_name, p->log_name, ntohs(req->header.sequenceId),
(unsigned long) tmv_to_nanoseconds(residence));
#endif
msg_get(req);
txd->msg = req;
txd->residence = residence;
txd->ingress_port = portnum(q);
TAILQ_INSERT_TAIL(&p->tc_transmitted, txd, list);
}
static void tc_complete_response(struct port *q, struct port *p,
struct ptp_message *resp, tmv_t residence)
{
enum tc_match type = TC_MISMATCH;
struct tc_txd *txd;
Integer64 c1, c2;
int cnt;
#ifdef DEBUG
pr_err("complete delay response from %s to %s seqid %hu",
q->log_name, p->log_name, ntohs(resp->header.sequenceId));
#endif
TAILQ_FOREACH(txd, &q->tc_transmitted, list) {
type = tc_match_delay(portnum(p), resp, txd);
if (type == TC_DELAY_REQRESP) {
residence = txd->residence;
break;
}
}
if (type != TC_DELAY_REQRESP) {
return;
}
c1 = net2host64(resp->header.correction);
c2 = c1 + tmv_to_TimeInterval(residence);
resp->header.correction = host2net64(c2);
cnt = transport_send(p->trp, &p->fda, TRANS_GENERAL, resp);
if (cnt <= 0) {
pr_err("tc failed to forward response on %s", p->log_name);
port_dispatch(p, EV_FAULT_DETECTED, 0);
}
/* Restore original correction value for next egress port. */
resp->header.correction = host2net64(c1);
TAILQ_REMOVE(&q->tc_transmitted, txd, list);
msg_put(txd->msg);
tc_recycle(txd);
}
static void tc_complete_syfup(struct port *q, struct port *p,
struct ptp_message *msg, tmv_t residence)
{
enum tc_match type = TC_MISMATCH;
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);
cnt = transport_send(p->trp, &p->fda, TRANS_GENERAL, fup);
if (cnt <= 0) {
pr_err("tc failed to forward follow up on %s", p->log_name);
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);
tc_recycle(txd);
}
static void tc_complete(struct port *q, struct port *p,
struct ptp_message *msg, tmv_t residence)
{
switch (msg_type(msg)) {
case SYNC:
case FOLLOW_UP:
tc_complete_syfup(q, p, msg, residence);
break;
case DELAY_REQ:
tc_complete_request(q, p, msg, residence);
break;
case DELAY_RESP:
tc_complete_response(q, p, msg, residence);
break;
}
}
static int tc_current(struct ptp_message *m, struct timespec now)
{
int64_t t1, t2;
t1 = m->ts.host.tv_sec * NSEC_PER_SEC + m->ts.host.tv_nsec;
t2 = now.tv_sec * NSEC_PER_SEC + now.tv_nsec;
return t2 - t1 < NSEC_PER_SEC;
}
static int tc_fwd_event(struct port *q, struct ptp_message *msg)
{
tmv_t egress, ingress = msg->hwts.ts, residence;
struct port *p;
int cnt, err;
double rr;
clock_gettime(CLOCK_MONOTONIC, &msg->ts.host);
/* 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;
}
cnt = transport_send(p->trp, &p->fda, TRANS_DEFER_EVENT, msg);
if (cnt <= 0) {
pr_err("failed to forward event from %s to %s",
q->log_name, p->log_name);
port_dispatch(p, EV_FAULT_DETECTED, 0);
}
}
/* 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 %s to %s event",
q->log_name, p->log_name);
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);
rr = clock_rate_ratio(q->clock);
if (rr != 1.0) {
residence = dbl_tmv(tmv_dbl(residence) * rr);
}
tc_complete(q, p, msg, residence);
}
return 0;
}
static int tc_match_delay(int ingress_port, struct ptp_message *resp,
struct tc_txd *txd)
{
struct ptp_message *req = txd->msg;
if (ingress_port != txd->ingress_port) {
return TC_MISMATCH;
}
if (req->header.sequenceId != resp->header.sequenceId) {
return TC_MISMATCH;
}
if (!pid_eq(&req->header.sourcePortIdentity,
&resp->delay_resp.requestingPortIdentity)) {
return TC_MISMATCH;
}
if (msg_type(req) == DELAY_REQ && msg_type(resp) == DELAY_RESP) {
return TC_DELAY_REQRESP;
}
return TC_MISMATCH;
}
static int tc_match_syfup(int ingress_port, struct ptp_message *msg,
struct tc_txd *txd)
{
if (ingress_port != txd->ingress_port) {
return TC_MISMATCH;
}
if (msg->header.sequenceId != txd->msg->header.sequenceId) {
return TC_MISMATCH;
}
if (!source_pid_eq(msg, txd->msg)) {
return TC_MISMATCH;
}
if (msg_type(txd->msg) == SYNC && msg_type(msg) == FOLLOW_UP) {
return TC_SYNC_FUP;
}
if (msg_type(txd->msg) == FOLLOW_UP && msg_type(msg) == SYNC) {
return TC_FUP_SYNC;
}
return TC_MISMATCH;
}
static void tc_recycle(struct tc_txd *txd)
{
TAILQ_INSERT_HEAD(&tc_pool, txd, list);
}
/* public methods */
void tc_cleanup(void)
{
struct tc_txd *txd;
while ((txd = TAILQ_FIRST(&tc_pool)) != NULL) {
TAILQ_REMOVE(&tc_pool, txd, list);
free(txd);
}
}
void tc_flush(struct port *q)
{
struct tc_txd *txd;
while ((txd = TAILQ_FIRST(&q->tc_transmitted)) != NULL) {
TAILQ_REMOVE(&q->tc_transmitted, txd, list);
msg_put(txd->msg);
tc_recycle(txd);
}
}
int tc_forward(struct port *q, struct ptp_message *msg)
{
uint16_t steps_removed;
struct port *p;
int cnt;
if (q->tc_spanning_tree && msg_type(msg) == ANNOUNCE) {
steps_removed = ntohs(msg->announce.stepsRemoved);
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;
}
cnt = transport_send(p->trp, &p->fda, TRANS_GENERAL, msg);
if (cnt <= 0) {
pr_err("tc failed to forward message on %s",
p->log_name);
port_dispatch(p, EV_FAULT_DETECTED, 0);
}
}
return 0;
}
int tc_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;
}
tc_complete(q, p, msg, tmv_zero());
}
return 0;
}
int tc_fwd_request(struct port *q, struct ptp_message *msg)
{
return tc_fwd_event(q, msg);
}
int tc_fwd_response(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;
}
tc_complete(q, p, msg, tmv_zero());
}
return 0;
}
int tc_fwd_sync(struct port *q, struct ptp_message *msg)
{
struct ptp_message *fup = NULL;
int err;
if (one_step(msg)) {
fup = msg_allocate();
if (!fup) {
return -1;
}
fup->header.tsmt = FOLLOW_UP | (msg->header.tsmt & 0xf0);
fup->header.ver = msg->header.ver;
fup->header.messageLength = htons(sizeof(struct follow_up_msg));
fup->header.domainNumber = msg->header.domainNumber;
fup->header.sourcePortIdentity = msg->header.sourcePortIdentity;
fup->header.sequenceId = msg->header.sequenceId;
fup->header.logMessageInterval = msg->header.logMessageInterval;
fup->follow_up.preciseOriginTimestamp = msg->sync.originTimestamp;
msg->header.flagField[0] |= TWO_STEP;
}
err = tc_fwd_event(q, msg);
if (err) {
return err;
}
if (fup) {
err = tc_fwd_folup(q, fup);
msg_put(fup);
}
return err;
}
int tc_ignore(struct port *p, struct ptp_message *m)
{
struct ClockIdentity c1, c2;
if (p->match_transport_specific &&
msg_transport_specific(m) != p->transportSpecific) {
return 1;
}
if (pid_eq(&m->header.sourcePortIdentity, &p->portIdentity)) {
return 1;
}
if (m->header.domainNumber != clock_domain_number(p->clock)) {
return 1;
}
c1 = clock_identity(p->clock);
c2 = m->header.sourcePortIdentity.clockIdentity;
if (cid_eq(&c1, &c2)) {
return 1;
}
return 0;
}
void tc_prune(struct port *q)
{
struct timespec now;
struct tc_txd *txd;
clock_gettime(CLOCK_MONOTONIC, &now);
while ((txd = TAILQ_FIRST(&q->tc_transmitted)) != NULL) {
if (tc_current(txd->msg, now)) {
break;
}
TAILQ_REMOVE(&q->tc_transmitted, txd, list);
msg_put(txd->msg);
tc_recycle(txd);
}
}
|