0001-Impove-cache-behaviour-for-TCP-connections.patch 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. From a799ca0c6314ad73a97bc6c89382d2712a9c0b0e Mon Sep 17 00:00:00 2001
  2. From: Simon Kelley <simon@thekelleys.org.uk>
  3. Date: Thu, 18 Oct 2018 19:35:29 +0100
  4. Subject: [PATCH 01/32] Impove cache behaviour for TCP connections.
  5. For ease of implementaion, dnsmasq has always forked a new process to
  6. handle each incoming TCP connection. A side-effect of this is that any
  7. DNS queries answered from TCP connections are not cached: when TCP
  8. connections were rare, this was not a problem. With the coming of
  9. DNSSEC, it's now the case that some DNSSEC queries have answers which
  10. spill to TCP, and if, for instance, this applies to the keys for the
  11. root then those never get cached, and performance is very bad. This
  12. fix passes cache entries back from the TCP child process to the main
  13. server process, and fixes the problem.
  14. Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
  15. ---
  16. CHANGELOG | 14 ++++
  17. src/blockdata.c | 37 ++++++++-
  18. src/cache.c | 196 ++++++++++++++++++++++++++++++++++++++++++++++--
  19. src/dnsmasq.c | 58 ++++++++++++--
  20. src/dnsmasq.h | 5 ++
  21. 5 files changed, 291 insertions(+), 19 deletions(-)
  22. --- a/CHANGELOG
  23. +++ b/CHANGELOG
  24. @@ -1,3 +1,17 @@
  25. +version 2.81
  26. + Impove cache behaviour for TCP connections. For ease of
  27. + implementaion, dnsmasq has always forked a new process to handle
  28. + each incoming TCP connection. A side-effect of this is that
  29. + any DNS queries answered from TCP connections are not cached:
  30. + when TCP connections were rare, this was not a problem.
  31. + With the coming of DNSSEC, it's now the case that some
  32. + DNSSEC queries have answers which spill to TCP, and if,
  33. + for instance, this applies to the keys for the root then
  34. + those never get cached, and performance is very bad.
  35. + This fix passes cache entries back from the TCP child process to
  36. + the main server process, and fixes the problem.
  37. +
  38. +
  39. version 2.80
  40. Add support for RFC 4039 DHCP rapid commit. Thanks to Ashram Method
  41. for the initial patch and motivation.
  42. --- a/src/blockdata.c
  43. +++ b/src/blockdata.c
  44. @@ -61,7 +61,7 @@ void blockdata_report(void)
  45. blockdata_alloced * sizeof(struct blockdata));
  46. }
  47. -struct blockdata *blockdata_alloc(char *data, size_t len)
  48. +static struct blockdata *blockdata_alloc_real(int fd, char *data, size_t len)
  49. {
  50. struct blockdata *block, *ret = NULL;
  51. struct blockdata **prev = &ret;
  52. @@ -89,8 +89,17 @@ struct blockdata *blockdata_alloc(char *
  53. blockdata_hwm = blockdata_count;
  54. blen = len > KEYBLOCK_LEN ? KEYBLOCK_LEN : len;
  55. - memcpy(block->key, data, blen);
  56. - data += blen;
  57. + if (data)
  58. + {
  59. + memcpy(block->key, data, blen);
  60. + data += blen;
  61. + }
  62. + else if (!read_write(fd, block->key, blen, 1))
  63. + {
  64. + /* failed read free partial chain */
  65. + blockdata_free(ret);
  66. + return NULL;
  67. + }
  68. len -= blen;
  69. *prev = block;
  70. prev = &block->next;
  71. @@ -100,6 +109,10 @@ struct blockdata *blockdata_alloc(char *
  72. return ret;
  73. }
  74. +struct blockdata *blockdata_alloc(char *data, size_t len)
  75. +{
  76. + return blockdata_alloc_real(0, data, len);
  77. +}
  78. void blockdata_free(struct blockdata *blocks)
  79. {
  80. @@ -148,5 +161,21 @@ void *blockdata_retrieve(struct blockdat
  81. return data;
  82. }
  83. -
  84. +
  85. +
  86. +void blockdata_write(struct blockdata *block, size_t len, int fd)
  87. +{
  88. + for (; len > 0 && block; block = block->next)
  89. + {
  90. + size_t blen = len > KEYBLOCK_LEN ? KEYBLOCK_LEN : len;
  91. + read_write(fd, block->key, blen, 0);
  92. + len -= blen;
  93. + }
  94. +}
  95. +
  96. +struct blockdata *blockdata_read(int fd, size_t len)
  97. +{
  98. + return blockdata_alloc_real(fd, NULL, len);
  99. +}
  100. +
  101. #endif
  102. --- a/src/cache.c
  103. +++ b/src/cache.c
  104. @@ -26,6 +26,8 @@ static union bigname *big_free = NULL;
  105. static int bignames_left, hash_size;
  106. static void make_non_terminals(struct crec *source);
  107. +static struct crec *really_insert(char *name, struct all_addr *addr,
  108. + time_t now, unsigned long ttl, unsigned short flags);
  109. /* type->string mapping: this is also used by the name-hash function as a mixing table. */
  110. static const struct {
  111. @@ -464,16 +466,10 @@ void cache_start_insert(void)
  112. new_chain = NULL;
  113. insert_error = 0;
  114. }
  115. -
  116. +
  117. struct crec *cache_insert(char *name, struct all_addr *addr,
  118. time_t now, unsigned long ttl, unsigned short flags)
  119. {
  120. - struct crec *new, *target_crec = NULL;
  121. - union bigname *big_name = NULL;
  122. - int freed_all = flags & F_REVERSE;
  123. - int free_avail = 0;
  124. - unsigned int target_uid;
  125. -
  126. /* Don't log DNSSEC records here, done elsewhere */
  127. if (flags & (F_IPV4 | F_IPV6 | F_CNAME))
  128. {
  129. @@ -484,7 +480,20 @@ struct crec *cache_insert(char *name, st
  130. if (daemon->min_cache_ttl != 0 && daemon->min_cache_ttl > ttl)
  131. ttl = daemon->min_cache_ttl;
  132. }
  133. +
  134. + return really_insert(name, addr, now, ttl, flags);
  135. +}
  136. +
  137. +static struct crec *really_insert(char *name, struct all_addr *addr,
  138. + time_t now, unsigned long ttl, unsigned short flags)
  139. +{
  140. + struct crec *new, *target_crec = NULL;
  141. + union bigname *big_name = NULL;
  142. + int freed_all = flags & F_REVERSE;
  143. + int free_avail = 0;
  144. + unsigned int target_uid;
  145. +
  146. /* if previous insertion failed give up now. */
  147. if (insert_error)
  148. return NULL;
  149. @@ -645,12 +654,185 @@ void cache_end_insert(void)
  150. cache_hash(new_chain);
  151. cache_link(new_chain);
  152. daemon->metrics[METRIC_DNS_CACHE_INSERTED]++;
  153. +
  154. + /* If we're a child process, send this cache entry up the pipe to the master.
  155. + The marshalling process is rather nasty. */
  156. + if (daemon->pipe_to_parent != -1)
  157. + {
  158. + char *name = cache_get_name(new_chain);
  159. + ssize_t m = strlen(name);
  160. + unsigned short flags = new_chain->flags;
  161. +#ifdef HAVE_DNSSEC
  162. + u16 class = new_chain->uid;
  163. +#endif
  164. +
  165. + read_write(daemon->pipe_to_parent, (unsigned char *)&m, sizeof(m), 0);
  166. + read_write(daemon->pipe_to_parent, (unsigned char *)name, m, 0);
  167. + read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->ttd, sizeof(new_chain->ttd), 0);
  168. + read_write(daemon->pipe_to_parent, (unsigned char *)&flags, sizeof(flags), 0);
  169. +
  170. + if (flags & (F_IPV4 | F_IPV6))
  171. + read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr, sizeof(new_chain->addr), 0);
  172. +#ifdef HAVE_DNSSEC
  173. + else if (flags & F_DNSKEY)
  174. + {
  175. + read_write(daemon->pipe_to_parent, (unsigned char *)&class, sizeof(class), 0);
  176. + read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.key.algo, sizeof(new_chain->addr.key.algo), 0);
  177. + read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.key.keytag, sizeof(new_chain->addr.key.keytag), 0);
  178. + read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.key.flags, sizeof(new_chain->addr.key.flags), 0);
  179. + read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.key.keylen, sizeof(new_chain->addr.key.keylen), 0);
  180. + blockdata_write(new_chain->addr.key.keydata, new_chain->addr.key.keylen, daemon->pipe_to_parent);
  181. + }
  182. + else if (flags & F_DS)
  183. + {
  184. + read_write(daemon->pipe_to_parent, (unsigned char *)&class, sizeof(class), 0);
  185. + /* A negative DS entry is possible and has no data, obviously. */
  186. + if (!(flags & F_NEG))
  187. + {
  188. + read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.ds.algo, sizeof(new_chain->addr.ds.algo), 0);
  189. + read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.ds.keytag, sizeof(new_chain->addr.ds.keytag), 0);
  190. + read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.ds.digest, sizeof(new_chain->addr.ds.digest), 0);
  191. + read_write(daemon->pipe_to_parent, (unsigned char *)&new_chain->addr.ds.keylen, sizeof(new_chain->addr.ds.keylen), 0);
  192. + blockdata_write(new_chain->addr.ds.keydata, new_chain->addr.ds.keylen, daemon->pipe_to_parent);
  193. + }
  194. + }
  195. +#endif
  196. +
  197. + }
  198. }
  199. +
  200. new_chain = tmp;
  201. }
  202. +
  203. + /* signal end of cache insert in master process */
  204. + if (daemon->pipe_to_parent != -1)
  205. + {
  206. + ssize_t m = -1;
  207. + read_write(daemon->pipe_to_parent, (unsigned char *)&m, sizeof(m), 0);
  208. + }
  209. +
  210. new_chain = NULL;
  211. }
  212. +
  213. +/* A marshalled cache entry arrives on fd, read, unmarshall and insert into cache of master process. */
  214. +int cache_recv_insert(time_t now, int fd)
  215. +{
  216. + ssize_t m;
  217. + struct all_addr addr;
  218. + unsigned long ttl;
  219. + time_t ttd;
  220. + unsigned short flags;
  221. + struct crec *crecp = NULL;
  222. +
  223. + cache_start_insert();
  224. +
  225. + while(1)
  226. + {
  227. +
  228. + if (!read_write(fd, (unsigned char *)&m, sizeof(m), 1))
  229. + return 0;
  230. +
  231. + if (m == -1)
  232. + {
  233. + cache_end_insert();
  234. + return 1;
  235. + }
  236. +
  237. + if (!read_write(fd, (unsigned char *)daemon->namebuff, m, 1) ||
  238. + !read_write(fd, (unsigned char *)&ttd, sizeof(ttd), 1) ||
  239. + !read_write(fd, (unsigned char *)&flags, sizeof(flags), 1))
  240. + return 0;
  241. +
  242. + daemon->namebuff[m] = 0;
  243. +
  244. + ttl = difftime(ttd, now);
  245. +
  246. + if (flags & (F_IPV4 | F_IPV6))
  247. + {
  248. + if (!read_write(fd, (unsigned char *)&addr, sizeof(addr), 1))
  249. + return 0;
  250. + crecp = really_insert(daemon->namebuff, &addr, now, ttl, flags);
  251. + }
  252. + else if (flags & F_CNAME)
  253. + {
  254. + struct crec *newc = really_insert(daemon->namebuff, NULL, now, ttl, flags);
  255. + /* This relies on the fact the the target of a CNAME immediately preceeds
  256. + it because of the order of extraction in extract_addresses, and
  257. + the order reversal on the new_chain. */
  258. + if (newc)
  259. + {
  260. + if (!crecp)
  261. + {
  262. + newc->addr.cname.target.cache = NULL;
  263. + /* anything other than zero, to avoid being mistaken for CNAME to interface-name */
  264. + newc->addr.cname.uid = 1;
  265. + }
  266. + else
  267. + {
  268. + next_uid(crecp);
  269. + newc->addr.cname.target.cache = crecp;
  270. + newc->addr.cname.uid = crecp->uid;
  271. + }
  272. + }
  273. + }
  274. +#ifdef HAVE_DNSSEC
  275. + else if (flags & (F_DNSKEY | F_DS))
  276. + {
  277. + unsigned short class, keylen, keyflags, keytag;
  278. + unsigned char algo, digest;
  279. + struct blockdata *keydata;
  280. +
  281. + if (!read_write(fd, (unsigned char *)&class, sizeof(class), 1))
  282. + return 0;
  283. + /* Cache needs to known class for DNSSEC stuff */
  284. + addr.addr.dnssec.class = class;
  285. +
  286. + crecp = really_insert(daemon->namebuff, &addr, now, ttl, flags);
  287. +
  288. + if (flags & F_DNSKEY)
  289. + {
  290. + if (!read_write(fd, (unsigned char *)&algo, sizeof(algo), 1) ||
  291. + !read_write(fd, (unsigned char *)&keytag, sizeof(keytag), 1) ||
  292. + !read_write(fd, (unsigned char *)&keyflags, sizeof(keyflags), 1) ||
  293. + !read_write(fd, (unsigned char *)&keylen, sizeof(keylen), 1) ||
  294. + !(keydata = blockdata_read(fd, keylen)))
  295. + return 0;
  296. + }
  297. + else if (!(flags & F_NEG))
  298. + {
  299. + if (!read_write(fd, (unsigned char *)&algo, sizeof(algo), 1) ||
  300. + !read_write(fd, (unsigned char *)&keytag, sizeof(keytag), 1) ||
  301. + !read_write(fd, (unsigned char *)&digest, sizeof(digest), 1) ||
  302. + !read_write(fd, (unsigned char *)&keylen, sizeof(keylen), 1) ||
  303. + !(keydata = blockdata_read(fd, keylen)))
  304. + return 0;
  305. + }
  306. +
  307. + if (crecp)
  308. + {
  309. + if (flags & F_DNSKEY)
  310. + {
  311. + crecp->addr.key.algo = algo;
  312. + crecp->addr.key.keytag = keytag;
  313. + crecp->addr.key.flags = flags;
  314. + crecp->addr.key.keylen = keylen;
  315. + crecp->addr.key.keydata = keydata;
  316. + }
  317. + else if (!(flags & F_NEG))
  318. + {
  319. + crecp->addr.ds.algo = algo;
  320. + crecp->addr.ds.keytag = keytag;
  321. + crecp->addr.ds.digest = digest;
  322. + crecp->addr.ds.keylen = keylen;
  323. + crecp->addr.ds.keydata = keydata;
  324. + }
  325. + }
  326. + }
  327. +#endif
  328. + }
  329. +}
  330. +
  331. int cache_find_non_terminal(char *name, time_t now)
  332. {
  333. struct crec *crecp;
  334. --- a/src/dnsmasq.c
  335. +++ b/src/dnsmasq.c
  336. @@ -930,6 +930,10 @@ int main (int argc, char **argv)
  337. check_servers();
  338. pid = getpid();
  339. +
  340. + daemon->pipe_to_parent = -1;
  341. + for (i = 0; i < MAX_PROCS; i++)
  342. + daemon->tcp_pipes[i] = -1;
  343. #ifdef HAVE_INOTIFY
  344. /* Using inotify, have to select a resolv file at startup */
  345. @@ -1611,7 +1615,7 @@ static int set_dns_listeners(time_t now)
  346. we don't need to explicitly arrange to wake up here */
  347. if (listener->tcpfd != -1)
  348. for (i = 0; i < MAX_PROCS; i++)
  349. - if (daemon->tcp_pids[i] == 0)
  350. + if (daemon->tcp_pids[i] == 0 && daemon->tcp_pipes[i] == -1)
  351. {
  352. poll_listen(listener->tcpfd, POLLIN);
  353. break;
  354. @@ -1624,6 +1628,13 @@ static int set_dns_listeners(time_t now)
  355. }
  356. +#ifndef NO_FORK
  357. + if (!option_bool(OPT_DEBUG))
  358. + for (i = 0; i < MAX_PROCS; i++)
  359. + if (daemon->tcp_pipes[i] != -1)
  360. + poll_listen(daemon->tcp_pipes[i], POLLIN);
  361. +#endif
  362. +
  363. return wait;
  364. }
  365. @@ -1632,7 +1643,10 @@ static void check_dns_listeners(time_t n
  366. struct serverfd *serverfdp;
  367. struct listener *listener;
  368. int i;
  369. -
  370. +#ifndef NO_FORK
  371. + int pipefd[2];
  372. +#endif
  373. +
  374. for (serverfdp = daemon->sfds; serverfdp; serverfdp = serverfdp->next)
  375. if (poll_check(serverfdp->fd, POLLIN))
  376. reply_query(serverfdp->fd, serverfdp->source_addr.sa.sa_family, now);
  377. @@ -1642,7 +1656,26 @@ static void check_dns_listeners(time_t n
  378. if (daemon->randomsocks[i].refcount != 0 &&
  379. poll_check(daemon->randomsocks[i].fd, POLLIN))
  380. reply_query(daemon->randomsocks[i].fd, daemon->randomsocks[i].family, now);
  381. -
  382. +
  383. +#ifndef NO_FORK
  384. + /* Races. The child process can die before we read all of the data from the
  385. + pipe, or vice versa. Therefore send tcp_pids to zero when we wait() the
  386. + process, and tcp_pipes to -1 and close the FD when we read the last
  387. + of the data - indicated by cache_recv_insert returning zero.
  388. + The order of these events is indeterminate, and both are needed
  389. + to free the process slot. Once the child process has gone, poll()
  390. + returns POLLHUP, not POLLIN, so have to check for both here. */
  391. + if (!option_bool(OPT_DEBUG))
  392. + for (i = 0; i < MAX_PROCS; i++)
  393. + if (daemon->tcp_pipes[i] != -1 &&
  394. + poll_check(daemon->tcp_pipes[i], POLLIN | POLLHUP) &&
  395. + !cache_recv_insert(now, daemon->tcp_pipes[i]))
  396. + {
  397. + close(daemon->tcp_pipes[i]);
  398. + daemon->tcp_pipes[i] = -1;
  399. + }
  400. +#endif
  401. +
  402. for (listener = daemon->listeners; listener; listener = listener->next)
  403. {
  404. if (listener->fd != -1 && poll_check(listener->fd, POLLIN))
  405. @@ -1736,15 +1769,20 @@ static void check_dns_listeners(time_t n
  406. while (retry_send(close(confd)));
  407. }
  408. #ifndef NO_FORK
  409. - else if (!option_bool(OPT_DEBUG) && (p = fork()) != 0)
  410. + else if (!option_bool(OPT_DEBUG) && pipe(pipefd) == 0 && (p = fork()) != 0)
  411. {
  412. - if (p != -1)
  413. + close(pipefd[1]); /* parent needs read pipe end. */
  414. + if (p == -1)
  415. + close(pipefd[0]);
  416. + else
  417. {
  418. int i;
  419. +
  420. for (i = 0; i < MAX_PROCS; i++)
  421. - if (daemon->tcp_pids[i] == 0)
  422. + if (daemon->tcp_pids[i] == 0 && daemon->tcp_pipes[i] == -1)
  423. {
  424. daemon->tcp_pids[i] = p;
  425. + daemon->tcp_pipes[i] = pipefd[0];
  426. break;
  427. }
  428. }
  429. @@ -1761,7 +1799,7 @@ static void check_dns_listeners(time_t n
  430. int flags;
  431. struct in_addr netmask;
  432. int auth_dns;
  433. -
  434. +
  435. if (iface)
  436. {
  437. netmask = iface->netmask;
  438. @@ -1777,7 +1815,11 @@ static void check_dns_listeners(time_t n
  439. /* Arrange for SIGALRM after CHILD_LIFETIME seconds to
  440. terminate the process. */
  441. if (!option_bool(OPT_DEBUG))
  442. - alarm(CHILD_LIFETIME);
  443. + {
  444. + alarm(CHILD_LIFETIME);
  445. + close(pipefd[0]); /* close read end in child. */
  446. + daemon->pipe_to_parent = pipefd[1];
  447. + }
  448. #endif
  449. /* start with no upstream connections. */
  450. --- a/src/dnsmasq.h
  451. +++ b/src/dnsmasq.h
  452. @@ -1091,6 +1091,8 @@ extern struct daemon {
  453. size_t packet_len; /* " " */
  454. struct randfd *rfd_save; /* " " */
  455. pid_t tcp_pids[MAX_PROCS];
  456. + int tcp_pipes[MAX_PROCS];
  457. + int pipe_to_parent;
  458. struct randfd randomsocks[RANDOM_SOCKS];
  459. int v6pktinfo;
  460. struct addrlist *interface_addrs; /* list of all addresses/prefix lengths associated with all local interfaces */
  461. @@ -1152,6 +1154,7 @@ struct crec *cache_find_by_name(struct c
  462. char *name, time_t now, unsigned int prot);
  463. void cache_end_insert(void);
  464. void cache_start_insert(void);
  465. +int cache_recv_insert(time_t now, int fd);
  466. struct crec *cache_insert(char *name, struct all_addr *addr,
  467. time_t now, unsigned long ttl, unsigned short flags);
  468. void cache_reload(void);
  469. @@ -1174,6 +1177,8 @@ void blockdata_init(void);
  470. void blockdata_report(void);
  471. struct blockdata *blockdata_alloc(char *data, size_t len);
  472. void *blockdata_retrieve(struct blockdata *block, size_t len, void *data);
  473. +struct blockdata *blockdata_read(int fd, size_t len);
  474. +void blockdata_write(struct blockdata *block, size_t len, int fd);
  475. void blockdata_free(struct blockdata *blocks);
  476. #endif