1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "net/quic/congestion_control/tcp_cubic_sender.h"
9 #include "base/metrics/histogram.h"
10 #include "net/quic/congestion_control/rtt_stats.h"
11 #include "net/quic/crypto/crypto_protocol.h"
19 // Constants based on TCP defaults.
20 // The minimum cwnd based on RFC 3782 (TCP NewReno) for cwnd reductions on a
21 // fast retransmission. The cwnd after a timeout is still 1.
22 const QuicTcpCongestionWindow kMinimumCongestionWindow
= 2;
23 const QuicByteCount kMaxSegmentSize
= kDefaultTCPMSS
;
24 const int64 kInitialCongestionWindow
= 10;
25 const int kMaxBurstLength
= 3;
28 TcpCubicSender::TcpCubicSender(
29 const QuicClock
* clock
,
30 const RttStats
* rtt_stats
,
32 QuicTcpCongestionWindow max_tcp_congestion_window
,
33 QuicConnectionStats
* stats
)
34 : hybrid_slow_start_(clock
),
36 rtt_stats_(rtt_stats
),
39 congestion_window_count_(0),
40 receive_window_(kDefaultSocketReceiveBuffer
),
43 ack_count_since_loss_(0),
44 bytes_in_flight_before_loss_(0),
45 largest_sent_sequence_number_(0),
46 largest_acked_sequence_number_(0),
47 largest_sent_at_last_cutback_(0),
48 congestion_window_(kInitialCongestionWindow
),
49 previous_congestion_window_(0),
50 slowstart_threshold_(max_tcp_congestion_window
),
51 previous_slowstart_threshold_(0),
52 last_cutback_exited_slowstart_(false),
53 max_tcp_congestion_window_(max_tcp_congestion_window
) {
56 TcpCubicSender::~TcpCubicSender() {
57 UMA_HISTOGRAM_COUNTS("Net.QuicSession.FinalTcpCwnd", congestion_window_
);
60 void TcpCubicSender::SetFromConfig(const QuicConfig
& config
, bool is_server
) {
62 if (config
.HasReceivedConnectionOptions() &&
63 ContainsQuicTag(config
.ReceivedConnectionOptions(), kIW10
)) {
64 // Initial window experiment. Ignore the initial congestion
65 // window suggested by the client and use the default ICWND of
67 congestion_window_
= kInitialCongestionWindow
;
68 } else if (config
.HasReceivedInitialCongestionWindow()) {
69 // Set the initial window size.
70 congestion_window_
= min(kMaxInitialWindow
,
71 config
.ReceivedInitialCongestionWindow());
74 if (config
.HasReceivedSocketReceiveBuffer()) {
75 // Set the initial socket receive buffer size in bytes.
76 receive_window_
= config
.ReceivedSocketReceiveBuffer();
80 void TcpCubicSender::OnIncomingQuicCongestionFeedbackFrame(
81 const QuicCongestionFeedbackFrame
& feedback
,
82 QuicTime feedback_receive_time
) {
83 if (feedback
.type
== kTCP
) {
84 receive_window_
= feedback
.tcp
.receive_window
;
88 void TcpCubicSender::OnCongestionEvent(
90 QuicByteCount bytes_in_flight
,
91 const CongestionMap
& acked_packets
,
92 const CongestionMap
& lost_packets
) {
93 if (rtt_updated
&& InSlowStart() &&
94 hybrid_slow_start_
.ShouldExitSlowStart(rtt_stats_
->latest_rtt(),
95 rtt_stats_
->min_rtt(),
96 congestion_window_
)) {
97 slowstart_threshold_
= congestion_window_
;
99 for (CongestionMap::const_iterator it
= lost_packets
.begin();
100 it
!= lost_packets
.end(); ++it
) {
101 OnPacketLost(it
->first
, bytes_in_flight
);
103 for (CongestionMap::const_iterator it
= acked_packets
.begin();
104 it
!= acked_packets
.end(); ++it
) {
105 OnPacketAcked(it
->first
, it
->second
.bytes_sent
, bytes_in_flight
);
109 void TcpCubicSender::OnPacketAcked(
110 QuicPacketSequenceNumber acked_sequence_number
,
111 QuicByteCount acked_bytes
,
112 QuicByteCount bytes_in_flight
) {
113 largest_acked_sequence_number_
= max(acked_sequence_number
,
114 largest_acked_sequence_number_
);
116 PrrOnPacketAcked(acked_bytes
);
119 MaybeIncreaseCwnd(acked_sequence_number
, bytes_in_flight
);
120 // TODO(ianswett): Should this even be called when not in slow start?
121 hybrid_slow_start_
.OnPacketAcked(acked_sequence_number
, InSlowStart());
124 void TcpCubicSender::OnPacketLost(QuicPacketSequenceNumber sequence_number
,
125 QuicByteCount bytes_in_flight
) {
126 // TCP NewReno (RFC6582) says that once a loss occurs, any losses in packets
127 // already sent should be treated as a single loss event, since it's expected.
128 if (sequence_number
<= largest_sent_at_last_cutback_
) {
129 if (last_cutback_exited_slowstart_
) {
130 ++stats_
->slowstart_packets_lost
;
132 DVLOG(1) << "Ignoring loss for largest_missing:" << sequence_number
133 << " because it was sent prior to the last CWND cutback.";
136 ++stats_
->tcp_loss_events
;
137 last_cutback_exited_slowstart_
= InSlowStart();
139 ++stats_
->slowstart_packets_lost
;
141 PrrOnPacketLost(bytes_in_flight
);
144 congestion_window_
= congestion_window_
>> 1;
147 cubic_
.CongestionWindowAfterPacketLoss(congestion_window_
);
149 slowstart_threshold_
= congestion_window_
;
150 // Enforce TCP's minimum congestion window of 2*MSS.
151 if (congestion_window_
< kMinimumCongestionWindow
) {
152 congestion_window_
= kMinimumCongestionWindow
;
154 largest_sent_at_last_cutback_
= largest_sent_sequence_number_
;
155 // reset packet count from congestion avoidance mode. We start
156 // counting again when we're out of recovery.
157 congestion_window_count_
= 0;
158 DVLOG(1) << "Incoming loss; congestion window: " << congestion_window_
159 << " slowstart threshold: " << slowstart_threshold_
;
162 bool TcpCubicSender::OnPacketSent(QuicTime
/*sent_time*/,
163 QuicByteCount
/*bytes_in_flight*/,
164 QuicPacketSequenceNumber sequence_number
,
166 HasRetransmittableData is_retransmittable
) {
167 // Only update bytes_in_flight_ for data packets.
168 if (is_retransmittable
!= HAS_RETRANSMITTABLE_DATA
) {
173 DCHECK_LT(largest_sent_sequence_number_
, sequence_number
);
174 largest_sent_sequence_number_
= sequence_number
;
175 hybrid_slow_start_
.OnPacketSent(sequence_number
);
179 QuicTime::Delta
TcpCubicSender::TimeUntilSend(
181 QuicByteCount bytes_in_flight
,
182 HasRetransmittableData has_retransmittable_data
) const {
183 if (has_retransmittable_data
== NO_RETRANSMITTABLE_DATA
) {
184 // For TCP we can always send an ACK immediately.
185 return QuicTime::Delta::Zero();
188 return PrrTimeUntilSend(bytes_in_flight
);
190 if (SendWindow() > bytes_in_flight
) {
191 return QuicTime::Delta::Zero();
193 return QuicTime::Delta::Infinite();
196 QuicByteCount
TcpCubicSender::SendWindow() const {
197 // What's the current send window in bytes.
198 return min(receive_window_
, GetCongestionWindow());
201 QuicBandwidth
TcpCubicSender::BandwidthEstimate() const {
202 return QuicBandwidth::FromBytesAndTimeDelta(GetCongestionWindow(),
203 rtt_stats_
->SmoothedRtt());
206 bool TcpCubicSender::HasReliableBandwidthEstimate() const {
207 return !InSlowStart() && !InRecovery();
210 QuicTime::Delta
TcpCubicSender::RetransmissionDelay() const {
211 if (!rtt_stats_
->HasUpdates()) {
212 return QuicTime::Delta::Zero();
214 return QuicTime::Delta::FromMicroseconds(
215 rtt_stats_
->SmoothedRtt().ToMicroseconds() +
216 4 * rtt_stats_
->mean_deviation().ToMicroseconds());
219 QuicByteCount
TcpCubicSender::GetCongestionWindow() const {
220 return congestion_window_
* kMaxSegmentSize
;
223 bool TcpCubicSender::InSlowStart() const {
224 return congestion_window_
< slowstart_threshold_
;
227 QuicByteCount
TcpCubicSender::GetSlowStartThreshold() const {
228 return slowstart_threshold_
* kMaxSegmentSize
;
231 bool TcpCubicSender::IsCwndLimited(QuicByteCount bytes_in_flight
) const {
232 const QuicByteCount congestion_window_bytes
= congestion_window_
*
234 if (bytes_in_flight
>= congestion_window_bytes
) {
237 const QuicByteCount max_burst
= kMaxBurstLength
* kMaxSegmentSize
;
238 const QuicByteCount available_bytes
=
239 congestion_window_bytes
- bytes_in_flight
;
240 const bool slow_start_limited
= InSlowStart() &&
241 bytes_in_flight
> congestion_window_bytes
/ 2;
242 return slow_start_limited
|| available_bytes
<= max_burst
;
245 bool TcpCubicSender::InRecovery() const {
246 return largest_acked_sequence_number_
<= largest_sent_at_last_cutback_
&&
247 largest_acked_sequence_number_
!= 0;
250 // Called when we receive an ack. Normal TCP tracks how many packets one ack
251 // represents, but quic has a separate ack for each packet.
252 void TcpCubicSender::MaybeIncreaseCwnd(
253 QuicPacketSequenceNumber acked_sequence_number
,
254 QuicByteCount bytes_in_flight
) {
255 LOG_IF(DFATAL
, InRecovery()) << "Never increase the CWND during recovery.";
256 if (!IsCwndLimited(bytes_in_flight
)) {
257 // We don't update the congestion window unless we are close to using the
258 // window we have available.
262 // congestion_window_cnt is the number of acks since last change of snd_cwnd
263 if (congestion_window_
< max_tcp_congestion_window_
) {
264 // TCP slow start, exponential growth, increase by one for each ACK.
265 ++congestion_window_
;
267 DVLOG(1) << "Slow start; congestion window: " << congestion_window_
268 << " slowstart threshold: " << slowstart_threshold_
;
271 if (congestion_window_
>= max_tcp_congestion_window_
) {
274 // Congestion avoidance
276 // Classic Reno congestion avoidance provided for testing.
278 ++congestion_window_count_
;
279 if (congestion_window_count_
>= congestion_window_
) {
280 ++congestion_window_
;
281 congestion_window_count_
= 0;
284 DVLOG(1) << "Reno; congestion window: " << congestion_window_
285 << " slowstart threshold: " << slowstart_threshold_
286 << " congestion window count: " << congestion_window_count_
;
288 congestion_window_
= min(max_tcp_congestion_window_
,
289 cubic_
.CongestionWindowAfterAck(
290 congestion_window_
, rtt_stats_
->min_rtt()));
291 DVLOG(1) << "Cubic; congestion window: " << congestion_window_
292 << " slowstart threshold: " << slowstart_threshold_
;
296 void TcpCubicSender::OnRetransmissionTimeout(bool packets_retransmitted
) {
297 largest_sent_at_last_cutback_
= 0;
298 if (!packets_retransmitted
) {
302 hybrid_slow_start_
.Restart();
303 previous_slowstart_threshold_
= slowstart_threshold_
;
304 slowstart_threshold_
= congestion_window_
/ 2;
305 previous_congestion_window_
= congestion_window_
;
306 congestion_window_
= kMinimumCongestionWindow
;
309 void TcpCubicSender::RevertRetransmissionTimeout() {
310 if (previous_congestion_window_
== 0) {
311 LOG(DFATAL
) << "No previous congestion window to revert to.";
314 congestion_window_
= previous_congestion_window_
;
315 slowstart_threshold_
= previous_slowstart_threshold_
;
316 previous_congestion_window_
= 0;
319 void TcpCubicSender::PrrOnPacketLost(QuicByteCount bytes_in_flight
) {
321 bytes_in_flight_before_loss_
= bytes_in_flight
;
323 ack_count_since_loss_
= 0;
326 void TcpCubicSender::PrrOnPacketAcked(QuicByteCount acked_bytes
) {
327 prr_delivered_
+= acked_bytes
;
328 ++ack_count_since_loss_
;
331 QuicTime::Delta
TcpCubicSender::PrrTimeUntilSend(
332 QuicByteCount bytes_in_flight
) const {
333 DCHECK(InRecovery());
334 // Return QuicTime::Zero In order to ensure limited transmit always works.
335 if (prr_out_
== 0 || bytes_in_flight
< kMaxSegmentSize
) {
336 return QuicTime::Delta::Zero();
338 if (SendWindow() > bytes_in_flight
) {
339 // During PRR-SSRB, limit outgoing packets to 1 extra MSS per ack, instead
340 // of sending the entire available window. This prevents burst retransmits
341 // when more packets are lost than the CWND reduction.
342 // limit = MAX(prr_delivered - prr_out, DeliveredData) + MSS
343 if (prr_delivered_
+ ack_count_since_loss_
* kMaxSegmentSize
<= prr_out_
) {
344 return QuicTime::Delta::Infinite();
346 return QuicTime::Delta::Zero();
348 // Implement Proportional Rate Reduction (RFC6937)
349 // Checks a simplified version of the PRR formula that doesn't use division:
350 // AvailableSendWindow =
351 // CEIL(prr_delivered * ssthresh / BytesInFlightAtLoss) - prr_sent
352 if (prr_delivered_
* slowstart_threshold_
* kMaxSegmentSize
>
353 prr_out_
* bytes_in_flight_before_loss_
) {
354 return QuicTime::Delta::Zero();
356 return QuicTime::Delta::Infinite();
359 CongestionControlType
TcpCubicSender::GetCongestionControlType() const {
360 return reno_
? kReno
: kCubic
;