diff --git a/linuxptp-holdover.patch b/linuxptp-holdover.patch new file mode 100644 index 0000000..1ba13cc --- /dev/null +++ b/linuxptp-holdover.patch @@ -0,0 +1,351 @@ +commit 9c48f5d7765f63d54669d202adbd2cf959abd2cf +Author: Miroslav Lichvar +Date: Thu May 16 15:52:48 2024 +0200 + + ts2phc: Avoid unnecessary call of getppstime(). + + Don't get the ToD timestamp for the pulse polarity detection if it won't + be needed (i.e. extts_polarity is not "both"). This allows PPS + timestamps to be saved even when the ToD source fails. + + Signed-off-by: Miroslav Lichvar + +diff --git a/ts2phc_pps_sink.c b/ts2phc_pps_sink.c +index 8f7745c..e3e9caf 100644 +--- a/ts2phc_pps_sink.c ++++ b/ts2phc_pps_sink.c +@@ -275,21 +275,22 @@ static enum extts_result ts2phc_pps_sink_event(struct ts2phc_private *priv, + goto out; + } + +- err = ts2phc_pps_source_getppstime(priv->src, &source_ts); +- if (err < 0) { +- pr_debug("source ts not valid"); +- return 0; +- } +- +- if (sink->polarity == (PTP_RISING_EDGE | PTP_FALLING_EDGE) && +- ts2phc_pps_sink_ignore(priv, sink, source_ts)) { ++ if (sink->polarity == (PTP_RISING_EDGE | PTP_FALLING_EDGE)) { ++ err = ts2phc_pps_source_getppstime(priv->src, &source_ts); ++ if (err < 0) { ++ pr_debug("source ts not valid"); ++ return 0; ++ } + +- pr_debug("%s SKIP extts index %u at %lld.%09u src %" PRIi64 ".%ld", +- sink->name, event.index, event.t.sec, event.t.nsec, +- (int64_t) source_ts.tv_sec, source_ts.tv_nsec); ++ if (ts2phc_pps_sink_ignore(priv, sink, source_ts)) { ++ pr_debug("%s SKIP extts index %u at %lld.%09u src %" PRIi64 ".%ld", ++ sink->name, event.index, event.t.sec, ++ event.t.nsec, (int64_t)source_ts.tv_sec, ++ source_ts.tv_nsec); + +- result = EXTTS_IGNORE; +- goto out; ++ result = EXTTS_IGNORE; ++ goto out; ++ } + } + + out: + +commit 5ce45bd1a93674fc9cf48552c7a4d2e07b286be5 +Author: Miroslav Lichvar +Date: Thu May 16 15:52:49 2024 +0200 + + ts2phc: Add holdover support. + + If the external PPS signal is generated by a clock with better long-term + stability than the clock synchronizing to it, it can work as a good time + source for some period of time after losing its own time source (e.g. + GPS receiver losing its signal). + + Add an option to specify a holdover interval where ts2phc can continue + synchronizing the clock without any ToD information. Allow that only in + the LOCKED_STABLE servo state, which needs to be enabled by the + servo_num_offset_values option. + + This is supported only in the non-automatic mode and when the pulse + polarity detection is disabled. + + Signed-off-by: Miroslav Lichvar + +diff --git a/config.c b/config.c +index edda408..5dcf7ca 100644 +--- a/config.c ++++ b/config.c +@@ -370,6 +370,7 @@ struct config_item config_tab[] = { + PORT_ITEM_INT("ts2phc.channel", 0, 0, INT_MAX), + PORT_ITEM_INT("ts2phc.extts_correction", 0, INT_MIN, INT_MAX), + PORT_ITEM_ENU("ts2phc.extts_polarity", PTP_RISING_EDGE, extts_polarity_enu), ++ PORT_ITEM_INT("ts2phc.holdover", 0, 0, INT_MAX), + PORT_ITEM_INT("ts2phc.master", 0, 0, 1), + PORT_ITEM_INT("ts2phc.nmea_baudrate", 9600, 300, INT_MAX), + GLOB_ITEM_STR("ts2phc.nmea_remote_host", ""), +diff --git a/ts2phc.8 b/ts2phc.8 +index 7d781c9..8b5bd8b 100644 +--- a/ts2phc.8 ++++ b/ts2phc.8 +@@ -170,6 +170,16 @@ with the log level of the message as a number. The default is an empty string + (which cannot be set in the configuration file as the option requires an + argument). + ++.TP ++.B ts2phc.holdover ++The holdover interval, specified in seconds. When the ToD information stops ++working (e.g. GNSS receiver lost its fix), ts2phc is allowed for the specified ++interval to continue synchronizing the target clock as long as the servo is in ++the SERVO_LOCKED_STABLE state. The servo state needs be enabled by the ++\fBservo_num_offset_values\fP option. The holdover is not supported with the ++\fB-a\fP option and when \fBts2phc.extts_polarity\fP is set to \fIboth\fP. ++The default is 0 (disabled). ++ + .TP + .B sa_file + Specifies the location of the file containing Security Associations used +diff --git a/ts2phc.c b/ts2phc.c +index ccbf772..9fca34d 100644 +--- a/ts2phc.c ++++ b/ts2phc.c +@@ -435,9 +435,10 @@ static int ts2phc_pps_source_implicit_tstamp(struct ts2phc_private *priv, + + static void ts2phc_synchronize_clocks(struct ts2phc_private *priv, int autocfg) + { ++ struct timespec source_ts, now; + tmv_t source_tmv; + struct ts2phc_clock *c; +- int valid, err; ++ int holdover, valid; + + if (autocfg) { + if (!priv->ref_clock) { +@@ -451,9 +452,20 @@ static void ts2phc_synchronize_clocks(struct ts2phc_private *priv, int autocfg) + return; + } + } else { +- err = ts2phc_pps_source_implicit_tstamp(priv, &source_tmv); +- if (err < 0) ++ valid = !ts2phc_pps_source_implicit_tstamp(priv, &source_tmv); ++ } ++ ++ if (valid) { ++ priv->holdover_start = 0; ++ holdover = 0; ++ } else { ++ clock_gettime(CLOCK_MONOTONIC, &now); ++ ++ if (!priv->holdover_start) ++ priv->holdover_start = now.tv_sec; ++ if (now.tv_sec >= priv->holdover_start + priv->holdover_length) + return; ++ holdover = 1; + } + + LIST_FOREACH(c, &priv->clocks, list) { +@@ -470,6 +482,16 @@ static void ts2phc_synchronize_clocks(struct ts2phc_private *priv, int autocfg) + continue; + } + ++ if (holdover) { ++ if (c->servo_state != SERVO_LOCKED_STABLE) ++ continue; ++ source_ts = tmv_to_timespec(ts); ++ if (source_ts.tv_nsec > NS_PER_SEC / 2) ++ source_ts.tv_sec++; ++ source_ts.tv_nsec = 0; ++ source_tmv = timespec_to_tmv(source_ts); ++ } ++ + offset = tmv_to_nanoseconds(tmv_sub(ts, source_tmv)); + + if (c->no_adj) { +@@ -481,8 +503,15 @@ static void ts2phc_synchronize_clocks(struct ts2phc_private *priv, int autocfg) + adj = servo_sample(c->servo, offset, tmv_to_nanoseconds(ts), + SAMPLE_WEIGHT, &c->servo_state); + +- pr_info("%s offset %10" PRId64 " s%d freq %+7.0f", +- c->name, offset, c->servo_state, adj); ++ if (holdover && c->servo_state != SERVO_LOCKED_STABLE) { ++ pr_info("%s lost holdover lock (offset %10" PRId64 ")", ++ c->name, offset); ++ continue; ++ } ++ ++ pr_info("%s offset %10" PRId64 " s%d freq %+7.0f%s", ++ c->name, offset, c->servo_state, adj, ++ holdover ? " holdover" : ""); + + switch (c->servo_state) { + case SERVO_UNLOCKED: +@@ -751,6 +780,9 @@ int main(int argc, char *argv[]) + return -1; + } + ++ priv.holdover_length = config_get_int(cfg, NULL, "ts2phc.holdover"); ++ priv.holdover_start = 0; ++ + while (is_running()) { + struct ts2phc_clock *clk; + +diff --git a/ts2phc.h b/ts2phc.h +index 4833ded..5dbde9b 100644 +--- a/ts2phc.h ++++ b/ts2phc.h +@@ -55,6 +55,8 @@ struct ts2phc_private { + bool state_changed; + LIST_HEAD(port_head, ts2phc_port) ports; + LIST_HEAD(clock_head, ts2phc_clock) clocks; ++ int holdover_length; ++ time_t holdover_start; + }; + + struct ts2phc_clock *ts2phc_clock_add(struct ts2phc_private *priv, + +commit 7ff632d9177d6024e5a340c7766562cb6bca696a +Author: Miroslav Lichvar +Date: Thu Jul 25 12:50:51 2024 +0200 + + ts2phc: Describe servo options in man page. + + Copy and adapt missing servo options used by ts2phc from the ptp4l man + page. + + Signed-off-by: Miroslav Lichvar + +diff --git a/ts2phc.8 b/ts2phc.8 +index 8b5bd8b..38e5be2 100644 +--- a/ts2phc.8 ++++ b/ts2phc.8 +@@ -126,6 +126,16 @@ are read from the file specified by \fBsa_file\fR. Requires \fBspp\fR + and \fBsa_file\fR directives. Must be in the range of 1 to 2^32-1, + inclusive. The default is 0 (disabled). + ++.TP ++.B clock_servo ++The servo which is used to synchronize the local clock. Valid values ++are "pi" for a PI controller, "linreg" for an adaptive controller ++using linear regression, "ntpshm" and "refclock_sock" for the NTP SHM and ++chrony SOCK reference clocks respectively to allow another process to ++synchronize the local clock, and "nullf" for a servo that always dials ++frequency offset zero (for use in SyncE nodes). ++The default is "pi". ++ + .TP + .B first_step_threshold + The maximum offset, specified in seconds, that the servo will correct by +@@ -170,6 +180,65 @@ with the log level of the message as a number. The default is an empty string + (which cannot be set in the configuration file as the option requires an + argument). + ++.TP ++.B ntpshm_segment ++The number of the SHM segment used by ntpshm servo. ++The default is 0. ++ ++.TP ++.B pi_integral_const ++The integral constant of the PI controller. When set to 0.0, the ++integral constant will be set by the following formula from the current ++sync interval. ++The default is 0.0. ++ ++ki = min(ki_scale * sync^ki_exponent, ki_norm_max / sync) ++ ++.TP ++.B pi_integral_exponent ++The ki_exponent constant in the formula used to set the integral constant of ++the PI controller from the sync interval. ++The default is 0.4. ++ ++.TP ++.B pi_integral_norm_max ++The ki_norm_max constant in the formula used to set the integral constant of ++the PI controller from the sync interval. ++The default is 0.3. ++ ++.TP ++.B pi_integral_scale ++The ki_scale constant in the formula used to set the integral constant of ++the PI controller from the sync interval. ++The default is 0.3. ++ ++.TP ++.B pi_proportional_const ++The proportional constant of the PI controller. When set to 0.0, the ++proportional constant will be set by the following formula from the current ++sync interval. ++The default is 0.0. ++ ++kp = min(kp_scale * sync^kp_exponent, kp_norm_max / sync) ++ ++.TP ++.B pi_proportional_exponent ++The kp_exponent constant in the formula used to set the proportional constant of ++the PI controller from the sync interval. ++The default is \-0.3. ++ ++.TP ++.B pi_proportional_norm_max ++The kp_norm_max constant in the formula used to set the proportional constant of ++the PI controller from the sync interval. ++The default is 0.7 ++ ++.TP ++.B pi_proportional_scale ++The kp_scale constant in the formula used to set the proportional constant of ++the PI controller from the sync interval. ++The default is 0.7. ++ + .TP + .B ts2phc.holdover + The holdover interval, specified in seconds. When the ToD information stops +@@ -188,6 +257,21 @@ the optional security mechanism defined in ieee1588-2019 ch 14.16. See + \fBSECURITY ASSOCIATION OPTIONS\fR for more info on file contents. + The default is an empty string. (disabled). + ++.TP ++.B servo_num_offset_values ++The number of offset values considered in order to transition from the ++SERVO_LOCKED to the SERVO_LOCKED_STABLE state. ++The transition occurs once the last 'servo_num_offset_values' offsets ++are all below the 'servo_offset_threshold' value. ++The default value is 10. ++ ++.TP ++.B servo_offset_threshold ++The offset threshold used in order to transition from the SERVO_LOCKED ++to the SERVO_LOCKED_STABLE state. The transition occurs once the ++last 'servo_num_offset_values' offsets are all below the threshold value. ++The default value of offset_threshold is 0 (disabled). ++ + .TP + .B step_threshold + The maximum offset, specified in seconds, that the servo will correct + +commit 772e59695ce3144fbc290a178df6ee2f7369ddc7 +Author: Miroslav Lichvar +Date: Thu Jul 25 12:50:52 2024 +0200 + + ts2phc: Fix description of holdover option in man page. + + Fix the man page to explain that the LOCKED_STABLE servo state is + enabled by setting the servo_offset_threshold, not + servo_num_offset_values, which is already enabled by default. + + Signed-off-by: Miroslav Lichvar + +diff --git a/ts2phc.8 b/ts2phc.8 +index 38e5be2..41f91d9 100644 +--- a/ts2phc.8 ++++ b/ts2phc.8 +@@ -245,7 +245,7 @@ The holdover interval, specified in seconds. When the ToD information stops + working (e.g. GNSS receiver lost its fix), ts2phc is allowed for the specified + interval to continue synchronizing the target clock as long as the servo is in + the SERVO_LOCKED_STABLE state. The servo state needs be enabled by the +-\fBservo_num_offset_values\fP option. The holdover is not supported with the ++\fBservo_offset_threshold\fP option. The holdover is not supported with the + \fB-a\fP option and when \fBts2phc.extts_polarity\fP is set to \fIboth\fP. + The default is 0 (disabled). + diff --git a/linuxptp.spec b/linuxptp.spec index 8029aba..bd0609d 100644 --- a/linuxptp.spec +++ b/linuxptp.spec @@ -29,6 +29,8 @@ Source22: linuxptp.te # limit unicast message rate per address and grant duration Patch1: linuxptp-ucastrate.patch +# add holdover support to ts2phc +Patch2: linuxptp-holdover.patch # fix ts2phc to handle large NMEA delay Patch4: linuxptp-nmeadelay.patch