diff --git a/.luacheckrc b/.luacheckrc index 2901cef061513f6780f348b38118c8e4722e0ac2..7ff62248929e41efc4e3fdd2e8d3cbfd2c81a8e6 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -40,7 +40,6 @@ new_read_globals = { 'libknot_SONAME', 'libzscanner_SONAME', 'table_print', - '__engine', '_ENV', } diff --git a/NEWS b/NEWS index 9cef9085ef3d1c507214c73d76b8cc9ed9a8f89c..1eb4da5d4a0e32f108941d8fb31ce369240b1ff5 100644 --- a/NEWS +++ b/NEWS @@ -5,6 +5,7 @@ Improvements ------------ - capabilities are no longer constrained when running as root (!1012) - cache: add percentage usage to cache.stats() (!1025) +- aarch64 support again, as some systems still didn't work (!1033) Bugfixes -------- diff --git a/daemon/bindings/cache.c b/daemon/bindings/cache.c index bf944b90fc443fd94cf5946e6135c6f52bb972b5..d2d37f24b7a0a23b50e37a6b83ba6a910952f1ba 100644 --- a/daemon/bindings/cache.c +++ b/daemon/bindings/cache.c @@ -5,14 +5,12 @@ #include "daemon/bindings/impl.h" #include "lib/cache/cdb_lmdb.h" -#include "daemon/worker.h" #include "daemon/zimport.h" /** @internal return cache, or throw lua error if not open */ -struct kr_cache * cache_assert_open(lua_State *L) +static struct kr_cache * cache_assert_open(lua_State *L) { - struct engine *engine = engine_luaget(L); - struct kr_cache *cache = &engine->resolver.cache; + struct kr_cache *cache = &the_worker->engine->resolver.cache; assert(cache); if (!cache || !kr_cache_is_open(cache)) lua_error_p(L, "no cache is open yet, use cache.open() or cache.size, etc."); @@ -22,7 +20,7 @@ struct kr_cache * cache_assert_open(lua_State *L) /** Return available cached backends. */ static int cache_backends(lua_State *L) { - struct engine *engine = engine_luaget(L); + struct engine *engine = the_worker->engine; lua_newtable(L); for (unsigned i = 0; i < engine->backends.len; ++i) { @@ -176,7 +174,7 @@ static int cache_open(lua_State *L) lua_error_p(L, "expected 'open(number max_size, string config = \"\")'"); /* Select cache storage backend */ - struct engine *engine = engine_luaget(L); + struct engine *engine = the_worker->engine; lua_Integer csize_lua = lua_tointeger(L, 1); if (!(csize_lua >= 8192 && csize_lua < SIZE_MAX)) { /* min. is basically arbitrary */ @@ -223,8 +221,7 @@ static int cache_open(lua_State *L) static int cache_close(lua_State *L) { - struct engine *engine = engine_luaget(L); - struct kr_cache *cache = &engine->resolver.cache; + struct kr_cache *cache = &the_worker->engine->resolver.cache; if (!kr_cache_is_open(cache)) { return 0; } @@ -264,10 +261,10 @@ static int cache_clear_everything(lua_State *L) lua_error_maybe(L, ret); /* Clear reputation tables */ - struct engine *engine = engine_luaget(L); - lru_reset(engine->resolver.cache_rtt); - lru_reset(engine->resolver.cache_rep); - lru_reset(engine->resolver.cache_cookie); + struct kr_context *ctx = &the_worker->engine->resolver; + lru_reset(ctx->cache_rtt); + lru_reset(ctx->cache_rep); + lru_reset(ctx->cache_cookie); lua_pushboolean(L, true); return 1; } @@ -339,8 +336,7 @@ static int cache_get(lua_State *L) * in NS elections again. */ static int cache_ns_tout(lua_State *L) { - struct engine *engine = engine_luaget(L); - struct kr_context *ctx = &engine->resolver; + struct kr_context *ctx = &the_worker->engine->resolver; /* Check parameters */ int n = lua_gettop(L); diff --git a/daemon/bindings/event.c b/daemon/bindings/event.c index 7230dd53b093f144ed8572708119d24467e41552..e53f228e99f40db0a9bce83d7ae4f23f6a25098b 100644 --- a/daemon/bindings/event.c +++ b/daemon/bindings/event.c @@ -4,15 +4,12 @@ #include "daemon/bindings/impl.h" -#include "daemon/worker.h" - #include <unistd.h> #include <uv.h> static void event_free(uv_timer_t *timer) { - struct worker_ctx *worker = timer->loop->data; - lua_State *L = worker->engine->L; + lua_State *L = the_worker->engine->L; int ref = (intptr_t) timer->data; luaL_unref(L, LUA_REGISTRYINDEX, ref); free(timer); @@ -20,8 +17,7 @@ static void event_free(uv_timer_t *timer) static void event_callback(uv_timer_t *timer) { - struct worker_ctx *worker = timer->loop->data; - lua_State *L = worker->engine->L; + lua_State *L = the_worker->engine->L; /* Retrieve callback and execute */ lua_rawgeti(L, LUA_REGISTRYINDEX, (intptr_t) timer->data); @@ -38,8 +34,7 @@ static void event_callback(uv_timer_t *timer) static void event_fdcallback(uv_poll_t* handle, int status, int events) { - struct worker_ctx *worker = handle->loop->data; - lua_State *L = worker->engine->L; + lua_State *L = the_worker->engine->L; /* Retrieve callback and execute */ lua_rawgeti(L, LUA_REGISTRYINDEX, (intptr_t) handle->data); diff --git a/daemon/bindings/impl.h b/daemon/bindings/impl.h index f981614dd1610159beb378fe204e46a23e9be877..9c9e8937c95192eefbf1696da1e522e765cd5197 100644 --- a/daemon/bindings/impl.h +++ b/daemon/bindings/impl.h @@ -5,6 +5,7 @@ #pragma once #include "daemon/engine.h" +#include "daemon/worker.h" /* the_worker is often useful */ #include <lua.h> #include <lauxlib.h> diff --git a/daemon/bindings/modules.c b/daemon/bindings/modules.c index e16eeedd36c96ee22a4ba59526f2b4fa12015baa..5116ff33c56d49aeb15ebb74e4b32a4eecf51647 100644 --- a/daemon/bindings/modules.c +++ b/daemon/bindings/modules.c @@ -8,10 +8,10 @@ /** List loaded modules */ static int mod_list(lua_State *L) { - struct engine *engine = engine_luaget(L); + const module_array_t * const modules = &the_worker->engine->modules; lua_newtable(L); - for (unsigned i = 0; i < engine->modules.len; ++i) { - struct kr_module *module = engine->modules.at[i]; + for (unsigned i = 0; i < modules->len; ++i) { + struct kr_module *module = modules->at[i]; lua_pushstring(L, module->name); lua_rawseti(L, -2, i + 1); } @@ -33,8 +33,7 @@ static int mod_load(lua_State *L) const char *precedence = strtok(NULL, " "); const char *ref = strtok(NULL, " "); /* Load engine module */ - struct engine *engine = engine_luaget(L); - int ret = engine_register(engine, name, precedence, ref); + int ret = engine_register(the_worker->engine, name, precedence, ref); free(declaration); if (ret != 0) { if (ret == kr_error(EIDRM)) { @@ -56,8 +55,7 @@ static int mod_unload(lua_State *L) if (n != 1 || !lua_isstring(L, 1)) lua_error_p(L, "expected 'unload(string name)'"); /* Unload engine module */ - struct engine *engine = engine_luaget(L); - int ret = engine_unregister(engine, lua_tostring(L, 1)); + int ret = engine_unregister(the_worker->engine, lua_tostring(L, 1)); lua_error_maybe(L, ret); lua_pushboolean(L, 1); diff --git a/daemon/bindings/net.c b/daemon/bindings/net.c index c558ec561dc4ff11db27156a810d351982a2b799..f888225349b710e7c427e048e257bf68ab6204f4 100644 --- a/daemon/bindings/net.c +++ b/daemon/bindings/net.c @@ -7,7 +7,6 @@ #include "contrib/base64.h" #include "daemon/network.h" #include "daemon/tls.h" -#include "daemon/worker.h" #include <stdlib.h> @@ -86,10 +85,9 @@ static int net_list_add(const char *key, void *val, void *ext) /** List active endpoints. */ static int net_list(lua_State *L) { - struct engine *engine = engine_luaget(L); lua_newtable(L); lua_pushinteger(L, 1); - map_walk(&engine->net.endpoints, net_list_add, L); + map_walk(&the_worker->engine->net.endpoints, net_list_add, L); lua_pop(L, 1); return 1; } @@ -110,21 +108,21 @@ static bool net_listen_addrs(lua_State *L, int port, bool tls, const char *kind, /* Case: string, representing a single address. */ const char *str = lua_tostring(L, -1); if (str != NULL) { - struct engine *engine = engine_luaget(L); + struct network *net = &the_worker->engine->net; int ret = 0; endpoint_flags_t flags = { .tls = tls, .freebind = freebind }; if (!kind && !flags.tls) { /* normal UDP */ flags.sock_type = SOCK_DGRAM; - ret = network_listen(&engine->net, str, port, flags); + ret = network_listen(net, str, port, flags); } if (!kind && ret == 0) { /* common for normal TCP and TLS */ flags.sock_type = SOCK_STREAM; - ret = network_listen(&engine->net, str, port, flags); + ret = network_listen(net, str, port, flags); } if (kind) { flags.kind = strdup(kind); flags.sock_type = SOCK_STREAM; /* TODO: allow to override this? */ - ret = network_listen(&engine->net, str, port, flags); + ret = network_listen(net, str, port, flags); } if (ret != 0) { if (str[0] == '/') { @@ -240,8 +238,7 @@ static int net_close(lua_State *L) if (!ok) lua_error_p(L, "expected 'close(string addr, [number port])'"); - struct network *net = &engine_luaget(L)->net; - int ret = network_close(net, addr, port); + int ret = network_close(&the_worker->engine->net, addr, port); lua_pushboolean(L, ret == 0); return 1; } @@ -301,8 +298,7 @@ static int net_interfaces(lua_State *L) /** Set UDP maximum payload size. */ static int net_bufsize(lua_State *L) { - struct engine *engine = engine_luaget(L); - knot_rrset_t *opt_rr = engine->resolver.opt_rr; + knot_rrset_t *opt_rr = the_worker->engine->resolver.opt_rr; if (!lua_isnumber(L, 1)) { lua_pushinteger(L, knot_edns_get_payload(opt_rr)); return 1; @@ -335,11 +331,7 @@ static int net_pipeline(lua_State *L) static int net_tls(lua_State *L) { - struct engine *engine = engine_luaget(L); - if (!engine) { - return 0; - } - struct network *net = &engine->net; + struct network *net = &the_worker->engine->net; if (!net) { return 0; } @@ -474,7 +466,7 @@ static int net_tls_client(lua_State *L) /* TODO idea: allow starting the lua table with *multiple* IP targets, * meaning the authentication config should be applied to each. */ - struct network *net = &engine_luaget(L)->net; + struct network *net = &the_worker->engine->net; if (lua_gettop(L) == 0) return tls_params2lua(L, net->tls_client_params); /* Various basic sanity-checking. */ @@ -688,7 +680,7 @@ int net_tls_client_clear(lua_State *L) if (!addr) lua_error_p(L, "invalid IP address"); /* Do the actual removal. */ - struct network *net = &engine_luaget(L)->net; + struct network *net = &the_worker->engine->net; int r = tls_client_param_remove(net->tls_client_params, addr); free_const(addr); lua_error_maybe(L, r); @@ -698,18 +690,18 @@ int net_tls_client_clear(lua_State *L) static int net_tls_padding(lua_State *L) { - struct engine *engine = engine_luaget(L); + struct kr_context *ctx = &the_worker->engine->resolver; /* Only return current padding. */ if (lua_gettop(L) == 0) { - if (engine->resolver.tls_padding < 0) { + if (ctx->tls_padding < 0) { lua_pushboolean(L, true); return 1; - } else if (engine->resolver.tls_padding == 0) { + } else if (ctx->tls_padding == 0) { lua_pushboolean(L, false); return 1; } - lua_pushinteger(L, engine->resolver.tls_padding); + lua_pushinteger(L, ctx->tls_padding); return 1; } @@ -720,15 +712,15 @@ static int net_tls_padding(lua_State *L) if (lua_isboolean(L, 1)) { bool x = lua_toboolean(L, 1); if (x) { - engine->resolver.tls_padding = -1; + ctx->tls_padding = -1; } else { - engine->resolver.tls_padding = 0; + ctx->tls_padding = 0; } } else if (lua_isnumber(L, 1)) { int padding = lua_tointeger(L, 1); if ((padding < 0) || (padding > MAX_TLS_PADDING)) lua_error_p(L, "%s", errstr); - engine->resolver.tls_padding = padding; + ctx->tls_padding = padding; } else { lua_error_p(L, "%s", errstr); } @@ -741,7 +733,7 @@ static int net_tls_padding(lua_State *L) static int net_tls_sticket_secret_string(lua_State *L) { - struct network *net = &engine_luaget(L)->net; + struct network *net = &the_worker->engine->net; size_t secret_len; const char *secret; @@ -807,7 +799,7 @@ static int net_tls_sticket_secret_file(lua_State *L) } fclose(fp); - struct network *net = &engine_luaget(L)->net; + struct network *net = &the_worker->engine->net; tls_session_ticket_ctx_destroy(net->tls_session_ticket_ctx); net->tls_session_ticket_ctx = @@ -896,17 +888,13 @@ static int net_update_timeout(lua_State *L, uint64_t *timeout, const char *name) static int net_tcp_in_idle(lua_State *L) { - struct engine *engine = engine_luaget(L); - struct network *net = &engine->net; - + struct network *net = &the_worker->engine->net; return net_update_timeout(L, &net->tcp.in_idle_timeout, "net.tcp_in_idle"); } static int net_tls_handshake_timeout(lua_State *L) { - struct engine *engine = engine_luaget(L); - struct network *net = &engine->net; - + struct network *net = &the_worker->engine->net; return net_update_timeout(L, &net->tcp.tls_handshake_timeout, "net.tls_handshake_timeout"); } @@ -919,8 +907,6 @@ static int net_bpf_set(lua_State *L) #if __linux__ - struct engine *engine = engine_luaget(L); - struct network *net = &engine->net; int progfd = lua_tointeger(L, 1); if (progfd == 0) { /* conversion error despite that fact @@ -930,7 +916,7 @@ static int net_bpf_set(lua_State *L) } lua_pop(L, 1); - if (network_set_bpf(net, progfd) == 0) { + if (network_set_bpf(&the_worker->engine->net, progfd) == 0) { lua_error_p(L, "failed to attach BPF program to some networks: %s", kr_strerror(errno)); } @@ -949,9 +935,7 @@ static int net_bpf_clear(lua_State *L) #if __linux__ - struct engine *engine = engine_luaget(L); - struct network *net = &engine->net; - network_clear_bpf(net); + network_clear_bpf(&the_worker->engine->net); lua_pushboolean(L, 1); return 1; @@ -970,7 +954,7 @@ static int net_register_endpoint_kind(lua_State *L) } size_t kind_len; const char *kind = lua_tolstring(L, 1, &kind_len); - struct network *net = &engine_luaget(L)->net; + struct network *net = &the_worker->engine->net; /* Unregistering */ if (param_count == 1) { diff --git a/daemon/bindings/worker.c b/daemon/bindings/worker.c index 720a56311a264335311620025d139bb92047022c..b28048fafafb6c95aed6abe581e0a6b5fc3ef858 100644 --- a/daemon/bindings/worker.c +++ b/daemon/bindings/worker.c @@ -4,7 +4,6 @@ #include "daemon/bindings/impl.h" -#include "daemon/worker.h" static inline double getseconds(uv_timeval_t *tv) { diff --git a/daemon/engine.c b/daemon/engine.c index ebf25555de7c5f9363e7b32dde4030e2a6ab5b6d..c45e99010bc9cde803b463c3a5cade8b80f899b5 100644 --- a/daemon/engine.c +++ b/daemon/engine.c @@ -21,6 +21,7 @@ #include "kresconfig.h" #include "daemon/engine.h" #include "daemon/ffimodule.h" +#include "daemon/worker.h" #include "lib/nsrep.h" #include "lib/cache/api.h" #include "lib/defines.h" @@ -135,7 +136,7 @@ static int l_setuser(lua_State *L) /** Quit current executable. */ static int l_quit(lua_State *L) { - engine_stop(engine_luaget(L)); + engine_stop(the_worker->engine); return 0; } @@ -184,7 +185,7 @@ int engine_set_hostname(struct engine *engine, const char *hostname) { /** Return hostname. */ static int l_hostname(lua_State *L) { - struct engine *engine = engine_luaget(L); + struct engine *engine = the_worker->engine; if (lua_gettop(L) == 0) { lua_pushstring(L, engine_get_hostname(engine)); return 1; @@ -209,8 +210,7 @@ static int l_package_version(lua_State *L) /** Load root hints from zonefile. */ static int l_hint_root_file(lua_State *L) { - struct engine *engine = engine_luaget(L); - struct kr_context *ctx = &engine->resolver; + struct kr_context *ctx = &the_worker->engine->resolver; const char *file = lua_tostring(L, 1); const char *err = engine_hint_root_file(ctx, file); @@ -383,7 +383,6 @@ static int l_map(lua_State *L) if (lua_gettop(L) != 1 || !lua_isstring(L, 1)) lua_error_p(L, "map('string with a lua expression')"); - struct engine *engine = engine_luaget(L); const char *cmd = lua_tostring(L, 1); uint32_t len = strlen(cmd); lua_newtable(L); @@ -394,8 +393,8 @@ static int l_map(lua_State *L) lua_settop(L, ntop + 1); /* Push only one return value to table */ lua_rawseti(L, -2, 1); - for (size_t i = 0; i < engine->ipc_set.len; ++i) { - int fd = engine->ipc_set.at[i]; + for (size_t i = 0; i < the_worker->engine->ipc_set.len; ++i) { + int fd = the_worker->engine->ipc_set.at[i]; /* Send command */ expr_checked(write(fd, &len, sizeof(len)) == sizeof(len)); expr_checked(write(fd, cmd, len) == len); @@ -498,8 +497,6 @@ static int init_state(struct engine *engine) lua_setglobal(engine->L, "fromjson"); lua_pushcfunction(engine->L, l_map); lua_setglobal(engine->L, "map"); - lua_pushlightuserdata(engine->L, engine); - lua_setglobal(engine->L, "__engine"); /* Random number generator */ lua_getfield(engine->L, LUA_GLOBALSINDEX, "math"); lua_getfield(engine->L, -1, "randomseed"); @@ -858,11 +855,3 @@ int engine_unregister(struct engine *engine, const char *name) return kr_error(ENOENT); } -struct engine *engine_luaget(lua_State *L) -{ - lua_getglobal(L, "__engine"); - struct engine *engine = lua_touserdata(L, -1); - if (!engine) luaL_error(L, "internal error, empty engine pointer"); - lua_pop(L, 1); - return engine; -} diff --git a/daemon/engine.h b/daemon/engine.h index ad32cee5e2d238f423f328e5c04821129a602daa..3de30320be93e1be65ae80c68d009eb6c31327a4 100644 --- a/daemon/engine.h +++ b/daemon/engine.h @@ -52,9 +52,6 @@ void engine_stop(struct engine *engine); int engine_register(struct engine *engine, const char *name, const char *precedence, const char* ref); int engine_unregister(struct engine *engine, const char *name); -/** Return engine light userdata. */ -struct engine *engine_luaget(struct lua_State *L); - /** Set/get the per engine hostname */ char *engine_get_hostname(struct engine *engine); int engine_set_hostname(struct engine *engine, const char *hostname); diff --git a/daemon/io.c b/daemon/io.c index 578722b49ab220cccd8a34e655c0c0b40edfa925..902b45759bc510fe01b1d1c1f4bf7074aef6ee53 100644 --- a/daemon/io.c +++ b/daemon/io.c @@ -60,8 +60,6 @@ static void handle_getbuf(uv_handle_t* handle, size_t suggested_size, uv_buf_t* void udp_recv(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags) { - uv_loop_t *loop = handle->loop; - struct worker_ctx *worker = loop->data; struct session *s = handle->data; if (session_flags(s)->closing) { return; @@ -89,7 +87,7 @@ void udp_recv(uv_udp_t *handle, ssize_t nread, const uv_buf_t *buf, assert(consumed == nread); (void)consumed; session_wirebuf_process(s, addr); session_wirebuf_discard(s); - mp_flush(worker->pkt_pool.ctx); + mp_flush(the_worker->pkt_pool.ctx); } static int family_to_freebind_option(sa_family_t sa_family, int *level, int *name) @@ -195,11 +193,9 @@ void tcp_timeout_trigger(uv_timer_t *timer) assert(!session_flags(s)->closing); - struct worker_ctx *worker = timer->loop->data; - if (!session_tasklist_is_empty(s)) { int finalized = session_tasklist_finalize_expired(s); - worker->stats.timeout += finalized; + the_worker->stats.timeout += finalized; /* session_tasklist_finalize_expired() may call worker_task_finalize(). * If session is a source session and there were IO errors, * worker_task_finalize() can filnalize all tasks and close session. */ @@ -220,13 +216,12 @@ void tcp_timeout_trigger(uv_timer_t *timer) struct qr_task *t = session_waitinglist_pop(s, false); worker_task_finalize(t, KR_STATE_FAIL); worker_task_unref(t); - worker->stats.timeout += 1; + the_worker->stats.timeout += 1; if (session_flags(s)->closing) { return; } } - const struct engine *engine = worker->engine; - const struct network *net = &engine->net; + const struct network *net = &the_worker->engine->net; uint64_t idle_in_timeout = net->tcp.in_idle_timeout; uint64_t last_activity = session_last_activity(s); uint64_t idle_time = kr_now() - last_activity; @@ -241,8 +236,8 @@ void tcp_timeout_trigger(uv_timer_t *timer) kr_log_verbose("[io] => closing connection to '%s'\n", peer_str ? peer_str : ""); if (session_flags(s)->outgoing) { - worker_del_tcp_waiting(worker, peer); - worker_del_tcp_connected(worker, peer); + worker_del_tcp_waiting(the_worker, peer); + worker_del_tcp_connected(the_worker, peer); } session_close(s); } @@ -312,8 +307,7 @@ static void tcp_recv(uv_stream_t *handle, ssize_t nread, const uv_buf_t *buf) worker_end_tcp(s); } session_wirebuf_compress(s); - struct worker_ctx *worker = handle->loop->data; - mp_flush(worker->pkt_pool.ctx); + mp_flush(the_worker->pkt_pool.ctx); } static void _tcp_accept(uv_stream_t *master, int status, bool tls) diff --git a/daemon/lua/kres-gen.lua b/daemon/lua/kres-gen.lua index c77ffc0ebde872d8e1c519ae02fb972ada6bc0ac..8d02db78e23da8aadff684f1c462dae669bb9d41 100644 --- a/daemon/lua/kres-gen.lua +++ b/daemon/lua/kres-gen.lua @@ -442,6 +442,15 @@ struct qr_task { int worker_resolve_exec(struct qr_task *, knot_pkt_t *); knot_pkt_t *worker_resolve_mk_pkt(const char *, uint16_t, uint16_t, const struct kr_qflags *); struct qr_task *worker_resolve_start(knot_pkt_t *, struct kr_qflags); +struct engine { + struct kr_context resolver; + char _stub[]; +}; +struct worker_ctx { + struct engine *engine; + char _stub[]; +}; +struct worker_ctx *the_worker; typedef struct { uint8_t bitmap[32]; uint8_t length; diff --git a/daemon/lua/kres-gen.sh b/daemon/lua/kres-gen.sh index be7aa367009dca81c3a3a489c80257566b0f0432..309b7f1d7b0aeb23ea4f8d0ed0a938955c74c37a 100755 --- a/daemon/lua/kres-gen.sh +++ b/daemon/lua/kres-gen.sh @@ -278,6 +278,14 @@ ${CDEFS} ${KRESD} functions <<-EOF worker_resolve_start EOF +echo "struct engine" | ${CDEFS} ${KRESD} types | sed '/struct network/,$ d' +printf "\tchar _stub[];\n};\n" + +echo "struct worker_ctx" | ${CDEFS} ${KRESD} types | sed '/uv_loop_t/,$ d' +printf "\tchar _stub[];\n};\n" + +echo "struct worker_ctx *the_worker;" + ## libzscanner API for ./zonefile.lua ${CDEFS} libzscanner types <<-EOF diff --git a/daemon/lua/kres.lua b/daemon/lua/kres.lua index 1bdc30a31544f9ec6813a6145c7042ebe0760ec8..d859fda963864f2bb8dae608efb6748fd12d1a2a 100644 --- a/daemon/lua/kres.lua +++ b/daemon/lua/kres.lua @@ -1061,7 +1061,7 @@ kres = { if ret ~= 1 then return nil end return ffi.string(addr_buf, C.kr_family_len(family)) end, - context = function () return ffi.cast('struct kr_context *', __engine) end, + context = function () return ffi.C.the_worker.engine.resolver end, knot_pkt_rr = knot_pkt_rr, } diff --git a/daemon/lua/sandbox.lua.in b/daemon/lua/sandbox.lua.in index 3eb9377fa6b1808bec911ac60d55c3913c5faa5b..b20068194de3cfd837ea96e3d9addfb255614c40 100644 --- a/daemon/lua/sandbox.lua.in +++ b/daemon/lua/sandbox.lua.in @@ -199,7 +199,7 @@ function modules_create_table_for_c(kr_module_ud) elseif arg ~= nil then arg_conv = tostring(arg) end - local ret_cstr = cb(__engine, kr_module, arg_conv) + local ret_cstr = cb(ffi.C.the_worker.engine, kr_module, arg_conv) if ret_cstr == nil then return nil end diff --git a/daemon/main.c b/daemon/main.c index 3295cd0d646aea04e7f9bc7214ea159a1adc2557..7fbae458acf48aff83f7f9d838fbc194ea5e71e2 100644 --- a/daemon/main.c +++ b/daemon/main.c @@ -602,8 +602,6 @@ int main(int argc, char **argv) .ctx = mp_new (4096), .alloc = (knot_mm_alloc_t) mp_alloc }; - /** Static to work around lua_pushlightuserdata() limitations. - * TODO: convert to a proper singleton like worker, most likely. */ static struct engine engine; ret = engine_init(&engine, &pool); if (ret != 0) { diff --git a/daemon/session.c b/daemon/session.c index 2a234879d675d37235731649321c08e5cb11a6cd..dc60cc8a154e44f789ec2ae7aed76bbc104e52dd 100644 --- a/daemon/session.c +++ b/daemon/session.c @@ -331,9 +331,8 @@ struct session *session_new(uv_handle_t *handle, bool has_tls) * We still need to keep in mind to only touch the buffer * in this callback... */ assert(handle->loop->data); - struct worker_ctx *worker = handle->loop->data; - session->wire_buf = worker->wire_buf; - session->wire_buf_size = sizeof(worker->wire_buf); + session->wire_buf = the_worker->wire_buf; + session->wire_buf_size = sizeof(the_worker->wire_buf); } uv_timer_init(handle->loop, &session->timeout); @@ -711,11 +710,10 @@ int session_wirebuf_process(struct session *session, const struct sockaddr *peer if (session->wire_buf_start_idx == session->wire_buf_end_idx) { return ret; } - struct worker_ctx *worker = session_get_handle(session)->loop->data; size_t wirebuf_data_size = session->wire_buf_end_idx - session->wire_buf_start_idx; uint32_t max_iterations = (wirebuf_data_size / (KNOT_WIRE_HEADER_SIZE + KNOT_WIRE_QUESTION_MIN_SIZE)) + 1; knot_pkt_t *query = NULL; - while (((query = session_produce_packet(session, &worker->pkt_pool)) != NULL) && + while (((query = session_produce_packet(session, &the_worker->pkt_pool)) != NULL) && (ret < max_iterations)) { assert (!session_wirebuf_error(session)); int res = worker_submit(session, peer, query); diff --git a/daemon/worker.c b/daemon/worker.c index 6e87229d8e7e5b145aaee142731aad838213dda8..c4374c3a6979cffee9c65418b55c61bc1a3a6124 100644 --- a/daemon/worker.c +++ b/daemon/worker.c @@ -664,11 +664,8 @@ static struct kr_query *task_get_last_pending_query(struct qr_task *task) static int session_tls_hs_cb(struct session *session, int status) { assert(session_flags(session)->outgoing); - uv_handle_t *handle = session_get_handle(session); - uv_loop_t *loop = handle->loop; - struct worker_ctx *worker = loop->data; struct sockaddr *peer = session_get_peer(session); - int deletion_res = worker_del_tcp_waiting(worker, peer); + int deletion_res = worker_del_tcp_waiting(the_worker, peer); int ret = kr_ok(); if (status) { @@ -677,7 +674,7 @@ static int session_tls_hs_cb(struct session *session, int status) struct kr_qflags *options = &task->ctx->req.options; unsigned score = options->FORWARD || options->STUB ? KR_NS_FWD_DEAD : KR_NS_DEAD; kr_nsrep_update_rtt(NULL, peer, score, - worker->engine->resolver.cache_rtt, + the_worker->engine->resolver.cache_rtt, KR_NS_UPDATE_NORESET); } #ifndef NDEBUG @@ -689,7 +686,7 @@ static int session_tls_hs_cb(struct session *session, int status) assert(deletion_res != 0); const char *key = tcpsess_key(peer); assert(key); - assert(map_contains(&worker->tcp_connected, key) != 0); + assert(map_contains(&the_worker->tcp_connected, key) != 0); } #endif return ret; @@ -717,7 +714,7 @@ static int session_tls_hs_cb(struct session *session, int status) } } - struct session *s = worker_find_tcp_connected(worker, peer); + struct session *s = worker_find_tcp_connected(the_worker, peer); ret = kr_ok(); if (deletion_res == kr_ok()) { /* peer was in the waiting list, add to the connected list. */ @@ -726,7 +723,7 @@ static int session_tls_hs_cb(struct session *session, int status) * peer already is in the connected list. */ ret = kr_error(EINVAL); } else { - ret = worker_add_tcp_connected(worker, peer, session); + ret = worker_add_tcp_connected(the_worker, peer, session); } } else { /* peer wasn't in the waiting list. @@ -758,7 +755,7 @@ static int session_tls_hs_cb(struct session *session, int status) /* Something went wrong. * Either addition to the list of connected sessions * or write to upstream failed. */ - worker_del_tcp_connected(worker, peer); + worker_del_tcp_connected(the_worker, peer); session_waitinglist_finalize(session, KR_STATE_FAIL); assert(session_tasklist_is_empty(session)); session_close(session); @@ -1576,8 +1573,6 @@ int worker_submit(struct session *session, const struct sockaddr *peer, knot_pkt return kr_error(EINVAL); } - struct worker_ctx *worker = handle->loop->data; - /* Parse packet */ int ret = parse_packet(query); @@ -1587,7 +1582,7 @@ int worker_submit(struct session *session, const struct sockaddr *peer, knot_pkt if (!query || (ret != kr_ok() && ret != kr_error(EMSGSIZE)) || (is_query == is_outgoing)) { - if (query && !is_outgoing) worker->stats.dropped += 1; + if (query && !is_outgoing) the_worker->stats.dropped += 1; return kr_error(EILSEQ); } @@ -1596,7 +1591,7 @@ int worker_submit(struct session *session, const struct sockaddr *peer, knot_pkt struct qr_task *task = NULL; const struct sockaddr *addr = NULL; if (!is_outgoing) { /* request from a client */ - struct request_ctx *ctx = request_create(worker, session, peer, + struct request_ctx *ctx = request_create(the_worker, session, peer, knot_wire_get_id(query->wire)); if (!ctx) { return kr_error(ENOMEM); @@ -1727,12 +1722,10 @@ int worker_end_tcp(struct session *session) session_timer_stop(session); - uv_handle_t *handle = session_get_handle(session); - struct worker_ctx *worker = handle->loop->data; struct sockaddr *peer = session_get_peer(session); - worker_del_tcp_waiting(worker, peer); - worker_del_tcp_connected(worker, peer); + worker_del_tcp_waiting(the_worker, peer); + worker_del_tcp_connected(the_worker, peer); session_flags(session)->connected = false; struct tls_client_ctx_t *tls_client_ctx = session_tls_get_client_ctx(session); @@ -2070,7 +2063,7 @@ int worker_init(struct engine *engine, int worker_id, int worker_count) the_worker = worker; loop->data = the_worker; - /* ^^^^ This shouldn't be used anymore, but it's hard to be 100% sure. */ + /* ^^^^ Now this shouldn't be used anymore, but it's hard to be 100% sure. */ return kr_ok(); }