From 2690dc48d357c85c59f0d72063d7a799e9d03bc0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ayd=C4=B1n=20Mercan?= Date: Tue, 2 Jan 2024 16:28:46 +0300 Subject: [PATCH] Expose the TCP client count in statistics channel The statistics channel does not expose the current number of TCP clients connected, only the highwater. Therefore, users did not have an easy means to collect statistics about TCP clients served over time. This information could only be measured as a seperate mechanism via rndc by looking at the TCP quota filled. In order to expose the exact current count of connected TCP clients (tracked by the "tcp-clients" quota) as a statistics counter, an extra, dedicated Network Manager callback would need to be implemented for that purpose (a counterpart of ns__client_tcpconn() that would be run when a TCP connection is torn down), which is inefficient. Instead, track the number of currently-connected TCP clients separately for IPv4 and IPv6, as Network Manager statistics. --- bin/named/statschannel.c | 4 ++++ lib/isc/include/isc/stats.h | 3 +++ lib/isc/netmgr/netmgr-int.h | 3 ++- lib/isc/netmgr/netmgr.c | 10 ++++++---- lib/isc/netmgr/tcp.c | 5 +++++ 5 files changed, 20 insertions(+), 5 deletions(-) diff --git a/bin/named/statschannel.c b/bin/named/statschannel.c index 94c97da1e1..83d420f65f 100644 --- a/bin/named/statschannel.c +++ b/bin/named/statschannel.c @@ -611,6 +611,10 @@ init_desc(void) { SET_SOCKSTATDESC(udp6active, "UDP/IPv6 sockets active", "UDP6Active"); SET_SOCKSTATDESC(tcp4active, "TCP/IPv4 sockets active", "TCP4Active"); SET_SOCKSTATDESC(tcp6active, "TCP/IPv6 sockets active", "TCP6Active"); + SET_SOCKSTATDESC(tcp4clients, "TCP/IPv4 clients currently connected", + "TCP4Clients"); + SET_SOCKSTATDESC(tcp6clients, "TCP/IPv6 clients currently connected", + "TCP6Clients"); INSIST(i == isc_sockstatscounter_max); /* Initialize DNSSEC statistics */ diff --git a/lib/isc/include/isc/stats.h b/lib/isc/include/isc/stats.h index 3cc60b0644..23a076eba9 100644 --- a/lib/isc/include/isc/stats.h +++ b/lib/isc/include/isc/stats.h @@ -77,6 +77,9 @@ enum { isc_sockstatscounter_tcp4active, isc_sockstatscounter_tcp6active, + isc_sockstatscounter_tcp4clients, + isc_sockstatscounter_tcp6clients, + isc_sockstatscounter_max, }; diff --git a/lib/isc/netmgr/netmgr-int.h b/lib/isc/netmgr/netmgr-int.h index 71822b3641..9363119d3e 100644 --- a/lib/isc/netmgr/netmgr-int.h +++ b/lib/isc/netmgr/netmgr-int.h @@ -386,7 +386,8 @@ typedef enum { STATID_SENDFAIL = 8, STATID_RECVFAIL = 9, STATID_ACTIVE = 10, - STATID_MAX = 11, + STATID_CLIENTS = 11, + STATID_MAX = 12, } isc__nm_statid_t; typedef struct isc_nmsocket_tls_send_req { diff --git a/lib/isc/netmgr/netmgr.c b/lib/isc/netmgr/netmgr.c index 41541cbb28..ea1c561a62 100644 --- a/lib/isc/netmgr/netmgr.c +++ b/lib/isc/netmgr/netmgr.c @@ -63,7 +63,8 @@ static const isc_statscounter_t udp4statsindex[] = { -1, isc_sockstatscounter_udp4sendfail, isc_sockstatscounter_udp4recvfail, - isc_sockstatscounter_udp4active + isc_sockstatscounter_udp4active, + -1, }; static const isc_statscounter_t udp6statsindex[] = { @@ -77,7 +78,8 @@ static const isc_statscounter_t udp6statsindex[] = { -1, isc_sockstatscounter_udp6sendfail, isc_sockstatscounter_udp6recvfail, - isc_sockstatscounter_udp6active + isc_sockstatscounter_udp6active, + -1, }; static const isc_statscounter_t tcp4statsindex[] = { @@ -86,7 +88,7 @@ static const isc_statscounter_t tcp4statsindex[] = { isc_sockstatscounter_tcp4connectfail, isc_sockstatscounter_tcp4connect, isc_sockstatscounter_tcp4acceptfail, isc_sockstatscounter_tcp4accept, isc_sockstatscounter_tcp4sendfail, isc_sockstatscounter_tcp4recvfail, - isc_sockstatscounter_tcp4active + isc_sockstatscounter_tcp4active, isc_sockstatscounter_tcp4clients, }; static const isc_statscounter_t tcp6statsindex[] = { @@ -95,7 +97,7 @@ static const isc_statscounter_t tcp6statsindex[] = { isc_sockstatscounter_tcp6connectfail, isc_sockstatscounter_tcp6connect, isc_sockstatscounter_tcp6acceptfail, isc_sockstatscounter_tcp6accept, isc_sockstatscounter_tcp6sendfail, isc_sockstatscounter_tcp6recvfail, - isc_sockstatscounter_tcp6active + isc_sockstatscounter_tcp6active, isc_sockstatscounter_tcp6clients, }; static void diff --git a/lib/isc/netmgr/tcp.c b/lib/isc/netmgr/tcp.c index f7fe1a415b..b6545a912d 100644 --- a/lib/isc/netmgr/tcp.c +++ b/lib/isc/netmgr/tcp.c @@ -855,6 +855,10 @@ accept_connection(isc_nmsocket_t *csock) { UV_RUNTIME_CHECK(uv_timer_init, r); uv_handle_set_data((uv_handle_t *)&csock->read_timer, csock); + if (csock->server->pquota != NULL) { + isc__nm_incstats(csock, STATID_CLIENTS); + } + /* * We need to initialize the tcp and timer before failing because * isc__nm_tcp_close() can't handle uninitalized TCP nmsocket. @@ -1105,6 +1109,7 @@ tcp_close_sock(isc_nmsocket_t *sock) { if (sock->server != NULL) { if (sock->server->pquota != NULL) { + isc__nm_decstats(sock, STATID_CLIENTS); isc_quota_release(sock->server->pquota); } isc__nmsocket_detach(&sock->server);