xref: /aosp_15_r20/external/cronet/net/third_party/quiche/src/quiche/quic/core/quic_received_packet_manager.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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_RECEIVED_PACKET_MANAGER_H_
6 #define QUICHE_QUIC_CORE_QUIC_RECEIVED_PACKET_MANAGER_H_
7 
8 #include <cstddef>
9 
10 #include "quiche/quic/core/frames/quic_ack_frequency_frame.h"
11 #include "quiche/quic/core/quic_config.h"
12 #include "quiche/quic/core/quic_framer.h"
13 #include "quiche/quic/core/quic_packets.h"
14 #include "quiche/quic/core/quic_types.h"
15 #include "quiche/quic/platform/api/quic_export.h"
16 
17 namespace quic {
18 
19 class RttStats;
20 
21 namespace test {
22 class QuicConnectionPeer;
23 class QuicReceivedPacketManagerPeer;
24 class UberReceivedPacketManagerPeer;
25 }  // namespace test
26 
27 struct QuicConnectionStats;
28 
29 // Records all received packets by a connection.
30 class QUICHE_EXPORT QuicReceivedPacketManager {
31  public:
32   QuicReceivedPacketManager();
33   explicit QuicReceivedPacketManager(QuicConnectionStats* stats);
34   QuicReceivedPacketManager(const QuicReceivedPacketManager&) = delete;
35   QuicReceivedPacketManager& operator=(const QuicReceivedPacketManager&) =
36       delete;
37   virtual ~QuicReceivedPacketManager();
38 
39   void SetFromConfig(const QuicConfig& config, Perspective perspective);
40 
41   // Updates the internal state concerning which packets have been received.
42   // header: the packet header.
43   // timestamp: the arrival time of the packet.
44   virtual void RecordPacketReceived(const QuicPacketHeader& header,
45                                     QuicTime receipt_time,
46                                     QuicEcnCodepoint ecn);
47 
48   // Checks whether |packet_number| is missing and less than largest observed.
49   virtual bool IsMissing(QuicPacketNumber packet_number);
50 
51   // Checks if we're still waiting for the packet with |packet_number|.
52   virtual bool IsAwaitingPacket(QuicPacketNumber packet_number) const;
53 
54   // Retrieves a frame containing a QuicAckFrame.  The ack frame may not be
55   // changed outside QuicReceivedPacketManager and must be serialized before
56   // another packet is received, or it will change.
57   const QuicFrame GetUpdatedAckFrame(QuicTime approximate_now);
58 
59   // Deletes all missing packets before least unacked. The connection won't
60   // process any packets with packet number before |least_unacked| that it
61   // received after this call.
62   void DontWaitForPacketsBefore(QuicPacketNumber least_unacked);
63 
64   // Called to update ack_timeout_ to the time when an ACK needs to be sent. A
65   // caller can decide whether and when to send an ACK by retrieving
66   // ack_timeout_. If ack_timeout_ is not initialized, no ACK needs to be sent.
67   // Otherwise, ACK needs to be sent by the specified time.
68   void MaybeUpdateAckTimeout(bool should_last_packet_instigate_acks,
69                              QuicPacketNumber last_received_packet_number,
70                              QuicTime last_packet_receipt_time, QuicTime now,
71                              const RttStats* rtt_stats);
72 
73   // Resets ACK related states, called after an ACK is successfully sent.
74   void ResetAckStates();
75 
76   // Returns true if there are any missing packets.
77   bool HasMissingPackets() const;
78 
79   // Returns true when there are new missing packets to be reported within 3
80   // packets of the largest observed.
81   virtual bool HasNewMissingPackets() const;
82 
83   virtual bool ack_frame_updated() const;
84 
85   QuicPacketNumber GetLargestObserved() const;
86 
87   // Returns peer first sending packet number to our best knowledge. Considers
88   // least_received_packet_number_ as peer first sending packet number. Please
89   // note, this function should only be called when at least one packet has been
90   // received.
91   QuicPacketNumber PeerFirstSendingPacketNumber() const;
92 
93   // Returns true if ack frame is empty.
94   bool IsAckFrameEmpty() const;
95 
set_connection_stats(QuicConnectionStats * stats)96   void set_connection_stats(QuicConnectionStats* stats) { stats_ = stats; }
97 
98   // For logging purposes.
ack_frame()99   const QuicAckFrame& ack_frame() const { return ack_frame_; }
100 
set_max_ack_ranges(size_t max_ack_ranges)101   void set_max_ack_ranges(size_t max_ack_ranges) {
102     max_ack_ranges_ = max_ack_ranges;
103   }
104 
set_save_timestamps(bool save_timestamps,bool in_order_packets_only)105   void set_save_timestamps(bool save_timestamps, bool in_order_packets_only) {
106     save_timestamps_ = save_timestamps;
107     save_timestamps_for_in_order_packets_ = in_order_packets_only;
108   }
109 
min_received_before_ack_decimation()110   size_t min_received_before_ack_decimation() const {
111     return min_received_before_ack_decimation_;
112   }
set_min_received_before_ack_decimation(size_t new_value)113   void set_min_received_before_ack_decimation(size_t new_value) {
114     min_received_before_ack_decimation_ = new_value;
115   }
116 
set_ack_frequency(size_t new_value)117   void set_ack_frequency(size_t new_value) {
118     QUICHE_DCHECK_GT(new_value, 0u);
119     ack_frequency_ = new_value;
120   }
121 
set_local_max_ack_delay(QuicTime::Delta local_max_ack_delay)122   void set_local_max_ack_delay(QuicTime::Delta local_max_ack_delay) {
123     local_max_ack_delay_ = local_max_ack_delay;
124   }
125 
ack_timeout()126   QuicTime ack_timeout() const { return ack_timeout_; }
127 
128   void OnAckFrequencyFrame(const QuicAckFrequencyFrame& frame);
129 
130  private:
131   friend class test::QuicConnectionPeer;
132   friend class test::QuicReceivedPacketManagerPeer;
133   friend class test::UberReceivedPacketManagerPeer;
134 
135   // Sets ack_timeout_ to |time| if ack_timeout_ is not initialized or > time.
136   void MaybeUpdateAckTimeoutTo(QuicTime time);
137 
138   // Maybe update ack_frequency_ when condition meets.
139   void MaybeUpdateAckFrequency(QuicPacketNumber last_received_packet_number);
140 
141   QuicTime::Delta GetMaxAckDelay(QuicPacketNumber last_received_packet_number,
142                                  const RttStats& rtt_stats) const;
143 
AckFrequencyFrameReceived()144   bool AckFrequencyFrameReceived() const {
145     return last_ack_frequency_frame_sequence_number_ >= 0;
146   }
147 
148   void MaybeTrimAckRanges();
149 
150   // Least packet number of the the packet sent by the peer for which it
151   // hasn't received an ack.
152   QuicPacketNumber peer_least_packet_awaiting_ack_;
153 
154   // Received packet information used to produce acks.
155   QuicAckFrame ack_frame_;
156 
157   // True if |ack_frame_| has been updated since UpdateReceivedPacketInfo was
158   // last called.
159   bool ack_frame_updated_;
160 
161   // Maximum number of ack ranges allowed to be stored in the ack frame.
162   size_t max_ack_ranges_;
163 
164   // The time we received the largest_observed packet number, or zero if
165   // no packet numbers have been received since UpdateReceivedPacketInfo.
166   // Needed for calculating ack_delay_time.
167   QuicTime time_largest_observed_;
168 
169   // If true, save timestamps in the ack_frame_.
170   bool save_timestamps_;
171 
172   // If true and |save_timestamps_|, only save timestamps for packets that are
173   // received in order.
174   bool save_timestamps_for_in_order_packets_;
175 
176   // Least packet number received from peer.
177   QuicPacketNumber least_received_packet_number_;
178 
179   QuicConnectionStats* stats_;
180 
181   // How many retransmittable packets have arrived without sending an ack.
182   QuicPacketCount num_retransmittable_packets_received_since_last_ack_sent_;
183   // Ack decimation will start happening after this many packets are received.
184   size_t min_received_before_ack_decimation_;
185   // Ack every n-th packet.
186   size_t ack_frequency_;
187   // The max delay in fraction of min_rtt to use when sending decimated acks.
188   float ack_decimation_delay_;
189   // When true, removes ack decimation's max number of packets(10) before
190   // sending an ack.
191   bool unlimited_ack_decimation_;
192   // When true, only send 1 immediate ACK when reordering is detected.
193   bool one_immediate_ack_;
194   // When true, do not ack immediately upon observation of packet reordering.
195   bool ignore_order_;
196 
197   // The local node's maximum ack delay time. This is the maximum amount of
198   // time to wait before sending an acknowledgement.
199   QuicTime::Delta local_max_ack_delay_;
200   // Time that an ACK needs to be sent. 0 means no ACK is pending. Used when
201   // decide_when_to_send_acks_ is true.
202   QuicTime ack_timeout_;
203 
204   // The time the previous ack-instigating packet was received and processed.
205   QuicTime time_of_previous_received_packet_;
206   // Whether the most recent packet was missing before it was received.
207   bool was_last_packet_missing_;
208 
209   // Last sent largest acked, which gets updated when ACK was successfully sent.
210   QuicPacketNumber last_sent_largest_acked_;
211 
212   // The sequence number of the last received AckFrequencyFrame. Negative if
213   // none received.
214   int64_t last_ack_frequency_frame_sequence_number_;
215 };
216 
217 }  // namespace quic
218 
219 #endif  // QUICHE_QUIC_CORE_QUIC_RECEIVED_PACKET_MANAGER_H_
220