-
Notifications
You must be signed in to change notification settings - Fork 2
/
nsm.c
666 lines (606 loc) · 15.7 KB
/
nsm.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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
/**
* @file nsm.c
* @brief NSM client program
* @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-1301 USA.
*/
#include <errno.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include <arpa/inet.h>
#include "config.h"
#include "print.h"
#include "rtnl.h"
#include "sad.h"
#include "util.h"
#include "version.h"
#define IFMT "\n\t\t"
#define NSM_NFD 3
struct interface {
STAILQ_ENTRY(interface) list;
};
struct nsm {
struct config *cfg;
struct fdarray fda;
struct transport *trp;
struct tsproc *tsproc;
struct ptp_message *nsm_delay_req;
struct ptp_message *nsm_delay_resp;
struct ptp_message *nsm_sync;
struct ptp_message *nsm_fup;
struct PortIdentity port_identity;
UInteger16 sequence_id;
const char *name;
int spp;
UInteger32 active_key_id;
} the_nsm;
static void nsm_help(FILE *fp);
static int nsm_request(struct nsm *nsm, char *target);
static void nsm_reset(struct nsm *nsm);
static int nsm_command(struct nsm *nsm, const char *cmd)
{
char action_str[10+1] = {0}, id_str[64+1] = {0};
if (0 == strncasecmp(cmd, "HELP", strlen(cmd))) {
nsm_help(stdout);
return 0;
}
if (2 != sscanf(cmd, " %10s %64s", action_str, id_str)) {
pr_err("bad command: %s", cmd);
return -1;
}
if (0 == strncasecmp(action_str, "NSM", strlen(action_str))) {
return nsm_request(nsm, id_str);
}
pr_err("bad command: %s", cmd);
return -1;
}
static int nsm_complete(struct nsm *nsm)
{
if (!nsm->nsm_sync) {
return 0;
}
if (one_step(nsm->nsm_sync)) {
return nsm->nsm_delay_resp ? 1 : 0;
}
return (nsm->nsm_delay_resp && nsm->nsm_fup) ? 1 : 0;
}
static int64_t nsm_compute_offset(struct tsproc *tsp,
struct ptp_message *syn,
struct ptp_message *fup,
struct ptp_message *req,
struct ptp_message *resp)
{
tmv_t c1, c2, c3, t1, t1c, t2, t3, t4, t4c, offset;
c1 = correction_to_tmv(syn->header.correction);
c2 = correction_to_tmv(fup->header.correction);
c3 = correction_to_tmv(resp->header.correction);
t1 = timestamp_to_tmv(fup->ts.pdu);
t2 = syn->hwts.ts;
t3 = req->hwts.ts;
t4 = timestamp_to_tmv(resp->ts.pdu);
t1c = tmv_add(t1, tmv_add(c1, c2));
t4c = tmv_sub(t4, c3);
tsproc_reset(tsp, 1);
tsproc_down_ts(tsp, t1c, t2);
tsproc_up_ts(tsp, t3, t4c);
tsproc_update_offset(tsp, &offset, NULL);
return tmv_to_nanoseconds(offset);
}
static void nsm_close(struct nsm *nsm)
{
nsm_reset(nsm);
transport_close(nsm->trp, &nsm->fda);
transport_destroy(nsm->trp);
tsproc_destroy(nsm->tsproc);
}
static void nsm_handle_msg(struct nsm *nsm, struct ptp_message *msg, FILE *fp)
{
struct nsm_resp_tlv_head *head;
struct nsm_resp_tlv_foot *foot;
struct timePropertiesDS *tp;
struct PortAddress *paddr;
struct currentDS cds;
struct parentDS *pds;
struct Timestamp ts;
unsigned char *ptr;
int64_t offset;
if (!nsm->nsm_delay_req) {
return;
}
if (msg->header.sequenceId !=
ntohs(nsm->nsm_delay_req->header.sequenceId)) {
return;
}
if (!msg_unicast(msg)) {
return;
}
switch (msg_type(msg)) {
case SYNC:
if (!nsm->nsm_sync) {
nsm->nsm_sync = msg;
msg_get(msg);
}
break;
case FOLLOW_UP:
if (!nsm->nsm_fup) {
nsm->nsm_fup = msg;
msg_get(msg);
}
break;
case DELAY_RESP:
if (!nsm->nsm_delay_resp) {
nsm->nsm_delay_resp = msg;
msg_get(msg);
}
break;
case DELAY_REQ:
case PDELAY_REQ:
case PDELAY_RESP:
case PDELAY_RESP_FOLLOW_UP:
case ANNOUNCE:
case SIGNALING:
case MANAGEMENT:
return;
}
if (!nsm_complete(nsm)) {
return;
}
head = (struct nsm_resp_tlv_head *) nsm->nsm_delay_resp->delay_resp.suffix;
paddr = &head->parent_addr;
ptr = (unsigned char *) head;
ptr += sizeof(*head) + paddr->addressLength;
foot = (struct nsm_resp_tlv_foot *) ptr;
pds = &foot->parent;
memcpy(&cds, &foot->current, sizeof(cds));
tp = &foot->timeprop;
memcpy(&ts, &foot->lastsync, sizeof(ts));
offset = nsm_compute_offset(nsm->tsproc, nsm->nsm_sync, nsm->nsm_fup,
nsm->nsm_delay_req, nsm->nsm_delay_resp);
fprintf(fp, "NSM MEASUREMENT COMPLETE"
IFMT "offset %" PRId64
IFMT "portState %s"
IFMT "parentPortAddress %hu %s\n",
offset,
ps_str[head->port_state],
head->parent_addr.networkProtocol,
portaddr2str(&head->parent_addr));
fprintf(fp, "\tparentDataset"
IFMT "parentPortIdentity %s"
IFMT "parentStats %hhu"
IFMT "observedParentOffsetScaledLogVariance 0x%04hx"
IFMT "observedParentClockPhaseChangeRate 0x%08x"
IFMT "grandmasterPriority1 %hhu"
IFMT "gm.ClockClass %hhu"
IFMT "gm.ClockAccuracy 0x%02hhx"
IFMT "gm.OffsetScaledLogVariance 0x%04hx"
IFMT "grandmasterPriority2 %hhu"
IFMT "grandmasterIdentity %s\n",
pid2str(&pds->parentPortIdentity),
pds->parentStats,
pds->observedParentOffsetScaledLogVariance,
pds->observedParentClockPhaseChangeRate,
pds->grandmasterPriority1,
pds->grandmasterClockQuality.clockClass,
pds->grandmasterClockQuality.clockAccuracy,
pds->grandmasterClockQuality.offsetScaledLogVariance,
pds->grandmasterPriority2,
cid2str(&pds->grandmasterIdentity));
fprintf(fp, "\tcurrentDataset"
IFMT "stepsRemoved %hd"
IFMT "offsetFromMaster %.1f"
IFMT "meanPathDelay %.1f\n",
cds.stepsRemoved, cds.offsetFromMaster / 65536.0,
cds.meanPathDelay / 65536.0);
fprintf(fp, "\ttimePropertiesDataset"
IFMT "currentUtcOffset %hd"
IFMT "leap61 %d"
IFMT "leap59 %d"
IFMT "currentUtcOffsetValid %d"
IFMT "ptpTimescale %d"
IFMT "timeTraceable %d"
IFMT "frequencyTraceable %d"
IFMT "timeSource 0x%02hhx\n",
tp->currentUtcOffset,
tp->flags & LEAP_61 ? 1 : 0,
tp->flags & LEAP_59 ? 1 : 0,
tp->flags & UTC_OFF_VALID ? 1 : 0,
tp->flags & PTP_TIMESCALE ? 1 : 0,
tp->flags & TIME_TRACEABLE ? 1 : 0,
tp->flags & FREQ_TRACEABLE ? 1 : 0,
tp->timeSource);
fprintf(fp, "\tlastSyncTimestamp %" PRId64 ".%09u\n",
((uint64_t)ts.seconds_lsb) | (((uint64_t)ts.seconds_msb) << 32),
ts.nanoseconds);
fflush(fp);
nsm_reset(nsm);
}
static void nsm_help(FILE *fp)
{
fprintf(fp, "\tSend a NetSync Monitor request to a specific port address:\n");
fprintf(fp, "\n");
fprintf(fp, "\tNSM 111.222.333.444\n");
fprintf(fp, "\tNSM aa:bb:cc:dd:ee:ff\n");
fprintf(fp, "\n");
}
static int nsm_open(struct nsm *nsm, struct config *cfg)
{
enum transport_type transport;
char ts_label[IF_NAMESIZE];
const char *ifname, *name;
struct interface *iface;
int count = 0;
STAILQ_FOREACH(iface, &cfg->interfaces, list) {
ifname = interface_name(iface);
memset(ts_label, 0, sizeof(ts_label));
if (!rtnl_get_ts_device(ifname, ts_label))
interface_set_label(iface, ts_label);
count++;
}
if (count != 1) {
pr_err("need exactly one interface");
return -1;
}
iface = STAILQ_FIRST(&cfg->interfaces);
nsm->name = name = interface_name(iface);
nsm->cfg = cfg;
nsm->spp = config_get_int(cfg, name, "spp");
nsm->active_key_id = config_get_uint(cfg, name, "active_key_id");
if (sad_readiness_check(nsm->spp, nsm->active_key_id, nsm->cfg)) {
return -1;
}
transport = config_get_int(cfg, name, "network_transport");
if (generate_clock_identity(&nsm->port_identity.clockIdentity, name)) {
pr_err("failed to generate a clock identity");
return -1;
}
nsm->port_identity.portNumber = 1;
nsm->tsproc = tsproc_create(TSPROC_RAW, FILTER_MOVING_AVERAGE, 10);
if (!nsm->tsproc) {
pr_err("failed to create time stamp processor");
goto no_tsproc;
}
nsm->trp = transport_create(cfg, transport);
if (!nsm->trp) {
pr_err("failed to create transport");
goto no_trans;
}
if (transport_open(nsm->trp, iface, &nsm->fda,
config_get_int(cfg, NULL, "time_stamping"))) {
pr_err("failed to open transport");
goto open_failed;
}
return 0;
open_failed:
transport_destroy(nsm->trp);
no_trans:
tsproc_destroy(nsm->tsproc);
no_tsproc:
return -1;
}
static struct ptp_message *nsm_recv(struct nsm *nsm, int fd)
{
struct ptp_message *msg, *dup = NULL;
int cnt, err;
msg = msg_allocate();
if (!msg) {
pr_err("low memory");
return NULL;
}
msg->hwts.type = config_get_int(nsm->cfg, NULL, "time_stamping");
cnt = transport_recv(nsm->trp, fd, msg);
if (cnt <= 0) {
pr_err("recv message failed");
goto failed;
}
if (nsm->spp >= 0) {
dup = msg_duplicate(msg, 0);
if (!dup) {
goto failed;
}
}
err = msg_post_recv(msg, cnt);
if (err) {
switch (err) {
case -EBADMSG:
pr_err("bad message");
break;
case -EPROTO:
pr_debug("ignoring message");
break;
}
goto failed;
}
if (msg_sots_missing(msg)) {
pr_err("received %s without timestamp",
msg_type_string(msg_type(msg)));
goto failed;
}
err = sad_process_auth(nsm->cfg, nsm->spp, msg, dup);
if (err) {
switch (err) {
case -EBADMSG:
pr_err("auth: bad message");
break;
case -EPROTO:
pr_debug("auth: ignoring message");
break;
}
goto failed;
}
if (dup) {
msg_put(dup);
}
return msg;
failed:
msg_put(msg);
if (dup) {
msg_put(dup);
}
return NULL;
}
static int nsm_request(struct nsm *nsm, char *target)
{
enum transport_type type = transport_type(nsm->trp);
UInteger8 transportSpecific;
struct ptp_message *msg;
struct tlv_extra *extra;
Integer64 asymmetry;
struct address dst;
int cnt, err;
if (str2addr(type, target, &dst)) {
return -1;
}
msg = msg_allocate();
if (!msg) {
return -1;
}
transportSpecific = config_get_int(nsm->cfg, nsm->name, "transportSpecific");
transportSpecific <<= 4;
asymmetry = config_get_int(nsm->cfg, nsm->name, "delayAsymmetry");
asymmetry <<= 16;
msg->hwts.type = config_get_int(nsm->cfg, NULL, "time_stamping");
msg->header.tsmt = DELAY_REQ | transportSpecific;
msg->header.ver = PTP_VERSION;
msg->header.messageLength = sizeof(struct delay_req_msg);
msg->header.domainNumber = config_get_int(nsm->cfg, NULL, "domainNumber");
msg->header.correction = -asymmetry;
msg->header.sourcePortIdentity = nsm->port_identity;
msg->header.sequenceId = nsm->sequence_id++;
msg->header.logMessageInterval = 0x7f;
msg->address = dst;
msg->header.flagField[0] |= UNICAST;
extra = msg_tlv_append(msg, sizeof(struct TLV));
if (!extra) {
msg_put(msg);
return -ENOMEM;
}
extra->tlv->type = TLV_PTPMON_REQ;
extra->tlv->length = 0;
if (nsm->spp >= 0) {
err = sad_append_auth_tlv(nsm->cfg, nsm->spp,
nsm->active_key_id, msg);
} else {
err = msg_pre_send(msg);
}
if (err) {
pr_err("msg_pre_send failed");
goto out;
}
cnt = transport_sendto(nsm->trp, &nsm->fda, TRANS_EVENT, msg);
if (cnt <= 0) {
pr_err("transport_sendto failed");
err = -1;
goto out;
}
if (msg_sots_missing(msg)) {
pr_err("missing timestamp on transmitted delay request");
err = -1;
goto out;
}
nsm_reset(nsm);
nsm->nsm_delay_req = msg;
return 0;
out:
msg_put(msg);
return err;
}
static void nsm_reset(struct nsm *nsm)
{
if (nsm->nsm_delay_req) {
msg_put(nsm->nsm_delay_req);
}
if (nsm->nsm_delay_resp) {
msg_put(nsm->nsm_delay_resp);
}
if (nsm->nsm_sync) {
msg_put(nsm->nsm_sync);
}
if (nsm->nsm_fup) {
msg_put(nsm->nsm_fup);
}
nsm->nsm_delay_req = NULL;
nsm->nsm_delay_resp = NULL;
nsm->nsm_sync = NULL;
nsm->nsm_fup = NULL;
}
static void usage(char *progname)
{
fprintf(stderr,
"\nusage: %s [options]\n\n"
" -f [file] read configuration from 'file'\n"
" -h prints this message and exits\n"
" -i [dev] interface device to use\n"
" -v prints the software version and exits\n"
"\n",
progname);
}
int main(int argc, char *argv[])
{
int batch_mode = 0, c, cnt, err = 0, index, length, tmo = -1;
char *cmd = NULL, *config = NULL, line[1024], *progname;
struct pollfd pollfd[NSM_NFD];
struct nsm *nsm = &the_nsm;
struct ptp_message *msg;
struct option *opts;
struct config *cfg;
if (handle_term_signals()) {
return -1;
}
cfg = config_create();
if (!cfg) {
return -1;
}
opts = config_long_options(cfg);
print_set_verbose(1);
print_set_syslog(0);
/* Process the command line arguments. */
progname = strrchr(argv[0], '/');
progname = progname ? 1+progname : argv[0];
while (EOF != (c = getopt_long(argc, argv, "f:hi:v", opts, &index))) {
switch (c) {
case 0:
if (config_parse_option(cfg, opts[index].name, optarg)) {
config_destroy(cfg);
return -1;
}
break;
case 'f':
config = optarg;
break;
case 'i':
if (!config_create_interface(optarg, cfg)) {
config_destroy(cfg);
return -1;
}
break;
case 'v':
version_show(stdout);
config_destroy(cfg);
return 0;
case 'h':
usage(progname);
config_destroy(cfg);
return 0;
case '?':
default:
usage(progname);
config_destroy(cfg);
return -1;
}
}
print_set_syslog(0);
print_set_verbose(1);
if (config && (err = config_read(config, cfg))) {
goto out;
}
print_set_progname(progname);
print_set_tag(config_get_string(cfg, NULL, "message_tag"));
print_set_level(config_get_int(cfg, NULL, "logging_level"));
if (sad_create(cfg)) {
goto out;
}
err = nsm_open(nsm, cfg);
if (err) {
goto out;
}
if (optind < argc) {
batch_mode = 1;
}
pollfd[0].fd = nsm->fda.fd[0];
pollfd[1].fd = nsm->fda.fd[1];
pollfd[2].fd = batch_mode ? -1 : STDIN_FILENO;
pollfd[0].events = POLLIN | POLLPRI;
pollfd[1].events = POLLIN | POLLPRI;
pollfd[2].events = batch_mode ? 0 : POLLIN | POLLPRI;
while (is_running()) {
if (batch_mode) {
if (optind < argc && !nsm->nsm_delay_req) {
cmd = argv[optind++];
if (nsm_command(nsm, cmd)) {
pr_err("command failed");
continue;
}
}
/* Wait a bit for any outstanding replies. */
tmo = 100;
}
cnt = poll(pollfd, NSM_NFD, tmo);
if (cnt < 0) {
if (EINTR == errno) {
continue;
} else {
pr_emerg("poll failed");
err = -1;
break;
}
} else if (!cnt && optind < argc) {
/* For batch mode. No response received from target node,
* continue with next command. */
nsm_reset(nsm);
continue;
} else if (!cnt) {
break;
}
if (pollfd[2].revents & POLLHUP) {
if (tmo == -1) {
/* Wait a bit longer for outstanding replies. */
tmo = 100;
pollfd[2].fd = -1;
pollfd[2].events = 0;
} else {
break;
}
}
if (pollfd[2].revents & (POLLIN|POLLPRI)) {
if (!fgets(line, sizeof(line), stdin)) {
break;
}
length = strlen(line);
if (length < 2) {
continue;
}
line[length - 1] = 0;
cmd = line;
if (nsm_command(nsm, cmd)) {
pr_err("command failed");
}
}
if (pollfd[0].revents & (POLLIN|POLLPRI)) {
msg = nsm_recv(nsm, pollfd[0].fd);
if (msg) {
nsm_handle_msg(nsm, msg, stdout);
msg_put(msg);
}
}
if (pollfd[1].revents & (POLLIN|POLLPRI)) {
msg = nsm_recv(nsm, pollfd[1].fd);
if (msg) {
nsm_handle_msg(nsm, msg, stdout);
msg_put(msg);
}
}
}
nsm_close(nsm);
out:
msg_cleanup();
sad_destroy(cfg);
config_destroy(cfg);
return err;
}