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_spdy_session.h"
|
|
|
|
|
2014-06-01 14:01:01 +02:00
|
|
|
#include <cassert>
|
2015-05-02 21:33:04 +02:00
|
|
|
#include <cerrno>
|
2014-06-01 14:01:01 +02:00
|
|
|
|
2014-02-28 01:10:42 +01:00
|
|
|
#include "h2load.h"
|
2014-08-27 17:45:12 +02:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
using namespace nghttp2;
|
2014-02-28 01:10:42 +01:00
|
|
|
|
|
|
|
namespace h2load {
|
|
|
|
|
|
|
|
SpdySession::SpdySession(Client *client, uint16_t spdy_version)
|
2014-11-27 15:39:04 +01:00
|
|
|
: client_(client), session_(nullptr), spdy_version_(spdy_version) {}
|
|
|
|
|
|
|
|
SpdySession::~SpdySession() { spdylay_session_del(session_); }
|
2014-02-28 01:10:42 +01:00
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void before_ctrl_send_callback(spdylay_session *session,
|
|
|
|
spdylay_frame_type type, spdylay_frame *frame,
|
|
|
|
void *user_data) {
|
|
|
|
auto client = static_cast<Client *>(user_data);
|
|
|
|
if (type != SPDYLAY_SYN_STREAM) {
|
2014-02-28 01:10:42 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
client->on_request(frame->syn_stream.stream_id);
|
2016-01-05 14:36:37 +01:00
|
|
|
auto req_stat = client->get_req_stat(frame->syn_stream.stream_id);
|
2015-01-31 10:13:07 +01:00
|
|
|
client->record_request_time(req_stat);
|
2014-02-28 01:10:42 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void on_ctrl_recv_callback(spdylay_session *session, spdylay_frame_type type,
|
|
|
|
spdylay_frame *frame, void *user_data) {
|
|
|
|
auto client = static_cast<Client *>(user_data);
|
|
|
|
if (type != SPDYLAY_SYN_REPLY) {
|
2014-02-28 01:10:42 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
for (auto p = frame->syn_reply.nv; *p; p += 2) {
|
2014-02-28 01:10:42 +01:00
|
|
|
auto name = *p;
|
|
|
|
auto value = *(p + 1);
|
2015-11-03 17:03:35 +01:00
|
|
|
auto namelen = strlen(name);
|
|
|
|
auto valuelen = strlen(value);
|
2014-02-28 01:10:42 +01:00
|
|
|
client->on_header(frame->syn_reply.stream_id,
|
2015-11-03 17:03:35 +01:00
|
|
|
reinterpret_cast<const uint8_t *>(name), namelen,
|
|
|
|
reinterpret_cast<const uint8_t *>(value), valuelen);
|
|
|
|
client->worker->stats.bytes_head_decomp += namelen + valuelen;
|
2014-02-28 01:10:42 +01:00
|
|
|
}
|
2015-11-03 17:03:35 +01:00
|
|
|
|
|
|
|
// Strictly speaking, we have to subtract 2 (unused field) if SPDY
|
|
|
|
// version is 2. But it is already deprecated, and we don't do
|
|
|
|
// extra work for it.
|
|
|
|
client->worker->stats.bytes_head += frame->syn_reply.hd.length - 4;
|
2015-09-12 04:18:54 +02:00
|
|
|
|
|
|
|
if (frame->syn_stream.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
|
|
|
|
client->record_ttfb();
|
|
|
|
}
|
2014-02-28 01:10:42 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void on_data_chunk_recv_callback(spdylay_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;
|
2014-03-13 17:40:41 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
auto spdy_session = static_cast<SpdySession *>(client->session.get());
|
2014-03-13 17:40:41 +01:00
|
|
|
|
|
|
|
spdy_session->handle_window_update(stream_id, len);
|
2014-02-28 01:10:42 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
void on_stream_close_callback(spdylay_session *session, int32_t stream_id,
|
|
|
|
spdylay_status_code status_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, status_code == SPDYLAY_OK);
|
2014-02-28 01:10:42 +01:00
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace {
|
2014-11-27 15:39:04 +01:00
|
|
|
ssize_t send_callback(spdylay_session *session, const uint8_t *data,
|
|
|
|
size_t length, int flags, void *user_data) {
|
|
|
|
auto client = static_cast<Client *>(user_data);
|
2014-12-27 18:59:06 +01:00
|
|
|
auto &wb = client->wb;
|
2014-03-03 13:18:24 +01:00
|
|
|
|
2016-06-12 11:50:52 +02:00
|
|
|
if (wb.rleft() >= BACKOFF_WRITE_BUFFER_THRES) {
|
2017-01-08 09:32:35 +01:00
|
|
|
return SPDYLAY_ERR_WOULDBLOCK;
|
2014-03-03 13:18:24 +01:00
|
|
|
}
|
2014-12-27 18:59:06 +01:00
|
|
|
|
2016-06-12 11:50:52 +02:00
|
|
|
return wb.append(data, length);
|
2014-02-28 01:10:42 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
} // namespace
|
2014-02-28 01:10:42 +01:00
|
|
|
|
2015-03-26 11:53:42 +01:00
|
|
|
namespace {
|
|
|
|
ssize_t file_read_callback(spdylay_session *session, int32_t stream_id,
|
|
|
|
uint8_t *buf, size_t length, int *eof,
|
|
|
|
spdylay_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-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 SPDYLAY_ERR_TEMPORAL_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
req_stat->data_offset += nread;
|
|
|
|
|
|
|
|
if (nread == 0 || req_stat->data_offset == config->data_length) {
|
|
|
|
*eof = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nread;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void SpdySession::on_connect() {
|
2014-02-28 01:10:42 +01:00
|
|
|
spdylay_session_callbacks callbacks = {0};
|
|
|
|
callbacks.send_callback = send_callback;
|
|
|
|
callbacks.before_ctrl_send_callback = before_ctrl_send_callback;
|
|
|
|
callbacks.on_data_chunk_recv_callback = on_data_chunk_recv_callback;
|
|
|
|
callbacks.on_stream_close_callback = on_stream_close_callback;
|
|
|
|
callbacks.on_ctrl_recv_callback = on_ctrl_recv_callback;
|
|
|
|
|
|
|
|
spdylay_session_client_new(&session_, spdy_version_, &callbacks, client_);
|
|
|
|
|
2014-03-13 17:40:41 +01:00
|
|
|
int val = 1;
|
2014-11-27 15:39:04 +01:00
|
|
|
spdylay_session_set_option(session_, SPDYLAY_OPT_NO_AUTO_WINDOW_UPDATE, &val,
|
|
|
|
sizeof(val));
|
2014-03-13 17:40:41 +01:00
|
|
|
|
2015-02-06 14:44:09 +01:00
|
|
|
spdylay_settings_entry iv;
|
|
|
|
iv.settings_id = SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE;
|
|
|
|
iv.flags = SPDYLAY_ID_FLAG_SETTINGS_NONE;
|
|
|
|
iv.value = (1 << client_->worker->config->window_bits);
|
|
|
|
spdylay_submit_settings(session_, SPDYLAY_FLAG_SETTINGS_NONE, &iv, 1);
|
2014-03-13 17:40:41 +01:00
|
|
|
|
|
|
|
auto config = client_->worker->config;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (spdy_version_ >= SPDYLAY_PROTO_SPDY3_1 &&
|
|
|
|
config->connection_window_bits > 16) {
|
|
|
|
auto delta =
|
|
|
|
(1 << config->connection_window_bits) - SPDYLAY_INITIAL_WINDOW_SIZE;
|
2014-03-13 17:40:41 +01:00
|
|
|
spdylay_submit_window_update(session_, 0, delta);
|
|
|
|
}
|
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 SpdySession::submit_request() {
|
2015-10-29 14:31:03 +01:00
|
|
|
int rv;
|
2014-03-14 15:15:01 +01:00
|
|
|
auto config = client_->worker->config;
|
2014-11-27 15:39:04 +01:00
|
|
|
auto &nv = config->nv[client_->reqidx++];
|
2014-03-14 15:15:01 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (client_->reqidx == config->nv.size()) {
|
2014-03-14 15:15:01 +01:00
|
|
|
client_->reqidx = 0;
|
|
|
|
}
|
|
|
|
|
2015-03-26 11:53:42 +01:00
|
|
|
spdylay_data_provider prd{{0}, file_read_callback};
|
|
|
|
|
2015-10-29 14:31:03 +01:00
|
|
|
rv = spdylay_submit_request(session_, 0, nv.data(),
|
2016-01-05 14:36:37 +01:00
|
|
|
config->data_fd == -1 ? nullptr : &prd, nullptr);
|
2015-10-29 14:31:03 +01:00
|
|
|
|
|
|
|
if (rv != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2014-02-28 01:10:42 +01:00
|
|
|
}
|
|
|
|
|
2014-12-27 18:59:06 +01:00
|
|
|
int SpdySession::on_read(const uint8_t *data, size_t len) {
|
|
|
|
auto rv = spdylay_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 (spdylay_session_want_read(session_) == 0 &&
|
|
|
|
spdylay_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 SpdySession::on_write() {
|
2014-12-27 18:59:06 +01:00
|
|
|
auto rv = spdylay_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-11-27 15:39:04 +01:00
|
|
|
if (spdylay_session_want_read(session_) == 0 &&
|
2014-12-27 18:59:06 +01:00
|
|
|
spdylay_session_want_write(session_) == 0 && client_->wb.rleft() == 0) {
|
2014-02-28 01:10:42 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void SpdySession::terminate() {
|
2014-02-28 01:10:42 +01:00
|
|
|
spdylay_session_fail_session(session_, SPDYLAY_OK);
|
|
|
|
}
|
|
|
|
|
2014-03-13 17:40:41 +01:00
|
|
|
namespace {
|
|
|
|
int32_t determine_window_update_transmission(spdylay_session *session,
|
|
|
|
int32_t stream_id,
|
2014-11-27 15:39:04 +01:00
|
|
|
size_t window_bits) {
|
2014-03-13 17:40:41 +01:00
|
|
|
int32_t recv_length;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream_id == 0) {
|
2014-03-13 17:40:41 +01:00
|
|
|
recv_length = spdylay_session_get_recv_data_length(session);
|
|
|
|
} else {
|
2014-11-27 15:39:04 +01:00
|
|
|
recv_length =
|
|
|
|
spdylay_session_get_stream_recv_data_length(session, stream_id);
|
2014-03-13 17:40:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
auto window_size = 1 << window_bits;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (recv_length != -1 && recv_length >= window_size / 2) {
|
2014-03-13 17:40:41 +01:00
|
|
|
return recv_length;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void SpdySession::handle_window_update(int32_t stream_id, size_t recvlen) {
|
2014-03-13 17:40:41 +01:00
|
|
|
auto config = client_->worker->config;
|
|
|
|
size_t connection_window_bits;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (config->connection_window_bits > 16) {
|
2014-03-13 17:40:41 +01:00
|
|
|
connection_window_bits = config->connection_window_bits;
|
|
|
|
} else {
|
|
|
|
connection_window_bits = 16;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
auto delta =
|
|
|
|
determine_window_update_transmission(session_, 0, connection_window_bits);
|
|
|
|
if (delta > 0) {
|
2014-03-13 17:40:41 +01:00
|
|
|
spdylay_submit_window_update(session_, 0, delta);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
delta = determine_window_update_transmission(session_, stream_id,
|
|
|
|
config->window_bits);
|
|
|
|
if (delta > 0) {
|
2014-03-13 17:40:41 +01:00
|
|
|
spdylay_submit_window_update(session_, stream_id, delta);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-12 10:42:12 +02:00
|
|
|
size_t SpdySession::max_concurrent_streams() {
|
|
|
|
return (size_t)client_->worker->config->max_concurrent_streams;
|
|
|
|
}
|
|
|
|
|
2014-02-28 01:10:42 +01:00
|
|
|
} // namespace h2load
|