Skip to content

Commit e114a71

Browse files
edumazetdavem330
authored andcommitted
tcp: fix cwnd limited checking to improve congestion control
Yuchung discovered tcp_is_cwnd_limited() was returning false in slow start phase even if the application filled the socket write queue. All congestion modules take into account tcp_is_cwnd_limited() before increasing cwnd, so this behavior limits slow start from probing the bandwidth at full speed. The problem is that even if write queue is full (aka we are _not_ application limited), cwnd can be under utilized if TSO should auto defer or TCP Small queues decided to hold packets. So the in_flight can be kept to smaller value, and we can get to the point tcp_is_cwnd_limited() returns false. With TCP Small Queues and FQ/pacing, this issue is more visible. We fix this by having tcp_cwnd_validate(), which is supposed to track such things, take into account unsent_segs, the number of segs that we are not sending at the moment due to TSO or TSQ, but intend to send real soon. Then when we are cwnd-limited, remember this fact while we are processing the window of ACKs that comes back. For example, suppose we have a brand new connection with cwnd=10; we are in slow start, and we send a flight of 9 packets. By the time we have received ACKs for all 9 packets we want our cwnd to be 18. We implement this by setting tp->lsnd_pending to 9, and considering ourselves to be cwnd-limited while cwnd is less than twice tp->lsnd_pending (2*9 -> 18). This makes tcp_is_cwnd_limited() more understandable, by removing the GSO/TSO kludge, that tried to work around the issue. Note the in_flight parameter can be removed in a followup cleanup patch. Signed-off-by: Eric Dumazet <edumazet@google.com> Signed-off-by: Neal Cardwell <ncardwell@google.com> Signed-off-by: Yuchung Cheng <ycheng@google.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 4e8bbb8 commit e114a71

4 files changed

Lines changed: 36 additions & 28 deletions

File tree

‎include/linux/tcp.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ struct tcp_sock {
230230
u32 snd_cwnd_clamp; /* Do not allow snd_cwnd to grow above this */
231231
u32 snd_cwnd_used;
232232
u32 snd_cwnd_stamp;
233+
u32 lsnd_pending; /* packets inflight or unsent since last xmit */
233234
u32 prior_cwnd; /* Congestion window at start of Recovery. */
234235
u32 prr_delivered; /* Number of newly delivered packets to
235236
* receiver in Recovery. */

‎include/net/tcp.h‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,27 @@ static inline u32 tcp_wnd_end(const struct tcp_sock *tp)
974974
{
975975
return tp->snd_una + tp->snd_wnd;
976976
}
977-
bool tcp_is_cwnd_limited(const struct sock *sk, u32 in_flight);
977+
978+
/* We follow the spirit of RFC2861 to validate cwnd but implement a more
979+
* flexible approach. The RFC suggests cwnd should not be raised unless
980+
* it was fully used previously. But we allow cwnd to grow as long as the
981+
* application has used half the cwnd.
982+
* Example :
983+
* cwnd is 10 (IW10), but application sends 9 frames.
984+
* We allow cwnd to reach 18 when all frames are ACKed.
985+
* This check is safe because it's as aggressive as slow start which already
986+
* risks 100% overshoot. The advantage is that we discourage application to
987+
* either send more filler packets or data to artificially blow up the cwnd
988+
* usage, and allow application-limited process to probe bw more aggressively.
989+
*
990+
* TODO: remove in_flight once we can fix all callers, and their callers...
991+
*/
992+
static inline bool tcp_is_cwnd_limited(const struct sock *sk, u32 in_flight)
993+
{
994+
const struct tcp_sock *tp = tcp_sk(sk);
995+
996+
return tp->snd_cwnd < 2 * tp->lsnd_pending;
997+
}
978998

979999
static inline void tcp_check_probe_timer(struct sock *sk)
9801000
{

‎net/ipv4/tcp_cong.c‎

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -276,26 +276,6 @@ int tcp_set_congestion_control(struct sock *sk, const char *name)
276276
return err;
277277
}
278278

279-
/* RFC2861 Check whether we are limited by application or congestion window
280-
* This is the inverse of cwnd check in tcp_tso_should_defer
281-
*/
282-
bool tcp_is_cwnd_limited(const struct sock *sk, u32 in_flight)
283-
{
284-
const struct tcp_sock *tp = tcp_sk(sk);
285-
u32 left;
286-
287-
if (in_flight >= tp->snd_cwnd)
288-
return true;
289-
290-
left = tp->snd_cwnd - in_flight;
291-
if (sk_can_gso(sk) &&
292-
left * sysctl_tcp_tso_win_divisor < tp->snd_cwnd &&
293-
left < tp->xmit_size_goal_segs)
294-
return true;
295-
return left <= tcp_max_tso_deferred_mss(tp);
296-
}
297-
EXPORT_SYMBOL_GPL(tcp_is_cwnd_limited);
298-
299279
/* Slow start is used when congestion window is no greater than the slow start
300280
* threshold. We base on RFC2581 and also handle stretch ACKs properly.
301281
* We do not implement RFC3465 Appropriate Byte Counting (ABC) per se but

‎net/ipv4/tcp_output.c‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,12 +1402,13 @@ static void tcp_cwnd_application_limited(struct sock *sk)
14021402
tp->snd_cwnd_stamp = tcp_time_stamp;
14031403
}
14041404

1405-
/* Congestion window validation. (RFC2861) */
1406-
static void tcp_cwnd_validate(struct sock *sk)
1405+
static void tcp_cwnd_validate(struct sock *sk, u32 unsent_segs)
14071406
{
14081407
struct tcp_sock *tp = tcp_sk(sk);
14091408

1410-
if (tp->packets_out >= tp->snd_cwnd) {
1409+
tp->lsnd_pending = tp->packets_out + unsent_segs;
1410+
1411+
if (tcp_is_cwnd_limited(sk, 0)) {
14111412
/* Network is feed fully. */
14121413
tp->snd_cwnd_used = 0;
14131414
tp->snd_cwnd_stamp = tcp_time_stamp;
@@ -1880,7 +1881,7 @@ static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
18801881
{
18811882
struct tcp_sock *tp = tcp_sk(sk);
18821883
struct sk_buff *skb;
1883-
unsigned int tso_segs, sent_pkts;
1884+
unsigned int tso_segs, sent_pkts, unsent_segs = 0;
18841885
int cwnd_quota;
18851886
int result;
18861887

@@ -1924,7 +1925,7 @@ static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
19241925
break;
19251926
} else {
19261927
if (!push_one && tcp_tso_should_defer(sk, skb))
1927-
break;
1928+
goto compute_unsent_segs;
19281929
}
19291930

19301931
/* TCP Small Queues :
@@ -1949,8 +1950,14 @@ static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
19491950
* there is no smp_mb__after_set_bit() yet
19501951
*/
19511952
smp_mb__after_clear_bit();
1952-
if (atomic_read(&sk->sk_wmem_alloc) > limit)
1953+
if (atomic_read(&sk->sk_wmem_alloc) > limit) {
1954+
u32 unsent_bytes;
1955+
1956+
compute_unsent_segs:
1957+
unsent_bytes = tp->write_seq - tp->snd_nxt;
1958+
unsent_segs = DIV_ROUND_UP(unsent_bytes, mss_now);
19531959
break;
1960+
}
19541961
}
19551962

19561963
limit = mss_now;
@@ -1990,7 +1997,7 @@ static bool tcp_write_xmit(struct sock *sk, unsigned int mss_now, int nonagle,
19901997
/* Send one loss probe per tail loss episode. */
19911998
if (push_one != 2)
19921999
tcp_schedule_loss_probe(sk);
1993-
tcp_cwnd_validate(sk);
2000+
tcp_cwnd_validate(sk, unsent_segs);
19942001
return false;
19952002
}
19962003
return (push_one == 2) || (!tp->packets_out && tcp_send_head(sk));

0 commit comments

Comments
 (0)