Skip to contentSkip to conversation

tcp: better receiver autotuning

PATCH In review Eric Dumazet 3 participants 8 messages Dec 11, 2017 – Dec 12, 2017

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

cover tcp: better receiver autotuning

Now TCP senders no longer backoff when a drop is detected, it appears we are very often receive window limited.

This series makes tcp_rcv_space_adjust() slightly more robust and responsive.

Eric Dumazet (3):
tcp: do not overshoot window_clamp in tcp_rcv_space_adjust()
tcp: avoid integer overflows in tcp_rcv_space_adjust()
tcp: smoother receiver autotuning

 include/linux/tcp.h  |  2 +-
 net/ipv4/tcp_input.c | 31 ++++++++++++-------------------
 2 files changed, 13 insertions(+), 20 deletions(-)
Signature · 1 line
2.15.1.424.g9478a66081-goog
#1 lore raw

PATCH 1/3 tcp: do not overshoot window_clamp in tcp_rcv_space_adjust()

While rcvbuf is properly clamped by tcp_rmem[2], rcvwin is left to a potentially too big value.

It has no serious effect, since :
1) tcp_grow_window() has very strict checks.
2) window_clamp can be mangled by user space to any value anyway.

tcp_init_buffer_space() and companions use tcp_full_space(), we use tcp_win_from_space() to avoid reloading sk->sk_rcvbuf

---
 net/ipv4/tcp_input.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv4/tcp_input.c +1 -1
index 9550cc42de2d9ba4cca6d961a2a3bca501755a69..746a6773c482f5d419ddc9d7c9d52949cbb74cfb 100644
@@ -631,7 +631,7 @@ void tcp_rcv_space_adjust(struct sock *sk)
 			sk->sk_rcvbuf = rcvbuf;
 
 			/* Make the window clamp follow along.  */
-			tp->window_clamp = rcvwin;
+			tp->window_clamp = tcp_win_from_space(sk, rcvbuf);
 		}
 	}
 	tp->rcvq_space.space = copied;
Signature · 1 line
2.15.1.424.g9478a66081-goog
#3 lore raw

PATCH 2/3 tcp: avoid integer overflows in tcp_rcv_space_adjust()

In reply to Eric Dumazet

When using large tcp_rmem[2] values (I did tests with 500 MB), I noticed overflows while computing rcvwin.

Lets fix this before the following patch.

---
 include/linux/tcp.h  |  2 +-
 net/ipv4/tcp_input.c | 12 +++++++-----
 2 files changed, 8 insertions(+), 6 deletions(-)
include/linux/tcp.h +1 -1
index ca4a6361389b8a3b268ca5b0f4778662a1f7d315..4f93f0953c411322dc6403af7d1b9b6c3e30bd4f 100644
@@ -344,7 +344,7 @@ struct tcp_sock {
 
 /* Receiver queue space */
 	struct {
-		int	space;
+		u32	space;
 		u32	seq;
 		u64	time;
 	} rcvq_space;
net/ipv4/tcp_input.c +7 -5
index 746a6773c482f5d419ddc9d7c9d52949cbb74cfb..2900e58738cde0ad1ab4a034b6300876ac276edb 100644
@@ -576,8 +576,8 @@ static inline void tcp_rcv_rtt_measure_ts(struct sock *sk,
 void tcp_rcv_space_adjust(struct sock *sk)
 {
 	struct tcp_sock *tp = tcp_sk(sk);
+	u32 copied;
 	int time;
-	int copied;
 
 	tcp_mstamp_refresh(tp);
 	time = tcp_stamp_us_delta(tp->tcp_mstamp, tp->rcvq_space.time);
@@ -600,12 +600,13 @@ void tcp_rcv_space_adjust(struct sock *sk)
 
 	if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf &&
 	    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
-		int rcvwin, rcvmem, rcvbuf;
+		int rcvmem, rcvbuf;
+		u64 rcvwin;
 
 		/* minimal window to cope with packet losses, assuming
 		 * steady state. Add some cushion because of small variations.
 		 */
-		rcvwin = (copied << 1) + 16 * tp->advmss;
+		rcvwin = ((u64)copied << 1) + 16 * tp->advmss;
 
 		/* If rate increased by 25%,
 		 *	assume slow start, rcvwin = 3 * copied
@@ -625,8 +626,9 @@ void tcp_rcv_space_adjust(struct sock *sk)
 		while (tcp_win_from_space(sk, rcvmem) < tp->advmss)
 			rcvmem += 128;
 
-		rcvbuf = min(rcvwin / tp->advmss * rcvmem,
-			     sock_net(sk)->ipv4.sysctl_tcp_rmem[2]);
+		do_div(rcvwin, tp->advmss);
+		rcvbuf = min_t(u64, rcvwin * rcvmem,
+			       sock_net(sk)->ipv4.sysctl_tcp_rmem[2]);
 		if (rcvbuf > sk->sk_rcvbuf) {
 			sk->sk_rcvbuf = rcvbuf;
 
Signature · 1 line
2.15.1.424.g9478a66081-goog
#5 lore raw

PATCH 3/3 tcp: smoother receiver autotuning

In reply to Eric Dumazet

Back in linux-3.13 (commit b0983d3c9b13 ("tcp: fix dynamic right sizing")) I addressed the pressing issues we had with receiver autotuning.

But DRS suffers from extra latencies caused by rcv_rtt_est.rtt_us drifts. One common problem happens during slow start, since the apparent RTT measured by the receiver can be inflated by ~50%, at the end of one packet train.

Also, a single drop can delay read() calls by one RTT, meaning tcp_rcv_space_adjust() can be called one RTT too late.

By replacing the tri-modal heuristic with a continuous function, we can offset the effects of not growing 'at the optimal time'.

The curve of the function matches prior behavior if the space increased by 25% and 50% exactly.

Cost of added multiply/divide is small, considering a TCP flow typically would run this part of the code few times in its life.

I tested this patch with 100 ms RTT / 1% loss link, 100 runs of (netperf -l 5), and got an average throughput of 4600 Mbit instead of 1700 Mbit.

---
 net/ipv4/tcp_input.c | 19 +++++--------------
 1 file changed, 5 insertions(+), 14 deletions(-)
net/ipv4/tcp_input.c +5 -14
index 2900e58738cde0ad1ab4a034b6300876ac276edb..fefb46c16de7b1da76443f714a3f42faacca708d 100644
@@ -601,26 +601,17 @@ void tcp_rcv_space_adjust(struct sock *sk)
 	if (sock_net(sk)->ipv4.sysctl_tcp_moderate_rcvbuf &&
 	    !(sk->sk_userlocks & SOCK_RCVBUF_LOCK)) {
 		int rcvmem, rcvbuf;
-		u64 rcvwin;
+		u64 rcvwin, grow;
 
 		/* minimal window to cope with packet losses, assuming
 		 * steady state. Add some cushion because of small variations.
 		 */
 		rcvwin = ((u64)copied << 1) + 16 * tp->advmss;
 
-		/* If rate increased by 25%,
-		 *	assume slow start, rcvwin = 3 * copied
-		 * If rate increased by 50%,
-		 *	assume sender can use 2x growth, rcvwin = 4 * copied
-		 */
-		if (copied >=
-		    tp->rcvq_space.space + (tp->rcvq_space.space >> 2)) {
-			if (copied >=
-			    tp->rcvq_space.space + (tp->rcvq_space.space >> 1))
-				rcvwin <<= 1;
-			else
-				rcvwin += (rcvwin >> 1);
-		}
+		/* Accommodate for sender rate increase (eg. slow start) */
+		grow = rcvwin * (copied - tp->rcvq_space.space);
+		do_div(grow, tp->rcvq_space.space);
+		rcvwin += (grow << 1);
 
 		rcvmem = SKB_TRUESIZE(tp->advmss + MAX_TCP_HEADER);
 		while (tcp_win_from_space(sk, rcvmem) < tp->advmss)
Signature · 1 line
2.15.1.424.g9478a66081-goog
maintainer Acked-by on v1 1/3
#2 lore raw

Re: [PATCH net-next 1/3] tcp: do not overshoot window_clamp in tcp_rcv_space_adjust()

In reply to Eric Dumazet
Quoted 8 lines · Eric Dumazet

While rcvbuf is properly clamped by tcp_rmem[2], rcvwin is left to a potentially too big value.

It has no serious effect, since :
1) tcp_grow_window() has very strict checks.
2) window_clamp can be mangled by user space to any value anyway.

Quoted 7 lines

tcp_init_buffer_space() and companions use tcp_full_space(), we use tcp_win_from_space() to avoid reloading sk->sk_rcvbuf

---

Thanks, Eric!

neal

maintainer Acked-by on v1 2/3
#4 lore raw

Re: [PATCH net-next 2/3] tcp: avoid integer overflows in tcp_rcv_space_adjust()

In reply to Eric Dumazet
Quoted 9 lines · Eric Dumazet

When using large tcp_rmem[2] values (I did tests with 500 MB), I noticed overflows while computing rcvwin.

Lets fix this before the following patch.

---

Thanks, Eric!

neal

maintainer Acked-by on v1 3/3
#6 lore raw

Re: [PATCH net-next 3/3] tcp: smoother receiver autotuning

In reply to Eric Dumazet
Quoted 20 lines · Eric Dumazet

Back in linux-3.13 (commit b0983d3c9b13 ("tcp: fix dynamic right sizing")) I addressed the pressing issues we had with receiver autotuning.

But DRS suffers from extra latencies caused by rcv_rtt_est.rtt_us drifts. One common problem happens during slow start, since the apparent RTT measured by the receiver can be inflated by ~50%, at the end of one packet train.

Also, a single drop can delay read() calls by one RTT, meaning tcp_rcv_space_adjust() can be called one RTT too late.

By replacing the tri-modal heuristic with a continuous function, we can offset the effects of not growing 'at the optimal time'.

The curve of the function matches prior behavior if the space increased by 25% and 50% exactly.

Cost of added multiply/divide is small, considering a TCP flow typically would run this part of the code few times in its life.

Quoted 8 lines

I tested this patch with 100 ms RTT / 1% loss link, 100 runs of (netperf -l 5), and got an average throughput of 4600 Mbit instead of 1700 Mbit.

---

Thanks, Eric!

neal

maintainer
#7 lore raw
In reply to Eric Dumazet

From: Eric Dumazet <[email protected]>
Date: Sun, 10 Dec 2017 17:55:01 -0800

Quoted 5 lines

Now TCP senders no longer backoff when a drop is detected, it appears we are very often receive window limited.

This series makes tcp_rcv_space_adjust() slightly more robust and responsive.

Series applied, thanks.