QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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/third_party/quiche/src/quic/core/quic_received_packet_manager.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | #include <limits> |
| 9 | #include <utility> |
| 10 | |
| 11 | #include "net/third_party/quiche/src/quic/core/congestion_control/rtt_stats.h" |
| 12 | #include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h" |
| 13 | #include "net/third_party/quiche/src/quic/core/quic_connection_stats.h" |
| 14 | #include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h" |
| 15 | #include "net/third_party/quiche/src/quic/platform/api/quic_logging.h" |
| 16 | |
| 17 | namespace quic { |
| 18 | |
| 19 | namespace { |
| 20 | |
| 21 | // The maximum number of packets to ack immediately after a missing packet for |
| 22 | // fast retransmission to kick in at the sender. This limit is created to |
| 23 | // reduce the number of acks sent that have no benefit for fast retransmission. |
| 24 | // Set to the number of nacks needed for fast retransmit plus one for protection |
| 25 | // against an ack loss |
| 26 | const size_t kMaxPacketsAfterNewMissing = 4; |
| 27 | |
| 28 | // Maximum number of retransmittable packets received before sending an ack. |
| 29 | const QuicPacketCount kDefaultRetransmittablePacketsBeforeAck = 2; |
| 30 | // Minimum number of packets received before ack decimation is enabled. |
| 31 | // This intends to avoid the beginning of slow start, when CWNDs may be |
| 32 | // rapidly increasing. |
| 33 | const QuicPacketCount kMinReceivedBeforeAckDecimation = 100; |
| 34 | // Wait for up to 10 retransmittable packets before sending an ack. |
| 35 | const QuicPacketCount kMaxRetransmittablePacketsBeforeAck = 10; |
| 36 | // One quarter RTT delay when doing ack decimation. |
| 37 | const float kAckDecimationDelay = 0.25; |
| 38 | // One eighth RTT delay when doing ack decimation. |
| 39 | const float kShortAckDecimationDelay = 0.125; |
| 40 | } // namespace |
| 41 | |
QUICHE team | 1dfa46b | 2019-03-22 10:39:10 -0700 | [diff] [blame] | 42 | QuicReceivedPacketManager::QuicReceivedPacketManager() |
| 43 | : QuicReceivedPacketManager(nullptr) {} |
| 44 | |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 45 | QuicReceivedPacketManager::QuicReceivedPacketManager(QuicConnectionStats* stats) |
| 46 | : ack_frame_updated_(false), |
| 47 | max_ack_ranges_(0), |
| 48 | time_largest_observed_(QuicTime::Zero()), |
| 49 | save_timestamps_(false), |
| 50 | stats_(stats), |
| 51 | ack_mode_(GetQuicReloadableFlag(quic_enable_ack_decimation) |
| 52 | ? ACK_DECIMATION |
| 53 | : TCP_ACKING), |
| 54 | num_retransmittable_packets_received_since_last_ack_sent_(0), |
| 55 | min_received_before_ack_decimation_(kMinReceivedBeforeAckDecimation), |
| 56 | ack_frequency_before_ack_decimation_( |
| 57 | kDefaultRetransmittablePacketsBeforeAck), |
| 58 | ack_decimation_delay_(kAckDecimationDelay), |
| 59 | unlimited_ack_decimation_(false), |
| 60 | fast_ack_after_quiescence_(false), |
| 61 | ack_timeout_(QuicTime::Zero()), |
| 62 | time_of_previous_received_packet_(QuicTime::Zero()), |
| 63 | was_last_packet_missing_(false), |
| 64 | decide_when_to_send_acks_( |
| 65 | GetQuicReloadableFlag(quic_deprecate_ack_bundling_mode) && |
| 66 | GetQuicReloadableFlag(quic_rpm_decides_when_to_send_acks)) {} |
| 67 | |
| 68 | QuicReceivedPacketManager::~QuicReceivedPacketManager() {} |
| 69 | |
| 70 | void QuicReceivedPacketManager::SetFromConfig(const QuicConfig& config, |
| 71 | Perspective perspective) { |
| 72 | DCHECK(decide_when_to_send_acks_); |
| 73 | if (GetQuicReloadableFlag(quic_enable_ack_decimation) && |
| 74 | config.HasClientSentConnectionOption(kACD0, perspective)) { |
| 75 | ack_mode_ = TCP_ACKING; |
| 76 | } |
| 77 | if (config.HasClientSentConnectionOption(kACKD, perspective)) { |
| 78 | ack_mode_ = ACK_DECIMATION; |
| 79 | } |
| 80 | if (config.HasClientSentConnectionOption(kAKD2, perspective)) { |
| 81 | ack_mode_ = ACK_DECIMATION_WITH_REORDERING; |
| 82 | } |
| 83 | if (config.HasClientSentConnectionOption(kAKD3, perspective)) { |
| 84 | ack_mode_ = ACK_DECIMATION; |
| 85 | ack_decimation_delay_ = kShortAckDecimationDelay; |
| 86 | } |
| 87 | if (config.HasClientSentConnectionOption(kAKD4, perspective)) { |
| 88 | ack_mode_ = ACK_DECIMATION_WITH_REORDERING; |
| 89 | ack_decimation_delay_ = kShortAckDecimationDelay; |
| 90 | } |
| 91 | if (config.HasClientSentConnectionOption(kAKDU, perspective)) { |
| 92 | unlimited_ack_decimation_ = true; |
| 93 | } |
| 94 | if (config.HasClientSentConnectionOption(kACKQ, perspective)) { |
| 95 | fast_ack_after_quiescence_ = true; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void QuicReceivedPacketManager::RecordPacketReceived( |
| 100 | const QuicPacketHeader& header, |
| 101 | QuicTime receipt_time) { |
| 102 | const QuicPacketNumber packet_number = header.packet_number; |
| 103 | DCHECK(IsAwaitingPacket(packet_number)) << " packet_number:" << packet_number; |
| 104 | if (decide_when_to_send_acks_) { |
| 105 | was_last_packet_missing_ = IsMissing(packet_number); |
| 106 | } |
| 107 | if (!ack_frame_updated_) { |
| 108 | ack_frame_.received_packet_times.clear(); |
| 109 | } |
| 110 | ack_frame_updated_ = true; |
| 111 | |
| 112 | if (LargestAcked(ack_frame_).IsInitialized() && |
| 113 | LargestAcked(ack_frame_) > packet_number) { |
| 114 | // Record how out of order stats. |
| 115 | ++stats_->packets_reordered; |
| 116 | stats_->max_sequence_reordering = |
| 117 | std::max(stats_->max_sequence_reordering, |
| 118 | LargestAcked(ack_frame_) - packet_number); |
| 119 | int64_t reordering_time_us = |
| 120 | (receipt_time - time_largest_observed_).ToMicroseconds(); |
| 121 | stats_->max_time_reordering_us = |
| 122 | std::max(stats_->max_time_reordering_us, reordering_time_us); |
| 123 | } |
| 124 | if (!LargestAcked(ack_frame_).IsInitialized() || |
| 125 | packet_number > LargestAcked(ack_frame_)) { |
| 126 | ack_frame_.largest_acked = packet_number; |
| 127 | time_largest_observed_ = receipt_time; |
| 128 | } |
| 129 | ack_frame_.packets.Add(packet_number); |
| 130 | |
| 131 | if (save_timestamps_) { |
| 132 | // The timestamp format only handles packets in time order. |
| 133 | if (!ack_frame_.received_packet_times.empty() && |
| 134 | ack_frame_.received_packet_times.back().second > receipt_time) { |
| 135 | LOG(WARNING) |
| 136 | << "Receive time went backwards from: " |
| 137 | << ack_frame_.received_packet_times.back().second.ToDebuggingValue() |
| 138 | << " to " << receipt_time.ToDebuggingValue(); |
| 139 | } else { |
| 140 | ack_frame_.received_packet_times.push_back( |
| 141 | std::make_pair(packet_number, receipt_time)); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | if (least_received_packet_number_.IsInitialized()) { |
| 146 | least_received_packet_number_ = |
| 147 | std::min(least_received_packet_number_, packet_number); |
| 148 | } else { |
| 149 | least_received_packet_number_ = packet_number; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | bool QuicReceivedPacketManager::IsMissing(QuicPacketNumber packet_number) { |
| 154 | return LargestAcked(ack_frame_).IsInitialized() && |
| 155 | packet_number < LargestAcked(ack_frame_) && |
| 156 | !ack_frame_.packets.Contains(packet_number); |
| 157 | } |
| 158 | |
| 159 | bool QuicReceivedPacketManager::IsAwaitingPacket( |
QUICHE team | b23daa7 | 2019-03-21 08:37:48 -0700 | [diff] [blame] | 160 | QuicPacketNumber packet_number) const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 161 | return quic::IsAwaitingPacket(ack_frame_, packet_number, |
| 162 | peer_least_packet_awaiting_ack_); |
| 163 | } |
| 164 | |
| 165 | const QuicFrame QuicReceivedPacketManager::GetUpdatedAckFrame( |
| 166 | QuicTime approximate_now) { |
| 167 | if (!decide_when_to_send_acks_) { |
| 168 | ack_frame_updated_ = false; |
| 169 | } |
| 170 | if (time_largest_observed_ == QuicTime::Zero()) { |
| 171 | // We have received no packets. |
| 172 | ack_frame_.ack_delay_time = QuicTime::Delta::Infinite(); |
| 173 | } else { |
| 174 | // Ensure the delta is zero if approximate now is "in the past". |
| 175 | ack_frame_.ack_delay_time = approximate_now < time_largest_observed_ |
| 176 | ? QuicTime::Delta::Zero() |
| 177 | : approximate_now - time_largest_observed_; |
| 178 | } |
| 179 | while (max_ack_ranges_ > 0 && |
| 180 | ack_frame_.packets.NumIntervals() > max_ack_ranges_) { |
| 181 | ack_frame_.packets.RemoveSmallestInterval(); |
| 182 | } |
| 183 | // Clear all packet times if any are too far from largest observed. |
| 184 | // It's expected this is extremely rare. |
| 185 | for (auto it = ack_frame_.received_packet_times.begin(); |
| 186 | it != ack_frame_.received_packet_times.end();) { |
| 187 | if (LargestAcked(ack_frame_) - it->first >= |
| 188 | std::numeric_limits<uint8_t>::max()) { |
| 189 | it = ack_frame_.received_packet_times.erase(it); |
| 190 | } else { |
| 191 | ++it; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | return QuicFrame(&ack_frame_); |
| 196 | } |
| 197 | |
| 198 | void QuicReceivedPacketManager::DontWaitForPacketsBefore( |
| 199 | QuicPacketNumber least_unacked) { |
| 200 | if (!least_unacked.IsInitialized()) { |
| 201 | return; |
| 202 | } |
| 203 | // ValidateAck() should fail if peer_least_packet_awaiting_ack shrinks. |
| 204 | DCHECK(!peer_least_packet_awaiting_ack_.IsInitialized() || |
| 205 | peer_least_packet_awaiting_ack_ <= least_unacked); |
| 206 | if (!peer_least_packet_awaiting_ack_.IsInitialized() || |
| 207 | least_unacked > peer_least_packet_awaiting_ack_) { |
| 208 | peer_least_packet_awaiting_ack_ = least_unacked; |
| 209 | bool packets_updated = ack_frame_.packets.RemoveUpTo(least_unacked); |
| 210 | if (packets_updated) { |
| 211 | // Ack frame gets updated because packets set is updated because of stop |
| 212 | // waiting frame. |
| 213 | ack_frame_updated_ = true; |
| 214 | } |
| 215 | } |
| 216 | DCHECK(ack_frame_.packets.Empty() || |
| 217 | !peer_least_packet_awaiting_ack_.IsInitialized() || |
| 218 | ack_frame_.packets.Min() >= peer_least_packet_awaiting_ack_); |
| 219 | } |
| 220 | |
| 221 | void QuicReceivedPacketManager::MaybeUpdateAckTimeout( |
| 222 | bool should_last_packet_instigate_acks, |
| 223 | QuicPacketNumber last_received_packet_number, |
| 224 | QuicTime time_of_last_received_packet, |
| 225 | QuicTime now, |
| 226 | const RttStats* rtt_stats, |
| 227 | QuicTime::Delta delayed_ack_time) { |
| 228 | DCHECK(decide_when_to_send_acks_); |
| 229 | if (!ack_frame_updated_) { |
| 230 | // ACK frame has not been updated, nothing to do. |
| 231 | return; |
| 232 | } |
| 233 | |
| 234 | if (was_last_packet_missing_ && last_sent_largest_acked_.IsInitialized() && |
| 235 | last_received_packet_number < last_sent_largest_acked_) { |
| 236 | // Only ack immediately if an ACK frame was sent with a larger largest acked |
| 237 | // than the newly received packet number. |
| 238 | ack_timeout_ = now; |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | if (!should_last_packet_instigate_acks) { |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | ++num_retransmittable_packets_received_since_last_ack_sent_; |
| 247 | if (ack_mode_ != TCP_ACKING && |
| 248 | last_received_packet_number >= PeerFirstSendingPacketNumber() + |
| 249 | min_received_before_ack_decimation_) { |
| 250 | // Ack up to 10 packets at once unless ack decimation is unlimited. |
| 251 | if (!unlimited_ack_decimation_ && |
| 252 | num_retransmittable_packets_received_since_last_ack_sent_ >= |
| 253 | kMaxRetransmittablePacketsBeforeAck) { |
| 254 | ack_timeout_ = now; |
| 255 | return; |
| 256 | } |
| 257 | // Wait for the minimum of the ack decimation delay or the delayed ack time |
| 258 | // before sending an ack. |
| 259 | QuicTime::Delta ack_delay = std::min( |
| 260 | delayed_ack_time, rtt_stats->min_rtt() * ack_decimation_delay_); |
| 261 | if (fast_ack_after_quiescence_ && now - time_of_previous_received_packet_ > |
| 262 | rtt_stats->SmoothedOrInitialRtt()) { |
| 263 | // Ack the first packet out of queiscence faster, because QUIC does |
| 264 | // not pace the first few packets and commonly these may be handshake |
| 265 | // or TLP packets, which we'd like to acknowledge quickly. |
| 266 | ack_delay = QuicTime::Delta::FromMilliseconds(1); |
| 267 | } |
| 268 | MaybeUpdateAckTimeoutTo(now + ack_delay); |
| 269 | } else { |
| 270 | // Ack with a timer or every 2 packets by default. |
| 271 | if (num_retransmittable_packets_received_since_last_ack_sent_ >= |
| 272 | ack_frequency_before_ack_decimation_) { |
| 273 | ack_timeout_ = now; |
| 274 | } else if (fast_ack_after_quiescence_ && |
| 275 | (now - time_of_previous_received_packet_) > |
| 276 | rtt_stats->SmoothedOrInitialRtt()) { |
| 277 | // Ack the first packet out of queiscence faster, because QUIC does |
| 278 | // not pace the first few packets and commonly these may be handshake |
| 279 | // or TLP packets, which we'd like to acknowledge quickly. |
| 280 | MaybeUpdateAckTimeoutTo(now + QuicTime::Delta::FromMilliseconds(1)); |
| 281 | } else { |
| 282 | MaybeUpdateAckTimeoutTo(now + delayed_ack_time); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | // If there are new missing packets to report, send an ack immediately. |
| 287 | if (HasNewMissingPackets()) { |
| 288 | if (ack_mode_ == ACK_DECIMATION_WITH_REORDERING) { |
| 289 | // Wait the minimum of an eighth min_rtt and the existing ack time. |
| 290 | QuicTime ack_time = now + kShortAckDecimationDelay * rtt_stats->min_rtt(); |
| 291 | MaybeUpdateAckTimeoutTo(ack_time); |
| 292 | } else { |
| 293 | ack_timeout_ = now; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | if (fast_ack_after_quiescence_) { |
| 298 | time_of_previous_received_packet_ = time_of_last_received_packet; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | void QuicReceivedPacketManager::ResetAckStates() { |
| 303 | DCHECK(decide_when_to_send_acks_); |
| 304 | ack_frame_updated_ = false; |
| 305 | ack_timeout_ = QuicTime::Zero(); |
| 306 | num_retransmittable_packets_received_since_last_ack_sent_ = 0; |
| 307 | last_sent_largest_acked_ = LargestAcked(ack_frame_); |
| 308 | } |
| 309 | |
| 310 | void QuicReceivedPacketManager::MaybeUpdateAckTimeoutTo(QuicTime time) { |
| 311 | DCHECK(decide_when_to_send_acks_); |
| 312 | if (!ack_timeout_.IsInitialized() || ack_timeout_ > time) { |
| 313 | ack_timeout_ = time; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | bool QuicReceivedPacketManager::HasMissingPackets() const { |
| 318 | if (ack_frame_.packets.Empty()) { |
| 319 | return false; |
| 320 | } |
| 321 | if (ack_frame_.packets.NumIntervals() > 1) { |
| 322 | return true; |
| 323 | } |
| 324 | if (!GetQuicRestartFlag(quic_enable_accept_random_ipn)) { |
| 325 | return ack_frame_.packets.Min() > |
| 326 | (peer_least_packet_awaiting_ack_.IsInitialized() |
| 327 | ? peer_least_packet_awaiting_ack_ |
| 328 | : QuicPacketNumber(1)); |
| 329 | } |
| 330 | return peer_least_packet_awaiting_ack_.IsInitialized() && |
| 331 | ack_frame_.packets.Min() > peer_least_packet_awaiting_ack_; |
| 332 | } |
| 333 | |
| 334 | bool QuicReceivedPacketManager::HasNewMissingPackets() const { |
| 335 | return HasMissingPackets() && |
| 336 | ack_frame_.packets.LastIntervalLength() <= kMaxPacketsAfterNewMissing; |
| 337 | } |
| 338 | |
| 339 | bool QuicReceivedPacketManager::ack_frame_updated() const { |
| 340 | return ack_frame_updated_; |
| 341 | } |
| 342 | |
| 343 | QuicPacketNumber QuicReceivedPacketManager::GetLargestObserved() const { |
| 344 | return LargestAcked(ack_frame_); |
| 345 | } |
| 346 | |
| 347 | QuicPacketNumber QuicReceivedPacketManager::PeerFirstSendingPacketNumber() |
| 348 | const { |
| 349 | if (!GetQuicRestartFlag(quic_enable_accept_random_ipn)) { |
| 350 | return QuicPacketNumber(1); |
| 351 | } |
| 352 | if (!least_received_packet_number_.IsInitialized()) { |
| 353 | QUIC_BUG << "No packets have been received yet"; |
| 354 | return QuicPacketNumber(1); |
| 355 | } |
| 356 | return least_received_packet_number_; |
| 357 | } |
| 358 | |
| 359 | } // namespace quic |