From 5fedd21e16eaa59f949d089c1ce3277035e8ae1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Witold=20Kr=C4=99cicki?= Date: Fri, 20 Mar 2020 11:55:10 +0100 Subject: [PATCH] netmgr refactoring: use generic functions when operating on sockets. tcpdns used transport-specific functions to operate on the outer socket. Use generic ones instead, and select the proper call in netmgr.c. Make the missing functions (e.g. isc_nm_read) generic and add type-specific calls (isc__nm_tcp_read). This is the preparation for netmgr TLS layer. --- lib/isc/include/isc/netmgr.h | 16 ++-------- lib/isc/netmgr/netmgr-int.h | 38 ++++++++++++++++++++--- lib/isc/netmgr/netmgr.c | 60 ++++++++++++++++++++++++++++++++++-- lib/isc/netmgr/tcp.c | 24 +++++++-------- lib/isc/netmgr/tcpdns.c | 6 ++-- lib/isc/netmgr/udp.c | 2 +- lib/isc/win32/libisc.def.in | 5 ++- lib/ns/interfacemgr.c | 4 +-- 8 files changed, 114 insertions(+), 41 deletions(-) diff --git a/lib/isc/include/isc/netmgr.h b/lib/isc/include/isc/netmgr.h index 61ce94b2b2..0c3f3adc7f 100644 --- a/lib/isc/include/isc/netmgr.h +++ b/lib/isc/include/isc/netmgr.h @@ -177,9 +177,9 @@ isc_nm_listenudp(isc_nm_t *mgr, isc_nmiface_t *iface, isc_nm_recv_cb_t cb, */ void -isc_nm_udp_stoplistening(isc_nmsocket_t *sock); +isc_nm_stoplistening(isc_nmsocket_t *sock); /*%< - * Stop listening for UDP packets on socket 'sock'. + * Stop listening on socket 'sock'. */ void @@ -251,12 +251,6 @@ isc_nm_listentcp(isc_nm_t *mgr, isc_nmiface_t *iface, isc_nm_cb_t cb, * prepended with a two-byte length field, and handles buffering. */ -void -isc_nm_tcp_stoplistening(isc_nmsocket_t *sock); -/*%< - * Stop listening on TCP socket 'sock'. - */ - isc_result_t isc_nm_listentcpdns(isc_nm_t *mgr, isc_nmiface_t *iface, isc_nm_recv_cb_t cb, void *cbarg, isc_nm_cb_t accept_cb, void *accept_cbarg, @@ -285,12 +279,6 @@ isc_nm_listentcpdns(isc_nm_t *mgr, isc_nmiface_t *iface, isc_nm_recv_cb_t cb, * 'quota' is passed to isc_nm_listentcp() when opening the raw TCP socket. */ -void -isc_nm_tcpdns_stoplistening(isc_nmsocket_t *sock); -/*%< - * Stop listening on TCPDNS socket 'sock'. - */ - void isc_nm_tcpdns_sequential(isc_nmhandle_t *handle); /*%< diff --git a/lib/isc/netmgr/netmgr-int.h b/lib/isc/netmgr/netmgr-int.h index 4818188570..6472115009 100644 --- a/lib/isc/netmgr/netmgr-int.h +++ b/lib/isc/netmgr/netmgr-int.h @@ -124,6 +124,8 @@ struct isc_nmiface { typedef enum isc__netievent_type { netievent_udpsend, netievent_udprecv, + netievent_udpstop, + netievent_tcpconnect, netievent_tcpsend, netievent_tcprecv, @@ -131,13 +133,13 @@ typedef enum isc__netievent_type { netievent_tcppauseread, netievent_tcpchildlisten, netievent_tcpchildstop, - netievent_closecb, - netievent_shutdown, - netievent_stop, - netievent_udpstop, netievent_tcpstop, netievent_tcpclose, netievent_tcpdnsclose, + + netievent_closecb, + netievent_shutdown, + netievent_stop, netievent_prio = 0xff, /* event type values higher than this * will be treated as high-priority * events, which can be processed @@ -647,6 +649,9 @@ isc__nm_udp_send(isc_nmhandle_t *handle, isc_region_t *region, isc_nm_cb_t cb, * Back-end implementation of isc_nm_send() for UDP handles. */ +void +isc__nm_udp_stoplistening(isc_nmsocket_t *sock); + void isc__nm_async_udplisten(isc__networker_t *worker, isc__netievent_t *ev0); @@ -665,11 +670,26 @@ isc__nm_tcp_send(isc_nmhandle_t *handle, isc_region_t *region, isc_nm_cb_t cb, * Back-end implementation of isc_nm_send() for TCP handles. */ +isc_result_t +isc__nm_tcp_read(isc_nmhandle_t *handle, isc_nm_recv_cb_t cb, void *cbarg); + void isc__nm_tcp_close(isc_nmsocket_t *sock); /*%< * Close a TCP socket. */ +isc_result_t +isc__nm_tcp_pauseread(isc_nmsocket_t *sock); +/*%< + * Pause reading on this socket, while still remembering the callback. + */ + +isc_result_t +isc__nm_tcp_resumeread(isc_nmsocket_t *sock); +/*%< + * Resume reading from socket. + * + */ void isc__nm_tcp_shutdown(isc_nmsocket_t *sock); @@ -677,6 +697,9 @@ isc__nm_tcp_shutdown(isc_nmsocket_t *sock); * Called on shutdown to close and clean up a listening TCP socket. */ +void +isc__nm_tcp_stoplistening(isc_nmsocket_t *sock); + void isc__nm_async_tcpconnect(isc__networker_t *worker, isc__netievent_t *ev0); void @@ -694,6 +717,10 @@ isc__nm_async_startread(isc__networker_t *worker, isc__netievent_t *ev0); void isc__nm_async_pauseread(isc__networker_t *worker, isc__netievent_t *ev0); void +isc__nm_async_tcp_startread(isc__networker_t *worker, isc__netievent_t *ev0); +void +isc__nm_async_tcp_pauseread(isc__networker_t *worker, isc__netievent_t *ev0); +void isc__nm_async_tcpclose(isc__networker_t *worker, isc__netievent_t *ev0); /*%< * Callback handlers for asynchronous TCP events (connect, listen, @@ -713,6 +740,9 @@ isc__nm_tcpdns_close(isc_nmsocket_t *sock); * Close a TCPDNS socket. */ +void +isc__nm_tcpdns_stoplistening(isc_nmsocket_t *sock); + void isc__nm_async_tcpdnsclose(isc__networker_t *worker, isc__netievent_t *ev0); diff --git a/lib/isc/netmgr/netmgr.c b/lib/isc/netmgr/netmgr.c index 9f49dfbd9f..55e7525436 100644 --- a/lib/isc/netmgr/netmgr.c +++ b/lib/isc/netmgr/netmgr.c @@ -607,10 +607,10 @@ process_queue(isc__networker_t *worker, isc_queue_t *queue) { isc__nm_async_tcpchildlisten(worker, ievent); break; case netievent_tcpstartread: - isc__nm_async_startread(worker, ievent); + isc__nm_async_tcp_startread(worker, ievent); break; case netievent_tcppauseread: - isc__nm_async_pauseread(worker, ievent); + isc__nm_async_tcp_pauseread(worker, ievent); break; case netievent_tcpsend: isc__nm_async_tcpsend(worker, ievent); @@ -1321,6 +1321,62 @@ isc_nm_send(isc_nmhandle_t *handle, isc_region_t *region, isc_nm_cb_t cb, } } +isc_result_t +isc_nm_read(isc_nmhandle_t *handle, isc_nm_recv_cb_t cb, void *cbarg) { + REQUIRE(VALID_NMHANDLE(handle)); + + switch (handle->sock->type) { + case isc_nm_tcpsocket: + return (isc__nm_tcp_read(handle, cb, cbarg)); + default: + INSIST(0); + ISC_UNREACHABLE(); + } +} + +isc_result_t +isc_nm_pauseread(isc_nmsocket_t *sock) { + REQUIRE(VALID_NMSOCK(sock)); + switch (sock->type) { + case isc_nm_tcpsocket: + return (isc__nm_tcp_pauseread(sock)); + default: + INSIST(0); + ISC_UNREACHABLE(); + } +} + +isc_result_t +isc_nm_resumeread(isc_nmsocket_t *sock) { + REQUIRE(VALID_NMSOCK(sock)); + switch (sock->type) { + case isc_nm_tcpsocket: + return (isc__nm_tcp_resumeread(sock)); + default: + INSIST(0); + ISC_UNREACHABLE(); + } +} + +void +isc_nm_stoplistening(isc_nmsocket_t *sock) { + REQUIRE(VALID_NMSOCK(sock)); + switch (sock->type) { + case isc_nm_udplistener: + isc__nm_udp_stoplistening(sock); + break; + case isc_nm_tcpdnslistener: + isc__nm_tcpdns_stoplistening(sock); + break; + case isc_nm_tcplistener: + isc__nm_tcp_stoplistening(sock); + break; + default: + INSIST(0); + ISC_UNREACHABLE(); + } +} + void isc__nm_async_closecb(isc__networker_t *worker, isc__netievent_t *ev0) { isc__netievent_closecb_t *ievent = (isc__netievent_closecb_t *)ev0; diff --git a/lib/isc/netmgr/tcp.c b/lib/isc/netmgr/tcp.c index 69eabeb3a1..ed951c5d71 100644 --- a/lib/isc/netmgr/tcp.c +++ b/lib/isc/netmgr/tcp.c @@ -377,7 +377,7 @@ isc__nm_async_tcpchildlisten(isc__networker_t *worker, isc__netievent_t *ev0) { } void -isc_nm_tcp_stoplistening(isc_nmsocket_t *sock) { +isc__nm_tcp_stoplistening(isc_nmsocket_t *sock) { isc__netievent_tcpstop_t *ievent = NULL; REQUIRE(VALID_NMSOCK(sock)); @@ -519,7 +519,7 @@ readtimeout_cb(uv_timer_t *handle) { } isc_result_t -isc_nm_read(isc_nmhandle_t *handle, isc_nm_recv_cb_t cb, void *cbarg) { +isc__nm_tcp_read(isc_nmhandle_t *handle, isc_nm_recv_cb_t cb, void *cbarg) { isc_nmsocket_t *sock = NULL; isc__netievent_startread_t *ievent = NULL; @@ -534,8 +534,8 @@ isc_nm_read(isc_nmhandle_t *handle, isc_nm_recv_cb_t cb, void *cbarg) { ievent->sock = sock; if (sock->tid == isc_nm_tid()) { - isc__nm_async_startread(&sock->mgr->workers[sock->tid], - (isc__netievent_t *)ievent); + isc__nm_async_tcp_startread(&sock->mgr->workers[sock->tid], + (isc__netievent_t *)ievent); isc__nm_put_ievent(sock->mgr, ievent); } else { isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid], @@ -546,7 +546,7 @@ isc_nm_read(isc_nmhandle_t *handle, isc_nm_recv_cb_t cb, void *cbarg) { } void -isc__nm_async_startread(isc__networker_t *worker, isc__netievent_t *ev0) { +isc__nm_async_tcp_startread(isc__networker_t *worker, isc__netievent_t *ev0) { isc__netievent_startread_t *ievent = (isc__netievent_startread_t *)ev0; isc_nmsocket_t *sock = ievent->sock; int r; @@ -569,7 +569,7 @@ isc__nm_async_startread(isc__networker_t *worker, isc__netievent_t *ev0) { } isc_result_t -isc_nm_pauseread(isc_nmsocket_t *sock) { +isc__nm_tcp_pauseread(isc_nmsocket_t *sock) { isc__netievent_pauseread_t *ievent = NULL; REQUIRE(VALID_NMSOCK(sock)); @@ -583,8 +583,8 @@ isc_nm_pauseread(isc_nmsocket_t *sock) { ievent->sock = sock; if (sock->tid == isc_nm_tid()) { - isc__nm_async_pauseread(&sock->mgr->workers[sock->tid], - (isc__netievent_t *)ievent); + isc__nm_async_tcp_pauseread(&sock->mgr->workers[sock->tid], + (isc__netievent_t *)ievent); isc__nm_put_ievent(sock->mgr, ievent); } else { isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid], @@ -595,7 +595,7 @@ isc_nm_pauseread(isc_nmsocket_t *sock) { } void -isc__nm_async_pauseread(isc__networker_t *worker, isc__netievent_t *ev0) { +isc__nm_async_tcp_pauseread(isc__networker_t *worker, isc__netievent_t *ev0) { isc__netievent_pauseread_t *ievent = (isc__netievent_pauseread_t *)ev0; isc_nmsocket_t *sock = ievent->sock; @@ -609,7 +609,7 @@ isc__nm_async_pauseread(isc__networker_t *worker, isc__netievent_t *ev0) { } isc_result_t -isc_nm_resumeread(isc_nmsocket_t *sock) { +isc__nm_tcp_resumeread(isc_nmsocket_t *sock) { isc__netievent_startread_t *ievent = NULL; REQUIRE(VALID_NMSOCK(sock)); @@ -625,8 +625,8 @@ isc_nm_resumeread(isc_nmsocket_t *sock) { ievent->sock = sock; if (sock->tid == isc_nm_tid()) { - isc__nm_async_startread(&sock->mgr->workers[sock->tid], - (isc__netievent_t *)ievent); + isc__nm_async_tcp_startread(&sock->mgr->workers[sock->tid], + (isc__netievent_t *)ievent); isc__nm_put_ievent(sock->mgr, ievent); } else { isc__nm_enqueue_ievent(&sock->mgr->workers[sock->tid], diff --git a/lib/isc/netmgr/tcpdns.c b/lib/isc/netmgr/tcpdns.c index 11d338593d..7e3d1728dd 100644 --- a/lib/isc/netmgr/tcpdns.c +++ b/lib/isc/netmgr/tcpdns.c @@ -316,7 +316,7 @@ isc_nm_listentcpdns(isc_nm_t *mgr, isc_nmiface_t *iface, isc_nm_recv_cb_t cb, } void -isc_nm_tcpdns_stoplistening(isc_nmsocket_t *sock) { +isc__nm_tcpdns_stoplistening(isc_nmsocket_t *sock) { REQUIRE(VALID_NMSOCK(sock)); REQUIRE(sock->type == isc_nm_tcpdnslistener); @@ -326,7 +326,7 @@ isc_nm_tcpdns_stoplistening(isc_nmsocket_t *sock) { sock->rcbarg = NULL; if (sock->outer != NULL) { - isc_nm_tcp_stoplistening(sock->outer); + isc_nm_stoplistening(sock->outer); isc_nmsocket_detach(&sock->outer); } } @@ -494,7 +494,7 @@ isc__nm_tcpdns_send(isc_nmhandle_t *handle, isc_region_t *region, *(uint16_t *)t->region.base = htons(region->length); memmove(t->region.base + 2, region->base, region->length); - return (isc__nm_tcp_send(t->handle, &t->region, tcpdnssend_cb, t)); + return (isc_nm_send(t->handle, &t->region, tcpdnssend_cb, t)); } static void diff --git a/lib/isc/netmgr/udp.c b/lib/isc/netmgr/udp.c index a79855b490..80ff77ee23 100644 --- a/lib/isc/netmgr/udp.c +++ b/lib/isc/netmgr/udp.c @@ -219,7 +219,7 @@ stoplistening(isc_nmsocket_t *sock) { } void -isc_nm_udp_stoplistening(isc_nmsocket_t *sock) { +isc__nm_udp_stoplistening(isc_nmsocket_t *sock) { isc__netievent_udpstop_t *ievent = NULL; /* We can't be launched from network thread, we'd deadlock */ diff --git a/lib/isc/win32/libisc.def.in b/lib/isc/win32/libisc.def.in index c0cbfc164a..c3dfad5a3e 100644 --- a/lib/isc/win32/libisc.def.in +++ b/lib/isc/win32/libisc.def.in @@ -461,14 +461,13 @@ isc_nm_maxudp isc_nm_send isc_nm_setstats isc_nm_start +isc_nm_stoplistening isc_nm_tcp_gettimeouts isc_nm_tcp_settimeouts -isc_nmsocket_detach isc_nm_tcpdns_keepalive isc_nm_tcpdns_sequential -isc_nm_tcpdns_stoplistening isc_nm_tid -isc_nm_udp_stoplistening +isc_nmsocket_detach isc__nm_acquire_interlocked isc__nm_drop_interlocked isc__nm_acquire_interlocked_force diff --git a/lib/ns/interfacemgr.c b/lib/ns/interfacemgr.c index ebf1b0421d..006fd2fad5 100644 --- a/lib/ns/interfacemgr.c +++ b/lib/ns/interfacemgr.c @@ -553,11 +553,11 @@ cleanup_interface: void ns_interface_shutdown(ns_interface_t *ifp) { if (ifp->udplistensocket != NULL) { - isc_nm_udp_stoplistening(ifp->udplistensocket); + isc_nm_stoplistening(ifp->udplistensocket); isc_nmsocket_detach(&ifp->udplistensocket); } if (ifp->tcplistensocket != NULL) { - isc_nm_tcpdns_stoplistening(ifp->tcplistensocket); + isc_nm_stoplistening(ifp->tcplistensocket); isc_nmsocket_detach(&ifp->tcplistensocket); } if (ifp->clientmgr != NULL) {