1999-11-29 17:58:39 +00:00
|
|
|
/*
|
2018-02-23 09:53:12 +01:00
|
|
|
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2016-06-27 14:56:38 +10: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 http://mozilla.org/MPL/2.0/.
|
2018-02-23 09:53:12 +01:00
|
|
|
*
|
|
|
|
* See the COPYRIGHT file distributed with this work for additional
|
|
|
|
* information regarding copyright ownership.
|
1999-11-29 17:58:39 +00:00
|
|
|
*/
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*! \file */
|
1999-11-29 17:58:39 +00:00
|
|
|
|
2018-03-28 14:56:40 +02:00
|
|
|
#include <inttypes.h>
|
2018-03-28 14:19:37 +02:00
|
|
|
#include <stdbool.h>
|
2018-03-28 14:56:40 +02:00
|
|
|
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/mem.h>
|
2012-05-14 10:06:05 -07:00
|
|
|
#include <isc/print.h>
|
2019-05-17 13:47:00 +02:00
|
|
|
#include <isc/refcount.h>
|
2012-05-14 10:06:05 -07:00
|
|
|
#include <isc/stats.h>
|
2020-02-12 13:59:18 +01:00
|
|
|
#include <isc/string.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/task.h>
|
2000-04-25 19:35:39 +00:00
|
|
|
#include <isc/time.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/timer.h>
|
2000-01-04 23:24:13 +00:00
|
|
|
#include <isc/util.h>
|
1999-11-29 17:58:39 +00:00
|
|
|
|
|
|
|
#include <dns/cache.h>
|
|
|
|
#include <dns/db.h>
|
|
|
|
#include <dns/dbiterator.h>
|
|
|
|
#include <dns/events.h>
|
2005-08-15 01:21:07 +00:00
|
|
|
#include <dns/lib.h>
|
1999-11-29 17:58:39 +00:00
|
|
|
#include <dns/log.h>
|
2001-01-12 22:22:17 +00:00
|
|
|
#include <dns/masterdump.h>
|
2001-11-27 03:10:32 +00:00
|
|
|
#include <dns/rdata.h>
|
|
|
|
#include <dns/rdataset.h>
|
|
|
|
#include <dns/rdatasetiter.h>
|
2000-05-02 03:54:17 +00:00
|
|
|
#include <dns/result.h>
|
2012-05-14 10:06:05 -07:00
|
|
|
#include <dns/stats.h>
|
1999-11-29 17:58:39 +00:00
|
|
|
|
2019-06-24 12:21:47 +02:00
|
|
|
#ifdef HAVE_JSON_C
|
|
|
|
#include <json_object.h>
|
|
|
|
#endif /* HAVE_JSON_C */
|
|
|
|
|
2019-06-24 14:25:55 +02:00
|
|
|
#ifdef HAVE_LIBXML2
|
|
|
|
#include <libxml/xmlwriter.h>
|
|
|
|
#define ISC_XMLCHAR (const xmlChar *)
|
|
|
|
#endif /* HAVE_LIBXML2 */
|
|
|
|
|
2011-03-03 04:42:25 +00:00
|
|
|
#include "rbtdb.h"
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
#define CACHE_MAGIC ISC_MAGIC('$', '$', '$', '$')
|
|
|
|
#define VALID_CACHE(cache) ISC_MAGIC_VALID(cache, CACHE_MAGIC)
|
1999-11-29 17:58:39 +00:00
|
|
|
|
2007-10-19 17:15:53 +00:00
|
|
|
/*!
|
2005-04-27 04:57:32 +00:00
|
|
|
* Control incremental cleaning.
|
2020-02-12 13:59:18 +01:00
|
|
|
* DNS_CACHE_MINSIZE is how many bytes is the floor for
|
|
|
|
* dns_cache_setcachesize(). See also DNS_CACHE_CLEANERINCREMENT
|
2005-04-27 04:57:32 +00:00
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
#define DNS_CACHE_MINSIZE 2097152U /*%< Bytes. 2097152 = 2 MB */
|
2007-10-19 17:15:53 +00:00
|
|
|
/*!
|
2005-04-27 04:57:32 +00:00
|
|
|
* Control incremental cleaning.
|
2001-06-05 22:27:51 +00:00
|
|
|
* CLEANERINCREMENT is how many nodes are examined in one pass.
|
2007-10-19 17:15:53 +00:00
|
|
|
* See also DNS_CACHE_MINSIZE
|
2001-06-05 22:27:51 +00:00
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
#define DNS_CACHE_CLEANERINCREMENT 1000U /*%< Number of nodes. */
|
2001-06-05 22:27:51 +00:00
|
|
|
|
1999-11-29 17:58:39 +00:00
|
|
|
/***
|
2008-05-01 18:23:07 +00:00
|
|
|
*** Types
|
1999-11-29 17:58:39 +00:00
|
|
|
***/
|
|
|
|
|
|
|
|
/*
|
2009-01-17 14:18:27 +00:00
|
|
|
* A cache_cleaner_t encapsulates the state of the periodic
|
2000-08-01 01:33:37 +00:00
|
|
|
* cache cleaning.
|
1999-11-29 17:58:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct cache_cleaner cache_cleaner_t;
|
|
|
|
|
|
|
|
typedef enum {
|
2020-02-12 13:59:18 +01:00
|
|
|
cleaner_s_idle, /*%< Waiting for cleaning-interval to expire. */
|
|
|
|
cleaner_s_busy, /*%< Currently cleaning. */
|
2008-05-01 18:23:07 +00:00
|
|
|
cleaner_s_done /*%< Freed enough memory after being overmem. */
|
1999-11-29 17:58:39 +00:00
|
|
|
} cleaner_state_t;
|
2000-01-06 01:12:20 +00:00
|
|
|
|
2000-05-08 14:38:29 +00:00
|
|
|
/*
|
|
|
|
* Convenience macros for comprehensive assertion checking.
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
#define CLEANER_IDLE(c) \
|
|
|
|
((c)->state == cleaner_s_idle && (c)->resched_event != NULL)
|
|
|
|
#define CLEANER_BUSY(c) \
|
|
|
|
((c)->state == cleaner_s_busy && (c)->iterator != NULL && \
|
|
|
|
(c)->resched_event == NULL)
|
2000-01-06 01:12:20 +00:00
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
2001-06-05 22:27:51 +00:00
|
|
|
* Accesses to a cache cleaner object are synchronized through
|
|
|
|
* task/event serialization, or locked from the cache object.
|
|
|
|
*/
|
1999-11-29 17:58:39 +00:00
|
|
|
struct cache_cleaner {
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_mutex_t lock;
|
2008-02-07 23:46:54 +00:00
|
|
|
/*%<
|
2008-05-01 18:23:07 +00:00
|
|
|
* Locks overmem_event, overmem. Note: never allocate memory
|
2008-02-07 23:46:54 +00:00
|
|
|
* while holding this lock - that could lead to deadlock since
|
|
|
|
* the lock is take by water() which is called from the memory
|
|
|
|
* allocator.
|
|
|
|
*/
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_t *cache;
|
|
|
|
isc_task_t * task;
|
|
|
|
isc_event_t *resched_event; /*% Sent by cleaner task to
|
|
|
|
itself to reschedule */
|
|
|
|
isc_event_t *overmem_event;
|
2008-02-07 23:46:54 +00:00
|
|
|
|
2008-05-01 18:23:07 +00:00
|
|
|
dns_dbiterator_t *iterator;
|
2020-02-12 13:59:18 +01:00
|
|
|
unsigned int increment; /*% Number of names to
|
|
|
|
clean in one increment */
|
|
|
|
cleaner_state_t state; /*% Idle/Busy. */
|
|
|
|
bool overmem; /*% The cache is in an overmem state. */
|
|
|
|
bool replaceiterator;
|
1999-11-29 17:58:39 +00:00
|
|
|
};
|
|
|
|
|
2005-04-27 04:57:32 +00:00
|
|
|
/*%
|
2000-08-01 01:33:37 +00:00
|
|
|
* The actual cache object.
|
1999-11-29 17:58:39 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
struct dns_cache {
|
2008-02-07 23:46:54 +00:00
|
|
|
/* Unlocked. */
|
2020-02-12 13:59:18 +01:00
|
|
|
unsigned int magic;
|
|
|
|
isc_mutex_t lock;
|
|
|
|
isc_mutex_t filelock;
|
|
|
|
isc_mem_t * mctx; /* Main cache memory */
|
|
|
|
isc_mem_t * hmctx; /* Heap memory */
|
|
|
|
char * name;
|
|
|
|
isc_refcount_t references;
|
|
|
|
isc_refcount_t live_tasks;
|
2008-02-07 23:46:54 +00:00
|
|
|
|
|
|
|
/* Locked by 'lock'. */
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_rdataclass_t rdclass;
|
|
|
|
dns_db_t * db;
|
|
|
|
cache_cleaner_t cleaner;
|
|
|
|
char * db_type;
|
|
|
|
int db_argc;
|
|
|
|
char ** db_argv;
|
|
|
|
size_t size;
|
|
|
|
dns_ttl_t serve_stale_ttl;
|
|
|
|
isc_stats_t * stats;
|
2008-02-07 23:46:54 +00:00
|
|
|
|
|
|
|
/* Locked by 'filelock'. */
|
2020-02-12 13:59:18 +01:00
|
|
|
char *filename;
|
2008-02-07 23:46:54 +00:00
|
|
|
/* Access to the on-disk cache file is also locked by 'filelock'. */
|
1999-11-29 17:58:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/***
|
2008-05-01 18:23:07 +00:00
|
|
|
*** Functions
|
1999-11-29 17:58:39 +00:00
|
|
|
***/
|
|
|
|
|
1999-12-22 17:37:31 +00:00
|
|
|
static isc_result_t
|
2001-06-05 22:27:51 +00:00
|
|
|
cache_cleaner_init(dns_cache_t *cache, isc_taskmgr_t *taskmgr,
|
2008-02-07 23:46:54 +00:00
|
|
|
isc_timermgr_t *timermgr, cache_cleaner_t *cleaner);
|
1999-11-29 17:58:39 +00:00
|
|
|
|
|
|
|
static void
|
2008-05-01 18:23:07 +00:00
|
|
|
incremental_cleaning_action(isc_task_t *task, isc_event_t *event);
|
1999-11-29 17:58:39 +00:00
|
|
|
|
2000-08-31 12:15:17 +00:00
|
|
|
static void
|
2008-05-01 18:23:07 +00:00
|
|
|
cleaner_shutdown_action(isc_task_t *task, isc_event_t *event);
|
2000-08-31 12:15:17 +00:00
|
|
|
|
2005-08-15 01:21:07 +00:00
|
|
|
static void
|
2008-05-01 18:23:07 +00:00
|
|
|
overmem_cleaning_action(isc_task_t *task, isc_event_t *event);
|
2005-08-15 01:21:07 +00:00
|
|
|
|
2001-04-11 20:37:50 +00:00
|
|
|
static inline isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
cache_create_db(dns_cache_t *cache, dns_db_t **db)
|
|
|
|
{
|
2017-09-06 09:58:29 +10:00
|
|
|
isc_result_t result;
|
|
|
|
result = dns_db_create(cache->mctx, cache->db_type, dns_rootname,
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_dbtype_cache, cache->rdclass, cache->db_argc,
|
|
|
|
cache->db_argv, db);
|
2017-09-06 09:58:29 +10:00
|
|
|
if (result == ISC_R_SUCCESS)
|
|
|
|
dns_db_setservestalettl(*db, cache->serve_stale_ttl);
|
|
|
|
return (result);
|
2001-04-11 20:37:50 +00:00
|
|
|
}
|
|
|
|
|
2000-08-01 01:33:37 +00:00
|
|
|
isc_result_t
|
2018-04-03 13:10:15 +02:00
|
|
|
dns_cache_create(isc_mem_t *cmctx, isc_mem_t *hmctx, isc_taskmgr_t *taskmgr,
|
2008-02-07 23:46:54 +00:00
|
|
|
isc_timermgr_t *timermgr, dns_rdataclass_t rdclass,
|
2018-04-03 13:10:15 +02:00
|
|
|
const char *cachename, const char *db_type,
|
|
|
|
unsigned int db_argc, char **db_argv, dns_cache_t **cachep)
|
1999-11-29 17:58:39 +00:00
|
|
|
{
|
2008-02-07 23:46:54 +00:00
|
|
|
isc_result_t result;
|
|
|
|
dns_cache_t *cache;
|
2020-02-12 13:59:18 +01:00
|
|
|
int i, extra = 0;
|
|
|
|
isc_task_t * dbtask;
|
2008-02-07 23:46:54 +00:00
|
|
|
|
|
|
|
REQUIRE(cachep != NULL);
|
|
|
|
REQUIRE(*cachep == NULL);
|
2011-03-03 04:42:25 +00:00
|
|
|
REQUIRE(cmctx != NULL);
|
|
|
|
REQUIRE(hmctx != NULL);
|
2009-01-09 22:24:37 +00:00
|
|
|
REQUIRE(cachename != NULL);
|
2008-02-07 23:46:54 +00:00
|
|
|
|
2011-03-03 04:42:25 +00:00
|
|
|
cache = isc_mem_get(cmctx, sizeof(*cache));
|
2008-02-07 23:46:54 +00:00
|
|
|
|
2011-03-03 04:42:25 +00:00
|
|
|
cache->mctx = cache->hmctx = NULL;
|
|
|
|
isc_mem_attach(cmctx, &cache->mctx);
|
|
|
|
isc_mem_attach(hmctx, &cache->hmctx);
|
2008-02-07 23:46:54 +00:00
|
|
|
|
2009-01-09 22:24:37 +00:00
|
|
|
cache->name = NULL;
|
|
|
|
if (cachename != NULL) {
|
2011-03-03 04:42:25 +00:00
|
|
|
cache->name = isc_mem_strdup(cmctx, cachename);
|
2009-01-09 22:24:37 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&cache->lock);
|
|
|
|
isc_mutex_init(&cache->filelock);
|
2008-02-07 23:46:54 +00:00
|
|
|
|
2019-05-17 13:47:00 +02:00
|
|
|
isc_refcount_init(&cache->references, 1);
|
2019-08-26 14:19:45 +10:00
|
|
|
isc_refcount_init(&cache->live_tasks, 1);
|
2008-02-07 23:46:54 +00:00
|
|
|
cache->rdclass = rdclass;
|
2017-11-23 16:11:49 +11:00
|
|
|
cache->serve_stale_ttl = 0;
|
2008-02-07 23:46:54 +00:00
|
|
|
|
2012-05-14 10:06:05 -07:00
|
|
|
cache->stats = NULL;
|
|
|
|
result = isc_stats_create(cmctx, &cache->stats,
|
|
|
|
dns_cachestatscounter_max);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_filelock;
|
|
|
|
|
2011-03-03 04:42:25 +00:00
|
|
|
cache->db_type = isc_mem_strdup(cmctx, db_type);
|
2008-02-07 23:46:54 +00:00
|
|
|
|
2011-03-03 04:42:25 +00:00
|
|
|
/*
|
|
|
|
* For databases of type "rbt" we pass hmctx to dns_db_create()
|
|
|
|
* via cache->db_argv, followed by the rest of the arguments in
|
|
|
|
* db_argv (of which there really shouldn't be any).
|
|
|
|
*/
|
|
|
|
if (strcmp(cache->db_type, "rbt") == 0)
|
|
|
|
extra = 1;
|
|
|
|
|
|
|
|
cache->db_argc = db_argc + extra;
|
|
|
|
cache->db_argv = NULL;
|
|
|
|
|
|
|
|
if (cache->db_argc != 0) {
|
2020-02-12 13:59:18 +01:00
|
|
|
cache->db_argv =
|
|
|
|
isc_mem_get(cmctx, cache->db_argc * sizeof(char *));
|
2011-03-03 04:42:25 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
for (i = 0; i < cache->db_argc; i++)
|
|
|
|
cache->db_argv[i] = NULL;
|
2011-03-03 04:42:25 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cache->db_argv[0] = (char *)hmctx;
|
2011-03-03 04:42:25 +00:00
|
|
|
for (i = extra; i < cache->db_argc; i++) {
|
2020-02-12 13:59:18 +01:00
|
|
|
cache->db_argv[i] =
|
|
|
|
isc_mem_strdup(cmctx, db_argv[i - extra]);
|
2008-02-07 23:46:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-03-03 04:42:25 +00:00
|
|
|
/*
|
|
|
|
* Create the database
|
|
|
|
*/
|
2008-02-07 23:46:54 +00:00
|
|
|
cache->db = NULL;
|
|
|
|
result = cache_create_db(cache, &cache->db);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_dbargv;
|
2009-05-06 22:53:54 +00:00
|
|
|
if (taskmgr != NULL) {
|
|
|
|
dbtask = NULL;
|
|
|
|
result = isc_task_create(taskmgr, 1, &dbtask);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_db;
|
2014-06-18 16:47:22 -07:00
|
|
|
|
|
|
|
isc_task_setname(dbtask, "cache_dbtask", NULL);
|
2009-05-06 22:53:54 +00:00
|
|
|
dns_db_settask(cache->db, dbtask);
|
|
|
|
isc_task_detach(&dbtask);
|
|
|
|
}
|
2008-02-07 23:46:54 +00:00
|
|
|
|
|
|
|
cache->filename = NULL;
|
|
|
|
|
|
|
|
cache->magic = CACHE_MAGIC;
|
|
|
|
|
2008-05-01 18:23:07 +00:00
|
|
|
/*
|
|
|
|
* RBT-type cache DB has its own mechanism of cache cleaning and doesn't
|
|
|
|
* need the control of the generic cleaner.
|
|
|
|
*/
|
|
|
|
if (strcmp(db_type, "rbt") == 0)
|
|
|
|
result = cache_cleaner_init(cache, NULL, NULL, &cache->cleaner);
|
|
|
|
else {
|
|
|
|
result = cache_cleaner_init(cache, taskmgr, timermgr,
|
|
|
|
&cache->cleaner);
|
|
|
|
}
|
2008-02-07 23:46:54 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_db;
|
|
|
|
|
2012-05-14 10:06:05 -07:00
|
|
|
result = dns_db_setcachestats(cache->db, cache->stats);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_db;
|
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
*cachep = cache;
|
|
|
|
return (ISC_R_SUCCESS);
|
2000-02-03 18:59:24 +00:00
|
|
|
|
2018-11-16 15:33:22 +01:00
|
|
|
cleanup_db:
|
2008-02-07 23:46:54 +00:00
|
|
|
dns_db_detach(&cache->db);
|
2018-11-16 15:33:22 +01:00
|
|
|
cleanup_dbargv:
|
2011-03-03 04:42:25 +00:00
|
|
|
for (i = extra; i < cache->db_argc; i++)
|
2008-02-07 23:46:54 +00:00
|
|
|
if (cache->db_argv[i] != NULL)
|
2011-03-03 04:42:25 +00:00
|
|
|
isc_mem_free(cmctx, cache->db_argv[i]);
|
2008-02-07 23:46:54 +00:00
|
|
|
if (cache->db_argv != NULL)
|
2011-03-03 04:42:25 +00:00
|
|
|
isc_mem_put(cmctx, cache->db_argv,
|
2008-02-07 23:46:54 +00:00
|
|
|
cache->db_argc * sizeof(char *));
|
2011-03-03 04:42:25 +00:00
|
|
|
isc_mem_free(cmctx, cache->db_type);
|
2018-11-16 15:33:22 +01:00
|
|
|
cleanup_filelock:
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&cache->filelock);
|
2012-05-14 10:06:05 -07:00
|
|
|
isc_stats_detach(&cache->stats);
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&cache->lock);
|
2018-11-16 15:33:22 +01:00
|
|
|
if (cache->name != NULL) {
|
2011-03-03 04:42:25 +00:00
|
|
|
isc_mem_free(cmctx, cache->name);
|
2018-11-16 15:33:22 +01:00
|
|
|
}
|
2011-03-03 04:42:25 +00:00
|
|
|
isc_mem_detach(&cache->hmctx);
|
|
|
|
isc_mem_putanddetach(&cache->mctx, cache, sizeof(*cache));
|
2008-02-07 23:46:54 +00:00
|
|
|
return (result);
|
1999-11-29 17:58:39 +00:00
|
|
|
}
|
|
|
|
|
2000-08-01 01:33:37 +00:00
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
cache_free(dns_cache_t *cache)
|
|
|
|
{
|
2008-02-07 23:46:54 +00:00
|
|
|
REQUIRE(VALID_CACHE(cache));
|
2019-08-26 14:19:45 +10:00
|
|
|
|
|
|
|
isc_refcount_destroy(&cache->references);
|
|
|
|
isc_refcount_destroy(&cache->live_tasks);
|
2000-08-31 12:15:17 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
isc_mem_setwater(cache->mctx, NULL, NULL, 0, 0);
|
1999-11-29 17:58:39 +00:00
|
|
|
|
2019-08-26 14:19:45 +10:00
|
|
|
if (cache->cleaner.task != NULL) {
|
2008-02-07 23:46:54 +00:00
|
|
|
isc_task_detach(&cache->cleaner.task);
|
2019-08-26 14:19:45 +10:00
|
|
|
}
|
1999-11-29 17:58:39 +00:00
|
|
|
|
2019-08-26 14:19:45 +10:00
|
|
|
if (cache->cleaner.overmem_event != NULL) {
|
2008-05-01 18:23:07 +00:00
|
|
|
isc_event_free(&cache->cleaner.overmem_event);
|
2019-08-26 14:19:45 +10:00
|
|
|
}
|
2008-05-01 18:23:07 +00:00
|
|
|
|
2019-08-26 14:19:45 +10:00
|
|
|
if (cache->cleaner.resched_event != NULL) {
|
2008-05-01 18:23:07 +00:00
|
|
|
isc_event_free(&cache->cleaner.resched_event);
|
2019-08-26 14:19:45 +10:00
|
|
|
}
|
2008-05-01 18:23:07 +00:00
|
|
|
|
2019-08-26 14:19:45 +10:00
|
|
|
if (cache->cleaner.iterator != NULL) {
|
2008-05-01 18:23:07 +00:00
|
|
|
dns_dbiterator_destroy(&cache->cleaner.iterator);
|
2019-08-26 14:19:45 +10:00
|
|
|
}
|
2008-05-01 18:23:07 +00:00
|
|
|
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&cache->cleaner.lock);
|
2001-10-23 00:56:33 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
if (cache->filename) {
|
|
|
|
isc_mem_free(cache->mctx, cache->filename);
|
|
|
|
cache->filename = NULL;
|
|
|
|
}
|
1999-11-29 17:58:39 +00:00
|
|
|
|
2019-08-26 14:19:45 +10:00
|
|
|
if (cache->db != NULL) {
|
2008-02-07 23:46:54 +00:00
|
|
|
dns_db_detach(&cache->db);
|
2019-08-26 14:19:45 +10:00
|
|
|
}
|
1999-11-29 17:58:39 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
if (cache->db_argv != NULL) {
|
2011-03-03 04:42:25 +00:00
|
|
|
/*
|
|
|
|
* We don't free db_argv[0] in "rbt" cache databases
|
|
|
|
* as it's a pointer to hmctx
|
|
|
|
*/
|
|
|
|
int extra = 0;
|
2019-08-26 14:19:45 +10:00
|
|
|
if (strcmp(cache->db_type, "rbt") == 0) {
|
2011-03-03 04:42:25 +00:00
|
|
|
extra = 1;
|
2019-08-26 14:19:45 +10:00
|
|
|
}
|
2019-07-23 08:27:30 -04:00
|
|
|
for (int i = extra; i < cache->db_argc; i++) {
|
2019-08-26 14:19:45 +10:00
|
|
|
if (cache->db_argv[i] != NULL) {
|
2019-07-23 08:27:30 -04:00
|
|
|
isc_mem_free(cache->mctx, cache->db_argv[i]);
|
2019-08-26 14:19:45 +10:00
|
|
|
}
|
|
|
|
}
|
2008-02-07 23:46:54 +00:00
|
|
|
isc_mem_put(cache->mctx, cache->db_argv,
|
|
|
|
cache->db_argc * sizeof(char *));
|
|
|
|
}
|
2001-04-11 20:37:50 +00:00
|
|
|
|
2019-08-26 14:19:45 +10:00
|
|
|
if (cache->db_type != NULL) {
|
2008-02-07 23:46:54 +00:00
|
|
|
isc_mem_free(cache->mctx, cache->db_type);
|
2019-08-26 14:19:45 +10:00
|
|
|
}
|
2001-04-11 20:37:50 +00:00
|
|
|
|
2019-08-26 14:19:45 +10:00
|
|
|
if (cache->name != NULL) {
|
2009-01-09 22:24:37 +00:00
|
|
|
isc_mem_free(cache->mctx, cache->name);
|
2019-08-26 14:19:45 +10:00
|
|
|
}
|
2009-01-09 22:24:37 +00:00
|
|
|
|
2019-08-26 14:19:45 +10:00
|
|
|
if (cache->stats != NULL) {
|
2012-05-14 10:06:05 -07:00
|
|
|
isc_stats_detach(&cache->stats);
|
2019-08-26 14:19:45 +10:00
|
|
|
}
|
2012-05-14 10:06:05 -07:00
|
|
|
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&cache->lock);
|
|
|
|
isc_mutex_destroy(&cache->filelock);
|
2011-03-03 04:42:25 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
cache->magic = 0;
|
2011-03-03 04:42:25 +00:00
|
|
|
isc_mem_detach(&cache->hmctx);
|
|
|
|
isc_mem_putanddetach(&cache->mctx, cache, sizeof(*cache));
|
2000-08-01 01:33:37 +00:00
|
|
|
}
|
1999-11-29 17:58:39 +00:00
|
|
|
|
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_attach(dns_cache_t *cache, dns_cache_t **targetp)
|
|
|
|
{
|
2008-02-07 23:46:54 +00:00
|
|
|
REQUIRE(VALID_CACHE(cache));
|
|
|
|
REQUIRE(targetp != NULL && *targetp == NULL);
|
1999-11-29 17:58:39 +00:00
|
|
|
|
2019-05-17 13:47:00 +02:00
|
|
|
isc_refcount_increment(&cache->references);
|
1999-11-29 17:58:39 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
*targetp = cache;
|
1999-11-29 17:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_detach(dns_cache_t **cachep)
|
|
|
|
{
|
2008-02-07 23:46:54 +00:00
|
|
|
dns_cache_t *cache;
|
|
|
|
|
|
|
|
REQUIRE(cachep != NULL);
|
|
|
|
cache = *cachep;
|
|
|
|
*cachep = NULL;
|
2020-02-08 04:37:54 -08:00
|
|
|
REQUIRE(VALID_CACHE(cache));
|
2008-02-07 23:46:54 +00:00
|
|
|
|
2019-05-17 13:47:00 +02:00
|
|
|
if (isc_refcount_decrement(&cache->references) == 1) {
|
|
|
|
cache->cleaner.overmem = false;
|
2008-02-07 23:46:54 +00:00
|
|
|
/*
|
|
|
|
* When the cache is shut down, dump it to a file if one is
|
|
|
|
* specified.
|
|
|
|
*/
|
|
|
|
isc_result_t result = dns_cache_dump(cache);
|
2019-08-26 14:19:45 +10:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
2008-02-07 23:46:54 +00:00
|
|
|
isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
|
|
|
|
DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
|
|
|
|
"error dumping cache: %s ",
|
|
|
|
isc_result_totext(result));
|
2019-08-26 14:19:45 +10:00
|
|
|
}
|
2008-02-07 23:46:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the cleaner task exists, let it free the cache.
|
|
|
|
*/
|
2019-08-26 14:19:45 +10:00
|
|
|
if (isc_refcount_decrement(&cache->live_tasks) > 1) {
|
2008-02-07 23:46:54 +00:00
|
|
|
isc_task_shutdown(cache->cleaner.task);
|
2019-08-26 14:19:45 +10:00
|
|
|
} else {
|
|
|
|
cache_free(cache);
|
2008-02-07 23:46:54 +00:00
|
|
|
}
|
2019-05-17 13:47:00 +02:00
|
|
|
}
|
1999-11-29 17:58:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_attachdb(dns_cache_t *cache, dns_db_t **dbp)
|
|
|
|
{
|
2008-02-07 23:46:54 +00:00
|
|
|
REQUIRE(VALID_CACHE(cache));
|
|
|
|
REQUIRE(dbp != NULL && *dbp == NULL);
|
|
|
|
REQUIRE(cache->db != NULL);
|
2001-06-05 22:27:51 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
LOCK(&cache->lock);
|
|
|
|
dns_db_attach(cache->db, dbp);
|
|
|
|
UNLOCK(&cache->lock);
|
1999-11-29 17:58:39 +00:00
|
|
|
}
|
|
|
|
|
1999-12-22 17:37:31 +00:00
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_setfilename(dns_cache_t *cache, const char *filename)
|
|
|
|
{
|
2008-02-07 23:46:54 +00:00
|
|
|
char *newname;
|
2001-01-12 22:22:17 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
REQUIRE(VALID_CACHE(cache));
|
|
|
|
REQUIRE(filename != NULL);
|
2001-01-12 22:22:17 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
newname = isc_mem_strdup(cache->mctx, filename);
|
2001-06-05 22:27:51 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
LOCK(&cache->filelock);
|
|
|
|
if (cache->filename)
|
|
|
|
isc_mem_free(cache->mctx, cache->filename);
|
|
|
|
cache->filename = newname;
|
|
|
|
UNLOCK(&cache->filelock);
|
2001-06-05 22:27:51 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
1999-11-29 17:58:39 +00:00
|
|
|
}
|
|
|
|
|
1999-12-22 17:37:31 +00:00
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_load(dns_cache_t *cache)
|
|
|
|
{
|
2008-02-07 23:46:54 +00:00
|
|
|
isc_result_t result;
|
2001-01-12 22:22:17 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
REQUIRE(VALID_CACHE(cache));
|
2001-01-12 22:22:17 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
if (cache->filename == NULL)
|
|
|
|
return (ISC_R_SUCCESS);
|
2001-06-05 22:27:51 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
LOCK(&cache->filelock);
|
2020-02-12 13:59:18 +01:00
|
|
|
result = dns_db_load(cache->db, cache->filename, dns_masterformat_text,
|
|
|
|
0);
|
2008-02-07 23:46:54 +00:00
|
|
|
UNLOCK(&cache->filelock);
|
2001-06-05 22:27:51 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
return (result);
|
1999-11-29 17:58:39 +00:00
|
|
|
}
|
|
|
|
|
1999-12-22 17:37:31 +00:00
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_dump(dns_cache_t *cache)
|
|
|
|
{
|
2008-02-07 23:46:54 +00:00
|
|
|
isc_result_t result;
|
2001-01-12 22:22:17 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
REQUIRE(VALID_CACHE(cache));
|
1999-11-29 17:58:39 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
if (cache->filename == NULL)
|
|
|
|
return (ISC_R_SUCCESS);
|
2001-06-05 22:27:51 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
LOCK(&cache->filelock);
|
|
|
|
result = dns_master_dump(cache->mctx, cache->db, NULL,
|
2018-04-03 13:09:55 +02:00
|
|
|
&dns_master_style_cache, cache->filename,
|
|
|
|
dns_masterformat_text, NULL);
|
2008-02-07 23:46:54 +00:00
|
|
|
UNLOCK(&cache->filelock);
|
2009-11-06 04:12:15 +00:00
|
|
|
return (result);
|
2001-01-12 22:22:17 +00:00
|
|
|
}
|
1999-11-29 17:58:39 +00:00
|
|
|
|
2009-01-09 22:24:37 +00:00
|
|
|
const char *
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_getname(dns_cache_t *cache)
|
|
|
|
{
|
2009-01-09 22:24:37 +00:00
|
|
|
REQUIRE(VALID_CACHE(cache));
|
|
|
|
|
|
|
|
return (cache->name);
|
|
|
|
}
|
|
|
|
|
1999-11-29 17:58:39 +00:00
|
|
|
/*
|
2000-08-01 01:33:37 +00:00
|
|
|
* Initialize the cache cleaner object at *cleaner.
|
1999-11-29 17:58:39 +00:00
|
|
|
* Space for the object must be allocated by the caller.
|
|
|
|
*/
|
|
|
|
|
1999-12-22 17:37:31 +00:00
|
|
|
static isc_result_t
|
2000-08-01 01:33:37 +00:00
|
|
|
cache_cleaner_init(dns_cache_t *cache, isc_taskmgr_t *taskmgr,
|
2008-02-07 23:46:54 +00:00
|
|
|
isc_timermgr_t *timermgr, cache_cleaner_t *cleaner)
|
1999-11-29 17:58:39 +00:00
|
|
|
{
|
2008-02-07 23:46:54 +00:00
|
|
|
isc_result_t result;
|
2007-10-19 17:15:53 +00:00
|
|
|
|
2018-11-16 15:33:22 +01:00
|
|
|
isc_mutex_init(&cleaner->lock);
|
2008-02-07 23:46:54 +00:00
|
|
|
|
|
|
|
cleaner->increment = DNS_CACHE_CLEANERINCREMENT;
|
|
|
|
cleaner->state = cleaner_s_idle;
|
|
|
|
cleaner->cache = cache;
|
2008-05-01 18:23:07 +00:00
|
|
|
cleaner->iterator = NULL;
|
2018-04-17 08:29:14 -07:00
|
|
|
cleaner->overmem = false;
|
|
|
|
cleaner->replaceiterator = false;
|
2008-02-07 23:46:54 +00:00
|
|
|
|
|
|
|
cleaner->task = NULL;
|
2008-05-01 18:23:07 +00:00
|
|
|
cleaner->resched_event = NULL;
|
|
|
|
cleaner->overmem_event = NULL;
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
result = dns_db_createiterator(cleaner->cache->db, false,
|
2008-05-01 18:23:07 +00:00
|
|
|
&cleaner->iterator);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup;
|
2008-02-07 23:46:54 +00:00
|
|
|
|
|
|
|
if (taskmgr != NULL && timermgr != NULL) {
|
|
|
|
result = isc_task_create(taskmgr, 1, &cleaner->task);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"isc_task_create() failed: %s",
|
|
|
|
dns_result_totext(result));
|
|
|
|
result = ISC_R_UNEXPECTED;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2019-08-26 14:19:45 +10:00
|
|
|
isc_refcount_increment(&cleaner->cache->live_tasks);
|
2008-02-07 23:46:54 +00:00
|
|
|
isc_task_setname(cleaner->task, "cachecleaner", cleaner);
|
|
|
|
|
|
|
|
result = isc_task_onshutdown(cleaner->task,
|
|
|
|
cleaner_shutdown_action, cache);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
2019-08-26 14:19:45 +10:00
|
|
|
isc_refcount_decrement(&cleaner->cache->live_tasks);
|
2008-02-07 23:46:54 +00:00
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"cache cleaner: "
|
|
|
|
"isc_task_onshutdown() failed: %s",
|
|
|
|
dns_result_totext(result));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleaner->resched_event = isc_event_allocate(
|
|
|
|
cache->mctx, cleaner, DNS_EVENT_CACHECLEAN,
|
|
|
|
incremental_cleaning_action, cleaner,
|
|
|
|
sizeof(isc_event_t));
|
|
|
|
|
|
|
|
cleaner->overmem_event = isc_event_allocate(
|
|
|
|
cache->mctx, cleaner, DNS_EVENT_CACHEOVERMEM,
|
|
|
|
overmem_cleaning_action, cleaner, sizeof(isc_event_t));
|
2008-02-07 23:46:54 +00:00
|
|
|
}
|
2007-10-19 17:15:53 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
2008-05-01 18:23:07 +00:00
|
|
|
if (cleaner->overmem_event != NULL)
|
|
|
|
isc_event_free(&cleaner->overmem_event);
|
|
|
|
if (cleaner->resched_event != NULL)
|
|
|
|
isc_event_free(&cleaner->resched_event);
|
2008-02-07 23:46:54 +00:00
|
|
|
if (cleaner->task != NULL)
|
|
|
|
isc_task_detach(&cleaner->task);
|
2008-05-01 18:23:07 +00:00
|
|
|
if (cleaner->iterator != NULL)
|
|
|
|
dns_dbiterator_destroy(&cleaner->iterator);
|
2018-11-19 10:31:09 +00:00
|
|
|
isc_mutex_destroy(&cleaner->lock);
|
2018-11-16 15:33:22 +01:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
return (result);
|
2000-01-06 01:12:20 +00:00
|
|
|
}
|
|
|
|
|
2008-05-01 18:23:07 +00:00
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
begin_cleaning(cache_cleaner_t *cleaner)
|
|
|
|
{
|
2008-05-01 18:23:07 +00:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
REQUIRE(CLEANER_IDLE(cleaner));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Create an iterator, if it does not already exist, and
|
|
|
|
* position it at the beginning of the cache.
|
|
|
|
*/
|
|
|
|
if (cleaner->iterator == NULL)
|
2018-04-17 08:29:14 -07:00
|
|
|
result = dns_db_createiterator(cleaner->cache->db, false,
|
2008-05-01 18:23:07 +00:00
|
|
|
&cleaner->iterator);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
|
|
|
|
DNS_LOGMODULE_CACHE, ISC_LOG_WARNING,
|
|
|
|
"cache cleaner could not create "
|
2020-02-12 13:59:18 +01:00
|
|
|
"iterator: %s",
|
|
|
|
isc_result_totext(result));
|
2008-05-01 18:23:07 +00:00
|
|
|
else {
|
2018-04-17 08:29:14 -07:00
|
|
|
dns_dbiterator_setcleanmode(cleaner->iterator, true);
|
2008-05-01 18:23:07 +00:00
|
|
|
result = dns_dbiterator_first(cleaner->iterator);
|
|
|
|
}
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
/*
|
|
|
|
* If the result is ISC_R_NOMORE, the database is empty,
|
|
|
|
* so there is nothing to be cleaned.
|
|
|
|
*/
|
|
|
|
if (result != ISC_R_NOMORE && cleaner->iterator != NULL) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"cache cleaner: "
|
|
|
|
"dns_dbiterator_first() failed: %s",
|
|
|
|
dns_result_totext(result));
|
|
|
|
dns_dbiterator_destroy(&cleaner->iterator);
|
|
|
|
} else if (cleaner->iterator != NULL) {
|
|
|
|
result = dns_dbiterator_pause(cleaner->iterator);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Pause the iterator to free its lock.
|
|
|
|
*/
|
|
|
|
result = dns_dbiterator_pause(cleaner->iterator);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_log_write(
|
|
|
|
dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_CACHE,
|
|
|
|
ISC_LOG_DEBUG(1), "begin cache cleaning, mem inuse %lu",
|
|
|
|
(unsigned long)isc_mem_inuse(cleaner->cache->mctx));
|
2008-05-01 18:23:07 +00:00
|
|
|
cleaner->state = cleaner_s_busy;
|
|
|
|
isc_task_send(cleaner->task, &cleaner->resched_event);
|
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
end_cleaning(cache_cleaner_t *cleaner, isc_event_t *event)
|
|
|
|
{
|
2008-05-01 18:23:07 +00:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
REQUIRE(CLEANER_BUSY(cleaner));
|
|
|
|
REQUIRE(event != NULL);
|
|
|
|
|
|
|
|
result = dns_dbiterator_pause(cleaner->iterator);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
dns_dbiterator_destroy(&cleaner->iterator);
|
|
|
|
|
|
|
|
isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_CACHE,
|
|
|
|
ISC_LOG_DEBUG(1), "end cache cleaning, mem inuse %lu",
|
|
|
|
(unsigned long)isc_mem_inuse(cleaner->cache->mctx));
|
|
|
|
|
|
|
|
cleaner->state = cleaner_s_idle;
|
|
|
|
cleaner->resched_event = event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is called when the cache either surpasses its upper limit
|
|
|
|
* or shrinks beyond its lower limit.
|
|
|
|
*/
|
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
overmem_cleaning_action(isc_task_t *task, isc_event_t *event)
|
|
|
|
{
|
2008-05-01 18:23:07 +00:00
|
|
|
cache_cleaner_t *cleaner = event->ev_arg;
|
2020-02-12 13:59:18 +01:00
|
|
|
bool want_cleaning = false;
|
2008-05-01 18:23:07 +00:00
|
|
|
|
|
|
|
UNUSED(task);
|
|
|
|
|
|
|
|
INSIST(task == cleaner->task);
|
|
|
|
INSIST(event->ev_type == DNS_EVENT_CACHEOVERMEM);
|
|
|
|
INSIST(cleaner->overmem_event == NULL);
|
|
|
|
|
|
|
|
isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_CACHE,
|
2020-02-12 13:59:18 +01:00
|
|
|
ISC_LOG_DEBUG(1),
|
|
|
|
"overmem_cleaning_action called, "
|
|
|
|
"overmem = %d, state = %d",
|
|
|
|
cleaner->overmem, cleaner->state);
|
2008-05-01 18:23:07 +00:00
|
|
|
|
|
|
|
LOCK(&cleaner->lock);
|
|
|
|
|
|
|
|
if (cleaner->overmem) {
|
|
|
|
if (cleaner->state == cleaner_s_idle)
|
2018-04-17 08:29:14 -07:00
|
|
|
want_cleaning = true;
|
2008-05-01 18:23:07 +00:00
|
|
|
} else {
|
|
|
|
if (cleaner->state == cleaner_s_busy)
|
|
|
|
/*
|
|
|
|
* end_cleaning() can't be called here because
|
|
|
|
* then both cleaner->overmem_event and
|
|
|
|
* cleaner->resched_event will point to this
|
|
|
|
* event. Set the state to done, and then
|
|
|
|
* when the incremental_cleaning_action() event
|
|
|
|
* is posted, it will handle the end_cleaning.
|
|
|
|
*/
|
|
|
|
cleaner->state = cleaner_s_done;
|
|
|
|
}
|
|
|
|
|
|
|
|
cleaner->overmem_event = event;
|
|
|
|
|
|
|
|
UNLOCK(&cleaner->lock);
|
|
|
|
|
|
|
|
if (want_cleaning)
|
|
|
|
begin_cleaning(cleaner);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Do incremental cleaning.
|
|
|
|
*/
|
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
incremental_cleaning_action(isc_task_t *task, isc_event_t *event)
|
|
|
|
{
|
2008-05-01 18:23:07 +00:00
|
|
|
cache_cleaner_t *cleaner = event->ev_arg;
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_result_t result;
|
|
|
|
unsigned int n_names;
|
|
|
|
isc_time_t start;
|
2008-05-01 18:23:07 +00:00
|
|
|
|
|
|
|
UNUSED(task);
|
|
|
|
|
|
|
|
INSIST(task == cleaner->task);
|
|
|
|
INSIST(event->ev_type == DNS_EVENT_CACHECLEAN);
|
|
|
|
|
|
|
|
if (cleaner->state == cleaner_s_done) {
|
|
|
|
cleaner->state = cleaner_s_busy;
|
|
|
|
end_cleaning(cleaner, event);
|
|
|
|
LOCK(&cleaner->cache->lock);
|
|
|
|
LOCK(&cleaner->lock);
|
|
|
|
if (cleaner->replaceiterator) {
|
|
|
|
dns_dbiterator_destroy(&cleaner->iterator);
|
2020-02-12 13:59:18 +01:00
|
|
|
(void)dns_db_createiterator(cleaner->cache->db, false,
|
|
|
|
&cleaner->iterator);
|
2018-04-17 08:29:14 -07:00
|
|
|
cleaner->replaceiterator = false;
|
2008-05-01 18:23:07 +00:00
|
|
|
}
|
|
|
|
UNLOCK(&cleaner->lock);
|
|
|
|
UNLOCK(&cleaner->cache->lock);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
INSIST(CLEANER_BUSY(cleaner));
|
|
|
|
|
|
|
|
n_names = cleaner->increment;
|
|
|
|
|
|
|
|
REQUIRE(DNS_DBITERATOR_VALID(cleaner->iterator));
|
|
|
|
|
|
|
|
isc_time_now(&start);
|
|
|
|
while (n_names-- > 0) {
|
|
|
|
dns_dbnode_t *node = NULL;
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
result = dns_dbiterator_current(cleaner->iterator, &node, NULL);
|
2008-05-01 18:23:07 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2020-02-12 13:59:18 +01:00
|
|
|
"cache cleaner: "
|
|
|
|
"dns_dbiterator_current() "
|
|
|
|
"failed: %s",
|
|
|
|
dns_result_totext(result));
|
2008-05-01 18:23:07 +00:00
|
|
|
|
|
|
|
end_cleaning(cleaner, event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The node was not needed, but was required by
|
|
|
|
* dns_dbiterator_current(). Give up its reference.
|
|
|
|
*/
|
|
|
|
dns_db_detachnode(cleaner->cache->db, &node);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Step to the next node.
|
|
|
|
*/
|
|
|
|
result = dns_dbiterator_next(cleaner->iterator);
|
|
|
|
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
/*
|
|
|
|
* Either the end was reached (ISC_R_NOMORE) or
|
|
|
|
* some error was signaled. If the cache is still
|
|
|
|
* overmem and no error was encountered,
|
|
|
|
* keep trying to clean it, otherwise stop cleaning.
|
|
|
|
*/
|
|
|
|
if (result != ISC_R_NOMORE)
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"cache cleaner: "
|
|
|
|
"dns_dbiterator_next() "
|
|
|
|
"failed: %s",
|
|
|
|
dns_result_totext(result));
|
|
|
|
else if (cleaner->overmem) {
|
2020-02-12 13:59:18 +01:00
|
|
|
result =
|
|
|
|
dns_dbiterator_first(cleaner->iterator);
|
2008-05-01 18:23:07 +00:00
|
|
|
if (result == ISC_R_SUCCESS) {
|
|
|
|
isc_log_write(dns_lctx,
|
|
|
|
DNS_LOGCATEGORY_DATABASE,
|
|
|
|
DNS_LOGMODULE_CACHE,
|
|
|
|
ISC_LOG_DEBUG(1),
|
|
|
|
"cache cleaner: "
|
|
|
|
"still overmem, "
|
|
|
|
"reset and try again");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
end_cleaning(cleaner, event);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We have successfully performed a cleaning increment but have
|
|
|
|
* not gone through the entire cache. Free the iterator locks
|
|
|
|
* and reschedule another batch. If it fails, just try to continue
|
|
|
|
* anyway.
|
|
|
|
*/
|
|
|
|
result = dns_dbiterator_pause(cleaner->iterator);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_CACHE,
|
2020-02-12 13:59:18 +01:00
|
|
|
ISC_LOG_DEBUG(1),
|
|
|
|
"cache cleaner: checked %u nodes, "
|
|
|
|
"mem inuse %lu, sleeping",
|
|
|
|
cleaner->increment,
|
2008-05-01 18:23:07 +00:00
|
|
|
(unsigned long)isc_mem_inuse(cleaner->cache->mctx));
|
|
|
|
|
|
|
|
isc_task_send(task, &event);
|
|
|
|
INSIST(CLEANER_BUSY(cleaner));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
1999-11-29 17:58:39 +00:00
|
|
|
/*
|
2000-08-01 01:33:37 +00:00
|
|
|
* Do immediate cleaning.
|
1999-11-29 17:58:39 +00:00
|
|
|
*/
|
1999-12-22 17:37:31 +00:00
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_clean(dns_cache_t *cache, isc_stdtime_t now)
|
|
|
|
{
|
|
|
|
isc_result_t result;
|
2008-02-07 23:46:54 +00:00
|
|
|
dns_dbiterator_t *iterator = NULL;
|
|
|
|
|
|
|
|
REQUIRE(VALID_CACHE(cache));
|
|
|
|
|
2008-09-24 02:46:23 +00:00
|
|
|
result = dns_db_createiterator(cache->db, 0, &iterator);
|
2008-02-07 23:46:54 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
result = dns_dbiterator_first(iterator);
|
|
|
|
|
|
|
|
while (result == ISC_R_SUCCESS) {
|
|
|
|
dns_dbnode_t *node = NULL;
|
|
|
|
result = dns_dbiterator_current(iterator, &node,
|
|
|
|
(dns_name_t *)NULL);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check TTLs, mark expired rdatasets stale.
|
|
|
|
*/
|
|
|
|
result = dns_db_expirenode(cache->db, node, now);
|
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
|
|
|
"cache cleaner: dns_db_expirenode() "
|
|
|
|
"failed: %s",
|
|
|
|
dns_result_totext(result));
|
|
|
|
/*
|
|
|
|
* Continue anyway.
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is where the actual freeing takes place.
|
|
|
|
*/
|
|
|
|
dns_db_detachnode(cache->db, &node);
|
|
|
|
|
|
|
|
result = dns_dbiterator_next(iterator);
|
|
|
|
}
|
|
|
|
|
|
|
|
dns_dbiterator_destroy(&iterator);
|
|
|
|
|
|
|
|
if (result == ISC_R_NOMORE)
|
|
|
|
result = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
return (result);
|
1999-11-29 17:58:39 +00:00
|
|
|
}
|
|
|
|
|
2000-08-31 12:15:17 +00:00
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
water(void *arg, int mark)
|
|
|
|
{
|
2008-02-07 23:46:54 +00:00
|
|
|
dns_cache_t *cache = arg;
|
2020-02-12 13:59:18 +01:00
|
|
|
bool overmem = (mark == ISC_MEM_HIWATER);
|
2000-08-31 12:15:17 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
REQUIRE(VALID_CACHE(cache));
|
2001-06-05 22:27:51 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
LOCK(&cache->cleaner.lock);
|
2000-08-31 12:15:17 +00:00
|
|
|
|
2008-02-07 02:41:26 +00:00
|
|
|
if (overmem != cache->cleaner.overmem) {
|
|
|
|
dns_db_overmem(cache->db, overmem);
|
|
|
|
cache->cleaner.overmem = overmem;
|
|
|
|
isc_mem_waterack(cache->mctx, mark);
|
|
|
|
}
|
2001-10-23 00:56:33 +00:00
|
|
|
|
2008-05-01 18:23:07 +00:00
|
|
|
if (cache->cleaner.overmem_event != NULL)
|
|
|
|
isc_task_send(cache->cleaner.task,
|
|
|
|
&cache->cleaner.overmem_event);
|
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
UNLOCK(&cache->cleaner.lock);
|
2000-08-31 12:15:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_setcachesize(dns_cache_t *cache, size_t size)
|
|
|
|
{
|
2013-02-28 09:29:12 -08:00
|
|
|
size_t hiwater, lowater;
|
2008-02-07 23:46:54 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_CACHE(cache));
|
|
|
|
|
|
|
|
/*
|
2009-01-17 14:18:27 +00:00
|
|
|
* Impose a minimum cache size; pathological things happen if there
|
2008-02-07 23:46:54 +00:00
|
|
|
* is too little room.
|
|
|
|
*/
|
2013-03-05 23:41:22 +11:00
|
|
|
if (size != 0U && size < DNS_CACHE_MINSIZE)
|
2008-02-07 23:46:54 +00:00
|
|
|
size = DNS_CACHE_MINSIZE;
|
|
|
|
|
2009-01-09 22:24:37 +00:00
|
|
|
LOCK(&cache->lock);
|
|
|
|
cache->size = size;
|
|
|
|
UNLOCK(&cache->lock);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
hiwater = size - (size >> 3); /* Approximately 7/8ths. */
|
|
|
|
lowater = size - (size >> 2); /* Approximately 3/4ths. */
|
2008-02-07 23:46:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* If the cache was overmem and cleaning, but now with the new limits
|
|
|
|
* it is no longer in an overmem condition, then the next
|
|
|
|
* isc_mem_put for cache memory will do the right thing and trigger
|
|
|
|
* water().
|
|
|
|
*/
|
|
|
|
|
2013-03-05 23:41:22 +11:00
|
|
|
if (size == 0U || hiwater == 0U || lowater == 0U)
|
2008-02-07 23:46:54 +00:00
|
|
|
/*
|
|
|
|
* Disable cache memory limiting.
|
|
|
|
*/
|
|
|
|
isc_mem_setwater(cache->mctx, water, cache, 0, 0);
|
|
|
|
else
|
|
|
|
/*
|
|
|
|
* Establish new cache memory limits (either for the first
|
|
|
|
* time, or replacing other limits).
|
|
|
|
*/
|
|
|
|
isc_mem_setwater(cache->mctx, water, cache, hiwater, lowater);
|
2000-08-31 12:15:17 +00:00
|
|
|
}
|
|
|
|
|
2013-02-28 09:29:12 -08:00
|
|
|
size_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_getcachesize(dns_cache_t *cache)
|
|
|
|
{
|
2013-02-28 09:29:12 -08:00
|
|
|
size_t size;
|
2009-01-09 22:24:37 +00:00
|
|
|
|
|
|
|
REQUIRE(VALID_CACHE(cache));
|
|
|
|
|
|
|
|
LOCK(&cache->lock);
|
|
|
|
size = cache->size;
|
|
|
|
UNLOCK(&cache->lock);
|
|
|
|
|
|
|
|
return (size);
|
|
|
|
}
|
|
|
|
|
2017-09-06 09:58:29 +10:00
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_setservestalettl(dns_cache_t *cache, dns_ttl_t ttl)
|
|
|
|
{
|
2017-09-06 09:58:29 +10:00
|
|
|
REQUIRE(VALID_CACHE(cache));
|
|
|
|
|
|
|
|
LOCK(&cache->lock);
|
|
|
|
cache->serve_stale_ttl = ttl;
|
|
|
|
UNLOCK(&cache->lock);
|
|
|
|
|
|
|
|
(void)dns_db_setservestalettl(cache->db, ttl);
|
|
|
|
}
|
|
|
|
|
|
|
|
dns_ttl_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_getservestalettl(dns_cache_t *cache)
|
|
|
|
{
|
|
|
|
dns_ttl_t ttl;
|
2017-09-06 09:58:29 +10:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
REQUIRE(VALID_CACHE(cache));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Could get it straight from the dns_cache_t, but use db
|
|
|
|
* to confirm the value that the db is really using.
|
|
|
|
*/
|
|
|
|
result = dns_db_getservestalettl(cache->db, &ttl);
|
|
|
|
return result == ISC_R_SUCCESS ? ttl : 0;
|
|
|
|
}
|
|
|
|
|
1999-11-29 17:58:39 +00:00
|
|
|
/*
|
|
|
|
* The cleaner task is shutting down; do the necessary cleanup.
|
|
|
|
*/
|
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
cleaner_shutdown_action(isc_task_t *task, isc_event_t *event)
|
|
|
|
{
|
2008-02-07 23:46:54 +00:00
|
|
|
dns_cache_t *cache = event->ev_arg;
|
2007-10-19 17:15:53 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
UNUSED(task);
|
2000-04-17 19:22:44 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
INSIST(task == cache->cleaner.task);
|
|
|
|
INSIST(event->ev_type == ISC_TASKEVENT_SHUTDOWN);
|
2000-04-17 19:22:44 +00:00
|
|
|
|
2019-08-26 14:19:45 +10:00
|
|
|
if (CLEANER_BUSY(&cache->cleaner)) {
|
2008-05-01 18:23:07 +00:00
|
|
|
end_cleaning(&cache->cleaner, event);
|
2019-08-26 14:19:45 +10:00
|
|
|
} else {
|
2008-05-01 18:23:07 +00:00
|
|
|
isc_event_free(&event);
|
2019-05-17 13:47:00 +02:00
|
|
|
}
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
/* Make sure we don't reschedule anymore. */
|
|
|
|
(void)isc_task_purge(task, NULL, DNS_EVENT_CACHECLEAN, NULL);
|
2000-01-04 23:24:13 +00:00
|
|
|
|
2019-08-26 14:19:45 +10:00
|
|
|
INSIST(isc_refcount_decrement(&cache->live_tasks) == 1);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2019-08-26 14:19:45 +10:00
|
|
|
cache_free(cache);
|
1999-11-29 17:58:39 +00:00
|
|
|
}
|
2001-04-11 20:37:50 +00:00
|
|
|
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_flush(dns_cache_t *cache)
|
|
|
|
{
|
|
|
|
dns_db_t * db = NULL, *olddb;
|
2015-07-03 10:17:33 +10:00
|
|
|
dns_dbiterator_t *dbiterator = NULL, *olddbiterator = NULL;
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_result_t result;
|
2008-02-07 23:46:54 +00:00
|
|
|
|
|
|
|
result = cache_create_db(cache, &db);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
return (result);
|
|
|
|
|
2018-04-17 08:29:14 -07:00
|
|
|
result = dns_db_createiterator(db, false, &dbiterator);
|
2015-07-03 10:17:33 +10:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
|
|
|
dns_db_detach(&db);
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
LOCK(&cache->lock);
|
|
|
|
LOCK(&cache->cleaner.lock);
|
|
|
|
if (cache->cleaner.state == cleaner_s_idle) {
|
2015-07-03 10:17:33 +10:00
|
|
|
olddbiterator = cache->cleaner.iterator;
|
|
|
|
cache->cleaner.iterator = dbiterator;
|
|
|
|
dbiterator = NULL;
|
2008-05-01 18:23:07 +00:00
|
|
|
} else {
|
|
|
|
if (cache->cleaner.state == cleaner_s_busy)
|
|
|
|
cache->cleaner.state = cleaner_s_done;
|
2018-04-17 08:29:14 -07:00
|
|
|
cache->cleaner.replaceiterator = true;
|
2008-02-07 23:46:54 +00:00
|
|
|
}
|
2015-07-03 10:17:33 +10:00
|
|
|
olddb = cache->db;
|
2008-02-07 23:46:54 +00:00
|
|
|
cache->db = db;
|
2012-05-14 12:20:34 -07:00
|
|
|
dns_db_setcachestats(cache->db, cache->stats);
|
2008-02-07 23:46:54 +00:00
|
|
|
UNLOCK(&cache->cleaner.lock);
|
|
|
|
UNLOCK(&cache->lock);
|
|
|
|
|
2015-07-03 10:17:33 +10:00
|
|
|
if (dbiterator != NULL)
|
|
|
|
dns_dbiterator_destroy(&dbiterator);
|
|
|
|
if (olddbiterator != NULL)
|
|
|
|
dns_dbiterator_destroy(&olddbiterator);
|
|
|
|
dns_db_detach(&olddb);
|
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
2001-04-11 20:37:50 +00:00
|
|
|
}
|
2001-11-27 03:10:32 +00:00
|
|
|
|
2011-08-02 20:36:13 +00:00
|
|
|
static isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
clearnode(dns_db_t *db, dns_dbnode_t *node)
|
|
|
|
{
|
|
|
|
isc_result_t result;
|
2008-02-07 23:46:54 +00:00
|
|
|
dns_rdatasetiter_t *iter = NULL;
|
|
|
|
|
2011-08-02 20:36:13 +00:00
|
|
|
result = dns_db_allrdatasets(db, node, NULL, (isc_stdtime_t)0, &iter);
|
2008-02-07 23:46:54 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
2011-08-02 20:36:13 +00:00
|
|
|
return (result);
|
2008-02-07 23:46:54 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
for (result = dns_rdatasetiter_first(iter); result == ISC_R_SUCCESS;
|
|
|
|
result = dns_rdatasetiter_next(iter)) {
|
2008-02-07 23:46:54 +00:00
|
|
|
dns_rdataset_t rdataset;
|
|
|
|
dns_rdataset_init(&rdataset);
|
|
|
|
|
|
|
|
dns_rdatasetiter_current(iter, &rdataset);
|
2020-02-12 13:59:18 +01:00
|
|
|
result = dns_db_deleterdataset(db, node, NULL, rdataset.type,
|
|
|
|
rdataset.covers);
|
2008-02-07 23:46:54 +00:00
|
|
|
dns_rdataset_disassociate(&rdataset);
|
|
|
|
if (result != ISC_R_SUCCESS && result != DNS_R_UNCHANGED)
|
|
|
|
break;
|
|
|
|
}
|
2011-08-02 20:36:13 +00:00
|
|
|
|
2008-02-07 23:46:54 +00:00
|
|
|
if (result == ISC_R_NOMORE)
|
|
|
|
result = ISC_R_SUCCESS;
|
|
|
|
|
|
|
|
dns_rdatasetiter_destroy(&iter);
|
2011-08-02 20:36:13 +00:00
|
|
|
return (result);
|
|
|
|
}
|
2001-11-27 03:10:32 +00:00
|
|
|
|
2011-08-02 20:36:13 +00:00
|
|
|
static isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
cleartree(dns_db_t *db, const dns_name_t *name)
|
|
|
|
{
|
|
|
|
isc_result_t result, answer = ISC_R_SUCCESS;
|
2011-08-02 20:36:13 +00:00
|
|
|
dns_dbiterator_t *iter = NULL;
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_dbnode_t * node = NULL, *top = NULL;
|
|
|
|
dns_fixedname_t fnodename;
|
|
|
|
dns_name_t * nodename;
|
2011-08-02 20:36:13 +00:00
|
|
|
|
2016-03-27 08:25:44 +11:00
|
|
|
/*
|
|
|
|
* Create the node if it doesn't exist so dns_dbiterator_seek()
|
|
|
|
* can find it. We will continue even if this fails.
|
|
|
|
*/
|
2018-04-17 08:29:14 -07:00
|
|
|
(void)dns_db_findnode(db, name, true, &top);
|
2016-03-27 08:25:44 +11:00
|
|
|
|
2018-03-28 14:38:09 +02:00
|
|
|
nodename = dns_fixedname_initname(&fnodename);
|
2011-08-02 20:36:13 +00:00
|
|
|
|
|
|
|
result = dns_db_createiterator(db, 0, &iter);
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
result = dns_dbiterator_seek(iter, name);
|
2016-03-24 11:31:25 +11:00
|
|
|
if (result == DNS_R_PARTIALMATCH)
|
|
|
|
result = dns_dbiterator_next(iter);
|
2011-08-02 20:36:13 +00:00
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup;
|
|
|
|
|
|
|
|
while (result == ISC_R_SUCCESS) {
|
|
|
|
result = dns_dbiterator_current(iter, &node, nodename);
|
2011-08-26 05:12:56 +00:00
|
|
|
if (result == DNS_R_NEWORIGIN)
|
|
|
|
result = ISC_R_SUCCESS;
|
|
|
|
if (result != ISC_R_SUCCESS)
|
2011-08-02 20:36:13 +00:00
|
|
|
goto cleanup;
|
2011-08-26 05:12:56 +00:00
|
|
|
/*
|
|
|
|
* Are we done?
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
if (!dns_name_issubdomain(nodename, name))
|
2011-08-02 20:36:13 +00:00
|
|
|
goto cleanup;
|
|
|
|
|
2011-08-26 05:12:56 +00:00
|
|
|
/*
|
|
|
|
* If clearnode fails record and move onto the next node.
|
|
|
|
*/
|
2011-08-02 20:36:13 +00:00
|
|
|
result = clearnode(db, node);
|
2011-08-26 05:12:56 +00:00
|
|
|
if (result != ISC_R_SUCCESS && answer == ISC_R_SUCCESS)
|
|
|
|
answer = result;
|
2011-08-02 20:36:13 +00:00
|
|
|
dns_db_detachnode(db, &node);
|
|
|
|
result = dns_dbiterator_next(iter);
|
|
|
|
}
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup:
|
2011-08-02 20:36:13 +00:00
|
|
|
if (result == ISC_R_NOMORE || result == ISC_R_NOTFOUND)
|
|
|
|
result = ISC_R_SUCCESS;
|
2011-08-26 05:12:56 +00:00
|
|
|
if (result != ISC_R_SUCCESS && answer == ISC_R_SUCCESS)
|
|
|
|
answer = result;
|
2011-08-02 20:36:13 +00:00
|
|
|
if (node != NULL)
|
|
|
|
dns_db_detachnode(db, &node);
|
|
|
|
if (iter != NULL)
|
|
|
|
dns_dbiterator_destroy(&iter);
|
2016-03-27 08:25:44 +11:00
|
|
|
if (top != NULL)
|
|
|
|
dns_db_detachnode(db, &top);
|
2011-08-02 20:36:13 +00:00
|
|
|
|
2011-08-26 05:12:56 +00:00
|
|
|
return (answer);
|
2011-08-02 20:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_flushname(dns_cache_t *cache, const dns_name_t *name)
|
|
|
|
{
|
2018-04-17 08:29:14 -07:00
|
|
|
return (dns_cache_flushnode(cache, name, false));
|
2011-08-02 20:36:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_flushnode(dns_cache_t *cache, const dns_name_t *name, bool tree)
|
2011-08-02 20:36:13 +00:00
|
|
|
{
|
2020-02-12 13:59:18 +01:00
|
|
|
isc_result_t result;
|
2011-08-02 20:36:13 +00:00
|
|
|
dns_dbnode_t *node = NULL;
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_db_t * db = NULL;
|
2011-08-02 20:36:13 +00:00
|
|
|
|
2016-03-24 11:31:25 +11:00
|
|
|
if (tree && dns_name_equal(name, dns_rootname))
|
2011-08-02 20:36:13 +00:00
|
|
|
return (dns_cache_flush(cache));
|
|
|
|
|
|
|
|
LOCK(&cache->lock);
|
|
|
|
if (cache->db != NULL)
|
|
|
|
dns_db_attach(cache->db, &db);
|
|
|
|
UNLOCK(&cache->lock);
|
|
|
|
if (db == NULL)
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
|
|
|
|
if (tree) {
|
|
|
|
result = cleartree(cache->db, name);
|
|
|
|
} else {
|
2018-04-17 08:29:14 -07:00
|
|
|
result = dns_db_findnode(cache->db, name, false, &node);
|
2011-08-02 20:36:13 +00:00
|
|
|
if (result == ISC_R_NOTFOUND) {
|
|
|
|
result = ISC_R_SUCCESS;
|
|
|
|
goto cleanup_db;
|
|
|
|
}
|
|
|
|
if (result != ISC_R_SUCCESS)
|
|
|
|
goto cleanup_db;
|
|
|
|
result = clearnode(cache->db, node);
|
|
|
|
dns_db_detachnode(cache->db, &node);
|
|
|
|
}
|
2001-11-27 03:10:32 +00:00
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
cleanup_db:
|
2008-02-07 23:46:54 +00:00
|
|
|
dns_db_detach(&db);
|
|
|
|
return (result);
|
2007-10-19 17:15:53 +00:00
|
|
|
}
|
2012-05-14 10:06:05 -07:00
|
|
|
|
|
|
|
isc_stats_t *
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_getstats(dns_cache_t *cache)
|
|
|
|
{
|
2012-05-14 10:06:05 -07:00
|
|
|
REQUIRE(VALID_CACHE(cache));
|
|
|
|
return (cache->stats);
|
|
|
|
}
|
|
|
|
|
2013-07-25 10:51:31 -07:00
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_updatestats(dns_cache_t *cache, isc_result_t result)
|
|
|
|
{
|
2013-07-25 10:51:31 -07:00
|
|
|
REQUIRE(VALID_CACHE(cache));
|
|
|
|
if (cache->stats == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
switch (result) {
|
|
|
|
case ISC_R_SUCCESS:
|
|
|
|
case DNS_R_NCACHENXDOMAIN:
|
|
|
|
case DNS_R_NCACHENXRRSET:
|
|
|
|
case DNS_R_CNAME:
|
|
|
|
case DNS_R_DNAME:
|
|
|
|
case DNS_R_GLUE:
|
|
|
|
case DNS_R_ZONECUT:
|
|
|
|
isc_stats_increment(cache->stats,
|
|
|
|
dns_cachestatscounter_queryhits);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
isc_stats_increment(cache->stats,
|
|
|
|
dns_cachestatscounter_querymisses);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-14 10:06:05 -07:00
|
|
|
/*
|
|
|
|
* XXX: Much of the following code has been copied in from statschannel.c.
|
|
|
|
* We should refactor this into a generic function in stats.c that can be
|
|
|
|
* called from both places.
|
|
|
|
*/
|
2020-02-12 13:59:18 +01:00
|
|
|
typedef struct cache_dumparg {
|
|
|
|
isc_statsformat_t type;
|
|
|
|
void * arg; /* type dependent argument */
|
|
|
|
int ncounters; /* for general statistics */
|
|
|
|
int * counterindices; /* for general statistics */
|
|
|
|
uint64_t * countervalues; /* for general statistics */
|
|
|
|
isc_result_t result;
|
2012-05-14 10:06:05 -07:00
|
|
|
} cache_dumparg_t;
|
|
|
|
|
|
|
|
static void
|
2020-02-12 13:59:18 +01:00
|
|
|
getcounter(isc_statscounter_t counter, uint64_t val, void *arg)
|
|
|
|
{
|
2012-05-14 10:06:05 -07:00
|
|
|
cache_dumparg_t *dumparg = arg;
|
|
|
|
|
|
|
|
REQUIRE(counter < dumparg->ncounters);
|
|
|
|
dumparg->countervalues[counter] = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
getcounters(isc_stats_t *stats, isc_statsformat_t type, int ncounters,
|
2018-03-28 14:19:37 +02:00
|
|
|
int *indices, uint64_t *values)
|
2012-05-14 10:06:05 -07:00
|
|
|
{
|
|
|
|
cache_dumparg_t dumparg;
|
|
|
|
|
|
|
|
memset(values, 0, sizeof(values[0]) * ncounters);
|
|
|
|
|
|
|
|
dumparg.type = type;
|
|
|
|
dumparg.ncounters = ncounters;
|
|
|
|
dumparg.counterindices = indices;
|
|
|
|
dumparg.countervalues = values;
|
|
|
|
|
|
|
|
isc_stats_dump(stats, getcounter, &dumparg, ISC_STATSDUMP_VERBOSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_dumpstats(dns_cache_t *cache, FILE *fp)
|
|
|
|
{
|
|
|
|
int indices[dns_cachestatscounter_max];
|
2018-03-28 14:19:37 +02:00
|
|
|
uint64_t values[dns_cachestatscounter_max];
|
2012-05-14 10:06:05 -07:00
|
|
|
|
|
|
|
REQUIRE(VALID_CACHE(cache));
|
|
|
|
|
|
|
|
getcounters(cache->stats, isc_statsformat_file,
|
|
|
|
dns_cachestatscounter_max, indices, values);
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
fprintf(fp, "%20" PRIu64 " %s\n", values[dns_cachestatscounter_hits],
|
2012-05-14 10:06:05 -07:00
|
|
|
"cache hits");
|
2020-02-12 13:59:18 +01:00
|
|
|
fprintf(fp, "%20" PRIu64 " %s\n", values[dns_cachestatscounter_misses],
|
2012-05-14 10:06:05 -07:00
|
|
|
"cache misses");
|
2018-03-28 14:56:40 +02:00
|
|
|
fprintf(fp, "%20" PRIu64 " %s\n",
|
2012-05-14 10:06:05 -07:00
|
|
|
values[dns_cachestatscounter_queryhits],
|
|
|
|
"cache hits (from query)");
|
2018-03-28 14:56:40 +02:00
|
|
|
fprintf(fp, "%20" PRIu64 " %s\n",
|
2012-05-14 10:06:05 -07:00
|
|
|
values[dns_cachestatscounter_querymisses],
|
|
|
|
"cache misses (from query)");
|
2018-03-28 14:56:40 +02:00
|
|
|
fprintf(fp, "%20" PRIu64 " %s\n",
|
2012-05-14 10:06:05 -07:00
|
|
|
values[dns_cachestatscounter_deletelru],
|
|
|
|
"cache records deleted due to memory exhaustion");
|
2018-03-28 14:56:40 +02:00
|
|
|
fprintf(fp, "%20" PRIu64 " %s\n",
|
2012-05-14 10:06:05 -07:00
|
|
|
values[dns_cachestatscounter_deletettl],
|
|
|
|
"cache records deleted due to TTL expiration");
|
|
|
|
fprintf(fp, "%20u %s\n", dns_db_nodecount(cache->db),
|
|
|
|
"cache database nodes");
|
2020-02-12 13:59:18 +01:00
|
|
|
fprintf(fp, "%20" PRIu64 " %s\n", (uint64_t)dns_db_hashsize(cache->db),
|
2012-05-14 10:06:05 -07:00
|
|
|
"cache database hash buckets");
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
fprintf(fp, "%20" PRIu64 " %s\n", (uint64_t)isc_mem_total(cache->mctx),
|
2012-05-14 10:06:05 -07:00
|
|
|
"cache tree memory total");
|
2020-02-12 13:59:18 +01:00
|
|
|
fprintf(fp, "%20" PRIu64 " %s\n", (uint64_t)isc_mem_inuse(cache->mctx),
|
2012-05-14 10:06:05 -07:00
|
|
|
"cache tree memory in use");
|
2018-03-28 14:56:40 +02:00
|
|
|
fprintf(fp, "%20" PRIu64 " %s\n",
|
2020-02-12 13:59:18 +01:00
|
|
|
(uint64_t)isc_mem_maxinuse(cache->mctx),
|
2012-05-14 10:06:05 -07:00
|
|
|
"cache tree highest memory in use");
|
|
|
|
|
2020-02-12 13:59:18 +01:00
|
|
|
fprintf(fp, "%20" PRIu64 " %s\n", (uint64_t)isc_mem_total(cache->hmctx),
|
2012-05-14 10:06:05 -07:00
|
|
|
"cache heap memory total");
|
2020-02-12 13:59:18 +01:00
|
|
|
fprintf(fp, "%20" PRIu64 " %s\n", (uint64_t)isc_mem_inuse(cache->hmctx),
|
2012-05-14 10:06:05 -07:00
|
|
|
"cache heap memory in use");
|
2018-03-28 14:56:40 +02:00
|
|
|
fprintf(fp, "%20" PRIu64 " %s\n",
|
2020-02-12 13:59:18 +01:00
|
|
|
(uint64_t)isc_mem_maxinuse(cache->hmctx),
|
2012-05-14 10:06:05 -07:00
|
|
|
"cache heap highest memory in use");
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBXML2
|
2020-02-12 13:59:18 +01:00
|
|
|
#define TRY0(a) \
|
|
|
|
do { \
|
|
|
|
xmlrc = (a); \
|
|
|
|
if (xmlrc < 0) \
|
|
|
|
goto error; \
|
|
|
|
} while (0)
|
2012-11-01 10:22:11 +11:00
|
|
|
static int
|
2020-02-12 13:59:18 +01:00
|
|
|
renderstat(const char *name, uint64_t value, xmlTextWriterPtr writer)
|
|
|
|
{
|
2012-11-01 10:22:11 +11:00
|
|
|
int xmlrc;
|
|
|
|
|
2013-02-08 14:53:14 -08:00
|
|
|
TRY0(xmlTextWriterStartElement(writer, ISC_XMLCHAR "counter"));
|
2020-02-12 13:59:18 +01:00
|
|
|
TRY0(xmlTextWriterWriteAttribute(writer, ISC_XMLCHAR "name",
|
|
|
|
ISC_XMLCHAR name));
|
|
|
|
TRY0(xmlTextWriterWriteFormatString(writer, "%" PRIu64 "", value));
|
2013-02-08 14:53:14 -08:00
|
|
|
TRY0(xmlTextWriterEndElement(writer)); /* counter */
|
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
error:
|
|
|
|
return (xmlrc);
|
2012-05-14 10:06:05 -07:00
|
|
|
}
|
|
|
|
|
2012-11-01 10:22:11 +11:00
|
|
|
int
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_renderxml(dns_cache_t *cache, void *writer0)
|
|
|
|
{
|
|
|
|
int indices[dns_cachestatscounter_max];
|
|
|
|
uint64_t values[dns_cachestatscounter_max];
|
|
|
|
int xmlrc;
|
2019-06-24 14:25:55 +02:00
|
|
|
xmlTextWriterPtr writer = (xmlTextWriterPtr)writer0;
|
2012-05-14 10:06:05 -07:00
|
|
|
|
|
|
|
REQUIRE(VALID_CACHE(cache));
|
|
|
|
|
|
|
|
getcounters(cache->stats, isc_statsformat_file,
|
|
|
|
dns_cachestatscounter_max, indices, values);
|
2020-02-12 13:59:18 +01:00
|
|
|
TRY0(renderstat("CacheHits", values[dns_cachestatscounter_hits],
|
|
|
|
writer));
|
|
|
|
TRY0(renderstat("CacheMisses", values[dns_cachestatscounter_misses],
|
|
|
|
writer));
|
|
|
|
TRY0(renderstat("QueryHits", values[dns_cachestatscounter_queryhits],
|
|
|
|
writer));
|
2012-11-01 10:22:11 +11:00
|
|
|
TRY0(renderstat("QueryMisses",
|
2020-02-12 13:59:18 +01:00
|
|
|
values[dns_cachestatscounter_querymisses], writer));
|
|
|
|
TRY0(renderstat("DeleteLRU", values[dns_cachestatscounter_deletelru],
|
|
|
|
writer));
|
|
|
|
TRY0(renderstat("DeleteTTL", values[dns_cachestatscounter_deletettl],
|
|
|
|
writer));
|
2012-11-01 10:22:11 +11:00
|
|
|
|
|
|
|
TRY0(renderstat("CacheNodes", dns_db_nodecount(cache->db), writer));
|
|
|
|
TRY0(renderstat("CacheBuckets", dns_db_hashsize(cache->db), writer));
|
|
|
|
|
|
|
|
TRY0(renderstat("TreeMemTotal", isc_mem_total(cache->mctx), writer));
|
|
|
|
TRY0(renderstat("TreeMemInUse", isc_mem_inuse(cache->mctx), writer));
|
|
|
|
TRY0(renderstat("TreeMemMax", isc_mem_maxinuse(cache->mctx), writer));
|
|
|
|
|
|
|
|
TRY0(renderstat("HeapMemTotal", isc_mem_total(cache->hmctx), writer));
|
|
|
|
TRY0(renderstat("HeapMemInUse", isc_mem_inuse(cache->hmctx), writer));
|
|
|
|
TRY0(renderstat("HeapMemMax", isc_mem_maxinuse(cache->hmctx), writer));
|
|
|
|
error:
|
|
|
|
return (xmlrc);
|
2012-05-14 10:06:05 -07:00
|
|
|
}
|
|
|
|
#endif
|
2013-03-13 14:24:50 -07:00
|
|
|
|
2019-02-06 11:56:42 +01:00
|
|
|
#ifdef HAVE_JSON_C
|
2020-02-12 13:59:18 +01:00
|
|
|
#define CHECKMEM(m) \
|
|
|
|
do { \
|
|
|
|
if (m == NULL) { \
|
|
|
|
result = ISC_R_NOMEMORY; \
|
|
|
|
goto error; \
|
|
|
|
} \
|
|
|
|
} while (0)
|
2013-03-13 14:24:50 -07:00
|
|
|
|
|
|
|
isc_result_t
|
2020-02-12 13:59:18 +01:00
|
|
|
dns_cache_renderjson(dns_cache_t *cache, void *cstats0)
|
|
|
|
{
|
2013-03-13 14:24:50 -07:00
|
|
|
isc_result_t result = ISC_R_SUCCESS;
|
2020-02-12 13:59:18 +01:00
|
|
|
int indices[dns_cachestatscounter_max];
|
|
|
|
uint64_t values[dns_cachestatscounter_max];
|
2013-03-13 14:24:50 -07:00
|
|
|
json_object *obj;
|
2019-06-24 12:21:47 +02:00
|
|
|
json_object *cstats = (json_object *)cstats0;
|
2013-03-13 14:24:50 -07:00
|
|
|
|
|
|
|
REQUIRE(VALID_CACHE(cache));
|
|
|
|
|
|
|
|
getcounters(cache->stats, isc_statsformat_file,
|
|
|
|
dns_cachestatscounter_max, indices, values);
|
|
|
|
|
|
|
|
obj = json_object_new_int64(values[dns_cachestatscounter_hits]);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(cstats, "CacheHits", obj);
|
|
|
|
|
|
|
|
obj = json_object_new_int64(values[dns_cachestatscounter_misses]);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(cstats, "CacheMisses", obj);
|
|
|
|
|
|
|
|
obj = json_object_new_int64(values[dns_cachestatscounter_queryhits]);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(cstats, "QueryHits", obj);
|
|
|
|
|
|
|
|
obj = json_object_new_int64(values[dns_cachestatscounter_querymisses]);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(cstats, "QueryMisses", obj);
|
|
|
|
|
|
|
|
obj = json_object_new_int64(values[dns_cachestatscounter_deletelru]);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(cstats, "DeleteLRU", obj);
|
|
|
|
|
|
|
|
obj = json_object_new_int64(values[dns_cachestatscounter_deletettl]);
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(cstats, "DeleteTTL", obj);
|
|
|
|
|
|
|
|
obj = json_object_new_int64(dns_db_nodecount(cache->db));
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(cstats, "CacheNodes", obj);
|
|
|
|
|
|
|
|
obj = json_object_new_int64(dns_db_hashsize(cache->db));
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(cstats, "CacheBuckets", obj);
|
|
|
|
|
|
|
|
obj = json_object_new_int64(isc_mem_total(cache->mctx));
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(cstats, "TreeMemTotal", obj);
|
|
|
|
|
|
|
|
obj = json_object_new_int64(isc_mem_inuse(cache->mctx));
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(cstats, "TreeMemInUse", obj);
|
|
|
|
|
|
|
|
obj = json_object_new_int64(isc_mem_maxinuse(cache->mctx));
|
|
|
|
CHECKMEM(obj);
|
2017-09-12 14:55:03 +10:00
|
|
|
json_object_object_add(cstats, "TreeMemMax", obj);
|
2013-03-13 14:24:50 -07:00
|
|
|
|
|
|
|
obj = json_object_new_int64(isc_mem_total(cache->hmctx));
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(cstats, "HeapMemTotal", obj);
|
|
|
|
|
|
|
|
obj = json_object_new_int64(isc_mem_inuse(cache->hmctx));
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(cstats, "HeapMemInUse", obj);
|
|
|
|
|
|
|
|
obj = json_object_new_int64(isc_mem_maxinuse(cache->hmctx));
|
|
|
|
CHECKMEM(obj);
|
|
|
|
json_object_object_add(cstats, "HeapMemMax", obj);
|
|
|
|
|
|
|
|
result = ISC_R_SUCCESS;
|
|
|
|
error:
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
#endif
|