/src/pdns/pdns/dnsdistdist/dnsdist-rings.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 | | |
23 | | #include <fstream> |
24 | | |
25 | | #include "dnsdist-rings.hh" |
26 | | |
27 | | Rings g_rings; |
28 | | |
29 | | thread_local size_t Rings::t_samplingQueryCounter{0}; |
30 | | thread_local size_t Rings::t_samplingResponseCounter{0}; |
31 | | |
32 | | void Rings::init(const RingsConfiguration& config) |
33 | 0 | { |
34 | 0 | if (d_initialized.exchange(true)) { |
35 | 0 | throw std::runtime_error("Rings::init() should only be called once"); |
36 | 0 | } |
37 | | |
38 | 0 | d_capacity = config.capacity; |
39 | 0 | d_numberOfShards = config.numberOfShards; |
40 | 0 | d_nbLockTries = config.nbLockTries; |
41 | 0 | d_samplingRate = config.samplingRate; |
42 | 0 | d_recordQueries = config.recordQueries; |
43 | 0 | d_recordResponses = config.recordResponses; |
44 | 0 | if (d_numberOfShards <= 1) { |
45 | 0 | d_nbLockTries = 0; |
46 | 0 | } |
47 | |
|
48 | 0 | d_shards.resize(d_numberOfShards); |
49 | | |
50 | | /* resize all the rings */ |
51 | 0 | for (auto& shard : d_shards) { |
52 | 0 | shard = std::make_unique<Shard>(); |
53 | 0 | if (shouldRecordQueries()) { |
54 | 0 | shard->queryRing.lock()->set_capacity(d_capacity / d_numberOfShards); |
55 | 0 | } |
56 | 0 | if (shouldRecordResponses()) { |
57 | 0 | shard->respRing.lock()->set_capacity(d_capacity / d_numberOfShards); |
58 | 0 | } |
59 | 0 | } |
60 | | |
61 | | /* we just recreated the shards so they are now empty */ |
62 | 0 | d_nbQueryEntries = 0; |
63 | 0 | d_nbResponseEntries = 0; |
64 | 0 | } |
65 | | |
66 | | size_t Rings::numDistinctRequestors() |
67 | 0 | { |
68 | 0 | std::set<ComboAddress, ComboAddress::addressOnlyLessThan> requestors; |
69 | 0 | for (const auto& shard : d_shards) { |
70 | 0 | auto queries = shard->queryRing.lock(); |
71 | 0 | for (const auto& query : *queries) { |
72 | 0 | requestors.insert(query.requestor); |
73 | 0 | } |
74 | 0 | } |
75 | 0 | return requestors.size(); |
76 | 0 | } |
77 | | |
78 | | std::unordered_map<int, vector<boost::variant<string, double>>> Rings::getTopBandwidth(unsigned int numentries) |
79 | 0 | { |
80 | 0 | map<ComboAddress, unsigned int, ComboAddress::addressOnlyLessThan> counts; |
81 | 0 | uint64_t total = 0; |
82 | 0 | for (const auto& shard : d_shards) { |
83 | 0 | { |
84 | 0 | auto queries = shard->queryRing.lock(); |
85 | 0 | for (const auto& query : *queries) { |
86 | 0 | counts[query.requestor] += query.size; |
87 | 0 | total += query.size; |
88 | 0 | } |
89 | 0 | } |
90 | 0 | { |
91 | 0 | auto responses = shard->respRing.lock(); |
92 | 0 | for (const auto& response : *responses) { |
93 | 0 | counts[response.requestor] += response.size; |
94 | 0 | total += response.size; |
95 | 0 | } |
96 | 0 | } |
97 | 0 | } |
98 | |
|
99 | 0 | using ret_t = vector<pair<unsigned int, ComboAddress>>; |
100 | 0 | ret_t rcounts; |
101 | 0 | rcounts.reserve(counts.size()); |
102 | 0 | for (const auto& count : counts) { |
103 | 0 | rcounts.emplace_back(count.second, count.first); |
104 | 0 | } |
105 | 0 | numentries = rcounts.size() < numentries ? rcounts.size() : numentries; |
106 | 0 | partial_sort(rcounts.begin(), rcounts.begin() + numentries, rcounts.end(), [](const ret_t::value_type& lhs, const ret_t::value_type& rhs) { |
107 | 0 | return (rhs.first < lhs.first); |
108 | 0 | }); |
109 | 0 | std::unordered_map<int, vector<boost::variant<string, double>>> ret; |
110 | 0 | uint64_t rest = 0; |
111 | 0 | int count = 1; |
112 | 0 | for (const auto& rcount : rcounts) { |
113 | 0 | if (count == static_cast<int>(numentries + 1)) { |
114 | 0 | rest += rcount.first; |
115 | 0 | } |
116 | 0 | else { |
117 | 0 | ret.insert({count++, {rcount.second.toString(), rcount.first, 100.0 * rcount.first / static_cast<double>(total)}}); |
118 | 0 | } |
119 | 0 | } |
120 | |
|
121 | 0 | if (total > 0) { |
122 | 0 | ret.insert({count, {"Rest", rest, 100.0 * static_cast<double>(rest) / static_cast<double>(total)}}); |
123 | 0 | } |
124 | 0 | else { |
125 | 0 | ret.insert({count, {"Rest", rest, 100.0}}); |
126 | 0 | } |
127 | |
|
128 | 0 | return ret; |
129 | 0 | } |
130 | | |
131 | | size_t Rings::loadFromFile(const std::string& filepath, const struct timespec& now) |
132 | 0 | { |
133 | 0 | ifstream ifs(filepath); |
134 | 0 | if (!ifs) { |
135 | 0 | throw std::runtime_error("unable to open the file at " + filepath); |
136 | 0 | } |
137 | | |
138 | 0 | size_t inserted = 0; |
139 | 0 | string line; |
140 | 0 | dnsheader dnsHeader{}; |
141 | 0 | memset(&dnsHeader, 0, sizeof(dnsHeader)); |
142 | |
|
143 | 0 | while (std::getline(ifs, line)) { |
144 | 0 | boost::trim_right_if(line, boost::is_any_of(" \r\n\x1a")); |
145 | 0 | boost::trim_left(line); |
146 | 0 | bool isResponse = false; |
147 | 0 | vector<string> parts; |
148 | 0 | stringtok(parts, line, " \t,"); |
149 | |
|
150 | 0 | if (parts.size() == 8) { |
151 | 0 | } |
152 | 0 | else if (parts.size() >= 11 && parts.size() <= 13) { |
153 | 0 | isResponse = true; |
154 | 0 | } |
155 | 0 | else { |
156 | 0 | cerr << "skipping line with " << parts.size() << "parts: " << line << endl; |
157 | 0 | continue; |
158 | 0 | } |
159 | | |
160 | 0 | size_t idx = 0; |
161 | 0 | vector<string> timeStr; |
162 | 0 | stringtok(timeStr, parts.at(idx++), "."); |
163 | 0 | if (timeStr.size() != 2) { |
164 | 0 | cerr << "skipping invalid time " << parts.at(0) << endl; |
165 | 0 | continue; |
166 | 0 | } |
167 | | |
168 | 0 | timespec when{}; |
169 | 0 | try { |
170 | 0 | when.tv_sec = now.tv_sec + std::stoi(timeStr.at(0)); |
171 | 0 | when.tv_nsec = now.tv_nsec + static_cast<long>(std::stoi(timeStr.at(1)) * 100 * 1000 * 1000); |
172 | 0 | } |
173 | 0 | catch (const std::exception& e) { |
174 | 0 | cerr << "error parsing time " << parts.at(idx - 1) << " from line " << line << endl; |
175 | 0 | continue; |
176 | 0 | } |
177 | | |
178 | 0 | ComboAddress from(parts.at(idx++)); |
179 | 0 | ComboAddress dest; |
180 | 0 | dnsdist::Protocol protocol(parts.at(idx++)); |
181 | 0 | if (isResponse) { |
182 | 0 | dest = ComboAddress(parts.at(idx++)); |
183 | 0 | } |
184 | | /* skip ID */ |
185 | 0 | idx++; |
186 | 0 | DNSName qname(parts.at(idx++)); |
187 | 0 | QType qtype(QType::chartocode(parts.at(idx++).c_str())); |
188 | |
|
189 | 0 | if (isResponse) { |
190 | 0 | insertResponse(when, from, std::move(qname), qtype.getCode(), 0, 0, dnsHeader, dest, protocol); |
191 | 0 | } |
192 | 0 | else { |
193 | 0 | insertQuery(when, from, qname, qtype.getCode(), 0, dnsHeader, protocol); |
194 | 0 | } |
195 | 0 | ++inserted; |
196 | 0 | } |
197 | | |
198 | 0 | return inserted; |
199 | 0 | } |
200 | | |
201 | | bool Rings::Response::isACacheHit() const |
202 | 0 | { |
203 | 0 | bool hit = ds.sin4.sin_family == 0; |
204 | 0 | if (!hit && ds.isIPv4() && ds.sin4.sin_addr.s_addr == 0 && ds.sin4.sin_port == 0) { |
205 | 0 | hit = true; |
206 | 0 | } |
207 | 0 | return hit; |
208 | 0 | } |
209 | | |
210 | | bool Rings::Response::isADrop() const |
211 | 0 | { |
212 | 0 | return dh.rcode == 0 && usec == std::numeric_limits<uint32_t>::max(); |
213 | 0 | } |