Skip to content

net/udp: Fix read-ahead accounting and add SO_TIMESTAMPING on the TX path - #22231

Merged
GUIDINGLI merged 6 commits into
apache:masterfrom
zhhyu7:upstream-2
Nov 25, 2026
Merged

GUIDINGLI merged 6 commits into
apache:masterfrom
zhhyu7:upstream-2

Conversation

@zhhyu7

@zhhyu7 zhhyu7 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

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:

  • errahead moves from pkt_conn_s to socket_conn_s as s_errahead, so more than one protocol can use the error queue. The PKT users are updated in the same commit.
  • Outgoing IOBs are tagged with io_conn in both the buffered and the unbuffered send path; ipv4_input() and ipv6_input() recognise a TX timestamp clone by that field and go straight to protocol dispatch;
    udp_input() matches the echo back to the socket and queues it on s_errahead; psock_udp_recvfrom() serves it on MSG_ERRQUEUE.
  • The last commit fixes two pre-existing checkpatch errors in udp_input.c, a file this series touches.

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.

@github-actions github-actions Bot added Area: Networking Effects networking subsystem Size: M The size of the change in this PR is medium labels Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown

GUIDINGLI
GUIDINGLI previously approved these changes Nov 24, 2026
jerpelea
jerpelea previously approved these changes Nov 24, 2026
HedongGao and others added 6 commits November 24, 2026 20:48
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>
@GUIDINGLI
GUIDINGLI merged commit bba4bd2 into apache:master Nov 25, 2026
59 of 66 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area: Networking Effects networking subsystem Size: M The size of the change in this PR is medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants