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 QUIC_EXPORT_PRIVATE 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 peer_least_packet_awaiting_ack()83 QuicPacketNumber peer_least_packet_awaiting_ack() const { 84 return peer_least_packet_awaiting_ack_; 85 } 86 87 virtual bool ack_frame_updated() const; 88 89 QuicPacketNumber GetLargestObserved() const; 90 91 // Returns peer first sending packet number to our best knowledge. Considers 92 // least_received_packet_number_ as peer first sending packet number. Please 93 // note, this function should only be called when at least one packet has been 94 // received. 95 QuicPacketNumber PeerFirstSendingPacketNumber() const; 96 97 // Returns true if ack frame is empty. 98 bool IsAckFrameEmpty() const; 99 set_connection_stats(QuicConnectionStats * stats)100 void set_connection_stats(QuicConnectionStats* stats) { stats_ = stats; } 101 102 // For logging purposes. ack_frame()103 const QuicAckFrame& ack_frame() const { return ack_frame_; } 104 set_max_ack_ranges(size_t max_ack_ranges)105 void set_max_ack_ranges(size_t max_ack_ranges) { 106 max_ack_ranges_ = max_ack_ranges; 107 } 108 set_save_timestamps(bool save_timestamps,bool in_order_packets_only)109 void set_save_timestamps(bool save_timestamps, bool in_order_packets_only) { 110 save_timestamps_ = save_timestamps; 111 save_timestamps_for_in_order_packets_ = in_order_packets_only; 112 } 113 min_received_before_ack_decimation()114 size_t min_received_before_ack_decimation() const { 115 return min_received_before_ack_decimation_; 116 } set_min_received_before_ack_decimation(size_t new_value)117 void set_min_received_before_ack_decimation(size_t new_value) { 118 min_received_before_ack_decimation_ = new_value; 119 } 120 set_ack_frequency(size_t new_value)121 void set_ack_frequency(size_t new_value) { 122 QUICHE_DCHECK_GT(new_value, 0u); 123 ack_frequency_ = new_value; 124 } 125 set_local_max_ack_delay(QuicTime::Delta local_max_ack_delay)126 void set_local_max_ack_delay(QuicTime::Delta local_max_ack_delay) { 127 local_max_ack_delay_ = local_max_ack_delay; 128 } 129 ack_timeout()130 QuicTime ack_timeout() const { return ack_timeout_; } 131 132 void OnAckFrequencyFrame(const QuicAckFrequencyFrame& frame); 133 134 private: 135 friend class test::QuicConnectionPeer; 136 friend class test::QuicReceivedPacketManagerPeer; 137 friend class test::UberReceivedPacketManagerPeer; 138 139 // Sets ack_timeout_ to |time| if ack_timeout_ is not initialized or > time. 140 void MaybeUpdateAckTimeoutTo(QuicTime time); 141 142 // Maybe update ack_frequency_ when condition meets. 143 void MaybeUpdateAckFrequency(QuicPacketNumber last_received_packet_number); 144 145 QuicTime::Delta GetMaxAckDelay(QuicPacketNumber last_received_packet_number, 146 const RttStats& rtt_stats) const; 147 AckFrequencyFrameReceived()148 bool AckFrequencyFrameReceived() const { 149 return last_ack_frequency_frame_sequence_number_ >= 0; 150 } 151 152 // Least packet number of the the packet sent by the peer for which it 153 // hasn't received an ack. 154 QuicPacketNumber peer_least_packet_awaiting_ack_; 155 156 // Received packet information used to produce acks. 157 QuicAckFrame ack_frame_; 158 159 // True if |ack_frame_| has been updated since UpdateReceivedPacketInfo was 160 // last called. 161 bool ack_frame_updated_; 162 163 // Maximum number of ack ranges allowed to be stored in the ack frame. 164 size_t max_ack_ranges_; 165 166 // The time we received the largest_observed packet number, or zero if 167 // no packet numbers have been received since UpdateReceivedPacketInfo. 168 // Needed for calculating ack_delay_time. 169 QuicTime time_largest_observed_; 170 171 // If true, save timestamps in the ack_frame_. 172 bool save_timestamps_; 173 174 // If true and |save_timestamps_|, only save timestamps for packets that are 175 // received in order. 176 bool save_timestamps_for_in_order_packets_; 177 178 // Least packet number received from peer. 179 QuicPacketNumber least_received_packet_number_; 180 181 QuicConnectionStats* stats_; 182 183 // How many retransmittable packets have arrived without sending an ack. 184 QuicPacketCount num_retransmittable_packets_received_since_last_ack_sent_; 185 // Ack decimation will start happening after this many packets are received. 186 size_t min_received_before_ack_decimation_; 187 // Ack every n-th packet. 188 size_t ack_frequency_; 189 // The max delay in fraction of min_rtt to use when sending decimated acks. 190 float ack_decimation_delay_; 191 // When true, removes ack decimation's max number of packets(10) before 192 // sending an ack. 193 bool unlimited_ack_decimation_; 194 // When true, only send 1 immediate ACK when reordering is detected. 195 bool one_immediate_ack_; 196 // When true, do not ack immediately upon observation of packet reordering. 197 bool ignore_order_; 198 199 // The local node's maximum ack delay time. This is the maximum amount of 200 // time to wait before sending an acknowledgement. 201 QuicTime::Delta local_max_ack_delay_; 202 // Time that an ACK needs to be sent. 0 means no ACK is pending. Used when 203 // decide_when_to_send_acks_ is true. 204 QuicTime ack_timeout_; 205 206 // The time the previous ack-instigating packet was received and processed. 207 QuicTime time_of_previous_received_packet_; 208 // Whether the most recent packet was missing before it was received. 209 bool was_last_packet_missing_; 210 211 // Last sent largest acked, which gets updated when ACK was successfully sent. 212 QuicPacketNumber last_sent_largest_acked_; 213 214 // The sequence number of the last received AckFrequencyFrame. Negative if 215 // none received. 216 int64_t last_ack_frequency_frame_sequence_number_; 217 }; 218 219 } // namespace quic 220 221 #endif // QUICHE_QUIC_CORE_QUIC_RECEIVED_PACKET_MANAGER_H_ 222