From fc6f751fbed1be60f2ae189e83d8158117a92f9a Mon Sep 17 00:00:00 2001 From: Evan Hunt Date: Wed, 14 Jul 2021 21:12:37 -0700 Subject: [PATCH] replace per-protocol keepalive functions with a common one this commit removes isc__nm_tcpdns_keepalive() and isc__nm_tlsdns_keepalive(); keepalive for these protocols and for TCP will now be set directly from isc_nmhandle_keepalive(). protocols that have an underlying TCP socket (i.e., TLS stream and HTTP), now have protocol-specific routines, called by isc_nmhandle_keeaplive(), to set the keepalive value on the underlying socket. --- lib/isc/netmgr/http.c | 17 +++++++++++++++++ lib/isc/netmgr/netmgr-int.h | 30 ++++++++++++------------------ lib/isc/netmgr/netmgr.c | 27 ++++++++++++++++++++++----- lib/isc/netmgr/tcpdns.c | 13 ------------- lib/isc/netmgr/tlsdns.c | 13 ------------- lib/isc/netmgr/tlsstream.c | 16 ++++++++++++++++ 6 files changed, 67 insertions(+), 49 deletions(-) diff --git a/lib/isc/netmgr/http.c b/lib/isc/netmgr/http.c index 58c2058cc6..6a8a4128c2 100644 --- a/lib/isc/netmgr/http.c +++ b/lib/isc/netmgr/http.c @@ -3064,6 +3064,23 @@ isc__nm_http_settimeout(isc_nmhandle_t *handle, uint32_t timeout) { } } +void +isc__nmhandle_http_keepalive(isc_nmhandle_t *handle, bool value) { + isc_nmsocket_t *sock = NULL; + + REQUIRE(VALID_NMHANDLE(handle)); + REQUIRE(VALID_NMSOCK(handle->sock)); + REQUIRE(handle->sock->type == isc_nm_httpsocket); + + sock = handle->sock; + if (sock->h2.session != NULL && sock->h2.session->handle) { + INSIST(VALID_HTTP2_SESSION(sock->h2.session)); + INSIST(VALID_NMHANDLE(sock->h2.session->handle)); + + isc_nmhandle_keepalive(sock->h2.session->handle, value); + } +} + /* * DoH GET Query String Scanner-less Recursive Descent Parser/Verifier * diff --git a/lib/isc/netmgr/netmgr-int.h b/lib/isc/netmgr/netmgr-int.h index 1902c3df73..60d6b83c51 100644 --- a/lib/isc/netmgr/netmgr-int.h +++ b/lib/isc/netmgr/netmgr-int.h @@ -1517,15 +1517,6 @@ isc__nm_tcpdns_cancelread(isc_nmhandle_t *handle); * Stop reading on a connected TCPDNS handle. */ -void -isc__nm_tcpdns_keepalive(isc_nmhandle_t *handle, bool value); -/*%< - * Enable/disable keepalive on this connection by setting it to 'value'. - * - * When keepalive is active, we switch to using the keepalive timeout - * to determine when to close a connection, rather than the idle timeout. - */ - void isc__nm_tlsdns_send(isc_nmhandle_t *handle, isc_region_t *region, isc_nm_cb_t cb, void *cbarg); @@ -1564,15 +1555,6 @@ isc__nm_tlsdns_cancelread(isc_nmhandle_t *handle); * Stop reading on a connected TLSDNS handle. */ -void -isc__nm_tlsdns_keepalive(isc_nmhandle_t *handle, bool value); -/*%< - * Enable/disable keepalive on this connection by setting it to 'value'. - * - * When keepalive is active, we switch to using the keepalive timeout - * to determine when to close a connection, rather than the idle timeout. - */ - void isc__nm_async_tlsdnscycle(isc__networker_t *worker, isc__netievent_t *ev0); void @@ -1647,6 +1629,12 @@ isc__nm_tls_cleartimeout(isc_nmhandle_t *handle); * around. */ +void +isc__nmhandle_tls_keepalive(isc_nmhandle_t *handle, bool value); +/*%< + * Set the keepalive value on the underlying TCP handle. + */ + void isc__nm_http_stoplistening(isc_nmsocket_t *sock); @@ -1660,6 +1648,12 @@ isc__nm_http_cleartimeout(isc_nmhandle_t *handle); * around. */ +void +isc__nmhandle_http_keepalive(isc_nmhandle_t *handle, bool value); +/*%< + * Set the keepalive value on the underlying session handle + */ + void isc__nm_http_initsocket(isc_nmsocket_t *sock); diff --git a/lib/isc/netmgr/netmgr.c b/lib/isc/netmgr/netmgr.c index 45b768d069..cd051ee517 100644 --- a/lib/isc/netmgr/netmgr.c +++ b/lib/isc/netmgr/netmgr.c @@ -2367,16 +2367,33 @@ isc_nmhandle_settimeout(isc_nmhandle_t *handle, uint32_t timeout) { void isc_nmhandle_keepalive(isc_nmhandle_t *handle, bool value) { - REQUIRE(VALID_NMHANDLE(handle)); + isc_nmsocket_t *sock = NULL; - switch (handle->sock->type) { + REQUIRE(VALID_NMHANDLE(handle)); + REQUIRE(VALID_NMSOCK(handle->sock)); + + sock = handle->sock; + + switch (sock->type) { + case isc_nm_tcpsocket: case isc_nm_tcpdnssocket: - isc__nm_tcpdns_keepalive(handle, value); - break; case isc_nm_tlsdnssocket: - isc__nm_tlsdns_keepalive(handle, value); + atomic_store(&sock->keepalive, value); + sock->read_timeout = value ? atomic_load(&sock->mgr->keepalive) + : atomic_load(&sock->mgr->idle); break; +#if HAVE_LIBNGHTTP2 + case isc_nm_tlssocket: + isc__nmhandle_tls_keepalive(handle, value); + break; + case isc_nm_httpsocket: + isc__nmhandle_http_keepalive(handle, value); + break; +#endif /* HAVE_LIBNGHTTP2 */ default: + /* + * For any other protocol, this is a no-op. + */ return; } } diff --git a/lib/isc/netmgr/tcpdns.c b/lib/isc/netmgr/tcpdns.c index a612bbbd31..11a7111b3a 100644 --- a/lib/isc/netmgr/tcpdns.c +++ b/lib/isc/netmgr/tcpdns.c @@ -1435,16 +1435,3 @@ isc__nm_async_tcpdnscancel(isc__networker_t *worker, isc__netievent_t *ev0) { isc__nm_failed_read_cb(sock, ISC_R_EOF, false); } - -void -isc__nm_tcpdns_keepalive(isc_nmhandle_t *handle, bool value) { - isc_nmsocket_t *sock = NULL; - - REQUIRE(VALID_NMHANDLE(handle)); - REQUIRE(VALID_NMSOCK(handle->sock)); - REQUIRE(handle->sock->type == isc_nm_tcpdnssocket); - - sock = handle->sock; - - atomic_store(&sock->keepalive, value); -} diff --git a/lib/isc/netmgr/tlsdns.c b/lib/isc/netmgr/tlsdns.c index 7e2f83e526..72fe096f5c 100644 --- a/lib/isc/netmgr/tlsdns.c +++ b/lib/isc/netmgr/tlsdns.c @@ -2005,16 +2005,3 @@ isc__nm_async_tlsdnscancel(isc__networker_t *worker, isc__netievent_t *ev0) { isc__nm_failed_read_cb(sock, ISC_R_EOF, false); } - -void -isc__nm_tlsdns_keepalive(isc_nmhandle_t *handle, bool value) { - isc_nmsocket_t *sock = NULL; - - REQUIRE(VALID_NMHANDLE(handle)); - REQUIRE(VALID_NMSOCK(handle->sock)); - REQUIRE(handle->sock->type == isc_nm_tlsdnssocket); - - sock = handle->sock; - - atomic_store(&sock->keepalive, value); -} diff --git a/lib/isc/netmgr/tlsstream.c b/lib/isc/netmgr/tlsstream.c index 0ccf68b4eb..8321b74f3b 100644 --- a/lib/isc/netmgr/tlsstream.c +++ b/lib/isc/netmgr/tlsstream.c @@ -1043,3 +1043,19 @@ isc__nm_tls_settimeout(isc_nmhandle_t *handle, uint32_t timeout) { isc_nmhandle_settimeout(sock->outerhandle, timeout); } } + +void +isc__nmhandle_tls_keepalive(isc_nmhandle_t *handle, bool value) { + isc_nmsocket_t *sock = NULL; + + REQUIRE(VALID_NMHANDLE(handle)); + REQUIRE(VALID_NMSOCK(handle->sock)); + REQUIRE(handle->sock->type == isc_nm_tlssocket); + + sock = handle->sock; + if (sock->outerhandle != NULL) { + INSIST(VALID_NMHANDLE(sock->outerhandle)); + + isc_nmhandle_keepalive(sock->outerhandle, value); + } +}