root/trunk/pdns/pdns/common_startup.cc @ 1960

Revision 1960, 15.3 KB (checked in by ahu, 2 years ago)

make the signingpipe multithreaded, achieving around 8000 RSASHA256/1024 signatures/s so far on an 8 core machine

  • Property svn:eol-style set to native
  • Property svn:keywords set to author date id revision
Line 
1/*
2    PowerDNS Versatile Database Driven Nameserver
3    Copyright (C) 2005 - 2010  PowerDNS.COM BV
4
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License version 2
7    as published by the Free Software Foundation
8   
9
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18*/
19#include "common_startup.hh"
20
21typedef Distributor<DNSPacket,DNSPacket,PacketHandler> DNSDistributor;
22
23
24ArgvMap theArg;
25StatBag S;  //!< Statistics are gathered accross PDNS via the StatBag class S
26PacketCache PC; //!< This is the main PacketCache, shared accross all threads
27DNSProxy *DP;
28DynListener *dl;
29CommunicatorClass Communicator;
30UDPNameserver *N;
31int avg_latency;
32TCPNameserver *TN;
33
34ArgvMap &arg()
35{
36  return theArg;
37}
38
39void declareArguments()
40{
41  ::arg().set("local-port","The port on which we listen")="53";
42  ::arg().setSwitch("log-failed-updates","If PDNS should log failed update requests")="";
43  ::arg().setSwitch("log-dns-details","If PDNS should log DNS non-erroneous details")="";
44  ::arg().setSwitch("allow-recursion-override","Set this so that local data fully overrides the recursor")="no";
45  ::arg().set("urlredirector","Where we send hosts to that need to be url redirected")="127.0.0.1";
46  ::arg().set("smtpredirector","Our smtpredir MX host")="a.misconfigured.powerdns.smtp.server";
47  ::arg().set("local-address","Local IP addresses to which we bind")="0.0.0.0";
48  ::arg().set("local-ipv6","Local IP address to which we bind")="";
49  ::arg().set("query-local-address","Source IP address for sending queries")="";
50  ::arg().set("max-queue-length","Maximum queuelength before considering situation lost")="5000";
51  ::arg().set("soa-serial-offset","Make sure that no SOA serial is less than this number")="0";
52 
53  ::arg().set("retrieval-threads", "Number of AXFR-retrieval threads for slave operation")="2";
54
55  ::arg().setCmd("help","Provide a helpful message");
56  ::arg().setCmd("version","Output version and compilation date");
57  ::arg().setCmd("config","Provide configuration file on standard output");
58  ::arg().setCmd("list-modules","Lists all modules available");
59  ::arg().setCmd("no-config","Don't parse configuration file");
60 
61  ::arg().set("version-string","PowerDNS version in packets - full, anonymous, powerdns or custom")="full"; 
62  ::arg().set("control-console","Debugging switch - don't use")="no"; // but I know you will!
63  ::arg().set("fancy-records","Process URL and MBOXFW records")="no";
64  ::arg().set("wildcard-url","Process URL and MBOXFW records")="no";
65  ::arg().set("wildcards","Honor wildcards in the database")="";
66  ::arg().set("loglevel","Amount of logging. Higher is more. Do not set below 3")="4";
67  ::arg().set("default-soa-name","name to insert in the SOA record if none set in the backend")="a.misconfigured.powerdns.server";
68  ::arg().set("distributor-threads","Default number of Distributor (backend) threads to start")="3";
69  ::arg().set("signing-threads","Default number of signer threads to start")="3";
70  ::arg().set("receiver-threads","Default number of Distributor (backend) threads to start")="1";
71  ::arg().set("queue-limit","Maximum number of milliseconds to queue a query")="1500"; 
72  ::arg().set("recursor","If recursion is desired, IP address of a recursing nameserver")="no"; 
73  ::arg().set("lazy-recursion","Only recurse if question cannot be answered locally")="yes";
74  ::arg().set("allow-recursion","List of subnets that are allowed to recurse")="0.0.0.0/0";
75  ::arg().set("pipebackend-abi-version","Version of the pipe backend ABI")="1";
76 
77  ::arg().set("disable-tcp","Do not listen to TCP queries")="no";
78  ::arg().set("disable-axfr","Do not allow zone transfers")="no";
79 
80  ::arg().set("config-name","Name of this virtual configuration - will rename the binary image")="";
81
82  ::arg().set("load-modules","Load this module - supply absolute or relative path")="";
83  ::arg().set("launch","Which backends to launch and order to query them in")="";
84  ::arg().setSwitch("disable-axfr","Disable zonetransfers but do allow TCP queries")="no";
85  ::arg().set("allow-axfr-ips","Allow zonetransfers only to these subnets")="0.0.0.0/0";
86  ::arg().set("slave-cycle-interval","Reschedule failed SOA serial checks once every .. seconds")="60";
87
88  ::arg().set("tcp-control-address","If set, PowerDNS can be controlled over TCP on this address")="";
89  ::arg().set("tcp-control-port","If set, PowerDNS can be controlled over TCP on this address")="53000";
90  ::arg().set("tcp-control-secret","If set, PowerDNS can be controlled over TCP after passing this secret")="";
91  ::arg().set("tcp-control-range","If set, remote control of PowerDNS is possible over these networks only")="127.0.0.0/8, 10.0.0.0/8, 192.168.0.0/16, 172.16.0.0/12, ::1/128, fe80::/10";
92 
93  ::arg().setSwitch("slave","Act as a slave")="no";
94  ::arg().setSwitch("master","Act as a master")="no";
95  ::arg().setSwitch("guardian","Run within a guardian process")="no";
96  ::arg().setSwitch("skip-cname","Do not perform CNAME indirection for each query")="no";
97  ::arg().setSwitch("strict-rfc-axfrs","Perform strictly rfc compliant axfrs (very slow)")="no";
98  ::arg().setSwitch("send-root-referral","Send out old-fashioned root-referral instead of ServFail in case of no authority")="no";
99
100  ::arg().setSwitch("webserver","Start a webserver for monitoring")="no"; 
101  ::arg().setSwitch("webserver-print-arguments","If the webserver should print arguments")="no"; 
102  ::arg().set("webserver-address","IP Address of webserver to listen on")="127.0.0.1";
103  ::arg().set("webserver-port","Port of webserver to listen on")="8081";
104  ::arg().set("webserver-password","Password required for accessing the webserver")="";
105
106  ::arg().setSwitch("out-of-zone-additional-processing","Do out of zone additional processing")="yes";
107  ::arg().setSwitch("do-ipv6-additional-processing", "Do AAAA additional processing")="no";
108  ::arg().setSwitch("query-logging","Hint backends that queries should be logged")="no";
109 
110  ::arg().set("cache-ttl","Seconds to store packets in the PacketCache")="20";
111  ::arg().set("recursive-cache-ttl","Seconds to store packets in the PacketCache")="10";
112  ::arg().set("negquery-cache-ttl","Seconds to store packets in the PacketCache")="60";
113  ::arg().set("query-cache-ttl","Seconds to store packets in the PacketCache")="20";
114  ::arg().set("soa-minimum-ttl","Default SOA mininum ttl")="3600";
115  ::arg().set("server-id", "Returned when queried for 'server.id' TXT or NSID, defaults to hostname")="";
116  ::arg().set("soa-refresh-default","Default SOA refresh")="10800";
117  ::arg().set("soa-retry-default","Default SOA retry")="3600";
118  ::arg().set("soa-expire-default","Default SOA expire")="604800";
119
120  ::arg().set("trusted-notification-proxy", "IP address of incoming notification proxy")="";
121  ::arg().set("slave-renotify", "If we should send out notifications for slaved updates")="no";
122
123  ::arg().set("default-ttl","Seconds a result is valid if not set otherwise")="3600";
124  ::arg().set("max-tcp-connections","Maximum number of TCP connections")="10";
125  ::arg().setSwitch("no-shuffle","Set this to prevent random shuffling of answers - for regression testing")="off";
126  ::arg().setSwitch("per-zone-axfr-acls","When set, backends that implement it perform per-zone AXFL ACL checks")="off";
127
128  ::arg().setSwitch( "use-logfile", "Use a log file (Windows only)" )= "no";
129  ::arg().set( "logfile", "Logfile to use (Windows only)" )= "pdns.log";
130  ::arg().set("setuid","If set, change user id to this uid for more security")="";
131  ::arg().set("setgid","If set, change group id to this gid for more security")="";
132
133  ::arg().set("max-cache-entries", "Maximum number of cache entries")="1000000";
134  ::arg().set("entropy-source", "If set, read entropy from this file")="/dev/urandom";
135}
136
137void declareStats(void)
138{
139  S.declare("udp-queries","Number of UDP queries received");
140  S.declare("udp-answers","Number of answers sent out over UDP");
141
142  S.declare("udp4-answers","Number of IPv4 answers sent out over UDP");
143  S.declare("udp4-queries","Number of IPv4UDP queries received");
144  S.declare("udp6-answers","Number of IPv6 answers sent out over UDP");
145  S.declare("udp6-queries","Number of IPv6 UDP queries received");
146
147  S.declare("recursing-answers","Number of recursive answers sent out");
148  S.declare("recursing-questions","Number of questions sent to recursor");
149  S.declare("corrupt-packets","Number of corrupt packets received");
150
151  S.declare("tcp-queries","Number of TCP queries received");
152  S.declare("tcp-answers","Number of answers sent out over TCP");
153
154  S.declare("qsize-q","Number of questions waiting for database attention");
155
156  S.declare("deferred-cache-inserts","Amount of cache inserts that were deferred because of maintenance");
157  S.declare("deferred-cache-lookup","Amount of cache lookups that were deferred because of maintenance");
158
159  S.declare("query-cache-hit","Number of hits on the query cache");
160  S.declare("query-cache-miss","Number of misses on the query cache");
161
162
163  S.declare("servfail-packets","Number of times a server-failed packet was sent out");
164  S.declare("latency","Average number of microseconds needed to answer a question");
165  S.declare("timedout-packets","Number of packets which weren't answered within timeout set");
166
167  S.declareRing("queries","UDP Queries Received");
168  S.declareRing("nxdomain-queries","Queries for non-existent records within existent domains");
169  S.declareRing("noerror-queries","Queries for existing records, but for type we don't have");
170  S.declareRing("servfail-queries","Queries that could not be answered due to backend errors");
171  S.declareRing("unauth-queries","Queries for domains that we are not authoritative for");
172  S.declareRing("logmessages","Log Messages");
173  S.declareRing("remotes","Remote server IP addresses");
174  S.declareRing("remotes-unauth","Remote hosts querying domains for which we are not auth");
175  S.declareRing("remotes-corrupt","Remote hosts sending corrupt packets");
176
177}
178
179
180int isGuarded(char **argv)
181{
182  char *p=strstr(argv[0],"-instance");
183
184  return !!p;
185}
186
187
188void sendout(const DNSDistributor::AnswerData &AD)
189{
190  static unsigned int &numanswered=*S.getPointer("udp-answers");
191  static unsigned int &numanswered4=*S.getPointer("udp4-answers");
192  static unsigned int &numanswered6=*S.getPointer("udp6-answers");
193
194  if(!AD.A)
195    return;
196 
197  N->send(AD.A);
198  numanswered++;
199
200  if(AD.A->remote.getSocklen()==sizeof(sockaddr_in))
201    numanswered4++;
202  else
203    numanswered6++;
204
205  int diff=AD.A->d_dt.udiff();
206  avg_latency=(int)(1023*avg_latency/1024+diff/1024);
207
208  delete AD.A; 
209}
210
211static DNSDistributor* g_distributor;
212static pthread_mutex_t d_distributorlock =PTHREAD_MUTEX_INITIALIZER;
213static bool g_mustlockdistributor;
214
215//! The qthread receives questions over the internet via the Nameserver class, and hands them to the Distributor for futher processing
216void *qthread(void *number)
217{
218  DNSPacket *P;
219
220  DNSPacket question;
221  DNSPacket cached;
222
223  unsigned int &numreceived=*S.getPointer("udp-queries");
224  unsigned int &numanswered=*S.getPointer("udp-answers");
225
226  unsigned int &numreceived4=*S.getPointer("udp4-queries");
227  unsigned int &numanswered4=*S.getPointer("udp4-answers");
228
229  unsigned int &numreceived6=*S.getPointer("udp6-queries");
230  unsigned int &numanswered6=*S.getPointer("udp6-answers");
231  numreceived=-1;
232  int diff;
233  bool logDNSDetails= ::arg().mustDo("log-dns-details");
234 
235  for(;;) {
236    if(number==0) { // only run on main thread
237      if(!((numreceived++)%250)) { // maintenance tasks
238        S.set("latency",(int)avg_latency);
239        int qcount, acount;
240        g_distributor->getQueueSizes(qcount, acount);
241        S.set("qsize-q",qcount);
242      }
243    }
244
245    if(!(P=N->receive(&question))) { // receive a packet         inline
246      continue;                    // packet was broken, try again
247    }
248
249    if(P->remote.getSocklen()==sizeof(sockaddr_in))
250      numreceived4++;
251    else
252      numreceived6++;
253
254    S.ringAccount("queries", P->qdomain+"/"+P->qtype.getName());
255    S.ringAccount("remotes",P->getRemote());
256    if(logDNSDetails) 
257      L << Logger::Notice<<"Remote "<< P->remote.toString() <<" wants '" << P->qdomain<<"|"<<P->qtype.getName() << 
258        "', do = " <<P->d_dnssecOk <<", bufsize = "<< P->getMaxReplyLen()<<": ";
259
260    if((P->d.opcode != Opcode::Notify) && P->couldBeCached() && PC.get(P, &cached)) { // short circuit - does the PacketCache recognize this question?
261      if(logDNSDetails)
262        L<<"packetcache HIT"<<endl;
263      cached.setRemote(&P->remote);  // inlined
264      cached.setSocket(P->getSocket());                               // inlined
265      cached.setMaxReplyLen(P->getMaxReplyLen());
266      cached.d.rd=P->d.rd; // copy in recursion desired bit
267      cached.d.id=P->d.id;
268      cached.commitD(); // commit d to the packet                        inlined
269
270      N->send(&cached);   // answer it then                              inlined
271      diff=P->d_dt.udiff();                                                   
272      avg_latency=(int)(0.999*avg_latency+0.001*diff); // 'EWMA'
273     
274      numanswered++;
275      if(P->remote.sin4.sin_family==AF_INET)
276        numanswered4++;
277      else
278        numanswered6++;
279
280      continue;
281    }
282    if(logDNSDetails)
283        L<<"packetcache MISS"<<endl;
284    if(g_mustlockdistributor) {
285      Lock l(&d_distributorlock);
286      g_distributor->question(P, &sendout); // otherwise, give to the distributor
287    }
288    else
289      g_distributor->question(P, &sendout); // otherwise, give to the distributor
290  }
291  return 0;
292}
293
294void mainthread()
295{
296  Utility::srandom(time(0));
297
298   int newgid=0;     
299   if(!::arg()["setgid"].empty()) 
300     newgid=Utility::makeGidNumeric(::arg()["setgid"]);     
301   int newuid=0;     
302   if(!::arg()["setuid"].empty())       
303     newuid=Utility::makeUidNumeric(::arg()["setuid"]); 
304#ifndef WIN32
305
306   if(!::arg()["chroot"].empty()) { 
307     if(::arg().mustDo("master") || ::arg().mustDo("slave"))
308        gethostbyname("a.root-servers.net"); // this forces all lookup libraries to be loaded
309     if(chroot(::arg()["chroot"].c_str())<0 || chdir("/")<0) {
310       L<<Logger::Error<<"Unable to chroot to '"+::arg()["chroot"]+"': "<<strerror(errno)<<", exiting"<<endl; 
311       exit(1);
312     }   
313     else
314       L<<Logger::Error<<"Chrooted to '"<<::arg()["chroot"]<<"'"<<endl;     
315   } 
316#endif
317  StatWebServer sws;
318  Utility::dropPrivs(newuid, newgid);
319
320  if(::arg().mustDo("recursor")){
321    DP=new DNSProxy(::arg()["recursor"]);
322    DP->onlyFrom(::arg()["allow-recursion"]);
323    DP->go();
324  }
325  // NOW SAFE TO CREATE THREADS!
326  dl->go();
327
328  pthread_t qtid;
329
330
331  if(::arg().mustDo("webserver"))
332    sws.go();
333 
334  if(::arg().mustDo("slave") || ::arg().mustDo("master"))
335    Communicator.go(); 
336
337  if(TN)
338    TN->go(); // tcp nameserver launch
339   
340  //  fork(); (this worked :-))
341  g_distributor = new DNSDistributor(::arg().asNum("distributor-threads")); // the big dispatcher!
342  if(::arg().asNum("receiver-threads") > 1) {
343    g_mustlockdistributor=true;
344  }
345  for(int n=0; n < ::arg().asNum("receiver-threads"); ++n)
346    pthread_create(&qtid,0,qthread, reinterpret_cast<void *>(n)); // receives packets
347
348  void *p;
349  pthread_join(qtid, &p);
350 
351  L<<Logger::Error<<"Mainthread exiting - should never happen"<<endl;
352}
353
354
355
356
Note: See TracBrowser for help on using the browser.