bind9/lib/dns/db.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

1214 lines
30 KiB
C
Raw Normal View History

1999-01-28 18:53:03 -05:00
/*
2018-02-15 14:56:13 -05:00
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
1999-01-28 18:53:03 -05:00
*
* SPDX-License-Identifier: MPL-2.0
*
1999-01-28 18:53:03 -05: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/.
*
1999-01-28 18:53:03 -05:00
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
1999-01-28 18:53:03 -05:00
*/
/*! \file */
2000-06-22 18:00:42 -04:00
/***
*** Imports
***/
#include <inttypes.h>
#include <stdbool.h>
#include <isc/buffer.h>
#include <isc/hash.h>
#include <isc/log.h>
#include <isc/mem.h>
#include <isc/result.h>
#include <isc/rwlock.h>
#include <isc/string.h>
#include <isc/tid.h>
#include <isc/urcu.h>
2000-04-27 21:12:23 -04:00
#include <isc/util.h>
1999-01-28 18:53:03 -05:00
#include <dns/callbacks.h>
#include <dns/clientinfo.h>
#include <dns/db.h>
1999-07-30 19:32:19 -04:00
#include <dns/master.h>
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rdataset.h>
#include <dns/rdatasetiter.h>
/***
*** Private Types
***/
struct dns_dbimplementation {
const char *name;
dns_dbcreatefunc_t create;
isc_mem_t *mctx;
void *driverarg;
ISC_LINK(dns_dbimplementation_t) link;
};
/***
*** Supported DB Implementations Registry
***/
/*
* Built in database implementations are registered here.
*/
1999-01-28 18:53:03 -05:00
#include "db_p.h"
#include "qpcache_p.h"
#include "qpzone_p.h"
1999-01-28 18:53:03 -05:00
unsigned int dns_pps = 0U;
static ISC_LIST(dns_dbimplementation_t) implementations;
static isc_rwlock_t implock;
static dns_dbimplementation_t qpimp;
static dns_dbimplementation_t qpzoneimp;
void
dns__db_initialize(void) {
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 12:52:56 -04:00
isc_rwlock_init(&implock);
ISC_LIST_INIT(implementations);
qpimp = (dns_dbimplementation_t){
.name = "qpcache",
.create = dns__qpcache_create,
.link = ISC_LINK_INITIALIZER,
};
qpzoneimp = (dns_dbimplementation_t){
.name = "qpzone",
.create = dns__qpzone_create,
.link = ISC_LINK_INITIALIZER,
};
ISC_LIST_APPEND(implementations, &qpimp, link);
ISC_LIST_APPEND(implementations, &qpzoneimp, link);
}
void
dns__db_shutdown(void) {
isc_rwlock_destroy(&implock);
}
static dns_dbimplementation_t *
impfind(const char *name) {
dns_dbimplementation_t *imp;
2008-04-01 19:47:10 -04:00
for (imp = ISC_LIST_HEAD(implementations); imp != NULL;
imp = ISC_LIST_NEXT(imp, link))
{
if (strcasecmp(name, imp->name) == 0) {
return imp;
}
}
return NULL;
}
static void
call_updatenotify(dns_db_t *db);
/***
*** Basic DB Methods
***/
isc_result_t
dns_db_create(isc_mem_t *mctx, const char *db_type, const dns_name_t *origin,
dns_dbtype_t type, dns_rdataclass_t rdclass, unsigned int argc,
1999-01-28 18:53:03 -05:00
char *argv[], dns_db_t **dbp) {
dns_dbimplementation_t *impinfo = NULL;
/*
* Create a new database using implementation 'db_type'.
*/
REQUIRE(dbp != NULL && *dbp == NULL);
REQUIRE(dns_name_isabsolute(origin));
1999-01-28 18:53:03 -05:00
RWLOCK(&implock, isc_rwlocktype_read);
impinfo = impfind(db_type);
if (impinfo != NULL) {
isc_result_t result;
result = ((impinfo->create)(mctx, origin, type, rdclass, argc,
argv, impinfo->driverarg, dbp));
RWUNLOCK(&implock, isc_rwlocktype_read);
#if DNS_DB_TRACE
fprintf(stderr, "dns_db_create:%s:%s:%d:%p->references = 1\n",
__func__, __FILE__, __LINE__ + 1, *dbp);
#endif
return result;
}
RWUNLOCK(&implock, isc_rwlocktype_read);
1999-01-28 18:53:03 -05:00
isc_log_write(DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DB, ISC_LOG_ERROR,
"unsupported database type '%s'", db_type);
return ISC_R_NOTFOUND;
1999-01-28 18:53:03 -05:00
}
static void
dns__db_destroy(dns_db_t *db) {
(db->methods->destroy)(db);
1999-01-28 18:53:03 -05:00
}
#if DNS_DB_TRACE
ISC_REFCOUNT_TRACE_IMPL(dns_db, dns__db_destroy);
#else
ISC_REFCOUNT_IMPL(dns_db, dns__db_destroy);
#endif
1999-01-28 18:53:03 -05:00
bool
1999-01-28 18:53:03 -05:00
dns_db_iscache(dns_db_t *db) {
/*
* Does 'db' have cache semantics?
*/
1999-01-28 18:53:03 -05:00
REQUIRE(DNS_DB_VALID(db));
if ((db->attributes & DNS_DBATTR_CACHE) != 0) {
return true;
}
return false;
1999-01-28 18:53:03 -05:00
}
bool
1999-01-28 18:53:03 -05:00
dns_db_iszone(dns_db_t *db) {
/*
* Does 'db' have zone semantics?
*/
1999-01-28 18:53:03 -05:00
REQUIRE(DNS_DB_VALID(db));
if ((db->attributes & (DNS_DBATTR_CACHE | DNS_DBATTR_STUB)) == 0) {
return true;
}
return false;
}
bool
dns_db_isstub(dns_db_t *db) {
/*
* Does 'db' have stub semantics?
*/
REQUIRE(DNS_DB_VALID(db));
if ((db->attributes & DNS_DBATTR_STUB) != 0) {
return true;
}
return false;
1999-01-28 18:53:03 -05:00
}
bool
1999-09-07 14:12:11 -04:00
dns_db_issecure(dns_db_t *db) {
/*
* Is 'db' secure?
*/
1999-09-07 14:12:11 -04:00
REQUIRE(DNS_DB_VALID(db));
REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
if (db->methods->issecure != NULL) {
return (db->methods->issecure)(db);
}
return false;
1999-09-07 14:12:11 -04:00
}
bool
dns_db_ispersistent(dns_db_t *db) {
/*
* Is 'db' persistent?
*/
REQUIRE(DNS_DB_VALID(db));
if (db->methods->beginload == NULL) {
/* If the database can't be loaded, assume it's persistent */
return true;
}
return false;
}
1999-02-01 16:21:35 -05:00
dns_name_t *
dns_db_origin(dns_db_t *db) {
/*
* The origin of the database.
*/
1999-02-01 16:21:35 -05:00
REQUIRE(DNS_DB_VALID(db));
return &db->origin;
1999-02-01 16:21:35 -05:00
}
1999-04-20 18:26:12 -04:00
dns_rdataclass_t
dns_db_class(dns_db_t *db) {
/*
* The class of the database.
*/
REQUIRE(DNS_DB_VALID(db));
return db->rdclass;
}
isc_result_t
dns_db_beginload(dns_db_t *db, dns_rdatacallbacks_t *callbacks) {
1999-07-30 19:32:19 -04:00
/*
* Begin loading 'db'.
*/
REQUIRE(DNS_DB_VALID(db));
REQUIRE(DNS_CALLBACK_VALID(callbacks));
1999-07-30 19:32:19 -04:00
if (db->methods->beginload != NULL) {
return (db->methods->beginload)(db, callbacks);
}
return ISC_R_NOTIMPLEMENTED;
1999-07-30 19:32:19 -04:00
}
isc_result_t
dns_db_endload(dns_db_t *db, dns_rdatacallbacks_t *callbacks) {
1999-07-30 19:32:19 -04:00
/*
* Finish loading 'db'.
*/
REQUIRE(DNS_DB_VALID(db));
REQUIRE(DNS_CALLBACK_VALID(callbacks));
REQUIRE(callbacks->add_private != NULL);
1999-07-30 19:32:19 -04:00
/*
* When dns_db_endload() is called, we call the onupdate function
* for all registered listeners, regardless of whether the underlying
* database has an 'endload' implementation.
*/
call_updatenotify(db);
if (db->methods->endload != NULL) {
return (db->methods->endload)(db, callbacks);
}
return ISC_R_NOTIMPLEMENTED;
1999-07-30 19:32:19 -04:00
}
isc_result_t
dns_db_load(dns_db_t *db, const char *filename, dns_masterformat_t format,
unsigned int options) {
isc_result_t result, eresult;
1999-07-30 19:32:19 -04:00
dns_rdatacallbacks_t callbacks;
/*
* Load master file 'filename' into 'db'.
*/
1999-01-29 02:03:42 -05:00
REQUIRE(DNS_DB_VALID(db));
1999-07-30 19:32:19 -04:00
if ((db->attributes & DNS_DBATTR_CACHE) != 0) {
options |= DNS_MASTER_AGETTL;
}
1999-07-30 19:32:19 -04:00
dns_rdatacallbacks_init(&callbacks);
result = dns_db_beginload(db, &callbacks);
if (result != ISC_R_SUCCESS) {
1999-07-30 19:32:19 -04:00
return result;
}
result = dns_master_loadfile(filename, &db->origin, &db->origin,
db->rdclass, options, 0, &callbacks, NULL,
NULL, db->mctx, format, 0);
eresult = dns_db_endload(db, &callbacks);
1999-07-30 19:32:19 -04:00
/*
* We always call dns_db_endload(), but we only want to return its
1999-08-05 18:12:38 -04:00
* result if dns_master_loadfile() succeeded. If dns_master_loadfile()
1999-07-30 19:32:19 -04:00
* failed, we want to return the result code it gave us.
*/
if (eresult != ISC_R_SUCCESS &&
(result == ISC_R_SUCCESS || result == DNS_R_SEENINCLUDE))
{
1999-07-30 19:32:19 -04:00
result = eresult;
}
1999-07-30 19:32:19 -04:00
return result;
1999-01-29 02:03:42 -05:00
}
/***
*** Version Methods
***/
1999-01-28 18:53:03 -05:00
void
dns_db_currentversion(dns_db_t *db, dns_dbversion_t **versionp) {
/*
* Open the current version for reading.
*/
1999-01-28 18:53:03 -05:00
REQUIRE(DNS_DB_VALID(db));
REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
REQUIRE(versionp != NULL && *versionp == NULL);
1999-01-28 18:53:03 -05:00
(db->methods->currentversion)(db, versionp);
}
isc_result_t
1999-01-28 18:53:03 -05:00
dns_db_newversion(dns_db_t *db, dns_dbversion_t **versionp) {
/*
* Open a new version for reading and writing.
*/
1999-01-28 18:53:03 -05:00
REQUIRE(DNS_DB_VALID(db));
REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
REQUIRE(versionp != NULL && *versionp == NULL);
1999-01-28 18:53:03 -05:00
if (db->methods->newversion != NULL) {
return (db->methods->newversion)(db, versionp);
}
return ISC_R_NOTIMPLEMENTED;
1999-01-28 18:53:03 -05:00
}
1999-04-19 18:51:48 -04:00
void
dns_db_attachversion(dns_db_t *db, dns_dbversion_t *source,
dns_dbversion_t **targetp) {
/*
* Attach '*targetp' to 'source'.
*/
REQUIRE(DNS_DB_VALID(db));
REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
REQUIRE(source != NULL);
REQUIRE(targetp != NULL && *targetp == NULL);
1999-04-19 18:51:48 -04:00
(db->methods->attachversion)(db, source, targetp);
ENSURE(*targetp != NULL);
}
1999-01-28 18:53:03 -05:00
void
dns__db_closeversion(dns_db_t *db, dns_dbversion_t **versionp,
bool commit DNS__DB_FLARG) {
/*
* Close version '*versionp'.
*/
1999-01-28 18:53:03 -05:00
REQUIRE(DNS_DB_VALID(db));
REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
REQUIRE(versionp != NULL && *versionp != NULL);
(db->methods->closeversion)(db, versionp, commit DNS__DB_FLARG_PASS);
1999-01-28 18:53:03 -05:00
if (commit) {
call_updatenotify(db);
}
ENSURE(*versionp == NULL);
1999-01-28 18:53:03 -05:00
}
/***
*** Node Methods
***/
1999-01-28 18:53:03 -05:00
isc_result_t
dns__db_findnode(dns_db_t *db, const dns_name_t *name, bool create,
dns_dbnode_t **nodep DNS__DB_FLARG) {
/*
* Find the node with name 'name'.
*/
1999-01-28 18:53:03 -05:00
REQUIRE(DNS_DB_VALID(db));
REQUIRE(nodep != NULL && *nodep == NULL);
1999-01-28 18:53:03 -05:00
if (db->methods->findnode != NULL) {
return (db->methods->findnode)(db, name, create,
nodep DNS__DB_FLARG_PASS);
} else {
return (db->methods->findnodeext)(db, name, create, NULL, NULL,
nodep DNS__DB_FLARG_PASS);
}
}
isc_result_t
dns__db_findnodeext(dns_db_t *db, const dns_name_t *name, bool create,
dns_clientinfomethods_t *methods,
dns_clientinfo_t *clientinfo,
dns_dbnode_t **nodep DNS__DB_FLARG) {
/*
* Find the node with name 'name', passing 'arg' to the database
* implementation.
*/
REQUIRE(DNS_DB_VALID(db));
REQUIRE(nodep != NULL && *nodep == NULL);
if (db->methods->findnodeext != NULL) {
return (db->methods->findnodeext)(db, name, create, methods,
clientinfo,
nodep DNS__DB_FLARG_PASS);
} else {
return (db->methods->findnode)(db, name, create,
nodep DNS__DB_FLARG_PASS);
}
1999-01-28 18:53:03 -05:00
}
isc_result_t
dns__db_findnsec3node(dns_db_t *db, const dns_name_t *name, bool create,
dns_dbnode_t **nodep DNS__DB_FLARG) {
/*
* Find the node with name 'name'.
*/
REQUIRE(DNS_DB_VALID(db));
REQUIRE(nodep != NULL && *nodep == NULL);
return (db->methods->findnsec3node)(db, name, create,
nodep DNS__DB_FLARG_PASS);
}
isc_result_t
dns__db_find(dns_db_t *db, const dns_name_t *name, dns_dbversion_t *version,
dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
dns_dbnode_t **nodep, dns_name_t *foundname,
dns_rdataset_t *rdataset,
dns_rdataset_t *sigrdataset DNS__DB_FLARG) {
/*
* Find the best match for 'name' and 'type' in version 'version'
* of 'db'.
*/
REQUIRE(DNS_DB_VALID(db));
REQUIRE(type != dns_rdatatype_rrsig);
2018-07-10 20:46:25 -04:00
REQUIRE(nodep == NULL || *nodep == NULL);
REQUIRE(dns_name_hasbuffer(foundname));
REQUIRE(rdataset == NULL || (DNS_RDATASET_VALID(rdataset) &&
!dns_rdataset_isassociated(rdataset)));
1999-08-31 18:14:06 -04:00
REQUIRE(sigrdataset == NULL ||
(DNS_RDATASET_VALID(sigrdataset) &&
!dns_rdataset_isassociated(sigrdataset)));
if (db->methods->find != NULL) {
return (db->methods->find)(db, name, version, type, options,
now, nodep, foundname, rdataset,
sigrdataset DNS__DB_FLARG_PASS);
} else {
return (db->methods->findext)(
db, name, version, type, options, now, nodep, foundname,
NULL, NULL, rdataset, sigrdataset DNS__DB_FLARG_PASS);
}
}
isc_result_t
dns__db_findext(dns_db_t *db, const dns_name_t *name, dns_dbversion_t *version,
dns_rdatatype_t type, unsigned int options, isc_stdtime_t now,
dns_dbnode_t **nodep, dns_name_t *foundname,
dns_clientinfomethods_t *methods, dns_clientinfo_t *clientinfo,
dns_rdataset_t *rdataset,
dns_rdataset_t *sigrdataset DNS__DB_FLARG) {
/*
* Find the best match for 'name' and 'type' in version 'version'
* of 'db', passing in 'arg'.
*/
REQUIRE(DNS_DB_VALID(db));
REQUIRE(type != dns_rdatatype_rrsig);
2018-07-10 20:46:25 -04:00
REQUIRE(nodep == NULL || *nodep == NULL);
REQUIRE(dns_name_hasbuffer(foundname));
REQUIRE(rdataset == NULL || (DNS_RDATASET_VALID(rdataset) &&
!dns_rdataset_isassociated(rdataset)));
REQUIRE(sigrdataset == NULL ||
(DNS_RDATASET_VALID(sigrdataset) &&
!dns_rdataset_isassociated(sigrdataset)));
if (db->methods->findext != NULL) {
return (db->methods->findext)(db, name, version, type, options,
now, nodep, foundname, methods,
clientinfo, rdataset,
sigrdataset DNS__DB_FLARG_PASS);
} else {
return (db->methods->find)(db, name, version, type, options,
now, nodep, foundname, rdataset,
sigrdataset DNS__DB_FLARG_PASS);
}
1999-03-31 23:03:22 -05:00
}
isc_result_t
dns__db_findzonecut(dns_db_t *db, const dns_name_t *name, unsigned int options,
isc_stdtime_t now, dns_dbnode_t **nodep,
dns_name_t *foundname, dns_name_t *dcname,
dns_rdataset_t *rdataset,
dns_rdataset_t *sigrdataset DNS__DB_FLARG) {
1999-10-21 13:50:36 -04:00
/*
* Find the deepest known zonecut which encloses 'name' in 'db'.
* foundname is the zonecut, dcname is the deepest name we have
* in database that is part of queried name.
1999-10-21 13:50:36 -04:00
*/
REQUIRE(DNS_DB_VALID(db));
REQUIRE((db->attributes & DNS_DBATTR_CACHE) != 0);
2018-07-10 20:46:25 -04:00
REQUIRE(nodep == NULL || *nodep == NULL);
1999-10-21 13:50:36 -04:00
REQUIRE(dns_name_hasbuffer(foundname));
REQUIRE(sigrdataset == NULL ||
(DNS_RDATASET_VALID(sigrdataset) &&
!dns_rdataset_isassociated(sigrdataset)));
1999-10-21 13:50:36 -04:00
if (db->methods->findzonecut != NULL) {
return (db->methods->findzonecut)(
db, name, options, now, nodep, foundname, dcname,
rdataset, sigrdataset DNS__DB_FLARG_PASS);
}
return ISC_R_NOTIMPLEMENTED;
1999-10-21 13:50:36 -04:00
}
1999-01-28 18:53:03 -05:00
void
dns__db_attachnode(dns_db_t *db, dns_dbnode_t *source,
dns_dbnode_t **targetp DNS__DB_FLARG) {
/*
* Attach *targetp to source.
*/
1999-01-28 18:53:03 -05:00
REQUIRE(DNS_DB_VALID(db));
REQUIRE(source != NULL);
REQUIRE(targetp != NULL && *targetp == NULL);
1999-01-28 18:53:03 -05:00
(db->methods->attachnode)(db, source, targetp DNS__DB_FLARG_PASS);
1999-01-28 18:53:03 -05:00
}
void
dns__db_detachnode(dns_db_t *db, dns_dbnode_t **nodep DNS__DB_FLARG) {
/*
* Detach *nodep from its node.
*/
1999-01-28 18:53:03 -05:00
REQUIRE(DNS_DB_VALID(db));
REQUIRE(nodep != NULL && *nodep != NULL);
1999-01-28 18:53:03 -05:00
(db->methods->detachnode)(db, nodep DNS__DB_FLARG_PASS);
ENSURE(*nodep == NULL);
1999-01-28 18:53:03 -05:00
}
void
dns_db_transfernode(dns_db_t *db, dns_dbnode_t **sourcep,
dns_dbnode_t **targetp) {
REQUIRE(DNS_DB_VALID(db));
REQUIRE(targetp != NULL && *targetp == NULL);
REQUIRE(sourcep != NULL && *sourcep != NULL);
*targetp = *sourcep;
*sourcep = NULL;
1999-03-05 18:25:44 -05:00
}
1999-03-08 14:00:55 -05:00
/***
*** DB Iterator Creation
***/
isc_result_t
dns_db_createiterator(dns_db_t *db, unsigned int flags,
1999-03-08 14:00:55 -05:00
dns_dbiterator_t **iteratorp) {
/*
* Create an iterator for version 'version' of 'db'.
*/
REQUIRE(DNS_DB_VALID(db));
REQUIRE(iteratorp != NULL && *iteratorp == NULL);
REQUIRE((flags & (DNS_DB_NSEC3ONLY | DNS_DB_NONSEC3)) !=
(DNS_DB_NSEC3ONLY | DNS_DB_NONSEC3));
1999-03-08 14:00:55 -05:00
if (db->methods->createiterator != NULL) {
return db->methods->createiterator(db, flags, iteratorp);
}
return ISC_R_NOTIMPLEMENTED;
1999-03-08 14:00:55 -05:00
}
/***
*** Rdataset Methods
***/
isc_result_t
dns__db_findrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
dns_rdatatype_t type, dns_rdatatype_t covers,
isc_stdtime_t now, dns_rdataset_t *rdataset,
dns_rdataset_t *sigrdataset DNS__DB_FLARG) {
1999-01-28 18:53:03 -05:00
REQUIRE(DNS_DB_VALID(db));
REQUIRE(node != NULL);
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(!dns_rdataset_isassociated(rdataset));
REQUIRE(covers == 0 || type == dns_rdatatype_rrsig);
1999-08-31 18:14:06 -04:00
REQUIRE(type != dns_rdatatype_any);
1999-09-07 21:10:08 -04:00
REQUIRE(sigrdataset == NULL ||
(DNS_RDATASET_VALID(sigrdataset) &&
!dns_rdataset_isassociated(sigrdataset)));
1999-01-28 18:53:03 -05:00
return (db->methods->findrdataset)(db, node, version, type, covers, now,
rdataset,
sigrdataset DNS__DB_FLARG_PASS);
1999-01-28 18:53:03 -05:00
}
isc_result_t
dns__db_allrdatasets(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
unsigned int options, isc_stdtime_t now,
dns_rdatasetiter_t **iteratorp DNS__DB_FLARG) {
1999-03-11 01:00:22 -05:00
/*
* Make '*iteratorp' an rdataset iteratator for all rdatasets at
* 'node' in version 'version' of 'db'.
*/
REQUIRE(DNS_DB_VALID(db));
REQUIRE(iteratorp != NULL && *iteratorp == NULL);
return (db->methods->allrdatasets)(db, node, version, options, now,
iteratorp DNS__DB_FLARG_PASS);
1999-03-11 01:00:22 -05:00
}
isc_result_t
dns__db_addrdataset(dns_db_t *db, dns_dbnode_t *node, dns_dbversion_t *version,
isc_stdtime_t now, dns_rdataset_t *rdataset,
unsigned int options,
dns_rdataset_t *addedrdataset DNS__DB_FLARG) {
/*
* Add 'rdataset' to 'node' in version 'version' of 'db'.
*/
1999-01-28 18:53:03 -05:00
REQUIRE(DNS_DB_VALID(db));
REQUIRE(node != NULL);
REQUIRE(((db->attributes & DNS_DBATTR_CACHE) == 0 && version != NULL) ||
((db->attributes & DNS_DBATTR_CACHE) != 0 && version == NULL &&
2000-01-25 14:26:45 -05:00
(options & DNS_DBADD_MERGE) == 0));
REQUIRE((options & DNS_DBADD_EXACT) == 0 ||
(options & DNS_DBADD_MERGE) != 0);
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(dns_rdataset_isassociated(rdataset));
REQUIRE(rdataset->rdclass == db->rdclass);
REQUIRE(addedrdataset == NULL ||
(DNS_RDATASET_VALID(addedrdataset) &&
!dns_rdataset_isassociated(addedrdataset)));
1999-01-28 18:53:03 -05:00
if (db->methods->addrdataset != NULL) {
return (db->methods->addrdataset)(
db, node, version, now, rdataset, options,
addedrdataset DNS__DB_FLARG_PASS);
}
return ISC_R_NOTIMPLEMENTED;
}
isc_result_t
dns__db_subtractrdataset(dns_db_t *db, dns_dbnode_t *node,
dns_dbversion_t *version, dns_rdataset_t *rdataset,
unsigned int options,
dns_rdataset_t *newrdataset DNS__DB_FLARG) {
/*
* Remove any rdata in 'rdataset' from 'node' in version 'version' of
* 'db'.
*/
REQUIRE(DNS_DB_VALID(db));
REQUIRE(node != NULL);
1999-06-16 20:30:46 -04:00
REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0 && version != NULL);
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(dns_rdataset_isassociated(rdataset));
REQUIRE(rdataset->rdclass == db->rdclass);
REQUIRE(newrdataset == NULL ||
(DNS_RDATASET_VALID(newrdataset) &&
!dns_rdataset_isassociated(newrdataset)));
if (db->methods->subtractrdataset != NULL) {
return (db->methods->subtractrdataset)(
db, node, version, rdataset, options,
newrdataset DNS__DB_FLARG_PASS);
}
return ISC_R_NOTIMPLEMENTED;
1999-01-28 18:53:03 -05:00
}
isc_result_t
dns__db_deleterdataset(dns_db_t *db, dns_dbnode_t *node,
dns_dbversion_t *version, dns_rdatatype_t type,
dns_rdatatype_t covers DNS__DB_FLARG) {
/*
* Make it so that no rdataset of type 'type' exists at 'node' in
* version version 'version' of 'db'.
*/
1999-01-28 18:53:03 -05:00
REQUIRE(DNS_DB_VALID(db));
REQUIRE(node != NULL);
REQUIRE(((db->attributes & DNS_DBATTR_CACHE) == 0 && version != NULL) ||
((db->attributes & DNS_DBATTR_CACHE) != 0 && version == NULL));
1999-01-28 18:53:03 -05:00
if (db->methods->deleterdataset != NULL) {
return (db->methods->deleterdataset)(db, node, version, type,
covers DNS__DB_FLARG_PASS);
}
return ISC_R_NOTIMPLEMENTED;
1999-01-28 18:53:03 -05:00
}
isc_result_t
dns_db_getsoaserial(dns_db_t *db, dns_dbversion_t *ver, uint32_t *serialp) {
isc_result_t result;
dns_dbnode_t *node = NULL;
dns_rdataset_t rdataset;
dns_rdata_t rdata = DNS_RDATA_INIT;
isc_buffer_t buffer;
REQUIRE(dns_db_iszone(db) || dns_db_isstub(db));
result = dns_db_findnode(db, dns_db_origin(db), false, &node);
if (result != ISC_R_SUCCESS) {
return result;
}
dns_rdataset_init(&rdataset);
result = dns_db_findrdataset(db, node, ver, dns_rdatatype_soa, 0,
(isc_stdtime_t)0, &rdataset, NULL);
2008-04-01 19:47:10 -04:00
if (result != ISC_R_SUCCESS) {
goto freenode;
}
result = dns_rdataset_first(&rdataset);
2008-04-01 19:47:10 -04:00
if (result != ISC_R_SUCCESS) {
goto freerdataset;
}
dns_rdataset_current(&rdataset, &rdata);
result = dns_rdataset_next(&rdataset);
INSIST(result == ISC_R_NOMORE);
INSIST(rdata.length > 20);
isc_buffer_init(&buffer, rdata.data, rdata.length);
isc_buffer_add(&buffer, rdata.length);
isc_buffer_forward(&buffer, rdata.length - 20);
*serialp = isc_buffer_getuint32(&buffer);
result = ISC_R_SUCCESS;
freerdataset:
dns_rdataset_disassociate(&rdataset);
freenode:
dns_db_detachnode(db, &node);
return result;
}
unsigned int
dns_db_nodecount(dns_db_t *db, dns_dbtree_t tree) {
REQUIRE(DNS_DB_VALID(db));
if (db->methods->nodecount != NULL) {
return (db->methods->nodecount)(db, tree);
}
return 0;
}
2015-12-09 08:37:20 -05:00
size_t
dns_db_hashsize(dns_db_t *db) {
REQUIRE(DNS_DB_VALID(db));
if (db->methods->hashsize == NULL) {
2015-12-09 08:37:20 -05:00
return 0;
}
return (db->methods->hashsize)(db);
}
void
dns_db_setloop(dns_db_t *db, isc_loop_t *loop) {
REQUIRE(DNS_DB_VALID(db));
if (db->methods->setloop != NULL) {
(db->methods->setloop)(db, loop);
}
}
isc_result_t
dns_db_register(const char *name, dns_dbcreatefunc_t create, void *driverarg,
isc_mem_t *mctx, dns_dbimplementation_t **dbimp) {
dns_dbimplementation_t *imp;
REQUIRE(name != NULL);
REQUIRE(dbimp != NULL && *dbimp == NULL);
RWLOCK(&implock, isc_rwlocktype_write);
imp = impfind(name);
if (imp != NULL) {
RWUNLOCK(&implock, isc_rwlocktype_write);
return ISC_R_EXISTS;
}
2008-04-01 19:47:10 -04:00
imp = isc_mem_get(mctx, sizeof(dns_dbimplementation_t));
imp->name = name;
imp->create = create;
imp->mctx = NULL;
imp->driverarg = driverarg;
isc_mem_attach(mctx, &imp->mctx);
ISC_LINK_INIT(imp, link);
ISC_LIST_APPEND(implementations, imp, link);
RWUNLOCK(&implock, isc_rwlocktype_write);
*dbimp = imp;
return ISC_R_SUCCESS;
}
void
dns_db_unregister(dns_dbimplementation_t **dbimp) {
dns_dbimplementation_t *imp;
REQUIRE(dbimp != NULL && *dbimp != NULL);
imp = *dbimp;
*dbimp = NULL;
RWLOCK(&implock, isc_rwlocktype_write);
ISC_LIST_UNLINK(implementations, imp, link);
isc_mem_putanddetach(&imp->mctx, imp, sizeof(dns_dbimplementation_t));
RWUNLOCK(&implock, isc_rwlocktype_write);
ENSURE(*dbimp == NULL);
}
isc_result_t
dns__db_getoriginnode(dns_db_t *db, dns_dbnode_t **nodep DNS__DB_FLARG) {
REQUIRE(DNS_DB_VALID(db));
REQUIRE(dns_db_iszone(db));
REQUIRE(nodep != NULL && *nodep == NULL);
if (db->methods->getoriginnode != NULL) {
return (db->methods->getoriginnode)(db,
nodep DNS__DB_FLARG_PASS);
}
return ISC_R_NOTFOUND;
}
dns_stats_t *
dns_db_getrrsetstats(dns_db_t *db) {
REQUIRE(DNS_DB_VALID(db));
if (db->methods->getrrsetstats != NULL) {
return (db->methods->getrrsetstats)(db);
}
return NULL;
}
isc_result_t
dns_db_setcachestats(dns_db_t *db, isc_stats_t *stats) {
REQUIRE(DNS_DB_VALID(db));
if (db->methods->setcachestats != NULL) {
return (db->methods->setcachestats)(db, stats);
}
return ISC_R_NOTIMPLEMENTED;
}
isc_result_t
dns_db_getnsec3parameters(dns_db_t *db, dns_dbversion_t *version,
dns_hash_t *hash, uint8_t *flags,
uint16_t *iterations, unsigned char *salt,
size_t *salt_length) {
REQUIRE(DNS_DB_VALID(db));
REQUIRE(dns_db_iszone(db));
if (db->methods->getnsec3parameters != NULL) {
return (db->methods->getnsec3parameters)(db, version, hash,
flags, iterations,
salt, salt_length);
}
return ISC_R_NOTFOUND;
}
isc_result_t
dns_db_getsize(dns_db_t *db, dns_dbversion_t *version, uint64_t *records,
uint64_t *bytes) {
REQUIRE(DNS_DB_VALID(db));
REQUIRE(dns_db_iszone(db));
if (db->methods->getsize != NULL) {
return (db->methods->getsize)(db, version, records, bytes);
}
return ISC_R_NOTFOUND;
}
isc_result_t
dns_db_setsigningtime(dns_db_t *db, dns_rdataset_t *rdataset,
isc_stdtime_t resign) {
if (db->methods->setsigningtime != NULL) {
return (db->methods->setsigningtime)(db, rdataset, resign);
}
return ISC_R_NOTIMPLEMENTED;
}
isc_result_t
dns_db_getsigningtime(dns_db_t *db, isc_stdtime_t *resign, dns_name_t *name,
dns_typepair_t *typepair) {
if (db->methods->getsigningtime != NULL) {
return (db->methods->getsigningtime)(db, resign, name,
typepair);
}
return ISC_R_NOTFOUND;
}
static void
call_updatenotify(dns_db_t *db) {
rcu_read_lock();
struct cds_lfht *update_listeners =
rcu_dereference(db->update_listeners);
if (update_listeners != NULL) {
struct cds_lfht_iter iter;
dns_dbonupdatelistener_t *listener;
cds_lfht_for_each_entry(update_listeners, &iter, listener,
ht_node) {
if (!cds_lfht_is_node_deleted(&listener->ht_node)) {
listener->onupdate(db, listener->onupdate_arg);
}
}
}
rcu_read_unlock();
}
static void
updatenotify_free(struct rcu_head *rcu_head) {
dns_dbonupdatelistener_t *listener =
caa_container_of(rcu_head, dns_dbonupdatelistener_t, rcu_head);
isc_mem_putanddetach(&listener->mctx, listener, sizeof(*listener));
}
static int
updatenotify_match(struct cds_lfht_node *ht_node, const void *_key) {
const dns_dbonupdatelistener_t *listener =
caa_container_of(ht_node, dns_dbonupdatelistener_t, ht_node);
const dns_dbonupdatelistener_t *key = _key;
return listener->onupdate == key->onupdate &&
listener->onupdate_arg == key->onupdate_arg;
}
/*
* Attach a notify-on-update function the database
*/
void
dns_db_updatenotify_register(dns_db_t *db, dns_dbupdate_callback_t fn,
void *fn_arg) {
REQUIRE(db != NULL);
REQUIRE(fn != NULL);
dns_dbonupdatelistener_t key = { .onupdate = fn,
.onupdate_arg = fn_arg };
uint32_t hash = isc_hash32(&key, sizeof(key), true);
dns_dbonupdatelistener_t *listener = isc_mem_get(db->mctx,
sizeof(*listener));
*listener = key;
isc_mem_attach(db->mctx, &listener->mctx);
rcu_read_lock();
struct cds_lfht *update_listeners =
rcu_dereference(db->update_listeners);
INSIST(update_listeners != NULL);
struct cds_lfht_node *ht_node =
cds_lfht_add_unique(update_listeners, hash, updatenotify_match,
&key, &listener->ht_node);
rcu_read_unlock();
if (ht_node != &listener->ht_node) {
updatenotify_free(&listener->rcu_head);
}
}
void
dns_db_updatenotify_unregister(dns_db_t *db, dns_dbupdate_callback_t fn,
void *fn_arg) {
REQUIRE(db != NULL);
dns_dbonupdatelistener_t key = { .onupdate = fn,
.onupdate_arg = fn_arg };
uint32_t hash = isc_hash32(&key, sizeof(key), true);
struct cds_lfht_iter iter;
rcu_read_lock();
struct cds_lfht *update_listeners =
rcu_dereference(db->update_listeners);
INSIST(update_listeners != NULL);
cds_lfht_lookup(update_listeners, hash, updatenotify_match, &key,
&iter);
struct cds_lfht_node *ht_node = cds_lfht_iter_get_node(&iter);
if (ht_node != NULL && !cds_lfht_del(update_listeners, ht_node)) {
dns_dbonupdatelistener_t *listener = caa_container_of(
ht_node, dns_dbonupdatelistener_t, ht_node);
call_rcu(&listener->rcu_head, updatenotify_free);
}
rcu_read_unlock();
}
isc_result_t
dns_db_setservestalettl(dns_db_t *db, dns_ttl_t ttl) {
REQUIRE(DNS_DB_VALID(db));
REQUIRE((db->attributes & DNS_DBATTR_CACHE) != 0);
if (db->methods->setservestalettl != NULL) {
return (db->methods->setservestalettl)(db, ttl);
}
return ISC_R_NOTIMPLEMENTED;
}
isc_result_t
dns_db_getservestalettl(dns_db_t *db, dns_ttl_t *ttl) {
REQUIRE(DNS_DB_VALID(db));
REQUIRE((db->attributes & DNS_DBATTR_CACHE) != 0);
if (db->methods->getservestalettl != NULL) {
return (db->methods->getservestalettl)(db, ttl);
}
return ISC_R_NOTIMPLEMENTED;
}
Add stale-refresh-time option Before this update, BIND would attempt to do a full recursive resolution process for each query received if the requested rrset had its ttl expired. If the resolution fails for any reason, only then BIND would check for stale rrset in cache (if 'stale-cache-enable' and 'stale-answer-enable' is on). The problem with this approach is that if an authoritative server is unreachable or is failing to respond, it is very unlikely that the problem will be fixed in the next seconds. A better approach to improve performance in those cases, is to mark the moment in which a resolution failed, and if new queries arrive for that same rrset, try to respond directly from the stale cache, and do that for a window of time configured via 'stale-refresh-time'. Only when this interval expires we then try to do a normal refresh of the rrset. The logic behind this commit is as following: - In query.c / query_gotanswer(), if the test of 'result' variable falls to the default case, an error is assumed to have happened, and a call to 'query_usestale()' is made to check if serving of stale rrset is enabled in configuration. - If serving of stale answers is enabled, a flag will be turned on in the query context to look for stale records: query.c:6839 qctx->client->query.dboptions |= DNS_DBFIND_STALEOK; - A call to query_lookup() will be made again, inside it a call to 'dns_db_findext()' is made, which in turn will invoke rbdb.c / cache_find(). - In rbtdb.c / cache_find() the important bits of this change is the call to 'check_stale_header()', which is a function that yields true if we should skip the stale entry, or false if we should consider it. - In check_stale_header() we now check if the DNS_DBFIND_STALEOK option is set, if that is the case we know that this new search for stale records was made due to a failure in a normal resolution, so we keep track of the time in which the failured occured in rbtdb.c:4559: header->last_refresh_fail_ts = search->now; - In check_stale_header(), if DNS_DBFIND_STALEOK is not set, then we know this is a normal lookup, if the record is stale and the query time is between last failure time + stale-refresh-time window, then we return false so cache_find() knows it can consider this stale rrset entry to return as a response. The last additions are two new methods to the database interface: - setservestale_refresh - getservestale_refresh Those were added so rbtdb can be aware of the value set in configuration option, since in that level we have no access to the view object.
2020-10-19 16:02:03 -04:00
isc_result_t
dns_db_setservestalerefresh(dns_db_t *db, uint32_t interval) {
REQUIRE(DNS_DB_VALID(db));
REQUIRE((db->attributes & DNS_DBATTR_CACHE) != 0);
if (db->methods->setservestalerefresh != NULL) {
return (db->methods->setservestalerefresh)(db, interval);
}
return ISC_R_NOTIMPLEMENTED;
}
isc_result_t
dns_db_getservestalerefresh(dns_db_t *db, uint32_t *interval) {
REQUIRE(DNS_DB_VALID(db));
REQUIRE((db->attributes & DNS_DBATTR_CACHE) != 0);
if (db->methods->getservestalerefresh != NULL) {
return (db->methods->getservestalerefresh)(db, interval);
}
return ISC_R_NOTIMPLEMENTED;
}
isc_result_t
dns_db_setgluecachestats(dns_db_t *db, isc_stats_t *stats) {
REQUIRE(dns_db_iszone(db));
REQUIRE(stats != NULL);
if (db->methods->setgluecachestats != NULL) {
return (db->methods->setgluecachestats)(db, stats);
}
return ISC_R_NOTIMPLEMENTED;
}
isc_result_t
dns_db_addglue(dns_db_t *db, dns_dbversion_t *version, dns_rdataset_t *rdataset,
dns_message_t *msg) {
REQUIRE(DNS_DB_VALID(db));
REQUIRE((db->attributes & DNS_DBATTR_CACHE) == 0);
REQUIRE(DNS_RDATASET_VALID(rdataset));
REQUIRE(rdataset->methods != NULL);
REQUIRE(rdataset->type == dns_rdatatype_ns);
if (db->methods->addglue != NULL) {
(db->methods->addglue)(db, version, rdataset, msg);
return ISC_R_SUCCESS;
}
return ISC_R_NOTIMPLEMENTED;
}
void
dns_db_locknode(dns_db_t *db, dns_dbnode_t *node, isc_rwlocktype_t type) {
if (db->methods->locknode != NULL) {
(db->methods->locknode)(db, node, type);
}
}
void
dns_db_unlocknode(dns_db_t *db, dns_dbnode_t *node, isc_rwlocktype_t type) {
if (db->methods->unlocknode != NULL) {
(db->methods->unlocknode)(db, node, type);
}
}
void
dns_db_expiredata(dns_db_t *db, dns_dbnode_t *node, void *data) {
if (db->methods->expiredata != NULL) {
(db->methods->expiredata)(db, node, data);
}
}
void
dns_db_deletedata(dns_db_t *db, dns_dbnode_t *node, void *data) {
if (db->methods->deletedata != NULL) {
(db->methods->deletedata)(db, node, data);
}
}
isc_result_t
dns_db_nodefullname(dns_db_t *db, dns_dbnode_t *node, dns_name_t *name) {
REQUIRE(db != NULL);
REQUIRE(node != NULL);
REQUIRE(name != NULL);
if (db->methods->nodefullname != NULL) {
return (db->methods->nodefullname)(db, node, name);
}
return ISC_R_NOTIMPLEMENTED;
}
void
dns_db_setmaxrrperset(dns_db_t *db, uint32_t value) {
REQUIRE(DNS_DB_VALID(db));
if (db->methods->setmaxrrperset != NULL) {
(db->methods->setmaxrrperset)(db, value);
}
}
void
dns_db_setmaxtypepername(dns_db_t *db, uint32_t value) {
REQUIRE(DNS_DB_VALID(db));
if (db->methods->setmaxtypepername != NULL) {
(db->methods->setmaxtypepername)(db, value);
}
}
void
dns__db_logtoomanyrecords(dns_db_t *db, const dns_name_t *name,
dns_rdatatype_t type, const char *op,
uint32_t limit) {
char namebuf[DNS_NAME_FORMATSIZE];
char originbuf[DNS_NAME_FORMATSIZE];
char typebuf[DNS_RDATATYPE_FORMATSIZE];
char clsbuf[DNS_RDATACLASS_FORMATSIZE];
dns_name_format(name, namebuf, sizeof(namebuf));
dns_name_format(&db->origin, originbuf, sizeof(originbuf));
dns_rdatatype_format(type, typebuf, sizeof(typebuf));
dns_rdataclass_format(db->rdclass, clsbuf, sizeof(clsbuf));
isc_log_write(
DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_DB, ISC_LOG_ERROR,
"error %s '%s/%s' in '%s/%s' (%s): %s (must not exceed %u)", op,
namebuf, typebuf, originbuf, clsbuf,
(db->attributes & DNS_DBATTR_CACHE) != 0 ? "cache" : "zone",
isc_result_totext(DNS_R_TOOMANYRECORDS), limit);
}
isc_result_t
dns_db_getzoneversion(dns_db_t *db, isc_buffer_t *b) {
REQUIRE(db != NULL);
REQUIRE(b != NULL);
if (db->methods->getzoneversion != NULL) {
return (db->methods->getzoneversion)(db, b);
}
return ISC_R_NOTIMPLEMENTED;
}