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 "quiche/quic/core/quic_sent_packet_manager.h"
6
7 #include <algorithm>
8 #include <cstddef>
9 #include <string>
10
11 #include "quiche/quic/core/congestion_control/general_loss_algorithm.h"
12 #include "quiche/quic/core/congestion_control/pacing_sender.h"
13 #include "quiche/quic/core/congestion_control/send_algorithm_interface.h"
14 #include "quiche/quic/core/crypto/crypto_protocol.h"
15 #include "quiche/quic/core/frames/quic_ack_frequency_frame.h"
16 #include "quiche/quic/core/proto/cached_network_parameters_proto.h"
17 #include "quiche/quic/core/quic_connection_stats.h"
18 #include "quiche/quic/core/quic_constants.h"
19 #include "quiche/quic/core/quic_packet_number.h"
20 #include "quiche/quic/core/quic_transmission_info.h"
21 #include "quiche/quic/core/quic_types.h"
22 #include "quiche/quic/core/quic_utils.h"
23 #include "quiche/quic/platform/api/quic_bug_tracker.h"
24 #include "quiche/quic/platform/api/quic_flag_utils.h"
25 #include "quiche/quic/platform/api/quic_flags.h"
26 #include "quiche/quic/platform/api/quic_logging.h"
27 #include "quiche/common/print_elements.h"
28
29 namespace quic {
30
31 namespace {
32 static const int64_t kDefaultRetransmissionTimeMs = 500;
33
34 // Ensure the handshake timer isnt't faster than 10ms.
35 // This limits the tenth retransmitted packet to 10s after the initial CHLO.
36 static const int64_t kMinHandshakeTimeoutMs = 10;
37
38 // Sends up to two tail loss probes before firing an RTO,
39 // per draft RFC draft-dukkipati-tcpm-tcp-loss-probe.
40 static const size_t kDefaultMaxTailLossProbes = 2;
41
42 // The multiplier for calculating PTO timeout before any RTT sample is
43 // available.
44 static const float kPtoMultiplierWithoutRttSamples = 3;
45
46 // Returns true of retransmissions of the specified type should retransmit
47 // the frames directly (as opposed to resulting in a loss notification).
ShouldForceRetransmission(TransmissionType transmission_type)48 inline bool ShouldForceRetransmission(TransmissionType transmission_type) {
49 return transmission_type == HANDSHAKE_RETRANSMISSION ||
50 transmission_type == PTO_RETRANSMISSION;
51 }
52
53 // If pacing rate is accurate, > 2 burst token is not likely to help first ACK
54 // to arrive earlier, and overly large burst token could cause incast packet
55 // losses.
56 static const uint32_t kConservativeUnpacedBurst = 2;
57
58 // The default number of PTOs to trigger path degrading.
59 static const uint32_t kNumProbeTimeoutsForPathDegradingDelay = 4;
60
61 } // namespace
62
63 #define ENDPOINT \
64 (unacked_packets_.perspective() == Perspective::IS_SERVER ? "Server: " \
65 : "Client: ")
66
QuicSentPacketManager(Perspective perspective,const QuicClock * clock,QuicRandom * random,QuicConnectionStats * stats,CongestionControlType congestion_control_type)67 QuicSentPacketManager::QuicSentPacketManager(
68 Perspective perspective, const QuicClock* clock, QuicRandom* random,
69 QuicConnectionStats* stats, CongestionControlType congestion_control_type)
70 : unacked_packets_(perspective),
71 clock_(clock),
72 random_(random),
73 stats_(stats),
74 debug_delegate_(nullptr),
75 network_change_visitor_(nullptr),
76 initial_congestion_window_(kInitialCongestionWindow),
77 loss_algorithm_(&uber_loss_algorithm_),
78 consecutive_crypto_retransmission_count_(0),
79 pending_timer_transmission_count_(0),
80 using_pacing_(false),
81 conservative_handshake_retransmits_(false),
82 largest_mtu_acked_(0),
83 handshake_finished_(false),
84 peer_max_ack_delay_(
85 QuicTime::Delta::FromMilliseconds(kDefaultDelayedAckTimeMs)),
86 rtt_updated_(false),
87 acked_packets_iter_(last_ack_frame_.packets.rbegin()),
88 consecutive_pto_count_(0),
89 handshake_mode_disabled_(false),
90 handshake_packet_acked_(false),
91 zero_rtt_packet_acked_(false),
92 one_rtt_packet_acked_(false),
93 num_ptos_for_path_degrading_(kNumProbeTimeoutsForPathDegradingDelay),
94 ignore_pings_(false),
95 ignore_ack_delay_(false) {
96 SetSendAlgorithm(congestion_control_type);
97 }
98
~QuicSentPacketManager()99 QuicSentPacketManager::~QuicSentPacketManager() {}
100
SetFromConfig(const QuicConfig & config)101 void QuicSentPacketManager::SetFromConfig(const QuicConfig& config) {
102 const Perspective perspective = unacked_packets_.perspective();
103 if (config.HasReceivedInitialRoundTripTimeUs() &&
104 config.ReceivedInitialRoundTripTimeUs() > 0) {
105 if (!config.HasClientSentConnectionOption(kNRTT, perspective)) {
106 SetInitialRtt(QuicTime::Delta::FromMicroseconds(
107 config.ReceivedInitialRoundTripTimeUs()),
108 /*trusted=*/false);
109 }
110 } else if (config.HasInitialRoundTripTimeUsToSend() &&
111 config.GetInitialRoundTripTimeUsToSend() > 0) {
112 SetInitialRtt(QuicTime::Delta::FromMicroseconds(
113 config.GetInitialRoundTripTimeUsToSend()),
114 /*trusted=*/false);
115 }
116 if (config.HasReceivedMaxAckDelayMs()) {
117 peer_max_ack_delay_ =
118 QuicTime::Delta::FromMilliseconds(config.ReceivedMaxAckDelayMs());
119 }
120 if (GetQuicReloadableFlag(quic_can_send_ack_frequency) &&
121 perspective == Perspective::IS_SERVER) {
122 if (config.HasReceivedMinAckDelayMs()) {
123 peer_min_ack_delay_ =
124 QuicTime::Delta::FromMilliseconds(config.ReceivedMinAckDelayMs());
125 }
126 if (config.HasClientSentConnectionOption(kAFF1, perspective)) {
127 use_smoothed_rtt_in_ack_delay_ = true;
128 }
129 }
130 if (config.HasClientSentConnectionOption(kMAD0, perspective)) {
131 ignore_ack_delay_ = true;
132 }
133
134 // Configure congestion control.
135 if (config.HasClientRequestedIndependentOption(kTBBR, perspective)) {
136 SetSendAlgorithm(kBBR);
137 }
138 if (GetQuicReloadableFlag(quic_allow_client_enabled_bbr_v2) &&
139 config.HasClientRequestedIndependentOption(kB2ON, perspective)) {
140 QUIC_RELOADABLE_FLAG_COUNT(quic_allow_client_enabled_bbr_v2);
141 SetSendAlgorithm(kBBRv2);
142 }
143
144 if (config.HasClientRequestedIndependentOption(kRENO, perspective)) {
145 SetSendAlgorithm(kRenoBytes);
146 } else if (config.HasClientRequestedIndependentOption(kBYTE, perspective) ||
147 (GetQuicReloadableFlag(quic_default_to_bbr) &&
148 config.HasClientRequestedIndependentOption(kQBIC, perspective))) {
149 SetSendAlgorithm(kCubicBytes);
150 }
151
152 // Initial window.
153 if (config.HasClientRequestedIndependentOption(kIW03, perspective)) {
154 initial_congestion_window_ = 3;
155 send_algorithm_->SetInitialCongestionWindowInPackets(3);
156 }
157 if (config.HasClientRequestedIndependentOption(kIW10, perspective)) {
158 initial_congestion_window_ = 10;
159 send_algorithm_->SetInitialCongestionWindowInPackets(10);
160 }
161 if (config.HasClientRequestedIndependentOption(kIW20, perspective)) {
162 initial_congestion_window_ = 20;
163 send_algorithm_->SetInitialCongestionWindowInPackets(20);
164 }
165 if (config.HasClientRequestedIndependentOption(kIW50, perspective)) {
166 initial_congestion_window_ = 50;
167 send_algorithm_->SetInitialCongestionWindowInPackets(50);
168 }
169 if (config.HasClientRequestedIndependentOption(kBWS5, perspective)) {
170 initial_congestion_window_ = 10;
171 send_algorithm_->SetInitialCongestionWindowInPackets(10);
172 }
173
174 if (config.HasClientRequestedIndependentOption(kIGNP, perspective)) {
175 ignore_pings_ = true;
176 }
177
178 using_pacing_ = !GetQuicFlag(quic_disable_pacing_for_perf_tests);
179 // Configure loss detection.
180 if (config.HasClientRequestedIndependentOption(kILD0, perspective)) {
181 uber_loss_algorithm_.SetReorderingShift(kDefaultIetfLossDelayShift);
182 uber_loss_algorithm_.DisableAdaptiveReorderingThreshold();
183 }
184 if (config.HasClientRequestedIndependentOption(kILD1, perspective)) {
185 uber_loss_algorithm_.SetReorderingShift(kDefaultLossDelayShift);
186 uber_loss_algorithm_.DisableAdaptiveReorderingThreshold();
187 }
188 if (config.HasClientRequestedIndependentOption(kILD2, perspective)) {
189 uber_loss_algorithm_.EnableAdaptiveReorderingThreshold();
190 uber_loss_algorithm_.SetReorderingShift(kDefaultIetfLossDelayShift);
191 }
192 if (config.HasClientRequestedIndependentOption(kILD3, perspective)) {
193 uber_loss_algorithm_.SetReorderingShift(kDefaultLossDelayShift);
194 uber_loss_algorithm_.EnableAdaptiveReorderingThreshold();
195 }
196 if (config.HasClientRequestedIndependentOption(kILD4, perspective)) {
197 uber_loss_algorithm_.SetReorderingShift(kDefaultLossDelayShift);
198 uber_loss_algorithm_.EnableAdaptiveReorderingThreshold();
199 uber_loss_algorithm_.EnableAdaptiveTimeThreshold();
200 }
201 if (config.HasClientRequestedIndependentOption(kRUNT, perspective)) {
202 uber_loss_algorithm_.DisablePacketThresholdForRuntPackets();
203 }
204 if (config.HasClientSentConnectionOption(kCONH, perspective)) {
205 conservative_handshake_retransmits_ = true;
206 }
207 send_algorithm_->SetFromConfig(config, perspective);
208 loss_algorithm_->SetFromConfig(config, perspective);
209
210 if (network_change_visitor_ != nullptr) {
211 network_change_visitor_->OnCongestionChange();
212 }
213
214 if (debug_delegate_ != nullptr) {
215 DebugDelegate::SendParameters parameters;
216 parameters.congestion_control_type =
217 send_algorithm_->GetCongestionControlType();
218 parameters.use_pacing = using_pacing_;
219 parameters.initial_congestion_window = initial_congestion_window_;
220 debug_delegate_->OnConfigProcessed(parameters);
221 }
222 }
223
ApplyConnectionOptions(const QuicTagVector & connection_options)224 void QuicSentPacketManager::ApplyConnectionOptions(
225 const QuicTagVector& connection_options) {
226 absl::optional<CongestionControlType> cc_type;
227 if (ContainsQuicTag(connection_options, kB2ON)) {
228 cc_type = kBBRv2;
229 } else if (ContainsQuicTag(connection_options, kTBBR)) {
230 cc_type = kBBR;
231 } else if (ContainsQuicTag(connection_options, kRENO)) {
232 cc_type = kRenoBytes;
233 } else if (ContainsQuicTag(connection_options, kQBIC)) {
234 cc_type = kCubicBytes;
235 }
236
237 if (cc_type.has_value()) {
238 SetSendAlgorithm(*cc_type);
239 }
240
241 send_algorithm_->ApplyConnectionOptions(connection_options);
242 }
243
ResumeConnectionState(const CachedNetworkParameters & cached_network_params,bool max_bandwidth_resumption)244 void QuicSentPacketManager::ResumeConnectionState(
245 const CachedNetworkParameters& cached_network_params,
246 bool max_bandwidth_resumption) {
247 QuicBandwidth bandwidth = QuicBandwidth::FromBytesPerSecond(
248 max_bandwidth_resumption
249 ? cached_network_params.max_bandwidth_estimate_bytes_per_second()
250 : cached_network_params.bandwidth_estimate_bytes_per_second());
251 QuicTime::Delta rtt =
252 QuicTime::Delta::FromMilliseconds(cached_network_params.min_rtt_ms());
253 // This calls the old AdjustNetworkParameters interface, and fills certain
254 // fields in SendAlgorithmInterface::NetworkParams
255 // (e.g., quic_bbr_fix_pacing_rate) using GFE flags.
256 SendAlgorithmInterface::NetworkParams params(
257 bandwidth, rtt, /*allow_cwnd_to_decrease = */ false);
258 // The rtt is trusted because it's a min_rtt measured from a previous
259 // connection with the same network path between client and server.
260 params.is_rtt_trusted = true;
261 AdjustNetworkParameters(params);
262 }
263
AdjustNetworkParameters(const SendAlgorithmInterface::NetworkParams & params)264 void QuicSentPacketManager::AdjustNetworkParameters(
265 const SendAlgorithmInterface::NetworkParams& params) {
266 const QuicBandwidth& bandwidth = params.bandwidth;
267 const QuicTime::Delta& rtt = params.rtt;
268
269 if (!rtt.IsZero()) {
270 if (params.is_rtt_trusted) {
271 // Always set initial rtt if it's trusted.
272 SetInitialRtt(rtt, /*trusted=*/true);
273 } else if (rtt_stats_.initial_rtt() ==
274 QuicTime::Delta::FromMilliseconds(kInitialRttMs)) {
275 // Only set initial rtt if we are using the default. This avoids
276 // overwriting a trusted initial rtt by an untrusted one.
277 SetInitialRtt(rtt, /*trusted=*/false);
278 }
279 }
280
281 const QuicByteCount old_cwnd = send_algorithm_->GetCongestionWindow();
282 if (GetQuicReloadableFlag(quic_conservative_bursts) && using_pacing_ &&
283 !bandwidth.IsZero()) {
284 QUIC_RELOADABLE_FLAG_COUNT(quic_conservative_bursts);
285 pacing_sender_.SetBurstTokens(kConservativeUnpacedBurst);
286 }
287 send_algorithm_->AdjustNetworkParameters(params);
288 if (debug_delegate_ != nullptr) {
289 debug_delegate_->OnAdjustNetworkParameters(
290 bandwidth, rtt.IsZero() ? rtt_stats_.MinOrInitialRtt() : rtt, old_cwnd,
291 send_algorithm_->GetCongestionWindow());
292 }
293 }
294
SetLossDetectionTuner(std::unique_ptr<LossDetectionTunerInterface> tuner)295 void QuicSentPacketManager::SetLossDetectionTuner(
296 std::unique_ptr<LossDetectionTunerInterface> tuner) {
297 uber_loss_algorithm_.SetLossDetectionTuner(std::move(tuner));
298 }
299
OnConfigNegotiated()300 void QuicSentPacketManager::OnConfigNegotiated() {
301 loss_algorithm_->OnConfigNegotiated();
302 }
303
OnConnectionClosed()304 void QuicSentPacketManager::OnConnectionClosed() {
305 loss_algorithm_->OnConnectionClosed();
306 }
307
SetHandshakeConfirmed()308 void QuicSentPacketManager::SetHandshakeConfirmed() {
309 if (!handshake_finished_) {
310 handshake_finished_ = true;
311 NeuterHandshakePackets();
312 }
313 }
314
PostProcessNewlyAckedPackets(QuicPacketNumber ack_packet_number,EncryptionLevel ack_decrypted_level,const QuicAckFrame & ack_frame,QuicTime ack_receive_time,bool rtt_updated,QuicByteCount prior_bytes_in_flight)315 void QuicSentPacketManager::PostProcessNewlyAckedPackets(
316 QuicPacketNumber ack_packet_number, EncryptionLevel ack_decrypted_level,
317 const QuicAckFrame& ack_frame, QuicTime ack_receive_time, bool rtt_updated,
318 QuicByteCount prior_bytes_in_flight) {
319 unacked_packets_.NotifyAggregatedStreamFrameAcked(
320 last_ack_frame_.ack_delay_time);
321 InvokeLossDetection(ack_receive_time);
322 MaybeInvokeCongestionEvent(rtt_updated, prior_bytes_in_flight,
323 ack_receive_time);
324 unacked_packets_.RemoveObsoletePackets();
325
326 sustained_bandwidth_recorder_.RecordEstimate(
327 send_algorithm_->InRecovery(), send_algorithm_->InSlowStart(),
328 send_algorithm_->BandwidthEstimate(), ack_receive_time, clock_->WallNow(),
329 rtt_stats_.smoothed_rtt());
330
331 // Anytime we are making forward progress and have a new RTT estimate, reset
332 // the backoff counters.
333 if (rtt_updated) {
334 // Records the max consecutive PTO before forward progress has been made.
335 if (consecutive_pto_count_ >
336 stats_->max_consecutive_rto_with_forward_progress) {
337 stats_->max_consecutive_rto_with_forward_progress =
338 consecutive_pto_count_;
339 }
340 // Reset all retransmit counters any time a new packet is acked.
341 consecutive_pto_count_ = 0;
342 consecutive_crypto_retransmission_count_ = 0;
343 }
344
345 if (debug_delegate_ != nullptr) {
346 debug_delegate_->OnIncomingAck(
347 ack_packet_number, ack_decrypted_level, ack_frame, ack_receive_time,
348 LargestAcked(ack_frame), rtt_updated, GetLeastUnacked());
349 }
350 // Remove packets below least unacked from all_packets_acked_ and
351 // last_ack_frame_.
352 last_ack_frame_.packets.RemoveUpTo(unacked_packets_.GetLeastUnacked());
353 last_ack_frame_.received_packet_times.clear();
354 }
355
MaybeInvokeCongestionEvent(bool rtt_updated,QuicByteCount prior_in_flight,QuicTime event_time)356 void QuicSentPacketManager::MaybeInvokeCongestionEvent(
357 bool rtt_updated, QuicByteCount prior_in_flight, QuicTime event_time) {
358 if (!rtt_updated && packets_acked_.empty() && packets_lost_.empty()) {
359 return;
360 }
361 const bool overshooting_detected =
362 stats_->overshooting_detected_with_network_parameters_adjusted;
363 if (using_pacing_) {
364 pacing_sender_.OnCongestionEvent(rtt_updated, prior_in_flight, event_time,
365 packets_acked_, packets_lost_, 0, 0);
366 } else {
367 send_algorithm_->OnCongestionEvent(rtt_updated, prior_in_flight, event_time,
368 packets_acked_, packets_lost_, 0, 0);
369 }
370 if (debug_delegate_ != nullptr && !overshooting_detected &&
371 stats_->overshooting_detected_with_network_parameters_adjusted) {
372 debug_delegate_->OnOvershootingDetected();
373 }
374 packets_acked_.clear();
375 packets_lost_.clear();
376 if (network_change_visitor_ != nullptr) {
377 network_change_visitor_->OnCongestionChange();
378 }
379 }
380
MarkInitialPacketsForRetransmission()381 void QuicSentPacketManager::MarkInitialPacketsForRetransmission() {
382 if (unacked_packets_.empty()) {
383 return;
384 }
385 QuicPacketNumber packet_number = unacked_packets_.GetLeastUnacked();
386 QuicPacketNumber largest_sent_packet = unacked_packets_.largest_sent_packet();
387 for (; packet_number <= largest_sent_packet; ++packet_number) {
388 QuicTransmissionInfo* transmission_info =
389 unacked_packets_.GetMutableTransmissionInfo(packet_number);
390 if (transmission_info->encryption_level == ENCRYPTION_INITIAL) {
391 if (transmission_info->in_flight) {
392 unacked_packets_.RemoveFromInFlight(transmission_info);
393 }
394 if (unacked_packets_.HasRetransmittableFrames(*transmission_info)) {
395 MarkForRetransmission(packet_number, ALL_INITIAL_RETRANSMISSION);
396 }
397 }
398 }
399 }
400
MarkZeroRttPacketsForRetransmission()401 void QuicSentPacketManager::MarkZeroRttPacketsForRetransmission() {
402 if (unacked_packets_.empty()) {
403 return;
404 }
405 QuicPacketNumber packet_number = unacked_packets_.GetLeastUnacked();
406 QuicPacketNumber largest_sent_packet = unacked_packets_.largest_sent_packet();
407 for (; packet_number <= largest_sent_packet; ++packet_number) {
408 QuicTransmissionInfo* transmission_info =
409 unacked_packets_.GetMutableTransmissionInfo(packet_number);
410 if (transmission_info->encryption_level == ENCRYPTION_ZERO_RTT) {
411 if (transmission_info->in_flight) {
412 // Remove 0-RTT packets and packets of the wrong version from flight,
413 // because neither can be processed by the peer.
414 unacked_packets_.RemoveFromInFlight(transmission_info);
415 }
416 if (unacked_packets_.HasRetransmittableFrames(*transmission_info)) {
417 MarkForRetransmission(packet_number, ALL_ZERO_RTT_RETRANSMISSION);
418 }
419 }
420 }
421 }
422
NeuterUnencryptedPackets()423 void QuicSentPacketManager::NeuterUnencryptedPackets() {
424 for (QuicPacketNumber packet_number :
425 unacked_packets_.NeuterUnencryptedPackets()) {
426 send_algorithm_->OnPacketNeutered(packet_number);
427 }
428 if (handshake_mode_disabled_) {
429 consecutive_pto_count_ = 0;
430 uber_loss_algorithm_.ResetLossDetection(INITIAL_DATA);
431 }
432 }
433
NeuterHandshakePackets()434 void QuicSentPacketManager::NeuterHandshakePackets() {
435 for (QuicPacketNumber packet_number :
436 unacked_packets_.NeuterHandshakePackets()) {
437 send_algorithm_->OnPacketNeutered(packet_number);
438 }
439 if (handshake_mode_disabled_) {
440 consecutive_pto_count_ = 0;
441 uber_loss_algorithm_.ResetLossDetection(HANDSHAKE_DATA);
442 }
443 }
444
ShouldAddMaxAckDelay(PacketNumberSpace space) const445 bool QuicSentPacketManager::ShouldAddMaxAckDelay(
446 PacketNumberSpace space) const {
447 // Do not include max_ack_delay when PTO is armed for Initial or Handshake
448 // packet number spaces.
449 return !supports_multiple_packet_number_spaces() || space == APPLICATION_DATA;
450 }
451
GetEarliestPacketSentTimeForPto(PacketNumberSpace * packet_number_space) const452 QuicTime QuicSentPacketManager::GetEarliestPacketSentTimeForPto(
453 PacketNumberSpace* packet_number_space) const {
454 QUICHE_DCHECK(supports_multiple_packet_number_spaces());
455 QuicTime earliest_sent_time = QuicTime::Zero();
456 for (int8_t i = 0; i < NUM_PACKET_NUMBER_SPACES; ++i) {
457 const QuicTime sent_time = unacked_packets_.GetLastInFlightPacketSentTime(
458 static_cast<PacketNumberSpace>(i));
459 if (!handshake_finished_ && i == APPLICATION_DATA) {
460 // Do not arm PTO for application data until handshake gets confirmed.
461 continue;
462 }
463 if (!sent_time.IsInitialized() || (earliest_sent_time.IsInitialized() &&
464 earliest_sent_time <= sent_time)) {
465 continue;
466 }
467 earliest_sent_time = sent_time;
468 *packet_number_space = static_cast<PacketNumberSpace>(i);
469 }
470
471 return earliest_sent_time;
472 }
473
MarkForRetransmission(QuicPacketNumber packet_number,TransmissionType transmission_type)474 void QuicSentPacketManager::MarkForRetransmission(
475 QuicPacketNumber packet_number, TransmissionType transmission_type) {
476 QuicTransmissionInfo* transmission_info =
477 unacked_packets_.GetMutableTransmissionInfo(packet_number);
478 // Packets without retransmittable frames can only be marked for loss
479 // retransmission.
480 QUIC_BUG_IF(quic_bug_12552_2, transmission_type != LOSS_RETRANSMISSION &&
481 !unacked_packets_.HasRetransmittableFrames(
482 *transmission_info))
483 << "packet number " << packet_number
484 << " transmission_type: " << transmission_type << " transmission_info "
485 << transmission_info->DebugString();
486 if (ShouldForceRetransmission(transmission_type)) {
487 if (!unacked_packets_.RetransmitFrames(
488 QuicFrames(transmission_info->retransmittable_frames),
489 transmission_type)) {
490 // Do not set packet state if the data is not fully retransmitted.
491 // This should only happen if packet payload size decreases which can be
492 // caused by:
493 // 1) connection tries to opportunistically retransmit data
494 // when sending a packet of a different packet number space, or
495 // 2) path MTU decreases, or
496 // 3) packet header size increases (e.g., packet number length
497 // increases).
498 QUIC_CODE_COUNT(quic_retransmit_frames_failed);
499 return;
500 }
501 QUIC_CODE_COUNT(quic_retransmit_frames_succeeded);
502 } else {
503 unacked_packets_.NotifyFramesLost(*transmission_info, transmission_type);
504
505 if (!transmission_info->retransmittable_frames.empty()) {
506 if (transmission_type == LOSS_RETRANSMISSION) {
507 // Record the first packet sent after loss, which allows to wait 1
508 // more RTT before giving up on this lost packet.
509 transmission_info->first_sent_after_loss =
510 unacked_packets_.largest_sent_packet() + 1;
511 } else {
512 // Clear the recorded first packet sent after loss when version or
513 // encryption changes.
514 transmission_info->first_sent_after_loss.Clear();
515 }
516 }
517 }
518
519 // Get the latest transmission_info here as it can be invalidated after
520 // HandleRetransmission adding new sent packets into unacked_packets_.
521 transmission_info =
522 unacked_packets_.GetMutableTransmissionInfo(packet_number);
523
524 // Update packet state according to transmission type.
525 transmission_info->state =
526 QuicUtils::RetransmissionTypeToPacketState(transmission_type);
527 }
528
RecordOneSpuriousRetransmission(const QuicTransmissionInfo & info)529 void QuicSentPacketManager::RecordOneSpuriousRetransmission(
530 const QuicTransmissionInfo& info) {
531 stats_->bytes_spuriously_retransmitted += info.bytes_sent;
532 ++stats_->packets_spuriously_retransmitted;
533 if (debug_delegate_ != nullptr) {
534 debug_delegate_->OnSpuriousPacketRetransmission(info.transmission_type,
535 info.bytes_sent);
536 }
537 }
538
MarkPacketHandled(QuicPacketNumber packet_number,QuicTransmissionInfo * info,QuicTime ack_receive_time,QuicTime::Delta ack_delay_time,QuicTime receive_timestamp)539 void QuicSentPacketManager::MarkPacketHandled(QuicPacketNumber packet_number,
540 QuicTransmissionInfo* info,
541 QuicTime ack_receive_time,
542 QuicTime::Delta ack_delay_time,
543 QuicTime receive_timestamp) {
544 if (info->has_ack_frequency) {
545 for (const auto& frame : info->retransmittable_frames) {
546 if (frame.type == ACK_FREQUENCY_FRAME) {
547 OnAckFrequencyFrameAcked(*frame.ack_frequency_frame);
548 }
549 }
550 }
551 // Try to aggregate acked stream frames if acked packet is not a
552 // retransmission.
553 if (info->transmission_type == NOT_RETRANSMISSION) {
554 unacked_packets_.MaybeAggregateAckedStreamFrame(*info, ack_delay_time,
555 receive_timestamp);
556 } else {
557 unacked_packets_.NotifyAggregatedStreamFrameAcked(ack_delay_time);
558 const bool new_data_acked = unacked_packets_.NotifyFramesAcked(
559 *info, ack_delay_time, receive_timestamp);
560 if (!new_data_acked && info->transmission_type != NOT_RETRANSMISSION) {
561 // Record as a spurious retransmission if this packet is a
562 // retransmission and no new data gets acked.
563 QUIC_DVLOG(1) << "Detect spurious retransmitted packet " << packet_number
564 << " transmission type: " << info->transmission_type;
565 RecordOneSpuriousRetransmission(*info);
566 }
567 }
568 if (info->state == LOST) {
569 // Record as a spurious loss as a packet previously declared lost gets
570 // acked.
571 const PacketNumberSpace packet_number_space =
572 unacked_packets_.GetPacketNumberSpace(info->encryption_level);
573 const QuicPacketNumber previous_largest_acked =
574 supports_multiple_packet_number_spaces()
575 ? unacked_packets_.GetLargestAckedOfPacketNumberSpace(
576 packet_number_space)
577 : unacked_packets_.largest_acked();
578 QUIC_DVLOG(1) << "Packet " << packet_number
579 << " was detected lost spuriously, "
580 "previous_largest_acked: "
581 << previous_largest_acked;
582 loss_algorithm_->SpuriousLossDetected(unacked_packets_, rtt_stats_,
583 ack_receive_time, packet_number,
584 previous_largest_acked);
585 ++stats_->packet_spuriously_detected_lost;
586 }
587
588 if (network_change_visitor_ != nullptr &&
589 info->bytes_sent > largest_mtu_acked_) {
590 largest_mtu_acked_ = info->bytes_sent;
591 network_change_visitor_->OnPathMtuIncreased(largest_mtu_acked_);
592 }
593 unacked_packets_.RemoveFromInFlight(info);
594 unacked_packets_.RemoveRetransmittability(info);
595 info->state = ACKED;
596 }
597
CanSendAckFrequency() const598 bool QuicSentPacketManager::CanSendAckFrequency() const {
599 return !peer_min_ack_delay_.IsInfinite() && handshake_finished_;
600 }
601
GetUpdatedAckFrequencyFrame() const602 QuicAckFrequencyFrame QuicSentPacketManager::GetUpdatedAckFrequencyFrame()
603 const {
604 QuicAckFrequencyFrame frame;
605 if (!CanSendAckFrequency()) {
606 QUIC_BUG(quic_bug_10750_1)
607 << "New AckFrequencyFrame is created while it shouldn't.";
608 return frame;
609 }
610
611 QUIC_RELOADABLE_FLAG_COUNT_N(quic_can_send_ack_frequency, 1, 3);
612 frame.packet_tolerance = kMaxRetransmittablePacketsBeforeAck;
613 auto rtt = use_smoothed_rtt_in_ack_delay_ ? rtt_stats_.SmoothedOrInitialRtt()
614 : rtt_stats_.MinOrInitialRtt();
615 frame.max_ack_delay = rtt * kAckDecimationDelay;
616 frame.max_ack_delay = std::max(frame.max_ack_delay, peer_min_ack_delay_);
617 // TODO(haoyuewang) Remove this once kDefaultMinAckDelayTimeMs is updated to
618 // 5 ms on the client side.
619 frame.max_ack_delay =
620 std::max(frame.max_ack_delay,
621 QuicTime::Delta::FromMilliseconds(kDefaultMinAckDelayTimeMs));
622 return frame;
623 }
624
OnPacketSent(SerializedPacket * mutable_packet,QuicTime sent_time,TransmissionType transmission_type,HasRetransmittableData has_retransmittable_data,bool measure_rtt,QuicEcnCodepoint ecn_codepoint)625 bool QuicSentPacketManager::OnPacketSent(
626 SerializedPacket* mutable_packet, QuicTime sent_time,
627 TransmissionType transmission_type,
628 HasRetransmittableData has_retransmittable_data, bool measure_rtt,
629 QuicEcnCodepoint ecn_codepoint) {
630 const SerializedPacket& packet = *mutable_packet;
631 QuicPacketNumber packet_number = packet.packet_number;
632 QUICHE_DCHECK_LE(FirstSendingPacketNumber(), packet_number);
633 QUICHE_DCHECK(!unacked_packets_.IsUnacked(packet_number));
634 QUIC_BUG_IF(quic_bug_10750_2, packet.encrypted_length == 0)
635 << "Cannot send empty packets.";
636 if (pending_timer_transmission_count_ > 0) {
637 --pending_timer_transmission_count_;
638 }
639
640 bool in_flight = has_retransmittable_data == HAS_RETRANSMITTABLE_DATA;
641 if (ignore_pings_ && mutable_packet->retransmittable_frames.size() == 1 &&
642 mutable_packet->retransmittable_frames[0].type == PING_FRAME) {
643 // Dot not use PING only packet for RTT measure or congestion control.
644 in_flight = false;
645 measure_rtt = false;
646 }
647 if (using_pacing_) {
648 pacing_sender_.OnPacketSent(sent_time, unacked_packets_.bytes_in_flight(),
649 packet_number, packet.encrypted_length,
650 has_retransmittable_data);
651 } else {
652 send_algorithm_->OnPacketSent(sent_time, unacked_packets_.bytes_in_flight(),
653 packet_number, packet.encrypted_length,
654 has_retransmittable_data);
655 }
656
657 // Deallocate message data in QuicMessageFrame immediately after packet
658 // sent.
659 if (packet.has_message) {
660 for (auto& frame : mutable_packet->retransmittable_frames) {
661 if (frame.type == MESSAGE_FRAME) {
662 frame.message_frame->message_data.clear();
663 frame.message_frame->message_length = 0;
664 }
665 }
666 }
667
668 if (packet.has_ack_frequency) {
669 for (const auto& frame : packet.retransmittable_frames) {
670 if (frame.type == ACK_FREQUENCY_FRAME) {
671 OnAckFrequencyFrameSent(*frame.ack_frequency_frame);
672 }
673 }
674 }
675 unacked_packets_.AddSentPacket(mutable_packet, transmission_type, sent_time,
676 in_flight, measure_rtt, ecn_codepoint);
677 // Reset the retransmission timer anytime a pending packet is sent.
678 return in_flight;
679 }
680
681 QuicSentPacketManager::RetransmissionTimeoutMode
OnRetransmissionTimeout()682 QuicSentPacketManager::OnRetransmissionTimeout() {
683 QUICHE_DCHECK(unacked_packets_.HasInFlightPackets() ||
684 (handshake_mode_disabled_ && !handshake_finished_));
685 QUICHE_DCHECK_EQ(0u, pending_timer_transmission_count_);
686 // Handshake retransmission, timer based loss detection, TLP, and RTO are
687 // implemented with a single alarm. The handshake alarm is set when the
688 // handshake has not completed, the loss alarm is set when the loss detection
689 // algorithm says to, and the TLP and RTO alarms are set after that.
690 // The TLP alarm is always set to run for under an RTO.
691 switch (GetRetransmissionMode()) {
692 case HANDSHAKE_MODE:
693 QUICHE_DCHECK(!handshake_mode_disabled_);
694 ++stats_->crypto_retransmit_count;
695 RetransmitCryptoPackets();
696 return HANDSHAKE_MODE;
697 case LOSS_MODE: {
698 ++stats_->loss_timeout_count;
699 QuicByteCount prior_in_flight = unacked_packets_.bytes_in_flight();
700 const QuicTime now = clock_->Now();
701 InvokeLossDetection(now);
702 MaybeInvokeCongestionEvent(false, prior_in_flight, now);
703 return LOSS_MODE;
704 }
705 case PTO_MODE:
706 QUIC_DVLOG(1) << ENDPOINT << "PTO mode";
707 ++stats_->pto_count;
708 if (handshake_mode_disabled_ && !handshake_finished_) {
709 ++stats_->crypto_retransmit_count;
710 }
711 ++consecutive_pto_count_;
712 pending_timer_transmission_count_ = 1;
713 return PTO_MODE;
714 }
715 QUIC_BUG(quic_bug_10750_3)
716 << "Unknown retransmission mode " << GetRetransmissionMode();
717 return GetRetransmissionMode();
718 }
719
RetransmitCryptoPackets()720 void QuicSentPacketManager::RetransmitCryptoPackets() {
721 QUICHE_DCHECK_EQ(HANDSHAKE_MODE, GetRetransmissionMode());
722 ++consecutive_crypto_retransmission_count_;
723 bool packet_retransmitted = false;
724 std::vector<QuicPacketNumber> crypto_retransmissions;
725 if (!unacked_packets_.empty()) {
726 QuicPacketNumber packet_number = unacked_packets_.GetLeastUnacked();
727 QuicPacketNumber largest_sent_packet =
728 unacked_packets_.largest_sent_packet();
729 for (; packet_number <= largest_sent_packet; ++packet_number) {
730 QuicTransmissionInfo* transmission_info =
731 unacked_packets_.GetMutableTransmissionInfo(packet_number);
732 // Only retransmit frames which are in flight, and therefore have been
733 // sent.
734 if (!transmission_info->in_flight ||
735 transmission_info->state != OUTSTANDING ||
736 !transmission_info->has_crypto_handshake ||
737 !unacked_packets_.HasRetransmittableFrames(*transmission_info)) {
738 continue;
739 }
740 packet_retransmitted = true;
741 crypto_retransmissions.push_back(packet_number);
742 ++pending_timer_transmission_count_;
743 }
744 }
745 QUICHE_DCHECK(packet_retransmitted)
746 << "No crypto packets found to retransmit.";
747 for (QuicPacketNumber retransmission : crypto_retransmissions) {
748 MarkForRetransmission(retransmission, HANDSHAKE_RETRANSMISSION);
749 }
750 }
751
MaybeRetransmitOldestPacket(TransmissionType type)752 bool QuicSentPacketManager::MaybeRetransmitOldestPacket(TransmissionType type) {
753 if (!unacked_packets_.empty()) {
754 QuicPacketNumber packet_number = unacked_packets_.GetLeastUnacked();
755 QuicPacketNumber largest_sent_packet =
756 unacked_packets_.largest_sent_packet();
757 for (; packet_number <= largest_sent_packet; ++packet_number) {
758 QuicTransmissionInfo* transmission_info =
759 unacked_packets_.GetMutableTransmissionInfo(packet_number);
760 // Only retransmit frames which are in flight, and therefore have been
761 // sent.
762 if (!transmission_info->in_flight ||
763 transmission_info->state != OUTSTANDING ||
764 !unacked_packets_.HasRetransmittableFrames(*transmission_info)) {
765 continue;
766 }
767 MarkForRetransmission(packet_number, type);
768 return true;
769 }
770 }
771 QUIC_DVLOG(1)
772 << "No retransmittable packets, so RetransmitOldestPacket failed.";
773 return false;
774 }
775
MaybeSendProbePacket()776 void QuicSentPacketManager::MaybeSendProbePacket() {
777 if (pending_timer_transmission_count_ == 0) {
778 return;
779 }
780 PacketNumberSpace packet_number_space;
781 if (supports_multiple_packet_number_spaces()) {
782 // Find out the packet number space to send probe packets.
783 if (!GetEarliestPacketSentTimeForPto(&packet_number_space)
784 .IsInitialized()) {
785 QUIC_BUG_IF(quic_earliest_sent_time_not_initialized,
786 unacked_packets_.perspective() == Perspective::IS_SERVER)
787 << "earliest_sent_time not initialized when trying to send PTO "
788 "retransmissions";
789 return;
790 }
791 }
792 std::vector<QuicPacketNumber> probing_packets;
793 if (!unacked_packets_.empty()) {
794 QuicPacketNumber packet_number = unacked_packets_.GetLeastUnacked();
795 QuicPacketNumber largest_sent_packet =
796 unacked_packets_.largest_sent_packet();
797 for (; packet_number <= largest_sent_packet; ++packet_number) {
798 QuicTransmissionInfo* transmission_info =
799 unacked_packets_.GetMutableTransmissionInfo(packet_number);
800 if (transmission_info->state == OUTSTANDING &&
801 unacked_packets_.HasRetransmittableFrames(*transmission_info) &&
802 (!supports_multiple_packet_number_spaces() ||
803 unacked_packets_.GetPacketNumberSpace(
804 transmission_info->encryption_level) == packet_number_space)) {
805 QUICHE_DCHECK(transmission_info->in_flight);
806 probing_packets.push_back(packet_number);
807 if (probing_packets.size() == pending_timer_transmission_count_) {
808 break;
809 }
810 }
811 }
812 }
813
814 for (QuicPacketNumber retransmission : probing_packets) {
815 QUIC_DVLOG(1) << ENDPOINT << "Marking " << retransmission
816 << " for probing retransmission";
817 MarkForRetransmission(retransmission, PTO_RETRANSMISSION);
818 }
819 // It is possible that there is not enough outstanding data for probing.
820 }
821
EnableIetfPtoAndLossDetection()822 void QuicSentPacketManager::EnableIetfPtoAndLossDetection() {
823 // Disable handshake mode.
824 handshake_mode_disabled_ = true;
825 }
826
RetransmitDataOfSpaceIfAny(PacketNumberSpace space)827 void QuicSentPacketManager::RetransmitDataOfSpaceIfAny(
828 PacketNumberSpace space) {
829 QUICHE_DCHECK(supports_multiple_packet_number_spaces());
830 if (!unacked_packets_.GetLastInFlightPacketSentTime(space).IsInitialized()) {
831 // No in flight data of space.
832 return;
833 }
834 if (unacked_packets_.empty()) {
835 return;
836 }
837 QuicPacketNumber packet_number = unacked_packets_.GetLeastUnacked();
838 QuicPacketNumber largest_sent_packet = unacked_packets_.largest_sent_packet();
839 for (; packet_number <= largest_sent_packet; ++packet_number) {
840 QuicTransmissionInfo* transmission_info =
841 unacked_packets_.GetMutableTransmissionInfo(packet_number);
842 if (transmission_info->state == OUTSTANDING &&
843 unacked_packets_.HasRetransmittableFrames(*transmission_info) &&
844 unacked_packets_.GetPacketNumberSpace(
845 transmission_info->encryption_level) == space) {
846 QUICHE_DCHECK(transmission_info->in_flight);
847 if (pending_timer_transmission_count_ == 0) {
848 pending_timer_transmission_count_ = 1;
849 }
850 MarkForRetransmission(packet_number, PTO_RETRANSMISSION);
851 return;
852 }
853 }
854 }
855
856 QuicSentPacketManager::RetransmissionTimeoutMode
GetRetransmissionMode() const857 QuicSentPacketManager::GetRetransmissionMode() const {
858 QUICHE_DCHECK(unacked_packets_.HasInFlightPackets() ||
859 (handshake_mode_disabled_ && !handshake_finished_));
860 if (!handshake_mode_disabled_ && !handshake_finished_ &&
861 unacked_packets_.HasPendingCryptoPackets()) {
862 return HANDSHAKE_MODE;
863 }
864 if (loss_algorithm_->GetLossTimeout() != QuicTime::Zero()) {
865 return LOSS_MODE;
866 }
867 return PTO_MODE;
868 }
869
InvokeLossDetection(QuicTime time)870 void QuicSentPacketManager::InvokeLossDetection(QuicTime time) {
871 if (!packets_acked_.empty()) {
872 QUICHE_DCHECK_LE(packets_acked_.front().packet_number,
873 packets_acked_.back().packet_number);
874 largest_newly_acked_ = packets_acked_.back().packet_number;
875 }
876 LossDetectionInterface::DetectionStats detection_stats =
877 loss_algorithm_->DetectLosses(unacked_packets_, time, rtt_stats_,
878 largest_newly_acked_, packets_acked_,
879 &packets_lost_);
880
881 if (detection_stats.sent_packets_max_sequence_reordering >
882 stats_->sent_packets_max_sequence_reordering) {
883 stats_->sent_packets_max_sequence_reordering =
884 detection_stats.sent_packets_max_sequence_reordering;
885 }
886
887 stats_->sent_packets_num_borderline_time_reorderings +=
888 detection_stats.sent_packets_num_borderline_time_reorderings;
889
890 stats_->total_loss_detection_response_time +=
891 detection_stats.total_loss_detection_response_time;
892
893 for (const LostPacket& packet : packets_lost_) {
894 QuicTransmissionInfo* info =
895 unacked_packets_.GetMutableTransmissionInfo(packet.packet_number);
896 ++stats_->packets_lost;
897 if (debug_delegate_ != nullptr) {
898 debug_delegate_->OnPacketLoss(packet.packet_number,
899 info->encryption_level, LOSS_RETRANSMISSION,
900 time);
901 }
902 unacked_packets_.RemoveFromInFlight(info);
903
904 MarkForRetransmission(packet.packet_number, LOSS_RETRANSMISSION);
905 }
906 }
907
MaybeUpdateRTT(QuicPacketNumber largest_acked,QuicTime::Delta ack_delay_time,QuicTime ack_receive_time)908 bool QuicSentPacketManager::MaybeUpdateRTT(QuicPacketNumber largest_acked,
909 QuicTime::Delta ack_delay_time,
910 QuicTime ack_receive_time) {
911 // We rely on ack_delay_time to compute an RTT estimate, so we
912 // only update rtt when the largest observed gets acked and the acked packet
913 // is not useless.
914 if (!unacked_packets_.IsUnacked(largest_acked)) {
915 return false;
916 }
917 // We calculate the RTT based on the highest ACKed packet number, the lower
918 // packet numbers will include the ACK aggregation delay.
919 const QuicTransmissionInfo& transmission_info =
920 unacked_packets_.GetTransmissionInfo(largest_acked);
921 // Ensure the packet has a valid sent time.
922 if (transmission_info.sent_time == QuicTime::Zero()) {
923 QUIC_BUG(quic_bug_10750_4)
924 << "Acked packet has zero sent time, largest_acked:" << largest_acked;
925 return false;
926 }
927 if (transmission_info.state == NOT_CONTRIBUTING_RTT) {
928 return false;
929 }
930 if (transmission_info.sent_time > ack_receive_time) {
931 QUIC_CODE_COUNT(quic_receive_acked_before_sending);
932 }
933
934 QuicTime::Delta send_delta = ack_receive_time - transmission_info.sent_time;
935 const bool min_rtt_available = !rtt_stats_.min_rtt().IsZero();
936 rtt_stats_.UpdateRtt(send_delta, ack_delay_time, ack_receive_time);
937
938 if (!min_rtt_available && !rtt_stats_.min_rtt().IsZero()) {
939 loss_algorithm_->OnMinRttAvailable();
940 }
941
942 return true;
943 }
944
TimeUntilSend(QuicTime now) const945 QuicTime::Delta QuicSentPacketManager::TimeUntilSend(QuicTime now) const {
946 // The TLP logic is entirely contained within QuicSentPacketManager, so the
947 // send algorithm does not need to be consulted.
948 if (pending_timer_transmission_count_ > 0) {
949 return QuicTime::Delta::Zero();
950 }
951
952 if (using_pacing_) {
953 return pacing_sender_.TimeUntilSend(now,
954 unacked_packets_.bytes_in_flight());
955 }
956
957 return send_algorithm_->CanSend(unacked_packets_.bytes_in_flight())
958 ? QuicTime::Delta::Zero()
959 : QuicTime::Delta::Infinite();
960 }
961
GetRetransmissionTime() const962 const QuicTime QuicSentPacketManager::GetRetransmissionTime() const {
963 if (!unacked_packets_.HasInFlightPackets() &&
964 PeerCompletedAddressValidation()) {
965 return QuicTime::Zero();
966 }
967 if (pending_timer_transmission_count_ > 0) {
968 // Do not set the timer if there is any credit left.
969 return QuicTime::Zero();
970 }
971 switch (GetRetransmissionMode()) {
972 case HANDSHAKE_MODE:
973 return unacked_packets_.GetLastCryptoPacketSentTime() +
974 GetCryptoRetransmissionDelay();
975 case LOSS_MODE:
976 return loss_algorithm_->GetLossTimeout();
977 case PTO_MODE: {
978 if (!supports_multiple_packet_number_spaces()) {
979 if (unacked_packets_.HasInFlightPackets() &&
980 consecutive_pto_count_ == 0) {
981 // Arm 1st PTO with earliest in flight sent time, and make sure at
982 // least kFirstPtoSrttMultiplier * RTT has been passed since last
983 // in flight packet.
984 return std::max(
985 clock_->ApproximateNow(),
986 std::max(unacked_packets_.GetFirstInFlightTransmissionInfo()
987 ->sent_time +
988 GetProbeTimeoutDelay(NUM_PACKET_NUMBER_SPACES),
989 unacked_packets_.GetLastInFlightPacketSentTime() +
990 kFirstPtoSrttMultiplier *
991 rtt_stats_.SmoothedOrInitialRtt()));
992 }
993 // Ensure PTO never gets set to a time in the past.
994 return std::max(clock_->ApproximateNow(),
995 unacked_packets_.GetLastInFlightPacketSentTime() +
996 GetProbeTimeoutDelay(NUM_PACKET_NUMBER_SPACES));
997 }
998
999 PacketNumberSpace packet_number_space = NUM_PACKET_NUMBER_SPACES;
1000 // earliest_right_edge is the earliest sent time of the last in flight
1001 // packet of all packet number spaces.
1002 QuicTime earliest_right_edge =
1003 GetEarliestPacketSentTimeForPto(&packet_number_space);
1004 if (!earliest_right_edge.IsInitialized()) {
1005 // Arm PTO from now if there is no in flight packets.
1006 earliest_right_edge = clock_->ApproximateNow();
1007 }
1008 if (packet_number_space == APPLICATION_DATA &&
1009 consecutive_pto_count_ == 0) {
1010 const QuicTransmissionInfo* first_application_info =
1011 unacked_packets_.GetFirstInFlightTransmissionInfoOfSpace(
1012 APPLICATION_DATA);
1013 if (first_application_info != nullptr) {
1014 // Arm 1st PTO with earliest in flight sent time, and make sure at
1015 // least kFirstPtoSrttMultiplier * RTT has been passed since last
1016 // in flight packet. Only do this for application data.
1017 return std::max(
1018 clock_->ApproximateNow(),
1019 std::max(
1020 first_application_info->sent_time +
1021 GetProbeTimeoutDelay(packet_number_space),
1022 earliest_right_edge + kFirstPtoSrttMultiplier *
1023 rtt_stats_.SmoothedOrInitialRtt()));
1024 }
1025 }
1026 return std::max(
1027 clock_->ApproximateNow(),
1028 earliest_right_edge + GetProbeTimeoutDelay(packet_number_space));
1029 }
1030 }
1031 QUICHE_DCHECK(false);
1032 return QuicTime::Zero();
1033 }
1034
GetPathDegradingDelay() const1035 const QuicTime::Delta QuicSentPacketManager::GetPathDegradingDelay() const {
1036 QUICHE_DCHECK_GT(num_ptos_for_path_degrading_, 0);
1037 return num_ptos_for_path_degrading_ * GetPtoDelay();
1038 }
1039
GetNetworkBlackholeDelay(int8_t num_rtos_for_blackhole_detection) const1040 const QuicTime::Delta QuicSentPacketManager::GetNetworkBlackholeDelay(
1041 int8_t num_rtos_for_blackhole_detection) const {
1042 return GetNConsecutiveRetransmissionTimeoutDelay(
1043 kDefaultMaxTailLossProbes + num_rtos_for_blackhole_detection);
1044 }
1045
GetMtuReductionDelay(int8_t num_rtos_for_blackhole_detection) const1046 QuicTime::Delta QuicSentPacketManager::GetMtuReductionDelay(
1047 int8_t num_rtos_for_blackhole_detection) const {
1048 return GetNetworkBlackholeDelay(num_rtos_for_blackhole_detection / 2);
1049 }
1050
GetCryptoRetransmissionDelay() const1051 const QuicTime::Delta QuicSentPacketManager::GetCryptoRetransmissionDelay()
1052 const {
1053 // This is equivalent to the TailLossProbeDelay, but slightly more aggressive
1054 // because crypto handshake messages don't incur a delayed ack time.
1055 QuicTime::Delta srtt = rtt_stats_.SmoothedOrInitialRtt();
1056 int64_t delay_ms;
1057 if (conservative_handshake_retransmits_) {
1058 // Using the delayed ack time directly could cause conservative handshake
1059 // retransmissions to actually be more aggressive than the default.
1060 delay_ms = std::max(peer_max_ack_delay_.ToMilliseconds(),
1061 static_cast<int64_t>(2 * srtt.ToMilliseconds()));
1062 } else {
1063 delay_ms = std::max(kMinHandshakeTimeoutMs,
1064 static_cast<int64_t>(1.5 * srtt.ToMilliseconds()));
1065 }
1066 return QuicTime::Delta::FromMilliseconds(
1067 delay_ms << consecutive_crypto_retransmission_count_);
1068 }
1069
GetProbeTimeoutDelay(PacketNumberSpace space) const1070 const QuicTime::Delta QuicSentPacketManager::GetProbeTimeoutDelay(
1071 PacketNumberSpace space) const {
1072 if (rtt_stats_.smoothed_rtt().IsZero()) {
1073 // Respect kMinHandshakeTimeoutMs to avoid a potential amplification attack.
1074 QUIC_BUG_IF(quic_bug_12552_6, rtt_stats_.initial_rtt().IsZero());
1075 return std::max(kPtoMultiplierWithoutRttSamples * rtt_stats_.initial_rtt(),
1076 QuicTime::Delta::FromMilliseconds(kMinHandshakeTimeoutMs)) *
1077 (1 << consecutive_pto_count_);
1078 }
1079 QuicTime::Delta pto_delay =
1080 rtt_stats_.smoothed_rtt() +
1081 std::max(kPtoRttvarMultiplier * rtt_stats_.mean_deviation(),
1082 kAlarmGranularity) +
1083 (ShouldAddMaxAckDelay(space) ? peer_max_ack_delay_
1084 : QuicTime::Delta::Zero());
1085 return pto_delay * (1 << consecutive_pto_count_);
1086 }
1087
GetSlowStartDuration() const1088 QuicTime::Delta QuicSentPacketManager::GetSlowStartDuration() const {
1089 if (send_algorithm_->GetCongestionControlType() == kBBR ||
1090 send_algorithm_->GetCongestionControlType() == kBBRv2) {
1091 return stats_->slowstart_duration.GetTotalElapsedTime(
1092 clock_->ApproximateNow());
1093 }
1094 return QuicTime::Delta::Infinite();
1095 }
1096
GetAvailableCongestionWindowInBytes() const1097 QuicByteCount QuicSentPacketManager::GetAvailableCongestionWindowInBytes()
1098 const {
1099 QuicByteCount congestion_window = GetCongestionWindowInBytes();
1100 QuicByteCount bytes_in_flight = GetBytesInFlight();
1101 return congestion_window - std::min(congestion_window, bytes_in_flight);
1102 }
1103
GetDebugState() const1104 std::string QuicSentPacketManager::GetDebugState() const {
1105 return send_algorithm_->GetDebugState();
1106 }
1107
SetSendAlgorithm(CongestionControlType congestion_control_type)1108 void QuicSentPacketManager::SetSendAlgorithm(
1109 CongestionControlType congestion_control_type) {
1110 if (send_algorithm_ &&
1111 send_algorithm_->GetCongestionControlType() == congestion_control_type) {
1112 return;
1113 }
1114
1115 SetSendAlgorithm(SendAlgorithmInterface::Create(
1116 clock_, &rtt_stats_, &unacked_packets_, congestion_control_type, random_,
1117 stats_, initial_congestion_window_, send_algorithm_.get()));
1118 }
1119
SetSendAlgorithm(SendAlgorithmInterface * send_algorithm)1120 void QuicSentPacketManager::SetSendAlgorithm(
1121 SendAlgorithmInterface* send_algorithm) {
1122 send_algorithm_.reset(send_algorithm);
1123 pacing_sender_.set_sender(send_algorithm);
1124 }
1125
1126 std::unique_ptr<SendAlgorithmInterface>
OnConnectionMigration(bool reset_send_algorithm)1127 QuicSentPacketManager::OnConnectionMigration(bool reset_send_algorithm) {
1128 consecutive_pto_count_ = 0;
1129 rtt_stats_.OnConnectionMigration();
1130 if (!reset_send_algorithm) {
1131 send_algorithm_->OnConnectionMigration();
1132 return nullptr;
1133 }
1134
1135 std::unique_ptr<SendAlgorithmInterface> old_send_algorithm =
1136 std::move(send_algorithm_);
1137 SetSendAlgorithm(old_send_algorithm->GetCongestionControlType());
1138 // Treat all in flight packets sent to the old peer address as lost and
1139 // retransmit them.
1140 QuicPacketNumber packet_number = unacked_packets_.GetLeastUnacked();
1141 for (auto it = unacked_packets_.begin(); it != unacked_packets_.end();
1142 ++it, ++packet_number) {
1143 if (it->in_flight) {
1144 // Proactively retransmit any packet which is in flight on the old path.
1145 // As a result, these packets will not contribute to congestion control.
1146 unacked_packets_.RemoveFromInFlight(packet_number);
1147 // Retransmitting these packets with PATH_CHANGE_RETRANSMISSION will mark
1148 // them as useless, thus not contributing to RTT stats.
1149 if (unacked_packets_.HasRetransmittableFrames(packet_number)) {
1150 MarkForRetransmission(packet_number, PATH_RETRANSMISSION);
1151 QUICHE_DCHECK_EQ(it->state, NOT_CONTRIBUTING_RTT);
1152 }
1153 }
1154 it->state = NOT_CONTRIBUTING_RTT;
1155 }
1156 return old_send_algorithm;
1157 }
1158
OnAckFrameStart(QuicPacketNumber largest_acked,QuicTime::Delta ack_delay_time,QuicTime ack_receive_time)1159 void QuicSentPacketManager::OnAckFrameStart(QuicPacketNumber largest_acked,
1160 QuicTime::Delta ack_delay_time,
1161 QuicTime ack_receive_time) {
1162 QUICHE_DCHECK(packets_acked_.empty());
1163 QUICHE_DCHECK_LE(largest_acked, unacked_packets_.largest_sent_packet());
1164 // Ignore peer_max_ack_delay and use received ack_delay during
1165 // handshake when supporting multiple packet number spaces.
1166 if (!supports_multiple_packet_number_spaces() || handshake_finished_) {
1167 if (ack_delay_time > peer_max_ack_delay()) {
1168 ack_delay_time = peer_max_ack_delay();
1169 }
1170 if (ignore_ack_delay_) {
1171 ack_delay_time = QuicTime::Delta::Zero();
1172 }
1173 }
1174 rtt_updated_ =
1175 MaybeUpdateRTT(largest_acked, ack_delay_time, ack_receive_time);
1176 last_ack_frame_.ack_delay_time = ack_delay_time;
1177 acked_packets_iter_ = last_ack_frame_.packets.rbegin();
1178 }
1179
OnAckRange(QuicPacketNumber start,QuicPacketNumber end)1180 void QuicSentPacketManager::OnAckRange(QuicPacketNumber start,
1181 QuicPacketNumber end) {
1182 if (!last_ack_frame_.largest_acked.IsInitialized() ||
1183 end > last_ack_frame_.largest_acked + 1) {
1184 // Largest acked increases.
1185 unacked_packets_.IncreaseLargestAcked(end - 1);
1186 last_ack_frame_.largest_acked = end - 1;
1187 }
1188 // Drop ack ranges which ack packets below least_unacked.
1189 QuicPacketNumber least_unacked = unacked_packets_.GetLeastUnacked();
1190 if (least_unacked.IsInitialized() && end <= least_unacked) {
1191 return;
1192 }
1193 start = std::max(start, least_unacked);
1194 do {
1195 QuicPacketNumber newly_acked_start = start;
1196 if (acked_packets_iter_ != last_ack_frame_.packets.rend()) {
1197 newly_acked_start = std::max(start, acked_packets_iter_->max());
1198 }
1199 for (QuicPacketNumber acked = end - 1; acked >= newly_acked_start;
1200 --acked) {
1201 // Check if end is above the current range. If so add newly acked packets
1202 // in descending order.
1203 packets_acked_.push_back(AckedPacket(acked, 0, QuicTime::Zero()));
1204 if (acked == FirstSendingPacketNumber()) {
1205 break;
1206 }
1207 }
1208 if (acked_packets_iter_ == last_ack_frame_.packets.rend() ||
1209 start > acked_packets_iter_->min()) {
1210 // Finish adding all newly acked packets.
1211 return;
1212 }
1213 end = std::min(end, acked_packets_iter_->min());
1214 ++acked_packets_iter_;
1215 } while (start < end);
1216 }
1217
OnAckTimestamp(QuicPacketNumber packet_number,QuicTime timestamp)1218 void QuicSentPacketManager::OnAckTimestamp(QuicPacketNumber packet_number,
1219 QuicTime timestamp) {
1220 last_ack_frame_.received_packet_times.push_back({packet_number, timestamp});
1221 for (AckedPacket& packet : packets_acked_) {
1222 if (packet.packet_number == packet_number) {
1223 packet.receive_timestamp = timestamp;
1224 return;
1225 }
1226 }
1227 }
1228
OnAckFrameEnd(QuicTime ack_receive_time,QuicPacketNumber ack_packet_number,EncryptionLevel ack_decrypted_level,const absl::optional<QuicEcnCounts> & ecn_counts)1229 AckResult QuicSentPacketManager::OnAckFrameEnd(
1230 QuicTime ack_receive_time, QuicPacketNumber ack_packet_number,
1231 EncryptionLevel ack_decrypted_level,
1232 const absl::optional<QuicEcnCounts>& ecn_counts) {
1233 QuicByteCount prior_bytes_in_flight = unacked_packets_.bytes_in_flight();
1234 // Reverse packets_acked_ so that it is in ascending order.
1235 std::reverse(packets_acked_.begin(), packets_acked_.end());
1236 for (AckedPacket& acked_packet : packets_acked_) {
1237 QuicTransmissionInfo* info =
1238 unacked_packets_.GetMutableTransmissionInfo(acked_packet.packet_number);
1239 if (!QuicUtils::IsAckable(info->state)) {
1240 if (info->state == ACKED) {
1241 QUIC_BUG(quic_bug_10750_5)
1242 << "Trying to ack an already acked packet: "
1243 << acked_packet.packet_number
1244 << ", last_ack_frame_: " << last_ack_frame_
1245 << ", least_unacked: " << unacked_packets_.GetLeastUnacked()
1246 << ", packets_acked_: " << quiche::PrintElements(packets_acked_);
1247 } else {
1248 QUIC_PEER_BUG(quic_peer_bug_10750_6)
1249 << "Received " << ack_decrypted_level
1250 << " ack for unackable packet: " << acked_packet.packet_number
1251 << " with state: "
1252 << QuicUtils::SentPacketStateToString(info->state);
1253 if (supports_multiple_packet_number_spaces()) {
1254 if (info->state == NEVER_SENT) {
1255 return UNSENT_PACKETS_ACKED;
1256 }
1257 return UNACKABLE_PACKETS_ACKED;
1258 }
1259 }
1260 continue;
1261 }
1262 QUIC_DVLOG(1) << ENDPOINT << "Got an " << ack_decrypted_level
1263 << " ack for packet " << acked_packet.packet_number
1264 << " , state: "
1265 << QuicUtils::SentPacketStateToString(info->state);
1266 const PacketNumberSpace packet_number_space =
1267 unacked_packets_.GetPacketNumberSpace(info->encryption_level);
1268 if (supports_multiple_packet_number_spaces() &&
1269 QuicUtils::GetPacketNumberSpace(ack_decrypted_level) !=
1270 packet_number_space) {
1271 return PACKETS_ACKED_IN_WRONG_PACKET_NUMBER_SPACE;
1272 }
1273 last_ack_frame_.packets.Add(acked_packet.packet_number);
1274 if (info->encryption_level == ENCRYPTION_HANDSHAKE) {
1275 handshake_packet_acked_ = true;
1276 } else if (info->encryption_level == ENCRYPTION_ZERO_RTT) {
1277 zero_rtt_packet_acked_ = true;
1278 } else if (info->encryption_level == ENCRYPTION_FORWARD_SECURE) {
1279 one_rtt_packet_acked_ = true;
1280 }
1281 largest_packet_peer_knows_is_acked_.UpdateMax(info->largest_acked);
1282 if (supports_multiple_packet_number_spaces()) {
1283 largest_packets_peer_knows_is_acked_[packet_number_space].UpdateMax(
1284 info->largest_acked);
1285 }
1286 // If data is associated with the most recent transmission of this
1287 // packet, then inform the caller.
1288 if (info->in_flight) {
1289 acked_packet.bytes_acked = info->bytes_sent;
1290 } else {
1291 // Unackable packets are skipped earlier.
1292 largest_newly_acked_ = acked_packet.packet_number;
1293 }
1294 unacked_packets_.MaybeUpdateLargestAckedOfPacketNumberSpace(
1295 packet_number_space, acked_packet.packet_number);
1296 MarkPacketHandled(acked_packet.packet_number, info, ack_receive_time,
1297 last_ack_frame_.ack_delay_time,
1298 acked_packet.receive_timestamp);
1299 }
1300 PacketNumberSpace packet_number_space =
1301 QuicUtils::GetPacketNumberSpace(ack_decrypted_level);
1302 const bool acked_new_packet = !packets_acked_.empty();
1303 PostProcessNewlyAckedPackets(ack_packet_number, ack_decrypted_level,
1304 last_ack_frame_, ack_receive_time, rtt_updated_,
1305 prior_bytes_in_flight);
1306 if (ecn_counts.has_value()) {
1307 peer_ack_ecn_counts_[packet_number_space] = ecn_counts.value();
1308 }
1309
1310 return acked_new_packet ? PACKETS_NEWLY_ACKED : NO_PACKETS_NEWLY_ACKED;
1311 }
1312
SetDebugDelegate(DebugDelegate * debug_delegate)1313 void QuicSentPacketManager::SetDebugDelegate(DebugDelegate* debug_delegate) {
1314 debug_delegate_ = debug_delegate;
1315 }
1316
OnApplicationLimited()1317 void QuicSentPacketManager::OnApplicationLimited() {
1318 if (using_pacing_) {
1319 pacing_sender_.OnApplicationLimited();
1320 }
1321 send_algorithm_->OnApplicationLimited(unacked_packets_.bytes_in_flight());
1322 if (debug_delegate_ != nullptr) {
1323 debug_delegate_->OnApplicationLimited();
1324 }
1325 }
1326
GetNextReleaseTime() const1327 NextReleaseTimeResult QuicSentPacketManager::GetNextReleaseTime() const {
1328 if (!using_pacing_) {
1329 return {QuicTime::Zero(), false};
1330 }
1331
1332 return pacing_sender_.GetNextReleaseTime();
1333 }
1334
SetInitialRtt(QuicTime::Delta rtt,bool trusted)1335 void QuicSentPacketManager::SetInitialRtt(QuicTime::Delta rtt, bool trusted) {
1336 const QuicTime::Delta min_rtt = QuicTime::Delta::FromMicroseconds(
1337 trusted ? kMinTrustedInitialRoundTripTimeUs
1338 : kMinUntrustedInitialRoundTripTimeUs);
1339 QuicTime::Delta max_rtt =
1340 QuicTime::Delta::FromMicroseconds(kMaxInitialRoundTripTimeUs);
1341 rtt_stats_.set_initial_rtt(std::max(min_rtt, std::min(max_rtt, rtt)));
1342 }
1343
EnableMultiplePacketNumberSpacesSupport()1344 void QuicSentPacketManager::EnableMultiplePacketNumberSpacesSupport() {
1345 EnableIetfPtoAndLossDetection();
1346 unacked_packets_.EnableMultiplePacketNumberSpacesSupport();
1347 }
1348
GetLargestAckedPacket(EncryptionLevel decrypted_packet_level) const1349 QuicPacketNumber QuicSentPacketManager::GetLargestAckedPacket(
1350 EncryptionLevel decrypted_packet_level) const {
1351 QUICHE_DCHECK(supports_multiple_packet_number_spaces());
1352 return unacked_packets_.GetLargestAckedOfPacketNumberSpace(
1353 QuicUtils::GetPacketNumberSpace(decrypted_packet_level));
1354 }
1355
GetLeastPacketAwaitedByPeer(EncryptionLevel encryption_level) const1356 QuicPacketNumber QuicSentPacketManager::GetLeastPacketAwaitedByPeer(
1357 EncryptionLevel encryption_level) const {
1358 QuicPacketNumber largest_acked;
1359 if (supports_multiple_packet_number_spaces()) {
1360 largest_acked = GetLargestAckedPacket(encryption_level);
1361 } else {
1362 largest_acked = GetLargestObserved();
1363 }
1364 if (!largest_acked.IsInitialized()) {
1365 // If no packets have been acked, return the first sent packet to ensure
1366 // we use a large enough packet number length.
1367 return FirstSendingPacketNumber();
1368 }
1369 QuicPacketNumber least_awaited = largest_acked + 1;
1370 QuicPacketNumber least_unacked = GetLeastUnacked();
1371 if (least_unacked.IsInitialized() && least_unacked < least_awaited) {
1372 least_awaited = least_unacked;
1373 }
1374 return least_awaited;
1375 }
1376
GetLargestPacketPeerKnowsIsAcked(EncryptionLevel decrypted_packet_level) const1377 QuicPacketNumber QuicSentPacketManager::GetLargestPacketPeerKnowsIsAcked(
1378 EncryptionLevel decrypted_packet_level) const {
1379 QUICHE_DCHECK(supports_multiple_packet_number_spaces());
1380 return largest_packets_peer_knows_is_acked_[QuicUtils::GetPacketNumberSpace(
1381 decrypted_packet_level)];
1382 }
1383
1384 QuicTime::Delta
GetNConsecutiveRetransmissionTimeoutDelay(int num_timeouts) const1385 QuicSentPacketManager::GetNConsecutiveRetransmissionTimeoutDelay(
1386 int num_timeouts) const {
1387 QuicTime::Delta total_delay = QuicTime::Delta::Zero();
1388 const QuicTime::Delta srtt = rtt_stats_.SmoothedOrInitialRtt();
1389 int num_tlps =
1390 std::min(num_timeouts, static_cast<int>(kDefaultMaxTailLossProbes));
1391 num_timeouts -= num_tlps;
1392 if (num_tlps > 0) {
1393 const QuicTime::Delta tlp_delay = std::max(
1394 2 * srtt,
1395 unacked_packets_.HasMultipleInFlightPackets()
1396 ? QuicTime::Delta::FromMilliseconds(kMinTailLossProbeTimeoutMs)
1397 : (1.5 * srtt +
1398 (QuicTime::Delta::FromMilliseconds(kMinRetransmissionTimeMs) *
1399 0.5)));
1400 total_delay = total_delay + num_tlps * tlp_delay;
1401 }
1402 if (num_timeouts == 0) {
1403 return total_delay;
1404 }
1405
1406 const QuicTime::Delta retransmission_delay =
1407 rtt_stats_.smoothed_rtt().IsZero()
1408 ? QuicTime::Delta::FromMilliseconds(kDefaultRetransmissionTimeMs)
1409 : std::max(
1410 srtt + 4 * rtt_stats_.mean_deviation(),
1411 QuicTime::Delta::FromMilliseconds(kMinRetransmissionTimeMs));
1412 total_delay = total_delay + ((1 << num_timeouts) - 1) * retransmission_delay;
1413 return total_delay;
1414 }
1415
PeerCompletedAddressValidation() const1416 bool QuicSentPacketManager::PeerCompletedAddressValidation() const {
1417 if (unacked_packets_.perspective() == Perspective::IS_SERVER ||
1418 !handshake_mode_disabled_) {
1419 return true;
1420 }
1421
1422 // To avoid handshake deadlock due to anti-amplification limit, client needs
1423 // to set PTO timer until server successfully processed any HANDSHAKE packet.
1424 return handshake_finished_ || handshake_packet_acked_;
1425 }
1426
IsLessThanThreePTOs(QuicTime::Delta timeout) const1427 bool QuicSentPacketManager::IsLessThanThreePTOs(QuicTime::Delta timeout) const {
1428 return timeout < 3 * GetPtoDelay();
1429 }
1430
GetPtoDelay() const1431 QuicTime::Delta QuicSentPacketManager::GetPtoDelay() const {
1432 return GetProbeTimeoutDelay(APPLICATION_DATA);
1433 }
1434
OnAckFrequencyFrameSent(const QuicAckFrequencyFrame & ack_frequency_frame)1435 void QuicSentPacketManager::OnAckFrequencyFrameSent(
1436 const QuicAckFrequencyFrame& ack_frequency_frame) {
1437 in_use_sent_ack_delays_.emplace_back(ack_frequency_frame.max_ack_delay,
1438 ack_frequency_frame.sequence_number);
1439 if (ack_frequency_frame.max_ack_delay > peer_max_ack_delay_) {
1440 peer_max_ack_delay_ = ack_frequency_frame.max_ack_delay;
1441 }
1442 }
1443
OnAckFrequencyFrameAcked(const QuicAckFrequencyFrame & ack_frequency_frame)1444 void QuicSentPacketManager::OnAckFrequencyFrameAcked(
1445 const QuicAckFrequencyFrame& ack_frequency_frame) {
1446 int stale_entry_count = 0;
1447 for (auto it = in_use_sent_ack_delays_.cbegin();
1448 it != in_use_sent_ack_delays_.cend(); ++it) {
1449 if (it->second < ack_frequency_frame.sequence_number) {
1450 ++stale_entry_count;
1451 } else {
1452 break;
1453 }
1454 }
1455 if (stale_entry_count > 0) {
1456 in_use_sent_ack_delays_.pop_front_n(stale_entry_count);
1457 }
1458 if (in_use_sent_ack_delays_.empty()) {
1459 QUIC_BUG(quic_bug_10750_7) << "in_use_sent_ack_delays_ is empty.";
1460 return;
1461 }
1462 peer_max_ack_delay_ = std::max_element(in_use_sent_ack_delays_.cbegin(),
1463 in_use_sent_ack_delays_.cend())
1464 ->first;
1465 }
1466
1467 #undef ENDPOINT // undef for jumbo builds
1468 } // namespace quic
1469