Commit Graph

144 Commits (467552d1cbc8c7e08c4684e2092f95e874b33218)

Author SHA1 Message Date
Richard Cochran dde0bf2e28 Balance the posix clock open function with a close method.
The one user of the function, posix_clock_open(), simply open codes
the closing call to phc_close().  This patch provides a method to
balance closing and opening of a posix clock.  In addition, the
phc_ctl program never explicitly closed the opened clock, and so this
patch adds the missing call.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
2020-03-21 04:36:10 -07:00
Richard Cochran 25d4e4fbdd Revert "phc2sys: Notify kernel if clock is not in sync"
This novel use of the STA_UNSYNC flag breaks with the established,
traditional meaning of that flag, which is:

   The clock has is believed to have an error larger than 16 seconds.

This reverts commit 944f5f7ac6.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
2020-03-06 10:13:50 -08:00
Ville Heikkinen 944f5f7ac6 phc2sys: Notify kernel if clock is not in sync
In case there is no connection to the server, notify the kernel
that the clock is currently unsynchronized.

Signed-off-by: Ville Heikkinen <ville.heikkinen@nokia.com>
2020-03-04 10:01:27 -08:00
Richard Cochran e0580929f4 phc2sys: Fix frequency estimation when synchronizing a PHC to the system clock.
When synchronizing a PHC to the Linux system clock (CLOCK_REALTIME),
the phc2sys uses the sysoff method, reversing the master and slave
roles.

The offset between a master clock and a slave clock is given by

    offset = slave_ts - master_ts,

and the call to sysoff_measure() provides the 'offset' and 'slave_ts'
values.  The needed local time stamp on the 'master' is given by

    master_ts = slave_ts - offset,

but the code calcuates

    master_ts = slave_ts + offset.

When passed to the servo, the local time stamp is used to estimate the
frequency offset between the two clocks before starting the main
synchronization loop.  The effect of the bug may be seen with a simple
test.  Here is a sample output with the existing code.

    $ sudo testptp -d /dev/ptp1 -f 62400000
    frequency adjustment okay
    $ sudo ./phc2sys -m -q -c eth6 -s CLOCK_REALTIME -O0
    phc2sys[90221.239]: eth6 sys offset 191001318 s0 freq -62400000 delay   5547
    phc2sys[90222.239]: eth6 sys offset 253380897 s1 freq  +8265884 delay   5507
    phc2sys[90223.239]: eth6 sys offset  -8301685 s2 freq    -35801 delay   5487
    phc2sys[90224.239]: eth6 sys offset  -8297136 s2 freq  -2521757 delay   5531
    phc2sys[90225.239]: eth6 sys offset  -5806117 s2 freq  -2519879 delay   5542
    phc2sys[90226.239]: eth6 sys offset  -3317009 s2 freq  -1772606 delay   5495
    phc2sys[90227.240]: eth6 sys offset  -1575231 s2 freq  -1025931 delay   5505
    phc2sys[90228.240]: eth6 sys offset   -580249 s2 freq   -503518 delay   5524
    phc2sys[90229.240]: eth6 sys offset   -107770 s2 freq   -205114 delay   5519
    phc2sys[90230.240]: eth6 sys offset     66298 s2 freq    -63377 delay   5490
    phc2sys[90230.881]: eth6 sys offset     86942 s2 freq    -22844 delay   5495

And this is the output with the bug fix in place.

    $ sudo testptp -d /dev/ptp1 -f 62400000
    frequency adjustment okay
    $ sudo ./phc2sys -m -q -c eth6 -s CLOCK_REALTIME -O0
    phc2sys[90365.624]: eth6 sys offset 311912675 s0 freq -62400000 delay   5490
    phc2sys[90366.624]: eth6 sys offset 374292766 s1 freq  -31098 delay   5642
    phc2sys[90367.624]: eth6 sys offset     -3825 s2 freq  -34923 delay   5617
    phc2sys[90368.625]: eth6 sys offset         6 s2 freq  -32240 delay   5564
    phc2sys[90369.625]: eth6 sys offset      1241 s2 freq  -31003 delay   5605
    phc2sys[90370.625]: eth6 sys offset      1131 s2 freq  -30741 delay   5600
    phc2sys[90371.625]: eth6 sys offset       801 s2 freq  -30732 delay   5621
    phc2sys[90372.625]: eth6 sys offset       458 s2 freq  -30834 delay   5640
    phc2sys[90373.626]: eth6 sys offset       186 s2 freq  -30969 delay   5598
    phc2sys[90374.626]: eth6 sys offset       134 s2 freq  -30965 delay   5599
    phc2sys[90375.626]: eth6 sys offset        43 s2 freq  -31016 delay   5595
    phc2sys[90375.681]: eth6 sys offset       -32 s2 freq  -31078 delay   5541

This patch fixes the issue by correcting the calculation of the local
time stamp value.

Fixes: 8142da41b6 ("phc2sys: Use reversed sysoff when synchronizing to system clock.")
Signed-off-by: Richard Cochran <richardcochran@gmail.com>
Reported-by: Cliff Spradlin <cspradlin@google.com>
Tested-by: Vladimir Oltean <olteanv@gmail.com>
2019-12-25 10:26:04 -08:00
Richard Cochran d32758aa09 phc2sys, phc_ctl: Re-factor common code.
The two programs, phc2sys and phc_ctl, use nearly identical subroutines to
open an instance of a dynamic posix clock.  In anticipation of yet a third
program with similar needs, this patch refactors the common code into the
utilities.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
2019-08-25 12:19:36 -07:00
Vedang Patel 3f5f5653d7 port: Add interval update mechanism.
This commit adds functionality to increase the sync and pdelay request
intervals once gptp synchronization has been achieved. This is useful
while running Automotive Profile where the network is usually static.

Here, we are assuming that the gPTP synchronization is achieved whenever
the last 'n' offsets calculated using the Sync messages are below a
certain threshold. Both, the number of offsets to consider and the offset
threshold are configurable.

In order to support the interval update, a new clock servo state
CLOCK_SERVO_STABLE has been added. The clock will transition to this
state whenever the above condition is satisfied. When this transition
occurs, the slave will send an "interval request" signaling message
asking the master to change the sync interval. It will also update the
pdelay request interval locally.

Four new config options have been added to provide this functionality:

- servo_offset_threshold: All the offset values being considered should
  be below this value.
- servo_num_offset_values: number of previously received offset values
  to consider.
- operLogSyncInterval: slave will ask the master to switch to this
  interval for sending sync messages when the offset stabilizes.
- operLogPdelayReqInterval: the slave will set this interval for pdelay
  request messages when the offset stabilizes.

Signed-off-by: Vedang Patel <vedang.patel@intel.com>
2019-05-08 21:29:48 -07:00
Miroslav Lichvar 3c7e4947e4 phc2sys: Fix crash when trying to synchronize unadjustable clock.
A clock that cannot be adjusted has no servo. Check if a servo is
present before updating the clock.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
2019-01-06 20:50:40 -08:00
Miroslav Lichvar 8142da41b6 phc2sys: Use reversed sysoff when synchronizing to system clock.
If synchronizing a PHC to the system clock, use one of the
PTP_SYS_OFFSET ioctls (if supported) to measure the offset between the
two clocks. Negate the offset and switch the timestamp before passing
them to the servo.

This makes the synchronization between PHC and system clock symmetric.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
2019-01-06 20:50:40 -08:00
Miroslav Lichvar 93baf34adb sysoff: Extend API for different sysoff methods.
The kernel supports different PTP_SYS_OFFSET* ioctls. Use the sysoff
enum to allow selecting between them in sysoff_measure().

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
2019-01-06 20:50:40 -08:00
Miroslav Lichvar d08bee2771 Fix -Wformat-overflow warnings.
Two warnings were observed with gcc-8.1.1.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
2018-08-12 13:29:39 -07:00
Michael Walle 8f484e8312 msg: Drop msg->tlv_count.
The field is redundant with the length tlv_list. Replace it with a
function msg_tlv_count() instead. This iterates over the tlv_list. The
computational overhead should be small, because the lists are very short
and the tlv_count is only used in management paths (yet).

Signed-off-by: Michael Walle <michael@walle.cc>
2018-07-21 16:28:07 -07:00
Rafaël Carré d52ef498cf phc2sys: document switch/case fallthrough so that gcc can understand it
Signed-off-by: Rafaël Carré <funman@videolan.org>
2018-06-25 07:24:17 -07:00
Michael Walle 6bc77ccf4b Replace memcmp() with pid_eq() and cid_eq()
Consistenly use the pid_eq() and cid_eq() helper functions. Move the
functions into utils.h (making them inline functions) because they are
used in multiple source files.

Signed-off-by: Michael Walle <michael@walle.cc>
2018-06-21 07:18:24 -07:00
Cliff Spradlin via Linuxptp-devel 4c5d180ab3 phc2sys: don't over-control a destination clock
If ptp4l is running as a boundary clock, and multiple master ports
share a hardware clock that is different than the slave port's
hardware clock, then phc2sys in autocfg mode will synchronize that
clock multiple times per sync loop, once per-port.

Example output when this happens:
CLOCK_REALTIME phc offset        -9 s2 freq    -355 delay   3635
eth9 phc offset         9 s2 freq   +1762 delay   7488
eth8 phc offset        41 s2 freq   +1985 delay   7424
eth7 phc offset        15 s2 freq   +1952 delay   7440
eth6 phc offset        20 s2 freq   +1059 delay   7440
eth1 phc offset        22 s2 freq   +1783 delay   7424
eth12.400 phc offset        15 s2 freq -4207943 delay   7551
eth12.300 phc offset       388 s2 freq +2336645 delay   7536
eth12.200 phc offset       451 s2 freq +2516553 delay   7438
eth12.100 phc offset       268 s2 freq  +64274 delay   7405

This is easily reproducible by adding VLANs to a single network
interface and configuring ptp4l to use those VLAN interfaces as PTP
ports.

This patch prevents this problem by introducing a new deduplicated
destination clock list to phc2sys. Each time phc2sys reconfigures,
the destination clock list is re-populated. In the synchronization
loop, phc2sys will only use the clocks in this list.

Example output of reconfiguration with this patch:
reconfiguring after port state change
selecting eth9 for synchronization
selecting eth8 for synchronization
selecting eth7 for synchronization
selecting eth6 for synchronization
selecting eth1 for synchronization
selecting eth13.400 for synchronization
skipping eth13.300: eth13.400 has the same clock and is already selected
skipping eth13.200: eth13.400 has the same clock and is already selected
skipping eth13.100: eth13.400 has the same clock and is already selected
selecting eth12.400 for synchronization
skipping eth12.300: eth12.400 has the same clock and is already selected
skipping eth12.200: eth12.400 has the same clock and is already selected
skipping eth12.100: eth12.400 has the same clock and is already selected
skipping eth13.500: eth13.400 has the same clock and is already selected

This patch was tested with autocfg mode and also when specifying source
and destination manually via commandline.

Signed-off-by: Cliff Spradlin <cspradlin@google.com>
2018-06-18 11:01:56 -07:00
Miroslav Lichvar a335ac556a phc2sys: fix PPS mode with no source clock.
When using a PPS source with no source clock specified with the -s
option, avoid referencing a NULL pointer and using CLOCK_INVALID in
system calls.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
2018-04-19 20:08:04 -07:00
Miroslav Lichvar 966eff0161 phc2sys: fix race between signals and port initialization.
Check if a signal was received when waiting for ptp4l in the automatic
mode to not get stuck in an infinite loop.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
2018-04-19 07:41:11 -07:00
Peter Schneider 79d290e222 phc2sys.c: Implement using transportSpecific
Signed-off-by: Peter Schneider <peter@psch.de>
2018-04-18 14:27:40 -07:00
Peter Schneider c93045b37b phc2sys.c: Implement handling of Long options
Signed-off-by: Peter Schneider <peter@psch.de>
2018-04-18 14:27:40 -07:00
Peter Schneider c70f9b3e07 phc2sys.c: Implement configuration file handling
Signed-off-by: Peter Schneider <peter@psch.de>
2018-04-18 14:27:40 -07:00
Peter Schneider 26308c219a phc2sys.c: Sync variable 'node.sanity_freq_limit with cfg item
Copy 'node.sanity_freq_limit' to struct config item 'sanity_freq_limit'.
Check for 'servo_type' == CLOCK_SERVO_NTPSHM.

Signed-off-by: Peter Schneider <peter@psch.de>
2018-04-18 14:27:40 -07:00
Peter Schneider c22864a807 phc2sys.c: Sync variable 'node.kernel_leap with cfg item
Use struct config item 'kernel_leap' for parameter handling. Copy this to
node.kernel_leap afterwards. Check for 'servo_type' == CLOCK_SERVO_NTPSHM.

Signed-off-by: Peter Schneider <peter@psch.de>
2018-04-18 14:27:40 -07:00
Peter Schneider 41fa8e9d32 phc2sys.c: Sync variable 'node.clock_servo with cfg item
Use struct config item 'clock_servo' for parameter handling. Copy this to
node.servo_type afterwards.

Signed-off-by: Peter Schneider <peter@psch.de>
2018-04-18 14:27:40 -07:00
Peter Schneider cfbc8bdc18 phc2sys.c: Replace usage of variable 'use_syslog'
Replaced usage of variable 'use_syslog' with struct config item
'use_syslog'. Original variable removed.

Signed-off-by: Peter Schneider <peter@psch.de>
2018-04-18 14:27:40 -07:00
Peter Schneider f7274b1e84 phc2sys.c: Replace variable 'verbose'
Replaced usage of variable 'verbose' with struct config item 'verbose'.
Original variable removed.

Signed-off-by: Peter Schneider <peter@psch.de>
2018-04-18 14:27:40 -07:00
Peter Schneider 97ff23cc07 phc2sys.c: Replace variable 'message_tag'
Replaced usage of variable 'message_tag' with struct config item
'message_tag'. Original variable removed.

Signed-off-by: Peter Schneider <peter@psch.de>
2018-04-18 14:27:40 -07:00
Peter Schneider 7986e10302 phc2sys.c: Replace variable 'print_level'
Replaced usage of variable 'print_level' with struct config item
'logging_level'. Original variable still used for range checking.

Signed-off-by: Peter Schneider <peter@psch.de>
2018-04-18 14:27:39 -07:00
Peter Schneider e8d01515cf phc2sys.c: Replace variable 'domain_number
Replaced usage of variable 'domain_number' with struct config item
'domainNumber'. Original variable still used for range checking. Removed
(now obsolete) third parameter of init_pmc().

Signed-off-by: Peter Schneider <peter@psch.de>
2018-04-18 14:27:39 -07:00
Richard Cochran 4e35931bc8 phc2sys: Clean up before exiting.
Valgrind complains about memory leaks, none of which are serious.  This
patch frees the allocations on exit, so that any future *real* memory leaks
will be obvious in the valgrind report.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
2018-03-31 11:32:55 -07:00
Hangbin Liu 8a349e557c phc2sys: re-create clock clkid and servo when phc index changed
When the clock device or phc index changed, the new phc device may have
different maximum adjustment. So we need to create a new clkid and servo
in clock_reinit().

At the same time, we should not only do clock_reinit() when the new state
is PS_MASTER. We also need to reinit clock after a failover in slave mode(
the new state is PS_SLAVE). We can do clock_reinit() even in PS_FAULTY so
we can finish adjust offset before come back to PS_LISTENING. So I removed
the check and let's do clock_reinit() whenever there is a new state.

And for servo reset, as Miroslav suggested, we will do it in these cases:
- the system clock is the new destination (master state)
- a PHC is the new destination (master state)
- a PHC is switched (in any state)

Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
2017-10-14 05:34:18 -07:00
Hangbin Liu cb53238d5d phc2sys: split servo_add from function clock_add
We also need this part in clock_reinit() later. So split it out of
function clock_add().

Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
2017-10-14 05:34:18 -07:00
Miroslav Lichvar 17c9787b1d phc2sys: fix handling of multiple state changes.
When the master clock changed its state and then changed it back to the
original state before phc2sys could process the first change, e.g. SLAVE
-> UNCALIBRATED -> SLAVE after a clockcheck failure, the second change
was ignored because the new value was the same as the original state,
which wasn't updated for the first change yet. This caused phc2sys to be
stuck with a wrong state.

Fix phc2sys to check both the state and new_state variables of the clock.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
2017-09-01 15:44:59 +02:00
Hangbin Liu 46cc187e82 phc2sys: free clock device when exit
Since we called strdup() for clock device, we need free the memory
before exit.

Signed-off-by: Hangbin Liu <liuhangbin@gmail.com>
2017-06-23 07:31:43 +02:00
Cliff Spradlin via Linuxptp-devel 6b18e12b9b phc2sys: Add described device to stats output
When phc2sys synced more than one ethernet interface, such as in a JBOD
boundary clock arrangement, it was unclear which interface a line of statistics
referred to.

Previous output:

phc2sys[583.098]: sys offset       -32 s2 freq +131974 delay   3871
phc2sys[583.098]: sys offset        33 s2 freq +125746 delay   4095
phc2sys[583.098]: sys offset       -73 s2 freq +125720 delay   4128
phc2sys[583.098]: sys offset       -19 s2 freq +133265 delay   3888
phc2sys[583.098]: sys offset         2 s2 freq +131986 delay   3920

Now:

phc2sys[583.098]: eth9 sys offset       -32 s2 freq +131974 delay   3871
phc2sys[583.098]: eth8 sys offset        33 s2 freq +125746 delay   4095
phc2sys[583.098]: eth7 sys offset       -73 s2 freq +125720 delay   4128
phc2sys[583.098]: eth6 sys offset       -19 s2 freq +133265 delay   3888
phc2sys[583.098]: eth1 sys offset         2 s2 freq +131986 delay   3920

Signed-off-by: Cliff Spradlin <cspradlin@google.com>
2017-06-19 08:14:40 +02:00
Miroslav Lichvar ac92a71161 phc2sys: don't synchronize clock to itself.
When ptp4l is using multiple interfaces sharing the same clock, phc2sys
in the automatic mode should not try to synchronize them to each other.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
Reported-by: Stefan Lange <s.lange@gateware.de>
2017-03-29 13:10:52 +02:00
Miroslav Lichvar 0f6c6972c7 Add options to tag ptp4l and phc2sys log messages.
When running multiple instances of ptp4l or phc2sys, it's difficult to
tell which log message belongs to which instance. Add new options to
ptp4l and phc2sys which can specify a tag for all messages printed to
the standard output or system log, so messages from different instances
can have different tags.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
2017-02-05 16:44:48 +01:00
Richard Cochran 7b438defe5 config: introduce a proper creation method.
Now that all of the legacy, open coded configuration fields are gone,
we can follow a normal create/destroy pattern for the configuration.
This patch add the new method and converts the programs to use it.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
2015-08-23 21:48:09 +02:00
Richard Cochran e27b036d09 config: convert 'uds_address' to the new scheme.
Signed-off-by: Richard Cochran <richardcochran@gmail.com>
2015-08-23 21:09:46 +02:00
Richard Cochran 4c9c447429 config: convert 'ntpshm_segment' to the new scheme.
Signed-off-by: Richard Cochran <richardcochran@gmail.com>
2015-08-23 21:09:45 +02:00
Richard Cochran 07b229907d config: convert 'pi_integral_const' to the new scheme.
Signed-off-by: Richard Cochran <richardcochran@gmail.com>
2015-08-23 21:09:45 +02:00
Richard Cochran 25c903b0d2 config: convert 'pi_proportional_const' to the new scheme.
Signed-off-by: Richard Cochran <richardcochran@gmail.com>
2015-08-23 21:09:45 +02:00
Richard Cochran 4f649b1ec4 config: convert the 'first_step_threshold' option to the new scheme.
Signed-off-by: Richard Cochran <richardcochran@gmail.com>
2015-08-23 21:09:45 +02:00
Richard Cochran df578135d3 config: convert the 'step_threshold' option to the new scheme.
Signed-off-by: Richard Cochran <richardcochran@gmail.com>
2015-08-23 21:09:45 +02:00
Richard Cochran bde8ef4360 servo: store the configuration in the servo data structure.
This will allow removing the code that passes configuration options via
global variables.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
2015-08-23 21:09:45 +02:00
Richard Cochran e7dbc068f6 config: introduce a new API for reading configuration settings.
This patch introduces generic code for adding and parsing new options.
The public 'get' methods return option values directly.  Although the
API is easy to use, it does not provide error checking in case the
option does not exist or if there is a type mismatch.

Therefore the code performs a BIST to ensure that the options are
properly populated.  In addition, the code terminates the program in
case of missing options or type mismatches.  This heavy handed
approach is meant to catch errors during development and should never
trigger during normal usage.

As a first element, we include an option for specifying the UDP TTL.

Users are required to call 'config_init', and so this patch add that into
all three programs, ptp4l, phc2sys and pmc.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
2015-08-23 21:09:19 +02:00
Richard Cochran bbbc0cd451 pmc: require a configuration for creating a PMC instance.
In the near future, the transports will need to consult the configuration
database in order to obtain various options.  This patch also introduces
the idea of a configuration file into the phc2sys and pmc programs.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
2015-08-16 11:20:23 +02:00
Miroslav Lichvar f0b0c1116a servo: add support for weighted samples.
Add weight parameter to the sample function. Samples with smaller weight
are less reliable, they can be ignored by the servo or the adjustments
of the clock can be smaller.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
2015-03-27 10:58:04 +01:00
Miroslav Lichvar 09e5e6e934 phc2sys: don't synchronize clock to itself in automatic mode.
When no source is found in the automatic mode and a clock is selected as
the default source, set its state temporarily to slave to prevent the
clock from being synchronized to itself and drifting quickly away.

Also, don't use this mode with only one PTP clock and don't include the
system clock.

This fixes phc2sys with SHM servo (e.g. used by timemaster).
2015-02-11 17:26:07 +01:00
Richard Cochran 2ab2fbbdda phc2sys: default to the first clock in automatic mode.
If we have clocks to synchronize but no source, just pick the last one in
the list, which is the first one from the ptp4l command line.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
2014-12-18 11:38:00 +01:00
Richard Cochran e1ea4ea810 phc2sys: automatic mode: synchronize all non-slave ports.
When running a "jbod" Boundary Clock, as long as we have one slaved port,
we always want the clocks on the other ports to be synchronized, regardless
of their port state.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
Acked-by: Jiri Benc <jbenc@redhat.com>
2014-12-18 11:38:00 +01:00
Jiri Benc 7455c24148 phc2sys: fix overwriting of the clock state
The reconfigure function is missing a check whether state for the given
clock actually changed or not. This caused state for all unchanged ports to
be zeroed.

Reported-by: Richard Cochran <richardcochran@gmail.com>
Signed-off-by: Jiri Benc <jbenc@redhat.com>
2014-11-18 15:58:25 +01:00