Skip to content

Commit bb68283

Browse files
wenquan1zhhyu7
authored andcommitted
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 <wenquan1@xiaomi.com>
1 parent a9af1c8 commit bb68283

8 files changed

Lines changed: 196 additions & 33 deletions

File tree

‎net/devif/ipv4_input.c‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -232,23 +232,40 @@ static int ipv4_in(FAR struct net_driver_s *dev)
232232
bool isfrag;
233233
int ret = OK;
234234

235-
/* Storing reception timestamp provided by realtime
236-
* if timestamp no provided by hardware.
235+
/* This is where the input processing starts.
236+
*
237+
* Get the size of the packet minus the size of link layer header,
238+
* and make sure that all packet processing logic knows that there
239+
* is an IPv4 packet in the device buffer.
237240
*/
238241

242+
dev->d_len -= NET_LL_HDRLEN(dev);
243+
IFF_SET_IPv4(dev->d_flags);
244+
239245
#ifdef CONFIG_NET_TIMESTAMP
246+
#ifdef NET_UDP_HAVE_STACK
247+
if (dev->d_iob->io_conn != NULL)
248+
{
249+
/* This is a TX timestamp clone looped back by the driver.
250+
* Skip all validation (dest IP, checksum, etc.) and deliver
251+
* it to the originating socket's error queue directly.
252+
*/
253+
254+
udp_ipv4_input(dev);
255+
goto drop;
256+
}
257+
#endif
258+
240259
if ((dev->d_features & NETDEV_RX_STAMP) == 0)
241260
{
242261
clock_gettime(CLOCK_REALTIME, &dev->d_iob->io_time);
243262
}
244-
#endif /* CONFIG_NET_TIMESTAMP */
263+
#endif
245264

246265
/* Handle ARP on input then give the IPv4 packet to the network layer */
247266

248267
arp_ipin(dev);
249268

250-
/* This is where the input processing starts. */
251-
252269
#ifdef CONFIG_NET_STATISTICS
253270
g_netstats.ipv4.recv++;
254271
#endif
@@ -275,22 +292,12 @@ static int ipv4_in(FAR struct net_driver_s *dev)
275292
goto drop;
276293
}
277294

278-
/* Get the size of the packet minus the size of link layer header */
279-
280-
dev->d_len -= NET_LL_HDRLEN(dev);
281-
282295
if (((ipv4->vhl & IPv4_HLMASK) << 2) > dev->d_len)
283296
{
284297
nwarn("WARNING: Packet shorter than IPv4 header\n");
285298
goto drop;
286299
}
287300

288-
/* Make sure that all packet processing logic knows that there is an IPv4
289-
* packet in the device buffer.
290-
*/
291-
292-
IFF_SET_IPv4(dev->d_flags);
293-
294301
/* Check the size of the packet. If the size reported to us in d_len is
295302
* smaller the size reported in the IP header, we assume that the packet
296303
* has been corrupted in transit. If the size of d_len is larger than the

‎net/devif/ipv6_input.c‎

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,35 @@ static int ipv6_in(FAR struct net_driver_s *dev)
229229
bool isfrag = false;
230230
#endif
231231

232-
/* Storing reception timestamp provided by realtime
233-
* if timestamp no provided by hardware.
232+
/* This is where the input processing starts.
233+
*
234+
* Get the size of the packet minus the size of link layer header,
235+
* and make sure that all packet processing logic knows that there
236+
* is an IPv6 packet in the device buffer.
234237
*/
235238

239+
dev->d_len -= NET_LL_HDRLEN(dev);
240+
IFF_SET_IPv6(dev->d_flags);
241+
236242
#ifdef CONFIG_NET_TIMESTAMP
243+
#ifdef NET_UDP_HAVE_STACK
244+
if (dev->d_iob->io_conn != NULL)
245+
{
246+
/* This is a TX timestamp clone looped back by the driver.
247+
* Skip all validation and deliver it to the originating
248+
* socket's error queue directly.
249+
*/
250+
251+
udp_ipv6_input(dev, IPv6_HDRLEN);
252+
goto drop;
253+
}
254+
#endif
255+
237256
if ((dev->d_features & NETDEV_RX_STAMP) == 0)
238257
{
239258
clock_gettime(CLOCK_REALTIME, &dev->d_iob->io_time);
240259
}
241-
#endif /* CONFIG_NET_TIMESTAMP */
242-
243-
/* This is where the input processing starts. */
260+
#endif
244261

245262
#ifdef CONFIG_NET_STATISTICS
246263
g_netstats.ipv6.recv++;
@@ -263,22 +280,12 @@ static int ipv6_in(FAR struct net_driver_s *dev)
263280
goto drop;
264281
}
265282

266-
/* Get the size of the packet minus the size of link layer header */
267-
268-
dev->d_len -= NET_LL_HDRLEN(dev);
269-
270283
if (IPv6_HDRLEN > dev->d_len)
271284
{
272285
nwarn("WARNING: Packet shorter than IPv6 header\n");
273286
goto drop;
274287
}
275288

276-
/* Make sure that all packet processing logic knows that there is an IPv6
277-
* packet in the device buffer.
278-
*/
279-
280-
IFF_SET_IPv6(dev->d_flags);
281-
282289
/* Check the size of the packet. If the size reported to us in d_len is
283290
* smaller the size reported in the IP header, we assume that the packet
284291
* has been corrupted in transit. If the size of d_len is larger than the

‎net/udp/udp_callback.c‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,11 @@ uint32_t udp_callback(FAR struct net_driver_s *dev,
304304
conn_lock(&conn->sconn);
305305
flags = devif_conn_event(dev, flags, conn->sconn.s_list);
306306

307-
if ((flags & UDP_NEWDATA) != 0)
307+
if ((flags & UDP_NEWDATA) != 0
308+
#ifdef CONFIG_NET_TIMESTAMP
309+
&& dev->d_iob->io_conn == NULL
310+
#endif
311+
)
308312
{
309313
/* Data was not handled.. dispose of it appropriately */
310314

‎net/udp/udp_input.c‎

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,11 @@
5555
#include <nuttx/net/netstats.h>
5656

5757
#include "devif/devif.h"
58-
#include "utils/utils.h"
59-
#include "udp/udp.h"
6058
#include "icmp/icmp.h"
6159
#include "icmpv6/icmpv6.h"
60+
#include "socket/socket.h"
61+
#include "udp/udp.h"
62+
#include "utils/utils.h"
6263

6364
/****************************************************************************
6465
* Private Functions
@@ -221,6 +222,52 @@ static int udp_input(FAR struct net_driver_s *dev, unsigned int iplen)
221222
#endif
222223
int ret = OK;
223224

225+
#ifdef CONFIG_NET_TIMESTAMP
226+
/* Handle TX timestamp: if the IOB was tagged with a connection on the
227+
* send path, queue it to that connection's error-ahead buffer for
228+
* MSG_ERRQUEUE. Skip checksum and normal RX processing entirely.
229+
*/
230+
231+
if (dev->d_iob->io_conn != NULL)
232+
{
233+
udp_conn_list_lock();
234+
235+
/* Validate that the connection is still in the active list.
236+
* The connection may have been closed and freed between the
237+
* send path (where io_conn was set) and now.
238+
*/
239+
240+
for (conn = udp_nextconn(NULL); conn != NULL;
241+
conn = udp_nextconn(conn))
242+
{
243+
if (&conn->sconn == dev->d_iob->io_conn)
244+
{
245+
break;
246+
}
247+
}
248+
249+
if (conn != NULL)
250+
{
251+
/* Move dev->d_iob directly into the error-ahead queue
252+
* (zero-copy), then prepare a fresh iob for the device.
253+
*/
254+
255+
iob_add_queue(dev->d_iob, &conn->sconn.s_errahead);
256+
257+
/* Notify all waiters (poll and blocking recvfrom)
258+
* through the standard callback chain.
259+
*/
260+
261+
udp_callback(dev, conn, UDP_NEWDATA);
262+
netdev_iob_clear(dev);
263+
}
264+
265+
dev->d_len = 0;
266+
udp_conn_list_unlock();
267+
return OK;
268+
}
269+
#endif
270+
224271
/* Update the count of UDP packets received */
225272

226273
#ifdef CONFIG_NET_STATISTICS

‎net/udp/udp_netpoll.c‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,15 @@ static uint32_t udp_poll_eventhandler(FAR struct net_driver_s *dev,
101101
eventset |= POLLOUT;
102102
}
103103

104+
#ifdef CONFIG_NET_TIMESTAMP
105+
/* Check for timestamping data */
106+
107+
if (!IOB_QEMPTY(&info->conn->sconn.s_errahead))
108+
{
109+
eventset |= POLLPRI | POLLERR;
110+
}
111+
#endif
112+
104113
/* Awaken the caller of poll() is requested event occurred. */
105114

106115
poll_notify(&info->fds, 1, eventset);
@@ -207,6 +216,13 @@ int udp_pollsetup(FAR struct socket *psock, FAR struct pollfd_s *fds)
207216
cb->flags |= UDP_NEWDATA;
208217
}
209218

219+
#ifdef CONFIG_NET_TIMESTAMP
220+
if ((fds->events & POLLPRI) != 0)
221+
{
222+
cb->flags |= UDP_NEWDATA;
223+
}
224+
#endif
225+
210226
/* Save the reference in the poll info structure as fds private as well
211227
* for use during poll teardown as well.
212228
*/
@@ -222,6 +238,13 @@ int udp_pollsetup(FAR struct socket *psock, FAR struct pollfd_s *fds)
222238
eventset |= POLLRDNORM;
223239
}
224240

241+
#ifdef CONFIG_NET_TIMESTAMP
242+
if (!IOB_QEMPTY(&conn->sconn.s_errahead))
243+
{
244+
eventset |= POLLPRI | POLLERR;
245+
}
246+
#endif
247+
225248
if (psock_udp_cansend(conn) >= 0)
226249
{
227250
/* Normal data may be sent without blocking (at least one byte). */

‎net/udp/udp_recvfrom.c‎

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,25 @@ static uint32_t udp_eventhandler(FAR struct net_driver_s *dev,
431431
udp_terminate(pstate, -ENETUNREACH);
432432
}
433433

434+
#ifdef CONFIG_NET_TIMESTAMP
435+
/* TX timestamp loopback packet: io_conn was set on the send
436+
* path. Data is already in s_errahead (queued by udp_input).
437+
* Wake up MSG_ERRQUEUE waiter if present, otherwise ignore.
438+
* Must be checked before normal UDP_NEWDATA handling.
439+
*/
440+
441+
else if ((flags & UDP_NEWDATA) != 0 &&
442+
dev->d_iob->io_conn != NULL)
443+
{
444+
/* TX timestamp loopback packet: io_conn was set on the send
445+
* path. Blocking recvfrom does not handle MSG_ERRQUEUE,
446+
* return -EAGAIN so the caller can retry.
447+
*/
448+
449+
udp_terminate(pstate, -EAGAIN);
450+
}
451+
#endif
452+
434453
/* If new data is available, then complete the read action. */
435454

436455
else if ((flags & UDP_NEWDATA) != 0)
@@ -671,6 +690,43 @@ ssize_t psock_udp_recvfrom(FAR struct socket *psock, FAR struct msghdr *msg,
671690

672691
conn_dev_lock(&conn->sconn, dev);
673692

693+
#ifdef CONFIG_NET_TIMESTAMP
694+
if (flags & MSG_ERRQUEUE)
695+
{
696+
if (!IOB_QEMPTY(&conn->sconn.s_errahead))
697+
{
698+
FAR struct iob_s *iob;
699+
struct timespec ts[3];
700+
701+
iob = iob_remove_queue(&conn->sconn.s_errahead);
702+
DEBUGASSERT(iob != NULL);
703+
704+
state.ir_recvlen = iob_copyout(msg->msg_iov->iov_base, iob,
705+
msg->msg_iov->iov_len, 0);
706+
707+
memset(&ts, 0, sizeof(ts));
708+
ts[0].tv_sec = iob->io_time.tv_sec;
709+
ts[0].tv_nsec = iob->io_time.tv_nsec;
710+
ts[2].tv_sec = iob->io_time.tv_sec;
711+
ts[2].tv_nsec = iob->io_time.tv_nsec;
712+
713+
cmsg_append(msg, SOL_SOCKET, SO_TIMESTAMPING, &ts, sizeof(ts));
714+
msg->msg_flags |= MSG_ERRQUEUE;
715+
716+
iob_free_chain(iob);
717+
ret = state.ir_recvlen;
718+
}
719+
else
720+
{
721+
/* MSG_ERRQUEUE only supports non-blocking mode */
722+
723+
ret = -EAGAIN;
724+
}
725+
726+
goto errout_with_state;
727+
}
728+
#endif
729+
674730
/* Copy the read-ahead data from the packet */
675731

676732
udp_readahead(&state);
@@ -756,6 +812,9 @@ ssize_t psock_udp_recvfrom(FAR struct socket *psock, FAR struct msghdr *msg,
756812
}
757813
}
758814

815+
#ifdef CONFIG_NET_TIMESTAMP
816+
errout_with_state:
817+
#endif
759818
conn_dev_unlock(&conn->sconn, dev);
760819
udp_notify_recvcpu(conn);
761820

‎net/udp/udp_sendto_buffered.c‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,13 @@ static uint32_t sendto_eventhandler(FAR struct net_driver_s *dev,
466466

467467
netdev_iob_replace(dev, wrb->wb_iob);
468468

469+
#ifdef CONFIG_NET_TIMESTAMP
470+
if (_SO_GETOPT(conn->sconn.s_options, SO_TIMESTAMPING))
471+
{
472+
dev->d_iob->io_conn = &conn->sconn;
473+
}
474+
#endif
475+
469476
/* Get the amount of data that we can send in the next packet.
470477
* We will send either the remaining data in the buffer I/O
471478
* buffer chain, or as much as will fit given the MSS and current

‎net/udp/udp_sendto_unbuffered.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,15 @@ static uint32_t sendto_eventhandler(FAR struct net_driver_s *dev,
224224
}
225225

226226
dev->d_len = dev->d_iob->io_pktlen;
227+
228+
#ifdef CONFIG_NET_TIMESTAMP
229+
if (_SO_GETOPT(pstate->st_conn->sconn.s_options,
230+
SO_TIMESTAMPING))
231+
{
232+
dev->d_iob->io_conn = &pstate->st_conn->sconn;
233+
}
234+
#endif
235+
227236
#ifdef NEED_IPDOMAIN_SUPPORT
228237
/* If both IPv4 and IPv6 support are enabled, then we will need to
229238
* select which one to use when generating the outgoing packet.

0 commit comments

Comments
 (0)