Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / net / quic / quic_sent_packet_manager.h
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.
4
5 #ifndef NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_
6 #define NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_
7
8 #include <deque>
9 #include <list>
10 #include <map>
11 #include <queue>
12 #include <set>
13 #include <utility>
14 #include <vector>
15
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"
25
26 namespace net {
27
28 namespace test {
29 class QuicConnectionPeer;
30 class QuicSentPacketManagerPeer;
31 }  // namespace test
32
33 class QuicClock;
34 class QuicConfig;
35 struct QuicConnectionStats;
36
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   // Interface which gets callbacks from the QuicSentPacketManager at
45   // interesting points.  Implementations must not mutate the state of
46   // the packet manager or connection as a result of these callbacks.
47   class NET_EXPORT_PRIVATE DebugDelegate {
48    public:
49     virtual ~DebugDelegate() {}
50
51     // Called when a spurious retransmission is detected.
52     virtual void OnSpuriousPacketRetransmition(
53         TransmissionType transmission_type,
54         QuicByteCount byte_size) {}
55
56     virtual void OnSentPacket(
57         QuicPacketSequenceNumber sequence_number,
58         QuicTime sent_time,
59         QuicByteCount bytes) {}
60
61     virtual void OnRetransmittedPacket(
62         QuicPacketSequenceNumber old_sequence_number,
63         QuicPacketSequenceNumber new_sequence_number,
64         TransmissionType transmission_type,
65         QuicTime time) {}
66
67     virtual void OnIncomingAck(
68         const QuicAckFrame& ack_frame,
69         QuicTime ack_receive_time,
70         QuicPacketSequenceNumber largest_observed,
71         bool largest_observed_acked,
72         QuicPacketSequenceNumber least_unacked_sent_packet) {}
73   };
74
75   // Interface which gets callbacks from the QuicSentPacketManager when
76   // network-related state changes. Implementations must not mutate the
77   // state of the packet manager as a result of these callbacks.
78   class NET_EXPORT_PRIVATE NetworkChangeVisitor {
79    public:
80     virtual ~NetworkChangeVisitor() {}
81
82     // Called when congestion window may have changed.
83     virtual void OnCongestionWindowChange(QuicByteCount congestion_window) = 0;
84     // TODO(jri): Add OnRttStatsChange() to this class as well.
85   };
86
87   // Struct to store the pending retransmission information.
88   struct PendingRetransmission {
89     PendingRetransmission(QuicPacketSequenceNumber sequence_number,
90                           TransmissionType transmission_type,
91                           const RetransmittableFrames& retransmittable_frames,
92                           QuicSequenceNumberLength sequence_number_length)
93             : sequence_number(sequence_number),
94               transmission_type(transmission_type),
95               retransmittable_frames(retransmittable_frames),
96               sequence_number_length(sequence_number_length) {
97         }
98
99         QuicPacketSequenceNumber sequence_number;
100         TransmissionType transmission_type;
101         const RetransmittableFrames& retransmittable_frames;
102         QuicSequenceNumberLength sequence_number_length;
103   };
104
105   QuicSentPacketManager(bool is_server,
106                         const QuicClock* clock,
107                         QuicConnectionStats* stats,
108                         CongestionControlType congestion_control_type,
109                         LossDetectionType loss_type);
110   virtual ~QuicSentPacketManager();
111
112   virtual void SetFromConfig(const QuicConfig& config);
113
114   void SetHandshakeConfirmed() { handshake_confirmed_ = true; }
115
116   // Called when a new packet is serialized.  If the packet contains
117   // retransmittable data, it will be added to the unacked packet map.
118   void OnSerializedPacket(const SerializedPacket& serialized_packet);
119
120   // Called when a packet is retransmitted with a new sequence number.
121   // Replaces the old entry in the unacked packet map with the new
122   // sequence number.
123   void OnRetransmittedPacket(QuicPacketSequenceNumber old_sequence_number,
124                              QuicPacketSequenceNumber new_sequence_number);
125
126   // Processes the incoming ack.
127   void OnIncomingAck(const QuicAckFrame& ack_frame,
128                      QuicTime ack_receive_time);
129
130   // Returns true if the non-FEC packet |sequence_number| is unacked.
131   bool IsUnacked(QuicPacketSequenceNumber sequence_number) const;
132
133   // Requests retransmission of all unacked packets of |retransmission_type|.
134   void RetransmitUnackedPackets(RetransmissionType retransmission_type);
135
136   // Retransmits the oldest pending packet there is still a tail loss probe
137   // pending.  Invoked after OnRetransmissionTimeout.
138   bool MaybeRetransmitTailLossProbe();
139
140   // Removes the retransmittable frames from all unencrypted packets to ensure
141   // they don't get retransmitted.
142   void NeuterUnencryptedPackets();
143
144   // Returns true if the unacked packet |sequence_number| has retransmittable
145   // frames.  This will only return false if the packet has been acked, if a
146   // previous transmission of this packet was ACK'd, or if this packet has been
147   // retransmitted as with different sequence number.
148   bool HasRetransmittableFrames(QuicPacketSequenceNumber sequence_number) const;
149
150   // Returns true if there are pending retransmissions.
151   bool HasPendingRetransmissions() const;
152
153   // Retrieves the next pending retransmission.
154   PendingRetransmission NextPendingRetransmission();
155
156   bool HasUnackedPackets() const;
157
158   // Returns the smallest sequence number of a serialized packet which has not
159   // been acked by the peer.  If there are no unacked packets, returns 0.
160   QuicPacketSequenceNumber GetLeastUnackedSentPacket() const;
161
162   // Called when a congestion feedback frame is received from peer.
163   virtual void OnIncomingQuicCongestionFeedbackFrame(
164       const QuicCongestionFeedbackFrame& frame,
165       const QuicTime& feedback_receive_time);
166
167   // Called when we have sent bytes to the peer.  This informs the manager both
168   // the number of bytes sent and if they were retransmitted.  Returns true if
169   // the sender should reset the retransmission timer.
170   virtual bool OnPacketSent(QuicPacketSequenceNumber sequence_number,
171                             QuicTime sent_time,
172                             QuicByteCount bytes,
173                             TransmissionType transmission_type,
174                             HasRetransmittableData has_retransmittable_data);
175
176   // Called when the retransmission timer expires.
177   virtual void OnRetransmissionTimeout();
178
179   // Calculate the time until we can send the next packet to the wire.
180   // Note 1: When kUnknownWaitTime is returned, there is no need to poll
181   // TimeUntilSend again until we receive an OnIncomingAckFrame event.
182   // Note 2: Send algorithms may or may not use |retransmit| in their
183   // calculations.
184   virtual QuicTime::Delta TimeUntilSend(QuicTime now,
185                                         HasRetransmittableData retransmittable);
186
187   // Returns amount of time for delayed ack timer.
188   const QuicTime::Delta DelayedAckTime() const;
189
190   // Returns the current delay for the retransmission timer, which may send
191   // either a tail loss probe or do a full RTO.  Returns QuicTime::Zero() if
192   // there are no retransmittable packets.
193   const QuicTime GetRetransmissionTime() const;
194
195   const RttStats* GetRttStats() const;
196
197   // Returns the estimated bandwidth calculated by the congestion algorithm.
198   QuicBandwidth BandwidthEstimate() const;
199
200   // Returns true if the current bandwidth estimate is reliable.
201   bool HasReliableBandwidthEstimate() const;
202
203   // Returns the size of the current congestion window in bytes.  Note, this is
204   // not the *available* window.  Some send algorithms may not use a congestion
205   // window and will return 0.
206   QuicByteCount GetCongestionWindow() const;
207
208   // Returns the size of the slow start congestion window in bytes,
209   // aka ssthresh.  Some send algorithms do not define a slow start
210   // threshold and will return 0.
211   QuicByteCount GetSlowStartThreshold() const;
212
213   // Enables pacing if it has not already been enabled, and if
214   // FLAGS_enable_quic_pacing is set.
215   void MaybeEnablePacing();
216
217   bool using_pacing() const { return using_pacing_; }
218
219   void set_debug_delegate(DebugDelegate* debug_delegate) {
220     debug_delegate_ = debug_delegate;
221   }
222
223   QuicPacketSequenceNumber largest_observed() const {
224     return largest_observed_;
225   }
226
227   void set_network_change_visitor(NetworkChangeVisitor* visitor) {
228     DCHECK(!network_change_visitor_);
229     DCHECK(visitor);
230     network_change_visitor_ = visitor;
231   }
232
233  private:
234   friend class test::QuicConnectionPeer;
235   friend class test::QuicSentPacketManagerPeer;
236
237   // The retransmission timer is a single timer which switches modes depending
238   // upon connection state.
239   enum RetransmissionTimeoutMode {
240     // A conventional TCP style RTO.
241     RTO_MODE,
242     // A tail loss probe.  By default, QUIC sends up to two before RTOing.
243     TLP_MODE,
244     // Retransmission of handshake packets prior to handshake completion.
245     HANDSHAKE_MODE,
246     // Re-invoke the loss detection when a packet is not acked before the
247     // loss detection algorithm expects.
248     LOSS_MODE,
249   };
250
251   typedef linked_hash_map<QuicPacketSequenceNumber,
252                           TransmissionType> PendingRetransmissionMap;
253
254   // Process the incoming ack looking for newly ack'd data packets.
255   void HandleAckForSentPackets(const QuicAckFrame& ack_frame);
256
257   // Returns the current retransmission mode.
258   RetransmissionTimeoutMode GetRetransmissionMode() const;
259
260   // Retransmits all crypto stream packets.
261   void RetransmitCryptoPackets();
262
263   // Retransmits all the packets and abandons by invoking a full RTO.
264   void RetransmitAllPackets();
265
266   // Returns the timer for retransmitting crypto handshake packets.
267   const QuicTime::Delta GetCryptoRetransmissionDelay() const;
268
269   // Returns the timer for a new tail loss probe.
270   const QuicTime::Delta GetTailLossProbeDelay() const;
271
272   // Returns the retransmission timeout, after which a full RTO occurs.
273   const QuicTime::Delta GetRetransmissionDelay() const;
274
275   // Update the RTT if the ack is for the largest acked sequence number.
276   // Returns true if the rtt was updated.
277   bool MaybeUpdateRTT(const QuicAckFrame& ack_frame,
278                       const QuicTime& ack_receive_time);
279
280   // Invokes the loss detection algorithm and loses and retransmits packets if
281   // necessary.
282   void InvokeLossDetection(QuicTime time);
283
284   // Invokes OnCongestionEvent if |rtt_updated| is true, there are pending acks,
285   // or pending losses.  Clears pending acks and pending losses afterwards.
286   // |bytes_in_flight| is the number of bytes in flight before the losses or
287   // acks.
288   void MaybeInvokeCongestionEvent(bool rtt_updated,
289                                   QuicByteCount bytes_in_flight);
290
291   // Marks |sequence_number| as having been revived by the peer, but not
292   // received, so the packet remains pending if it is and the congestion control
293   // does not consider the packet acked.
294   void MarkPacketRevived(QuicPacketSequenceNumber sequence_number,
295                          QuicTime::Delta delta_largest_observed);
296
297   // Removes the retransmittability and pending properties from the packet at
298   // |it| due to receipt by the peer.  Returns an iterator to the next remaining
299   // unacked packet.
300   QuicUnackedPacketMap::const_iterator MarkPacketHandled(
301       QuicUnackedPacketMap::const_iterator it,
302       QuicTime::Delta delta_largest_observed);
303
304   // Request that |sequence_number| be retransmitted after the other pending
305   // retransmissions.  Does not add it to the retransmissions if it's already
306   // a pending retransmission.
307   void MarkForRetransmission(QuicPacketSequenceNumber sequence_number,
308                              TransmissionType transmission_type);
309
310   // Notify observers about spurious retransmits.
311   void RecordSpuriousRetransmissions(
312       const SequenceNumberSet& all_transmissions,
313       QuicPacketSequenceNumber acked_sequence_number);
314
315   // Newly serialized retransmittable and fec packets are added to this map,
316   // which contains owning pointers to any contained frames.  If a packet is
317   // retransmitted, this map will contain entries for both the old and the new
318   // packet. The old packet's retransmittable frames entry will be NULL, while
319   // the new packet's entry will contain the frames to retransmit.
320   // If the old packet is acked before the new packet, then the old entry will
321   // be removed from the map and the new entry's retransmittable frames will be
322   // set to NULL.
323   QuicUnackedPacketMap unacked_packets_;
324
325   // Pending retransmissions which have not been packetized and sent yet.
326   PendingRetransmissionMap pending_retransmissions_;
327
328   // Tracks if the connection was created by the server.
329   bool is_server_;
330
331   // An AckNotifier can register to be informed when ACKs have been received for
332   // all packets that a given block of data was sent in. The AckNotifierManager
333   // maintains the currently active notifiers.
334   AckNotifierManager ack_notifier_manager_;
335
336   const QuicClock* clock_;
337   QuicConnectionStats* stats_;
338   DebugDelegate* debug_delegate_;
339   NetworkChangeVisitor* network_change_visitor_;
340   RttStats rtt_stats_;
341   scoped_ptr<SendAlgorithmInterface> send_algorithm_;
342   scoped_ptr<LossDetectionInterface> loss_algorithm_;
343
344   // The largest sequence number which we have sent and received an ACK for
345   // from the peer.
346   QuicPacketSequenceNumber largest_observed_;
347
348   // Tracks the first RTO packet.  If any packet before that packet gets acked,
349   // it indicates the RTO was spurious and should be reversed(F-RTO).
350   QuicPacketSequenceNumber first_rto_transmission_;
351   // Number of times the RTO timer has fired in a row without receiving an ack.
352   size_t consecutive_rto_count_;
353   // Number of times the tail loss probe has been sent.
354   size_t consecutive_tlp_count_;
355   // Number of times the crypto handshake has been retransmitted.
356   size_t consecutive_crypto_retransmission_count_;
357   // Whether a tlp packet can be sent even if the send algorithm says not to.
358   bool pending_tlp_transmission_;
359   // Maximum number of tail loss probes to send before firing an RTO.
360   size_t max_tail_loss_probes_;
361   bool using_pacing_;
362
363   // Sets of packets acked and lost as a result of the last congestion event.
364   SendAlgorithmInterface::CongestionMap packets_acked_;
365   SendAlgorithmInterface::CongestionMap packets_lost_;
366
367   // Set to true after the crypto handshake has successfully completed. After
368   // this is true we no longer use HANDSHAKE_MODE, and further frames sent on
369   // the crypto stream (i.e. SCUP messages) are treated like normal
370   // retransmittable frames.
371   bool handshake_confirmed_;
372
373   DISALLOW_COPY_AND_ASSIGN(QuicSentPacketManager);
374 };
375
376 }  // namespace net
377
378 #endif  // NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_