source:
npl/internetserver/djbdns/patches/0007-dnscache-merge-similar-outgoing-udp-packets.patch
@
720681d
Last change on this file since 720681d was 37aaf89, checked in by , 3 years ago | |
---|---|
|
|
File size: 9.6 KB |
-
new file clients.h
Description: dnscache: merge similar outgoing udp packets This patch takes a slightly different approach to merging than the previous qmerge patch: rather than merging between the "query" and "dns_transmit" layers, it actually incorporates the merging into the dns_transmit layer. . This makes it a much more invasive and messy patch, but means that it should correctly handle all situations (in particular, ones where sending to two different IPs will gives different results, generally because one server is down or lame). . The general strategy is this: . - when a dns_transmit is about to send() a UDP query, it checks for an equivalent inprogress query. . If it finds one, it marks the inprogress dns_transmit as its "master", and itself as a "slave" of the master. . If it doesn't, it sends the packet and registers itself as inprogress. . - when a dns_transmit recv()s a UDP packet, it creates a copy of the packet for each of its slaves . Two outgoing packets are considered equivalent if: . 1. they are going to the same IP 2. they have the same qtype 3. they have the same qname . Because this change affects the dns library itself, this change can potentially affect not just dnscache, but all of the diagnostic tools. To address this, merging must be enabled explicitly by the caller; dnscache will enable merging if the MERGEQUERIES environment variable is set. . I tried to keep the patch as small and simple as possible so that its correctness could be verified by readers. There are a few places where performance might benefit from making it more complex: . - dns_transmit now knows the value of MAXUDP, since it is an upper bound on the number of slaves and inprogress queries. As a result: . - each non-merging program which uses the dns library wastes MAXUDP*sizeof(pointer) bytes of static memory for the inprogress list . - each dns_transmit uses an extra MAXUDP*sizeof(pointer) bytes for its slaves list. For dnscache, this translates to 160K total on a 32-bit platform with default MAXUDP. . Both could be avoided by using dynamic allocation. . - We have to do an O(MAXUDP) linear search to find similar inprogress queries (non-merge-enabled users of the library avoid paying this price, though). . This could be avoided by using a data structure with a fast key lookup for inprogress. . This patch is only lightly tested. Use on production servers at your own risk (and please report to the list if you have success using it). Author: Jeff King <peff () peff ! net> Date: Wed, 1 Apr 2009 14:10:54 +0000 Debian-Bug: https://bugs.debian.org/516394 Last-Update: 2020-07-26 diff --git a/clients.h b/clients.h new file mode 100644 index 0000000..983a4ad
- + 1 #ifndef CLIENTS_H 2 #define CLIENTS_H 3 4 #define MAXUDP 200 5 #define MAXTCP 20 6 7 #endif /* CLIENTS_H */ -
dns.h
diff --git a/dns.h b/dns.h index 3849f4c..d1e2ffc 100644
a b 4 4 #include "stralloc.h" 5 5 #include "iopause.h" 6 6 #include "taia.h" 7 #include "clients.h" 7 8 8 9 #define DNS_C_IN "\0\1" 9 10 #define DNS_C_ANY "\0\377" … … struct dns_transmit { 38 39 const char *servers; 39 40 char localip[4]; 40 41 char qtype[2]; 42 struct dns_transmit *master; 43 struct dns_transmit *slaves[MAXUDP]; 44 int nslaves; 41 45 } ; 42 46 47 extern void dns_enable_merge(void (*logger)(const char *, const char *, 48 const char *)); 49 43 50 extern void dns_random_init(const char *); 44 51 extern unsigned int dns_random(unsigned int); 45 52 -
dns_transmit.c
diff --git a/dns_transmit.c b/dns_transmit.c index 4d6e39f..3984776 100644
a b 7 7 #include "byte.h" 8 8 #include "uint16.h" 9 9 #include "dns.h" 10 #include "strerr.h" 11 12 static int merge_enable; 13 static void (*merge_logger)(const char *, const char *, const char *); 14 void dns_enable_merge(void (*f)(const char *, const char *, const char *)) 15 { 16 merge_enable = 1; 17 merge_logger = f; 18 } 19 20 static int merge_equal(struct dns_transmit *a, struct dns_transmit *b) 21 { 22 const char *ip1 = a->servers + 4 * a->curserver; 23 const char *ip2 = b->servers + 4 * b->curserver; 24 return 25 byte_equal(ip1, 4, ip2) && 26 byte_equal(a->qtype, 2, b->qtype) && 27 dns_domain_equal(a->query + 14, b->query + 14); 28 } 29 30 struct dns_transmit *inprogress[MAXUDP]; 31 32 static int try_merge(struct dns_transmit *d) 33 { 34 int i; 35 for (i = 0; i < MAXUDP; i++) { 36 if (!inprogress[i]) continue; 37 if (!merge_equal(d, inprogress[i])) continue; 38 d->master = inprogress[i]; 39 inprogress[i]->slaves[inprogress[i]->nslaves++] = d; 40 return 1; 41 } 42 return 0; 43 } 44 45 static void register_inprogress(struct dns_transmit *d) 46 { 47 int i; 48 for (i = 0; i < MAXUDP; i++) { 49 if (!inprogress[i]) { 50 inprogress[i] = d; 51 return; 52 } 53 } 54 strerr_die1x(100, "BUG: out of inprogress slots"); 55 } 56 57 static void unregister_inprogress(struct dns_transmit *d) 58 { 59 int i; 60 for (i = 0; i < MAXUDP; i++) { 61 if (inprogress[i] == d) 62 inprogress[i] = 0; 63 } 64 } 10 65 11 66 static int serverwantstcp(const char *buf,unsigned int len) 12 67 { … … static void packetfree(struct dns_transmit *d) 59 114 d->packet = 0; 60 115 } 61 116 117 static void mergefree(struct dns_transmit *d) 118 { 119 int i; 120 if (merge_enable) 121 unregister_inprogress(d); 122 /* unregister us from our master */ 123 if (d->master) { 124 for (i = 0; i < d->master->nslaves; i++) 125 if (d->master->slaves[i] == d) 126 d->master->slaves[i] = 0; 127 } 128 /* and unregister all of our slaves from us */ 129 for (i = 0; i < d->nslaves; i++) { 130 if (d->slaves[i]) 131 d->slaves[i]->master = NULL; 132 } 133 d->nslaves = 0; 134 } 135 62 136 static void queryfree(struct dns_transmit *d) 63 137 { 138 mergefree(d); 64 139 if (!d->query) return; 65 140 alloc_free(d->query); 66 141 d->query = 0; … … static int thisudp(struct dns_transmit *d) 99 174 const char *ip; 100 175 101 176 socketfree(d); 177 mergefree(d); 102 178 103 179 while (d->udploop < 4) { 104 180 for (;d->curserver < 16;++d->curserver) { 105 181 ip = d->servers + 4 * d->curserver; 106 182 if (byte_diff(ip,4,"\0\0\0\0")) { 183 if (merge_enable && try_merge(d)) { 184 if (merge_logger) 185 merge_logger(ip, d->qtype, d->query + 14); 186 return 0; 187 } 188 107 189 d->query[2] = dns_random(256); 108 190 d->query[3] = dns_random(256); 109 191 … … static int thisudp(struct dns_transmit *d) 118 200 taia_uint(&d->deadline,timeouts[d->udploop]); 119 201 taia_add(&d->deadline,&d->deadline,&now); 120 202 d->tcpstate = 0; 203 if (merge_enable) 204 register_inprogress(d); 121 205 return 0; 122 206 } 123 207 … … void dns_transmit_io(struct dns_transmit *d,iopause_fd *x,struct taia *deadline) 226 310 x->fd = d->s1 - 1; 227 311 228 312 switch(d->tcpstate) { 229 case 0: case 3: case 4: case 5: 230 x->events = IOPAUSE_READ; 313 case 0: 314 if (d->master) return; 315 if (d->packet) { taia_now(deadline); return; } 316 /* otherwise, fall through */ 317 case 3: case 4: case 5: 318 x->events = IOPAUSE_READ; 231 319 break; 232 320 case 1: case 2: 233 321 x->events = IOPAUSE_WRITE; … … int dns_transmit_get(struct dns_transmit *d,const iopause_fd *x,const struct tai 244 332 unsigned char ch; 245 333 int r; 246 334 int fd; 335 int i; 247 336 248 337 errno = error_io; 249 338 fd = d->s1 - 1; 250 339 340 if (d->tcpstate == 0 && d->master) return 0; 341 if (d->tcpstate == 0 && d->packet) return 1; 342 251 343 if (!x->revents) { 252 344 if (taia_less(when,&d->deadline)) return 0; 253 345 errno = error_timeout; … … have sent query to curserver on UDP socket s 279 371 d->packet = alloc(d->packetlen); 280 372 if (!d->packet) { dns_transmit_free(d); return -1; } 281 373 byte_copy(d->packet,d->packetlen,udpbuf); 374 375 for (i = 0; i < d->nslaves; i++) { 376 if (!d->slaves[i]) continue; 377 d->slaves[i]->packetlen = d->packetlen; 378 d->slaves[i]->packet = alloc(d->packetlen); 379 if (!d->slaves[i]->packet) { dns_transmit_free(d->slaves[i]); continue; } 380 byte_copy(d->slaves[i]->packet,d->packetlen,udpbuf); 381 } 382 282 383 queryfree(d); 283 384 return 1; 284 385 } -
dnscache.c
diff --git a/dnscache.c b/dnscache.c index 8c899a3..c8db179 100644
a b uint64 numqueries = 0; 54 54 55 55 static int udp53; 56 56 57 #define MAXUDP 20058 57 static struct udpclient { 59 58 struct query q; 60 59 struct taia start; … … void u_new(void) 131 130 132 131 static int tcp53; 133 132 134 #define MAXTCP 20135 133 struct tcpclient { 136 134 struct query q; 137 135 struct taia start; … … int main() 435 433 response_hidettl(); 436 434 if (env_get("FORWARDONLY")) 437 435 query_forwardonly(); 436 if (env_get("MERGEQUERIES")) 437 dns_enable_merge(log_merge); 438 438 439 439 if (!roots_init()) 440 440 strerr_die2sys(111,FATAL,"unable to read servers: "); -
log.c
diff --git a/log.c b/log.c index c43e8b0..3e1c674 100644
a b void log_tx(const char *q,const char qtype[2],const char *control,const char ser 150 150 line(); 151 151 } 152 152 153 void log_merge(const char *addr, const char qtype[2], const char *q) 154 { 155 string("merge "); ip(addr); space(); logtype(qtype); space(); name(q); 156 line(); 157 } 158 153 159 void log_cachedanswer(const char *q,const char type[2]) 154 160 { 155 161 string("cached "); logtype(type); space(); -
log.h
diff --git a/log.h b/log.h index fe62fa3..dd6408a 100644
a b extern void log_cachednxdomain(const char *); 18 18 extern void log_cachedns(const char *,const char *); 19 19 20 20 extern void log_tx(const char *,const char *,const char *,const char *,unsigned int); 21 extern void log_merge(const char *, const char *, const char *); 21 22 22 23 extern void log_nxdomain(const char *,const char *,unsigned int); 23 24 extern void log_nodata(const char *,const char *,const char *,unsigned int);
Note: See TracBrowser
for help on using the repository browser.