Add a sysctl to allow TCP window shrinking in order to honor memory limits
Subsystems (3)
RFC PATCH 1/1 Add a sysctl to allow TCP window shrinking in order to honor memory limits
From: "[email protected]" <[email protected]>
Under certain circumstances, the tcp receive buffer memory limit set by autotuning is ignored, and the receive buffer can grow unrestrained until it reaches tcp_rmem[2].
To reproduce: Connect a TCP session with the receiver doing nothing and the sender sending small packets (an infinite loop of socket send() with 4 bytes of payload with a sleep of 1 ms in between each send()). This will fill the tcp receive buffer all the way to tcp_rmem[2], ignoring the autotuning limit (sk_rcvbuf).
As a result, a host can have individual tcp sessions with receive buffers of size tcp_rmem[2], and the host itself can reach tcp_mem limits, causing the host to go into tcp memory pressure mode.
The fundamental issue is the relationship between the granularity of the window scaling factor and the number of byte ACKed back to the sender. This problem has previously been identified in RFC 7323, appendix F [1].
The Linux kernel currently adheres to never shrinking the window.
In addition to the overallocation of memory mentioned above, this is also functionally incorrect, because once tcp_rmem[2] is reached, the receiver will drop in-window packets resulting in retransmissions and an eventual timeout of the tcp session. A receive buffer full condition should instead result in a zero window and an indefinite wait.
In practice, this problem is largely hidden for most flows. It is not applicable to mice flows. Elephant flows can send data fast enough to "overrun" the sk_rcvbuf limit (in a single ACK), triggering a zero window.
But this problem does show up for other types of flows. A good example are websockets and other type of flows that send small amounts of data spaced apart slightly in time. In these cases, we directly encounter the problem described in [1].
RFC 7323, section 2.4 [2], says there are instances when a retracted window can be offered, and that TCP implementations MUST ensure that they handle a shrinking window, as specified in RFC 1122, section 4.2.2.16 [3]. All prior RFCs on the topic of tcp window management have made clear that sender must accept a shrunk window from the receiver, including RFC 793 [4] and RFC 1323 [5].
This patch implements the functionality to shrink the tcp window when necessary to keep the right edge within the memory limit by autotuning (sk_rcvbuf). This new functionality is enabled with the following sysctl:
sysctl: net.ipv4.tcp_shrink_window
This sysctl changes how the TCP window is calculated.
If sysctl tcp_shrink_window is zero (the default value), then the window is never shrunk.
If sysctl tcp_shrink_window is non-zero, then the memory limit set by autotuning is honored. This requires that the TCP window be shrunk ("retracted") as described in RFC 1122.
[1] https://www.rfc-editor.org/rfc/rfc7323#appendix-F
[2] https://www.rfc-editor.org/rfc/rfc7323#section-2.4
[3] https://www.rfc-editor.org/rfc/rfc1122#page-91
[4] https://www.rfc-editor.org/rfc/rfc793
[5] https://www.rfc-editor.org/rfc/rfc1323
--- include/net/netns/ipv4.h | 1 + net/ipv4/sysctl_net_ipv4.c | 7 ++++ net/ipv4/tcp_ipv4.c | 2 + net/ipv4/tcp_output.c | 82 +++++++++++++++++++++++++++++++------- 4 files changed, 78 insertions(+), 14 deletions(-)
include/net/netns/ipv4.h +1 -0
@@ -194,6 +194,7 @@ struct netns_ipv4 {int sysctl_udp_rmem_min;u8 sysctl_fib_notify_on_flag_change; +unsigned int sysctl_tcp_shrink_window;#ifdef CONFIG_NET_L3_MASTER_DEV u8 sysctl_udp_l3mdev_accept;
net/ipv4/sysctl_net_ipv4.c +7 -0
@@ -1467,6 +1467,13 @@ static struct ctl_table ipv4_net_table[] = { .extra1 = SYSCTL_ZERO, .extra2 = &tcp_plb_max_cong_thresh, }, + { + .procname = "tcp_shrink_window", + .data = &init_net.ipv4.sysctl_tcp_shrink_window, + .maxlen = sizeof(unsigned int), + .mode = 0644, + .proc_handler = proc_douintvec_minmax, + }, { } };
net/ipv4/tcp_ipv4.c +2 -0
@@ -3275,6 +3275,8 @@ static int __net_init tcp_sk_init(struct net *net)else net->ipv4.tcp_congestion_control = &tcp_reno; + net->ipv4.sysctl_tcp_shrink_window =0 ; +return 0 ; }
net/ipv4/tcp_output.c +68 -14
129 lines not on this page · raw message
Signature · 1 line
2.39.2
Quoted 10 lines · On Mon, 6 Mar 2023 15:30:58 -0600 Mike Freemon <[email protected]> wrote:
net/ipv4/tcp_output.c +10
-0+* RFC 7323, section 2.4, says there are instances when a retracted +* window can be offered, and that TCP implementations MUST ensure +* that they handle a shrinking window, as specified in RFC 1122. +* +* This patch implements that functionality, which is enabled by +* setting the following sysctl. +* +* sysctl: net.ipv4.tcp_shrink_window +* +* This sysctl changes how the TCP window is calculated.
net/ipv4/tcp_output.c static u16 tcp_select_window(struct sock *sk) · line 295
Quoted 10 lines
net/ipv4/tcp_output.c +10
-0+* +* If sysctl tcp_shrink_window is zero (the default value), then the +* window is never shrunk. +* +* If sysctl tcp_shrink_window is non-zero, then the memory limit +* set by autotuning is honored. This requires that the TCP window +* be shrunk ("retracted") as described in RFC 1122. +* +* For context and additional information about this patch, see the +* blog post at TODO
This comment should be reworded such that it can be read at a much later date with out all the associated context described here.
I.e. Get rid of "this patch part" and the blog post part.
Best to just refer to tcp_shrink_window sysctl and put the details in the regular documentation spot (Documentation/networking/ip-sysctl.rst).
net/ipv4/tcp_output.c u32 __tcp_select_window(struct sock *sk) · line 3031
Quoted 4 lines · On Mon, 6 Mar 2023 15:30:58 -0600 Mike Freemon <[email protected]> wrote:
net/ipv4/tcp_output.c +4
-0+if (net->ipv4.sysctl_tcp_shrink_window) { +if (free_space < (1 << tp->rx_opt.rcv_wscale)) +return 0 ; + }
Please combine if clauses with &&