0bf752f4835374c820f9c22ac6b64980729d4b4aTinderbox User * Copyright (C) 2004-2008, 2012, 2013, 2015-2017 Internet Systems Consortium, Inc. ("ISC")
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater/* $Id: acache.c,v 1.22 2008/02/07 23:46:54 tbox Exp $ */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉#define ACACHE_MAGIC ISC_MAGIC('A', 'C', 'H', 'E')
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉#define DNS_ACACHE_VALID(acache) ISC_MAGIC_VALID(acache, ACACHE_MAGIC)
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉#define ACACHEENTRY_MAGIC ISC_MAGIC('A', 'C', 'E', 'T')
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉#define DNS_ACACHEENTRY_VALID(entry) ISC_MAGIC_VALID(entry, ACACHEENTRY_MAGIC)
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉#define ATRACE(m) isc_log_write(dns_lctx, \
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉#define AATRACE(a,m) isc_log_write(dns_lctx, \
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 "acache %p: %s", (a), (m))
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * The following variables control incremental cleaning.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * MINSIZE is how many bytes is the floor for dns_acache_setcachesize().
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * CLEANERINCREMENT is how many entries are examined in one pass.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * (XXX simply derived from definitions in cache.c There may be better
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * constants here.)
f3350b671881f175d03e16fa5d0f685a1691bcabMark Andrews#define DNS_ACACHE_MINSIZE 2097152U /* Bytes. 2097152 = 2 MB */
f3350b671881f175d03e16fa5d0f685a1691bcabMark Andrews#define DNS_ACACHE_CLEANERINCREMENT 1000 /* Number of entries. */
f3350b671881f175d03e16fa5d0f685a1691bcabMark Andrews#define DEFAULT_ACACHE_ENTRY_LOCK_COUNT 1009 /*%< Should be prime. */
d0d2ba954683265b9d4726d31cbddff7fef7a308Mukund Sivaraman ((defined(ISC_PLATFORM_HAVESTDATOMIC) && defined(ATOMIC_LONG_LOCK_FREE)) || \
d0d2ba954683265b9d4726d31cbddff7fef7a308Mukund Sivaraman#if (defined(ISC_PLATFORM_HAVESTDATOMIC) && defined(ATOMIC_LONG_LOCK_FREE))
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉#define ACACHE_INITLOCK(l) isc_rwlock_init((l), 0, 0)
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉#define ACACHE_DESTROYLOCK(l) isc_rwlock_destroy(l)
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉#define ACACHE_LOCK(l, t) RWLOCK((l), (t))
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉#define ACACHE_UNLOCK(l, t) RWUNLOCK((l), (t))
d0d2ba954683265b9d4726d31cbddff7fef7a308Mukund Sivaraman atomic_store_explicit(&(entry)->lastused, (t), \
3d5430b75a454e3b4ebe4aecb2083fab1bb9569bTatuya JINMEI 神明達哉 (isc_atomic_store((isc_int32_t *)&(entry)->lastused, (t)))
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉#define ACACHE_INITLOCK(l) isc_mutex_init(l)
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉#define ACACHE_DESTROYLOCK(l) DESTROYLOCK(l)
5597be9bb88de138dfec9fa9176708443813925eTatuya JINMEI 神明達哉#define acache_storetime(entry, t) ((entry)->lastused = (t))
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉/* Locked by acache lock */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉typedef ISC_LIST(dbentry_t) dbentrylist_t;
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉typedef struct acache_cleaner acache_cleaner_t;
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 cleaner_s_idle, /* Waiting for cleaning-interval to expire. */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 cleaner_s_busy, /* Currently cleaning. */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 cleaner_s_done /* Freed enough memory after being overmem. */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * Convenience macros for comprehensive assertion checking.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉#define CLEANER_IDLE(c) ((c)->state == cleaner_s_idle && \
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉#define CLEANER_BUSY(c) ((c)->state == cleaner_s_busy && \
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * Locks overmem_event, overmem. (See cache.c)
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 unsigned int cleaning_interval; /* The cleaning-interval
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 in seconds. */
602784c4ceb4f36f614afe5da80c7964eb9a766cShane Kerr isc_stdtime_t last_cleanup_time; /* The time when the last
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater cleanup task completed */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_event_t *resched_event; /* Sent by cleaner task to
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 itself to reschedule */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_acacheentry_t *current_entry; /* The bookmark entry to
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 restart the cleaning.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 Locked by acache lock. */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 clean in one increment */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 unsigned long ncleaned; /* Number of entries cleaned
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 up (for logging purposes) */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 cleaner_state_t state; /* Idle/Busy/Done. */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_boolean_t overmem; /* The acache is in an overmem
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * The actual acache object.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 /* Data for Management of cache entries */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 holding this entry */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 /* Cache data */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_db_t *db; /* DB this entry belongs to */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_dbversion_t *version; /* the version of the DB */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_name_t *foundname; /* corresponding DNS name
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 and rdataset */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 /* Callback function and its argument */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 void (*callback)(dns_acacheentry_t *, void **);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 /* Timestamp of the last time this entry is referred to */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * Internal functions (and prototypes).
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉static inline isc_boolean_t check_noentry(dns_acache_t *acache);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉static void destroy(dns_acache_t *acache);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉static void shutdown_entries(dns_acache_t *acache);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉static void shutdown_buckets(dns_acache_t *acache);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉static void destroy_entry(dns_acacheentry_t *ent);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉static inline void unlink_dbentries(dns_acache_t *acache,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉static inline isc_result_t finddbent(dns_acache_t *acache,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉static inline void clear_entry(dns_acache_t *acache, dns_acacheentry_t *entry);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉static isc_result_t acache_cleaner_init(dns_acache_t *acache,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉static void acache_cleaning_timer_action(isc_task_t *task, isc_event_t *event);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉static void acache_incremental_cleaning_action(isc_task_t *task,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉static void acache_overmem_cleaning_action(isc_task_t *task,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉static void acache_cleaner_shutdown_action(isc_task_t *task,
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr * acache should be locked. If it is not, the stats can get out of whack,
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr * which is not a big deal for us since this is for debugging / stats
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * The acache must be locked before calling.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 if (ISC_LIST_EMPTY(acache->entries) && acache->dbentries == 0) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * The acache must be locked before calling.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * Release the dependency of all entries, and detach them.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 for (entry = ISC_LIST_HEAD(acache->entries);
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_LOCK(&acache->entrylocks[entry->locknum],
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * If the cleaner holds this entry, it will be unlinked and
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * freed in the cleaner later.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 if (acache->cleaner.current_entry != entry)
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_UNLINK(acache->entries, entry, link);
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_UNLOCK(&acache->entrylocks[entry->locknum],
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 if (acache->cleaner.current_entry != entry)
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * The acache must be locked before calling.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 for (i = 0; i < DBBUCKETS; i++) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 while ((dbent = ISC_LIST_HEAD(acache->dbbucket[i])) != NULL) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 INSIST(ISC_LIST_EMPTY(dbent->originlist) &&
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_UNLINK(acache->dbbucket[i], dbent, link);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_mem_put(acache->mctx, dbent, sizeof(*dbent));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉shutdown_task(isc_task_t *task, isc_event_t *ev) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉/* The acache and the entry must be locked before calling. */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉static inline void
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉unlink_dbentries(dns_acache_t *acache, dns_acacheentry_t *ent) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 result = finddbent(acache, ent->origdb, &dbent);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_UNLINK(dbent->originlist, ent, olink);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 result = finddbent(acache, ent->db, &dbent);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_UNLINK(dbent->referlist, ent, rlink);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉/* There must not be a reference to this entry. */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * Since there is no reference to this entry, it is safe to call
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * clear_entry() here.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_mem_put(acache->mctx, entry, sizeof(*entry));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_mem_setwater(acache->mctx, NULL, NULL, 0, 0);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 if (acache->cleaner.overmem_event != NULL)
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_event_free(&acache->cleaner.overmem_event);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 if (acache->cleaner.resched_event != NULL)
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_event_free(&acache->cleaner.resched_event);
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 for (i = 0; i < DEFAULT_ACACHE_ENTRY_LOCK_COUNT; i++)
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_DESTROYLOCK(&acache->entrylocks[i]);
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 isc_mem_put(acache->mctx, acache->entrylocks,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_mem_putanddetach(&acache->mctx, acache, sizeof(*acache));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉finddbent(dns_acache_t *acache, dns_db_t *db, dbentry_t **dbentryp) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 REQUIRE(dbentryp != NULL && *dbentryp == NULL);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * The caller must be holding the acache lock.
5d79b60fc5e4dad4f04da39570517d20a2425f8bMukund Sivaraman bucket = isc_hash_function(&db, sizeof(db), ISC_TRUE, NULL) % DBBUCKETS;
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 for (dbentry = ISC_LIST_HEAD(acache->dbbucket[bucket]);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dbentry = ISC_LIST_NEXT(dbentry, link)) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉static inline void
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉clear_entry(dns_acache_t *acache, dns_acacheentry_t *entry) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * The caller must be holing the entry lock.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_rdataset_t *rdataset, *rdataset_next;
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 for (rdataset = ISC_LIST_HEAD(entry->foundname->list);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 rdataset_next = ISC_LIST_NEXT(rdataset, link);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_mem_put(acache->mctx, rdataset, sizeof(*rdataset));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_name_free(entry->foundname, acache->mctx);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_mem_put(acache->mctx, entry->foundname,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_db_detachnode(entry->db, &entry->node);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_db_closeversion(entry->db, &entry->version, ISC_FALSE);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉acache_cleaner_init(dns_acache_t *acache, isc_timermgr_t *timermgr,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 cleaner->increment = DNS_ACACHE_CLEANERINCREMENT;
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 result = isc_task_onshutdown(acache->task,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 "acache cleaner: "
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 "isc_task_onshutdown() failed: %s",
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 cleaner->cleaning_interval = 0; /* Initially turned off. */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 result = isc_timer_create(timermgr, isc_timertype_inactive,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 "isc_timer_create() failed: %s",
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_event_allocate(acache->mctx, cleaner,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_event_allocate(acache->mctx, cleaner,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_timer_detach(&cleaner->cleaning_timer);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉begin_cleaning(acache_cleaner_t *cleaner) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * This function does not have to lock the cleaner, since critical
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * parameters (except current_entry, which is locked by acache lock,)
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * are only used in a single task context.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 "begin acache cleaning, mem inuse %lu",
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 (unsigned long)isc_mem_inuse(cleaner->acache->mctx));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_acache_attachentry(head, &cleaner->current_entry);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_task_send(acache->task, &cleaner->resched_event);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉end_cleaning(acache_cleaner_t *cleaner, isc_event_t *event) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 REQUIRE(DNS_ACACHEENTRY_VALID(cleaner->current_entry));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 /* No need to lock the cleaner (see begin_cleaning()). */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * Even if the cleaner has the last reference to the entry, which means
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * the entry has been unused, it may still be linked if unlinking the
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * entry has been delayed due to the reference.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 if (isc_refcount_current(&cleaner->current_entry->references) == 1) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 INSIST(cleaner->current_entry->callback == NULL);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 if (ISC_LINK_LINKED(cleaner->current_entry, link)) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_acache_detachentry(&cleaner->current_entry);
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ACACHE,
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr "acache %p stats: hits=%d misses=%d queries=%d "
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr "adds=%d deleted=%d "
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr "cleaned=%d cleaner_runs=%d overmem=%d "
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr "overmem_nocreates=%d nomem=%d",
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr acache->stats.cleaned, acache->stats.cleaner_runs,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater acache->stats.overmem, acache->stats.overmem_nocreates,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_acache_setcleaninginterval(cleaner->acache,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ACACHE,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LOG_DEBUG(1), "end acache cleaning, "
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 "%lu entries cleaned, mem inuse %lu",
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 (unsigned long)isc_mem_inuse(cleaner->acache->mctx));
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr "acache is still in overmem state "
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr "after cleaning");
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * This is run once for every acache-cleaning-interval as defined
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉acache_cleaning_timer_action(isc_task_t *task, isc_event_t *event) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 acache_cleaner_t *cleaner = event->ev_arg;
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 INSIST(event->ev_type == ISC_TIMEREVENT_TICK);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ACACHE,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LOG_DEBUG(1), "acache cleaning timer fired, "
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉/* The caller must hold entry lock. */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉entry_stale(acache_cleaner_t *cleaner, dns_acacheentry_t *entry,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * If the callback has been canceled, we definitely do not need the
602784c4ceb4f36f614afe5da80c7964eb9a766cShane Kerr * If the acache is in the overmem state, probabilistically decide if
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * the entry should be purged, based on the time passed from its last
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * use and the cleaning interval.
f3e10cca1b9a95fab5ac7a162c0705e2d9da7370Mark Andrews passed = now32 - entry->lastused; /* <= interval */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * Do incremental cleaning.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉acache_incremental_cleaning_action(isc_task_t *task, isc_event_t *event) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 acache_cleaner_t *cleaner = event->ev_arg;
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 INSIST(event->ev_type == DNS_EVENT_ACACHECLEAN);
602784c4ceb4f36f614afe5da80c7964eb9a766cShane Kerr isc_stdtime_convert32(cleaner->last_cleanup_time, &last32);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 while (n_entries-- > 0) {
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_LOCK(&acache->entrylocks[entry->locknum],
602784c4ceb4f36f614afe5da80c7964eb9a766cShane Kerr is_stale = entry_stale(cleaner, entry, now32, interval);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_UNLINK(acache->entries, entry, link);
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_UNLOCK(&acache->entrylocks[entry->locknum],
602784c4ceb4f36f614afe5da80c7964eb9a766cShane Kerr * If we are still in the overmem
ff5ac6d4213e3e2f3f6a93db8c5e65cc170a7e2bEvan Hunt * state, keep cleaning. In case we
ff5ac6d4213e3e2f3f6a93db8c5e65cc170a7e2bEvan Hunt * exit from the loop immediately after
ff5ac6d4213e3e2f3f6a93db8c5e65cc170a7e2bEvan Hunt * this, reset next to the head entry
ff5ac6d4213e3e2f3f6a93db8c5e65cc170a7e2bEvan Hunt * as we'll expect it will be never
602784c4ceb4f36f614afe5da80c7964eb9a766cShane Kerr "acache cleaner: "
602784c4ceb4f36f614afe5da80c7964eb9a766cShane Kerr "still overmem, "
602784c4ceb4f36f614afe5da80c7964eb9a766cShane Kerr "reset and try again");
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * We have successfully performed a cleaning increment but have
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * not gone through the entire cache. Remember the entry that will
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * be the starting point in the next clean-up, and reschedule another
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * batch. If it fails, just try to continue anyway.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_acache_detachentry(&cleaner->current_entry);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_acache_attachentry(next, &cleaner->current_entry);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ACACHE,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LOG_DEBUG(1), "acache cleaner: checked %d entries, "
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 "mem inuse %lu, sleeping", cleaner->increment,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 (unsigned long)isc_mem_inuse(cleaner->acache->mctx));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * This is called when the acache either surpasses its upper limit
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * or shrinks beyond its lower limit.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉acache_overmem_cleaning_action(isc_task_t *task, isc_event_t *event) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 acache_cleaner_t *cleaner = event->ev_arg;
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 INSIST(event->ev_type == DNS_EVENT_ACACHEOVERMEM);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE, DNS_LOGMODULE_ACACHE,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LOG_DEBUG(1), "overmem_cleaning_action called, "
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 "overmem = %d, state = %d", cleaner->overmem,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * end_cleaning() can't be called here because
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * then both cleaner->overmem_event and
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * cleaner->resched_event will point to this
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * event. Set the state to done, and then
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * when the acache_incremental_cleaning_action() event
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * is posted, it will handle the end_cleaning.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_boolean_t overmem = ISC_TF(mark == ISC_MEM_HIWATER);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 "acache memory reaches %s watermark, mem inuse %lu",
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 (unsigned long)isc_mem_inuse(acache->mctx));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * The cleaner task is shutting down; do the necessary cleanup.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉acache_cleaner_shutdown_action(isc_task_t *task, isc_event_t *event) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 INSIST(event->ev_type == ISC_TASKEVENT_SHUTDOWN);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 if (isc_refcount_current(&acache->refs) == 0) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 INSIST(check_noentry(acache) == ISC_TRUE);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * By detaching the timer in the context of its task,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * we are guaranteed that there will be no further timer
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 if (acache->cleaner.cleaning_timer != NULL)
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_timer_detach(&acache->cleaner.cleaning_timer);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 /* Make sure we don't reschedule anymore. */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 (void)isc_task_purge(task, NULL, DNS_EVENT_ACACHECLEAN, NULL);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * Public functions.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉dns_acache_create(dns_acache_t **acachep, isc_mem_t *mctx,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_taskmgr_t *taskmgr, isc_timermgr_t *timermgr)
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 REQUIRE(acachep != NULL && *acachep == NULL);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 acache = isc_mem_get(mctx, sizeof(*acache));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_mem_put(mctx, acache, sizeof(*acache));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 result = isc_task_create(taskmgr, 1, &acache->task);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 "isc_task_create() failed(): %s",
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_task_setname(acache->task, "acachetask", acache);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_EVENT_INIT(&acache->cevent, sizeof(acache->cevent), 0, NULL,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 DNS_EVENT_ACACHECONTROL, shutdown_task, NULL,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 for (i = 0; i < DBBUCKETS; i++)
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 acache->entrylocks = isc_mem_get(mctx, sizeof(*acache->entrylocks) *
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 for (i = 0; i < DEFAULT_ACACHE_ENTRY_LOCK_COUNT; i++) {
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 result = ACACHE_INITLOCK(&acache->entrylocks[i]);
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 while (i-- > 0)
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_DESTROYLOCK(&acache->entrylocks[i]);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater result = acache_cleaner_init(acache, timermgr, &acache->cleaner);
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 for (i = 0; i < DEFAULT_ACACHE_ENTRY_LOCK_COUNT; i++)
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_DESTROYLOCK(&acache->entrylocks[i]);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_mem_put(mctx, acache, sizeof(*acache));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉dns_acache_attach(dns_acache_t *source, dns_acache_t **targetp) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 REQUIRE(targetp != NULL && *targetp == NULL);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_refcount_increment(&source->refs, NULL);
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr acache->stats.misses++; /* XXXSK danger: unlocked! */
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr acache->stats.queries++; /* XXXSK danger: unlocked! */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉dns_acache_detach(dns_acache_t **acachep) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 REQUIRE(acachep != NULL && DNS_ACACHE_VALID(*acachep));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_refcount_decrement(&acache->refs, &refs);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 INSIST(check_noentry(acache) == ISC_TRUE);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * If we're exiting and the cleaner task exists, let it free the cache.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 if (should_free && acache->live_cleaners > 0) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉dns_acache_shutdown(dns_acache_t *acache) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_mem_setwater(acache->mctx, NULL, NULL, 0, 0);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * Self attach the object in order to prevent it from being
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * destroyed while waiting for the event.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_acache_attach(acache, &acache_evarg);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉dns_acache_setdb(dns_acache_t *acache, dns_db_t *db) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 result = finddbent(acache, db, &dbentry);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dbentry = isc_mem_get(acache->mctx, sizeof(*dbentry));
5d79b60fc5e4dad4f04da39570517d20a2425f8bMukund Sivaraman bucket = isc_hash_function(&db, sizeof(db), ISC_TRUE, NULL) % DBBUCKETS;
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_APPEND(acache->dbbucket[bucket], dbentry, link);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉dns_acache_putdb(dns_acache_t *acache, dns_db_t *db) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 result = finddbent(acache, db, &dbentry);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * The entry may have not been created due to memory shortage.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * Release corresponding cache entries: for each entry, release all
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * links the entry has, and then callback to the entry holder (if any).
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * If no other external references exist (this can happen if the
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * original holder has canceled callback,) destroy it here.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 while ((entry = ISC_LIST_HEAD(dbentry->originlist)) != NULL) {
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_LOCK(&acache->entrylocks[entry->locknum],
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * Releasing olink first would avoid finddbent() in
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * unlink_dbentries().
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_UNLINK(dbentry->originlist, entry, olink);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_UNLINK(acache->entries, entry, link);
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_UNLOCK(&acache->entrylocks[entry->locknum],
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 while ((entry = ISC_LIST_HEAD(dbentry->referlist)) != NULL) {
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_LOCK(&acache->entrylocks[entry->locknum],
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_UNLINK(dbentry->referlist, entry, rlink);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_UNLINK(acache->entries, entry, link);
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_UNLOCK(&acache->entrylocks[entry->locknum],
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 INSIST(ISC_LIST_EMPTY(dbentry->originlist) &&
5d79b60fc5e4dad4f04da39570517d20a2425f8bMukund Sivaraman bucket = isc_hash_function(&db, sizeof(db), ISC_TRUE, NULL) % DBBUCKETS;
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_UNLINK(acache->dbbucket[bucket], dbentry, link);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_mem_put(acache->mctx, dbentry, sizeof(*dbentry));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉dns_acache_createentry(dns_acache_t *acache, dns_db_t *origdb,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 void (*callback)(dns_acacheentry_t *, void **),
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 REQUIRE(entryp != NULL && *entryp == NULL);
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * Should we exceed our memory limit for some reason (for
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * example, if the cleaner does not run aggressively enough),
1e844d04a716346f08ec027e365ce43e7b360c51Shane Kerr * then we will not create additional entries.
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr * XXXSK: It might be better to lock the acache->cleaner->lock,
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * but locking may be an expensive bottleneck. If we misread
f172f06ff2e7609dd7d91914a44b4e24cff8bb7aAutomatic Updater * the value, we will occasionally refuse to create a few
1e844d04a716346f08ec027e365ce43e7b360c51Shane Kerr * cache entries, or create a few that we should not. I do not
1e844d04a716346f08ec027e365ce43e7b360c51Shane Kerr * expect this to happen often, and it will not have very bad
1e844d04a716346f08ec027e365ce43e7b360c51Shane Kerr * effects when it does. So no lock for now.
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr acache->stats.overmem_nocreates++; /* XXXSK danger: unlocked! */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 newentry = isc_mem_get(acache->mctx, sizeof(*newentry));
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr acache->stats.nomem++; /* XXXMLG danger: unlocked! */
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 newentry->locknum = r % DEFAULT_ACACHE_ENTRY_LOCK_COUNT;
2674e1a455d4f71de09b2b60e7a8304b9a305588Mark Andrews result = isc_refcount_init(&newentry->references, 1);
2674e1a455d4f71de09b2b60e7a8304b9a305588Mark Andrews isc_mem_put(acache->mctx, newentry, sizeof(*newentry));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_acache_attach(acache, &newentry->acache);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_db_attach(origdb, &newentry->origdb);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉dns_acache_getentry(dns_acacheentry_t *entry, dns_zone_t **zonep,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_db_t **dbp, dns_dbversion_t **versionp,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 REQUIRE(zonep == NULL || *zonep == NULL);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 REQUIRE(versionp != NULL && *versionp == NULL);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 REQUIRE(nodep != NULL && *nodep == NULL);
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_LOCK(&acache->entrylocks[locknum], isc_rwlocktype_read);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 if (entry->zone != NULL && zonep != NULL)
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_db_attachversion(entry->db, entry->version, versionp);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_db_attachnode(entry->db, entry->node, nodep);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_name_copy(entry->foundname, fname, NULL);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 for (erdataset = ISC_LIST_HEAD(entry->foundname->list);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 erdataset = ISC_LIST_NEXT(erdataset, link)) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 result = dns_message_gettemprdataset(msg, &ardataset);
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_UNLOCK(&acache->entrylocks[locknum],
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * XXXJT: if we simply clone the rdataset, we'll get
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * lost wrt cyclic ordering. We'll need an additional
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * trick to get the latest counter from the original
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_rdataset_clone(erdataset, ardataset);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_APPEND(fname->list, ardataset, link);
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr entry->acache->stats.hits++; /* XXXMLG danger: unlocked! */
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_UNLOCK(&acache->entrylocks[locknum], isc_rwlocktype_read);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 while ((erdataset = ISC_LIST_HEAD(fname->list)) != NULL) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_UNLINK(fname->list, erdataset, link);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_message_puttemprdataset(msg, &erdataset);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_db_closeversion(*dbp, versionp, ISC_FALSE);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉dns_acache_setentry(dns_acache_t *acache, dns_acacheentry_t *entry,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_zone_t *zone, dns_db_t *db, dns_dbversion_t *version,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 LOCK(&acache->lock); /* XXX: need to lock it here for ordering */
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_LOCK(&acache->entrylocks[entry->locknum], isc_rwlocktype_write);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 /* Set zone */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * Set DB version. If the version is not given by the caller,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * which is the case for glue or cache DBs, use the current version.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_db_attachversion(db, version, &entry->version);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_db_closeversion(db, &version, ISC_FALSE);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 /* Set DB node. */
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_db_attachnode(db, node, &entry->node);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * Set list of the corresponding rdatasets, if given.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * To minimize the overhead and memory consumption, we'll do this for
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * positive cache only, in which case the DB node is non NULL.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * We do not want to cache incomplete information, so give up the
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * entire entry when a memory shortage happen during the process.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 entry->foundname = isc_mem_get(acache->mctx,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 result = dns_name_dup(fname, acache->mctx,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 for (ardataset = ISC_LIST_HEAD(fname->list);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ardataset = ISC_LIST_NEXT(ardataset, link)) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 dns_rdataset_clone(ardataset, crdataset);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_APPEND(entry->foundname->list, crdataset,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 result = finddbent(acache, entry->origdb, &odbent);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_APPEND(acache->entries, entry, link);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_APPEND(odbent->originlist, entry, olink);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ISC_LIST_APPEND(rdbent->referlist, entry, rlink);
a1cc4108f61d1fd2a57810242b06dd929eaab4bdMark Andrews * The additional cache needs an implicit reference to entries in its
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_UNLOCK(&acache->entrylocks[entry->locknum],
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_UNLOCK(&acache->entrylocks[entry->locknum],
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉dns_acache_cancelentry(dns_acacheentry_t *entry) {
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_LOCK(&acache->entrylocks[entry->locknum], isc_rwlocktype_write);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * Release dependencies stored in this entry as much as possible.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * The main link cannot be released, since the acache object has
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * a reference to this entry; the empty entry will be released in
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * the next cleaning action.
9a6522317c97e5487cea816173f63a0e5b4e428aTatuya JINMEI 神明達哉 ACACHE_UNLOCK(&acache->entrylocks[entry->locknum],
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉dns_acache_attachentry(dns_acacheentry_t *source,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 REQUIRE(targetp != NULL && *targetp == NULL);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_refcount_increment(&source->references, NULL);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉dns_acache_detachentry(dns_acacheentry_t **entryp) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 REQUIRE(entryp != NULL && DNS_ACACHEENTRY_VALID(*entryp));
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_refcount_decrement(&entry->references, &refs);
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * If there are no references to the entry, the entry must have been
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * unlinked and can be destroyed safely.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉dns_acache_setcleaninginterval(dns_acache_t *acache, unsigned int t) {
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 ATRACE("dns_acache_setcleaninginterval");
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * It may be the case that the acache has already shut down.
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * If so, it has no timer. (Not sure if this can really happen.)
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 if (acache->cleaner.cleaning_timer == NULL)
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 result = isc_timer_reset(acache->cleaner.cleaning_timer,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_interval_set(&interval, acache->cleaner.cleaning_interval,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 result = isc_timer_reset(acache->cleaner.cleaning_timer,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 "could not set acache cleaning interval: %s",
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr isc_log_write(dns_lctx, DNS_LOGCATEGORY_DATABASE,
0d8971a4b8abed599ec9d9b7d1b51b8de8038ce2Shane Kerr "acache %p cleaning interval set to %d.",
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * This function was derived from cache.c:dns_cache_setcachesize(). See the
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 * function for more details about the logic.
2a184ff86544cc67c36e2ce6bb3ddb5ac44684b8Evan Huntdns_acache_setcachesize(dns_acache_t *acache, size_t size) {
f3350b671881f175d03e16fa5d0f685a1691bcabMark Andrews if (size == 0U || hiwater == 0U || lowater == 0U)
d0eb2cc33c5db3366a16b1cb0abcca6ec7c8ee3cTatuya JINMEI 神明達哉 isc_mem_setwater(acache->mctx, water, acache, 0, 0);