Branch data Line data Source code
1 : : // Copyright (C) 2010 Internet Systems Consortium, Inc. ("ISC")
2 : : //
3 : : // Permission to use, copy, modify, and/or distribute this software for any
4 : : // purpose with or without fee is hereby granted, provided that the above
5 : : // copyright notice and this permission notice appear in all copies.
6 : : //
7 : : // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
8 : : // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
9 : : // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
10 : : // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
11 : : // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
12 : : // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
13 : : // PERFORMANCE OF THIS SOFTWARE.
14 : :
15 : : #include <config.h>
16 : :
17 : : #include "rrset_cache.h"
18 : : #include "logger.h"
19 : : #include <string>
20 : : #include <nsas/nsas_entry_compare.h>
21 : : #include <nsas/hash_table.h>
22 : : #include <nsas/hash_deleter.h>
23 : :
24 : : using namespace isc::nsas;
25 : : using namespace isc::dns;
26 : : using namespace std;
27 : :
28 : : namespace isc {
29 : : namespace cache {
30 : :
31 : 105 : RRsetCache::RRsetCache(uint32_t cache_size,
32 : : uint16_t rrset_class):
33 : : class_(rrset_class),
34 : : rrset_table_(new NsasEntryCompare<RRsetEntry>, cache_size),
35 : : rrset_lru_((3 * cache_size),
36 [ + - ]: 105 : new HashDeleter<RRsetEntry>(rrset_table_))
37 : : {
38 [ + - ][ + - ]: 210 : LOG_DEBUG(logger, DBG_TRACE_BASIC, CACHE_RRSET_INIT).arg(cache_size).
[ + - ][ + - ]
[ + - ]
39 [ + - ]: 105 : arg(RRClass(rrset_class));
40 : 105 : }
41 : :
42 : : RRsetEntryPtr
43 : 285 : RRsetCache::lookup(const isc::dns::Name& qname,
44 : : const isc::dns::RRType& qtype)
45 : : {
46 [ + - ][ + - ]: 570 : LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_LOOKUP).arg(qname).
47 [ + - ][ + - ]: 285 : arg(qtype).arg(RRClass(class_));
48 : 570 : const string entry_name = genCacheEntryName(qname, qtype);
49 : :
50 : : RRsetEntryPtr entry_ptr = rrset_table_.get(HashKey(entry_name,
51 : 570 : RRClass(class_)));
52 [ + + ]: 285 : if (entry_ptr) {
53 [ + - ][ + + ]: 102 : if (entry_ptr->getExpireTime() > time(NULL)) {
54 : : // Only touch the non-expired rrset entries
55 [ + - ]: 101 : rrset_lru_.touch(entry_ptr);
56 : : return (entry_ptr);
57 : : } else {
58 [ + - ][ + - ]: 2 : LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_EXPIRED).arg(qname).
[ + - ][ + - ]
[ + - ]
59 [ + - ][ + - ]: 1 : arg(qtype).arg(RRClass(class_));
60 : : // the rrset entry has expired, so just remove it from
61 : : // hash table and lru list.
62 [ + - ][ + - ]: 1 : rrset_table_.remove(entry_ptr->hashKey());
63 [ + - ]: 1 : rrset_lru_.remove(entry_ptr);
64 : : }
65 : : }
66 : :
67 [ + - ][ + - ]: 368 : LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_NOT_FOUND).arg(qname).
[ + - ][ + - ]
[ + - ]
68 [ + - ][ + - ]: 184 : arg(qtype).arg(RRClass(class_));
69 : : return (RRsetEntryPtr());
70 : : }
71 : :
72 : : RRsetEntryPtr
73 : 173 : RRsetCache::update(const isc::dns::AbstractRRset& rrset,
74 : : const RRsetTrustLevel& level)
75 : : {
76 [ + - + - ]: 519 : LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_UPDATE).arg(rrset.getName()).
77 [ + - ][ + - ]: 346 : arg(rrset.getType()).arg(rrset.getClass());
78 : : // TODO: If the RRset is an NS, we should update the NSAS as well
79 : : // lookup first
80 : 173 : RRsetEntryPtr entry_ptr = lookup(rrset.getName(), rrset.getType());
81 [ + + ]: 173 : if (entry_ptr) {
82 [ + + ]: 31 : if (entry_ptr->getTrustLevel() > level) {
83 [ + - ][ + - ]: 22 : LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_UNTRUSTED).
[ + - ]
84 [ + - ][ + - ]: 11 : arg(rrset.getName()).arg(rrset.getType()).
[ + - ][ + - ]
[ + - ]
85 [ + - ][ + - ]: 22 : arg(rrset.getClass());
86 : : // existed rrset entry is more authoritative, just return it
87 : : return (entry_ptr);
88 : : } else {
89 [ + - ][ + - ]: 40 : LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_REMOVE_OLD).
[ + - ]
90 [ + - ][ + - ]: 20 : arg(rrset.getName()).arg(rrset.getType()).
[ + - ][ + - ]
[ + - ]
91 [ + - ][ + - ]: 40 : arg(rrset.getClass());
92 : : // Remove the old rrset entry from the lru list.
93 [ + - ]: 20 : rrset_lru_.remove(entry_ptr);
94 : : }
95 : : }
96 : :
97 [ + - ][ + - ]: 162 : entry_ptr.reset(new RRsetEntry(rrset, level));
98 [ + - ]: 162 : rrset_table_.add(entry_ptr, entry_ptr->hashKey(), true);
99 [ + - ]: 173 : rrset_lru_.add(entry_ptr);
100 : : return (entry_ptr);
101 : : }
102 : :
103 : : } // namespace cache
104 : 3 : } // namespace isc
105 : :
|