]>
Commit | Line | Data |
---|---|---|
12471842 PL |
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 | */ | |
df111b53 | 22 | #pragma once |
11e1e08b | 23 | #include "config.h" |
df111b53 | 24 | #include "ext/luawrapper/include/LuaContext.hpp" |
25 | #include <time.h> | |
26 | #include "misc.hh" | |
27 | #include "iputils.hh" | |
28 | #include "dnsname.hh" | |
29 | #include <atomic> | |
30 | #include <boost/circular_buffer.hpp> | |
e16fd59c | 31 | #include <boost/variant.hpp> |
df111b53 | 32 | #include <mutex> |
33 | #include <thread> | |
bffca8b9 | 34 | #include <unistd.h> |
ecbe9133 | 35 | #include "sholder.hh" |
11e1e08b | 36 | #include "dnscrypt.hh" |
886e2cf2 | 37 | #include "dnsdist-cache.hh" |
85c7ca75 | 38 | #include "gettime.hh" |
87b515ed RG |
39 | #include "dnsdist-dynbpf.hh" |
40 | #include "bpf-filter.hh" | |
26a6373d SO |
41 | #include <string> |
42 | #include <unordered_map> | |
26a6373d SO |
43 | |
44 | ||
d8c19b98 RG |
45 | #ifdef HAVE_PROTOBUF |
46 | #include <boost/uuid/uuid.hpp> | |
47 | #include <boost/uuid/uuid_generators.hpp> | |
48 | #endif | |
49 | ||
42fae326 | 50 | void* carbonDumpThread(); |
61d1b966 | 51 | uint64_t uptimeOfProcess(const std::string& str); |
bd1c631b | 52 | |
7b925432 RG |
53 | extern uint16_t g_ECSSourcePrefixV4; |
54 | extern uint16_t g_ECSSourcePrefixV6; | |
55 | extern bool g_ECSOverride; | |
56 | ||
26a6373d SO |
57 | class QTag |
58 | { | |
26a6373d | 59 | public: |
5b8255ba RG |
60 | QTag() |
61 | { | |
62 | } | |
26a6373d | 63 | |
5b8255ba RG |
64 | ~QTag() |
65 | { | |
66 | } | |
26a6373d | 67 | |
5b8255ba RG |
68 | void add(const std::string strLabel, const std::string strValue) |
69 | { | |
70 | tagData.insert( {strLabel, strValue}); | |
71 | return; | |
741ebe08 | 72 | } |
26a6373d | 73 | |
5b8255ba RG |
74 | std::string getMatch(const std::string& strLabel) const |
75 | { | |
76 | std::unordered_map<std::string, std::string>::const_iterator got =tagData.find (strLabel); | |
77 | if(got == tagData.end()) { | |
78 | return ""; | |
79 | } else { | |
80 | return got->second; | |
741ebe08 SO |
81 | } |
82 | } | |
e6956c91 | 83 | |
5b8255ba RG |
84 | std::string getEntry(size_t iEntry) const |
85 | { | |
86 | std::string strEntry; | |
87 | size_t iCounter = 0; | |
88 | ||
89 | for (const auto& itr : tagData) { | |
90 | iCounter++; | |
91 | if(iCounter == iEntry) { | |
92 | strEntry = itr.first; | |
93 | strEntry += strSep; | |
94 | strEntry += itr.second; | |
95 | break; | |
96 | } | |
97 | } | |
26a6373d | 98 | |
5b8255ba RG |
99 | return strEntry; |
100 | } | |
26a6373d | 101 | |
5b8255ba RG |
102 | size_t count() const |
103 | { | |
104 | return tagData.size(); | |
105 | } | |
26a6373d | 106 | |
5b8255ba RG |
107 | std::string dumpString() const |
108 | { | |
109 | std::string strRet; | |
e6956c91 | 110 | |
5b8255ba RG |
111 | for (const auto& itr : tagData) { |
112 | strRet += itr.first; | |
113 | strRet += strSep; | |
114 | strRet += itr.second; | |
115 | strRet += "\n"; | |
116 | } | |
117 | return strRet; | |
741ebe08 | 118 | } |
26a6373d | 119 | |
29cd61cc | 120 | std::unordered_map<std::string, std::string>tagData; |
26a6373d SO |
121 | |
122 | private: | |
c959e01a | 123 | static constexpr char const *strSep = "\t"; |
26a6373d SO |
124 | }; |
125 | ||
0beaa5c8 RG |
126 | #ifdef HAVE_PROTOBUF |
127 | extern thread_local boost::uuids::random_generator t_uuidGenerator; | |
128 | #endif | |
26a6373d | 129 | |
7b925432 RG |
130 | struct DNSQuestion |
131 | { | |
132 | DNSQuestion(const DNSName* name, uint16_t type, uint16_t class_, const ComboAddress* lc, const ComboAddress* rem, struct dnsheader* header, size_t bufferSize, uint16_t queryLen, bool isTcp): qname(name), qtype(type), qclass(class_), local(lc), remote(rem), dh(header), size(bufferSize), len(queryLen), ecsPrefixLength(rem->sin4.sin_family == AF_INET ? g_ECSSourcePrefixV4 : g_ECSSourcePrefixV6), tcp(isTcp), ecsOverride(g_ECSOverride) { } | |
133 | ||
134 | #ifdef HAVE_PROTOBUF | |
ec48a28d | 135 | boost::optional<boost::uuids::uuid> uniqueId; |
7b925432 RG |
136 | #endif |
137 | const DNSName* qname; | |
138 | const uint16_t qtype; | |
139 | const uint16_t qclass; | |
140 | const ComboAddress* local; | |
141 | const ComboAddress* remote; | |
e6956c91 | 142 | std::shared_ptr<QTag> qTag; |
7b925432 RG |
143 | struct dnsheader* dh; |
144 | size_t size; | |
145 | uint16_t len; | |
146 | uint16_t ecsPrefixLength; | |
147 | const bool tcp; | |
148 | bool skipCache{false}; | |
149 | bool ecsOverride; | |
5b8255ba | 150 | bool useECS{true}; |
7b925432 RG |
151 | }; |
152 | ||
153 | struct DNSResponse : DNSQuestion | |
154 | { | |
2b3eefc3 | 155 | DNSResponse(const DNSName* name, uint16_t type, uint16_t class_, const ComboAddress* lc, const ComboAddress* rem, struct dnsheader* header, size_t bufferSize, uint16_t responseLen, bool isTcp, const struct timespec* queryTime_): DNSQuestion(name, type, class_, lc, rem, header, bufferSize, responseLen, isTcp), queryTime(queryTime_) { } |
7b925432 RG |
156 | |
157 | const struct timespec* queryTime; | |
158 | }; | |
159 | ||
160 | /* so what could you do: | |
161 | drop, | |
162 | fake up nxdomain, | |
163 | provide actual answer, | |
164 | allow & and stop processing, | |
165 | continue processing, | |
166 | modify header: (servfail|refused|notimp), set TC=1, | |
167 | send to pool */ | |
168 | ||
169 | class DNSAction | |
170 | { | |
171 | public: | |
c4f5aeff | 172 | enum class Action { Drop, Nxdomain, Refused, Spoof, Allow, HeaderModify, Pool, Delay, Truncate, None}; |
7b925432 | 173 | virtual Action operator()(DNSQuestion*, string* ruleresult) const =0; |
205f2081 RG |
174 | virtual ~DNSAction() |
175 | { | |
176 | } | |
7b925432 RG |
177 | virtual string toString() const = 0; |
178 | virtual std::unordered_map<string, double> getStats() const | |
179 | { | |
180 | return {{}}; | |
181 | } | |
182 | }; | |
183 | ||
184 | class DNSResponseAction | |
185 | { | |
186 | public: | |
187 | enum class Action { Allow, Delay, Drop, HeaderModify, None }; | |
188 | virtual Action operator()(DNSResponse*, string* ruleresult) const =0; | |
205f2081 RG |
189 | virtual ~DNSResponseAction() |
190 | { | |
191 | } | |
7b925432 RG |
192 | virtual string toString() const = 0; |
193 | }; | |
194 | ||
78ffa782 | 195 | struct DynBlock |
196 | { | |
197 | DynBlock& operator=(const DynBlock& rhs) | |
198 | { | |
199 | reason=rhs.reason; | |
200 | until=rhs.until; | |
71c94675 | 201 | domain=rhs.domain; |
7b925432 | 202 | action=rhs.action; |
78ffa782 | 203 | blocks.store(rhs.blocks); |
204 | return *this; | |
205 | } | |
71c94675 | 206 | |
78ffa782 | 207 | string reason; |
208 | struct timespec until; | |
71c94675 | 209 | DNSName domain; |
7b925432 | 210 | DNSAction::Action action; |
78ffa782 | 211 | mutable std::atomic<unsigned int> blocks; |
212 | }; | |
213 | ||
214 | extern GlobalStateHolder<NetmaskTree<DynBlock>> g_dynblockNMG; | |
f758857a | 215 | |
216 | extern vector<pair<struct timeval, std::string> > g_confDelta; | |
217 | ||
e48090d1 | 218 | struct DNSDistStats |
219 | { | |
6ad8b29a | 220 | using stat_t=std::atomic<uint64_t>; // aww yiss ;-) |
e48090d1 | 221 | stat_t responses{0}; |
222 | stat_t servfailResponses{0}; | |
223 | stat_t queries{0}; | |
e73ec7d3 | 224 | stat_t nonCompliantQueries{0}; |
d08b1cdf | 225 | stat_t nonCompliantResponses{0}; |
643a182a | 226 | stat_t rdQueries{0}; |
2efd427d | 227 | stat_t emptyQueries{0}; |
e48090d1 | 228 | stat_t aclDrops{0}; |
bd1c631b | 229 | stat_t dynBlocked{0}; |
e48090d1 | 230 | stat_t ruleDrop{0}; |
231 | stat_t ruleNXDomain{0}; | |
dd46e5e3 | 232 | stat_t ruleRefused{0}; |
e48090d1 | 233 | stat_t selfAnswered{0}; |
234 | stat_t downstreamTimeouts{0}; | |
235 | stat_t downstreamSendErrors{0}; | |
6ad8b29a | 236 | stat_t truncFail{0}; |
b8bc7e61 | 237 | stat_t noPolicy{0}; |
886e2cf2 RG |
238 | stat_t cacheHits{0}; |
239 | stat_t cacheMisses{0}; | |
42fae326 | 240 | stat_t latency0_1{0}, latency1_10{0}, latency10_50{0}, latency50_100{0}, latency100_1000{0}, latencySlow{0}; |
e48090d1 | 241 | |
e16fd59c | 242 | double latencyAvg100{0}, latencyAvg1000{0}, latencyAvg10000{0}, latencyAvg1000000{0}; |
a1a787dc | 243 | typedef std::function<uint64_t(const std::string&)> statfunction_t; |
244 | typedef boost::variant<stat_t*, double*, statfunction_t> entry_t; | |
e16fd59c | 245 | std::vector<std::pair<std::string, entry_t>> entries{ |
dd46e5e3 RG |
246 | {"responses", &responses}, |
247 | {"servfail-responses", &servfailResponses}, | |
248 | {"queries", &queries}, | |
249 | {"acl-drops", &aclDrops}, | |
dd46e5e3 RG |
250 | {"rule-drop", &ruleDrop}, |
251 | {"rule-nxdomain", &ruleNXDomain}, | |
252 | {"rule-refused", &ruleRefused}, | |
253 | {"self-answered", &selfAnswered}, | |
254 | {"downstream-timeouts", &downstreamTimeouts}, | |
255 | {"downstream-send-errors", &downstreamSendErrors}, | |
256 | {"trunc-failures", &truncFail}, | |
257 | {"no-policy", &noPolicy}, | |
258 | {"latency0-1", &latency0_1}, | |
259 | {"latency1-10", &latency1_10}, | |
260 | {"latency10-50", &latency10_50}, | |
261 | {"latency50-100", &latency50_100}, | |
262 | {"latency100-1000", &latency100_1000}, | |
263 | {"latency-slow", &latencySlow}, | |
264 | {"latency-avg100", &latencyAvg100}, | |
265 | {"latency-avg1000", &latencyAvg1000}, | |
266 | {"latency-avg10000", &latencyAvg10000}, | |
267 | {"latency-avg1000000", &latencyAvg1000000}, | |
61d1b966 | 268 | {"uptime", uptimeOfProcess}, |
a9b6db56 | 269 | {"real-memory-usage", getRealMemoryUsage}, |
a2aa00ed | 270 | {"noncompliant-queries", &nonCompliantQueries}, |
d08b1cdf | 271 | {"noncompliant-responses", &nonCompliantResponses}, |
643a182a | 272 | {"rdqueries", &rdQueries}, |
2efd427d | 273 | {"empty-queries", &emptyQueries}, |
886e2cf2 RG |
274 | {"cache-hits", &cacheHits}, |
275 | {"cache-misses", &cacheMisses}, | |
4f99f3d3 RG |
276 | {"cpu-user-msec", getCPUTimeUser}, |
277 | {"cpu-sys-msec", getCPUTimeSystem}, | |
dd46e5e3 RG |
278 | {"fd-usage", getOpenFileDescriptors}, |
279 | {"dyn-blocked", &dynBlocked}, | |
bd1c631b | 280 | {"dyn-block-nmg-size", [](const std::string&) { return g_dynblockNMG.getLocal()->size(); }} |
42fae326 | 281 | }; |
e48090d1 | 282 | }; |
283 | ||
e16fd59c | 284 | |
e48090d1 | 285 | extern struct DNSDistStats g_stats; |
286 | ||
638184e9 | 287 | |
df111b53 | 288 | struct StopWatch |
289 | { | |
58307a85 RG |
290 | StopWatch(bool realTime=false): d_needRealTime(realTime) |
291 | { | |
292 | } | |
df111b53 | 293 | struct timespec d_start{0,0}; |
58307a85 RG |
294 | bool d_needRealTime{false}; |
295 | ||
df111b53 | 296 | void start() { |
58307a85 | 297 | if(gettime(&d_start, d_needRealTime) < 0) |
df111b53 | 298 | unixDie("Getting timestamp"); |
299 | ||
300 | } | |
cf48b0ce RG |
301 | |
302 | void set(const struct timespec& from) { | |
303 | d_start = from; | |
304 | } | |
df111b53 | 305 | |
306 | double udiff() const { | |
307 | struct timespec now; | |
58307a85 | 308 | if(gettime(&now, d_needRealTime) < 0) |
df111b53 | 309 | unixDie("Getting timestamp"); |
310 | ||
311 | return 1000000.0*(now.tv_sec - d_start.tv_sec) + (now.tv_nsec - d_start.tv_nsec)/1000.0; | |
312 | } | |
313 | ||
314 | double udiffAndSet() { | |
315 | struct timespec now; | |
58307a85 | 316 | if(gettime(&now, d_needRealTime) < 0) |
df111b53 | 317 | unixDie("Getting timestamp"); |
318 | ||
319 | auto ret= 1000000.0*(now.tv_sec - d_start.tv_sec) + (now.tv_nsec - d_start.tv_nsec)/1000.0; | |
320 | d_start = now; | |
321 | return ret; | |
322 | } | |
323 | ||
324 | }; | |
325 | ||
326 | class QPSLimiter | |
327 | { | |
328 | public: | |
329 | QPSLimiter() | |
330 | { | |
331 | } | |
332 | ||
333 | QPSLimiter(unsigned int rate, unsigned int burst) : d_rate(rate), d_burst(burst), d_tokens(burst) | |
334 | { | |
335 | d_passthrough=false; | |
336 | d_prev.start(); | |
337 | } | |
338 | ||
339 | unsigned int getRate() const | |
340 | { | |
341 | return d_passthrough? 0 : d_rate; | |
342 | } | |
343 | ||
344 | int getPassed() const | |
345 | { | |
346 | return d_passed; | |
347 | } | |
348 | int getBlocked() const | |
349 | { | |
350 | return d_blocked; | |
351 | } | |
352 | ||
ecbe9133 | 353 | bool check() const // this is not quite fair |
df111b53 | 354 | { |
355 | if(d_passthrough) | |
356 | return true; | |
357 | auto delta = d_prev.udiffAndSet(); | |
358 | ||
359 | d_tokens += 1.0*d_rate * (delta/1000000.0); | |
360 | ||
361 | if(d_tokens > d_burst) | |
362 | d_tokens = d_burst; | |
363 | ||
364 | bool ret=false; | |
365 | if(d_tokens >= 1.0) { // we need this because burst=1 is weird otherwise | |
366 | ret=true; | |
367 | --d_tokens; | |
368 | d_passed++; | |
369 | } | |
370 | else | |
371 | d_blocked++; | |
372 | ||
373 | return ret; | |
374 | } | |
375 | private: | |
376 | bool d_passthrough{true}; | |
377 | unsigned int d_rate; | |
378 | unsigned int d_burst; | |
ecbe9133 | 379 | mutable double d_tokens; |
380 | mutable StopWatch d_prev; | |
381 | mutable unsigned int d_passed{0}; | |
382 | mutable unsigned int d_blocked{0}; | |
df111b53 | 383 | }; |
384 | ||
b5b93e0b RG |
385 | struct ClientState; |
386 | ||
df111b53 | 387 | struct IDState |
388 | { | |
58307a85 | 389 | IDState() : origFD(-1), sentTime(true), delayMsec(0) { origDest.sin4.sin_family = 0;} |
df111b53 | 390 | IDState(const IDState& orig) |
391 | { | |
392 | origFD = orig.origFD; | |
393 | origID = orig.origID; | |
394 | origRemote = orig.origRemote; | |
549d63c9 | 395 | origDest = orig.origDest; |
7b3865cd | 396 | delayMsec = orig.delayMsec; |
df111b53 | 397 | age.store(orig.age.load()); |
398 | } | |
399 | ||
2bf26975 | 400 | int origFD; // set to <0 to indicate this state is empty // 4 |
401 | ||
402 | ComboAddress origRemote; // 28 | |
549d63c9 | 403 | ComboAddress origDest; // 28 |
2bf26975 | 404 | StopWatch sentTime; // 16 |
405 | DNSName qname; // 80 | |
11e1e08b RG |
406 | #ifdef HAVE_DNSCRYPT |
407 | std::shared_ptr<DnsCryptQuery> dnsCryptQuery{0}; | |
d8c19b98 RG |
408 | #endif |
409 | #ifdef HAVE_PROTOBUF | |
ec48a28d | 410 | boost::optional<boost::uuids::uuid> uniqueId; |
11e1e08b | 411 | #endif |
886e2cf2 | 412 | std::shared_ptr<DNSDistPacketCache> packetCache{nullptr}; |
b5b93e0b | 413 | const ClientState* cs{nullptr}; |
886e2cf2 | 414 | uint32_t cacheKey; // 8 |
2bf26975 | 415 | std::atomic<uint16_t> age; // 4 |
416 | uint16_t qtype; // 2 | |
886e2cf2 | 417 | uint16_t qclass; // 2 |
2bf26975 | 418 | uint16_t origID; // 2 |
aeb36780 | 419 | uint16_t origFlags; // 2 |
7b3865cd | 420 | int delayMsec; |
ca404e94 | 421 | bool ednsAdded{false}; |
ff73f02b | 422 | bool ecsAdded{false}; |
886e2cf2 | 423 | bool skipCache{false}; |
7cea4e39 | 424 | bool destHarvested{false}; // if true, origDest holds the original dest addr, otherwise the listening addr |
df111b53 | 425 | }; |
426 | ||
427 | struct Rings { | |
e4c24bb3 | 428 | Rings(size_t capacity=10000) |
df111b53 | 429 | { |
e4c24bb3 RG |
430 | queryRing.set_capacity(capacity); |
431 | respRing.set_capacity(capacity); | |
0e41337b | 432 | pthread_rwlock_init(&queryLock, 0); |
df111b53 | 433 | } |
0ba5eecf | 434 | struct Query |
435 | { | |
436 | struct timespec when; | |
437 | ComboAddress requestor; | |
438 | DNSName name; | |
03ebf8b2 | 439 | uint16_t size; |
0ba5eecf | 440 | uint16_t qtype; |
3fcaeeac | 441 | struct dnsheader dh; |
0ba5eecf | 442 | }; |
443 | boost::circular_buffer<Query> queryRing; | |
df111b53 | 444 | struct Response |
445 | { | |
80a216c9 | 446 | struct timespec when; |
447 | ComboAddress requestor; | |
df111b53 | 448 | DNSName name; |
449 | uint16_t qtype; | |
df111b53 | 450 | unsigned int usec; |
80a216c9 | 451 | unsigned int size; |
3fcaeeac | 452 | struct dnsheader dh; |
2d11d1b2 | 453 | ComboAddress ds; // who handled it |
df111b53 | 454 | }; |
455 | boost::circular_buffer<Response> respRing; | |
456 | std::mutex respMutex; | |
0e41337b | 457 | pthread_rwlock_t queryLock; |
03ebf8b2 | 458 | |
7fc00937 | 459 | std::unordered_map<int, vector<boost::variant<string,double> > > getTopBandwidth(unsigned int numentries); |
a683e8bd | 460 | size_t numDistinctRequestors(); |
e4c24bb3 RG |
461 | void setCapacity(size_t newCapacity) |
462 | { | |
463 | { | |
464 | WriteLock wl(&queryLock); | |
465 | queryRing.set_capacity(newCapacity); | |
466 | } | |
467 | { | |
468 | std::lock_guard<std::mutex> lock(respMutex); | |
469 | respRing.set_capacity(newCapacity); | |
470 | } | |
471 | } | |
df111b53 | 472 | }; |
473 | ||
0e41337b | 474 | extern Rings g_rings; |
df111b53 | 475 | |
786e4d8c RS |
476 | typedef std::unordered_map<string, unsigned int> QueryCountRecords; |
477 | typedef std::function<std::tuple<bool, string>(DNSQuestion dq)> QueryCountFilter; | |
478 | struct QueryCount { | |
479 | QueryCount() | |
480 | { | |
481 | pthread_rwlock_init(&queryLock, 0); | |
482 | } | |
483 | QueryCountRecords records; | |
484 | QueryCountFilter filter; | |
485 | pthread_rwlock_t queryLock; | |
486 | bool enabled{false}; | |
487 | }; | |
488 | ||
489 | extern QueryCount g_qcount; | |
490 | ||
8a5d5053 | 491 | struct ClientState |
492 | { | |
f0e4dcba | 493 | std::set<int> cpus; |
8a5d5053 | 494 | ComboAddress local; |
11e1e08b RG |
495 | #ifdef HAVE_DNSCRYPT |
496 | DnsCryptContext* dnscryptCtx{0}; | |
497 | #endif | |
963bef8d | 498 | std::atomic<uint64_t> queries{0}; |
a36ce055 RG |
499 | int udpFD{-1}; |
500 | int tcpFD{-1}; | |
b5b93e0b | 501 | bool muted{false}; |
8429ad04 RG |
502 | |
503 | int getSocket() const | |
504 | { | |
505 | return udpFD != -1 ? udpFD : tcpFD; | |
506 | } | |
507 | ||
508 | #ifdef HAVE_EBPF | |
509 | shared_ptr<BPFFilter> d_filter; | |
510 | ||
511 | void detachFilter() | |
512 | { | |
513 | if (d_filter) { | |
514 | d_filter->removeSocket(getSocket()); | |
515 | d_filter = nullptr; | |
516 | } | |
517 | } | |
518 | ||
519 | void attachFilter(shared_ptr<BPFFilter> bpf) | |
520 | { | |
521 | detachFilter(); | |
522 | ||
523 | bpf->addSocket(getSocket()); | |
524 | d_filter = bpf; | |
525 | } | |
526 | #endif /* HAVE_EBPF */ | |
8a5d5053 | 527 | }; |
528 | ||
529 | class TCPClientCollection { | |
530 | std::vector<int> d_tcpclientthreads; | |
ded1985a | 531 | std::atomic<uint64_t> d_numthreads{0}; |
a9bf3ec4 | 532 | std::atomic<uint64_t> d_pos{0}; |
ded1985a | 533 | std::atomic<uint64_t> d_queued{0}; |
6c1ca990 | 534 | uint64_t d_maxthreads{0}; |
ded1985a | 535 | std::mutex d_mutex; |
edbda1ad RG |
536 | int d_singlePipe[2]; |
537 | bool d_useSinglePipe; | |
ded1985a | 538 | public: |
8a5d5053 | 539 | |
b79e4996 RG |
540 | TCPClientCollection(size_t maxThreads, bool useSinglePipe=false): d_maxthreads(maxThreads), d_singlePipe{-1,-1}, d_useSinglePipe(useSinglePipe) |
541 | ||
8a5d5053 | 542 | { |
a9bf3ec4 | 543 | d_tcpclientthreads.reserve(maxThreads); |
edbda1ad RG |
544 | |
545 | if (d_useSinglePipe) { | |
546 | if (pipe(d_singlePipe) < 0) { | |
547 | throw std::runtime_error("Error creating the TCP single communication pipe: " + string(strerror(errno))); | |
548 | } | |
549 | if (!setNonBlocking(d_singlePipe[1])) { | |
550 | int err = errno; | |
551 | close(d_singlePipe[0]); | |
552 | close(d_singlePipe[1]); | |
553 | throw std::runtime_error("Error setting the TCP single communication pipe non-blocking: " + string(strerror(err))); | |
554 | } | |
555 | } | |
8a5d5053 | 556 | } |
a9bf3ec4 | 557 | int getThread() |
8a5d5053 | 558 | { |
6c1ca990 | 559 | uint64_t pos = d_pos++; |
8a5d5053 | 560 | ++d_queued; |
561 | return d_tcpclientthreads[pos % d_numthreads]; | |
562 | } | |
ded1985a RG |
563 | bool hasReachedMaxThreads() const |
564 | { | |
565 | return d_numthreads >= d_maxthreads; | |
566 | } | |
567 | uint64_t getThreadsCount() const | |
568 | { | |
569 | return d_numthreads; | |
570 | } | |
571 | uint64_t getQueuedCount() const | |
572 | { | |
573 | return d_queued; | |
574 | } | |
575 | void decrementQueuedCount() | |
576 | { | |
577 | --d_queued; | |
578 | } | |
8a5d5053 | 579 | void addTCPClientThread(); |
580 | }; | |
581 | ||
a9bf3ec4 | 582 | extern std::shared_ptr<TCPClientCollection> g_tcpclientthreads; |
8a5d5053 | 583 | |
df111b53 | 584 | struct DownstreamState |
585 | { | |
fbe2a2e0 RG |
586 | DownstreamState(const ComboAddress& remote_, const ComboAddress& sourceAddr_, unsigned int sourceItf); |
587 | DownstreamState(const ComboAddress& remote_): DownstreamState(remote_, ComboAddress(), 0) {} | |
6a62c0e3 RG |
588 | ~DownstreamState() |
589 | { | |
590 | if (fd >= 0) | |
591 | close(fd); | |
592 | } | |
df111b53 | 593 | |
f99e3aaf | 594 | int fd{-1}; |
df111b53 | 595 | std::thread tid; |
596 | ComboAddress remote; | |
597 | QPSLimiter qps; | |
598 | vector<IDState> idStates; | |
fbe2a2e0 RG |
599 | ComboAddress sourceAddr; |
600 | DNSName checkName{"a.root-servers.net."}; | |
601 | QType checkType{QType::A}; | |
df111b53 | 602 | std::atomic<uint64_t> idOffset{0}; |
603 | std::atomic<uint64_t> sendErrors{0}; | |
604 | std::atomic<uint64_t> outstanding{0}; | |
605 | std::atomic<uint64_t> reuseds{0}; | |
606 | std::atomic<uint64_t> queries{0}; | |
607 | struct { | |
608 | std::atomic<uint64_t> sendErrors{0}; | |
609 | std::atomic<uint64_t> reuseds{0}; | |
610 | std::atomic<uint64_t> queries{0}; | |
611 | } prev; | |
18eeccc9 | 612 | string name; |
df111b53 | 613 | double queryLoad{0.0}; |
614 | double dropRate{0.0}; | |
615 | double latencyUsec{0.0}; | |
616 | int order{1}; | |
617 | int weight{1}; | |
b40cffe7 | 618 | int tcpConnectTimeout{5}; |
3f6d07a4 RG |
619 | int tcpRecvTimeout{30}; |
620 | int tcpSendTimeout{30}; | |
fbe2a2e0 | 621 | unsigned int sourceItf{0}; |
3f6d07a4 | 622 | uint16_t retries{5}; |
9e87dcb8 RG |
623 | uint8_t currentCheckFailures{0}; |
624 | uint8_t maxCheckFailures{1}; | |
df111b53 | 625 | StopWatch sw; |
626 | set<string> pools; | |
627 | enum class Availability { Up, Down, Auto} availability{Availability::Auto}; | |
fbe2a2e0 | 628 | bool mustResolve{false}; |
df111b53 | 629 | bool upStatus{false}; |
ca404e94 | 630 | bool useECS{false}; |
21830638 | 631 | bool setCD{false}; |
7565f4e6 | 632 | std::atomic<bool> connected{false}; |
284d460c | 633 | bool tcpFastOpen{false}; |
5602f131 | 634 | bool ipBindAddrNoPort{true}; |
df111b53 | 635 | bool isUp() const |
636 | { | |
637 | if(availability == Availability::Down) | |
638 | return false; | |
639 | if(availability == Availability::Up) | |
640 | return true; | |
641 | return upStatus; | |
642 | } | |
643 | void setUp() { availability = Availability::Up; } | |
644 | void setDown() { availability = Availability::Down; } | |
645 | void setAuto() { availability = Availability::Auto; } | |
18eeccc9 RG |
646 | string getName() const { |
647 | if (name.empty()) { | |
648 | return remote.toStringWithPort(); | |
649 | } | |
650 | return name; | |
651 | } | |
a7940c06 | 652 | string getNameWithAddr() const { |
653 | if (name.empty()) { | |
654 | return remote.toStringWithPort(); | |
655 | } | |
656 | return name + " (" + remote.toStringWithPort()+ ")"; | |
657 | } | |
9f4eb5cc RG |
658 | string getStatus() const |
659 | { | |
660 | string status; | |
661 | if(availability == DownstreamState::Availability::Up) | |
662 | status = "UP"; | |
663 | else if(availability == DownstreamState::Availability::Down) | |
664 | status = "DOWN"; | |
665 | else | |
666 | status = (upStatus ? "up" : "down"); | |
667 | return status; | |
668 | } | |
b58f08e5 | 669 | void reconnect(); |
df111b53 | 670 | }; |
671 | using servers_t =vector<std::shared_ptr<DownstreamState>>; | |
df111b53 | 672 | |
da4e7813 | 673 | template <class T> using NumberedVector = std::vector<std::pair<unsigned int, T> >; |
674 | ||
675 | void* responderThread(std::shared_ptr<DownstreamState> state); | |
676 | extern std::mutex g_luamutex; | |
677 | extern LuaContext g_lua; | |
678 | extern std::string g_outputBuffer; // locking for this is ok, as locked by g_luamutex | |
679 | ||
0940e4eb | 680 | class DNSRule |
681 | { | |
682 | public: | |
205f2081 RG |
683 | virtual ~DNSRule () |
684 | { | |
685 | } | |
497a6e3a | 686 | virtual bool matches(const DNSQuestion* dq) const =0; |
0940e4eb | 687 | virtual string toString() const = 0; |
688 | mutable std::atomic<uint64_t> d_matches{0}; | |
689 | }; | |
690 | ||
da4e7813 | 691 | using NumberedServerVector = NumberedVector<shared_ptr<DownstreamState>>; |
497a6e3a | 692 | typedef std::function<shared_ptr<DownstreamState>(const NumberedServerVector& servers, const DNSQuestion*)> policyfunc_t; |
df111b53 | 693 | |
694 | struct ServerPolicy | |
695 | { | |
696 | string name; | |
70a57b05 | 697 | policyfunc_t policy; |
df111b53 | 698 | }; |
699 | ||
886e2cf2 RG |
700 | struct ServerPool |
701 | { | |
702 | const std::shared_ptr<DNSDistPacketCache> getCache() const { return packetCache; }; | |
703 | ||
704 | NumberedVector<shared_ptr<DownstreamState>> servers; | |
705 | std::shared_ptr<DNSDistPacketCache> packetCache{nullptr}; | |
b9f8a6c8 | 706 | std::shared_ptr<ServerPolicy> policy{nullptr}; |
886e2cf2 RG |
707 | }; |
708 | using pools_t=map<std::string,std::shared_ptr<ServerPool>>; | |
742c079a | 709 | void setPoolPolicy(pools_t& pools, const string& poolName, std::shared_ptr<ServerPolicy> policy); |
886e2cf2 RG |
710 | void addServerToPool(pools_t& pools, const string& poolName, std::shared_ptr<DownstreamState> server); |
711 | void removeServerFromPool(pools_t& pools, const string& poolName, std::shared_ptr<DownstreamState> server); | |
712 | ||
42fae326 | 713 | struct CarbonConfig |
714 | { | |
d617b22c | 715 | ComboAddress server; |
42fae326 | 716 | std::string ourname; |
d617b22c | 717 | unsigned int interval; |
42fae326 | 718 | }; |
719 | ||
ca404e94 RG |
720 | enum ednsHeaderFlags { |
721 | EDNS_HEADER_FLAG_NONE = 0, | |
722 | EDNS_HEADER_FLAG_DO = 32768 | |
723 | }; | |
724 | ||
71c94675 | 725 | extern GlobalStateHolder<SuffixMatchTree<DynBlock>> g_dynblockSMT; |
dd46e5e3 | 726 | extern DNSAction::Action g_dynBlockAction; |
71c94675 | 727 | |
d617b22c | 728 | extern GlobalStateHolder<vector<CarbonConfig> > g_carbon; |
ecbe9133 | 729 | extern GlobalStateHolder<ServerPolicy> g_policy; |
730 | extern GlobalStateHolder<servers_t> g_dstates; | |
886e2cf2 | 731 | extern GlobalStateHolder<pools_t> g_pools; |
0940e4eb | 732 | extern GlobalStateHolder<vector<pair<std::shared_ptr<DNSRule>, std::shared_ptr<DNSAction> > > > g_rulactions; |
8146444b | 733 | extern GlobalStateHolder<vector<pair<std::shared_ptr<DNSRule>, std::shared_ptr<DNSResponseAction> > > > g_resprulactions; |
cf48b0ce | 734 | extern GlobalStateHolder<vector<pair<std::shared_ptr<DNSRule>, std::shared_ptr<DNSResponseAction> > > > g_cachehitresprulactions; |
638184e9 | 735 | extern GlobalStateHolder<NetmaskGroup> g_ACL; |
2e72cc0e | 736 | |
ecbe9133 | 737 | extern ComboAddress g_serverControl; // not changed during runtime |
738 | ||
f0e4dcba | 739 | extern std::vector<std::tuple<ComboAddress, bool, bool, int, std::string, std::set<int>>> g_locals; // not changed at runtime (we hope XXX) |
963bef8d | 740 | extern vector<ClientState*> g_frontends; |
ecbe9133 | 741 | extern std::string g_key; // in theory needs locking |
6ad8b29a | 742 | extern bool g_truncateTC; |
b29edbee | 743 | extern bool g_fixupCase; |
3f6d07a4 RG |
744 | extern int g_tcpRecvTimeout; |
745 | extern int g_tcpSendTimeout; | |
e0b5e49d | 746 | extern int g_udpTimeout; |
e41f8165 RG |
747 | extern uint16_t g_maxOutstanding; |
748 | extern std::atomic<bool> g_configurationDone; | |
6c1ca990 RG |
749 | extern uint64_t g_maxTCPClientThreads; |
750 | extern uint64_t g_maxTCPQueuedConnections; | |
9396d955 RG |
751 | extern size_t g_maxTCPQueriesPerConn; |
752 | extern size_t g_maxTCPConnectionDuration; | |
753 | extern size_t g_maxTCPConnectionsPerClient; | |
886e2cf2 | 754 | extern std::atomic<uint16_t> g_cacheCleaningDelay; |
f65ea0c2 | 755 | extern std::atomic<uint16_t> g_cacheCleaningPercentage; |
9e87dcb8 | 756 | extern bool g_verboseHealthChecks; |
1ea747c0 | 757 | extern uint32_t g_staleCacheEntriesTTL; |
56d68fad RG |
758 | extern bool g_apiReadWrite; |
759 | extern std::string g_apiConfigDirectory; | |
26a3cdb7 | 760 | extern bool g_servFailOnNoPolicy; |
36e763fa | 761 | extern uint32_t g_hashperturb; |
edbda1ad | 762 | extern bool g_useTCPSinglePipe; |
840ed663 | 763 | extern std::atomic<uint16_t> g_downstreamTCPCleanupInterval; |
0beaa5c8 | 764 | extern size_t g_udpVectorSize; |
ca404e94 | 765 | |
ca4252e0 RG |
766 | struct ConsoleKeyword { |
767 | std::string name; | |
768 | bool function; | |
769 | std::string parameters; | |
770 | std::string description; | |
771 | std::string toString() const | |
772 | { | |
773 | std::string res(name); | |
774 | if (function) { | |
775 | res += "(" + parameters + ")"; | |
776 | } | |
777 | res += ": "; | |
778 | res += description; | |
779 | return res; | |
780 | } | |
781 | }; | |
782 | extern const std::vector<ConsoleKeyword> g_consoleKeywords; | |
506bb661 | 783 | extern bool g_logConsoleConnections; |
ca4252e0 | 784 | |
87b515ed RG |
785 | #ifdef HAVE_EBPF |
786 | extern shared_ptr<BPFFilter> g_defaultBPFFilter; | |
8429ad04 | 787 | extern std::vector<std::shared_ptr<DynBPFFilter> > g_dynBPFFilters; |
87b515ed RG |
788 | #endif /* HAVE_EBPF */ |
789 | ||
0beaa5c8 RG |
790 | struct LocalHolders |
791 | { | |
792 | LocalHolders(): acl(g_ACL.getLocal()), policy(g_policy.getLocal()), rulactions(g_rulactions.getLocal()), cacheHitRespRulactions(g_cachehitresprulactions.getLocal()), servers(g_dstates.getLocal()), dynNMGBlock(g_dynblockNMG.getLocal()), dynSMTBlock(g_dynblockSMT.getLocal()), pools(g_pools.getLocal()) | |
793 | { | |
794 | } | |
795 | ||
796 | LocalStateHolder<NetmaskGroup> acl; | |
797 | LocalStateHolder<ServerPolicy> policy; | |
798 | LocalStateHolder<vector<pair<std::shared_ptr<DNSRule>, std::shared_ptr<DNSAction> > > > rulactions; | |
799 | LocalStateHolder<vector<pair<std::shared_ptr<DNSRule>, std::shared_ptr<DNSResponseAction> > > > cacheHitRespRulactions; | |
800 | LocalStateHolder<servers_t> servers; | |
801 | LocalStateHolder<NetmaskTree<DynBlock> > dynNMGBlock; | |
802 | LocalStateHolder<SuffixMatchTree<DynBlock> > dynSMTBlock; | |
803 | LocalStateHolder<pools_t> pools; | |
804 | }; | |
805 | ||
ecbe9133 | 806 | struct dnsheader; |
807 | ||
808 | void controlThread(int fd, ComboAddress local); | |
839f3021 | 809 | vector<std::function<void(void)>> setupLua(bool client, const std::string& config); |
886e2cf2 RG |
810 | std::shared_ptr<ServerPool> getPool(const pools_t& pools, const std::string& poolName); |
811 | std::shared_ptr<ServerPool> createPoolIfNotExists(pools_t& pools, const string& poolName); | |
812 | const NumberedServerVector& getDownstreamCandidates(const pools_t& pools, const std::string& poolName); | |
da4e7813 | 813 | |
497a6e3a | 814 | std::shared_ptr<DownstreamState> firstAvailable(const NumberedServerVector& servers, const DNSQuestion* dq); |
ecbe9133 | 815 | |
497a6e3a RG |
816 | std::shared_ptr<DownstreamState> leastOutstanding(const NumberedServerVector& servers, const DNSQuestion* dq); |
817 | std::shared_ptr<DownstreamState> wrandom(const NumberedServerVector& servers, const DNSQuestion* dq); | |
818 | std::shared_ptr<DownstreamState> whashed(const NumberedServerVector& servers, const DNSQuestion* dq); | |
819 | std::shared_ptr<DownstreamState> roundrobin(const NumberedServerVector& servers, const DNSQuestion* dq); | |
520eb5a0 | 820 | int getEDNSZ(const char* packet, unsigned int len); |
ca404e94 | 821 | uint16_t getEDNSOptionCode(const char * packet, size_t len); |
002decab | 822 | void dnsdistWebserverThread(int sock, const ComboAddress& local, const string& password, const string& apiKey, const boost::optional<std::map<std::string, std::string> >&); |
6885d4bf | 823 | bool getMsgLen32(int fd, uint32_t* len); |
824 | bool putMsgLen32(int fd, uint32_t len); | |
d8d85a30 | 825 | void* tcpAcceptorThread(void* p); |
80a216c9 | 826 | |
886e2cf2 | 827 | void moreLua(bool client); |
ffb07158 | 828 | void doClient(ComboAddress server, const std::string& command); |
829 | void doConsole(); | |
830 | void controlClientThread(int fd, ComboAddress client); | |
831 | extern "C" { | |
832 | char** my_completion( const char * text , int start, int end); | |
833 | } | |
f758857a | 834 | void setLuaNoSideEffect(); // if nothing has been declared, set that there are no side effects |
835 | void setLuaSideEffect(); // set to report a side effect, cancelling all _no_ side effect calls | |
836 | bool getLuaNoSideEffect(); // set if there were only explicit declarations of _no_ side effect | |
837 | void resetLuaSideEffect(); // reset to indeterminate state | |
11e1e08b | 838 | |
fcffc585 | 839 | bool responseContentMatches(const char* response, const uint16_t responseLen, const DNSName& qname, const uint16_t qtype, const uint16_t qclass, const ComboAddress& remote); |
0beaa5c8 | 840 | bool processQuery(LocalHolders& holders, DNSQuestion& dq, string& poolname, int* delayMsec, const struct timespec& now); |
788c3243 | 841 | bool processResponse(LocalStateHolder<vector<pair<std::shared_ptr<DNSRule>, std::shared_ptr<DNSResponseAction> > > >& localRespRulactions, DNSResponse& dr, int* delayMsec); |
ff73f02b | 842 | bool fixUpResponse(char** response, uint16_t* responseLen, size_t* responseSize, const DNSName& qname, uint16_t origFlags, bool ednsAdded, bool ecsAdded, std::vector<uint8_t>& rewrittenResponse, uint16_t addRoom); |
0f72fd5c | 843 | void restoreFlags(struct dnsheader* dh, uint16_t origFlags); |
0beaa5c8 | 844 | bool checkQueryHeaders(const struct dnsheader* dh); |
fcffc585 | 845 | |
11e1e08b | 846 | #ifdef HAVE_DNSCRYPT |
f0e4dcba | 847 | extern std::vector<std::tuple<ComboAddress,DnsCryptContext,bool,int, std::string, std::set<int>>> g_dnsCryptLocals; |
11e1e08b | 848 | |
3596a52b | 849 | int handleDnsCryptQuery(DnsCryptContext* ctx, char* packet, uint16_t len, std::shared_ptr<DnsCryptQuery>& query, uint16_t* decryptedQueryLen, bool tcp, std::vector<uint8_t>& response); |
57847d65 | 850 | bool encryptResponse(char* response, uint16_t* responseLen, size_t responseSize, bool tcp, std::shared_ptr<DnsCryptQuery> dnsCryptQuery, dnsheader** dh, dnsheader* dhCopy); |
11e1e08b | 851 | #endif |
9f4eb5cc RG |
852 | |
853 | #include "dnsdist-snmp.hh" | |
854 | ||
855 | extern bool g_snmpEnabled; | |
856 | extern bool g_snmpTrapsEnabled; | |
857 | extern DNSDistSNMPAgent* g_snmpAgent; |