A network timestamp control system and its control method
By introducing a packet extension module and a timestamp control module into SylixOS's LWIP, the problem of SylixOS's inability to obtain end-to-end timestamps was solved, enabling the acquisition and transmission of accurate timestamps, supporting time-sensitive services, reducing porting costs, and maintaining lightweight characteristics.
Patent Information
- Application Number
- CN202611367595.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-04
- Publication Date
- 2026-11-10
- Estimated Expiration
- 2046-09-04
AI Technical Summary
SylixOS's LWIP packet structure lacks a timestamp field, making it impossible to carry precise time information as packets flow up or down the protocol stack. Furthermore, it lacks socket timestamp interfaces and hardware timestamp configuration interfaces, resulting in the application layer being unable to obtain end-to-end send and receive timestamps. This affects the development of time-sensitive services such as PTP/1588, TSN synchronization, and 5G bearer.
A network timestamp control system is provided, including a data packet extension module, a timestamp semantic mark unification module, a timestamp acquisition and configuration module, and a timestamp receiving/sending module. By extending the data structure and managing the timestamp through an object pool, and introducing compatible socket options and control messages, the system realizes the acquisition and transmission of hardware and software timestamps, forming a timestamp data path from hardware to the socket layer.
It enables the acquisition of accurate network timestamps on SylixOS, supports time-sensitive services such as PTP/1588, TSN, industrial real-time control and 5G fronthaul, reduces porting costs, maintains the lightweight characteristics and resource efficiency of LWIP, and is suitable for embedded real-time systems.
Smart Images

Figure CN122870323B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the technical field of network timestamps, and specifically to a network timestamp control system and its control method. Background Technology
[0002] With the rapid development of scenarios such as the Industrial Internet, Time-Sensitive Networking (TSN), Precision Time Protocol (PTP / IEEE 1588), Automotive Ethernet, 5G fronthaul, and high-precision measurement, more and more real-time applications are placing stringent requirements on the time stamping of network packets during transmission and reception. The application layer needs to obtain the precise time when a packet is received by the protocol stack, sent by the driver, and timestamped by the hardware (MAC / PHY) for clock synchronization, link latency measurement, latency jitter assessment, and fault location.
[0003] In general-purpose Linux systems, the kernel provides three types of socket options: second-microsecond socket option SO_TIMESTAMP, second-nanosecond socket option SO_TIMESTAMPNS, and bitmask socket option SO_TIMESTAMPING. These options allow user space to receive auxiliary data control messages (cmsg) returned with packets via the message receive interface recvmsg(). The send timestamp is typically read asynchronously from the socket error queue by setting the socket error flag MSG_ERRQUEUE in the message receive interface recvmsg(). The network interface card (NIC) hardware timestamp function can be configured using the hardware timestamp configuration command SIOCSHWTSTAMP in the network device input / output control ioctl command, and queried using the hardware timestamp query command SIOCGHWTSTAMP. The implementation of these interfaces heavily relies on the Linux kernel's skb data structure, error queue mechanism, socket cache hooks, and the ethtool (Linux commands for querying and configuring network interface parameters) ecosystem.
[0004] SylixOS is an independently developed and controllable operating system for embedded and real-time applications. Its network protocol stack is based on a trimmed and enhanced version of LWIP (Lightweight IP). Native LWIP does not support the aforementioned network timestamp semantics: its core data structure, pbuf packets, lacks a field for storing timestamps; the API layer and socket layer lack processing logic for second / microsecond socket options SO_TIMESTAMP, second / nanosecond socket options SO_TIMESTAMPNS, and bitmask socket options SO_TIMESTAMPING; there is no complete path between the driver layer and the protocol stack for reading timestamps from the hardware MAC / PHY and sending them to the application layer; asynchronous timestamp transmission is also not supported. Therefore, when using SylixOS for time-sensitive service development such as PTP (Precision Time Protocol), TSN (Time-Sensitive Networking), industrial control, and 5G transport, applications cannot obtain precise timestamps of sent and received packets through the socket timestamp interface as they can in Linux.
[0005] Therefore, the defects and shortcomings of the existing technology are as follows: 1) The native LWIP pbuf packet structure does not have a timestamp field, which makes it impossible to carry accurate time information as the packet flows up or down the protocol stack, resulting in the inability to obtain the end-to-end transmission and reception time at the application layer.
[0006] 2) SylixOS's LWIP does not implement three types of socket options: second-microsecond socket option, second-nanosecond socket option, and bitmask socket option, as well as the corresponding second-microsecond control message, second-nanosecond control message, and bitmask control message. The application layer cannot obtain the receiving timestamp through the message receiving interface, nor can it notify the protocol stack to collect the sending timestamp of a certain message.
[0007] 3) There is a lack of a unified data channel for reading the raw timestamp from the network card MAC / PHY hardware timestamp register and pushing it up to the socket layer. There is also a lack of hardware timestamp configuration interfaces such as hardware timestamp configuration commands and hardware timestamp query commands. There is no standardized hardware timestamp agreement between the driver and the protocol stack.
[0008] 4) The sending timestamp is inherently asynchronous (it can only be obtained after the message is actually sent by the driver or hardware), and the original LWIP does not have a corresponding asynchronous return mechanism, so it is impossible to send the sending timestamp back to the application layer without introducing socket error flags.
[0009] 5) Directly porting the Linux kernel code would require introducing a large number of complex mechanisms such as skb and error queues, which would significantly destroy the lightweight nature of LWIP and would not meet the resource and deterministic requirements of SylixOS embedded real-time scenarios.
[0010] 6) Due to the lack of the above capabilities, applications based on SylixOS such as PTP / 1588, TSN synchronization, time-sensitive control, and delay measurement cannot adopt the socket timestamp programming model similar to existing Linux applications. The application layer needs to modify or even bypass the protocol stack, resulting in poor portability and high development costs.
[0011] Therefore, there is an urgent need to provide a new solution to address the defects and shortcomings of the existing technologies. Summary of the Invention
[0012] To address the shortcomings and deficiencies in existing technologies, this invention provides a network timestamp control system and its control method.
[0013] The specific solution provided by this invention is as follows: A network timestamp control system, characterized in that: the system comprises: A data packet extension module, wherein the data packet extension module configures an extended data structure for the data packet, and the extended data structure internally stores various timestamps and timestamp type flags; The timestamp semantic mark unified module is connected to the data packet extension module. The timestamp semantic mark unified module internally defines preset socket options, preset control message types and preset timestamp type flags that are compatible with the target operating system. The timestamp semantic mark unified module establishes the mapping relationship between the preset socket options, preset control message types and preset timestamp type flags and various timestamps in the extended data structure. The timestamp acquisition configuration module is connected to the timestamp semantic mark unification module. It sets timestamp acquisition points in the sending path and receiving path according to the unified timestamp type mark, and writes the acquired timestamps into the corresponding extended data structure configured for the data packet. The timestamp receiving module is connected to the timestamp semantic mark unification module and the timestamp acquisition configuration module respectively, so as to save the receiving timestamp option state in the connection layer, and encapsulate the receiving timestamp carried by the data packet into a control message according to the receiving timestamp option state, and send it up to the application layer along with the received data. The timestamp sending module is connected to the timestamp semantic mark unification module and the timestamp acquisition configuration module, respectively, to associate the data packet with the timestamp to be collected with the corresponding connected timestamp relay object, and after the timestamp is collected, to deliver the data packet or timestamp object with the timestamp to the timestamp relay object for the application layer to read.
[0014] As a further preferred embodiment of the present invention The data packet extension module is equipped with an object pool, which manages the extended data structure in a unified manner. Furthermore, each data packet configured with an extended data structure is provided with a unique extended object index, which can locate the extended data structure corresponding to the data packet in the object pool.
[0015] As a further preferred embodiment of the present invention The extended data structure includes a timestamp substructure, which includes: A software timestamp storage unit, wherein the software timestamp is stored internally, and the software timestamp is collected by the protocol stack before the send function of the network device driver is called in the send path, before the send scheduling is enqueued, or at the receive entry point of the protocol stack in the receive path. A hardware timestamp storage unit stores a hardware timestamp. The hardware timestamp is generated by the network device when a message is sent or received, and is read by the network device driver from the send descriptor or hardware timestamp register in the send path, and from the receive descriptor or hardware timestamp register in the receive path. An extended timestamp storage unit stores protocol extended timestamps from within the protocol stack. The protocol extended timestamps include at least a precise time protocol master-slave interaction timestamp and a transmission scheduling timestamp. The timestamp substructure also includes a timestamp type flag field, which records the timestamp type corresponding to the data packet.
[0016] As a further preferred embodiment of the present invention, the data packet extension module further includes the following corresponding interfaces for extending data structures: An extended data allocation interface is provided to query data packets with configured extended data structures, or to allocate extended data structures from the object pool on demand for data packets without configured extended data structures, and to return a pointer to the timestamp substructure in the extended data structure. A timestamp writing interface, wherein the timestamp writing interface writes a timestamp to an extended data structure configured for the data packet at the timestamp collection point; A timestamp determination interface is provided to determine whether a data packet has a specified type of timestamp enabled. The macro command interface converts the internal 64-bit unsigned integer timestamp into a second-microsecond time structure and a second-nanosecond time structure compatible with the target interface. When data packets are copied, fragmented, reassembled, or cloned, the timestamps inside the extended data structure are also copied.
[0017] As a further preferred embodiment of the present invention, in the timestamp semantic mark unification module: The preset socket options include at least a second-microsecond socket option, a second-nanosecond socket option, and a bitmask socket option; wherein, the second-microsecond socket option enables the received software timestamp returned in a second-microsecond time structure, the second-nanosecond socket option enables the received software timestamp returned in a second-nanosecond time structure, and the bitmask socket option configures at least one or more combinations of the send timestamp, receive timestamp, software timestamp, and hardware timestamp through bitmask parameters; The preset control message types include at least second-microsecond control messages, second-nanosecond control messages, and bitmask control messages corresponding to bitmask socket options; wherein, the second-microsecond control message carries a timestamp of the second-microsecond time structure, the second-nanosecond control message carries a timestamp of the second-nanosecond time structure, and the bitmask control message uses a multi-slot timestamp structure as the payload, and writes at least one timestamp into the corresponding slot in the multi-slot timestamp structure according to the timestamp type enabled by the bitmask parameters and the timestamp type that has been collected; The multi-slot timestamp structure includes software timestamp slots, reserved slots, and hardware timestamp slots set sequentially. For timestamp types that are not enabled or have not yet been collected, their corresponding slots are set to zero. The preset timestamp type flags include at least a sending hardware timestamp flag, a sending software timestamp flag, a sending timestamp scheduling flag, a receiving hardware timestamp flag, and a receiving software timestamp flag; In addition, the unified timestamp semantic mark module is also equipped with a sending timestamp aggregation mask and a receiving timestamp aggregation mask; wherein, the sending timestamp aggregation mask determines whether a sending timestamp mark exists; and the receiving timestamp aggregation mask determines whether a receiving timestamp mark exists.
[0018] As a further preferred embodiment of the present invention, in the timestamp acquisition configuration module... 1) When setting timestamp collection points on the sending path: Software timestamp: When the timestamp type flag field of the data packet to be sent contains the sending software timestamp flag, the system clock is read through the software timestamp macro before the protocol stack calls the sending function of the network device driver, and the time value is written into the software timestamp storage unit; when the data packet to be sent is sent through the sending scheduling queue and the timestamp type flag field contains the sending timestamp scheduling flag, the sending scheduling timestamp is collected before the data packet enters the sending scheduling queue. Hardware timestamp: For network devices that support hardware timestamps, the hardware timestamp configuration is set through the hardware timestamp configuration command and queried through the hardware timestamp query command; the network device generates a transmission hardware timestamp when the packet transmission is completed and writes the transmission hardware timestamp to the transmission descriptor or hardware timestamp register; the network device driver reads the transmission hardware timestamp when it learns that the transmission is complete, writes the transmission hardware timestamp to the hardware timestamp storage unit through the timestamp write interface, and sets the transmission hardware timestamp flag; 2) When setting timestamp collection points on the receiving path: Software timestamp: At the protocol stack receiving entry point, the software timestamp for data packet acquisition and reception is set according to the global configuration or network interface configuration. The received software timestamp is written into the software timestamp storage unit, and the received software timestamp flag is set. Hardware timestamp: When a network device receives a packet, it generates a received hardware timestamp and writes the received hardware timestamp to the receive descriptor or hardware timestamp register. After the network device driver reads the received hardware timestamp, it writes the received hardware timestamp to the hardware timestamp storage unit through the timestamp write interface and sets the received hardware timestamp flag. The receive descriptor records or carries the receive hardware timestamp in the receive path, and the send descriptor records or carries the send hardware timestamp in the send path.
[0019] As a further preferred embodiment of the present invention, the timestamp receiving module receives timestamps in the following manner: In the connection layer and socket layer, add second-microsecond socket option status bits, second-nanosecond socket option status bits, and bitmask socket option status bits to the connection. Then, use socket option read function and socket option set function to read and set the second-microsecond socket option, second-nanosecond socket option, and bitmask socket option respectively. After the internal function used to receive socket messages obtains the network buffer, it checks whether the data packets in the network buffer carry a receive timestamp: If carried, construct the sockets according to the enabled socket options for the connection: Second-microsecond control messages with a second-microsecond time structure as the payload; Second-nanosecond control messages with a second-nanosecond time structure as the load; And bitmask control messages with a multi-slot timestamp structure as the payload; The constructed control message is appended to the auxiliary data buffer, and when the space in the auxiliary data buffer is insufficient, a control data truncation flag is set in the flag field of the message header structure.
[0020] In a further preferred embodiment of the present invention, the timestamp sending module sends timestamps in the following manner: In the connection layer, a sending timestamp relay object is set up. The sending timestamp relay object includes a connection extension handle and a waiting queue for sending timestamps. When the application layer enables the bitmask socket option and the bitmask parameter of the bitmask socket option includes a sending timestamp type flag, the association information between the data packet to be sent and the corresponding connection layer is written to the connection extension handle so that the data packet to be sent is associated with the sending timestamp relay object. After obtaining the sending software timestamp from the protocol stack, or after the network device generates the sending hardware timestamp and the network device driver obtains the sending hardware timestamp, the corresponding sending timestamp is written to the extended data structure of the data packet through the timestamp writing interface. Then, the sending function is called according to the connection extension handle to deliver the data packet with the sending timestamp to the corresponding connection layer's sending timestamp relay object. For packet socket messages, the data packet is delivered to the corresponding packet socket sending timestamp queue. After the application layer detects the sending timestamp read flag through the message receiving interface, it enters the sending timestamp processing branch, retrieves the reported sending timestamp object from the sending timestamp relay object, writes the timestamp in the sending timestamp object into the corresponding slot of the multi-slot timestamp structure according to the timestamp type flag in the sending timestamp object, and controls the message return with a bitmask.
[0021] As a further preferred embodiment of the present invention When the message receiving interface detects the non-blocking flag, the timestamp sending module reads the sending timestamp relay object in a non-blocking manner and returns immediately if there is no sending timestamp in the sending timestamp relay object. When the message receiving interface does not detect the non-blocking flag, the timestamp sending module waits for the sending timestamp relay object to receive the sending timestamp before returning.
[0022] Furthermore, the present invention also provides a control method for a network timestamp control system, characterized by comprising the following steps: S100: The packet extension module configures an extended data structure for the packet; S200: The unified module for timestamp semantic flags establishes a mapping relationship between preset socket options, preset control message types, and preset timestamp type flags and various timestamps in the extended data structure; S300: The timestamp acquisition configuration module sets timestamp acquisition points in the sending and receiving paths according to the unified timestamp type flag, and writes the acquired timestamps into the corresponding extended data structure configured for the data packet; S400: The timestamp receiving module saves the receiving timestamp option status in the connection layer, and encapsulates the receiving timestamp carried by the data packet into a control message according to the receiving timestamp option status, and sends it up to the application layer along with the received data; S500: The timestamp sending module associates the data packet with the timestamp to be collected with the corresponding timestamp relay object, and after collecting the timestamp, delivers the data packet or timestamp object with the timestamp to the timestamp relay object for the application layer to read.
[0023] Compared with existing technologies, the technical effects that this invention can achieve include: 1) This invention provides a network timestamp control system and control method, which sets up an extended data structure with timestamps outside the LWIP data packet pbuf, and allocates it on demand through an object pool, so that the timestamps and data packets maintain the lightweight characteristics of LWIP; and the extended data is synchronously copied during data packet copying, fragmentation, reassembly and cloning, maintaining the consistency of timestamps within the protocol stack.
[0024] 2) This invention provides a network timestamp control system and its control method, which introduces socket options, control message types and flag sets that are compatible with Linux semantics, and fully implements socket option reading, socket option setting and corresponding type control message construction at the interface layer and socket layer, thereby improving the portability of user-space code.
[0025] 3) This invention provides a network timestamp control system and its control method, which unifies the software timestamp acquisition interface and hardware timestamp configuration interface among the driver, network device abstraction layer, and protocol stack, forming a timestamp data path from hardware to socket layer.
[0026] 4) This invention provides a network timestamp control system and its control method. It proposes and implements a sending timestamp reading flag and a sending timestamp relay object. After the timestamp is written into the sending path, the data packet with the timestamp is delivered to the corresponding connection layer according to the connection extension handle associated with the data packet. The application layer reads the sending timestamp relay object through the message receiving interface and can choose to block and wait or perform non-blocking reading, thus eliminating the need to introduce the Linux error queue mechanism. While maintaining semantic clarity, it is more suitable for embedded real-time systems.
[0027] 5) This invention provides a network timestamp control system and its control method. The timestamp transmission path can cover scenarios such as AF_PACKET packet sockets, UDP sockets, RAW raw sockets, and IPv4 / IPv6 fragmentation and reassembly, forming a unified network timestamp framework for the entire SylixOS protocol stack.
[0028] 6) This invention provides a network timestamp control system and its control method, which completes the network precise timestamp capability for SylixOS. The application layer can obtain the software and hardware timestamps of message sending and receiving through the POSIX-compatible socket basic interface and Linux timestamp extended semantics. It can be used for time-sensitive services such as PTP / 1588, gPTP, TSN, industrial real-time control, 5G fronthaul, and latency measurement.
[0029] 7) This invention provides a network timestamp control system and its control method, which is compatible with Linux in terms of socket options and receive control message format. The transmission timestamp reading adopts a relay mechanism adapted to SylixOS, thereby reducing the modification cost of porting Linux PTP, PTP4L and TSN measurement tools to SylixOS.
[0030] 8) This invention provides a network timestamp control system and its control method. Through the design of pbuf packet extended data and object pool, timestamp structure is allocated to the message only when needed, which ensures accuracy without destroying the memory usage characteristics and transmission throughput of LWIP, and is suitable for resource-constrained embedded devices.
[0031] 9) This invention provides a network timestamp control system and its control method, which significantly simplifies protocol stack modifications compared to error queue mechanisms, helps reduce real-time jitter and obtain deterministic latency analysis results.
[0032] 10) This invention provides a network timestamp control system and its control method, a unified driver layer hardware timestamp access interface and timestamp flag system, providing a standardized infrastructure for subsequent expansion of more types of timestamps (such as acknowledgment timestamps, packet information options, hardware and software dual timestamp options, etc.) and more network card drivers. Attached Figure Description
[0033] Figure 1 The diagram shown is a logical structure diagram of the system provided by the present invention.
[0034] Figure 2 The diagram shown is a flowchart of the steps of the method provided by the present invention. Detailed Implementation
[0035] The technical solutions of the embodiments of the present invention will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0036] In the description of this invention, it should be noted that the terms "upper," "lower," "inner," "outer," "front end," "rear end," "both ends," "one end," and "the other end," etc., indicate the orientation or positional relationship based on the orientation or positional relationship shown in the accompanying drawings. They are used only for the convenience of describing this invention and for simplifying the description, and do not indicate or imply that the device or element referred to must have a specific orientation, or be constructed and operated in a specific orientation. Therefore, they should not be construed as limitations on this invention. Furthermore, the terms "first" and "second" are used for descriptive purposes only and should not be construed as indicating or implying relative importance.
[0037] In the description of this invention, it should be noted that, unless otherwise explicitly specified and limited, the terms "installed," "equipped with," "connected," etc., should be interpreted broadly. For example, "connection" can be a fixed connection, a detachable connection, or an integral connection; it can be a mechanical connection or an electrical connection; it can be a direct connection or an indirect connection through an intermediate medium; it can be a connection within two components. Those skilled in the art can understand the specific meaning of the above terms in this invention based on the specific circumstances.
[0038] [First Embodiment] like Figure 1 The image shows a network timestamp control system provided in the first embodiment of the present invention. The system includes: The packet extension module configures the extended data structure pbuf_ext_data for the packet. The extended data structure internally stores various timestamps and timestamp type flags. In this embodiment, the data packet extension module is equipped with an object pool, which manages the extended data structure in a unified manner. Furthermore, each data packet configured with an extended data structure is assigned a unique extended object index ext_idx. The extended object index can locate the extended data structure corresponding to the data packet in the object pool, thereby avoiding the constant use of additional memory on all data packets.
[0039] The extended data structure in this embodiment includes a timestamp substructure pbuf_tstamp, and its code example is as follows: struct pbuf_tstamp { u64_t stamp[3]; / * SW / EXT / HW three-slot timestamp * / u32_t sof_flags; / * SOF_TIMESTAMPING_* Flag set * / }; typedef enum { PBUF_TIMESTAMP_SW = 0, PBUF_TIMESTAMP_EXT, PBUF_TIMESTAMP_HW } pbuf_tstamp_type; The timestamp substructure includes: The software timestamp storage unit stamp[0] stores the software timestamp. The software timestamp is collected by the protocol stack before the network device driver sends the function in the sending path, before the sending schedule is enqueued, or at the protocol stack receiving entry point in the receiving path. The hardware timestamp storage unit stamp[2] stores the hardware timestamp inside. The hardware timestamp is generated by the network device when the message is sent or received, and is read by the network device driver from the send descriptor or hardware timestamp register or send descriptor in the sending path, and from the receive descriptor or hardware timestamp register in the receiving path. The extended timestamp storage unit stamp[1] contains the protocol extended timestamp inside the protocol stack. The protocol extended timestamp includes at least the precise time protocol master-slave interaction timestamp and the sending scheduling timestamp. The timestamp substructure also includes a timestamp type flag field, which records the timestamp type corresponding to the data packet to identify the timestamp type of the data packet that has been collected.
[0040] The data packet extension module provided in this embodiment also includes the following corresponding interfaces for extending data structures: The extended data allocation interface `pbuf_get_timestamp(p, alloc)` provides a query function for data packets with configured extended data structures, or allocates extended data structures from the object pool on demand for data packets without configured extended data structures, and returns a pointer to the timestamp substructure in the extended data structure for the timestamp acquisition configuration module to read or write to the timestamp substructure. The parameter `p` specifies the data packet, and the parameter `alloc` indicates whether to perform allocation when no extended data structure is configured. The returned pointer serves as the entry point for the caller to access the timestamp storage cell array and the timestamp type flag field. The timestamp writing interface pbuf_set_timestamp(p, ts, force) writes timestamps to the extended data structure configured for the data packets at the timestamp collection point. The timestamp check interface pbuf_check_timestamp_flags(p, flags) determines whether a data packet has enabled a specified type of timestamp. The macro command interface PBUF_TIMESTAMP_TO_TIMEVAL / TIMESPEC converts the internal 64-bit unsigned integer timestamp into second-microsecond and second-nanosecond time structures compatible with the target interface. When data packets are copied, fragmented, reassembled, or cloned, the timestamps inside the extended data structure are also copied to ensure that the timestamps are not lost during transmission within the protocol stack.
[0041] The system provided in this embodiment also includes a unified timestamp semantic mark module. The unified timestamp semantic mark module is connected to the data packet extension module. The unified timestamp semantic mark module internally defines preset socket options, preset control message types and preset timestamp type flags that are compatible with the target operating system. The unified timestamp semantic mark module establishes the mapping relationship between the preset socket options, preset control message types and preset timestamp type flags and various timestamps in the extended data structure. In the unified timestamp semantic tag module provided in this embodiment: The default socket options include at least the second-microsecond socket option SO_TIMESTAMP, the second-nanosecond socket option SO_TIMESTAMPNS, and the bitmask socket option SO_TIMESTAMPING. Among them, the second-microsecond socket option enables the received software timestamp returned in the second-microsecond time structure timeval (time expressed in seconds and microseconds), the second-nanosecond socket option enables the received software timestamp returned in the second-nanosecond time structure timesspec (time expressed in seconds and nanoseconds), and the bitmask socket option configures at least one or more combinations of the send timestamp, receive timestamp, software timestamp, and hardware timestamp through bitmask parameters. The preset control message types include at least the second-microsecond control message SCM_TIMESTAMP, the second-nanosecond control message SCM_TIMESTAMPNS, and the bitmask control message SCM_TIMESTAMPING corresponding to the bitmask socket option; wherein, the second-microsecond control message carries the timestamp of the second-microsecond time structure, the second-nanosecond control message carries the timestamp of the second-nanosecond time structure, and the bitmask control message uses the multi-slot timestamp structure scm_timestamping as the payload, and writes at least one timestamp into the corresponding slot in the multi-slot timestamp structure according to the timestamp type enabled by the bitmask parameters and the timestamp type that has been collected; The multi-slot timestamp structure includes a software timestamp slot ts[0], a reserved slot ts[1], and a hardware timestamp slot ts[2] set sequentially. For timestamp types that are not enabled or have not yet been collected, their corresponding slots are set to zero. The bitmask parameter is used to select the generation and reporting type of timestamp. In this embodiment, the extended timestamp storage unit stamp[1] is an internal extended slot of the protocol stack and does not correspond to the reserved slot ts[1] in the multi-slot timestamp structure scm_timestamping. When constructing the bitmask control message SCM_TIMESTAMPIN, the software timestamp storage unit stamp[0] is mapped to the software timestamp slot ts[0], the hardware timestamp storage unit stamp
[02] is mapped to the hardware timestamp slot ts[2], and the reserved slot ts[1] is set to zero. The protocol extended time slot is carried by the sending timestamp object with a timestamp type flag for internal processing of the protocol stack and differentiation by the application layer.
[0042] The default timestamp type flag SOF_TIMESTAMPING_* includes at least the transmit hardware timestamp flag TX_HARDWARE, the transmit software timestamp flag TX_SOFTWARE, the transmit timestamp scheduling flag TX_SCHED, the receive hardware timestamp flag RX_HARDWARE, and the receive software timestamp flag RX_SOFTWARE. In addition, the unified timestamp semantic flag module also includes a sending timestamp aggregation mask SOF_TIMESTAMPING_TX_SUPPORT and a receiving timestamp aggregation mask SOF_TIMESTAMPING_RX_SUPPORT; the sending timestamp aggregation mask determines whether a sending timestamp flag exists; and the receiving timestamp aggregation mask determines whether a receiving timestamp flag exists.
[0043] This allows application layer code to configure timestamps in a manner similar to Linux, greatly reducing the adaptation cost of porting PTP / TSN / time-sensitive applications from Linux to SylixOS.
[0044] like Figure 1 As shown, the system provided in this embodiment also includes a timestamp acquisition and configuration module. The timestamp acquisition and configuration module is connected to the timestamp semantic mark unification module to set timestamp acquisition points in the sending path and receiving path respectively according to the unified timestamp type mark, and write the acquired timestamps into the corresponding extended data structure configured for the data packet. In the timestamp collection configuration module provided in this embodiment, 1) When setting timestamp collection points on the sending path: Software timestamp: When the timestamp type flag field of the data packet to be sent contains the sending software timestamp flag, the system clock is read through the software timestamp generation macro PBUF_GEN_SW_TIMESTAMP before the protocol stack calls the send function of the network device driver. The software timestamp generation macro is used to encapsulate the system clock reading and internal time value generation operations to generate an internal 64-bit unsigned integer time value representing the current acquisition time, and write the time value into the software timestamp storage unit. When the data packet to be sent is sent through the sending scheduling queue and the timestamp type flag field contains the sending timestamp scheduling flag, the sending scheduling timestamp is acquired before the data packet enters the sending scheduling queue. Hardware timestamp: For network devices that support hardware timestamp, the hardware timestamp configuration is set by the hardware timestamp configuration command SIOCSHWTSTAMP, and the hardware timestamp configuration is queried by the hardware timestamp query command SIOCGHWTSTAMP; the network device generates a sending hardware timestamp when the packet is sent and writes the sending hardware timestamp to the sending descriptor or hardware timestamp register; the network device driver reads the sending hardware timestamp when it knows that the sending is complete, writes the sending hardware timestamp to the hardware timestamp storage unit stamp[2] through the timestamp writing interface pbuf_set_timestamp(), and sets the sending hardware timestamp flag TX_HARDWARE; 2) When setting timestamp collection points on the receiving path: Software timestamp: At the protocol stack receiving entry point, the software timestamp for data packet acquisition and reception is set according to the global configuration or network interface configuration. The received software timestamp is written into the software timestamp storage unit, and the received software timestamp flag is set. Hardware timestamp: When a network device receives a message, it generates a receiving hardware timestamp and writes the receiving hardware timestamp into the receiving descriptor or hardware timestamp register. After the network device driver reads the receiving hardware timestamp, it writes the receiving hardware timestamp into the hardware timestamp storage unit stamp[2] through the timestamp writing interface pbuf_set_timestamp() and sets the receiving hardware timestamp flag RX_HARDWARE. The receive descriptor records or carries the receive hardware timestamp in the receive path, and the send descriptor records or carries the send hardware timestamp in the send path.
[0045] like Figure 1 As shown, the system provided in this embodiment also includes a timestamp receiving module. The timestamp receiving module is connected to the timestamp semantic mark unification module and the timestamp acquisition configuration module, respectively, so as to save the receiving timestamp option state in the connection layer, and encapsulate the receiving timestamp carried by the data packet into a control message according to the receiving timestamp option state, and send it to the application layer along with the received data.
[0046] Because timestamp data is bound to data packets, it can be transmitted and preserved through the extended data mechanism when the message goes through processes such as IP fragmentation and reassembly, queuing, and stacking.
[0047] In the timestamp receiving module provided in this embodiment, timestamps are received in the following manner: To enhance connectivity at the connection and socket layers, the following status bits are added: NETCONN_SO_TIMESTAMP for second-microsecond socket options, NETCONN_SO_TIMESTAMPNS for second-nanosecond socket options, and NETCONN_SO_TIMESTAMPING for bitmask socket options. The socket option read function lwip_getsockopt() and the socket option set function lwip_setsockopt() are used to read and set the second-microsecond socket options, second-nanosecond socket options, and bitmask socket options, respectively. After the internal function used to receive socket messages obtains the network buffer, it checks whether the data packets in the network buffer carry a receive timestamp: If carried, construct the sockets according to the enabled socket options for the connection: The second-microsecond control message SCM_TIMESTAMP with a second-microsecond time structure as its payload; The second-nanosecond control message SCM_TIMESTAMPNS with a second-nanosecond time structure as its payload; And the bitmask control message SCM_TIMESTAMPING with a multi-slot timestamp structure as the payload; The constructed control message is appended to the auxiliary data buffer, and when the auxiliary data buffer is insufficient, the control data truncation flag MSG_CTRUNC is set in the msg_flags field of the message header structure msghdr.
[0048] With the above settings, the received timestamp is returned as auxiliary data in the same message receiving call as the received payload. It can output timestamps of corresponding precision and type according to the socket options enabled by each connection without changing the message payload. It can also explicitly prompt that the auxiliary data buffer space is insufficient by controlling the data truncation flag, thereby avoiding mismatch or silent loss of timestamps and data packets, improving interface compatibility and the reliability of received timestamp transmission.
[0049] like Figure 1 As shown, the system provided in this embodiment also includes a timestamp sending module. The timestamp sending module is connected to the timestamp semantic mark unification module and the timestamp acquisition configuration module, respectively, to associate the data packet with the timestamp to be collected with the corresponding connected timestamp relay object, and after the timestamp is collected, to deliver the data packet or timestamp object with the timestamp to the timestamp relay object for the application layer to read.
[0050] In the timestamp sending module provided in this embodiment, timestamps are sent in the following manner: In the connection layer, a send timestamp relay object is set up. The send timestamp relay object includes a connection extension handle conn_ext_handle and a waiting queue txtstamp for queuing to send timestamps. The send timestamp objects in the waiting queue include at least a timestamp value and a timestamp type flag. When the application layer enables the bitmasked socket option SO_TIMESTAMPING and the bitmasked socket option's bitmask parameter includes the send timestamp type flag, the association information between the data packet to be sent and the corresponding connection layer is written to the connection extension handle so that the data packet to be sent is associated with the send timestamp relay object. After obtaining the sending software timestamp from the protocol stack, or after the network device generates the sending hardware timestamp and the network device driver obtains the sending hardware timestamp, the corresponding sending timestamp is written to the extended data structure of the data packet through the timestamp writing interface. Then, the sending function is called according to the connection extension handle to deliver the data packet with the sending timestamp to the corresponding connection layer sending timestamp relay object. For packet socket messages, the data packet is delivered to the corresponding AF_PACKET packet socket sending timestamp queue. After the application layer detects the sending timestamp read flag MSG_TX_TIMESTAMP through the message receiving interface recvmsg(), it enters the sending timestamp processing branch. It retrieves the reported sending timestamp object from the sending timestamp relay object, writes the timestamp in the sending timestamp object into the corresponding slot of the multi-slot timestamp structure according to the timestamp type flag of the sending timestamp object, and controls the message return with a bitmask.
[0051] With the above settings, the timestamp generation process is decoupled from the application layer reading process. The correspondence between data packets, connections, and timestamps is maintained through connection extension handles, ensuring that hardware timestamps generated after message transmission can still be accurately returned to the corresponding connection, avoiding timestamp mismatches when multiple connections are concurrent. Compared with the Linux error queue mechanism, there is no need to maintain an error queue at each socket layer or introduce sk_buff cloning, which helps reduce cache copying and scheduling overhead and facilitates deterministic latency analysis in real-time embedded environments.
[0052] As a further preferred option, When the message receiving interface detects the non-blocking flag, the timestamp sending module reads the sending timestamp relay object in a non-blocking manner and returns immediately if there is no sending timestamp in the sending timestamp relay object; When the message receiving interface does not detect the non-blocking flag, the timestamp sending module waits for the sending timestamp relay object to receive the sending timestamp before returning.
[0053] With the above settings, the non-blocking mode can prevent the calling thread from waiting before the timestamp is generated, which is conducive to maintaining determinism in polling or event-driven applications; the blocking mode can return after the timestamp is available, avoiding repeated polling at the application layer, thus balancing real-time response and processing efficiency.
[0054] [Second Embodiment] like Figure 2 As shown, the second embodiment of the present invention also provides a control method for a network timestamp control system, comprising the following steps: S100: The packet extension module configures an extended data structure for the packet; S200: The unified module for timestamp semantic flags establishes a mapping relationship between preset socket options, preset control message types, and preset timestamp type flags and various timestamps in the extended data structure; S300: The timestamp acquisition configuration module sets timestamp acquisition points in the sending and receiving paths according to the unified timestamp type flag, and writes the acquired timestamps into the corresponding extended data structure configured for the data packet; S400: The timestamp receiving module saves the receiving timestamp option status in the connection layer, and encapsulates the receiving timestamp carried by the data packet into a control message according to the receiving timestamp option status, and sends it up to the application layer along with the received data; S500: The timestamp sending module associates the data packet with the timestamp to be collected with the corresponding timestamp relay object, and after collecting the timestamp, delivers the data packet or timestamp object with the timestamp to the timestamp relay object for the application layer to read.
[0055] It will be apparent to those skilled in the art that the present invention is not limited to the details of the exemplary embodiments described above, and that the invention can be implemented in other specific forms without departing from its spirit or essential characteristics. Therefore, the embodiments should be considered in all respects as exemplary and non-limiting, and the scope of the invention is defined by the appended claims rather than the foregoing description. Thus, all variations falling within the meaning and scope of equivalents of the claims are intended to be included within the present invention. No reference numerals in the claims should be construed as limiting the scope of the claims.
Claims
1. A network timestamp control system, characterized in that: The system includes: A data packet extension module, wherein the data packet extension module configures an extended data structure for the data packet, and the extended data structure internally stores various timestamps and timestamp type flags; The timestamp semantic mark unified module is connected to the data packet extension module. The timestamp semantic mark unified module internally defines preset socket options, preset control message types and preset timestamp type flags that are compatible with the target operating system. The timestamp semantic mark unified module establishes the mapping relationship between the preset socket options, preset control message types and preset timestamp type flags and various timestamps in the extended data structure. The preset socket options include at least second-microsecond socket options, second-nanosecond socket options, and bitmask socket options; the preset control message types include at least second-microsecond control messages, second-nanosecond control messages, and bitmask control messages corresponding to the bitmask socket options; the preset timestamp type flags include at least transmit hardware timestamp flags, transmit software timestamp flags, transmit timestamp scheduling flags, receive hardware timestamp flags, and receive software timestamp flags. The timestamp acquisition configuration module is connected to the timestamp semantic mark unification module. It sets timestamp acquisition points in the sending path and receiving path according to the unified timestamp type mark, and writes the acquired timestamps into the corresponding extended data structure configured for the data packet. The timestamp receiving module is connected to the timestamp semantic mark unification module and the timestamp acquisition configuration module respectively, so as to save the receiving timestamp option state in the connection layer, and encapsulate the receiving timestamp carried by the data packet into a control message according to the receiving timestamp option state, and send it up to the application layer along with the received data. The timestamp sending module is connected to the timestamp semantic mark unification module and the timestamp acquisition configuration module, respectively, to associate the data packet with the timestamp to be collected with the corresponding connected timestamp relay object, and after the timestamp is collected, to deliver the data packet or timestamp object with the timestamp to the timestamp relay object for the application layer to read.
2. The network timestamp control system according to claim 1, characterized in that: The data packet extension module is equipped with an object pool, which manages the extended data structure in a unified manner. Furthermore, each data packet configured with an extended data structure is provided with a unique extended object index, which can locate the extended data structure corresponding to the data packet in the object pool.
3. A network timestamp control system according to claim 2, characterized in that: The extended data structure includes a timestamp substructure, which includes: A software timestamp storage unit, wherein the software timestamp is stored internally, and the software timestamp is collected by the protocol stack before the send function of the network device driver is called in the send path, before the send scheduling is enqueued, or at the receive entry point of the protocol stack in the receive path. A hardware timestamp storage unit stores a hardware timestamp. The hardware timestamp is generated by the network device when a message is sent or received, and is read by the network device driver from the send descriptor or hardware timestamp register in the send path, and from the receive descriptor or hardware timestamp register in the receive path. An extended timestamp storage unit stores protocol extended timestamps from within the protocol stack. The protocol extended timestamps include at least a precise time protocol master-slave interaction timestamp and a transmission scheduling timestamp. The timestamp substructure also includes a timestamp type flag field, which records the timestamp type corresponding to the data packet.
4. A network timestamp control system according to claim 3, characterized in that: The data packet extension module also includes the following corresponding interfaces for extending data structures: An extended data allocation interface is provided to query data packets with configured extended data structures, or to allocate extended data structures from the object pool on demand for data packets without configured extended data structures, and to return a pointer to the timestamp substructure in the extended data structure. A timestamp writing interface, wherein the timestamp writing interface writes a timestamp to an extended data structure configured for the data packet at the timestamp collection point; A timestamp determination interface is provided to determine whether a data packet has a specified type of timestamp enabled. The macro command interface converts the internal 64-bit unsigned integer timestamp into a second-microsecond time structure and a second-nanosecond time structure compatible with the target interface. When data packets are copied, fragmented, reassembled, or cloned, the timestamps inside the extended data structure are also copied.
5. A network timestamp control system according to claim 4, characterized in that: In the unified timestamp semantic identifier module: The second-microsecond socket option enables the receiving software timestamp returned in a second-microsecond time structure, the second-nanosecond socket option enables the receiving software timestamp returned in a second-nanosecond time structure, and the bitmask socket option configures at least one or more combinations of the sending timestamp, receiving timestamp, software timestamp, and hardware timestamp through bitmask parameters. The second-microsecond control message carries the timestamp of the second-microsecond time structure, the second-nanosecond control message carries the timestamp of the second-nanosecond time structure, and the bitmask control message uses a multi-slot timestamp structure as the payload and writes at least one timestamp into the corresponding slot in the multi-slot timestamp structure according to the timestamp type enabled by the bitmask parameters and the timestamp type that has been collected. The multi-slot timestamp structure includes software timestamp slots, reserved slots, and hardware timestamp slots set sequentially. For timestamp types that are not enabled or have not yet been collected, their corresponding slots are set to zero. In addition, the unified timestamp semantic mark module is also equipped with a sending timestamp aggregation mask and a receiving timestamp aggregation mask; wherein, the sending timestamp aggregation mask determines whether a sending timestamp mark exists; and the receiving timestamp aggregation mask determines whether a receiving timestamp mark exists.
6. A network timestamp control system according to claim 5, characterized in that: In the timestamp acquisition configuration module 1) When setting timestamp collection points on the sending path: Software timestamp: When the timestamp type flag field of the data packet to be sent contains the sending software timestamp flag, the system clock is read through the software timestamp macro before the protocol stack calls the sending function of the network device driver, and the time value is written into the software timestamp storage unit; when the data packet to be sent is sent through the sending scheduling queue and the timestamp type flag field contains the sending timestamp scheduling flag, the sending scheduling timestamp is collected before the data packet enters the sending scheduling queue. Hardware timestamp: For network devices that support hardware timestamps, the hardware timestamp configuration is set through the hardware timestamp configuration command and queried through the hardware timestamp query command; the network device generates a transmission hardware timestamp when the packet transmission is completed and writes the transmission hardware timestamp to the transmission descriptor or hardware timestamp register; the network device driver reads the transmission hardware timestamp when it learns that the transmission is complete, writes the transmission hardware timestamp to the hardware timestamp storage unit through the timestamp write interface, and sets the transmission hardware timestamp flag; 2) When setting timestamp collection points on the receiving path: Software timestamp: At the protocol stack receiving entry point, the software timestamp for data packet acquisition and reception is set according to the global configuration or network interface configuration. The received software timestamp is written into the software timestamp storage unit, and the received software timestamp flag is set. Hardware timestamp: When a network device receives a packet, it generates a received hardware timestamp and writes the received hardware timestamp to the receive descriptor or hardware timestamp register. After the network device driver reads the received hardware timestamp, it writes the received hardware timestamp to the hardware timestamp storage unit through the timestamp write interface and sets the received hardware timestamp flag. The receive descriptor records or carries the receive hardware timestamp in the receive path, and the send descriptor records or carries the send hardware timestamp in the send path.
7. A network timestamp control system according to claim 6, characterized in that: The timestamp receiving module receives timestamps in the following manner: In the connection layer and socket layer, add second-microsecond socket option status bits, second-nanosecond socket option status bits, and bitmask socket option status bits to the connection. Then, use socket option read function and socket option set function to read and set the second-microsecond socket option, second-nanosecond socket option, and bitmask socket option respectively. After the internal function used to receive socket messages obtains the network buffer, it checks whether the data packets in the network buffer carry a receive timestamp: If carried, construct the sockets according to the enabled socket options for the connection: Second-microsecond control messages with a second-microsecond time structure as the payload; Second-nanosecond control messages with a second-nanosecond time structure as the load; And bitmask control messages with a multi-slot timestamp structure as the payload; The constructed control message is appended to the auxiliary data buffer, and when the space in the auxiliary data buffer is insufficient, a control data truncation flag is set in the flag field of the message header structure.
8. A network timestamp control system according to claim 5, characterized in that: In the timestamp sending module, timestamps are sent in the following manner: In the connection layer, a sending timestamp relay object is set up. The sending timestamp relay object includes a connection extension handle and a waiting queue for sending timestamps. When the application layer enables the bitmask socket option and the bitmask parameter of the bitmask socket option includes a sending timestamp type flag, the association information between the data packet to be sent and the corresponding connection layer is written to the connection extension handle so that the data packet to be sent is associated with the sending timestamp relay object. After obtaining the sending software timestamp from the protocol stack, or after the network device generates the sending hardware timestamp and the network device driver obtains the sending hardware timestamp, the corresponding sending timestamp is written to the extended data structure of the data packet through the timestamp writing interface. Then, the sending function is called according to the connection extension handle to deliver the data packet with the sending timestamp to the corresponding connection layer's sending timestamp relay object. For packet socket messages, the data packet is delivered to the corresponding packet socket sending timestamp queue. After the application layer detects the sending timestamp read flag through the message receiving interface, it enters the sending timestamp processing branch, retrieves the reported sending timestamp object from the sending timestamp relay object, writes the timestamp in the sending timestamp object into the corresponding slot of the multi-slot timestamp structure according to the timestamp type flag in the sending timestamp object, and controls the message return with a bitmask.
9. A network timestamp control system according to claim 8, characterized in that: When the message receiving interface detects the non-blocking flag, the timestamp sending module reads the sending timestamp relay object in a non-blocking manner and returns immediately if there is no sending timestamp in the sending timestamp relay object. When the message receiving interface does not detect the non-blocking flag, the timestamp sending module waits for the sending timestamp relay object to receive the sending timestamp before returning.
10. A control method for a network timestamp control system according to any one of claims 1-9, characterized in that: Includes the following steps: S100: The packet extension module configures extended data structures for packets; S200: The unified module for timestamp semantic flags establishes a mapping relationship between preset socket options, preset control message types, and preset timestamp type flags and various timestamps in the extended data structure; S300: The timestamp acquisition configuration module sets timestamp acquisition points in the sending and receiving paths according to the unified timestamp type flag, and writes the acquired timestamps into the corresponding extended data structure configured for the data packet; S400: The timestamp receiving module saves the receiving timestamp option status in the connection layer, and encapsulates the receiving timestamp carried by the data packet into a control message according to the receiving timestamp option status, and sends it up to the application layer along with the received data; S500: The timestamp sending module associates the data packet with the timestamp to be collected with the corresponding timestamp relay object, and after collecting the timestamp, delivers the data packet or timestamp object with the timestamp to the timestamp relay object for the application layer to read.
Citation Information
Patent Citations
Message timestamp processing method, system and device and readable storage medium
CN116192318A
Time series data processing method and system based on measuring point timestamps
CN121636502A
