]> git.ipfire.org Git - thirdparty/pdns.git/blob - pdns/packetcache.hh
document cache cleaning rate adjustment, plus switch to symbolic names for limits
[thirdparty/pdns.git] / pdns / packetcache.hh
1 /*
2 * This file is part of PowerDNS or dnsdist.
3 * Copyright -- PowerDNS.COM B.V. and its contributors
4 *
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.
8 *
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.
12 *
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.
17 *
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.
21 */
22 #ifndef PACKETCACHE_HH
23 #define PACKETCACHE_HH
24
25 #include <string>
26 #include <utility>
27 #include <map>
28 #include <map>
29 #include "dns.hh"
30 #include <boost/version.hpp>
31 #include "namespaces.hh"
32 using namespace ::boost::multi_index;
33
34 #include "namespaces.hh"
35 #include <boost/multi_index/hashed_index.hpp>
36 #include "dnspacket.hh"
37 #include "lock.hh"
38 #include "statbag.hh"
39
40 /** This class performs 'whole packet caching'. Feed it a question packet and it will
41 try to find an answer. If you have an answer, insert it to have it cached for later use.
42 Take care not to replace existing cache entries. While this works, it is wasteful. Only
43 insert packets that where not found by get()
44
45 Locking!
46
47 The cache itself is protected by a read/write lock. Because deleting is a two step process, which
48 first marks and then sweeps, a second lock is present to prevent simultaneous inserts and deletes.
49 */
50
51 class PacketCache : public boost::noncopyable
52 {
53 public:
54 PacketCache();
55 ~PacketCache();
56 enum CacheEntryType { PACKETCACHE, QUERYCACHE};
57
58 void insert(DNSPacket *q, DNSPacket *r, bool recursive, unsigned int maxttl=UINT_MAX); //!< We copy the contents of *p into our cache. Do not needlessly call this to insert questions already in the cache as it wastes resources
59
60 void insert(const DNSName &qname, const QType& qtype, CacheEntryType cet, const string& value, unsigned int ttl, int zoneID=-1, bool meritsRecursion=false,
61 unsigned int maxReplyLen=512, bool dnssecOk=false, bool EDNS=false);
62
63 void insert(const DNSName &qname, const QType& qtype, CacheEntryType cet, const vector<DNSZoneRecord>& content, unsigned int ttl, int zoneID=-1);
64
65 int get(DNSPacket *p, DNSPacket *q, bool recursive); //!< We return a dynamically allocated copy out of our cache. You need to delete it. You also need to spoof in the right ID with the DNSPacket.spoofID() method.
66 bool getEntry(const DNSName &qname, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1,
67 bool meritsRecursion=false, unsigned int maxReplyLen=512, bool dnssecOk=false, bool hasEDNS=false, unsigned int *age=0);
68 bool getEntry(const DNSName &qname, const QType& qtype, CacheEntryType cet, vector<DNSZoneRecord>& entry, int zoneID=-1);
69
70
71 int size() { return *d_statnumentries; } //!< number of entries in the cache
72 void cleanupIfNeeded();
73 void cleanup(); //!< force the cache to preen itself from expired packets
74 int purge();
75 int purge(const std::string& match); // could be $ terminated. Is not a dnsname!
76 int purgeExact(const DNSName& qname); // no wildcard matching here
77
78 map<char,int> getCounts();
79 private:
80 bool getEntryLocked(const DNSName &content, const QType& qtype, CacheEntryType cet, string& entry, int zoneID=-1,
81 bool meritsRecursion=false, unsigned int maxReplyLen=512, bool dnssecOk=false, bool hasEDNS=false, unsigned int *age=0);
82 bool getEntryLocked(const DNSName &content, const QType& qtype, CacheEntryType cet, vector<DNSZoneRecord>& entry, int zoneID=-1);
83
84
85 struct CacheEntry
86 {
87 CacheEntry() { qtype = ctype = 0; zoneID = -1; meritsRecursion=false; dnssecOk=false; hasEDNS=false; created=0; ttd=0; maxReplyLen=512;}
88
89 DNSName qname;
90 string value;
91 vector<DNSZoneRecord> drs;
92 time_t created;
93 time_t ttd;
94
95 uint16_t qtype;
96 uint16_t ctype;
97 int zoneID;
98 unsigned int maxReplyLen;
99
100 bool meritsRecursion;
101 bool dnssecOk;
102 bool hasEDNS;
103 };
104
105 void getTTLS();
106
107 struct UnorderedNameTag{};
108 struct SequenceTag{};
109 typedef multi_index_container<
110 CacheEntry,
111 indexed_by <
112 ordered_unique<
113 composite_key<
114 CacheEntry,
115 member<CacheEntry,DNSName,&CacheEntry::qname>,
116 member<CacheEntry,uint16_t,&CacheEntry::qtype>,
117 member<CacheEntry,uint16_t, &CacheEntry::ctype>,
118 member<CacheEntry,int, &CacheEntry::zoneID>,
119 member<CacheEntry,bool, &CacheEntry::meritsRecursion>,
120 member<CacheEntry,unsigned int, &CacheEntry::maxReplyLen>,
121 member<CacheEntry,bool, &CacheEntry::dnssecOk>,
122 member<CacheEntry,bool, &CacheEntry::hasEDNS>
123 >,
124 composite_key_compare<CanonDNSNameCompare, std::less<uint16_t>, std::less<uint16_t>, std::less<int>, std::less<bool>,
125 std::less<unsigned int>, std::less<bool>, std::less<bool> >
126 >,
127 hashed_non_unique<tag<UnorderedNameTag>, composite_key<CacheEntry,
128 member<CacheEntry,DNSName,&CacheEntry::qname>,
129 member<CacheEntry,uint16_t,&CacheEntry::qtype>,
130 member<CacheEntry,uint16_t, &CacheEntry::ctype>,
131 member<CacheEntry,int, &CacheEntry::zoneID> > > ,
132 sequenced<tag<SequenceTag>>
133 >
134 > cmap_t;
135
136
137 struct MapCombo
138 {
139 pthread_rwlock_t d_mut;
140 cmap_t d_map;
141 };
142
143 vector<MapCombo> d_maps;
144 MapCombo& getMap(const DNSName& qname)
145 {
146 return d_maps[qname.hash() % d_maps.size()];
147 }
148
149 AtomicCounter d_ops;
150 time_t d_lastclean; // doesn't need to be atomic
151 unsigned long d_nextclean;
152 unsigned int d_cleaninterval;
153 bool d_cleanskipped;
154 AtomicCounter *d_statnumhit;
155 AtomicCounter *d_statnummiss;
156 AtomicCounter *d_statnumentries;
157
158 int d_ttl;
159 int d_recursivettl;
160 bool d_doRecursion;
161
162 static constexpr unsigned int s_mincleaninterval=1000, s_maxcleaninterval=300000;
163 };
164
165
166
167 #endif /* PACKETCACHE_HH */