2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * In addition, for the avoidance of any doubt, permission is granted to
10 * link this program with OpenSSL and to (re)distribute the binaries
11 * produced as the result of such linking.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <boost/version.hpp>
27 #include "namespaces.hh"
28 using namespace ::boost::multi_index;
30 #include <boost/multi_index/hashed_index.hpp>
33 #include "dnspacket.hh"
36 class AuthQueryCache : public boost::noncopyable
39 AuthQueryCache(size_t mapsCount=1024);
42 void insert(const DNSName &qname, const QType& qtype, vector<DNSZoneRecord>&& content, uint32_t ttl, int zoneID);
44 bool getEntry(const DNSName &qname, const QType& qtype, vector<DNSZoneRecord>& entry, int zoneID);
46 size_t size() { return *d_statnumentries; } //!< number of entries in the cache
47 void cleanup(); //!< force the cache to preen itself from expired querys
49 uint64_t purge(const std::string& match); // could be $ terminated. Is not a dnsname!
50 uint64_t purgeExact(const DNSName& qname); // no wildcard matching here
52 map<char,uint64_t> getCounts();
54 void setMaxEntries(uint64_t maxEntries)
56 d_maxEntries = maxEntries;
57 for (auto& shard : d_maps) {
58 shard.reserve(maxEntries / d_maps.size());
66 mutable vector<DNSZoneRecord> drs;
67 mutable time_t created{0};
68 mutable time_t ttd{0};
75 struct SequencedTag{};
76 typedef multi_index_container<
79 hashed_unique<tag<HashTag>, composite_key<CacheEntry,
80 member<CacheEntry,DNSName,&CacheEntry::qname>,
81 member<CacheEntry,uint16_t,&CacheEntry::qtype>,
82 member<CacheEntry,int, &CacheEntry::zoneID> > > ,
83 ordered_non_unique<tag<NameTag>, member<CacheEntry,DNSName,&CacheEntry::qname>, CanonDNSNameCompare >,
84 /* Note that this sequence holds 'least recently inserted or replaced', not least recently used.
85 Making it a LRU would require taking a write-lock when fetching from the cache, making the RW-lock inefficient compared to a mutex */
86 sequenced<tag<SequencedTag>>
97 MapCombo(const MapCombo &) = delete;
98 MapCombo & operator=(const MapCombo &) = delete;
100 void reserve(size_t numberOfEntries);
106 vector<MapCombo> d_maps;
107 MapCombo& getMap(const DNSName& qname)
109 return d_maps[qname.hash() % d_maps.size()];
112 bool getEntryLocked(cmap_t& map, const DNSName &content, uint16_t qtype, vector<DNSZoneRecord>& entry, int zoneID, time_t now);
113 void cleanupIfNeeded();
115 AtomicCounter d_ops{0};
116 AtomicCounter *d_statnumhit;
117 AtomicCounter *d_statnummiss;
118 AtomicCounter *d_statnumentries;
120 uint64_t d_maxEntries{0};
121 time_t d_lastclean; // doesn't need to be atomic
122 unsigned long d_nextclean{4096};
123 unsigned int d_cleaninterval{4096};
124 bool d_cleanskipped{false};
126 static const unsigned int s_mincleaninterval=1000, s_maxcleaninterval=300000;