Fix more MSVC warnings, courgette/ edition.
[chromium-blink-merge.git] / net / quic / quic_sent_packet_manager.h
blob2b5f738b25c7aec145cc985a54526e62a4c12a92
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 OnSpuriousPacketRetransmition(
51 TransmissionType transmission_type,
52 QuicByteCount byte_size) {}
54 virtual void OnSentPacket(
55 const SerializedPacket& packet,
56 QuicPacketSequenceNumber original_sequence_number,
57 QuicTime sent_time,
58 QuicByteCount bytes,
59 TransmissionType transmission_type) {}
61 virtual void OnIncomingAck(
62 const QuicAckFrame& ack_frame,
63 QuicTime ack_receive_time,
64 QuicPacketSequenceNumber largest_observed,
65 bool largest_observed_acked,
66 QuicPacketSequenceNumber least_unacked_sent_packet) {}
69 // Interface which gets callbacks from the QuicSentPacketManager when
70 // network-related state changes. Implementations must not mutate the
71 // state of the packet manager as a result of these callbacks.
72 class NET_EXPORT_PRIVATE NetworkChangeVisitor {
73 public:
74 virtual ~NetworkChangeVisitor() {}
76 // Called when congestion window may have changed.
77 virtual void OnCongestionWindowChange(QuicByteCount congestion_window) = 0;
78 // TODO(jri): Add OnRttStatsChange() to this class as well.
81 // Struct to store the pending retransmission information.
82 struct PendingRetransmission {
83 PendingRetransmission(QuicPacketSequenceNumber sequence_number,
84 TransmissionType transmission_type,
85 const RetransmittableFrames& retransmittable_frames,
86 QuicSequenceNumberLength sequence_number_length)
87 : sequence_number(sequence_number),
88 transmission_type(transmission_type),
89 retransmittable_frames(retransmittable_frames),
90 sequence_number_length(sequence_number_length) {
93 QuicPacketSequenceNumber sequence_number;
94 TransmissionType transmission_type;
95 const RetransmittableFrames& retransmittable_frames;
96 QuicSequenceNumberLength sequence_number_length;
99 QuicSentPacketManager(bool is_server,
100 const QuicClock* clock,
101 QuicConnectionStats* stats,
102 CongestionControlType congestion_control_type,
103 LossDetectionType loss_type);
104 virtual ~QuicSentPacketManager();
106 virtual void SetFromConfig(const QuicConfig& config);
108 void SetHandshakeConfirmed() { handshake_confirmed_ = true; }
110 // Processes the incoming ack.
111 void OnIncomingAck(const QuicAckFrame& ack_frame,
112 QuicTime ack_receive_time);
114 // Returns true if the non-FEC packet |sequence_number| is unacked.
115 bool IsUnacked(QuicPacketSequenceNumber sequence_number) const;
117 // Requests retransmission of all unacked packets of |retransmission_type|.
118 void RetransmitUnackedPackets(TransmissionType retransmission_type);
120 // Retransmits the oldest pending packet there is still a tail loss probe
121 // pending. Invoked after OnRetransmissionTimeout.
122 bool MaybeRetransmitTailLossProbe();
124 // Removes the retransmittable frames from all unencrypted packets to ensure
125 // they don't get retransmitted.
126 void NeuterUnencryptedPackets();
128 // Returns true if the unacked packet |sequence_number| has retransmittable
129 // frames. This will only return false if the packet has been acked, if a
130 // previous transmission of this packet was ACK'd, or if this packet has been
131 // retransmitted as with different sequence number.
132 bool HasRetransmittableFrames(QuicPacketSequenceNumber sequence_number) const;
134 // Returns true if there are pending retransmissions.
135 bool HasPendingRetransmissions() const;
137 // Retrieves the next pending retransmission.
138 PendingRetransmission NextPendingRetransmission();
140 bool HasUnackedPackets() const;
142 // Returns the smallest sequence number of a serialized packet which has not
143 // been acked by the peer.
144 QuicPacketSequenceNumber GetLeastUnacked() const;
146 // Called when a congestion feedback frame is received from peer.
147 virtual void OnIncomingQuicCongestionFeedbackFrame(
148 const QuicCongestionFeedbackFrame& frame,
149 const QuicTime& feedback_receive_time);
151 // Called when we have sent bytes to the peer. This informs the manager both
152 // the number of bytes sent and if they were retransmitted. Returns true if
153 // the sender should reset the retransmission timer.
154 virtual bool OnPacketSent(SerializedPacket* serialized_packet,
155 QuicPacketSequenceNumber original_sequence_number,
156 QuicTime sent_time,
157 QuicByteCount bytes,
158 TransmissionType transmission_type,
159 HasRetransmittableData has_retransmittable_data);
161 // Called when the retransmission timer expires.
162 virtual void OnRetransmissionTimeout();
164 // Calculate the time until we can send the next packet to the wire.
165 // Note 1: When kUnknownWaitTime is returned, there is no need to poll
166 // TimeUntilSend again until we receive an OnIncomingAckFrame event.
167 // Note 2: Send algorithms may or may not use |retransmit| in their
168 // calculations.
169 virtual QuicTime::Delta TimeUntilSend(QuicTime now,
170 HasRetransmittableData retransmittable);
172 // Returns amount of time for delayed ack timer.
173 const QuicTime::Delta DelayedAckTime() const;
175 // Returns the current delay for the retransmission timer, which may send
176 // either a tail loss probe or do a full RTO. Returns QuicTime::Zero() if
177 // there are no retransmittable packets.
178 const QuicTime GetRetransmissionTime() const;
180 const RttStats* GetRttStats() const;
182 // Returns the estimated bandwidth calculated by the congestion algorithm.
183 QuicBandwidth BandwidthEstimate() const;
185 // Returns true if the current instantaneous bandwidth estimate is reliable.
186 bool HasReliableBandwidthEstimate() const;
188 const QuicSustainedBandwidthRecorder& SustainedBandwidthRecorder() const;
190 // Returns the size of the current congestion window in bytes. Note, this is
191 // not the *available* window. Some send algorithms may not use a congestion
192 // window and will return 0.
193 QuicByteCount GetCongestionWindow() const;
195 // Returns the size of the slow start congestion window in bytes,
196 // aka ssthresh. Some send algorithms do not define a slow start
197 // threshold and will return 0.
198 QuicByteCount GetSlowStartThreshold() const;
200 // Enables pacing if it has not already been enabled.
201 void EnablePacing();
203 bool using_pacing() const { return using_pacing_; }
205 void set_debug_delegate(DebugDelegate* debug_delegate) {
206 debug_delegate_ = debug_delegate;
209 QuicPacketSequenceNumber largest_observed() const {
210 return unacked_packets_.largest_observed();
213 QuicPacketSequenceNumber least_packet_awaited_by_peer() {
214 return least_packet_awaited_by_peer_;
217 void set_network_change_visitor(NetworkChangeVisitor* visitor) {
218 DCHECK(!network_change_visitor_);
219 DCHECK(visitor);
220 network_change_visitor_ = visitor;
223 size_t consecutive_rto_count() const {
224 return consecutive_rto_count_;
227 size_t consecutive_tlp_count() const {
228 return consecutive_tlp_count_;
231 private:
232 friend class test::QuicConnectionPeer;
233 friend class test::QuicSentPacketManagerPeer;
235 // The retransmission timer is a single timer which switches modes depending
236 // upon connection state.
237 enum RetransmissionTimeoutMode {
238 // A conventional TCP style RTO.
239 RTO_MODE,
240 // A tail loss probe. By default, QUIC sends up to two before RTOing.
241 TLP_MODE,
242 // Retransmission of handshake packets prior to handshake completion.
243 HANDSHAKE_MODE,
244 // Re-invoke the loss detection when a packet is not acked before the
245 // loss detection algorithm expects.
246 LOSS_MODE,
249 typedef linked_hash_map<QuicPacketSequenceNumber,
250 TransmissionType> PendingRetransmissionMap;
252 // Called when a packet is retransmitted with a new sequence number.
253 // Replaces the old entry in the unacked packet map with the new
254 // sequence number.
255 void OnRetransmittedPacket(QuicPacketSequenceNumber old_sequence_number,
256 QuicPacketSequenceNumber new_sequence_number);
258 // Updates the least_packet_awaited_by_peer.
259 void UpdatePacketInformationReceivedByPeer(const QuicAckFrame& ack_frame);
261 // Process the incoming ack looking for newly ack'd data packets.
262 void HandleAckForSentPackets(const QuicAckFrame& ack_frame);
264 // Returns the current retransmission mode.
265 RetransmissionTimeoutMode GetRetransmissionMode() const;
267 // Retransmits all crypto stream packets.
268 void RetransmitCryptoPackets();
270 // Retransmits all the packets and abandons by invoking a full RTO.
271 void RetransmitAllPackets();
273 // Returns the timer for retransmitting crypto handshake packets.
274 const QuicTime::Delta GetCryptoRetransmissionDelay() const;
276 // Returns the timer for a new tail loss probe.
277 const QuicTime::Delta GetTailLossProbeDelay() const;
279 // Returns the retransmission timeout, after which a full RTO occurs.
280 const QuicTime::Delta GetRetransmissionDelay() const;
282 // Update the RTT if the ack is for the largest acked sequence number.
283 // Returns true if the rtt was updated.
284 bool MaybeUpdateRTT(const QuicAckFrame& ack_frame,
285 const QuicTime& ack_receive_time);
287 // Invokes the loss detection algorithm and loses and retransmits packets if
288 // necessary.
289 void InvokeLossDetection(QuicTime time);
291 // Invokes OnCongestionEvent if |rtt_updated| is true, there are pending acks,
292 // or pending losses. Clears pending acks and pending losses afterwards.
293 // |bytes_in_flight| is the number of bytes in flight before the losses or
294 // acks.
295 void MaybeInvokeCongestionEvent(bool rtt_updated,
296 QuicByteCount bytes_in_flight);
298 // Marks |sequence_number| as having been revived by the peer, but not
299 // received, so the packet remains pending if it is and the congestion control
300 // does not consider the packet acked.
301 void MarkPacketRevived(QuicPacketSequenceNumber sequence_number,
302 QuicTime::Delta delta_largest_observed);
304 // Removes the retransmittability and pending properties from the packet at
305 // |it| due to receipt by the peer. Returns an iterator to the next remaining
306 // unacked packet.
307 void MarkPacketHandled(QuicPacketSequenceNumber sequence_number,
308 const TransmissionInfo& info,
309 QuicTime::Delta delta_largest_observed);
311 // Request that |sequence_number| be retransmitted after the other pending
312 // retransmissions. Does not add it to the retransmissions if it's already
313 // a pending retransmission.
314 void MarkForRetransmission(QuicPacketSequenceNumber sequence_number,
315 TransmissionType transmission_type);
317 // Notify observers about spurious retransmits.
318 void RecordSpuriousRetransmissions(
319 const SequenceNumberList& all_transmissions,
320 QuicPacketSequenceNumber acked_sequence_number);
322 // Returns true if the client is sending or the server has received a
323 // connection option.
324 bool HasClientSentConnectionOption(const QuicConfig& config,
325 QuicTag tag) const;
327 // Newly serialized retransmittable and fec packets are added to this map,
328 // which contains owning pointers to any contained frames. If a packet is
329 // retransmitted, this map will contain entries for both the old and the new
330 // packet. The old packet's retransmittable frames entry will be nullptr,
331 // while the new packet's entry will contain the frames to retransmit.
332 // If the old packet is acked before the new packet, then the old entry will
333 // be removed from the map and the new entry's retransmittable frames will be
334 // set to nullptr.
335 QuicUnackedPacketMap unacked_packets_;
337 // Pending retransmissions which have not been packetized and sent yet.
338 PendingRetransmissionMap pending_retransmissions_;
340 // Tracks if the connection was created by the server.
341 bool is_server_;
343 // An AckNotifier can register to be informed when ACKs have been received for
344 // all packets that a given block of data was sent in. The AckNotifierManager
345 // maintains the currently active notifiers.
346 AckNotifierManager ack_notifier_manager_;
348 const QuicClock* clock_;
349 QuicConnectionStats* stats_;
350 DebugDelegate* debug_delegate_;
351 NetworkChangeVisitor* network_change_visitor_;
352 RttStats rtt_stats_;
353 scoped_ptr<SendAlgorithmInterface> send_algorithm_;
354 scoped_ptr<LossDetectionInterface> loss_algorithm_;
356 // Least sequence number which the peer is still waiting for.
357 QuicPacketSequenceNumber least_packet_awaited_by_peer_;
359 // Tracks the first RTO packet. If any packet before that packet gets acked,
360 // it indicates the RTO was spurious and should be reversed(F-RTO).
361 QuicPacketSequenceNumber first_rto_transmission_;
362 // Number of times the RTO timer has fired in a row without receiving an ack.
363 size_t consecutive_rto_count_;
364 // Number of times the tail loss probe has been sent.
365 size_t consecutive_tlp_count_;
366 // Number of times the crypto handshake has been retransmitted.
367 size_t consecutive_crypto_retransmission_count_;
368 // Number of pending transmissions of TLP or crypto packets.
369 size_t pending_timer_transmission_count_;
370 // Maximum number of tail loss probes to send before firing an RTO.
371 size_t max_tail_loss_probes_;
372 bool using_pacing_;
374 // Vectors packets acked and lost as a result of the last congestion event.
375 SendAlgorithmInterface::CongestionVector packets_acked_;
376 SendAlgorithmInterface::CongestionVector packets_lost_;
378 // Set to true after the crypto handshake has successfully completed. After
379 // this is true we no longer use HANDSHAKE_MODE, and further frames sent on
380 // the crypto stream (i.e. SCUP messages) are treated like normal
381 // retransmittable frames.
382 bool handshake_confirmed_;
384 // Records bandwidth from server to client in normal operation, over periods
385 // of time with no loss events.
386 QuicSustainedBandwidthRecorder sustained_bandwidth_recorder_;
388 DISALLOW_COPY_AND_ASSIGN(QuicSentPacketManager);
391 } // namespace net
393 #endif // NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_