Land Recent QUIC Changes
[chromium-blink-merge.git] / net / quic / quic_sent_packet_manager.h
blob512fa4883a285d571cec53d38f6ce48c25fbfb96
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 // Struct to store the pending retransmission information.
45 struct PendingRetransmission {
46 PendingRetransmission(QuicPacketSequenceNumber sequence_number,
47 TransmissionType transmission_type,
48 const RetransmittableFrames& retransmittable_frames,
49 QuicSequenceNumberLength sequence_number_length)
50 : sequence_number(sequence_number),
51 transmission_type(transmission_type),
52 retransmittable_frames(retransmittable_frames),
53 sequence_number_length(sequence_number_length) {
56 QuicPacketSequenceNumber sequence_number;
57 TransmissionType transmission_type;
58 const RetransmittableFrames& retransmittable_frames;
59 QuicSequenceNumberLength sequence_number_length;
62 QuicSentPacketManager(bool is_server,
63 const QuicClock* clock,
64 QuicConnectionStats* stats,
65 CongestionFeedbackType congestion_type);
66 virtual ~QuicSentPacketManager();
68 virtual void SetFromConfig(const QuicConfig& config);
70 // Called when a new packet is serialized. If the packet contains
71 // retransmittable data, it will be added to the unacked packet map.
72 void OnSerializedPacket(const SerializedPacket& serialized_packet);
74 // Called when a packet is retransmitted with a new sequence number.
75 // Replaces the old entry in the unacked packet map with the new
76 // sequence number.
77 void OnRetransmittedPacket(QuicPacketSequenceNumber old_sequence_number,
78 QuicPacketSequenceNumber new_sequence_number);
80 // Processes the incoming ack and returns true if the retransmission or ack
81 // alarm should be reset.
82 bool OnIncomingAck(const ReceivedPacketInfo& received_info,
83 QuicTime ack_receive_time);
85 // Discards any information for the packet corresponding to |sequence_number|.
86 // If this packet has been retransmitted, information on those packets
87 // will be discarded as well. Also discards it from the congestion window if
88 // it is present.
89 void DiscardUnackedPacket(QuicPacketSequenceNumber sequence_number);
91 // Returns true if the non-FEC packet |sequence_number| is unacked.
92 bool IsUnacked(QuicPacketSequenceNumber sequence_number) const;
94 // Requests retransmission of all unacked packets of |retransmission_type|.
95 void RetransmitUnackedPackets(RetransmissionType retransmission_type);
97 // Returns true if the unacked packet |sequence_number| has retransmittable
98 // frames. This will only return false if the packet has been acked, if a
99 // previous transmission of this packet was ACK'd, or if this packet has been
100 // retransmitted as with different sequence number.
101 bool HasRetransmittableFrames(QuicPacketSequenceNumber sequence_number) const;
103 // Returns true if there are pending retransmissions.
104 bool HasPendingRetransmissions() const;
106 // Retrieves the next pending retransmission.
107 PendingRetransmission NextPendingRetransmission();
109 bool HasUnackedPackets() const;
111 // Returns the smallest sequence number of a serialized packet which has not
112 // been acked by the peer. If there are no unacked packets, returns 0.
113 QuicPacketSequenceNumber GetLeastUnackedSentPacket() const;
115 // Called when a congestion feedback frame is received from peer.
116 virtual void OnIncomingQuicCongestionFeedbackFrame(
117 const QuicCongestionFeedbackFrame& frame,
118 const QuicTime& feedback_receive_time);
120 // Called when we have sent bytes to the peer. This informs the manager both
121 // the number of bytes sent and if they were retransmitted. Returns true if
122 // the sender should reset the retransmission timer.
123 virtual bool OnPacketSent(QuicPacketSequenceNumber sequence_number,
124 QuicTime sent_time,
125 QuicByteCount bytes,
126 TransmissionType transmission_type,
127 HasRetransmittableData has_retransmittable_data);
129 // Called when the retransmission timer expires.
130 virtual void OnRetransmissionTimeout();
132 // Calculate the time until we can send the next packet to the wire.
133 // Note 1: When kUnknownWaitTime is returned, there is no need to poll
134 // TimeUntilSend again until we receive an OnIncomingAckFrame event.
135 // Note 2: Send algorithms may or may not use |retransmit| in their
136 // calculations.
137 virtual QuicTime::Delta TimeUntilSend(QuicTime now,
138 TransmissionType transmission_type,
139 HasRetransmittableData retransmittable);
141 // Returns amount of time for delayed ack timer.
142 const QuicTime::Delta DelayedAckTime() const;
144 // Returns the current delay for the retransmission timer, which may send
145 // either a tail loss probe or do a full RTO. Returns QuicTime::Zero() if
146 // there are no retransmittable packets.
147 const QuicTime GetRetransmissionTime() const;
149 // Returns the estimated smoothed RTT calculated by the congestion algorithm.
150 const QuicTime::Delta SmoothedRtt() const;
152 // Returns the estimated bandwidth calculated by the congestion algorithm.
153 QuicBandwidth BandwidthEstimate() const;
155 // Returns the size of the current congestion window in bytes. Note, this is
156 // not the *available* window. Some send algorithms may not use a congestion
157 // window and will return 0.
158 QuicByteCount GetCongestionWindow() const;
160 // Enables pacing if it has not already been enabled, and if
161 // FLAGS_enable_quic_pacing is set.
162 void MaybeEnablePacing();
164 bool using_pacing() const { return using_pacing_; }
166 private:
167 friend class test::QuicConnectionPeer;
168 friend class test::QuicSentPacketManagerPeer;
170 enum ReceivedByPeer {
171 RECEIVED_BY_PEER,
172 NOT_RECEIVED_BY_PEER,
175 // The retransmission timer is a single timer which switches modes depending
176 // upon connection state.
177 enum RetransmissionTimeoutMode {
178 // A conventional TCP style RTO.
179 RTO_MODE,
180 // A tail loss probe. By default, QUIC sends up to two before RTOing.
181 TLP_MODE,
182 // Retransmission of handshake packets prior to handshake completion.
183 HANDSHAKE_MODE,
184 // Re-invoke the loss detection when a packet is not acked before the
185 // loss detection algorithm expects.
186 LOSS_MODE,
189 typedef linked_hash_map<QuicPacketSequenceNumber,
190 TransmissionType> PendingRetransmissionMap;
192 // Process the incoming ack looking for newly ack'd data packets.
193 void HandleAckForSentPackets(const ReceivedPacketInfo& received_info);
195 // Called when a packet is timed out, such as an RTO. Removes the bytes from
196 // the congestion manager, but does not change the congestion window size.
197 void OnPacketAbandoned(QuicPacketSequenceNumber sequence_number);
199 // Returns the current retransmission mode.
200 RetransmissionTimeoutMode GetRetransmissionMode() const;
202 // Retransmits all crypto stream packets.
203 void RetransmitCryptoPackets();
205 // Retransmits the oldest pending packet.
206 void RetransmitOldestPacket();
208 // Retransmits all the packets and abandons by invoking a full RTO.
209 void RetransmitAllPackets();
211 // Returns the timer for retransmitting crypto handshake packets.
212 const QuicTime::Delta GetCryptoRetransmissionDelay() const;
214 // Returns the timer for a new tail loss probe.
215 const QuicTime::Delta GetTailLossProbeDelay() const;
217 // Returns the retransmission timeout, after which a full RTO occurs.
218 const QuicTime::Delta GetRetransmissionDelay() const;
220 // Update the RTT if the ack is for the largest acked sequence number.
221 void MaybeUpdateRTT(const ReceivedPacketInfo& received_info,
222 const QuicTime& ack_receive_time);
224 // Chooses whether to nack retransmit any packets based on the receipt info.
225 // All acks have been handled before this method is invoked.
226 void MaybeRetransmitOnAckFrame(const ReceivedPacketInfo& received_info,
227 const QuicTime& ack_receive_time);
229 // Invokes the loss detection algorithm and loses and retransmits packets if
230 // necessary.
231 void InvokeLossDetection(QuicTime time);
233 // Marks |sequence_number| as being fully handled, either due to receipt
234 // by the peer, or having been discarded as indecipherable. Returns an
235 // iterator to the next remaining unacked packet.
236 QuicUnackedPacketMap::const_iterator MarkPacketHandled(
237 QuicPacketSequenceNumber sequence_number,
238 ReceivedByPeer received_by_peer);
240 // Request that |sequence_number| be retransmitted after the other pending
241 // retransmissions. Does not add it to the retransmissions if it's already
242 // a pending retransmission.
243 void MarkForRetransmission(QuicPacketSequenceNumber sequence_number,
244 TransmissionType transmission_type);
246 // Newly serialized retransmittable and fec packets are added to this map,
247 // which contains owning pointers to any contained frames. If a packet is
248 // retransmitted, this map will contain entries for both the old and the new
249 // packet. The old packet's retransmittable frames entry will be NULL, while
250 // the new packet's entry will contain the frames to retransmit.
251 // If the old packet is acked before the new packet, then the old entry will
252 // be removed from the map and the new entry's retransmittable frames will be
253 // set to NULL.
254 QuicUnackedPacketMap unacked_packets_;
256 // Pending retransmissions which have not been packetized and sent yet.
257 PendingRetransmissionMap pending_retransmissions_;
259 // Tracks if the connection was created by the server.
260 bool is_server_;
262 // An AckNotifier can register to be informed when ACKs have been received for
263 // all packets that a given block of data was sent in. The AckNotifierManager
264 // maintains the currently active notifiers.
265 AckNotifierManager ack_notifier_manager_;
267 const QuicClock* clock_;
268 QuicConnectionStats* stats_;
269 RttStats rtt_stats_;
270 scoped_ptr<SendAlgorithmInterface> send_algorithm_;
271 scoped_ptr<LossDetectionInterface> loss_algorithm_;
273 QuicPacketSequenceNumber largest_observed_; // From the most recent ACK.
274 // Number of times the RTO timer has fired in a row without receiving an ack.
275 size_t consecutive_rto_count_;
276 // Number of times the tail loss probe has been sent.
277 size_t consecutive_tlp_count_;
278 // Number of times the crypto handshake has been retransmitted.
279 size_t consecutive_crypto_retransmission_count_;
280 // Maximum number of tail loss probes to send before firing an RTO.
281 size_t max_tail_loss_probes_;
282 bool using_pacing_;
284 DISALLOW_COPY_AND_ASSIGN(QuicSentPacketManager);
287 } // namespace net
289 #endif // NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_