telemetry: Compute multiple first gesture scroll updates if there were multiple gestures
[chromium-blink-merge.git] / net / quic / quic_sent_packet_manager.h
blob05e6254018bc0b2cd4121cc105e0799a5c9b7e72
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(
55 const QuicAckFrame& ack_frame,
56 QuicTime ack_receive_time,
57 QuicPacketSequenceNumber largest_observed,
58 bool rtt_updated,
59 QuicPacketSequenceNumber least_unacked_sent_packet) {}
62 // Interface which gets callbacks from the QuicSentPacketManager when
63 // network-related state changes. Implementations must not mutate the
64 // state of the packet manager as a result of these callbacks.
65 class NET_EXPORT_PRIVATE NetworkChangeVisitor {
66 public:
67 virtual ~NetworkChangeVisitor() {}
69 // Called when congestion window may have changed.
70 virtual void OnCongestionWindowChange() = 0;
72 // Called when RTT may have changed, including when an RTT is read from
73 // the config.
74 virtual void OnRttChange() = 0;
77 // Struct to store the pending retransmission information.
78 struct PendingRetransmission {
79 PendingRetransmission(QuicPacketSequenceNumber sequence_number,
80 TransmissionType transmission_type,
81 const RetransmittableFrames& retransmittable_frames,
82 QuicSequenceNumberLength sequence_number_length)
83 : sequence_number(sequence_number),
84 transmission_type(transmission_type),
85 retransmittable_frames(retransmittable_frames),
86 sequence_number_length(sequence_number_length) {
89 QuicPacketSequenceNumber sequence_number;
90 TransmissionType transmission_type;
91 const RetransmittableFrames& retransmittable_frames;
92 QuicSequenceNumberLength sequence_number_length;
95 QuicSentPacketManager(Perspective perspective,
96 const QuicClock* clock,
97 QuicConnectionStats* stats,
98 CongestionControlType congestion_control_type,
99 LossDetectionType loss_type,
100 bool is_secure);
101 virtual ~QuicSentPacketManager();
103 virtual void SetFromConfig(const QuicConfig& config);
105 // Pass the CachedNetworkParameters to the send algorithm.
106 void ResumeConnectionState(
107 const CachedNetworkParameters& cached_network_params,
108 bool max_bandwidth_resumption);
110 void SetNumOpenStreams(size_t num_streams);
112 void SetHandshakeConfirmed() { handshake_confirmed_ = true; }
114 // Processes the incoming ack.
115 void OnIncomingAck(const QuicAckFrame& ack_frame,
116 QuicTime ack_receive_time);
118 // Returns true if the non-FEC packet |sequence_number| is unacked.
119 bool IsUnacked(QuicPacketSequenceNumber sequence_number) const;
121 // Requests retransmission of all unacked packets of |retransmission_type|.
122 // The behavior of this method depends on the value of |retransmission_type|:
123 // ALL_UNACKED_RETRANSMISSION - All unacked packets will be retransmitted.
124 // This can happen, for example, after a version negotiation packet has been
125 // received and all packets needs to be retransmitted with the new version.
126 // ALL_INITIAL_RETRANSMISSION - Only initially encrypted packets will be
127 // retransmitted. This can happen, for example, when a CHLO has been rejected
128 // and the previously encrypted data needs to be encrypted with a new key.
129 void RetransmitUnackedPackets(TransmissionType retransmission_type);
131 // Retransmits the oldest pending packet there is still a tail loss probe
132 // pending. Invoked after OnRetransmissionTimeout.
133 bool MaybeRetransmitTailLossProbe();
135 // Removes the retransmittable frames from all unencrypted packets to ensure
136 // they don't get retransmitted.
137 void NeuterUnencryptedPackets();
139 // Returns true if the unacked packet |sequence_number| has retransmittable
140 // frames. This will only return false if the packet has been acked, if a
141 // previous transmission of this packet was ACK'd, or if this packet has been
142 // retransmitted as with different sequence number.
143 bool HasRetransmittableFrames(QuicPacketSequenceNumber sequence_number) const;
145 // Returns true if there are pending retransmissions.
146 bool HasPendingRetransmissions() const;
148 // Retrieves the next pending retransmission. You must ensure that
149 // there are pending retransmissions prior to calling this function.
150 PendingRetransmission NextPendingRetransmission();
152 bool HasUnackedPackets() const;
154 // Returns the smallest sequence number of a serialized packet which has not
155 // been acked by the peer.
156 QuicPacketSequenceNumber GetLeastUnacked() const;
158 // Called when we have sent bytes to the peer. This informs the manager both
159 // the number of bytes sent and if they were retransmitted. Returns true if
160 // the sender should reset the retransmission timer.
161 virtual bool OnPacketSent(SerializedPacket* serialized_packet,
162 QuicPacketSequenceNumber original_sequence_number,
163 QuicTime sent_time,
164 QuicByteCount bytes,
165 TransmissionType transmission_type,
166 HasRetransmittableData has_retransmittable_data);
168 // Called when the retransmission timer expires.
169 virtual void OnRetransmissionTimeout();
171 // Calculate the time until we can send the next packet to the wire.
172 // Note 1: When kUnknownWaitTime is returned, there is no need to poll
173 // TimeUntilSend again until we receive an OnIncomingAckFrame event.
174 // Note 2: Send algorithms may or may not use |retransmit| in their
175 // calculations.
176 virtual QuicTime::Delta TimeUntilSend(QuicTime now,
177 HasRetransmittableData retransmittable);
179 // Returns amount of time for delayed ack timer.
180 const QuicTime::Delta DelayedAckTime() const;
182 // Returns the current delay for the retransmission timer, which may send
183 // either a tail loss probe or do a full RTO. Returns QuicTime::Zero() if
184 // there are no retransmittable packets.
185 const QuicTime GetRetransmissionTime() const;
187 const RttStats* GetRttStats() const;
189 // Returns the estimated bandwidth calculated by the congestion algorithm.
190 QuicBandwidth BandwidthEstimate() const;
192 const QuicSustainedBandwidthRecorder& SustainedBandwidthRecorder() const;
194 // Returns the size of the current congestion window in number of
195 // kDefaultTCPMSS-sized segments. Note, this is not the *available* window.
196 // Some send algorithms may not use a congestion window and will return 0.
197 QuicPacketCount GetCongestionWindowInTcpMss() const;
199 // Returns the number of packets of length |max_packet_length| which fit in
200 // the current congestion window. More packets may end up in flight if the
201 // congestion window has been recently reduced, of if non-full packets are
202 // sent.
203 QuicPacketCount EstimateMaxPacketsInFlight(
204 QuicByteCount max_packet_length) const;
206 // Returns the size of the slow start congestion window in nume of 1460 byte
207 // TCP segments, aka ssthresh. Some send algorithms do not define a slow
208 // start threshold and will return 0.
209 QuicPacketCount GetSlowStartThresholdInTcpMss() const;
211 // Called by the connection every time it receives a serialized packet.
212 void OnSerializedPacket(const SerializedPacket& serialized_packet);
214 // No longer retransmit data for |stream_id|.
215 void CancelRetransmissionsForStream(QuicStreamId stream_id);
217 // Enables pacing if it has not already been enabled.
218 void EnablePacing();
220 bool using_pacing() const { return using_pacing_; }
222 void set_debug_delegate(DebugDelegate* debug_delegate) {
223 debug_delegate_ = debug_delegate;
226 QuicPacketSequenceNumber largest_observed() const {
227 return unacked_packets_.largest_observed();
230 QuicPacketSequenceNumber least_packet_awaited_by_peer() {
231 return least_packet_awaited_by_peer_;
234 void set_network_change_visitor(NetworkChangeVisitor* visitor) {
235 DCHECK(!network_change_visitor_);
236 DCHECK(visitor);
237 network_change_visitor_ = visitor;
240 // Used in Chromium, but not in the server.
241 size_t consecutive_rto_count() const {
242 return consecutive_rto_count_;
245 // Used in Chromium, but not in the server.
246 size_t consecutive_tlp_count() const {
247 return consecutive_tlp_count_;
250 private:
251 friend class test::QuicConnectionPeer;
252 friend class test::QuicSentPacketManagerPeer;
254 // The retransmission timer is a single timer which switches modes depending
255 // upon connection state.
256 enum RetransmissionTimeoutMode {
257 // A conventional TCP style RTO.
258 RTO_MODE,
259 // A tail loss probe. By default, QUIC sends up to two before RTOing.
260 TLP_MODE,
261 // Retransmission of handshake packets prior to handshake completion.
262 HANDSHAKE_MODE,
263 // Re-invoke the loss detection when a packet is not acked before the
264 // loss detection algorithm expects.
265 LOSS_MODE,
268 typedef linked_hash_map<QuicPacketSequenceNumber,
269 TransmissionType> PendingRetransmissionMap;
271 // Updates the least_packet_awaited_by_peer.
272 void UpdatePacketInformationReceivedByPeer(const QuicAckFrame& ack_frame);
274 // Process the incoming ack looking for newly ack'd data packets.
275 void HandleAckForSentPackets(const QuicAckFrame& ack_frame);
277 // Returns the current retransmission mode.
278 RetransmissionTimeoutMode GetRetransmissionMode() const;
280 // Retransmits all crypto stream packets.
281 void RetransmitCryptoPackets();
283 // Retransmits two packets for an RTO and removes any non-retransmittable
284 // packets from flight.
285 void RetransmitRtoPackets();
287 // Returns the timer for retransmitting crypto handshake packets.
288 const QuicTime::Delta GetCryptoRetransmissionDelay() const;
290 // Returns the timer for a new tail loss probe.
291 const QuicTime::Delta GetTailLossProbeDelay() const;
293 // Returns the retransmission timeout, after which a full RTO occurs.
294 const QuicTime::Delta GetRetransmissionDelay() const;
296 // Update the RTT if the ack is for the largest acked sequence number.
297 // Returns true if the rtt was updated.
298 bool MaybeUpdateRTT(const QuicAckFrame& ack_frame,
299 const QuicTime& ack_receive_time);
301 // Invokes the loss detection algorithm and loses and retransmits packets if
302 // necessary.
303 void InvokeLossDetection(QuicTime time);
305 // Invokes OnCongestionEvent if |rtt_updated| is true, there are pending acks,
306 // or pending losses. Clears pending acks and pending losses afterwards.
307 // |bytes_in_flight| is the number of bytes in flight before the losses or
308 // acks.
309 void MaybeInvokeCongestionEvent(bool rtt_updated,
310 QuicByteCount bytes_in_flight);
312 // Marks |sequence_number| as having been revived by the peer, but not
313 // received, so the packet remains pending if it is and the congestion control
314 // does not consider the packet acked.
315 void MarkPacketRevived(QuicPacketSequenceNumber sequence_number,
316 QuicTime::Delta delta_largest_observed);
318 // Removes the retransmittability and pending properties from the packet at
319 // |it| due to receipt by the peer. Returns an iterator to the next remaining
320 // unacked packet.
321 void MarkPacketHandled(QuicPacketSequenceNumber sequence_number,
322 const TransmissionInfo& info,
323 QuicTime::Delta delta_largest_observed);
325 // Request that |sequence_number| be retransmitted after the other pending
326 // retransmissions. Does not add it to the retransmissions if it's already
327 // a pending retransmission.
328 void MarkForRetransmission(QuicPacketSequenceNumber sequence_number,
329 TransmissionType transmission_type);
331 // Notify observers about spurious retransmits.
332 void RecordSpuriousRetransmissions(
333 const SequenceNumberList& all_transmissions,
334 QuicPacketSequenceNumber acked_sequence_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 sequence number which the peer is still waiting for.
371 QuicPacketSequenceNumber 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 QuicPacketSequenceNumber 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 bool using_pacing_;
387 // If true, use the new RTO with loss based CWND reduction instead of the send
388 // algorithms's OnRetransmissionTimeout to reduce the congestion window.
389 bool use_new_rto_;
391 // Vectors packets acked and lost as a result of the last congestion event.
392 SendAlgorithmInterface::CongestionVector packets_acked_;
393 SendAlgorithmInterface::CongestionVector packets_lost_;
395 // Set to true after the crypto handshake has successfully completed. After
396 // this is true we no longer use HANDSHAKE_MODE, and further frames sent on
397 // the crypto stream (i.e. SCUP messages) are treated like normal
398 // retransmittable frames.
399 bool handshake_confirmed_;
401 // Records bandwidth from server to client in normal operation, over periods
402 // of time with no loss events.
403 QuicSustainedBandwidthRecorder sustained_bandwidth_recorder_;
405 DISALLOW_COPY_AND_ASSIGN(QuicSentPacketManager);
408 } // namespace net
410 #endif // NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_