Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 76 additions & 1 deletion modules/http3/h3_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ void* h3_merge_server_config(apr_pool_t* p, void* base_conf, void* new_conf)
h3_server_conf* merged = apr_pcalloc(p, sizeof(h3_server_conf));
h3_server_conf* base = (h3_server_conf*)base_conf;
h3_server_conf* new = (h3_server_conf*)new_conf;


merged->h3_enabled = new->h3_enabled != H3_FLAG_UNSET ? new->h3_enabled : base->h3_enabled;
merged->h3_port = new->h3_port ? new->h3_port : base->h3_port;
merged->h3_max_concurrent_streams = new->h3_max_concurrent_streams ? new->h3_max_concurrent_streams : base->h3_max_concurrent_streams;
merged->h3_max_connections = new->h3_max_connections ? new->h3_max_connections : base->h3_max_connections;
Expand All @@ -80,6 +81,8 @@ void* h3_merge_server_config(apr_pool_t* p, void* base_conf, void* new_conf)
merged->h3_max_stream_errors = new->h3_max_stream_errors ? new->h3_max_stream_errors : base->h3_max_stream_errors;
merged->h3_qpack_capacity_set = new->h3_qpack_capacity_set ? new->h3_qpack_capacity_set : base->h3_qpack_capacity_set;
merged->h3_qpack_table_capacity = new->h3_qpack_capacity_set ? new->h3_qpack_table_capacity : base->h3_qpack_table_capacity;
merged->h3_qpack_encoder_capacity_set = new->h3_qpack_encoder_capacity_set ? new->h3_qpack_encoder_capacity_set : base->h3_qpack_encoder_capacity_set;
merged->h3_qpack_encoder_table_capacity = new->h3_qpack_encoder_capacity_set ? new->h3_qpack_encoder_table_capacity : base->h3_qpack_encoder_table_capacity;
merged->h3_qpack_blocked_set = new->h3_qpack_blocked_set ? new->h3_qpack_blocked_set : base->h3_qpack_blocked_set;
merged->h3_qpack_blocked_streams = new->h3_qpack_blocked_set ? new->h3_qpack_blocked_streams : base->h3_qpack_blocked_streams;
merged->h3_min_workers = new->h3_min_workers ? new->h3_min_workers : base->h3_min_workers;
Expand All @@ -96,6 +99,17 @@ static int mpm_query(int code)
return ap_mpm_query(code, &value) == APR_SUCCESS ? value : -1;
}

static const char* set_h3_engine(cmd_parms* cmd, void* dummy H3_UNUSED, int flag)
{
const char *err = ap_check_cmd_context(cmd, GLOBAL_ONLY);
if (err != NULL)
return err;
h3_server_conf *conf = ap_get_module_config(cmd->server->module_config, &http3_module);
CHECK(conf);
conf->h3_enabled = flag ? H3_FLAG_ON : H3_FLAG_OFF;
return NULL;
}

static const char* set_h3_port(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg)
{
if (!arg || !*arg)
Expand Down Expand Up @@ -318,6 +332,42 @@ static const char* set_h3_qpack_table_capacity(cmd_parms* cmd, void* dummy H3_UN
return NULL;
}

static const char* set_h3_qpack_encoder_table_capacity(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg)
{
if (!arg || !*arg)
{
return "H3QpackEncoderTableCapacity: empty value";
}
h3_server_conf* conf = ap_get_module_config(cmd->server->module_config, &http3_module);
CHECK(conf);

if (strcmp(arg, "-1") == 0 || strcasecmp(arg, "auto") == 0)
{
conf->h3_qpack_encoder_table_capacity = -1;
conf->h3_qpack_encoder_capacity_set = 1;
return NULL;
}

apr_uint64_t val = 0;
apr_status_t rv = apr_cstr_atoui64(&val, arg);
if (rv == APR_EINVAL)
{
return apr_psprintf(cmd->pool, "H3QpackEncoderTableCapacity: '%s' is not a number", arg);
}
if (rv == APR_ERANGE)
{
return apr_psprintf(cmd->pool, "H3QpackEncoderTableCapacity: '%s' is out of representable range", arg);
}
if (val > H3_QPACK_TABLE_CAPACITY_MAX)
{
return apr_psprintf(cmd->pool, "H3QpackEncoderTableCapacity: '%s' is out of allowed range (0-%lu)", arg, (unsigned long)H3_QPACK_TABLE_CAPACITY_MAX);
}

conf->h3_qpack_encoder_table_capacity = (apr_int32_t)val;
conf->h3_qpack_encoder_capacity_set = 1;
return NULL;
}

static const char* set_h3_qpack_blocked_streams(cmd_parms* cmd, void* dummy H3_UNUSED, const char* arg)
{
if (!arg || !*arg)
Expand Down Expand Up @@ -646,8 +696,22 @@ static int h3_sni_select_cert(SSL* ssl, void* arg)
return 1; /* no match: the listener's own certificate */
}

int h3_is_enabled(const server_rec *s)
{
server_rec *base = s ? s : ap_server_conf;
h3_server_conf *conf = ap_get_module_config(base->module_config, &http3_module);

return conf ? conf->h3_enabled != H3_FLAG_OFF : 1;
}

int h3_post_config(apr_pool_t* p, apr_pool_t* plog H3_UNUSED, apr_pool_t* ptemp, server_rec* s)
{
if (!h3_is_enabled(s)) {
ap_log_error(APLOG_MARK, APLOG_INFO, 0, s,
"mod_http3: inhibited globally via H3Engine Off");
return OK;
}

CHECK(p);
CHECK(ptemp);
CHECK(s);
Expand Down Expand Up @@ -687,6 +751,10 @@ int h3_post_config(apr_pool_t* p, apr_pool_t* plog H3_UNUSED, apr_pool_t* ptemp,
e->ctx = vc->ssl_ctx;
}
vc->host_port = get_server_port(vs);
if (vc->h3_enabled == H3_FLAG_UNSET)
{
vc->h3_enabled = H3_FLAG_ON;
}
if (vc->h3_port == 0)
{
vc->h3_port = vc->host_port;
Expand Down Expand Up @@ -719,6 +787,10 @@ int h3_post_config(apr_pool_t* p, apr_pool_t* plog H3_UNUSED, apr_pool_t* ptemp,
{
vc->h3_qpack_table_capacity = H3_QPACK_TABLE_CAPACITY_DEFAULT;
}
if (!vc->h3_qpack_encoder_capacity_set)
{
vc->h3_qpack_encoder_table_capacity = 0;
}
if (!vc->h3_qpack_blocked_set)
{
vc->h3_qpack_blocked_streams = H3_QPACK_BLOCKED_STREAMS_DEFAULT;
Expand Down Expand Up @@ -811,6 +883,8 @@ void* h3_merge_dir_config(apr_pool_t* p H3_UNUSED, void* base, void* add H3_UNUS
}

const command_rec h3_cmds[] = {
AP_INIT_FLAG("H3Engine", set_h3_engine, NULL, RSRC_CONF,
"Enable or inhibit mod_http3 globally (on|off)"),
AP_INIT_TAKE1("H3Port", set_h3_port, NULL, RSRC_CONF, "UDP port to listen on for QUIC/HTTP-3 (default: same as main server)"),
AP_INIT_TAKE1("H3MaxConcurrentStreams", set_h3_max_concurrent_streams, NULL, RSRC_CONF, "Maximum number of concurrent HTTP/3 streams per connection (default: 100)"),
AP_INIT_TAKE1("H3MaxConnections", set_h3_max_connections, NULL, RSRC_CONF, "Maximum concurrent QUIC/HTTP/3 connections per child process (default: 256)"),
Expand All @@ -829,6 +903,7 @@ const command_rec h3_cmds[] = {
AP_INIT_TAKE1("H3StreamTimeout", set_h3_stream_timeout, NULL, RSRC_CONF, "Seconds a response may make no progress before the stream is aborted (default: the server Timeout)"),
AP_INIT_TAKE1("H3MaxStreamErrors", set_h3_max_stream_errors, NULL, RSRC_CONF, "Stream errors one connection may cause before it is closed with H3_EXCESSIVE_LOAD (default: 8)"),
AP_INIT_TAKE1("H3QpackTableCapacity", set_h3_qpack_table_capacity, NULL, RSRC_CONF, "Bytes of QPACK dynamic table a client may use for request headers; 0 disables it (default: 4096)"),
AP_INIT_TAKE1("H3QpackEncoderTableCapacity", set_h3_qpack_encoder_table_capacity, NULL, RSRC_CONF, "Maximum QPACK dynamic table capacity used by the server encoder, -1 don't set a value : keep nghttp3 default settings (default: 0)"),
AP_INIT_TAKE1("H3QpackBlockedStreams", set_h3_qpack_blocked_streams, NULL, RSRC_CONF, "Requests that may wait on a QPACK table insert that has not arrived; each one is unbounded memory (default: 0)"),
AP_INIT_TAKE1("H3MinWorkers", set_h3_min_workers, NULL, RSRC_CONF, "Request worker threads started per child process (default: 16)"),
AP_INIT_TAKE1("H3MaxWorkers", set_h3_max_workers, NULL, RSRC_CONF, "Maximum request worker threads per child process (default: 64)"),
Expand Down
54 changes: 33 additions & 21 deletions modules/http3/h3_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static void teardown(h3_io_t* io)
CHECK(io);
if (io->event_thread)
{
io->thread_running = 0;
apr_atomic_set32(&io->thread_running,0);
h3_wakeup_signal_always(&io->wakeup);
apr_status_t status;
apr_thread_join(&status, io->event_thread);
Expand Down Expand Up @@ -109,7 +109,7 @@ static void teardown(h3_io_t* io)
static apr_status_t listen_stop_cleanup(void* data)
{
h3_io_t* io = data;
if (io && io->draining)
if (io && apr_atomic_read32(&io->draining))
{
h3_io_listen_stop(io);
}
Expand Down Expand Up @@ -172,15 +172,15 @@ apr_status_t h3_io_listen_start(apr_pool_t* pchild, server_rec* s, h3_server_con
ap_log_error(APLOG_MARK, APLOG_WARNING, 0, s, "active MPM '%s' does not report externally accepted connections; graceful child shutdown may end active HTTP/3 connections early (an MPM with ap_mpm_note_extra_connection_added/_removed avoids this)", ap_show_mpm());
}

io->thread_running = 1;
apr_atomic_set32(&io->thread_running,1);
child_h3_io = io;
/* The event thread allocates from pchild too, so allocate everything
* from it here before the thread starts. */
apr_threadattr_t* attr = NULL;
apr_pool_pre_cleanup_register(pchild, io, listen_stop_cleanup);
if (apr_threadattr_create(&attr, pchild) != APR_SUCCESS || apr_thread_create(&io->event_thread, attr, h3_event_thread, io, pchild) != APR_SUCCESS)
{
io->thread_running = 0;
apr_atomic_set32(&io->thread_running,0);
teardown(io);
child_h3_io = NULL;
return APR_EGENERAL;
Expand All @@ -192,9 +192,9 @@ apr_status_t h3_io_listen_start(apr_pool_t* pchild, server_rec* s, h3_server_con

void h3_io_listen_drain(h3_io_t* io)
{
if (io && !io->draining)
if (io && !apr_atomic_read32(&io->draining))
{
io->draining = 1;
apr_atomic_set32(&io->draining,1);
ap_log_error(APLOG_MARK, APLOG_INFO, 0, io->server, "graceful stop: draining %d connection(s), the MPM waits for them to finish", io->active_sessions ? io->active_sessions->nelts : 0);
h3_wakeup_signal_always(&io->wakeup);
}
Expand Down Expand Up @@ -413,14 +413,15 @@ static int h3_method_is_safe(const char* method)
return m == M_GET || m == M_OPTIONS || m == M_TRACE;
}

static void abandon_stalled_responses(h3_session* session, apr_interval_time_t stall_timeout)
static void abandon_stalled_responses(h3_session* session, apr_pool_t* loop_pool, apr_interval_time_t stall_timeout)
{
if (stall_timeout <= 0)
if (!session || !loop_pool || stall_timeout <= 0)
{
return;
}
apr_time_t now = apr_time_now();
for (apr_hash_index_t* hi = apr_hash_first(NULL, session->streams); hi; hi = apr_hash_next(hi))
apr_array_header_t* stalled = apr_array_make(loop_pool, 4, sizeof(h3_stream*));
for (apr_hash_index_t* hi = apr_hash_first(loop_pool, session->streams); hi; hi = apr_hash_next(hi))
{
h3_stream* h3s = apr_hash_this_val(hi);
if (!h3s || h3s->response_cancelled || h3s->response_complete)
Expand All @@ -435,6 +436,17 @@ static void abandon_stalled_responses(h3_session* session, apr_interval_time_t s
{
continue;
}
*(h3_stream**)apr_array_push(stalled) = h3s;
}

for (int i = 0; i < stalled->nelts; i++)
{
h3_stream* h3s = ((h3_stream**)stalled->elts)[i];
if (!h3s || h3s->response_cancelled || h3s->response_complete)
{
continue;
}

ap_log_error(APLOG_MARK, APLOG_INFO, 0, session->s, "HTTP/3 stream %" APR_INT64_T_FMT " made no progress for %" APR_TIME_T_FMT " seconds; abandoning the response", (apr_int64_t)h3s->stream_id, apr_time_sec(stall_timeout));
h3_stream_response_cancel_locked(h3s);
}
Expand All @@ -447,20 +459,20 @@ int service_session_pass(h3_io_t* io, h3_session* session)
server_rec* s = session->s;
h3q_conn* conn = session->qconn;

if (session->aborted)
if (apr_atomic_read32(&session->aborted))
{
return 0;
}

if (io->draining || !io->thread_running)
if (apr_atomic_read32(&io->draining) || !apr_atomic_read32(&io->thread_running))
{
int drained;
apr_thread_mutex_lock(session->lock);
if (!session->goaway_sent)
{
nghttp3_conn_submit_shutdown_notice(session->ngh3);
}
if (!io->thread_running && !session->final_goaway_sent)
if (!apr_atomic_read32(&io->thread_running) && !session->final_goaway_sent)
{
nghttp3_conn_shutdown(session->ngh3);
session->final_goaway_sent = 1;
Expand All @@ -473,7 +485,7 @@ int service_session_pass(h3_io_t* io, h3_session* session)
session->goaway_sent = 1;
ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, "sent HTTP/3 GOAWAY; letting in-flight streams finish");
}
if ((!io->thread_running || io->draining) && !session->goaway_deadline)
if ((!apr_atomic_read32(&io->thread_running) || apr_atomic_read32(&io->draining)) && !session->goaway_deadline)
{
session->goaway_deadline = apr_time_now() + apr_time_from_sec(H3_GOAWAY_GRACE_SECS);
ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, "stopping: allowing up to %d more second(s) for in-flight streams", H3_GOAWAY_GRACE_SECS);
Expand All @@ -488,7 +500,7 @@ int service_session_pass(h3_io_t* io, h3_session* session)
}
flush_nghttp3(session);
apr_thread_mutex_unlock(session->lock);
session->aborted = 1;
apr_atomic_set32(&session->aborted,1);
return 0;
}
}
Expand All @@ -498,13 +510,13 @@ int service_session_pass(h3_io_t* io, h3_session* session)
char detail[H3Q_ERRLEN] = {0};
h3q_conn_close_reason(conn, detail, sizeof(detail));
ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, "QUIC connection terminated (idle timeout, peer close, or transport error) close=[%s]", detail);
session->aborted = 1;
apr_atomic_set32(&session->aborted,1);
return 0;
}

/* ngtcp2 gave the peer more streams: nghttp3 checks PRIORITY_UPDATE against its own copy of the limit. */
uint64_t max_bidi = h3q_conn_max_remote_streams_bidi(conn);
if (max_bidi > session->max_client_streams_bidi && session->ngh3 && !session->ngh3_dead)
if (max_bidi > session->max_client_streams_bidi && session->ngh3 && !apr_atomic_read32(&session->ngh3_dead))
{
apr_thread_mutex_lock(session->lock);
nghttp3_conn_set_max_client_streams_bidi(session->ngh3, max_bidi);
Expand Down Expand Up @@ -545,9 +557,9 @@ int service_session_pass(h3_io_t* io, h3_session* session)
flush_nghttp3(session);
apr_thread_mutex_unlock(session->lock);

if (session->ngh3_dead)
if (apr_atomic_read32(&session->ngh3_dead))
{
session->aborted = 1;
apr_atomic_set32(&session->aborted,1);
apr_pool_destroy(scratch);
return 0;
}
Expand All @@ -567,7 +579,7 @@ int service_session_pass(h3_io_t* io, h3_session* session)
h3_server_conf* sconf = ap_get_module_config(s->module_config, &http3_module);
apr_thread_mutex_lock(session->lock);
flush_nghttp3(session);
abandon_stalled_responses(session, (sconf && sconf->h3_stream_timeout) ? apr_time_from_sec(sconf->h3_stream_timeout) : s->timeout);
abandon_stalled_responses(session, scratch, (sconf && sconf->h3_stream_timeout) ? apr_time_from_sec(sconf->h3_stream_timeout) : s->timeout);
apr_thread_mutex_unlock(session->lock);
apr_pool_destroy(scratch);

Expand All @@ -579,13 +591,13 @@ int service_session_pass(h3_io_t* io, h3_session* session)
{
session->last_activity = apr_time_now();
}
else if (io->thread_running && apr_atomic_read32(&session->active_tasks) == 0)
else if (apr_atomic_read32(&io->thread_running) && apr_atomic_read32(&session->active_tasks) == 0)
{
h3_server_conf* conf = ap_get_module_config(s->module_config, &http3_module);
if (conf && apr_time_now() - session->last_activity >= apr_time_from_sec(conf->h3_idle_timeout))
{
ap_log_error(APLOG_MARK, APLOG_INFO, 0, s, "closing HTTP/3 connection idle for %u second(s)", (unsigned)conf->h3_idle_timeout);
session->aborted = 1;
apr_atomic_set32(&session->aborted,1);
}
}
return data_read;
Expand Down
15 changes: 8 additions & 7 deletions modules/http3/h3_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,13 +202,15 @@ static apr_status_t submit_response_nva(h3_stream* h3s, nghttp3_nv* nva, size_t
apr_thread_mutex_unlock(session->lock);
return APR_SUCCESS;
}
if (h3s->response_cancelled || session->aborted || session->ngh3_dead || !session->ngh3)
if (h3s->response_cancelled || apr_atomic_read32(&session->aborted) || apr_atomic_read32(&session->ngh3_dead) || !session->ngh3)
{
apr_thread_mutex_unlock(session->lock);
return APR_ECONNABORTED;
}
nghttp3_data_reader dr = {.read_data = h3_session_read_data};
int rv = nghttp3_conn_submit_response(session->ngh3, h3s->stream_id, nva, nvlen, &dr);
int64_t sid = h3s->stream_id;

int rv = nghttp3_conn_submit_response(session->ngh3, sid, nva, nvlen, &dr);
if (rv == 0)
{
h3s->response_submitted = 1;
Expand All @@ -220,7 +222,7 @@ static apr_status_t submit_response_nva(h3_stream* h3s, nghttp3_nv* nva, size_t
apr_thread_mutex_unlock(session->lock);
if (rv != 0)
{
ap_log_error(APLOG_MARK, APLOG_ERR, 0, session->s, "nghttp3_conn_submit_response failed for stream %" APR_INT64_T_FMT ": %d", (apr_int64_t)h3s->stream_id, rv);
ap_log_error(APLOG_MARK, APLOG_ERR, 0, session->s, "nghttp3_conn_submit_response failed for stream %" APR_INT64_T_FMT ": %d", (apr_int64_t)sid, rv);
return APR_EGENERAL;
}
h3_session_wake(session);
Expand All @@ -238,7 +240,7 @@ apr_status_t h3_response_start(request_rec* r, h3_conn_ctx_t* h3ctx)
h3_response_finalize(r, h3ctx);
#endif
nghttp3_nv nva[64] = {0};
size_t nvlen = build_response_nva(nva, OSSL_NELEM(nva), r, h3ctx, h3s->pool);
size_t nvlen = build_response_nva(nva, OSSL_NELEM(nva), r, h3ctx, r->pool);
return submit_response_nva(h3s, nva, nvlen);
}

Expand Down Expand Up @@ -379,7 +381,7 @@ static void* APR_THREAD_FUNC stream_worker(apr_thread_t* thd, void* data)
/* The partial body and its Content-Length no longer agree; send a clean error. */
static const char oversized_msg[] = "Response exceeded H3MaxResponseBodySize\n";
status = HTTP_INTERNAL_SERVER_ERROR;
char* status_str = apr_psprintf(h3s->pool, "%d", status);
char* status_str = apr_psprintf(r->pool, "%d", status);
nghttp3_nv nva[2] = {0};
NV_SET(nva, 0, ":status", status_str);
NV_SET(nva, 1, "content-type", "text/plain");
Expand All @@ -406,9 +408,8 @@ static void* APR_THREAD_FUNC stream_worker(apr_thread_t* thd, void* data)
{
response_rv = h3_stream_response_append(h3s, buffered_body, buffered_body_len);
}
h3_stream_response_complete(h3s);

size_t body_len = h3s->response_len;
const size_t body_len = h3_stream_response_complete(h3s);
int64_t sid = h3s->stream_id;
if (response_rv != APR_SUCCESS && response_rv != APR_ECONNABORTED)
{
Expand Down
Loading