This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit f5295df46a3e3304613c730ed9e0687385d2c964
Author: wenquan1 <[email protected]>
AuthorDate: Thu Apr 2 09:44:01 2026 +0800

    net/udp: support SO_TIMESTAMPING TX path.
    
    Add TX hardware timestamping support for UDP sockets, following the
    existing PKT socket implementation pattern:
    
    - Tag outgoing IOBs with io_conn in both buffered and unbuffered send
      paths when SO_TIMESTAMPING is enabled.
    - In ipv4/ipv6 input, detect TX timestamp clone (io_conn != NULL) and
      skip validation, jumping straight to protocol dispatch.
    - In udp_input, match returning packets by io_conn to identify TX
      timestamp echoes, queue them to s_errahead.
    - Handle MSG_ERRQUEUE in psock_udp_recvfrom() to dequeue TX timestamp
      packets from s_errahead with SO_TIMESTAMPING cmsg.
    - Report POLLPRI|POLLERR in udp_netpoll when s_errahead is non-empty.
    - Skip UDP checksum verification for TX timestamp clone packets.
    
    Signed-off-by: wenquan1 <[email protected]>
---
 net/devif/ipv4_input.c          | 37 +++++++++++++++-----------
 net/devif/ipv6_input.c          | 37 +++++++++++++++-----------
 net/udp/udp_callback.c          |  6 ++++-
 net/udp/udp_input.c             | 51 +++++++++++++++++++++++++++++++++--
 net/udp/udp_netpoll.c           | 23 ++++++++++++++++
 net/udp/udp_recvfrom.c          | 59 +++++++++++++++++++++++++++++++++++++++++
 net/udp/udp_sendto_buffered.c   |  7 +++++
 net/udp/udp_sendto_unbuffered.c |  9 +++++++
 8 files changed, 196 insertions(+), 33 deletions(-)

diff --git a/net/devif/ipv4_input.c b/net/devif/ipv4_input.c
index c6a42591438..417f6e0dff1 100644
--- a/net/devif/ipv4_input.c
+++ b/net/devif/ipv4_input.c
@@ -232,23 +232,40 @@ static int ipv4_in(FAR struct net_driver_s *dev)
   bool isfrag;
   int ret = OK;
 
-  /* Storing reception timestamp provided by realtime
-   * if timestamp no provided by hardware.
+  /* This is where the input processing starts.
+   *
+   * Get the size of the packet minus the size of link layer header,
+   * and make sure that all packet processing logic knows that there
+   * is an IPv4 packet in the device buffer.
    */
 
+  dev->d_len -= NET_LL_HDRLEN(dev);
+  IFF_SET_IPv4(dev->d_flags);
+
 #ifdef CONFIG_NET_TIMESTAMP
+#ifdef NET_UDP_HAVE_STACK
+  if (dev->d_iob->io_conn != NULL)
+    {
+      /* This is a TX timestamp clone looped back by the driver.
+       * Skip all validation (dest IP, checksum, etc.) and deliver
+       * it to the originating socket's error queue directly.
+       */
+
+      udp_ipv4_input(dev);
+      goto drop;
+    }
+#endif
+
   if ((dev->d_features & NETDEV_RX_STAMP) == 0)
     {
       clock_gettime(CLOCK_REALTIME, &dev->d_iob->io_time);
     }
-#endif /* CONFIG_NET_TIMESTAMP */
+#endif
 
   /* Handle ARP on input then give the IPv4 packet to the network layer */
 
   arp_ipin(dev);
 
-  /* This is where the input processing starts. */
-
 #ifdef CONFIG_NET_STATISTICS
   g_netstats.ipv4.recv++;
 #endif
@@ -275,22 +292,12 @@ static int ipv4_in(FAR struct net_driver_s *dev)
       goto drop;
     }
 
-  /* Get the size of the packet minus the size of link layer header */
-
-  dev->d_len -= NET_LL_HDRLEN(dev);
-
   if (((ipv4->vhl & IPv4_HLMASK) << 2) > dev->d_len)
     {
       nwarn("WARNING: Packet shorter than IPv4 header\n");
       goto drop;
     }
 
-  /* Make sure that all packet processing logic knows that there is an IPv4
-   * packet in the device buffer.
-   */
-
-  IFF_SET_IPv4(dev->d_flags);
-
   /* Check the size of the packet.  If the size reported to us in d_len is
    * smaller the size reported in the IP header, we assume that the packet
    * has been corrupted in transit.  If the size of d_len is larger than the
diff --git a/net/devif/ipv6_input.c b/net/devif/ipv6_input.c
index ef6de6f609c..4fb92f2a949 100644
--- a/net/devif/ipv6_input.c
+++ b/net/devif/ipv6_input.c
@@ -229,18 +229,35 @@ static int ipv6_in(FAR struct net_driver_s *dev)
   bool isfrag = false;
 #endif
 
-  /* Storing reception timestamp provided by realtime
-   * if timestamp no provided by hardware.
+  /* This is where the input processing starts.
+   *
+   * Get the size of the packet minus the size of link layer header,
+   * and make sure that all packet processing logic knows that there
+   * is an IPv6 packet in the device buffer.
    */
 
+  dev->d_len -= NET_LL_HDRLEN(dev);
+  IFF_SET_IPv6(dev->d_flags);
+
 #ifdef CONFIG_NET_TIMESTAMP
+#ifdef NET_UDP_HAVE_STACK
+  if (dev->d_iob->io_conn != NULL)
+    {
+      /* This is a TX timestamp clone looped back by the driver.
+       * Skip all validation and deliver it to the originating
+       * socket's error queue directly.
+       */
+
+      udp_ipv6_input(dev, IPv6_HDRLEN);
+      goto drop;
+    }
+#endif
+
   if ((dev->d_features & NETDEV_RX_STAMP) == 0)
     {
       clock_gettime(CLOCK_REALTIME, &dev->d_iob->io_time);
     }
-#endif /* CONFIG_NET_TIMESTAMP */
-
-  /* This is where the input processing starts. */
+#endif
 
 #ifdef CONFIG_NET_STATISTICS
   g_netstats.ipv6.recv++;
@@ -263,22 +280,12 @@ static int ipv6_in(FAR struct net_driver_s *dev)
       goto drop;
     }
 
-  /* Get the size of the packet minus the size of link layer header */
-
-  dev->d_len -= NET_LL_HDRLEN(dev);
-
   if (IPv6_HDRLEN > dev->d_len)
     {
       nwarn("WARNING: Packet shorter than IPv6 header\n");
       goto drop;
     }
 
-  /* Make sure that all packet processing logic knows that there is an IPv6
-   * packet in the device buffer.
-   */
-
-  IFF_SET_IPv6(dev->d_flags);
-
   /* Check the size of the packet. If the size reported to us in d_len is
    * smaller the size reported in the IP header, we assume that the packet
    * has been corrupted in transit. If the size of d_len is larger than the
diff --git a/net/udp/udp_callback.c b/net/udp/udp_callback.c
index 0ef8923a337..60843bf6f2a 100644
--- a/net/udp/udp_callback.c
+++ b/net/udp/udp_callback.c
@@ -304,7 +304,11 @@ uint32_t udp_callback(FAR struct net_driver_s *dev,
       conn_lock(&conn->sconn);
       flags = devif_conn_event(dev, flags, conn->sconn.s_list);
 
-      if ((flags & UDP_NEWDATA) != 0)
+      if ((flags & UDP_NEWDATA) != 0
+#ifdef CONFIG_NET_TIMESTAMP
+          && dev->d_iob->io_conn == NULL
+#endif
+         )
         {
           /* Data was not handled.. dispose of it appropriately */
 
diff --git a/net/udp/udp_input.c b/net/udp/udp_input.c
index c48dd49d126..cbef1c7a1c8 100644
--- a/net/udp/udp_input.c
+++ b/net/udp/udp_input.c
@@ -55,10 +55,11 @@
 #include <nuttx/net/netstats.h>
 
 #include "devif/devif.h"
-#include "utils/utils.h"
-#include "udp/udp.h"
 #include "icmp/icmp.h"
 #include "icmpv6/icmpv6.h"
+#include "socket/socket.h"
+#include "udp/udp.h"
+#include "utils/utils.h"
 
 /****************************************************************************
  * Private Functions
@@ -221,6 +222,52 @@ static int udp_input(FAR struct net_driver_s *dev, 
unsigned int iplen)
 #endif
   int ret = OK;
 
+#ifdef CONFIG_NET_TIMESTAMP
+  /* Handle TX timestamp: if the IOB was tagged with a connection on the
+   * send path, queue it to that connection's error-ahead buffer for
+   * MSG_ERRQUEUE.  Skip checksum and normal RX processing entirely.
+   */
+
+  if (dev->d_iob->io_conn != NULL)
+    {
+      udp_conn_list_lock();
+
+      /* Validate that the connection is still in the active list.
+       * The connection may have been closed and freed between the
+       * send path (where io_conn was set) and now.
+       */
+
+      for (conn = udp_nextconn(NULL); conn != NULL;
+           conn = udp_nextconn(conn))
+        {
+          if (&conn->sconn == dev->d_iob->io_conn)
+            {
+              break;
+            }
+        }
+
+      if (conn != NULL)
+        {
+          /* Move dev->d_iob directly into the error-ahead queue
+           * (zero-copy), then prepare a fresh iob for the device.
+           */
+
+          iob_add_queue(dev->d_iob, &conn->sconn.s_errahead);
+
+          /* Notify all waiters (poll and blocking recvfrom)
+           * through the standard callback chain.
+           */
+
+          udp_callback(dev, conn, UDP_NEWDATA);
+          netdev_iob_clear(dev);
+        }
+
+      dev->d_len = 0;
+      udp_conn_list_unlock();
+      return OK;
+    }
+#endif
+
   /* Update the count of UDP packets received */
 
 #ifdef CONFIG_NET_STATISTICS
diff --git a/net/udp/udp_netpoll.c b/net/udp/udp_netpoll.c
index d792170382a..1c787682840 100644
--- a/net/udp/udp_netpoll.c
+++ b/net/udp/udp_netpoll.c
@@ -101,6 +101,15 @@ static uint32_t udp_poll_eventhandler(FAR struct 
net_driver_s *dev,
           eventset |= POLLOUT;
         }
 
+#ifdef CONFIG_NET_TIMESTAMP
+      /* Check for timestamping data */
+
+      if (!IOB_QEMPTY(&info->conn->sconn.s_errahead))
+        {
+          eventset |= POLLPRI | POLLERR;
+        }
+#endif
+
       /* Awaken the caller of poll() is requested event occurred. */
 
       poll_notify(&info->fds, 1, eventset);
@@ -207,6 +216,13 @@ int udp_pollsetup(FAR struct socket *psock, FAR struct 
pollfd_s *fds)
       cb->flags |= UDP_NEWDATA;
     }
 
+#ifdef CONFIG_NET_TIMESTAMP
+  if ((fds->events & POLLPRI) != 0)
+    {
+      cb->flags |= UDP_NEWDATA;
+    }
+#endif
+
   /* Save the reference in the poll info structure as fds private as well
    * for use during poll teardown as well.
    */
@@ -222,6 +238,13 @@ int udp_pollsetup(FAR struct socket *psock, FAR struct 
pollfd_s *fds)
       eventset |= POLLRDNORM;
     }
 
+#ifdef CONFIG_NET_TIMESTAMP
+  if (!IOB_QEMPTY(&conn->sconn.s_errahead))
+    {
+      eventset |= POLLPRI | POLLERR;
+    }
+#endif
+
   if (psock_udp_cansend(conn) >= 0)
     {
       /* Normal data may be sent without blocking (at least one byte). */
diff --git a/net/udp/udp_recvfrom.c b/net/udp/udp_recvfrom.c
index 8cbf97233b8..756619876ac 100644
--- a/net/udp/udp_recvfrom.c
+++ b/net/udp/udp_recvfrom.c
@@ -431,6 +431,25 @@ static uint32_t udp_eventhandler(FAR struct net_driver_s 
*dev,
           udp_terminate(pstate, -ENETUNREACH);
         }
 
+#ifdef CONFIG_NET_TIMESTAMP
+      /* TX timestamp loopback packet: io_conn was set on the send
+       * path.  Data is already in s_errahead (queued by udp_input).
+       * Wake up MSG_ERRQUEUE waiter if present, otherwise ignore.
+       * Must be checked before normal UDP_NEWDATA handling.
+       */
+
+      else if ((flags & UDP_NEWDATA) != 0 &&
+               dev->d_iob->io_conn != NULL)
+        {
+          /* TX timestamp loopback packet: io_conn was set on the send
+           * path.  Blocking recvfrom does not handle MSG_ERRQUEUE,
+           * return -EAGAIN so the caller can retry.
+           */
+
+          udp_terminate(pstate, -EAGAIN);
+        }
+#endif
+
       /* If new data is available, then complete the read action. */
 
       else if ((flags & UDP_NEWDATA) != 0)
@@ -671,6 +690,43 @@ ssize_t psock_udp_recvfrom(FAR struct socket *psock, FAR 
struct msghdr *msg,
 
   conn_dev_lock(&conn->sconn, dev);
 
+#ifdef CONFIG_NET_TIMESTAMP
+  if (flags & MSG_ERRQUEUE)
+    {
+      if (!IOB_QEMPTY(&conn->sconn.s_errahead))
+        {
+          FAR struct iob_s *iob;
+          struct timespec ts[3];
+
+          iob = iob_remove_queue(&conn->sconn.s_errahead);
+          DEBUGASSERT(iob != NULL);
+
+          state.ir_recvlen = iob_copyout(msg->msg_iov->iov_base, iob,
+                                         msg->msg_iov->iov_len, 0);
+
+          memset(&ts, 0, sizeof(ts));
+          ts[0].tv_sec  = iob->io_time.tv_sec;
+          ts[0].tv_nsec = iob->io_time.tv_nsec;
+          ts[2].tv_sec  = iob->io_time.tv_sec;
+          ts[2].tv_nsec = iob->io_time.tv_nsec;
+
+          cmsg_append(msg, SOL_SOCKET, SO_TIMESTAMPING, &ts, sizeof(ts));
+          msg->msg_flags |= MSG_ERRQUEUE;
+
+          iob_free_chain(iob);
+          ret = state.ir_recvlen;
+        }
+      else
+        {
+          /* MSG_ERRQUEUE only supports non-blocking mode */
+
+          ret = -EAGAIN;
+        }
+
+      goto errout_with_state;
+    }
+#endif
+
   /* Copy the read-ahead data from the packet */
 
   udp_readahead(&state);
@@ -756,6 +812,9 @@ ssize_t psock_udp_recvfrom(FAR struct socket *psock, FAR 
struct msghdr *msg,
         }
     }
 
+#ifdef CONFIG_NET_TIMESTAMP
+errout_with_state:
+#endif
   conn_dev_unlock(&conn->sconn, dev);
   udp_notify_recvcpu(conn);
 
diff --git a/net/udp/udp_sendto_buffered.c b/net/udp/udp_sendto_buffered.c
index e66b40ef09a..6ecd7fd832c 100644
--- a/net/udp/udp_sendto_buffered.c
+++ b/net/udp/udp_sendto_buffered.c
@@ -466,6 +466,13 @@ static uint32_t sendto_eventhandler(FAR struct 
net_driver_s *dev,
 
       netdev_iob_replace(dev, wrb->wb_iob);
 
+#ifdef CONFIG_NET_TIMESTAMP
+      if (_SO_GETOPT(conn->sconn.s_options, SO_TIMESTAMPING))
+        {
+          dev->d_iob->io_conn = &conn->sconn;
+        }
+#endif
+
       /* Get the amount of data that we can send in the next packet.
        * We will send either the remaining data in the buffer I/O
        * buffer chain, or as much as will fit given the MSS and current
diff --git a/net/udp/udp_sendto_unbuffered.c b/net/udp/udp_sendto_unbuffered.c
index 37736aa3b5e..19e14fd5a0e 100644
--- a/net/udp/udp_sendto_unbuffered.c
+++ b/net/udp/udp_sendto_unbuffered.c
@@ -224,6 +224,15 @@ static uint32_t sendto_eventhandler(FAR struct 
net_driver_s *dev,
             }
 
           dev->d_len = dev->d_iob->io_pktlen;
+
+#ifdef CONFIG_NET_TIMESTAMP
+          if (_SO_GETOPT(pstate->st_conn->sconn.s_options,
+                         SO_TIMESTAMPING))
+            {
+              dev->d_iob->io_conn = &pstate->st_conn->sconn;
+            }
+#endif
+
 #ifdef NEED_IPDOMAIN_SUPPORT
           /* If both IPv4 and IPv6 support are enabled, then we will need to
            * select which one to use when generating the outgoing packet.

Reply via email to