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