diff --git a/modules/http3/h3_config.c b/modules/http3/h3_config.c index 2cd85a82730..c6f9fc83867 100644 --- a/modules/http3/h3_config.c +++ b/modules/http3/h3_config.c @@ -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; @@ -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; @@ -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) @@ -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) @@ -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); @@ -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; @@ -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; @@ -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)"), @@ -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)"), diff --git a/modules/http3/h3_io.c b/modules/http3/h3_io.c index 5f2ca30b116..087e46e03ad 100644 --- a/modules/http3/h3_io.c +++ b/modules/http3/h3_io.c @@ -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); @@ -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); } @@ -172,7 +172,7 @@ 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. */ @@ -180,7 +180,7 @@ apr_status_t h3_io_listen_start(apr_pool_t* pchild, server_rec* s, h3_server_con 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; @@ -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); } @@ -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) @@ -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); } @@ -447,12 +459,12 @@ 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); @@ -460,7 +472,7 @@ int service_session_pass(h3_io_t* io, h3_session* session) { 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; @@ -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); @@ -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; } } @@ -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); @@ -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; } @@ -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); @@ -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; diff --git a/modules/http3/h3_request.c b/modules/http3/h3_request.c index babb3b51c75..99a25a187aa 100644 --- a/modules/http3/h3_request.c +++ b/modules/http3/h3_request.c @@ -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; @@ -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); @@ -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); } @@ -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"); @@ -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) { diff --git a/modules/http3/h3_session.c b/modules/http3/h3_session.c index 999f6407d7d..7a6cedaaaa3 100644 --- a/modules/http3/h3_session.c +++ b/modules/http3/h3_session.c @@ -90,7 +90,9 @@ apr_status_t h3_session_create(h3_session** psession, server_rec* s, h3q_conn* q CHECK(conf, return APR_EGENERAL;); settings.qpack_max_dtable_capacity = conf->h3_qpack_table_capacity; settings.qpack_blocked_streams = conf->h3_qpack_blocked_streams; - + if(conf->h3_qpack_encoder_table_capacity > -1) + settings.qpack_encoder_max_dtable_capacity = (size_t) conf->h3_qpack_encoder_table_capacity; + if (s->limit_req_fields > 0 && s->limit_req_fieldsize > 0) { settings.max_field_section_size = (uint64_t)s->limit_req_fields * ((uint64_t)s->limit_req_fieldsize + 32); @@ -174,7 +176,7 @@ apr_status_t h3_session_create_control_streams(h3_session* session) } track_stream(session, cs[0].id, cs[0].st); - track_stream(session, cs[1].id, cs[1].st); + session->qpack_encoder_stream = track_stream(session, cs[1].id, cs[1].st); track_stream(session, cs[2].id, cs[2].st); session->control_streams_created = 1; return APR_SUCCESS; @@ -192,7 +194,7 @@ void h3_session_destroy(h3_session* session) nghttp3_conn_del(session->ngh3); session->ngh3 = NULL; } - for (apr_hash_index_t* hi = apr_hash_first(NULL, session->streams); hi; hi = apr_hash_next(hi)) + for (apr_hash_index_t* hi = apr_hash_first(session->pool, session->streams); hi; hi = apr_hash_next(hi)) { h3_stream* stream = apr_hash_this_val(hi); if (stream) @@ -240,7 +242,7 @@ apr_status_t h3_stream_response_append(h3_stream* stream, const uint8_t* data, s while (offset < len) { apr_thread_mutex_lock(session->lock); - while (stream->response_buffered >= stream->response_buffer_limit && !stream->response_cancelled && !session->aborted && !session->ngh3_dead) + while (stream->response_buffered >= stream->response_buffer_limit && !stream->response_cancelled && !apr_atomic_read32(&session->aborted) && !apr_atomic_read32(&session->ngh3_dead)) { apr_status_t rv = apr_thread_cond_timedwait(stream->response_cond, session->lock, apr_time_from_msec(100)); if (rv != APR_SUCCESS && !APR_STATUS_IS_TIMEUP(rv)) @@ -249,7 +251,7 @@ apr_status_t h3_stream_response_append(h3_stream* stream, const uint8_t* data, s return rv; } } - if (stream->response_cancelled || session->aborted || session->ngh3_dead || !session->ngh3) + if (stream->response_cancelled || apr_atomic_read32(&session->aborted) || apr_atomic_read32(&session->ngh3_dead) || !session->ngh3) { apr_thread_mutex_unlock(session->lock); return APR_ECONNABORTED; @@ -311,22 +313,27 @@ apr_status_t h3_stream_response_append(h3_stream* stream, const uint8_t* data, s return APR_SUCCESS; } -void h3_stream_response_complete(h3_stream* stream) +size_t h3_stream_response_complete(h3_stream* stream) { if (!stream) { - return; + return 0; } h3_session* session = stream->session; + size_t resplen = 0; apr_thread_mutex_lock(session->lock); stream->response_complete = 1; - if (stream->response_submitted && session->ngh3 && !session->ngh3_dead) + if (stream->response_submitted && session->ngh3 && !apr_atomic_read32(&session->ngh3_dead)) { (void)nghttp3_conn_resume_stream(session->ngh3, stream->stream_id); } + + resplen = stream->response_len; apr_thread_cond_broadcast(stream->response_cond); apr_thread_mutex_unlock(session->lock); h3_session_wake(session); + + return resplen; } void h3_stream_response_ack_locked(h3_stream* stream, uint64_t datalen) diff --git a/modules/http3/h3_stream.c b/modules/http3/h3_stream.c index 131511e35c9..6ac6d7954f4 100644 --- a/modules/http3/h3_stream.c +++ b/modules/http3/h3_stream.c @@ -52,11 +52,20 @@ static void mark_ngh3_dead(h3_session* session, const char* op, int64_t stream_i /* Re-enable write-blocked streams once their QUIC send buffer has room. */ static void unblock_writable_streams(h3_session* session) { - if (session->blocked_streams <= 0) + if (!session || session->blocked_streams <= 0 || !session->ngh3) { return; } - for (apr_hash_index_t* hi = apr_hash_first(NULL, session->streams); hi; hi = apr_hash_next(hi)) + + apr_pool_t* tmp_pool = NULL; + if (apr_pool_create(&tmp_pool, session->pool) != APR_SUCCESS) + { + return; + } + + apr_array_header_t* unblocked = apr_array_make(tmp_pool, 4, sizeof(h3_stream*)); + + for (apr_hash_index_t* hi = apr_hash_first(tmp_pool, session->streams); hi; hi = apr_hash_next(hi)) { h3_stream* h3s = apr_hash_this_val(hi); if (!h3s || !h3s->write_blocked) @@ -67,17 +76,35 @@ static void unblock_writable_streams(h3_session* session) { continue; /* still full */ } + *(h3_stream**)apr_array_push(unblocked) = h3s; + } + + for (int i = 0; i < unblocked->nelts; i++) + { + h3_stream* h3s = ((h3_stream**)unblocked->elts)[i]; + if (!h3s || !h3s->write_blocked || !session->ngh3) + { + continue; + } h3s->write_blocked = 0; - session->blocked_streams--; + session->blocked_streams--; nghttp3_conn_unblock_stream(session->ngh3, h3s->stream_id); } + + apr_pool_destroy(tmp_pool); } void flush_nghttp3(h3_session* session) { CHECK(session); - CHECK(!session->ngh3_dead, return;); + CHECK(!apr_atomic_read32(&session->ngh3_dead), return;); unblock_writable_streams(session); + + if (session->qpack_encoder_stream && session->qpack_encoder_stream->write_blocked) + { + goto drain_pending_free; + } + for (;;) { nghttp3_vec vec[16] = {0}; @@ -137,7 +164,7 @@ void flush_nghttp3(h3_session* session) session->blocked_streams++; } nghttp3_conn_block_stream(session->ngh3, sid); - if (res.conn_blocked) + if (res.conn_blocked || h3s == session->qpack_encoder_stream) { /* A stream written later in this pass could overtake this one, * e.g. HEADERS overtaking the QPACK inserts they reference. */ @@ -146,6 +173,8 @@ void flush_nghttp3(h3_session* session) continue; } } + + drain_pending_free: if (session->pending_free->nelts > 0) { while (session->pending_free->nelts > 0) @@ -191,10 +220,17 @@ h3_stream* track_stream(h3_session* session, int64_t sid, h3q_stream* qstream) static void mark_ngh3_dead(h3_session* session, const char* op, int64_t stream_id, nghttp3_ssize liberr) { - session->ngh3_dead = 1; - session->abort_quic_error_code = nghttp3_err_infer_quic_app_error_code((int)liberr); - session->abort_reason = nghttp3_strerror((int)liberr); - ap_log_error(APLOG_MARK, APLOG_ERR, 0, session->s, "%s failed for stream %" APR_INT64_T_FMT " (%s, err=%" APR_INT64_T_FMT "); closing with QUIC error 0x%" APR_UINT64_T_HEX_FMT, op, (apr_int64_t)stream_id, session->abort_reason, (apr_int64_t)liberr, (apr_uint64_t)session->abort_quic_error_code); + uint64_t code = nghttp3_err_infer_quic_app_error_code((int)liberr); + const char* reason = nghttp3_strerror((int)liberr); + + if (apr_atomic_cas32(&session->ngh3_dead, 1, 0) == 0) + { + session->abort_quic_error_code = code; + session->abort_reason = reason; + apr_atomic_set32(&session->aborted, 1); + } + + ap_log_error(APLOG_MARK, APLOG_ERR, 0, session->s, "%s failed for stream %" APR_INT64_T_FMT " (%s, err=%" APR_INT64_T_FMT "); closing with QUIC error 0x%" APR_UINT64_T_HEX_FMT, op, (apr_int64_t)stream_id, reason, (apr_int64_t)liberr, (apr_uint64_t)code); } /* nghttp3 reports an RFC 9114 4.x header violation as one of these non-fatal read errors. */ @@ -237,7 +273,7 @@ static void reject_malformed_stream(h3_session* session, h3_stream* h3s, nghttp3 ap_log_error(APLOG_MARK, APLOG_WARNING, 0, session->s, "closing HTTP/3 connection after %u client-caused stream errors (H3MaxStreamErrors %u)", (unsigned)session->stream_errors, (unsigned)limit); session->abort_quic_error_code = NGHTTP3_H3_EXCESSIVE_LOAD; session->abort_reason = "too many malformed requests"; - session->ngh3_dead = 1; + apr_atomic_set32(&session->ngh3_dead,1); } } @@ -401,12 +437,12 @@ apr_array_header_t* drain_ready_streams(h3_session* session, apr_pool_t* loop_po unsigned int total_streams = apr_hash_count(session->streams); unsigned int iterations = 0; apr_array_header_t* snapshot = apr_array_make(loop_pool, 8, sizeof(h3_stream*)); - for (apr_hash_index_t* hi = apr_hash_first(NULL, session->streams); hi; hi = apr_hash_next(hi)) + for (apr_hash_index_t* hi = apr_hash_first(loop_pool, session->streams); hi; hi = apr_hash_next(hi)) { if (++iterations > total_streams + 100) { ap_log_error(APLOG_MARK, APLOG_ERR, 0, session->s, "hash iteration did not terminate after %u entries (hash reports %u) - hash corruption, aborting connection", iterations, total_streams); - session->aborted = 1; + apr_atomic_set32(&session->aborted,1); break; } h3_stream* h3s = apr_hash_this_val(hi); @@ -421,7 +457,7 @@ apr_array_header_t* drain_ready_streams(h3_session* session, apr_pool_t* loop_po size_t start = snapshot->nelts > 0 ? session->stream_drain_cursor % (size_t)snapshot->nelts : 0; for (int offset = 0; offset < snapshot->nelts; offset++) { - if (session->ngh3_dead) + if (apr_atomic_read32(&session->ngh3_dead)) { break; } @@ -436,7 +472,7 @@ apr_array_header_t* drain_ready_streams(h3_session* session, apr_pool_t* loop_po { continue; } - if (drain_one_stream(session, h3s, data_read, &reads_remaining, &bytes_remaining) && !session->ngh3_dead) + if (drain_one_stream(session, h3s, data_read, &reads_remaining, &bytes_remaining) && !apr_atomic_read32(&session->ngh3_dead)) { h3_stream** slot = (h3_stream**)apr_array_push(completed); *slot = h3s; @@ -497,7 +533,7 @@ void h3_stream_acked(void* user, int64_t stream_id, size_t len) { h3_session* session = user; apr_thread_mutex_lock(session->lock); - if (!session->ngh3_dead) + if (!apr_atomic_read32(&session->ngh3_dead)) { (void)nghttp3_conn_add_ack_offset(session->ngh3, stream_id, len); } diff --git a/modules/http3/h3_threads.c b/modules/http3/h3_threads.c index ec6c4576987..06297f126e4 100644 --- a/modules/http3/h3_threads.c +++ b/modules/http3/h3_threads.c @@ -73,7 +73,7 @@ void* APR_THREAD_FUNC h3_event_thread(apr_thread_t* thread, void* data) ap_log_error(APLOG_MARK, APLOG_INFO, 0, io->server, "event thread started"); int work_pending = 0; int listener_failed = 0; - while (io->thread_running || io->active_sessions->nelts > 0) + while (apr_atomic_read32(&io->thread_running) || io->active_sessions->nelts > 0) { if (!work_pending) { @@ -103,7 +103,7 @@ void* APR_THREAD_FUNC h3_event_thread(apr_thread_t* thread, void* data) } while (pumped > 0 && ++pump_rounds < H3_PUMP_BUDGET); } - if (io->thread_running) + if (apr_atomic_read32(&io->thread_running)) { for (;;) { @@ -113,7 +113,7 @@ void* APR_THREAD_FUNC h3_event_thread(apr_thread_t* thread, void* data) break; } /* The engine has already answered these clients: refuse them, or they wait out their idle timeout. */ - if (io->draining) + if (apr_atomic_read32(&io->draining)) { h3q_conn_refuse(io->qengine, conn); /* no new connections during a graceful stop */ continue; @@ -132,7 +132,7 @@ void* APR_THREAD_FUNC h3_event_thread(apr_thread_t* thread, void* data) } } } - if (io->thread_running) + if (apr_atomic_read32(&io->thread_running)) { /* Also while draining: a pending handshake counts as an MPM connection, so it must finish or time out. */ progress_pending_handshakes(io); @@ -156,7 +156,7 @@ void* APR_THREAD_FUNC h3_event_thread(apr_thread_t* thread, void* data) * stream limit change in the engine, no worker output. The sweep still * serves it once a second, for its idle and stream timeouts. Every session * keeps its control streams, so a stream count cannot tell idle sessions. */ - if (!io->draining && io->thread_running && !session->aborted + if (!apr_atomic_read32(&io->draining) && apr_atomic_read32(&io->thread_running) && !apr_atomic_read32(&session->aborted) && apr_atomic_read32(&session->active_tasks) == 0 && !h3q_conn_take_activity(session->qconn) && !apr_atomic_xchg32(&session->wake, 0) @@ -167,16 +167,16 @@ void* APR_THREAD_FUNC h3_event_thread(apr_thread_t* thread, void* data) } session->next_sweep = now + apr_time_from_sec(1); - if (!session->aborted && service_session_pass(io, session)) + if (!apr_atomic_read32(&session->aborted) && service_session_pass(io, session)) { work_pending = 1; apr_atomic_set32(&session->wake, 1); /* the pass ran out of budget: serve it again next round */ } - if (session->aborted) + if (apr_atomic_read32(&session->aborted)) { - int is_rapid = (!io->thread_running || io->draining); - int shutdown_done = h3q_conn_shutdown(session->qconn, is_rapid, session->abort_quic_error_code, session->ngh3_dead ? session->abort_reason : NULL); + int is_rapid = (!apr_atomic_read32(&io->thread_running) || apr_atomic_read32(&io->draining)); + int shutdown_done = h3q_conn_shutdown(session->qconn, is_rapid, session->abort_quic_error_code, apr_atomic_read32(&session->ngh3_dead) ? session->abort_reason : NULL); if (shutdown_done && apr_atomic_read32(&session->active_tasks) == 0) { diff --git a/modules/http3/include/h3_config.h b/modules/http3/include/h3_config.h index ddbeea0e009..20cf806d617 100644 --- a/modules/http3/include/h3_config.h +++ b/modules/http3/include/h3_config.h @@ -39,6 +39,7 @@ struct h3_server_conf /** QUIC TLS context, built from mod_ssl's certificate when the host lists h3 in Protocols. */ struct ssl_ctx_st* ssl_ctx; + h3_tri_flag h3_enabled; apr_port_t h3_port; apr_uint32_t h3_max_concurrent_streams; apr_uint32_t h3_max_connections; @@ -57,8 +58,10 @@ struct h3_server_conf apr_uint32_t h3_stream_timeout; apr_uint32_t h3_max_stream_errors; int h3_qpack_capacity_set; + int h3_qpack_encoder_capacity_set; int h3_qpack_blocked_set; apr_uint32_t h3_qpack_table_capacity; + apr_int32_t h3_qpack_encoder_table_capacity; apr_uint32_t h3_qpack_blocked_streams; apr_uint32_t h3_min_workers; apr_uint32_t h3_max_workers; @@ -137,4 +140,12 @@ int h3_ssl_add_cert_files(server_rec* s, apr_pool_t* p, apr_array_header_t* cert */ int h3_post_config(apr_pool_t* p, apr_pool_t* plog, apr_pool_t* ptemp, server_rec* s); +/** + * Test if http3 is enabled. + * @param s The server_rec. + * @return 1 if HTTP3 is enabled, else 0. + */ + +int h3_is_enabled(const server_rec *s); + #endif /* H3_CONFIG_H */ diff --git a/modules/http3/include/h3_io.h b/modules/http3/include/h3_io.h index 917dbed7bbb..3ea6966e9ac 100644 --- a/modules/http3/include/h3_io.h +++ b/modules/http3/include/h3_io.h @@ -44,11 +44,11 @@ typedef struct h3_io_t volatile apr_uint32_t total_streams; volatile apr_uint64_t total_bytes_read; volatile apr_uint64_t total_bytes_written; - volatile int thread_running; + volatile apr_uint32_t thread_running; /* Set by a graceful stop: keep servicing the sessions we already have, * but accept no new ones. The event thread keeps running so in-flight * streams can finish; the MPM bounds how long the child waits for them. */ - volatile int draining; + volatile apr_uint32_t draining; h3_wakeup wakeup; APR_OPTIONAL_FN_TYPE(ap_mpm_note_extra_connection_added) * note_conn_added; diff --git a/modules/http3/include/h3_session.h b/modules/http3/include/h3_session.h index 0d5ac91ee81..4b13788a9c9 100644 --- a/modules/http3/include/h3_session.h +++ b/modules/http3/include/h3_session.h @@ -58,11 +58,11 @@ struct h3_session apr_thread_mutex_t* lock; apr_hash_t* streams; - int aborted; + volatile apr_uint32_t aborted; /* Number of streams write-blocked in nghttp3 (see flush_nghttp3). */ int blocked_streams; - int ngh3_dead; + volatile apr_uint32_t ngh3_dead; uint64_t abort_quic_error_code; const char* abort_reason; @@ -90,6 +90,8 @@ struct h3_session int64_t sid; h3_stream* h3s; } pending; + + h3_stream* qpack_encoder_stream; }; struct h3_stream @@ -201,7 +203,7 @@ nghttp3_ssize h3_session_read_data(nghttp3_conn* conn, int64_t stream_id, nghttp apr_status_t h3_stream_response_append(h3_stream* stream, const uint8_t* data, size_t len); /** Mark the response producer complete and resume a blocked nghttp3 reader. */ -void h3_stream_response_complete(h3_stream* stream); +size_t h3_stream_response_complete(h3_stream* stream); /** * Account application response bytes acknowledged by nghttp3. The session diff --git a/test/modules/http3/test_011_directives.py b/test/modules/http3/test_011_directives.py index 825f8f34903..a8df3a79037 100644 --- a/test/modules/http3/test_011_directives.py +++ b/test/modules/http3/test_011_directives.py @@ -19,6 +19,7 @@ ("H3StreamTimeout", 1, 86400), ("H3MaxStreamErrors", 1, 10000), ("H3QpackTableCapacity", 0, 1048576), + ("H3QpackEncoderTableCapacity", -1, 1048576), ("H3QpackBlockedStreams", 0, 1000), ("H3MinWorkers", 1, 4096), ("H3MaxWorkers", 1, 4096), diff --git a/test/modules/http3/test_205_qpack_order.py b/test/modules/http3/test_205_qpack_order.py index 5a73987da27..12d57473396 100644 --- a/test/modules/http3/test_205_qpack_order.py +++ b/test/modules/http3/test_205_qpack_order.py @@ -67,19 +67,21 @@ def start_get(self, authority: str, path: str) -> int: class TestQpackOrder: - @pytest.fixture(autouse=True, scope="class") - @classmethod - def _class_scope(cls, env): - # a new value per response: each header block needs a fresh insert - H3Conf(env).add_h3_vhost(directives=[ - 'Header always set Cache-Control "expr=max-age=%{QUERY_STRING}"', - ]).install() - assert env.apache_restart() == 0 - - def test_h3_205_01_inserts_sent_before_header_blocks(self, env): + @pytest.mark.parametrize("cap", [0, -1]) + def test_h3_205_01_inserts_sent_before_header_blocks(self, env, cap): env.require(env.has_python_package("aioquic"), "python package 'aioquic' is not installed", dep="aioquic") + directives = [ + 'Header always set Cache-Control "expr=max-age=%{QUERY_STRING}"', + ] + if cap != 0: + # Explicit -1 enables nghttp3/client dynamic table; 0 tests the default + directives.insert(0, f"H3QpackEncoderTableCapacity {cap}") + + H3Conf(env).add_h3_vhost(directives=directives).install() + assert env.apache_restart() == 0 + authority = env.h3_domain seq = itertools.count() @@ -108,12 +110,18 @@ async def run(): hdrs = client.headers.get(sid, {}) assert hdrs.get(b":status") == b"200", f"stream {sid}: {hdrs}" assert hdrs.get(b"cache-control") == value, f"stream {sid}: {hdrs}" - # Without the dynamic table (aioquic >= 1.8 turns it off) nothing can - # be blocked, and the check below would pass whatever the server does. - assert client.http.dynamic >= len(expected) // 2, \ - f"only {client.http.dynamic} of {len(expected)} header blocks " \ - f"used the QPACK dynamic table" - # A header block sent ahead of the encoder stream data it depends on - # stalls the stream, and a client that closes it then loses the response. + + if cap == 0: + # Default static-only mode: server must never emit dynamic inserts + assert client.http.dynamic == 0, \ + f"expected 0 dynamic header blocks when cap=0, got {client.http.dynamic}" + else: + # Without the dynamic table (aioquic >= 1.8 turns it off) nothing can + # be blocked, and the check below would pass whatever the server does. + assert client.http.dynamic >= len(expected) // 2, \ + f"only {client.http.dynamic} of {len(expected)} header blocks " \ + f"used the QPACK dynamic table" + # A header block sent ahead of the encoder stream data it depends on + # stalls the stream, and a client that closes it then loses the response. assert client.http.blocked == 0, \ f"{client.http.blocked} of {len(expected)} header blocks were blocked"