]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Convert accepting new TCP connection to to isc_async callback
authorOndřej Surý <ondrej@isc.org>
Thu, 23 Mar 2023 21:37:55 +0000 (22:37 +0100)
committerOndřej Surý <ondrej@isc.org>
Fri, 24 Mar 2023 06:58:52 +0000 (07:58 +0100)
Simplify the acception the new TCP connection by using the isc_async API
from the loopmgr instead of using the asychronous netievent mechanism in
the netmgr.

lib/isc/netmgr/netmgr-int.h
lib/isc/netmgr/netmgr.c
lib/isc/netmgr/tcp.c

index c7f18eb0ff120e8423245aa01d35db9aec106194..acee7fb8fe7734c8d26b7857732bd45c4c316d43 100644 (file)
@@ -250,8 +250,6 @@ struct isc_nmhandle {
 };
 
 typedef enum isc__netievent_type {
-       netievent_tcpaccept,
-
        netievent_tlsclose,
        netievent_tlssend,
        netievent_tlsconnect,
@@ -1281,13 +1279,6 @@ isc__nm_tcp_settimeout(isc_nmhandle_t *handle, uint32_t timeout);
 void
 isc__nmhandle_tcp_set_manual_timer(isc_nmhandle_t *handle, const bool manual);
 
-void
-isc__nm_async_tcpaccept(isc__networker_t *worker, isc__netievent_t *ev0);
-/*%<
- * Callback handlers for asynchronous TCP events (connect, listen,
- * stoplisten, send, read, pause, close).
- */
-
 void
 isc__nm_tcp_senddns(isc_nmhandle_t *handle, const isc_region_t *region,
                    isc_nm_cb_t cb, void *cbarg);
@@ -1695,8 +1686,6 @@ NETIEVENT_SOCKET_REQ_TYPE(tlssend);
 
 NETIEVENT_SOCKET_REQ_RESULT_TYPE(sendcb);
 
-NETIEVENT_SOCKET_QUOTA_TYPE(tcpaccept);
-
 NETIEVENT_SOCKET_TYPE(streamdnsread);
 NETIEVENT_SOCKET_HANDLE_TYPE(streamdnscancel);
 
@@ -1719,8 +1708,6 @@ NETIEVENT_SOCKET_REQ_DECL(tlssend);
 
 NETIEVENT_SOCKET_REQ_RESULT_DECL(sendcb);
 
-NETIEVENT_SOCKET_QUOTA_DECL(tcpaccept);
-
 NETIEVENT_SOCKET_DECL(streamdnsread);
 NETIEVENT_SOCKET_HANDLE_DECL(streamdnscancel);
 
index 6059c7850b38476480b3db14e7dfa81c32d2dc40..2a05a3fc63b053682d178435a1cf0e0ea2e35507 100644 (file)
@@ -439,8 +439,6 @@ process_netievent(void *arg) {
        isc__networker_t *worker = ievent->worker;
 
        switch (ievent->type) {
-               NETIEVENT_CASE(tcpaccept);
-
                NETIEVENT_CASE(tlssend);
                NETIEVENT_CASE(tlsclose);
                NETIEVENT_CASE(tlsdobio);
@@ -490,8 +488,6 @@ NETIEVENT_SOCKET_HTTP_EPS_DEF(httpendpoints);
 
 NETIEVENT_SOCKET_REQ_DEF(tlssend);
 
-NETIEVENT_SOCKET_QUOTA_DEF(tcpaccept);
-
 NETIEVENT_SOCKET_DEF(streamdnsread);
 NETIEVENT_SOCKET_HANDLE_DEF(streamdnscancel);
 
index 2cc936009a7e760f1ea29e6a958c2789232cd17f..66c3a0f9e48169cdb83807ab635e67f1e9b810bc 100644 (file)
@@ -70,10 +70,10 @@ static void
 tcp_close_cb(uv_handle_t *uvhandle);
 
 static isc_result_t
-accept_connection(isc_nmsocket_t *ssock, isc_quota_t *quota);
+accept_connection(isc_nmsocket_t *ssock);
 
 static void
-quota_accept_cb(isc_quota_t *quota, void *sock0);
+quota_accept_cb(isc_quota_t *quota, void *arg);
 
 static void
 failed_accept_cb(isc_nmsocket_t *sock, isc_result_t eresult);
@@ -451,7 +451,6 @@ start_tcp_child(isc_nm_t *mgr, isc_sockaddr_t *iface, isc_nmsocket_t *sock,
         * increasing quota unnecessarily.
         */
        csock->pquota = sock->pquota;
-       isc_quota_cb_init(&csock->quotacb, quota_accept_cb, csock);
 
        if (mgr->load_balance_sockets) {
                UNUSED(fd);
@@ -553,7 +552,6 @@ static void
 tcp_connection_cb(uv_stream_t *server, int status) {
        isc_nmsocket_t *ssock = uv_handle_get_data((uv_handle_t *)server);
        isc_result_t result;
-       isc_quota_t *quota = NULL;
 
        if (status != 0) {
                result = isc_uverr2result(status);
@@ -569,6 +567,8 @@ tcp_connection_cb(uv_stream_t *server, int status) {
        }
 
        if (ssock->pquota != NULL) {
+               isc_quota_t *quota = NULL;
+               isc_quota_cb_init(&ssock->quotacb, quota_accept_cb, ssock);
                result = isc_quota_attach_cb(ssock->pquota, &quota,
                                             &ssock->quotacb);
                if (result == ISC_R_QUOTA) {
@@ -577,7 +577,7 @@ tcp_connection_cb(uv_stream_t *server, int status) {
                }
        }
 
-       result = accept_connection(ssock, quota);
+       result = accept_connection(ssock);
 done:
        isc__nm_accept_connection_log(ssock, result, can_log_tcp_quota());
 }
@@ -834,42 +834,40 @@ free:
        isc__nm_free_uvbuf(sock, buf);
 }
 
+/*
+ * This is called after we get a quota_accept_cb() callback.
+ */
 static void
-quota_accept_cb(isc_quota_t *quota, void *sock0) {
-       isc_nmsocket_t *sock = (isc_nmsocket_t *)sock0;
-       isc__netievent_tcpaccept_t *ievent = NULL;
+tcpaccept_cb(void *arg) {
+       isc_nmsocket_t *sock = arg;
 
        REQUIRE(VALID_NMSOCK(sock));
+       REQUIRE(sock->tid == isc_tid());
 
-       /*
-        * Create a tcpaccept event and pass it using the async channel.  This
-        * needs to be asynchronous, because the quota might have been released
-        * by a different child socket.
-        */
-       ievent = isc__nm_get_netievent_tcpaccept(sock->worker, sock, quota);
-       isc__nm_enqueue_ievent(sock->worker, (isc__netievent_t *)ievent);
+       isc_result_t result = accept_connection(sock);
+       isc__nm_accept_connection_log(sock, result, can_log_tcp_quota());
+       sock->pquota = NULL;
+       isc__nmsocket_detach(&sock);
 }
 
-/*
- * This is called after we get a quota_accept_cb() callback.
- */
-void
-isc__nm_async_tcpaccept(isc__networker_t *worker, isc__netievent_t *ev0) {
-       isc__netievent_tcpaccept_t *ievent = (isc__netievent_tcpaccept_t *)ev0;
-       isc_nmsocket_t *sock = ievent->sock;
-       isc_result_t result;
-
-       UNUSED(worker);
+static void
+quota_accept_cb(isc_quota_t *quota, void *arg) {
+       isc_nmsocket_t *sock = arg;
 
        REQUIRE(VALID_NMSOCK(sock));
-       REQUIRE(sock->tid == isc_tid());
 
-       result = accept_connection(sock, ievent->quota);
-       isc__nm_accept_connection_log(sock, result, can_log_tcp_quota());
+       UNUSED(quota);
+
+       /*
+        * This needs to be asynchronous, because the quota might have been
+        * released by a different child socket.
+        */
+       isc__nmsocket_attach(sock, &(isc_nmsocket_t *){ NULL });
+       isc_async_run(sock->worker->loop, tcpaccept_cb, sock);
 }
 
 static isc_result_t
-accept_connection(isc_nmsocket_t *ssock, isc_quota_t *quota) {
+accept_connection(isc_nmsocket_t *ssock) {
        isc_nmsocket_t *csock = NULL;
        isc__networker_t *worker = NULL;
        int r;
@@ -882,9 +880,10 @@ accept_connection(isc_nmsocket_t *ssock, isc_quota_t *quota) {
        REQUIRE(ssock->tid == isc_tid());
 
        if (isc__nmsocket_closing(ssock)) {
-               if (quota != NULL) {
-                       isc_quota_detach(&quota);
+               if (ssock->pquota != NULL) {
+                       isc_quota_detach(&ssock->pquota);
                }
+
                return (ISC_R_CANCELED);
        }
 
@@ -896,8 +895,8 @@ accept_connection(isc_nmsocket_t *ssock, isc_quota_t *quota) {
        isc__nmsocket_attach(ssock, &csock->server);
        csock->recv_cb = ssock->recv_cb;
        csock->recv_cbarg = ssock->recv_cbarg;
-       csock->quota = quota;
        atomic_init(&csock->accepting, true);
+       csock->quota = ssock->pquota;
 
        worker = csock->worker;