Test interaction between DevToolsWindow and BrowserView.
[chromium-blink-merge.git] / net / quic / quic_sent_packet_manager.h
blob4ebe1516a04dc8845ba4b69174c07ea532cd46ef
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_unacked_packet_map.h"
26 namespace net {
28 namespace test {
29 class QuicConnectionPeer;
30 class QuicSentPacketManagerPeer;
31 } // namespace test
33 class QuicClock;
34 class QuicConfig;
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 {
43 public:
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 {
48 public:
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,
58 QuicTime sent_time,
59 QuicByteCount bytes) {}
61 virtual void OnRetransmittedPacket(
62 QuicPacketSequenceNumber old_sequence_number,
63 QuicPacketSequenceNumber new_sequence_number,
64 TransmissionType transmission_type,
65 QuicTime time) {}
67 virtual void OnIncomingAck(
68 const ReceivedPacketInfo& received_info,
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 {
79 public:
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 // Called when a new packet is serialized. If the packet contains
115 // retransmittable data, it will be added to the unacked packet map.
116 void OnSerializedPacket(const SerializedPacket& serialized_packet);
118 // Called when a packet is retransmitted with a new sequence number.
119 // Replaces the old entry in the unacked packet map with the new
120 // sequence number.
121 void OnRetransmittedPacket(QuicPacketSequenceNumber old_sequence_number,
122 QuicPacketSequenceNumber new_sequence_number);
124 // Processes the incoming ack.
125 void OnIncomingAck(const ReceivedPacketInfo& received_info,
126 QuicTime ack_receive_time);
128 // Returns true if the non-FEC packet |sequence_number| is unacked.
129 bool IsUnacked(QuicPacketSequenceNumber sequence_number) const;
131 // Requests retransmission of all unacked packets of |retransmission_type|.
132 void RetransmitUnackedPackets(RetransmissionType retransmission_type);
134 // Retransmits the oldest pending packet there is still a tail loss probe
135 // pending. Invoked after OnRetransmissionTimeout.
136 bool MaybeRetransmitTailLossProbe();
138 // Removes the retransmittable frames from all unencrypted packets to ensure
139 // they don't get retransmitted.
140 void NeuterUnencryptedPackets();
142 // Returns true if the unacked packet |sequence_number| has retransmittable
143 // frames. This will only return false if the packet has been acked, if a
144 // previous transmission of this packet was ACK'd, or if this packet has been
145 // retransmitted as with different sequence number.
146 bool HasRetransmittableFrames(QuicPacketSequenceNumber sequence_number) const;
148 // Returns true if there are pending retransmissions.
149 bool HasPendingRetransmissions() const;
151 // Retrieves the next pending retransmission.
152 PendingRetransmission NextPendingRetransmission();
154 bool HasUnackedPackets() const;
156 // Returns the smallest sequence number of a serialized packet which has not
157 // been acked by the peer. If there are no unacked packets, returns 0.
158 QuicPacketSequenceNumber GetLeastUnackedSentPacket() const;
160 // Called when a congestion feedback frame is received from peer.
161 virtual void OnIncomingQuicCongestionFeedbackFrame(
162 const QuicCongestionFeedbackFrame& frame,
163 const QuicTime& feedback_receive_time);
165 // Called when we have sent bytes to the peer. This informs the manager both
166 // the number of bytes sent and if they were retransmitted. Returns true if
167 // the sender should reset the retransmission timer.
168 virtual bool OnPacketSent(QuicPacketSequenceNumber sequence_number,
169 QuicTime sent_time,
170 QuicByteCount bytes,
171 TransmissionType transmission_type,
172 HasRetransmittableData has_retransmittable_data);
174 // Called when the retransmission timer expires.
175 virtual void OnRetransmissionTimeout();
177 // Calculate the time until we can send the next packet to the wire.
178 // Note 1: When kUnknownWaitTime is returned, there is no need to poll
179 // TimeUntilSend again until we receive an OnIncomingAckFrame event.
180 // Note 2: Send algorithms may or may not use |retransmit| in their
181 // calculations.
182 virtual QuicTime::Delta TimeUntilSend(QuicTime now,
183 HasRetransmittableData retransmittable);
185 // Returns amount of time for delayed ack timer.
186 const QuicTime::Delta DelayedAckTime() const;
188 // Returns the current delay for the retransmission timer, which may send
189 // either a tail loss probe or do a full RTO. Returns QuicTime::Zero() if
190 // there are no retransmittable packets.
191 const QuicTime GetRetransmissionTime() const;
193 const RttStats* GetRttStats() const;
195 // Returns the estimated bandwidth calculated by the congestion algorithm.
196 QuicBandwidth BandwidthEstimate() const;
198 // Returns true if the current bandwidth estimate is reliable.
199 bool HasReliableBandwidthEstimate() const;
201 // Returns the size of the current congestion window in bytes. Note, this is
202 // not the *available* window. Some send algorithms may not use a congestion
203 // window and will return 0.
204 QuicByteCount GetCongestionWindow() const;
206 // Returns the size of the slow start congestion window in bytes,
207 // aka ssthresh. Some send algorithms do not define a slow start
208 // threshold and will return 0.
209 QuicByteCount GetSlowStartThreshold() const;
211 // Enables pacing if it has not already been enabled, and if
212 // FLAGS_enable_quic_pacing is set.
213 void MaybeEnablePacing();
215 bool using_pacing() const { return using_pacing_; }
217 void set_debug_delegate(DebugDelegate* debug_delegate) {
218 debug_delegate_ = debug_delegate;
221 QuicPacketSequenceNumber largest_observed() const {
222 return largest_observed_;
225 void set_network_change_visitor(NetworkChangeVisitor* visitor) {
226 DCHECK(!network_change_visitor_);
227 DCHECK(visitor);
228 network_change_visitor_ = visitor;
231 private:
232 friend class test::QuicConnectionPeer;
233 friend class test::QuicSentPacketManagerPeer;
235 // The retransmission timer is a single timer which switches modes depending
236 // upon connection state.
237 enum RetransmissionTimeoutMode {
238 // A conventional TCP style RTO.
239 RTO_MODE,
240 // A tail loss probe. By default, QUIC sends up to two before RTOing.
241 TLP_MODE,
242 // Retransmission of handshake packets prior to handshake completion.
243 HANDSHAKE_MODE,
244 // Re-invoke the loss detection when a packet is not acked before the
245 // loss detection algorithm expects.
246 LOSS_MODE,
249 typedef linked_hash_map<QuicPacketSequenceNumber,
250 TransmissionType> PendingRetransmissionMap;
252 // Process the incoming ack looking for newly ack'd data packets.
253 void HandleAckForSentPackets(const ReceivedPacketInfo& received_info);
255 // Returns the current retransmission mode.
256 RetransmissionTimeoutMode GetRetransmissionMode() const;
258 // Retransmits all crypto stream packets.
259 void RetransmitCryptoPackets();
261 // Retransmits all the packets and abandons by invoking a full RTO.
262 void RetransmitAllPackets();
264 // Returns the timer for retransmitting crypto handshake packets.
265 const QuicTime::Delta GetCryptoRetransmissionDelay() const;
267 // Returns the timer for a new tail loss probe.
268 const QuicTime::Delta GetTailLossProbeDelay() const;
270 // Returns the retransmission timeout, after which a full RTO occurs.
271 const QuicTime::Delta GetRetransmissionDelay() const;
273 // Update the RTT if the ack is for the largest acked sequence number.
274 // Returns true if the rtt was updated.
275 bool MaybeUpdateRTT(const ReceivedPacketInfo& received_info,
276 const QuicTime& ack_receive_time);
278 // Invokes the loss detection algorithm and loses and retransmits packets if
279 // necessary.
280 void InvokeLossDetection(QuicTime time);
282 // Invokes OnCongestionEvent if |rtt_updated| is true, there are pending acks,
283 // or pending losses. Clears pending acks and pending losses afterwards.
284 // |bytes_in_flight| is the number of bytes in flight before the losses or
285 // acks.
286 void MaybeInvokeCongestionEvent(bool rtt_updated,
287 QuicByteCount bytes_in_flight);
289 // Marks |sequence_number| as having been revived by the peer, but not
290 // received, so the packet remains pending if it is and the congestion control
291 // does not consider the packet acked.
292 void MarkPacketRevived(QuicPacketSequenceNumber sequence_number,
293 QuicTime::Delta delta_largest_observed);
295 // Removes the retransmittability and pending properties from the packet at
296 // |it| due to receipt by the peer. Returns an iterator to the next remaining
297 // unacked packet.
298 QuicUnackedPacketMap::const_iterator MarkPacketHandled(
299 QuicUnackedPacketMap::const_iterator it,
300 QuicTime::Delta delta_largest_observed);
302 // Request that |sequence_number| be retransmitted after the other pending
303 // retransmissions. Does not add it to the retransmissions if it's already
304 // a pending retransmission.
305 void MarkForRetransmission(QuicPacketSequenceNumber sequence_number,
306 TransmissionType transmission_type);
308 // Notify observers about spurious retransmits.
309 void RecordSpuriousRetransmissions(
310 const SequenceNumberSet& all_transmissions,
311 QuicPacketSequenceNumber acked_sequence_number);
313 // Newly serialized retransmittable and fec packets are added to this map,
314 // which contains owning pointers to any contained frames. If a packet is
315 // retransmitted, this map will contain entries for both the old and the new
316 // packet. The old packet's retransmittable frames entry will be NULL, while
317 // the new packet's entry will contain the frames to retransmit.
318 // If the old packet is acked before the new packet, then the old entry will
319 // be removed from the map and the new entry's retransmittable frames will be
320 // set to NULL.
321 QuicUnackedPacketMap unacked_packets_;
323 // Pending retransmissions which have not been packetized and sent yet.
324 PendingRetransmissionMap pending_retransmissions_;
326 // Tracks if the connection was created by the server.
327 bool is_server_;
329 // An AckNotifier can register to be informed when ACKs have been received for
330 // all packets that a given block of data was sent in. The AckNotifierManager
331 // maintains the currently active notifiers.
332 AckNotifierManager ack_notifier_manager_;
334 const QuicClock* clock_;
335 QuicConnectionStats* stats_;
336 DebugDelegate* debug_delegate_;
337 NetworkChangeVisitor* network_change_visitor_;
338 RttStats rtt_stats_;
339 scoped_ptr<SendAlgorithmInterface> send_algorithm_;
340 scoped_ptr<LossDetectionInterface> loss_algorithm_;
342 // The largest sequence number which we have sent and received an ACK for
343 // from the peer.
344 QuicPacketSequenceNumber largest_observed_;
346 // Tracks the first RTO packet. If any packet before that packet gets acked,
347 // it indicates the RTO was spurious and should be reversed(F-RTO).
348 QuicPacketSequenceNumber first_rto_transmission_;
349 // Number of times the RTO timer has fired in a row without receiving an ack.
350 size_t consecutive_rto_count_;
351 // Number of times the tail loss probe has been sent.
352 size_t consecutive_tlp_count_;
353 // Number of times the crypto handshake has been retransmitted.
354 size_t consecutive_crypto_retransmission_count_;
355 // Whether a tlp packet can be sent even if the send algorithm says not to.
356 bool pending_tlp_transmission_;
357 // Maximum number of tail loss probes to send before firing an RTO.
358 size_t max_tail_loss_probes_;
359 bool using_pacing_;
361 // Sets of packets acked and lost as a result of the last congestion event.
362 SendAlgorithmInterface::CongestionMap packets_acked_;
363 SendAlgorithmInterface::CongestionMap packets_lost_;
365 DISALLOW_COPY_AND_ASSIGN(QuicSentPacketManager);
368 } // namespace net
370 #endif // NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_