From 7a30654585b77ca05aa2adbba328dde2d0bb01c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Sur=C3=BD?= Date: Wed, 30 Mar 2022 21:44:04 +0200 Subject: [PATCH] fixup! WIP: Add isc_work_queue() to replace isc_nm_offload_work() - but don't use it yet --- lib/isc/Makefile.am | 11 ++-- lib/isc/include/isc/loop.h | 14 +++++- lib/isc/loop.c | 35 +++++++++++-- lib/isc/loop_p.h | 18 ++++--- lib/isc/netmgr/netmgr-int.h | 3 +- lib/isc/netmgr/netmgr.c | 2 +- lib/isc/netmgr/tcp.c | 2 +- lib/isc/netmgr/tcpdns.c | 2 +- lib/isc/netmgr/tlsdns.c | 2 +- lib/isc/netmgr/tlsstream.c | 2 +- lib/isc/netmgr/udp.c | 2 +- lib/isc/netmgr/uv-compat.h | 94 +---------------------------------- lib/isc/netmgr/uverr2result.c | 82 ------------------------------ lib/isc/signal.c | 2 +- lib/isc/tests/doh_test.c | 3 +- lib/isc/tests/netmgr_test.c | 3 +- lib/isc/tests/uv_wrap.h | 3 +- lib/isc/work.c | 15 +++++- 18 files changed, 84 insertions(+), 211 deletions(-) diff --git a/lib/isc/Makefile.am b/lib/isc/Makefile.am index 682c4723b6..42c3f6d36f 100644 --- a/lib/isc/Makefile.am +++ b/lib/isc/Makefile.am @@ -100,7 +100,9 @@ libisc_la_HEADERS = \ include/isc/types.h \ include/isc/url.h \ include/isc/utf8.h \ - include/isc/util.h + include/isc/util.h \ + include/isc/uv.h \ + include/isc/work.h libisc_la_SOURCES = \ $(libisc_la_HEADERS) \ @@ -111,9 +113,6 @@ libisc_la_SOURCES = \ netmgr/timer.c \ netmgr/tlsdns.c \ netmgr/udp.c \ - netmgr/uv-compat.c \ - netmgr/uv-compat.h \ - netmgr/uverr2result.c \ aes.c \ assertions.c \ astack.c \ @@ -205,7 +204,9 @@ libisc_la_SOURCES = \ trampoline.c \ trampoline_p.h \ url.c \ - utf8.c + utf8.c \ + uv.c \ + work.c libisc_la_CPPFLAGS = \ $(AM_CPPFLAGS) \ diff --git a/lib/isc/include/isc/loop.h b/lib/isc/include/isc/loop.h index 91e451c014..56c03a6d63 100644 --- a/lib/isc/include/isc/loop.h +++ b/lib/isc/include/isc/loop.h @@ -29,6 +29,11 @@ typedef struct isc_signal isc_signal_t; typedef void (*isc_signal_cb)(void *, int); +typedef struct isc_work isc_work_t; + +typedef void (*isc_work_cb)(void *arg); +typedef void (*isc_after_work_cb)(void *arg, isc_result_t result); + #define ISC_LOOPMGR_TID_UNKNOWN UINT32_MAX ISC_LANG_BEGINDECLS @@ -118,9 +123,9 @@ isc_loopmgr_resume(isc_loopmgr_t *loopmgr); *\li We are in a running loop. */ -void +isc_job_t * isc_loop_setup(isc_loop_t *loop, isc_job_cb cb, void *cbarg); -void +isc_job_t * isc_loop_teardown(isc_loop_t *loop, isc_job_cb cb, void *cbarg); /*%< * Schedule actions to be run when starting, and when shutting down, @@ -132,6 +137,11 @@ isc_loop_teardown(isc_loop_t *loop, isc_job_cb cb, void *cbarg); * yet been started. */ +void +isc_loop_nosetup(isc_loop_t *loop, isc_job_t *job); +void +isc_loop_noteardown(isc_loop_t *loop, isc_job_t *job); + void isc_loopmgr_setup(isc_loopmgr_t *loopmgr, isc_job_cb cb, void *cbarg); void diff --git a/lib/isc/loop.c b/lib/isc/loop.c index c1310dbdc3..b9c53338e0 100644 --- a/lib/isc/loop.c +++ b/lib/isc/loop.c @@ -30,9 +30,9 @@ #include #include #include +#include #include "loop_p.h" -#include "netmgr/uv-compat.h" /** * Private @@ -240,6 +240,30 @@ enum { }; static void +isc__loop_deschedule(isc_loop_t *loop, int when, isc_job_t *job) { + switch (when) { + case isc_loop_ctor: + ISC_LIST_DEQUEUE(loop->setup_jobs, job, link); + break; + case isc_loop_dtor: + ISC_LIST_DEQUEUE(loop->teardown_jobs, job, link); + break; + default: + UNREACHABLE(); + } +} + +void +isc_loop_nosetup(isc_loop_t *loop, isc_job_t *job) { + isc__loop_deschedule(loop, isc_loop_ctor, job); +} + +void +isc_loop_noteardown(isc_loop_t *loop, isc_job_t *job) { + isc__loop_deschedule(loop, isc_loop_dtor, job); +} + +static isc_job_t * isc__loop_schedule(isc_loop_t *loop, int when, isc_job_cb cb, void *cbarg) { isc_job_t *job = NULL; isc_loopmgr_t *loopmgr; @@ -283,6 +307,7 @@ isc__loop_schedule(isc_loop_t *loop, int when, isc_job_cb cb, void *cbarg) { default: UNREACHABLE(); } + return (job); } /** @@ -337,14 +362,14 @@ isc_loopmgr_new(isc_mem_t *mctx, uint32_t nloops) { return (loopmgr); } -void +isc_job_t * isc_loop_setup(isc_loop_t *loop, isc_job_cb cb, void *cbarg) { - isc__loop_schedule(loop, isc_loop_ctor, cb, cbarg); + return (isc__loop_schedule(loop, isc_loop_ctor, cb, cbarg)); } -void +isc_job_t * isc_loop_teardown(isc_loop_t *loop, isc_job_cb cb, void *cbarg) { - isc__loop_schedule(loop, isc_loop_dtor, cb, cbarg); + return (isc__loop_schedule(loop, isc_loop_dtor, cb, cbarg)); } static void diff --git a/lib/isc/loop_p.h b/lib/isc/loop_p.h index 15604b4b23..c079895c1d 100644 --- a/lib/isc/loop_p.h +++ b/lib/isc/loop_p.h @@ -17,6 +17,8 @@ #include #include +#include +#include #include #include #include @@ -45,6 +47,15 @@ struct isc_job { LINK(isc_job_t) link; }; +struct isc_work { + uv_work_t work; + isc_loop_t *loop; + isc_work_cb work_cb; + isc_after_work_cb after_work_cb; + isc_job_t *cancel_job; + void *cbarg; +}; + struct isc_loop { int magic; isc_refcount_t references; @@ -99,10 +110,3 @@ struct isc_loopmgr { /* per-thread objects */ isc_loop_t *loops; }; - -/* FIXME: Deduplicate with netmgr-int.h */ -#define UV_RUNTIME_CHECK(func, ret) \ - if (ret != 0) { \ - isc_error_fatal(__FILE__, __LINE__, "%s failed: %s\n", #func, \ - uv_strerror(ret)); \ - } diff --git a/lib/isc/netmgr/netmgr-int.h b/lib/isc/netmgr/netmgr-int.h index d1965d12fd..ed24d2d3f0 100644 --- a/lib/isc/netmgr/netmgr-int.h +++ b/lib/isc/netmgr/netmgr-int.h @@ -37,8 +37,7 @@ #include #include #include - -#include "uv-compat.h" +#include #define ISC_NETMGR_TID_UNKNOWN -1 diff --git a/lib/isc/netmgr/netmgr.c b/lib/isc/netmgr/netmgr.c index 17d2004685..fcffef00ae 100644 --- a/lib/isc/netmgr/netmgr.c +++ b/lib/isc/netmgr/netmgr.c @@ -39,12 +39,12 @@ #include #include #include +#include #include "netmgr-int.h" #include "netmgr_p.h" #include "openssl_shim.h" #include "trampoline_p.h" -#include "uv-compat.h" /*% * How many isc_nmhandles and isc_nm_uvreqs will we be diff --git a/lib/isc/netmgr/tcp.c b/lib/isc/netmgr/tcp.c index 1f4d3bf088..0bce2f3e83 100644 --- a/lib/isc/netmgr/tcp.c +++ b/lib/isc/netmgr/tcp.c @@ -33,9 +33,9 @@ #include #include #include +#include #include "netmgr-int.h" -#include "uv-compat.h" static atomic_uint_fast32_t last_tcpquota_log = 0; diff --git a/lib/isc/netmgr/tcpdns.c b/lib/isc/netmgr/tcpdns.c index cab6fc34f6..bb5b5f3ac7 100644 --- a/lib/isc/netmgr/tcpdns.c +++ b/lib/isc/netmgr/tcpdns.c @@ -33,9 +33,9 @@ #include #include #include +#include #include "netmgr-int.h" -#include "uv-compat.h" static atomic_uint_fast32_t last_tcpdnsquota_log = 0; diff --git a/lib/isc/netmgr/tlsdns.c b/lib/isc/netmgr/tlsdns.c index e5ed8dd9bf..bcd919a893 100644 --- a/lib/isc/netmgr/tlsdns.c +++ b/lib/isc/netmgr/tlsdns.c @@ -33,10 +33,10 @@ #include #include #include +#include #include "netmgr-int.h" #include "openssl_shim.h" -#include "uv-compat.h" static atomic_uint_fast32_t last_tlsdnsquota_log = 0; diff --git a/lib/isc/netmgr/tlsstream.c b/lib/isc/netmgr/tlsstream.c index d0c4868fae..badaedbb32 100644 --- a/lib/isc/netmgr/tlsstream.c +++ b/lib/isc/netmgr/tlsstream.c @@ -36,10 +36,10 @@ #include #include #include +#include #include "../openssl_shim.h" #include "netmgr-int.h" -#include "uv-compat.h" #define TLS_BUF_SIZE (UINT16_MAX) diff --git a/lib/isc/netmgr/udp.c b/lib/isc/netmgr/udp.c index 7b556aa3e3..0a04aaa97d 100644 --- a/lib/isc/netmgr/udp.c +++ b/lib/isc/netmgr/udp.c @@ -29,9 +29,9 @@ #include #include #include +#include #include "netmgr-int.h" -#include "uv-compat.h" #ifdef HAVE_NET_ROUTE_H #include diff --git a/lib/isc/netmgr/uv-compat.h b/lib/isc/netmgr/uv-compat.h index 387d3c6940..3d1955da5b 100644 --- a/lib/isc/netmgr/uv-compat.h +++ b/lib/isc/netmgr/uv-compat.h @@ -14,96 +14,4 @@ #pragma once #include -/* - * These functions were introduced in newer libuv, but we still - * want BIND9 compile on older ones so we emulate them. - * They're inline to avoid conflicts when running with a newer - * library version. - */ - -#define UV_VERSION(major, minor, patch) ((major << 16) | (minor << 8) | (patch)) - -#if !defined(UV__ERR) -#define UV__ERR(x) (-(x)) -#endif - -#if UV_VERSION_HEX < UV_VERSION(1, 19, 0) -static inline void * -uv_handle_get_data(const uv_handle_t *handle) { - return (handle->data); -} - -static inline void -uv_handle_set_data(uv_handle_t *handle, void *data) { - handle->data = data; -} - -static inline void * -uv_req_get_data(const uv_req_t *req) { - return (req->data); -} - -static inline void -uv_req_set_data(uv_req_t *req, void *data) { - req->data = data; -} -#endif /* UV_VERSION_HEX < UV_VERSION(1, 19, 0) */ - -#if UV_VERSION_HEX < UV_VERSION(1, 32, 0) -int -uv_tcp_close_reset(uv_tcp_t *handle, uv_close_cb close_cb); -#endif - -#if UV_VERSION_HEX < UV_VERSION(1, 34, 0) -#define uv_sleep(msec) usleep(msec * 1000) -#endif /* UV_VERSION_HEX < UV_VERSION(1, 34, 0) */ - -#if UV_VERSION_HEX < UV_VERSION(1, 27, 0) -int -isc_uv_udp_connect(uv_udp_t *handle, const struct sockaddr *addr); -/*%< - * Associate the UDP handle to a remote address and port, so every message sent - * by this handle is automatically sent to that destination. - * - * NOTE: This is just a limited shim for uv_udp_connect() as it requires the - * handle to be bound. - */ -#else /* UV_VERSION_HEX < UV_VERSION(1, 27, 0) */ -#define isc_uv_udp_connect uv_udp_connect -#endif /* UV_VERSION_HEX < UV_VERSION(1, 27, 0) */ - -#if UV_VERSION_HEX < UV_VERSION(1, 12, 0) -#include -#include - -static inline int -uv_os_getenv(const char *name, char *buffer, size_t *size) { - size_t len; - char *buf = getenv(name); - - if (buf == NULL) { - return (UV_ENOENT); - } - - len = strlen(buf) + 1; - if (len > *size) { - *size = len; - return (UV_ENOBUFS); - } - - *size = len; - memmove(buffer, buf, len); - - return (0); -} - -#define uv_os_setenv(name, value) setenv(name, value, 0) -#endif /* UV_VERSION_HEX < UV_VERSION(1, 12, 0) */ - -int -isc_uv_udp_freebind(uv_udp_t *handle, const struct sockaddr *addr, - unsigned int flags); - -int -isc_uv_tcp_freebind(uv_tcp_t *handle, const struct sockaddr *addr, - unsigned int flags); +#include diff --git a/lib/isc/netmgr/uverr2result.c b/lib/isc/netmgr/uverr2result.c index 5ce953d729..d16a39999c 100644 --- a/lib/isc/netmgr/uverr2result.c +++ b/lib/isc/netmgr/uverr2result.c @@ -20,85 +20,3 @@ #include #include "netmgr-int.h" - -/*% - * Convert a libuv error value into an isc_result_t. The - * list of supported error values is not complete; new users - * of this function should add any expected errors that are - * not already there. - */ -isc_result_t -isc___nm_uverr2result(int uverr, bool dolog, const char *file, - unsigned int line, const char *func) { - switch (uverr) { - case 0: - return (ISC_R_SUCCESS); - case UV_ENOTDIR: - case UV_ELOOP: - case UV_EINVAL: /* XXX sometimes this is not for files */ - case UV_ENAMETOOLONG: - case UV_EBADF: - return (ISC_R_INVALIDFILE); - case UV_ENOENT: - return (ISC_R_FILENOTFOUND); - case UV_EAGAIN: - return (ISC_R_NOCONN); - case UV_EACCES: - case UV_EPERM: - return (ISC_R_NOPERM); - case UV_EEXIST: - return (ISC_R_FILEEXISTS); - case UV_EIO: - return (ISC_R_IOERROR); - case UV_ENOMEM: - return (ISC_R_NOMEMORY); - case UV_ENFILE: - case UV_EMFILE: - return (ISC_R_TOOMANYOPENFILES); - case UV_ENOSPC: - return (ISC_R_DISCFULL); - case UV_EPIPE: - case UV_ECONNRESET: - case UV_ECONNABORTED: - return (ISC_R_CONNECTIONRESET); - case UV_ENOTCONN: - return (ISC_R_NOTCONNECTED); - case UV_ETIMEDOUT: - return (ISC_R_TIMEDOUT); - case UV_ENOBUFS: - return (ISC_R_NORESOURCES); - case UV_EAFNOSUPPORT: - return (ISC_R_FAMILYNOSUPPORT); - case UV_ENETDOWN: - return (ISC_R_NETDOWN); - case UV_EHOSTDOWN: - return (ISC_R_HOSTDOWN); - case UV_ENETUNREACH: - return (ISC_R_NETUNREACH); - case UV_EHOSTUNREACH: - return (ISC_R_HOSTUNREACH); - case UV_EADDRINUSE: - return (ISC_R_ADDRINUSE); - case UV_EADDRNOTAVAIL: - return (ISC_R_ADDRNOTAVAIL); - case UV_ECONNREFUSED: - return (ISC_R_CONNREFUSED); - case UV_ECANCELED: - return (ISC_R_CANCELED); - case UV_EOF: - return (ISC_R_EOF); - case UV_EMSGSIZE: - return (ISC_R_MAXSIZE); - case UV_ENOTSUP: - return (ISC_R_FAMILYNOSUPPORT); - default: - if (dolog) { - UNEXPECTED_ERROR( - file, line, - "unable to convert libuv " - "error code in %s to isc_result: %d: %s", - func, uverr, uv_strerror(uverr)); - } - return (ISC_R_UNEXPECTED); - } -} diff --git a/lib/isc/signal.c b/lib/isc/signal.c index 967d18cd4a..a1d7b323d6 100644 --- a/lib/isc/signal.c +++ b/lib/isc/signal.c @@ -16,9 +16,9 @@ #include #include +#include #include "loop_p.h" -#include "netmgr/uv-compat.h" isc_signal_t * isc_signal_new(isc_loopmgr_t *loopmgr, isc_signal_cb cb, void *cbarg, diff --git a/lib/isc/tests/doh_test.c b/lib/isc/tests/doh_test.c index 65c5ec6106..0d245680f3 100644 --- a/lib/isc/tests/doh_test.c +++ b/lib/isc/tests/doh_test.c @@ -37,14 +37,13 @@ #include #include #include +#include #include "uv_wrap.h" #define KEEP_BEFORE #include "../netmgr/http.c" #include "../netmgr/netmgr-int.h" -#include "../netmgr/uv-compat.c" -#include "../netmgr/uv-compat.h" #include "../netmgr_p.h" #include "isctest.h" diff --git a/lib/isc/tests/netmgr_test.c b/lib/isc/tests/netmgr_test.c index dbf3b695c3..73ffe70bdc 100644 --- a/lib/isc/tests/netmgr_test.c +++ b/lib/isc/tests/netmgr_test.c @@ -30,14 +30,13 @@ #include #include #include +#include #include "uv_wrap.h" #define KEEP_BEFORE #include "../netmgr/netmgr-int.h" #include "../netmgr/udp.c" -#include "../netmgr/uv-compat.c" -#include "../netmgr/uv-compat.h" #include "../netmgr_p.h" #include "isctest.h" diff --git a/lib/isc/tests/uv_wrap.h b/lib/isc/tests/uv_wrap.h index 5c557b8988..32837b2d07 100644 --- a/lib/isc/tests/uv_wrap.h +++ b/lib/isc/tests/uv_wrap.h @@ -26,12 +26,11 @@ #include #include +#include #define UNIT_TESTING #include -#include "../netmgr/uv-compat.h" - /* uv_udp_t */ int diff --git a/lib/isc/work.c b/lib/isc/work.c index a89e593686..1443cb130a 100644 --- a/lib/isc/work.c +++ b/lib/isc/work.c @@ -38,7 +38,16 @@ isc__after_work_cb(uv_work_t *req, int status) { work->after_work_cb(work->cbarg, result); - isc_mem_put(work->mctx, work, sizeof(*work)); + isc_loop_noteardown(work->loop, work->cancel_job); + + isc_mem_put(work->loop->mctx, work, sizeof(*work)); +} + +static void +isc__work_cancel(void *arg) { + isc_work_t *work = (isc_work_t *)arg; + int r = uv_cancel((uv_req_t *)&work->work); + UV_RUNTIME_CHECK(uv_cancel, r); } void @@ -53,15 +62,17 @@ isc_queue_work(isc_loop_t *loop, isc_work_cb work_cb, work = isc_mem_get(loop->mctx, sizeof(*work)); *work = (isc_work_t){ + .loop = loop, .work_cb = work_cb, .after_work_cb = after_work_cb, .cbarg = cbarg, }; - isc_mem_attach(loop->mctx, &work->mctx); uv_req_set_data((uv_req_t *)&work->work, work); r = uv_queue_work(&loop->loop, &work->work, isc__work_cb, isc__after_work_cb); UV_RUNTIME_CHECK(uv_queue_work, r); + + work->cancel_job = isc_loop_teardown(loop, isc__work_cancel, work); }