blob: 89757de5790f1a5b352d83b2becb95df161117d0 [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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_sent_packet_manager.h"
6
7#include <algorithm>
vasilvv872e7a32019-03-12 16:42:44 -07008#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -05009
10#include "net/third_party/quiche/src/quic/core/congestion_control/general_loss_algorithm.h"
11#include "net/third_party/quiche/src/quic/core/congestion_control/pacing_sender.h"
12#include "net/third_party/quiche/src/quic/core/crypto/crypto_protocol.h"
13#include "net/third_party/quiche/src/quic/core/proto/cached_network_parameters.pb.h"
14#include "net/third_party/quiche/src/quic/core/quic_connection_stats.h"
15#include "net/third_party/quiche/src/quic/core/quic_pending_retransmission.h"
wub967ba572019-04-01 09:27:52 -070016#include "net/third_party/quiche/src/quic/core/quic_types.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050017#include "net/third_party/quiche/src/quic/core/quic_utils.h"
18#include "net/third_party/quiche/src/quic/platform/api/quic_bug_tracker.h"
19#include "net/third_party/quiche/src/quic/platform/api/quic_flag_utils.h"
20#include "net/third_party/quiche/src/quic/platform/api/quic_flags.h"
21#include "net/third_party/quiche/src/quic/platform/api/quic_logging.h"
22#include "net/third_party/quiche/src/quic/platform/api/quic_map_util.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050023
24namespace quic {
25
26namespace {
27static const int64_t kDefaultRetransmissionTimeMs = 500;
28static const int64_t kMaxRetransmissionTimeMs = 60000;
29// Maximum number of exponential backoffs used for RTO timeouts.
30static const size_t kMaxRetransmissions = 10;
31// Maximum number of packets retransmitted upon an RTO.
32static const size_t kMaxRetransmissionsOnTimeout = 2;
33// The path degrading delay is the sum of this number of consecutive RTO delays.
34const size_t kNumRetransmissionDelaysForPathDegradingDelay = 2;
35
36// Ensure the handshake timer isnt't faster than 10ms.
37// This limits the tenth retransmitted packet to 10s after the initial CHLO.
38static const int64_t kMinHandshakeTimeoutMs = 10;
39
40// Sends up to two tail loss probes before firing an RTO,
41// per draft RFC draft-dukkipati-tcpm-tcp-loss-probe.
42static const size_t kDefaultMaxTailLossProbes = 2;
43
44inline bool HasCryptoHandshake(const QuicTransmissionInfo& transmission_info) {
45 DCHECK(!transmission_info.has_crypto_handshake ||
46 !transmission_info.retransmittable_frames.empty());
47 return transmission_info.has_crypto_handshake;
48}
49
50// Returns true if retransmissions the specified type leave the data in flight.
51inline bool RetransmissionLeavesBytesInFlight(
52 TransmissionType transmission_type) {
53 // Both TLP and the new RTO leave the packets in flight and let the loss
54 // detection decide if packets are lost.
55 return transmission_type == TLP_RETRANSMISSION ||
56 transmission_type == PROBING_RETRANSMISSION ||
57 transmission_type == RTO_RETRANSMISSION;
58}
59
60// Returns true of retransmissions of the specified type should retransmit
61// the frames directly (as opposed to resulting in a loss notification).
62inline bool ShouldForceRetransmission(TransmissionType transmission_type) {
63 return transmission_type == HANDSHAKE_RETRANSMISSION ||
64 transmission_type == TLP_RETRANSMISSION ||
65 transmission_type == PROBING_RETRANSMISSION ||
66 transmission_type == RTO_RETRANSMISSION;
67}
68
69} // namespace
70
71#define ENDPOINT \
72 (unacked_packets_.perspective() == Perspective::IS_SERVER ? "Server: " \
73 : "Client: ")
74
75QuicSentPacketManager::QuicSentPacketManager(
76 Perspective perspective,
77 const QuicClock* clock,
78 QuicConnectionStats* stats,
79 CongestionControlType congestion_control_type,
80 LossDetectionType loss_type)
81 : unacked_packets_(perspective),
82 clock_(clock),
83 stats_(stats),
84 debug_delegate_(nullptr),
85 network_change_visitor_(nullptr),
86 initial_congestion_window_(kInitialCongestionWindow),
87 loss_algorithm_(
88 unacked_packets_.use_uber_loss_algorithm()
89 ? dynamic_cast<LossDetectionInterface*>(&uber_loss_algorithm_)
90 : dynamic_cast<LossDetectionInterface*>(
91 &general_loss_algorithm_)),
92 general_loss_algorithm_(loss_type),
93 uber_loss_algorithm_(loss_type),
94 consecutive_rto_count_(0),
95 consecutive_tlp_count_(0),
96 consecutive_crypto_retransmission_count_(0),
97 pending_timer_transmission_count_(0),
98 max_tail_loss_probes_(kDefaultMaxTailLossProbes),
99 max_rto_packets_(kMaxRetransmissionsOnTimeout),
100 enable_half_rtt_tail_loss_probe_(false),
101 using_pacing_(false),
102 use_new_rto_(false),
103 conservative_handshake_retransmits_(false),
104 min_tlp_timeout_(
105 QuicTime::Delta::FromMilliseconds(kMinTailLossProbeTimeoutMs)),
106 min_rto_timeout_(
107 QuicTime::Delta::FromMilliseconds(kMinRetransmissionTimeMs)),
108 ietf_style_tlp_(false),
109 ietf_style_2x_tlp_(false),
110 largest_mtu_acked_(0),
111 handshake_confirmed_(false),
112 delayed_ack_time_(
113 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs)),
114 rtt_updated_(false),
QUICHE team9929cc42019-03-13 08:17:43 -0700115 acked_packets_iter_(last_ack_frame_.packets.rbegin()),
116 tolerate_reneging_(GetQuicReloadableFlag(quic_tolerate_reneging)) {
117 if (tolerate_reneging_) {
118 QUIC_RELOADABLE_FLAG_COUNT(quic_tolerate_reneging);
119 }
QUICHE teama6ef0a62019-03-07 20:34:33 -0500120 SetSendAlgorithm(congestion_control_type);
121}
122
123QuicSentPacketManager::~QuicSentPacketManager() {}
124
125void QuicSentPacketManager::SetFromConfig(const QuicConfig& config) {
126 const Perspective perspective = unacked_packets_.perspective();
127 if (config.HasReceivedInitialRoundTripTimeUs() &&
128 config.ReceivedInitialRoundTripTimeUs() > 0) {
129 if (!config.HasClientSentConnectionOption(kNRTT, perspective)) {
130 SetInitialRtt(QuicTime::Delta::FromMicroseconds(
131 config.ReceivedInitialRoundTripTimeUs()));
132 }
133 } else if (config.HasInitialRoundTripTimeUsToSend() &&
134 config.GetInitialRoundTripTimeUsToSend() > 0) {
135 SetInitialRtt(QuicTime::Delta::FromMicroseconds(
136 config.GetInitialRoundTripTimeUsToSend()));
137 }
138 if (config.HasClientSentConnectionOption(kMAD0, perspective)) {
139 rtt_stats_.set_ignore_max_ack_delay(true);
140 }
141 if (config.HasClientSentConnectionOption(kMAD1, perspective)) {
142 rtt_stats_.set_initial_max_ack_delay(delayed_ack_time_);
143 }
144 if (config.HasClientSentConnectionOption(kMAD2, perspective)) {
145 min_tlp_timeout_ = QuicTime::Delta::Zero();
146 }
147 if (config.HasClientSentConnectionOption(kMAD3, perspective)) {
148 min_rto_timeout_ = QuicTime::Delta::Zero();
149 }
150 if (config.HasClientSentConnectionOption(kMAD4, perspective)) {
151 ietf_style_tlp_ = true;
152 }
153 if (config.HasClientSentConnectionOption(kMAD5, perspective)) {
154 ietf_style_2x_tlp_ = true;
155 }
156
157 // Configure congestion control.
158 if (config.HasClientRequestedIndependentOption(kTBBR, perspective)) {
159 SetSendAlgorithm(kBBR);
160 }
161 if (config.HasClientRequestedIndependentOption(kRENO, perspective)) {
162 SetSendAlgorithm(kRenoBytes);
163 } else if (config.HasClientRequestedIndependentOption(kBYTE, perspective) ||
164 (GetQuicReloadableFlag(quic_default_to_bbr) &&
165 config.HasClientRequestedIndependentOption(kQBIC, perspective))) {
166 SetSendAlgorithm(kCubicBytes);
167 } else if (GetQuicReloadableFlag(quic_enable_pcc3) &&
168 config.HasClientRequestedIndependentOption(kTPCC, perspective)) {
169 SetSendAlgorithm(kPCC);
170 }
171 // Initial window.
172 if (GetQuicReloadableFlag(quic_unified_iw_options)) {
173 if (config.HasClientRequestedIndependentOption(kIW03, perspective)) {
174 initial_congestion_window_ = 3;
175 send_algorithm_->SetInitialCongestionWindowInPackets(3);
176 }
177 if (config.HasClientRequestedIndependentOption(kIW10, perspective)) {
178 initial_congestion_window_ = 10;
179 send_algorithm_->SetInitialCongestionWindowInPackets(10);
180 }
181 if (config.HasClientRequestedIndependentOption(kIW20, perspective)) {
182 initial_congestion_window_ = 20;
183 send_algorithm_->SetInitialCongestionWindowInPackets(20);
184 }
185 if (config.HasClientRequestedIndependentOption(kIW50, perspective)) {
186 initial_congestion_window_ = 50;
187 send_algorithm_->SetInitialCongestionWindowInPackets(50);
188 }
189 }
190
191 using_pacing_ = !FLAGS_quic_disable_pacing_for_perf_tests;
192
193 if (config.HasClientSentConnectionOption(k1CON, perspective)) {
194 send_algorithm_->SetNumEmulatedConnections(1);
195 }
196 if (config.HasClientSentConnectionOption(kNTLP, perspective)) {
197 max_tail_loss_probes_ = 0;
198 }
199 if (config.HasClientSentConnectionOption(k1TLP, perspective)) {
200 max_tail_loss_probes_ = 1;
201 }
202 if (config.HasClientSentConnectionOption(k1RTO, perspective)) {
203 max_rto_packets_ = 1;
204 }
205 if (config.HasClientSentConnectionOption(kTLPR, perspective)) {
206 enable_half_rtt_tail_loss_probe_ = true;
207 }
208 if (config.HasClientSentConnectionOption(kNRTO, perspective)) {
209 use_new_rto_ = true;
210 }
211 // Configure loss detection.
212 if (config.HasClientRequestedIndependentOption(kTIME, perspective)) {
213 if (unacked_packets_.use_uber_loss_algorithm()) {
214 uber_loss_algorithm_.SetLossDetectionType(kTime);
215 } else {
216 general_loss_algorithm_.SetLossDetectionType(kTime);
217 }
218 }
219 if (config.HasClientRequestedIndependentOption(kATIM, perspective)) {
220 if (unacked_packets_.use_uber_loss_algorithm()) {
221 uber_loss_algorithm_.SetLossDetectionType(kAdaptiveTime);
222 } else {
223 general_loss_algorithm_.SetLossDetectionType(kAdaptiveTime);
224 }
225 }
226 if (config.HasClientRequestedIndependentOption(kLFAK, perspective)) {
227 if (unacked_packets_.use_uber_loss_algorithm()) {
228 uber_loss_algorithm_.SetLossDetectionType(kLazyFack);
229 } else {
230 general_loss_algorithm_.SetLossDetectionType(kLazyFack);
231 }
232 }
233 if (config.HasClientSentConnectionOption(kCONH, perspective)) {
234 conservative_handshake_retransmits_ = true;
235 }
236 send_algorithm_->SetFromConfig(config, perspective);
237
238 if (network_change_visitor_ != nullptr) {
239 network_change_visitor_->OnCongestionChange();
240 }
241}
242
243void QuicSentPacketManager::ResumeConnectionState(
244 const CachedNetworkParameters& cached_network_params,
245 bool max_bandwidth_resumption) {
246 QuicBandwidth bandwidth = QuicBandwidth::FromBytesPerSecond(
247 max_bandwidth_resumption
248 ? cached_network_params.max_bandwidth_estimate_bytes_per_second()
249 : cached_network_params.bandwidth_estimate_bytes_per_second());
250 QuicTime::Delta rtt =
251 QuicTime::Delta::FromMilliseconds(cached_network_params.min_rtt_ms());
252 AdjustNetworkParameters(bandwidth, rtt);
253}
254
255void QuicSentPacketManager::AdjustNetworkParameters(QuicBandwidth bandwidth,
256 QuicTime::Delta rtt) {
257 if (!rtt.IsZero()) {
258 SetInitialRtt(rtt);
259 }
260 send_algorithm_->AdjustNetworkParameters(bandwidth, rtt);
261 if (debug_delegate_ != nullptr) {
262 debug_delegate_->OnAdjustNetworkParameters(bandwidth, rtt);
263 }
264}
265
266void QuicSentPacketManager::SetHandshakeConfirmed() {
267 handshake_confirmed_ = true;
268 if (unacked_packets_.use_uber_loss_algorithm()) {
269 NeuterHandshakePackets();
270 }
271}
272
273void QuicSentPacketManager::PostProcessAfterMarkingPacketHandled(
274 const QuicAckFrame& ack_frame,
275 QuicTime ack_receive_time,
276 bool rtt_updated,
277 QuicByteCount prior_bytes_in_flight) {
278 if (session_decides_what_to_write()) {
279 unacked_packets_.NotifyAggregatedStreamFrameAcked(
280 last_ack_frame_.ack_delay_time);
281 }
282 InvokeLossDetection(ack_receive_time);
283 // Ignore losses in RTO mode.
284 if (consecutive_rto_count_ > 0 && !use_new_rto_) {
285 packets_lost_.clear();
286 }
287 MaybeInvokeCongestionEvent(rtt_updated, prior_bytes_in_flight,
288 ack_receive_time);
289 unacked_packets_.RemoveObsoletePackets();
290
291 sustained_bandwidth_recorder_.RecordEstimate(
292 send_algorithm_->InRecovery(), send_algorithm_->InSlowStart(),
293 send_algorithm_->BandwidthEstimate(), ack_receive_time, clock_->WallNow(),
294 rtt_stats_.smoothed_rtt());
295
296 // Anytime we are making forward progress and have a new RTT estimate, reset
297 // the backoff counters.
298 if (rtt_updated) {
299 if (consecutive_rto_count_ > 0) {
300 // If the ack acknowledges data sent prior to the RTO,
301 // the RTO was spurious.
302 if (LargestAcked(ack_frame) < first_rto_transmission_) {
303 // Replace SRTT with latest_rtt and increase the variance to prevent
304 // a spurious RTO from happening again.
305 rtt_stats_.ExpireSmoothedMetrics();
306 } else {
307 if (!use_new_rto_) {
308 send_algorithm_->OnRetransmissionTimeout(true);
309 }
310 }
311 }
312 // Reset all retransmit counters any time a new packet is acked.
313 consecutive_rto_count_ = 0;
314 consecutive_tlp_count_ = 0;
315 consecutive_crypto_retransmission_count_ = 0;
316 }
317
318 if (debug_delegate_ != nullptr) {
319 debug_delegate_->OnIncomingAck(ack_frame, ack_receive_time,
320 LargestAcked(ack_frame), rtt_updated,
321 GetLeastUnacked());
322 }
323 // Remove packets below least unacked from all_packets_acked_ and
324 // last_ack_frame_.
325 last_ack_frame_.packets.RemoveUpTo(unacked_packets_.GetLeastUnacked());
326 last_ack_frame_.received_packet_times.clear();
327}
328
329void QuicSentPacketManager::MaybeInvokeCongestionEvent(
330 bool rtt_updated,
331 QuicByteCount prior_in_flight,
332 QuicTime event_time) {
333 if (!rtt_updated && packets_acked_.empty() && packets_lost_.empty()) {
334 return;
335 }
336 if (using_pacing_) {
337 pacing_sender_.OnCongestionEvent(rtt_updated, prior_in_flight, event_time,
338 packets_acked_, packets_lost_);
339 } else {
340 send_algorithm_->OnCongestionEvent(rtt_updated, prior_in_flight, event_time,
341 packets_acked_, packets_lost_);
342 }
343 packets_acked_.clear();
344 packets_lost_.clear();
345 if (network_change_visitor_ != nullptr) {
346 network_change_visitor_->OnCongestionChange();
347 }
348}
349
350void QuicSentPacketManager::RetransmitUnackedPackets(
351 TransmissionType retransmission_type) {
352 DCHECK(retransmission_type == ALL_UNACKED_RETRANSMISSION ||
353 retransmission_type == ALL_INITIAL_RETRANSMISSION);
354 QuicPacketNumber packet_number = unacked_packets_.GetLeastUnacked();
355 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
356 it != unacked_packets_.end(); ++it, ++packet_number) {
357 if ((retransmission_type == ALL_UNACKED_RETRANSMISSION ||
358 it->encryption_level == ENCRYPTION_ZERO_RTT) &&
359 unacked_packets_.HasRetransmittableFrames(*it)) {
360 MarkForRetransmission(packet_number, retransmission_type);
361 }
362 }
363}
364
365void QuicSentPacketManager::NeuterUnencryptedPackets() {
366 QuicPacketNumber packet_number = unacked_packets_.GetLeastUnacked();
367 if (session_decides_what_to_write()) {
368 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
369 it != unacked_packets_.end(); ++it, ++packet_number) {
370 if (!it->retransmittable_frames.empty() &&
QUICHE team6987b4a2019-03-15 16:23:04 -0700371 it->encryption_level == ENCRYPTION_INITIAL) {
QUICHE teama6ef0a62019-03-07 20:34:33 -0500372 // Once the connection swithes to forward secure, no unencrypted packets
373 // will be sent. The data has been abandoned in the cryto stream. Remove
374 // it from in flight.
375 unacked_packets_.RemoveFromInFlight(packet_number);
376 }
377 }
378 return;
379 }
380 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
381 it != unacked_packets_.end(); ++it, ++packet_number) {
QUICHE team6987b4a2019-03-15 16:23:04 -0700382 if (it->encryption_level == ENCRYPTION_INITIAL &&
QUICHE teama6ef0a62019-03-07 20:34:33 -0500383 unacked_packets_.HasRetransmittableFrames(*it)) {
384 // Once you're forward secure, no unencrypted packets will be sent, crypto
385 // or otherwise. Unencrypted packets are neutered and abandoned, to ensure
386 // they are not retransmitted or considered lost from a congestion control
387 // perspective.
388 pending_retransmissions_.erase(packet_number);
389 unacked_packets_.RemoveFromInFlight(packet_number);
390 unacked_packets_.RemoveRetransmittability(packet_number);
391 }
392 }
393}
394
395void QuicSentPacketManager::NeuterHandshakePackets() {
396 DCHECK(unacked_packets_.use_uber_loss_algorithm());
397 QuicPacketNumber packet_number = unacked_packets_.GetLeastUnacked();
398 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
399 it != unacked_packets_.end(); ++it, ++packet_number) {
400 if (session_decides_what_to_write()) {
401 if (!it->retransmittable_frames.empty() &&
402 unacked_packets_.GetPacketNumberSpace(it->encryption_level) ==
403 HANDSHAKE_DATA) {
404 unacked_packets_.RemoveFromInFlight(packet_number);
405 }
406 continue;
407 }
408 if (unacked_packets_.GetPacketNumberSpace(it->encryption_level) ==
409 HANDSHAKE_DATA &&
410 unacked_packets_.HasRetransmittableFrames(*it)) {
411 pending_retransmissions_.erase(packet_number);
412 unacked_packets_.RemoveFromInFlight(packet_number);
413 unacked_packets_.RemoveRetransmittability(packet_number);
414 }
415 }
416}
417
418void QuicSentPacketManager::MarkForRetransmission(
419 QuicPacketNumber packet_number,
420 TransmissionType transmission_type) {
421 QuicTransmissionInfo* transmission_info =
422 unacked_packets_.GetMutableTransmissionInfo(packet_number);
423 // When session decides what to write, a previous RTO retransmission may cause
424 // connection close; packets without retransmittable frames can be marked for
425 // loss retransmissions.
426 QUIC_BUG_IF((transmission_type != LOSS_RETRANSMISSION &&
427 (!session_decides_what_to_write() ||
428 transmission_type != RTO_RETRANSMISSION)) &&
429 !unacked_packets_.HasRetransmittableFrames(*transmission_info))
430 << "transmission_type: "
431 << QuicUtils::TransmissionTypeToString(transmission_type);
432 // Handshake packets should never be sent as probing retransmissions.
433 DCHECK(!transmission_info->has_crypto_handshake ||
434 transmission_type != PROBING_RETRANSMISSION);
435 if (!RetransmissionLeavesBytesInFlight(transmission_type)) {
436 unacked_packets_.RemoveFromInFlight(transmission_info);
437 }
438
439 if (!session_decides_what_to_write()) {
440 if (!unacked_packets_.HasRetransmittableFrames(*transmission_info)) {
441 return;
442 }
443 if (!QuicContainsKey(pending_retransmissions_, packet_number)) {
444 pending_retransmissions_[packet_number] = transmission_type;
445 }
446 return;
447 }
448
449 HandleRetransmission(transmission_type, transmission_info);
450
451 // Update packet state according to transmission type.
452 transmission_info->state =
453 QuicUtils::RetransmissionTypeToPacketState(transmission_type);
454}
455
456void QuicSentPacketManager::HandleRetransmission(
457 TransmissionType transmission_type,
458 QuicTransmissionInfo* transmission_info) {
459 DCHECK(session_decides_what_to_write());
460 if (ShouldForceRetransmission(transmission_type)) {
461 // TODO(fayang): Consider to make RTO and PROBING retransmission
462 // strategies be configurable by applications. Today, TLP, RTO and PROBING
463 // retransmissions are handled similarly, i.e., always retranmist the
464 // oldest outstanding data. This is not ideal in general because different
465 // applications may want different strategies. For example, some
466 // applications may want to use higher priority stream data for bandwidth
467 // probing, and some applications want to consider RTO is an indication of
468 // loss, etc.
469 unacked_packets_.RetransmitFrames(*transmission_info, transmission_type);
470 return;
471 }
472
473 unacked_packets_.NotifyFramesLost(*transmission_info, transmission_type);
474 if (transmission_info->retransmittable_frames.empty()) {
475 return;
476 }
477
478 if (transmission_type == LOSS_RETRANSMISSION) {
479 // Record the first packet sent after loss, which allows to wait 1
480 // more RTT before giving up on this lost packet.
481 transmission_info->retransmission =
482 unacked_packets_.largest_sent_packet() + 1;
483 } else {
484 // Clear the recorded first packet sent after loss when version or
485 // encryption changes.
486 transmission_info->retransmission.Clear();
487 }
488}
489
490void QuicSentPacketManager::RecordOneSpuriousRetransmission(
491 const QuicTransmissionInfo& info) {
492 stats_->bytes_spuriously_retransmitted += info.bytes_sent;
493 ++stats_->packets_spuriously_retransmitted;
494 if (debug_delegate_ != nullptr) {
495 debug_delegate_->OnSpuriousPacketRetransmission(info.transmission_type,
496 info.bytes_sent);
497 }
498}
499
500void QuicSentPacketManager::RecordSpuriousRetransmissions(
501 const QuicTransmissionInfo& info,
502 QuicPacketNumber acked_packet_number) {
503 if (session_decides_what_to_write()) {
504 RecordOneSpuriousRetransmission(info);
505 if (info.transmission_type == LOSS_RETRANSMISSION) {
506 // Only inform the loss detection of spurious retransmits it caused.
507 loss_algorithm_->SpuriousRetransmitDetected(
508 unacked_packets_, clock_->Now(), rtt_stats_, acked_packet_number);
509 }
510 return;
511 }
512 QuicPacketNumber retransmission = info.retransmission;
513 while (retransmission.IsInitialized()) {
514 const QuicTransmissionInfo& retransmit_info =
515 unacked_packets_.GetTransmissionInfo(retransmission);
516 retransmission = retransmit_info.retransmission;
517 RecordOneSpuriousRetransmission(retransmit_info);
518 }
519 // Only inform the loss detection of spurious retransmits it caused.
520 if (unacked_packets_.GetTransmissionInfo(info.retransmission)
521 .transmission_type == LOSS_RETRANSMISSION) {
522 loss_algorithm_->SpuriousRetransmitDetected(
523 unacked_packets_, clock_->Now(), rtt_stats_, info.retransmission);
524 }
525}
526
527QuicPendingRetransmission QuicSentPacketManager::NextPendingRetransmission() {
528 QUIC_BUG_IF(pending_retransmissions_.empty())
529 << "Unexpected call to NextPendingRetransmission() with empty pending "
530 << "retransmission list. Corrupted memory usage imminent.";
531 QUIC_BUG_IF(session_decides_what_to_write())
532 << "Unexpected call to NextPendingRetransmission() when session handles "
533 "retransmissions";
534 QuicPacketNumber packet_number = pending_retransmissions_.begin()->first;
535 TransmissionType transmission_type = pending_retransmissions_.begin()->second;
536 if (unacked_packets_.HasPendingCryptoPackets()) {
537 // Ensure crypto packets are retransmitted before other packets.
538 for (const auto& pair : pending_retransmissions_) {
539 if (HasCryptoHandshake(
540 unacked_packets_.GetTransmissionInfo(pair.first))) {
541 packet_number = pair.first;
542 transmission_type = pair.second;
543 break;
544 }
545 }
546 }
547 DCHECK(unacked_packets_.IsUnacked(packet_number)) << packet_number;
548 const QuicTransmissionInfo& transmission_info =
549 unacked_packets_.GetTransmissionInfo(packet_number);
550 DCHECK(unacked_packets_.HasRetransmittableFrames(transmission_info));
551
552 return QuicPendingRetransmission(packet_number, transmission_type,
553 transmission_info);
554}
555
556QuicPacketNumber QuicSentPacketManager::GetNewestRetransmission(
557 QuicPacketNumber packet_number,
558 const QuicTransmissionInfo& transmission_info) const {
559 if (session_decides_what_to_write()) {
560 return packet_number;
561 }
562 QuicPacketNumber retransmission = transmission_info.retransmission;
563 while (retransmission.IsInitialized()) {
564 packet_number = retransmission;
565 retransmission =
566 unacked_packets_.GetTransmissionInfo(retransmission).retransmission;
567 }
568 return packet_number;
569}
570
571void QuicSentPacketManager::MarkPacketHandled(QuicPacketNumber packet_number,
572 QuicTransmissionInfo* info,
573 QuicTime::Delta ack_delay_time) {
574 QuicPacketNumber newest_transmission =
575 GetNewestRetransmission(packet_number, *info);
576 // Remove the most recent packet, if it is pending retransmission.
577 pending_retransmissions_.erase(newest_transmission);
578
579 if (newest_transmission == packet_number) {
580 // Try to aggregate acked stream frames if acked packet is not a
581 // retransmission.
582 const bool fast_path = session_decides_what_to_write() &&
583 info->transmission_type == NOT_RETRANSMISSION;
584 if (fast_path) {
585 unacked_packets_.MaybeAggregateAckedStreamFrame(*info, ack_delay_time);
586 } else {
587 if (session_decides_what_to_write()) {
588 unacked_packets_.NotifyAggregatedStreamFrameAcked(ack_delay_time);
589 }
590 const bool new_data_acked =
591 unacked_packets_.NotifyFramesAcked(*info, ack_delay_time);
592 if (session_decides_what_to_write() && !new_data_acked &&
593 info->transmission_type != NOT_RETRANSMISSION) {
594 // Record as a spurious retransmission if this packet is a
595 // retransmission and no new data gets acked.
596 QUIC_DVLOG(1) << "Detect spurious retransmitted packet "
597 << packet_number << " transmission type: "
598 << QuicUtils::TransmissionTypeToString(
599 info->transmission_type);
600 RecordSpuriousRetransmissions(*info, packet_number);
601 }
602 }
603 } else {
604 DCHECK(!session_decides_what_to_write());
605 RecordSpuriousRetransmissions(*info, packet_number);
606 // Remove the most recent packet from flight if it's a crypto handshake
607 // packet, since they won't be acked now that one has been processed.
608 // Other crypto handshake packets won't be in flight, only the newest
609 // transmission of a crypto packet is in flight at once.
610 // TODO(ianswett): Instead of handling all crypto packets special,
611 // only handle nullptr encrypted packets in a special way.
612 const QuicTransmissionInfo& newest_transmission_info =
613 unacked_packets_.GetTransmissionInfo(newest_transmission);
614 unacked_packets_.NotifyFramesAcked(newest_transmission_info,
615 ack_delay_time);
616 if (HasCryptoHandshake(newest_transmission_info)) {
617 unacked_packets_.RemoveFromInFlight(newest_transmission);
618 }
619 }
620
621 if (network_change_visitor_ != nullptr &&
622 info->bytes_sent > largest_mtu_acked_) {
623 largest_mtu_acked_ = info->bytes_sent;
624 network_change_visitor_->OnPathMtuIncreased(largest_mtu_acked_);
625 }
626 unacked_packets_.RemoveFromInFlight(info);
627 unacked_packets_.RemoveRetransmittability(info);
628 info->state = ACKED;
629}
630
631bool QuicSentPacketManager::OnPacketSent(
632 SerializedPacket* serialized_packet,
633 QuicPacketNumber original_packet_number,
634 QuicTime sent_time,
635 TransmissionType transmission_type,
636 HasRetransmittableData has_retransmittable_data) {
637 QuicPacketNumber packet_number = serialized_packet->packet_number;
638 DCHECK_LE(FirstSendingPacketNumber(), packet_number);
639 DCHECK(!unacked_packets_.IsUnacked(packet_number));
640 QUIC_BUG_IF(serialized_packet->encrypted_length == 0)
641 << "Cannot send empty packets.";
642
643 if (original_packet_number.IsInitialized()) {
644 pending_retransmissions_.erase(original_packet_number);
645 }
646
647 if (pending_timer_transmission_count_ > 0) {
648 --pending_timer_transmission_count_;
649 }
650
651 bool in_flight = has_retransmittable_data == HAS_RETRANSMITTABLE_DATA;
652 if (using_pacing_) {
653 pacing_sender_.OnPacketSent(
654 sent_time, unacked_packets_.bytes_in_flight(), packet_number,
655 serialized_packet->encrypted_length, has_retransmittable_data);
656 } else {
657 send_algorithm_->OnPacketSent(
658 sent_time, unacked_packets_.bytes_in_flight(), packet_number,
659 serialized_packet->encrypted_length, has_retransmittable_data);
660 }
661
662 unacked_packets_.AddSentPacket(serialized_packet, original_packet_number,
663 transmission_type, sent_time, in_flight);
664 // Reset the retransmission timer anytime a pending packet is sent.
665 return in_flight;
666}
667
668void QuicSentPacketManager::OnRetransmissionTimeout() {
669 DCHECK(unacked_packets_.HasInFlightPackets());
670 DCHECK_EQ(0u, pending_timer_transmission_count_);
671 // Handshake retransmission, timer based loss detection, TLP, and RTO are
672 // implemented with a single alarm. The handshake alarm is set when the
673 // handshake has not completed, the loss alarm is set when the loss detection
674 // algorithm says to, and the TLP and RTO alarms are set after that.
675 // The TLP alarm is always set to run for under an RTO.
676 switch (GetRetransmissionMode()) {
677 case HANDSHAKE_MODE:
678 ++stats_->crypto_retransmit_count;
679 RetransmitCryptoPackets();
680 return;
681 case LOSS_MODE: {
682 ++stats_->loss_timeout_count;
683 QuicByteCount prior_in_flight = unacked_packets_.bytes_in_flight();
684 const QuicTime now = clock_->Now();
685 InvokeLossDetection(now);
686 MaybeInvokeCongestionEvent(false, prior_in_flight, now);
687 return;
688 }
689 case TLP_MODE:
690 ++stats_->tlp_count;
691 ++consecutive_tlp_count_;
692 pending_timer_transmission_count_ = 1;
693 // TLPs prefer sending new data instead of retransmitting data, so
694 // give the connection a chance to write before completing the TLP.
695 return;
696 case RTO_MODE:
697 ++stats_->rto_count;
698 RetransmitRtoPackets();
699 return;
700 }
701}
702
703void QuicSentPacketManager::RetransmitCryptoPackets() {
704 DCHECK_EQ(HANDSHAKE_MODE, GetRetransmissionMode());
705 ++consecutive_crypto_retransmission_count_;
706 bool packet_retransmitted = false;
707 QuicPacketNumber packet_number = unacked_packets_.GetLeastUnacked();
708 std::vector<QuicPacketNumber> crypto_retransmissions;
709 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
710 it != unacked_packets_.end(); ++it, ++packet_number) {
711 // Only retransmit frames which are in flight, and therefore have been sent.
712 if (!it->in_flight ||
713 (session_decides_what_to_write() && it->state != OUTSTANDING) ||
714 !it->has_crypto_handshake ||
715 !unacked_packets_.HasRetransmittableFrames(*it)) {
716 continue;
717 }
718 packet_retransmitted = true;
719 if (session_decides_what_to_write()) {
720 crypto_retransmissions.push_back(packet_number);
721 } else {
722 MarkForRetransmission(packet_number, HANDSHAKE_RETRANSMISSION);
723 }
724 ++pending_timer_transmission_count_;
725 }
726 DCHECK(packet_retransmitted) << "No crypto packets found to retransmit.";
727 if (session_decides_what_to_write()) {
728 for (QuicPacketNumber retransmission : crypto_retransmissions) {
729 MarkForRetransmission(retransmission, HANDSHAKE_RETRANSMISSION);
730 }
731 }
732}
733
734bool QuicSentPacketManager::MaybeRetransmitTailLossProbe() {
735 if (pending_timer_transmission_count_ == 0) {
736 return false;
737 }
738 if (!MaybeRetransmitOldestPacket(TLP_RETRANSMISSION)) {
739 // If no tail loss probe can be sent, because there are no retransmittable
740 // packets, execute a conventional RTO to abandon old packets.
741 if (GetQuicReloadableFlag(quic_optimize_inflight_check)) {
742 QUIC_RELOADABLE_FLAG_COUNT(quic_optimize_inflight_check);
743 pending_timer_transmission_count_ = 0;
744 RetransmitRtoPackets();
745 }
746 return false;
747 }
748 return true;
749}
750
751bool QuicSentPacketManager::MaybeRetransmitOldestPacket(TransmissionType type) {
752 QuicPacketNumber packet_number = unacked_packets_.GetLeastUnacked();
753 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
754 it != unacked_packets_.end(); ++it, ++packet_number) {
755 // Only retransmit frames which are in flight, and therefore have been sent.
756 if (!it->in_flight ||
757 (session_decides_what_to_write() && it->state != OUTSTANDING) ||
758 !unacked_packets_.HasRetransmittableFrames(*it)) {
759 continue;
760 }
761 MarkForRetransmission(packet_number, type);
762 return true;
763 }
764 QUIC_DVLOG(1)
765 << "No retransmittable packets, so RetransmitOldestPacket failed.";
766 return false;
767}
768
769void QuicSentPacketManager::RetransmitRtoPackets() {
770 QUIC_BUG_IF(pending_timer_transmission_count_ > 0)
771 << "Retransmissions already queued:" << pending_timer_transmission_count_;
772 // Mark two packets for retransmission.
773 QuicPacketNumber packet_number = unacked_packets_.GetLeastUnacked();
774 std::vector<QuicPacketNumber> retransmissions;
775 for (QuicUnackedPacketMap::const_iterator it = unacked_packets_.begin();
776 it != unacked_packets_.end(); ++it, ++packet_number) {
777 if ((!session_decides_what_to_write() || it->state == OUTSTANDING) &&
778 unacked_packets_.HasRetransmittableFrames(*it) &&
779 pending_timer_transmission_count_ < max_rto_packets_) {
780 if (session_decides_what_to_write()) {
781 retransmissions.push_back(packet_number);
782 } else {
783 MarkForRetransmission(packet_number, RTO_RETRANSMISSION);
784 }
785 ++pending_timer_transmission_count_;
786 }
787 // Abandon non-retransmittable data that's in flight to ensure it doesn't
788 // fill up the congestion window.
789 bool has_retransmissions = it->retransmission.IsInitialized();
790 if (session_decides_what_to_write()) {
791 has_retransmissions = it->state != OUTSTANDING;
792 }
793 if (it->in_flight && !has_retransmissions &&
794 !unacked_packets_.HasRetransmittableFrames(*it)) {
795 // Log only for non-retransmittable data.
796 // Retransmittable data is marked as lost during loss detection, and will
797 // be logged later.
798 unacked_packets_.RemoveFromInFlight(packet_number);
799 if (debug_delegate_ != nullptr) {
800 debug_delegate_->OnPacketLoss(packet_number, RTO_RETRANSMISSION,
801 clock_->Now());
802 }
803 }
804 }
805 if (pending_timer_transmission_count_ > 0) {
806 if (consecutive_rto_count_ == 0) {
807 first_rto_transmission_ = unacked_packets_.largest_sent_packet() + 1;
808 }
809 ++consecutive_rto_count_;
810 }
811 if (session_decides_what_to_write()) {
812 for (QuicPacketNumber retransmission : retransmissions) {
813 MarkForRetransmission(retransmission, RTO_RETRANSMISSION);
814 }
815 }
816}
817
818QuicSentPacketManager::RetransmissionTimeoutMode
819QuicSentPacketManager::GetRetransmissionMode() const {
820 DCHECK(unacked_packets_.HasInFlightPackets());
821 if (!handshake_confirmed_ && unacked_packets_.HasPendingCryptoPackets()) {
822 return HANDSHAKE_MODE;
823 }
824 if (loss_algorithm_->GetLossTimeout() != QuicTime::Zero()) {
825 return LOSS_MODE;
826 }
827 if (consecutive_tlp_count_ < max_tail_loss_probes_) {
828 if (GetQuicReloadableFlag(quic_optimize_inflight_check) ||
829 unacked_packets_.HasUnackedRetransmittableFrames()) {
830 return TLP_MODE;
831 }
832 }
833 return RTO_MODE;
834}
835
836void QuicSentPacketManager::InvokeLossDetection(QuicTime time) {
837 if (!packets_acked_.empty()) {
838 DCHECK_LE(packets_acked_.front().packet_number,
839 packets_acked_.back().packet_number);
840 largest_newly_acked_ = packets_acked_.back().packet_number;
841 }
842 loss_algorithm_->DetectLosses(unacked_packets_, time, rtt_stats_,
843 largest_newly_acked_, packets_acked_,
844 &packets_lost_);
845 for (const LostPacket& packet : packets_lost_) {
846 ++stats_->packets_lost;
847 if (debug_delegate_ != nullptr) {
848 debug_delegate_->OnPacketLoss(packet.packet_number, LOSS_RETRANSMISSION,
849 time);
850 }
851
852 MarkForRetransmission(packet.packet_number, LOSS_RETRANSMISSION);
853 }
854}
855
856bool QuicSentPacketManager::MaybeUpdateRTT(QuicPacketNumber largest_acked,
857 QuicTime::Delta ack_delay_time,
858 QuicTime ack_receive_time) {
859 // We rely on ack_delay_time to compute an RTT estimate, so we
860 // only update rtt when the largest observed gets acked.
861 if (!unacked_packets_.IsUnacked(largest_acked)) {
862 return false;
863 }
864 // We calculate the RTT based on the highest ACKed packet number, the lower
865 // packet numbers will include the ACK aggregation delay.
866 const QuicTransmissionInfo& transmission_info =
867 unacked_packets_.GetTransmissionInfo(largest_acked);
868 // Ensure the packet has a valid sent time.
869 if (transmission_info.sent_time == QuicTime::Zero()) {
870 QUIC_BUG << "Acked packet has zero sent time, largest_acked:"
871 << largest_acked;
872 return false;
873 }
874 if (transmission_info.sent_time > ack_receive_time) {
875 QUIC_CODE_COUNT(quic_receive_acked_before_sending);
876 }
877
878 QuicTime::Delta send_delta = ack_receive_time - transmission_info.sent_time;
879 rtt_stats_.UpdateRtt(send_delta, ack_delay_time, ack_receive_time);
880
881 return true;
882}
883
884QuicTime::Delta QuicSentPacketManager::TimeUntilSend(QuicTime now) const {
885 // The TLP logic is entirely contained within QuicSentPacketManager, so the
886 // send algorithm does not need to be consulted.
887 if (pending_timer_transmission_count_ > 0) {
888 return QuicTime::Delta::Zero();
889 }
890
891 if (using_pacing_) {
892 return pacing_sender_.TimeUntilSend(now,
893 unacked_packets_.bytes_in_flight());
894 }
895
896 return send_algorithm_->CanSend(unacked_packets_.bytes_in_flight())
897 ? QuicTime::Delta::Zero()
898 : QuicTime::Delta::Infinite();
899}
900
901const QuicTime QuicSentPacketManager::GetRetransmissionTime() const {
902 // Don't set the timer if there is nothing to retransmit or we've already
903 // queued a tlp transmission and it hasn't been sent yet.
904 if (!unacked_packets_.HasInFlightPackets() ||
905 pending_timer_transmission_count_ > 0) {
906 return QuicTime::Zero();
907 }
908 if (!GetQuicReloadableFlag(quic_optimize_inflight_check) &&
909 !unacked_packets_.HasUnackedRetransmittableFrames()) {
910 return QuicTime::Zero();
911 }
912 switch (GetRetransmissionMode()) {
913 case HANDSHAKE_MODE:
914 return unacked_packets_.GetLastCryptoPacketSentTime() +
915 GetCryptoRetransmissionDelay();
916 case LOSS_MODE:
917 return loss_algorithm_->GetLossTimeout();
918 case TLP_MODE: {
919 // TODO(ianswett): When CWND is available, it would be preferable to
920 // set the timer based on the earliest retransmittable packet.
921 // Base the updated timer on the send time of the last packet.
922 const QuicTime sent_time = unacked_packets_.GetLastPacketSentTime();
923 const QuicTime tlp_time = sent_time + GetTailLossProbeDelay();
924 // Ensure the TLP timer never gets set to a time in the past.
925 return std::max(clock_->ApproximateNow(), tlp_time);
926 }
927 case RTO_MODE: {
928 // The RTO is based on the first outstanding packet.
929 const QuicTime sent_time = unacked_packets_.GetLastPacketSentTime();
930 QuicTime rto_time = sent_time + GetRetransmissionDelay();
931 // Wait for TLP packets to be acked before an RTO fires.
932 QuicTime tlp_time =
933 unacked_packets_.GetLastPacketSentTime() + GetTailLossProbeDelay();
934 return std::max(tlp_time, rto_time);
935 }
936 }
937 DCHECK(false);
938 return QuicTime::Zero();
939}
940
941const QuicTime::Delta QuicSentPacketManager::GetPathDegradingDelay() const {
942 QuicTime::Delta delay = QuicTime::Delta::Zero();
943 for (size_t i = 0; i < max_tail_loss_probes_; ++i) {
944 delay = delay + GetTailLossProbeDelay(i);
945 }
946 for (size_t i = 0; i < kNumRetransmissionDelaysForPathDegradingDelay; ++i) {
947 delay = delay + GetRetransmissionDelay(i);
948 }
949 return delay;
950}
951
952const QuicTime::Delta QuicSentPacketManager::GetCryptoRetransmissionDelay()
953 const {
954 // This is equivalent to the TailLossProbeDelay, but slightly more aggressive
955 // because crypto handshake messages don't incur a delayed ack time.
956 QuicTime::Delta srtt = rtt_stats_.SmoothedOrInitialRtt();
957 int64_t delay_ms;
958 if (conservative_handshake_retransmits_) {
959 // Using the delayed ack time directly could cause conservative handshake
960 // retransmissions to actually be more aggressive than the default.
961 delay_ms = std::max(delayed_ack_time_.ToMilliseconds(),
962 static_cast<int64_t>(2 * srtt.ToMilliseconds()));
963 } else {
964 delay_ms = std::max(kMinHandshakeTimeoutMs,
965 static_cast<int64_t>(1.5 * srtt.ToMilliseconds()));
966 }
967 return QuicTime::Delta::FromMilliseconds(
968 delay_ms << consecutive_crypto_retransmission_count_);
969}
970
971const QuicTime::Delta QuicSentPacketManager::GetTailLossProbeDelay(
972 size_t consecutive_tlp_count) const {
973 QuicTime::Delta srtt = rtt_stats_.SmoothedOrInitialRtt();
974 if (enable_half_rtt_tail_loss_probe_ && consecutive_tlp_count == 0u) {
975 return std::max(min_tlp_timeout_, srtt * 0.5);
976 }
977 if (ietf_style_tlp_) {
978 return std::max(min_tlp_timeout_, 1.5 * srtt + rtt_stats_.max_ack_delay());
979 }
980 if (ietf_style_2x_tlp_) {
981 return std::max(min_tlp_timeout_, 2 * srtt + rtt_stats_.max_ack_delay());
982 }
983 if (!unacked_packets_.HasMultipleInFlightPackets()) {
984 // This expression really should be using the delayed ack time, but in TCP
985 // MinRTO was traditionally set to 2x the delayed ack timer and this
986 // expression assumed QUIC did the same.
987 return std::max(2 * srtt, 1.5 * srtt + (min_rto_timeout_ * 0.5));
988 }
989 return std::max(min_tlp_timeout_, 2 * srtt);
990}
991
992const QuicTime::Delta QuicSentPacketManager::GetRetransmissionDelay(
993 size_t consecutive_rto_count) const {
994 QuicTime::Delta retransmission_delay = QuicTime::Delta::Zero();
995 if (rtt_stats_.smoothed_rtt().IsZero()) {
996 // We are in the initial state, use default timeout values.
997 retransmission_delay =
998 QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs);
999 } else {
1000 retransmission_delay =
1001 rtt_stats_.smoothed_rtt() + 4 * rtt_stats_.mean_deviation();
1002 if (retransmission_delay < min_rto_timeout_) {
1003 retransmission_delay = min_rto_timeout_;
1004 }
1005 }
1006
1007 // Calculate exponential back off.
1008 retransmission_delay =
1009 retransmission_delay *
1010 (1 << std::min<size_t>(consecutive_rto_count, kMaxRetransmissions));
1011
1012 if (retransmission_delay.ToMilliseconds() > kMaxRetransmissionTimeMs) {
1013 return QuicTime::Delta::FromMilliseconds(kMaxRetransmissionTimeMs);
1014 }
1015 return retransmission_delay;
1016}
1017
wub967ba572019-04-01 09:27:52 -07001018QuicTime::Delta QuicSentPacketManager::GetSlowStartDuration() const {
1019 if (send_algorithm_->GetCongestionControlType() != kBBR) {
1020 return QuicTime::Delta::Infinite();
1021 }
1022
1023 if (!send_algorithm_->InSlowStart()) {
1024 return stats_->slowstart_duration;
1025 }
1026
1027 return clock_->ApproximateNow() - stats_->slowstart_start_time +
1028 stats_->slowstart_duration;
1029}
1030
vasilvvc48c8712019-03-11 13:38:16 -07001031std::string QuicSentPacketManager::GetDebugState() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -05001032 return send_algorithm_->GetDebugState();
1033}
1034
1035void QuicSentPacketManager::CancelRetransmissionsForStream(
1036 QuicStreamId stream_id) {
1037 if (session_decides_what_to_write()) {
1038 return;
1039 }
1040 unacked_packets_.CancelRetransmissionsForStream(stream_id);
1041 auto it = pending_retransmissions_.begin();
1042 while (it != pending_retransmissions_.end()) {
1043 if (unacked_packets_.HasRetransmittableFrames(it->first)) {
1044 ++it;
1045 continue;
1046 }
1047 it = pending_retransmissions_.erase(it);
1048 }
1049}
1050
1051void QuicSentPacketManager::SetSendAlgorithm(
1052 CongestionControlType congestion_control_type) {
1053 SetSendAlgorithm(SendAlgorithmInterface::Create(
1054 clock_, &rtt_stats_, &unacked_packets_, congestion_control_type,
1055 QuicRandom::GetInstance(), stats_, initial_congestion_window_));
1056}
1057
1058void QuicSentPacketManager::SetSendAlgorithm(
1059 SendAlgorithmInterface* send_algorithm) {
1060 send_algorithm_.reset(send_algorithm);
1061 pacing_sender_.set_sender(send_algorithm);
1062}
1063
1064void QuicSentPacketManager::OnConnectionMigration(AddressChangeType type) {
1065 if (type == PORT_CHANGE || type == IPV4_SUBNET_CHANGE) {
1066 // Rtt and cwnd do not need to be reset when the peer address change is
1067 // considered to be caused by NATs.
1068 return;
1069 }
1070 consecutive_rto_count_ = 0;
1071 consecutive_tlp_count_ = 0;
1072 rtt_stats_.OnConnectionMigration();
1073 send_algorithm_->OnConnectionMigration();
1074}
1075
1076void QuicSentPacketManager::OnAckFrameStart(QuicPacketNumber largest_acked,
1077 QuicTime::Delta ack_delay_time,
1078 QuicTime ack_receive_time) {
1079 DCHECK(packets_acked_.empty());
1080 DCHECK_LE(largest_acked, unacked_packets_.largest_sent_packet());
1081 rtt_updated_ =
1082 MaybeUpdateRTT(largest_acked, ack_delay_time, ack_receive_time);
1083 DCHECK(!unacked_packets_.largest_acked().IsInitialized() ||
QUICHE team9929cc42019-03-13 08:17:43 -07001084 largest_acked >= unacked_packets_.largest_acked() ||
1085 tolerate_reneging_);
QUICHE teama6ef0a62019-03-07 20:34:33 -05001086 last_ack_frame_.ack_delay_time = ack_delay_time;
1087 acked_packets_iter_ = last_ack_frame_.packets.rbegin();
1088}
1089
1090void QuicSentPacketManager::OnAckRange(QuicPacketNumber start,
1091 QuicPacketNumber end) {
1092 if (!last_ack_frame_.largest_acked.IsInitialized() ||
1093 end > last_ack_frame_.largest_acked + 1) {
1094 // Largest acked increases.
1095 unacked_packets_.IncreaseLargestAcked(end - 1);
1096 last_ack_frame_.largest_acked = end - 1;
1097 }
1098 // Drop ack ranges which ack packets below least_unacked.
1099 QuicPacketNumber least_unacked = unacked_packets_.GetLeastUnacked();
1100 if (least_unacked.IsInitialized() && end <= least_unacked) {
1101 return;
1102 }
1103 start = std::max(start, least_unacked);
1104 do {
1105 QuicPacketNumber newly_acked_start = start;
1106 if (acked_packets_iter_ != last_ack_frame_.packets.rend()) {
1107 newly_acked_start = std::max(start, acked_packets_iter_->max());
1108 }
1109 for (QuicPacketNumber acked = end - 1; acked >= newly_acked_start;
1110 --acked) {
1111 // Check if end is above the current range. If so add newly acked packets
1112 // in descending order.
1113 packets_acked_.push_back(AckedPacket(acked, 0, QuicTime::Zero()));
1114 if (acked == FirstSendingPacketNumber()) {
1115 break;
1116 }
1117 }
1118 if (acked_packets_iter_ == last_ack_frame_.packets.rend() ||
1119 start > acked_packets_iter_->min()) {
1120 // Finish adding all newly acked packets.
1121 return;
1122 }
1123 end = std::min(end, acked_packets_iter_->min());
1124 ++acked_packets_iter_;
1125 } while (start < end);
1126}
1127
1128void QuicSentPacketManager::OnAckTimestamp(QuicPacketNumber packet_number,
1129 QuicTime timestamp) {
1130 last_ack_frame_.received_packet_times.push_back({packet_number, timestamp});
1131 for (AckedPacket& packet : packets_acked_) {
1132 if (packet.packet_number == packet_number) {
1133 packet.receive_timestamp = timestamp;
1134 return;
1135 }
1136 }
1137}
1138
1139bool QuicSentPacketManager::OnAckFrameEnd(QuicTime ack_receive_time) {
1140 QuicByteCount prior_bytes_in_flight = unacked_packets_.bytes_in_flight();
1141 // Reverse packets_acked_ so that it is in ascending order.
1142 reverse(packets_acked_.begin(), packets_acked_.end());
1143 for (AckedPacket& acked_packet : packets_acked_) {
1144 QuicTransmissionInfo* info =
1145 unacked_packets_.GetMutableTransmissionInfo(acked_packet.packet_number);
1146 if (!QuicUtils::IsAckable(info->state)) {
1147 if (info->state == ACKED) {
1148 QUIC_BUG << "Trying to ack an already acked packet: "
1149 << acked_packet.packet_number
1150 << ", last_ack_frame_: " << last_ack_frame_
1151 << ", least_unacked: " << unacked_packets_.GetLeastUnacked()
1152 << ", packets_acked_: " << packets_acked_;
1153 } else {
1154 QUIC_PEER_BUG << "Received ack for unackable packet: "
1155 << acked_packet.packet_number << " with state: "
1156 << QuicUtils::SentPacketStateToString(info->state);
1157 }
1158 continue;
1159 }
1160 QUIC_DVLOG(1) << ENDPOINT << "Got an ack for packet "
1161 << acked_packet.packet_number;
1162 last_ack_frame_.packets.Add(acked_packet.packet_number);
QUICHE teamc264e362019-03-19 14:21:06 -07001163 largest_packet_peer_knows_is_acked_.UpdateMax(info->largest_acked);
QUICHE teamc279cec2019-03-22 06:51:48 -07001164 if (supports_multiple_packet_number_spaces()) {
1165 largest_packets_peer_knows_is_acked_[QuicUtils::GetPacketNumberSpace(
1166 info->encryption_level)]
1167 .UpdateMax(info->largest_acked);
1168 }
QUICHE teama6ef0a62019-03-07 20:34:33 -05001169 // If data is associated with the most recent transmission of this
1170 // packet, then inform the caller.
1171 if (info->in_flight) {
1172 acked_packet.bytes_acked = info->bytes_sent;
1173 } else {
1174 // Unackable packets are skipped earlier.
1175 largest_newly_acked_ = acked_packet.packet_number;
1176 }
1177 if (unacked_packets_.use_uber_loss_algorithm()) {
1178 unacked_packets_.MaybeUpdateLargestAckedOfPacketNumberSpace(
1179 info->encryption_level, acked_packet.packet_number);
1180 }
1181 MarkPacketHandled(acked_packet.packet_number, info,
1182 last_ack_frame_.ack_delay_time);
1183 }
1184 const bool acked_new_packet = !packets_acked_.empty();
1185 PostProcessAfterMarkingPacketHandled(last_ack_frame_, ack_receive_time,
1186 rtt_updated_, prior_bytes_in_flight);
1187
1188 return acked_new_packet;
1189}
1190
1191void QuicSentPacketManager::SetDebugDelegate(DebugDelegate* debug_delegate) {
1192 debug_delegate_ = debug_delegate;
1193}
1194
1195void QuicSentPacketManager::OnApplicationLimited() {
1196 if (using_pacing_) {
1197 pacing_sender_.OnApplicationLimited();
1198 }
1199 send_algorithm_->OnApplicationLimited(unacked_packets_.bytes_in_flight());
1200 if (debug_delegate_ != nullptr) {
1201 debug_delegate_->OnApplicationLimited();
1202 }
1203}
1204
1205QuicTime QuicSentPacketManager::GetNextReleaseTime() const {
1206 return using_pacing_ ? pacing_sender_.ideal_next_packet_send_time()
1207 : QuicTime::Zero();
1208}
1209
1210void QuicSentPacketManager::SetInitialRtt(QuicTime::Delta rtt) {
1211 const QuicTime::Delta min_rtt =
1212 QuicTime::Delta::FromMicroseconds(kMinInitialRoundTripTimeUs);
1213 const QuicTime::Delta max_rtt =
1214 QuicTime::Delta::FromMicroseconds(kMaxInitialRoundTripTimeUs);
1215 rtt_stats_.set_initial_rtt(std::max(min_rtt, std::min(max_rtt, rtt)));
1216}
1217
QUICHE teamc279cec2019-03-22 06:51:48 -07001218void QuicSentPacketManager::EnableMultiplePacketNumberSpacesSupport() {
1219 unacked_packets_.EnableMultiplePacketNumberSpacesSupport();
1220}
1221
1222QuicPacketNumber QuicSentPacketManager::GetLargestAckedPacket(
1223 EncryptionLevel decrypted_packet_level) const {
1224 DCHECK(supports_multiple_packet_number_spaces());
1225 return unacked_packets_.GetLargestAckedOfPacketNumberSpace(
1226 QuicUtils::GetPacketNumberSpace(decrypted_packet_level));
1227}
1228
1229QuicPacketNumber QuicSentPacketManager::GetLargestSentPacket(
1230 EncryptionLevel decrypted_packet_level) const {
1231 DCHECK(supports_multiple_packet_number_spaces());
1232 return unacked_packets_.GetLargestSentPacketOfPacketNumberSpace(
1233 decrypted_packet_level);
1234}
1235
1236QuicPacketNumber QuicSentPacketManager::GetLargestPacketPeerKnowsIsAcked(
1237 EncryptionLevel decrypted_packet_level) const {
1238 DCHECK(supports_multiple_packet_number_spaces());
1239 return largest_packets_peer_knows_is_acked_[QuicUtils::GetPacketNumberSpace(
1240 decrypted_packet_level)];
1241}
1242
QUICHE teama6ef0a62019-03-07 20:34:33 -05001243#undef ENDPOINT // undef for jumbo builds
1244} // namespace quic