Home | History | Annotate | Line # | Download | only in dns
badcache.c revision 1.5
      1  1.1  christos /*	$NetBSD: badcache.c,v 1.5 2021/02/19 16:42:15 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  1.1  christos  *
      6  1.1  christos  * This Source Code Form is subject to the terms of the Mozilla Public
      7  1.1  christos  * License, v. 2.0. If a copy of the MPL was not distributed with this
      8  1.5  christos  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
      9  1.1  christos  *
     10  1.1  christos  * See the COPYRIGHT file distributed with this work for additional
     11  1.1  christos  * information regarding copyright ownership.
     12  1.1  christos  */
     13  1.1  christos 
     14  1.1  christos /*! \file */
     15  1.1  christos 
     16  1.3  christos #include <inttypes.h>
     17  1.3  christos #include <stdbool.h>
     18  1.3  christos 
     19  1.1  christos #include <isc/buffer.h>
     20  1.4  christos #include <isc/hash.h>
     21  1.1  christos #include <isc/log.h>
     22  1.1  christos #include <isc/mem.h>
     23  1.1  christos #include <isc/mutex.h>
     24  1.1  christos #include <isc/platform.h>
     25  1.1  christos #include <isc/print.h>
     26  1.4  christos #include <isc/rwlock.h>
     27  1.1  christos #include <isc/string.h>
     28  1.1  christos #include <isc/time.h>
     29  1.1  christos #include <isc/util.h>
     30  1.1  christos 
     31  1.1  christos #include <dns/badcache.h>
     32  1.1  christos #include <dns/name.h>
     33  1.1  christos #include <dns/rdatatype.h>
     34  1.1  christos #include <dns/types.h>
     35  1.1  christos 
     36  1.1  christos typedef struct dns_bcentry dns_bcentry_t;
     37  1.1  christos 
     38  1.1  christos struct dns_badcache {
     39  1.4  christos 	unsigned int magic;
     40  1.4  christos 	isc_rwlock_t lock;
     41  1.4  christos 	isc_mem_t *mctx;
     42  1.4  christos 
     43  1.4  christos 	isc_mutex_t *tlocks;
     44  1.4  christos 	dns_bcentry_t **table;
     45  1.4  christos 
     46  1.4  christos 	atomic_uint_fast32_t count;
     47  1.4  christos 	atomic_uint_fast32_t sweep;
     48  1.4  christos 
     49  1.4  christos 	unsigned int minsize;
     50  1.4  christos 	unsigned int size;
     51  1.1  christos };
     52  1.1  christos 
     53  1.4  christos #define BADCACHE_MAGIC	  ISC_MAGIC('B', 'd', 'C', 'a')
     54  1.4  christos #define VALID_BADCACHE(m) ISC_MAGIC_VALID(m, BADCACHE_MAGIC)
     55  1.1  christos 
     56  1.1  christos struct dns_bcentry {
     57  1.4  christos 	dns_bcentry_t *next;
     58  1.4  christos 	dns_rdatatype_t type;
     59  1.4  christos 	isc_time_t expire;
     60  1.4  christos 	uint32_t flags;
     61  1.4  christos 	unsigned int hashval;
     62  1.4  christos 	dns_name_t name;
     63  1.1  christos };
     64  1.1  christos 
     65  1.4  christos static void
     66  1.4  christos badcache_resize(dns_badcache_t *bc, isc_time_t *now);
     67  1.1  christos 
     68  1.1  christos isc_result_t
     69  1.1  christos dns_badcache_init(isc_mem_t *mctx, unsigned int size, dns_badcache_t **bcp) {
     70  1.1  christos 	dns_badcache_t *bc = NULL;
     71  1.4  christos 	unsigned int i;
     72  1.1  christos 
     73  1.1  christos 	REQUIRE(bcp != NULL && *bcp == NULL);
     74  1.1  christos 	REQUIRE(mctx != NULL);
     75  1.1  christos 
     76  1.1  christos 	bc = isc_mem_get(mctx, sizeof(dns_badcache_t));
     77  1.1  christos 	memset(bc, 0, sizeof(dns_badcache_t));
     78  1.1  christos 
     79  1.1  christos 	isc_mem_attach(mctx, &bc->mctx);
     80  1.4  christos 	isc_rwlock_init(&bc->lock, 0, 0);
     81  1.1  christos 
     82  1.1  christos 	bc->table = isc_mem_get(bc->mctx, sizeof(*bc->table) * size);
     83  1.4  christos 	bc->tlocks = isc_mem_get(bc->mctx, sizeof(isc_mutex_t) * size);
     84  1.4  christos 	for (i = 0; i < size; i++) {
     85  1.4  christos 		isc_mutex_init(&bc->tlocks[i]);
     86  1.1  christos 	}
     87  1.1  christos 	bc->size = bc->minsize = size;
     88  1.1  christos 	memset(bc->table, 0, bc->size * sizeof(dns_bcentry_t *));
     89  1.1  christos 
     90  1.4  christos 	atomic_init(&bc->count, 0);
     91  1.4  christos 	atomic_init(&bc->sweep, 0);
     92  1.1  christos 	bc->magic = BADCACHE_MAGIC;
     93  1.1  christos 
     94  1.1  christos 	*bcp = bc;
     95  1.1  christos 	return (ISC_R_SUCCESS);
     96  1.1  christos }
     97  1.1  christos 
     98  1.1  christos void
     99  1.1  christos dns_badcache_destroy(dns_badcache_t **bcp) {
    100  1.1  christos 	dns_badcache_t *bc;
    101  1.4  christos 	unsigned int i;
    102  1.1  christos 
    103  1.1  christos 	REQUIRE(bcp != NULL && *bcp != NULL);
    104  1.1  christos 	bc = *bcp;
    105  1.4  christos 	*bcp = NULL;
    106  1.1  christos 
    107  1.1  christos 	dns_badcache_flush(bc);
    108  1.1  christos 
    109  1.1  christos 	bc->magic = 0;
    110  1.4  christos 	isc_rwlock_destroy(&bc->lock);
    111  1.4  christos 	for (i = 0; i < bc->size; i++) {
    112  1.4  christos 		isc_mutex_destroy(&bc->tlocks[i]);
    113  1.4  christos 	}
    114  1.1  christos 	isc_mem_put(bc->mctx, bc->table, sizeof(dns_bcentry_t *) * bc->size);
    115  1.4  christos 	isc_mem_put(bc->mctx, bc->tlocks, sizeof(isc_mutex_t) * bc->size);
    116  1.1  christos 	isc_mem_putanddetach(&bc->mctx, bc, sizeof(dns_badcache_t));
    117  1.1  christos }
    118  1.1  christos 
    119  1.4  christos static void
    120  1.4  christos badcache_resize(dns_badcache_t *bc, isc_time_t *now) {
    121  1.1  christos 	dns_bcentry_t **newtable, *bad, *next;
    122  1.4  christos 	isc_mutex_t *newlocks;
    123  1.1  christos 	unsigned int newsize, i;
    124  1.4  christos 	bool grow;
    125  1.4  christos 
    126  1.4  christos 	RWLOCK(&bc->lock, isc_rwlocktype_write);
    127  1.1  christos 
    128  1.4  christos 	/*
    129  1.4  christos 	 * XXXWPK we will have a thundering herd problem here,
    130  1.4  christos 	 * as all threads will wait on the RWLOCK when there's
    131  1.4  christos 	 * a need to resize badcache.
    132  1.4  christos 	 * However, it happens so rarely it should not be a
    133  1.4  christos 	 * performance issue. This is because we double the
    134  1.4  christos 	 * size every time we grow it, and we don't shrink
    135  1.4  christos 	 * unless the number of entries really shrunk. In a
    136  1.4  christos 	 * high load situation, the number of badcache entries
    137  1.4  christos 	 * will eventually stabilize.
    138  1.4  christos 	 */
    139  1.4  christos 	if (atomic_load_relaxed(&bc->count) > bc->size * 8) {
    140  1.4  christos 		grow = true;
    141  1.4  christos 	} else if (atomic_load_relaxed(&bc->count) < bc->size * 2 &&
    142  1.4  christos 		   bc->size > bc->minsize)
    143  1.4  christos 	{
    144  1.4  christos 		grow = false;
    145  1.4  christos 	} else {
    146  1.4  christos 		/* Someone resized it already, bail. */
    147  1.4  christos 		RWUNLOCK(&bc->lock, isc_rwlocktype_write);
    148  1.4  christos 		return;
    149  1.4  christos 	}
    150  1.4  christos 
    151  1.4  christos 	if (grow) {
    152  1.1  christos 		newsize = bc->size * 2 + 1;
    153  1.4  christos 	} else {
    154  1.1  christos 		newsize = (bc->size - 1) / 2;
    155  1.4  christos #ifdef __clang_analyzer__
    156  1.4  christos 		/*
    157  1.4  christos 		 * XXXWPK there's a bug in clang static analyzer -
    158  1.4  christos 		 * `value % newsize` is considered undefined even though
    159  1.4  christos 		 * we check if newsize is larger than 0. This helps.
    160  1.4  christos 		 */
    161  1.4  christos 		newsize += 1;
    162  1.4  christos #endif
    163  1.4  christos 	}
    164  1.4  christos 	RUNTIME_CHECK(newsize > 0);
    165  1.1  christos 
    166  1.1  christos 	newtable = isc_mem_get(bc->mctx, sizeof(dns_bcentry_t *) * newsize);
    167  1.1  christos 	memset(newtable, 0, sizeof(dns_bcentry_t *) * newsize);
    168  1.1  christos 
    169  1.4  christos 	newlocks = isc_mem_get(bc->mctx, sizeof(isc_mutex_t) * newsize);
    170  1.4  christos 
    171  1.4  christos 	/* Copy existing mutexes */
    172  1.4  christos 	for (i = 0; i < newsize && i < bc->size; i++) {
    173  1.4  christos 		newlocks[i] = bc->tlocks[i];
    174  1.4  christos 	}
    175  1.4  christos 	/* Initialize additional mutexes if we're growing */
    176  1.4  christos 	for (i = bc->size; i < newsize; i++) {
    177  1.4  christos 		isc_mutex_init(&newlocks[i]);
    178  1.4  christos 	}
    179  1.4  christos 	/* Destroy extra mutexes if we're shrinking */
    180  1.4  christos 	for (i = newsize; i < bc->size; i++) {
    181  1.4  christos 		isc_mutex_destroy(&bc->tlocks[i]);
    182  1.4  christos 	}
    183  1.4  christos 
    184  1.4  christos 	for (i = 0; atomic_load_relaxed(&bc->count) > 0 && i < bc->size; i++) {
    185  1.1  christos 		for (bad = bc->table[i]; bad != NULL; bad = next) {
    186  1.1  christos 			next = bad->next;
    187  1.1  christos 			if (isc_time_compare(&bad->expire, now) < 0) {
    188  1.1  christos 				isc_mem_put(bc->mctx, bad,
    189  1.1  christos 					    sizeof(*bad) + bad->name.length);
    190  1.4  christos 				atomic_fetch_sub_relaxed(&bc->count, 1);
    191  1.1  christos 			} else {
    192  1.1  christos 				bad->next = newtable[bad->hashval % newsize];
    193  1.1  christos 				newtable[bad->hashval % newsize] = bad;
    194  1.1  christos 			}
    195  1.1  christos 		}
    196  1.1  christos 		bc->table[i] = NULL;
    197  1.1  christos 	}
    198  1.1  christos 
    199  1.4  christos 	isc_mem_put(bc->mctx, bc->tlocks, sizeof(isc_mutex_t) * bc->size);
    200  1.4  christos 	bc->tlocks = newlocks;
    201  1.4  christos 
    202  1.1  christos 	isc_mem_put(bc->mctx, bc->table, sizeof(*bc->table) * bc->size);
    203  1.1  christos 	bc->size = newsize;
    204  1.1  christos 	bc->table = newtable;
    205  1.1  christos 
    206  1.4  christos 	RWUNLOCK(&bc->lock, isc_rwlocktype_write);
    207  1.1  christos }
    208  1.1  christos 
    209  1.1  christos void
    210  1.1  christos dns_badcache_add(dns_badcache_t *bc, const dns_name_t *name,
    211  1.4  christos 		 dns_rdatatype_t type, bool update, uint32_t flags,
    212  1.4  christos 		 isc_time_t *expire) {
    213  1.1  christos 	isc_result_t result;
    214  1.4  christos 	unsigned int hashval, hash;
    215  1.1  christos 	dns_bcentry_t *bad, *prev, *next;
    216  1.1  christos 	isc_time_t now;
    217  1.4  christos 	bool resize = false;
    218  1.1  christos 
    219  1.1  christos 	REQUIRE(VALID_BADCACHE(bc));
    220  1.1  christos 	REQUIRE(name != NULL);
    221  1.1  christos 	REQUIRE(expire != NULL);
    222  1.1  christos 
    223  1.4  christos 	RWLOCK(&bc->lock, isc_rwlocktype_read);
    224  1.1  christos 
    225  1.1  christos 	result = isc_time_now(&now);
    226  1.4  christos 	if (result != ISC_R_SUCCESS) {
    227  1.1  christos 		isc_time_settoepoch(&now);
    228  1.4  christos 	}
    229  1.1  christos 
    230  1.3  christos 	hashval = dns_name_hash(name, false);
    231  1.4  christos 	hash = hashval % bc->size;
    232  1.4  christos 	LOCK(&bc->tlocks[hash]);
    233  1.1  christos 	prev = NULL;
    234  1.4  christos 	for (bad = bc->table[hash]; bad != NULL; bad = next) {
    235  1.1  christos 		next = bad->next;
    236  1.1  christos 		if (bad->type == type && dns_name_equal(name, &bad->name)) {
    237  1.1  christos 			if (update) {
    238  1.1  christos 				bad->expire = *expire;
    239  1.1  christos 				bad->flags = flags;
    240  1.1  christos 			}
    241  1.1  christos 			break;
    242  1.1  christos 		}
    243  1.1  christos 		if (isc_time_compare(&bad->expire, &now) < 0) {
    244  1.4  christos 			if (prev == NULL) {
    245  1.4  christos 				bc->table[hash] = bad->next;
    246  1.4  christos 			} else {
    247  1.1  christos 				prev->next = bad->next;
    248  1.4  christos 			}
    249  1.1  christos 			isc_mem_put(bc->mctx, bad,
    250  1.1  christos 				    sizeof(*bad) + bad->name.length);
    251  1.4  christos 			atomic_fetch_sub_relaxed(&bc->count, 1);
    252  1.4  christos 		} else {
    253  1.1  christos 			prev = bad;
    254  1.4  christos 		}
    255  1.1  christos 	}
    256  1.1  christos 
    257  1.1  christos 	if (bad == NULL) {
    258  1.1  christos 		isc_buffer_t buffer;
    259  1.1  christos 		bad = isc_mem_get(bc->mctx, sizeof(*bad) + name->length);
    260  1.1  christos 		bad->type = type;
    261  1.1  christos 		bad->hashval = hashval;
    262  1.1  christos 		bad->expire = *expire;
    263  1.1  christos 		bad->flags = flags;
    264  1.1  christos 		isc_buffer_init(&buffer, bad + 1, name->length);
    265  1.1  christos 		dns_name_init(&bad->name, NULL);
    266  1.1  christos 		dns_name_copy(name, &bad->name, &buffer);
    267  1.4  christos 		bad->next = bc->table[hash];
    268  1.4  christos 		bc->table[hash] = bad;
    269  1.4  christos 		unsigned count = atomic_fetch_add_relaxed(&bc->count, 1);
    270  1.4  christos 		if ((count > bc->size * 8) ||
    271  1.4  christos 		    (count < bc->size * 2 && bc->size > bc->minsize)) {
    272  1.4  christos 			resize = true;
    273  1.4  christos 		}
    274  1.4  christos 	} else {
    275  1.1  christos 		bad->expire = *expire;
    276  1.4  christos 	}
    277  1.1  christos 
    278  1.4  christos 	UNLOCK(&bc->tlocks[hash]);
    279  1.4  christos 	RWUNLOCK(&bc->lock, isc_rwlocktype_read);
    280  1.4  christos 	if (resize) {
    281  1.4  christos 		badcache_resize(bc, &now);
    282  1.4  christos 	}
    283  1.1  christos }
    284  1.1  christos 
    285  1.3  christos bool
    286  1.1  christos dns_badcache_find(dns_badcache_t *bc, const dns_name_t *name,
    287  1.4  christos 		  dns_rdatatype_t type, uint32_t *flagp, isc_time_t *now) {
    288  1.1  christos 	dns_bcentry_t *bad, *prev, *next;
    289  1.3  christos 	bool answer = false;
    290  1.5  christos 	unsigned int i;
    291  1.5  christos 	unsigned int hash;
    292  1.1  christos 
    293  1.1  christos 	REQUIRE(VALID_BADCACHE(bc));
    294  1.1  christos 	REQUIRE(name != NULL);
    295  1.1  christos 	REQUIRE(now != NULL);
    296  1.1  christos 
    297  1.4  christos 	RWLOCK(&bc->lock, isc_rwlocktype_read);
    298  1.1  christos 
    299  1.1  christos 	/*
    300  1.1  christos 	 * XXXMUKS: dns_name_equal() is expensive as it does a
    301  1.1  christos 	 * octet-by-octet comparison, and it can be made better in two
    302  1.1  christos 	 * ways here. First, lowercase the names (use
    303  1.1  christos 	 * dns_name_downcase() instead of dns_name_copy() in
    304  1.1  christos 	 * dns_badcache_add()) so that dns_name_caseequal() can be used
    305  1.1  christos 	 * which the compiler will emit as SIMD instructions. Second,
    306  1.1  christos 	 * don't put multiple copies of the same name in the chain (or
    307  1.1  christos 	 * multiple names will have to be matched for equality), but use
    308  1.1  christos 	 * name->link to store the type specific part.
    309  1.1  christos 	 */
    310  1.1  christos 
    311  1.4  christos 	if (atomic_load_relaxed(&bc->count) == 0) {
    312  1.1  christos 		goto skip;
    313  1.4  christos 	}
    314  1.1  christos 
    315  1.4  christos 	hash = dns_name_hash(name, false) % bc->size;
    316  1.1  christos 	prev = NULL;
    317  1.4  christos 	LOCK(&bc->tlocks[hash]);
    318  1.4  christos 	for (bad = bc->table[hash]; bad != NULL; bad = next) {
    319  1.1  christos 		next = bad->next;
    320  1.1  christos 		/*
    321  1.1  christos 		 * Search the hash list. Clean out expired records as we go.
    322  1.1  christos 		 */
    323  1.1  christos 		if (isc_time_compare(&bad->expire, now) < 0) {
    324  1.4  christos 			if (prev != NULL) {
    325  1.1  christos 				prev->next = bad->next;
    326  1.4  christos 			} else {
    327  1.4  christos 				bc->table[hash] = bad->next;
    328  1.4  christos 			}
    329  1.1  christos 
    330  1.4  christos 			isc_mem_put(bc->mctx, bad,
    331  1.4  christos 				    sizeof(*bad) + bad->name.length);
    332  1.4  christos 			atomic_fetch_sub(&bc->count, 1);
    333  1.1  christos 			continue;
    334  1.1  christos 		}
    335  1.1  christos 		if (bad->type == type && dns_name_equal(name, &bad->name)) {
    336  1.4  christos 			if (flagp != NULL) {
    337  1.1  christos 				*flagp = bad->flags;
    338  1.4  christos 			}
    339  1.3  christos 			answer = true;
    340  1.1  christos 			break;
    341  1.1  christos 		}
    342  1.1  christos 		prev = bad;
    343  1.1  christos 	}
    344  1.4  christos 	UNLOCK(&bc->tlocks[hash]);
    345  1.4  christos skip:
    346  1.1  christos 
    347  1.1  christos 	/*
    348  1.1  christos 	 * Slow sweep to clean out stale records.
    349  1.1  christos 	 */
    350  1.4  christos 	i = atomic_fetch_add(&bc->sweep, 1) % bc->size;
    351  1.4  christos 	if (isc_mutex_trylock(&bc->tlocks[i]) == ISC_R_SUCCESS) {
    352  1.4  christos 		bad = bc->table[i];
    353  1.4  christos 		if (bad != NULL && isc_time_compare(&bad->expire, now) < 0) {
    354  1.4  christos 			bc->table[i] = bad->next;
    355  1.4  christos 			isc_mem_put(bc->mctx, bad,
    356  1.4  christos 				    sizeof(*bad) + bad->name.length);
    357  1.4  christos 			atomic_fetch_sub_relaxed(&bc->count, 1);
    358  1.4  christos 		}
    359  1.4  christos 		UNLOCK(&bc->tlocks[i]);
    360  1.1  christos 	}
    361  1.1  christos 
    362  1.4  christos 	RWUNLOCK(&bc->lock, isc_rwlocktype_read);
    363  1.1  christos 	return (answer);
    364  1.1  christos }
    365  1.1  christos 
    366  1.1  christos void
    367  1.1  christos dns_badcache_flush(dns_badcache_t *bc) {
    368  1.1  christos 	dns_bcentry_t *entry, *next;
    369  1.1  christos 	unsigned int i;
    370  1.1  christos 
    371  1.4  christos 	RWLOCK(&bc->lock, isc_rwlocktype_write);
    372  1.1  christos 	REQUIRE(VALID_BADCACHE(bc));
    373  1.1  christos 
    374  1.4  christos 	for (i = 0; atomic_load_relaxed(&bc->count) > 0 && i < bc->size; i++) {
    375  1.1  christos 		for (entry = bc->table[i]; entry != NULL; entry = next) {
    376  1.1  christos 			next = entry->next;
    377  1.4  christos 			isc_mem_put(bc->mctx, entry,
    378  1.4  christos 				    sizeof(*entry) + entry->name.length);
    379  1.4  christos 			atomic_fetch_sub_relaxed(&bc->count, 1);
    380  1.1  christos 		}
    381  1.1  christos 		bc->table[i] = NULL;
    382  1.1  christos 	}
    383  1.4  christos 	RWUNLOCK(&bc->lock, isc_rwlocktype_write);
    384  1.1  christos }
    385  1.1  christos 
    386  1.1  christos void
    387  1.1  christos dns_badcache_flushname(dns_badcache_t *bc, const dns_name_t *name) {
    388  1.1  christos 	dns_bcentry_t *bad, *prev, *next;
    389  1.1  christos 	isc_result_t result;
    390  1.1  christos 	isc_time_t now;
    391  1.5  christos 	unsigned int hash;
    392  1.1  christos 
    393  1.1  christos 	REQUIRE(VALID_BADCACHE(bc));
    394  1.1  christos 	REQUIRE(name != NULL);
    395  1.1  christos 
    396  1.4  christos 	RWLOCK(&bc->lock, isc_rwlocktype_read);
    397  1.1  christos 
    398  1.1  christos 	result = isc_time_now(&now);
    399  1.4  christos 	if (result != ISC_R_SUCCESS) {
    400  1.1  christos 		isc_time_settoepoch(&now);
    401  1.4  christos 	}
    402  1.5  christos 	hash = dns_name_hash(name, false) % bc->size;
    403  1.5  christos 	LOCK(&bc->tlocks[hash]);
    404  1.1  christos 	prev = NULL;
    405  1.5  christos 	for (bad = bc->table[hash]; bad != NULL; bad = next) {
    406  1.1  christos 		int n;
    407  1.1  christos 		next = bad->next;
    408  1.1  christos 		n = isc_time_compare(&bad->expire, &now);
    409  1.1  christos 		if (n < 0 || dns_name_equal(name, &bad->name)) {
    410  1.4  christos 			if (prev == NULL) {
    411  1.5  christos 				bc->table[hash] = bad->next;
    412  1.4  christos 			} else {
    413  1.1  christos 				prev->next = bad->next;
    414  1.4  christos 			}
    415  1.1  christos 
    416  1.4  christos 			isc_mem_put(bc->mctx, bad,
    417  1.4  christos 				    sizeof(*bad) + bad->name.length);
    418  1.4  christos 			atomic_fetch_sub_relaxed(&bc->count, 1);
    419  1.4  christos 		} else {
    420  1.1  christos 			prev = bad;
    421  1.4  christos 		}
    422  1.1  christos 	}
    423  1.5  christos 	UNLOCK(&bc->tlocks[hash]);
    424  1.1  christos 
    425  1.4  christos 	RWUNLOCK(&bc->lock, isc_rwlocktype_read);
    426  1.1  christos }
    427  1.1  christos 
    428  1.1  christos void
    429  1.1  christos dns_badcache_flushtree(dns_badcache_t *bc, const dns_name_t *name) {
    430  1.1  christos 	dns_bcentry_t *bad, *prev, *next;
    431  1.1  christos 	unsigned int i;
    432  1.1  christos 	int n;
    433  1.1  christos 	isc_time_t now;
    434  1.1  christos 	isc_result_t result;
    435  1.1  christos 
    436  1.1  christos 	REQUIRE(VALID_BADCACHE(bc));
    437  1.1  christos 	REQUIRE(name != NULL);
    438  1.1  christos 
    439  1.4  christos 	/*
    440  1.4  christos 	 * We write lock the tree to avoid relocking every node
    441  1.4  christos 	 * individually.
    442  1.4  christos 	 */
    443  1.4  christos 	RWLOCK(&bc->lock, isc_rwlocktype_write);
    444  1.1  christos 
    445  1.1  christos 	result = isc_time_now(&now);
    446  1.4  christos 	if (result != ISC_R_SUCCESS) {
    447  1.1  christos 		isc_time_settoepoch(&now);
    448  1.4  christos 	}
    449  1.1  christos 
    450  1.4  christos 	for (i = 0; atomic_load_relaxed(&bc->count) > 0 && i < bc->size; i++) {
    451  1.1  christos 		prev = NULL;
    452  1.1  christos 		for (bad = bc->table[i]; bad != NULL; bad = next) {
    453  1.1  christos 			next = bad->next;
    454  1.1  christos 			n = isc_time_compare(&bad->expire, &now);
    455  1.1  christos 			if (n < 0 || dns_name_issubdomain(&bad->name, name)) {
    456  1.4  christos 				if (prev == NULL) {
    457  1.1  christos 					bc->table[i] = bad->next;
    458  1.4  christos 				} else {
    459  1.1  christos 					prev->next = bad->next;
    460  1.4  christos 				}
    461  1.1  christos 
    462  1.4  christos 				isc_mem_put(bc->mctx, bad,
    463  1.4  christos 					    sizeof(*bad) + bad->name.length);
    464  1.4  christos 				atomic_fetch_sub_relaxed(&bc->count, 1);
    465  1.4  christos 			} else {
    466  1.1  christos 				prev = bad;
    467  1.4  christos 			}
    468  1.1  christos 		}
    469  1.1  christos 	}
    470  1.1  christos 
    471  1.4  christos 	RWUNLOCK(&bc->lock, isc_rwlocktype_write);
    472  1.1  christos }
    473  1.1  christos 
    474  1.1  christos void
    475  1.1  christos dns_badcache_print(dns_badcache_t *bc, const char *cachename, FILE *fp) {
    476  1.1  christos 	char namebuf[DNS_NAME_FORMATSIZE];
    477  1.1  christos 	char typebuf[DNS_RDATATYPE_FORMATSIZE];
    478  1.1  christos 	dns_bcentry_t *bad, *next, *prev;
    479  1.1  christos 	isc_time_t now;
    480  1.1  christos 	unsigned int i;
    481  1.3  christos 	uint64_t t;
    482  1.1  christos 
    483  1.1  christos 	REQUIRE(VALID_BADCACHE(bc));
    484  1.1  christos 	REQUIRE(cachename != NULL);
    485  1.1  christos 	REQUIRE(fp != NULL);
    486  1.1  christos 
    487  1.4  christos 	/*
    488  1.4  christos 	 * We write lock the tree to avoid relocking every node
    489  1.4  christos 	 * individually.
    490  1.4  christos 	 */
    491  1.4  christos 	RWLOCK(&bc->lock, isc_rwlocktype_write);
    492  1.1  christos 	fprintf(fp, ";\n; %s\n;\n", cachename);
    493  1.1  christos 
    494  1.1  christos 	TIME_NOW(&now);
    495  1.4  christos 	for (i = 0; atomic_load_relaxed(&bc->count) > 0 && i < bc->size; i++) {
    496  1.1  christos 		prev = NULL;
    497  1.1  christos 		for (bad = bc->table[i]; bad != NULL; bad = next) {
    498  1.1  christos 			next = bad->next;
    499  1.1  christos 			if (isc_time_compare(&bad->expire, &now) < 0) {
    500  1.4  christos 				if (prev != NULL) {
    501  1.1  christos 					prev->next = bad->next;
    502  1.4  christos 				} else {
    503  1.1  christos 					bc->table[i] = bad->next;
    504  1.4  christos 				}
    505  1.1  christos 
    506  1.4  christos 				isc_mem_put(bc->mctx, bad,
    507  1.4  christos 					    sizeof(*bad) + bad->name.length);
    508  1.4  christos 				atomic_fetch_sub_relaxed(&bc->count, 1);
    509  1.1  christos 				continue;
    510  1.1  christos 			}
    511  1.1  christos 			prev = bad;
    512  1.1  christos 			dns_name_format(&bad->name, namebuf, sizeof(namebuf));
    513  1.1  christos 			dns_rdatatype_format(bad->type, typebuf,
    514  1.1  christos 					     sizeof(typebuf));
    515  1.1  christos 			t = isc_time_microdiff(&bad->expire, &now);
    516  1.1  christos 			t /= 1000;
    517  1.4  christos 			fprintf(fp,
    518  1.4  christos 				"; %s/%s [ttl "
    519  1.3  christos 				"%" PRIu64 "]\n",
    520  1.1  christos 				namebuf, typebuf, t);
    521  1.1  christos 		}
    522  1.1  christos 	}
    523  1.4  christos 	RWUNLOCK(&bc->lock, isc_rwlocktype_write);
    524  1.1  christos }
    525