Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / quic / congestion_control / tcp_cubic_sender.cc
blob5e1fb255401d49001a222543578af399f6bc1f4f
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"
7 #include <algorithm>
9 #include "base/metrics/histogram_macros.h"
10 #include "net/quic/congestion_control/prr_sender.h"
11 #include "net/quic/congestion_control/rtt_stats.h"
12 #include "net/quic/crypto/crypto_protocol.h"
13 #include "net/quic/proto/cached_network_parameters.pb.h"
15 using std::max;
16 using std::min;
18 namespace net {
20 namespace {
21 // Constants based on TCP defaults.
22 // The minimum cwnd based on RFC 3782 (TCP NewReno) for cwnd reductions on a
23 // fast retransmission. The cwnd after a timeout is still 1.
24 const QuicPacketCount kDefaultMinimumCongestionWindow = 2;
25 const QuicByteCount kMaxSegmentSize = kDefaultTCPMSS;
26 const QuicByteCount kMaxBurstBytes = 3 * kMaxSegmentSize;
27 const float kRenoBeta = 0.7f; // Reno backoff factor.
28 const uint32 kDefaultNumConnections = 2; // N-connection emulation.
29 } // namespace
31 TcpCubicSender::TcpCubicSender(const QuicClock* clock,
32 const RttStats* rtt_stats,
33 bool reno,
34 QuicPacketCount initial_tcp_congestion_window,
35 QuicPacketCount max_tcp_congestion_window,
36 QuicConnectionStats* stats)
37 : cubic_(clock),
38 rtt_stats_(rtt_stats),
39 stats_(stats),
40 reno_(reno),
41 num_connections_(kDefaultNumConnections),
42 congestion_window_count_(0),
43 largest_sent_sequence_number_(0),
44 largest_acked_sequence_number_(0),
45 largest_sent_at_last_cutback_(0),
46 congestion_window_(initial_tcp_congestion_window),
47 min_congestion_window_(kDefaultMinimumCongestionWindow),
48 min4_mode_(false),
49 slowstart_threshold_(max_tcp_congestion_window),
50 last_cutback_exited_slowstart_(false),
51 max_tcp_congestion_window_(max_tcp_congestion_window),
52 clock_(clock) {
55 TcpCubicSender::~TcpCubicSender() {
56 UMA_HISTOGRAM_COUNTS("Net.QuicSession.FinalTcpCwnd", congestion_window_);
59 void TcpCubicSender::SetFromConfig(const QuicConfig& config,
60 Perspective perspective) {
61 if (perspective == Perspective::IS_SERVER) {
62 if (config.HasReceivedConnectionOptions() &&
63 ContainsQuicTag(config.ReceivedConnectionOptions(), kIW03)) {
64 // Initial window experiment.
65 congestion_window_ = 3;
67 if (config.HasReceivedConnectionOptions() &&
68 ContainsQuicTag(config.ReceivedConnectionOptions(), kIW10)) {
69 // Initial window experiment.
70 congestion_window_ = 10;
72 if (config.HasReceivedConnectionOptions() &&
73 ContainsQuicTag(config.ReceivedConnectionOptions(), kIW20)) {
74 // Initial window experiment.
75 congestion_window_ = 20;
77 if (config.HasReceivedConnectionOptions() &&
78 ContainsQuicTag(config.ReceivedConnectionOptions(), kIW50)) {
79 // Initial window experiment.
80 congestion_window_ = 50;
82 if (config.HasReceivedConnectionOptions() &&
83 ContainsQuicTag(config.ReceivedConnectionOptions(), kMIN1)) {
84 // Min CWND experiment.
85 min_congestion_window_ = 1;
87 if (config.HasReceivedConnectionOptions() &&
88 ContainsQuicTag(config.ReceivedConnectionOptions(), kMIN4)) {
89 // Min CWND of 4 experiment.
90 min4_mode_ = true;
91 min_congestion_window_ = 1;
96 void TcpCubicSender::ResumeConnectionState(
97 const CachedNetworkParameters& cached_network_params,
98 bool max_bandwidth_resumption) {
99 QuicBandwidth bandwidth = QuicBandwidth::FromBytesPerSecond(
100 max_bandwidth_resumption
101 ? cached_network_params.max_bandwidth_estimate_bytes_per_second()
102 : cached_network_params.bandwidth_estimate_bytes_per_second());
103 QuicTime::Delta rtt_ms =
104 QuicTime::Delta::FromMilliseconds(cached_network_params.min_rtt_ms());
106 // Make sure CWND is in appropriate range (in case of bad data).
107 QuicPacketCount new_congestion_window =
108 bandwidth.ToBytesPerPeriod(rtt_ms) / kMaxPacketSize;
109 congestion_window_ = max(min(new_congestion_window, kMaxCongestionWindow),
110 kMinCongestionWindowForBandwidthResumption);
113 void TcpCubicSender::SetNumEmulatedConnections(int num_connections) {
114 num_connections_ = max(1, num_connections);
115 cubic_.SetNumConnections(num_connections_);
118 void TcpCubicSender::SetMaxCongestionWindow(
119 QuicByteCount max_congestion_window) {
120 max_tcp_congestion_window_ = max_congestion_window / kMaxPacketSize;
123 float TcpCubicSender::RenoBeta() const {
124 // kNConnectionBeta is the backoff factor after loss for our N-connection
125 // emulation, which emulates the effective backoff of an ensemble of N
126 // TCP-Reno connections on a single loss event. The effective multiplier is
127 // computed as:
128 return (num_connections_ - 1 + kRenoBeta) / num_connections_;
131 void TcpCubicSender::OnCongestionEvent(
132 bool rtt_updated,
133 QuicByteCount bytes_in_flight,
134 const CongestionVector& acked_packets,
135 const CongestionVector& lost_packets) {
136 if (rtt_updated && InSlowStart() &&
137 hybrid_slow_start_.ShouldExitSlowStart(rtt_stats_->latest_rtt(),
138 rtt_stats_->min_rtt(),
139 congestion_window_)) {
140 slowstart_threshold_ = congestion_window_;
142 for (CongestionVector::const_iterator it = lost_packets.begin();
143 it != lost_packets.end(); ++it) {
144 OnPacketLost(it->first, bytes_in_flight);
146 for (CongestionVector::const_iterator it = acked_packets.begin();
147 it != acked_packets.end(); ++it) {
148 OnPacketAcked(it->first, it->second.bytes_sent, bytes_in_flight);
152 void TcpCubicSender::OnPacketAcked(
153 QuicPacketSequenceNumber acked_sequence_number,
154 QuicByteCount acked_bytes,
155 QuicByteCount bytes_in_flight) {
156 largest_acked_sequence_number_ = max(acked_sequence_number,
157 largest_acked_sequence_number_);
158 if (InRecovery()) {
159 // PRR is used when in recovery.
160 prr_.OnPacketAcked(acked_bytes);
161 return;
163 MaybeIncreaseCwnd(acked_sequence_number, bytes_in_flight);
164 // TODO(ianswett): Should this even be called when not in slow start?
165 hybrid_slow_start_.OnPacketAcked(acked_sequence_number, InSlowStart());
168 void TcpCubicSender::OnPacketLost(QuicPacketSequenceNumber sequence_number,
169 QuicByteCount bytes_in_flight) {
170 // TCP NewReno (RFC6582) says that once a loss occurs, any losses in packets
171 // already sent should be treated as a single loss event, since it's expected.
172 if (sequence_number <= largest_sent_at_last_cutback_) {
173 if (last_cutback_exited_slowstart_) {
174 ++stats_->slowstart_packets_lost;
176 DVLOG(1) << "Ignoring loss for largest_missing:" << sequence_number
177 << " because it was sent prior to the last CWND cutback.";
178 return;
180 ++stats_->tcp_loss_events;
181 last_cutback_exited_slowstart_ = InSlowStart();
182 if (InSlowStart()) {
183 ++stats_->slowstart_packets_lost;
186 prr_.OnPacketLost(bytes_in_flight);
188 if (reno_) {
189 congestion_window_ = congestion_window_ * RenoBeta();
190 } else {
191 congestion_window_ =
192 cubic_.CongestionWindowAfterPacketLoss(congestion_window_);
194 slowstart_threshold_ = congestion_window_;
195 // Enforce a minimum congestion window.
196 if (congestion_window_ < min_congestion_window_) {
197 congestion_window_ = min_congestion_window_;
199 largest_sent_at_last_cutback_ = largest_sent_sequence_number_;
200 // reset packet count from congestion avoidance mode. We start
201 // counting again when we're out of recovery.
202 congestion_window_count_ = 0;
203 DVLOG(1) << "Incoming loss; congestion window: " << congestion_window_
204 << " slowstart threshold: " << slowstart_threshold_;
207 bool TcpCubicSender::OnPacketSent(QuicTime /*sent_time*/,
208 QuicByteCount /*bytes_in_flight*/,
209 QuicPacketSequenceNumber sequence_number,
210 QuicByteCount bytes,
211 HasRetransmittableData is_retransmittable) {
212 if (InSlowStart()) {
213 ++(stats_->slowstart_packets_sent);
216 // Only update bytes_in_flight_ for data packets.
217 if (is_retransmittable != HAS_RETRANSMITTABLE_DATA) {
218 return false;
220 if (InRecovery()) {
221 // PRR is used when in recovery.
222 prr_.OnPacketSent(bytes);
224 DCHECK_LT(largest_sent_sequence_number_, sequence_number);
225 largest_sent_sequence_number_ = sequence_number;
226 hybrid_slow_start_.OnPacketSent(sequence_number);
227 return true;
230 QuicTime::Delta TcpCubicSender::TimeUntilSend(
231 QuicTime /* now */,
232 QuicByteCount bytes_in_flight,
233 HasRetransmittableData has_retransmittable_data) const {
234 if (has_retransmittable_data == NO_RETRANSMITTABLE_DATA) {
235 // For TCP we can always send an ACK immediately.
236 return QuicTime::Delta::Zero();
238 if (InRecovery()) {
239 // PRR is used when in recovery.
240 return prr_.TimeUntilSend(GetCongestionWindow(), bytes_in_flight,
241 slowstart_threshold_ * kMaxSegmentSize);
243 if (GetCongestionWindow() > bytes_in_flight) {
244 return QuicTime::Delta::Zero();
246 if (min4_mode_ && bytes_in_flight < 4 * kMaxSegmentSize) {
247 return QuicTime::Delta::Zero();
249 return QuicTime::Delta::Infinite();
252 QuicBandwidth TcpCubicSender::PacingRate() const {
253 // We pace at twice the rate of the underlying sender's bandwidth estimate
254 // during slow start and 1.25x during congestion avoidance to ensure pacing
255 // doesn't prevent us from filling the window.
256 QuicTime::Delta srtt = rtt_stats_->smoothed_rtt();
257 if (srtt.IsZero()) {
258 srtt = QuicTime::Delta::FromMicroseconds(rtt_stats_->initial_rtt_us());
260 const QuicBandwidth bandwidth =
261 QuicBandwidth::FromBytesAndTimeDelta(GetCongestionWindow(), srtt);
262 return bandwidth.Scale(InSlowStart() ? 2 : 1.25);
265 QuicBandwidth TcpCubicSender::BandwidthEstimate() const {
266 QuicTime::Delta srtt = rtt_stats_->smoothed_rtt();
267 if (srtt.IsZero()) {
268 // If we haven't measured an rtt, the bandwidth estimate is unknown.
269 return QuicBandwidth::Zero();
271 return QuicBandwidth::FromBytesAndTimeDelta(GetCongestionWindow(), srtt);
274 QuicTime::Delta TcpCubicSender::RetransmissionDelay() const {
275 if (rtt_stats_->smoothed_rtt().IsZero()) {
276 return QuicTime::Delta::Zero();
278 return rtt_stats_->smoothed_rtt().Add(
279 rtt_stats_->mean_deviation().Multiply(4));
282 QuicByteCount TcpCubicSender::GetCongestionWindow() const {
283 return congestion_window_ * kMaxSegmentSize;
286 bool TcpCubicSender::InSlowStart() const {
287 return congestion_window_ < slowstart_threshold_;
290 QuicByteCount TcpCubicSender::GetSlowStartThreshold() const {
291 return slowstart_threshold_ * kMaxSegmentSize;
294 bool TcpCubicSender::IsCwndLimited(QuicByteCount bytes_in_flight) const {
295 const QuicByteCount congestion_window_bytes = GetCongestionWindow();
296 if (bytes_in_flight >= congestion_window_bytes) {
297 return true;
299 const QuicByteCount available_bytes =
300 congestion_window_bytes - bytes_in_flight;
301 const bool slow_start_limited = InSlowStart() &&
302 bytes_in_flight > congestion_window_bytes / 2;
303 return slow_start_limited || available_bytes <= kMaxBurstBytes;
306 bool TcpCubicSender::InRecovery() const {
307 return largest_acked_sequence_number_ <= largest_sent_at_last_cutback_ &&
308 largest_acked_sequence_number_ != 0;
311 // Called when we receive an ack. Normal TCP tracks how many packets one ack
312 // represents, but quic has a separate ack for each packet.
313 void TcpCubicSender::MaybeIncreaseCwnd(
314 QuicPacketSequenceNumber acked_sequence_number,
315 QuicByteCount bytes_in_flight) {
316 LOG_IF(DFATAL, InRecovery()) << "Never increase the CWND during recovery.";
317 if (!IsCwndLimited(bytes_in_flight)) {
318 // We don't update the congestion window unless we are close to using the
319 // window we have available.
320 return;
322 if (congestion_window_ >= max_tcp_congestion_window_) {
323 return;
325 if (InSlowStart()) {
326 // TCP slow start, exponential growth, increase by one for each ACK.
327 ++congestion_window_;
328 DVLOG(1) << "Slow start; congestion window: " << congestion_window_
329 << " slowstart threshold: " << slowstart_threshold_;
330 return;
332 // Congestion avoidance
333 if (reno_) {
334 // Classic Reno congestion avoidance.
335 ++congestion_window_count_;
336 // Divide by num_connections to smoothly increase the CWND at a faster
337 // rate than conventional Reno.
338 if (congestion_window_count_ * num_connections_ >= congestion_window_) {
339 ++congestion_window_;
340 congestion_window_count_ = 0;
343 DVLOG(1) << "Reno; congestion window: " << congestion_window_
344 << " slowstart threshold: " << slowstart_threshold_
345 << " congestion window count: " << congestion_window_count_;
346 } else {
347 congestion_window_ = min(max_tcp_congestion_window_,
348 cubic_.CongestionWindowAfterAck(
349 congestion_window_, rtt_stats_->min_rtt()));
350 DVLOG(1) << "Cubic; congestion window: " << congestion_window_
351 << " slowstart threshold: " << slowstart_threshold_;
355 void TcpCubicSender::OnRetransmissionTimeout(bool packets_retransmitted) {
356 largest_sent_at_last_cutback_ = 0;
357 if (!packets_retransmitted) {
358 return;
360 cubic_.Reset();
361 hybrid_slow_start_.Restart();
362 slowstart_threshold_ = congestion_window_ / 2;
363 congestion_window_ = min_congestion_window_;
366 CongestionControlType TcpCubicSender::GetCongestionControlType() const {
367 return reno_ ? kReno : kCubic;
370 } // namespace net