This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 5814b404c95fe3d4051ac6fc4d8173797ae2c5c0 Author: raiden00pl <[email protected]> AuthorDate: Fri Nov 20 09:21:14 2026 +0100 net/tcp: limit the receive window after a loss. The peer sends a whole receive window at line rate. A device that buffers fewer frames and receives slower than the link drops the rest, and without out-of-order buffering TCP loses all of the window after the first lost frame. With 200 IOBs nucleo-f207zg advertised 15 segments for its 8 RX frames and received at 2 Mbit/s. A segment after a gap shows that frames were lost. Halve the window that the connection advertises, then let it grow by one segment per window up to one segment less than the window that lost frames. nucleo-f207zg with 200 IOBs receives at 9.2 Mbit/s, as with 100 IOBs. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: raiden00pl <[email protected]> --- net/tcp/tcp.h | 20 +++++++++++++++ net/tcp/tcp_input.c | 2 ++ net/tcp/tcp_recvwindow.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+) diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h index 067d34cdbd1..432eecc6dbd 100644 --- a/net/tcp/tcp.h +++ b/net/tcp/tcp.h @@ -256,6 +256,10 @@ struct tcp_conn_s * connection */ #endif uint32_t rcv_adv; /* The right edge of the recv window advertised */ + uint32_t rcv_clamp; /* Recv window limit after a loss, 0: none */ + uint32_t rcv_clampseq; /* rcvseq to grow rcv_clamp at */ + uint32_t rcv_lossseq; /* rcv_adv at the last loss */ + uint32_t rcv_clampmax; /* rcv_clamp limit below the lost windows */ #ifdef CONFIG_NET_TCP_CC_NEWRENO uint32_t last_ackno; /* The ack number at the last receive ack */ uint32_t dupacks; /* The number of duplicate ack */ @@ -1847,6 +1851,22 @@ int tcp_getsockopt(FAR struct socket *psock, int option, FAR void *value, FAR socklen_t *value_len); #endif +/**************************************************************************** + * Name: tcp_recvwindow_loss + * + * Description: + * A segment arrived after a gap: the frames in front of it were lost. + * Limit the receive window that the connection advertises. + * + * Input Parameters: + * dev - The device that received the segment. + * conn - The TCP connection. + * + ****************************************************************************/ + +void tcp_recvwindow_loss(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn); + /**************************************************************************** * Name: tcp_get_recvwindow * diff --git a/net/tcp/tcp_input.c b/net/tcp/tcp_input.c index eac4414138b..ad0caa3b3a6 100644 --- a/net/tcp/tcp_input.c +++ b/net/tcp/tcp_input.c @@ -1468,6 +1468,8 @@ skip_rtt: } else if ((conn->tcpstateflags & TCP_STATE_MASK) <= TCP_FIN_WAIT_2) { + tcp_recvwindow_loss(dev, conn); + #ifdef CONFIG_NET_TCP_OUT_OF_ORDER /* Queue out-of-order segments. */ diff --git a/net/tcp/tcp_recvwindow.c b/net/tcp/tcp_recvwindow.c index 26ac55ff871..e2f2dd2c5f7 100644 --- a/net/tcp/tcp_recvwindow.c +++ b/net/tcp/tcp_recvwindow.c @@ -28,6 +28,7 @@ #include <stdint.h> #include <stdbool.h> +#include <sys/param.h> #include <nuttx/debug.h> #include <net/if.h> @@ -162,6 +163,51 @@ static uint32_t tcp_maxrcvwin(FAR struct tcp_conn_s *conn) * Public Functions ****************************************************************************/ +/**************************************************************************** + * Name: tcp_recvwindow_loss + * + * Description: + * A segment arrived after a gap: the frames in front of it were lost. + * Limit the receive window that the connection advertises. + * + * Input Parameters: + * dev - The device that received the segment. + * conn - The TCP connection. + * + ****************************************************************************/ + +void tcp_recvwindow_loss(FAR struct net_driver_s *dev, + FAR struct tcp_conn_s *conn) +{ + uint32_t rcvseq = tcp_getsequence(conn->rcvseq); + uint32_t mss = tcp_rx_mss(dev); + uint32_t wnd = 0; + + /* The other segments of the same window are lost with the first one: + * limit the window once per window. + */ + + if (conn->rcv_clamp != 0 && TCP_SEQ_LT(rcvseq, conn->rcv_lossseq)) + { + return; + } + + if (TCP_SEQ_GT(conn->rcv_adv, rcvseq)) + { + wnd = TCP_SEQ_SUB(conn->rcv_adv, rcvseq); + } + + if (conn->rcv_clamp != 0 && conn->rcv_clamp < wnd) + { + wnd = conn->rcv_clamp; + } + + conn->rcv_clampmax = MAX(wnd > mss ? wnd - mss : 0, 2 * mss); + conn->rcv_clamp = MAX(wnd / 2, 2 * mss); + conn->rcv_clampseq = conn->rcv_adv; + conn->rcv_lossseq = conn->rcv_adv; +} + /**************************************************************************** * Name: tcp_get_recvwindow * @@ -263,6 +309,25 @@ uint32_t tcp_get_recvwindow(FAR struct net_driver_s *dev, recvwndo = tailroom; } + /* After a loss the window grows by one segment per window, up to one + * segment less than the window that lost frames. + */ + + if (conn->rcv_clamp != 0) + { + uint32_t rcvseq = tcp_getsequence(conn->rcvseq); + + if (TCP_SEQ_GTE(rcvseq, conn->rcv_clampseq) && + conn->rcv_clamp < conn->rcv_clampmax) + { + conn->rcv_clamp = MIN(conn->rcv_clamp + mss, + conn->rcv_clampmax); + conn->rcv_clampseq = rcvseq + conn->rcv_clamp; + } + + recvwndo = MIN(recvwndo, conn->rcv_clamp); + } + recvwndo = tcp_calc_rcvsize(conn, recvwndo); #ifdef CONFIG_NET_TCP_WINDOW_SCALE
