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 #ifndef NET_QUIC_QUIC_FLOW_CONTROLLER_H_
6 #define NET_QUIC_QUIC_FLOW_CONTROLLER_H_
8 #include "base/basictypes.h"
9 #include "net/base/net_export.h"
10 #include "net/quic/quic_protocol.h"
15 class QuicFlowControllerPeer
;
20 const QuicStreamId kConnectionLevelId
= 0;
22 // QuicFlowController allows a QUIC stream or connection to perform flow
23 // control. The stream/connection owns a QuicFlowController which keeps track of
24 // bytes sent/received, can tell the owner if it is flow control blocked, and
25 // can send WINDOW_UPDATE or BLOCKED frames when needed.
26 class NET_EXPORT_PRIVATE QuicFlowController
{
28 QuicFlowController(QuicConnection
* connection
,
30 Perspective perspective
,
31 QuicStreamOffset send_window_offset
,
32 QuicStreamOffset receive_window_offset
,
33 bool should_auto_tune_receive_window
);
35 ~QuicFlowController() {}
37 // Called when we see a new highest received byte offset from the peer, either
38 // via a data frame or a RST.
39 // Returns true if this call changes highest_received_byte_offset_, and false
40 // in the case where |new_offset| is <= highest_received_byte_offset_.
41 bool UpdateHighestReceivedOffset(QuicStreamOffset new_offset
);
43 // Called when bytes received from the peer are consumed locally. This may
44 // trigger the sending of a WINDOW_UPDATE frame using |connection|.
45 void AddBytesConsumed(QuicByteCount bytes_consumed
);
47 // Called when bytes are sent to the peer.
48 void AddBytesSent(QuicByteCount bytes_sent
);
50 // Set a new send window offset.
51 // Returns true if this increases send_window_offset_ and is now blocked.
52 bool UpdateSendWindowOffset(QuicStreamOffset new_send_window_offset
);
54 // Returns the current available send window.
55 QuicByteCount
SendWindowSize() const;
57 // Send a BLOCKED frame if appropriate.
58 void MaybeSendBlocked();
60 // Returns true if flow control send limits have been reached.
61 bool IsBlocked() const;
63 // Returns true if flow control receive limits have been violated by the peer.
64 bool FlowControlViolation();
66 QuicByteCount
bytes_consumed() const { return bytes_consumed_
; }
68 QuicStreamOffset
highest_received_byte_offset() const {
69 return highest_received_byte_offset_
;
72 void set_receive_window_size_limit(QuicByteCount receive_window_size_limit
) {
73 DCHECK_GE(receive_window_size_limit
, receive_window_size_limit_
);
74 receive_window_size_limit_
= receive_window_size_limit
;
77 void set_auto_tune_receive_window(bool enable
) {
78 auto_tune_receive_window_
= enable
;
81 bool auto_tune_receive_window() { return auto_tune_receive_window_
; }
84 friend class test::QuicFlowControllerPeer
;
86 // Send a WINDOW_UPDATE frame if appropriate.
87 void MaybeSendWindowUpdate();
89 // Auto-tune the max receive window size.
90 void MaybeIncreaseMaxWindowSize();
92 // The parent connection, used to send connection close on flow control
93 // violation, and WINDOW_UPDATE and BLOCKED frames when appropriate.
95 QuicConnection
* connection_
;
97 // ID of stream this flow controller belongs to. This can be 0 if this is a
98 // connection level flow controller.
101 // Tracks if this is owned by a server or a client.
102 Perspective perspective_
;
104 // Tracks number of bytes sent to the peer.
105 QuicByteCount bytes_sent_
;
107 // The absolute offset in the outgoing byte stream. If this offset is reached
108 // then we become flow control blocked until we receive a WINDOW_UPDATE.
109 QuicStreamOffset send_window_offset_
;
111 // Overview of receive flow controller.
113 // 0=...===1=======2-------3 ...... FIN
117 // 1) bytes_consumed_ - moves forward when data is read out of the
120 // 2) highest_received_byte_offset_ - moves when data is received
123 // 3) receive_window_offset_ - moves when WINDOW_UPDATE is sent.
125 // 4) receive_window_size_ - maximum allowed unread data (3 - 1).
126 // This value may be increased by auto-tuning.
128 // 5) receive_window_size_limit_ - limit on receive_window_size_;
129 // auto-tuning will not increase window size beyond this limit.
131 // Track number of bytes received from the peer, which have been consumed
133 QuicByteCount bytes_consumed_
;
135 // The highest byte offset we have seen from the peer. This could be the
136 // highest offset in a data frame, or a final value in a RST.
137 QuicStreamOffset highest_received_byte_offset_
;
140 // The absolute offset in the incoming byte stream. The peer should never send
141 // us bytes which are beyond this offset.
142 QuicStreamOffset receive_window_offset_
;
144 // Largest size the receive window can grow to.
145 QuicByteCount receive_window_size_
;
147 // Upper limit on receive_window_size_;
148 QuicByteCount receive_window_size_limit_
;
150 // Used to dynamically enable receive window auto-tuning.
151 bool auto_tune_receive_window_
;
153 // Send window update when receive window size drops below this.
154 QuicByteCount
WindowUpdateThreshold();
156 // Keep track of the last time we sent a BLOCKED frame. We should only send
157 // another when the number of bytes we have sent has changed.
158 QuicStreamOffset last_blocked_send_window_offset_
;
160 // Keep time of the last time a window update was sent. We use this
161 // as part of the receive window auto tuning.
162 QuicTime prev_window_update_time_
;
164 DISALLOW_COPY_AND_ASSIGN(QuicFlowController
);
169 #endif // NET_QUIC_QUIC_FLOW_CONTROLLER_H_