1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/** @file
Http2ConnectionState.
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#pragma once
#include <atomic>
#include "NetTimeout.h"
#include "HTTP2.h"
#include "HPACK.h"
#include "Http2Stream.h"
#include "Http2DependencyTree.h"
#include "Http2FrequencyCounter.h"
class Http2CommonSession;
class Http2Frame;
enum class Http2SendDataFrameResult {
NO_ERROR = 0,
NO_WINDOW,
NO_PAYLOAD,
NOT_WRITE_AVAIL,
ERROR,
DONE,
};
enum Http2ShutdownState { HTTP2_SHUTDOWN_NONE, HTTP2_SHUTDOWN_NOT_INITIATED, HTTP2_SHUTDOWN_INITIATED, HTTP2_SHUTDOWN_IN_PROGRESS };
class Http2ConnectionSettings
{
public:
Http2ConnectionSettings();
void settings_from_configs();
unsigned get(Http2SettingsIdentifier id) const;
unsigned set(Http2SettingsIdentifier id, unsigned value);
private:
// Settings ID is 1-based, so convert it to a 0-based index.
static unsigned indexof(Http2SettingsIdentifier id);
unsigned settings[HTTP2_SETTINGS_MAX - 1];
};
// Http2ConnectionState
//
// Capture the semantics of a HTTP/2 connection. The client session captures the
// frame layer, and the
// connection state captures the connection-wide state.
class Http2ConnectionState : public Continuation
{
public:
Http2ConnectionState();
// noncopyable
Http2ConnectionState(const Http2ConnectionState &) = delete;
Http2ConnectionState &operator=(const Http2ConnectionState &) = delete;
ProxyError rx_error_code;
ProxyError tx_error_code;
Http2CommonSession *session = nullptr;
HpackHandle *local_hpack_handle = nullptr;
HpackHandle *remote_hpack_handle = nullptr;
DependencyTree *dependency_tree = nullptr;
ActivityCop<Http2Stream> _cop;
/** Whether the session window is in a shrinking state before we send the
* first WINDOW_UPDATE frame.
*
* Unlike HTTP/2 streams, the HTTP/2 session window has no way to initialize
* it to a value lower than 65,535. If the initial value is lower than
* 65,535, the session window will have to shrink while we receive DATA
* frames without incrementing the window via WINDOW_UPDATE frames. Once the
* window gets to the desired size, we start maintaining the window via
* WINDOW_UPDATE frames.
*/
bool server_rwnd_is_shrinking = false;
// Settings.
Http2ConnectionSettings server_settings;
Http2ConnectionSettings client_settings;
uint32_t configured_max_settings_frames_per_minute = 0;
uint32_t configured_max_ping_frames_per_minute = 0;
uint32_t configured_max_priority_frames_per_minute = 0;
uint32_t configured_max_rst_stream_frames_per_minute = 0;
uint32_t configured_max_continuation_frames_per_minute = 0;
void init(Http2CommonSession *ssn);
void send_connection_preface();
void destroy();
void rcv_frame(const Http2Frame *frame);
// Event handlers
int main_event_handler(int, void *);
int state_closed(int, void *);
// Stream control interfaces
Http2Stream *create_stream(Http2StreamId new_id, Http2Error &error);
Http2Stream *find_stream(Http2StreamId id) const;
void restart_streams();
bool delete_stream(Http2Stream *stream);
void release_stream();
void cleanup_streams();
void restart_receiving(Http2Stream *stream);
void update_initial_rwnd(Http2WindowSize new_size);
Http2StreamId get_latest_stream_id_in() const;
Http2StreamId get_latest_stream_id_out() const;
int get_stream_requests() const;
void increment_stream_requests();
// Continuated header decoding
Http2StreamId get_continued_stream_id() const;
void set_continued_stream_id(Http2StreamId stream_id);
void clear_continued_stream_id();
uint32_t get_client_stream_count() const;
void decrement_stream_count();
double get_stream_error_rate() const;
Http2ErrorCode get_shutdown_reason() const;
// HTTP/2 frame sender
void schedule_stream(Http2Stream *stream);
void send_data_frames_depends_on_priority();
void send_data_frames(Http2Stream *stream);
Http2SendDataFrameResult send_a_data_frame(Http2Stream *stream, size_t &payload_length);
void send_headers_frame(Http2Stream *stream);
bool send_push_promise_frame(Http2Stream *stream, URL &url, const MIMEField *accept_encoding);
void send_rst_stream_frame(Http2StreamId id, Http2ErrorCode ec);
void send_settings_frame(const Http2ConnectionSettings &new_settings);
void send_ping_frame(Http2StreamId id, uint8_t flag, const uint8_t *opaque_data);
void send_goaway_frame(Http2StreamId id, Http2ErrorCode ec);
void send_window_update_frame(Http2StreamId id, uint32_t size);
bool is_state_closed() const;
bool is_recursing() const;
bool is_valid_streamid(Http2StreamId id) const;
Http2ShutdownState get_shutdown_state() const;
void set_shutdown_state(Http2ShutdownState state, Http2ErrorCode reason = Http2ErrorCode::HTTP2_ERROR_NO_ERROR);
Event *get_zombie_event();
void schedule_zombie_event();
void increment_received_settings_count(uint32_t count);
uint32_t get_received_settings_count();
void increment_received_settings_frame_count();
uint32_t get_received_settings_frame_count();
void increment_received_ping_frame_count();
uint32_t get_received_ping_frame_count();
void increment_received_priority_frame_count();
uint32_t get_received_priority_frame_count();
void increment_received_rst_stream_frame_count();
uint32_t get_received_rst_stream_frame_count();
void increment_received_continuation_frame_count();
uint32_t get_received_continuation_frame_count();
ssize_t client_rwnd() const;
Http2ErrorCode increment_client_rwnd(size_t amount);
Http2ErrorCode decrement_client_rwnd(size_t amount);
ssize_t server_rwnd() const;
Http2ErrorCode increment_server_rwnd(size_t amount);
Http2ErrorCode decrement_server_rwnd(size_t amount);
private:
unsigned _adjust_concurrent_stream();
// NOTE: 'stream_list' has only active streams.
// If given Stream Identifier is not found in stream_list and it is less
// than or equal to latest_streamid_in, the state of Stream
// is CLOSED.
// If given Stream Identifier is not found in stream_list and it is greater
// than latest_streamid_in, the state of Stream is IDLE.
Queue<Http2Stream> stream_list;
Http2StreamId latest_streamid_in = 0;
Http2StreamId latest_streamid_out = 0;
std::atomic<int> stream_requests = 0;
// Counter for current active streams which is started by client
std::atomic<uint32_t> client_streams_in_count = 0;
// Counter for current active streams which is started by server
std::atomic<uint32_t> client_streams_out_count = 0;
// Counter for current active streams and streams in the process of shutting down
std::atomic<uint32_t> total_client_streams_count = 0;
// Counter for stream errors ATS sent
uint32_t stream_error_count = 0;
// Connection level window size
ssize_t _client_rwnd = HTTP2_INITIAL_WINDOW_SIZE;
ssize_t _server_rwnd = 0;
std::vector<size_t> _recent_rwnd_increment = {SIZE_MAX, SIZE_MAX, SIZE_MAX, SIZE_MAX, SIZE_MAX};
int _recent_rwnd_increment_index = 0;
Http2FrequencyCounter _received_settings_counter;
Http2FrequencyCounter _received_settings_frame_counter;
Http2FrequencyCounter _received_ping_frame_counter;
Http2FrequencyCounter _received_priority_frame_counter;
Http2FrequencyCounter _received_rst_stream_frame_counter;
Http2FrequencyCounter _received_continuation_frame_counter;
// NOTE: Id of stream which MUST receive CONTINUATION frame.
// - [RFC 7540] 6.2 HEADERS
// "A HEADERS frame without the END_HEADERS flag set MUST be followed by a
// CONTINUATION frame for the same stream."
// - [RFC 7540] 6.10 CONTINUATION
// "If the END_HEADERS bit is not set, this frame MUST be followed by
// another CONTINUATION frame."
Http2StreamId continued_stream_id = 0;
bool _scheduled = false;
bool fini_received = false;
bool in_destroy = false;
int recursion = 0;
Http2ShutdownState shutdown_state = HTTP2_SHUTDOWN_NONE;
Http2ErrorCode shutdown_reason = Http2ErrorCode::HTTP2_ERROR_MAX;
Event *shutdown_cont_event = nullptr;
Event *fini_event = nullptr;
Event *zombie_event = nullptr;
};
///////////////////////////////////////////////
// INLINE
//
inline Http2StreamId
Http2ConnectionState::get_latest_stream_id_in() const
{
return latest_streamid_in;
}
inline Http2StreamId
Http2ConnectionState::get_latest_stream_id_out() const
{
return latest_streamid_out;
}
inline int
Http2ConnectionState::get_stream_requests() const
{
return stream_requests;
}
inline void
Http2ConnectionState::increment_stream_requests()
{
stream_requests++;
}
// Continuated header decoding
inline Http2StreamId
Http2ConnectionState::get_continued_stream_id() const
{
return continued_stream_id;
}
inline void
Http2ConnectionState::set_continued_stream_id(Http2StreamId stream_id)
{
continued_stream_id = stream_id;
}
inline void
Http2ConnectionState::clear_continued_stream_id()
{
continued_stream_id = 0;
}
inline uint32_t
Http2ConnectionState::get_client_stream_count() const
{
return client_streams_in_count;
}
inline void
Http2ConnectionState::decrement_stream_count()
{
--total_client_streams_count;
}
inline double
Http2ConnectionState::get_stream_error_rate() const
{
int total = get_stream_requests();
if (static_cast<uint32_t>(total) < Http2::stream_error_sampling_threshold) {
return 0;
}
if (total >= (1 / Http2::stream_error_rate_threshold)) {
return (double)stream_error_count / (double)total;
} else {
return 0;
}
}
inline Http2ErrorCode
Http2ConnectionState::get_shutdown_reason() const
{
return shutdown_reason;
}
inline bool
Http2ConnectionState::is_state_closed() const
{
return session == nullptr || fini_received;
}
inline bool
Http2ConnectionState::is_recursing() const
{
return recursion > 0;
}
inline bool
Http2ConnectionState::is_valid_streamid(Http2StreamId id) const
{
if (http2_is_client_streamid(id)) {
return id <= get_latest_stream_id_in();
} else {
return id <= get_latest_stream_id_out();
}
}
inline Http2ShutdownState
Http2ConnectionState::get_shutdown_state() const
{
return shutdown_state;
}
inline void
Http2ConnectionState::set_shutdown_state(Http2ShutdownState state, Http2ErrorCode reason)
{
shutdown_state = state;
shutdown_reason = reason;
}
inline Event *
Http2ConnectionState::get_zombie_event()
{
return zombie_event;
}
inline void
Http2ConnectionState::schedule_zombie_event()
{
if (Http2::zombie_timeout_in) { // If we have zombie debugging enabled
if (zombie_event) {
zombie_event->cancel();
}
zombie_event = this_ethread()->schedule_in(this, HRTIME_SECONDS(Http2::zombie_timeout_in));
}
}
|