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