Skip to content

Commit f9616c3

Browse files
edumazetdavem330
authored andcommitted
tcp: implement TSQ for retransmits
We saw sch_fq drops caused by the per flow limit of 100 packets and TCP when dealing with large cwnd and bursts of retransmits. Even after increasing the limit to 1000, and even after commit 10d3be5 ("tcp-tso: do not split TSO packets at retransmit time"), we can still have these drops. Under certain conditions, TCP can spend a considerable amount of time queuing thousands of skbs in a single tcp_xmit_retransmit_queue() invocation, incurring latency spikes and stalls of other softirq handlers. This patch implements TSQ for retransmits, limiting number of packets and giving more chance for scheduling packets in both ways. Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: Yuchung Cheng <ycheng@google.com> Signed-off-by: Neal Cardwell <ncardwell@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 0f1100c commit f9616c3

1 file changed

Lines changed: 47 additions & 25 deletions

File tree

‎net/ipv4/tcp_output.c‎

Lines changed: 47 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -734,9 +734,16 @@ static void tcp_tsq_handler(struct sock *sk)
734734
{
735735
if ((1 << sk->sk_state) &
736736
(TCPF_ESTABLISHED | TCPF_FIN_WAIT1 | TCPF_CLOSING |
737-
TCPF_CLOSE_WAIT | TCPF_LAST_ACK))
738-
tcp_write_xmit(sk, tcp_current_mss(sk), tcp_sk(sk)->nonagle,
737+
TCPF_CLOSE_WAIT | TCPF_LAST_ACK)) {
738+
struct tcp_sock *tp = tcp_sk(sk);
739+
740+
if (tp->lost_out > tp->retrans_out &&
741+
tp->snd_cwnd > tcp_packets_in_flight(tp))
742+
tcp_xmit_retransmit_queue(sk);
743+
744+
tcp_write_xmit(sk, tcp_current_mss(sk), tp->nonagle,
739745
0, GFP_ATOMIC);
746+
}
740747
}
741748
/*
742749
* One tasklet per cpu tries to send more skbs.
@@ -2039,6 +2046,39 @@ static int tcp_mtu_probe(struct sock *sk)
20392046
return -1;
20402047
}
20412048

2049+
/* TCP Small Queues :
2050+
* Control number of packets in qdisc/devices to two packets / or ~1 ms.
2051+
* (These limits are doubled for retransmits)
2052+
* This allows for :
2053+
* - better RTT estimation and ACK scheduling
2054+
* - faster recovery
2055+
* - high rates
2056+
* Alas, some drivers / subsystems require a fair amount
2057+
* of queued bytes to ensure line rate.
2058+
* One example is wifi aggregation (802.11 AMPDU)
2059+
*/
2060+
static bool tcp_small_queue_check(struct sock *sk, const struct sk_buff *skb,
2061+
unsigned int factor)
2062+
{
2063+
unsigned int limit;
2064+
2065+
limit = max(2 * skb->truesize, sk->sk_pacing_rate >> 10);
2066+
limit = min_t(u32, limit, sysctl_tcp_limit_output_bytes);
2067+
limit <<= factor;
2068+
2069+
if (atomic_read(&sk->sk_wmem_alloc) > limit) {
2070+
set_bit(TSQ_THROTTLED, &tcp_sk(sk)->tsq_flags);
2071+
/* It is possible TX completion already happened
2072+
* before we set TSQ_THROTTLED, so we must
2073+
* test again the condition.
2074+
*/
2075+
smp_mb__after_atomic();
2076+
if (atomic_read(&sk->sk_wmem_alloc) > limit)
2077+
return true;
2078+
}
2079+
return false;
2080+
}
2081+
20422082
/* This routine writes packets to the network. It advances the
20432083
* send_head. This happens as incoming acks open up the remote
20442084
* window for us.
@@ -2125,29 +2165,8 @@ static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
21252165
unlikely(tso_fragment(sk, skb, limit, mss_now, gfp)))
21262166
break;
21272167

2128-
/* TCP Small Queues :
2129-
* Control number of packets in qdisc/devices to two packets / or ~1 ms.
2130-
* This allows for :
2131-
* - better RTT estimation and ACK scheduling
2132-
* - faster recovery
2133-
* - high rates
2134-
* Alas, some drivers / subsystems require a fair amount
2135-
* of queued bytes to ensure line rate.
2136-
* One example is wifi aggregation (802.11 AMPDU)
2137-
*/
2138-
limit = max(2 * skb->truesize, sk->sk_pacing_rate >> 10);
2139-
limit = min_t(u32, limit, sysctl_tcp_limit_output_bytes);
2140-
2141-
if (atomic_read(&sk->sk_wmem_alloc) > limit) {
2142-
set_bit(TSQ_THROTTLED, &tp->tsq_flags);
2143-
/* It is possible TX completion already happened
2144-
* before we set TSQ_THROTTLED, so we must
2145-
* test again the condition.
2146-
*/
2147-
smp_mb__after_atomic();
2148-
if (atomic_read(&sk->sk_wmem_alloc) > limit)
2149-
break;
2150-
}
2168+
if (tcp_small_queue_check(sk, skb, 0))
2169+
break;
21512170

21522171
if (unlikely(tcp_transmit_skb(sk, skb, 1, gfp)))
21532172
break;
@@ -2847,6 +2866,9 @@ void tcp_xmit_retransmit_queue(struct sock *sk)
28472866
if (sacked & (TCPCB_SACKED_ACKED|TCPCB_SACKED_RETRANS))
28482867
continue;
28492868

2869+
if (tcp_small_queue_check(sk, skb, 1))
2870+
return;
2871+
28502872
if (tcp_retransmit_skb(sk, skb, segs))
28512873
return;
28522874

0 commit comments

Comments
 (0)