rrset_cache.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. #include <config.h>
  15. #include "rrset_cache.h"
  16. #include "logger.h"
  17. #include <string>
  18. #include <nsas/nsas_entry_compare.h>
  19. #include <nsas/hash_table.h>
  20. #include <nsas/hash_deleter.h>
  21. using namespace isc::nsas;
  22. using namespace isc::dns;
  23. using namespace std;
  24. namespace isc {
  25. namespace cache {
  26. RRsetCache::RRsetCache(uint32_t cache_size,
  27. uint16_t rrset_class):
  28. class_(rrset_class),
  29. rrset_table_(new NsasEntryCompare<RRsetEntry>, cache_size),
  30. rrset_lru_((3 * cache_size),
  31. new HashDeleter<RRsetEntry>(rrset_table_))
  32. {
  33. LOG_DEBUG(logger, DBG_TRACE_BASIC, CACHE_RRSET_INIT).arg(cache_size).
  34. arg(RRClass(rrset_class));
  35. }
  36. RRsetEntryPtr
  37. RRsetCache::lookup(const isc::dns::Name& qname,
  38. const isc::dns::RRType& qtype)
  39. {
  40. LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_LOOKUP).arg(qname).
  41. arg(qtype).arg(RRClass(class_));
  42. const string entry_name = genCacheEntryName(qname, qtype);
  43. RRsetEntryPtr entry_ptr = rrset_table_.get(HashKey(entry_name,
  44. RRClass(class_)));
  45. if (entry_ptr) {
  46. if (entry_ptr->getExpireTime() > time(NULL)) {
  47. // Only touch the non-expired rrset entries
  48. rrset_lru_.touch(entry_ptr);
  49. return (entry_ptr);
  50. } else {
  51. LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_EXPIRED).arg(qname).
  52. arg(qtype).arg(RRClass(class_));
  53. // the rrset entry has expired, so just remove it from
  54. // hash table and lru list.
  55. rrset_table_.remove(entry_ptr->hashKey());
  56. rrset_lru_.remove(entry_ptr);
  57. }
  58. }
  59. LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_NOT_FOUND).arg(qname).
  60. arg(qtype).arg(RRClass(class_));
  61. return (RRsetEntryPtr());
  62. }
  63. RRsetEntryPtr
  64. RRsetCache::update(const isc::dns::AbstractRRset& rrset,
  65. const RRsetTrustLevel& level)
  66. {
  67. LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_UPDATE).arg(rrset.getName()).
  68. arg(rrset.getType()).arg(rrset.getClass());
  69. // TODO: If the RRset is an NS, we should update the NSAS as well
  70. // lookup first
  71. RRsetEntryPtr entry_ptr = lookup(rrset.getName(), rrset.getType());
  72. if (entry_ptr) {
  73. if (entry_ptr->getTrustLevel() > level) {
  74. LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_UNTRUSTED).
  75. arg(rrset.getName()).arg(rrset.getType()).
  76. arg(rrset.getClass());
  77. // existed rrset entry is more authoritative, just return it
  78. return (entry_ptr);
  79. } else {
  80. LOG_DEBUG(logger, DBG_TRACE_DATA, CACHE_RRSET_REMOVE_OLD).
  81. arg(rrset.getName()).arg(rrset.getType()).
  82. arg(rrset.getClass());
  83. // Remove the old rrset entry from the lru list.
  84. rrset_lru_.remove(entry_ptr);
  85. }
  86. }
  87. entry_ptr.reset(new RRsetEntry(rrset, level));
  88. rrset_table_.add(entry_ptr, entry_ptr->hashKey(), true);
  89. rrset_lru_.add(entry_ptr);
  90. return (entry_ptr);
  91. }
  92. } // namespace cache
  93. } // namespace isc