1 // Copyright 2014 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 QUICHE_QUIC_CORE_QUIC_FLOW_CONTROLLER_H_ 6 #define QUICHE_QUIC_CORE_QUIC_FLOW_CONTROLLER_H_ 7 8 #include "quiche/quic/core/quic_packets.h" 9 #include "quiche/quic/platform/api/quic_export.h" 10 11 namespace quic { 12 13 namespace test { 14 class QuicFlowControllerPeer; 15 } // namespace test 16 17 class QuicConnection; 18 class QuicSession; 19 20 // How much larger the session flow control window needs to be relative to any 21 // stream's flow control window. 22 inline constexpr float kSessionFlowControlMultiplier = 1.5; 23 24 class QUICHE_EXPORT QuicFlowControllerInterface { 25 public: ~QuicFlowControllerInterface()26 virtual ~QuicFlowControllerInterface() {} 27 28 // Ensures the flow control window is at least |window_size| and send out an 29 // update frame if it is increased. 30 virtual void EnsureWindowAtLeast(QuicByteCount window_size) = 0; 31 }; 32 33 // QuicFlowController allows a QUIC stream or connection to perform flow 34 // control. The stream/connection owns a QuicFlowController which keeps track of 35 // bytes sent/received, can tell the owner if it is flow control blocked, and 36 // can send WINDOW_UPDATE or BLOCKED frames when needed. 37 class QUICHE_EXPORT QuicFlowController : public QuicFlowControllerInterface { 38 public: 39 QuicFlowController(QuicSession* session, QuicStreamId id, 40 bool is_connection_flow_controller, 41 QuicStreamOffset send_window_offset, 42 QuicStreamOffset receive_window_offset, 43 QuicByteCount receive_window_size_limit, 44 bool should_auto_tune_receive_window, 45 QuicFlowControllerInterface* session_flow_controller); 46 47 QuicFlowController(const QuicFlowController&) = delete; 48 QuicFlowController(QuicFlowController&&) = default; 49 QuicFlowController& operator=(const QuicFlowController&) = delete; 50 ~QuicFlowController()51 ~QuicFlowController() override {} 52 53 // Called when we see a new highest received byte offset from the peer, either 54 // via a data frame or a RST. 55 // Returns true if this call changes highest_received_byte_offset_, and false 56 // in the case where |new_offset| is <= highest_received_byte_offset_. 57 bool UpdateHighestReceivedOffset(QuicStreamOffset new_offset); 58 59 // Called when bytes received from the peer are consumed locally. This may 60 // trigger the sending of a WINDOW_UPDATE frame using |connection|. 61 void AddBytesConsumed(QuicByteCount bytes_consumed); 62 63 // Called when bytes are sent to the peer. 64 void AddBytesSent(QuicByteCount bytes_sent); 65 66 // Increases |send_window_offset_| if |new_send_window_offset| is 67 // greater than the current value. Returns true if this increase 68 // also causes us to change from a blocked state to unblocked. In 69 // all other cases, returns false. 70 bool UpdateSendWindowOffset(QuicStreamOffset new_send_window_offset); 71 72 // QuicFlowControllerInterface. 73 void EnsureWindowAtLeast(QuicByteCount window_size) override; 74 75 // Returns the current available send window. 76 QuicByteCount SendWindowSize() const; 77 receive_window_size()78 QuicByteCount receive_window_size() const { return receive_window_size_; } 79 80 // Sends a BLOCKED frame if needed. 81 void MaybeSendBlocked(); 82 83 // Returns true if flow control send limits have been reached. 84 bool IsBlocked() const; 85 86 // Returns true if flow control receive limits have been violated by the peer. 87 bool FlowControlViolation(); 88 89 // Inform the peer of new receive window. 90 void SendWindowUpdate(); 91 bytes_consumed()92 QuicByteCount bytes_consumed() const { return bytes_consumed_; } 93 bytes_sent()94 QuicByteCount bytes_sent() const { return bytes_sent_; } 95 send_window_offset()96 QuicStreamOffset send_window_offset() const { return send_window_offset_; } 97 highest_received_byte_offset()98 QuicStreamOffset highest_received_byte_offset() const { 99 return highest_received_byte_offset_; 100 } 101 set_receive_window_size_limit(QuicByteCount receive_window_size_limit)102 void set_receive_window_size_limit(QuicByteCount receive_window_size_limit) { 103 QUICHE_DCHECK_GE(receive_window_size_limit, receive_window_size_limit_); 104 receive_window_size_limit_ = receive_window_size_limit; 105 } 106 107 // Should only be called before any data is received. 108 void UpdateReceiveWindowSize(QuicStreamOffset size); 109 auto_tune_receive_window()110 bool auto_tune_receive_window() { return auto_tune_receive_window_; } 111 112 private: 113 friend class test::QuicFlowControllerPeer; 114 115 // Send a WINDOW_UPDATE frame if appropriate. 116 void MaybeSendWindowUpdate(); 117 118 // Auto-tune the max receive window size. 119 void MaybeIncreaseMaxWindowSize(); 120 121 // Updates the current offset and sends a window update frame. 122 void UpdateReceiveWindowOffsetAndSendWindowUpdate( 123 QuicStreamOffset available_window); 124 125 // Double the window size as long as we haven't hit the max window size. 126 void IncreaseWindowSize(); 127 128 // Returns "stream $ID" (where $ID is set to |id_|) or "connection" based on 129 // |is_connection_flow_controller_|. 130 std::string LogLabel(); 131 132 // The parent session/connection, used to send connection close on flow 133 // control violation, and WINDOW_UPDATE and BLOCKED frames when appropriate. 134 // Not owned. 135 QuicSession* session_; 136 QuicConnection* connection_; 137 138 // ID of stream this flow controller belongs to. If 139 // |is_connection_flow_controller_| is false, this must be a valid stream ID. 140 QuicStreamId id_; 141 142 // Whether this flow controller is the connection level flow controller 143 // instead of the flow controller for a stream. If true, |id_| is ignored. 144 bool is_connection_flow_controller_; 145 146 // Tracks if this is owned by a server or a client. 147 Perspective perspective_; 148 149 // Tracks number of bytes sent to the peer. 150 QuicByteCount bytes_sent_; 151 152 // The absolute offset in the outgoing byte stream. If this offset is reached 153 // then we become flow control blocked until we receive a WINDOW_UPDATE. 154 QuicStreamOffset send_window_offset_; 155 156 // Overview of receive flow controller. 157 // 158 // 0=...===1=======2-------3 ...... FIN 159 // |<--- <= 4 --->| 160 // 161 162 // 1) bytes_consumed_ - moves forward when data is read out of the 163 // stream. 164 // 165 // 2) highest_received_byte_offset_ - moves when data is received 166 // from the peer. 167 // 168 // 3) receive_window_offset_ - moves when WINDOW_UPDATE is sent. 169 // 170 // 4) receive_window_size_ - maximum allowed unread data (3 - 1). 171 // This value may be increased by auto-tuning. 172 // 173 // 5) receive_window_size_limit_ - limit on receive_window_size_; 174 // auto-tuning will not increase window size beyond this limit. 175 176 // Track number of bytes received from the peer, which have been consumed 177 // locally. 178 QuicByteCount bytes_consumed_; 179 180 // The highest byte offset we have seen from the peer. This could be the 181 // highest offset in a data frame, or a final value in a RST. 182 QuicStreamOffset highest_received_byte_offset_; 183 184 // The absolute offset in the incoming byte stream. The peer should never send 185 // us bytes which are beyond this offset. 186 QuicStreamOffset receive_window_offset_; 187 188 // Largest size the receive window can grow to. 189 QuicByteCount receive_window_size_; 190 191 // Upper limit on receive_window_size_; 192 QuicByteCount receive_window_size_limit_; 193 194 // Used to dynamically enable receive window auto-tuning. 195 bool auto_tune_receive_window_; 196 197 // The session's flow controller. Null if this is the session flow controller. 198 // Not owned. 199 QuicFlowControllerInterface* session_flow_controller_; 200 201 // Send window update when receive window size drops below this. 202 QuicByteCount WindowUpdateThreshold(); 203 204 // Keep track of the last time we sent a BLOCKED frame. We should only send 205 // another when the number of bytes we have sent has changed. 206 QuicStreamOffset last_blocked_send_window_offset_; 207 208 // Keep time of the last time a window update was sent. We use this 209 // as part of the receive window auto tuning. 210 QuicTime prev_window_update_time_; 211 }; 212 213 } // namespace quic 214 215 #endif // QUICHE_QUIC_CORE_QUIC_FLOW_CONTROLLER_H_ 216