Update the project URL to DOM Distiller
[chromium-blink-merge.git] / net / quic / quic_sent_packet_manager.h
blobc2a8ef6cbd91d00205b8ea3d61619bf78ad568d3
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 <map>
9 #include <set>
10 #include <utility>
11 #include <vector>
13 #include "base/containers/hash_tables.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "net/base/linked_hash_map.h"
16 #include "net/quic/congestion_control/loss_detection_interface.h"
17 #include "net/quic/congestion_control/rtt_stats.h"
18 #include "net/quic/congestion_control/send_algorithm_interface.h"
19 #include "net/quic/crypto/cached_network_parameters.h"
20 #include "net/quic/quic_ack_notifier_manager.h"
21 #include "net/quic/quic_protocol.h"
22 #include "net/quic/quic_sustained_bandwidth_recorder.h"
23 #include "net/quic/quic_unacked_packet_map.h"
25 namespace net {
27 namespace test {
28 class QuicConnectionPeer;
29 class QuicSentPacketManagerPeer;
30 } // namespace test
32 class QuicClock;
33 class QuicConfig;
34 struct QuicConnectionStats;
36 // Class which tracks the set of packets sent on a QUIC connection and contains
37 // a send algorithm to decide when to send new packets. It keeps track of any
38 // retransmittable data associated with each packet. If a packet is
39 // retransmitted, it will keep track of each version of a packet so that if a
40 // previous transmission is acked, the data will not be retransmitted.
41 class NET_EXPORT_PRIVATE QuicSentPacketManager {
42 public:
43 // Interface which gets callbacks from the QuicSentPacketManager at
44 // interesting points. Implementations must not mutate the state of
45 // the packet manager or connection as a result of these callbacks.
46 class NET_EXPORT_PRIVATE DebugDelegate {
47 public:
48 virtual ~DebugDelegate() {}
50 // Called when a spurious retransmission is detected.
51 virtual void OnSpuriousPacketRetransmission(
52 TransmissionType transmission_type,
53 QuicByteCount byte_size) {}
55 virtual void OnIncomingAck(
56 const QuicAckFrame& ack_frame,
57 QuicTime ack_receive_time,
58 QuicPacketSequenceNumber largest_observed,
59 bool rtt_updated,
60 QuicPacketSequenceNumber least_unacked_sent_packet) {}
63 // Interface which gets callbacks from the QuicSentPacketManager when
64 // network-related state changes. Implementations must not mutate the
65 // state of the packet manager as a result of these callbacks.
66 class NET_EXPORT_PRIVATE NetworkChangeVisitor {
67 public:
68 virtual ~NetworkChangeVisitor() {}
70 // Called when congestion window may have changed.
71 virtual void OnCongestionWindowChange() = 0;
73 // Called when RTT may have changed, including when an RTT is read from
74 // the config.
75 virtual void OnRttChange() = 0;
78 // Struct to store the pending retransmission information.
79 struct PendingRetransmission {
80 PendingRetransmission(QuicPacketSequenceNumber sequence_number,
81 TransmissionType transmission_type,
82 const RetransmittableFrames& retransmittable_frames,
83 QuicSequenceNumberLength sequence_number_length)
84 : sequence_number(sequence_number),
85 transmission_type(transmission_type),
86 retransmittable_frames(retransmittable_frames),
87 sequence_number_length(sequence_number_length) {
90 QuicPacketSequenceNumber sequence_number;
91 TransmissionType transmission_type;
92 const RetransmittableFrames& retransmittable_frames;
93 QuicSequenceNumberLength sequence_number_length;
96 QuicSentPacketManager(bool is_server,
97 const QuicClock* clock,
98 QuicConnectionStats* stats,
99 CongestionControlType congestion_control_type,
100 LossDetectionType loss_type,
101 bool is_secure);
102 virtual ~QuicSentPacketManager();
104 virtual void SetFromConfig(const QuicConfig& config);
106 // Pass the CachedNetworkParameters to the send algorithm.
107 // Returns true if this changes the initial connection state.
108 bool ResumeConnectionState(
109 const CachedNetworkParameters& cached_network_params);
111 void SetNumOpenStreams(size_t num_streams);
113 void SetHandshakeConfirmed() { handshake_confirmed_ = true; }
115 // Processes the incoming ack.
116 void OnIncomingAck(const QuicAckFrame& ack_frame,
117 QuicTime ack_receive_time);
119 // Returns true if the non-FEC packet |sequence_number| is unacked.
120 bool IsUnacked(QuicPacketSequenceNumber sequence_number) const;
122 // Requests retransmission of all unacked packets of |retransmission_type|.
123 // The behavior of this method depends on the value of |retransmission_type|:
124 // ALL_UNACKED_RETRANSMISSION - All unacked packets will be retransmitted.
125 // This can happen, for example, after a version negotiation packet has been
126 // received and all packets needs to be retransmitted with the new version.
127 // ALL_INITIAL_RETRANSMISSION - Only initially encrypted packets will be
128 // retransmitted. This can happen, for example, when a CHLO has been rejected
129 // and the previously encrypted data needs to be encrypted with a new key.
130 void RetransmitUnackedPackets(TransmissionType retransmission_type);
132 // Retransmits the oldest pending packet there is still a tail loss probe
133 // pending. Invoked after OnRetransmissionTimeout.
134 bool MaybeRetransmitTailLossProbe();
136 // Removes the retransmittable frames from all unencrypted packets to ensure
137 // they don't get retransmitted.
138 void NeuterUnencryptedPackets();
140 // Returns true if the unacked packet |sequence_number| has retransmittable
141 // frames. This will only return false if the packet has been acked, if a
142 // previous transmission of this packet was ACK'd, or if this packet has been
143 // retransmitted as with different sequence number.
144 bool HasRetransmittableFrames(QuicPacketSequenceNumber sequence_number) const;
146 // Returns true if there are pending retransmissions.
147 bool HasPendingRetransmissions() const;
149 // Retrieves the next pending retransmission. You must ensure that
150 // there are pending retransmissions prior to calling this function.
151 PendingRetransmission NextPendingRetransmission();
153 bool HasUnackedPackets() const;
155 // Returns the smallest sequence number of a serialized packet which has not
156 // been acked by the peer.
157 QuicPacketSequenceNumber GetLeastUnacked() const;
159 // Called when we have sent bytes to the peer. This informs the manager both
160 // the number of bytes sent and if they were retransmitted. Returns true if
161 // the sender should reset the retransmission timer.
162 virtual bool OnPacketSent(SerializedPacket* serialized_packet,
163 QuicPacketSequenceNumber original_sequence_number,
164 QuicTime sent_time,
165 QuicByteCount bytes,
166 TransmissionType transmission_type,
167 HasRetransmittableData has_retransmittable_data);
169 // Called when the retransmission timer expires.
170 virtual void OnRetransmissionTimeout();
172 // Calculate the time until we can send the next packet to the wire.
173 // Note 1: When kUnknownWaitTime is returned, there is no need to poll
174 // TimeUntilSend again until we receive an OnIncomingAckFrame event.
175 // Note 2: Send algorithms may or may not use |retransmit| in their
176 // calculations.
177 virtual QuicTime::Delta TimeUntilSend(QuicTime now,
178 HasRetransmittableData retransmittable);
180 // Returns amount of time for delayed ack timer.
181 const QuicTime::Delta DelayedAckTime() const;
183 // Returns the current delay for the retransmission timer, which may send
184 // either a tail loss probe or do a full RTO. Returns QuicTime::Zero() if
185 // there are no retransmittable packets.
186 const QuicTime GetRetransmissionTime() const;
188 const RttStats* GetRttStats() const;
190 // Returns the estimated bandwidth calculated by the congestion algorithm.
191 QuicBandwidth BandwidthEstimate() const;
193 // Returns true if the current instantaneous bandwidth estimate is reliable.
194 bool HasReliableBandwidthEstimate() const;
196 const QuicSustainedBandwidthRecorder& SustainedBandwidthRecorder() const;
198 // Returns the size of the current congestion window in number of
199 // kDefaultTCPMSS-sized segments. Note, this is not the *available* window.
200 // Some send algorithms may not use a congestion window and will return 0.
201 QuicPacketCount GetCongestionWindowInTcpMss() const;
203 // Returns the number of packets of length |max_packet_length| which fit in
204 // the current congestion window. More packets may end up in flight if the
205 // congestion window has been recently reduced, of if non-full packets are
206 // sent.
207 QuicPacketCount EstimateMaxPacketsInFlight(
208 QuicByteCount max_packet_length) const;
210 // Returns the size of the slow start congestion window in nume of 1460 byte
211 // TCP segments, aka ssthresh. Some send algorithms do not define a slow
212 // start threshold and will return 0.
213 QuicPacketCount GetSlowStartThresholdInTcpMss() const;
215 // Called by the connection every time it receives a serialized packet.
216 void OnSerializedPacket(const SerializedPacket& serialized_packet);
218 // Enables pacing if it has not already been enabled.
219 void EnablePacing();
221 bool using_pacing() const { return using_pacing_; }
223 void set_debug_delegate(DebugDelegate* debug_delegate) {
224 debug_delegate_ = debug_delegate;
227 QuicPacketSequenceNumber largest_observed() const {
228 return unacked_packets_.largest_observed();
231 QuicPacketSequenceNumber least_packet_awaited_by_peer() {
232 return least_packet_awaited_by_peer_;
235 void set_network_change_visitor(NetworkChangeVisitor* visitor) {
236 DCHECK(!network_change_visitor_);
237 DCHECK(visitor);
238 network_change_visitor_ = visitor;
241 size_t consecutive_rto_count() const {
242 return consecutive_rto_count_;
245 size_t consecutive_tlp_count() const {
246 return consecutive_tlp_count_;
249 private:
250 friend class test::QuicConnectionPeer;
251 friend class test::QuicSentPacketManagerPeer;
253 // The retransmission timer is a single timer which switches modes depending
254 // upon connection state.
255 enum RetransmissionTimeoutMode {
256 // A conventional TCP style RTO.
257 RTO_MODE,
258 // A tail loss probe. By default, QUIC sends up to two before RTOing.
259 TLP_MODE,
260 // Retransmission of handshake packets prior to handshake completion.
261 HANDSHAKE_MODE,
262 // Re-invoke the loss detection when a packet is not acked before the
263 // loss detection algorithm expects.
264 LOSS_MODE,
267 typedef linked_hash_map<QuicPacketSequenceNumber,
268 TransmissionType> PendingRetransmissionMap;
270 // Called when a packet is retransmitted with a new sequence number.
271 // Replaces the old entry in the unacked packet map with the new
272 // sequence number.
273 void OnRetransmittedPacket(QuicPacketSequenceNumber old_sequence_number,
274 QuicPacketSequenceNumber new_sequence_number);
276 // Updates the least_packet_awaited_by_peer.
277 void UpdatePacketInformationReceivedByPeer(const QuicAckFrame& ack_frame);
279 // Process the incoming ack looking for newly ack'd data packets.
280 void HandleAckForSentPackets(const QuicAckFrame& ack_frame);
282 // Returns the current retransmission mode.
283 RetransmissionTimeoutMode GetRetransmissionMode() const;
285 // Retransmits all crypto stream packets.
286 void RetransmitCryptoPackets();
288 // Retransmits two packets for an RTO and removes any non-retransmittable
289 // packets from flight.
290 void RetransmitRtoPackets();
292 // Retransmits all the packets and abandons by invoking a full RTO.
293 void RetransmitAllPackets();
295 // Returns the timer for retransmitting crypto handshake packets.
296 const QuicTime::Delta GetCryptoRetransmissionDelay() const;
298 // Returns the timer for a new tail loss probe.
299 const QuicTime::Delta GetTailLossProbeDelay() const;
301 // Returns the retransmission timeout, after which a full RTO occurs.
302 const QuicTime::Delta GetRetransmissionDelay() const;
304 // Update the RTT if the ack is for the largest acked sequence number.
305 // Returns true if the rtt was updated.
306 bool MaybeUpdateRTT(const QuicAckFrame& ack_frame,
307 const QuicTime& ack_receive_time);
309 // Invokes the loss detection algorithm and loses and retransmits packets if
310 // necessary.
311 void InvokeLossDetection(QuicTime time);
313 // Invokes OnCongestionEvent if |rtt_updated| is true, there are pending acks,
314 // or pending losses. Clears pending acks and pending losses afterwards.
315 // |bytes_in_flight| is the number of bytes in flight before the losses or
316 // acks.
317 void MaybeInvokeCongestionEvent(bool rtt_updated,
318 QuicByteCount bytes_in_flight);
320 // Marks |sequence_number| as having been revived by the peer, but not
321 // received, so the packet remains pending if it is and the congestion control
322 // does not consider the packet acked.
323 void MarkPacketRevived(QuicPacketSequenceNumber sequence_number,
324 QuicTime::Delta delta_largest_observed);
326 // Removes the retransmittability and pending properties from the packet at
327 // |it| due to receipt by the peer. Returns an iterator to the next remaining
328 // unacked packet.
329 void MarkPacketHandled(QuicPacketSequenceNumber sequence_number,
330 const TransmissionInfo& info,
331 QuicTime::Delta delta_largest_observed);
333 // Request that |sequence_number| be retransmitted after the other pending
334 // retransmissions. Does not add it to the retransmissions if it's already
335 // a pending retransmission.
336 void MarkForRetransmission(QuicPacketSequenceNumber sequence_number,
337 TransmissionType transmission_type);
339 // Notify observers about spurious retransmits.
340 void RecordSpuriousRetransmissions(
341 const SequenceNumberList& all_transmissions,
342 QuicPacketSequenceNumber acked_sequence_number);
344 // Returns true if the client is sending or the server has received a
345 // connection option.
346 bool HasClientSentConnectionOption(const QuicConfig& config,
347 QuicTag tag) const;
349 // Newly serialized retransmittable and fec packets are added to this map,
350 // which contains owning pointers to any contained frames. If a packet is
351 // retransmitted, this map will contain entries for both the old and the new
352 // packet. The old packet's retransmittable frames entry will be nullptr,
353 // while the new packet's entry will contain the frames to retransmit.
354 // If the old packet is acked before the new packet, then the old entry will
355 // be removed from the map and the new entry's retransmittable frames will be
356 // set to nullptr.
357 QuicUnackedPacketMap unacked_packets_;
359 // Pending retransmissions which have not been packetized and sent yet.
360 PendingRetransmissionMap pending_retransmissions_;
362 // Tracks if the connection was created by the server.
363 bool is_server_;
365 // An AckNotifier can register to be informed when ACKs have been received for
366 // all packets that a given block of data was sent in. The AckNotifierManager
367 // maintains the currently active notifiers.
368 AckNotifierManager ack_notifier_manager_;
370 const QuicClock* clock_;
371 QuicConnectionStats* stats_;
372 DebugDelegate* debug_delegate_;
373 NetworkChangeVisitor* network_change_visitor_;
374 const QuicPacketCount initial_congestion_window_;
375 RttStats rtt_stats_;
376 scoped_ptr<SendAlgorithmInterface> send_algorithm_;
377 scoped_ptr<LossDetectionInterface> loss_algorithm_;
378 bool n_connection_simulation_;
380 // Receiver side buffer in bytes.
381 QuicByteCount receive_buffer_bytes_;
383 // Least sequence number which the peer is still waiting for.
384 QuicPacketSequenceNumber least_packet_awaited_by_peer_;
386 // Tracks the first RTO packet. If any packet before that packet gets acked,
387 // it indicates the RTO was spurious and should be reversed(F-RTO).
388 QuicPacketSequenceNumber first_rto_transmission_;
389 // Number of times the RTO timer has fired in a row without receiving an ack.
390 size_t consecutive_rto_count_;
391 // Number of times the tail loss probe has been sent.
392 size_t consecutive_tlp_count_;
393 // Number of times the crypto handshake has been retransmitted.
394 size_t consecutive_crypto_retransmission_count_;
395 // Number of pending transmissions of TLP, RTO, or crypto packets.
396 size_t pending_timer_transmission_count_;
397 // Maximum number of tail loss probes to send before firing an RTO.
398 size_t max_tail_loss_probes_;
399 bool using_pacing_;
400 // If true, use the new RTO with loss based CWND reduction instead of the send
401 // algorithms's OnRetransmissionTimeout to reduce the congestion window.
402 bool use_new_rto_;
404 // Vectors packets acked and lost as a result of the last congestion event.
405 SendAlgorithmInterface::CongestionVector packets_acked_;
406 SendAlgorithmInterface::CongestionVector packets_lost_;
408 // Set to true after the crypto handshake has successfully completed. After
409 // this is true we no longer use HANDSHAKE_MODE, and further frames sent on
410 // the crypto stream (i.e. SCUP messages) are treated like normal
411 // retransmittable frames.
412 bool handshake_confirmed_;
414 // Records bandwidth from server to client in normal operation, over periods
415 // of time with no loss events.
416 QuicSustainedBandwidthRecorder sustained_bandwidth_recorder_;
418 DISALLOW_COPY_AND_ASSIGN(QuicSentPacketManager);
421 } // namespace net
423 #endif // NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_