1 /** @file
2  *
3  *  A brief file description
4  *
5  *  @section license License
6  *
7  *  Licensed to the Apache Software Foundation (ASF) under one
8  *  or more contributor license agreements.  See the NOTICE file
9  *  distributed with this work for additional information
10  *  regarding copyright ownership.  The ASF licenses this file
11  *  to you under the Apache License, Version 2.0 (the
12  *  "License"); you may not use this file except in compliance
13  *  with the License.  You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  *  Unless required by applicable law or agreed to in writing, software
18  *  distributed under the License is distributed on an "AS IS" BASIS,
19  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  *  See the License for the specific language governing permissions and
21  *  limitations under the License.
22  */
23 
24 #include "QUICFlowController.h"
25 #include "QUICFrame.h"
26 
27 //
28 // QUICRateAnalyzer
29 //
30 void
update(QUICOffset offset)31 QUICRateAnalyzer::update(QUICOffset offset)
32 {
33   ink_hrtime now = Thread::get_hrtime();
34   if (offset > 0 && now > this->_start_time) {
35     this->_rate = static_cast<double>(offset) / (now - this->_start_time);
36   }
37 }
38 
39 uint64_t
expect_recv_bytes(ink_hrtime time)40 QUICRateAnalyzer::expect_recv_bytes(ink_hrtime time)
41 {
42   return static_cast<uint64_t>(time * this->_rate);
43 }
44 
45 //
46 // QUICFlowController
47 //
48 uint64_t
credit() const49 QUICFlowController::credit() const
50 {
51   return this->current_limit() - this->current_offset();
52 }
53 
54 QUICOffset
current_offset() const55 QUICFlowController::current_offset() const
56 {
57   return this->_offset;
58 }
59 
60 QUICOffset
current_limit() const61 QUICFlowController::current_limit() const
62 {
63   return this->_limit;
64 }
65 
66 int
update(QUICOffset offset)67 QUICFlowController::update(QUICOffset offset)
68 {
69   if (this->_offset <= offset) {
70     if (offset > this->_limit) {
71       return -1;
72     }
73     this->_offset = offset;
74   }
75 
76   return 0;
77 }
78 
79 void
forward_limit(QUICOffset limit)80 QUICFlowController::forward_limit(QUICOffset limit)
81 {
82   // MAX_(STREAM_)DATA might be unorderd due to delay
83   // Just ignore if the size was smaller than the last one
84   if (this->_limit > limit) {
85     return;
86   }
87   this->_limit = limit;
88 }
89 
90 void
set_limit(QUICOffset limit)91 QUICFlowController::set_limit(QUICOffset limit)
92 {
93   ink_assert(this->_limit == UINT64_MAX || this->_limit == limit);
94   this->_limit = limit;
95 }
96 
97 // For RemoteFlowController, caller of this function should also check QUICStreamManager::will_generate_frame()
98 bool
will_generate_frame(QUICEncryptionLevel level,size_t current_packet_size,bool ack_eliciting,uint32_t seq_num)99 QUICFlowController::will_generate_frame(QUICEncryptionLevel level, size_t current_packet_size, bool ack_eliciting, uint32_t seq_num)
100 {
101   if (!this->_is_level_matched(level)) {
102     return false;
103   }
104 
105   return this->_should_create_frame;
106 }
107 
108 /**
109  * @param connection_credit This is not used. Because MAX_(STREAM_)DATA frame are not flow-controlled
110  */
111 QUICFrame *
generate_frame(uint8_t * buf,QUICEncryptionLevel level,uint64_t,uint16_t maximum_frame_size,size_t current_packet_size,uint32_t seq_num)112 QUICFlowController::generate_frame(uint8_t *buf, QUICEncryptionLevel level, uint64_t /* connection_credit */,
113                                    uint16_t maximum_frame_size, size_t current_packet_size, uint32_t seq_num)
114 {
115   QUICFrame *frame = nullptr;
116 
117   if (!this->_is_level_matched(level)) {
118     return frame;
119   }
120 
121   if (this->_should_create_frame) {
122     frame = this->_create_frame(buf);
123     if (frame && frame->size() <= maximum_frame_size) {
124       this->_should_create_frame = false;
125     }
126   }
127 
128   return frame;
129 }
130 
131 //
132 // QUICRemoteFlowController
133 //
134 void
forward_limit(QUICOffset new_limit)135 QUICRemoteFlowController::forward_limit(QUICOffset new_limit)
136 {
137   QUICFlowController::forward_limit(new_limit);
138   this->_blocked             = false;
139   this->_should_create_frame = false;
140 }
141 
142 int
update(QUICOffset offset)143 QUICRemoteFlowController::update(QUICOffset offset)
144 {
145   int ret = QUICFlowController::update(offset);
146 
147   // Create BLOCKED(_STREAM) frame
148   // The frame will be sent if stream has something to send.
149   if (offset >= this->_limit) {
150     this->_should_create_frame = true;
151     this->_blocked             = true;
152   }
153 
154   return ret;
155 }
156 
157 void
_on_frame_lost(QUICFrameInformationUPtr & info)158 QUICRemoteFlowController::_on_frame_lost(QUICFrameInformationUPtr &info)
159 {
160   ink_assert(info->type == QUICFrameType::DATA_BLOCKED || info->type == QUICFrameType::STREAM_DATA_BLOCKED);
161   if (this->_offset == *reinterpret_cast<QUICOffset *>(info->data)) {
162     this->_should_create_frame = true;
163   }
164 }
165 
166 //
167 // QUICLocalFlowController
168 //
169 QUICOffset
current_limit() const170 QUICLocalFlowController::current_limit() const
171 {
172   return this->_limit;
173 }
174 
175 void
forward_limit(QUICOffset new_limit)176 QUICLocalFlowController::forward_limit(QUICOffset new_limit)
177 {
178   // Create MAX_(STREAM_)DATA frame. The frame will be sent on next WRITE_READY event on QUICNetVC
179   if (this->_need_to_forward_limit()) {
180     QUICFlowController::forward_limit(new_limit);
181     this->_should_create_frame = true;
182   }
183 }
184 
185 int
update(QUICOffset offset)186 QUICLocalFlowController::update(QUICOffset offset)
187 {
188   if (this->_offset <= offset) {
189     this->_analyzer.update(offset);
190   }
191   return QUICFlowController::update(offset);
192 }
193 
194 void
set_limit(QUICOffset limit)195 QUICLocalFlowController::set_limit(QUICOffset limit)
196 {
197   QUICFlowController::set_limit(limit);
198 }
199 
200 void
_on_frame_lost(QUICFrameInformationUPtr & info)201 QUICLocalFlowController::_on_frame_lost(QUICFrameInformationUPtr &info)
202 {
203   ink_assert(info->type == QUICFrameType::MAX_DATA || info->type == QUICFrameType::MAX_STREAM_DATA);
204   if (this->_limit == *reinterpret_cast<QUICOffset *>(info->data)) {
205     this->_should_create_frame = true;
206   }
207 }
208 
209 bool
_need_to_forward_limit()210 QUICLocalFlowController::_need_to_forward_limit()
211 {
212   QUICOffset threshold = this->_analyzer.expect_recv_bytes(2 * this->_rtt_provider->smoothed_rtt());
213   if (this->_offset + threshold >= this->_limit) {
214     return true;
215   }
216 
217   return false;
218 }
219 
220 //
221 // QUIC[Remote|Local][Connection|Stream]FlowController
222 //
223 QUICFrame *
_create_frame(uint8_t * buf)224 QUICRemoteConnectionFlowController::_create_frame(uint8_t *buf)
225 {
226   auto frame                    = QUICFrameFactory::create_data_blocked_frame(buf, this->_offset, this->_issue_frame_id(), this);
227   QUICFrameInformationUPtr info = QUICFrameInformationUPtr(quicFrameInformationAllocator.alloc());
228   info->type                    = frame->type();
229   info->level                   = QUICEncryptionLevel::NONE;
230   *(reinterpret_cast<QUICOffset *>(info->data)) = this->_offset;
231   this->_records_frame(frame->id(), std::move(info));
232   return frame;
233 }
234 
235 QUICFrame *
_create_frame(uint8_t * buf)236 QUICLocalConnectionFlowController::_create_frame(uint8_t *buf)
237 {
238   auto frame                    = QUICFrameFactory::create_max_data_frame(buf, this->_limit, this->_issue_frame_id(), this);
239   QUICFrameInformationUPtr info = QUICFrameInformationUPtr(quicFrameInformationAllocator.alloc());
240   info->type                    = frame->type();
241   info->level                   = QUICEncryptionLevel::NONE;
242   *(reinterpret_cast<QUICOffset *>(info->data)) = this->_limit;
243   this->_records_frame(frame->id(), std::move(info));
244   return frame;
245 }
246 
247 QUICFrame *
_create_frame(uint8_t * buf)248 QUICRemoteStreamFlowController::_create_frame(uint8_t *buf)
249 {
250   auto frame =
251     QUICFrameFactory::create_stream_data_blocked_frame(buf, this->_stream_id, this->_offset, this->_issue_frame_id(), this);
252   QUICFrameInformationUPtr info                 = QUICFrameInformationUPtr(quicFrameInformationAllocator.alloc());
253   info->type                                    = frame->type();
254   info->level                                   = QUICEncryptionLevel::NONE;
255   *(reinterpret_cast<QUICOffset *>(info->data)) = this->_offset;
256   this->_records_frame(frame->id(), std::move(info));
257   return frame;
258 }
259 
260 QUICFrame *
_create_frame(uint8_t * buf)261 QUICLocalStreamFlowController::_create_frame(uint8_t *buf)
262 {
263   auto frame = QUICFrameFactory::create_max_stream_data_frame(buf, this->_stream_id, this->_limit, this->_issue_frame_id(), this);
264   QUICFrameInformationUPtr info                 = QUICFrameInformationUPtr(quicFrameInformationAllocator.alloc());
265   info->type                                    = frame->type();
266   info->level                                   = QUICEncryptionLevel::NONE;
267   *(reinterpret_cast<QUICOffset *>(info->data)) = this->_limit;
268   this->_records_frame(frame->id(), std::move(info));
269   return frame;
270 }
271