Repository navigation
net/udp: Fix read-ahead accounting and add SO_TIMESTAMPING on the TX path - #22231
Merged
Merged
Conversation
zhhyu7
requested review from
Donny9,
btashton,
jerpelea,
pkarashchenko and
xiaoxiang781216
as code owners
November 24, 2026 02:38
|
xiaoxiang781216
previously approved these changes
Nov 24, 2026
xiaoxiang781216
previously approved these changes
Nov 24, 2026
GUIDINGLI
previously approved these changes
Nov 24, 2026
jerpelea
previously approved these changes
Nov 24, 2026
Coverity CID 3934125 / 3934127 (MISRA C-2012 Rule 9.1): src_addr_size and srcaddr are read before being explicitly initialized. CID 3934125 points at the iob_copyout() call that uses src_addr_size as the copy length, CID 3934127 at the memcpy() that copies srcaddr into msg_name. Both are filled in by an earlier iob_copyout(), but iob_copyout() has two early returns (-ESPIPE on a bad offset, 0 when the iob chain is exhausted) that write nothing, so Rule 9.1 is violated on those paths. In practice those paths are unreachable here: udp_callback() writes the whole |datalen|ifindex|src_addr_size|src_addr| header into one iob with consecutive iob_trycopyin() calls and drops the packet entirely if any of them fails, so the reader always finds a complete fixed-size header. The DEBUGASSERT() checks that follow cannot stand in for initialization either - they run after the read and compile to nothing when CONFIG_DEBUG_ASSERTIONS is disabled. This change is therefore a static-compliance fix with no runtime behaviour change. Note the earlier attempt (change 7995723) initialized only srcaddr and left src_addr_size untouched, which is why CID 3934125 kept recurring. Signed-off-by: gaohedong <gaohedong@xiaomi.com>
udp_callback() drops an incoming datagram when the read-ahead buffer already holds more than SO_RCVBUF bytes, but returned without touching the statistics. The drop was therefore invisible in both /proc/net/stat and the netdev statistics, while the neighbouring "no free read-ahead buffers" path does count it. Count the datagram and log it, as that path already does. Assisted-by: Kiro:claude-opus-4 Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
The IPv4 send paths in tcp_sendcommon() and udp_send() built the outgoing IP header with dev->d_ipaddr as the source address. On a multi-interface host this is wrong when the connection's bound local IP lives on a different interface than the one the packet is leaving on: the peer sees a source IP that does not match the destination it originally addressed and rejects the segment. Use conn->u.ipv4.laddr instead, mirroring the existing IPv6 paths in the same functions which use netdev_ipv6_srcaddr() with the same intent. TCP can use laddr unconditionally because it is always populated (tcp_alloc_accept copies ip->destipaddr; tcp_connect fills from conn->dev->d_ipaddr when INADDR_ANY). UDP keeps a fallback to dev->d_ipaddr because INADDR_ANY-bound sockets are common. Single-interface devices and INADDR_ANY-bound UDP sockets are unaffected: the new path produces the same value as before. Signed-off-by: zhangshuai39 <zhangshuai39@xiaomi.com>
Move the error-ahead queue (errahead) from pkt_conn_s to the common socket_conn_s as s_errahead. This allows other protocol stacks (e.g. UDP) to reuse the same error queue for TX timestamping support. Signed-off-by: wenquan1 <wenquan1@xiaomi.com>
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>
checkpatch reports a missing blank line after a declaration in udp_is_mcast() and misaligned code inside the #ifdef-guarded destination-unreachable block. Both are pre-existing, in a file that this series touches. Assisted-by: Kiro:claude-opus-4 Signed-off-by: zhanghongyu <zhanghongyu@xiaomi.com>
zhhyu7
dismissed stale reviews from jerpelea, GUIDINGLI, and xiaoxiang781216
via
November 24, 2026 13:27
1f42289
xiaoxiang781216
approved these changes
Nov 25, 2026
GUIDINGLI
approved these changes
Nov 25, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Six changes to the UDP stack, picked from a downstream tree and adapted.
Two are read-ahead fixes:
udp_readahead() read src_addr_size and srcaddr before initializing them (Coverity CID 3934125 / 3934127, MISRA C-2012 Rule 9.1). Both are filled in by an earlier iob_copyout(), but that function has two
early returns, -ESPIPE on a bad offset and 0 on an exhausted chain, which write nothing. The paths are unreachable today because udp_callback() always writes the metadata, so this removes a latent trap
rather than a live bug.
udp_callback() drops a datagram when the read-ahead buffer already holds more than SO_RCVBUF bytes, but returned without touching the statistics, so the loss was invisible in /proc/net/stat. The adjacent
"no free read-ahead buffers" path does count it. It now counts and logs the drop the same way.
One is a source-address fix. The IPv4 send paths in tcp_sendcommon() and udp_send() built the IP header with dev->d_ipaddr. On a multi-interface host that is wrong when the connection's bound local
address lives on a different interface than the one the packet leaves on: the peer sees a source that does not match the destination it addressed and rejects the datagram. They now use the connection's
bound local address, which is what the IPv6 paths in the same two functions already do via netdev_ipv6_srcaddr().
The remaining three add SO_TIMESTAMPING on the UDP TX path, following the pattern the PKT sockets already use:
udp_input() matches the echo back to the socket and queues it on s_errahead; psock_udp_recvfrom() serves it on MSG_ERRQUEUE.
Where the downstream version depended on infrastructure this tree does not have, the logic was rewritten against what is here rather than pulling in more commits. In particular the timestamp control
message is appended inline with cmsg_append(), exactly as append_timestamping() in net/pkt/pkt_recvmsg.c does, instead of introducing a new helper.
Impact
struct socket_conn_s gains s_errahead under CONFIG_NET_TIMESTAMP and struct pkt_conn_s loses errahead. Out-of-tree code that touches pkt_conn_s::errahead directly must use conn->sconn.s_errahead instead.
All in-tree users are converted here.
The IPv4 source-address change alters which address appears in outgoing headers for a socket bound to a specific local address on a multi-interface host. That is the point of the fix, but it is a visible
behaviour change on such setups. A socket bound to INADDR_ANY keeps using dev->d_ipaddr and is unaffected.
The new drop counter makes a previously silent loss show up in the UDP Dropped column. Anything that parses those statistics will see a number that was always zero start to move.
SO_TIMESTAMPING on UDP is new functionality behind CONFIG_NET_TIMESTAMP, which is off by default. With it off the only remaining changes are the two read-ahead fixes, the source-address fix and the style
fix.
Testing
sim:dynconns with CONFIG_SIM_NET_BRIDGE=y, CONFIG_DEBUG_ASSERTIONS=y, CONFIG_NET_STATISTICS=y, tcpecho on port 80 and apps/examples/udp with EXAMPLES_UDP_SERVER1=y on port 5471. Host bridge nuttx0
10.0.1.1/24, target 10.0.1.2/24. Built and run both with CONFIG_NET_TIMESTAMP=n and =y; the results below are identical for both.
UDP, 256 datagrams. The target's udpserver validates the sequence byte and the payload pattern of every datagram and exits on the first mismatch:
udp client: sent 256 datagrams of 96 bytes (24576 bytes payload)
datagrams accepted by target: 256
server error lines: none
Malformed IPv4 input, sent from a raw socket with a forged ip.total_len, to confirm the read-ahead paths stay sane on input the stack must reject:
ip.total_len=4000 with only 60 bytes on the wire -> rejected
ip.total_len=10 (smaller than the IP header) -> rejected
node responsive afterwards: 3 packets transmitted, 3 received, 0% packet loss
Statistics after the run:
UDP Received 0105 Dropped 0000
IPv4 Received 011a Dropped 0014 (includes the two malformed datagrams)
TCP unaffected, 4 concurrent connections echoing 256 KiB each:
conn0..conn3: sent=262144 recv=262144 err=None
ICMP: 4 transmitted, 4 received, 0% packet loss. No assertions, no segmentation fault, node alive after every run.