nghttp2/src/h2load_quic.cc

869 lines
24 KiB
C++
Raw Normal View History

2019-06-08 15:05:25 +02:00
/*
* nghttp2 - HTTP/2 C Library
*
* Copyright (c) 2019 nghttp2 contributors
*
* 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_quic.h"
2019-12-18 06:23:05 +01:00
#include <netinet/udp.h>
2019-06-08 15:05:25 +02:00
#include <iostream>
2021-10-15 12:52:01 +02:00
#ifdef HAVE_LIBNGTCP2_CRYPTO_OPENSSL
# include <ngtcp2/ngtcp2_crypto_openssl.h>
#endif // HAVE_LIBNGTCP2_CRYPTO_OPENSSL
#ifdef HAVE_LIBNGTCP2_CRYPTO_BORINGSSL
# include <ngtcp2/ngtcp2_crypto_boringssl.h>
#endif // HAVE_LIBNGTCP2_CRYPTO_BORINGSSL
2019-06-08 15:05:25 +02:00
#include <openssl/err.h>
#include <openssl/rand.h>
2019-06-08 15:05:25 +02:00
#include "h2load_http3_session.h"
namespace h2load {
namespace {
int handshake_completed(ngtcp2_conn *conn, void *user_data) {
auto c = static_cast<Client *>(user_data);
if (c->quic_handshake_completed() != 0) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
} // namespace
int Client::quic_handshake_completed() { return connection_made(); }
2019-06-08 15:05:25 +02:00
namespace {
2020-06-07 15:49:34 +02:00
int recv_stream_data(ngtcp2_conn *conn, uint32_t flags, int64_t stream_id,
2019-06-08 15:05:25 +02:00
uint64_t offset, const uint8_t *data, size_t datalen,
void *user_data, void *stream_user_data) {
auto c = static_cast<Client *>(user_data);
2020-06-07 15:49:34 +02:00
if (c->quic_recv_stream_data(flags, stream_id, data, datalen) != 0) {
2019-06-08 15:05:25 +02:00
// TODO Better to do this gracefully rather than
// NGTCP2_ERR_CALLBACK_FAILURE. Perhaps, call
// ngtcp2_conn_write_application_close() ?
2019-08-31 10:33:00 +02:00
return NGTCP2_ERR_CALLBACK_FAILURE;
2019-06-08 15:05:25 +02:00
}
return 0;
}
} // namespace
2020-06-07 15:49:34 +02:00
int Client::quic_recv_stream_data(uint32_t flags, int64_t stream_id,
2019-06-08 15:05:25 +02:00
const uint8_t *data, size_t datalen) {
2019-11-20 15:40:21 +01:00
if (worker->current_phase == Phase::MAIN_DURATION) {
worker->stats.bytes_total += datalen;
}
2019-06-08 15:05:25 +02:00
auto s = static_cast<Http3Session *>(session.get());
2020-06-07 15:49:34 +02:00
auto nconsumed = s->read_stream(flags, stream_id, data, datalen);
2019-06-08 15:05:25 +02:00
if (nconsumed == -1) {
return -1;
}
ngtcp2_conn_extend_max_stream_offset(quic.conn, stream_id, nconsumed);
ngtcp2_conn_extend_max_offset(quic.conn, nconsumed);
return 0;
}
namespace {
int acked_stream_data_offset(ngtcp2_conn *conn, int64_t stream_id,
2020-06-03 17:49:40 +02:00
uint64_t offset, uint64_t datalen, void *user_data,
void *stream_user_data) {
auto c = static_cast<Client *>(user_data);
if (c->quic_acked_stream_data_offset(stream_id, datalen) != 0) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
} // namespace
int Client::quic_acked_stream_data_offset(int64_t stream_id, size_t datalen) {
auto s = static_cast<Http3Session *>(session.get());
if (s->add_ack_offset(stream_id, datalen) != 0) {
return -1;
}
return 0;
}
2019-06-08 15:05:25 +02:00
namespace {
2021-09-11 09:57:14 +02:00
int stream_close(ngtcp2_conn *conn, uint32_t flags, int64_t stream_id,
uint64_t app_error_code, void *user_data,
void *stream_user_data) {
2019-06-08 15:05:25 +02:00
auto c = static_cast<Client *>(user_data);
2021-09-11 09:57:14 +02:00
if (!(flags & NGTCP2_STREAM_CLOSE_FLAG_APP_ERROR_CODE_SET)) {
app_error_code = NGHTTP3_H3_NO_ERROR;
}
2019-06-08 15:05:25 +02:00
if (c->quic_stream_close(stream_id, app_error_code) != 0) {
2022-01-23 15:46:19 +01:00
return NGTCP2_ERR_CALLBACK_FAILURE;
2019-06-08 15:05:25 +02:00
}
return 0;
}
} // namespace
2019-07-14 15:11:42 +02:00
int Client::quic_stream_close(int64_t stream_id, uint64_t app_error_code) {
2019-06-08 15:05:25 +02:00
auto s = static_cast<Http3Session *>(session.get());
2021-09-11 09:59:56 +02:00
if (s->close_stream(stream_id, app_error_code) != 0) {
2019-06-08 15:05:25 +02:00
return -1;
}
return 0;
}
namespace {
int stream_reset(ngtcp2_conn *conn, int64_t stream_id, uint64_t final_size,
2019-07-14 15:11:42 +02:00
uint64_t app_error_code, void *user_data,
2019-06-08 15:05:25 +02:00
void *stream_user_data) {
auto c = static_cast<Client *>(user_data);
if (c->quic_stream_reset(stream_id, app_error_code) != 0) {
2022-01-23 15:46:19 +01:00
return NGTCP2_ERR_CALLBACK_FAILURE;
2019-06-08 15:05:25 +02:00
}
return 0;
}
} // namespace
2019-07-14 15:11:42 +02:00
int Client::quic_stream_reset(int64_t stream_id, uint64_t app_error_code) {
2019-06-08 15:05:25 +02:00
auto s = static_cast<Http3Session *>(session.get());
if (s->shutdown_stream_read(stream_id) != 0) {
return -1;
}
return 0;
}
namespace {
int stream_stop_sending(ngtcp2_conn *conn, int64_t stream_id,
uint64_t app_error_code, void *user_data,
void *stream_user_data) {
auto c = static_cast<Client *>(user_data);
if (c->quic_stream_stop_sending(stream_id, app_error_code) != 0) {
2022-01-23 15:46:19 +01:00
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
} // namespace
int Client::quic_stream_stop_sending(int64_t stream_id,
uint64_t app_error_code) {
auto s = static_cast<Http3Session *>(session.get());
if (s->shutdown_stream_read(stream_id) != 0) {
2019-06-08 15:05:25 +02:00
return -1;
}
return 0;
}
namespace {
int extend_max_local_streams_bidi(ngtcp2_conn *conn, uint64_t max_streams,
void *user_data) {
auto c = static_cast<Client *>(user_data);
if (c->quic_extend_max_local_streams() != 0) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
} // namespace
int Client::quic_extend_max_local_streams() {
auto s = static_cast<Http3Session *>(session.get());
if (s->extend_max_local_streams() != 0) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
return 0;
}
namespace {
int get_new_connection_id(ngtcp2_conn *conn, ngtcp2_cid *cid, uint8_t *token,
size_t cidlen, void *user_data) {
if (RAND_bytes(cid->data, cidlen) != 1) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
2019-06-08 15:05:25 +02:00
cid->datalen = cidlen;
if (RAND_bytes(token, NGTCP2_STATELESS_RESET_TOKENLEN) != 1) {
return NGTCP2_ERR_CALLBACK_FAILURE;
}
2019-06-08 15:05:25 +02:00
return 0;
}
} // namespace
namespace {
void debug_log_printf(void *user_data, const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
2019-07-24 16:15:15 +02:00
fprintf(stderr, "\n");
2019-06-08 15:05:25 +02:00
}
} // namespace
namespace {
int generate_cid(ngtcp2_cid &dest) {
2019-06-08 15:05:25 +02:00
dest.datalen = 8;
if (RAND_bytes(dest.data, dest.datalen) != 1) {
return -1;
}
return 0;
2019-06-08 15:05:25 +02:00
}
} // namespace
namespace {
ngtcp2_tstamp timestamp(struct ev_loop *loop) {
return ev_now(loop) * NGTCP2_SECONDS;
}
} // namespace
2021-10-15 12:52:01 +02:00
#ifdef HAVE_LIBNGTCP2_CRYPTO_OPENSSL
2019-06-08 15:05:25 +02:00
namespace {
int set_encryption_secrets(SSL *ssl, OSSL_ENCRYPTION_LEVEL ossl_level,
const uint8_t *rx_secret, const uint8_t *tx_secret,
size_t secret_len) {
auto c = static_cast<Client *>(SSL_get_app_data(ssl));
2021-10-15 12:52:01 +02:00
auto level = ngtcp2_crypto_openssl_from_ossl_encryption_level(ossl_level);
2019-06-08 15:05:25 +02:00
2021-10-15 12:52:01 +02:00
if (c->quic_on_rx_secret(level, rx_secret, secret_len) != 0) {
return 0;
}
if (c->quic_on_tx_secret(level, tx_secret, secret_len) != 0) {
return 0;
2019-06-08 15:05:25 +02:00
}
return 1;
}
} // namespace
namespace {
int add_handshake_data(SSL *ssl, OSSL_ENCRYPTION_LEVEL ossl_level,
const uint8_t *data, size_t len) {
auto c = static_cast<Client *>(SSL_get_app_data(ssl));
c->quic_write_client_handshake(
2020-12-22 08:01:36 +01:00
ngtcp2_crypto_openssl_from_ossl_encryption_level(ossl_level), data, len);
2019-06-08 15:05:25 +02:00
return 1;
}
} // namespace
namespace {
int flush_flight(SSL *ssl) { return 1; }
2019-06-08 15:05:25 +02:00
} // namespace
namespace {
int send_alert(SSL *ssl, enum ssl_encryption_level_t level, uint8_t alert) {
auto c = static_cast<Client *>(SSL_get_app_data(ssl));
c->quic_set_tls_alert(alert);
2019-06-08 15:05:25 +02:00
return 1;
}
} // namespace
namespace {
auto quic_method = SSL_QUIC_METHOD{
set_encryption_secrets,
add_handshake_data,
flush_flight,
send_alert,
};
2019-06-08 15:05:25 +02:00
} // namespace
2021-10-15 12:52:01 +02:00
#endif // HAVE_LIBNGTCP2_CRYPTO_OPENSSL
#ifdef HAVE_LIBNGTCP2_CRYPTO_BORINGSSL
namespace {
int set_read_secret(SSL *ssl, ssl_encryption_level_t ssl_level,
const SSL_CIPHER *cipher, const uint8_t *secret,
size_t secretlen) {
auto c = static_cast<Client *>(SSL_get_app_data(ssl));
if (c->quic_on_rx_secret(
ngtcp2_crypto_boringssl_from_ssl_encryption_level(ssl_level), secret,
secretlen) != 0) {
return 0;
}
return 1;
}
} // namespace
namespace {
int set_write_secret(SSL *ssl, ssl_encryption_level_t ssl_level,
const SSL_CIPHER *cipher, const uint8_t *secret,
size_t secretlen) {
auto c = static_cast<Client *>(SSL_get_app_data(ssl));
if (c->quic_on_tx_secret(
ngtcp2_crypto_boringssl_from_ssl_encryption_level(ssl_level), secret,
secretlen) != 0) {
return 0;
}
return 1;
}
} // namespace
namespace {
int add_handshake_data(SSL *ssl, ssl_encryption_level_t ssl_level,
const uint8_t *data, size_t len) {
auto c = static_cast<Client *>(SSL_get_app_data(ssl));
c->quic_write_client_handshake(
ngtcp2_crypto_boringssl_from_ssl_encryption_level(ssl_level), data, len);
return 1;
}
} // namespace
namespace {
int flush_flight(SSL *ssl) { return 1; }
} // namespace
namespace {
int send_alert(SSL *ssl, ssl_encryption_level_t level, uint8_t alert) {
auto c = static_cast<Client *>(SSL_get_app_data(ssl));
c->quic_set_tls_alert(alert);
return 1;
}
} // namespace
namespace {
auto quic_method = SSL_QUIC_METHOD{
set_read_secret, set_write_secret, add_handshake_data,
flush_flight, send_alert,
};
} // namespace
#endif // HAVE_LIBNGTCP2_CRYPTO_BORINGSSL
2019-06-08 15:05:25 +02:00
2021-03-12 20:12:46 +01:00
// qlog write callback -- excerpted from ngtcp2/examples/client_base.cc
namespace {
void qlog_write_cb(void *user_data, uint32_t flags, const void *data,
size_t datalen) {
auto c = static_cast<Client *>(user_data);
c->quic_write_qlog(data, datalen);
}
} // namespace
void Client::quic_write_qlog(const void *data, size_t datalen) {
assert(quic.qlog_file != nullptr);
fwrite(data, 1, datalen, quic.qlog_file);
}
namespace {
void rand(uint8_t *dest, size_t destlen, const ngtcp2_rand_ctx *rand_ctx) {
util::random_bytes(dest, dest + destlen,
*static_cast<std::mt19937 *>(rand_ctx->native_handle));
}
} // namespace
2019-06-08 15:05:25 +02:00
int Client::quic_init(const sockaddr *local_addr, socklen_t local_addrlen,
const sockaddr *remote_addr, socklen_t remote_addrlen) {
int rv;
if (!ssl) {
ssl = SSL_new(worker->ssl_ctx);
SSL_set_app_data(ssl, this);
SSL_set_connect_state(ssl);
SSL_set_quic_method(ssl, &quic_method);
SSL_set_quic_use_legacy_codepoint(ssl, 0);
2019-06-08 15:05:25 +02:00
}
auto callbacks = ngtcp2_callbacks{
2020-05-22 15:11:50 +02:00
ngtcp2_crypto_client_initial_cb,
2019-06-08 15:05:25 +02:00
nullptr, // recv_client_initial
ngtcp2_crypto_recv_crypto_data_cb,
2019-06-08 15:05:25 +02:00
h2load::handshake_completed,
nullptr, // recv_version_negotiation
ngtcp2_crypto_encrypt_cb,
ngtcp2_crypto_decrypt_cb,
ngtcp2_crypto_hp_mask_cb,
2019-06-08 15:05:25 +02:00
h2load::recv_stream_data,
h2load::acked_stream_data_offset,
2019-06-08 15:05:25 +02:00
nullptr, // stream_open
h2load::stream_close,
nullptr, // recv_stateless_reset
2020-05-22 15:11:50 +02:00
ngtcp2_crypto_recv_retry_cb,
2019-06-08 15:05:25 +02:00
h2load::extend_max_local_streams_bidi,
nullptr, // extend_max_local_streams_uni
h2load::rand,
2019-06-08 15:05:25 +02:00
get_new_connection_id,
nullptr, // remove_connection_id
2019-12-28 02:09:17 +01:00
ngtcp2_crypto_update_key_cb,
2019-06-08 15:05:25 +02:00
nullptr, // path_validation
2021-07-31 10:23:50 +02:00
nullptr, // select_preferred_addr
2019-06-08 15:05:25 +02:00
h2load::stream_reset,
nullptr, // extend_max_remote_streams_bidi
nullptr, // extend_max_remote_streams_uni
nullptr, // extend_max_stream_data
2020-06-28 12:10:49 +02:00
nullptr, // dcid_status
nullptr, // handshake_confirmed
nullptr, // recv_new_token
ngtcp2_crypto_delete_crypto_aead_ctx_cb,
ngtcp2_crypto_delete_crypto_cipher_ctx_cb,
nullptr, // recv_datagram
nullptr, // ack_datagram
nullptr, // lost_datagram
ngtcp2_crypto_get_path_challenge_data_cb,
h2load::stream_stop_sending,
2019-06-08 15:05:25 +02:00
};
ngtcp2_cid scid, dcid;
if (generate_cid(scid) != 0) {
return -1;
}
if (generate_cid(dcid) != 0) {
return -1;
}
2019-06-08 15:05:25 +02:00
2019-06-15 10:49:46 +02:00
auto config = worker->config;
2019-06-08 15:05:25 +02:00
ngtcp2_settings settings;
ngtcp2_settings_default(&settings);
2019-07-24 16:15:15 +02:00
if (config->verbose) {
settings.log_printf = debug_log_printf;
}
2019-06-08 15:05:25 +02:00
settings.initial_ts = timestamp(worker->loop);
settings.rand_ctx.native_handle = &worker->randgen;
2021-03-12 20:12:46 +01:00
if (!config->qlog_file_base.empty()) {
assert(quic.qlog_file == nullptr);
2021-03-13 02:08:50 +01:00
auto path = config->qlog_file_base;
path += '.';
path += util::utos(worker->id);
path += '.';
path += util::utos(id);
path += ".sqlog";
2021-03-13 02:08:50 +01:00
quic.qlog_file = fopen(path.c_str(), "w");
2021-03-12 20:12:46 +01:00
if (quic.qlog_file == nullptr) {
2021-03-13 02:08:50 +01:00
std::cerr << "Failed to open a qlog file: " << path << std::endl;
2021-03-12 20:12:46 +01:00
return -1;
}
settings.qlog.write = qlog_write_cb;
}
if (config->max_udp_payload_size) {
settings.max_udp_payload_size = config->max_udp_payload_size;
settings.no_udp_payload_size_shaping = 1;
}
2021-01-31 02:56:45 +01:00
ngtcp2_transport_params params;
ngtcp2_transport_params_default(&params);
2020-09-28 11:14:32 +02:00
auto max_stream_data =
2020-09-28 17:58:47 +02:00
std::min((1 << 26) - 1, (1 << config->window_bits) - 1);
2020-09-28 11:14:32 +02:00
params.initial_max_stream_data_bidi_local = max_stream_data;
params.initial_max_stream_data_uni = max_stream_data;
2019-10-03 03:11:37 +02:00
params.initial_max_data = (1 << config->connection_window_bits) - 1;
params.initial_max_streams_bidi = 0;
params.initial_max_streams_uni = 100;
2020-01-30 09:01:56 +01:00
params.max_idle_timeout = 30 * NGTCP2_SECONDS;
2019-06-08 15:05:25 +02:00
auto path = ngtcp2_path{
2021-11-09 07:44:06 +01:00
{
const_cast<sockaddr *>(local_addr),
local_addrlen,
},
{
const_cast<sockaddr *>(remote_addr),
remote_addrlen,
},
2019-06-08 15:05:25 +02:00
};
assert(config->npn_list.size());
uint32_t quic_version;
if (config->npn_list[0] == NGHTTP3_ALPN_H3) {
quic_version = NGTCP2_PROTO_VER_V1;
} else {
quic_version = NGTCP2_PROTO_VER_MIN;
}
rv = ngtcp2_conn_client_new(&quic.conn, &dcid, &scid, &path, quic_version,
&callbacks, &settings, &params, nullptr, this);
2019-06-08 15:05:25 +02:00
if (rv != 0) {
return -1;
}
2020-05-23 12:01:23 +02:00
ngtcp2_conn_set_tls_native_handle(quic.conn, ssl);
2019-06-08 15:05:25 +02:00
return 0;
}
2021-03-12 20:12:46 +01:00
void Client::quic_free() {
ngtcp2_conn_del(quic.conn);
if (quic.qlog_file != nullptr) {
fclose(quic.qlog_file);
quic.qlog_file = nullptr;
}
}
2019-06-08 15:05:25 +02:00
void Client::quic_close_connection() {
if (!quic.conn) {
return;
}
std::array<uint8_t, NGTCP2_MAX_UDP_PAYLOAD_SIZE> buf;
2019-06-08 15:05:25 +02:00
ngtcp2_path_storage ps;
ngtcp2_path_storage_zero(&ps);
2022-03-29 14:22:16 +02:00
auto nwrite = ngtcp2_conn_write_connection_close(
quic.conn, &ps.path, nullptr, buf.data(), buf.size(), &quic.last_error,
timestamp(worker->loop));
if (nwrite <= 0) {
2019-06-08 15:05:25 +02:00
return;
}
write_udp(reinterpret_cast<sockaddr *>(ps.path.remote.addr),
2019-12-18 06:23:05 +01:00
ps.path.remote.addrlen, buf.data(), nwrite, 0);
2019-06-08 15:05:25 +02:00
}
2021-10-15 12:52:01 +02:00
int Client::quic_on_rx_secret(ngtcp2_crypto_level level, const uint8_t *secret,
size_t secretlen) {
2020-05-23 02:29:03 +02:00
if (ngtcp2_crypto_derive_and_install_rx_key(quic.conn, nullptr, nullptr,
2021-10-15 12:52:01 +02:00
nullptr, level, secret,
secretlen) != 0) {
std::cerr << "ngtcp2_crypto_derive_and_install_rx_key() failed"
<< std::endl;
return -1;
}
2020-12-01 06:57:51 +01:00
if (level == NGTCP2_CRYPTO_LEVEL_APPLICATION) {
auto s = std::make_unique<Http3Session>(this);
if (s->init_conn() == -1) {
2019-06-08 15:05:25 +02:00
return -1;
}
session = std::move(s);
2019-06-08 15:05:25 +02:00
}
return 0;
}
2021-10-15 12:52:01 +02:00
int Client::quic_on_tx_secret(ngtcp2_crypto_level level, const uint8_t *secret,
size_t secretlen) {
if (ngtcp2_crypto_derive_and_install_tx_key(quic.conn, nullptr, nullptr,
nullptr, level, secret,
secretlen) != 0) {
std::cerr << "ngtcp2_crypto_derive_and_install_tx_key() failed"
<< std::endl;
return -1;
}
return 0;
}
2019-06-08 15:05:25 +02:00
void Client::quic_set_tls_alert(uint8_t alert) {
2022-03-29 14:22:16 +02:00
ngtcp2_connection_close_error_set_transport_error_tls_alert(
&quic.last_error, alert, nullptr, 0);
2019-06-08 15:05:25 +02:00
}
void Client::quic_write_client_handshake(ngtcp2_crypto_level level,
const uint8_t *data, size_t datalen) {
assert(level < 2);
2019-06-08 15:05:25 +02:00
2021-08-16 09:58:11 +02:00
ngtcp2_conn_submit_crypto_data(quic.conn, level, data, datalen);
2019-06-08 15:05:25 +02:00
}
void quic_pkt_timeout_cb(struct ev_loop *loop, ev_timer *w, int revents) {
auto c = static_cast<Client *>(w->data);
if (c->quic_pkt_timeout() != 0) {
c->fail();
c->worker->free_client(c);
delete c;
return;
}
}
int Client::quic_pkt_timeout() {
int rv;
auto now = timestamp(worker->loop);
2020-05-22 15:59:49 +02:00
rv = ngtcp2_conn_handle_expiry(quic.conn, now);
if (rv != 0) {
2022-03-29 14:22:16 +02:00
ngtcp2_connection_close_error_set_transport_error_liberr(&quic.last_error,
rv, nullptr, 0);
2020-05-22 15:59:49 +02:00
return -1;
2019-06-08 15:05:25 +02:00
}
return write_quic();
2019-06-08 15:05:25 +02:00
}
void Client::quic_restart_pkt_timer() {
auto expiry = ngtcp2_conn_get_expiry(quic.conn);
auto now = timestamp(worker->loop);
2019-09-07 16:27:29 +02:00
auto t = expiry > now ? static_cast<ev_tstamp>(expiry - now) / NGTCP2_SECONDS
: 1e-9;
2019-06-08 15:05:25 +02:00
quic.pkt_timer.repeat = t;
ev_timer_again(worker->loop, &quic.pkt_timer);
}
int Client::read_quic() {
2021-03-17 10:56:34 +01:00
std::array<uint8_t, 65536> buf;
2019-06-08 15:05:25 +02:00
sockaddr_union su;
socklen_t addrlen = sizeof(su);
int rv;
2019-11-20 15:40:51 +01:00
size_t pktcnt = 0;
2020-08-26 08:20:20 +02:00
ngtcp2_pkt_info pi{};
2019-06-08 15:05:25 +02:00
2019-11-20 15:40:51 +01:00
for (;;) {
auto nread =
recvfrom(fd, buf.data(), buf.size(), MSG_DONTWAIT, &su.sa, &addrlen);
if (nread == -1) {
return 0;
}
2019-06-08 15:05:25 +02:00
2019-11-20 15:40:51 +01:00
assert(quic.conn);
2019-06-08 15:05:25 +02:00
++worker->stats.udp_dgram_recv;
2019-11-20 15:40:51 +01:00
auto path = ngtcp2_path{
2021-11-09 07:44:06 +01:00
{
&local_addr.su.sa,
2022-02-02 11:19:00 +01:00
static_cast<socklen_t>(local_addr.len),
2021-11-09 07:44:06 +01:00
},
{
&su.sa,
addrlen,
},
2019-11-20 15:40:51 +01:00
};
2019-06-08 15:05:25 +02:00
2020-08-26 08:20:20 +02:00
rv = ngtcp2_conn_read_pkt(quic.conn, &path, &pi, buf.data(), nread,
2019-11-20 15:40:51 +01:00
timestamp(worker->loop));
if (rv != 0) {
std::cerr << "ngtcp2_conn_read_pkt: " << ngtcp2_strerror(rv) << std::endl;
2022-03-29 14:22:16 +02:00
if (!quic.last_error.error_code) {
ngtcp2_connection_close_error_set_transport_error_liberr(
&quic.last_error, rv, nullptr, 0);
}
2019-11-20 15:40:51 +01:00
return -1;
}
2019-06-08 15:05:25 +02:00
2021-03-18 15:07:42 +01:00
if (++pktcnt == 100) {
2019-11-20 15:40:51 +01:00
break;
}
2019-06-08 15:05:25 +02:00
}
return 0;
}
int Client::write_quic() {
int rv;
2019-11-20 15:40:51 +01:00
ev_io_stop(worker->loop, &wev);
2019-06-08 15:05:25 +02:00
if (quic.close_requested) {
return -1;
}
if (quic.tx.send_blocked) {
rv = send_blocked_packet();
if (rv != 0) {
return -1;
}
if (quic.tx.send_blocked) {
return 0;
}
}
2019-06-08 15:05:25 +02:00
std::array<nghttp3_vec, 16> vec;
2019-12-18 06:23:05 +01:00
size_t pktcnt = 0;
2022-03-29 14:22:16 +02:00
auto max_udp_payload_size = ngtcp2_conn_get_max_udp_payload_size(quic.conn);
2019-12-18 06:23:05 +01:00
size_t max_pktcnt =
#ifdef UDP_SEGMENT
worker->config->no_udp_gso
? 1
: std::min(static_cast<size_t>(10),
static_cast<size_t>(64_k / max_udp_payload_size));
2019-12-18 06:23:05 +01:00
#else // !UDP_SEGMENT
1;
#endif // !UDP_SEGMENT
uint8_t *bufpos = quic.tx.data.get();
2019-06-08 15:05:25 +02:00
ngtcp2_path_storage ps;
size_t gso_size = 0;
2019-06-08 15:05:25 +02:00
ngtcp2_path_storage_zero(&ps);
2019-06-15 07:41:25 +02:00
auto s = static_cast<Http3Session *>(session.get());
2019-06-08 15:05:25 +02:00
for (;;) {
2019-09-08 04:14:50 +02:00
int64_t stream_id = -1;
int fin = 0;
2019-06-08 15:05:25 +02:00
ssize_t sveccnt = 0;
2019-09-08 05:31:37 +02:00
if (session && ngtcp2_conn_get_max_data_left(quic.conn)) {
2019-06-08 15:05:25 +02:00
sveccnt = s->write_stream(stream_id, fin, vec.data(), vec.size());
if (sveccnt == -1) {
return -1;
}
}
2019-11-09 10:00:46 +01:00
ngtcp2_ssize ndatalen;
2019-06-08 15:05:25 +02:00
auto v = vec.data();
auto vcnt = static_cast<size_t>(sveccnt);
2020-06-07 15:49:34 +02:00
uint32_t flags = NGTCP2_WRITE_STREAM_FLAG_MORE;
if (fin) {
flags |= NGTCP2_WRITE_STREAM_FLAG_FIN;
}
2019-09-08 05:31:37 +02:00
auto nwrite = ngtcp2_conn_writev_stream(
quic.conn, &ps.path, nullptr, bufpos, max_udp_payload_size, &ndatalen,
flags, stream_id, reinterpret_cast<const ngtcp2_vec *>(v), vcnt,
2020-06-07 15:49:34 +02:00
timestamp(worker->loop));
2019-09-08 05:31:37 +02:00
if (nwrite < 0) {
switch (nwrite) {
case NGTCP2_ERR_STREAM_DATA_BLOCKED:
2020-04-04 09:28:38 +02:00
assert(ndatalen == -1);
2019-09-08 05:31:37 +02:00
if (s->block_stream(stream_id) != 0) {
return -1;
}
continue;
2019-12-18 06:23:05 +01:00
case NGTCP2_ERR_STREAM_SHUT_WR:
assert(ndatalen == -1);
if (s->shutdown_stream_write(stream_id) != 0) {
return -1;
}
continue;
2020-08-05 12:25:08 +02:00
case NGTCP2_ERR_WRITE_MORE:
assert(ndatalen >= 0);
2019-06-08 15:05:25 +02:00
if (s->add_write_offset(stream_id, ndatalen) != 0) {
return -1;
}
2019-09-08 05:31:37 +02:00
continue;
2019-06-08 15:05:25 +02:00
}
2022-03-29 14:22:16 +02:00
ngtcp2_connection_close_error_set_transport_error_liberr(
&quic.last_error, nwrite, nullptr, 0);
return -1;
} else if (ndatalen >= 0 && s->add_write_offset(stream_id, ndatalen) != 0) {
return -1;
2019-09-08 05:31:37 +02:00
}
2019-06-08 15:05:25 +02:00
2019-09-08 05:31:37 +02:00
quic_restart_pkt_timer();
2019-06-08 15:05:25 +02:00
2019-09-08 05:31:37 +02:00
if (nwrite == 0) {
if (bufpos - quic.tx.data.get()) {
auto data = quic.tx.data.get();
auto datalen = bufpos - quic.tx.data.get();
rv = write_udp(ps.path.remote.addr, ps.path.remote.addrlen, data,
datalen, gso_size);
if (rv == 1) {
on_send_blocked(ps.path.remote, data, datalen, gso_size);
signal_write();
return 0;
}
2019-12-18 06:23:05 +01:00
}
2019-06-08 15:05:25 +02:00
return 0;
}
2019-09-08 05:31:37 +02:00
2019-12-18 06:23:05 +01:00
bufpos += nwrite;
if (pktcnt == 0) {
gso_size = nwrite;
2022-04-20 14:28:23 +02:00
} else if (static_cast<size_t>(nwrite) != gso_size) {
auto data = quic.tx.data.get();
auto datalen = bufpos - quic.tx.data.get() - nwrite;
rv = write_udp(ps.path.remote.addr, ps.path.remote.addrlen, data, datalen,
gso_size);
if (rv == 1) {
on_send_blocked(ps.path.remote, data, datalen, gso_size);
on_send_blocked(ps.path.remote, bufpos - nwrite, nwrite, 0);
} else {
auto data = bufpos - nwrite;
rv = write_udp(ps.path.remote.addr, ps.path.remote.addrlen, data,
nwrite, 0);
if (rv == 1) {
on_send_blocked(ps.path.remote, data, nwrite, 0);
}
}
signal_write();
return 0;
}
2019-12-18 06:23:05 +01:00
// Assume that the path does not change.
2022-04-20 14:28:23 +02:00
if (++pktcnt == max_pktcnt) {
auto data = quic.tx.data.get();
auto datalen = bufpos - quic.tx.data.get();
rv = write_udp(ps.path.remote.addr, ps.path.remote.addrlen, data, datalen,
gso_size);
if (rv == 1) {
on_send_blocked(ps.path.remote, data, datalen, gso_size);
}
2019-12-18 06:23:05 +01:00
signal_write();
return 0;
}
2019-06-08 15:05:25 +02:00
}
}
void Client::on_send_blocked(const ngtcp2_addr &remote_addr,
const uint8_t *data, size_t datalen,
size_t gso_size) {
assert(quic.tx.num_blocked || !quic.tx.send_blocked);
assert(quic.tx.num_blocked < 2);
quic.tx.send_blocked = true;
auto &p = quic.tx.blocked[quic.tx.num_blocked++];
memcpy(&p.remote_addr.su, remote_addr.addr, remote_addr.addrlen);
p.remote_addr.len = remote_addr.addrlen;
p.data = data;
p.datalen = datalen;
p.gso_size = gso_size;
}
int Client::send_blocked_packet() {
int rv;
assert(quic.tx.send_blocked);
for (; quic.tx.num_blocked_sent < quic.tx.num_blocked;
++quic.tx.num_blocked_sent) {
auto &p = quic.tx.blocked[quic.tx.num_blocked_sent];
rv = write_udp(&p.remote_addr.su.sa, p.remote_addr.len, p.data, p.datalen,
p.gso_size);
if (rv == 1) {
signal_write();
return 0;
}
}
quic.tx.send_blocked = false;
quic.tx.num_blocked = 0;
quic.tx.num_blocked_sent = 0;
return 0;
}
2019-06-08 15:05:25 +02:00
} // namespace h2load