blob: b8a702463f9810826808fee556989bc4f3f08e5c [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]77b5d50b2014-05-07 22:48:4844bool HasCryptoHandshake(const TransmissionInfo& transmission_info) {
[email protected]cb23a922014-02-20 17:42:3845 if (transmission_info.retransmittable_frames == NULL) {
46 return false;
47 }
48 return transmission_info.retransmittable_frames->HasCryptoHandshake() ==
49 IS_HANDSHAKE;
50}
[email protected]066d8182014-01-04 02:02:4551
[email protected]2adef90e2013-12-02 20:26:5752} // namespace
[email protected]4d1789c32013-09-18 23:54:3653
54#define ENDPOINT (is_server_ ? "Server: " : " Client: ")
55
[email protected]4d1789c32013-09-18 23:54:3656QuicSentPacketManager::QuicSentPacketManager(bool is_server,
[email protected]2adef90e2013-12-02 20:26:5757 const QuicClock* clock,
[email protected]6ae6e342014-02-06 02:21:4258 QuicConnectionStats* stats,
[email protected]c5cc9bd2014-03-31 23:17:1459 CongestionFeedbackType type,
60 LossDetectionType loss_type)
[email protected]ffc34bf2014-03-07 02:42:0261 : unacked_packets_(),
[email protected]cb23a922014-02-20 17:42:3862 is_server_(is_server),
[email protected]2adef90e2013-12-02 20:26:5763 clock_(clock),
[email protected]6ae6e342014-02-06 02:21:4264 stats_(stats),
[email protected]730b35d72014-06-05 03:23:2265 debug_delegate_(NULL),
[email protected]ffc34bf2014-03-07 02:42:0266 send_algorithm_(
67 SendAlgorithmInterface::Create(clock, &rtt_stats_, type, stats)),
[email protected]c5cc9bd2014-03-31 23:17:1468 loss_algorithm_(LossDetectionInterface::Create(loss_type)),
[email protected]93dd91f2014-02-27 00:09:0369 largest_observed_(0),
[email protected]cc1aa272014-06-30 19:48:2270 first_rto_transmission_(0),
[email protected]2adef90e2013-12-02 20:26:5771 consecutive_rto_count_(0),
[email protected]066d8182014-01-04 02:02:4572 consecutive_tlp_count_(0),
73 consecutive_crypto_retransmission_count_(0),
[email protected]9cda5fd2014-06-03 10:20:2874 pending_tlp_transmission_(false),
[email protected]066d8182014-01-04 02:02:4575 max_tail_loss_probes_(kDefaultMaxTailLossProbes),
[email protected]2adef90e2013-12-02 20:26:5776 using_pacing_(false) {
[email protected]4d1789c32013-09-18 23:54:3677}
78
79QuicSentPacketManager::~QuicSentPacketManager() {
[email protected]2adef90e2013-12-02 20:26:5780}
81
82void QuicSentPacketManager::SetFromConfig(const QuicConfig& config) {
[email protected]2d43c40122014-04-21 14:51:2783 if (config.HasReceivedInitialRoundTripTimeUs() &&
84 config.ReceivedInitialRoundTripTimeUs() > 0) {
85 rtt_stats_.set_initial_rtt_us(min(kMaxInitialRoundTripTimeUs,
86 config.ReceivedInitialRoundTripTimeUs()));
[email protected]2adef90e2013-12-02 20:26:5787 }
[email protected]9cda5fd2014-06-03 10:20:2888 // TODO(ianswett): BBR is currently a server only feature.
[email protected]9dc43a12014-06-26 23:40:1489 if (config.HasReceivedConnectionOptions() &&
90 ContainsQuicTag(config.ReceivedConnectionOptions(), kTBBR)) {
[email protected]9cda5fd2014-06-03 10:20:2891 send_algorithm_.reset(
92 SendAlgorithmInterface::Create(clock_, &rtt_stats_, kTCPBBR, stats_));
93 }
[email protected]9dc43a12014-06-26 23:40:1494 if (config.congestion_feedback() == kPACE ||
95 (config.HasReceivedConnectionOptions() &&
96 ContainsQuicTag(config.ReceivedConnectionOptions(), kPACE))) {
[email protected]2adef90e2013-12-02 20:26:5797 MaybeEnablePacing();
98 }
[email protected]9dc43a12014-06-26 23:40:1499 // TODO(ianswett): Remove the "HasReceivedLossDetection" branch once
100 // the ConnectionOptions code is live everywhere.
101 if ((config.HasReceivedLossDetection() &&
102 config.ReceivedLossDetection() == kTIME) ||
103 (config.HasReceivedConnectionOptions() &&
104 ContainsQuicTag(config.ReceivedConnectionOptions(), kTIME))) {
[email protected]c5cc9bd2014-03-31 23:17:14105 loss_algorithm_.reset(LossDetectionInterface::Create(kTime));
106 }
[email protected]2adef90e2013-12-02 20:26:57107 send_algorithm_->SetFromConfig(config, is_server_);
108}
109
[email protected]c0680202013-12-18 04:52:34110// TODO(ianswett): Combine this method with OnPacketSent once packets are always
111// sent in order and the connection tracks RetransmittableFrames for longer.
[email protected]4d1789c32013-09-18 23:54:36112void QuicSentPacketManager::OnSerializedPacket(
[email protected]457d6952013-12-13 09:24:58113 const SerializedPacket& serialized_packet) {
[email protected]c0680202013-12-18 04:52:34114 if (serialized_packet.retransmittable_frames) {
115 ack_notifier_manager_.OnSerializedPacket(serialized_packet);
[email protected]4d1789c32013-09-18 23:54:36116 }
117
[email protected]cb23a922014-02-20 17:42:38118 unacked_packets_.AddPacket(serialized_packet);
[email protected]4d1789c32013-09-18 23:54:36119}
120
121void QuicSentPacketManager::OnRetransmittedPacket(
122 QuicPacketSequenceNumber old_sequence_number,
123 QuicPacketSequenceNumber new_sequence_number) {
[email protected]730b35d72014-06-05 03:23:22124 TransmissionType transmission_type;
125 PendingRetransmissionMap::iterator it =
126 pending_retransmissions_.find(old_sequence_number);
127 if (it != pending_retransmissions_.end()) {
128 transmission_type = it->second;
129 pending_retransmissions_.erase(it);
130 } else {
131 DLOG(DFATAL) << "Expected sequence number to be in "
132 "pending_retransmissions_. sequence_number: " << old_sequence_number;
133 transmission_type = NOT_RETRANSMISSION;
134 }
[email protected]0741d712013-10-08 00:10:11135
136 // A notifier may be waiting to hear about ACKs for the original sequence
137 // number. Inform them that the sequence number has changed.
138 ack_notifier_manager_.UpdateSequenceNumber(old_sequence_number,
139 new_sequence_number);
140
[email protected]cb23a922014-02-20 17:42:38141 unacked_packets_.OnRetransmittedPacket(old_sequence_number,
[email protected]730b35d72014-06-05 03:23:22142 new_sequence_number,
143 transmission_type);
[email protected]c67a82cb2013-09-24 02:53:21144}
145
[email protected]257f24f2014-04-01 09:15:37146void QuicSentPacketManager::OnIncomingAck(
[email protected]2d43c40122014-04-21 14:51:27147 const ReceivedPacketInfo& received_info,
148 QuicTime ack_receive_time) {
[email protected]77b5d50b2014-05-07 22:48:48149 QuicByteCount bytes_in_flight = unacked_packets_.bytes_in_flight();
150
[email protected]9f0dcd4e2014-01-16 15:58:14151 // We rely on delta_time_largest_observed to compute an RTT estimate, so
152 // we only update rtt when the largest observed gets acked.
[email protected]77b5d50b2014-05-07 22:48:48153 bool largest_observed_acked = MaybeUpdateRTT(received_info, ack_receive_time);
[email protected]aa7e4ef2014-05-28 03:53:15154 if (largest_observed_ < received_info.largest_observed) {
155 largest_observed_ = received_info.largest_observed;
156 unacked_packets_.IncreaseLargestObserved(largest_observed_);
157 }
[email protected]e6ccc1a62013-12-02 07:02:12158 HandleAckForSentPackets(received_info);
[email protected]ef0da582014-05-09 07:16:30159 InvokeLossDetection(ack_receive_time);
[email protected]77b5d50b2014-05-07 22:48:48160 MaybeInvokeCongestionEvent(largest_observed_acked, bytes_in_flight);
[email protected]41fb6372013-12-10 05:26:40161
[email protected]66ae5962014-05-22 11:13:05162 // If we have received a truncated ack, then we need to clear out some
163 // previous transmissions to allow the peer to actually ACK new packets.
164 if (received_info.is_truncated) {
165 unacked_packets_.ClearPreviousRetransmissions(
166 received_info.missing_packets.size() / 2);
167 }
168
[email protected]9f0dcd4e2014-01-16 15:58:14169 // Anytime we are making forward progress and have a new RTT estimate, reset
170 // the backoff counters.
171 if (largest_observed_acked) {
[email protected]066d8182014-01-04 02:02:45172 // Reset all retransmit counters any time a new packet is acked.
[email protected]41fb6372013-12-10 05:26:40173 consecutive_rto_count_ = 0;
[email protected]066d8182014-01-04 02:02:45174 consecutive_tlp_count_ = 0;
175 consecutive_crypto_retransmission_count_ = 0;
[email protected]41fb6372013-12-10 05:26:40176 }
[email protected]c67a82cb2013-09-24 02:53:21177}
178
[email protected]77b5d50b2014-05-07 22:48:48179void QuicSentPacketManager::MaybeInvokeCongestionEvent(
180 bool rtt_updated, QuicByteCount bytes_in_flight) {
181 if (rtt_updated || !packets_acked_.empty() ||
182 !packets_lost_.empty()) {
183 send_algorithm_->OnCongestionEvent(
184 rtt_updated, bytes_in_flight, packets_acked_, packets_lost_);
185 packets_acked_.clear();
186 packets_lost_.clear();
187 }
188}
189
[email protected]4d1789c32013-09-18 23:54:36190void QuicSentPacketManager::HandleAckForSentPackets(
[email protected]e6ccc1a62013-12-02 07:02:12191 const ReceivedPacketInfo& received_info) {
[email protected]4d1789c32013-09-18 23:54:36192 // Go through the packets we have not received an ack for and see if this
193 // incoming_ack shows they've been seen by the peer.
[email protected]2d43c40122014-04-21 14:51:27194 QuicTime::Delta delta_largest_observed =
195 received_info.delta_time_largest_observed;
[email protected]cb23a922014-02-20 17:42:38196 QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
[email protected]4d1789c32013-09-18 23:54:36197 while (it != unacked_packets_.end()) {
198 QuicPacketSequenceNumber sequence_number = it->first;
[email protected]c67a82cb2013-09-24 02:53:21199 if (sequence_number > received_info.largest_observed) {
[email protected]c5cc9bd2014-03-31 23:17:14200 // These packets are still in flight.
[email protected]4d1789c32013-09-18 23:54:36201 break;
202 }
[email protected]c67a82cb2013-09-24 02:53:21203
[email protected]622f94762013-11-20 21:58:10204 if (IsAwaitingPacket(received_info, sequence_number)) {
[email protected]aa7e4ef2014-05-28 03:53:15205 // Consider it multiple nacks when there is a gap between the missing
206 // packet and the largest observed, since the purpose of a nack
207 // threshold is to tolerate re-ordering. This handles both StretchAcks
208 // and Forward Acks.
209 // The nack count only increases when the largest observed increases.
210 size_t min_nacks = received_info.largest_observed - sequence_number;
211 // Truncated acks can nack the largest observed, so use a min of 1.
212 if (min_nacks == 0) {
213 min_nacks = 1;
[email protected]c5cc9bd2014-03-31 23:17:14214 }
[email protected]aa7e4ef2014-05-28 03:53:15215 unacked_packets_.NackPacket(sequence_number, min_nacks);
216 ++it;
[email protected]c67a82cb2013-09-24 02:53:21217 continue;
218 }
[email protected]622f94762013-11-20 21:58:10219 // Packet was acked, so remove it from our unacked packet list.
[email protected]77b5d50b2014-05-07 22:48:48220 DVLOG(1) << ENDPOINT << "Got an ack for packet " << sequence_number;
[email protected]622f94762013-11-20 21:58:10221 // If data is associated with the most recent transmission of this
222 // packet, then inform the caller.
[email protected]aa7e4ef2014-05-28 03:53:15223 if (it->second.in_flight) {
[email protected]77b5d50b2014-05-07 22:48:48224 packets_acked_[sequence_number] = it->second;
225 }
[email protected]aa7e4ef2014-05-28 03:53:15226 it = MarkPacketHandled(it, delta_largest_observed);
[email protected]c67a82cb2013-09-24 02:53:21227 }
228
[email protected]bdf2d432014-02-09 10:45:41229 // Discard any retransmittable frames associated with revived packets.
230 for (SequenceNumberSet::const_iterator revived_it =
231 received_info.revived_packets.begin();
232 revived_it != received_info.revived_packets.end(); ++revived_it) {
[email protected]2d43c40122014-04-21 14:51:27233 MarkPacketRevived(*revived_it, delta_largest_observed);
[email protected]bdf2d432014-02-09 10:45:41234 }
[email protected]4d1789c32013-09-18 23:54:36235}
236
[email protected]c67a82cb2013-09-24 02:53:21237bool QuicSentPacketManager::HasRetransmittableFrames(
238 QuicPacketSequenceNumber sequence_number) const {
[email protected]cb23a922014-02-20 17:42:38239 return unacked_packets_.HasRetransmittableFrames(sequence_number);
[email protected]c67a82cb2013-09-24 02:53:21240}
241
[email protected]41fb6372013-12-10 05:26:40242void QuicSentPacketManager::RetransmitUnackedPackets(
243 RetransmissionType retransmission_type) {
[email protected]aa7e4ef2014-05-28 03:53:15244 QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
245 while (it != unacked_packets_.end()) {
246 const RetransmittableFrames* frames = it->second.retransmittable_frames;
[email protected]698863af2014-02-07 22:32:15247 // TODO(ianswett): Consider adding a new retransmission type which removes
248 // all these old packets from unacked and retransmits them as new sequence
249 // numbers with no connection to the previous ones.
250 if (frames != NULL && (retransmission_type == ALL_PACKETS ||
251 frames->encryption_level() == ENCRYPTION_INITIAL)) {
[email protected]aa7e4ef2014-05-28 03:53:15252 MarkForRetransmission(it->first, ALL_UNACKED_RETRANSMISSION);
[email protected]698863af2014-02-07 22:32:15253 }
[email protected]aa7e4ef2014-05-28 03:53:15254 ++it;
[email protected]41fb6372013-12-10 05:26:40255 }
256}
257
[email protected]6d9ca3b2014-05-13 07:44:22258void QuicSentPacketManager::NeuterUnencryptedPackets() {
[email protected]aa7e4ef2014-05-28 03:53:15259 QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
260 while (it != unacked_packets_.end()) {
261 const RetransmittableFrames* frames = it->second.retransmittable_frames;
262 QuicPacketSequenceNumber sequence_number = it->first;
263 ++it;
[email protected]82168ee22014-04-30 22:25:48264 if (frames != NULL && frames->encryption_level() == ENCRYPTION_NONE) {
[email protected]27e16d02014-05-14 21:28:02265 // Once you're forward secure, no unencrypted packets will be sent, crypto
266 // or otherwise. Unencrypted packets are neutered and abandoned, to ensure
267 // they are not retransmitted or considered lost from a congestion control
268 // perspective.
[email protected]aa7e4ef2014-05-28 03:53:15269 pending_retransmissions_.erase(sequence_number);
270 unacked_packets_.RemoveFromInFlight(sequence_number);
271 // RemoveRetransmittibility is safe because only the newest sequence
272 // number can have frames.
273 unacked_packets_.RemoveRetransmittability(sequence_number);
[email protected]82168ee22014-04-30 22:25:48274 }
[email protected]82168ee22014-04-30 22:25:48275 }
276}
277
[email protected]c0680202013-12-18 04:52:34278void QuicSentPacketManager::MarkForRetransmission(
[email protected]45a3e152013-09-25 16:35:11279 QuicPacketSequenceNumber sequence_number,
280 TransmissionType transmission_type) {
[email protected]77b5d50b2014-05-07 22:48:48281 const TransmissionInfo& transmission_info =
[email protected]cb23a922014-02-20 17:42:38282 unacked_packets_.GetTransmissionInfo(sequence_number);
283 LOG_IF(DFATAL, transmission_info.retransmittable_frames == NULL);
[email protected]6d9ca3b2014-05-13 07:44:22284 if (transmission_type != TLP_RETRANSMISSION) {
[email protected]aa7e4ef2014-05-28 03:53:15285 unacked_packets_.RemoveFromInFlight(sequence_number);
[email protected]6d9ca3b2014-05-13 07:44:22286 }
[email protected]c0680202013-12-18 04:52:34287 // TODO(ianswett): Currently the RTO can fire while there are pending NACK
288 // retransmissions for the same data, which is not ideal.
[email protected]45a3e152013-09-25 16:35:11289 if (ContainsKey(pending_retransmissions_, sequence_number)) {
[email protected]c0680202013-12-18 04:52:34290 return;
[email protected]45a3e152013-09-25 16:35:11291 }
292
293 pending_retransmissions_[sequence_number] = transmission_type;
[email protected]45a3e152013-09-25 16:35:11294}
295
[email protected]730b35d72014-06-05 03:23:22296void QuicSentPacketManager::RecordSpuriousRetransmissions(
297 const SequenceNumberSet& all_transmissions,
298 QuicPacketSequenceNumber acked_sequence_number) {
[email protected]cc1aa272014-06-30 19:48:22299 if (acked_sequence_number < first_rto_transmission_) {
300 // Cancel all pending RTO transmissions and restore their in flight status.
301 // Replace SRTT with latest_rtt and increase the variance to prevent
302 // a spurious RTO from happening again.
303 rtt_stats_.ExpireSmoothedMetrics();
304 for (PendingRetransmissionMap::const_iterator it =
305 pending_retransmissions_.begin();
306 it != pending_retransmissions_.end(); ++it) {
307 DCHECK_EQ(it->second, RTO_RETRANSMISSION);
308 unacked_packets_.RestoreInFlight(it->first);
309 }
310 pending_retransmissions_.clear();
311 send_algorithm_->RevertRetransmissionTimeout();
312 first_rto_transmission_ = 0;
313 ++stats_->spurious_rto_count;
314 }
[email protected]730b35d72014-06-05 03:23:22315 for (SequenceNumberSet::const_iterator
316 it = all_transmissions.upper_bound(acked_sequence_number),
317 end = all_transmissions.end();
318 it != end;
319 ++it) {
320 const TransmissionInfo& retransmit_info =
321 unacked_packets_.GetTransmissionInfo(*it);
322
323 stats_->bytes_spuriously_retransmitted += retransmit_info.bytes_sent;
324 ++stats_->packets_spuriously_retransmitted;
325 if (debug_delegate_ != NULL) {
326 debug_delegate_->OnSpuriousPacketRetransmition(
327 retransmit_info.transmission_type,
328 retransmit_info.bytes_sent);
329 }
330 }
331}
332
[email protected]45a3e152013-09-25 16:35:11333bool QuicSentPacketManager::HasPendingRetransmissions() const {
334 return !pending_retransmissions_.empty();
335}
336
337QuicSentPacketManager::PendingRetransmission
338 QuicSentPacketManager::NextPendingRetransmission() {
339 DCHECK(!pending_retransmissions_.empty());
340 QuicPacketSequenceNumber sequence_number =
341 pending_retransmissions_.begin()->first;
[email protected]9bb57c72014-03-31 20:36:04342 TransmissionType transmission_type = pending_retransmissions_.begin()->second;
343 if (unacked_packets_.HasPendingCryptoPackets()) {
344 // Ensure crypto packets are retransmitted before other packets.
345 PendingRetransmissionMap::const_iterator it =
346 pending_retransmissions_.begin();
347 do {
[email protected]77b5d50b2014-05-07 22:48:48348 if (HasCryptoHandshake(unacked_packets_.GetTransmissionInfo(it->first))) {
[email protected]9bb57c72014-03-31 20:36:04349 sequence_number = it->first;
350 transmission_type = it->second;
351 break;
352 }
353 ++it;
354 } while (it != pending_retransmissions_.end());
355 }
[email protected]66ae5962014-05-22 11:13:05356 DCHECK(unacked_packets_.IsUnacked(sequence_number)) << sequence_number;
[email protected]77b5d50b2014-05-07 22:48:48357 const TransmissionInfo& transmission_info =
[email protected]cb23a922014-02-20 17:42:38358 unacked_packets_.GetTransmissionInfo(sequence_number);
[email protected]14a95752014-01-08 19:01:52359 DCHECK(transmission_info.retransmittable_frames);
[email protected]45a3e152013-09-25 16:35:11360
361 return PendingRetransmission(sequence_number,
[email protected]9bb57c72014-03-31 20:36:04362 transmission_type,
[email protected]14a95752014-01-08 19:01:52363 *transmission_info.retransmittable_frames,
364 transmission_info.sequence_number_length);
[email protected]45a3e152013-09-25 16:35:11365}
366
[email protected]2d43c40122014-04-21 14:51:27367void QuicSentPacketManager::MarkPacketRevived(
368 QuicPacketSequenceNumber sequence_number,
369 QuicTime::Delta delta_largest_observed) {
370 if (!unacked_packets_.IsUnacked(sequence_number)) {
371 return;
372 }
[email protected]2d43c40122014-04-21 14:51:27373
[email protected]77b5d50b2014-05-07 22:48:48374 const TransmissionInfo& transmission_info =
[email protected]2d43c40122014-04-21 14:51:27375 unacked_packets_.GetTransmissionInfo(sequence_number);
[email protected]66ae5962014-05-22 11:13:05376 QuicPacketSequenceNumber newest_transmission =
377 *transmission_info.all_transmissions->rbegin();
378 // This packet has been revived at the receiver. If we were going to
379 // retransmit it, do not retransmit it anymore.
380 pending_retransmissions_.erase(newest_transmission);
381
[email protected]2d43c40122014-04-21 14:51:27382 // The AckNotifierManager needs to be notified for revived packets,
383 // since it indicates the packet arrived from the appliction's perspective.
384 if (transmission_info.retransmittable_frames) {
385 ack_notifier_manager_.OnPacketAcked(
[email protected]66ae5962014-05-22 11:13:05386 newest_transmission, delta_largest_observed);
[email protected]2d43c40122014-04-21 14:51:27387 }
388
[email protected]aa7e4ef2014-05-28 03:53:15389 unacked_packets_.RemoveRetransmittability(sequence_number);
[email protected]2d43c40122014-04-21 14:51:27390}
391
[email protected]51cc1342014-04-18 23:44:37392QuicUnackedPacketMap::const_iterator QuicSentPacketManager::MarkPacketHandled(
[email protected]aa7e4ef2014-05-28 03:53:15393 QuicUnackedPacketMap::const_iterator it,
[email protected]77b5d50b2014-05-07 22:48:48394 QuicTime::Delta delta_largest_observed) {
[email protected]aa7e4ef2014-05-28 03:53:15395 LOG_IF(DFATAL, it == unacked_packets_.end())
396 << "MarkPacketHandled must be passed a valid iterator entry.";
397 const QuicPacketSequenceNumber sequence_number = it->first;
398 const TransmissionInfo& transmission_info = it->second;
[email protected]c0680202013-12-18 04:52:34399
[email protected]66ae5962014-05-22 11:13:05400 QuicPacketSequenceNumber newest_transmission =
401 *transmission_info.all_transmissions->rbegin();
[email protected]6d9ca3b2014-05-13 07:44:22402 // Remove the most recent packet, if it is pending retransmission.
403 pending_retransmissions_.erase(newest_transmission);
404
[email protected]730b35d72014-06-05 03:23:22405 // Notify observers about the ACKed packet.
406 {
407 // The AckNotifierManager needs to be notified about the most recent
408 // transmission, since that's the one only one it tracks.
409 ack_notifier_manager_.OnPacketAcked(newest_transmission,
410 delta_largest_observed);
411 if (newest_transmission != sequence_number) {
412 RecordSpuriousRetransmissions(*transmission_info.all_transmissions,
413 sequence_number);
414 }
415 }
416
[email protected]6d9ca3b2014-05-13 07:44:22417 // Two cases for MarkPacketHandled:
418 // 1) Handle the most recent or a crypto packet, so remove all transmissions.
419 // 2) Handle old transmission, keep all other pending transmissions,
420 // but disassociate them from one another.
[email protected]b9475b582014-03-20 20:04:33421
[email protected]66ae5962014-05-22 11:13:05422 // If it's a crypto handshake packet, discard it and all retransmissions,
423 // since they won't be acked now that one has been processed.
[email protected]6d9ca3b2014-05-13 07:44:22424 // TODO(ianswett): Instead of handling all crypto packets in a special way,
425 // only handle NULL encrypted packets in a special way.
[email protected]66ae5962014-05-22 11:13:05426 if (HasCryptoHandshake(
427 unacked_packets_.GetTransmissionInfo(newest_transmission))) {
[email protected]aa7e4ef2014-05-28 03:53:15428 unacked_packets_.RemoveFromInFlight(newest_transmission);
[email protected]c67a82cb2013-09-24 02:53:21429 }
[email protected]aa7e4ef2014-05-28 03:53:15430 unacked_packets_.RemoveFromInFlight(sequence_number);
431 unacked_packets_.RemoveRetransmittability(sequence_number);
[email protected]7ad98f42014-01-08 07:11:30432
[email protected]cb23a922014-02-20 17:42:38433 QuicUnackedPacketMap::const_iterator next_unacked = unacked_packets_.begin();
[email protected]c67a82cb2013-09-24 02:53:21434 while (next_unacked != unacked_packets_.end() &&
[email protected]66ae5962014-05-22 11:13:05435 next_unacked->first <= sequence_number) {
[email protected]41fb6372013-12-10 05:26:40436 ++next_unacked;
437 }
[email protected]c67a82cb2013-09-24 02:53:21438 return next_unacked;
[email protected]4d1789c32013-09-18 23:54:36439}
440
[email protected]4d1789c32013-09-18 23:54:36441bool QuicSentPacketManager::IsUnacked(
442 QuicPacketSequenceNumber sequence_number) const {
[email protected]cb23a922014-02-20 17:42:38443 return unacked_packets_.IsUnacked(sequence_number);
[email protected]4d1789c32013-09-18 23:54:36444}
445
[email protected]4d1789c32013-09-18 23:54:36446bool QuicSentPacketManager::HasUnackedPackets() const {
[email protected]cb23a922014-02-20 17:42:38447 return unacked_packets_.HasUnackedPackets();
[email protected]4d1789c32013-09-18 23:54:36448}
449
450QuicPacketSequenceNumber
451QuicSentPacketManager::GetLeastUnackedSentPacket() const {
[email protected]cb23a922014-02-20 17:42:38452 return unacked_packets_.GetLeastUnackedSentPacket();
[email protected]4d1789c32013-09-18 23:54:36453}
454
[email protected]2115d33ba2014-01-02 21:53:52455bool QuicSentPacketManager::OnPacketSent(
[email protected]2adef90e2013-12-02 20:26:57456 QuicPacketSequenceNumber sequence_number,
457 QuicTime sent_time,
458 QuicByteCount bytes,
459 TransmissionType transmission_type,
460 HasRetransmittableData has_retransmittable_data) {
461 DCHECK_LT(0u, sequence_number);
[email protected]cb23a922014-02-20 17:42:38462 LOG_IF(DFATAL, bytes == 0) << "Cannot send empty packets.";
[email protected]9cda5fd2014-06-03 10:20:28463 pending_tlp_transmission_ = false;
[email protected]9f0dcd4e2014-01-16 15:58:14464 // In rare circumstances, the packet could be serialized, sent, and then acked
465 // before OnPacketSent is called.
[email protected]cb23a922014-02-20 17:42:38466 if (!unacked_packets_.IsUnacked(sequence_number)) {
[email protected]9f0dcd4e2014-01-16 15:58:14467 return false;
468 }
[email protected]2adef90e2013-12-02 20:26:57469
[email protected]ce7bb1412014-05-17 15:51:33470 if (unacked_packets_.bytes_in_flight() == 0) {
471 // TODO(ianswett): Consider being less aggressive to force a new
472 // recent_min_rtt, likely by not discarding a relatively new sample.
473 DVLOG(1) << "Sampling a new recent min rtt within 2 samples. currently:"
474 << rtt_stats_.recent_min_rtt().ToMilliseconds() << "ms";
475 rtt_stats_.SampleNewRecentMinRtt(kNumMinRttSamplesAfterQuiescence);
[email protected]2adef90e2013-12-02 20:26:57476 }
[email protected]c0680202013-12-18 04:52:34477
[email protected]aa7e4ef2014-05-28 03:53:15478 // Only track packets as in flight that the send algorithm wants us to track.
479 const bool in_flight =
[email protected]ce7bb1412014-05-17 15:51:33480 send_algorithm_->OnPacketSent(sent_time,
481 unacked_packets_.bytes_in_flight(),
482 sequence_number,
483 bytes,
484 has_retransmittable_data);
[email protected]aa7e4ef2014-05-28 03:53:15485 unacked_packets_.SetSent(sequence_number, sent_time, bytes, in_flight);
[email protected]cb23a922014-02-20 17:42:38486
[email protected]ce7bb1412014-05-17 15:51:33487 // Reset the retransmission timer anytime a pending packet is sent.
[email protected]aa7e4ef2014-05-28 03:53:15488 return in_flight;
[email protected]2adef90e2013-12-02 20:26:57489}
490
491void QuicSentPacketManager::OnRetransmissionTimeout() {
[email protected]aa7e4ef2014-05-28 03:53:15492 DCHECK(unacked_packets_.HasInFlightPackets());
[email protected]d8bbef62014-06-12 08:02:50493 DCHECK(!pending_tlp_transmission_);
[email protected]3aa9ca72014-02-27 19:39:43494 // Handshake retransmission, timer based loss detection, TLP, and RTO are
495 // implemented with a single alarm. The handshake alarm is set when the
496 // handshake has not completed, the loss alarm is set when the loss detection
497 // algorithm says to, and the TLP and RTO alarms are set after that.
[email protected]066d8182014-01-04 02:02:45498 // The TLP alarm is always set to run for under an RTO.
499 switch (GetRetransmissionMode()) {
500 case HANDSHAKE_MODE:
[email protected]6ae6e342014-02-06 02:21:42501 ++stats_->crypto_retransmit_count;
[email protected]066d8182014-01-04 02:02:45502 RetransmitCryptoPackets();
503 return;
[email protected]77b5d50b2014-05-07 22:48:48504 case LOSS_MODE: {
[email protected]3aa9ca72014-02-27 19:39:43505 ++stats_->loss_timeout_count;
[email protected]77b5d50b2014-05-07 22:48:48506 QuicByteCount bytes_in_flight = unacked_packets_.bytes_in_flight();
[email protected]3aa9ca72014-02-27 19:39:43507 InvokeLossDetection(clock_->Now());
[email protected]77b5d50b2014-05-07 22:48:48508 MaybeInvokeCongestionEvent(false, bytes_in_flight);
[email protected]3aa9ca72014-02-27 19:39:43509 return;
[email protected]77b5d50b2014-05-07 22:48:48510 }
[email protected]066d8182014-01-04 02:02:45511 case TLP_MODE:
512 // If no tail loss probe can be sent, because there are no retransmittable
513 // packets, execute a conventional RTO to abandon old packets.
[email protected]6ae6e342014-02-06 02:21:42514 ++stats_->tlp_count;
[email protected]d8bbef62014-06-12 08:02:50515 ++consecutive_tlp_count_;
[email protected]9cda5fd2014-06-03 10:20:28516 pending_tlp_transmission_ = true;
[email protected]d8bbef62014-06-12 08:02:50517 // TLPs prefer sending new data instead of retransmitting data, so
518 // give the connection a chance to write before completing the TLP.
[email protected]066d8182014-01-04 02:02:45519 return;
520 case RTO_MODE:
[email protected]6ae6e342014-02-06 02:21:42521 ++stats_->rto_count;
[email protected]066d8182014-01-04 02:02:45522 RetransmitAllPackets();
523 return;
524 }
525}
526
527void QuicSentPacketManager::RetransmitCryptoPackets() {
528 DCHECK_EQ(HANDSHAKE_MODE, GetRetransmissionMode());
529 // TODO(ianswett): Typical TCP implementations only retransmit 5 times.
530 consecutive_crypto_retransmission_count_ =
531 min(kMaxHandshakeRetransmissionBackoffs,
532 consecutive_crypto_retransmission_count_ + 1);
533 bool packet_retransmitted = false;
[email protected]cb23a922014-02-20 17:42:38534 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
[email protected]14a95752014-01-08 19:01:52535 it != unacked_packets_.end(); ++it) {
536 QuicPacketSequenceNumber sequence_number = it->first;
537 const RetransmittableFrames* frames = it->second.retransmittable_frames;
[email protected]aa7e4ef2014-05-28 03:53:15538 // Only retransmit frames which are in flight, and therefore have been sent.
539 if (!it->second.in_flight || frames == NULL ||
[email protected]14a95752014-01-08 19:01:52540 frames->HasCryptoHandshake() != IS_HANDSHAKE) {
[email protected]066d8182014-01-04 02:02:45541 continue;
542 }
543 packet_retransmitted = true;
[email protected]b9475b582014-03-20 20:04:33544 MarkForRetransmission(sequence_number, HANDSHAKE_RETRANSMISSION);
[email protected]066d8182014-01-04 02:02:45545 }
546 DCHECK(packet_retransmitted) << "No crypto packets found to retransmit.";
547}
548
[email protected]d8bbef62014-06-12 08:02:50549bool QuicSentPacketManager::MaybeRetransmitTailLossProbe() {
550 if (!pending_tlp_transmission_) {
551 return false;
552 }
[email protected]cb23a922014-02-20 17:42:38553 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
[email protected]14a95752014-01-08 19:01:52554 it != unacked_packets_.end(); ++it) {
555 QuicPacketSequenceNumber sequence_number = it->first;
556 const RetransmittableFrames* frames = it->second.retransmittable_frames;
[email protected]aa7e4ef2014-05-28 03:53:15557 // Only retransmit frames which are in flight, and therefore have been sent.
558 if (!it->second.in_flight || frames == NULL) {
[email protected]066d8182014-01-04 02:02:45559 continue;
560 }
561 DCHECK_NE(IS_HANDSHAKE, frames->HasCryptoHandshake());
562 MarkForRetransmission(sequence_number, TLP_RETRANSMISSION);
[email protected]d8bbef62014-06-12 08:02:50563 return true;
[email protected]066d8182014-01-04 02:02:45564 }
565 DLOG(FATAL)
566 << "No retransmittable packets, so RetransmitOldestPacket failed.";
[email protected]d8bbef62014-06-12 08:02:50567 return false;
[email protected]066d8182014-01-04 02:02:45568}
569
570void QuicSentPacketManager::RetransmitAllPackets() {
[email protected]9cda5fd2014-06-03 10:20:28571 DVLOG(1) << "RetransmitAllPackets() called with "
[email protected]cb23a922014-02-20 17:42:38572 << unacked_packets_.GetNumUnackedPackets() << " unacked packets.";
[email protected]066d8182014-01-04 02:02:45573 // Request retransmission of all retransmittable packets when the RTO
574 // fires, and let the congestion manager decide how many to send
575 // immediately and the remaining packets will be queued.
576 // Abandon any non-retransmittable packets that are sufficiently old.
[email protected]457d6952013-12-13 09:24:58577 bool packets_retransmitted = false;
[email protected]aa7e4ef2014-05-28 03:53:15578 QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
579 while (it != unacked_packets_.end()) {
580 const RetransmittableFrames* frames = it->second.retransmittable_frames;
581 QuicPacketSequenceNumber sequence_number = it->first;
582 ++it;
583 if (frames != NULL) {
[email protected]457d6952013-12-13 09:24:58584 packets_retransmitted = true;
[email protected]aa7e4ef2014-05-28 03:53:15585 MarkForRetransmission(sequence_number, RTO_RETRANSMISSION);
[email protected]6d9ca3b2014-05-13 07:44:22586 } else {
[email protected]aa7e4ef2014-05-28 03:53:15587 unacked_packets_.RemoveFromInFlight(sequence_number);
[email protected]41fb6372013-12-10 05:26:40588 }
589 }
[email protected]2adef90e2013-12-02 20:26:57590
[email protected]7ad98f42014-01-08 07:11:30591 send_algorithm_->OnRetransmissionTimeout(packets_retransmitted);
[email protected]457d6952013-12-13 09:24:58592 if (packets_retransmitted) {
[email protected]cc1aa272014-06-30 19:48:22593 if (consecutive_rto_count_ == 0) {
594 first_rto_transmission_ = unacked_packets_.largest_sent_packet() + 1;
595 }
[email protected]457d6952013-12-13 09:24:58596 ++consecutive_rto_count_;
[email protected]41fb6372013-12-10 05:26:40597 }
[email protected]2adef90e2013-12-02 20:26:57598}
599
[email protected]066d8182014-01-04 02:02:45600QuicSentPacketManager::RetransmissionTimeoutMode
601 QuicSentPacketManager::GetRetransmissionMode() const {
[email protected]aa7e4ef2014-05-28 03:53:15602 DCHECK(unacked_packets_.HasInFlightPackets());
[email protected]ffc34bf2014-03-07 02:42:02603 if (unacked_packets_.HasPendingCryptoPackets()) {
[email protected]066d8182014-01-04 02:02:45604 return HANDSHAKE_MODE;
605 }
[email protected]3aa9ca72014-02-27 19:39:43606 if (loss_algorithm_->GetLossTimeout() != QuicTime::Zero()) {
607 return LOSS_MODE;
608 }
[email protected]066d8182014-01-04 02:02:45609 if (consecutive_tlp_count_ < max_tail_loss_probes_) {
[email protected]cb23a922014-02-20 17:42:38610 if (unacked_packets_.HasUnackedRetransmittableFrames()) {
611 return TLP_MODE;
[email protected]066d8182014-01-04 02:02:45612 }
613 }
614 return RTO_MODE;
615}
616
[email protected]2adef90e2013-12-02 20:26:57617void QuicSentPacketManager::OnIncomingQuicCongestionFeedbackFrame(
[email protected]41fb6372013-12-10 05:26:40618 const QuicCongestionFeedbackFrame& frame,
619 const QuicTime& feedback_receive_time) {
[email protected]2adef90e2013-12-02 20:26:57620 send_algorithm_->OnIncomingQuicCongestionFeedbackFrame(
[email protected]cb23a922014-02-20 17:42:38621 frame, feedback_receive_time);
[email protected]2adef90e2013-12-02 20:26:57622}
623
[email protected]93dd91f2014-02-27 00:09:03624void QuicSentPacketManager::InvokeLossDetection(QuicTime time) {
[email protected]cb23a922014-02-20 17:42:38625 SequenceNumberSet lost_packets =
[email protected]93dd91f2014-02-27 00:09:03626 loss_algorithm_->DetectLostPackets(unacked_packets_,
627 time,
628 largest_observed_,
[email protected]ffc34bf2014-03-07 02:42:02629 rtt_stats_);
[email protected]cb23a922014-02-20 17:42:38630 for (SequenceNumberSet::const_iterator it = lost_packets.begin();
631 it != lost_packets.end(); ++it) {
632 QuicPacketSequenceNumber sequence_number = *it;
[email protected]77b5d50b2014-05-07 22:48:48633 const TransmissionInfo& transmission_info =
634 unacked_packets_.GetTransmissionInfo(sequence_number);
[email protected]cb23a922014-02-20 17:42:38635 // TODO(ianswett): If it's expected the FEC packet may repair the loss, it
636 // should be recorded as a loss to the send algorithm, but not retransmitted
637 // until it's known whether the FEC packet arrived.
638 ++stats_->packets_lost;
[email protected]77b5d50b2014-05-07 22:48:48639 packets_lost_[sequence_number] = transmission_info;
[email protected]6d9ca3b2014-05-13 07:44:22640 DVLOG(1) << ENDPOINT << "Lost packet " << sequence_number;
[email protected]cb23a922014-02-20 17:42:38641
[email protected]77b5d50b2014-05-07 22:48:48642 if (transmission_info.retransmittable_frames != NULL) {
[email protected]b9475b582014-03-20 20:04:33643 MarkForRetransmission(sequence_number, LOSS_RETRANSMISSION);
[email protected]cb23a922014-02-20 17:42:38644 } else {
645 // Since we will not retransmit this, we need to remove it from
646 // unacked_packets_. This is either the current transmission of
[email protected]6d9ca3b2014-05-13 07:44:22647 // a packet whose previous transmission has been acked, a packet that has
648 // been TLP retransmitted, or an FEC packet.
[email protected]aa7e4ef2014-05-28 03:53:15649 unacked_packets_.RemoveFromInFlight(sequence_number);
[email protected]cb23a922014-02-20 17:42:38650 }
651 }
652}
653
[email protected]77b5d50b2014-05-07 22:48:48654bool QuicSentPacketManager::MaybeUpdateRTT(
[email protected]41fb6372013-12-10 05:26:40655 const ReceivedPacketInfo& received_info,
656 const QuicTime& ack_receive_time) {
[email protected]cb23a922014-02-20 17:42:38657 if (!unacked_packets_.IsUnacked(received_info.largest_observed)) {
[email protected]77b5d50b2014-05-07 22:48:48658 return false;
[email protected]41fb6372013-12-10 05:26:40659 }
[email protected]cb23a922014-02-20 17:42:38660 // We calculate the RTT based on the highest ACKed sequence number, the lower
661 // sequence numbers will include the ACK aggregation delay.
[email protected]77b5d50b2014-05-07 22:48:48662 const TransmissionInfo& transmission_info =
[email protected]cb23a922014-02-20 17:42:38663 unacked_packets_.GetTransmissionInfo(received_info.largest_observed);
[email protected]9f0dcd4e2014-01-16 15:58:14664 // Don't update the RTT if it hasn't been sent.
[email protected]cb23a922014-02-20 17:42:38665 if (transmission_info.sent_time == QuicTime::Zero()) {
[email protected]77b5d50b2014-05-07 22:48:48666 return false;
[email protected]9f0dcd4e2014-01-16 15:58:14667 }
[email protected]41fb6372013-12-10 05:26:40668
[email protected]14a95752014-01-08 19:01:52669 QuicTime::Delta send_delta =
[email protected]cb23a922014-02-20 17:42:38670 ack_receive_time.Subtract(transmission_info.sent_time);
[email protected]82168ee22014-04-30 22:25:48671 rtt_stats_.UpdateRtt(
672 send_delta, received_info.delta_time_largest_observed, ack_receive_time);
[email protected]77b5d50b2014-05-07 22:48:48673 return true;
[email protected]41fb6372013-12-10 05:26:40674}
675
[email protected]2adef90e2013-12-02 20:26:57676QuicTime::Delta QuicSentPacketManager::TimeUntilSend(
677 QuicTime now,
[email protected]9bb57c72014-03-31 20:36:04678 HasRetransmittableData retransmittable) {
679 // The TLP logic is entirely contained within QuicSentPacketManager, so the
680 // send algorithm does not need to be consulted.
[email protected]9cda5fd2014-06-03 10:20:28681 if (pending_tlp_transmission_) {
[email protected]9bb57c72014-03-31 20:36:04682 return QuicTime::Delta::Zero();
683 }
[email protected]77b5d50b2014-05-07 22:48:48684 return send_algorithm_->TimeUntilSend(
685 now, unacked_packets_.bytes_in_flight(), retransmittable);
[email protected]2adef90e2013-12-02 20:26:57686}
687
[email protected]2adef90e2013-12-02 20:26:57688// Ensures that the Delayed Ack timer is always set to a value lesser
689// than the retransmission timer's minimum value (MinRTO). We want the
690// delayed ack to get back to the QUIC peer before the sender's
691// retransmission timer triggers. Since we do not know the
692// reverse-path one-way delay, we assume equal delays for forward and
693// reverse paths, and ensure that the timer is set to less than half
694// of the MinRTO.
695// There may be a value in making this delay adaptive with the help of
696// the sender and a signaling mechanism -- if the sender uses a
697// different MinRTO, we may get spurious retransmissions. May not have
698// any benefits, but if the delayed ack becomes a significant source
699// of (likely, tail) latency, then consider such a mechanism.
[email protected]2115d33ba2014-01-02 21:53:52700const QuicTime::Delta QuicSentPacketManager::DelayedAckTime() const {
[email protected]2adef90e2013-12-02 20:26:57701 return QuicTime::Delta::FromMilliseconds(kMinRetransmissionTimeMs/2);
702}
703
[email protected]c0680202013-12-18 04:52:34704const QuicTime QuicSentPacketManager::GetRetransmissionTime() const {
[email protected]d8bbef62014-06-12 08:02:50705 // Don't set the timer if there are no packets in flight or we've already
706 // queued a tlp transmission and it hasn't been sent yet.
707 if (!unacked_packets_.HasInFlightPackets() || pending_tlp_transmission_) {
[email protected]c0680202013-12-18 04:52:34708 return QuicTime::Zero();
709 }
[email protected]066d8182014-01-04 02:02:45710 switch (GetRetransmissionMode()) {
711 case HANDSHAKE_MODE:
712 return clock_->ApproximateNow().Add(GetCryptoRetransmissionDelay());
[email protected]3aa9ca72014-02-27 19:39:43713 case LOSS_MODE:
714 return loss_algorithm_->GetLossTimeout();
[email protected]066d8182014-01-04 02:02:45715 case TLP_MODE: {
716 // TODO(ianswett): When CWND is available, it would be preferable to
717 // set the timer based on the earliest retransmittable packet.
718 // Base the updated timer on the send time of the last packet.
[email protected]cb23a922014-02-20 17:42:38719 const QuicTime sent_time = unacked_packets_.GetLastPacketSentTime();
[email protected]066d8182014-01-04 02:02:45720 const QuicTime tlp_time = sent_time.Add(GetTailLossProbeDelay());
[email protected]6d9ca3b2014-05-13 07:44:22721 // Ensure the TLP timer never gets set to a time in the past.
[email protected]066d8182014-01-04 02:02:45722 return QuicTime::Max(clock_->ApproximateNow(), tlp_time);
723 }
724 case RTO_MODE: {
[email protected]aa7e4ef2014-05-28 03:53:15725 // The RTO is based on the first outstanding packet.
[email protected]cb23a922014-02-20 17:42:38726 const QuicTime sent_time =
[email protected]aa7e4ef2014-05-28 03:53:15727 unacked_packets_.GetFirstInFlightPacketSentTime();
[email protected]cc1aa272014-06-30 19:48:22728 QuicTime rto_time = sent_time.Add(GetRetransmissionDelay());
729 // Wait for TLP packets to be acked before an RTO fires.
730 QuicTime tlp_time =
731 unacked_packets_.GetLastPacketSentTime().Add(GetTailLossProbeDelay());
732 return QuicTime::Max(tlp_time, rto_time);
[email protected]066d8182014-01-04 02:02:45733 }
[email protected]066d8182014-01-04 02:02:45734 }
[email protected]8a15a6c82014-01-09 13:04:46735 DCHECK(false);
[email protected]066d8182014-01-04 02:02:45736 return QuicTime::Zero();
737}
738
[email protected]7ad98f42014-01-08 07:11:30739const QuicTime::Delta QuicSentPacketManager::GetCryptoRetransmissionDelay()
740 const {
[email protected]066d8182014-01-04 02:02:45741 // This is equivalent to the TailLossProbeDelay, but slightly more aggressive
742 // because crypto handshake messages don't incur a delayed ack time.
743 int64 delay_ms = max<int64>(kMinHandshakeTimeoutMs,
[email protected]fb35b0a2014-04-15 21:06:49744 1.5 * rtt_stats_.SmoothedRtt().ToMilliseconds());
[email protected]066d8182014-01-04 02:02:45745 return QuicTime::Delta::FromMilliseconds(
746 delay_ms << consecutive_crypto_retransmission_count_);
747}
748
749const QuicTime::Delta QuicSentPacketManager::GetTailLossProbeDelay() const {
[email protected]fb35b0a2014-04-15 21:06:49750 QuicTime::Delta srtt = rtt_stats_.SmoothedRtt();
[email protected]aa7e4ef2014-05-28 03:53:15751 if (!unacked_packets_.HasMultipleInFlightPackets()) {
[email protected]066d8182014-01-04 02:02:45752 return QuicTime::Delta::Max(
753 srtt.Multiply(1.5).Add(DelayedAckTime()), srtt.Multiply(2));
754 }
755 return QuicTime::Delta::FromMilliseconds(
756 max(kMinTailLossProbeTimeoutMs,
757 static_cast<int64>(2 * srtt.ToMilliseconds())));
[email protected]c0680202013-12-18 04:52:34758}
759
[email protected]2adef90e2013-12-02 20:26:57760const QuicTime::Delta QuicSentPacketManager::GetRetransmissionDelay() const {
[email protected]2adef90e2013-12-02 20:26:57761 QuicTime::Delta retransmission_delay = send_algorithm_->RetransmissionDelay();
[email protected]6ae6e342014-02-06 02:21:42762 // TODO(rch): This code should move to |send_algorithm_|.
[email protected]2adef90e2013-12-02 20:26:57763 if (retransmission_delay.IsZero()) {
764 // We are in the initial state, use default timeout values.
765 retransmission_delay =
766 QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs);
[email protected]6ae6e342014-02-06 02:21:42767 } else if (retransmission_delay.ToMilliseconds() < kMinRetransmissionTimeMs) {
768 retransmission_delay =
769 QuicTime::Delta::FromMilliseconds(kMinRetransmissionTimeMs);
[email protected]2adef90e2013-12-02 20:26:57770 }
[email protected]6ae6e342014-02-06 02:21:42771
[email protected]2adef90e2013-12-02 20:26:57772 // Calculate exponential back off.
[email protected]066d8182014-01-04 02:02:45773 retransmission_delay = retransmission_delay.Multiply(
[email protected]7ad98f42014-01-08 07:11:30774 1 << min<size_t>(consecutive_rto_count_, kMaxRetransmissions));
[email protected]2adef90e2013-12-02 20:26:57775
[email protected]2adef90e2013-12-02 20:26:57776 if (retransmission_delay.ToMilliseconds() > kMaxRetransmissionTimeMs) {
777 return QuicTime::Delta::FromMilliseconds(kMaxRetransmissionTimeMs);
778 }
779 return retransmission_delay;
780}
781
[email protected]fb35b0a2014-04-15 21:06:49782const RttStats* QuicSentPacketManager::GetRttStats() const {
783 return &rtt_stats_;
[email protected]2adef90e2013-12-02 20:26:57784}
785
786QuicBandwidth QuicSentPacketManager::BandwidthEstimate() const {
787 return send_algorithm_->BandwidthEstimate();
788}
789
790QuicByteCount QuicSentPacketManager::GetCongestionWindow() const {
791 return send_algorithm_->GetCongestionWindow();
792}
793
[email protected]2adef90e2013-12-02 20:26:57794void QuicSentPacketManager::MaybeEnablePacing() {
795 if (!FLAGS_enable_quic_pacing) {
796 return;
797 }
798
799 if (using_pacing_) {
800 return;
801 }
802
[email protected]9cda5fd2014-06-03 10:20:28803 // Set up a pacing sender with a 5 millisecond alarm granularity.
[email protected]2adef90e2013-12-02 20:26:57804 using_pacing_ = true;
805 send_algorithm_.reset(
806 new PacingSender(send_algorithm_.release(),
[email protected]9cda5fd2014-06-03 10:20:28807 QuicTime::Delta::FromMilliseconds(5)));
[email protected]2adef90e2013-12-02 20:26:57808}
809
[email protected]4d1789c32013-09-18 23:54:36810} // namespace net