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_
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"
27 class QuicConnectionPeer
;
28 class QuicSentPacketManagerPeer
;
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
{
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
{
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
,
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
{
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
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
,
101 virtual ~QuicSentPacketManager();
103 virtual void SetFromConfig(const QuicConfig
& config
);
105 // Pass the CachedNetworkParameters to the send algorithm.
106 // Returns true if this changes the initial connection state.
107 bool ResumeConnectionState(
108 const CachedNetworkParameters
& cached_network_params
,
109 bool max_bandwidth_resumption
);
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
,
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
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
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 // No longer retransmit data for |stream_id|.
219 void CancelRetransmissionsForStream(QuicStreamId stream_id
);
221 // Enables pacing if it has not already been enabled.
224 bool using_pacing() const { return using_pacing_
; }
226 void set_debug_delegate(DebugDelegate
* debug_delegate
) {
227 debug_delegate_
= debug_delegate
;
230 QuicPacketSequenceNumber
largest_observed() const {
231 return unacked_packets_
.largest_observed();
234 QuicPacketSequenceNumber
least_packet_awaited_by_peer() {
235 return least_packet_awaited_by_peer_
;
238 void set_network_change_visitor(NetworkChangeVisitor
* visitor
) {
239 DCHECK(!network_change_visitor_
);
241 network_change_visitor_
= visitor
;
244 // Used in Chromium, but not in the server.
245 size_t consecutive_rto_count() const {
246 return consecutive_rto_count_
;
249 // Used in Chromium, but not in the server.
250 size_t consecutive_tlp_count() const {
251 return consecutive_tlp_count_
;
255 friend class test::QuicConnectionPeer
;
256 friend class test::QuicSentPacketManagerPeer
;
258 // The retransmission timer is a single timer which switches modes depending
259 // upon connection state.
260 enum RetransmissionTimeoutMode
{
261 // A conventional TCP style RTO.
263 // A tail loss probe. By default, QUIC sends up to two before RTOing.
265 // Retransmission of handshake packets prior to handshake completion.
267 // Re-invoke the loss detection when a packet is not acked before the
268 // loss detection algorithm expects.
272 typedef linked_hash_map
<QuicPacketSequenceNumber
,
273 TransmissionType
> PendingRetransmissionMap
;
275 // Updates the least_packet_awaited_by_peer.
276 void UpdatePacketInformationReceivedByPeer(const QuicAckFrame
& ack_frame
);
278 // Process the incoming ack looking for newly ack'd data packets.
279 void HandleAckForSentPackets(const QuicAckFrame
& ack_frame
);
281 // Returns the current retransmission mode.
282 RetransmissionTimeoutMode
GetRetransmissionMode() const;
284 // Retransmits all crypto stream packets.
285 void RetransmitCryptoPackets();
287 // Retransmits two packets for an RTO and removes any non-retransmittable
288 // packets from flight.
289 void RetransmitRtoPackets();
291 // Returns the timer for retransmitting crypto handshake packets.
292 const QuicTime::Delta
GetCryptoRetransmissionDelay() const;
294 // Returns the timer for a new tail loss probe.
295 const QuicTime::Delta
GetTailLossProbeDelay() const;
297 // Returns the retransmission timeout, after which a full RTO occurs.
298 const QuicTime::Delta
GetRetransmissionDelay() const;
300 // Update the RTT if the ack is for the largest acked sequence number.
301 // Returns true if the rtt was updated.
302 bool MaybeUpdateRTT(const QuicAckFrame
& ack_frame
,
303 const QuicTime
& ack_receive_time
);
305 // Invokes the loss detection algorithm and loses and retransmits packets if
307 void InvokeLossDetection(QuicTime time
);
309 // Invokes OnCongestionEvent if |rtt_updated| is true, there are pending acks,
310 // or pending losses. Clears pending acks and pending losses afterwards.
311 // |bytes_in_flight| is the number of bytes in flight before the losses or
313 void MaybeInvokeCongestionEvent(bool rtt_updated
,
314 QuicByteCount bytes_in_flight
);
316 // Marks |sequence_number| as having been revived by the peer, but not
317 // received, so the packet remains pending if it is and the congestion control
318 // does not consider the packet acked.
319 void MarkPacketRevived(QuicPacketSequenceNumber sequence_number
,
320 QuicTime::Delta delta_largest_observed
);
322 // Removes the retransmittability and pending properties from the packet at
323 // |it| due to receipt by the peer. Returns an iterator to the next remaining
325 void MarkPacketHandled(QuicPacketSequenceNumber sequence_number
,
326 const TransmissionInfo
& info
,
327 QuicTime::Delta delta_largest_observed
);
329 // Request that |sequence_number| be retransmitted after the other pending
330 // retransmissions. Does not add it to the retransmissions if it's already
331 // a pending retransmission.
332 void MarkForRetransmission(QuicPacketSequenceNumber sequence_number
,
333 TransmissionType transmission_type
);
335 // Notify observers about spurious retransmits.
336 void RecordSpuriousRetransmissions(
337 const SequenceNumberList
& all_transmissions
,
338 QuicPacketSequenceNumber acked_sequence_number
);
340 // Newly serialized retransmittable and fec packets are added to this map,
341 // which contains owning pointers to any contained frames. If a packet is
342 // retransmitted, this map will contain entries for both the old and the new
343 // packet. The old packet's retransmittable frames entry will be nullptr,
344 // while the new packet's entry will contain the frames to retransmit.
345 // If the old packet is acked before the new packet, then the old entry will
346 // be removed from the map and the new entry's retransmittable frames will be
348 QuicUnackedPacketMap unacked_packets_
;
350 // Pending retransmissions which have not been packetized and sent yet.
351 PendingRetransmissionMap pending_retransmissions_
;
353 // Tracks if the connection was created by the server or the client.
354 Perspective perspective_
;
356 // An AckNotifier can register to be informed when ACKs have been received for
357 // all packets that a given block of data was sent in. The AckNotifierManager
358 // maintains the currently active notifiers.
359 AckNotifierManager ack_notifier_manager_
;
361 const QuicClock
* clock_
;
362 QuicConnectionStats
* stats_
;
363 DebugDelegate
* debug_delegate_
;
364 NetworkChangeVisitor
* network_change_visitor_
;
365 const QuicPacketCount initial_congestion_window_
;
367 scoped_ptr
<SendAlgorithmInterface
> send_algorithm_
;
368 scoped_ptr
<LossDetectionInterface
> loss_algorithm_
;
369 bool n_connection_simulation_
;
371 // Receiver side buffer in bytes.
372 QuicByteCount receive_buffer_bytes_
;
374 // Least sequence number which the peer is still waiting for.
375 QuicPacketSequenceNumber least_packet_awaited_by_peer_
;
377 // Tracks the first RTO packet. If any packet before that packet gets acked,
378 // it indicates the RTO was spurious and should be reversed(F-RTO).
379 QuicPacketSequenceNumber first_rto_transmission_
;
380 // Number of times the RTO timer has fired in a row without receiving an ack.
381 size_t consecutive_rto_count_
;
382 // Number of times the tail loss probe has been sent.
383 size_t consecutive_tlp_count_
;
384 // Number of times the crypto handshake has been retransmitted.
385 size_t consecutive_crypto_retransmission_count_
;
386 // Number of pending transmissions of TLP, RTO, or crypto packets.
387 size_t pending_timer_transmission_count_
;
388 // Maximum number of tail loss probes to send before firing an RTO.
389 size_t max_tail_loss_probes_
;
391 // If true, use the new RTO with loss based CWND reduction instead of the send
392 // algorithms's OnRetransmissionTimeout to reduce the congestion window.
395 // Vectors packets acked and lost as a result of the last congestion event.
396 SendAlgorithmInterface::CongestionVector packets_acked_
;
397 SendAlgorithmInterface::CongestionVector packets_lost_
;
399 // Set to true after the crypto handshake has successfully completed. After
400 // this is true we no longer use HANDSHAKE_MODE, and further frames sent on
401 // the crypto stream (i.e. SCUP messages) are treated like normal
402 // retransmittable frames.
403 bool handshake_confirmed_
;
405 // Records bandwidth from server to client in normal operation, over periods
406 // of time with no loss events.
407 QuicSustainedBandwidthRecorder sustained_bandwidth_recorder_
;
409 DISALLOW_COPY_AND_ASSIGN(QuicSentPacketManager
);
414 #endif // NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_