From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server-vie001.gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,URIBL_DBL_BLOCKED_OPENDNS, URIBL_ZEN_BLOCKED_OPENDNS autolearn=ham autolearn_force=no version=3.4.6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=new2025; t=1756391832; bh=hZ4YCc5XM41pu/Wcu4w1jMm4Gh47gNGXXroTU/XH2G0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Message-ID:Date:From: Reply-To:Subject:To:Cc:In-Reply-To:References:Resent-Date: Resent-From:Resent-To:Resent-Cc:User-Agent:Content-Type: Content-Transfer-Encoding; b=wpjQY2+FBrPOC6uH65qhY944+ZXGOlQCq5xlD13L5/xBFuRZQLT6/og5ADSBWOc1c b+byaI8zR+ZddOe2rbK/zVNzSAWiVVu+eXw3nAzEguLe7nvmO/DCegYMHs/YXJjgFa DdrZaSgPNAJlry1IrsfhxFu66oMy9bAa8PGJsw5zbNc4a55sZnFotyb1DbN2MV1Inp X0eZKg/D6VgKnaawcTh1BcVFodgtQi65D8On9IdxMT8GAoSJP5RULwfD+ZvZmwJ31f OTGlu4qnnARW5wKlCxEkBMHJ/XoiIV90w3iXkPUo9CUWjb+tvMs/AdI712ohL7Fkms JoP/dj0bZ0N2A== Received: from zero (unknown [182.253.228.107]) by server-vie001.gnuweeb.org (Postfix) with ESMTPSA id 425D53127F75; Thu, 28 Aug 2025 14:37:01 +0000 (UTC) From: Ahmad Gani To: Ammar Faizi Cc: Ahmad Gani , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH gwproxy v6 06/11] dns: Remove code block related to the usage of glibc's getaddrinfo_a function Date: Thu, 28 Aug 2025 21:34:28 +0700 Message-ID: <20250828143444.540247-7-reyuki@gnuweeb.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250828143444.540247-1-reyuki@gnuweeb.org> References: <20250828143444.540247-1-reyuki@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: This change is required for preparation in migration to no dedicated DNS threads alternative. Signed-off-by: Ahmad Gani --- configure | 20 ----- src/gwproxy/dns.c | 220 ---------------------------------------------- 2 files changed, 240 deletions(-) diff --git a/configure b/configure index cef07b1cb652..19baa1585237 100755 --- a/configure +++ b/configure @@ -355,26 +355,6 @@ else CXXFLAGS="${CXXFLAGS} -DNDEBUG"; fi; -# ------ Check for getaddrinfo_a() ------ -cat > $tmp_c << EOF -#include -int main(void) -{ - struct sigevent ev; - struct gaicb *gc = (void *)0; - getaddrinfo_a(GAI_NOWAIT, &gc, 1, &ev); - return 0; -} -EOF -l="Do we have getaddrinfo_a()?"; -if compile_cc "" "-lanl" "getaddrinfo_a()"; then - add_config "CONFIG_HAVE_GETADDRINFO_A"; - print_supp "yes" "${l}"; - LIB_LDFLAGS="${LIB_LDFLAGS} -lanl"; -else - print_supp "no" "${l}"; -fi; - add_make_var "CC" "${cc}"; add_make_var "CXX" "${cxx}"; add_make_var "CFLAGS" "$(echo "${CFLAGS} ${USER_CFLAGS}" | awk '{$1=$1};1')"; diff --git a/src/gwproxy/dns.c b/src/gwproxy/dns.c index cdc62a5bcf78..1e271a388493 100644 --- a/src/gwproxy/dns.c +++ b/src/gwproxy/dns.c @@ -234,204 +234,6 @@ static void wait_for_queue_entry(struct gwp_dns_ctx *ctx) cond_scan_cache(ctx); } -#ifdef CONFIG_HAVE_GETADDRINFO_A -/* - * Must be called with ctx->lock held. - */ -static struct gwp_dns_entry *unplug_queue_list(struct gwp_dns_ctx *ctx) -{ - struct gwp_dns_entry *head = ctx->head; - - ctx->head = ctx->tail = NULL; - ctx->nr_entries = 0; - return head; -} - -struct dbq_entry { - struct gwp_dns_entry *e; - struct gaicb cb; -}; - -struct dns_batch_query { - struct dbq_entry *entries; - struct gaicb **reqs; - struct addrinfo hints; - uint32_t nr_entries; - uint32_t cap; -}; - -static void dbq_free(struct dns_batch_query *dbq) -{ - uint32_t i; - - if (!dbq) - return; - - if (dbq->reqs) { - for (i = 0; i < dbq->nr_entries; i++) { - if (dbq->reqs[i]->ar_result) - freeaddrinfo(dbq->reqs[i]->ar_result); - } - } - - free(dbq->entries); - free(dbq->reqs); - free(dbq); -} - -static int dbq_add_entry(struct dns_batch_query *dbq, struct gwp_dns_entry *e) -{ - struct dbq_entry *de; - - if (dbq->nr_entries >= dbq->cap) { - uint32_t new_cap = dbq->cap ? dbq->cap * 2 : 16; - struct dbq_entry *nentries; - - nentries = realloc(dbq->entries, new_cap * sizeof(*nentries)); - if (!nentries) - return -ENOMEM; - dbq->entries = nentries; - dbq->cap = new_cap; - } - - de = &dbq->entries[dbq->nr_entries]; - de->e = e; - memset(&de->cb, 0, sizeof(de->cb)); - de->cb.ar_name = e->name; - de->cb.ar_service = e->service; - de->cb.ar_request = &dbq->hints; - de->cb.ar_result = NULL; - dbq->nr_entries++; - - return 0; -} - -static int collect_active_queries(struct gwp_dns_ctx *ctx, - struct gwp_dns_entry **head_p, - struct dns_batch_query **dbq_p) -{ - struct gwp_dns_entry *e, *next, *prev = NULL, *head = *head_p; - struct dns_batch_query *dbq; - - dbq = calloc(1, sizeof(*dbq)); - if (!dbq) - return -ENOMEM; - - assert(head); - prep_hints(&dbq->hints, ctx->cfg.restyp); - for (e = head; e; e = next) { - int x = atomic_load(&e->refcnt); - next = e->next; - if (x > 1) { - - if (dbq_add_entry(dbq, e)) { - dbq_free(dbq); - return -ENOMEM; - } - - prev = e; - continue; - } - - assert(x == 1); - /* - * If the refcnt is 1, it means we are the last reference - * to this entry. The client no longer cares about the - * result. We can free it immediately. No need to resolve - * the query nor to signal the eventfd. - */ - if (prev) - prev->next = next; - else - head = next; - - gwp_dns_entry_free(e); - } - - *head_p = head; - *dbq_p = dbq; - return 0; -} - -static void dispatch_batch_result(int r, struct gwp_dns_ctx *ctx, - struct dns_batch_query *dbq, - uint32_t restyp) -{ - struct gwp_dns_entry *e; - struct addrinfo *ai; - uint32_t i; - - for (i = 0; i < dbq->nr_entries; i++) { - e = dbq->entries[i].e; - ai = dbq->reqs[i]->ar_result; - - if (!r) { - e->res = gai_error(dbq->reqs[i]); - if (!e->res) { - if (!iterate_addr_list(ai, &e->addr, restyp)) - e->res = -EHOSTUNREACH; - } - } else { - e->res = r; - } - - eventfd_write(e->ev_fd, 1); - if (!e->res) - try_pass_result_to_cache(ctx, e->name, ai); - } -} - -/* - * Filling dbq->reqs[n] cannot be done in dbq_add_entry() because - * the reallocation of dbq->entries may change the address of - * dbq->entries[n].cb. - */ -static int prep_reqs(struct dns_batch_query *dbq) -{ - uint32_t i; - - dbq->reqs = malloc(dbq->nr_entries * sizeof(*dbq->reqs)); - if (!dbq->reqs) - return -ENOMEM; - - for (i = 0; i < dbq->nr_entries; i++) - dbq->reqs[i] = &dbq->entries[i].cb; - - return 0; -} - -/* - * Must be called with ctx->lock held. May release the lock, but - * it will reacquire it before returning. - */ -static void process_queue_entry_batch(struct gwp_dns_ctx *ctx) -{ - struct gwp_dns_entry *head = unplug_queue_list(ctx); - struct dns_batch_query *dbq = NULL; - - if (!head) - return; - - pthread_mutex_unlock(&ctx->lock); - - if (!collect_active_queries(ctx, &head, &dbq)) { - if (!prep_reqs(dbq)) { - struct sigevent ev; - int r; - - memset(&ev, 0, sizeof(ev)); - ev.sigev_notify = SIGEV_NONE; - r = getaddrinfo_a(GAI_WAIT, dbq->reqs, dbq->nr_entries, &ev); - dispatch_batch_result(r, ctx, dbq, ctx->cfg.restyp); - } - } - - dbq_free(dbq); - put_all_entries(head); - pthread_mutex_lock(&ctx->lock); -} -#endif /* #ifdef CONFIG_HAVE_GETADDRINFO_A */ - /* * Must be called with ctx->lock held. May release the lock, but * it will reacquire it before returning. @@ -477,28 +279,6 @@ out: */ static void process_queue_entry(struct gwp_dns_ctx *ctx) { - /* - * There are two cases here: - * - * 1. All of the DNS threads are busy, and there are still a lot - * of queued entries. Process them in batch via getaddrinfo_a(). - * - * 2. The number of threads is sufficient to handle the queued - * entries, so process them one by one. - * - * Why not always getaddrinfo_a()? Because getaddrinfo_a() has - * a higher overhead than processing entries individually as it - * will spawn a new thread for each query. Don't bother invoking - * clone() for each entry if we can process them in the current - * thread. - */ -#ifdef CONFIG_HAVE_GETADDRINFO_A - if (ctx->nr_entries > (ctx->nr_sleeping + 16)) { - process_queue_entry_batch(ctx); - return; - } -#endif - process_queue_entry_single(ctx); } -- Ahmad Gani