Lots of random cleanups, mostly for native_theme_win.cc:
[chromium-blink-merge.git] / net / quic / quic_sent_packet_manager.h
blob89a3b6d26a93e6ce344ab6d1fb1254cdaf3fef11
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) {}
57 // Struct to store the pending retransmission information.
58 struct PendingRetransmission {
59 PendingRetransmission(QuicPacketSequenceNumber sequence_number,
60 TransmissionType transmission_type,
61 const RetransmittableFrames& retransmittable_frames,
62 QuicSequenceNumberLength sequence_number_length)
63 : sequence_number(sequence_number),
64 transmission_type(transmission_type),
65 retransmittable_frames(retransmittable_frames),
66 sequence_number_length(sequence_number_length) {
69 QuicPacketSequenceNumber sequence_number;
70 TransmissionType transmission_type;
71 const RetransmittableFrames& retransmittable_frames;
72 QuicSequenceNumberLength sequence_number_length;
75 QuicSentPacketManager(bool is_server,
76 const QuicClock* clock,
77 QuicConnectionStats* stats,
78 CongestionFeedbackType congestion_type,
79 LossDetectionType loss_type);
80 virtual ~QuicSentPacketManager();
82 virtual void SetFromConfig(const QuicConfig& config);
84 // Called when a new packet is serialized. If the packet contains
85 // retransmittable data, it will be added to the unacked packet map.
86 void OnSerializedPacket(const SerializedPacket& serialized_packet);
88 // Called when a packet is retransmitted with a new sequence number.
89 // Replaces the old entry in the unacked packet map with the new
90 // sequence number.
91 void OnRetransmittedPacket(QuicPacketSequenceNumber old_sequence_number,
92 QuicPacketSequenceNumber new_sequence_number);
94 // Processes the incoming ack.
95 void OnIncomingAck(const ReceivedPacketInfo& received_info,
96 QuicTime ack_receive_time);
98 // Returns true if the non-FEC packet |sequence_number| is unacked.
99 bool IsUnacked(QuicPacketSequenceNumber sequence_number) const;
101 // Requests retransmission of all unacked packets of |retransmission_type|.
102 void RetransmitUnackedPackets(RetransmissionType retransmission_type);
104 // Retransmits the oldest pending packet there is still a tail loss probe
105 // pending. Invoked after OnRetransmissionTimeout.
106 bool MaybeRetransmitTailLossProbe();
108 // Removes the retransmittable frames from all unencrypted packets to ensure
109 // they don't get retransmitted.
110 void NeuterUnencryptedPackets();
112 // Returns true if the unacked packet |sequence_number| has retransmittable
113 // frames. This will only return false if the packet has been acked, if a
114 // previous transmission of this packet was ACK'd, or if this packet has been
115 // retransmitted as with different sequence number.
116 bool HasRetransmittableFrames(QuicPacketSequenceNumber sequence_number) const;
118 // Returns true if there are pending retransmissions.
119 bool HasPendingRetransmissions() const;
121 // Retrieves the next pending retransmission.
122 PendingRetransmission NextPendingRetransmission();
124 bool HasUnackedPackets() const;
126 // Returns the smallest sequence number of a serialized packet which has not
127 // been acked by the peer. If there are no unacked packets, returns 0.
128 QuicPacketSequenceNumber GetLeastUnackedSentPacket() const;
130 // Called when a congestion feedback frame is received from peer.
131 virtual void OnIncomingQuicCongestionFeedbackFrame(
132 const QuicCongestionFeedbackFrame& frame,
133 const QuicTime& feedback_receive_time);
135 // Called when we have sent bytes to the peer. This informs the manager both
136 // the number of bytes sent and if they were retransmitted. Returns true if
137 // the sender should reset the retransmission timer.
138 virtual bool OnPacketSent(QuicPacketSequenceNumber sequence_number,
139 QuicTime sent_time,
140 QuicByteCount bytes,
141 TransmissionType transmission_type,
142 HasRetransmittableData has_retransmittable_data);
144 // Called when the retransmission timer expires.
145 virtual void OnRetransmissionTimeout();
147 // Calculate the time until we can send the next packet to the wire.
148 // Note 1: When kUnknownWaitTime is returned, there is no need to poll
149 // TimeUntilSend again until we receive an OnIncomingAckFrame event.
150 // Note 2: Send algorithms may or may not use |retransmit| in their
151 // calculations.
152 virtual QuicTime::Delta TimeUntilSend(QuicTime now,
153 HasRetransmittableData retransmittable);
155 // Returns amount of time for delayed ack timer.
156 const QuicTime::Delta DelayedAckTime() const;
158 // Returns the current delay for the retransmission timer, which may send
159 // either a tail loss probe or do a full RTO. Returns QuicTime::Zero() if
160 // there are no retransmittable packets.
161 const QuicTime GetRetransmissionTime() const;
163 const RttStats* GetRttStats() const;
165 // Returns the estimated bandwidth calculated by the congestion algorithm.
166 QuicBandwidth BandwidthEstimate() const;
168 // Returns true if the current bandwidth estimate is reliable.
169 bool HasReliableBandwidthEstimate() const;
171 // Returns the size of the current congestion window in bytes. Note, this is
172 // not the *available* window. Some send algorithms may not use a congestion
173 // window and will return 0.
174 QuicByteCount GetCongestionWindow() const;
176 // Enables pacing if it has not already been enabled, and if
177 // FLAGS_enable_quic_pacing is set.
178 void MaybeEnablePacing();
180 bool using_pacing() const { return using_pacing_; }
182 void set_debug_delegate(DebugDelegate* debug_delegate) {
183 debug_delegate_ = debug_delegate;
186 QuicPacketSequenceNumber largest_observed() const {
187 return largest_observed_;
190 private:
191 friend class test::QuicConnectionPeer;
192 friend class test::QuicSentPacketManagerPeer;
194 // The retransmission timer is a single timer which switches modes depending
195 // upon connection state.
196 enum RetransmissionTimeoutMode {
197 // A conventional TCP style RTO.
198 RTO_MODE,
199 // A tail loss probe. By default, QUIC sends up to two before RTOing.
200 TLP_MODE,
201 // Retransmission of handshake packets prior to handshake completion.
202 HANDSHAKE_MODE,
203 // Re-invoke the loss detection when a packet is not acked before the
204 // loss detection algorithm expects.
205 LOSS_MODE,
208 typedef linked_hash_map<QuicPacketSequenceNumber,
209 TransmissionType> PendingRetransmissionMap;
211 // Process the incoming ack looking for newly ack'd data packets.
212 void HandleAckForSentPackets(const ReceivedPacketInfo& received_info);
214 // Returns the current retransmission mode.
215 RetransmissionTimeoutMode GetRetransmissionMode() const;
217 // Retransmits all crypto stream packets.
218 void RetransmitCryptoPackets();
220 // Retransmits all the packets and abandons by invoking a full RTO.
221 void RetransmitAllPackets();
223 // Returns the timer for retransmitting crypto handshake packets.
224 const QuicTime::Delta GetCryptoRetransmissionDelay() const;
226 // Returns the timer for a new tail loss probe.
227 const QuicTime::Delta GetTailLossProbeDelay() const;
229 // Returns the retransmission timeout, after which a full RTO occurs.
230 const QuicTime::Delta GetRetransmissionDelay() const;
232 // Update the RTT if the ack is for the largest acked sequence number.
233 // Returns true if the rtt was updated.
234 bool MaybeUpdateRTT(const ReceivedPacketInfo& received_info,
235 const QuicTime& ack_receive_time);
237 // Invokes the loss detection algorithm and loses and retransmits packets if
238 // necessary.
239 void InvokeLossDetection(QuicTime time);
241 // Invokes OnCongestionEvent if |rtt_updated| is true, there are pending acks,
242 // or pending losses. Clears pending acks and pending losses afterwards.
243 // |bytes_in_flight| is the number of bytes in flight before the losses or
244 // acks.
245 void MaybeInvokeCongestionEvent(bool rtt_updated,
246 QuicByteCount bytes_in_flight);
248 // Marks |sequence_number| as having been revived by the peer, but not
249 // received, so the packet remains pending if it is and the congestion control
250 // does not consider the packet acked.
251 void MarkPacketRevived(QuicPacketSequenceNumber sequence_number,
252 QuicTime::Delta delta_largest_observed);
254 // Removes the retransmittability and pending properties from the packet at
255 // |it| due to receipt by the peer. Returns an iterator to the next remaining
256 // unacked packet.
257 QuicUnackedPacketMap::const_iterator MarkPacketHandled(
258 QuicUnackedPacketMap::const_iterator it,
259 QuicTime::Delta delta_largest_observed);
261 // Request that |sequence_number| be retransmitted after the other pending
262 // retransmissions. Does not add it to the retransmissions if it's already
263 // a pending retransmission.
264 void MarkForRetransmission(QuicPacketSequenceNumber sequence_number,
265 TransmissionType transmission_type);
267 // Notify observers about spurious retransmits.
268 void RecordSpuriousRetransmissions(
269 const SequenceNumberSet& all_transmissions,
270 QuicPacketSequenceNumber acked_sequence_number);
272 // Newly serialized retransmittable and fec packets are added to this map,
273 // which contains owning pointers to any contained frames. If a packet is
274 // retransmitted, this map will contain entries for both the old and the new
275 // packet. The old packet's retransmittable frames entry will be NULL, while
276 // the new packet's entry will contain the frames to retransmit.
277 // If the old packet is acked before the new packet, then the old entry will
278 // be removed from the map and the new entry's retransmittable frames will be
279 // set to NULL.
280 QuicUnackedPacketMap unacked_packets_;
282 // Pending retransmissions which have not been packetized and sent yet.
283 PendingRetransmissionMap pending_retransmissions_;
285 // Tracks if the connection was created by the server.
286 bool is_server_;
288 // An AckNotifier can register to be informed when ACKs have been received for
289 // all packets that a given block of data was sent in. The AckNotifierManager
290 // maintains the currently active notifiers.
291 AckNotifierManager ack_notifier_manager_;
293 const QuicClock* clock_;
294 QuicConnectionStats* stats_;
295 DebugDelegate* debug_delegate_;
296 RttStats rtt_stats_;
297 scoped_ptr<SendAlgorithmInterface> send_algorithm_;
298 scoped_ptr<LossDetectionInterface> loss_algorithm_;
300 // The largest sequence number which we have sent and received an ACK for
301 // from the peer.
302 QuicPacketSequenceNumber largest_observed_;
304 // Tracks the first RTO packet. If any packet before that packet gets acked,
305 // it indicates the RTO was spurious and should be reversed(F-RTO).
306 QuicPacketSequenceNumber first_rto_transmission_;
307 // Number of times the RTO timer has fired in a row without receiving an ack.
308 size_t consecutive_rto_count_;
309 // Number of times the tail loss probe has been sent.
310 size_t consecutive_tlp_count_;
311 // Number of times the crypto handshake has been retransmitted.
312 size_t consecutive_crypto_retransmission_count_;
313 // Whether a tlp packet can be sent even if the send algorithm says not to.
314 bool pending_tlp_transmission_;
315 // Maximum number of tail loss probes to send before firing an RTO.
316 size_t max_tail_loss_probes_;
317 bool using_pacing_;
319 // Sets of packets acked and lost as a result of the last congestion event.
320 SendAlgorithmInterface::CongestionMap packets_acked_;
321 SendAlgorithmInterface::CongestionMap packets_lost_;
323 DISALLOW_COPY_AND_ASSIGN(QuicSentPacketManager);
326 } // namespace net
328 #endif // NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_