Switch global error menu icon to vectorized MD asset
[chromium-blink-merge.git] / net / quic / quic_sent_packet_manager.h
blob0b915cb0e2611dacab5624f2c5393e2542e2279b
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/quic_ack_notifier_manager.h"
20 #include "net/quic/quic_protocol.h"
21 #include "net/quic/quic_sustained_bandwidth_recorder.h"
22 #include "net/quic/quic_unacked_packet_map.h"
24 namespace net {
26 namespace test {
27 class QuicConnectionPeer;
28 class QuicSentPacketManagerPeer;
29 } // namespace test
31 class QuicClock;
32 class QuicConfig;
33 struct QuicConnectionStats;
35 // Class which tracks the set of packets sent on a QUIC connection and contains
36 // a send algorithm to decide when to send new packets. It keeps track of any
37 // retransmittable data associated with each packet. If a packet is
38 // retransmitted, it will keep track of each version of a packet so that if a
39 // previous transmission is acked, the data will not be retransmitted.
40 class NET_EXPORT_PRIVATE QuicSentPacketManager {
41 public:
42 // Interface which gets callbacks from the QuicSentPacketManager at
43 // interesting points. Implementations must not mutate the state of
44 // the packet manager or connection as a result of these callbacks.
45 class NET_EXPORT_PRIVATE DebugDelegate {
46 public:
47 virtual ~DebugDelegate() {}
49 // Called when a spurious retransmission is detected.
50 virtual void OnSpuriousPacketRetransmission(
51 TransmissionType transmission_type,
52 QuicByteCount byte_size) {}
54 virtual void OnIncomingAck(const QuicAckFrame& ack_frame,
55 QuicTime ack_receive_time,
56 QuicPacketNumber largest_observed,
57 bool rtt_updated,
58 QuicPacketNumber least_unacked_sent_packet) {}
61 // Interface which gets callbacks from the QuicSentPacketManager when
62 // network-related state changes. Implementations must not mutate the
63 // state of the packet manager as a result of these callbacks.
64 class NET_EXPORT_PRIVATE NetworkChangeVisitor {
65 public:
66 virtual ~NetworkChangeVisitor() {}
68 // Called when congestion window may have changed.
69 virtual void OnCongestionWindowChange() = 0;
71 // Called when RTT may have changed, including when an RTT is read from
72 // the config.
73 virtual void OnRttChange() = 0;
76 // Struct to store the pending retransmission information.
77 struct PendingRetransmission {
78 PendingRetransmission(QuicPacketNumber packet_number,
79 TransmissionType transmission_type,
80 const RetransmittableFrames& retransmittable_frames,
81 QuicPacketNumberLength packet_number_length)
82 : packet_number(packet_number),
83 transmission_type(transmission_type),
84 retransmittable_frames(retransmittable_frames),
85 packet_number_length(packet_number_length) {}
87 QuicPacketNumber packet_number;
88 TransmissionType transmission_type;
89 const RetransmittableFrames& retransmittable_frames;
90 QuicPacketNumberLength packet_number_length;
93 QuicSentPacketManager(Perspective perspective,
94 const QuicClock* clock,
95 QuicConnectionStats* stats,
96 CongestionControlType congestion_control_type,
97 LossDetectionType loss_type,
98 bool is_secure);
99 virtual ~QuicSentPacketManager();
101 virtual void SetFromConfig(const QuicConfig& config);
103 // Pass the CachedNetworkParameters to the send algorithm.
104 void ResumeConnectionState(
105 const CachedNetworkParameters& cached_network_params,
106 bool max_bandwidth_resumption);
108 void SetNumOpenStreams(size_t num_streams);
110 void SetHandshakeConfirmed() { handshake_confirmed_ = true; }
112 // Processes the incoming ack.
113 void OnIncomingAck(const QuicAckFrame& ack_frame,
114 QuicTime ack_receive_time);
116 // Returns true if the non-FEC packet |packet_number| is unacked.
117 bool IsUnacked(QuicPacketNumber packet_number) const;
119 // Requests retransmission of all unacked packets of |retransmission_type|.
120 // The behavior of this method depends on the value of |retransmission_type|:
121 // ALL_UNACKED_RETRANSMISSION - All unacked packets will be retransmitted.
122 // This can happen, for example, after a version negotiation packet has been
123 // received and all packets needs to be retransmitted with the new version.
124 // ALL_INITIAL_RETRANSMISSION - Only initially encrypted packets will be
125 // retransmitted. This can happen, for example, when a CHLO has been rejected
126 // and the previously encrypted data needs to be encrypted with a new key.
127 void RetransmitUnackedPackets(TransmissionType retransmission_type);
129 // Retransmits the oldest pending packet there is still a tail loss probe
130 // pending. Invoked after OnRetransmissionTimeout.
131 bool MaybeRetransmitTailLossProbe();
133 // Removes the retransmittable frames from all unencrypted packets to ensure
134 // they don't get retransmitted.
135 void NeuterUnencryptedPackets();
137 // Returns true if the unacked packet |packet_number| has retransmittable
138 // frames. This will only return false if the packet has been acked, if a
139 // previous transmission of this packet was ACK'd, or if this packet has been
140 // retransmitted as with different packet number.
141 bool HasRetransmittableFrames(QuicPacketNumber packet_number) const;
143 // Returns true if there are pending retransmissions.
144 bool HasPendingRetransmissions() const;
146 // Retrieves the next pending retransmission. You must ensure that
147 // there are pending retransmissions prior to calling this function.
148 PendingRetransmission NextPendingRetransmission();
150 bool HasUnackedPackets() const;
152 // Returns the smallest packet number of a serialized packet which has not
153 // been acked by the peer.
154 QuicPacketNumber GetLeastUnacked() const;
156 // Called when we have sent bytes to the peer. This informs the manager both
157 // the number of bytes sent and if they were retransmitted. Returns true if
158 // the sender should reset the retransmission timer.
159 virtual bool OnPacketSent(SerializedPacket* serialized_packet,
160 QuicPacketNumber original_packet_number,
161 QuicTime sent_time,
162 QuicByteCount bytes,
163 TransmissionType transmission_type,
164 HasRetransmittableData has_retransmittable_data);
166 // Called when the retransmission timer expires.
167 virtual void OnRetransmissionTimeout();
169 // Calculate the time until we can send the next packet to the wire.
170 // Note 1: When kUnknownWaitTime is returned, there is no need to poll
171 // TimeUntilSend again until we receive an OnIncomingAckFrame event.
172 // Note 2: Send algorithms may or may not use |retransmit| in their
173 // calculations.
174 virtual QuicTime::Delta TimeUntilSend(QuicTime now,
175 HasRetransmittableData retransmittable);
177 // Returns amount of time for delayed ack timer.
178 const QuicTime::Delta DelayedAckTime() const;
180 // Returns the current delay for the retransmission timer, which may send
181 // either a tail loss probe or do a full RTO. Returns QuicTime::Zero() if
182 // there are no retransmittable packets.
183 const QuicTime GetRetransmissionTime() const;
185 const RttStats* GetRttStats() const;
187 // Returns the estimated bandwidth calculated by the congestion algorithm.
188 QuicBandwidth BandwidthEstimate() const;
190 const QuicSustainedBandwidthRecorder& SustainedBandwidthRecorder() const;
192 // Returns the size of the current congestion window in number of
193 // kDefaultTCPMSS-sized segments. Note, this is not the *available* window.
194 // Some send algorithms may not use a congestion window and will return 0.
195 QuicPacketCount GetCongestionWindowInTcpMss() const;
197 // Returns the number of packets of length |max_packet_length| which fit in
198 // the current congestion window. More packets may end up in flight if the
199 // congestion window has been recently reduced, of if non-full packets are
200 // sent.
201 QuicPacketCount EstimateMaxPacketsInFlight(
202 QuicByteCount max_packet_length) const;
204 // Returns the size of the current congestion window size in bytes.
205 QuicByteCount GetCongestionWindowInBytes() const;
207 // Returns the size of the slow start congestion window in nume of 1460 byte
208 // TCP segments, aka ssthresh. Some send algorithms do not define a slow
209 // start threshold and will return 0.
210 QuicPacketCount GetSlowStartThresholdInTcpMss() const;
212 // Called by the connection every time it receives a serialized packet.
213 void OnSerializedPacket(const SerializedPacket& serialized_packet);
215 // No longer retransmit data for |stream_id|.
216 void CancelRetransmissionsForStream(QuicStreamId stream_id);
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 QuicPacketNumber largest_observed() const {
228 return unacked_packets_.largest_observed();
231 QuicPacketNumber 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 // Used in Chromium, but not in the server.
242 size_t consecutive_rto_count() const {
243 return consecutive_rto_count_;
246 // Used in Chromium, but not in the server.
247 size_t consecutive_tlp_count() const {
248 return consecutive_tlp_count_;
251 private:
252 friend class test::QuicConnectionPeer;
253 friend class test::QuicSentPacketManagerPeer;
255 // The retransmission timer is a single timer which switches modes depending
256 // upon connection state.
257 enum RetransmissionTimeoutMode {
258 // A conventional TCP style RTO.
259 RTO_MODE,
260 // A tail loss probe. By default, QUIC sends up to two before RTOing.
261 TLP_MODE,
262 // Retransmission of handshake packets prior to handshake completion.
263 HANDSHAKE_MODE,
264 // Re-invoke the loss detection when a packet is not acked before the
265 // loss detection algorithm expects.
266 LOSS_MODE,
269 typedef linked_hash_map<QuicPacketNumber, TransmissionType>
270 PendingRetransmissionMap;
272 // Updates the least_packet_awaited_by_peer.
273 void UpdatePacketInformationReceivedByPeer(const QuicAckFrame& ack_frame);
275 // Process the incoming ack looking for newly ack'd data packets.
276 void HandleAckForSentPackets(const QuicAckFrame& ack_frame);
278 // Returns the current retransmission mode.
279 RetransmissionTimeoutMode GetRetransmissionMode() const;
281 // Retransmits all crypto stream packets.
282 void RetransmitCryptoPackets();
284 // Retransmits two packets for an RTO and removes any non-retransmittable
285 // packets from flight.
286 void RetransmitRtoPackets();
288 // Returns the timer for retransmitting crypto handshake packets.
289 const QuicTime::Delta GetCryptoRetransmissionDelay() const;
291 // Returns the timer for a new tail loss probe.
292 const QuicTime::Delta GetTailLossProbeDelay() const;
294 // Returns the retransmission timeout, after which a full RTO occurs.
295 const QuicTime::Delta GetRetransmissionDelay() const;
297 // Update the RTT if the ack is for the largest acked packet number.
298 // Returns true if the rtt was updated.
299 bool MaybeUpdateRTT(const QuicAckFrame& ack_frame,
300 const QuicTime& ack_receive_time);
302 // Invokes the loss detection algorithm and loses and retransmits packets if
303 // necessary.
304 void InvokeLossDetection(QuicTime time);
306 // Invokes OnCongestionEvent if |rtt_updated| is true, there are pending acks,
307 // or pending losses. Clears pending acks and pending losses afterwards.
308 // |bytes_in_flight| is the number of bytes in flight before the losses or
309 // acks.
310 void MaybeInvokeCongestionEvent(bool rtt_updated,
311 QuicByteCount bytes_in_flight);
313 // Marks |packet_number| as having been revived by the peer, but not
314 // received, so the packet remains pending if it is and the congestion control
315 // does not consider the packet acked.
316 void MarkPacketRevived(QuicPacketNumber packet_number,
317 QuicTime::Delta delta_largest_observed);
319 // Removes the retransmittability and pending properties from the packet at
320 // |it| due to receipt by the peer. Returns an iterator to the next remaining
321 // unacked packet.
322 void MarkPacketHandled(QuicPacketNumber packet_number,
323 const TransmissionInfo& info,
324 QuicTime::Delta delta_largest_observed);
326 // Request that |packet_number| be retransmitted after the other pending
327 // retransmissions. Does not add it to the retransmissions if it's already
328 // a pending retransmission.
329 void MarkForRetransmission(QuicPacketNumber packet_number,
330 TransmissionType transmission_type);
332 // Notify observers about spurious retransmits.
333 void RecordSpuriousRetransmissions(const PacketNumberList& all_transmissions,
334 QuicPacketNumber acked_packet_number);
336 // Newly serialized retransmittable and fec packets are added to this map,
337 // which contains owning pointers to any contained frames. If a packet is
338 // retransmitted, this map will contain entries for both the old and the new
339 // packet. The old packet's retransmittable frames entry will be nullptr,
340 // while the new packet's entry will contain the frames to retransmit.
341 // If the old packet is acked before the new packet, then the old entry will
342 // be removed from the map and the new entry's retransmittable frames will be
343 // set to nullptr.
344 QuicUnackedPacketMap unacked_packets_;
346 // Pending retransmissions which have not been packetized and sent yet.
347 PendingRetransmissionMap pending_retransmissions_;
349 // Tracks if the connection was created by the server or the client.
350 Perspective perspective_;
352 // An AckNotifier can register to be informed when ACKs have been received for
353 // all packets that a given block of data was sent in. The AckNotifierManager
354 // maintains the currently active notifiers.
355 AckNotifierManager ack_notifier_manager_;
357 const QuicClock* clock_;
358 QuicConnectionStats* stats_;
359 DebugDelegate* debug_delegate_;
360 NetworkChangeVisitor* network_change_visitor_;
361 const QuicPacketCount initial_congestion_window_;
362 RttStats rtt_stats_;
363 scoped_ptr<SendAlgorithmInterface> send_algorithm_;
364 scoped_ptr<LossDetectionInterface> loss_algorithm_;
365 bool n_connection_simulation_;
367 // Receiver side buffer in bytes.
368 QuicByteCount receive_buffer_bytes_;
370 // Least packet number which the peer is still waiting for.
371 QuicPacketNumber least_packet_awaited_by_peer_;
373 // Tracks the first RTO packet. If any packet before that packet gets acked,
374 // it indicates the RTO was spurious and should be reversed(F-RTO).
375 QuicPacketNumber first_rto_transmission_;
376 // Number of times the RTO timer has fired in a row without receiving an ack.
377 size_t consecutive_rto_count_;
378 // Number of times the tail loss probe has been sent.
379 size_t consecutive_tlp_count_;
380 // Number of times the crypto handshake has been retransmitted.
381 size_t consecutive_crypto_retransmission_count_;
382 // Number of pending transmissions of TLP, RTO, or crypto packets.
383 size_t pending_timer_transmission_count_;
384 // Maximum number of tail loss probes to send before firing an RTO.
385 size_t max_tail_loss_probes_;
386 // If true, send the TLP at 0.5 RTT.
387 bool enable_half_rtt_tail_loss_probe_;
388 bool using_pacing_;
389 // If true, use the new RTO with loss based CWND reduction instead of the send
390 // algorithms's OnRetransmissionTimeout to reduce the congestion window.
391 bool use_new_rto_;
393 // Vectors packets acked and lost as a result of the last congestion event.
394 SendAlgorithmInterface::CongestionVector packets_acked_;
395 SendAlgorithmInterface::CongestionVector packets_lost_;
397 // Set to true after the crypto handshake has successfully completed. After
398 // this is true we no longer use HANDSHAKE_MODE, and further frames sent on
399 // the crypto stream (i.e. SCUP messages) are treated like normal
400 // retransmittable frames.
401 bool handshake_confirmed_;
403 // Records bandwidth from server to client in normal operation, over periods
404 // of time with no loss events.
405 QuicSustainedBandwidthRecorder sustained_bandwidth_recorder_;
407 DISALLOW_COPY_AND_ASSIGN(QuicSentPacketManager);
410 } // namespace net
412 #endif // NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_