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/loss_detection_interface.h"
20 #include "net/quic/congestion_control/rtt_stats.h"
21 #include "net/quic/congestion_control/send_algorithm_interface.h"
22 #include "net/quic/quic_ack_notifier_manager.h"
23 #include "net/quic/quic_protocol.h"
24 #include "net/quic/quic_unacked_packet_map.h"
29 class QuicConnectionPeer;
30 class QuicSentPacketManagerPeer;
35 struct QuicConnectionStats;
37 // Class which tracks the set of packets sent on a QUIC connection and contains
38 // a send algorithm to decide when to send new packets. It keeps track of any
39 // retransmittable data associated with each packet. If a packet is
40 // retransmitted, it will keep track of each version of a packet so that if a
41 // previous transmission is acked, the data will not be retransmitted.
42 class NET_EXPORT_PRIVATE QuicSentPacketManager {
44 // Interface which gets callbacks from the QuicSentPacketManager at
45 // interesting points. Implementations must not mutate the state of
46 // the packet manager or connection as a result of these callbacks.
47 class NET_EXPORT_PRIVATE DebugDelegate {
49 virtual ~DebugDelegate() {}
51 // Called when a spurious retransmission is detected.
52 virtual void OnSpuriousPacketRetransmition(
53 TransmissionType transmission_type,
54 QuicByteCount byte_size) {}
56 virtual void OnSentPacket(
57 QuicPacketSequenceNumber sequence_number,
59 QuicByteCount bytes) {}
61 virtual void OnRetransmittedPacket(
62 QuicPacketSequenceNumber old_sequence_number,
63 QuicPacketSequenceNumber new_sequence_number,
64 TransmissionType transmission_type,
67 virtual void OnIncomingAck(
68 const QuicAckFrame& ack_frame,
69 QuicTime ack_receive_time,
70 QuicPacketSequenceNumber largest_observed,
71 bool largest_observed_acked,
72 QuicPacketSequenceNumber least_unacked_sent_packet) {}
75 // Interface which gets callbacks from the QuicSentPacketManager when
76 // network-related state changes. Implementations must not mutate the
77 // state of the packet manager as a result of these callbacks.
78 class NET_EXPORT_PRIVATE NetworkChangeVisitor {
80 virtual ~NetworkChangeVisitor() {}
82 // Called when congestion window may have changed.
83 virtual void OnCongestionWindowChange(QuicByteCount congestion_window) = 0;
84 // TODO(jri): Add OnRttStatsChange() to this class as well.
87 // Struct to store the pending retransmission information.
88 struct PendingRetransmission {
89 PendingRetransmission(QuicPacketSequenceNumber sequence_number,
90 TransmissionType transmission_type,
91 const RetransmittableFrames& retransmittable_frames,
92 QuicSequenceNumberLength sequence_number_length)
93 : sequence_number(sequence_number),
94 transmission_type(transmission_type),
95 retransmittable_frames(retransmittable_frames),
96 sequence_number_length(sequence_number_length) {
99 QuicPacketSequenceNumber sequence_number;
100 TransmissionType transmission_type;
101 const RetransmittableFrames& retransmittable_frames;
102 QuicSequenceNumberLength sequence_number_length;
105 QuicSentPacketManager(bool is_server,
106 const QuicClock* clock,
107 QuicConnectionStats* stats,
108 CongestionControlType congestion_control_type,
109 LossDetectionType loss_type);
110 virtual ~QuicSentPacketManager();
112 virtual void SetFromConfig(const QuicConfig& config);
114 void SetHandshakeConfirmed() { handshake_confirmed_ = true; }
116 // Called when a new packet is serialized. If the packet contains
117 // retransmittable data, it will be added to the unacked packet map.
118 void OnSerializedPacket(const SerializedPacket& serialized_packet);
120 // Called when a packet is retransmitted with a new sequence number.
121 // Replaces the old entry in the unacked packet map with the new
123 void OnRetransmittedPacket(QuicPacketSequenceNumber old_sequence_number,
124 QuicPacketSequenceNumber new_sequence_number);
126 // Processes the incoming ack.
127 void OnIncomingAck(const QuicAckFrame& ack_frame,
128 QuicTime ack_receive_time);
130 // Returns true if the non-FEC packet |sequence_number| is unacked.
131 bool IsUnacked(QuicPacketSequenceNumber sequence_number) const;
133 // Requests retransmission of all unacked packets of |retransmission_type|.
134 void RetransmitUnackedPackets(RetransmissionType retransmission_type);
136 // Retransmits the oldest pending packet there is still a tail loss probe
137 // pending. Invoked after OnRetransmissionTimeout.
138 bool MaybeRetransmitTailLossProbe();
140 // Removes the retransmittable frames from all unencrypted packets to ensure
141 // they don't get retransmitted.
142 void NeuterUnencryptedPackets();
144 // Returns true if the unacked packet |sequence_number| has retransmittable
145 // frames. This will only return false if the packet has been acked, if a
146 // previous transmission of this packet was ACK'd, or if this packet has been
147 // retransmitted as with different sequence number.
148 bool HasRetransmittableFrames(QuicPacketSequenceNumber sequence_number) const;
150 // Returns true if there are pending retransmissions.
151 bool HasPendingRetransmissions() const;
153 // Retrieves the next pending retransmission.
154 PendingRetransmission NextPendingRetransmission();
156 bool HasUnackedPackets() const;
158 // Returns the smallest sequence number of a serialized packet which has not
159 // been acked by the peer. If there are no unacked packets, returns 0.
160 QuicPacketSequenceNumber GetLeastUnackedSentPacket() const;
162 // Called when a congestion feedback frame is received from peer.
163 virtual void OnIncomingQuicCongestionFeedbackFrame(
164 const QuicCongestionFeedbackFrame& frame,
165 const QuicTime& feedback_receive_time);
167 // Called when we have sent bytes to the peer. This informs the manager both
168 // the number of bytes sent and if they were retransmitted. Returns true if
169 // the sender should reset the retransmission timer.
170 virtual bool OnPacketSent(QuicPacketSequenceNumber sequence_number,
173 TransmissionType transmission_type,
174 HasRetransmittableData has_retransmittable_data);
176 // Called when the retransmission timer expires.
177 virtual void OnRetransmissionTimeout();
179 // Calculate the time until we can send the next packet to the wire.
180 // Note 1: When kUnknownWaitTime is returned, there is no need to poll
181 // TimeUntilSend again until we receive an OnIncomingAckFrame event.
182 // Note 2: Send algorithms may or may not use |retransmit| in their
184 virtual QuicTime::Delta TimeUntilSend(QuicTime now,
185 HasRetransmittableData retransmittable);
187 // Returns amount of time for delayed ack timer.
188 const QuicTime::Delta DelayedAckTime() const;
190 // Returns the current delay for the retransmission timer, which may send
191 // either a tail loss probe or do a full RTO. Returns QuicTime::Zero() if
192 // there are no retransmittable packets.
193 const QuicTime GetRetransmissionTime() const;
195 const RttStats* GetRttStats() const;
197 // Returns the estimated bandwidth calculated by the congestion algorithm.
198 QuicBandwidth BandwidthEstimate() const;
200 // Returns true if the current bandwidth estimate is reliable.
201 bool HasReliableBandwidthEstimate() const;
203 // Returns the size of the current congestion window in bytes. Note, this is
204 // not the *available* window. Some send algorithms may not use a congestion
205 // window and will return 0.
206 QuicByteCount GetCongestionWindow() const;
208 // Returns the size of the slow start congestion window in bytes,
209 // aka ssthresh. Some send algorithms do not define a slow start
210 // threshold and will return 0.
211 QuicByteCount GetSlowStartThreshold() const;
213 // Enables pacing if it has not already been enabled, and if
214 // FLAGS_enable_quic_pacing is set.
215 void MaybeEnablePacing();
217 bool using_pacing() const { return using_pacing_; }
219 void set_debug_delegate(DebugDelegate* debug_delegate) {
220 debug_delegate_ = debug_delegate;
223 QuicPacketSequenceNumber largest_observed() const {
224 return largest_observed_;
227 void set_network_change_visitor(NetworkChangeVisitor* visitor) {
228 DCHECK(!network_change_visitor_);
230 network_change_visitor_ = visitor;
234 friend class test::QuicConnectionPeer;
235 friend class test::QuicSentPacketManagerPeer;
237 // The retransmission timer is a single timer which switches modes depending
238 // upon connection state.
239 enum RetransmissionTimeoutMode {
240 // A conventional TCP style RTO.
242 // A tail loss probe. By default, QUIC sends up to two before RTOing.
244 // Retransmission of handshake packets prior to handshake completion.
246 // Re-invoke the loss detection when a packet is not acked before the
247 // loss detection algorithm expects.
251 typedef linked_hash_map<QuicPacketSequenceNumber,
252 TransmissionType> PendingRetransmissionMap;
254 // Process the incoming ack looking for newly ack'd data packets.
255 void HandleAckForSentPackets(const QuicAckFrame& ack_frame);
257 // Returns the current retransmission mode.
258 RetransmissionTimeoutMode GetRetransmissionMode() const;
260 // Retransmits all crypto stream packets.
261 void RetransmitCryptoPackets();
263 // Retransmits all the packets and abandons by invoking a full RTO.
264 void RetransmitAllPackets();
266 // Returns the timer for retransmitting crypto handshake packets.
267 const QuicTime::Delta GetCryptoRetransmissionDelay() const;
269 // Returns the timer for a new tail loss probe.
270 const QuicTime::Delta GetTailLossProbeDelay() const;
272 // Returns the retransmission timeout, after which a full RTO occurs.
273 const QuicTime::Delta GetRetransmissionDelay() const;
275 // Update the RTT if the ack is for the largest acked sequence number.
276 // Returns true if the rtt was updated.
277 bool MaybeUpdateRTT(const QuicAckFrame& ack_frame,
278 const QuicTime& ack_receive_time);
280 // Invokes the loss detection algorithm and loses and retransmits packets if
282 void InvokeLossDetection(QuicTime time);
284 // Invokes OnCongestionEvent if |rtt_updated| is true, there are pending acks,
285 // or pending losses. Clears pending acks and pending losses afterwards.
286 // |bytes_in_flight| is the number of bytes in flight before the losses or
288 void MaybeInvokeCongestionEvent(bool rtt_updated,
289 QuicByteCount bytes_in_flight);
291 // Marks |sequence_number| as having been revived by the peer, but not
292 // received, so the packet remains pending if it is and the congestion control
293 // does not consider the packet acked.
294 void MarkPacketRevived(QuicPacketSequenceNumber sequence_number,
295 QuicTime::Delta delta_largest_observed);
297 // Removes the retransmittability and pending properties from the packet at
298 // |it| due to receipt by the peer. Returns an iterator to the next remaining
300 QuicUnackedPacketMap::const_iterator MarkPacketHandled(
301 QuicUnackedPacketMap::const_iterator it,
302 QuicTime::Delta delta_largest_observed);
304 // Request that |sequence_number| be retransmitted after the other pending
305 // retransmissions. Does not add it to the retransmissions if it's already
306 // a pending retransmission.
307 void MarkForRetransmission(QuicPacketSequenceNumber sequence_number,
308 TransmissionType transmission_type);
310 // Notify observers about spurious retransmits.
311 void RecordSpuriousRetransmissions(
312 const SequenceNumberSet& all_transmissions,
313 QuicPacketSequenceNumber acked_sequence_number);
315 // Newly serialized retransmittable and fec packets are added to this map,
316 // which contains owning pointers to any contained frames. If a packet is
317 // retransmitted, this map will contain entries for both the old and the new
318 // packet. The old packet's retransmittable frames entry will be NULL, while
319 // the new packet's entry will contain the frames to retransmit.
320 // If the old packet is acked before the new packet, then the old entry will
321 // be removed from the map and the new entry's retransmittable frames will be
323 QuicUnackedPacketMap unacked_packets_;
325 // Pending retransmissions which have not been packetized and sent yet.
326 PendingRetransmissionMap pending_retransmissions_;
328 // Tracks if the connection was created by the server.
331 // An AckNotifier can register to be informed when ACKs have been received for
332 // all packets that a given block of data was sent in. The AckNotifierManager
333 // maintains the currently active notifiers.
334 AckNotifierManager ack_notifier_manager_;
336 const QuicClock* clock_;
337 QuicConnectionStats* stats_;
338 DebugDelegate* debug_delegate_;
339 NetworkChangeVisitor* network_change_visitor_;
341 scoped_ptr<SendAlgorithmInterface> send_algorithm_;
342 scoped_ptr<LossDetectionInterface> loss_algorithm_;
344 // The largest sequence number which we have sent and received an ACK for
346 QuicPacketSequenceNumber largest_observed_;
348 // Tracks the first RTO packet. If any packet before that packet gets acked,
349 // it indicates the RTO was spurious and should be reversed(F-RTO).
350 QuicPacketSequenceNumber first_rto_transmission_;
351 // Number of times the RTO timer has fired in a row without receiving an ack.
352 size_t consecutive_rto_count_;
353 // Number of times the tail loss probe has been sent.
354 size_t consecutive_tlp_count_;
355 // Number of times the crypto handshake has been retransmitted.
356 size_t consecutive_crypto_retransmission_count_;
357 // Whether a tlp packet can be sent even if the send algorithm says not to.
358 bool pending_tlp_transmission_;
359 // Maximum number of tail loss probes to send before firing an RTO.
360 size_t max_tail_loss_probes_;
363 // Sets of packets acked and lost as a result of the last congestion event.
364 SendAlgorithmInterface::CongestionMap packets_acked_;
365 SendAlgorithmInterface::CongestionMap packets_lost_;
367 // Set to true after the crypto handshake has successfully completed. After
368 // this is true we no longer use HANDSHAKE_MODE, and further frames sent on
369 // the crypto stream (i.e. SCUP messages) are treated like normal
370 // retransmittable frames.
371 bool handshake_confirmed_;
373 DISALLOW_COPY_AND_ASSIGN(QuicSentPacketManager);
378 #endif // NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_