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.
5 #include "net/quic/quic_flow_controller.h"
7 #include "base/basictypes.h"
8 #include "net/quic/quic_connection.h"
9 #include "net/quic/quic_flags.h"
10 #include "net/quic/quic_protocol.h"
14 #define ENDPOINT (is_server_ ? "Server: " : " Client: ")
16 QuicFlowController::QuicFlowController(QuicConnection
* connection
,
19 uint64 send_window_offset
,
20 uint64 receive_window_offset
,
21 uint64 max_receive_window
)
22 : connection_(connection
),
25 is_server_(is_server
),
27 highest_received_byte_offset_(0),
29 send_window_offset_(send_window_offset
),
30 receive_window_offset_(receive_window_offset
),
31 max_receive_window_(max_receive_window
),
32 last_blocked_send_window_offset_(0) {
33 DVLOG(1) << ENDPOINT
<< "Created flow controller for stream " << id_
34 << ", setting initial receive window offset to: "
35 << receive_window_offset_
36 << ", max receive window to: "
37 << max_receive_window_
38 << ", setting send window offset to: " << send_window_offset_
;
41 void QuicFlowController::AddBytesConsumed(uint64 bytes_consumed
) {
46 bytes_consumed_
+= bytes_consumed
;
47 DVLOG(1) << ENDPOINT
<< "Stream " << id_
<< " consumed: " << bytes_consumed_
;
49 MaybeSendWindowUpdate();
52 bool QuicFlowController::UpdateHighestReceivedOffset(uint64 new_offset
) {
57 // Only update if offset has increased.
58 if (new_offset
<= highest_received_byte_offset_
) {
62 DVLOG(1) << ENDPOINT
<< "Stream " << id_
63 << " highest byte offset increased from: "
64 << highest_received_byte_offset_
<< " to " << new_offset
;
65 highest_received_byte_offset_
= new_offset
;
69 void QuicFlowController::AddBytesSent(uint64 bytes_sent
) {
74 if (bytes_sent_
+ bytes_sent
> send_window_offset_
) {
75 LOG(DFATAL
) << ENDPOINT
<< "Stream " << id_
<< " Trying to send an extra "
76 << bytes_sent
<< " bytes, when bytes_sent = " << bytes_sent_
77 << ", and send_window_offset_ = " << send_window_offset_
;
78 bytes_sent_
= send_window_offset_
;
80 // This is an error on our side, close the connection as soon as possible.
81 connection_
->SendConnectionClose(QUIC_FLOW_CONTROL_SENT_TOO_MUCH_DATA
);
85 bytes_sent_
+= bytes_sent
;
86 DVLOG(1) << ENDPOINT
<< "Stream " << id_
<< " sent: " << bytes_sent_
;
89 bool QuicFlowController::FlowControlViolation() {
94 if (highest_received_byte_offset_
> receive_window_offset_
) {
95 LOG(ERROR
) << ENDPOINT
<< "Flow control violation on stream "
96 << id_
<< ", receive window offset: "
97 << receive_window_offset_
98 << ", highest received byte offset: "
99 << highest_received_byte_offset_
;
105 void QuicFlowController::MaybeSendWindowUpdate() {
110 // Send WindowUpdate to increase receive window if
111 // (receive window offset - consumed bytes) < (max window / 2).
112 // This is behaviour copied from SPDY.
113 DCHECK_LT(bytes_consumed_
, receive_window_offset_
);
114 size_t consumed_window
= receive_window_offset_
- bytes_consumed_
;
115 size_t threshold
= (max_receive_window_
/ 2);
117 if (consumed_window
< threshold
) {
118 // Update our receive window.
119 receive_window_offset_
+= (max_receive_window_
- consumed_window
);
121 DVLOG(1) << ENDPOINT
<< "Sending WindowUpdate frame for stream " << id_
122 << ", consumed bytes: " << bytes_consumed_
123 << ", consumed window: " << consumed_window
124 << ", and threshold: " << threshold
125 << ", and max recvw: " << max_receive_window_
126 << ". New receive window offset is: " << receive_window_offset_
;
128 // Inform the peer of our new receive window.
129 connection_
->SendWindowUpdate(id_
, receive_window_offset_
);
133 void QuicFlowController::MaybeSendBlocked() {
138 if (SendWindowSize() == 0 &&
139 last_blocked_send_window_offset_
< send_window_offset_
) {
140 DVLOG(1) << ENDPOINT
<< "Stream " << id_
<< " is flow control blocked. "
141 << "Send window: " << SendWindowSize()
142 << ", bytes sent: " << bytes_sent_
143 << ", send limit: " << send_window_offset_
;
144 // The entire send_window has been consumed, we are now flow control
146 connection_
->SendBlocked(id_
);
148 // Keep track of when we last sent a BLOCKED frame so that we only send one
149 // at a given send offset.
150 last_blocked_send_window_offset_
= send_window_offset_
;
154 bool QuicFlowController::UpdateSendWindowOffset(uint64 new_send_window_offset
) {
159 // Only update if send window has increased.
160 if (new_send_window_offset
<= send_window_offset_
) {
164 DVLOG(1) << ENDPOINT
<< "UpdateSendWindowOffset for stream " << id_
165 << " with new offset " << new_send_window_offset
166 << " current offset: " << send_window_offset_
167 << " bytes_sent: " << bytes_sent_
;
169 const bool blocked
= IsBlocked();
170 send_window_offset_
= new_send_window_offset
;
174 void QuicFlowController::Disable() {
178 bool QuicFlowController::IsEnabled() const {
182 bool QuicFlowController::IsBlocked() const {
183 return IsEnabled() && SendWindowSize() == 0;
186 uint64
QuicFlowController::SendWindowSize() const {
187 if (bytes_sent_
> send_window_offset_
) {
190 return send_window_offset_
- bytes_sent_
;