| OLD | NEW |
| (Empty) |
| 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 <stddef.h> | |
| 9 | |
| 10 #include <map> | |
| 11 #include <memory> | |
| 12 #include <set> | |
| 13 #include <utility> | |
| 14 #include <vector> | |
| 15 | |
| 16 #include "base/macros.h" | |
| 17 #include "net/base/linked_hash_map.h" | |
| 18 #include "net/quic/congestion_control/loss_detection_interface.h" | |
| 19 #include "net/quic/congestion_control/rtt_stats.h" | |
| 20 #include "net/quic/congestion_control/send_algorithm_interface.h" | |
| 21 #include "net/quic/quic_protocol.h" | |
| 22 #include "net/quic/quic_sent_packet_manager_interface.h" | |
| 23 #include "net/quic/quic_unacked_packet_map.h" | |
| 24 | |
| 25 namespace net { | |
| 26 | |
| 27 namespace test { | |
| 28 class QuicConnectionPeer; | |
| 29 class QuicSentPacketManagerPeer; | |
| 30 } // namespace test | |
| 31 | |
| 32 class QuicClock; | |
| 33 class QuicConfig; | |
| 34 struct QuicConnectionStats; | |
| 35 | |
| 36 // Class which tracks the set of packets sent on a QUIC connection and contains | |
| 37 // a send algorithm to decide when to send new packets. It keeps track of any | |
| 38 // retransmittable data associated with each packet. If a packet is | |
| 39 // retransmitted, it will keep track of each version of a packet so that if a | |
| 40 // previous transmission is acked, the data will not be retransmitted. | |
| 41 class NET_EXPORT_PRIVATE QuicSentPacketManager | |
| 42 : public QuicSentPacketManagerInterface { | |
| 43 public: | |
| 44 // A delegate interface which manages pending retransmissions. | |
| 45 class MultipathDelegateInterface { | |
| 46 public: | |
| 47 virtual ~MultipathDelegateInterface() {} | |
| 48 | |
| 49 // Called when unencrypted |packet_number| is requested to be neutered. | |
| 50 virtual void OnUnencryptedPacketsNeutered( | |
| 51 QuicPathId path_id, | |
| 52 QuicPacketNumber packet_number) = 0; | |
| 53 // Called when |packet_number| is requested to be retransmitted. | |
| 54 virtual void OnRetransmissionMarked(QuicPathId path_id, | |
| 55 QuicPacketNumber packet_number, | |
| 56 TransmissionType transmission_type) = 0; | |
| 57 // Called when |packet_number| is marked as not retransmittable. | |
| 58 virtual void OnPacketMarkedNotRetransmittable( | |
| 59 QuicPathId path_id, | |
| 60 QuicPacketNumber packet_number, | |
| 61 QuicTime::Delta delta_largest_observed) = 0; | |
| 62 // Called when any transmission of |packet_number| is handled. | |
| 63 virtual void OnPacketMarkedHandled( | |
| 64 QuicPathId path_id, | |
| 65 QuicPacketNumber packet_number, | |
| 66 QuicTime::Delta delta_largest_observed) = 0; | |
| 67 }; | |
| 68 | |
| 69 QuicSentPacketManager(Perspective perspective, | |
| 70 QuicPathId path_id, | |
| 71 const QuicClock* clock, | |
| 72 QuicConnectionStats* stats, | |
| 73 CongestionControlType congestion_control_type, | |
| 74 LossDetectionType loss_type, | |
| 75 MultipathDelegateInterface* delegate); | |
| 76 ~QuicSentPacketManager() override; | |
| 77 | |
| 78 // Start implementation of QuicSentPacketManagerInterface. | |
| 79 void SetFromConfig(const QuicConfig& config) override; | |
| 80 | |
| 81 // Pass the CachedNetworkParameters to the send algorithm. | |
| 82 void ResumeConnectionState( | |
| 83 const CachedNetworkParameters& cached_network_params, | |
| 84 bool max_bandwidth_resumption) override; | |
| 85 | |
| 86 void SetNumOpenStreams(size_t num_streams) override; | |
| 87 | |
| 88 void SetMaxPacingRate(QuicBandwidth max_pacing_rate) override; | |
| 89 | |
| 90 void SetHandshakeConfirmed() override; | |
| 91 | |
| 92 // Processes the incoming ack. | |
| 93 void OnIncomingAck(const QuicAckFrame& ack_frame, | |
| 94 QuicTime ack_receive_time) override; | |
| 95 | |
| 96 // Requests retransmission of all unacked packets of |retransmission_type|. | |
| 97 // The behavior of this method depends on the value of |retransmission_type|: | |
| 98 // ALL_UNACKED_RETRANSMISSION - All unacked packets will be retransmitted. | |
| 99 // This can happen, for example, after a version negotiation packet has been | |
| 100 // received and all packets needs to be retransmitted with the new version. | |
| 101 // ALL_INITIAL_RETRANSMISSION - Only initially encrypted packets will be | |
| 102 // retransmitted. This can happen, for example, when a CHLO has been rejected | |
| 103 // and the previously encrypted data needs to be encrypted with a new key. | |
| 104 void RetransmitUnackedPackets(TransmissionType retransmission_type) override; | |
| 105 | |
| 106 // Retransmits the oldest pending packet there is still a tail loss probe | |
| 107 // pending. Invoked after OnRetransmissionTimeout. | |
| 108 bool MaybeRetransmitTailLossProbe() override; | |
| 109 | |
| 110 // Removes the retransmittable frames from all unencrypted packets to ensure | |
| 111 // they don't get retransmitted. | |
| 112 void NeuterUnencryptedPackets() override; | |
| 113 | |
| 114 // Returns true if there are pending retransmissions. | |
| 115 // Not const because retransmissions may be cancelled before returning. | |
| 116 bool HasPendingRetransmissions() const override; | |
| 117 | |
| 118 // Retrieves the next pending retransmission. You must ensure that | |
| 119 // there are pending retransmissions prior to calling this function. | |
| 120 PendingRetransmission NextPendingRetransmission() override; | |
| 121 | |
| 122 bool HasUnackedPackets() const override; | |
| 123 | |
| 124 // Returns the smallest packet number of a serialized packet which has not | |
| 125 // been acked by the peer. | |
| 126 QuicPacketNumber GetLeastUnacked(QuicPathId) const override; | |
| 127 | |
| 128 // Called when we have sent bytes to the peer. This informs the manager both | |
| 129 // the number of bytes sent and if they were retransmitted. Returns true if | |
| 130 // the sender should reset the retransmission timer. | |
| 131 bool OnPacketSent(SerializedPacket* serialized_packet, | |
| 132 QuicPathId /*original_path_id*/, | |
| 133 QuicPacketNumber original_packet_number, | |
| 134 QuicTime sent_time, | |
| 135 TransmissionType transmission_type, | |
| 136 HasRetransmittableData has_retransmittable_data) override; | |
| 137 | |
| 138 // Called when the retransmission timer expires. | |
| 139 void OnRetransmissionTimeout() override; | |
| 140 | |
| 141 // Calculate the time until we can send the next packet to the wire. | |
| 142 // Note 1: When kUnknownWaitTime is returned, there is no need to poll | |
| 143 // TimeUntilSend again until we receive an OnIncomingAckFrame event. | |
| 144 // Note 2: Send algorithms may or may not use |retransmit| in their | |
| 145 // calculations. | |
| 146 QuicTime::Delta TimeUntilSend(QuicTime now, | |
| 147 HasRetransmittableData retransmittable, | |
| 148 QuicPathId* path_id) override; | |
| 149 | |
| 150 // Returns the current delay for the retransmission timer, which may send | |
| 151 // either a tail loss probe or do a full RTO. Returns QuicTime::Zero() if | |
| 152 // there are no retransmittable packets. | |
| 153 const QuicTime GetRetransmissionTime() const override; | |
| 154 | |
| 155 const RttStats* GetRttStats() const override; | |
| 156 | |
| 157 // Returns the estimated bandwidth calculated by the congestion algorithm. | |
| 158 QuicBandwidth BandwidthEstimate() const override; | |
| 159 | |
| 160 const QuicSustainedBandwidthRecorder* SustainedBandwidthRecorder() | |
| 161 const override; | |
| 162 | |
| 163 // Returns the size of the current congestion window in number of | |
| 164 // kDefaultTCPMSS-sized segments. Note, this is not the *available* window. | |
| 165 // Some send algorithms may not use a congestion window and will return 0. | |
| 166 QuicPacketCount GetCongestionWindowInTcpMss() const override; | |
| 167 | |
| 168 // Returns the number of packets of length |max_packet_length| which fit in | |
| 169 // the current congestion window. More packets may end up in flight if the | |
| 170 // congestion window has been recently reduced, of if non-full packets are | |
| 171 // sent. | |
| 172 QuicPacketCount EstimateMaxPacketsInFlight( | |
| 173 QuicByteCount max_packet_length) const override; | |
| 174 | |
| 175 // Returns the size of the current congestion window size in bytes. | |
| 176 QuicByteCount GetCongestionWindowInBytes() const override; | |
| 177 | |
| 178 // Returns the size of the slow start congestion window in nume of 1460 byte | |
| 179 // TCP segments, aka ssthresh. Some send algorithms do not define a slow | |
| 180 // start threshold and will return 0. | |
| 181 QuicPacketCount GetSlowStartThresholdInTcpMss() const override; | |
| 182 | |
| 183 // No longer retransmit data for |stream_id|. | |
| 184 void CancelRetransmissionsForStream(QuicStreamId stream_id) override; | |
| 185 | |
| 186 // Called when peer address changes and the connection migrates. | |
| 187 void OnConnectionMigration(QuicPathId, PeerAddressChangeType type) override; | |
| 188 | |
| 189 bool IsHandshakeConfirmed() const override; | |
| 190 | |
| 191 void SetDebugDelegate(DebugDelegate* debug_delegate) override; | |
| 192 | |
| 193 QuicPacketNumber GetLargestObserved(QuicPathId) const override; | |
| 194 | |
| 195 QuicPacketNumber GetLargestSentPacket(QuicPathId) const override; | |
| 196 | |
| 197 QuicPacketNumber GetLeastPacketAwaitedByPeer(QuicPathId) const override; | |
| 198 | |
| 199 void SetNetworkChangeVisitor(NetworkChangeVisitor* visitor) override; | |
| 200 | |
| 201 bool InSlowStart() const override; | |
| 202 | |
| 203 size_t GetConsecutiveRtoCount() const override; | |
| 204 | |
| 205 size_t GetConsecutiveTlpCount() const override; | |
| 206 | |
| 207 private: | |
| 208 friend class test::QuicConnectionPeer; | |
| 209 friend class test::QuicSentPacketManagerPeer; | |
| 210 | |
| 211 // The retransmission timer is a single timer which switches modes depending | |
| 212 // upon connection state. | |
| 213 enum RetransmissionTimeoutMode { | |
| 214 // A conventional TCP style RTO. | |
| 215 RTO_MODE, | |
| 216 // A tail loss probe. By default, QUIC sends up to two before RTOing. | |
| 217 TLP_MODE, | |
| 218 // Retransmission of handshake packets prior to handshake completion. | |
| 219 HANDSHAKE_MODE, | |
| 220 // Re-invoke the loss detection when a packet is not acked before the | |
| 221 // loss detection algorithm expects. | |
| 222 LOSS_MODE, | |
| 223 }; | |
| 224 | |
| 225 typedef linked_hash_map<QuicPacketNumber, TransmissionType> | |
| 226 PendingRetransmissionMap; | |
| 227 | |
| 228 // Updates the least_packet_awaited_by_peer. | |
| 229 void UpdatePacketInformationReceivedByPeer(const QuicAckFrame& ack_frame); | |
| 230 | |
| 231 // Process the incoming ack looking for newly ack'd data packets. | |
| 232 void HandleAckForSentPackets(const QuicAckFrame& ack_frame); | |
| 233 | |
| 234 // Returns the current retransmission mode. | |
| 235 RetransmissionTimeoutMode GetRetransmissionMode() const; | |
| 236 | |
| 237 // Retransmits all crypto stream packets. | |
| 238 void RetransmitCryptoPackets(); | |
| 239 | |
| 240 // Retransmits two packets for an RTO and removes any non-retransmittable | |
| 241 // packets from flight. | |
| 242 void RetransmitRtoPackets(); | |
| 243 | |
| 244 // Returns the timer for retransmitting crypto handshake packets. | |
| 245 const QuicTime::Delta GetCryptoRetransmissionDelay() const; | |
| 246 | |
| 247 // Returns the timer for a new tail loss probe. | |
| 248 const QuicTime::Delta GetTailLossProbeDelay() const; | |
| 249 | |
| 250 // Returns the retransmission timeout, after which a full RTO occurs. | |
| 251 const QuicTime::Delta GetRetransmissionDelay() const; | |
| 252 | |
| 253 // Returns the newest transmission associated with a packet. | |
| 254 QuicPacketNumber GetNewestRetransmission( | |
| 255 QuicPacketNumber packet_number, | |
| 256 const TransmissionInfo& transmission_info) const; | |
| 257 | |
| 258 // Update the RTT if the ack is for the largest acked packet number. | |
| 259 // Returns true if the rtt was updated. | |
| 260 bool MaybeUpdateRTT(const QuicAckFrame& ack_frame, QuicTime ack_receive_time); | |
| 261 | |
| 262 // Invokes the loss detection algorithm and loses and retransmits packets if | |
| 263 // necessary. | |
| 264 void InvokeLossDetection(QuicTime time); | |
| 265 | |
| 266 // Invokes OnCongestionEvent if |rtt_updated| is true, there are pending acks, | |
| 267 // or pending losses. Clears pending acks and pending losses afterwards. | |
| 268 // |bytes_in_flight| is the number of bytes in flight before the losses or | |
| 269 // acks. | |
| 270 void MaybeInvokeCongestionEvent(bool rtt_updated, | |
| 271 QuicByteCount bytes_in_flight); | |
| 272 | |
| 273 // Called when frames of |packet_number| has been received but the packet | |
| 274 // itself has not been received by the peer. Currently, this method is not | |
| 275 // used. | |
| 276 // TODO(fayang): Update the comment when multipath sent packet manager is | |
| 277 // landed. | |
| 278 // The packet needs no longer to be retransmitted, but the packet remains | |
| 279 // pending if it is and the congestion control does not consider the packet | |
| 280 // acked. | |
| 281 void MarkPacketNotRetransmittable(QuicPacketNumber packet_number, | |
| 282 QuicTime::Delta ack_delay_time); | |
| 283 | |
| 284 // Removes the retransmittability and in flight properties from the packet at | |
| 285 // |info| due to receipt by the peer. | |
| 286 void MarkPacketHandled(QuicPacketNumber packet_number, | |
| 287 TransmissionInfo* info, | |
| 288 QuicTime::Delta ack_delay_time); | |
| 289 | |
| 290 // Request that |packet_number| be retransmitted after the other pending | |
| 291 // retransmissions. Does not add it to the retransmissions if it's already | |
| 292 // a pending retransmission. | |
| 293 void MarkForRetransmission(QuicPacketNumber packet_number, | |
| 294 TransmissionType transmission_type); | |
| 295 | |
| 296 // Notify observers that packet with TransmissionInfo |info| is a spurious | |
| 297 // retransmission. It is caller's responsibility to guarantee the packet with | |
| 298 // TransmissionInfo |info| is a spurious retransmission before calling this | |
| 299 // function. | |
| 300 void RecordOneSpuriousRetransmission(const TransmissionInfo& info); | |
| 301 | |
| 302 // Notify observers about spurious retransmits of packet with TransmissionInfo | |
| 303 // |info|. | |
| 304 void RecordSpuriousRetransmissions(const TransmissionInfo& info, | |
| 305 QuicPacketNumber acked_packet_number); | |
| 306 | |
| 307 // Returns mutable TransmissionInfo associated with |packet_number|, which | |
| 308 // must be unacked. | |
| 309 TransmissionInfo* GetMutableTransmissionInfo(QuicPacketNumber packet_number); | |
| 310 | |
| 311 // Remove any packets no longer needed for retransmission, congestion, or | |
| 312 // RTT measurement purposes. | |
| 313 void RemoveObsoletePackets(); | |
| 314 | |
| 315 // Enables pacing if it has not already been enabled. | |
| 316 void EnablePacing(); | |
| 317 | |
| 318 // Newly serialized retransmittable packets are added to this map, which | |
| 319 // contains owning pointers to any contained frames. If a packet is | |
| 320 // retransmitted, this map will contain entries for both the old and the new | |
| 321 // packet. The old packet's retransmittable frames entry will be nullptr, | |
| 322 // while the new packet's entry will contain the frames to retransmit. | |
| 323 // If the old packet is acked before the new packet, then the old entry will | |
| 324 // be removed from the map and the new entry's retransmittable frames will be | |
| 325 // set to nullptr. | |
| 326 QuicUnackedPacketMap unacked_packets_; | |
| 327 | |
| 328 // Pending retransmissions which have not been packetized and sent yet. | |
| 329 PendingRetransmissionMap pending_retransmissions_; | |
| 330 | |
| 331 // Tracks if the connection was created by the server or the client. | |
| 332 Perspective perspective_; | |
| 333 | |
| 334 QuicPathId path_id_; | |
| 335 | |
| 336 const QuicClock* clock_; | |
| 337 QuicConnectionStats* stats_; | |
| 338 | |
| 339 // Pending retransmissions are managed by delegate_ if it is not null. | |
| 340 MultipathDelegateInterface* delegate_; // Not owned. | |
| 341 | |
| 342 DebugDelegate* debug_delegate_; | |
| 343 NetworkChangeVisitor* network_change_visitor_; | |
| 344 const QuicPacketCount initial_congestion_window_; | |
| 345 RttStats rtt_stats_; | |
| 346 std::unique_ptr<SendAlgorithmInterface> send_algorithm_; | |
| 347 std::unique_ptr<LossDetectionInterface> loss_algorithm_; | |
| 348 bool n_connection_simulation_; | |
| 349 | |
| 350 // Receiver side buffer in bytes. | |
| 351 QuicByteCount receive_buffer_bytes_; | |
| 352 | |
| 353 // Least packet number which the peer is still waiting for. | |
| 354 QuicPacketNumber least_packet_awaited_by_peer_; | |
| 355 | |
| 356 // Tracks the first RTO packet. If any packet before that packet gets acked, | |
| 357 // it indicates the RTO was spurious and should be reversed(F-RTO). | |
| 358 QuicPacketNumber first_rto_transmission_; | |
| 359 // Number of times the RTO timer has fired in a row without receiving an ack. | |
| 360 size_t consecutive_rto_count_; | |
| 361 // Number of times the tail loss probe has been sent. | |
| 362 size_t consecutive_tlp_count_; | |
| 363 // Number of times the crypto handshake has been retransmitted. | |
| 364 size_t consecutive_crypto_retransmission_count_; | |
| 365 // Number of pending transmissions of TLP, RTO, or crypto packets. | |
| 366 size_t pending_timer_transmission_count_; | |
| 367 // Maximum number of tail loss probes to send before firing an RTO. | |
| 368 size_t max_tail_loss_probes_; | |
| 369 // If true, send the TLP at 0.5 RTT. | |
| 370 bool enable_half_rtt_tail_loss_probe_; | |
| 371 bool using_pacing_; | |
| 372 // If true, use the new RTO with loss based CWND reduction instead of the send | |
| 373 // algorithms's OnRetransmissionTimeout to reduce the congestion window. | |
| 374 bool use_new_rto_; | |
| 375 // If true, cancel pending retransmissions if they're larger than | |
| 376 // largest_newly_acked. | |
| 377 bool undo_pending_retransmits_; | |
| 378 | |
| 379 // Vectors packets acked and lost as a result of the last congestion event. | |
| 380 SendAlgorithmInterface::CongestionVector packets_acked_; | |
| 381 SendAlgorithmInterface::CongestionVector packets_lost_; | |
| 382 // Largest newly acknowledged packet. | |
| 383 QuicPacketNumber largest_newly_acked_; | |
| 384 // Largest packet in bytes ever acknowledged. | |
| 385 QuicPacketLength largest_mtu_acked_; | |
| 386 | |
| 387 // Set to true after the crypto handshake has successfully completed. After | |
| 388 // this is true we no longer use HANDSHAKE_MODE, and further frames sent on | |
| 389 // the crypto stream (i.e. SCUP messages) are treated like normal | |
| 390 // retransmittable frames. | |
| 391 bool handshake_confirmed_; | |
| 392 | |
| 393 // Records bandwidth from server to client in normal operation, over periods | |
| 394 // of time with no loss events. | |
| 395 QuicSustainedBandwidthRecorder sustained_bandwidth_recorder_; | |
| 396 | |
| 397 DISALLOW_COPY_AND_ASSIGN(QuicSentPacketManager); | |
| 398 }; | |
| 399 | |
| 400 } // namespace net | |
| 401 | |
| 402 #endif // NET_QUIC_QUIC_SENT_PACKET_MANAGER_H_ | |
| OLD | NEW |