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 "net/third_party/quiche/src/quic/core/quic_config.h"
9 #include "net/third_party/quiche/src/quic/core/quic_framer.h"
10 #include "net/third_party/quiche/src/quic/core/quic_packets.h"
11 #include "net/third_party/quiche/src/quic/platform/api/quic_export.h"
12 
13 namespace quic {
14 
15 class RttStats;
16 
17 namespace test {
18 class QuicConnectionPeer;
19 class QuicReceivedPacketManagerPeer;
20 class UberReceivedPacketManagerPeer;
21 }  // namespace test
22 
23 struct QuicConnectionStats;
24 
25 // Records all received packets by a connection.
26 class QUIC_EXPORT_PRIVATE QuicReceivedPacketManager {
27  public:
28   QuicReceivedPacketManager();
29   explicit QuicReceivedPacketManager(QuicConnectionStats* stats);
30   QuicReceivedPacketManager(const QuicReceivedPacketManager&) = delete;
31   QuicReceivedPacketManager& operator=(const QuicReceivedPacketManager&) =
32       delete;
33   virtual ~QuicReceivedPacketManager();
34 
35   void SetFromConfig(const QuicConfig& config, Perspective perspective);
36 
37   // Updates the internal state concerning which packets have been received.
38   // header: the packet header.
39   // timestamp: the arrival time of the packet.
40   virtual void RecordPacketReceived(const QuicPacketHeader& header,
41                                     QuicTime receipt_time);
42 
43   // Checks whether |packet_number| is missing and less than largest observed.
44   virtual bool IsMissing(QuicPacketNumber packet_number);
45 
46   // Checks if we're still waiting for the packet with |packet_number|.
47   virtual bool IsAwaitingPacket(QuicPacketNumber packet_number) const;
48 
49   // Retrieves a frame containing a QuicAckFrame.  The ack frame may not be
50   // changed outside QuicReceivedPacketManager and must be serialized before
51   // another packet is received, or it will change.
52   const QuicFrame GetUpdatedAckFrame(QuicTime approximate_now);
53 
54   // Deletes all missing packets before least unacked. The connection won't
55   // process any packets with packet number before |least_unacked| that it
56   // received after this call.
57   void DontWaitForPacketsBefore(QuicPacketNumber least_unacked);
58 
59   // Called to update ack_timeout_ to the time when an ACK needs to be sent. A
60   // caller can decide whether and when to send an ACK by retrieving
61   // ack_timeout_. If ack_timeout_ is not initialized, no ACK needs to be sent.
62   // Otherwise, ACK needs to be sent by the specified time.
63   void MaybeUpdateAckTimeout(bool should_last_packet_instigate_acks,
64                              QuicPacketNumber last_received_packet_number,
65                              QuicTime time_of_last_received_packet,
66                              QuicTime now,
67                              const RttStats* rtt_stats);
68 
69   // Resets ACK related states, called after an ACK is successfully sent.
70   void ResetAckStates();
71 
72   // Returns true if there are any missing packets.
73   bool HasMissingPackets() const;
74 
75   // Returns true when there are new missing packets to be reported within 3
76   // packets of the largest observed.
77   virtual bool HasNewMissingPackets() const;
78 
peer_least_packet_awaiting_ack()79   QuicPacketNumber peer_least_packet_awaiting_ack() const {
80     return peer_least_packet_awaiting_ack_;
81   }
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)105   void set_save_timestamps(bool save_timestamps) {
106     save_timestamps_ = save_timestamps;
107   }
108 
min_received_before_ack_decimation()109   size_t min_received_before_ack_decimation() const {
110     return min_received_before_ack_decimation_;
111   }
set_min_received_before_ack_decimation(size_t new_value)112   void set_min_received_before_ack_decimation(size_t new_value) {
113     min_received_before_ack_decimation_ = new_value;
114   }
115 
ack_frequency_before_ack_decimation()116   size_t ack_frequency_before_ack_decimation() const {
117     return ack_frequency_before_ack_decimation_;
118   }
set_ack_frequency_before_ack_decimation(size_t new_value)119   void set_ack_frequency_before_ack_decimation(size_t new_value) {
120     DCHECK_GT(new_value, 0u);
121     ack_frequency_before_ack_decimation_ = new_value;
122   }
123 
local_max_ack_delay()124   QuicTime::Delta local_max_ack_delay() const { return local_max_ack_delay_; }
set_local_max_ack_delay(QuicTime::Delta local_max_ack_delay)125   void set_local_max_ack_delay(QuicTime::Delta local_max_ack_delay) {
126     local_max_ack_delay_ = local_max_ack_delay;
127   }
128 
ack_timeout()129   QuicTime ack_timeout() const { return ack_timeout_; }
130 
131  private:
132   friend class test::QuicConnectionPeer;
133   friend class test::QuicReceivedPacketManagerPeer;
134   friend class test::UberReceivedPacketManagerPeer;
135 
136   // Sets ack_timeout_ to |time| if ack_timeout_ is not initialized or > time.
137   void MaybeUpdateAckTimeoutTo(QuicTime time);
138 
139   // Least packet number of the the packet sent by the peer for which it
140   // hasn't received an ack.
141   QuicPacketNumber peer_least_packet_awaiting_ack_;
142 
143   // Received packet information used to produce acks.
144   QuicAckFrame ack_frame_;
145 
146   // True if |ack_frame_| has been updated since UpdateReceivedPacketInfo was
147   // last called.
148   bool ack_frame_updated_;
149 
150   // Maximum number of ack ranges allowed to be stored in the ack frame.
151   size_t max_ack_ranges_;
152 
153   // The time we received the largest_observed packet number, or zero if
154   // no packet numbers have been received since UpdateReceivedPacketInfo.
155   // Needed for calculating ack_delay_time.
156   QuicTime time_largest_observed_;
157 
158   // If true, save timestamps in the ack_frame_.
159   bool save_timestamps_;
160 
161   // Least packet number received from peer.
162   QuicPacketNumber least_received_packet_number_;
163 
164   QuicConnectionStats* stats_;
165 
166   AckMode ack_mode_;
167   // How many retransmittable packets have arrived without sending an ack.
168   QuicPacketCount num_retransmittable_packets_received_since_last_ack_sent_;
169   // Ack decimation will start happening after this many packets are received.
170   size_t min_received_before_ack_decimation_;
171   // Before ack decimation starts (if enabled), we ack every n-th packet.
172   size_t ack_frequency_before_ack_decimation_;
173   // The max delay in fraction of min_rtt to use when sending decimated acks.
174   float ack_decimation_delay_;
175   // When true, removes ack decimation's max number of packets(10) before
176   // sending an ack.
177   bool unlimited_ack_decimation_;
178   // When true, use a 1ms delayed ack timer if it's been an SRTT since a packet
179   // was received.
180   bool fast_ack_after_quiescence_;
181   // When true, only send 1 immediate ACK when reordering is detected.
182   bool one_immediate_ack_;
183 
184   // The local node's maximum ack delay time. This is the maximum amount of
185   // time to wait before sending an acknowledgement.
186   QuicTime::Delta local_max_ack_delay_;
187   // Time that an ACK needs to be sent. 0 means no ACK is pending. Used when
188   // decide_when_to_send_acks_ is true.
189   QuicTime ack_timeout_;
190 
191   // The time the previous ack-instigating packet was received and processed.
192   QuicTime time_of_previous_received_packet_;
193   // Whether the most recent packet was missing before it was received.
194   bool was_last_packet_missing_;
195 
196   // Last sent largest acked, which gets updated when ACK was successfully sent.
197   QuicPacketNumber last_sent_largest_acked_;
198 };
199 
200 }  // namespace quic
201 
202 #endif  // QUICHE_QUIC_CORE_QUIC_RECEIVED_PACKET_MANAGER_H_
203