The dns_adb unit has been refactored to be much simpler. Following changes have been made: 1. Simplify the ADB to always allow GLUE and hints There were only two places where dns_adb_createfind() was used - in the dns_resolver unit where hints and GLUE addresses were ok, and in the dns_zone where dns_adb_createfind() would be called without DNS_ADBFIND_HINTOK and DNS_ADBFIND_GLUEOK set. Simplify the logic by allowing hint and GLUE addresses when looking up the nameserver addresses to notify. The difference is negligible and would cause a difference in the notified addresses only when there's mismatch between the parent and child addresses and we haven't cached the child addresses yet. 2. Drop the namebuckets and entrybuckets Formerly, the namebuckets and entrybuckets were used to reduced the lock contention when accessing the double-linked lists stored in each bucket. In the previous refactoring, the custom hashtable for the buckets has been replaced with isc_ht/isc_hashmap, so only a single item (mostly, see below) would end up in each bucket. Removing the entrybuckets has been straightforward, the only matching was done on the isc_sockaddr_t member of the dns_adbentry. Removing the zonebuckets required GLUEOK and HINTOK bits to be removed because the find could match entries with-or-without the bits set, and creating a custom key that stores the DNS_ADBFIND_STARTATZONE in the first byte of the key, so we can do a straightforward lookup into the hashtable without traversing a list that contains items with different flags. 3. Remove unassociated entries from ADB database Previously, the adbentries could live in the ADB database even after unlinking them from dns_adbnames. Such entries would show up as "Unassociated entries" in the ADB dump. The benefit of keeping such entries is little - the chance that we link such entry to a adbname is small, and it's simpler to evict unlinked entries from the ADB cache (and the hashtable) than create second LRU cleaning mechanism. Unlinked ADB entries are now directly deleted from the hash table (hashmap) upon destruction. 4. Cleanup expired entries from the hash table When buckets were still in place, the code would keep the buckets always allocated and never shrink the hash table (hashmap). With proper reference counting in place, we can delete the adbnames from the hash table and the LRU list. 5. Stop purging the names early when we hit the time limit Because the LRU list is now time ordered, we can stop purging the names when we find a first entry that doesn't fullfil our time-based eviction criteria because no further entry on the LRU list will meet the criteria. Future work: 1. Lock contention In this commit, the focus was on correctness of the data structure, but in the future, the lock contention in the ADB database needs to be addressed. Currently, we use simple mutex to lock the hash tables, because we almost always need to use a write lock for properly purging the hashtables. The ADB database needs to be sharded (similar to the effect that buckets had in the past). Each shard would contain own hashmap and own LRU list. 2. Time-based purging The ADB names and entries stay intact when there are no lookups. When we add separate shards, a timer needs to be added for time-based cleaning in case there's no traffic hashing to the inactive shard. 3. Revisit the 30 minutes limit The ADB cache is capped at 30 minutes. This needs to be revisited, and at least the limit should be configurable (in both directions).
245 lines
10 KiB
C
245 lines
10 KiB
C
/*
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
|
*
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
*
|
|
* 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/.
|
|
*
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
* information regarding copyright ownership.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <isc/assertions.h>
|
|
#include <isc/atomic.h>
|
|
#include <isc/error.h>
|
|
#include <isc/lang.h>
|
|
#include <isc/mutex.h>
|
|
#include <isc/types.h>
|
|
|
|
/*! \file isc/refcount.h
|
|
* \brief Implements a locked reference counter.
|
|
*
|
|
* These macros uses C11(-like) atomic functions to implement reference
|
|
* counting. The isc_refcount_t type must not be accessed directly.
|
|
*/
|
|
|
|
ISC_LANG_BEGINDECLS
|
|
|
|
typedef atomic_uint_fast32_t isc_refcount_t;
|
|
|
|
/** \def isc_refcount_init(ref, n)
|
|
* \brief Initialize the reference counter.
|
|
* \param[in] ref pointer to reference counter.
|
|
* \param[in] n an initial number of references.
|
|
* \return nothing.
|
|
*
|
|
* \warning No memory barrier are being imposed here.
|
|
*/
|
|
#define isc_refcount_init(target, value) atomic_init(target, value)
|
|
|
|
/** \def isc_refcount_current(ref)
|
|
* \brief Returns current number of references.
|
|
* \param[in] ref pointer to reference counter.
|
|
* \returns current value of reference counter.
|
|
*
|
|
* Undo implicit promotion to 64 bits in our Windows implementation of
|
|
* atomic_load_explicit() by casting to uint_fast32_t.
|
|
*/
|
|
|
|
#define isc_refcount_current(target) (uint_fast32_t) atomic_load_acquire(target)
|
|
|
|
/** \def isc_refcount_destroy(ref)
|
|
* \brief a destructor that makes sure that all references were cleared.
|
|
* \param[in] ref pointer to reference counter.
|
|
* \returns nothing.
|
|
*/
|
|
#define isc_refcount_destroy(target) \
|
|
ISC_REQUIRE(isc_refcount_current(target) == 0)
|
|
|
|
/** \def isc_refcount_increment0(ref)
|
|
* \brief increases reference counter by 1.
|
|
* \param[in] ref pointer to reference counter.
|
|
* \returns previous value of reference counter.
|
|
*/
|
|
#if _MSC_VER
|
|
static inline uint_fast32_t
|
|
isc_refcount_increment0(isc_refcount_t *target) {
|
|
uint_fast32_t __v;
|
|
__v = (uint_fast32_t)atomic_fetch_add_relaxed(target, 1);
|
|
INSIST(__v < UINT32_MAX);
|
|
return (__v);
|
|
}
|
|
#else /* _MSC_VER */
|
|
#define isc_refcount_increment0(target) \
|
|
({ \
|
|
uint_fast32_t __v; \
|
|
__v = atomic_fetch_add_relaxed(target, 1); \
|
|
INSIST(__v < UINT32_MAX); \
|
|
__v; \
|
|
})
|
|
#endif /* _MSC_VER */
|
|
|
|
/** \def isc_refcount_increment(ref)
|
|
* \brief increases reference counter by 1.
|
|
* \param[in] ref pointer to reference counter.
|
|
* \returns previous value of reference counter.
|
|
*/
|
|
#if _MSC_VER
|
|
static inline uint_fast32_t
|
|
isc_refcount_increment(isc_refcount_t *target) {
|
|
uint_fast32_t __v;
|
|
__v = (uint_fast32_t)atomic_fetch_add_relaxed(target, 1);
|
|
INSIST(__v > 0 && __v < UINT32_MAX);
|
|
return (__v);
|
|
}
|
|
#else /* _MSC_VER */
|
|
#define isc_refcount_increment(target) \
|
|
({ \
|
|
uint_fast32_t __v; \
|
|
__v = atomic_fetch_add_relaxed(target, 1); \
|
|
INSIST(__v > 0 && __v < UINT32_MAX); \
|
|
__v; \
|
|
})
|
|
#endif /* _MSC_VER */
|
|
|
|
/** \def isc_refcount_decrement(ref)
|
|
* \brief decreases reference counter by 1.
|
|
* \param[in] ref pointer to reference counter.
|
|
* \returns previous value of reference counter.
|
|
*/
|
|
#if _MSC_VER
|
|
static inline uint_fast32_t
|
|
isc_refcount_decrement(isc_refcount_t *target) {
|
|
uint_fast32_t __v;
|
|
__v = (uint_fast32_t)atomic_fetch_sub_acq_rel(target, 1);
|
|
INSIST(__v > 0);
|
|
return (__v);
|
|
}
|
|
#else /* _MSC_VER */
|
|
#define isc_refcount_decrement(target) \
|
|
({ \
|
|
uint_fast32_t __v; \
|
|
__v = atomic_fetch_sub_acq_rel(target, 1); \
|
|
INSIST(__v > 0); \
|
|
__v; \
|
|
})
|
|
#endif /* _MSC_VER */
|
|
|
|
#define isc_refcount_decrementz(target) \
|
|
do { \
|
|
uint_fast32_t _refs = isc_refcount_decrement(target); \
|
|
ISC_INSIST(_refs == 1); \
|
|
} while (0)
|
|
|
|
#define isc_refcount_decrement1(target) \
|
|
do { \
|
|
uint_fast32_t _refs = isc_refcount_decrement(target); \
|
|
ISC_INSIST(_refs > 1); \
|
|
} while (0)
|
|
|
|
#define isc_refcount_decrement0(target) \
|
|
do { \
|
|
uint_fast32_t _refs = isc_refcount_decrement(target); \
|
|
ISC_INSIST(_refs > 0); \
|
|
} while (0)
|
|
|
|
#define ISC_REFCOUNT_TRACE_DECL(name) \
|
|
name##_t *name##__ref(name##_t *ptr, const char *func, \
|
|
const char *file, unsigned int line); \
|
|
void name##__unref(name##_t *ptr, const char *func, const char *file, \
|
|
unsigned int line); \
|
|
void name##__attach(name##_t *ptr, name##_t **ptrp, const char *func, \
|
|
const char *file, unsigned int line); \
|
|
void name##__detach(name##_t **ptrp, const char *func, \
|
|
const char *file, unsigned int line)
|
|
|
|
#define ISC_REFCOUNT_TRACE_IMPL(name, destroy) \
|
|
name##_t *name##__ref(name##_t *ptr, const char *func, \
|
|
const char *file, unsigned int line) { \
|
|
REQUIRE(ptr != NULL); \
|
|
uint_fast32_t refs = \
|
|
isc_refcount_increment(&ptr->references) + 1; \
|
|
fprintf(stderr, \
|
|
"%s:%s:%s:%u:%p->references = %" PRIuFAST32 "\n", \
|
|
__func__, func, file, line, ptr, refs); \
|
|
return (ptr); \
|
|
} \
|
|
\
|
|
void name##__unref(name##_t *ptr, const char *func, const char *file, \
|
|
unsigned int line) { \
|
|
REQUIRE(ptr != NULL); \
|
|
uint_fast32_t refs = \
|
|
isc_refcount_decrement(&ptr->references) - 1; \
|
|
if (refs == 0) { \
|
|
destroy(ptr); \
|
|
} \
|
|
fprintf(stderr, \
|
|
"%s:%s:%s:%u:%p->references = %" PRIuFAST32 "\n", \
|
|
__func__, func, file, line, ptr, refs); \
|
|
} \
|
|
void name##__attach(name##_t *ptr, name##_t **ptrp, const char *func, \
|
|
const char *file, unsigned int line) { \
|
|
REQUIRE(ptrp != NULL && *ptrp == NULL); \
|
|
uint_fast32_t refs = \
|
|
isc_refcount_increment(&ptr->references) + 1; \
|
|
fprintf(stderr, \
|
|
"%s:%s:%s:%u:%p->references = %" PRIuFAST32 "\n", \
|
|
__func__, func, file, line, ptr, refs); \
|
|
*ptrp = ptr; \
|
|
} \
|
|
\
|
|
void name##__detach(name##_t **ptrp, const char *func, \
|
|
const char *file, unsigned int line) { \
|
|
REQUIRE(ptrp != NULL && *ptrp != NULL); \
|
|
name##_t *ptr = *ptrp; \
|
|
*ptrp = NULL; \
|
|
uint_fast32_t refs = \
|
|
isc_refcount_decrement(&ptr->references) - 1; \
|
|
if (refs == 0) { \
|
|
destroy(ptr); \
|
|
} \
|
|
fprintf(stderr, \
|
|
"%s:%s:%s:%u:%p->references = %" PRIuFAST32 "\n", \
|
|
__func__, func, file, line, ptr, refs); \
|
|
}
|
|
|
|
#define ISC_REFCOUNT_DECL(name) \
|
|
name##_t *name##_ref(name##_t *ptr); \
|
|
void name##_unref(name##_t *ptr); \
|
|
void name##_attach(name##_t *ptr, name##_t **ptrp); \
|
|
void name##_detach(name##_t **ptrp)
|
|
|
|
#define ISC_REFCOUNT_IMPL(name, destroy) \
|
|
name##_t *name##_ref(name##_t *ptr) { \
|
|
REQUIRE(ptr != NULL); \
|
|
isc_refcount_increment(&ptr->references); \
|
|
return (ptr); \
|
|
} \
|
|
\
|
|
void name##_unref(name##_t *ptr) { \
|
|
REQUIRE(ptr != NULL); \
|
|
if (isc_refcount_decrement(&ptr->references) == 1) { \
|
|
destroy(ptr); \
|
|
} \
|
|
} \
|
|
void name##_attach(name##_t *ptr, name##_t **ptrp) { \
|
|
REQUIRE(ptrp != NULL && *ptrp == NULL); \
|
|
name##_ref(ptr); \
|
|
*ptrp = ptr; \
|
|
} \
|
|
\
|
|
void name##_detach(name##_t **ptrp) { \
|
|
REQUIRE(ptrp != NULL && *ptrp != NULL); \
|
|
name##_t *ptr = *ptrp; \
|
|
*ptrp = NULL; \
|
|
name##_unref(ptr); \
|
|
}
|
|
|
|
ISC_LANG_ENDDECLS
|