/src/pdns/pdns/dnsdistdist/dnsdist-cache.hh
Line | Count | Source (jump to first uncovered line) |
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 | | #pragma once |
23 | | |
24 | | #include <atomic> |
25 | | #include <unordered_map> |
26 | | |
27 | | #include "iputils.hh" |
28 | | #include "lock.hh" |
29 | | #include "noinitvector.hh" |
30 | | #include "stat_t.hh" |
31 | | #include "ednsoptions.hh" |
32 | | |
33 | | struct DNSQuestion; |
34 | | |
35 | | class DNSDistPacketCache : boost::noncopyable |
36 | | { |
37 | | public: |
38 | | DNSDistPacketCache(size_t maxEntries, uint32_t maxTTL=86400, uint32_t minTTL=0, uint32_t tempFailureTTL=60, uint32_t maxNegativeTTL=3600, uint32_t staleTTL=60, bool dontAge=false, uint32_t shards=1, bool deferrableInsertLock=true, bool parseECS=false); |
39 | | |
40 | | void insert(uint32_t key, const boost::optional<Netmask>& subnet, uint16_t queryFlags, bool dnssecOK, const DNSName& qname, uint16_t qtype, uint16_t qclass, const PacketBuffer& response, bool receivedOverUDP, uint8_t rcode, boost::optional<uint32_t> tempFailureTTL); |
41 | | bool get(DNSQuestion& dq, uint16_t queryId, uint32_t* keyOut, boost::optional<Netmask>& subnet, bool dnssecOK, bool receivedOverUDP, uint32_t allowExpired = 0, bool skipAging = false, bool truncatedOK = true, bool recordMiss = true); |
42 | | size_t purgeExpired(size_t upTo, const time_t now); |
43 | | size_t expunge(size_t upTo=0); |
44 | | size_t expungeByName(const DNSName& name, uint16_t qtype=QType::ANY, bool suffixMatch=false); |
45 | | bool isFull(); |
46 | | string toString(); |
47 | | uint64_t getSize(); |
48 | 0 | uint64_t getHits() const { return d_hits.load(); } |
49 | 0 | uint64_t getMisses() const { return d_misses.load(); } |
50 | 0 | uint64_t getDeferredLookups() const { return d_deferredLookups.load(); } |
51 | 0 | uint64_t getDeferredInserts() const { return d_deferredInserts.load(); } |
52 | 0 | uint64_t getLookupCollisions() const { return d_lookupCollisions.load(); } |
53 | 0 | uint64_t getInsertCollisions() const { return d_insertCollisions.load(); } |
54 | 0 | uint64_t getMaxEntries() const { return d_maxEntries; } |
55 | 0 | uint64_t getTTLTooShorts() const { return d_ttlTooShorts.load(); } |
56 | 0 | uint64_t getCleanupCount() const { return d_cleanupCount.load(); } |
57 | | uint64_t getEntriesCount(); |
58 | | uint64_t dump(int fd); |
59 | | |
60 | | /* get the list of domains (qnames) that contains the given address in an A or AAAA record */ |
61 | | std::set<DNSName> getDomainsContainingRecords(const ComboAddress& addr); |
62 | | /* get the list of IP addresses contained in A or AAAA for a given domains (qname) */ |
63 | | std::set<ComboAddress> getRecordsForDomain(const DNSName& domain); |
64 | | |
65 | | void setSkippedOptions(const std::unordered_set<uint16_t>& optionsToSkip); |
66 | | |
67 | 0 | bool isECSParsingEnabled() const { return d_parseECS; } |
68 | | |
69 | | bool keepStaleData() const |
70 | 0 | { |
71 | 0 | return d_keepStaleData; |
72 | 0 | } |
73 | | void setKeepStaleData(bool keep) |
74 | 0 | { |
75 | 0 | d_keepStaleData = keep; |
76 | 0 | } |
77 | | |
78 | | |
79 | | void setECSParsingEnabled(bool enabled) |
80 | 2.45k | { |
81 | 2.45k | d_parseECS = enabled; |
82 | 2.45k | } |
83 | | |
84 | | uint32_t getKey(const DNSName::string_t& qname, size_t qnameWireLength, const PacketBuffer& packet, bool receivedOverUDP); |
85 | | |
86 | | static uint32_t getMinTTL(const char* packet, uint16_t length, bool* seenNoDataSOA); |
87 | | static bool getClientSubnet(const PacketBuffer& packet, size_t qnameWireLength, boost::optional<Netmask>& subnet); |
88 | | |
89 | | private: |
90 | | |
91 | | struct CacheValue |
92 | | { |
93 | 0 | time_t getTTD() const { return validity; } |
94 | | std::string value; |
95 | | DNSName qname; |
96 | | boost::optional<Netmask> subnet; |
97 | | uint16_t qtype{0}; |
98 | | uint16_t qclass{0}; |
99 | | uint16_t queryFlags{0}; |
100 | | time_t added{0}; |
101 | | time_t validity{0}; |
102 | | uint16_t len{0}; |
103 | | bool receivedOverUDP{false}; |
104 | | bool dnssecOK{false}; |
105 | | }; |
106 | | |
107 | | class CacheShard |
108 | | { |
109 | | public: |
110 | | CacheShard() |
111 | 2.45k | { |
112 | 2.45k | } |
113 | | CacheShard(const CacheShard& /* old */) |
114 | 0 | { |
115 | 0 | } |
116 | | |
117 | | void setSize(size_t maxSize) |
118 | 2.45k | { |
119 | 2.45k | d_map.write_lock()->reserve(maxSize); |
120 | 2.45k | } |
121 | | |
122 | | SharedLockGuarded<std::unordered_map<uint32_t,CacheValue>> d_map; |
123 | | std::atomic<uint64_t> d_entriesCount{0}; |
124 | | }; |
125 | | |
126 | | bool cachedValueMatches(const CacheValue& cachedValue, uint16_t queryFlags, const DNSName& qname, uint16_t qtype, uint16_t qclass, bool receivedOverUDP, bool dnssecOK, const boost::optional<Netmask>& subnet) const; |
127 | | uint32_t getShardIndex(uint32_t key) const; |
128 | | void insertLocked(CacheShard& shard, std::unordered_map<uint32_t,CacheValue>& map, uint32_t key, CacheValue& newValue); |
129 | | |
130 | | std::vector<CacheShard> d_shards; |
131 | | std::unordered_set<uint16_t> d_optionsToSkip{EDNSOptionCode::COOKIE}; |
132 | | |
133 | | pdns::stat_t d_deferredLookups{0}; |
134 | | pdns::stat_t d_deferredInserts{0}; |
135 | | pdns::stat_t d_hits{0}; |
136 | | pdns::stat_t d_misses{0}; |
137 | | pdns::stat_t d_insertCollisions{0}; |
138 | | pdns::stat_t d_lookupCollisions{0}; |
139 | | pdns::stat_t d_ttlTooShorts{0}; |
140 | | pdns::stat_t d_cleanupCount{0}; |
141 | | |
142 | | size_t d_maxEntries; |
143 | | uint32_t d_shardCount; |
144 | | uint32_t d_maxTTL; |
145 | | uint32_t d_tempFailureTTL; |
146 | | uint32_t d_maxNegativeTTL; |
147 | | uint32_t d_minTTL; |
148 | | uint32_t d_staleTTL; |
149 | | bool d_dontAge; |
150 | | bool d_deferrableInsertLock; |
151 | | bool d_parseECS; |
152 | | bool d_keepStaleData{false}; |
153 | | }; |