2014-02-28 01:10:42 +01:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2014-02-28 01:10:42 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 2014 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
#include "h2load_http2_session.h"
|
|
|
|
|
2014-04-05 13:04:09 +02:00
|
|
|
#include <cassert>
|
2015-03-30 16:59:03 +02:00
|
|
|
#include <cerrno>
|
2017-09-22 11:12:20 +02:00
|
|
|
#include <iostream>
|
2014-04-05 13:04:09 +02:00
|
|
|
|
2014-02-28 01:10:42 +01:00
|
|
|
#include "h2load.h"
|
2014-03-04 16:23:33 +01:00
|
|
|
#include "util.h"
|
2015-02-06 15:27:15 +01:00
|
|
|
#include "template.h"
|
2014-03-04 16:23:33 +01:00
|
|
|
|
|
|
|
using namespace nghttp2;
|
2014-02-28 01:10:42 +01:00
|
|
|
|
|
|
|
namespace h2load {
|
|
|
|
|
|
|
|
Http2Session::Http2Session(Client *client)
|
2014-11-27 15:39:04 +01:00
|
|
|
: client_(client), session_(nullptr) {}
|
2014-02-28 01:10:42 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
Http2Session::~Http2Session() { nghttp2_session_del(session_); }
|
2014-02-28 01:10:42 +01:00
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_header_callback(nghttp2_session *session, const nghttp2_frame *frame,
|
2014-02-28 01:10:42 +01:00
|
|
|
const uint8_t *name, size_t namelen,
|
2014-11-27 15:39:04 +01:00
|
|
|
const uint8_t *value, size_t valuelen, uint8_t flags,
|
|
|
|
void *user_data) {
|
|
|
|
auto client = static_cast<Client *>(user_data);
|
|
|
|
if (frame->hd.type != NGHTTP2_HEADERS ||
|
|
|
|
frame->headers.cat != NGHTTP2_HCAT_RESPONSE) {
|
2014-02-28 01:10:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
client->on_header(frame->hd.stream_id, name, namelen, value, valuelen);
|
2015-11-03 17:03:35 +01:00
|
|
|
client->worker->stats.bytes_head_decomp += namelen + valuelen;
|
2017-09-22 11:12:20 +02:00
|
|
|
|
|
|
|
if (client->worker->config->verbose) {
|
|
|
|
std::cout << "[stream_id=" << frame->hd.stream_id << "] ";
|
|
|
|
std::cout.write(reinterpret_cast<const char *>(name), namelen);
|
|
|
|
std::cout << ": ";
|
|
|
|
std::cout.write(reinterpret_cast<const char *>(value), valuelen);
|
|
|
|
std::cout << "\n";
|
|
|
|
}
|
|
|
|
|
2014-02-28 01:10:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_frame_recv_callback(nghttp2_session *session, const nghttp2_frame *frame,
|
|
|
|
void *user_data) {
|
|
|
|
auto client = static_cast<Client *>(user_data);
|
|
|
|
if (frame->hd.type != NGHTTP2_HEADERS ||
|
|
|
|
frame->headers.cat != NGHTTP2_HCAT_RESPONSE) {
|
2014-02-28 01:10:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-11-03 17:03:35 +01:00
|
|
|
client->worker->stats.bytes_head +=
|
|
|
|
frame->hd.length - frame->headers.padlen -
|
|
|
|
((frame->hd.flags & NGHTTP2_FLAG_PRIORITY) ? 5 : 0);
|
2015-09-12 04:18:54 +02:00
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
|
|
|
client->record_ttfb();
|
|
|
|
}
|
2014-02-28 01:10:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_data_chunk_recv_callback(nghttp2_session *session, uint8_t flags,
|
|
|
|
int32_t stream_id, const uint8_t *data,
|
|
|
|
size_t len, void *user_data) {
|
|
|
|
auto client = static_cast<Client *>(user_data);
|
2015-09-12 04:18:54 +02:00
|
|
|
client->record_ttfb();
|
2014-02-28 01:10:42 +01:00
|
|
|
client->worker->stats.bytes_body += len;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
int on_stream_close_callback(nghttp2_session *session, int32_t stream_id,
|
|
|
|
uint32_t error_code, void *user_data) {
|
|
|
|
auto client = static_cast<Client *>(user_data);
|
2016-01-05 14:36:37 +01:00
|
|
|
client->on_stream_close(stream_id, error_code == NGHTTP2_NO_ERROR);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int on_frame_not_send_callback(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame, int lib_error_code,
|
|
|
|
void *user_data) {
|
|
|
|
if (frame->hd.type != NGHTTP2_HEADERS ||
|
|
|
|
frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
|
2015-02-11 15:20:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2016-01-05 14:36:37 +01:00
|
|
|
|
|
|
|
auto client = static_cast<Client *>(user_data);
|
|
|
|
// request was not sent. Mark it as error.
|
|
|
|
client->on_stream_close(frame->hd.stream_id, false);
|
|
|
|
|
2015-01-31 10:13:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int before_frame_send_callback(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame, void *user_data) {
|
|
|
|
if (frame->hd.type != NGHTTP2_HEADERS ||
|
|
|
|
frame->headers.cat != NGHTTP2_HCAT_REQUEST) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto client = static_cast<Client *>(user_data);
|
2016-01-05 14:36:37 +01:00
|
|
|
auto req_stat = client->get_req_stat(frame->hd.stream_id);
|
2015-02-11 15:20:13 +01:00
|
|
|
assert(req_stat);
|
2015-01-31 10:13:07 +01:00
|
|
|
client->record_request_time(req_stat);
|
|
|
|
|
2014-02-28 01:10:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2015-03-26 11:53:42 +01:00
|
|
|
namespace {
|
|
|
|
ssize_t file_read_callback(nghttp2_session *session, int32_t stream_id,
|
|
|
|
uint8_t *buf, size_t length, uint32_t *data_flags,
|
|
|
|
nghttp2_data_source *source, void *user_data) {
|
|
|
|
auto client = static_cast<Client *>(user_data);
|
|
|
|
auto config = client->worker->config;
|
2016-01-05 14:36:37 +01:00
|
|
|
auto req_stat = client->get_req_stat(stream_id);
|
2015-04-09 17:21:31 +02:00
|
|
|
assert(req_stat);
|
2015-03-26 11:53:42 +01:00
|
|
|
ssize_t nread;
|
|
|
|
while ((nread = pread(config->data_fd, buf, length, req_stat->data_offset)) ==
|
|
|
|
-1 &&
|
|
|
|
errno == EINTR)
|
|
|
|
;
|
|
|
|
|
|
|
|
if (nread == -1) {
|
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
req_stat->data_offset += nread;
|
|
|
|
|
2016-05-13 17:40:35 +02:00
|
|
|
if (req_stat->data_offset == config->data_length) {
|
2015-03-26 11:53:42 +01:00
|
|
|
*data_flags |= NGHTTP2_DATA_FLAG_EOF;
|
2016-05-13 17:40:35 +02:00
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req_stat->data_offset > config->data_length || nread == 0) {
|
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
2015-03-26 11:53:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
namespace {
|
|
|
|
ssize_t send_callback(nghttp2_session *session, const uint8_t *data,
|
|
|
|
size_t length, int flags, void *user_data) {
|
|
|
|
auto client = static_cast<Client *>(user_data);
|
|
|
|
auto &wb = client->wb;
|
|
|
|
|
2016-06-12 11:50:52 +02:00
|
|
|
if (wb.rleft() >= BACKOFF_WRITE_BUFFER_THRES) {
|
2014-12-27 18:59:06 +01:00
|
|
|
return NGHTTP2_ERR_WOULDBLOCK;
|
|
|
|
}
|
|
|
|
|
2016-06-12 11:50:52 +02:00
|
|
|
return wb.append(data, length);
|
2014-12-27 18:59:06 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Http2Session::on_connect() {
|
2014-04-05 13:04:09 +02:00
|
|
|
int rv;
|
|
|
|
|
2017-05-18 16:10:44 +02:00
|
|
|
// This is required with --disable-assert.
|
|
|
|
(void)rv;
|
|
|
|
|
2014-08-22 13:59:50 +02:00
|
|
|
nghttp2_session_callbacks *callbacks;
|
2014-02-28 01:10:42 +01:00
|
|
|
|
2014-08-22 13:59:50 +02:00
|
|
|
nghttp2_session_callbacks_new(&callbacks);
|
|
|
|
|
2015-02-06 15:27:15 +01:00
|
|
|
auto callbacks_deleter = defer(nghttp2_session_callbacks_del, callbacks);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_frame_recv_callback(callbacks,
|
|
|
|
on_frame_recv_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_data_chunk_recv_callback(
|
|
|
|
callbacks, on_data_chunk_recv_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_stream_close_callback(
|
|
|
|
callbacks, on_stream_close_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_callbacks_set_on_header_callback(callbacks,
|
|
|
|
on_header_callback);
|
2014-08-22 13:59:50 +02:00
|
|
|
|
2016-01-05 14:36:37 +01:00
|
|
|
nghttp2_session_callbacks_set_on_frame_not_send_callback(
|
|
|
|
callbacks, on_frame_not_send_callback);
|
|
|
|
|
2015-01-31 10:13:07 +01:00
|
|
|
nghttp2_session_callbacks_set_before_frame_send_callback(
|
|
|
|
callbacks, before_frame_send_callback);
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
nghttp2_session_callbacks_set_send_callback(callbacks, send_callback);
|
|
|
|
|
2016-10-06 15:20:10 +02:00
|
|
|
nghttp2_option *opt;
|
2014-02-28 01:10:42 +01:00
|
|
|
|
2016-10-06 15:20:10 +02:00
|
|
|
rv = nghttp2_option_new(&opt);
|
|
|
|
assert(rv == 0);
|
|
|
|
|
|
|
|
auto config = client_->worker->config;
|
|
|
|
|
|
|
|
if (config->encoder_header_table_size != NGHTTP2_DEFAULT_HEADER_TABLE_SIZE) {
|
|
|
|
nghttp2_option_set_max_deflate_dynamic_table_size(
|
|
|
|
opt, config->encoder_header_table_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
nghttp2_session_client_new2(&session_, callbacks, client_, opt);
|
|
|
|
|
|
|
|
nghttp2_option_del(opt);
|
|
|
|
|
|
|
|
std::array<nghttp2_settings_entry, 3> iv;
|
|
|
|
size_t niv = 2;
|
2014-02-28 01:10:42 +01:00
|
|
|
iv[0].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
|
|
|
|
iv[0].value = 0;
|
|
|
|
iv[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
|
2016-10-06 15:20:10 +02:00
|
|
|
iv[1].value = (1 << config->window_bits) - 1;
|
|
|
|
|
|
|
|
if (config->header_table_size != NGHTTP2_DEFAULT_HEADER_TABLE_SIZE) {
|
|
|
|
iv[niv].settings_id = NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
|
|
|
|
iv[niv].value = config->header_table_size;
|
|
|
|
++niv;
|
|
|
|
}
|
2014-04-05 13:04:09 +02:00
|
|
|
|
2016-10-06 15:20:10 +02:00
|
|
|
rv = nghttp2_submit_settings(session_, NGHTTP2_FLAG_NONE, iv.data(), niv);
|
2014-04-05 13:04:09 +02:00
|
|
|
|
|
|
|
assert(rv == 0);
|
2014-02-28 01:10:42 +01:00
|
|
|
|
2016-10-06 15:20:10 +02:00
|
|
|
auto connection_window = (1 << config->connection_window_bits) - 1;
|
2016-05-29 16:34:38 +02:00
|
|
|
nghttp2_session_set_local_window_size(session_, NGHTTP2_FLAG_NONE, 0,
|
|
|
|
connection_window);
|
2014-02-28 01:10:42 +01:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
client_->signal_write();
|
2014-02-28 01:10:42 +01:00
|
|
|
}
|
|
|
|
|
2016-01-05 14:36:37 +01:00
|
|
|
int Http2Session::submit_request() {
|
2015-10-30 14:48:27 +01:00
|
|
|
if (nghttp2_session_check_request_allowed(session_) == 0) {
|
2015-10-29 14:49:27 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-03-14 15:15:01 +01:00
|
|
|
auto config = client_->worker->config;
|
2014-11-27 15:39:04 +01:00
|
|
|
auto &nva = config->nva[client_->reqidx++];
|
2014-03-14 15:15:01 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (client_->reqidx == config->nva.size()) {
|
2014-03-14 15:15:01 +01:00
|
|
|
client_->reqidx = 0;
|
|
|
|
}
|
|
|
|
|
2015-03-26 11:53:42 +01:00
|
|
|
nghttp2_data_provider prd{{0}, file_read_callback};
|
|
|
|
|
|
|
|
auto stream_id =
|
|
|
|
nghttp2_submit_request(session_, nullptr, nva.data(), nva.size(),
|
2016-01-05 14:36:37 +01:00
|
|
|
config->data_fd == -1 ? nullptr : &prd, nullptr);
|
2015-10-29 14:31:03 +01:00
|
|
|
if (stream_id < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-01-05 14:36:37 +01:00
|
|
|
client_->on_request(stream_id);
|
|
|
|
|
2015-10-29 14:31:03 +01:00
|
|
|
return 0;
|
2014-02-28 01:10:42 +01:00
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
int Http2Session::on_read(const uint8_t *data, size_t len) {
|
|
|
|
auto rv = nghttp2_session_mem_recv(session_, data, len);
|
|
|
|
if (rv < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
assert(static_cast<size_t>(rv) == len);
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
if (nghttp2_session_want_read(session_) == 0 &&
|
|
|
|
nghttp2_session_want_write(session_) == 0 && client_->wb.rleft() == 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
client_->signal_write();
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
return 0;
|
2014-02-28 01:10:42 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int Http2Session::on_write() {
|
2014-12-27 18:59:06 +01:00
|
|
|
auto rv = nghttp2_session_send(session_);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-03-03 13:18:24 +01:00
|
|
|
return -1;
|
2014-02-28 01:10:42 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_session_want_read(session_) == 0 &&
|
2014-12-27 18:59:06 +01:00
|
|
|
nghttp2_session_want_write(session_) == 0 && client_->wb.rleft() == 0) {
|
2014-02-28 01:10:42 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2014-02-28 01:10:42 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void Http2Session::terminate() {
|
2014-02-28 01:10:42 +01:00
|
|
|
nghttp2_session_terminate_session(session_, NGHTTP2_NO_ERROR);
|
|
|
|
}
|
|
|
|
|
2016-06-12 10:42:12 +02:00
|
|
|
size_t Http2Session::max_concurrent_streams() {
|
|
|
|
return (size_t)client_->worker->config->max_concurrent_streams;
|
|
|
|
}
|
|
|
|
|
2014-02-28 01:10:42 +01:00
|
|
|
} // namespace h2load
|