Home | History | Annotate | Line # | Download | only in dns
      1 /*	$NetBSD: cache.c,v 1.14 2026/09/17 18:01:14 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
      5  *
      6  * SPDX-License-Identifier: MPL-2.0
      7  *
      8  * This Source Code Form is subject to the terms of the Mozilla Public
      9  * License, v. 2.0. If a copy of the MPL was not distributed with this
     10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
     11  *
     12  * See the COPYRIGHT file distributed with this work for additional
     13  * information regarding copyright ownership.
     14  */
     15 
     16 /*! \file */
     17 
     18 #include <inttypes.h>
     19 #include <stdbool.h>
     20 
     21 #include <isc/loop.h>
     22 #include <isc/mem.h>
     23 #include <isc/refcount.h>
     24 #include <isc/result.h>
     25 #include <isc/stats.h>
     26 #include <isc/string.h>
     27 #include <isc/time.h>
     28 #include <isc/timer.h>
     29 #include <isc/util.h>
     30 
     31 #include <dns/cache.h>
     32 #include <dns/db.h>
     33 #include <dns/dbiterator.h>
     34 #include <dns/log.h>
     35 #include <dns/masterdump.h>
     36 #include <dns/rdata.h>
     37 #include <dns/rdataset.h>
     38 #include <dns/rdatasetiter.h>
     39 #include <dns/stats.h>
     40 
     41 #ifdef HAVE_JSON_C
     42 #include <json_object.h>
     43 #endif /* HAVE_JSON_C */
     44 
     45 #ifdef HAVE_LIBXML2
     46 #include <libxml/xmlwriter.h>
     47 #define ISC_XMLCHAR (const xmlChar *)
     48 #endif /* HAVE_LIBXML2 */
     49 
     50 #define CACHE_MAGIC	   ISC_MAGIC('$', '$', '$', '$')
     51 #define VALID_CACHE(cache) ISC_MAGIC_VALID(cache, CACHE_MAGIC)
     52 
     53 /*
     54  * DNS_CACHE_MINSIZE is how many bytes is the floor for
     55  * dns_cache_setcachesize().
     56  */
     57 #define DNS_CACHE_MINSIZE 2097152U /*%< Bytes.  2097152 = 2 MB */
     58 
     59 /***
     60  ***	Types
     61  ***/
     62 
     63 /*%
     64  * The actual cache object.
     65  */
     66 
     67 struct dns_cache {
     68 	/* Unlocked. */
     69 	unsigned int magic;
     70 	isc_mutex_t lock;
     71 	isc_mem_t *mctx;  /* Memory context for the dns_cache object */
     72 	isc_mem_t *hmctx; /* Heap memory */
     73 	isc_mem_t *tmctx; /* Tree memory */
     74 	isc_loopmgr_t *loopmgr;
     75 	char *name;
     76 	isc_refcount_t references;
     77 
     78 	/* Locked by 'lock'. */
     79 	dns_rdataclass_t rdclass;
     80 	dns_db_t *db;
     81 	size_t size;
     82 	dns_ttl_t serve_stale_ttl;
     83 	dns_ttl_t serve_stale_refresh;
     84 	isc_stats_t *stats;
     85 	uint32_t maxrrperset;
     86 	uint32_t maxtypepername;
     87 };
     88 
     89 /***
     90  ***	Functions
     91  ***/
     92 
     93 static isc_result_t
     94 cache_create_db(dns_cache_t *cache, dns_db_t **dbp, isc_mem_t **tmctxp,
     95 		isc_mem_t **hmctxp) {
     96 	isc_result_t result;
     97 	char *argv[1] = { 0 };
     98 	dns_db_t *db = NULL;
     99 	isc_mem_t *tmctx = NULL, *hmctx = NULL;
    100 
    101 	/*
    102 	 * This will be the cache memory context, which is subject
    103 	 * to cleaning when the configured memory limits are exceeded.
    104 	 */
    105 	isc_mem_create(&tmctx);
    106 	isc_mem_setname(tmctx, "cache");
    107 
    108 	/*
    109 	 * This will be passed to RBTDB to use for heaps. This is separate
    110 	 * from the main cache memory because it can grow quite large under
    111 	 * heavy load and could otherwise cause the cache to be cleaned too
    112 	 * aggressively.
    113 	 */
    114 	isc_mem_create(&hmctx);
    115 	isc_mem_setname(hmctx, "cache_heap");
    116 
    117 	/*
    118 	 * For databases of type "qpcache" or "rbt" (which are the
    119 	 * only cache implementations currently in existence) we pass
    120 	 * hmctx to dns_db_create() via argv[0].
    121 	 */
    122 	argv[0] = (char *)hmctx;
    123 	result = dns_db_create(tmctx, CACHEDB_DEFAULT, dns_rootname,
    124 			       dns_dbtype_cache, cache->rdclass, 1, argv, &db);
    125 	if (result != ISC_R_SUCCESS) {
    126 		goto cleanup_mctx;
    127 	}
    128 	result = dns_db_setcachestats(db, cache->stats);
    129 	if (result != ISC_R_SUCCESS) {
    130 		goto cleanup_db;
    131 	}
    132 
    133 	dns_db_setservestalettl(db, cache->serve_stale_ttl);
    134 	dns_db_setservestalerefresh(db, cache->serve_stale_refresh);
    135 	dns_db_setmaxrrperset(db, cache->maxrrperset);
    136 	dns_db_setmaxtypepername(db, cache->maxtypepername);
    137 
    138 	/*
    139 	 * XXX this is only used by the RBT cache, and can
    140 	 * be removed when it is.
    141 	 */
    142 	dns_db_setloop(db, isc_loop_main(cache->loopmgr));
    143 
    144 	*dbp = db;
    145 	*hmctxp = hmctx;
    146 	*tmctxp = tmctx;
    147 
    148 	return ISC_R_SUCCESS;
    149 
    150 cleanup_db:
    151 	dns_db_detach(&db);
    152 cleanup_mctx:
    153 	isc_mem_detach(&hmctx);
    154 	isc_mem_detach(&tmctx);
    155 
    156 	return result;
    157 }
    158 
    159 static void
    160 cache_destroy(dns_cache_t *cache) {
    161 	isc_stats_detach(&cache->stats);
    162 	isc_mutex_destroy(&cache->lock);
    163 	isc_mem_free(cache->mctx, cache->name);
    164 	if (cache->hmctx != NULL) {
    165 		isc_mem_detach(&cache->hmctx);
    166 	}
    167 	if (cache->tmctx != NULL) {
    168 		isc_mem_detach(&cache->tmctx);
    169 	}
    170 	isc_mem_putanddetach(&cache->mctx, cache, sizeof(*cache));
    171 }
    172 
    173 isc_result_t
    174 dns_cache_create(isc_loopmgr_t *loopmgr, dns_rdataclass_t rdclass,
    175 		 const char *cachename, isc_mem_t *mctx, dns_cache_t **cachep) {
    176 	isc_result_t result;
    177 	dns_cache_t *cache = NULL;
    178 
    179 	REQUIRE(loopmgr != NULL);
    180 	REQUIRE(cachename != NULL);
    181 	REQUIRE(cachep != NULL && *cachep == NULL);
    182 
    183 	cache = isc_mem_get(mctx, sizeof(*cache));
    184 	*cache = (dns_cache_t){
    185 		.rdclass = rdclass,
    186 		.name = isc_mem_strdup(mctx, cachename),
    187 		.loopmgr = loopmgr,
    188 		.references = ISC_REFCOUNT_INITIALIZER(1),
    189 		.magic = CACHE_MAGIC,
    190 	};
    191 
    192 	isc_mutex_init(&cache->lock);
    193 	isc_mem_attach(mctx, &cache->mctx);
    194 
    195 	isc_stats_create(mctx, &cache->stats, dns_cachestatscounter_max);
    196 
    197 	/*
    198 	 * Create the database
    199 	 */
    200 	result = cache_create_db(cache, &cache->db, &cache->tmctx,
    201 				 &cache->hmctx);
    202 	if (result != ISC_R_SUCCESS) {
    203 		goto cleanup;
    204 	}
    205 
    206 	*cachep = cache;
    207 	return ISC_R_SUCCESS;
    208 
    209 cleanup:
    210 	cache_destroy(cache);
    211 	return result;
    212 }
    213 
    214 static void
    215 cache_cleanup(dns_cache_t *cache) {
    216 	REQUIRE(VALID_CACHE(cache));
    217 
    218 	isc_refcount_destroy(&cache->references);
    219 	cache->magic = 0;
    220 
    221 	isc_mem_clearwater(cache->tmctx);
    222 	dns_db_detach(&cache->db);
    223 
    224 	cache_destroy(cache);
    225 }
    226 
    227 #if DNS_CACHE_TRACE
    228 ISC_REFCOUNT_TRACE_IMPL(dns_cache, cache_cleanup);
    229 #else
    230 ISC_REFCOUNT_IMPL(dns_cache, cache_cleanup);
    231 #endif
    232 
    233 void
    234 dns_cache_attachdb(dns_cache_t *cache, dns_db_t **dbp) {
    235 	REQUIRE(VALID_CACHE(cache));
    236 	REQUIRE(dbp != NULL && *dbp == NULL);
    237 	REQUIRE(cache->db != NULL);
    238 
    239 	LOCK(&cache->lock);
    240 	dns_db_attach(cache->db, dbp);
    241 	UNLOCK(&cache->lock);
    242 }
    243 
    244 const char *
    245 dns_cache_getname(dns_cache_t *cache) {
    246 	REQUIRE(VALID_CACHE(cache));
    247 
    248 	return cache->name;
    249 }
    250 
    251 static void
    252 updatewater(dns_cache_t *cache) {
    253 	size_t hi = cache->size - (cache->size >> 3); /* ~ 7/8ths. */
    254 	size_t lo = cache->size - (cache->size >> 2); /* ~ 3/4ths. */
    255 	if (cache->size == 0U || hi == 0U || lo == 0U) {
    256 		isc_mem_clearwater(cache->tmctx);
    257 	} else {
    258 		isc_mem_setwater(cache->tmctx, hi, lo);
    259 	}
    260 }
    261 
    262 void
    263 dns_cache_setcachesize(dns_cache_t *cache, size_t size) {
    264 	REQUIRE(VALID_CACHE(cache));
    265 
    266 	/*
    267 	 * Impose a minimum cache size; pathological things happen if there
    268 	 * is too little room.
    269 	 */
    270 	if (size != 0U && size < DNS_CACHE_MINSIZE) {
    271 		size = DNS_CACHE_MINSIZE;
    272 	}
    273 
    274 	LOCK(&cache->lock);
    275 	cache->size = size;
    276 	updatewater(cache);
    277 	UNLOCK(&cache->lock);
    278 }
    279 
    280 size_t
    281 dns_cache_getcachesize(dns_cache_t *cache) {
    282 	size_t size;
    283 
    284 	REQUIRE(VALID_CACHE(cache));
    285 
    286 	LOCK(&cache->lock);
    287 	size = cache->size;
    288 	UNLOCK(&cache->lock);
    289 
    290 	return size;
    291 }
    292 
    293 void
    294 dns_cache_setservestalettl(dns_cache_t *cache, dns_ttl_t ttl) {
    295 	REQUIRE(VALID_CACHE(cache));
    296 
    297 	LOCK(&cache->lock);
    298 	cache->serve_stale_ttl = ttl;
    299 	UNLOCK(&cache->lock);
    300 
    301 	(void)dns_db_setservestalettl(cache->db, ttl);
    302 }
    303 
    304 dns_ttl_t
    305 dns_cache_getservestalettl(dns_cache_t *cache) {
    306 	dns_ttl_t ttl;
    307 	isc_result_t result;
    308 
    309 	REQUIRE(VALID_CACHE(cache));
    310 
    311 	/*
    312 	 * Could get it straight from the dns_cache_t, but use db
    313 	 * to confirm the value that the db is really using.
    314 	 */
    315 	result = dns_db_getservestalettl(cache->db, &ttl);
    316 	return result == ISC_R_SUCCESS ? ttl : 0;
    317 }
    318 
    319 void
    320 dns_cache_setservestalerefresh(dns_cache_t *cache, dns_ttl_t interval) {
    321 	REQUIRE(VALID_CACHE(cache));
    322 
    323 	LOCK(&cache->lock);
    324 	cache->serve_stale_refresh = interval;
    325 	UNLOCK(&cache->lock);
    326 
    327 	(void)dns_db_setservestalerefresh(cache->db, interval);
    328 }
    329 
    330 dns_ttl_t
    331 dns_cache_getservestalerefresh(dns_cache_t *cache) {
    332 	isc_result_t result;
    333 	dns_ttl_t interval;
    334 
    335 	REQUIRE(VALID_CACHE(cache));
    336 
    337 	result = dns_db_getservestalerefresh(cache->db, &interval);
    338 	return result == ISC_R_SUCCESS ? interval : 0;
    339 }
    340 
    341 isc_result_t
    342 dns_cache_flush(dns_cache_t *cache) {
    343 	dns_db_t *db = NULL, *olddb;
    344 	isc_mem_t *tmctx = NULL, *oldtmctx;
    345 	isc_mem_t *hmctx = NULL, *oldhmctx;
    346 	isc_result_t result;
    347 
    348 	result = cache_create_db(cache, &db, &tmctx, &hmctx);
    349 	if (result != ISC_R_SUCCESS) {
    350 		return result;
    351 	}
    352 
    353 	LOCK(&cache->lock);
    354 	isc_mem_clearwater(cache->tmctx);
    355 	oldhmctx = cache->hmctx;
    356 	cache->hmctx = hmctx;
    357 	oldtmctx = cache->tmctx;
    358 	cache->tmctx = tmctx;
    359 	updatewater(cache);
    360 	olddb = cache->db;
    361 	cache->db = db;
    362 	UNLOCK(&cache->lock);
    363 
    364 	dns_db_detach(&olddb);
    365 	isc_mem_detach(&oldhmctx);
    366 	isc_mem_detach(&oldtmctx);
    367 
    368 	return ISC_R_SUCCESS;
    369 }
    370 
    371 static isc_result_t
    372 clearnode(dns_db_t *db, dns_dbnode_t *node) {
    373 	isc_result_t result;
    374 	dns_rdatasetiter_t *iter = NULL;
    375 
    376 	result = dns_db_allrdatasets(db, node, NULL, DNS_DB_STALEOK,
    377 				     (isc_stdtime_t)0, &iter);
    378 	if (result != ISC_R_SUCCESS) {
    379 		return result;
    380 	}
    381 
    382 	for (result = dns_rdatasetiter_first(iter); result == ISC_R_SUCCESS;
    383 	     result = dns_rdatasetiter_next(iter))
    384 	{
    385 		dns_rdataset_t rdataset;
    386 		dns_rdataset_init(&rdataset);
    387 
    388 		dns_rdatasetiter_current(iter, &rdataset);
    389 		result = dns_db_deleterdataset(db, node, NULL, rdataset.type,
    390 					       rdataset.covers);
    391 		dns_rdataset_disassociate(&rdataset);
    392 		if (result != ISC_R_SUCCESS && result != DNS_R_UNCHANGED) {
    393 			break;
    394 		}
    395 	}
    396 
    397 	if (result == ISC_R_NOMORE) {
    398 		result = ISC_R_SUCCESS;
    399 	}
    400 
    401 	dns_rdatasetiter_destroy(&iter);
    402 	return result;
    403 }
    404 
    405 static isc_result_t
    406 cleartree(dns_db_t *db, const dns_name_t *name) {
    407 	isc_result_t result, answer = ISC_R_SUCCESS;
    408 	dns_dbiterator_t *iter = NULL;
    409 	dns_dbnode_t *node = NULL, *top = NULL;
    410 	dns_fixedname_t fnodename;
    411 	dns_name_t *nodename;
    412 
    413 	/*
    414 	 * Create the node if it doesn't exist so dns_dbiterator_seek()
    415 	 * can find it.  We will continue even if this fails.
    416 	 */
    417 	(void)dns_db_findnode(db, name, true, &top);
    418 
    419 	nodename = dns_fixedname_initname(&fnodename);
    420 
    421 	result = dns_db_createiterator(db, 0, &iter);
    422 	if (result != ISC_R_SUCCESS) {
    423 		goto cleanup;
    424 	}
    425 
    426 	result = dns_dbiterator_seek(iter, name);
    427 	if (result == DNS_R_PARTIALMATCH) {
    428 		result = dns_dbiterator_next(iter);
    429 	}
    430 	if (result != ISC_R_SUCCESS) {
    431 		goto cleanup;
    432 	}
    433 
    434 	while (result == ISC_R_SUCCESS) {
    435 		result = dns_dbiterator_current(iter, &node, nodename);
    436 		if (result == DNS_R_NEWORIGIN) {
    437 			result = ISC_R_SUCCESS;
    438 		}
    439 		if (result != ISC_R_SUCCESS) {
    440 			goto cleanup;
    441 		}
    442 		/*
    443 		 * Are we done?
    444 		 */
    445 		if (!dns_name_issubdomain(nodename, name)) {
    446 			goto cleanup;
    447 		}
    448 
    449 		/*
    450 		 * If clearnode fails record and move onto the next node.
    451 		 */
    452 		result = clearnode(db, node);
    453 		if (result != ISC_R_SUCCESS && answer == ISC_R_SUCCESS) {
    454 			answer = result;
    455 		}
    456 		dns_db_detachnode(db, &node);
    457 		result = dns_dbiterator_next(iter);
    458 	}
    459 
    460 cleanup:
    461 	if (result == ISC_R_NOMORE || result == ISC_R_NOTFOUND) {
    462 		result = ISC_R_SUCCESS;
    463 	}
    464 	if (result != ISC_R_SUCCESS && answer == ISC_R_SUCCESS) {
    465 		answer = result;
    466 	}
    467 	if (node != NULL) {
    468 		dns_db_detachnode(db, &node);
    469 	}
    470 	if (iter != NULL) {
    471 		dns_dbiterator_destroy(&iter);
    472 	}
    473 	if (top != NULL) {
    474 		dns_db_detachnode(db, &top);
    475 	}
    476 
    477 	return answer;
    478 }
    479 
    480 isc_result_t
    481 dns_cache_flushname(dns_cache_t *cache, const dns_name_t *name) {
    482 	return dns_cache_flushnode(cache, name, false);
    483 }
    484 
    485 isc_result_t
    486 dns_cache_flushnode(dns_cache_t *cache, const dns_name_t *name, bool tree) {
    487 	isc_result_t result;
    488 	dns_dbnode_t *node = NULL;
    489 	dns_db_t *db = NULL;
    490 
    491 	REQUIRE(!(tree && dns_name_equal(name, dns_rootname)));
    492 
    493 	LOCK(&cache->lock);
    494 	if (cache->db != NULL) {
    495 		dns_db_attach(cache->db, &db);
    496 	}
    497 	UNLOCK(&cache->lock);
    498 	if (db == NULL) {
    499 		return ISC_R_SUCCESS;
    500 	}
    501 
    502 	if (tree) {
    503 		result = cleartree(cache->db, name);
    504 	} else {
    505 		result = dns_db_findnode(cache->db, name, false, &node);
    506 		if (result == ISC_R_NOTFOUND) {
    507 			result = ISC_R_SUCCESS;
    508 			goto cleanup_db;
    509 		}
    510 		if (result != ISC_R_SUCCESS) {
    511 			goto cleanup_db;
    512 		}
    513 		result = clearnode(cache->db, node);
    514 		dns_db_detachnode(cache->db, &node);
    515 	}
    516 
    517 cleanup_db:
    518 	dns_db_detach(&db);
    519 	return result;
    520 }
    521 
    522 isc_stats_t *
    523 dns_cache_getstats(dns_cache_t *cache) {
    524 	REQUIRE(VALID_CACHE(cache));
    525 	return cache->stats;
    526 }
    527 
    528 void
    529 dns_cache_updatestats(dns_cache_t *cache, isc_result_t result) {
    530 	REQUIRE(VALID_CACHE(cache));
    531 	if (cache->stats == NULL) {
    532 		return;
    533 	}
    534 
    535 	switch (result) {
    536 	case ISC_R_SUCCESS:
    537 	case DNS_R_NCACHENXDOMAIN:
    538 	case DNS_R_NCACHENXRRSET:
    539 	case DNS_R_CNAME:
    540 	case DNS_R_DNAME:
    541 	case DNS_R_GLUE:
    542 	case DNS_R_ZONECUT:
    543 	case DNS_R_COVERINGNSEC:
    544 		isc_stats_increment(cache->stats,
    545 				    dns_cachestatscounter_queryhits);
    546 		break;
    547 	default:
    548 		isc_stats_increment(cache->stats,
    549 				    dns_cachestatscounter_querymisses);
    550 	}
    551 }
    552 
    553 void
    554 dns_cache_setmaxrrperset(dns_cache_t *cache, uint32_t value) {
    555 	REQUIRE(VALID_CACHE(cache));
    556 
    557 	cache->maxrrperset = value;
    558 	if (cache->db != NULL) {
    559 		dns_db_setmaxrrperset(cache->db, value);
    560 	}
    561 }
    562 
    563 void
    564 dns_cache_setmaxtypepername(dns_cache_t *cache, uint32_t value) {
    565 	REQUIRE(VALID_CACHE(cache));
    566 
    567 	cache->maxtypepername = value;
    568 	if (cache->db != NULL) {
    569 		dns_db_setmaxtypepername(cache->db, value);
    570 	}
    571 }
    572 
    573 /*
    574  * XXX: Much of the following code has been copied in from statschannel.c.
    575  * We should refactor this into a generic function in stats.c that can be
    576  * called from both places.
    577  */
    578 typedef struct cache_dumparg {
    579 	isc_statsformat_t type;
    580 	void *arg;		 /* type dependent argument */
    581 	int ncounters;		 /* for general statistics */
    582 	int *counterindices;	 /* for general statistics */
    583 	uint64_t *countervalues; /* for general statistics */
    584 	isc_result_t result;
    585 } cache_dumparg_t;
    586 
    587 static void
    588 getcounter(isc_statscounter_t counter, uint64_t val, void *arg) {
    589 	cache_dumparg_t *dumparg = arg;
    590 
    591 	REQUIRE(counter < dumparg->ncounters);
    592 	dumparg->countervalues[counter] = val;
    593 }
    594 
    595 static void
    596 getcounters(isc_stats_t *stats, isc_statsformat_t type, int ncounters,
    597 	    int *indices, uint64_t *values) {
    598 	cache_dumparg_t dumparg;
    599 
    600 	memset(values, 0, sizeof(values[0]) * ncounters);
    601 
    602 	dumparg.type = type;
    603 	dumparg.ncounters = ncounters;
    604 	dumparg.counterindices = indices;
    605 	dumparg.countervalues = values;
    606 
    607 	isc_stats_dump(stats, getcounter, &dumparg, ISC_STATSDUMP_VERBOSE);
    608 }
    609 
    610 void
    611 dns_cache_dumpstats(dns_cache_t *cache, FILE *fp) {
    612 	int indices[dns_cachestatscounter_max];
    613 	uint64_t values[dns_cachestatscounter_max];
    614 
    615 	REQUIRE(VALID_CACHE(cache));
    616 
    617 	getcounters(cache->stats, isc_statsformat_file,
    618 		    dns_cachestatscounter_max, indices, values);
    619 
    620 	fprintf(fp, "%20" PRIu64 " %s\n", values[dns_cachestatscounter_hits],
    621 		"cache hits");
    622 	fprintf(fp, "%20" PRIu64 " %s\n", values[dns_cachestatscounter_misses],
    623 		"cache misses");
    624 	fprintf(fp, "%20" PRIu64 " %s\n",
    625 		values[dns_cachestatscounter_queryhits],
    626 		"cache hits (from query)");
    627 	fprintf(fp, "%20" PRIu64 " %s\n",
    628 		values[dns_cachestatscounter_querymisses],
    629 		"cache misses (from query)");
    630 	fprintf(fp, "%20" PRIu64 " %s\n",
    631 		values[dns_cachestatscounter_deletelru],
    632 		"cache records deleted due to memory exhaustion");
    633 	fprintf(fp, "%20" PRIu64 " %s\n",
    634 		values[dns_cachestatscounter_deletettl],
    635 		"cache records deleted due to TTL expiration");
    636 	fprintf(fp, "%20" PRIu64 " %s\n",
    637 		values[dns_cachestatscounter_coveringnsec],
    638 		"covering nsec returned");
    639 	fprintf(fp, "%20u %s\n", dns_db_nodecount(cache->db, dns_dbtree_main),
    640 		"cache database nodes");
    641 	fprintf(fp, "%20u %s\n", dns_db_nodecount(cache->db, dns_dbtree_nsec),
    642 		"cache NSEC auxiliary database nodes");
    643 	fprintf(fp, "%20" PRIu64 " %s\n", (uint64_t)dns_db_hashsize(cache->db),
    644 		"cache database hash buckets");
    645 
    646 	fprintf(fp, "%20" PRIu64 " %s\n", (uint64_t)isc_mem_inuse(cache->tmctx),
    647 		"cache tree memory in use");
    648 
    649 	fprintf(fp, "%20" PRIu64 " %s\n", (uint64_t)isc_mem_inuse(cache->hmctx),
    650 		"cache heap memory in use");
    651 }
    652 
    653 #ifdef HAVE_LIBXML2
    654 #define TRY0(a)                     \
    655 	do {                        \
    656 		xmlrc = (a);        \
    657 		if (xmlrc < 0)      \
    658 			goto error; \
    659 	} while (0)
    660 static int
    661 renderstat(const char *name, uint64_t value, xmlTextWriterPtr writer) {
    662 	int xmlrc;
    663 
    664 	TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counter"));
    665 	TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "name",
    666 					 ISC_XMLCHAR name));
    667 	TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "", value));
    668 	TRY0(xmlTextWriterEndElement(writer)); /* counter */
    669 
    670 error:
    671 	return xmlrc;
    672 }
    673 
    674 int
    675 dns_cache_renderxml(dns_cache_t *cache, void *writer0) {
    676 	int indices[dns_cachestatscounter_max];
    677 	uint64_t values[dns_cachestatscounter_max];
    678 	int xmlrc;
    679 	xmlTextWriterPtr writer = (xmlTextWriterPtr)writer0;
    680 
    681 	REQUIRE(VALID_CACHE(cache));
    682 
    683 	getcounters(cache->stats, isc_statsformat_file,
    684 		    dns_cachestatscounter_max, indices, values);
    685 	TRY0(renderstat("CacheHits", values[dns_cachestatscounter_hits],
    686 			writer));
    687 	TRY0(renderstat("CacheMisses", values[dns_cachestatscounter_misses],
    688 			writer));
    689 	TRY0(renderstat("QueryHits", values[dns_cachestatscounter_queryhits],
    690 			writer));
    691 	TRY0(renderstat("QueryMisses",
    692 			values[dns_cachestatscounter_querymisses], writer));
    693 	TRY0(renderstat("DeleteLRU", values[dns_cachestatscounter_deletelru],
    694 			writer));
    695 	TRY0(renderstat("DeleteTTL", values[dns_cachestatscounter_deletettl],
    696 			writer));
    697 	TRY0(renderstat("CoveringNSEC",
    698 			values[dns_cachestatscounter_coveringnsec], writer));
    699 
    700 	TRY0(renderstat("CacheNodes",
    701 			dns_db_nodecount(cache->db, dns_dbtree_main), writer));
    702 	TRY0(renderstat("CacheNSECNodes",
    703 			dns_db_nodecount(cache->db, dns_dbtree_nsec), writer));
    704 	TRY0(renderstat("CacheBuckets", dns_db_hashsize(cache->db), writer));
    705 
    706 	TRY0(renderstat("TreeMemInUse", isc_mem_inuse(cache->tmctx), writer));
    707 
    708 	TRY0(renderstat("HeapMemInUse", isc_mem_inuse(cache->hmctx), writer));
    709 error:
    710 	return xmlrc;
    711 }
    712 #endif /* ifdef HAVE_LIBXML2 */
    713 
    714 #ifdef HAVE_JSON_C
    715 #define CHECKMEM(m)                              \
    716 	do {                                     \
    717 		if (m == NULL) {                 \
    718 			result = ISC_R_NOMEMORY; \
    719 			goto error;              \
    720 		}                                \
    721 	} while (0)
    722 
    723 isc_result_t
    724 dns_cache_renderjson(dns_cache_t *cache, void *cstats0) {
    725 	isc_result_t result = ISC_R_SUCCESS;
    726 	int indices[dns_cachestatscounter_max];
    727 	uint64_t values[dns_cachestatscounter_max];
    728 	json_object *obj;
    729 	json_object *cstats = (json_object *)cstats0;
    730 
    731 	REQUIRE(VALID_CACHE(cache));
    732 
    733 	getcounters(cache->stats, isc_statsformat_file,
    734 		    dns_cachestatscounter_max, indices, values);
    735 
    736 	obj = json_object_new_int64(values[dns_cachestatscounter_hits]);
    737 	CHECKMEM(obj);
    738 	json_object_object_add(cstats, "CacheHits", obj);
    739 
    740 	obj = json_object_new_int64(values[dns_cachestatscounter_misses]);
    741 	CHECKMEM(obj);
    742 	json_object_object_add(cstats, "CacheMisses", obj);
    743 
    744 	obj = json_object_new_int64(values[dns_cachestatscounter_queryhits]);
    745 	CHECKMEM(obj);
    746 	json_object_object_add(cstats, "QueryHits", obj);
    747 
    748 	obj = json_object_new_int64(values[dns_cachestatscounter_querymisses]);
    749 	CHECKMEM(obj);
    750 	json_object_object_add(cstats, "QueryMisses", obj);
    751 
    752 	obj = json_object_new_int64(values[dns_cachestatscounter_deletelru]);
    753 	CHECKMEM(obj);
    754 	json_object_object_add(cstats, "DeleteLRU", obj);
    755 
    756 	obj = json_object_new_int64(values[dns_cachestatscounter_deletettl]);
    757 	CHECKMEM(obj);
    758 	json_object_object_add(cstats, "DeleteTTL", obj);
    759 
    760 	obj = json_object_new_int64(values[dns_cachestatscounter_coveringnsec]);
    761 	CHECKMEM(obj);
    762 	json_object_object_add(cstats, "CoveringNSEC", obj);
    763 
    764 	obj = json_object_new_int64(
    765 		dns_db_nodecount(cache->db, dns_dbtree_main));
    766 	CHECKMEM(obj);
    767 	json_object_object_add(cstats, "CacheNodes", obj);
    768 
    769 	obj = json_object_new_int64(
    770 		dns_db_nodecount(cache->db, dns_dbtree_nsec));
    771 	CHECKMEM(obj);
    772 	json_object_object_add(cstats, "CacheNSECNodes", obj);
    773 
    774 	obj = json_object_new_int64(dns_db_hashsize(cache->db));
    775 	CHECKMEM(obj);
    776 	json_object_object_add(cstats, "CacheBuckets", obj);
    777 
    778 	obj = json_object_new_int64(isc_mem_inuse(cache->tmctx));
    779 	CHECKMEM(obj);
    780 	json_object_object_add(cstats, "TreeMemInUse", obj);
    781 
    782 	obj = json_object_new_int64(isc_mem_inuse(cache->hmctx));
    783 	CHECKMEM(obj);
    784 	json_object_object_add(cstats, "HeapMemInUse", obj);
    785 
    786 	result = ISC_R_SUCCESS;
    787 error:
    788 	return result;
    789 }
    790 #endif /* ifdef HAVE_JSON_C */
    791