• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef QUICHE_QUIC_CORE_QUIC_SENT_PACKET_MANAGER_H_
6 #define QUICHE_QUIC_CORE_QUIC_SENT_PACKET_MANAGER_H_
7 
8 #include <cstddef>
9 #include <cstdint>
10 #include <map>
11 #include <memory>
12 #include <set>
13 #include <string>
14 #include <utility>
15 #include <vector>
16 
17 #include "quiche/quic/core/congestion_control/pacing_sender.h"
18 #include "quiche/quic/core/congestion_control/rtt_stats.h"
19 #include "quiche/quic/core/congestion_control/send_algorithm_interface.h"
20 #include "quiche/quic/core/congestion_control/uber_loss_algorithm.h"
21 #include "quiche/quic/core/proto/cached_network_parameters_proto.h"
22 #include "quiche/quic/core/quic_packets.h"
23 #include "quiche/quic/core/quic_sustained_bandwidth_recorder.h"
24 #include "quiche/quic/core/quic_time.h"
25 #include "quiche/quic/core/quic_transmission_info.h"
26 #include "quiche/quic/core/quic_types.h"
27 #include "quiche/quic/core/quic_unacked_packet_map.h"
28 #include "quiche/quic/platform/api/quic_export.h"
29 #include "quiche/quic/platform/api/quic_flags.h"
30 #include "quiche/common/quiche_circular_deque.h"
31 
32 namespace quic {
33 
34 namespace test {
35 class QuicConnectionPeer;
36 class QuicSentPacketManagerPeer;
37 }  // namespace test
38 
39 class QuicClock;
40 class QuicConfig;
41 struct QuicConnectionStats;
42 
43 // Class which tracks the set of packets sent on a QUIC connection and contains
44 // a send algorithm to decide when to send new packets.  It keeps track of any
45 // retransmittable data associated with each packet. If a packet is
46 // retransmitted, it will keep track of each version of a packet so that if a
47 // previous transmission is acked, the data will not be retransmitted.
48 class QUIC_EXPORT_PRIVATE QuicSentPacketManager {
49  public:
50   // Interface which gets callbacks from the QuicSentPacketManager at
51   // interesting points.  Implementations must not mutate the state of
52   // the packet manager or connection as a result of these callbacks.
53   class QUIC_EXPORT_PRIVATE DebugDelegate {
54    public:
55     struct QUIC_EXPORT_PRIVATE SendParameters {
56       CongestionControlType congestion_control_type;
57       bool use_pacing;
58       QuicPacketCount initial_congestion_window;
59     };
60 
~DebugDelegate()61     virtual ~DebugDelegate() {}
62 
63     // Called when a spurious retransmission is detected.
OnSpuriousPacketRetransmission(TransmissionType,QuicByteCount)64     virtual void OnSpuriousPacketRetransmission(
65         TransmissionType /*transmission_type*/, QuicByteCount /*byte_size*/) {}
66 
OnIncomingAck(QuicPacketNumber,EncryptionLevel,const QuicAckFrame &,QuicTime,QuicPacketNumber,bool,QuicPacketNumber)67     virtual void OnIncomingAck(QuicPacketNumber /*ack_packet_number*/,
68                                EncryptionLevel /*ack_decrypted_level*/,
69                                const QuicAckFrame& /*ack_frame*/,
70                                QuicTime /*ack_receive_time*/,
71                                QuicPacketNumber /*largest_observed*/,
72                                bool /*rtt_updated*/,
73                                QuicPacketNumber /*least_unacked_sent_packet*/) {
74     }
75 
OnPacketLoss(QuicPacketNumber,EncryptionLevel,TransmissionType,QuicTime)76     virtual void OnPacketLoss(QuicPacketNumber /*lost_packet_number*/,
77                               EncryptionLevel /*encryption_level*/,
78                               TransmissionType /*transmission_type*/,
79                               QuicTime /*detection_time*/) {}
80 
OnApplicationLimited()81     virtual void OnApplicationLimited() {}
82 
OnAdjustNetworkParameters(QuicBandwidth,QuicTime::Delta,QuicByteCount,QuicByteCount)83     virtual void OnAdjustNetworkParameters(QuicBandwidth /*bandwidth*/,
84                                            QuicTime::Delta /*rtt*/,
85                                            QuicByteCount /*old_cwnd*/,
86                                            QuicByteCount /*new_cwnd*/) {}
87 
OnAdjustBurstSize(int,int)88     virtual void OnAdjustBurstSize(int /*old_burst_size*/,
89                                    int /*new_burst_size*/) {}
90 
OnOvershootingDetected()91     virtual void OnOvershootingDetected() {}
92 
OnConfigProcessed(const SendParameters &)93     virtual void OnConfigProcessed(const SendParameters& /*parameters*/) {}
94   };
95 
96   // Interface which gets callbacks from the QuicSentPacketManager when
97   // network-related state changes. Implementations must not mutate the
98   // state of the packet manager as a result of these callbacks.
99   class QUIC_EXPORT_PRIVATE NetworkChangeVisitor {
100    public:
~NetworkChangeVisitor()101     virtual ~NetworkChangeVisitor() {}
102 
103     // Called when congestion window or RTT may have changed.
104     virtual void OnCongestionChange() = 0;
105 
106     // Called when the Path MTU may have increased.
107     virtual void OnPathMtuIncreased(QuicPacketLength packet_size) = 0;
108   };
109 
110   // The retransmission timer is a single timer which switches modes depending
111   // upon connection state.
112   enum RetransmissionTimeoutMode {
113     // Retransmission of handshake packets prior to handshake completion.
114     HANDSHAKE_MODE,
115     // Re-invoke the loss detection when a packet is not acked before the
116     // loss detection algorithm expects.
117     LOSS_MODE,
118     // A probe timeout. At least one probe packet must be sent when timer
119     // expires.
120     PTO_MODE,
121   };
122 
123   QuicSentPacketManager(Perspective perspective, const QuicClock* clock,
124                         QuicRandom* random, QuicConnectionStats* stats,
125                         CongestionControlType congestion_control_type);
126   QuicSentPacketManager(const QuicSentPacketManager&) = delete;
127   QuicSentPacketManager& operator=(const QuicSentPacketManager&) = delete;
128   virtual ~QuicSentPacketManager();
129 
130   virtual void SetFromConfig(const QuicConfig& config);
131 
ReserveUnackedPacketsInitialCapacity(int initial_capacity)132   void ReserveUnackedPacketsInitialCapacity(int initial_capacity) {
133     unacked_packets_.ReserveInitialCapacity(initial_capacity);
134   }
135 
136   void ApplyConnectionOptions(const QuicTagVector& connection_options);
137 
138   // Pass the CachedNetworkParameters to the send algorithm.
139   void ResumeConnectionState(
140       const CachedNetworkParameters& cached_network_params,
141       bool max_bandwidth_resumption);
142 
SetMaxPacingRate(QuicBandwidth max_pacing_rate)143   void SetMaxPacingRate(QuicBandwidth max_pacing_rate) {
144     pacing_sender_.set_max_pacing_rate(max_pacing_rate);
145   }
146 
MaxPacingRate()147   QuicBandwidth MaxPacingRate() const {
148     return pacing_sender_.max_pacing_rate();
149   }
150 
151   // Called to mark the handshake state complete, and all handshake packets are
152   // neutered.
153   // TODO(fayang): Rename this function to OnHandshakeComplete.
154   void SetHandshakeConfirmed();
155 
156   // Requests retransmission of all unacked 0-RTT packets.
157   // Only 0-RTT encrypted packets will be retransmitted. This can happen,
158   // for example, when a CHLO has been rejected and the previously encrypted
159   // data needs to be encrypted with a new key.
160   void MarkZeroRttPacketsForRetransmission();
161 
162   // Request retransmission of all unacked INITIAL packets.
163   void MarkInitialPacketsForRetransmission();
164 
165   // Notify the sent packet manager of an external network measurement or
166   // prediction for either |bandwidth| or |rtt|; either can be empty.
167   void AdjustNetworkParameters(
168       const SendAlgorithmInterface::NetworkParams& params);
169 
170   void SetLossDetectionTuner(
171       std::unique_ptr<LossDetectionTunerInterface> tuner);
172   void OnConfigNegotiated();
173   void OnConnectionClosed();
174 
175   // Retransmits the oldest pending packet.
176   bool MaybeRetransmitOldestPacket(TransmissionType type);
177 
178   // Removes the retransmittable frames from all unencrypted packets to ensure
179   // they don't get retransmitted.
180   void NeuterUnencryptedPackets();
181 
182   // Returns true if there's outstanding crypto data.
HasUnackedCryptoPackets()183   bool HasUnackedCryptoPackets() const {
184     return unacked_packets_.HasPendingCryptoPackets();
185   }
186 
187   // Returns true if there are packets in flight expecting to be acknowledged.
HasInFlightPackets()188   bool HasInFlightPackets() const {
189     return unacked_packets_.HasInFlightPackets();
190   }
191 
192   // Returns the smallest packet number of a serialized packet which has not
193   // been acked by the peer.
GetLeastUnacked()194   QuicPacketNumber GetLeastUnacked() const {
195     return unacked_packets_.GetLeastUnacked();
196   }
197 
198   // Called when we have sent bytes to the peer.  This informs the manager both
199   // the number of bytes sent and if they were retransmitted and if this packet
200   // is used for rtt measuring.  Returns true if the sender should reset the
201   // retransmission timer.
202   bool OnPacketSent(SerializedPacket* mutable_packet, QuicTime sent_time,
203                     TransmissionType transmission_type,
204                     HasRetransmittableData has_retransmittable_data,
205                     bool measure_rtt, QuicEcnCodepoint ecn_codepoint);
206 
207   bool CanSendAckFrequency() const;
208 
209   QuicAckFrequencyFrame GetUpdatedAckFrequencyFrame() const;
210 
211   // Called when the retransmission timer expires and returns the retransmission
212   // mode.
213   RetransmissionTimeoutMode OnRetransmissionTimeout();
214 
215   // Calculate the time until we can send the next packet to the wire.
216   // Note 1: When kUnknownWaitTime is returned, there is no need to poll
217   // TimeUntilSend again until we receive an OnIncomingAckFrame event.
218   // Note 2: Send algorithms may or may not use |retransmit| in their
219   // calculations.
220   QuicTime::Delta TimeUntilSend(QuicTime now) const;
221 
222   // Returns the current delay for the retransmission timer, which may send
223   // either a tail loss probe or do a full RTO.  Returns QuicTime::Zero() if
224   // there are no retransmittable packets.
225   const QuicTime GetRetransmissionTime() const;
226 
227   // Returns the current delay for the path degrading timer, which is used to
228   // notify the session that this connection is degrading.
229   const QuicTime::Delta GetPathDegradingDelay() const;
230 
231   // Returns the current delay for detecting network blackhole.
232   const QuicTime::Delta GetNetworkBlackholeDelay(
233       int8_t num_rtos_for_blackhole_detection) const;
234 
235   // Returns the delay before reducing max packet size. This delay is guranteed
236   // to be smaller than the network blackhole delay.
237   QuicTime::Delta GetMtuReductionDelay(
238       int8_t num_rtos_for_blackhole_detection) const;
239 
GetRttStats()240   const RttStats* GetRttStats() const { return &rtt_stats_; }
241 
SetRttStats(const RttStats & rtt_stats)242   void SetRttStats(const RttStats& rtt_stats) {
243     rtt_stats_.CloneFrom(rtt_stats);
244   }
245 
246   // Returns the estimated bandwidth calculated by the congestion algorithm.
BandwidthEstimate()247   QuicBandwidth BandwidthEstimate() const {
248     return send_algorithm_->BandwidthEstimate();
249   }
250 
SustainedBandwidthRecorder()251   const QuicSustainedBandwidthRecorder* SustainedBandwidthRecorder() const {
252     return &sustained_bandwidth_recorder_;
253   }
254 
255   // Returns the size of the current congestion window in number of
256   // kDefaultTCPMSS-sized segments. Note, this is not the *available* window.
257   // Some send algorithms may not use a congestion window and will return 0.
GetCongestionWindowInTcpMss()258   QuicPacketCount GetCongestionWindowInTcpMss() const {
259     return send_algorithm_->GetCongestionWindow() / kDefaultTCPMSS;
260   }
261 
262   // Returns the number of packets of length |max_packet_length| which fit in
263   // the current congestion window. More packets may end up in flight if the
264   // congestion window has been recently reduced, of if non-full packets are
265   // sent.
EstimateMaxPacketsInFlight(QuicByteCount max_packet_length)266   QuicPacketCount EstimateMaxPacketsInFlight(
267       QuicByteCount max_packet_length) const {
268     return send_algorithm_->GetCongestionWindow() / max_packet_length;
269   }
270 
271   // Returns the size of the current congestion window size in bytes.
GetCongestionWindowInBytes()272   QuicByteCount GetCongestionWindowInBytes() const {
273     return send_algorithm_->GetCongestionWindow();
274   }
275 
276   // Returns the difference between current congestion window and bytes in
277   // flight. Returns 0 if bytes in flight is bigger than the current congestion
278   // window.
279   QuicByteCount GetAvailableCongestionWindowInBytes() const;
280 
GetPacingRate()281   QuicBandwidth GetPacingRate() const {
282     return send_algorithm_->PacingRate(GetBytesInFlight());
283   }
284 
285   // Returns the size of the slow start congestion window in nume of 1460 byte
286   // TCP segments, aka ssthresh.  Some send algorithms do not define a slow
287   // start threshold and will return 0.
GetSlowStartThresholdInTcpMss()288   QuicPacketCount GetSlowStartThresholdInTcpMss() const {
289     return send_algorithm_->GetSlowStartThreshold() / kDefaultTCPMSS;
290   }
291 
292   // Return the total time spent in slow start so far. If the sender is
293   // currently in slow start, the return value will include the duration between
294   // the most recent entry to slow start and now.
295   //
296   // Only implemented for BBR. Return QuicTime::Delta::Infinite() for other
297   // congestion controllers.
298   QuicTime::Delta GetSlowStartDuration() const;
299 
300   // Returns debugging information about the state of the congestion controller.
301   std::string GetDebugState() const;
302 
303   // Returns the number of bytes that are considered in-flight, i.e. not lost or
304   // acknowledged.
GetBytesInFlight()305   QuicByteCount GetBytesInFlight() const {
306     return unacked_packets_.bytes_in_flight();
307   }
308 
309   // Called when peer address changes. Must be called IFF the address change is
310   // not NAT rebinding. If reset_send_algorithm is true, switch to a new send
311   // algorithm object and retransmit all the in-flight packets. Return the send
312   // algorithm object used on the previous path.
313   std::unique_ptr<SendAlgorithmInterface> OnConnectionMigration(
314       bool reset_send_algorithm);
315 
316   // Called when an ack frame is initially parsed.
317   void OnAckFrameStart(QuicPacketNumber largest_acked,
318                        QuicTime::Delta ack_delay_time,
319                        QuicTime ack_receive_time);
320 
321   // Called when ack range [start, end) is received. Populates packets_acked_
322   // with newly acked packets.
323   void OnAckRange(QuicPacketNumber start, QuicPacketNumber end);
324 
325   // Called when a timestamp is processed.  If it's present in packets_acked_,
326   // the timestamp field is set.  Otherwise, the timestamp is ignored.
327   void OnAckTimestamp(QuicPacketNumber packet_number, QuicTime timestamp);
328 
329   // Called when an ack frame is parsed completely.
330   AckResult OnAckFrameEnd(QuicTime ack_receive_time,
331                           QuicPacketNumber ack_packet_number,
332                           EncryptionLevel ack_decrypted_level,
333                           const absl::optional<QuicEcnCounts>& ecn_counts);
334 
335   void EnableMultiplePacketNumberSpacesSupport();
336 
337   void SetDebugDelegate(DebugDelegate* debug_delegate);
338 
SetPacingAlarmGranularity(QuicTime::Delta alarm_granularity)339   void SetPacingAlarmGranularity(QuicTime::Delta alarm_granularity) {
340     pacing_sender_.set_alarm_granularity(alarm_granularity);
341   }
342 
GetLargestObserved()343   QuicPacketNumber GetLargestObserved() const {
344     return unacked_packets_.largest_acked();
345   }
346 
347   QuicPacketNumber GetLargestAckedPacket(
348       EncryptionLevel decrypted_packet_level) const;
349 
GetLargestSentPacket()350   QuicPacketNumber GetLargestSentPacket() const {
351     return unacked_packets_.largest_sent_packet();
352   }
353 
354   // Returns the lowest of the largest acknowledged packet and the least
355   // unacked packet. This is designed to be used when computing the packet
356   // number length to send.
357   QuicPacketNumber GetLeastPacketAwaitedByPeer(
358       EncryptionLevel encryption_level) const;
359 
360   QuicPacketNumber GetLargestPacketPeerKnowsIsAcked(
361       EncryptionLevel decrypted_packet_level) const;
362 
SetNetworkChangeVisitor(NetworkChangeVisitor * visitor)363   void SetNetworkChangeVisitor(NetworkChangeVisitor* visitor) {
364     QUICHE_DCHECK(!network_change_visitor_);
365     QUICHE_DCHECK(visitor);
366     network_change_visitor_ = visitor;
367   }
368 
InSlowStart()369   bool InSlowStart() const { return send_algorithm_->InSlowStart(); }
370 
GetConsecutivePtoCount()371   size_t GetConsecutivePtoCount() const { return consecutive_pto_count_; }
372 
373   void OnApplicationLimited();
374 
GetSendAlgorithm()375   const SendAlgorithmInterface* GetSendAlgorithm() const {
376     return send_algorithm_.get();
377   }
378 
SetSessionNotifier(SessionNotifierInterface * session_notifier)379   void SetSessionNotifier(SessionNotifierInterface* session_notifier) {
380     unacked_packets_.SetSessionNotifier(session_notifier);
381   }
382 
383   NextReleaseTimeResult GetNextReleaseTime() const;
384 
initial_congestion_window()385   QuicPacketCount initial_congestion_window() const {
386     return initial_congestion_window_;
387   }
388 
largest_packet_peer_knows_is_acked()389   QuicPacketNumber largest_packet_peer_knows_is_acked() const {
390     QUICHE_DCHECK(!supports_multiple_packet_number_spaces());
391     return largest_packet_peer_knows_is_acked_;
392   }
393 
pending_timer_transmission_count()394   size_t pending_timer_transmission_count() const {
395     return pending_timer_transmission_count_;
396   }
397 
peer_max_ack_delay()398   QuicTime::Delta peer_max_ack_delay() const { return peer_max_ack_delay_; }
399 
set_peer_max_ack_delay(QuicTime::Delta peer_max_ack_delay)400   void set_peer_max_ack_delay(QuicTime::Delta peer_max_ack_delay) {
401     // The delayed ack time should never be more than one half the min RTO time.
402     QUICHE_DCHECK_LE(
403         peer_max_ack_delay,
404         (QuicTime::Delta::FromMilliseconds(kMinRetransmissionTimeMs) * 0.5));
405     peer_max_ack_delay_ = peer_max_ack_delay;
406   }
407 
unacked_packets()408   const QuicUnackedPacketMap& unacked_packets() const {
409     return unacked_packets_;
410   }
411 
uber_loss_algorithm()412   const UberLossAlgorithm* uber_loss_algorithm() const {
413     return &uber_loss_algorithm_;
414   }
415 
416   // Sets the send algorithm to the given congestion control type and points the
417   // pacing sender at |send_algorithm_|. Can be called any number of times.
418   void SetSendAlgorithm(CongestionControlType congestion_control_type);
419 
420   // Sets the send algorithm to |send_algorithm| and points the pacing sender at
421   // |send_algorithm_|. Takes ownership of |send_algorithm|. Can be called any
422   // number of times.
423   // Setting the send algorithm once the connection is underway is dangerous.
424   void SetSendAlgorithm(SendAlgorithmInterface* send_algorithm);
425 
426   // Sends one probe packet.
427   void MaybeSendProbePacket();
428 
429   // Called to disable HANDSHAKE_MODE, and only PTO and LOSS modes are used.
430   // Also enable IETF loss detection.
431   void EnableIetfPtoAndLossDetection();
432 
433   // Called to retransmit in flight packet of |space| if any.
434   void RetransmitDataOfSpaceIfAny(PacketNumberSpace space);
435 
436   // Returns true if |timeout| is less than 3 * RTO/PTO delay.
437   bool IsLessThanThreePTOs(QuicTime::Delta timeout) const;
438 
439   // Returns current PTO delay.
440   QuicTime::Delta GetPtoDelay() const;
441 
supports_multiple_packet_number_spaces()442   bool supports_multiple_packet_number_spaces() const {
443     return unacked_packets_.supports_multiple_packet_number_spaces();
444   }
445 
handshake_mode_disabled()446   bool handshake_mode_disabled() const { return handshake_mode_disabled_; }
447 
zero_rtt_packet_acked()448   bool zero_rtt_packet_acked() const { return zero_rtt_packet_acked_; }
449 
one_rtt_packet_acked()450   bool one_rtt_packet_acked() const { return one_rtt_packet_acked_; }
451 
OnUserAgentIdKnown()452   void OnUserAgentIdKnown() { loss_algorithm_->OnUserAgentIdKnown(); }
453 
454   // Gets the earliest in flight packet sent time to calculate PTO. Also
455   // updates |packet_number_space| if a PTO timer should be armed.
456   QuicTime GetEarliestPacketSentTimeForPto(
457       PacketNumberSpace* packet_number_space) const;
458 
set_num_ptos_for_path_degrading(int num_ptos_for_path_degrading)459   void set_num_ptos_for_path_degrading(int num_ptos_for_path_degrading) {
460     num_ptos_for_path_degrading_ = num_ptos_for_path_degrading;
461   }
462 
463   // Sets the initial RTT of the connection. The inital RTT is clamped to
464   // - A maximum of kMaxInitialRoundTripTimeUs.
465   // - A minimum of kMinTrustedInitialRoundTripTimeUs if |trusted|, or
466   // kMinUntrustedInitialRoundTripTimeUs if not |trusted|.
467   void SetInitialRtt(QuicTime::Delta rtt, bool trusted);
468 
469  private:
470   friend class test::QuicConnectionPeer;
471   friend class test::QuicSentPacketManagerPeer;
472 
473   // Returns the current retransmission mode.
474   RetransmissionTimeoutMode GetRetransmissionMode() const;
475 
476   // Retransmits all crypto stream packets.
477   void RetransmitCryptoPackets();
478 
479   // Returns the timeout for retransmitting crypto handshake packets.
480   const QuicTime::Delta GetCryptoRetransmissionDelay() const;
481 
482   // Returns the probe timeout.
483   const QuicTime::Delta GetProbeTimeoutDelay(PacketNumberSpace space) const;
484 
485   // Update the RTT if the ack is for the largest acked packet number.
486   // Returns true if the rtt was updated.
487   bool MaybeUpdateRTT(QuicPacketNumber largest_acked,
488                       QuicTime::Delta ack_delay_time,
489                       QuicTime ack_receive_time);
490 
491   // Invokes the loss detection algorithm and loses and retransmits packets if
492   // necessary.
493   void InvokeLossDetection(QuicTime time);
494 
495   // Invokes OnCongestionEvent if |rtt_updated| is true, there are pending acks,
496   // or pending losses.  Clears pending acks and pending losses afterwards.
497   // |prior_in_flight| is the number of bytes in flight before the losses or
498   // acks, |event_time| is normally the timestamp of the ack packet which caused
499   // the event, although it can be the time at which loss detection was
500   // triggered.
501   void MaybeInvokeCongestionEvent(bool rtt_updated,
502                                   QuicByteCount prior_in_flight,
503                                   QuicTime event_time);
504 
505   // Removes the retransmittability and in flight properties from the packet at
506   // |info| due to receipt by the peer.
507   void MarkPacketHandled(QuicPacketNumber packet_number,
508                          QuicTransmissionInfo* info, QuicTime ack_receive_time,
509                          QuicTime::Delta ack_delay_time,
510                          QuicTime receive_timestamp);
511 
512   // Request that |packet_number| be retransmitted after the other pending
513   // retransmissions.  Does not add it to the retransmissions if it's already
514   // a pending retransmission. Do not reuse iterator of the underlying
515   // unacked_packets_ after calling this function as it can be invalidated.
516   void MarkForRetransmission(QuicPacketNumber packet_number,
517                              TransmissionType transmission_type);
518 
519   // Called after packets have been marked handled with last received ack frame.
520   void PostProcessNewlyAckedPackets(QuicPacketNumber ack_packet_number,
521                                     EncryptionLevel ack_decrypted_level,
522                                     const QuicAckFrame& ack_frame,
523                                     QuicTime ack_receive_time, bool rtt_updated,
524                                     QuicByteCount prior_bytes_in_flight);
525 
526   // Notify observers that packet with QuicTransmissionInfo |info| is a spurious
527   // retransmission. It is caller's responsibility to guarantee the packet with
528   // QuicTransmissionInfo |info| is a spurious retransmission before calling
529   // this function.
530   void RecordOneSpuriousRetransmission(const QuicTransmissionInfo& info);
531 
532   // Called when handshake is confirmed to remove the retransmittable frames
533   // from all packets of HANDSHAKE_DATA packet number space to ensure they don't
534   // get retransmitted and will eventually be removed from unacked packets map.
535   void NeuterHandshakePackets();
536 
537   // Indicates whether including peer_max_ack_delay_ when calculating PTO
538   // timeout.
539   bool ShouldAddMaxAckDelay(PacketNumberSpace space) const;
540 
541   // A helper function to return total delay of |num_timeouts| retransmission
542   // timeout with TLP and RTO mode.
543   // TODO(fayang): remove this method and calculate blackhole delay by PTO.
544   QuicTime::Delta GetNConsecutiveRetransmissionTimeoutDelay(
545       int num_timeouts) const;
546 
547   // Returns true if peer has finished address validation, such that
548   // retransmission timer is not armed if there is no packets in flight.
549   bool PeerCompletedAddressValidation() const;
550 
551   // Called when an AckFrequencyFrame is sent.
552   void OnAckFrequencyFrameSent(
553       const QuicAckFrequencyFrame& ack_frequency_frame);
554 
555   // Called when an AckFrequencyFrame is acked.
556   void OnAckFrequencyFrameAcked(
557       const QuicAckFrequencyFrame& ack_frequency_frame);
558 
559   // Newly serialized retransmittable packets are added to this map, which
560   // contains owning pointers to any contained frames.  If a packet is
561   // retransmitted, this map will contain entries for both the old and the new
562   // packet. The old packet's retransmittable frames entry will be nullptr,
563   // while the new packet's entry will contain the frames to retransmit.
564   // If the old packet is acked before the new packet, then the old entry will
565   // be removed from the map and the new entry's retransmittable frames will be
566   // set to nullptr.
567   QuicUnackedPacketMap unacked_packets_;
568 
569   const QuicClock* clock_;
570   QuicRandom* random_;
571   QuicConnectionStats* stats_;
572 
573   DebugDelegate* debug_delegate_;
574   NetworkChangeVisitor* network_change_visitor_;
575   QuicPacketCount initial_congestion_window_;
576   RttStats rtt_stats_;
577   std::unique_ptr<SendAlgorithmInterface> send_algorithm_;
578   // Not owned. Always points to |uber_loss_algorithm_| outside of tests.
579   LossDetectionInterface* loss_algorithm_;
580   UberLossAlgorithm uber_loss_algorithm_;
581 
582   // Number of times the crypto handshake has been retransmitted.
583   size_t consecutive_crypto_retransmission_count_;
584   // Number of pending transmissions of PTO or crypto packets.
585   size_t pending_timer_transmission_count_;
586 
587   bool using_pacing_;
588   // If true, use a more conservative handshake retransmission policy.
589   bool conservative_handshake_retransmits_;
590 
591   // Vectors packets acked and lost as a result of the last congestion event.
592   AckedPacketVector packets_acked_;
593   LostPacketVector packets_lost_;
594   // Largest newly acknowledged packet.
595   QuicPacketNumber largest_newly_acked_;
596   // Largest packet in bytes ever acknowledged.
597   QuicPacketLength largest_mtu_acked_;
598 
599   // Replaces certain calls to |send_algorithm_| when |using_pacing_| is true.
600   // Calls into |send_algorithm_| for the underlying congestion control.
601   PacingSender pacing_sender_;
602 
603   // Indicates whether handshake is finished. This is purely used to determine
604   // retransmission mode. DONOT use this to infer handshake state.
605   bool handshake_finished_;
606 
607   // Records bandwidth from server to client in normal operation, over periods
608   // of time with no loss events.
609   QuicSustainedBandwidthRecorder sustained_bandwidth_recorder_;
610 
611   // The largest acked value that was sent in an ack, which has then been acked.
612   QuicPacketNumber largest_packet_peer_knows_is_acked_;
613   // The largest acked value that was sent in an ack, which has then been acked
614   // for per packet number space. Only used when connection supports multiple
615   // packet number spaces.
616   QuicPacketNumber
617       largest_packets_peer_knows_is_acked_[NUM_PACKET_NUMBER_SPACES];
618 
619   // The maximum ACK delay time that the peer might uses. Initialized to be the
620   // same as local_max_ack_delay_, may be changed via transport parameter
621   // negotiation or subsequently by AckFrequencyFrame.
622   QuicTime::Delta peer_max_ack_delay_;
623 
624   // Peer sends min_ack_delay in TransportParameter to advertise its support for
625   // AckFrequencyFrame.
626   QuicTime::Delta peer_min_ack_delay_ = QuicTime::Delta::Infinite();
627 
628   // Use smoothed RTT for computing max_ack_delay in AckFrequency frame.
629   bool use_smoothed_rtt_in_ack_delay_ = false;
630 
631   // The history of outstanding max_ack_delays sent to peer. Outstanding means
632   // a max_ack_delay is sent as part of the last acked AckFrequencyFrame or
633   // an unacked AckFrequencyFrame after that.
634   quiche::QuicheCircularDeque<
635       std::pair<QuicTime::Delta, /*sequence_number=*/uint64_t>>
636       in_use_sent_ack_delays_;
637 
638   // Latest received ack frame.
639   QuicAckFrame last_ack_frame_;
640 
641   // Record whether RTT gets updated by last largest acked..
642   bool rtt_updated_;
643 
644   // A reverse iterator of last_ack_frame_.packets. This is reset in
645   // OnAckRangeStart, and gradually moves in OnAckRange..
646   PacketNumberQueue::const_reverse_iterator acked_packets_iter_;
647 
648   // Number of times the PTO timer has fired in a row without receiving an ack.
649   size_t consecutive_pto_count_;
650 
651   // True if HANDSHAKE mode has been disabled.
652   bool handshake_mode_disabled_;
653 
654   // True if any ENCRYPTION_HANDSHAKE packet gets acknowledged.
655   bool handshake_packet_acked_;
656 
657   // True if any 0-RTT packet gets acknowledged.
658   bool zero_rtt_packet_acked_;
659 
660   // True if any 1-RTT packet gets acknowledged.
661   bool one_rtt_packet_acked_;
662 
663   // The number of PTOs needed for path degrading alarm. If equals to 0, the
664   // traditional path degrading mechanism will be used.
665   int num_ptos_for_path_degrading_;
666 
667   // If true, do not use PING only packets for RTT measurement or congestion
668   // control.
669   bool ignore_pings_;
670 
671   // Whether to ignore the ack_delay in received ACKs.
672   bool ignore_ack_delay_;
673 
674   // Most recent ECN codepoint counts received in an ACK frame sent by the peer.
675   QuicEcnCounts peer_ack_ecn_counts_[NUM_PACKET_NUMBER_SPACES];
676 };
677 
678 }  // namespace quic
679 
680 #endif  // QUICHE_QUIC_CORE_QUIC_SENT_PACKET_MANAGER_H_
681