1 // 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 7 #include <algorithm> 8 9 #include "base/logging.h" 10 #include "base/stl_util.h" 11 #include "net/quic/congestion_control/pacing_sender.h" 12 #include "net/quic/crypto/crypto_protocol.h" 13 #include "net/quic/quic_ack_notifier_manager.h" 14 #include "net/quic/quic_connection_stats.h" 15 #include "net/quic/quic_flags.h" 16 #include "net/quic/quic_utils_chromium.h" 17 18 using std::make_pair; 19 using std::max; 20 using std::min; 21 22 namespace net { 23 namespace { 24 static 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. 28 static const int kMinRetransmissionTimeMs = 200; 29 static const int kMaxRetransmissionTimeMs = 60000; 30 static const size_t kMaxRetransmissions = 10; 31 32 // Only exponentially back off the handshake timer 5 times due to a timeout. 33 static const size_t kMaxHandshakeRetransmissionBackoffs = 5; 34 static 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. 38 static const size_t kDefaultMaxTailLossProbes = 2; 39 static const int64 kMinTailLossProbeTimeoutMs = 10; 40 41 // Number of samples before we force a new recent min rtt to be captured. 42 static const size_t kNumMinRttSamplesAfterQuiescence = 2; 43 44 bool HasCryptoHandshake(const TransmissionInfo& transmission_info) { 45 if (transmission_info.retransmittable_frames == NULL) { 46 return false; 47 } 48 return transmission_info.retransmittable_frames->HasCryptoHandshake() == 49 IS_HANDSHAKE; 50 } 51 52 } // namespace 53 54 #define ENDPOINT (is_server_ ? "Server: " : " Client: ") 55 56 QuicSentPacketManager::QuicSentPacketManager(bool is_server, 57 const QuicClock* clock, 58 QuicConnectionStats* stats, 59 CongestionFeedbackType type, 60 LossDetectionType loss_type) 61 : unacked_packets_(), 62 is_server_(is_server), 63 clock_(clock), 64 stats_(stats), 65 debug_delegate_(NULL), 66 send_algorithm_( 67 SendAlgorithmInterface::Create(clock, &rtt_stats_, type, stats)), 68 loss_algorithm_(LossDetectionInterface::Create(loss_type)), 69 largest_observed_(0), 70 consecutive_rto_count_(0), 71 consecutive_tlp_count_(0), 72 consecutive_crypto_retransmission_count_(0), 73 pending_tlp_transmission_(false), 74 max_tail_loss_probes_(kDefaultMaxTailLossProbes), 75 using_pacing_(false) { 76 } 77 78 QuicSentPacketManager::~QuicSentPacketManager() { 79 } 80 81 void QuicSentPacketManager::SetFromConfig(const QuicConfig& config) { 82 if (config.HasReceivedInitialRoundTripTimeUs() && 83 config.ReceivedInitialRoundTripTimeUs() > 0) { 84 rtt_stats_.set_initial_rtt_us(min(kMaxInitialRoundTripTimeUs, 85 config.ReceivedInitialRoundTripTimeUs())); 86 } 87 // TODO(ianswett): BBR is currently a server only feature. 88 if (config.HasReceivedCongestionOptions() && 89 ContainsQuicTag(config.ReceivedCongestionOptions(), kTBBR)) { 90 send_algorithm_.reset( 91 SendAlgorithmInterface::Create(clock_, &rtt_stats_, kTCPBBR, stats_)); 92 } 93 if (config.congestion_feedback() == kPACE) { 94 MaybeEnablePacing(); 95 } 96 if (config.HasReceivedLossDetection() && 97 config.ReceivedLossDetection() == kTIME) { 98 loss_algorithm_.reset(LossDetectionInterface::Create(kTime)); 99 } 100 send_algorithm_->SetFromConfig(config, is_server_); 101 } 102 103 // TODO(ianswett): Combine this method with OnPacketSent once packets are always 104 // sent in order and the connection tracks RetransmittableFrames for longer. 105 void QuicSentPacketManager::OnSerializedPacket( 106 const SerializedPacket& serialized_packet) { 107 if (serialized_packet.retransmittable_frames) { 108 ack_notifier_manager_.OnSerializedPacket(serialized_packet); 109 } 110 111 unacked_packets_.AddPacket(serialized_packet); 112 } 113 114 void QuicSentPacketManager::OnRetransmittedPacket( 115 QuicPacketSequenceNumber old_sequence_number, 116 QuicPacketSequenceNumber new_sequence_number) { 117 TransmissionType transmission_type; 118 PendingRetransmissionMap::iterator it = 119 pending_retransmissions_.find(old_sequence_number); 120 if (it != pending_retransmissions_.end()) { 121 transmission_type = it->second; 122 pending_retransmissions_.erase(it); 123 } else { 124 DLOG(DFATAL) << "Expected sequence number to be in " 125 "pending_retransmissions_. sequence_number: " << old_sequence_number; 126 transmission_type = NOT_RETRANSMISSION; 127 } 128 129 // A notifier may be waiting to hear about ACKs for the original sequence 130 // number. Inform them that the sequence number has changed. 131 ack_notifier_manager_.UpdateSequenceNumber(old_sequence_number, 132 new_sequence_number); 133 134 unacked_packets_.OnRetransmittedPacket(old_sequence_number, 135 new_sequence_number, 136 transmission_type); 137 } 138 139 void QuicSentPacketManager::OnIncomingAck( 140 const ReceivedPacketInfo& received_info, 141 QuicTime ack_receive_time) { 142 QuicByteCount bytes_in_flight = unacked_packets_.bytes_in_flight(); 143 144 // We rely on delta_time_largest_observed to compute an RTT estimate, so 145 // we only update rtt when the largest observed gets acked. 146 bool largest_observed_acked = MaybeUpdateRTT(received_info, ack_receive_time); 147 if (largest_observed_ < received_info.largest_observed) { 148 largest_observed_ = received_info.largest_observed; 149 unacked_packets_.IncreaseLargestObserved(largest_observed_); 150 } 151 HandleAckForSentPackets(received_info); 152 InvokeLossDetection(ack_receive_time); 153 MaybeInvokeCongestionEvent(largest_observed_acked, bytes_in_flight); 154 155 // If we have received a truncated ack, then we need to clear out some 156 // previous transmissions to allow the peer to actually ACK new packets. 157 if (received_info.is_truncated) { 158 unacked_packets_.ClearPreviousRetransmissions( 159 received_info.missing_packets.size() / 2); 160 } 161 162 // Anytime we are making forward progress and have a new RTT estimate, reset 163 // the backoff counters. 164 if (largest_observed_acked) { 165 // Reset all retransmit counters any time a new packet is acked. 166 consecutive_rto_count_ = 0; 167 consecutive_tlp_count_ = 0; 168 consecutive_crypto_retransmission_count_ = 0; 169 } 170 } 171 172 void QuicSentPacketManager::MaybeInvokeCongestionEvent( 173 bool rtt_updated, QuicByteCount bytes_in_flight) { 174 if (rtt_updated || !packets_acked_.empty() || 175 !packets_lost_.empty()) { 176 send_algorithm_->OnCongestionEvent( 177 rtt_updated, bytes_in_flight, packets_acked_, packets_lost_); 178 packets_acked_.clear(); 179 packets_lost_.clear(); 180 } 181 } 182 183 void QuicSentPacketManager::HandleAckForSentPackets( 184 const ReceivedPacketInfo& received_info) { 185 // Go through the packets we have not received an ack for and see if this 186 // incoming_ack shows they've been seen by the peer. 187 QuicTime::Delta delta_largest_observed = 188 received_info.delta_time_largest_observed; 189 QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin(); 190 while (it != unacked_packets_.end()) { 191 QuicPacketSequenceNumber sequence_number = it->first; 192 if (sequence_number > received_info.largest_observed) { 193 // These packets are still in flight. 194 break; 195 } 196 197 if (IsAwaitingPacket(received_info, sequence_number)) { 198 // Consider it multiple nacks when there is a gap between the missing 199 // packet and the largest observed, since the purpose of a nack 200 // threshold is to tolerate re-ordering. This handles both StretchAcks 201 // and Forward Acks. 202 // The nack count only increases when the largest observed increases. 203 size_t min_nacks = received_info.largest_observed - sequence_number; 204 // Truncated acks can nack the largest observed, so use a min of 1. 205 if (min_nacks == 0) { 206 min_nacks = 1; 207 } 208 unacked_packets_.NackPacket(sequence_number, min_nacks); 209 ++it; 210 continue; 211 } 212 // Packet was acked, so remove it from our unacked packet list. 213 DVLOG(1) << ENDPOINT << "Got an ack for packet " << sequence_number; 214 // If data is associated with the most recent transmission of this 215 // packet, then inform the caller. 216 if (it->second.in_flight) { 217 packets_acked_[sequence_number] = it->second; 218 } 219 it = MarkPacketHandled(it, delta_largest_observed); 220 } 221 222 // Discard any retransmittable frames associated with revived packets. 223 for (SequenceNumberSet::const_iterator revived_it = 224 received_info.revived_packets.begin(); 225 revived_it != received_info.revived_packets.end(); ++revived_it) { 226 MarkPacketRevived(*revived_it, delta_largest_observed); 227 } 228 } 229 230 bool QuicSentPacketManager::HasRetransmittableFrames( 231 QuicPacketSequenceNumber sequence_number) const { 232 return unacked_packets_.HasRetransmittableFrames(sequence_number); 233 } 234 235 void QuicSentPacketManager::RetransmitUnackedPackets( 236 RetransmissionType retransmission_type) { 237 QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin(); 238 while (it != unacked_packets_.end()) { 239 const RetransmittableFrames* frames = it->second.retransmittable_frames; 240 // TODO(ianswett): Consider adding a new retransmission type which removes 241 // all these old packets from unacked and retransmits them as new sequence 242 // numbers with no connection to the previous ones. 243 if (frames != NULL && (retransmission_type == ALL_PACKETS || 244 frames->encryption_level() == ENCRYPTION_INITIAL)) { 245 MarkForRetransmission(it->first, ALL_UNACKED_RETRANSMISSION); 246 } 247 ++it; 248 } 249 } 250 251 void QuicSentPacketManager::NeuterUnencryptedPackets() { 252 QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin(); 253 while (it != unacked_packets_.end()) { 254 const RetransmittableFrames* frames = it->second.retransmittable_frames; 255 QuicPacketSequenceNumber sequence_number = it->first; 256 ++it; 257 if (frames != NULL && frames->encryption_level() == ENCRYPTION_NONE) { 258 // Once you're forward secure, no unencrypted packets will be sent, crypto 259 // or otherwise. Unencrypted packets are neutered and abandoned, to ensure 260 // they are not retransmitted or considered lost from a congestion control 261 // perspective. 262 pending_retransmissions_.erase(sequence_number); 263 unacked_packets_.RemoveFromInFlight(sequence_number); 264 // RemoveRetransmittibility is safe because only the newest sequence 265 // number can have frames. 266 unacked_packets_.RemoveRetransmittability(sequence_number); 267 } 268 } 269 } 270 271 void QuicSentPacketManager::MarkForRetransmission( 272 QuicPacketSequenceNumber sequence_number, 273 TransmissionType transmission_type) { 274 const TransmissionInfo& transmission_info = 275 unacked_packets_.GetTransmissionInfo(sequence_number); 276 LOG_IF(DFATAL, transmission_info.retransmittable_frames == NULL); 277 if (transmission_type != TLP_RETRANSMISSION) { 278 unacked_packets_.RemoveFromInFlight(sequence_number); 279 } 280 // TODO(ianswett): Currently the RTO can fire while there are pending NACK 281 // retransmissions for the same data, which is not ideal. 282 if (ContainsKey(pending_retransmissions_, sequence_number)) { 283 return; 284 } 285 286 pending_retransmissions_[sequence_number] = transmission_type; 287 } 288 289 void QuicSentPacketManager::RecordSpuriousRetransmissions( 290 const SequenceNumberSet& all_transmissions, 291 QuicPacketSequenceNumber acked_sequence_number) { 292 for (SequenceNumberSet::const_iterator 293 it = all_transmissions.upper_bound(acked_sequence_number), 294 end = all_transmissions.end(); 295 it != end; 296 ++it) { 297 const TransmissionInfo& retransmit_info = 298 unacked_packets_.GetTransmissionInfo(*it); 299 300 stats_->bytes_spuriously_retransmitted += retransmit_info.bytes_sent; 301 ++stats_->packets_spuriously_retransmitted; 302 if (debug_delegate_ != NULL) { 303 debug_delegate_->OnSpuriousPacketRetransmition( 304 retransmit_info.transmission_type, 305 retransmit_info.bytes_sent); 306 } 307 } 308 } 309 310 bool QuicSentPacketManager::HasPendingRetransmissions() const { 311 return !pending_retransmissions_.empty(); 312 } 313 314 QuicSentPacketManager::PendingRetransmission 315 QuicSentPacketManager::NextPendingRetransmission() { 316 DCHECK(!pending_retransmissions_.empty()); 317 QuicPacketSequenceNumber sequence_number = 318 pending_retransmissions_.begin()->first; 319 TransmissionType transmission_type = pending_retransmissions_.begin()->second; 320 if (unacked_packets_.HasPendingCryptoPackets()) { 321 // Ensure crypto packets are retransmitted before other packets. 322 PendingRetransmissionMap::const_iterator it = 323 pending_retransmissions_.begin(); 324 do { 325 if (HasCryptoHandshake(unacked_packets_.GetTransmissionInfo(it->first))) { 326 sequence_number = it->first; 327 transmission_type = it->second; 328 break; 329 } 330 ++it; 331 } while (it != pending_retransmissions_.end()); 332 } 333 DCHECK(unacked_packets_.IsUnacked(sequence_number)) << sequence_number; 334 const TransmissionInfo& transmission_info = 335 unacked_packets_.GetTransmissionInfo(sequence_number); 336 DCHECK(transmission_info.retransmittable_frames); 337 338 return PendingRetransmission(sequence_number, 339 transmission_type, 340 *transmission_info.retransmittable_frames, 341 transmission_info.sequence_number_length); 342 } 343 344 void QuicSentPacketManager::MarkPacketRevived( 345 QuicPacketSequenceNumber sequence_number, 346 QuicTime::Delta delta_largest_observed) { 347 if (!unacked_packets_.IsUnacked(sequence_number)) { 348 return; 349 } 350 351 const TransmissionInfo& transmission_info = 352 unacked_packets_.GetTransmissionInfo(sequence_number); 353 QuicPacketSequenceNumber newest_transmission = 354 *transmission_info.all_transmissions->rbegin(); 355 // This packet has been revived at the receiver. If we were going to 356 // retransmit it, do not retransmit it anymore. 357 pending_retransmissions_.erase(newest_transmission); 358 359 // The AckNotifierManager needs to be notified for revived packets, 360 // since it indicates the packet arrived from the appliction's perspective. 361 if (transmission_info.retransmittable_frames) { 362 ack_notifier_manager_.OnPacketAcked( 363 newest_transmission, delta_largest_observed); 364 } 365 366 unacked_packets_.RemoveRetransmittability(sequence_number); 367 } 368 369 QuicUnackedPacketMap::const_iterator QuicSentPacketManager::MarkPacketHandled( 370 QuicUnackedPacketMap::const_iterator it, 371 QuicTime::Delta delta_largest_observed) { 372 LOG_IF(DFATAL, it == unacked_packets_.end()) 373 << "MarkPacketHandled must be passed a valid iterator entry."; 374 const QuicPacketSequenceNumber sequence_number = it->first; 375 const TransmissionInfo& transmission_info = it->second; 376 377 QuicPacketSequenceNumber newest_transmission = 378 *transmission_info.all_transmissions->rbegin(); 379 // Remove the most recent packet, if it is pending retransmission. 380 pending_retransmissions_.erase(newest_transmission); 381 382 // Notify observers about the ACKed packet. 383 { 384 // The AckNotifierManager needs to be notified about the most recent 385 // transmission, since that's the one only one it tracks. 386 ack_notifier_manager_.OnPacketAcked(newest_transmission, 387 delta_largest_observed); 388 if (newest_transmission != sequence_number) { 389 RecordSpuriousRetransmissions(*transmission_info.all_transmissions, 390 sequence_number); 391 } 392 } 393 394 // Two cases for MarkPacketHandled: 395 // 1) Handle the most recent or a crypto packet, so remove all transmissions. 396 // 2) Handle old transmission, keep all other pending transmissions, 397 // but disassociate them from one another. 398 399 // If it's a crypto handshake packet, discard it and all retransmissions, 400 // since they won't be acked now that one has been processed. 401 // TODO(ianswett): Instead of handling all crypto packets in a special way, 402 // only handle NULL encrypted packets in a special way. 403 if (HasCryptoHandshake( 404 unacked_packets_.GetTransmissionInfo(newest_transmission))) { 405 unacked_packets_.RemoveFromInFlight(newest_transmission); 406 } 407 unacked_packets_.RemoveFromInFlight(sequence_number); 408 unacked_packets_.RemoveRetransmittability(sequence_number); 409 410 QuicUnackedPacketMap::const_iterator next_unacked = unacked_packets_.begin(); 411 while (next_unacked != unacked_packets_.end() && 412 next_unacked->first <= sequence_number) { 413 ++next_unacked; 414 } 415 return next_unacked; 416 } 417 418 bool QuicSentPacketManager::IsUnacked( 419 QuicPacketSequenceNumber sequence_number) const { 420 return unacked_packets_.IsUnacked(sequence_number); 421 } 422 423 bool QuicSentPacketManager::HasUnackedPackets() const { 424 return unacked_packets_.HasUnackedPackets(); 425 } 426 427 QuicPacketSequenceNumber 428 QuicSentPacketManager::GetLeastUnackedSentPacket() const { 429 return unacked_packets_.GetLeastUnackedSentPacket(); 430 } 431 432 bool QuicSentPacketManager::OnPacketSent( 433 QuicPacketSequenceNumber sequence_number, 434 QuicTime sent_time, 435 QuicByteCount bytes, 436 TransmissionType transmission_type, 437 HasRetransmittableData has_retransmittable_data) { 438 DCHECK_LT(0u, sequence_number); 439 LOG_IF(DFATAL, bytes == 0) << "Cannot send empty packets."; 440 pending_tlp_transmission_ = false; 441 // In rare circumstances, the packet could be serialized, sent, and then acked 442 // before OnPacketSent is called. 443 if (!unacked_packets_.IsUnacked(sequence_number)) { 444 return false; 445 } 446 447 if (unacked_packets_.bytes_in_flight() == 0) { 448 // TODO(ianswett): Consider being less aggressive to force a new 449 // recent_min_rtt, likely by not discarding a relatively new sample. 450 DVLOG(1) << "Sampling a new recent min rtt within 2 samples. currently:" 451 << rtt_stats_.recent_min_rtt().ToMilliseconds() << "ms"; 452 rtt_stats_.SampleNewRecentMinRtt(kNumMinRttSamplesAfterQuiescence); 453 } 454 455 // Only track packets as in flight that the send algorithm wants us to track. 456 const bool in_flight = 457 send_algorithm_->OnPacketSent(sent_time, 458 unacked_packets_.bytes_in_flight(), 459 sequence_number, 460 bytes, 461 has_retransmittable_data); 462 unacked_packets_.SetSent(sequence_number, sent_time, bytes, in_flight); 463 464 // Reset the retransmission timer anytime a pending packet is sent. 465 return in_flight; 466 } 467 468 void QuicSentPacketManager::OnRetransmissionTimeout() { 469 DCHECK(unacked_packets_.HasInFlightPackets()); 470 DCHECK(!pending_tlp_transmission_); 471 // Handshake retransmission, timer based loss detection, TLP, and RTO are 472 // implemented with a single alarm. The handshake alarm is set when the 473 // handshake has not completed, the loss alarm is set when the loss detection 474 // algorithm says to, and the TLP and RTO alarms are set after that. 475 // The TLP alarm is always set to run for under an RTO. 476 switch (GetRetransmissionMode()) { 477 case HANDSHAKE_MODE: 478 ++stats_->crypto_retransmit_count; 479 RetransmitCryptoPackets(); 480 return; 481 case LOSS_MODE: { 482 ++stats_->loss_timeout_count; 483 QuicByteCount bytes_in_flight = unacked_packets_.bytes_in_flight(); 484 InvokeLossDetection(clock_->Now()); 485 MaybeInvokeCongestionEvent(false, bytes_in_flight); 486 return; 487 } 488 case TLP_MODE: 489 // If no tail loss probe can be sent, because there are no retransmittable 490 // packets, execute a conventional RTO to abandon old packets. 491 ++stats_->tlp_count; 492 ++consecutive_tlp_count_; 493 pending_tlp_transmission_ = true; 494 // TLPs prefer sending new data instead of retransmitting data, so 495 // give the connection a chance to write before completing the TLP. 496 return; 497 case RTO_MODE: 498 ++stats_->rto_count; 499 RetransmitAllPackets(); 500 return; 501 } 502 } 503 504 void QuicSentPacketManager::RetransmitCryptoPackets() { 505 DCHECK_EQ(HANDSHAKE_MODE, GetRetransmissionMode()); 506 // TODO(ianswett): Typical TCP implementations only retransmit 5 times. 507 consecutive_crypto_retransmission_count_ = 508 min(kMaxHandshakeRetransmissionBackoffs, 509 consecutive_crypto_retransmission_count_ + 1); 510 bool packet_retransmitted = false; 511 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin(); 512 it != unacked_packets_.end(); ++it) { 513 QuicPacketSequenceNumber sequence_number = it->first; 514 const RetransmittableFrames* frames = it->second.retransmittable_frames; 515 // Only retransmit frames which are in flight, and therefore have been sent. 516 if (!it->second.in_flight || frames == NULL || 517 frames->HasCryptoHandshake() != IS_HANDSHAKE) { 518 continue; 519 } 520 packet_retransmitted = true; 521 MarkForRetransmission(sequence_number, HANDSHAKE_RETRANSMISSION); 522 } 523 DCHECK(packet_retransmitted) << "No crypto packets found to retransmit."; 524 } 525 526 bool QuicSentPacketManager::MaybeRetransmitTailLossProbe() { 527 if (!pending_tlp_transmission_) { 528 return false; 529 } 530 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin(); 531 it != unacked_packets_.end(); ++it) { 532 QuicPacketSequenceNumber sequence_number = it->first; 533 const RetransmittableFrames* frames = it->second.retransmittable_frames; 534 // Only retransmit frames which are in flight, and therefore have been sent. 535 if (!it->second.in_flight || frames == NULL) { 536 continue; 537 } 538 DCHECK_NE(IS_HANDSHAKE, frames->HasCryptoHandshake()); 539 MarkForRetransmission(sequence_number, TLP_RETRANSMISSION); 540 return true; 541 } 542 DLOG(FATAL) 543 << "No retransmittable packets, so RetransmitOldestPacket failed."; 544 return false; 545 } 546 547 void QuicSentPacketManager::RetransmitAllPackets() { 548 DVLOG(1) << "RetransmitAllPackets() called with " 549 << unacked_packets_.GetNumUnackedPackets() << " unacked packets."; 550 // Request retransmission of all retransmittable packets when the RTO 551 // fires, and let the congestion manager decide how many to send 552 // immediately and the remaining packets will be queued. 553 // Abandon any non-retransmittable packets that are sufficiently old. 554 bool packets_retransmitted = false; 555 QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin(); 556 while (it != unacked_packets_.end()) { 557 const RetransmittableFrames* frames = it->second.retransmittable_frames; 558 QuicPacketSequenceNumber sequence_number = it->first; 559 ++it; 560 if (frames != NULL) { 561 packets_retransmitted = true; 562 MarkForRetransmission(sequence_number, RTO_RETRANSMISSION); 563 } else { 564 unacked_packets_.RemoveFromInFlight(sequence_number); 565 } 566 } 567 568 send_algorithm_->OnRetransmissionTimeout(packets_retransmitted); 569 if (packets_retransmitted) { 570 ++consecutive_rto_count_; 571 } 572 } 573 574 QuicSentPacketManager::RetransmissionTimeoutMode 575 QuicSentPacketManager::GetRetransmissionMode() const { 576 DCHECK(unacked_packets_.HasInFlightPackets()); 577 if (unacked_packets_.HasPendingCryptoPackets()) { 578 return HANDSHAKE_MODE; 579 } 580 if (loss_algorithm_->GetLossTimeout() != QuicTime::Zero()) { 581 return LOSS_MODE; 582 } 583 if (consecutive_tlp_count_ < max_tail_loss_probes_) { 584 if (unacked_packets_.HasUnackedRetransmittableFrames()) { 585 return TLP_MODE; 586 } 587 } 588 return RTO_MODE; 589 } 590 591 void QuicSentPacketManager::OnIncomingQuicCongestionFeedbackFrame( 592 const QuicCongestionFeedbackFrame& frame, 593 const QuicTime& feedback_receive_time) { 594 send_algorithm_->OnIncomingQuicCongestionFeedbackFrame( 595 frame, feedback_receive_time); 596 } 597 598 void QuicSentPacketManager::InvokeLossDetection(QuicTime time) { 599 SequenceNumberSet lost_packets = 600 loss_algorithm_->DetectLostPackets(unacked_packets_, 601 time, 602 largest_observed_, 603 rtt_stats_); 604 for (SequenceNumberSet::const_iterator it = lost_packets.begin(); 605 it != lost_packets.end(); ++it) { 606 QuicPacketSequenceNumber sequence_number = *it; 607 const TransmissionInfo& transmission_info = 608 unacked_packets_.GetTransmissionInfo(sequence_number); 609 // TODO(ianswett): If it's expected the FEC packet may repair the loss, it 610 // should be recorded as a loss to the send algorithm, but not retransmitted 611 // until it's known whether the FEC packet arrived. 612 ++stats_->packets_lost; 613 packets_lost_[sequence_number] = transmission_info; 614 DVLOG(1) << ENDPOINT << "Lost packet " << sequence_number; 615 616 if (transmission_info.retransmittable_frames != NULL) { 617 MarkForRetransmission(sequence_number, LOSS_RETRANSMISSION); 618 } else { 619 // Since we will not retransmit this, we need to remove it from 620 // unacked_packets_. This is either the current transmission of 621 // a packet whose previous transmission has been acked, a packet that has 622 // been TLP retransmitted, or an FEC packet. 623 unacked_packets_.RemoveFromInFlight(sequence_number); 624 } 625 } 626 } 627 628 bool QuicSentPacketManager::MaybeUpdateRTT( 629 const ReceivedPacketInfo& received_info, 630 const QuicTime& ack_receive_time) { 631 if (!unacked_packets_.IsUnacked(received_info.largest_observed)) { 632 return false; 633 } 634 // We calculate the RTT based on the highest ACKed sequence number, the lower 635 // sequence numbers will include the ACK aggregation delay. 636 const TransmissionInfo& transmission_info = 637 unacked_packets_.GetTransmissionInfo(received_info.largest_observed); 638 // Don't update the RTT if it hasn't been sent. 639 if (transmission_info.sent_time == QuicTime::Zero()) { 640 return false; 641 } 642 643 QuicTime::Delta send_delta = 644 ack_receive_time.Subtract(transmission_info.sent_time); 645 rtt_stats_.UpdateRtt( 646 send_delta, received_info.delta_time_largest_observed, ack_receive_time); 647 return true; 648 } 649 650 QuicTime::Delta QuicSentPacketManager::TimeUntilSend( 651 QuicTime now, 652 HasRetransmittableData retransmittable) { 653 // The TLP logic is entirely contained within QuicSentPacketManager, so the 654 // send algorithm does not need to be consulted. 655 if (pending_tlp_transmission_) { 656 return QuicTime::Delta::Zero(); 657 } 658 return send_algorithm_->TimeUntilSend( 659 now, unacked_packets_.bytes_in_flight(), retransmittable); 660 } 661 662 // Ensures that the Delayed Ack timer is always set to a value lesser 663 // than the retransmission timer's minimum value (MinRTO). We want the 664 // delayed ack to get back to the QUIC peer before the sender's 665 // retransmission timer triggers. Since we do not know the 666 // reverse-path one-way delay, we assume equal delays for forward and 667 // reverse paths, and ensure that the timer is set to less than half 668 // of the MinRTO. 669 // There may be a value in making this delay adaptive with the help of 670 // the sender and a signaling mechanism -- if the sender uses a 671 // different MinRTO, we may get spurious retransmissions. May not have 672 // any benefits, but if the delayed ack becomes a significant source 673 // of (likely, tail) latency, then consider such a mechanism. 674 const QuicTime::Delta QuicSentPacketManager::DelayedAckTime() const { 675 return QuicTime::Delta::FromMilliseconds(kMinRetransmissionTimeMs/2); 676 } 677 678 const QuicTime QuicSentPacketManager::GetRetransmissionTime() const { 679 // Don't set the timer if there are no packets in flight or we've already 680 // queued a tlp transmission and it hasn't been sent yet. 681 if (!unacked_packets_.HasInFlightPackets() || pending_tlp_transmission_) { 682 return QuicTime::Zero(); 683 } 684 switch (GetRetransmissionMode()) { 685 case HANDSHAKE_MODE: 686 return clock_->ApproximateNow().Add(GetCryptoRetransmissionDelay()); 687 case LOSS_MODE: 688 return loss_algorithm_->GetLossTimeout(); 689 case TLP_MODE: { 690 // TODO(ianswett): When CWND is available, it would be preferable to 691 // set the timer based on the earliest retransmittable packet. 692 // Base the updated timer on the send time of the last packet. 693 const QuicTime sent_time = unacked_packets_.GetLastPacketSentTime(); 694 const QuicTime tlp_time = sent_time.Add(GetTailLossProbeDelay()); 695 // Ensure the TLP timer never gets set to a time in the past. 696 return QuicTime::Max(clock_->ApproximateNow(), tlp_time); 697 } 698 case RTO_MODE: { 699 // The RTO is based on the first outstanding packet. 700 const QuicTime sent_time = 701 unacked_packets_.GetFirstInFlightPacketSentTime(); 702 QuicTime rto_timeout = sent_time.Add(GetRetransmissionDelay()); 703 // Always wait at least 1.5 * RTT from now. 704 QuicTime min_timeout = clock_->ApproximateNow().Add( 705 rtt_stats_.SmoothedRtt().Multiply(1.5)); 706 707 return QuicTime::Max(min_timeout, rto_timeout); 708 } 709 } 710 DCHECK(false); 711 return QuicTime::Zero(); 712 } 713 714 const QuicTime::Delta QuicSentPacketManager::GetCryptoRetransmissionDelay() 715 const { 716 // This is equivalent to the TailLossProbeDelay, but slightly more aggressive 717 // because crypto handshake messages don't incur a delayed ack time. 718 int64 delay_ms = max<int64>(kMinHandshakeTimeoutMs, 719 1.5 * rtt_stats_.SmoothedRtt().ToMilliseconds()); 720 return QuicTime::Delta::FromMilliseconds( 721 delay_ms << consecutive_crypto_retransmission_count_); 722 } 723 724 const QuicTime::Delta QuicSentPacketManager::GetTailLossProbeDelay() const { 725 QuicTime::Delta srtt = rtt_stats_.SmoothedRtt(); 726 if (!unacked_packets_.HasMultipleInFlightPackets()) { 727 return QuicTime::Delta::Max( 728 srtt.Multiply(1.5).Add(DelayedAckTime()), srtt.Multiply(2)); 729 } 730 return QuicTime::Delta::FromMilliseconds( 731 max(kMinTailLossProbeTimeoutMs, 732 static_cast<int64>(2 * srtt.ToMilliseconds()))); 733 } 734 735 const QuicTime::Delta QuicSentPacketManager::GetRetransmissionDelay() const { 736 QuicTime::Delta retransmission_delay = send_algorithm_->RetransmissionDelay(); 737 // TODO(rch): This code should move to |send_algorithm_|. 738 if (retransmission_delay.IsZero()) { 739 // We are in the initial state, use default timeout values. 740 retransmission_delay = 741 QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs); 742 } else if (retransmission_delay.ToMilliseconds() < kMinRetransmissionTimeMs) { 743 retransmission_delay = 744 QuicTime::Delta::FromMilliseconds(kMinRetransmissionTimeMs); 745 } 746 747 // Calculate exponential back off. 748 retransmission_delay = retransmission_delay.Multiply( 749 1 << min<size_t>(consecutive_rto_count_, kMaxRetransmissions)); 750 751 if (retransmission_delay.ToMilliseconds() > kMaxRetransmissionTimeMs) { 752 return QuicTime::Delta::FromMilliseconds(kMaxRetransmissionTimeMs); 753 } 754 return retransmission_delay; 755 } 756 757 const RttStats* QuicSentPacketManager::GetRttStats() const { 758 return &rtt_stats_; 759 } 760 761 QuicBandwidth QuicSentPacketManager::BandwidthEstimate() const { 762 return send_algorithm_->BandwidthEstimate(); 763 } 764 765 QuicByteCount QuicSentPacketManager::GetCongestionWindow() const { 766 return send_algorithm_->GetCongestionWindow(); 767 } 768 769 void QuicSentPacketManager::MaybeEnablePacing() { 770 if (!FLAGS_enable_quic_pacing) { 771 return; 772 } 773 774 if (using_pacing_) { 775 return; 776 } 777 778 // Set up a pacing sender with a 5 millisecond alarm granularity. 779 using_pacing_ = true; 780 send_algorithm_.reset( 781 new PacingSender(send_algorithm_.release(), 782 QuicTime::Delta::FromMilliseconds(5))); 783 } 784 785 } // namespace net 786