1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
/*
* This file is part of PowerDNS or dnsdist.
* Copyright -- PowerDNS.COM B.V. and its contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* In addition, for the avoidance of any doubt, permission is granted to
* link this program with OpenSSL and to (re)distribute the binaries
* produced as the result of such linking.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#pragma once
#include <time.h>
#include <unordered_map>
#include <boost/variant.hpp>
#include "circular_buffer.hh"
#include "dnsname.hh"
#include "iputils.hh"
#include "lock.hh"
#include "stat_t.hh"
#include "dnsdist-protocols.hh"
#include "dnsdist-mac-address.hh"
struct Rings {
struct Query
{
ComboAddress requestor;
DNSName name;
struct timespec when;
struct dnsheader dh;
uint16_t size;
uint16_t qtype;
// incoming protocol
dnsdist::Protocol protocol;
#if defined(DNSDIST_RINGS_WITH_MACADDRESS)
dnsdist::MacAddress macaddress;
bool hasmac{false};
#endif
};
struct Response
{
ComboAddress requestor;
ComboAddress ds; // who handled it
DNSName name;
struct timespec when;
struct dnsheader dh;
unsigned int usec;
unsigned int size;
uint16_t qtype;
// outgoing protocol
dnsdist::Protocol protocol;
};
struct Shard
{
LockGuarded<boost::circular_buffer<Query>> queryRing;
LockGuarded<boost::circular_buffer<Response>> respRing;
};
Rings(size_t capacity=10000, size_t numberOfShards=10, size_t nbLockTries=5, bool keepLockingStats=false): d_blockingQueryInserts(0), d_blockingResponseInserts(0), d_deferredQueryInserts(0), d_deferredResponseInserts(0), d_nbQueryEntries(0), d_nbResponseEntries(0), d_currentShardId(0), d_capacity(capacity), d_numberOfShards(numberOfShards), d_nbLockTries(nbLockTries), d_keepLockingStats(keepLockingStats)
{
}
std::unordered_map<int, vector<boost::variant<string,double> > > getTopBandwidth(unsigned int numentries);
size_t numDistinctRequestors();
/* this function should not be called after init() has been called */
void setCapacity(size_t newCapacity, size_t numberOfShards);
/* This function should only be called at configuration time before any query or response has been inserted */
void init();
void setNumberOfLockRetries(size_t retries);
void setRecordQueries(bool);
void setRecordResponses(bool);
size_t getNumberOfShards() const
{
return d_numberOfShards;
}
size_t getNumberOfQueryEntries() const
{
return d_nbQueryEntries;
}
size_t getNumberOfResponseEntries() const
{
return d_nbResponseEntries;
}
void insertQuery(const struct timespec& when, const ComboAddress& requestor, const DNSName& name, uint16_t qtype, uint16_t size, const struct dnsheader& dh, dnsdist::Protocol protocol)
{
#if defined(DNSDIST_RINGS_WITH_MACADDRESS)
dnsdist::MacAddress macaddress;
bool hasmac{false};
if (dnsdist::MacAddressesCache::get(requestor, macaddress.data(), macaddress.size()) == 0) {
hasmac = true;
}
#endif
for (size_t idx = 0; idx < d_nbLockTries; idx++) {
auto& shard = getOneShard();
auto lock = shard->queryRing.try_lock();
if (lock.owns_lock()) {
#if defined(DNSDIST_RINGS_WITH_MACADDRESS)
insertQueryLocked(*lock, when, requestor, name, qtype, size, dh, protocol, macaddress, hasmac);
#else
insertQueryLocked(*lock, when, requestor, name, qtype, size, dh, protocol);
#endif
return;
}
if (d_keepLockingStats) {
++d_deferredQueryInserts;
}
}
/* out of luck, let's just wait */
if (d_keepLockingStats) {
++d_blockingResponseInserts;
}
auto& shard = getOneShard();
auto lock = shard->queryRing.lock();
#if defined(DNSDIST_RINGS_WITH_MACADDRESS)
insertQueryLocked(*lock, when, requestor, name, qtype, size, dh, protocol, macaddress, hasmac);
#else
insertQueryLocked(*lock, when, requestor, name, qtype, size, dh, protocol);
#endif
}
void insertResponse(const struct timespec& when, const ComboAddress& requestor, const DNSName& name, uint16_t qtype, unsigned int usec, unsigned int size, const struct dnsheader& dh, const ComboAddress& backend, dnsdist::Protocol protocol)
{
for (size_t idx = 0; idx < d_nbLockTries; idx++) {
auto& shard = getOneShard();
auto lock = shard->respRing.try_lock();
if (lock.owns_lock()) {
insertResponseLocked(*lock, when, requestor, name, qtype, usec, size, dh, backend, protocol);
return;
}
if (d_keepLockingStats) {
++d_deferredResponseInserts;
}
}
/* out of luck, let's just wait */
if (d_keepLockingStats) {
++d_blockingResponseInserts;
}
auto& shard = getOneShard();
auto lock = shard->respRing.lock();
insertResponseLocked(*lock, when, requestor, name, qtype, usec, size, dh, backend, protocol);
}
void clear()
{
for (auto& shard : d_shards) {
shard->queryRing.lock()->clear();
shard->respRing.lock()->clear();
}
d_nbQueryEntries.store(0);
d_nbResponseEntries.store(0);
d_currentShardId.store(0);
d_blockingQueryInserts.store(0);
d_blockingResponseInserts.store(0);
d_deferredQueryInserts.store(0);
d_deferredResponseInserts.store(0);
}
/* this should be called in the unit tests, and never at runtime */
void reset()
{
clear();
d_initialized = false;
}
/* load the content of the ring buffer from a file in the format emitted by grepq(),
only useful for debugging purposes */
size_t loadFromFile(const std::string& filepath, const struct timespec& now);
bool shouldRecordQueries() const
{
return d_recordQueries;
}
bool shouldRecordResponses() const
{
return d_recordResponses;
}
std::vector<std::unique_ptr<Shard> > d_shards;
pdns::stat_t d_blockingQueryInserts;
pdns::stat_t d_blockingResponseInserts;
pdns::stat_t d_deferredQueryInserts;
pdns::stat_t d_deferredResponseInserts;
private:
size_t getShardId()
{
return (d_currentShardId++ % d_numberOfShards);
}
std::unique_ptr<Shard>& getOneShard()
{
return d_shards[getShardId()];
}
#if defined(DNSDIST_RINGS_WITH_MACADDRESS)
void insertQueryLocked(boost::circular_buffer<Query>& ring, const struct timespec& when, const ComboAddress& requestor, const DNSName& name, uint16_t qtype, uint16_t size, const struct dnsheader& dh, dnsdist::Protocol protocol, const dnsdist::MacAddress& macaddress, const bool hasmac)
#else
void insertQueryLocked(boost::circular_buffer<Query>& ring, const struct timespec& when, const ComboAddress& requestor, const DNSName& name, uint16_t qtype, uint16_t size, const struct dnsheader& dh, dnsdist::Protocol protocol)
#endif
{
if (!ring.full()) {
d_nbQueryEntries++;
}
#if defined(DNSDIST_RINGS_WITH_MACADDRESS)
Rings::Query query{requestor, name, when, dh, size, qtype, protocol, dnsdist::MacAddress{""}, hasmac};
if (hasmac) {
memcpy(query.macaddress.data(), macaddress.data(), macaddress.size());
}
ring.push_back(std::move(query));
#else
ring.push_back({requestor, name, when, dh, size, qtype, protocol});
#endif
}
void insertResponseLocked(boost::circular_buffer<Response>& ring, const struct timespec& when, const ComboAddress& requestor, const DNSName& name, uint16_t qtype, unsigned int usec, unsigned int size, const struct dnsheader& dh, const ComboAddress& backend, dnsdist::Protocol protocol)
{
if (!ring.full()) {
d_nbResponseEntries++;
}
ring.push_back({requestor, backend, name, when, dh, usec, size, qtype, protocol});
}
std::atomic<size_t> d_nbQueryEntries;
std::atomic<size_t> d_nbResponseEntries;
std::atomic<size_t> d_currentShardId;
std::atomic<bool> d_initialized{false};
size_t d_capacity;
size_t d_numberOfShards;
size_t d_nbLockTries = 5;
bool d_keepLockingStats{false};
bool d_recordQueries{true};
bool d_recordResponses{true};
};
extern Rings g_rings;
|