]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Convert starting TCP children to to isc_async callback
authorOndřej Surý <ondrej@isc.org>
Thu, 23 Mar 2023 11:18:18 +0000 (12:18 +0100)
committerOndřej Surý <ondrej@isc.org>
Fri, 24 Mar 2023 06:58:52 +0000 (07:58 +0100)
Simplify the starting of the TCP children 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 9af3da4447d47ca05c384fb2a6fe0c4df167ac15..585382a335526a8f4369de2ec69d5ce2b0e02c74 100644 (file)
@@ -269,7 +269,6 @@ typedef enum isc__netievent_type {
        netievent_settlsctx,
        netievent_sockstop, /* for multilayer sockets */
 
-       netievent_tcplisten,
        netievent_tcpstop,
 } isc__netievent_type;
 
@@ -1292,8 +1291,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_tcplisten(isc__networker_t *worker, isc__netievent_t *ev0);
 void
 isc__nm_async_tcpaccept(isc__networker_t *worker, isc__netievent_t *ev0);
 void
@@ -1695,7 +1692,6 @@ isc__nmsocket_stop(isc_nmsocket_t *listener);
  * typedef all the netievent types
  */
 
-NETIEVENT_SOCKET_TYPE(tcplisten);
 NETIEVENT_SOCKET_TYPE(tcpstop);
 NETIEVENT_SOCKET_TYPE(tlsclose);
 /* NETIEVENT_SOCKET_TYPE(tlsconnect); */ /* unique type, defined independently
@@ -1724,7 +1720,6 @@ NETIEVENT_SOCKET_TYPE(sockstop);
 
 /* Now declared the helper functions */
 
-NETIEVENT_SOCKET_DECL(tcplisten);
 NETIEVENT_SOCKET_DECL(tcpstop);
 NETIEVENT_SOCKET_DECL(tlsclose);
 NETIEVENT_SOCKET_DECL(tlsconnect);
index 2e08db021895feed490550d4b61300844f8ff83a..ad94c121ab230eda1c0c3a6591f788342a2653cb 100644 (file)
@@ -442,7 +442,6 @@ process_netievent(void *arg) {
                NETIEVENT_CASE(udpcancel);
 
                NETIEVENT_CASE(tcpaccept);
-               NETIEVENT_CASE(tcplisten);
                NETIEVENT_CASE(tcpstop);
 
                NETIEVENT_CASE(tlssend);
@@ -482,7 +481,6 @@ isc__nm_put_netievent(isc__networker_t *worker, void *ievent) {
        isc__networker_unref(worker);
 }
 
-NETIEVENT_SOCKET_DEF(tcplisten);
 NETIEVENT_SOCKET_DEF(tcpstop);
 NETIEVENT_SOCKET_DEF(tlsclose);
 NETIEVENT_SOCKET_DEF(tlsconnect);
index ab2651522af444f32ffc7edf0fd3f9ad0d31b1bb..44a2ae7e85448d950bd6cfba868ecf4144ffa69d 100644 (file)
@@ -14,6 +14,7 @@
 #include <libgen.h>
 #include <unistd.h>
 
+#include <isc/async.h>
 #include <isc/atomic.h>
 #include <isc/barrier.h>
 #include <isc/buffer.h>
@@ -339,10 +340,104 @@ isc__nm_tcp_lb_socket(isc_nm_t *mgr, sa_family_t sa_family) {
        return (sock);
 }
 
+static void
+start_tcp_child_job(void *arg) {
+       isc_nmsocket_t *sock = arg;
+
+       REQUIRE(VALID_NMSOCK(sock));
+       REQUIRE(VALID_NMSOCK(sock->parent));
+       REQUIRE(sock->type == isc_nm_tcpsocket);
+       REQUIRE(sock->tid == isc_tid());
+
+       sa_family_t sa_family = sock->iface.type.sa.sa_family;
+       int r;
+       int flags = 0;
+       isc_result_t result = ISC_R_UNSET;
+       isc_loop_t *loop = sock->worker->loop;
+
+       (void)isc__nm_socket_min_mtu(sock->fd, sa_family);
+       (void)isc__nm_socket_tcp_maxseg(sock->fd, NM_MAXSEG);
+
+       r = uv_tcp_init(&loop->loop, &sock->uv_handle.tcp);
+       UV_RUNTIME_CHECK(uv_tcp_init, r);
+       uv_handle_set_data(&sock->uv_handle.handle, sock);
+       /* This keeps the socket alive after everything else is gone */
+       isc__nmsocket_attach(sock, &(isc_nmsocket_t *){ NULL });
+
+       r = uv_timer_init(&loop->loop, &sock->read_timer);
+       UV_RUNTIME_CHECK(uv_timer_init, r);
+       uv_handle_set_data((uv_handle_t *)&sock->read_timer, sock);
+
+       r = uv_tcp_open(&sock->uv_handle.tcp, sock->fd);
+       if (r < 0) {
+               isc__nm_closesocket(sock->fd);
+               isc__nm_incstats(sock, STATID_OPENFAIL);
+               goto done;
+       }
+       isc__nm_incstats(sock, STATID_OPEN);
+
+       if (sa_family == AF_INET6) {
+               flags = UV_TCP_IPV6ONLY;
+       }
+
+       if (sock->worker->netmgr->load_balance_sockets) {
+               r = isc__nm_tcp_freebind(&sock->uv_handle.tcp,
+                                        &sock->iface.type.sa, flags);
+               if (r < 0) {
+                       isc__nm_incstats(sock, STATID_BINDFAIL);
+                       goto done;
+               }
+       } else if (sock->tid == 0) {
+               r = isc__nm_tcp_freebind(&sock->uv_handle.tcp,
+                                        &sock->iface.type.sa, flags);
+               if (r < 0) {
+                       isc__nm_incstats(sock, STATID_BINDFAIL);
+                       goto done;
+               }
+               sock->parent->uv_handle.tcp.flags = sock->uv_handle.tcp.flags;
+       } else {
+               /* The socket is already bound, just copy the flags */
+               sock->uv_handle.tcp.flags = sock->parent->uv_handle.tcp.flags;
+       }
+
+       isc__nm_set_network_buffers(sock->worker->netmgr,
+                                   &sock->uv_handle.handle);
+
+       /*
+        * The callback will run in the same thread uv_listen() was called
+        * from, so a race with tcp_connection_cb() isn't possible.
+        */
+       r = uv_listen((uv_stream_t *)&sock->uv_handle.tcp, sock->backlog,
+                     tcp_connection_cb);
+       if (r != 0) {
+               isc__nmsocket_log(sock, ISC_LOG_ERROR, "uv_listen failed: %s",
+                                 isc_result_totext(isc_uverr2result(r)));
+               isc__nm_incstats(sock, STATID_BINDFAIL);
+               goto done;
+       }
+
+       atomic_store(&sock->listening, true);
+
+done:
+       result = isc_uverr2result(r);
+       atomic_fetch_add(&sock->parent->rchildren, 1);
+
+       if (result != ISC_R_SUCCESS) {
+               sock->pquota = NULL;
+       }
+
+       sock->result = result;
+
+       REQUIRE(!loop->paused);
+
+       if (sock->tid != 0) {
+               isc_barrier_wait(&sock->parent->listen_barrier);
+       }
+}
+
 static void
 start_tcp_child(isc_nm_t *mgr, isc_sockaddr_t *iface, isc_nmsocket_t *sock,
                uv_os_sock_t fd, int tid) {
-       isc__netievent_tcplisten_t *ievent = NULL;
        isc_nmsocket_t *csock = &sock->children[tid];
        isc__networker_t *worker = &mgr->workers[tid];
 
@@ -367,14 +462,10 @@ start_tcp_child(isc_nm_t *mgr, isc_sockaddr_t *iface, isc_nmsocket_t *sock,
        }
        REQUIRE(csock->fd >= 0);
 
-       ievent = isc__nm_get_netievent_tcplisten(csock->worker, csock);
-
        if (tid == 0) {
-               isc__nm_process_ievent(csock->worker,
-                                      (isc__netievent_t *)ievent);
+               start_tcp_child_job(csock);
        } else {
-               isc__nm_enqueue_ievent(csock->worker,
-                                      (isc__netievent_t *)ievent);
+               isc_async_run(worker->loop, start_tcp_child_job, csock);
        }
 }
 
@@ -458,104 +549,6 @@ isc_nm_listentcp(isc_nm_t *mgr, uint32_t workers, isc_sockaddr_t *iface,
        return (ISC_R_SUCCESS);
 }
 
-void
-isc__nm_async_tcplisten(isc__networker_t *worker, isc__netievent_t *ev0) {
-       isc__netievent_tcplisten_t *ievent = (isc__netievent_tcplisten_t *)ev0;
-       sa_family_t sa_family;
-       int r;
-       int flags = 0;
-       isc_nmsocket_t *sock = NULL;
-       isc_result_t result = ISC_R_UNSET;
-
-       REQUIRE(VALID_NMSOCK(ievent->sock));
-       REQUIRE(VALID_NMSOCK(ievent->sock->parent));
-
-       sock = ievent->sock;
-       sa_family = sock->iface.type.sa.sa_family;
-
-       REQUIRE(sock->type == isc_nm_tcpsocket);
-       REQUIRE(sock->tid == isc_tid());
-
-       (void)isc__nm_socket_min_mtu(sock->fd, sa_family);
-       (void)isc__nm_socket_tcp_maxseg(sock->fd, NM_MAXSEG);
-
-       r = uv_tcp_init(&worker->loop->loop, &sock->uv_handle.tcp);
-       UV_RUNTIME_CHECK(uv_tcp_init, r);
-       uv_handle_set_data(&sock->uv_handle.handle, sock);
-       /* This keeps the socket alive after everything else is gone */
-       isc__nmsocket_attach(sock, &(isc_nmsocket_t *){ NULL });
-
-       r = uv_timer_init(&worker->loop->loop, &sock->read_timer);
-       UV_RUNTIME_CHECK(uv_timer_init, r);
-       uv_handle_set_data((uv_handle_t *)&sock->read_timer, sock);
-
-       r = uv_tcp_open(&sock->uv_handle.tcp, sock->fd);
-       if (r < 0) {
-               isc__nm_closesocket(sock->fd);
-               isc__nm_incstats(sock, STATID_OPENFAIL);
-               goto done;
-       }
-       isc__nm_incstats(sock, STATID_OPEN);
-
-       if (sa_family == AF_INET6) {
-               flags = UV_TCP_IPV6ONLY;
-       }
-
-       if (sock->worker->netmgr->load_balance_sockets) {
-               r = isc__nm_tcp_freebind(&sock->uv_handle.tcp,
-                                        &sock->iface.type.sa, flags);
-               if (r < 0) {
-                       isc__nm_incstats(sock, STATID_BINDFAIL);
-                       goto done;
-               }
-       } else if (sock->tid == 0) {
-               r = isc__nm_tcp_freebind(&sock->uv_handle.tcp,
-                                        &sock->iface.type.sa, flags);
-               if (r < 0) {
-                       isc__nm_incstats(sock, STATID_BINDFAIL);
-                       goto done;
-               }
-               sock->parent->uv_handle.tcp.flags = sock->uv_handle.tcp.flags;
-       } else {
-               /* The socket is already bound, just copy the flags */
-               sock->uv_handle.tcp.flags = sock->parent->uv_handle.tcp.flags;
-       }
-
-       isc__nm_set_network_buffers(sock->worker->netmgr,
-                                   &sock->uv_handle.handle);
-
-       /*
-        * The callback will run in the same thread uv_listen() was called
-        * from, so a race with tcp_connection_cb() isn't possible.
-        */
-       r = uv_listen((uv_stream_t *)&sock->uv_handle.tcp, sock->backlog,
-                     tcp_connection_cb);
-       if (r != 0) {
-               isc__nmsocket_log(sock, ISC_LOG_ERROR, "uv_listen failed: %s",
-                                 isc_result_totext(isc_uverr2result(r)));
-               isc__nm_incstats(sock, STATID_BINDFAIL);
-               goto done;
-       }
-
-       atomic_store(&sock->listening, true);
-
-done:
-       result = isc_uverr2result(r);
-       atomic_fetch_add(&sock->parent->rchildren, 1);
-
-       if (result != ISC_R_SUCCESS) {
-               sock->pquota = NULL;
-       }
-
-       sock->result = result;
-
-       REQUIRE(!worker->loop->paused);
-
-       if (sock->tid != 0) {
-               isc_barrier_wait(&sock->parent->listen_barrier);
-       }
-}
-
 static void
 tcp_connection_cb(uv_stream_t *server, int status) {
        isc_nmsocket_t *ssock = uv_handle_get_data((uv_handle_t *)server);