/src/pdns/pdns/dnsdistdist/dnsdist-lua-bindings-packetcache.cc
Line | Count | Source |
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 | | #include <fcntl.h> |
23 | | #include <sys/stat.h> |
24 | | #include <sys/types.h> |
25 | | |
26 | | #include "config.h" |
27 | | #include "dnsdist.hh" |
28 | | #include "dnsdist-cache.hh" |
29 | | #include "dnsdist-lua.hh" |
30 | | |
31 | | void setupLuaBindingsPacketCache(LuaContext& luaCtx, bool client) |
32 | 0 | { |
33 | | /* PacketCache */ |
34 | 0 | luaCtx.writeFunction("newPacketCache", [client](size_t maxEntries, std::optional<LuaAssociativeTable<boost::variant<bool, size_t, LuaArray<uint16_t>>>> vars) { |
35 | 0 | DNSDistPacketCache::CacheSettings settings{ |
36 | 0 | .d_maxEntries = maxEntries, |
37 | 0 | .d_shardCount = 20, |
38 | 0 | }; |
39 | 0 | bool cookieHashing = false; |
40 | 0 | LuaArray<uint16_t> skipOptions; |
41 | 0 | LuaArray<uint16_t> payloadRanks; |
42 | 0 | std::unordered_set<uint16_t> ranks; |
43 | 0 | size_t maximumEntrySize{4096}; |
44 | |
|
45 | 0 | getOptionalValue<bool>(vars, "deferrableInsertLock", settings.d_deferrableInsertLock); |
46 | 0 | getOptionalValue<bool>(vars, "dontAge", settings.d_dontAge); |
47 | 0 | getOptionalValue<bool>(vars, "keepStaleData", settings.d_keepStaleData); |
48 | 0 | getOptionalValue<bool>(vars, "shuffle", settings.d_shuffle); |
49 | 0 | getOptionalValue<size_t>(vars, "maxNegativeTTL", settings.d_maxNegativeTTL); |
50 | 0 | getOptionalValue<size_t>(vars, "maxTTL", settings.d_maxTTL); |
51 | 0 | getOptionalValue<size_t>(vars, "minTTL", settings.d_minTTL); |
52 | 0 | getOptionalValue<size_t>(vars, "numberOfShards", settings.d_shardCount); |
53 | 0 | getOptionalValue<bool>(vars, "parseECS", settings.d_parseECS); |
54 | 0 | getOptionalValue<size_t>(vars, "staleTTL", settings.d_staleTTL); |
55 | 0 | getOptionalValue<size_t>(vars, "temporaryFailureTTL", settings.d_tempFailureTTL); |
56 | 0 | getOptionalValue<size_t>(vars, "truncatedTTL", settings.d_truncatedTTL); |
57 | 0 | getOptionalValue<bool>(vars, "cookieHashing", cookieHashing); |
58 | 0 | getOptionalValue<size_t>(vars, "maximumEntrySize", maximumEntrySize); |
59 | |
|
60 | 0 | if (maximumEntrySize >= sizeof(dnsheader)) { |
61 | 0 | settings.d_maximumEntrySize = maximumEntrySize; |
62 | 0 | } |
63 | |
|
64 | 0 | if (getOptionalValue<decltype(skipOptions)>(vars, "skipOptions", skipOptions) > 0) { |
65 | 0 | settings.d_optionsToSkip.clear(); |
66 | 0 | settings.d_optionsToSkip.insert(EDNSOptionCode::COOKIE); |
67 | 0 | for (const auto& option : skipOptions) { |
68 | 0 | settings.d_optionsToSkip.insert(option.second); |
69 | 0 | } |
70 | 0 | } |
71 | |
|
72 | 0 | if (getOptionalValue<decltype(payloadRanks)>(vars, "payloadRanks", payloadRanks) > 0) { |
73 | 0 | for (const auto& rank : payloadRanks) { |
74 | 0 | if (rank.second < 512 || rank.second > settings.d_maximumEntrySize) { |
75 | 0 | continue; |
76 | 0 | } |
77 | 0 | ranks.insert(rank.second); |
78 | 0 | } |
79 | 0 | if (!ranks.empty()) { |
80 | 0 | settings.d_payloadRanks.assign(ranks.begin(), ranks.end()); |
81 | 0 | std::sort(settings.d_payloadRanks.begin(), settings.d_payloadRanks.end()); |
82 | 0 | } |
83 | 0 | } |
84 | |
|
85 | 0 | if (cookieHashing) { |
86 | 0 | settings.d_optionsToSkip.erase(EDNSOptionCode::COOKIE); |
87 | 0 | } |
88 | |
|
89 | 0 | checkAllParametersConsumed("newPacketCache", vars); |
90 | |
|
91 | 0 | if (maxEntries < settings.d_shardCount) { |
92 | 0 | SLOG(warnlog("The number of entries (%d) in the packet cache is smaller than the number of shards (%d), decreasing the number of shards to %d", maxEntries, settings.d_shardCount, maxEntries), |
93 | 0 | dnsdist::logging::getTopLogger("configuration")->info(Logr::Warning, "The number of entries in the packet cache is smaller than the number of shards, decreasing the number of shards to the number of entries", "number_of_entries", Logging::Loggable(maxEntries), "number_of_shards", Logging::Loggable(settings.d_shardCount))); |
94 | 0 | g_outputBuffer += "The number of entries (" + std::to_string(maxEntries) + " in the packet cache is smaller than the number of shards (" + std::to_string(settings.d_shardCount) + "), decreasing the number of shards to " + std::to_string(maxEntries); |
95 | 0 | settings.d_shardCount = maxEntries; |
96 | 0 | } |
97 | |
|
98 | 0 | if (client) { |
99 | 0 | settings.d_maxEntries = 1; |
100 | 0 | settings.d_shardCount = 1; |
101 | 0 | } |
102 | |
|
103 | 0 | return std::make_shared<DNSDistPacketCache>(settings); |
104 | 0 | }); |
105 | |
|
106 | 0 | #ifndef DISABLE_PACKETCACHE_BINDINGS |
107 | 0 | luaCtx.registerFunction<std::string (std::shared_ptr<DNSDistPacketCache>::*)() const>("toString", [](const std::shared_ptr<DNSDistPacketCache>& cache) { |
108 | 0 | if (cache) { |
109 | 0 | return cache->toString(); |
110 | 0 | } |
111 | 0 | return std::string(); |
112 | 0 | }); |
113 | 0 | luaCtx.registerFunction<bool (std::shared_ptr<DNSDistPacketCache>::*)() const>("isFull", [](const std::shared_ptr<DNSDistPacketCache>& cache) { |
114 | 0 | if (cache) { |
115 | 0 | return cache->isFull(); |
116 | 0 | } |
117 | 0 | return false; |
118 | 0 | }); |
119 | 0 | luaCtx.registerFunction<size_t (std::shared_ptr<DNSDistPacketCache>::*)(size_t)>("purgeExpired", [](std::shared_ptr<DNSDistPacketCache>& cache, size_t upTo) { |
120 | 0 | if (cache) { |
121 | 0 | const time_t now = time(nullptr); |
122 | |
|
123 | 0 | return cache->purgeExpired(upTo, now); |
124 | 0 | } |
125 | 0 | return static_cast<size_t>(0); |
126 | 0 | }); |
127 | 0 | luaCtx.registerFunction<size_t (std::shared_ptr<DNSDistPacketCache>::*)(size_t)>("expunge", [](std::shared_ptr<DNSDistPacketCache>& cache, size_t upTo) { |
128 | 0 | if (cache) { |
129 | 0 | return cache->expunge(upTo); |
130 | 0 | } |
131 | 0 | return static_cast<size_t>(0); |
132 | 0 | }); |
133 | 0 | luaCtx.registerFunction<void (std::shared_ptr<DNSDistPacketCache>::*)(const boost::variant<LuaArray<DNSName>, LuaArray<string>, DNSName, string>& dname, std::optional<uint16_t> qtype, std::optional<bool> suffixMatch)>("expungeByName", [](std::shared_ptr<DNSDistPacketCache>& cache, const boost::variant<LuaArray<DNSName>, LuaArray<string>, DNSName, string>& dname, std::optional<uint16_t> qtype, std::optional<bool> suffixMatch) { |
134 | 0 | if (cache == nullptr) { |
135 | 0 | return; |
136 | 0 | } |
137 | 0 | DNSName qname; |
138 | 0 | vector<DNSName> qnames; |
139 | 0 | if (dname.type() == typeid(DNSName)) { |
140 | 0 | qname = boost::get<DNSName>(dname); |
141 | 0 | } |
142 | 0 | else if (dname.type() == typeid(string)) { |
143 | 0 | qname = DNSName(boost::get<string>(dname)); |
144 | 0 | } |
145 | 0 | else if (dname.type() == typeid(LuaArray<DNSName>)) { |
146 | 0 | auto qnamesArr = boost::get<LuaArray<DNSName>>(dname); |
147 | 0 | qnames.reserve(qnamesArr.size()); |
148 | 0 | for (auto const& qnameVal : qnamesArr) { |
149 | 0 | qnames.push_back(qnameVal.second); |
150 | 0 | } |
151 | 0 | } |
152 | 0 | else if (dname.type() == typeid(LuaArray<string>)) { |
153 | 0 | auto strArr = boost::get<LuaArray<string>>(dname); |
154 | 0 | qnames.reserve(strArr.size()); |
155 | 0 | for (auto const& strVal : strArr) { |
156 | 0 | qnames.push_back(DNSName(strVal.second)); |
157 | 0 | } |
158 | 0 | } |
159 | 0 | if (qnames.empty()) { |
160 | 0 | g_outputBuffer += "Expunged " + std::to_string(cache->expungeByName(qname, qtype ? *qtype : QType(QType::ANY).getCode(), suffixMatch ? *suffixMatch : false)) + " records\n"; |
161 | 0 | } |
162 | 0 | else { |
163 | 0 | g_outputBuffer += "Expunged " + std::to_string(cache->expungeByName(qnames, qtype ? *qtype : QType(QType::ANY).getCode(), suffixMatch ? *suffixMatch : false)) + " records\n"; |
164 | 0 | } |
165 | 0 | }); |
166 | 0 | luaCtx.registerFunction<void (std::shared_ptr<DNSDistPacketCache>::*)() const>("printStats", [](const std::shared_ptr<DNSDistPacketCache>& cache) { |
167 | 0 | if (cache) { |
168 | 0 | g_outputBuffer = "Entries: " + std::to_string(cache->getEntriesCount()) + "/" + std::to_string(cache->getMaxEntries()) + "\n"; |
169 | 0 | g_outputBuffer += "Hits: " + std::to_string(cache->getHits()) + "\n"; |
170 | 0 | g_outputBuffer += "Misses: " + std::to_string(cache->getMisses()) + "\n"; |
171 | 0 | g_outputBuffer += "Deferred inserts: " + std::to_string(cache->getDeferredInserts()) + "\n"; |
172 | 0 | g_outputBuffer += "Deferred lookups: " + std::to_string(cache->getDeferredLookups()) + "\n"; |
173 | 0 | g_outputBuffer += "Lookup Collisions: " + std::to_string(cache->getLookupCollisions()) + "\n"; |
174 | 0 | g_outputBuffer += "Insert Collisions: " + std::to_string(cache->getInsertCollisions()) + "\n"; |
175 | 0 | g_outputBuffer += "TTL Too Shorts: " + std::to_string(cache->getTTLTooShorts()) + "\n"; |
176 | 0 | g_outputBuffer += "Cleanup Count: " + std::to_string(cache->getCleanupCount()) + "\n"; |
177 | 0 | } |
178 | 0 | }); |
179 | 0 | luaCtx.registerFunction<LuaAssociativeTable<uint64_t> (std::shared_ptr<DNSDistPacketCache>::*)() const>("getStats", [](const std::shared_ptr<DNSDistPacketCache>& cache) { |
180 | 0 | LuaAssociativeTable<uint64_t> stats; |
181 | 0 | if (cache) { |
182 | 0 | stats["entries"] = cache->getEntriesCount(); |
183 | 0 | stats["maxEntries"] = cache->getMaxEntries(); |
184 | 0 | stats["hits"] = cache->getHits(); |
185 | 0 | stats["misses"] = cache->getMisses(); |
186 | 0 | stats["deferredInserts"] = cache->getDeferredInserts(); |
187 | 0 | stats["deferredLookups"] = cache->getDeferredLookups(); |
188 | 0 | stats["lookupCollisions"] = cache->getLookupCollisions(); |
189 | 0 | stats["insertCollisions"] = cache->getInsertCollisions(); |
190 | 0 | stats["ttlTooShorts"] = cache->getTTLTooShorts(); |
191 | 0 | stats["cleanupCount"] = cache->getCleanupCount(); |
192 | 0 | } |
193 | 0 | return stats; |
194 | 0 | }); |
195 | |
|
196 | 0 | luaCtx.registerFunction<LuaArray<DNSName> (std::shared_ptr<DNSDistPacketCache>::*)(const ComboAddress& addr) const>("getDomainListByAddress", [](const std::shared_ptr<DNSDistPacketCache>& cache, const ComboAddress& addr) { |
197 | 0 | LuaArray<DNSName> results; |
198 | 0 | if (!cache) { |
199 | 0 | return results; |
200 | 0 | } |
201 | | |
202 | 0 | int counter = 1; |
203 | 0 | auto domains = cache->getDomainsContainingRecords(addr); |
204 | 0 | results.reserve(domains.size()); |
205 | 0 | for (auto& domain : domains) { |
206 | 0 | results.emplace_back(counter, std::move(domain)); |
207 | 0 | counter++; |
208 | 0 | } |
209 | 0 | return results; |
210 | 0 | }); |
211 | |
|
212 | 0 | luaCtx.registerFunction<LuaArray<ComboAddress> (std::shared_ptr<DNSDistPacketCache>::*)(const DNSName& domain) const>("getAddressListByDomain", [](const std::shared_ptr<DNSDistPacketCache>& cache, const DNSName& domain) { |
213 | 0 | LuaArray<ComboAddress> results; |
214 | 0 | if (!cache) { |
215 | 0 | return results; |
216 | 0 | } |
217 | | |
218 | 0 | int counter = 1; |
219 | 0 | auto addresses = cache->getRecordsForDomain(domain); |
220 | 0 | results.reserve(addresses.size()); |
221 | 0 | for (auto& address : addresses) { |
222 | 0 | results.emplace_back(counter, std::move(address)); |
223 | 0 | counter++; |
224 | 0 | } |
225 | 0 | return results; |
226 | 0 | }); |
227 | |
|
228 | 0 | luaCtx.registerFunction<void (std::shared_ptr<DNSDistPacketCache>::*)(const std::string& fname, std::optional<bool> rawResponse) const>("dump", [](const std::shared_ptr<DNSDistPacketCache>& cache, const std::string& fname, std::optional<bool> rawResponse) { |
229 | 0 | if (cache) { |
230 | |
|
231 | 0 | int fd = open(fname.c_str(), O_CREAT | O_EXCL | O_WRONLY, 0660); |
232 | 0 | if (fd < 0) { |
233 | 0 | g_outputBuffer = "Error opening dump file for writing: " + stringerror() + "\n"; |
234 | 0 | return; |
235 | 0 | } |
236 | | |
237 | 0 | uint64_t records = 0; |
238 | 0 | try { |
239 | 0 | records = cache->dump(fd, rawResponse ? *rawResponse : false); |
240 | 0 | } |
241 | 0 | catch (const std::exception& e) { |
242 | 0 | close(fd); |
243 | 0 | throw; |
244 | 0 | } |
245 | | |
246 | 0 | close(fd); |
247 | |
|
248 | 0 | g_outputBuffer += "Dumped " + std::to_string(records) + " records\n"; |
249 | 0 | } |
250 | 0 | }); |
251 | 0 | #endif /* DISABLE_PACKETCACHE_BINDINGS */ |
252 | 0 | } |