blob: 5705c0b03b5c7eb3a7e7b3e312897b5697f98646 [file] [log] [blame]
[email protected]4d1789c32013-09-18 23:54:361// Copyright 2013 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.
4
5#include "net/quic/quic_sent_packet_manager.h"
6
[email protected]b9475b582014-03-20 20:04:337#include <algorithm>
8
[email protected]4d1789c32013-09-18 23:54:369#include "base/logging.h"
10#include "base/stl_util.h"
[email protected]2adef90e2013-12-02 20:26:5711#include "net/quic/congestion_control/pacing_sender.h"
[email protected]79d13dcb2014-02-05 07:23:1312#include "net/quic/crypto/crypto_protocol.h"
[email protected]0741d712013-10-08 00:10:1113#include "net/quic/quic_ack_notifier_manager.h"
[email protected]6ae6e342014-02-06 02:21:4214#include "net/quic/quic_connection_stats.h"
[email protected]9bb57c72014-03-31 20:36:0415#include "net/quic/quic_flags.h"
[email protected]cc7ec7f2014-01-25 20:32:5116#include "net/quic/quic_utils_chromium.h"
[email protected]4d1789c32013-09-18 23:54:3617
18using std::make_pair;
[email protected]066d8182014-01-04 02:02:4519using std::max;
[email protected]2adef90e2013-12-02 20:26:5720using std::min;
[email protected]4d1789c32013-09-18 23:54:3621
22namespace net {
[email protected]2adef90e2013-12-02 20:26:5723namespace {
[email protected]2adef90e2013-12-02 20:26:5724static const int kDefaultRetransmissionTimeMs = 500;
25// TCP RFC calls for 1 second RTO however Linux differs from this default and
26// define the minimum RTO to 200ms, we will use the same until we have data to
27// support a higher or lower value.
28static const int kMinRetransmissionTimeMs = 200;
29static const int kMaxRetransmissionTimeMs = 60000;
30static const size_t kMaxRetransmissions = 10;
31
[email protected]066d8182014-01-04 02:02:4532// Only exponentially back off the handshake timer 5 times due to a timeout.
33static const size_t kMaxHandshakeRetransmissionBackoffs = 5;
34static const size_t kMinHandshakeTimeoutMs = 10;
35
36// Sends up to two tail loss probes before firing an RTO,
37// per draft RFC draft-dukkipati-tcpm-tcp-loss-probe.
38static const size_t kDefaultMaxTailLossProbes = 2;
39static const int64 kMinTailLossProbeTimeoutMs = 10;
40
[email protected]ce7bb1412014-05-17 15:51:3341// Number of samples before we force a new recent min rtt to be captured.
42static const size_t kNumMinRttSamplesAfterQuiescence = 2;
43
[email protected]0c6b10ad2014-07-02 19:47:0044// Number of unpaced packets to send after quiescence.
45static const size_t kInitialUnpacedBurst = 10;
46
[email protected]77b5d50b2014-05-07 22:48:4847bool HasCryptoHandshake(const TransmissionInfo& transmission_info) {
[email protected]cb23a922014-02-20 17:42:3848 if (transmission_info.retransmittable_frames == NULL) {
49 return false;
50 }
51 return transmission_info.retransmittable_frames->HasCryptoHandshake() ==
52 IS_HANDSHAKE;
53}
[email protected]066d8182014-01-04 02:02:4554
[email protected]2adef90e2013-12-02 20:26:5755} // namespace
[email protected]4d1789c32013-09-18 23:54:3656
57#define ENDPOINT (is_server_ ? "Server: " : " Client: ")
58
[email protected]4d1789c32013-09-18 23:54:3659QuicSentPacketManager::QuicSentPacketManager(bool is_server,
[email protected]2adef90e2013-12-02 20:26:5760 const QuicClock* clock,
[email protected]6ae6e342014-02-06 02:21:4261 QuicConnectionStats* stats,
[email protected]c5cc9bd2014-03-31 23:17:1462 CongestionFeedbackType type,
63 LossDetectionType loss_type)
[email protected]ffc34bf2014-03-07 02:42:0264 : unacked_packets_(),
[email protected]cb23a922014-02-20 17:42:3865 is_server_(is_server),
[email protected]2adef90e2013-12-02 20:26:5766 clock_(clock),
[email protected]6ae6e342014-02-06 02:21:4267 stats_(stats),
[email protected]730b35d72014-06-05 03:23:2268 debug_delegate_(NULL),
[email protected]ffc34bf2014-03-07 02:42:0269 send_algorithm_(
70 SendAlgorithmInterface::Create(clock, &rtt_stats_, type, stats)),
[email protected]c5cc9bd2014-03-31 23:17:1471 loss_algorithm_(LossDetectionInterface::Create(loss_type)),
[email protected]93dd91f2014-02-27 00:09:0372 largest_observed_(0),
[email protected]cc1aa272014-06-30 19:48:2273 first_rto_transmission_(0),
[email protected]2adef90e2013-12-02 20:26:5774 consecutive_rto_count_(0),
[email protected]066d8182014-01-04 02:02:4575 consecutive_tlp_count_(0),
76 consecutive_crypto_retransmission_count_(0),
[email protected]9cda5fd2014-06-03 10:20:2877 pending_tlp_transmission_(false),
[email protected]066d8182014-01-04 02:02:4578 max_tail_loss_probes_(kDefaultMaxTailLossProbes),
[email protected]2adef90e2013-12-02 20:26:5779 using_pacing_(false) {
[email protected]4d1789c32013-09-18 23:54:3680}
81
82QuicSentPacketManager::~QuicSentPacketManager() {
[email protected]2adef90e2013-12-02 20:26:5783}
84
85void QuicSentPacketManager::SetFromConfig(const QuicConfig& config) {
[email protected]2d43c40122014-04-21 14:51:2786 if (config.HasReceivedInitialRoundTripTimeUs() &&
87 config.ReceivedInitialRoundTripTimeUs() > 0) {
88 rtt_stats_.set_initial_rtt_us(min(kMaxInitialRoundTripTimeUs,
89 config.ReceivedInitialRoundTripTimeUs()));
[email protected]2adef90e2013-12-02 20:26:5790 }
[email protected]9cda5fd2014-06-03 10:20:2891 // TODO(ianswett): BBR is currently a server only feature.
[email protected]9dc43a12014-06-26 23:40:1492 if (config.HasReceivedConnectionOptions() &&
93 ContainsQuicTag(config.ReceivedConnectionOptions(), kTBBR)) {
[email protected]9cda5fd2014-06-03 10:20:2894 send_algorithm_.reset(
95 SendAlgorithmInterface::Create(clock_, &rtt_stats_, kTCPBBR, stats_));
96 }
[email protected]9dc43a12014-06-26 23:40:1497 if (config.congestion_feedback() == kPACE ||
98 (config.HasReceivedConnectionOptions() &&
99 ContainsQuicTag(config.ReceivedConnectionOptions(), kPACE))) {
[email protected]2adef90e2013-12-02 20:26:57100 MaybeEnablePacing();
101 }
[email protected]9dc43a12014-06-26 23:40:14102 // TODO(ianswett): Remove the "HasReceivedLossDetection" branch once
103 // the ConnectionOptions code is live everywhere.
104 if ((config.HasReceivedLossDetection() &&
105 config.ReceivedLossDetection() == kTIME) ||
106 (config.HasReceivedConnectionOptions() &&
107 ContainsQuicTag(config.ReceivedConnectionOptions(), kTIME))) {
[email protected]c5cc9bd2014-03-31 23:17:14108 loss_algorithm_.reset(LossDetectionInterface::Create(kTime));
109 }
[email protected]2adef90e2013-12-02 20:26:57110 send_algorithm_->SetFromConfig(config, is_server_);
111}
112
[email protected]c0680202013-12-18 04:52:34113// TODO(ianswett): Combine this method with OnPacketSent once packets are always
114// sent in order and the connection tracks RetransmittableFrames for longer.
[email protected]4d1789c32013-09-18 23:54:36115void QuicSentPacketManager::OnSerializedPacket(
[email protected]457d6952013-12-13 09:24:58116 const SerializedPacket& serialized_packet) {
[email protected]c0680202013-12-18 04:52:34117 if (serialized_packet.retransmittable_frames) {
118 ack_notifier_manager_.OnSerializedPacket(serialized_packet);
[email protected]4d1789c32013-09-18 23:54:36119 }
120
[email protected]cb23a922014-02-20 17:42:38121 unacked_packets_.AddPacket(serialized_packet);
[email protected]4d1789c32013-09-18 23:54:36122}
123
124void QuicSentPacketManager::OnRetransmittedPacket(
125 QuicPacketSequenceNumber old_sequence_number,
126 QuicPacketSequenceNumber new_sequence_number) {
[email protected]730b35d72014-06-05 03:23:22127 TransmissionType transmission_type;
128 PendingRetransmissionMap::iterator it =
129 pending_retransmissions_.find(old_sequence_number);
130 if (it != pending_retransmissions_.end()) {
131 transmission_type = it->second;
132 pending_retransmissions_.erase(it);
133 } else {
134 DLOG(DFATAL) << "Expected sequence number to be in "
135 "pending_retransmissions_. sequence_number: " << old_sequence_number;
136 transmission_type = NOT_RETRANSMISSION;
137 }
[email protected]0741d712013-10-08 00:10:11138
139 // A notifier may be waiting to hear about ACKs for the original sequence
140 // number. Inform them that the sequence number has changed.
141 ack_notifier_manager_.UpdateSequenceNumber(old_sequence_number,
142 new_sequence_number);
143
[email protected]cb23a922014-02-20 17:42:38144 unacked_packets_.OnRetransmittedPacket(old_sequence_number,
[email protected]730b35d72014-06-05 03:23:22145 new_sequence_number,
146 transmission_type);
[email protected]c67a82cb2013-09-24 02:53:21147}
148
[email protected]257f24f2014-04-01 09:15:37149void QuicSentPacketManager::OnIncomingAck(
[email protected]2d43c40122014-04-21 14:51:27150 const ReceivedPacketInfo& received_info,
151 QuicTime ack_receive_time) {
[email protected]77b5d50b2014-05-07 22:48:48152 QuicByteCount bytes_in_flight = unacked_packets_.bytes_in_flight();
153
[email protected]9f0dcd4e2014-01-16 15:58:14154 // We rely on delta_time_largest_observed to compute an RTT estimate, so
155 // we only update rtt when the largest observed gets acked.
[email protected]77b5d50b2014-05-07 22:48:48156 bool largest_observed_acked = MaybeUpdateRTT(received_info, ack_receive_time);
[email protected]aa7e4ef2014-05-28 03:53:15157 if (largest_observed_ < received_info.largest_observed) {
158 largest_observed_ = received_info.largest_observed;
159 unacked_packets_.IncreaseLargestObserved(largest_observed_);
160 }
[email protected]e6ccc1a62013-12-02 07:02:12161 HandleAckForSentPackets(received_info);
[email protected]ef0da582014-05-09 07:16:30162 InvokeLossDetection(ack_receive_time);
[email protected]77b5d50b2014-05-07 22:48:48163 MaybeInvokeCongestionEvent(largest_observed_acked, bytes_in_flight);
[email protected]41fb6372013-12-10 05:26:40164
[email protected]66ae5962014-05-22 11:13:05165 // If we have received a truncated ack, then we need to clear out some
166 // previous transmissions to allow the peer to actually ACK new packets.
167 if (received_info.is_truncated) {
168 unacked_packets_.ClearPreviousRetransmissions(
169 received_info.missing_packets.size() / 2);
170 }
171
[email protected]9f0dcd4e2014-01-16 15:58:14172 // Anytime we are making forward progress and have a new RTT estimate, reset
173 // the backoff counters.
174 if (largest_observed_acked) {
[email protected]066d8182014-01-04 02:02:45175 // Reset all retransmit counters any time a new packet is acked.
[email protected]41fb6372013-12-10 05:26:40176 consecutive_rto_count_ = 0;
[email protected]066d8182014-01-04 02:02:45177 consecutive_tlp_count_ = 0;
178 consecutive_crypto_retransmission_count_ = 0;
[email protected]41fb6372013-12-10 05:26:40179 }
[email protected]c67a82cb2013-09-24 02:53:21180}
181
[email protected]77b5d50b2014-05-07 22:48:48182void QuicSentPacketManager::MaybeInvokeCongestionEvent(
183 bool rtt_updated, QuicByteCount bytes_in_flight) {
184 if (rtt_updated || !packets_acked_.empty() ||
185 !packets_lost_.empty()) {
186 send_algorithm_->OnCongestionEvent(
187 rtt_updated, bytes_in_flight, packets_acked_, packets_lost_);
188 packets_acked_.clear();
189 packets_lost_.clear();
190 }
191}
192
[email protected]4d1789c32013-09-18 23:54:36193void QuicSentPacketManager::HandleAckForSentPackets(
[email protected]e6ccc1a62013-12-02 07:02:12194 const ReceivedPacketInfo& received_info) {
[email protected]4d1789c32013-09-18 23:54:36195 // Go through the packets we have not received an ack for and see if this
196 // incoming_ack shows they've been seen by the peer.
[email protected]2d43c40122014-04-21 14:51:27197 QuicTime::Delta delta_largest_observed =
198 received_info.delta_time_largest_observed;
[email protected]cb23a922014-02-20 17:42:38199 QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
[email protected]4d1789c32013-09-18 23:54:36200 while (it != unacked_packets_.end()) {
201 QuicPacketSequenceNumber sequence_number = it->first;
[email protected]c67a82cb2013-09-24 02:53:21202 if (sequence_number > received_info.largest_observed) {
[email protected]c5cc9bd2014-03-31 23:17:14203 // These packets are still in flight.
[email protected]4d1789c32013-09-18 23:54:36204 break;
205 }
[email protected]c67a82cb2013-09-24 02:53:21206
[email protected]622f94762013-11-20 21:58:10207 if (IsAwaitingPacket(received_info, sequence_number)) {
[email protected]aa7e4ef2014-05-28 03:53:15208 // Consider it multiple nacks when there is a gap between the missing
209 // packet and the largest observed, since the purpose of a nack
210 // threshold is to tolerate re-ordering. This handles both StretchAcks
211 // and Forward Acks.
212 // The nack count only increases when the largest observed increases.
213 size_t min_nacks = received_info.largest_observed - sequence_number;
214 // Truncated acks can nack the largest observed, so use a min of 1.
215 if (min_nacks == 0) {
216 min_nacks = 1;
[email protected]c5cc9bd2014-03-31 23:17:14217 }
[email protected]aa7e4ef2014-05-28 03:53:15218 unacked_packets_.NackPacket(sequence_number, min_nacks);
219 ++it;
[email protected]c67a82cb2013-09-24 02:53:21220 continue;
221 }
[email protected]622f94762013-11-20 21:58:10222 // Packet was acked, so remove it from our unacked packet list.
[email protected]77b5d50b2014-05-07 22:48:48223 DVLOG(1) << ENDPOINT << "Got an ack for packet " << sequence_number;
[email protected]622f94762013-11-20 21:58:10224 // If data is associated with the most recent transmission of this
225 // packet, then inform the caller.
[email protected]aa7e4ef2014-05-28 03:53:15226 if (it->second.in_flight) {
[email protected]77b5d50b2014-05-07 22:48:48227 packets_acked_[sequence_number] = it->second;
228 }
[email protected]aa7e4ef2014-05-28 03:53:15229 it = MarkPacketHandled(it, delta_largest_observed);
[email protected]c67a82cb2013-09-24 02:53:21230 }
231
[email protected]bdf2d432014-02-09 10:45:41232 // Discard any retransmittable frames associated with revived packets.
233 for (SequenceNumberSet::const_iterator revived_it =
234 received_info.revived_packets.begin();
235 revived_it != received_info.revived_packets.end(); ++revived_it) {
[email protected]2d43c40122014-04-21 14:51:27236 MarkPacketRevived(*revived_it, delta_largest_observed);
[email protected]bdf2d432014-02-09 10:45:41237 }
[email protected]4d1789c32013-09-18 23:54:36238}
239
[email protected]c67a82cb2013-09-24 02:53:21240bool QuicSentPacketManager::HasRetransmittableFrames(
241 QuicPacketSequenceNumber sequence_number) const {
[email protected]cb23a922014-02-20 17:42:38242 return unacked_packets_.HasRetransmittableFrames(sequence_number);
[email protected]c67a82cb2013-09-24 02:53:21243}
244
[email protected]41fb6372013-12-10 05:26:40245void QuicSentPacketManager::RetransmitUnackedPackets(
246 RetransmissionType retransmission_type) {
[email protected]aa7e4ef2014-05-28 03:53:15247 QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
248 while (it != unacked_packets_.end()) {
249 const RetransmittableFrames* frames = it->second.retransmittable_frames;
[email protected]698863af2014-02-07 22:32:15250 // TODO(ianswett): Consider adding a new retransmission type which removes
251 // all these old packets from unacked and retransmits them as new sequence
252 // numbers with no connection to the previous ones.
253 if (frames != NULL && (retransmission_type == ALL_PACKETS ||
254 frames->encryption_level() == ENCRYPTION_INITIAL)) {
[email protected]aa7e4ef2014-05-28 03:53:15255 MarkForRetransmission(it->first, ALL_UNACKED_RETRANSMISSION);
[email protected]698863af2014-02-07 22:32:15256 }
[email protected]aa7e4ef2014-05-28 03:53:15257 ++it;
[email protected]41fb6372013-12-10 05:26:40258 }
259}
260
[email protected]6d9ca3b2014-05-13 07:44:22261void QuicSentPacketManager::NeuterUnencryptedPackets() {
[email protected]aa7e4ef2014-05-28 03:53:15262 QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
263 while (it != unacked_packets_.end()) {
264 const RetransmittableFrames* frames = it->second.retransmittable_frames;
265 QuicPacketSequenceNumber sequence_number = it->first;
266 ++it;
[email protected]82168ee22014-04-30 22:25:48267 if (frames != NULL && frames->encryption_level() == ENCRYPTION_NONE) {
[email protected]27e16d02014-05-14 21:28:02268 // Once you're forward secure, no unencrypted packets will be sent, crypto
269 // or otherwise. Unencrypted packets are neutered and abandoned, to ensure
270 // they are not retransmitted or considered lost from a congestion control
271 // perspective.
[email protected]aa7e4ef2014-05-28 03:53:15272 pending_retransmissions_.erase(sequence_number);
273 unacked_packets_.RemoveFromInFlight(sequence_number);
274 // RemoveRetransmittibility is safe because only the newest sequence
275 // number can have frames.
276 unacked_packets_.RemoveRetransmittability(sequence_number);
[email protected]82168ee22014-04-30 22:25:48277 }
[email protected]82168ee22014-04-30 22:25:48278 }
279}
280
[email protected]c0680202013-12-18 04:52:34281void QuicSentPacketManager::MarkForRetransmission(
[email protected]45a3e152013-09-25 16:35:11282 QuicPacketSequenceNumber sequence_number,
283 TransmissionType transmission_type) {
[email protected]77b5d50b2014-05-07 22:48:48284 const TransmissionInfo& transmission_info =
[email protected]cb23a922014-02-20 17:42:38285 unacked_packets_.GetTransmissionInfo(sequence_number);
286 LOG_IF(DFATAL, transmission_info.retransmittable_frames == NULL);
[email protected]6d9ca3b2014-05-13 07:44:22287 if (transmission_type != TLP_RETRANSMISSION) {
[email protected]aa7e4ef2014-05-28 03:53:15288 unacked_packets_.RemoveFromInFlight(sequence_number);
[email protected]6d9ca3b2014-05-13 07:44:22289 }
[email protected]c0680202013-12-18 04:52:34290 // TODO(ianswett): Currently the RTO can fire while there are pending NACK
291 // retransmissions for the same data, which is not ideal.
[email protected]45a3e152013-09-25 16:35:11292 if (ContainsKey(pending_retransmissions_, sequence_number)) {
[email protected]c0680202013-12-18 04:52:34293 return;
[email protected]45a3e152013-09-25 16:35:11294 }
295
296 pending_retransmissions_[sequence_number] = transmission_type;
[email protected]45a3e152013-09-25 16:35:11297}
298
[email protected]730b35d72014-06-05 03:23:22299void QuicSentPacketManager::RecordSpuriousRetransmissions(
300 const SequenceNumberSet& all_transmissions,
301 QuicPacketSequenceNumber acked_sequence_number) {
[email protected]cc1aa272014-06-30 19:48:22302 if (acked_sequence_number < first_rto_transmission_) {
303 // Cancel all pending RTO transmissions and restore their in flight status.
304 // Replace SRTT with latest_rtt and increase the variance to prevent
305 // a spurious RTO from happening again.
306 rtt_stats_.ExpireSmoothedMetrics();
307 for (PendingRetransmissionMap::const_iterator it =
308 pending_retransmissions_.begin();
309 it != pending_retransmissions_.end(); ++it) {
310 DCHECK_EQ(it->second, RTO_RETRANSMISSION);
311 unacked_packets_.RestoreInFlight(it->first);
312 }
313 pending_retransmissions_.clear();
314 send_algorithm_->RevertRetransmissionTimeout();
315 first_rto_transmission_ = 0;
316 ++stats_->spurious_rto_count;
317 }
[email protected]730b35d72014-06-05 03:23:22318 for (SequenceNumberSet::const_iterator
319 it = all_transmissions.upper_bound(acked_sequence_number),
320 end = all_transmissions.end();
321 it != end;
322 ++it) {
323 const TransmissionInfo& retransmit_info =
324 unacked_packets_.GetTransmissionInfo(*it);
325
326 stats_->bytes_spuriously_retransmitted += retransmit_info.bytes_sent;
327 ++stats_->packets_spuriously_retransmitted;
328 if (debug_delegate_ != NULL) {
329 debug_delegate_->OnSpuriousPacketRetransmition(
330 retransmit_info.transmission_type,
331 retransmit_info.bytes_sent);
332 }
333 }
334}
335
[email protected]45a3e152013-09-25 16:35:11336bool QuicSentPacketManager::HasPendingRetransmissions() const {
337 return !pending_retransmissions_.empty();
338}
339
340QuicSentPacketManager::PendingRetransmission
341 QuicSentPacketManager::NextPendingRetransmission() {
342 DCHECK(!pending_retransmissions_.empty());
343 QuicPacketSequenceNumber sequence_number =
344 pending_retransmissions_.begin()->first;
[email protected]9bb57c72014-03-31 20:36:04345 TransmissionType transmission_type = pending_retransmissions_.begin()->second;
346 if (unacked_packets_.HasPendingCryptoPackets()) {
347 // Ensure crypto packets are retransmitted before other packets.
348 PendingRetransmissionMap::const_iterator it =
349 pending_retransmissions_.begin();
350 do {
[email protected]77b5d50b2014-05-07 22:48:48351 if (HasCryptoHandshake(unacked_packets_.GetTransmissionInfo(it->first))) {
[email protected]9bb57c72014-03-31 20:36:04352 sequence_number = it->first;
353 transmission_type = it->second;
354 break;
355 }
356 ++it;
357 } while (it != pending_retransmissions_.end());
358 }
[email protected]66ae5962014-05-22 11:13:05359 DCHECK(unacked_packets_.IsUnacked(sequence_number)) << sequence_number;
[email protected]77b5d50b2014-05-07 22:48:48360 const TransmissionInfo& transmission_info =
[email protected]cb23a922014-02-20 17:42:38361 unacked_packets_.GetTransmissionInfo(sequence_number);
[email protected]14a95752014-01-08 19:01:52362 DCHECK(transmission_info.retransmittable_frames);
[email protected]45a3e152013-09-25 16:35:11363
364 return PendingRetransmission(sequence_number,
[email protected]9bb57c72014-03-31 20:36:04365 transmission_type,
[email protected]14a95752014-01-08 19:01:52366 *transmission_info.retransmittable_frames,
367 transmission_info.sequence_number_length);
[email protected]45a3e152013-09-25 16:35:11368}
369
[email protected]2d43c40122014-04-21 14:51:27370void QuicSentPacketManager::MarkPacketRevived(
371 QuicPacketSequenceNumber sequence_number,
372 QuicTime::Delta delta_largest_observed) {
373 if (!unacked_packets_.IsUnacked(sequence_number)) {
374 return;
375 }
[email protected]2d43c40122014-04-21 14:51:27376
[email protected]77b5d50b2014-05-07 22:48:48377 const TransmissionInfo& transmission_info =
[email protected]2d43c40122014-04-21 14:51:27378 unacked_packets_.GetTransmissionInfo(sequence_number);
[email protected]66ae5962014-05-22 11:13:05379 QuicPacketSequenceNumber newest_transmission =
380 *transmission_info.all_transmissions->rbegin();
381 // This packet has been revived at the receiver. If we were going to
382 // retransmit it, do not retransmit it anymore.
383 pending_retransmissions_.erase(newest_transmission);
384
[email protected]2d43c40122014-04-21 14:51:27385 // The AckNotifierManager needs to be notified for revived packets,
386 // since it indicates the packet arrived from the appliction's perspective.
387 if (transmission_info.retransmittable_frames) {
388 ack_notifier_manager_.OnPacketAcked(
[email protected]66ae5962014-05-22 11:13:05389 newest_transmission, delta_largest_observed);
[email protected]2d43c40122014-04-21 14:51:27390 }
391
[email protected]aa7e4ef2014-05-28 03:53:15392 unacked_packets_.RemoveRetransmittability(sequence_number);
[email protected]2d43c40122014-04-21 14:51:27393}
394
[email protected]51cc1342014-04-18 23:44:37395QuicUnackedPacketMap::const_iterator QuicSentPacketManager::MarkPacketHandled(
[email protected]aa7e4ef2014-05-28 03:53:15396 QuicUnackedPacketMap::const_iterator it,
[email protected]77b5d50b2014-05-07 22:48:48397 QuicTime::Delta delta_largest_observed) {
[email protected]aa7e4ef2014-05-28 03:53:15398 LOG_IF(DFATAL, it == unacked_packets_.end())
399 << "MarkPacketHandled must be passed a valid iterator entry.";
400 const QuicPacketSequenceNumber sequence_number = it->first;
401 const TransmissionInfo& transmission_info = it->second;
[email protected]c0680202013-12-18 04:52:34402
[email protected]66ae5962014-05-22 11:13:05403 QuicPacketSequenceNumber newest_transmission =
404 *transmission_info.all_transmissions->rbegin();
[email protected]6d9ca3b2014-05-13 07:44:22405 // Remove the most recent packet, if it is pending retransmission.
406 pending_retransmissions_.erase(newest_transmission);
407
[email protected]730b35d72014-06-05 03:23:22408 // Notify observers about the ACKed packet.
409 {
410 // The AckNotifierManager needs to be notified about the most recent
411 // transmission, since that's the one only one it tracks.
412 ack_notifier_manager_.OnPacketAcked(newest_transmission,
413 delta_largest_observed);
414 if (newest_transmission != sequence_number) {
415 RecordSpuriousRetransmissions(*transmission_info.all_transmissions,
416 sequence_number);
417 }
418 }
419
[email protected]6d9ca3b2014-05-13 07:44:22420 // Two cases for MarkPacketHandled:
421 // 1) Handle the most recent or a crypto packet, so remove all transmissions.
422 // 2) Handle old transmission, keep all other pending transmissions,
423 // but disassociate them from one another.
[email protected]b9475b582014-03-20 20:04:33424
[email protected]66ae5962014-05-22 11:13:05425 // If it's a crypto handshake packet, discard it and all retransmissions,
426 // since they won't be acked now that one has been processed.
[email protected]6d9ca3b2014-05-13 07:44:22427 // TODO(ianswett): Instead of handling all crypto packets in a special way,
428 // only handle NULL encrypted packets in a special way.
[email protected]66ae5962014-05-22 11:13:05429 if (HasCryptoHandshake(
430 unacked_packets_.GetTransmissionInfo(newest_transmission))) {
[email protected]aa7e4ef2014-05-28 03:53:15431 unacked_packets_.RemoveFromInFlight(newest_transmission);
[email protected]c67a82cb2013-09-24 02:53:21432 }
[email protected]aa7e4ef2014-05-28 03:53:15433 unacked_packets_.RemoveFromInFlight(sequence_number);
434 unacked_packets_.RemoveRetransmittability(sequence_number);
[email protected]7ad98f42014-01-08 07:11:30435
[email protected]cb23a922014-02-20 17:42:38436 QuicUnackedPacketMap::const_iterator next_unacked = unacked_packets_.begin();
[email protected]c67a82cb2013-09-24 02:53:21437 while (next_unacked != unacked_packets_.end() &&
[email protected]66ae5962014-05-22 11:13:05438 next_unacked->first <= sequence_number) {
[email protected]41fb6372013-12-10 05:26:40439 ++next_unacked;
440 }
[email protected]c67a82cb2013-09-24 02:53:21441 return next_unacked;
[email protected]4d1789c32013-09-18 23:54:36442}
443
[email protected]4d1789c32013-09-18 23:54:36444bool QuicSentPacketManager::IsUnacked(
445 QuicPacketSequenceNumber sequence_number) const {
[email protected]cb23a922014-02-20 17:42:38446 return unacked_packets_.IsUnacked(sequence_number);
[email protected]4d1789c32013-09-18 23:54:36447}
448
[email protected]4d1789c32013-09-18 23:54:36449bool QuicSentPacketManager::HasUnackedPackets() const {
[email protected]cb23a922014-02-20 17:42:38450 return unacked_packets_.HasUnackedPackets();
[email protected]4d1789c32013-09-18 23:54:36451}
452
453QuicPacketSequenceNumber
454QuicSentPacketManager::GetLeastUnackedSentPacket() const {
[email protected]cb23a922014-02-20 17:42:38455 return unacked_packets_.GetLeastUnackedSentPacket();
[email protected]4d1789c32013-09-18 23:54:36456}
457
[email protected]2115d33ba2014-01-02 21:53:52458bool QuicSentPacketManager::OnPacketSent(
[email protected]2adef90e2013-12-02 20:26:57459 QuicPacketSequenceNumber sequence_number,
460 QuicTime sent_time,
461 QuicByteCount bytes,
462 TransmissionType transmission_type,
463 HasRetransmittableData has_retransmittable_data) {
464 DCHECK_LT(0u, sequence_number);
[email protected]cb23a922014-02-20 17:42:38465 LOG_IF(DFATAL, bytes == 0) << "Cannot send empty packets.";
[email protected]9cda5fd2014-06-03 10:20:28466 pending_tlp_transmission_ = false;
[email protected]9f0dcd4e2014-01-16 15:58:14467 // In rare circumstances, the packet could be serialized, sent, and then acked
468 // before OnPacketSent is called.
[email protected]cb23a922014-02-20 17:42:38469 if (!unacked_packets_.IsUnacked(sequence_number)) {
[email protected]9f0dcd4e2014-01-16 15:58:14470 return false;
471 }
[email protected]2adef90e2013-12-02 20:26:57472
[email protected]ce7bb1412014-05-17 15:51:33473 if (unacked_packets_.bytes_in_flight() == 0) {
474 // TODO(ianswett): Consider being less aggressive to force a new
475 // recent_min_rtt, likely by not discarding a relatively new sample.
476 DVLOG(1) << "Sampling a new recent min rtt within 2 samples. currently:"
477 << rtt_stats_.recent_min_rtt().ToMilliseconds() << "ms";
478 rtt_stats_.SampleNewRecentMinRtt(kNumMinRttSamplesAfterQuiescence);
[email protected]2adef90e2013-12-02 20:26:57479 }
[email protected]c0680202013-12-18 04:52:34480
[email protected]aa7e4ef2014-05-28 03:53:15481 // Only track packets as in flight that the send algorithm wants us to track.
482 const bool in_flight =
[email protected]ce7bb1412014-05-17 15:51:33483 send_algorithm_->OnPacketSent(sent_time,
484 unacked_packets_.bytes_in_flight(),
485 sequence_number,
486 bytes,
487 has_retransmittable_data);
[email protected]aa7e4ef2014-05-28 03:53:15488 unacked_packets_.SetSent(sequence_number, sent_time, bytes, in_flight);
[email protected]cb23a922014-02-20 17:42:38489
[email protected]ce7bb1412014-05-17 15:51:33490 // Reset the retransmission timer anytime a pending packet is sent.
[email protected]aa7e4ef2014-05-28 03:53:15491 return in_flight;
[email protected]2adef90e2013-12-02 20:26:57492}
493
494void QuicSentPacketManager::OnRetransmissionTimeout() {
[email protected]aa7e4ef2014-05-28 03:53:15495 DCHECK(unacked_packets_.HasInFlightPackets());
[email protected]d8bbef62014-06-12 08:02:50496 DCHECK(!pending_tlp_transmission_);
[email protected]3aa9ca72014-02-27 19:39:43497 // Handshake retransmission, timer based loss detection, TLP, and RTO are
498 // implemented with a single alarm. The handshake alarm is set when the
499 // handshake has not completed, the loss alarm is set when the loss detection
500 // algorithm says to, and the TLP and RTO alarms are set after that.
[email protected]066d8182014-01-04 02:02:45501 // The TLP alarm is always set to run for under an RTO.
502 switch (GetRetransmissionMode()) {
503 case HANDSHAKE_MODE:
[email protected]6ae6e342014-02-06 02:21:42504 ++stats_->crypto_retransmit_count;
[email protected]066d8182014-01-04 02:02:45505 RetransmitCryptoPackets();
506 return;
[email protected]77b5d50b2014-05-07 22:48:48507 case LOSS_MODE: {
[email protected]3aa9ca72014-02-27 19:39:43508 ++stats_->loss_timeout_count;
[email protected]77b5d50b2014-05-07 22:48:48509 QuicByteCount bytes_in_flight = unacked_packets_.bytes_in_flight();
[email protected]3aa9ca72014-02-27 19:39:43510 InvokeLossDetection(clock_->Now());
[email protected]77b5d50b2014-05-07 22:48:48511 MaybeInvokeCongestionEvent(false, bytes_in_flight);
[email protected]3aa9ca72014-02-27 19:39:43512 return;
[email protected]77b5d50b2014-05-07 22:48:48513 }
[email protected]066d8182014-01-04 02:02:45514 case TLP_MODE:
515 // If no tail loss probe can be sent, because there are no retransmittable
516 // packets, execute a conventional RTO to abandon old packets.
[email protected]6ae6e342014-02-06 02:21:42517 ++stats_->tlp_count;
[email protected]d8bbef62014-06-12 08:02:50518 ++consecutive_tlp_count_;
[email protected]9cda5fd2014-06-03 10:20:28519 pending_tlp_transmission_ = true;
[email protected]d8bbef62014-06-12 08:02:50520 // TLPs prefer sending new data instead of retransmitting data, so
521 // give the connection a chance to write before completing the TLP.
[email protected]066d8182014-01-04 02:02:45522 return;
523 case RTO_MODE:
[email protected]6ae6e342014-02-06 02:21:42524 ++stats_->rto_count;
[email protected]066d8182014-01-04 02:02:45525 RetransmitAllPackets();
526 return;
527 }
528}
529
530void QuicSentPacketManager::RetransmitCryptoPackets() {
531 DCHECK_EQ(HANDSHAKE_MODE, GetRetransmissionMode());
532 // TODO(ianswett): Typical TCP implementations only retransmit 5 times.
533 consecutive_crypto_retransmission_count_ =
534 min(kMaxHandshakeRetransmissionBackoffs,
535 consecutive_crypto_retransmission_count_ + 1);
536 bool packet_retransmitted = false;
[email protected]cb23a922014-02-20 17:42:38537 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
[email protected]14a95752014-01-08 19:01:52538 it != unacked_packets_.end(); ++it) {
539 QuicPacketSequenceNumber sequence_number = it->first;
540 const RetransmittableFrames* frames = it->second.retransmittable_frames;
[email protected]aa7e4ef2014-05-28 03:53:15541 // Only retransmit frames which are in flight, and therefore have been sent.
542 if (!it->second.in_flight || frames == NULL ||
[email protected]14a95752014-01-08 19:01:52543 frames->HasCryptoHandshake() != IS_HANDSHAKE) {
[email protected]066d8182014-01-04 02:02:45544 continue;
545 }
546 packet_retransmitted = true;
[email protected]b9475b582014-03-20 20:04:33547 MarkForRetransmission(sequence_number, HANDSHAKE_RETRANSMISSION);
[email protected]066d8182014-01-04 02:02:45548 }
549 DCHECK(packet_retransmitted) << "No crypto packets found to retransmit.";
550}
551
[email protected]d8bbef62014-06-12 08:02:50552bool QuicSentPacketManager::MaybeRetransmitTailLossProbe() {
553 if (!pending_tlp_transmission_) {
554 return false;
555 }
[email protected]cb23a922014-02-20 17:42:38556 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
[email protected]14a95752014-01-08 19:01:52557 it != unacked_packets_.end(); ++it) {
558 QuicPacketSequenceNumber sequence_number = it->first;
559 const RetransmittableFrames* frames = it->second.retransmittable_frames;
[email protected]aa7e4ef2014-05-28 03:53:15560 // Only retransmit frames which are in flight, and therefore have been sent.
561 if (!it->second.in_flight || frames == NULL) {
[email protected]066d8182014-01-04 02:02:45562 continue;
563 }
564 DCHECK_NE(IS_HANDSHAKE, frames->HasCryptoHandshake());
565 MarkForRetransmission(sequence_number, TLP_RETRANSMISSION);
[email protected]d8bbef62014-06-12 08:02:50566 return true;
[email protected]066d8182014-01-04 02:02:45567 }
568 DLOG(FATAL)
569 << "No retransmittable packets, so RetransmitOldestPacket failed.";
[email protected]d8bbef62014-06-12 08:02:50570 return false;
[email protected]066d8182014-01-04 02:02:45571}
572
573void QuicSentPacketManager::RetransmitAllPackets() {
[email protected]9cda5fd2014-06-03 10:20:28574 DVLOG(1) << "RetransmitAllPackets() called with "
[email protected]cb23a922014-02-20 17:42:38575 << unacked_packets_.GetNumUnackedPackets() << " unacked packets.";
[email protected]066d8182014-01-04 02:02:45576 // Request retransmission of all retransmittable packets when the RTO
577 // fires, and let the congestion manager decide how many to send
578 // immediately and the remaining packets will be queued.
579 // Abandon any non-retransmittable packets that are sufficiently old.
[email protected]457d6952013-12-13 09:24:58580 bool packets_retransmitted = false;
[email protected]aa7e4ef2014-05-28 03:53:15581 QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
582 while (it != unacked_packets_.end()) {
583 const RetransmittableFrames* frames = it->second.retransmittable_frames;
584 QuicPacketSequenceNumber sequence_number = it->first;
585 ++it;
586 if (frames != NULL) {
[email protected]457d6952013-12-13 09:24:58587 packets_retransmitted = true;
[email protected]aa7e4ef2014-05-28 03:53:15588 MarkForRetransmission(sequence_number, RTO_RETRANSMISSION);
[email protected]6d9ca3b2014-05-13 07:44:22589 } else {
[email protected]aa7e4ef2014-05-28 03:53:15590 unacked_packets_.RemoveFromInFlight(sequence_number);
[email protected]41fb6372013-12-10 05:26:40591 }
592 }
[email protected]2adef90e2013-12-02 20:26:57593
[email protected]7ad98f42014-01-08 07:11:30594 send_algorithm_->OnRetransmissionTimeout(packets_retransmitted);
[email protected]457d6952013-12-13 09:24:58595 if (packets_retransmitted) {
[email protected]cc1aa272014-06-30 19:48:22596 if (consecutive_rto_count_ == 0) {
597 first_rto_transmission_ = unacked_packets_.largest_sent_packet() + 1;
598 }
[email protected]457d6952013-12-13 09:24:58599 ++consecutive_rto_count_;
[email protected]41fb6372013-12-10 05:26:40600 }
[email protected]2adef90e2013-12-02 20:26:57601}
602
[email protected]066d8182014-01-04 02:02:45603QuicSentPacketManager::RetransmissionTimeoutMode
604 QuicSentPacketManager::GetRetransmissionMode() const {
[email protected]aa7e4ef2014-05-28 03:53:15605 DCHECK(unacked_packets_.HasInFlightPackets());
[email protected]ffc34bf2014-03-07 02:42:02606 if (unacked_packets_.HasPendingCryptoPackets()) {
[email protected]066d8182014-01-04 02:02:45607 return HANDSHAKE_MODE;
608 }
[email protected]3aa9ca72014-02-27 19:39:43609 if (loss_algorithm_->GetLossTimeout() != QuicTime::Zero()) {
610 return LOSS_MODE;
611 }
[email protected]066d8182014-01-04 02:02:45612 if (consecutive_tlp_count_ < max_tail_loss_probes_) {
[email protected]cb23a922014-02-20 17:42:38613 if (unacked_packets_.HasUnackedRetransmittableFrames()) {
614 return TLP_MODE;
[email protected]066d8182014-01-04 02:02:45615 }
616 }
617 return RTO_MODE;
618}
619
[email protected]2adef90e2013-12-02 20:26:57620void QuicSentPacketManager::OnIncomingQuicCongestionFeedbackFrame(
[email protected]41fb6372013-12-10 05:26:40621 const QuicCongestionFeedbackFrame& frame,
622 const QuicTime& feedback_receive_time) {
[email protected]2adef90e2013-12-02 20:26:57623 send_algorithm_->OnIncomingQuicCongestionFeedbackFrame(
[email protected]cb23a922014-02-20 17:42:38624 frame, feedback_receive_time);
[email protected]2adef90e2013-12-02 20:26:57625}
626
[email protected]93dd91f2014-02-27 00:09:03627void QuicSentPacketManager::InvokeLossDetection(QuicTime time) {
[email protected]cb23a922014-02-20 17:42:38628 SequenceNumberSet lost_packets =
[email protected]93dd91f2014-02-27 00:09:03629 loss_algorithm_->DetectLostPackets(unacked_packets_,
630 time,
631 largest_observed_,
[email protected]ffc34bf2014-03-07 02:42:02632 rtt_stats_);
[email protected]cb23a922014-02-20 17:42:38633 for (SequenceNumberSet::const_iterator it = lost_packets.begin();
634 it != lost_packets.end(); ++it) {
635 QuicPacketSequenceNumber sequence_number = *it;
[email protected]77b5d50b2014-05-07 22:48:48636 const TransmissionInfo& transmission_info =
637 unacked_packets_.GetTransmissionInfo(sequence_number);
[email protected]cb23a922014-02-20 17:42:38638 // TODO(ianswett): If it's expected the FEC packet may repair the loss, it
639 // should be recorded as a loss to the send algorithm, but not retransmitted
640 // until it's known whether the FEC packet arrived.
641 ++stats_->packets_lost;
[email protected]77b5d50b2014-05-07 22:48:48642 packets_lost_[sequence_number] = transmission_info;
[email protected]6d9ca3b2014-05-13 07:44:22643 DVLOG(1) << ENDPOINT << "Lost packet " << sequence_number;
[email protected]cb23a922014-02-20 17:42:38644
[email protected]77b5d50b2014-05-07 22:48:48645 if (transmission_info.retransmittable_frames != NULL) {
[email protected]b9475b582014-03-20 20:04:33646 MarkForRetransmission(sequence_number, LOSS_RETRANSMISSION);
[email protected]cb23a922014-02-20 17:42:38647 } else {
648 // Since we will not retransmit this, we need to remove it from
649 // unacked_packets_. This is either the current transmission of
[email protected]6d9ca3b2014-05-13 07:44:22650 // a packet whose previous transmission has been acked, a packet that has
651 // been TLP retransmitted, or an FEC packet.
[email protected]aa7e4ef2014-05-28 03:53:15652 unacked_packets_.RemoveFromInFlight(sequence_number);
[email protected]cb23a922014-02-20 17:42:38653 }
654 }
655}
656
[email protected]77b5d50b2014-05-07 22:48:48657bool QuicSentPacketManager::MaybeUpdateRTT(
[email protected]41fb6372013-12-10 05:26:40658 const ReceivedPacketInfo& received_info,
659 const QuicTime& ack_receive_time) {
[email protected]cb23a922014-02-20 17:42:38660 if (!unacked_packets_.IsUnacked(received_info.largest_observed)) {
[email protected]77b5d50b2014-05-07 22:48:48661 return false;
[email protected]41fb6372013-12-10 05:26:40662 }
[email protected]cb23a922014-02-20 17:42:38663 // We calculate the RTT based on the highest ACKed sequence number, the lower
664 // sequence numbers will include the ACK aggregation delay.
[email protected]77b5d50b2014-05-07 22:48:48665 const TransmissionInfo& transmission_info =
[email protected]cb23a922014-02-20 17:42:38666 unacked_packets_.GetTransmissionInfo(received_info.largest_observed);
[email protected]9f0dcd4e2014-01-16 15:58:14667 // Don't update the RTT if it hasn't been sent.
[email protected]cb23a922014-02-20 17:42:38668 if (transmission_info.sent_time == QuicTime::Zero()) {
[email protected]77b5d50b2014-05-07 22:48:48669 return false;
[email protected]9f0dcd4e2014-01-16 15:58:14670 }
[email protected]41fb6372013-12-10 05:26:40671
[email protected]14a95752014-01-08 19:01:52672 QuicTime::Delta send_delta =
[email protected]cb23a922014-02-20 17:42:38673 ack_receive_time.Subtract(transmission_info.sent_time);
[email protected]82168ee22014-04-30 22:25:48674 rtt_stats_.UpdateRtt(
675 send_delta, received_info.delta_time_largest_observed, ack_receive_time);
[email protected]77b5d50b2014-05-07 22:48:48676 return true;
[email protected]41fb6372013-12-10 05:26:40677}
678
[email protected]2adef90e2013-12-02 20:26:57679QuicTime::Delta QuicSentPacketManager::TimeUntilSend(
680 QuicTime now,
[email protected]9bb57c72014-03-31 20:36:04681 HasRetransmittableData retransmittable) {
682 // The TLP logic is entirely contained within QuicSentPacketManager, so the
683 // send algorithm does not need to be consulted.
[email protected]9cda5fd2014-06-03 10:20:28684 if (pending_tlp_transmission_) {
[email protected]9bb57c72014-03-31 20:36:04685 return QuicTime::Delta::Zero();
686 }
[email protected]77b5d50b2014-05-07 22:48:48687 return send_algorithm_->TimeUntilSend(
688 now, unacked_packets_.bytes_in_flight(), retransmittable);
[email protected]2adef90e2013-12-02 20:26:57689}
690
[email protected]2adef90e2013-12-02 20:26:57691// Ensures that the Delayed Ack timer is always set to a value lesser
692// than the retransmission timer's minimum value (MinRTO). We want the
693// delayed ack to get back to the QUIC peer before the sender's
694// retransmission timer triggers. Since we do not know the
695// reverse-path one-way delay, we assume equal delays for forward and
696// reverse paths, and ensure that the timer is set to less than half
697// of the MinRTO.
698// There may be a value in making this delay adaptive with the help of
699// the sender and a signaling mechanism -- if the sender uses a
700// different MinRTO, we may get spurious retransmissions. May not have
701// any benefits, but if the delayed ack becomes a significant source
702// of (likely, tail) latency, then consider such a mechanism.
[email protected]2115d33ba2014-01-02 21:53:52703const QuicTime::Delta QuicSentPacketManager::DelayedAckTime() const {
[email protected]2adef90e2013-12-02 20:26:57704 return QuicTime::Delta::FromMilliseconds(kMinRetransmissionTimeMs/2);
705}
706
[email protected]c0680202013-12-18 04:52:34707const QuicTime QuicSentPacketManager::GetRetransmissionTime() const {
[email protected]d8bbef62014-06-12 08:02:50708 // Don't set the timer if there are no packets in flight or we've already
709 // queued a tlp transmission and it hasn't been sent yet.
710 if (!unacked_packets_.HasInFlightPackets() || pending_tlp_transmission_) {
[email protected]c0680202013-12-18 04:52:34711 return QuicTime::Zero();
712 }
[email protected]066d8182014-01-04 02:02:45713 switch (GetRetransmissionMode()) {
714 case HANDSHAKE_MODE:
715 return clock_->ApproximateNow().Add(GetCryptoRetransmissionDelay());
[email protected]3aa9ca72014-02-27 19:39:43716 case LOSS_MODE:
717 return loss_algorithm_->GetLossTimeout();
[email protected]066d8182014-01-04 02:02:45718 case TLP_MODE: {
719 // TODO(ianswett): When CWND is available, it would be preferable to
720 // set the timer based on the earliest retransmittable packet.
721 // Base the updated timer on the send time of the last packet.
[email protected]cb23a922014-02-20 17:42:38722 const QuicTime sent_time = unacked_packets_.GetLastPacketSentTime();
[email protected]066d8182014-01-04 02:02:45723 const QuicTime tlp_time = sent_time.Add(GetTailLossProbeDelay());
[email protected]6d9ca3b2014-05-13 07:44:22724 // Ensure the TLP timer never gets set to a time in the past.
[email protected]066d8182014-01-04 02:02:45725 return QuicTime::Max(clock_->ApproximateNow(), tlp_time);
726 }
727 case RTO_MODE: {
[email protected]aa7e4ef2014-05-28 03:53:15728 // The RTO is based on the first outstanding packet.
[email protected]cb23a922014-02-20 17:42:38729 const QuicTime sent_time =
[email protected]aa7e4ef2014-05-28 03:53:15730 unacked_packets_.GetFirstInFlightPacketSentTime();
[email protected]cc1aa272014-06-30 19:48:22731 QuicTime rto_time = sent_time.Add(GetRetransmissionDelay());
732 // Wait for TLP packets to be acked before an RTO fires.
733 QuicTime tlp_time =
734 unacked_packets_.GetLastPacketSentTime().Add(GetTailLossProbeDelay());
735 return QuicTime::Max(tlp_time, rto_time);
[email protected]066d8182014-01-04 02:02:45736 }
[email protected]066d8182014-01-04 02:02:45737 }
[email protected]8a15a6c82014-01-09 13:04:46738 DCHECK(false);
[email protected]066d8182014-01-04 02:02:45739 return QuicTime::Zero();
740}
741
[email protected]7ad98f42014-01-08 07:11:30742const QuicTime::Delta QuicSentPacketManager::GetCryptoRetransmissionDelay()
743 const {
[email protected]066d8182014-01-04 02:02:45744 // This is equivalent to the TailLossProbeDelay, but slightly more aggressive
745 // because crypto handshake messages don't incur a delayed ack time.
746 int64 delay_ms = max<int64>(kMinHandshakeTimeoutMs,
[email protected]fb35b0a2014-04-15 21:06:49747 1.5 * rtt_stats_.SmoothedRtt().ToMilliseconds());
[email protected]066d8182014-01-04 02:02:45748 return QuicTime::Delta::FromMilliseconds(
749 delay_ms << consecutive_crypto_retransmission_count_);
750}
751
752const QuicTime::Delta QuicSentPacketManager::GetTailLossProbeDelay() const {
[email protected]fb35b0a2014-04-15 21:06:49753 QuicTime::Delta srtt = rtt_stats_.SmoothedRtt();
[email protected]aa7e4ef2014-05-28 03:53:15754 if (!unacked_packets_.HasMultipleInFlightPackets()) {
[email protected]066d8182014-01-04 02:02:45755 return QuicTime::Delta::Max(
756 srtt.Multiply(1.5).Add(DelayedAckTime()), srtt.Multiply(2));
757 }
758 return QuicTime::Delta::FromMilliseconds(
759 max(kMinTailLossProbeTimeoutMs,
760 static_cast<int64>(2 * srtt.ToMilliseconds())));
[email protected]c0680202013-12-18 04:52:34761}
762
[email protected]2adef90e2013-12-02 20:26:57763const QuicTime::Delta QuicSentPacketManager::GetRetransmissionDelay() const {
[email protected]2adef90e2013-12-02 20:26:57764 QuicTime::Delta retransmission_delay = send_algorithm_->RetransmissionDelay();
[email protected]6ae6e342014-02-06 02:21:42765 // TODO(rch): This code should move to |send_algorithm_|.
[email protected]2adef90e2013-12-02 20:26:57766 if (retransmission_delay.IsZero()) {
767 // We are in the initial state, use default timeout values.
768 retransmission_delay =
769 QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs);
[email protected]6ae6e342014-02-06 02:21:42770 } else if (retransmission_delay.ToMilliseconds() < kMinRetransmissionTimeMs) {
771 retransmission_delay =
772 QuicTime::Delta::FromMilliseconds(kMinRetransmissionTimeMs);
[email protected]2adef90e2013-12-02 20:26:57773 }
[email protected]6ae6e342014-02-06 02:21:42774
[email protected]2adef90e2013-12-02 20:26:57775 // Calculate exponential back off.
[email protected]066d8182014-01-04 02:02:45776 retransmission_delay = retransmission_delay.Multiply(
[email protected]7ad98f42014-01-08 07:11:30777 1 << min<size_t>(consecutive_rto_count_, kMaxRetransmissions));
[email protected]2adef90e2013-12-02 20:26:57778
[email protected]2adef90e2013-12-02 20:26:57779 if (retransmission_delay.ToMilliseconds() > kMaxRetransmissionTimeMs) {
780 return QuicTime::Delta::FromMilliseconds(kMaxRetransmissionTimeMs);
781 }
782 return retransmission_delay;
783}
784
[email protected]fb35b0a2014-04-15 21:06:49785const RttStats* QuicSentPacketManager::GetRttStats() const {
786 return &rtt_stats_;
[email protected]2adef90e2013-12-02 20:26:57787}
788
789QuicBandwidth QuicSentPacketManager::BandwidthEstimate() const {
790 return send_algorithm_->BandwidthEstimate();
791}
792
[email protected]0c6b10ad2014-07-02 19:47:00793bool QuicSentPacketManager::HasReliableBandwidthEstimate() const {
794 return send_algorithm_->HasReliableBandwidthEstimate();
795}
796
[email protected]2adef90e2013-12-02 20:26:57797QuicByteCount QuicSentPacketManager::GetCongestionWindow() const {
798 return send_algorithm_->GetCongestionWindow();
799}
800
[email protected]2adef90e2013-12-02 20:26:57801void QuicSentPacketManager::MaybeEnablePacing() {
802 if (!FLAGS_enable_quic_pacing) {
803 return;
804 }
805
806 if (using_pacing_) {
807 return;
808 }
809
[email protected]9cda5fd2014-06-03 10:20:28810 // Set up a pacing sender with a 5 millisecond alarm granularity.
[email protected]2adef90e2013-12-02 20:26:57811 using_pacing_ = true;
812 send_algorithm_.reset(
813 new PacingSender(send_algorithm_.release(),
[email protected]0c6b10ad2014-07-02 19:47:00814 QuicTime::Delta::FromMilliseconds(5),
815 kInitialUnpacedBurst));
[email protected]2adef90e2013-12-02 20:26:57816}
817
[email protected]4d1789c32013-09-18 23:54:36818} // namespace net