/
dnstcpbench.cc
304 lines (248 loc) · 8.47 KB
/
dnstcpbench.cc
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
PowerDNS Versatile Database Driven Nameserver
Copyright (C) 2002-2013 PowerDNS.COM BV
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
as published by the Free Software Foundation
Additionally, the license of this program contains a special
exception which allows to distribute the program in binary form when
it is linked against OpenSSL.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <boost/accumulators/statistics/median.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics.hpp>
#include "dnsparser.hh"
#include "sstuff.hh"
#include "misc.hh"
#include "dnswriter.hh"
#include "dnsrecords.hh"
#include "statbag.hh"
#include <netinet/tcp.h>
#include <boost/array.hpp>
#include <boost/program_options.hpp>
StatBag S;
namespace po = boost::program_options;
po::variables_map g_vm;
bool g_verbose;
bool g_onlyTCP;
bool g_tcpNoDelay;
unsigned int g_timeoutMsec;
AtomicCounter g_networkErrors, g_otherErrors, g_OK, g_truncates, g_authAnswers, g_timeOuts;
ComboAddress g_dest;
unsigned int makeUsec(const struct timeval& tv)
{
return 1000000*tv.tv_sec + tv.tv_usec;
}
/* On Linux, run echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
to prevent running out of free TCP ports */
struct BenchQuery
{
BenchQuery(const std::string& qname_, uint16_t qtype_) : qname(qname_), qtype(qtype_), udpUsec(0), tcpUsec(0), answerSecond(0) {}
BenchQuery(){}
DNSName qname;
uint16_t qtype;
uint32_t udpUsec, tcpUsec;
time_t answerSecond;
};
void doQuery(BenchQuery* q)
try
{
vector<uint8_t> packet;
DNSPacketWriter pw(packet, q->qname, q->qtype);
int res;
string reply;
struct timeval tv, now;
gettimeofday(&tv, 0);
if(!g_onlyTCP) {
Socket udpsock(g_dest.sin4.sin_family, SOCK_DGRAM);
udpsock.sendTo(string((char*)&*packet.begin(), (char*)&*packet.end()), g_dest);
ComboAddress origin;
res = waitForData(udpsock.getHandle(), 0, 1000 * g_timeoutMsec);
if(res < 0)
throw NetworkError("Error waiting for response");
if(!res) {
g_timeOuts++;
return;
}
udpsock.recvFrom(reply, origin);
gettimeofday(&now, 0);
q->udpUsec = makeUsec(now - tv);
tv=now;
MOADNSParser mdp(reply);
if(!mdp.d_header.tc)
return;
g_truncates++;
}
Socket sock(g_dest.sin4.sin_family, SOCK_STREAM);
int tmp=1;
if(setsockopt(sock.getHandle(),SOL_SOCKET,SO_REUSEADDR,(char*)&tmp,sizeof tmp)<0)
throw runtime_error("Unable to set socket reuse: "+string(strerror(errno)));
if(g_tcpNoDelay && setsockopt(sock.getHandle(), IPPROTO_TCP, TCP_NODELAY,(char*)&tmp,sizeof tmp)<0)
throw runtime_error("Unable to set socket no delay: "+string(strerror(errno)));
sock.connect(g_dest);
uint16_t len = htons(packet.size());
string tcppacket((char*)& len, 2);
tcppacket.append((char*)&*packet.begin(), (char*)&*packet.end());
sock.writen(tcppacket);
res = waitForData(sock.getHandle(), 0, 1000 * g_timeoutMsec);
if(res < 0)
throw NetworkError("Error waiting for response");
if(!res) {
g_timeOuts++;
return;
}
if(sock.read((char *) &len, 2) != 2)
throw PDNSException("tcp read failed");
len=ntohs(len);
char *creply = new char[len];
int n=0;
int numread;
while(n<len) {
numread=sock.read(creply+n, len-n);
if(numread<0)
throw PDNSException("tcp read failed");
n+=numread;
}
reply=string(creply, len);
delete[] creply;
gettimeofday(&now, 0);
q->tcpUsec = makeUsec(now - tv);
q->answerSecond = now.tv_sec;
MOADNSParser mdp(reply);
// cout<<"Had correct TCP/IP response, "<<mdp.d_answers.size()<<" answers, aabit="<<mdp.d_header.aa<<endl;
if(mdp.d_header.aa)
g_authAnswers++;
g_OK++;
}
catch(NetworkError& ne)
{
cerr<<"Network error: "<<ne.what()<<endl;
g_networkErrors++;
}
catch(...)
{
g_otherErrors++;
}
/* read queries from stdin, put in vector
launch n worker threads, each picks a query using AtomicCounter
If a worker reaches the end of its queue, it stops */
AtomicCounter g_pos;
vector<BenchQuery> g_queries;
static void* worker(void*)
{
for(;;) {
unsigned int pos = g_pos++;
if(pos >= g_queries.size())
break;
doQuery(&g_queries[pos]); // this is safe as long as nobody *inserts* to g_queries
}
return 0;
}
int main(int argc, char** argv)
try
{
po::options_description desc("Allowed options"), hidden, alloptions;
desc.add_options()
("help,h", "produce help message")
("verbose,v", "be verbose")
("udp-first,u", "try UDP first")
("file,f", po::value<string>(), "source file - if not specified, defaults to stdin")
("tcp-no-delay", po::value<bool>()->default_value(true), "use TCP_NODELAY socket option")
("timeout-msec", po::value<int>()->default_value(10), "wait for this amount of milliseconds for an answer")
("workers", po::value<int>()->default_value(100), "number of parallel workers");
hidden.add_options()
("remote-host", po::value<string>(), "remote-host")
("remote-port", po::value<int>()->default_value(53), "remote-port");
alloptions.add(desc).add(hidden);
po::positional_options_description p;
p.add("remote-host", 1);
p.add("remote-port", 1);
po::store(po::command_line_parser(argc, argv).options(alloptions).positional(p).run(), g_vm);
po::notify(g_vm);
if(g_vm.count("help")) {
cout << desc<<endl;
exit(EXIT_SUCCESS);
}
g_tcpNoDelay = g_vm["tcp-no-delay"].as<bool>();
g_onlyTCP = !g_vm.count("udp-first");
g_verbose = g_vm.count("verbose");
g_timeoutMsec = g_vm["timeout-msec"].as<int>();
reportAllTypes();
if(g_vm["remote-host"].empty()) {
cerr<<"Syntax: tcpbench remote [port] < queries"<<endl;
cerr<<"Where queries is one query per line, format: qname qtype, just 1 space"<<endl;
cerr<<desc<<endl;
exit(EXIT_FAILURE);
}
g_dest = ComboAddress(g_vm["remote-host"].as<string>().c_str(), g_vm["remote-port"].as<int>());
unsigned int numworkers=g_vm["workers"].as<int>();
if(g_verbose) {
cout<<"Sending queries to: "<<g_dest.toStringWithPort()<<endl;
cout<<"Attempting UDP first: " << (g_onlyTCP ? "no" : "yes") <<endl;
cout<<"Timeout: "<< g_timeoutMsec<<"msec"<<endl;
cout << "Using TCP_NODELAY: "<<g_tcpNoDelay<<endl;
}
pthread_t workers[numworkers];
FILE* fp;
if(!g_vm.count("file"))
fp=fdopen(0, "r");
else {
fp=fopen(g_vm["file"].as<string>().c_str(), "r");
if(!fp)
unixDie("Unable to open "+g_vm["file"].as<string>()+" for input");
}
pair<string, string> q;
string line;
while(stringfgets(fp, line)) {
trim_right(line);
q=splitField(line, ' ');
g_queries.push_back(BenchQuery(q.first, DNSRecordContent::TypeToNumber(q.second)));
}
fclose(fp);
for(unsigned int n = 0; n < numworkers; ++n) {
pthread_create(&workers[n], 0, worker, 0);
}
for(unsigned int n = 0; n < numworkers; ++n) {
void* status;
pthread_join(workers[n], &status);
}
using namespace boost::accumulators;
typedef accumulator_set<
double
, stats<boost::accumulators::tag::median(with_p_square_quantile),
boost::accumulators::tag::mean(immediate)
>
> acc_t;
acc_t udpspeeds, tcpspeeds, qps;
typedef map<time_t, uint32_t> counts_t;
counts_t counts;
for(const BenchQuery& bq : g_queries) {
counts[bq.answerSecond]++;
udpspeeds(bq.udpUsec);
tcpspeeds(bq.tcpUsec);
}
for(const counts_t::value_type& val : counts) {
qps(val.second);
}
cout<<"Average qps: "<<mean(qps)<<", median qps: "<<median(qps)<<endl;
cout<<"Average UDP latency: "<<mean(udpspeeds)<<"usec, median: "<<median(udpspeeds)<<"usec"<<endl;
cout<<"Average TCP latency: "<<mean(tcpspeeds)<<"usec, median: "<<median(tcpspeeds)<<"usec"<<endl;
cout<<"OK: "<<g_OK<<", network errors: "<<g_networkErrors<<", other errors: "<<g_otherErrors<<endl;
cout<<"Timeouts: "<<g_timeOuts<<endl;
cout<<"Truncateds: "<<g_truncates<<", auth answers: "<<g_authAnswers<<endl;
}
catch(std::exception &e)
{
cerr<<"Fatal: "<<e.what()<<endl;
}