This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 24457324fdb17fab752ff57b7fae833e3f2c6a0d Author: Marco Casaroli <[email protected]> AuthorDate: Fri Nov 20 17:19:08 2026 +0100 net/tcp: Limit the buffers of a connection by default. Since ea5d2c9ae49, tcp_datahandler() drops the received data while fewer than CONFIG_IOB_THROTTLE IOBs are free. With CONFIG_NET_SEND_BUFSIZE and CONFIG_NET_RECV_BUFSIZE at 0 (the default), one connection can take all IOBs above the throttle with its write buffers or with its read-ahead. Over the loopback device the write buffers of the sender then hold the pool, the receiver drops every segment, and the transfer stops; the write buffers free their IOBs only when the dropped data is acknowledged. In the same way the read-ahead of one connection that the application does not read can take the IOBs of all other connections. When the options are 0, a TCP connection now uses a default limit for its write buffers and for its read-ahead: 1/8 of the IOBs above the throttle each, and at least one segment of the largest MSS. One connection then cannot take the pool: the receiver of a local transfer finds IOBs, and other connections keep theirs. The drop of ea5d2c9ae49 stays for the case that many connections use the pool together. The code of the options is now always compiled in TCP: the send wait in psock_tcp_send(), POLLOUT, FIONSPACE and the window of tcp_calc_rcvsize(). SO_RCVBUF and SO_SNDBUF (with write buffers) are now available for TCP sockets also when the options are 0, so that an application can change the limit of a connection. FIONSPACE no longer reports a negative space when SO_SNDBUF is set below the queued data. UDP is not changed: its socket options and limits still need the options > 0. psock_tcp_send() checked the limit only before each write buffer, and a write buffer takes up to four segments (tcp_max_wrb_size()): one step could queue almost four segments beyond the limit. With the default limit, two threads that send to each other over the loopback device at the same time then took all IOBs with their write buffers, the received segments were dropped, and the transfer went on only at the retransmission timeouts. A step now queues no more than the limit allows. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- net/Kconfig | 9 +++++++-- net/inet/inet_sockif.c | 42 ++++++++++++++++++++++++++++++------------ net/procfs/net_tcp.c | 6 +++--- net/tcp/tcp.h | 34 ++++++++++++++++++++++++++++------ net/tcp/tcp_conn.c | 18 ++++-------------- net/tcp/tcp_ioctl.c | 13 +++++-------- net/tcp/tcp_recvwindow.c | 2 -- net/tcp/tcp_send_buffered.c | 26 +++++++++++++------------- net/tcp/tcp_wrbuffer.c | 2 -- 9 files changed, 90 insertions(+), 62 deletions(-) diff --git a/net/Kconfig b/net/Kconfig index 5df57c7b015..b38f7d027aa 100644 --- a/net/Kconfig +++ b/net/Kconfig @@ -184,7 +184,9 @@ config NET_RECV_BUFSIZE int "Net Default Receive buffer size" default 0 ---help--- - This is the default value for receive buffer size. + This is the default value for receive buffer size. With 0, a TCP + connection uses 1/8 of the IOBs above IOB_THROTTLE, and at least + one segment of the largest MSS; other sockets have no limit. config NET_MAX_RECV_BUFSIZE int "Net Max Receive buffer size" @@ -199,7 +201,10 @@ config NET_SEND_BUFSIZE depends on NET_TCP_WRITE_BUFFERS || NET_UDP_WRITE_BUFFERS || NET_CAN_WRITE_BUFFERS default 0 ---help--- - This is the default value for send buffer size. + This is the default value for send buffer size. With 0, the TCP + write buffers of a connection use 1/8 of the IOBs above + IOB_THROTTLE, and at least one segment of the largest MSS; other + sockets have no limit. config NET_MAX_SEND_BUFSIZE int "Net Max Send buffer size" diff --git a/net/inet/inet_sockif.c b/net/inet/inet_sockif.c index ae5a65f6e22..4ad2c15e5d0 100644 --- a/net/inet/inet_sockif.c +++ b/net/inet/inet_sockif.c @@ -62,6 +62,24 @@ union sockaddr_u struct sockaddr_in6 in6addr; }; +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* SO_RCVBUF and SO_SNDBUF: a TCP connection always has a receive limit, + * and a send limit with write buffers (net/tcp/tcp.h). UDP has them only + * when CONFIG_NET_RECV_BUFSIZE or CONFIG_NET_SEND_BUFSIZE is > 0. + */ + +#if CONFIG_NET_RECV_BUFSIZE > 0 || defined(NET_TCP_HAVE_STACK) +# define INET_HAVE_RCVBUF 1 +#endif + +#if CONFIG_NET_SEND_BUFSIZE > 0 || \ + (defined(NET_TCP_HAVE_STACK) && defined(CONFIG_NET_TCP_WRITE_BUFFERS)) +# define INET_HAVE_SNDBUF 1 +#endif + /**************************************************************************** * Private Function Prototypes ****************************************************************************/ @@ -649,7 +667,7 @@ static int inet_get_socketlevel_option(FAR struct socket *psock, int option, { switch (option) { -#if CONFIG_NET_RECV_BUFSIZE > 0 +#ifdef INET_HAVE_RCVBUF case SO_RCVBUF: /* Reports receive buffer size */ { if (*value_len != sizeof(int)) @@ -666,7 +684,7 @@ static int inet_get_socketlevel_option(FAR struct socket *psock, int option, } else #endif -#ifdef NET_UDP_HAVE_STACK +#if defined(NET_UDP_HAVE_STACK) && CONFIG_NET_RECV_BUFSIZE > 0 if (psock->s_type == SOCK_DGRAM) { FAR struct udp_conn_s *udp = psock->s_conn; @@ -682,7 +700,7 @@ static int inet_get_socketlevel_option(FAR struct socket *psock, int option, break; #endif -#if CONFIG_NET_SEND_BUFSIZE > 0 +#ifdef INET_HAVE_SNDBUF case SO_SNDBUF: /* Reports send buffer size */ { if (*value_len != sizeof(int)) @@ -690,7 +708,7 @@ static int inet_get_socketlevel_option(FAR struct socket *psock, int option, return -EINVAL; } -#ifdef NET_TCP_HAVE_STACK +#if defined(NET_TCP_HAVE_STACK) && defined(CONFIG_NET_TCP_WRITE_BUFFERS) if (psock->s_type == SOCK_STREAM) { FAR struct tcp_conn_s *tcp = psock->s_conn; @@ -699,7 +717,7 @@ static int inet_get_socketlevel_option(FAR struct socket *psock, int option, } else #endif -#ifdef NET_UDP_HAVE_STACK +#if defined(NET_UDP_HAVE_STACK) && CONFIG_NET_SEND_BUFSIZE > 0 if (psock->s_type == SOCK_DGRAM) { FAR struct udp_conn_s *udp = psock->s_conn; @@ -900,7 +918,7 @@ static int inet_set_socketlevel_option(FAR struct socket *psock, int option, break; #endif -#if CONFIG_NET_RECV_BUFSIZE > 0 +#ifdef INET_HAVE_RCVBUF case SO_RCVBUF: /* Sets receive buffer size */ { int buffersize; @@ -922,7 +940,7 @@ static int inet_set_socketlevel_option(FAR struct socket *psock, int option, return -EINVAL; } -#if CONFIG_NET_MAX_RECV_BUFSIZE > 0 +#if defined(CONFIG_NET_MAX_RECV_BUFSIZE) && CONFIG_NET_MAX_RECV_BUFSIZE > 0 buffersize = MIN(buffersize, CONFIG_NET_MAX_RECV_BUFSIZE); #endif @@ -939,7 +957,7 @@ static int inet_set_socketlevel_option(FAR struct socket *psock, int option, } else #endif -#ifdef NET_UDP_HAVE_STACK +#if defined(NET_UDP_HAVE_STACK) && CONFIG_NET_RECV_BUFSIZE > 0 if (psock->s_type == SOCK_DGRAM) { FAR struct udp_conn_s *udp = psock->s_conn; @@ -960,7 +978,7 @@ static int inet_set_socketlevel_option(FAR struct socket *psock, int option, break; #endif -#if CONFIG_NET_SEND_BUFSIZE > 0 +#ifdef INET_HAVE_SNDBUF case SO_SNDBUF: /* Sets send buffer size */ { int buffersize; @@ -983,13 +1001,13 @@ static int inet_set_socketlevel_option(FAR struct socket *psock, int option, return -EINVAL; } -#if CONFIG_NET_MAX_SEND_BUFSIZE > 0 +#if defined(CONFIG_NET_MAX_SEND_BUFSIZE) && CONFIG_NET_MAX_SEND_BUFSIZE > 0 buffersize = MIN(buffersize, CONFIG_NET_MAX_SEND_BUFSIZE); #endif conn_lock(psock->s_conn); -#ifdef NET_TCP_HAVE_STACK +#if defined(NET_TCP_HAVE_STACK) && defined(CONFIG_NET_TCP_WRITE_BUFFERS) if (psock->s_type == SOCK_STREAM) { FAR struct tcp_conn_s *tcp = psock->s_conn; @@ -1000,7 +1018,7 @@ static int inet_set_socketlevel_option(FAR struct socket *psock, int option, } else #endif -#ifdef NET_UDP_HAVE_STACK +#if defined(NET_UDP_HAVE_STACK) && CONFIG_NET_SEND_BUFSIZE > 0 if (psock->s_type == SOCK_DGRAM) { FAR struct udp_conn_s *udp = psock->s_conn; diff --git a/net/procfs/net_tcp.c b/net/procfs/net_tcp.c index 5e08ff7a5c4..33563c86f29 100644 --- a/net/procfs/net_tcp.c +++ b/net/procfs/net_tcp.c @@ -97,7 +97,7 @@ static ssize_t netprocfs_tcpstats(FAR struct netprocfs_file_s *priv, " %3" PRIu8 " %4" PRIu32 " %3" PRIu8 -#if CONFIG_NET_SEND_BUFSIZE > 0 +#if CONFIG_NET_SEND_BUFSIZE > 0 && defined(CONFIG_NET_TCP_WRITE_BUFFERS) " %6" PRIu32 #endif " %6u", @@ -108,7 +108,7 @@ static ssize_t netprocfs_tcpstats(FAR struct netprocfs_file_s *priv, conn->timer, (uint32_t)conn->tx_unacked, conn->nrtx, -#if CONFIG_NET_SEND_BUFSIZE > 0 +#if CONFIG_NET_SEND_BUFSIZE > 0 && defined(CONFIG_NET_TCP_WRITE_BUFFERS) tcp_wrbuffer_inqueue_size(conn), #endif (conn->readahead) ? conn->readahead->io_pktlen : 0); @@ -162,7 +162,7 @@ ssize_t netprocfs_read_tcpstats(FAR struct netprocfs_file_s *priv, { len = snprintf(buffer, buflen, "TCP sl " "st flg ref tmr uack nrt " -#if CONFIG_NET_SEND_BUFSIZE > 0 +#if CONFIG_NET_SEND_BUFSIZE > 0 && defined(CONFIG_NET_TCP_WRITE_BUFFERS) "txsz " #endif "rxsz " diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h index c739f302538..189a26ef6b5 100644 --- a/net/tcp/tcp.h +++ b/net/tcp/tcp.h @@ -100,6 +100,30 @@ #define TCP_SEQ_ADD(a, b) ((uint32_t)((a) + (b))) #define TCP_SEQ_SUB(a, b) ((uint32_t)((a) - (b))) +/* The send and receive buffer size of a connection. When + * CONFIG_NET_SEND_BUFSIZE or CONFIG_NET_RECV_BUFSIZE is 0, a connection + * may queue in its write buffers and in its read-ahead 1/8 of the IOBs + * above the throttle each, but at least one segment of the largest MSS. + * So one connection cannot take all free IOBs. + */ + +#define TCP_IOB_BUFSIZE \ + ((CONFIG_IOB_NBUFFERS - CONFIG_IOB_THROTTLE) * CONFIG_IOB_BUFSIZE / 8) +#define TCP_DEFAULT_BUFSIZE \ + (TCP_IOB_BUFSIZE > MAX_TCP_MSS ? TCP_IOB_BUFSIZE : MAX_TCP_MSS) + +#if CONFIG_NET_RECV_BUFSIZE > 0 +# define TCP_RECV_BUFSIZE CONFIG_NET_RECV_BUFSIZE +#else +# define TCP_RECV_BUFSIZE TCP_DEFAULT_BUFSIZE +#endif + +#if CONFIG_NET_SEND_BUFSIZE > 0 +# define TCP_SEND_BUFSIZE CONFIG_NET_SEND_BUFSIZE +#else +# define TCP_SEND_BUFSIZE TCP_DEFAULT_BUFSIZE +#endif + /* The error of a send on a TCP socket that is not connected: -ECONNRESET * if the peer reset the connection and this is not yet reported, -EPIPE if * the connection was closed (POSIX: the socket is no longer connected), @@ -280,10 +304,8 @@ struct tcp_conn_s #endif uint32_t snd_wl1; uint32_t snd_wl2; -#if CONFIG_NET_RECV_BUFSIZE > 0 int32_t rcv_bufs; /* Maximum amount of bytes queued in recv */ -#endif -#if CONFIG_NET_SEND_BUFSIZE > 0 +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS int32_t snd_bufs; /* Maximum amount of bytes queued in send */ sem_t snd_sem; /* Semaphore signals send completion */ #endif @@ -2003,7 +2025,7 @@ void tcp_wrbuffer_release(FAR struct tcp_wrbuffer_s *wrb); * ****************************************************************************/ -#if CONFIG_NET_SEND_BUFSIZE > 0 +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS uint32_t tcp_wrbuffer_inqueue_size(FAR struct tcp_conn_s *conn); #endif @@ -2329,9 +2351,9 @@ int tcp_ioctl(FAR struct tcp_conn_s *conn, int cmd, unsigned long arg); * ****************************************************************************/ -#if CONFIG_NET_SEND_BUFSIZE > 0 +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS void tcp_sendbuffer_notify(FAR struct tcp_conn_s *conn); -#endif /* CONFIG_NET_SEND_BUFSIZE */ +#endif /**************************************************************************** * Name: tcpip_hdrsize diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c index 2a5d792f57e..602a5a0ae89 100644 --- a/net/tcp/tcp_conn.c +++ b/net/tcp/tcp_conn.c @@ -839,11 +839,9 @@ static FAR struct tcp_conn_s *tcp_alloc_conn(uint8_t domain, conn->keepintvl = 2 * DSEC_PER_SEC; conn->keepcnt = 3; #endif -#if CONFIG_NET_RECV_BUFSIZE > 0 - conn->rcv_bufs = CONFIG_NET_RECV_BUFSIZE; -#endif -#if CONFIG_NET_SEND_BUFSIZE > 0 - conn->snd_bufs = CONFIG_NET_SEND_BUFSIZE; + conn->rcv_bufs = TCP_RECV_BUFSIZE; +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS + conn->snd_bufs = TCP_SEND_BUFSIZE; nxsem_init(&conn->snd_sem, 0, 0); #endif @@ -1005,15 +1003,9 @@ void tcp_free(FAR struct tcp_conn_s *conn) tcp_wrbuffer_release(wrbuffer); } -#if CONFIG_NET_SEND_BUFSIZE > 0 /* Notify the send buffer available */ tcp_sendbuffer_notify(conn); -#endif /* CONFIG_NET_SEND_BUFSIZE */ - -#endif - -#if CONFIG_NET_SEND_BUFSIZE > 0 nxsem_destroy(&conn->snd_sem); #endif @@ -1296,10 +1288,8 @@ FAR struct tcp_conn_s *tcp_alloc_accept(FAR struct net_driver_s *dev, conn->sconn.s_tos = listener->sconn.s_tos; conn->sconn.s_ttl = listener->sconn.s_ttl; -#if CONFIG_NET_RECV_BUFSIZE > 0 conn->rcv_bufs = listener->rcv_bufs; -#endif -#if CONFIG_NET_SEND_BUFSIZE > 0 +#ifdef CONFIG_NET_TCP_WRITE_BUFFERS conn->snd_bufs = listener->snd_bufs; #endif conn->mss = listener->mss; diff --git a/net/tcp/tcp_ioctl.c b/net/tcp/tcp_ioctl.c index fd6cbbf595f..39ad37d95c5 100644 --- a/net/tcp/tcp_ioctl.c +++ b/net/tcp/tcp_ioctl.c @@ -68,7 +68,7 @@ static void tcp_path(FAR struct tcp_conn_s *conn, FAR char *buf, size_t len) snprintf(buf, len, "tcp:[" "%s:%" PRIu16 "<->%s:%" PRIu16 -#if CONFIG_NET_SEND_BUFSIZE > 0 +#if CONFIG_NET_SEND_BUFSIZE > 0 && defined(CONFIG_NET_TCP_WRITE_BUFFERS) ", tx %" PRIu32 "/%" PRId32 #endif #if CONFIG_NET_RECV_BUFSIZE > 0 @@ -84,7 +84,7 @@ static void tcp_path(FAR struct tcp_conn_s *conn, FAR char *buf, size_t len) ntohs(conn->lport), inet_ntop(domain, raddr, remote, sizeof(remote)), ntohs(conn->rport), -#if CONFIG_NET_SEND_BUFSIZE > 0 +#if CONFIG_NET_SEND_BUFSIZE > 0 && defined(CONFIG_NET_TCP_WRITE_BUFFERS) tcp_wrbuffer_inqueue_size(conn), conn->snd_bufs, #endif @@ -138,13 +138,10 @@ int tcp_ioctl(FAR struct tcp_conn_s *conn, int cmd, unsigned long arg) break; case FIONSPACE: #ifdef CONFIG_NET_TCP_WRITE_BUFFERS -# if CONFIG_NET_SEND_BUFSIZE == 0 - *(FAR int *)((uintptr_t)arg) = - iob_navail(true) * CONFIG_IOB_BUFSIZE; -# else + /* SO_SNDBUF can be set below the data already queued */ + *(FAR int *)((uintptr_t)arg) = - conn->snd_bufs - tcp_wrbuffer_inqueue_size(conn); -# endif + MAX(conn->snd_bufs - (int32_t)tcp_wrbuffer_inqueue_size(conn), 0); #else *(FAR int *)((uintptr_t)arg) = MIN_TCP_MSS; #endif diff --git a/net/tcp/tcp_recvwindow.c b/net/tcp/tcp_recvwindow.c index aa6553f51ee..c7a5febc86e 100644 --- a/net/tcp/tcp_recvwindow.c +++ b/net/tcp/tcp_recvwindow.c @@ -62,7 +62,6 @@ static uint32_t tcp_calc_rcvsize(FAR struct tcp_conn_s *conn, uint32_t recvwndo) { -#if CONFIG_NET_RECV_BUFSIZE > 0 uint32_t recvsize; uint32_t desire; @@ -79,7 +78,6 @@ static uint32_t tcp_calc_rcvsize(FAR struct tcp_conn_s *conn, { recvwndo = 0; } -#endif return recvwndo; } diff --git a/net/tcp/tcp_send_buffered.c b/net/tcp/tcp_send_buffered.c index cba7ffb35c3..bf2f7bc66c1 100644 --- a/net/tcp/tcp_send_buffered.c +++ b/net/tcp/tcp_send_buffered.c @@ -263,11 +263,9 @@ static inline void psock_lost_connection(FAR struct tcp_conn_s *conn, tcp_wrbuffer_release((FAR struct tcp_wrbuffer_s *)entry); } -#if CONFIG_NET_SEND_BUFSIZE > 0 /* Notify the send buffer available */ tcp_sendbuffer_notify(conn); -#endif /* CONFIG_NET_SEND_BUFSIZE */ /* Reset write buffering variables */ @@ -889,11 +887,9 @@ static uint32_t psock_send_eventhandler(FAR struct net_driver_s *dev, } } -#if CONFIG_NET_SEND_BUFSIZE > 0 /* Notify the send buffer available if wrbbuffer drained */ tcp_sendbuffer_notify(conn); -#endif /* CONFIG_NET_SEND_BUFSIZE */ /* Check if the outgoing packet is available (it may have been claimed * by a sendto event serving a different thread). @@ -1316,6 +1312,7 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR const void *buf, while (len > 0) { uint32_t max_wrb_size; + uint32_t room; unsigned int off; size_t chunk_len = len; ssize_t chunk_result; @@ -1365,7 +1362,6 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR const void *buf, conn->sndcb->priv = (FAR void *)conn; conn->sndcb->event = psock_send_eventhandler; -#if CONFIG_NET_SEND_BUFSIZE > 0 /* If the send buffer size exceeds the send limit, * wait for the write buffer to be released */ @@ -1403,7 +1399,16 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR const void *buf, goto errout_with_lock; } } -#endif /* CONFIG_NET_SEND_BUFSIZE */ + + /* Queue no more than the limit allows: one write buffer can take + * four segments (tcp_max_wrb_size()), more than the limit. + */ + + room = conn->snd_bufs - tcp_wrbuffer_inqueue_size(conn); + if (chunk_len > room) + { + chunk_len = room; + } while (true) { @@ -1684,11 +1689,8 @@ int psock_tcp_cansend(FAR struct tcp_conn_s *conn) * but we don't know how many more. */ - if (tcp_wrbuffer_test() < 0 || iob_navail(true) <= 0 -#if CONFIG_NET_SEND_BUFSIZE > 0 - || tcp_wrbuffer_inqueue_size(conn) >= conn->snd_bufs -#endif - ) + if (tcp_wrbuffer_test() < 0 || iob_navail(true) <= 0 || + tcp_wrbuffer_inqueue_size(conn) >= conn->snd_bufs) { return -EWOULDBLOCK; } @@ -1710,7 +1712,6 @@ int psock_tcp_cansend(FAR struct tcp_conn_s *conn) * ****************************************************************************/ -#if CONFIG_NET_SEND_BUFSIZE > 0 void tcp_sendbuffer_notify(FAR struct tcp_conn_s *conn) { int val = 0; @@ -1726,6 +1727,5 @@ void tcp_sendbuffer_notify(FAR struct tcp_conn_s *conn) nxsem_post(&conn->snd_sem); } } -#endif /* CONFIG_NET_SEND_BUFSIZE */ #endif /* CONFIG_NET && CONFIG_NET_TCP && CONFIG_NET_TCP_WRITE_BUFFERS */ diff --git a/net/tcp/tcp_wrbuffer.c b/net/tcp/tcp_wrbuffer.c index 828c0e0ea34..2ec44e3588f 100644 --- a/net/tcp/tcp_wrbuffer.c +++ b/net/tcp/tcp_wrbuffer.c @@ -193,7 +193,6 @@ void tcp_wrbuffer_release(FAR struct tcp_wrbuffer_s *wrb) * ****************************************************************************/ -#if CONFIG_NET_SEND_BUFSIZE > 0 uint32_t tcp_wrbuffer_inqueue_size(FAR struct tcp_conn_s *conn) { FAR struct tcp_wrbuffer_s *wrb; @@ -217,7 +216,6 @@ uint32_t tcp_wrbuffer_inqueue_size(FAR struct tcp_conn_s *conn) return total; } -#endif /* CONFIG_NET_SEND_BUFSIZE */ /**************************************************************************** * Name: tcp_wrbuffer_test
