forked from richardcochran/linuxptp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phc2sys.c
1384 lines (1243 loc) · 33 KB
/
phc2sys.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
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file phc2sys.c
* @brief Utility program to synchronize two clocks via a PPS.
* @note Copyright (C) 2012 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 <fcntl.h>
#include <float.h>
#include <inttypes.h>
#include <limits.h>
#include <net/if.h>
#include <poll.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <linux/pps.h>
#include <linux/ptp_clock.h>
#include "clockadj.h"
#include "clockcheck.h"
#include "contain.h"
#include "ds.h"
#include "fsm.h"
#include "missing.h"
#include "notification.h"
#include "ntpshm.h"
#include "phc.h"
#include "pi.h"
#include "pmc_agent.h"
#include "print.h"
#include "servo.h"
#include "sk.h"
#include "stats.h"
#include "sysoff.h"
#include "tlv.h"
#include "uds.h"
#include "util.h"
#include "version.h"
#define KP 0.7
#define KI 0.3
#define NS_PER_SEC 1000000000LL
#define PHC_PPS_OFFSET_LIMIT 10000000
struct clock {
LIST_ENTRY(clock) list;
LIST_ENTRY(clock) dst_list;
clockid_t clkid;
int phc_index;
int sysoff_method;
int is_utc;
int dest_only;
int state;
int new_state;
int sync_offset;
int leap_set;
int utc_offset_set;
struct servo *servo;
enum servo_state servo_state;
char *device;
const char *source_label;
struct stats *offset_stats;
struct stats *freq_stats;
struct stats *delay_stats;
struct clockcheck *sanity_check;
};
struct port {
LIST_ENTRY(port) list;
unsigned int number;
int state;
struct clock *clock;
};
struct phc2sys_private {
unsigned int stats_max_count;
int sanity_freq_limit;
enum servo_type servo_type;
int phc_readings;
double phc_interval;
int forced_sync_offset;
int kernel_leap;
int state_changed;
int free_running;
struct pmc_agent *agent;
LIST_HEAD(port_head, port) ports;
LIST_HEAD(clock_head, clock) clocks;
LIST_HEAD(dst_clock_head, clock) dst_clocks;
struct clock *master;
};
static struct config *phc2sys_config;
static int clock_handle_leap(struct phc2sys_private *priv,
struct clock *clock,
int64_t offset, uint64_t ts);
static struct servo *servo_add(struct phc2sys_private *priv,
struct clock *clock)
{
double ppb;
int max_ppb;
struct servo *servo;
clockadj_init(clock->clkid);
ppb = clockadj_get_freq(clock->clkid);
/* The reading may silently fail and return 0, reset the frequency to
make sure ppb is the actual frequency of the clock. */
if (!priv->free_running)
clockadj_set_freq(clock->clkid, ppb);
if (clock->clkid == CLOCK_REALTIME) {
sysclk_set_leap(0);
max_ppb = sysclk_max_freq();
} else {
max_ppb = phc_max_adj(clock->clkid);
if (!max_ppb) {
pr_err("clock is not adjustable");
return NULL;
}
}
servo = servo_create(phc2sys_config, priv->servo_type,
-ppb, max_ppb, 0);
if (!servo) {
pr_err("Failed to create servo");
return NULL;
}
servo_sync_interval(servo, priv->phc_interval);
return servo;
}
static struct clock *clock_add(struct phc2sys_private *priv, const char *device,
int phc_index)
{
struct clock *c;
clockid_t clkid = CLOCK_INVALID;
char phc_device[19];
if (device) {
if (phc_index >= 0) {
snprintf(phc_device, sizeof(phc_device), "/dev/ptp%d",
phc_index);
clkid = posix_clock_open(phc_device, &phc_index);
} else {
clkid = posix_clock_open(device, &phc_index);
}
if (clkid == CLOCK_INVALID)
return NULL;
}
c = calloc(1, sizeof(*c));
if (!c) {
pr_err("failed to allocate memory for a clock");
return NULL;
}
c->clkid = clkid;
c->phc_index = phc_index;
c->servo_state = SERVO_UNLOCKED;
c->device = device ? strdup(device) : NULL;
if (c->clkid == CLOCK_REALTIME) {
c->source_label = "sys";
c->is_utc = 1;
} else {
c->source_label = "phc";
}
if (priv->stats_max_count > 0) {
c->offset_stats = stats_create();
c->freq_stats = stats_create();
c->delay_stats = stats_create();
if (!c->offset_stats ||
!c->freq_stats ||
!c->delay_stats) {
pr_err("failed to create stats");
return NULL;
}
}
if (priv->sanity_freq_limit) {
c->sanity_check = clockcheck_create(priv->sanity_freq_limit);
if (!c->sanity_check) {
pr_err("failed to create clock check");
return NULL;
}
}
if (clkid != CLOCK_INVALID && clkid != CLOCK_REALTIME)
c->sysoff_method = sysoff_probe(CLOCKID_TO_FD(clkid),
priv->phc_readings);
LIST_INSERT_HEAD(&priv->clocks, c, list);
return c;
}
static void clock_cleanup(struct phc2sys_private *priv)
{
struct clock *c, *tmp;
LIST_FOREACH_SAFE(c, &priv->clocks, list, tmp) {
if (c->servo) {
servo_destroy(c->servo);
}
if (c->sanity_check) {
clockcheck_destroy(c->sanity_check);
}
if (c->delay_stats) {
stats_destroy(c->delay_stats);
}
if (c->freq_stats) {
stats_destroy(c->freq_stats);
}
if (c->offset_stats) {
stats_destroy(c->offset_stats);
}
if (c->device) {
free(c->device);
}
free(c);
}
}
static void port_cleanup(struct phc2sys_private *priv)
{
struct port *p, *tmp;
LIST_FOREACH_SAFE(p, &priv->ports, list, tmp) {
free(p);
}
}
static struct port *port_get(struct phc2sys_private *priv, unsigned int number)
{
struct port *p;
LIST_FOREACH(p, &priv->ports, list) {
if (p->number == number)
return p;
}
return NULL;
}
static struct port *port_add(struct phc2sys_private *priv, unsigned int number,
char *device, int phc_index)
{
struct port *p;
struct clock *c = NULL, *tmp;
p = port_get(priv, number);
if (p)
return p;
/* port is a new one, look whether we have the device already on
* a different port */
LIST_FOREACH(tmp, &priv->clocks, list) {
if (!strcmp(tmp->device, device)) {
c = tmp;
break;
}
}
if (!c) {
c = clock_add(priv, device, phc_index);
if (!c)
return NULL;
}
p = malloc(sizeof(*p));
if (!p) {
pr_err("failed to allocate memory for a port");
return NULL;
}
p->number = number;
p->clock = c;
LIST_INSERT_HEAD(&priv->ports, p, list);
return p;
}
static void clock_reinit(struct phc2sys_private *priv, struct clock *clock,
int new_state)
{
int err = -1, phc_index = -1, phc_switched = 0, timestamping;
char iface[IFNAMSIZ], phc_device[19];
enum port_state state;
struct port *p;
clockid_t clkid = CLOCK_INVALID;
LIST_FOREACH(p, &priv->ports, list) {
if (p->clock != clock) {
continue;
}
err = pmc_agent_query_port_properties(priv->agent, 1000,
p->number, &state,
×tamping, &phc_index,
iface);
if (!err) {
p->state = port_state_normalize(state);
}
break;
}
if (!err && timestamping != TS_SOFTWARE) {
/* Check if device changed */
if (strcmp(clock->device, iface)) {
free(clock->device);
clock->device = strdup(iface);
}
/* Check if phc index changed */
if (clock->phc_index != phc_index) {
snprintf(phc_device, sizeof(phc_device), "/dev/ptp%d",
phc_index);
clkid = posix_clock_open(phc_device, &phc_index);
if (clkid == CLOCK_INVALID)
return;
posix_clock_close(clock->clkid);
clock->clkid = clkid;
clock->phc_index = phc_index;
if (clock->servo) {
servo_destroy(clock->servo);
clock->servo = NULL;
}
phc_switched = 1;
}
}
if (new_state == PS_MASTER || phc_switched) {
if (clock->servo)
servo_reset(clock->servo);
clock->servo_state = SERVO_UNLOCKED;
if (clock->offset_stats) {
stats_reset(clock->offset_stats);
stats_reset(clock->freq_stats);
stats_reset(clock->delay_stats);
}
}
pr_debug("%s: state change %s -> %s", clock->device,
ps_str[clock->state], ps_str[new_state]);
}
static struct clock *find_dst_clock(struct phc2sys_private *priv,
int phc_index)
{
struct clock *c = NULL;
LIST_FOREACH(c, &priv->dst_clocks, dst_list) {
if (c->phc_index == phc_index) {
break;
}
}
return c;
}
static void reconfigure(struct phc2sys_private *priv)
{
struct clock *c, *rt = NULL, *src = NULL, *last = NULL, *dup = NULL;
int src_cnt = 0, dst_cnt = 0;
pr_info("reconfiguring after port state change");
priv->state_changed = 0;
while (priv->dst_clocks.lh_first != NULL) {
LIST_REMOVE(priv->dst_clocks.lh_first, dst_list);
}
LIST_FOREACH(c, &priv->clocks, list) {
if (c->clkid == CLOCK_REALTIME) {
rt = c;
continue;
}
if (c->new_state) {
clock_reinit(priv, c, c->new_state);
c->state = c->new_state;
c->new_state = 0;
}
switch (c->state) {
case PS_FAULTY:
case PS_DISABLED:
case PS_LISTENING:
case PS_PRE_MASTER:
case PS_MASTER:
case PS_PASSIVE:
dup = find_dst_clock(priv, c->phc_index);
if (!dup) {
pr_info("selecting %s for synchronization",
c->device);
dst_cnt++;
LIST_INSERT_HEAD(&priv->dst_clocks,
c, dst_list);
} else {
pr_info("skipping %s: %s has the same clock "
"and is already selected",
c->device, dup->device);
}
break;
case PS_UNCALIBRATED:
src_cnt++;
break;
case PS_SLAVE:
src = c;
src_cnt++;
break;
}
last = c;
}
if (src_cnt > 1) {
pr_info("multiple master clocks available, postponing sync...");
priv->master = NULL;
return;
}
if (src_cnt > 0 && !src) {
pr_info("master clock not ready, waiting...");
priv->master = NULL;
return;
}
if (!src_cnt && !dst_cnt) {
pr_info("no PHC ready, waiting...");
priv->master = NULL;
return;
}
if (dst_cnt > 1 && !src) {
if (!rt || rt->dest_only) {
priv->master = last;
/* Reset to original state in next reconfiguration. */
priv->master->new_state = priv->master->state;
priv->master->state = PS_SLAVE;
if (rt)
rt->state = PS_SLAVE;
pr_info("no source, selecting %s as the default clock",
last->device);
return;
}
}
if ((!src_cnt && (!rt || rt->dest_only)) ||
(!dst_cnt && !rt)) {
pr_info("nothing to synchronize");
priv->master = NULL;
return;
}
if (!src_cnt) {
src = rt;
rt->state = PS_SLAVE;
} else if (rt) {
if (rt->state != PS_MASTER) {
rt->state = PS_MASTER;
clock_reinit(priv, rt, rt->state);
}
LIST_INSERT_HEAD(&priv->dst_clocks, rt, dst_list);
pr_info("selecting %s for synchronization", rt->device);
}
priv->master = src;
pr_info("selecting %s as the master clock", src->device);
}
static int64_t get_sync_offset(struct phc2sys_private *priv, struct clock *dst)
{
int direction = priv->forced_sync_offset;
if (!direction)
direction = dst->is_utc - priv->master->is_utc;
return (int64_t)dst->sync_offset * NS_PER_SEC * direction;
}
static void update_clock_stats(struct clock *clock, unsigned int max_count,
int64_t offset, double freq, int64_t delay)
{
struct stats_result offset_stats, freq_stats, delay_stats;
stats_add_value(clock->offset_stats, offset);
stats_add_value(clock->freq_stats, freq);
if (delay >= 0)
stats_add_value(clock->delay_stats, delay);
if (stats_get_num_values(clock->offset_stats) < max_count)
return;
stats_get_result(clock->offset_stats, &offset_stats);
stats_get_result(clock->freq_stats, &freq_stats);
if (!stats_get_result(clock->delay_stats, &delay_stats)) {
pr_info("%s "
"rms %4.0f max %4.0f "
"freq %+6.0f +/- %3.0f "
"delay %5.0f +/- %3.0f",
clock->device,
offset_stats.rms, offset_stats.max_abs,
freq_stats.mean, freq_stats.stddev,
delay_stats.mean, delay_stats.stddev);
} else {
pr_info("%s "
"rms %4.0f max %4.0f "
"freq %+6.0f +/- %3.0f",
clock->device,
offset_stats.rms, offset_stats.max_abs,
freq_stats.mean, freq_stats.stddev);
}
stats_reset(clock->offset_stats);
stats_reset(clock->freq_stats);
stats_reset(clock->delay_stats);
}
static void update_clock(struct phc2sys_private *priv, struct clock *clock,
int64_t offset, uint64_t ts, int64_t delay)
{
enum servo_state state = SERVO_UNLOCKED;
double ppb = 0.0;
if (!clock->servo) {
clock->servo = servo_add(priv, clock);
if (!clock->servo)
return;
}
if (clock_handle_leap(priv, clock, offset, ts))
return;
offset += get_sync_offset(priv, clock);
if (priv->free_running)
goto report;
if (clock->sanity_check && clockcheck_sample(clock->sanity_check, ts))
servo_reset(clock->servo);
ppb = servo_sample(clock->servo, offset, ts, 1.0, &state);
clock->servo_state = state;
switch (state) {
case SERVO_UNLOCKED:
break;
case SERVO_JUMP:
clockadj_step(clock->clkid, -offset);
if (clock->sanity_check)
clockcheck_step(clock->sanity_check, -offset);
/* Fall through. */
case SERVO_LOCKED:
case SERVO_LOCKED_STABLE:
clockadj_set_freq(clock->clkid, -ppb);
if (clock->clkid == CLOCK_REALTIME)
sysclk_set_sync();
if (clock->sanity_check)
clockcheck_set_freq(clock->sanity_check, -ppb);
break;
}
report:
if (clock->offset_stats) {
update_clock_stats(clock, priv->stats_max_count, offset, ppb, delay);
} else {
if (delay >= 0) {
pr_info("%s %s offset %9" PRId64 " s%d freq %+7.0f "
"delay %6" PRId64,
clock->device, priv->master->source_label,
offset, state, ppb, delay);
} else {
pr_info("%s %s offset %9" PRId64 " s%d freq %+7.0f",
clock->device, priv->master->source_label,
offset, state, ppb);
}
}
}
static void enable_pps_output(clockid_t src)
{
int enable = 1;
if (!phc_has_pps(src))
return;
if (ioctl(CLOCKID_TO_FD(src), PTP_ENABLE_PPS, enable) < 0)
pr_warning("failed to enable PPS output");
}
static int read_pps(int fd, int64_t *offset, uint64_t *ts)
{
struct pps_fdata pfd;
pfd.timeout.sec = 10;
pfd.timeout.nsec = 0;
pfd.timeout.flags = ~PPS_TIME_INVALID;
if (ioctl(fd, PPS_FETCH, &pfd)) {
pr_err("failed to fetch PPS: %m");
return 0;
}
*ts = pfd.info.assert_tu.sec * NS_PER_SEC;
*ts += pfd.info.assert_tu.nsec;
*offset = *ts % NS_PER_SEC;
if (*offset > NS_PER_SEC / 2)
*offset -= NS_PER_SEC;
return 1;
}
static int do_pps_loop(struct phc2sys_private *priv, struct clock *clock,
int fd)
{
int64_t pps_offset, phc_offset, phc_delay;
uint64_t pps_ts, phc_ts;
clockid_t src = priv->master->clkid;
int err;
priv->master->source_label = "pps";
if (src == CLOCK_INVALID) {
/* The sync offset can't be applied with PPS alone. */
pmc_agent_set_sync_offset(priv->agent, 0);
} else {
enable_pps_output(priv->master->clkid);
}
while (is_running()) {
if (!read_pps(fd, &pps_offset, &pps_ts)) {
continue;
}
/* If a PHC is available, use it to get the whole number
of seconds in the offset and PPS for the rest. */
if (src != CLOCK_INVALID) {
err = clockadj_compare(src, clock->clkid,
priv->phc_readings,
&phc_offset, &phc_ts,
&phc_delay);
if (err == -EBUSY)
continue;
if (err)
return -1;
/* Convert the time stamp to the PHC time. */
phc_ts -= phc_offset;
/* Check if it is close to the start of the second. */
if (phc_ts % NS_PER_SEC > PHC_PPS_OFFSET_LIMIT) {
pr_warning("PPS is not in sync with PHC"
" (0.%09lld)", phc_ts % NS_PER_SEC);
continue;
}
phc_ts = phc_ts / NS_PER_SEC * NS_PER_SEC;
pps_offset = pps_ts - phc_ts;
}
if (pmc_agent_update(priv->agent) < 0)
continue;
update_clock(priv, clock, pps_offset, pps_ts, -1);
}
close(fd);
return 0;
}
static int update_needed(struct clock *c)
{
switch (c->state) {
case PS_FAULTY:
case PS_DISABLED:
case PS_LISTENING:
case PS_PRE_MASTER:
case PS_MASTER:
case PS_PASSIVE:
return 1;
case PS_UNCALIBRATED:
case PS_SLAVE:
break;
}
return 0;
}
static int do_loop(struct phc2sys_private *priv)
{
struct timespec interval;
struct clock *clock;
uint64_t ts;
int64_t offset, delay;
int err;
interval.tv_sec = priv->phc_interval;
interval.tv_nsec = (priv->phc_interval - interval.tv_sec) * 1e9;
while (is_running()) {
clock_nanosleep(CLOCK_MONOTONIC, 0, &interval, NULL);
if (pmc_agent_update(priv->agent) < 0) {
continue;
}
if (priv->state_changed) {
/* force getting offset, as it may have
* changed after the port state change */
if (pmc_agent_query_utc_offset(priv->agent, 1000)) {
pr_err("failed to get UTC offset");
continue;
}
reconfigure(priv);
}
if (!priv->master)
continue;
LIST_FOREACH(clock, &priv->dst_clocks, dst_list) {
if (!update_needed(clock))
continue;
/* don't try to synchronize the clock to itself */
if (clock->clkid == priv->master->clkid ||
(clock->phc_index >= 0 &&
clock->phc_index == priv->master->phc_index) ||
!strcmp(clock->device, priv->master->device))
continue;
if (clock->clkid == CLOCK_REALTIME &&
priv->master->sysoff_method >= 0) {
/* use sysoff */
err = sysoff_measure(CLOCKID_TO_FD(priv->master->clkid),
priv->master->sysoff_method,
priv->phc_readings,
&offset, &ts, &delay);
} else if (priv->master->clkid == CLOCK_REALTIME &&
clock->sysoff_method >= 0) {
/* use reversed sysoff */
err = sysoff_measure(CLOCKID_TO_FD(clock->clkid),
clock->sysoff_method,
priv->phc_readings,
&offset, &ts, &delay);
if (!err) {
offset = -offset;
ts += offset;
}
} else {
/* use phc */
err = clockadj_compare(priv->master->clkid,
clock->clkid,
priv->phc_readings,
&offset, &ts, &delay);
}
if (err == -EBUSY)
continue;
if (err)
return -1;
update_clock(priv, clock, offset, ts, delay);
}
}
return 0;
}
static int clock_compute_state(struct phc2sys_private *priv,
struct clock *clock)
{
struct port *p;
int state = PS_DISABLED;
LIST_FOREACH(p, &priv->ports, list) {
if (p->clock != clock)
continue;
/* PS_SLAVE takes the highest precedence, PS_UNCALIBRATED
* after that, PS_MASTER is third, PS_PRE_MASTER fourth and
* all of that overrides PS_DISABLED, which corresponds
* nicely with the numerical values */
if (p->state > state)
state = p->state;
}
return state;
}
static int phc2sys_recv_subscribed(void *context, struct ptp_message *msg,
int excluded)
{
struct phc2sys_private *priv = (struct phc2sys_private *) context;
int mgt_id, state;
struct portDS *pds;
struct port *port;
struct clock *clock;
mgt_id = management_tlv_id(msg);
if (mgt_id == excluded)
return 0;
switch (mgt_id) {
case MID_PORT_DATA_SET:
pds = management_tlv_data(msg);
port = port_get(priv, pds->portIdentity.portNumber);
if (!port) {
pr_info("received data for unknown port %s",
pid2str(&pds->portIdentity));
return 1;
}
state = port_state_normalize(pds->portState);
if (port->state != state) {
pr_info("port %s changed state",
pid2str(&pds->portIdentity));
port->state = state;
clock = port->clock;
state = clock_compute_state(priv, clock);
if (clock->state != state || clock->new_state) {
clock->new_state = state;
priv->state_changed = 1;
}
}
return 1;
}
return 0;
}
static int auto_init_ports(struct phc2sys_private *priv, int add_rt)
{
int err, number_ports, phc_index, timestamping;
enum port_state state;
char iface[IFNAMSIZ];
struct clock *clock;
struct port *port;
unsigned int i;
while (1) {
if (!is_running()) {
return -1;
}
err = pmc_agent_query_dds(priv->agent, 1000);
if (!err) {
break;
}
if (err == -ETIMEDOUT) {
pr_notice("Waiting for ptp4l...");
} else {
return -1;
}
}
number_ports = pmc_agent_get_number_ports(priv->agent);
if (number_ports <= 0) {
pr_err("failed to get number of ports");
return -1;
}
err = pmc_agent_subscribe(priv->agent, 1000);
if (err) {
pr_err("failed to subscribe");
return -1;
}
for (i = 1; i <= number_ports; i++) {
err = pmc_agent_query_port_properties(priv->agent, 1000, i,
&state, ×tamping,
&phc_index, iface);
if (err == -ENODEV) {
/* port does not exist, ignore the port */
continue;
}
if (err) {
pr_err("failed to get port properties");
return -1;
}
if (timestamping == TS_SOFTWARE) {
/* ignore ports with software time stamping */
continue;
}
port = port_add(priv, i, iface, phc_index);
if (!port)
return -1;
port->state = port_state_normalize(state);
}
if (LIST_EMPTY(&priv->clocks)) {
pr_err("no suitable ports available");
return -1;
}
LIST_FOREACH(clock, &priv->clocks, list) {
clock->new_state = clock_compute_state(priv, clock);
}
priv->state_changed = 1;
if (add_rt) {
clock = clock_add(priv, "CLOCK_REALTIME", -1);
if (!clock)
return -1;
if (add_rt == 1)
clock->dest_only = 1;
}
/* get initial offset */
if (pmc_agent_query_utc_offset(priv->agent, 1000)) {
pr_err("failed to get UTC offset");
return -1;
}
return 0;
}
/* Returns: non-zero to skip clock update */
static int clock_handle_leap(struct phc2sys_private *priv, struct clock *clock,
int64_t offset, uint64_t ts)
{
int clock_leap, node_leap = pmc_agent_get_leap(priv->agent);
clock->sync_offset = pmc_agent_get_sync_offset(priv->agent);
if ((node_leap || clock->leap_set) &&
clock->is_utc != priv->master->is_utc) {
/* If the master clock is in UTC, get a time stamp from it, as
it is the clock which will include the leap second. */
if (priv->master->is_utc) {
struct timespec tp;
if (clock_gettime(priv->master->clkid, &tp)) {
pr_err("failed to read clock: %m");
return -1;
}
ts = tp.tv_sec * NS_PER_SEC + tp.tv_nsec;
}
/* If the clock will be stepped, the time stamp has to be the
new time. Ignore possible 1 second error in UTC offset. */
if (clock->is_utc && clock->servo_state == SERVO_UNLOCKED)
ts -= offset + get_sync_offset(priv, clock);
/* Suspend clock updates in the last second before midnight. */
if (is_utc_ambiguous(ts)) {
pr_info("clock update suspended due to leap second");
return 1;
}
clock_leap = leap_second_status(ts, clock->leap_set,
&node_leap,
&clock->sync_offset);
if (clock->leap_set != clock_leap) {
/* Only the system clock can leap. */
if (clock->clkid == CLOCK_REALTIME &&
priv->kernel_leap)
sysclk_set_leap(clock_leap);
else
servo_leap(clock->servo, clock_leap);
clock->leap_set = clock_leap;
}
}
if (pmc_agent_utc_offset_traceable(priv->agent) &&
clock->utc_offset_set != clock->sync_offset) {
if (clock->clkid == CLOCK_REALTIME)
sysclk_set_tai_offset(clock->sync_offset);
clock->utc_offset_set = clock->sync_offset;
}
return 0;
}
static bool hardpps_configured(int fd)
{
return fd >= 0;
}
static int phc2sys_static_src_configuration(struct phc2sys_private *priv,
const char *src_name)
{
struct clock *src;
src = clock_add(priv, src_name, -1);
if (!src) {
fprintf(stderr, "valid source clock must be selected.\n");
return -1;
}
src->state = PS_SLAVE;
priv->master = src;
return 0;
}
static int phc2sys_static_dst_configuration(struct phc2sys_private *priv,
const char *dst_name)
{
struct clock *dst;
dst = clock_add(priv, dst_name, -1);
if (!dst) {
fprintf(stderr, "valid destination clock must be selected.\n");
return -1;
}
dst->state = PS_MASTER;
LIST_INSERT_HEAD(&priv->dst_clocks, dst, dst_list);