2000-08-24 22:15:40 +00:00
|
|
|
/*
|
2009-09-02 23:48:03 +00:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-24 22:15:40 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
2021-06-03 08:37:05 +02:00
|
|
|
*
|
2000-08-24 22:15:40 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
2000-08-24 22:15:40 +00:00
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
#include <isc/magic.h>
|
|
|
|
#include <isc/mem.h>
|
2021-10-04 17:14:53 +02:00
|
|
|
#include <isc/result.h>
|
2000-08-24 22:15:40 +00:00
|
|
|
#include <isc/rwlock.h>
|
|
|
|
#include <isc/util.h>
|
|
|
|
|
|
|
|
#include <dns/forward.h>
|
2022-12-08 14:17:24 +00:00
|
|
|
#include <dns/name.h>
|
2000-08-24 22:15:40 +00:00
|
|
|
#include <dns/rbt.h>
|
|
|
|
#include <dns/types.h>
|
|
|
|
|
|
|
|
struct dns_fwdtable {
|
|
|
|
/* Unlocked. */
|
2000-12-11 19:24:30 +00:00
|
|
|
unsigned int magic;
|
2000-08-24 22:15:40 +00:00
|
|
|
isc_mem_t *mctx;
|
|
|
|
isc_rwlock_t rwlock;
|
|
|
|
/* Locked by lock. */
|
|
|
|
dns_rbt_t *table;
|
|
|
|
};
|
|
|
|
|
2001-06-04 19:33:39 +00:00
|
|
|
#define FWDTABLEMAGIC ISC_MAGIC('F', 'w', 'd', 'T')
|
2000-08-24 22:15:40 +00:00
|
|
|
#define VALID_FWDTABLE(ft) ISC_MAGIC_VALID(ft, FWDTABLEMAGIC)
|
|
|
|
|
|
|
|
static void
|
|
|
|
auto_detach(void *, void *);
|
|
|
|
|
|
|
|
isc_result_t
|
|
|
|
dns_fwdtable_create(isc_mem_t *mctx, dns_fwdtable_t **fwdtablep) {
|
|
|
|
dns_fwdtable_t *fwdtable;
|
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
REQUIRE(fwdtablep != NULL && *fwdtablep == NULL);
|
|
|
|
|
2022-12-12 09:20:48 +00:00
|
|
|
fwdtable = isc_mem_get(mctx, sizeof(*fwdtable));
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
fwdtable->table = NULL;
|
|
|
|
result = dns_rbt_create(mctx, auto_detach, fwdtable, &fwdtable->table);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto cleanup_fwdtable;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-08-24 22:15:40 +00:00
|
|
|
|
Add the reader-writer synchronization with modified C-RW-WP
This changes the internal isc_rwlock implementation to:
Irina Calciu, Dave Dice, Yossi Lev, Victor Luchangco, Virendra
J. Marathe, and Nir Shavit. 2013. NUMA-aware reader-writer locks.
SIGPLAN Not. 48, 8 (August 2013), 157–166.
DOI:https://doi.org/10.1145/2517327.24425
(The full article available from:
http://mcg.cs.tau.ac.il/papers/ppopp2013-rwlocks.pdf)
The implementation is based on the The Writer-Preference Lock (C-RW-WP)
variant (see the 3.4 section of the paper for the rationale).
The implemented algorithm has been modified for simplicity and for usage
patterns in rbtdb.c.
The changes compared to the original algorithm:
* We haven't implemented the cohort locks because that would require a
knowledge of NUMA nodes, instead a simple atomic_bool is used as
synchronization point for writer lock.
* The per-thread reader counters are not being used - this would
require the internal thread id (isc_tid_v) to be always initialized,
even in the utilities; the change has a slight performance penalty,
so we might revisit this change in the future. However, this change
also saves a lot of memory, because cache-line aligned counters were
used, so on 32-core machine, the rwlock would be 4096+ bytes big.
* The readers use a writer_barrier that will raise after a while when
readers lock can't be acquired to prevent readers starvation.
* Separate ingress and egress readers counters queues to reduce both
inter and intra-thread contention.
2021-03-24 17:52:56 +01:00
|
|
|
isc_rwlock_init(&fwdtable->rwlock);
|
2000-08-24 22:15:40 +00:00
|
|
|
fwdtable->mctx = NULL;
|
|
|
|
isc_mem_attach(mctx, &fwdtable->mctx);
|
|
|
|
fwdtable->magic = FWDTABLEMAGIC;
|
|
|
|
*fwdtablep = fwdtable;
|
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
cleanup_fwdtable:
|
2022-12-12 09:20:48 +00:00
|
|
|
isc_mem_put(mctx, fwdtable, sizeof(*fwdtable));
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
isc_result_t
|
2016-12-30 15:45:08 +11:00
|
|
|
dns_fwdtable_addfwd(dns_fwdtable_t *fwdtable, const dns_name_t *name,
|
2013-03-22 12:27:54 -07:00
|
|
|
dns_forwarderlist_t *fwdrs, dns_fwdpolicy_t fwdpolicy) {
|
|
|
|
isc_result_t result;
|
|
|
|
dns_forwarders_t *forwarders;
|
|
|
|
dns_forwarder_t *fwd, *nfwd;
|
|
|
|
|
|
|
|
REQUIRE(VALID_FWDTABLE(fwdtable));
|
|
|
|
|
2022-12-12 09:20:48 +00:00
|
|
|
forwarders = isc_mem_get(fwdtable->mctx, sizeof(*forwarders));
|
2013-03-22 12:27:54 -07:00
|
|
|
|
|
|
|
ISC_LIST_INIT(forwarders->fwdrs);
|
|
|
|
for (fwd = ISC_LIST_HEAD(*fwdrs); fwd != NULL;
|
2022-11-02 19:33:14 +01:00
|
|
|
fwd = ISC_LIST_NEXT(fwd, link))
|
|
|
|
{
|
2022-12-12 09:20:48 +00:00
|
|
|
nfwd = isc_mem_get(fwdtable->mctx, sizeof(*nfwd));
|
2013-03-22 12:27:54 -07:00
|
|
|
*nfwd = *fwd;
|
2022-12-08 14:17:24 +00:00
|
|
|
|
|
|
|
if (fwd->tlsname != NULL) {
|
|
|
|
nfwd->tlsname = isc_mem_get(fwdtable->mctx,
|
|
|
|
sizeof(*nfwd->tlsname));
|
|
|
|
dns_name_init(nfwd->tlsname, NULL);
|
|
|
|
dns_name_dup(fwd->tlsname, fwdtable->mctx,
|
|
|
|
nfwd->tlsname);
|
|
|
|
}
|
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
ISC_LINK_INIT(nfwd, link);
|
|
|
|
ISC_LIST_APPEND(forwarders->fwdrs, nfwd, link);
|
|
|
|
}
|
|
|
|
forwarders->fwdpolicy = fwdpolicy;
|
|
|
|
|
|
|
|
RWLOCK(&fwdtable->rwlock, isc_rwlocktype_write);
|
|
|
|
result = dns_rbt_addname(fwdtable->table, name, forwarders);
|
|
|
|
RWUNLOCK(&fwdtable->rwlock, isc_rwlocktype_write);
|
|
|
|
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto cleanup;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2013-03-22 12:27:54 -07:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
while (!ISC_LIST_EMPTY(forwarders->fwdrs)) {
|
|
|
|
fwd = ISC_LIST_HEAD(forwarders->fwdrs);
|
|
|
|
ISC_LIST_UNLINK(forwarders->fwdrs, fwd, link);
|
2022-12-08 14:17:24 +00:00
|
|
|
if (fwd->tlsname != NULL) {
|
|
|
|
dns_name_free(fwd->tlsname, fwdtable->mctx);
|
|
|
|
isc_mem_put(fwdtable->mctx, fwd->tlsname,
|
|
|
|
sizeof(*fwd->tlsname));
|
|
|
|
}
|
2022-12-12 09:20:48 +00:00
|
|
|
isc_mem_put(fwdtable->mctx, fwd, sizeof(*fwd));
|
2013-03-22 12:27:54 -07:00
|
|
|
}
|
2022-12-12 09:20:48 +00:00
|
|
|
isc_mem_put(fwdtable->mctx, forwarders, sizeof(*forwarders));
|
2013-03-22 12:27:54 -07:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2000-08-24 22:15:40 +00:00
|
|
|
isc_result_t
|
2016-12-30 15:45:08 +11:00
|
|
|
dns_fwdtable_add(dns_fwdtable_t *fwdtable, const dns_name_t *name,
|
2000-08-24 22:15:40 +00:00
|
|
|
isc_sockaddrlist_t *addrs, dns_fwdpolicy_t fwdpolicy) {
|
|
|
|
isc_result_t result;
|
|
|
|
dns_forwarders_t *forwarders;
|
2013-03-22 12:27:54 -07:00
|
|
|
dns_forwarder_t *fwd;
|
|
|
|
isc_sockaddr_t *sa;
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_FWDTABLE(fwdtable));
|
|
|
|
|
2022-12-12 09:20:48 +00:00
|
|
|
forwarders = isc_mem_get(fwdtable->mctx, sizeof(*forwarders));
|
2000-08-24 22:15:40 +00:00
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
ISC_LIST_INIT(forwarders->fwdrs);
|
2000-08-24 22:15:40 +00:00
|
|
|
for (sa = ISC_LIST_HEAD(*addrs); sa != NULL;
|
2022-11-02 19:33:14 +01:00
|
|
|
sa = ISC_LIST_NEXT(sa, link))
|
|
|
|
{
|
2022-12-12 09:20:48 +00:00
|
|
|
fwd = isc_mem_get(fwdtable->mctx, sizeof(*fwd));
|
2022-12-08 14:17:24 +00:00
|
|
|
*fwd = (dns_forwarder_t){ .addr = *sa,
|
|
|
|
.link = ISC_LINK_INITIALIZER };
|
2013-03-22 12:27:54 -07:00
|
|
|
ISC_LIST_APPEND(forwarders->fwdrs, fwd, link);
|
2000-08-24 22:15:40 +00:00
|
|
|
}
|
|
|
|
forwarders->fwdpolicy = fwdpolicy;
|
|
|
|
|
2000-12-11 19:24:30 +00:00
|
|
|
RWLOCK(&fwdtable->rwlock, isc_rwlocktype_write);
|
2000-08-24 22:15:40 +00:00
|
|
|
result = dns_rbt_addname(fwdtable->table, name, forwarders);
|
|
|
|
RWUNLOCK(&fwdtable->rwlock, isc_rwlocktype_write);
|
|
|
|
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
goto cleanup;
|
2020-02-13 21:48:23 +01:00
|
|
|
}
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
cleanup:
|
2013-03-22 12:27:54 -07:00
|
|
|
while (!ISC_LIST_EMPTY(forwarders->fwdrs)) {
|
|
|
|
fwd = ISC_LIST_HEAD(forwarders->fwdrs);
|
|
|
|
ISC_LIST_UNLINK(forwarders->fwdrs, fwd, link);
|
2022-12-12 09:20:48 +00:00
|
|
|
isc_mem_put(fwdtable->mctx, fwd, sizeof(*fwd));
|
2000-08-24 22:15:40 +00:00
|
|
|
}
|
2022-12-12 09:20:48 +00:00
|
|
|
isc_mem_put(fwdtable->mctx, forwarders, sizeof(*forwarders));
|
2000-08-24 22:15:40 +00:00
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2016-12-30 15:45:08 +11:00
|
|
|
dns_fwdtable_find(dns_fwdtable_t *fwdtable, const dns_name_t *name,
|
2018-04-05 16:23:56 +02:00
|
|
|
dns_name_t *foundname, dns_forwarders_t **forwardersp) {
|
2000-08-24 22:15:40 +00:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
REQUIRE(VALID_FWDTABLE(fwdtable));
|
|
|
|
|
|
|
|
RWLOCK(&fwdtable->rwlock, isc_rwlocktype_read);
|
2005-03-16 03:50:47 +00:00
|
|
|
result = dns_rbt_findname(fwdtable->table, name, 0, foundname,
|
2000-08-24 22:15:40 +00:00
|
|
|
(void **)forwardersp);
|
|
|
|
RWUNLOCK(&fwdtable->rwlock, isc_rwlocktype_read);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
dns_fwdtable_destroy(dns_fwdtable_t **fwdtablep) {
|
|
|
|
dns_fwdtable_t *fwdtable;
|
|
|
|
|
|
|
|
REQUIRE(fwdtablep != NULL && VALID_FWDTABLE(*fwdtablep));
|
|
|
|
|
|
|
|
fwdtable = *fwdtablep;
|
2020-02-08 04:37:54 -08:00
|
|
|
*fwdtablep = NULL;
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
dns_rbt_destroy(&fwdtable->table);
|
|
|
|
isc_rwlock_destroy(&fwdtable->rwlock);
|
|
|
|
fwdtable->magic = 0;
|
2019-07-23 17:16:57 -04:00
|
|
|
|
2022-12-12 09:20:48 +00:00
|
|
|
isc_mem_putanddetach(&fwdtable->mctx, fwdtable, sizeof(*fwdtable));
|
2000-08-24 22:15:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
*** Private
|
|
|
|
***/
|
|
|
|
|
|
|
|
static void
|
|
|
|
auto_detach(void *data, void *arg) {
|
|
|
|
dns_forwarders_t *forwarders = data;
|
|
|
|
dns_fwdtable_t *fwdtable = arg;
|
2013-03-22 12:27:54 -07:00
|
|
|
dns_forwarder_t *fwd;
|
2000-08-24 22:15:40 +00:00
|
|
|
|
|
|
|
UNUSED(arg);
|
|
|
|
|
2013-03-22 12:27:54 -07:00
|
|
|
while (!ISC_LIST_EMPTY(forwarders->fwdrs)) {
|
|
|
|
fwd = ISC_LIST_HEAD(forwarders->fwdrs);
|
|
|
|
ISC_LIST_UNLINK(forwarders->fwdrs, fwd, link);
|
2022-12-08 14:17:24 +00:00
|
|
|
if (fwd->tlsname != NULL) {
|
|
|
|
dns_name_free(fwd->tlsname, fwdtable->mctx);
|
|
|
|
isc_mem_put(fwdtable->mctx, fwd->tlsname,
|
|
|
|
sizeof(*fwd->tlsname));
|
|
|
|
}
|
2022-12-12 09:20:48 +00:00
|
|
|
isc_mem_put(fwdtable->mctx, fwd, sizeof(*fwd));
|
2000-08-24 22:15:40 +00:00
|
|
|
}
|
2022-12-12 09:20:48 +00:00
|
|
|
isc_mem_put(fwdtable->mctx, forwarders, sizeof(*forwarders));
|
2000-08-24 22:15:40 +00:00
|
|
|
}
|