Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / quic / quic_sent_packet_manager.h
blob342dd3d4653144005b9ad3ae86db254a8ba58c10
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_
8 #include <deque>
9 #include <list>
10 #include <map>
11 #include <queue>
12 #include <set>
13 #include <utility>
14 #include <vector>
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_sustained_bandwidth_recorder.h"
25 #include "net/quic/quic_unacked_packet_map.h"
27 namespace net {
29 namespace test {
30 class QuicConnectionPeer;
31 class QuicSentPacketManagerPeer;
32 } // namespace test
34 class QuicClock;
35 class QuicConfig;
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 {
44 public:
45 // Interface which gets callbacks from the QuicSentPacketManager at
46 // interesting points. Implementations must not mutate the state of
47 // the packet manager or connection as a result of these callbacks.
48 class NET_EXPORT_PRIVATE DebugDelegate {
49 public:
50 virtual ~DebugDelegate() {}
52 // Called when a spurious retransmission is detected.
53 virtual void OnSpuriousPacketRetransmition(
54 TransmissionType transmission_type,
55 QuicByteCount byte_size) {}
57 virtual void OnSentPacket(
58 QuicPacketSequenceNumber sequence_number,
59 QuicTime sent_time,
60 QuicByteCount bytes) {}
62 virtual void OnRetransmittedPacket(
63 QuicPacketSequenceNumber old_sequence_number,
64 QuicPacketSequenceNumber new_sequence_number,
65 TransmissionType transmission_type,
66 QuicTime time) {}
68 virtual void OnIncomingAck(
69 const QuicAckFrame& ack_frame,
70 QuicTime ack_receive_time,
71 QuicPacketSequenceNumber largest_observed,
72 bool largest_observed_acked,
73 QuicPacketSequenceNumber least_unacked_sent_packet) {}
76 // Interface which gets callbacks from the QuicSentPacketManager when
77 // network-related state changes. Implementations must not mutate the
78 // state of the packet manager as a result of these callbacks.
79 class NET_EXPORT_PRIVATE NetworkChangeVisitor {
80 public:
81 virtual ~NetworkChangeVisitor() {}
83 // Called when congestion window may have changed.
84 virtual void OnCongestionWindowChange(QuicByteCount congestion_window) = 0;
85 // TODO(jri): Add OnRttStatsChange() to this class as well.
88 // Struct to store the pending retransmission information.
89 struct PendingRetransmission {
90 PendingRetransmission(QuicPacketSequenceNumber sequence_number,
91 TransmissionType transmission_type,
92 const RetransmittableFrames& retransmittable_frames,
93 QuicSequenceNumberLength sequence_number_length)
94 : sequence_number(sequence_number),
95 transmission_type(transmission_type),
96 retransmittable_frames(retransmittable_frames),
97 sequence_number_length(sequence_number_length) {
100 QuicPacketSequenceNumber sequence_number;
101 TransmissionType transmission_type;
102 const RetransmittableFrames& retransmittable_frames;
103 QuicSequenceNumberLength sequence_number_length;
106 QuicSentPacketManager(bool is_server,
107 const QuicClock* clock,
108 QuicConnectionStats* stats,
109 CongestionControlType congestion_control_type,
110 LossDetectionType loss_type);
111 virtual ~QuicSentPacketManager();
113 virtual void SetFromConfig(const QuicConfig& config);
115 void SetHandshakeConfirmed() { handshake_confirmed_ = true; }
117 // Called when a new packet is serialized. If the packet contains
118 // retransmittable data, it will be added to the unacked packet map.
119 void OnSerializedPacket(const SerializedPacket& serialized_packet);
121 // Called when a packet is retransmitted with a new sequence number.
122 // Replaces the old entry in the unacked packet map with the new
123 // sequence number.
124 void OnRetransmittedPacket(QuicPacketSequenceNumber old_sequence_number,
125 QuicPacketSequenceNumber new_sequence_number);
127 // Processes the incoming ack.
128 void OnIncomingAck(const QuicAckFrame& ack_frame,
129 QuicTime ack_receive_time);
131 // Returns true if the non-FEC packet |sequence_number| is unacked.
132 bool IsUnacked(QuicPacketSequenceNumber sequence_number) const;
134 // Requests retransmission of all unacked packets of |retransmission_type|.
135 void RetransmitUnackedPackets(RetransmissionType retransmission_type);
137 // Retransmits the oldest pending packet there is still a tail loss probe
138 // pending. Invoked after OnRetransmissionTimeout.
139 bool MaybeRetransmitTailLossProbe();
141 // Removes the retransmittable frames from all unencrypted packets to ensure
142 // they don't get retransmitted.
143 void NeuterUnencryptedPackets();
145 // Returns true if the unacked packet |sequence_number| has retransmittable
146 // frames. This will only return false if the packet has been acked, if a
147 // previous transmission of this packet was ACK'd, or if this packet has been
148 // retransmitted as with different sequence number.
149 bool HasRetransmittableFrames(QuicPacketSequenceNumber sequence_number) const;
151 // Returns true if there are pending retransmissions.
152 bool HasPendingRetransmissions() const;
154 // Retrieves the next pending retransmission.
155 PendingRetransmission NextPendingRetransmission();
157 bool HasUnackedPackets() const;
159 // Returns the smallest sequence number of a serialized packet which has not
160 // been acked by the peer. If there are no unacked packets, returns 0.
161 QuicPacketSequenceNumber GetLeastUnackedSentPacket() const;
163 // Called when a congestion feedback frame is received from peer.
164 virtual void OnIncomingQuicCongestionFeedbackFrame(
165 const QuicCongestionFeedbackFrame& frame,
166 const QuicTime& feedback_receive_time);
168 // Called when we have sent bytes to the peer. This informs the manager both
169 // the number of bytes sent and if they were retransmitted. Returns true if
170 // the sender should reset the retransmission timer.
171 virtual bool OnPacketSent(QuicPacketSequenceNumber sequence_number,
172 QuicTime sent_time,
173 QuicByteCount bytes,
174 TransmissionType transmission_type,
175 HasRetransmittableData has_retransmittable_data);
177 // Called when the retransmission timer expires.
178 virtual void OnRetransmissionTimeout();
180 // Calculate the time until we can send the next packet to the wire.
181 // Note 1: When kUnknownWaitTime is returned, there is no need to poll
182 // TimeUntilSend again until we receive an OnIncomingAckFrame event.
183 // Note 2: Send algorithms may or may not use |retransmit| in their
184 // calculations.
185 virtual QuicTime::Delta TimeUntilSend(QuicTime now,
186 HasRetransmittableData retransmittable);
188 // Returns amount of time for delayed ack timer.
189 const QuicTime::Delta DelayedAckTime() const;
191 // Returns the current delay for the retransmission timer, which may send
192 // either a tail loss probe or do a full RTO. Returns QuicTime::Zero() if
193 // there are no retransmittable packets.
194 const QuicTime GetRetransmissionTime() const;
196 const RttStats* GetRttStats() const;
198 // Returns the estimated bandwidth calculated by the congestion algorithm.
199 QuicBandwidth BandwidthEstimate() const;
201 // Returns true if the current instantaneous bandwidth estimate is reliable.
202 bool HasReliableBandwidthEstimate() const;
204 const QuicSustainedBandwidthRecorder& SustainedBandwidthRecorder() const;
206 // Returns the size of the current congestion window in bytes. Note, this is
207 // not the *available* window. Some send algorithms may not use a congestion
208 // window and will return 0.
209 QuicByteCount GetCongestionWindow() const;
211 // Returns the size of the slow start congestion window in bytes,
212 // aka ssthresh. Some send algorithms do not define a slow start
213 // threshold and will return 0.
214 QuicByteCount GetSlowStartThreshold() const;
216 // Enables pacing if it has not already been enabled.
217 void EnablePacing();
219 bool using_pacing() const { return using_pacing_; }
221 void set_debug_delegate(DebugDelegate* debug_delegate) {
222 debug_delegate_ = debug_delegate;
225 QuicPacketSequenceNumber largest_observed() const {
226 return unacked_packets_.largest_observed();
229 QuicPacketSequenceNumber least_packet_awaited_by_peer() {
230 return least_packet_awaited_by_peer_;
233 void set_network_change_visitor(NetworkChangeVisitor* visitor) {
234 DCHECK(!network_change_visitor_);
235 DCHECK(visitor);
236 network_change_visitor_ = visitor;
239 size_t consecutive_rto_count() const {
240 return consecutive_rto_count_;
243 size_t consecutive_tlp_count() const {
244 return consecutive_tlp_count_;
247 private:
248 friend class test::QuicConnectionPeer;
249 friend class test::QuicSentPacketManagerPeer;
251 // The retransmission timer is a single timer which switches modes depending
252 // upon connection state.
253 enum RetransmissionTimeoutMode {
254 // A conventional TCP style RTO.
255 RTO_MODE,
256 // A tail loss probe. By default, QUIC sends up to two before RTOing.
257 TLP_MODE,
258 // Retransmission of handshake packets prior to handshake completion.
259 HANDSHAKE_MODE,
260 // Re-invoke the loss detection when a packet is not acked before the
261 // loss detection algorithm expects.
262 LOSS_MODE,
265 typedef linked_hash_map<QuicPacketSequenceNumber,
266 TransmissionType> PendingRetransmissionMap;
268 // Updates the least_packet_awaited_by_peer.
269 void UpdatePacketInformationReceivedByPeer(const QuicAckFrame& ack_frame);
271 // Process the incoming ack looking for newly ack'd data packets.
272 void HandleAckForSentPackets(const QuicAckFrame& ack_frame);
274 // Returns the current retransmission mode.
275 RetransmissionTimeoutMode GetRetransmissionMode() const;
277 // Retransmits all crypto stream packets.
278 void RetransmitCryptoPackets();
280 // Retransmits all the packets and abandons by invoking a full RTO.
281 void RetransmitAllPackets();
283 // Returns the timer for retransmitting crypto handshake packets.
284 const QuicTime::Delta GetCryptoRetransmissionDelay() const;
286 // Returns the timer for a new tail loss probe.
287 const QuicTime::Delta GetTailLossProbeDelay() const;
289 // Returns the retransmission timeout, after which a full RTO occurs.
290 const QuicTime::Delta GetRetransmissionDelay() const;
292 // Update the RTT if the ack is for the largest acked sequence number.
293 // Returns true if the rtt was updated.
294 bool MaybeUpdateRTT(const QuicAckFrame& ack_frame,
295 const QuicTime& ack_receive_time);
297 // Invokes the loss detection algorithm and loses and retransmits packets if
298 // necessary.
299 void InvokeLossDetection(QuicTime time);
301 // Invokes OnCongestionEvent if |rtt_updated| is true, there are pending acks,
302 // or pending losses. Clears pending acks and pending losses afterwards.
303 // |bytes_in_flight| is the number of bytes in flight before the losses or
304 // acks.
305 void MaybeInvokeCongestionEvent(bool rtt_updated,
306 QuicByteCount bytes_in_flight);
308 // Marks |sequence_number| as having been revived by the peer, but not
309 // received, so the packet remains pending if it is and the congestion control
310 // does not consider the packet acked.
311 void MarkPacketRevived(QuicPacketSequenceNumber sequence_number,
312 QuicTime::Delta delta_largest_observed);
314 // Removes the retransmittability and pending properties from the packet at
315 // |it| due to receipt by the peer. Returns an iterator to the next remaining
316 // unacked packet.
317 QuicUnackedPacketMap::const_iterator MarkPacketHandled(
318 QuicUnackedPacketMap::const_iterator it,
319 QuicTime::Delta delta_largest_observed);
321 // Request that |sequence_number| be retransmitted after the other pending
322 // retransmissions. Does not add it to the retransmissions if it's already
323 // a pending retransmission.
324 void MarkForRetransmission(QuicPacketSequenceNumber sequence_number,
325 TransmissionType transmission_type);
327 // Notify observers about spurious retransmits.
328 void RecordSpuriousRetransmissions(
329 const SequenceNumberSet& all_transmissions,
330 QuicPacketSequenceNumber acked_sequence_number);
332 // Newly serialized retransmittable and fec packets are added to this map,
333 // which contains owning pointers to any contained frames. If a packet is
334 // retransmitted, this map will contain entries for both the old and the new
335 // packet. The old packet's retransmittable frames entry will be NULL, while
336 // the new packet's entry will contain the frames to retransmit.
337 // If the old packet is acked before the new packet, then the old entry will
338 // be removed from the map and the new entry's retransmittable frames will be
339 // set to NULL.
340 QuicUnackedPacketMap unacked_packets_;
342 // Pending retransmissions which have not been packetized and sent yet.
343 PendingRetransmissionMap pending_retransmissions_;
345 // Tracks if the connection was created by the server.
346 bool is_server_;
348 // An AckNotifier can register to be informed when ACKs have been received for
349 // all packets that a given block of data was sent in. The AckNotifierManager
350 // maintains the currently active notifiers.
351 AckNotifierManager ack_notifier_manager_;
353 const QuicClock* clock_;
354 QuicConnectionStats* stats_;
355 DebugDelegate* debug_delegate_;
356 NetworkChangeVisitor* network_change_visitor_;
357 RttStats rtt_stats_;
358 scoped_ptr<SendAlgorithmInterface> send_algorithm_;
359 scoped_ptr<LossDetectionInterface> loss_algorithm_;
361 // Least sequence number which the peer is still waiting for.
362 QuicPacketSequenceNumber least_packet_awaited_by_peer_;
364 // Tracks the first RTO packet. If any packet before that packet gets acked,
365 // it indicates the RTO was spurious and should be reversed(F-RTO).
366 QuicPacketSequenceNumber first_rto_transmission_;
367 // Number of times the RTO timer has fired in a row without receiving an ack.
368 size_t consecutive_rto_count_;
369 // Number of times the tail loss probe has been sent.
370 size_t consecutive_tlp_count_;
371 // Number of times the crypto handshake has been retransmitted.
372 size_t consecutive_crypto_retransmission_count_;
373 // Number of pending transmissions of TLP or crypto packets.
374 size_t pending_timer_transmission_count_;
375 // Maximum number of tail loss probes to send before firing an RTO.
376 size_t max_tail_loss_probes_;
377 bool using_pacing_;
379 // Sets of packets acked and lost as a result of the last congestion event.
380 SendAlgorithmInterface::CongestionMap packets_acked_;
381 SendAlgorithmInterface::CongestionMap packets_lost_;
383 // Set to true after the crypto handshake has successfully completed. After
384 // this is true we no longer use HANDSHAKE_MODE, and further frames sent on
385 // the crypto stream (i.e. SCUP messages) are treated like normal
386 // retransmittable frames.
387 bool handshake_confirmed_;
389 // Records bandwidth from server to client in normal operation, over periods
390 // of time with no loss events.
391 QuicSustainedBandwidthRecorder sustained_bandwidth_recorder_;
393 DISALLOW_COPY_AND_ASSIGN(QuicSentPacketManager);
396 } // namespace net
398 #endif // NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_