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.
5 #ifndef NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_
6 #define NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_
16 #include "base/containers/hash_tables.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "net/base/linked_hash_map.h"
19 #include "net/quic/congestion_control/send_algorithm_interface.h"
20 #include "net/quic/quic_ack_notifier_manager.h"
21 #include "net/quic/quic_protocol.h"
22 #include "net/quic/quic_unacked_packet_map.h"
24 NET_EXPORT_PRIVATE
extern bool FLAGS_track_retransmission_history
;
25 NET_EXPORT_PRIVATE
extern bool FLAGS_enable_quic_pacing
;
30 class QuicConnectionPeer
;
31 class QuicSentPacketManagerPeer
;
36 struct QuicConnectionStats
;
38 // Class which tracks the set of packets sent on a QUIC connection and contains
39 // a send algorithm to decide when to send new packets. It keeps track of any
40 // retransmittable data associated with each packet. If a packet is
41 // retransmitted, it will keep track of each version of a packet so that if a
42 // previous transmission is acked, the data will not be retransmitted.
43 class NET_EXPORT_PRIVATE QuicSentPacketManager
{
45 // Struct to store the pending retransmission information.
46 struct PendingRetransmission
{
47 PendingRetransmission(QuicPacketSequenceNumber sequence_number
,
48 TransmissionType transmission_type
,
49 const RetransmittableFrames
& retransmittable_frames
,
50 QuicSequenceNumberLength sequence_number_length
)
51 : sequence_number(sequence_number
),
52 transmission_type(transmission_type
),
53 retransmittable_frames(retransmittable_frames
),
54 sequence_number_length(sequence_number_length
) {
57 QuicPacketSequenceNumber sequence_number
;
58 TransmissionType transmission_type
;
59 const RetransmittableFrames
& retransmittable_frames
;
60 QuicSequenceNumberLength sequence_number_length
;
63 QuicSentPacketManager(bool is_server
,
64 const QuicClock
* clock
,
65 QuicConnectionStats
* stats
,
66 CongestionFeedbackType congestion_type
);
67 virtual ~QuicSentPacketManager();
69 virtual void SetFromConfig(const QuicConfig
& config
);
71 // Called when a new packet is serialized. If the packet contains
72 // retransmittable data, it will be added to the unacked packet map.
73 void OnSerializedPacket(const SerializedPacket
& serialized_packet
);
75 // Called when a packet is retransmitted with a new sequence number.
76 // Replaces the old entry in the unacked packet map with the new
78 void OnRetransmittedPacket(QuicPacketSequenceNumber old_sequence_number
,
79 QuicPacketSequenceNumber new_sequence_number
);
81 // Processes the incoming ack and returns true if the retransmission or ack
82 // alarm should be reset.
83 bool OnIncomingAck(const ReceivedPacketInfo
& received_info
,
84 QuicTime ack_receive_time
);
86 // Discards any information for the packet corresponding to |sequence_number|.
87 // If this packet has been retransmitted, information on those packets
88 // will be discarded as well. Also discards it from the congestion window if
90 void DiscardUnackedPacket(QuicPacketSequenceNumber sequence_number
);
92 // Returns true if the non-FEC packet |sequence_number| is unacked.
93 bool IsUnacked(QuicPacketSequenceNumber sequence_number
) const;
95 // Requests retransmission of all unacked packets of |retransmission_type|.
96 void RetransmitUnackedPackets(RetransmissionType retransmission_type
);
98 // Returns true if the unacked packet |sequence_number| has retransmittable
99 // frames. This will only return false if the packet has been acked, if a
100 // previous transmission of this packet was ACK'd, or if this packet has been
101 // retransmitted as with different sequence number.
102 bool HasRetransmittableFrames(QuicPacketSequenceNumber sequence_number
) const;
104 // Returns true if there are pending retransmissions.
105 bool HasPendingRetransmissions() const;
107 // Retrieves the next pending retransmission.
108 PendingRetransmission
NextPendingRetransmission();
110 bool HasUnackedPackets() const;
112 // Returns the smallest sequence number of a serialized packet which has not
113 // been acked by the peer. If there are no unacked packets, returns 0.
114 QuicPacketSequenceNumber
GetLeastUnackedSentPacket() const;
116 // Called when a congestion feedback frame is received from peer.
117 virtual void OnIncomingQuicCongestionFeedbackFrame(
118 const QuicCongestionFeedbackFrame
& frame
,
119 const QuicTime
& feedback_receive_time
);
121 // Called when we have sent bytes to the peer. This informs the manager both
122 // the number of bytes sent and if they were retransmitted. Returns true if
123 // the sender should reset the retransmission timer.
124 virtual bool OnPacketSent(QuicPacketSequenceNumber sequence_number
,
127 TransmissionType transmission_type
,
128 HasRetransmittableData has_retransmittable_data
);
130 // Called when the retransmission timer expires.
131 virtual void OnRetransmissionTimeout();
133 // Calculate the time until we can send the next packet to the wire.
134 // Note 1: When kUnknownWaitTime is returned, there is no need to poll
135 // TimeUntilSend again until we receive an OnIncomingAckFrame event.
136 // Note 2: Send algorithms may or may not use |retransmit| in their
138 virtual QuicTime::Delta
TimeUntilSend(QuicTime now
,
139 TransmissionType transmission_type
,
140 HasRetransmittableData retransmittable
,
141 IsHandshake handshake
);
143 // Returns amount of time for delayed ack timer.
144 const QuicTime::Delta
DelayedAckTime() const;
146 // Returns the current delay for the retransmission timer, which may send
147 // either a tail loss probe or do a full RTO. Returns QuicTime::Zero() if
148 // there are no retransmittable packets.
149 const QuicTime
GetRetransmissionTime() const;
151 // Returns the estimated smoothed RTT calculated by the congestion algorithm.
152 const QuicTime::Delta
SmoothedRtt() const;
154 // Returns the estimated bandwidth calculated by the congestion algorithm.
155 QuicBandwidth
BandwidthEstimate() const;
157 // Returns the size of the current congestion window in bytes. Note, this is
158 // not the *available* window. Some send algorithms may not use a congestion
159 // window and will return 0.
160 QuicByteCount
GetCongestionWindow() const;
162 // Enables pacing if it has not already been enabled, and if
163 // FLAGS_enable_quic_pacing is set.
164 void MaybeEnablePacing();
166 bool using_pacing() const { return using_pacing_
; }
169 friend class test::QuicConnectionPeer
;
170 friend class test::QuicSentPacketManagerPeer
;
172 enum ReceivedByPeer
{
174 NOT_RECEIVED_BY_PEER
,
177 enum RetransmissionTimeoutMode
{
183 typedef linked_hash_map
<QuicPacketSequenceNumber
,
184 TransmissionType
> PendingRetransmissionMap
;
186 // Process the incoming ack looking for newly ack'd data packets.
187 void HandleAckForSentPackets(const ReceivedPacketInfo
& received_info
);
189 // Called when a packet is timed out, such as an RTO. Removes the bytes from
190 // the congestion manager, but does not change the congestion window size.
191 void OnPacketAbandoned(QuicPacketSequenceNumber sequence_number
);
193 // Returns the current retransmission mode.
194 RetransmissionTimeoutMode
GetRetransmissionMode() const;
196 // Retransmits all crypto stream packets.
197 void RetransmitCryptoPackets();
199 // Retransmits the oldest pending packet.
200 void RetransmitOldestPacket();
202 // Retransmits all the packets and abandons by invoking a full RTO.
203 void RetransmitAllPackets();
205 // Returns the timer for retransmitting crypto handshake packets.
206 const QuicTime::Delta
GetCryptoRetransmissionDelay() const;
208 // Returns the timer for a new tail loss probe.
209 const QuicTime::Delta
GetTailLossProbeDelay() const;
211 // Returns the retransmission timeout, after which a full RTO occurs.
212 const QuicTime::Delta
GetRetransmissionDelay() const;
214 // Update the RTT if the ack is for the largest acked sequence number.
215 void MaybeUpdateRTT(const ReceivedPacketInfo
& received_info
,
216 const QuicTime
& ack_receive_time
);
218 // Chooses whether to nack retransmit any packets based on the receipt info.
219 // All acks have been handled before this method is invoked.
220 void MaybeRetransmitOnAckFrame(const ReceivedPacketInfo
& received_info
,
221 const QuicTime
& ack_receive_time
);
223 // Marks |sequence_number| as being fully handled, either due to receipt
224 // by the peer, or having been discarded as indecipherable. Returns an
225 // iterator to the next remaining unacked packet.
226 QuicUnackedPacketMap::const_iterator
MarkPacketHandled(
227 QuicPacketSequenceNumber sequence_number
,
228 ReceivedByPeer received_by_peer
);
230 // Request that |sequence_number| be retransmitted after the other pending
231 // retransmissions. Does not add it to the retransmissions if it's already
232 // a pending retransmission.
233 void MarkForRetransmission(QuicPacketSequenceNumber sequence_number
,
234 TransmissionType transmission_type
);
236 static SequenceNumberSet
DetectLostPackets(
237 const QuicUnackedPacketMap
& unacked_packets
,
238 const QuicTime
& time
,
239 QuicPacketSequenceNumber largest_observed
);
241 // Newly serialized retransmittable and fec packets are added to this map,
242 // which contains owning pointers to any contained frames. If a packet is
243 // retransmitted, this map will contain entries for both the old and the new
244 // packet. The old packet's retransmittable frames entry will be NULL, while
245 // the new packet's entry will contain the frames to retransmit.
246 // If the old packet is acked before the new packet, then the old entry will
247 // be removed from the map and the new entry's retransmittable frames will be
249 QuicUnackedPacketMap unacked_packets_
;
251 // Pending retransmissions which have not been packetized and sent yet.
252 PendingRetransmissionMap pending_retransmissions_
;
254 // Tracks if the connection was created by the server.
257 // An AckNotifier can register to be informed when ACKs have been received for
258 // all packets that a given block of data was sent in. The AckNotifierManager
259 // maintains the currently active notifiers.
260 AckNotifierManager ack_notifier_manager_
;
262 const QuicClock
* clock_
;
263 QuicConnectionStats
* stats_
;
264 scoped_ptr
<SendAlgorithmInterface
> send_algorithm_
;
265 QuicTime::Delta rtt_sample_
; // RTT estimate from the most recent ACK.
266 // Number of outstanding crypto handshake packets.
267 size_t pending_crypto_packet_count_
;
268 // Number of times the RTO timer has fired in a row without receiving an ack.
269 size_t consecutive_rto_count_
;
270 // Number of times the tail loss probe has been sent.
271 size_t consecutive_tlp_count_
;
272 // Number of times the crypto handshake has been retransmitted.
273 size_t consecutive_crypto_retransmission_count_
;
274 // Maximum number of tail loss probes to send before firing an RTO.
275 size_t max_tail_loss_probes_
;
278 DISALLOW_COPY_AND_ASSIGN(QuicSentPacketManager
);
283 #endif // NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_