Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

subsys: net: lib: Implement Precision Time Protocol (PTP) Support #73713

Merged
merged 36 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8a42d4e
net: if: Increment the packet ref count when adding to timestamp queue
awojasinski May 8, 2024
c140f0f
net: pkt: Add flags to indicate the pkt should be timestamped
awojasinski May 13, 2024
61f1c25
net: context: Add support for timestamping in network context
awojasinski May 13, 2024
5049962
net: socket: Add support for SO_TIMESTAMPING socket level option
awojasinski May 13, 2024
84609c8
drivers: eth: eth_stm32_hal: Add support for rx/tx timestamping
awojasinski May 13, 2024
270b9a7
net: socket: Add SO_TIMESTAMPING data to messages received via socket
awojasinski May 19, 2024
9c278b5
net: ptp: Initial PTP library implementation
awojasinski May 19, 2024
0237a1c
net: ptp: Add derived data type header
awojasinski Mar 27, 2024
f1cc094
net: ptp: Add finite state machine implementation for PTP stack
awojasinski Mar 27, 2024
defb6cf
net: ptp: Add definitions for PTP datasets
awojasinski May 19, 2024
13c55b8
net: ptp: Add PTP Clock and PTP Port initialization
awojasinski May 20, 2024
fecb408
net: ptp: Add TIME_RECEIVER_ONLY feature
awojasinski Mar 27, 2024
83fbb1a
net: ptp: Add Kconfig symbols and preprocessor guards for some states
awojasinski Mar 27, 2024
ca93316
net: ptp: Add definition of PTP messages types
awojasinski May 20, 2024
97d6be1
net: ptp: Add PTP message allocation and management
awojasinski May 20, 2024
f13b878
net: ptp: Add byte order manipulation for PTP messages
awojasinski May 20, 2024
717aa55
net: ptp: Add TLV extension to PTP
awojasinski May 20, 2024
e948609
net: ptp: Extend TLV with Management TLVs
awojasinski May 20, 2024
cd8cd5f
net: ptp: Add sockets to PTP stack
awojasinski May 20, 2024
75888da
net: ptp: Add PTP Port ID comparison function
awojasinski May 20, 2024
77475f3
net: ptp: Add foreign timeTransmitter records
awojasinski May 20, 2024
e449027
net: ptp: Introduce routines for PTP Hardware Clock adjustment
awojasinski May 23, 2024
268cef8
net: ptp: Implement PTP message processing for ports
awojasinski May 20, 2024
a60e1c3
net: ptp: Implement Best TimeTransmitter Clock algorithm for PTP stack
awojasinski Mar 27, 2024
f304600
net: ptp: Add management message processing
awojasinski May 21, 2024
f6ae8b9
net: ptp: Add functions for messages transmission
awojasinski May 21, 2024
5c022e6
net: ptp: Add timers processing for PTP Port
awojasinski May 21, 2024
2e450f6
net: ptp: Implement PTP Port enabling and disabling functions
awojasinski May 21, 2024
993d803
net: ptp: Add event generation and event handler for PTP Port
awojasinski May 21, 2024
ecb5f61
net: ptp: Add handler for STATE_DECISION_EVENT
awojasinski May 21, 2024
dc2c9bd
net: ptp: Implement PTP thread
awojasinski May 22, 2024
92e9a28
net: ptp: Network interface status check in PTP thread
awojasinski May 22, 2024
f8738fd
doc: net: ptp: Add PTP library documentation
awojasinski May 22, 2024
dc483f7
drivers: eth: native: Align to support ptp_clock driver for PTP subsys
awojasinski Jun 3, 2024
5801133
samples: net: ptp: Add sample for PTP
awojasinski May 28, 2024
d6c510f
MAINTAINERS: ptp: Add PTP entry to the maintenance list
awojasinski Jun 4, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions MAINTAINERS.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2587,9 +2587,11 @@ Networking:
files-exclude:
- doc/connectivity/networking/api/gptp.rst
- doc/connectivity/networking/api/ieee802154.rst
- doc/connectivity/networking/api/ptp.rst
- doc/connectivity/networking/api/wifi.rst
- include/zephyr/net/gptp.h
- include/zephyr/net/ieee802154*.h
- include/zephyr/net/ptp.h
- include/zephyr/net/wifi*.h
- include/zephyr/net/buf.h
- include/zephyr/net/dhcpv4*.h
Expand All @@ -2605,6 +2607,7 @@ Networking:
- subsys/net/lib/coap/
- subsys/net/lib/config/ieee802154*
- subsys/net/lib/lwm2m/
- subsys/net/lib/ptp/
- subsys/net/lib/tls_credentials/
- subsys/net/lib/dhcpv4/
- tests/net/dhcpv4/
Expand Down Expand Up @@ -2760,6 +2763,20 @@ Networking:
tests:
- net.mqtt_sn

"Networking: PTP":
status: maintained
maintainers:
- awojasinski
files:
- doc/connectivity/networking/api/ptp.rst
- include/zephyr/net/ptp.h
- subsys/net/lib/ptp/
- samples/net/ptp/
labels:
- "area: Networking"
tests:
- sample.net.ptp

"Networking: Native IEEE 802.15.4":
status: maintained
maintainers:
Expand Down
1 change: 1 addition & 0 deletions boards/st/nucleo_h563zi/nucleo_h563zi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ supported:
- adc
- dac
- pwm
- netif:eth
- counter
- spi
- usb_device
Expand Down
1 change: 1 addition & 0 deletions doc/connectivity/networking/api/protocols.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ Protocols
lwm2m
mqtt
mqtt_sn
ptp
tftp
140 changes: 140 additions & 0 deletions doc/connectivity/networking/api/ptp.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
.. _ptp_interface:

Precision Time Protocol (PTP)
#############################

.. contents::
:local:
:depth: 2

Overview
********

PTP is a network protocol implemented in the application layer, used to synchronize
clocks in a computer network. It's accurate up to less than a microsecond.
The stack supports the protocol and procedures as defined in the `IEEE 1588-2019 standard`_
(IEEE Standard for a Precision Clock Synchronization Protocol
for Networked Measurement and Control Systems). It has multiple profiles,
and can be implemented on top of L2 (Ethernet) or L3 (UDP/IPv4 or UDP/IPv6).
Its accuracy is achieved by using hardware timestamping of the protocol packets.

Zephyr's implementation of PTP stack consist following items:

* PTP stack thread that handles incoming messages and events
* Integration with ptp_clock driver
* PTP stack initialization executed during system init

The implementation automatically creates PTP Ports (each PTP Port coresponds to unique interface).

Supported features
******************

Implementation of the stack doesn't support all features specified in the standard.
In the table below all supported features are listed.

.. csv-table:: Supported features
:header: Feature, Supported
:widths: 50,10

Ordinary Clock, yes
Boundary Clock, yes
Transparent Clock,
Management Node,
End to end delay mechanism, yes
Peer to peer delay mechanism,
Multicast operation mode,
Hybrid operation mode,
Unicast operation mode,
Non-volatile storage,
UDP IPv4 transport protocol, yes
UDP IPv6 transport protocol, yes
IEEE 802.3 (Ethernet) transport protocol,
Hardware timestamping, yes
Software timestamping,
TIME_RECEIVER_ONLY PTP Instance, yes
TIME_TRANSMITTER_ONLY PTP Instance,

Supported Management messages
*****************************

Based on Table 59 from section 15.5.2.3 of the IEEE 1588-2019 following management TLVs
are supported:

.. csv-table:: Supported management message's IDs
:header: Management_ID, Management_ID name, Allowed acctions
:widths: 10,40,25

0x0000, NULL_PTP_MANAGEMENT, GET SET COMMAND
0x0001, CLOCK_DESCRIPTION, GET
0x0002, USER_DESCRIPTION, GET
0x0003, SAVE_IN_NON_VOLATILE_STORAGE, -
0x0004, RESET_NON_VOLATILE_STORAGE, -
0x0005, INITIALIZE, -
0x0006, FAULT_LOG, -
0x0007, FAULT_LOG_RESET, -
0x2000, DEFAULT_DATA_SET, GET
0x2001, CURRENT_DATA_SET, GET
0x2002, PARENT_DATA_SET, GET
0x2003, TIME_PROPERTIES_DATA_SET, GET
0x2004, PORT_DATA_SET, GET
0x2005, PRIORITY1, GET SET
0x2006, PRIORITY2, GET SET
0x2007, DOMAIN, GET SET
0x2008, TIME_RECEIVER_ONLY, GET SET
0x2009, LOG_ANNOUNCE_INTERVAL, GET SET
0x200A, ANNOUNCE_RECEIPT_TIMEOUT, GET SET
0x200B, LOG_SYNC_INTERVAL, GET SET
0x200C, VERSION_NUMBER, GET SET
0x200D, ENABLE_PORT, COMMAND
0x200E, DISABLE_PORT, COMMAND
0x200F, TIME, GET SET
0x2010, CLOCK_ACCURACY, GET SET
0x2011, UTC_PROPERTIES, GET SET
0x2012, TRACEBILITY_PROPERTIES, GET SET
0x2013, TIMESCALE_PROPERTIES, GET SET
0x2014, UNICAST_NEGOTIATION_ENABLE, -
0x2015, PATH_TRACE_LIST, -
0x2016, PATH_TRACE_ENABLE, -
0x2017, GRANDMASTER_CLUSTER_TABLE, -
0x2018, UNICAST_TIME_TRANSMITTER_TABLE, -
0x2019, UNICAST_TIME_TRANSMITTER_MAX_TABLE_SIZE, -
0x201A, ACCEPTABLE_TIME_TRANSMITTER_TABLE, -
0x201B, ACCEPTABLE_TIME_TRANSMITTER_TABLE_ENABLED, -
0x201C, ACCEPTABLE_TIME_TRANSMITTER_MAX_TABLE_SIZE, -
0x201D, ALTERNATE_TIME_TRANSMITTER, -
0x201E, ALTERNATE_TIME_OFFSET_ENABLE, -
0x201F, ALTERNATE_TIME_OFFSET_NAME, -
0x2020, ALTERNATE_TIME_OFFSET_MAX_KEY, -
0x2021, ALTERNATE_TIME_OFFSET_PROPERTIES, -
0x3000, EXTERNAL_PORT_CONFIGURATION_ENABLED,
0x3001, TIME_TRANSMITTER_ONLY, -
0x3002, HOLDOVER_UPGRADE_ENABLE, -
0x3003, EXT_PORT_CONFIG_PORT_DATA_SET, -
0x4000, TRANSPARENT_CLOCK_DEFAULT_DATA_SET, -
0x4001, TRANSPARENT_CLOCK_PORT_DATA_SET, -
0x4002, PRIMARY_DOMAIN, -
0x6000, DELAY_MECHANISM, GET
0x6001, LOG_MIN_PDELAY_REQ_INTERVAL, GET SET

Enabling the stack
******************

The following configuration option must me enabled in :file:`prj.conf` file.

- :kconfig:option:`CONFIG_PTP`

Testing
*******

The stack has been informally tested using the
`Linux ptp4l <http://linuxptp.sourceforge.net/>`_ daemons.
The :zephyr:code-sample:`PTP sample application <ptp>` from the Zephyr
source distribution can be used for testing.

.. _IEEE 1588-2019 standard:
https://standards.ieee.org/ieee/1588/6825/

API Reference
*************

.. doxygengroup:: ptp
1 change: 1 addition & 0 deletions doc/connectivity/networking/api/tsn.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ Time Sensitive Networking
gptp.rst
net_time.rst
ptp_time.rst
ptp.rst
7 changes: 4 additions & 3 deletions drivers/ethernet/Kconfig.native_posix
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ if ETH_NATIVE_POSIX
config ETH_NATIVE_POSIX_INTERFACE_COUNT
int "Number of network interfaces created"
default NET_GPTP_NUM_PORTS if NET_GPTP
default PTP_NUM_PORTS if PTP
default 1
range 1 32
help
By default only one network interface is created. It is possible
to create multiple interfaces in certain use cases. For example if
multiple ports are defined in gPTP, then multiple network interfaces
multiple ports are defined in gPTP or PTP, then multiple network interfaces
must be created here.

config ETH_NATIVE_POSIX_DRV_NAME
Expand All @@ -40,9 +41,9 @@ config ETH_NATIVE_POSIX_DEV_NAME

config ETH_NATIVE_POSIX_PTP_CLOCK
bool "PTP clock driver support"
default y if NET_GPTP
default y if NET_GPTP || PTP
select PTP_CLOCK
depends on NET_GPTP
depends on NET_GPTP || PTP
help
Enable PTP clock support.

Expand Down
14 changes: 6 additions & 8 deletions drivers/ethernet/eth_stm32_hal.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ static int eth_tx(const struct device *dev, struct net_pkt *pkt)
#endif /* CONFIG_ETH_STM32_HAL_API_V2 */

#if defined(CONFIG_PTP_CLOCK_STM32_HAL)
timestamped_frame = eth_is_ptp_pkt(net_pkt_iface(pkt), pkt);
timestamped_frame = eth_is_ptp_pkt(net_pkt_iface(pkt), pkt) ||
net_pkt_is_tx_timestamping(pkt);
if (timestamped_frame) {
/* Enable transmit timestamp */
#if defined(CONFIG_ETH_STM32_HAL_API_V2)
Expand Down Expand Up @@ -789,13 +790,10 @@ static struct net_pkt *eth_rx(const struct device *dev)
}

#if defined(CONFIG_PTP_CLOCK_STM32_HAL)
if (eth_is_ptp_pkt(get_iface(dev_data), pkt)) {
pkt->timestamp.second = timestamp.second;
pkt->timestamp.nanosecond = timestamp.nanosecond;
} else {
/* Invalid value */
pkt->timestamp.second = UINT64_MAX;
pkt->timestamp.nanosecond = UINT32_MAX;
pkt->timestamp.second = timestamp.second;
pkt->timestamp.nanosecond = timestamp.nanosecond;
if (timestamp.second != UINT64_MAX) {
net_pkt_set_rx_timestamping(pkt, true);
}
#endif /* CONFIG_PTP_CLOCK_STM32_HAL */

Expand Down
5 changes: 5 additions & 0 deletions include/zephyr/net/net_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,10 @@ __net_socket struct net_context {
* see RFC 5014 for details.
*/
uint16_t addr_preferences;
#endif
#if defined(CONFIG_NET_CONTEXT_TIMESTAMPING)
/** Enable RX, TX or both timestamps of packets send through sockets. */
uint8_t timestamping;
#endif
} options;

Expand Down Expand Up @@ -1285,6 +1289,7 @@ enum net_context_option {
NET_OPT_UNICAST_HOP_LIMIT = 15, /**< IPv6 unicast hop limit */
NET_OPT_TTL = 16, /**< IPv4 unicast TTL */
NET_OPT_ADDR_PREFERENCES = 17, /**< IPv6 address preference */
NET_OPT_TIMESTAMPING = 18, /**< Packet timestamping */
};

/**
Expand Down
36 changes: 36 additions & 0 deletions include/zephyr/net/net_pkt.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ struct net_pkt {
*/
#if defined(CONFIG_NET_IP_FRAGMENT)
uint8_t ip_reassembled : 1; /* Packet is a reassembled IP packet. */
#endif
#if defined(CONFIG_NET_PKT_TIMESTAMP)
uint8_t tx_timestamping : 1; /** Timestamp transmitted packet */
uint8_t rx_timestamping : 1; /** Timestamp received packet */
#endif
/* bitfield byte alignment boundary */

Expand Down Expand Up @@ -389,6 +393,38 @@ static inline void net_pkt_set_ptp(struct net_pkt *pkt, bool is_ptp)
pkt->ptp_pkt = is_ptp;
}

static inline bool net_pkt_is_tx_timestamping(struct net_pkt *pkt)
{
#if defined(CONFIG_NET_PKT_TIMESTAMP)
return !!(pkt->tx_timestamping);
#else
return false;
#endif
}

static inline void net_pkt_set_tx_timestamping(struct net_pkt *pkt, bool is_timestamping)
{
#if defined(CONFIG_NET_PKT_TIMESTAMP)
pkt->tx_timestamping = is_timestamping;
#endif
}

static inline bool net_pkt_is_rx_timestamping(struct net_pkt *pkt)
{
#if defined(CONFIG_NET_PKT_TIMESTAMP)
return !!(pkt->rx_timestamping);
#else
return false;
#endif
}

static inline void net_pkt_set_rx_timestamping(struct net_pkt *pkt, bool is_timestamping)
{
#if defined(CONFIG_NET_PKT_TIMESTAMP)
pkt->rx_timestamping = is_timestamping;
#endif
}

static inline bool net_pkt_is_captured(struct net_pkt *pkt)
{
return !!(pkt->captured);
Expand Down
43 changes: 43 additions & 0 deletions include/zephyr/net/ptp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2024 BayLibre SAS
*
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @file
* @brief Public functions for the Precision Time Protocol.
*
* References are to version 2019 of IEEE 1588, ("PTP")
*/

#ifndef ZEPHYR_INCLUDE_NET_PTP_H_
#define ZEPHYR_INCLUDE_NET_PTP_H_

/**
* @brief Precision Time Protocol (PTP) support
* @defgroup ptp PTP support
* @ingroup networking
* @{
*/

#include <zephyr/net/ptp_time.h>

#ifdef __cplusplus
extern "C" {
#endif

#define PTP_MAJOR_VERSION 2 /**< Major PTP Version */
#define PTP_MINOR_VERSION 1 /**< Minor PTP Version */

#define PTP_VERSION (PTP_MINOR_VERSION << 4 | PTP_MAJOR_VERSION) /**< PTP version IEEE-1588:2019 */

#ifdef __cplusplus
}
#endif

/**
* @}
*/

#endif /* ZEPHYR_INCLUDE_NET_PTP_H_ */
15 changes: 14 additions & 1 deletion include/zephyr/net/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -1163,8 +1163,9 @@ struct ifreq {
/** Socket accepts incoming connections (ignored, for compatibility) */
#define SO_ACCEPTCONN 30

/** Timestamp TX packets */
/** Timestamp TX RX or both packets. Supports multiple timestamp sources. */
#define SO_TIMESTAMPING 37

/** Protocol used with the socket */
#define SO_PROTOCOL 38

Expand All @@ -1179,6 +1180,18 @@ struct ifreq {
/** Socket TX time (same as SO_TXTIME) */
#define SCM_TXTIME SO_TXTIME

/** Timestamp generation flags */

/** Request RX timestamps generated by network adapter. */
#define SOF_TIMESTAMPING_RX_HARDWARE BIT(0)
/**
* Request TX timestamps generated by network adapter.
* This can be enabled via socket option or control messages.
*/
#define SOF_TIMESTAMPING_TX_HARDWARE BIT(1)

/** */

/** @} */

/**
Expand Down
Loading
Loading