2 * services/mesh.c - deal with mesh of query states and handle events for that.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This file contains functions to assist in dealing with a mesh of
40 * query states. This mesh is supposed to be thread-specific.
41 * It consists of query states (per qname, qtype, qclass) and connections
42 * between query states and the super and subquery states, and replies to
43 * send back to clients.
46 #include "services/mesh.h"
47 #include "services/outbound_list.h"
48 #include "services/cache/dns.h"
50 #include "util/net_help.h"
51 #include "util/module.h"
52 #include "util/regional.h"
53 #include "util/data/msgencode.h"
54 #include "util/timehist.h"
55 #include "util/fptr_wlist.h"
56 #include "util/alloc.h"
57 #include "util/config_file.h"
58 #include "sldns/sbuffer.h"
59 #include "sldns/wire2str.h"
60 #include "services/localzone.h"
61 #include "util/data/dname.h"
62 #include "respip/respip.h"
64 /** subtract timers and the values do not overflow or become negative */
66 timeval_subtract(struct timeval* d, const struct timeval* end, const struct timeval* start)
69 time_t end_usec = end->tv_usec;
70 d->tv_sec = end->tv_sec - start->tv_sec;
71 if(end_usec < start->tv_usec) {
75 d->tv_usec = end_usec - start->tv_usec;
79 /** add timers and the values do not overflow or become negative */
81 timeval_add(struct timeval* d, const struct timeval* add)
84 d->tv_sec += add->tv_sec;
85 d->tv_usec += add->tv_usec;
86 if(d->tv_usec > 1000000 ) {
87 d->tv_usec -= 1000000;
93 /** divide sum of timers to get average */
95 timeval_divide(struct timeval* avg, const struct timeval* sum, size_t d)
104 avg->tv_sec = sum->tv_sec / d;
105 avg->tv_usec = sum->tv_usec / d;
106 /* handle fraction from seconds divide */
107 leftover = sum->tv_sec - avg->tv_sec*d;
108 avg->tv_usec += (leftover*1000000)/d;
112 /** histogram compare of time values */
114 timeval_smaller(const struct timeval* x, const struct timeval* y)
117 if(x->tv_sec < y->tv_sec)
119 else if(x->tv_sec == y->tv_sec) {
120 if(x->tv_usec <= y->tv_usec)
129 * Compare two response-ip client info entries for the purpose of mesh state
130 * compare. It returns 0 if ci_a and ci_b are considered equal; otherwise
131 * 1 or -1 (they mean 'ci_a is larger/smaller than ci_b', respectively, but
132 * in practice it should be only used to mean they are different).
133 * We cannot share the mesh state for two queries if different response-ip
134 * actions can apply in the end, even if those queries are otherwise identical.
135 * For this purpose we compare tag lists and tag action lists; they should be
136 * identical to share the same state.
137 * For tag data, we don't look into the data content, as it can be
138 * expensive; unless tag data are not defined for both or they point to the
139 * exact same data in memory (i.e., they come from the same ACL entry), we
140 * consider these data different.
141 * Likewise, if the client info is associated with views, we don't look into
142 * the views. They are considered different unless they are exactly the same
143 * even if the views only differ in the names.
146 client_info_compare(const struct respip_client_info* ci_a,
147 const struct respip_client_info* ci_b)
157 if(ci_a->taglen != ci_b->taglen)
158 return (ci_a->taglen < ci_b->taglen) ? -1 : 1;
159 cmp = memcmp(ci_a->taglist, ci_b->taglist, ci_a->taglen);
162 if(ci_a->tag_actions_size != ci_b->tag_actions_size)
163 return (ci_a->tag_actions_size < ci_b->tag_actions_size) ?
165 cmp = memcmp(ci_a->tag_actions, ci_b->tag_actions,
166 ci_a->tag_actions_size);
169 if(ci_a->tag_datas != ci_b->tag_datas)
170 return ci_a->tag_datas < ci_b->tag_datas ? -1 : 1;
171 if(ci_a->view != ci_b->view)
172 return ci_a->view < ci_b->view ? -1 : 1;
173 /* For the unbound daemon these should be non-NULL and identical,
174 * but we check that just in case. */
175 if(ci_a->respip_set != ci_b->respip_set)
176 return ci_a->respip_set < ci_b->respip_set ? -1 : 1;
181 mesh_state_compare(const void* ap, const void* bp)
183 struct mesh_state* a = (struct mesh_state*)ap;
184 struct mesh_state* b = (struct mesh_state*)bp;
187 if(a->unique < b->unique)
189 if(a->unique > b->unique)
192 if(a->s.is_priming && !b->s.is_priming)
194 if(!a->s.is_priming && b->s.is_priming)
197 if(a->s.is_valrec && !b->s.is_valrec)
199 if(!a->s.is_valrec && b->s.is_valrec)
202 if((a->s.query_flags&BIT_RD) && !(b->s.query_flags&BIT_RD))
204 if(!(a->s.query_flags&BIT_RD) && (b->s.query_flags&BIT_RD))
207 if((a->s.query_flags&BIT_CD) && !(b->s.query_flags&BIT_CD))
209 if(!(a->s.query_flags&BIT_CD) && (b->s.query_flags&BIT_CD))
212 cmp = query_info_compare(&a->s.qinfo, &b->s.qinfo);
215 return client_info_compare(a->s.client_info, b->s.client_info);
219 mesh_state_ref_compare(const void* ap, const void* bp)
221 struct mesh_state_ref* a = (struct mesh_state_ref*)ap;
222 struct mesh_state_ref* b = (struct mesh_state_ref*)bp;
223 return mesh_state_compare(a->s, b->s);
227 mesh_create(struct module_stack* stack, struct module_env* env)
229 struct mesh_area* mesh = calloc(1, sizeof(struct mesh_area));
231 log_err("mesh area alloc: out of memory");
234 mesh->histogram = timehist_setup();
235 mesh->qbuf_bak = sldns_buffer_new(env->cfg->msg_buffer_size);
236 if(!mesh->histogram || !mesh->qbuf_bak) {
238 log_err("mesh area alloc: out of memory");
243 rbtree_init(&mesh->run, &mesh_state_compare);
244 rbtree_init(&mesh->all, &mesh_state_compare);
245 mesh->num_reply_addrs = 0;
246 mesh->num_reply_states = 0;
247 mesh->num_detached_states = 0;
248 mesh->num_forever_states = 0;
249 mesh->stats_jostled = 0;
250 mesh->stats_dropped = 0;
251 mesh->max_reply_states = env->cfg->num_queries_per_thread;
252 mesh->max_forever_states = (mesh->max_reply_states+1)/2;
254 mesh->jostle_max.tv_sec = (time_t)(env->cfg->jostle_time / 1000);
255 mesh->jostle_max.tv_usec = (time_t)((env->cfg->jostle_time % 1000)
261 /** help mesh delete delete mesh states */
263 mesh_delete_helper(rbnode_type* n)
265 struct mesh_state* mstate = (struct mesh_state*)n->key;
266 /* perform a full delete, not only 'cleanup' routine,
267 * because other callbacks expect a clean state in the mesh.
268 * For 're-entrant' calls */
269 mesh_state_delete(&mstate->s);
270 /* but because these delete the items from the tree, postorder
271 * traversal and rbtree rebalancing do not work together */
275 mesh_delete(struct mesh_area* mesh)
279 /* free all query states */
280 while(mesh->all.count)
281 mesh_delete_helper(mesh->all.root);
282 timehist_delete(mesh->histogram);
283 sldns_buffer_free(mesh->qbuf_bak);
288 mesh_delete_all(struct mesh_area* mesh)
290 /* free all query states */
291 while(mesh->all.count)
292 mesh_delete_helper(mesh->all.root);
293 mesh->stats_dropped += mesh->num_reply_addrs;
294 /* clear mesh area references */
295 rbtree_init(&mesh->run, &mesh_state_compare);
296 rbtree_init(&mesh->all, &mesh_state_compare);
297 mesh->num_reply_addrs = 0;
298 mesh->num_reply_states = 0;
299 mesh->num_detached_states = 0;
300 mesh->num_forever_states = 0;
301 mesh->forever_first = NULL;
302 mesh->forever_last = NULL;
303 mesh->jostle_first = NULL;
304 mesh->jostle_last = NULL;
307 int mesh_make_new_space(struct mesh_area* mesh, sldns_buffer* qbuf)
309 struct mesh_state* m = mesh->jostle_first;
310 /* free space is available */
311 if(mesh->num_reply_states < mesh->max_reply_states)
313 /* try to kick out a jostle-list item */
314 if(m && m->reply_list && m->list_select == mesh_jostle_list) {
317 timeval_subtract(&age, mesh->env->now_tv,
318 &m->reply_list->start_time);
319 if(timeval_smaller(&mesh->jostle_max, &age)) {
321 log_nametypeclass(VERB_ALGO, "query jostled out to "
322 "make space for a new one",
323 m->s.qinfo.qname, m->s.qinfo.qtype,
325 /* backup the query */
326 if(qbuf) sldns_buffer_copy(mesh->qbuf_bak, qbuf);
328 if(m->super_set.count > 0) {
329 verbose(VERB_ALGO, "notify supers of failure");
330 m->s.return_msg = NULL;
331 m->s.return_rcode = LDNS_RCODE_SERVFAIL;
332 mesh_walk_supers(mesh, m);
334 mesh->stats_jostled ++;
335 mesh_state_delete(&m->s);
336 /* restore the query - note that the qinfo ptr to
337 * the querybuffer is then correct again. */
338 if(qbuf) sldns_buffer_copy(qbuf, mesh->qbuf_bak);
342 /* no space for new item */
346 void mesh_new_client(struct mesh_area* mesh, struct query_info* qinfo,
347 struct respip_client_info* cinfo, uint16_t qflags,
348 struct edns_data* edns, struct comm_reply* rep, uint16_t qid)
350 struct mesh_state* s = NULL;
351 int unique = unique_mesh_state(edns->opt_list, mesh->env);
352 int was_detached = 0;
356 s = mesh_area_find(mesh, cinfo, qinfo, qflags&(BIT_RD|BIT_CD), 0, 0);
357 /* does this create a new reply state? */
358 if(!s || s->list_select == mesh_no_list) {
359 if(!mesh_make_new_space(mesh, rep->c->buffer)) {
360 verbose(VERB_ALGO, "Too many queries. dropping "
362 comm_point_drop_reply(rep);
363 mesh->stats_dropped ++;
366 /* for this new reply state, the reply address is free,
367 * so the limit of reply addresses does not stop reply states*/
369 /* protect our memory usage from storing reply addresses */
370 if(mesh->num_reply_addrs > mesh->max_reply_states*16) {
371 verbose(VERB_ALGO, "Too many requests queued. "
372 "dropping incoming query.");
373 mesh->stats_dropped++;
374 comm_point_drop_reply(rep);
378 /* see if it already exists, if not, create one */
381 struct rbnode_type* n;
383 s = mesh_state_create(mesh->env, qinfo, cinfo,
384 qflags&(BIT_RD|BIT_CD), 0, 0);
386 log_err("mesh_state_create: out of memory; SERVFAIL");
387 if(!inplace_cb_reply_servfail_call(mesh->env, qinfo, NULL, NULL,
388 LDNS_RCODE_SERVFAIL, edns, mesh->env->scratch))
389 edns->opt_list = NULL;
390 error_encode(rep->c->buffer, LDNS_RCODE_SERVFAIL,
391 qinfo, qid, qflags, edns);
392 comm_point_send_reply(rep);
396 mesh_state_make_unique(s);
397 /* copy the edns options we got from the front */
399 s->s.edns_opts_front_in = edns_opt_copy_region(edns->opt_list,
401 if(!s->s.edns_opts_front_in) {
402 log_err("mesh_state_create: out of memory; SERVFAIL");
403 if(!inplace_cb_reply_servfail_call(mesh->env, qinfo, NULL,
404 NULL, LDNS_RCODE_SERVFAIL, edns, mesh->env->scratch))
405 edns->opt_list = NULL;
406 error_encode(rep->c->buffer, LDNS_RCODE_SERVFAIL,
407 qinfo, qid, qflags, edns);
408 comm_point_send_reply(rep);
418 rbtree_insert(&mesh->all, &s->node);
419 log_assert(n != NULL);
420 /* set detached (it is now) */
421 mesh->num_detached_states++;
424 if(!s->reply_list && !s->cb_list && s->super_set.count == 0)
426 if(!s->reply_list && !s->cb_list)
429 if(!mesh_state_add_reply(s, edns, rep, qid, qflags, qinfo)) {
430 log_err("mesh_new_client: out of memory; SERVFAIL");
431 if(!inplace_cb_reply_servfail_call(mesh->env, qinfo, &s->s,
432 NULL, LDNS_RCODE_SERVFAIL, edns, mesh->env->scratch))
433 edns->opt_list = NULL;
434 error_encode(rep->c->buffer, LDNS_RCODE_SERVFAIL,
435 qinfo, qid, qflags, edns);
436 comm_point_send_reply(rep);
438 mesh_state_delete(&s->s);
441 /* update statistics */
443 log_assert(mesh->num_detached_states > 0);
444 mesh->num_detached_states--;
447 mesh->num_reply_states ++;
449 mesh->num_reply_addrs++;
450 if(s->list_select == mesh_no_list) {
451 /* move to either the forever or the jostle_list */
452 if(mesh->num_forever_states < mesh->max_forever_states) {
453 mesh->num_forever_states ++;
454 mesh_list_insert(s, &mesh->forever_first,
455 &mesh->forever_last);
456 s->list_select = mesh_forever_list;
458 mesh_list_insert(s, &mesh->jostle_first,
460 s->list_select = mesh_jostle_list;
464 mesh_run(mesh, s, module_event_new, NULL);
468 mesh_new_callback(struct mesh_area* mesh, struct query_info* qinfo,
469 uint16_t qflags, struct edns_data* edns, sldns_buffer* buf,
470 uint16_t qid, mesh_cb_func_type cb, void* cb_arg)
472 struct mesh_state* s = NULL;
473 int unique = unique_mesh_state(edns->opt_list, mesh->env);
474 int was_detached = 0;
478 s = mesh_area_find(mesh, NULL, qinfo, qflags&(BIT_RD|BIT_CD), 0, 0);
480 /* there are no limits on the number of callbacks */
482 /* see if it already exists, if not, create one */
485 struct rbnode_type* n;
487 s = mesh_state_create(mesh->env, qinfo, NULL,
488 qflags&(BIT_RD|BIT_CD), 0, 0);
493 mesh_state_make_unique(s);
495 s->s.edns_opts_front_in = edns_opt_copy_region(edns->opt_list,
497 if(!s->s.edns_opts_front_in) {
506 rbtree_insert(&mesh->all, &s->node);
507 log_assert(n != NULL);
508 /* set detached (it is now) */
509 mesh->num_detached_states++;
512 if(!s->reply_list && !s->cb_list && s->super_set.count == 0)
514 if(!s->reply_list && !s->cb_list)
517 if(!mesh_state_add_cb(s, edns, buf, cb, cb_arg, qid, qflags)) {
519 mesh_state_delete(&s->s);
522 /* update statistics */
524 log_assert(mesh->num_detached_states > 0);
525 mesh->num_detached_states--;
528 mesh->num_reply_states ++;
530 mesh->num_reply_addrs++;
532 mesh_run(mesh, s, module_event_new, NULL);
536 static void mesh_schedule_prefetch(struct mesh_area* mesh,
537 struct query_info* qinfo, uint16_t qflags, time_t leeway, int run);
539 void mesh_new_prefetch(struct mesh_area* mesh, struct query_info* qinfo,
540 uint16_t qflags, time_t leeway)
542 mesh_schedule_prefetch(mesh, qinfo, qflags, leeway, 1);
545 /* Internal backend routine of mesh_new_prefetch(). It takes one additional
546 * parameter, 'run', which controls whether to run the prefetch state
547 * immediately. When this function is called internally 'run' could be
548 * 0 (false), in which case the new state is only made runnable so it
549 * will not be run recursively on top of the current state. */
550 static void mesh_schedule_prefetch(struct mesh_area* mesh,
551 struct query_info* qinfo, uint16_t qflags, time_t leeway, int run)
553 struct mesh_state* s = mesh_area_find(mesh, NULL, qinfo,
554 qflags&(BIT_RD|BIT_CD), 0, 0);
556 struct rbnode_type* n;
558 /* already exists, and for a different purpose perhaps.
559 * if mesh_no_list, keep it that way. */
561 /* make it ignore the cache from now on */
563 sock_list_insert(&s->s.blacklist, NULL, 0, s->s.region);
564 if(s->s.prefetch_leeway < leeway)
565 s->s.prefetch_leeway = leeway;
568 if(!mesh_make_new_space(mesh, NULL)) {
569 verbose(VERB_ALGO, "Too many queries. dropped prefetch.");
570 mesh->stats_dropped ++;
574 s = mesh_state_create(mesh->env, qinfo, NULL,
575 qflags&(BIT_RD|BIT_CD), 0, 0);
577 log_err("prefetch mesh_state_create: out of memory");
585 rbtree_insert(&mesh->all, &s->node);
586 log_assert(n != NULL);
587 /* set detached (it is now) */
588 mesh->num_detached_states++;
589 /* make it ignore the cache */
590 sock_list_insert(&s->s.blacklist, NULL, 0, s->s.region);
591 s->s.prefetch_leeway = leeway;
593 if(s->list_select == mesh_no_list) {
594 /* move to either the forever or the jostle_list */
595 if(mesh->num_forever_states < mesh->max_forever_states) {
596 mesh->num_forever_states ++;
597 mesh_list_insert(s, &mesh->forever_first,
598 &mesh->forever_last);
599 s->list_select = mesh_forever_list;
601 mesh_list_insert(s, &mesh->jostle_first,
603 s->list_select = mesh_jostle_list;
613 rbtree_insert(&mesh->run, &s->run_node);
614 log_assert(n != NULL);
618 mesh_run(mesh, s, module_event_new, NULL);
621 void mesh_report_reply(struct mesh_area* mesh, struct outbound_entry* e,
622 struct comm_reply* reply, int what)
624 enum module_ev event = module_event_reply;
625 e->qstate->reply = reply;
626 if(what != NETEVENT_NOERROR) {
627 event = module_event_noreply;
628 if(what == NETEVENT_CAPSFAIL)
629 event = module_event_capsfail;
631 mesh_run(mesh, e->qstate->mesh_info, event, e);
635 mesh_state_create(struct module_env* env, struct query_info* qinfo,
636 struct respip_client_info* cinfo, uint16_t qflags, int prime,
639 struct regional* region = alloc_reg_obtain(env->alloc);
640 struct mesh_state* mstate;
644 mstate = (struct mesh_state*)regional_alloc(region,
645 sizeof(struct mesh_state));
647 alloc_reg_release(env->alloc, region);
650 memset(mstate, 0, sizeof(*mstate));
651 mstate->node = *RBTREE_NULL;
652 mstate->run_node = *RBTREE_NULL;
653 mstate->node.key = mstate;
654 mstate->run_node.key = mstate;
655 mstate->reply_list = NULL;
656 mstate->list_select = mesh_no_list;
657 mstate->replies_sent = 0;
658 rbtree_init(&mstate->super_set, &mesh_state_ref_compare);
659 rbtree_init(&mstate->sub_set, &mesh_state_ref_compare);
660 mstate->num_activated = 0;
661 mstate->unique = NULL;
662 /* init module qstate */
663 mstate->s.qinfo.qtype = qinfo->qtype;
664 mstate->s.qinfo.qclass = qinfo->qclass;
665 mstate->s.qinfo.local_alias = NULL;
666 mstate->s.qinfo.qname_len = qinfo->qname_len;
667 mstate->s.qinfo.qname = regional_alloc_init(region, qinfo->qname,
669 if(!mstate->s.qinfo.qname) {
670 alloc_reg_release(env->alloc, region);
674 mstate->s.client_info = regional_alloc_init(region, cinfo,
676 if(!mstate->s.client_info) {
677 alloc_reg_release(env->alloc, region);
681 /* remove all weird bits from qflags */
682 mstate->s.query_flags = (qflags & (BIT_RD|BIT_CD));
683 mstate->s.is_priming = prime;
684 mstate->s.is_valrec = valrec;
685 mstate->s.reply = NULL;
686 mstate->s.region = region;
687 mstate->s.curmod = 0;
688 mstate->s.return_msg = 0;
689 mstate->s.return_rcode = LDNS_RCODE_NOERROR;
691 mstate->s.mesh_info = mstate;
692 mstate->s.prefetch_leeway = 0;
693 mstate->s.no_cache_lookup = 0;
694 mstate->s.no_cache_store = 0;
695 mstate->s.need_refetch = 0;
698 for(i=0; i<env->mesh->mods.num; i++) {
699 mstate->s.minfo[i] = NULL;
700 mstate->s.ext_state[i] = module_state_initial;
702 /* init edns option lists */
703 mstate->s.edns_opts_front_in = NULL;
704 mstate->s.edns_opts_back_out = NULL;
705 mstate->s.edns_opts_back_in = NULL;
706 mstate->s.edns_opts_front_out = NULL;
712 mesh_state_is_unique(struct mesh_state* mstate)
714 return mstate->unique != NULL;
718 mesh_state_make_unique(struct mesh_state* mstate)
720 mstate->unique = mstate;
724 mesh_state_cleanup(struct mesh_state* mstate)
726 struct mesh_area* mesh;
730 mesh = mstate->s.env->mesh;
731 /* drop unsent replies */
732 if(!mstate->replies_sent) {
733 struct mesh_reply* rep;
735 for(rep=mstate->reply_list; rep; rep=rep->next) {
736 comm_point_drop_reply(&rep->query_reply);
737 mesh->num_reply_addrs--;
739 while((cb = mstate->cb_list)!=NULL) {
740 mstate->cb_list = cb->next;
741 fptr_ok(fptr_whitelist_mesh_cb(cb->cb));
742 (*cb->cb)(cb->cb_arg, LDNS_RCODE_SERVFAIL, NULL,
743 sec_status_unchecked, NULL);
744 mesh->num_reply_addrs--;
748 /* de-init modules */
749 for(i=0; i<mesh->mods.num; i++) {
750 fptr_ok(fptr_whitelist_mod_clear(mesh->mods.mod[i]->clear));
751 (*mesh->mods.mod[i]->clear)(&mstate->s, i);
752 mstate->s.minfo[i] = NULL;
753 mstate->s.ext_state[i] = module_finished;
755 alloc_reg_release(mstate->s.env->alloc, mstate->s.region);
759 mesh_state_delete(struct module_qstate* qstate)
761 struct mesh_area* mesh;
762 struct mesh_state_ref* super, ref;
763 struct mesh_state* mstate;
766 mstate = qstate->mesh_info;
767 mesh = mstate->s.env->mesh;
768 mesh_detach_subs(&mstate->s);
769 if(mstate->list_select == mesh_forever_list) {
770 mesh->num_forever_states --;
771 mesh_list_remove(mstate, &mesh->forever_first,
772 &mesh->forever_last);
773 } else if(mstate->list_select == mesh_jostle_list) {
774 mesh_list_remove(mstate, &mesh->jostle_first,
777 if(!mstate->reply_list && !mstate->cb_list
778 && mstate->super_set.count == 0) {
779 log_assert(mesh->num_detached_states > 0);
780 mesh->num_detached_states--;
782 if(mstate->reply_list || mstate->cb_list) {
783 log_assert(mesh->num_reply_states > 0);
784 mesh->num_reply_states--;
788 RBTREE_FOR(super, struct mesh_state_ref*, &mstate->super_set) {
789 (void)rbtree_delete(&super->s->sub_set, &ref);
791 (void)rbtree_delete(&mesh->run, mstate);
792 (void)rbtree_delete(&mesh->all, mstate);
793 mesh_state_cleanup(mstate);
796 /** helper recursive rbtree find routine */
798 find_in_subsub(struct mesh_state* m, struct mesh_state* tofind, size_t *c)
800 struct mesh_state_ref* r;
801 if((*c)++ > MESH_MAX_SUBSUB)
803 RBTREE_FOR(r, struct mesh_state_ref*, &m->sub_set) {
804 if(r->s == tofind || find_in_subsub(r->s, tofind, c))
810 /** find cycle for already looked up mesh_state */
812 mesh_detect_cycle_found(struct module_qstate* qstate, struct mesh_state* dep_m)
814 struct mesh_state* cyc_m = qstate->mesh_info;
818 if(dep_m == cyc_m || find_in_subsub(dep_m, cyc_m, &counter)) {
819 if(counter > MESH_MAX_SUBSUB)
826 void mesh_detach_subs(struct module_qstate* qstate)
828 struct mesh_area* mesh = qstate->env->mesh;
829 struct mesh_state_ref* ref, lookup;
831 struct rbnode_type* n;
833 lookup.node.key = &lookup;
834 lookup.s = qstate->mesh_info;
835 RBTREE_FOR(ref, struct mesh_state_ref*, &qstate->mesh_info->sub_set) {
841 rbtree_delete(&ref->s->super_set, &lookup);
842 log_assert(n != NULL); /* must have been present */
843 if(!ref->s->reply_list && !ref->s->cb_list
844 && ref->s->super_set.count == 0) {
845 mesh->num_detached_states++;
846 log_assert(mesh->num_detached_states +
847 mesh->num_reply_states <= mesh->all.count);
850 rbtree_init(&qstate->mesh_info->sub_set, &mesh_state_ref_compare);
853 int mesh_add_sub(struct module_qstate* qstate, struct query_info* qinfo,
854 uint16_t qflags, int prime, int valrec, struct module_qstate** newq,
855 struct mesh_state** sub)
857 /* find it, if not, create it */
858 struct mesh_area* mesh = qstate->env->mesh;
859 *sub = mesh_area_find(mesh, NULL, qinfo, qflags,
861 if(mesh_detect_cycle_found(qstate, *sub)) {
862 verbose(VERB_ALGO, "attach failed, cycle detected");
867 struct rbnode_type* n;
869 /* create a new one */
870 *sub = mesh_state_create(qstate->env, qinfo, NULL, qflags, prime,
873 log_err("mesh_attach_sub: out of memory");
881 rbtree_insert(&mesh->all, &(*sub)->node);
882 log_assert(n != NULL);
883 /* set detached (it is now) */
884 mesh->num_detached_states++;
885 /* set new query state to run */
891 rbtree_insert(&mesh->run, &(*sub)->run_node);
892 log_assert(n != NULL);
899 int mesh_attach_sub(struct module_qstate* qstate, struct query_info* qinfo,
900 uint16_t qflags, int prime, int valrec, struct module_qstate** newq)
902 struct mesh_area* mesh = qstate->env->mesh;
903 struct mesh_state* sub = NULL;
905 if(!mesh_add_sub(qstate, qinfo, qflags, prime, valrec, newq, &sub))
907 was_detached = (sub->super_set.count == 0);
908 if(!mesh_state_attachment(qstate->mesh_info, sub))
910 /* if it was a duplicate attachment, the count was not zero before */
911 if(!sub->reply_list && !sub->cb_list && was_detached &&
912 sub->super_set.count == 1) {
913 /* it used to be detached, before this one got added */
914 log_assert(mesh->num_detached_states > 0);
915 mesh->num_detached_states--;
917 /* *newq will be run when inited after the current module stops */
921 int mesh_state_attachment(struct mesh_state* super, struct mesh_state* sub)
924 struct rbnode_type* n;
926 struct mesh_state_ref* subref; /* points to sub, inserted in super */
927 struct mesh_state_ref* superref; /* points to super, inserted in sub */
928 if( !(subref = regional_alloc(super->s.region,
929 sizeof(struct mesh_state_ref))) ||
930 !(superref = regional_alloc(sub->s.region,
931 sizeof(struct mesh_state_ref))) ) {
932 log_err("mesh_state_attachment: out of memory");
935 superref->node.key = superref;
937 subref->node.key = subref;
939 if(!rbtree_insert(&sub->super_set, &superref->node)) {
940 /* this should not happen, iterator and validator do not
941 * attach subqueries that are identical. */
942 /* already attached, we are done, nothing todo.
943 * since superref and subref already allocated in region,
944 * we cannot free them */
952 rbtree_insert(&super->sub_set, &subref->node);
953 log_assert(n != NULL); /* we checked above if statement, the reverse
954 administration should not fail now, unless they are out of sync */
959 * callback results to mesh cb entry
960 * @param m: mesh state to send it for.
961 * @param rcode: if not 0, error code.
962 * @param rep: reply to send (or NULL if rcode is set).
963 * @param r: callback entry
966 mesh_do_callback(struct mesh_state* m, int rcode, struct reply_info* rep,
971 /* bogus messages are not made into servfail, sec_status passed
972 * to the callback function */
973 if(rep && rep->security == sec_status_secure)
976 if(!rep && rcode == LDNS_RCODE_NOERROR)
977 rcode = LDNS_RCODE_SERVFAIL;
978 if(!rcode && (rep->security == sec_status_bogus ||
979 rep->security == sec_status_secure_sentinel_fail)) {
980 if(!(reason = errinf_to_str(&m->s)))
981 rcode = LDNS_RCODE_SERVFAIL;
985 if(rcode == LDNS_RCODE_SERVFAIL) {
986 if(!inplace_cb_reply_servfail_call(m->s.env, &m->s.qinfo, &m->s,
987 rep, rcode, &r->edns, m->s.region))
988 r->edns.opt_list = NULL;
990 if(!inplace_cb_reply_call(m->s.env, &m->s.qinfo, &m->s, rep, rcode,
991 &r->edns, m->s.region))
992 r->edns.opt_list = NULL;
994 fptr_ok(fptr_whitelist_mesh_cb(r->cb));
995 (*r->cb)(r->cb_arg, rcode, r->buf, sec_status_unchecked, NULL);
997 size_t udp_size = r->edns.udp_size;
998 sldns_buffer_clear(r->buf);
999 r->edns.edns_version = EDNS_ADVERTISED_VERSION;
1000 r->edns.udp_size = EDNS_ADVERTISED_SIZE;
1001 r->edns.ext_rcode = 0;
1002 r->edns.bits &= EDNS_DO;
1004 if(!inplace_cb_reply_call(m->s.env, &m->s.qinfo, &m->s, rep,
1005 LDNS_RCODE_NOERROR, &r->edns, m->s.region) ||
1006 !reply_info_answer_encode(&m->s.qinfo, rep, r->qid,
1007 r->qflags, r->buf, 0, 1,
1008 m->s.env->scratch, udp_size, &r->edns,
1009 (int)(r->edns.bits & EDNS_DO), secure))
1011 fptr_ok(fptr_whitelist_mesh_cb(r->cb));
1012 (*r->cb)(r->cb_arg, LDNS_RCODE_SERVFAIL, r->buf,
1013 sec_status_unchecked, NULL);
1015 fptr_ok(fptr_whitelist_mesh_cb(r->cb));
1016 (*r->cb)(r->cb_arg, LDNS_RCODE_NOERROR, r->buf,
1017 rep->security, reason);
1021 m->s.env->mesh->num_reply_addrs--;
1025 * Send reply to mesh reply entry
1026 * @param m: mesh state to send it for.
1027 * @param rcode: if not 0, error code.
1028 * @param rep: reply to send (or NULL if rcode is set).
1029 * @param r: reply entry
1030 * @param prev: previous reply, already has its answer encoded in buffer.
1033 mesh_send_reply(struct mesh_state* m, int rcode, struct reply_info* rep,
1034 struct mesh_reply* r, struct mesh_reply* prev)
1036 struct timeval end_time;
1037 struct timeval duration;
1039 /* Copy the client's EDNS for later restore, to make sure the edns
1040 * compare is with the correct edns options. */
1041 struct edns_data edns_bak = r->edns;
1042 /* examine security status */
1043 if(m->s.env->need_to_validate && (!(r->qflags&BIT_CD) ||
1044 m->s.env->cfg->ignore_cd) && rep &&
1045 (rep->security <= sec_status_bogus ||
1046 rep->security == sec_status_secure_sentinel_fail)) {
1047 rcode = LDNS_RCODE_SERVFAIL;
1048 if(m->s.env->cfg->stat_extended)
1049 m->s.env->mesh->ans_bogus++;
1051 if(rep && rep->security == sec_status_secure)
1054 if(!rep && rcode == LDNS_RCODE_NOERROR)
1055 rcode = LDNS_RCODE_SERVFAIL;
1056 /* send the reply */
1057 /* We don't reuse the encoded answer if either the previous or current
1058 * response has a local alias. We could compare the alias records
1059 * and still reuse the previous answer if they are the same, but that
1060 * would be complicated and error prone for the relatively minor case.
1061 * So we err on the side of safety. */
1062 if(prev && prev->qflags == r->qflags &&
1063 !prev->local_alias && !r->local_alias &&
1064 prev->edns.edns_present == r->edns.edns_present &&
1065 prev->edns.bits == r->edns.bits &&
1066 prev->edns.udp_size == r->edns.udp_size &&
1067 edns_opt_list_compare(prev->edns.opt_list, r->edns.opt_list)
1069 /* if the previous reply is identical to this one, fix ID */
1070 if(prev->query_reply.c->buffer != r->query_reply.c->buffer)
1071 sldns_buffer_copy(r->query_reply.c->buffer,
1072 prev->query_reply.c->buffer);
1073 sldns_buffer_write_at(r->query_reply.c->buffer, 0,
1074 &r->qid, sizeof(uint16_t));
1075 sldns_buffer_write_at(r->query_reply.c->buffer, 12,
1076 r->qname, m->s.qinfo.qname_len);
1077 comm_point_send_reply(&r->query_reply);
1079 m->s.qinfo.qname = r->qname;
1080 m->s.qinfo.local_alias = r->local_alias;
1081 if(rcode == LDNS_RCODE_SERVFAIL) {
1082 if(!inplace_cb_reply_servfail_call(m->s.env, &m->s.qinfo, &m->s,
1083 rep, rcode, &r->edns, m->s.region))
1084 r->edns.opt_list = NULL;
1086 if(!inplace_cb_reply_call(m->s.env, &m->s.qinfo, &m->s, rep, rcode,
1087 &r->edns, m->s.region))
1088 r->edns.opt_list = NULL;
1090 error_encode(r->query_reply.c->buffer, rcode, &m->s.qinfo,
1091 r->qid, r->qflags, &r->edns);
1092 comm_point_send_reply(&r->query_reply);
1094 size_t udp_size = r->edns.udp_size;
1095 r->edns.edns_version = EDNS_ADVERTISED_VERSION;
1096 r->edns.udp_size = EDNS_ADVERTISED_SIZE;
1097 r->edns.ext_rcode = 0;
1098 r->edns.bits &= EDNS_DO;
1099 m->s.qinfo.qname = r->qname;
1100 m->s.qinfo.local_alias = r->local_alias;
1101 if(!inplace_cb_reply_call(m->s.env, &m->s.qinfo, &m->s, rep,
1102 LDNS_RCODE_NOERROR, &r->edns, m->s.region) ||
1103 !reply_info_answer_encode(&m->s.qinfo, rep, r->qid,
1104 r->qflags, r->query_reply.c->buffer, 0, 1,
1105 m->s.env->scratch, udp_size, &r->edns,
1106 (int)(r->edns.bits & EDNS_DO), secure))
1108 if(!inplace_cb_reply_servfail_call(m->s.env, &m->s.qinfo, &m->s,
1109 rep, LDNS_RCODE_SERVFAIL, &r->edns, m->s.region))
1110 r->edns.opt_list = NULL;
1111 error_encode(r->query_reply.c->buffer,
1112 LDNS_RCODE_SERVFAIL, &m->s.qinfo, r->qid,
1113 r->qflags, &r->edns);
1116 comm_point_send_reply(&r->query_reply);
1119 m->s.env->mesh->num_reply_addrs--;
1120 end_time = *m->s.env->now_tv;
1121 timeval_subtract(&duration, &end_time, &r->start_time);
1122 verbose(VERB_ALGO, "query took " ARG_LL "d.%6.6d sec",
1123 (long long)duration.tv_sec, (int)duration.tv_usec);
1124 m->s.env->mesh->replies_sent++;
1125 timeval_add(&m->s.env->mesh->replies_sum_wait, &duration);
1126 timehist_insert(m->s.env->mesh->histogram, &duration);
1127 if(m->s.env->cfg->stat_extended) {
1128 uint16_t rc = FLAGS_GET_RCODE(sldns_buffer_read_u16_at(r->
1129 query_reply.c->buffer, 2));
1130 if(secure) m->s.env->mesh->ans_secure++;
1131 m->s.env->mesh->ans_rcode[ rc ] ++;
1132 if(rc == 0 && LDNS_ANCOUNT(sldns_buffer_begin(r->
1133 query_reply.c->buffer)) == 0)
1134 m->s.env->mesh->ans_nodata++;
1136 /* Log reply sent */
1137 if(m->s.env->cfg->log_replies) {
1138 log_reply_info(0, &m->s.qinfo, &r->query_reply.addr,
1139 r->query_reply.addrlen, duration, 0,
1140 r->query_reply.c->buffer);
1144 void mesh_query_done(struct mesh_state* mstate)
1146 struct mesh_reply* r;
1147 struct mesh_reply* prev = NULL;
1149 struct reply_info* rep = (mstate->s.return_msg?
1150 mstate->s.return_msg->rep:NULL);
1151 for(r = mstate->reply_list; r; r = r->next) {
1152 /* if a response-ip address block has been stored the
1153 * information should be logged for each client. */
1154 if(mstate->s.respip_action_info &&
1155 mstate->s.respip_action_info->addrinfo) {
1156 respip_inform_print(mstate->s.respip_action_info->addrinfo,
1157 r->qname, mstate->s.qinfo.qtype,
1158 mstate->s.qinfo.qclass, r->local_alias,
1162 /* if this query is determined to be dropped during the
1163 * mesh processing, this is the point to take that action. */
1164 if(mstate->s.is_drop)
1165 comm_point_drop_reply(&r->query_reply);
1167 mesh_send_reply(mstate, mstate->s.return_rcode, rep,
1172 mstate->replies_sent = 1;
1173 while((c = mstate->cb_list) != NULL) {
1174 /* take this cb off the list; so that the list can be
1175 * changed, eg. by adds from the callback routine */
1176 mstate->cb_list = c->next;
1177 if(!mstate->reply_list && !mstate->cb_list &&
1178 mstate->super_set.count == 0)
1179 mstate->s.env->mesh->num_detached_states++;
1180 mesh_do_callback(mstate, mstate->s.return_rcode, rep, c);
1184 void mesh_walk_supers(struct mesh_area* mesh, struct mesh_state* mstate)
1186 struct mesh_state_ref* ref;
1187 RBTREE_FOR(ref, struct mesh_state_ref*, &mstate->super_set)
1189 /* make super runnable */
1190 (void)rbtree_insert(&mesh->run, &ref->s->run_node);
1191 /* callback the function to inform super of result */
1192 fptr_ok(fptr_whitelist_mod_inform_super(
1193 mesh->mods.mod[ref->s->s.curmod]->inform_super));
1194 (*mesh->mods.mod[ref->s->s.curmod]->inform_super)(&mstate->s,
1195 ref->s->s.curmod, &ref->s->s);
1199 struct mesh_state* mesh_area_find(struct mesh_area* mesh,
1200 struct respip_client_info* cinfo, struct query_info* qinfo,
1201 uint16_t qflags, int prime, int valrec)
1203 struct mesh_state key;
1204 struct mesh_state* result;
1206 key.node.key = &key;
1207 key.s.is_priming = prime;
1208 key.s.is_valrec = valrec;
1209 key.s.qinfo = *qinfo;
1210 key.s.query_flags = qflags;
1211 /* We are searching for a similar mesh state when we DO want to
1212 * aggregate the state. Thus unique is set to NULL. (default when we
1213 * desire aggregation).*/
1215 key.s.client_info = cinfo;
1217 result = (struct mesh_state*)rbtree_search(&mesh->all, &key);
1221 int mesh_state_add_cb(struct mesh_state* s, struct edns_data* edns,
1222 sldns_buffer* buf, mesh_cb_func_type cb, void* cb_arg,
1223 uint16_t qid, uint16_t qflags)
1225 struct mesh_cb* r = regional_alloc(s->s.region,
1226 sizeof(struct mesh_cb));
1230 log_assert(fptr_whitelist_mesh_cb(cb)); /* early failure ifmissing*/
1234 if(edns->opt_list) {
1235 r->edns.opt_list = edns_opt_copy_region(edns->opt_list,
1237 if(!r->edns.opt_list)
1242 r->next = s->cb_list;
1248 int mesh_state_add_reply(struct mesh_state* s, struct edns_data* edns,
1249 struct comm_reply* rep, uint16_t qid, uint16_t qflags,
1250 const struct query_info* qinfo)
1252 struct mesh_reply* r = regional_alloc(s->s.region,
1253 sizeof(struct mesh_reply));
1256 r->query_reply = *rep;
1258 if(edns->opt_list) {
1259 r->edns.opt_list = edns_opt_copy_region(edns->opt_list,
1261 if(!r->edns.opt_list)
1266 r->start_time = *s->s.env->now_tv;
1267 r->next = s->reply_list;
1268 r->qname = regional_alloc_init(s->s.region, qinfo->qname,
1269 s->s.qinfo.qname_len);
1273 /* Data related to local alias stored in 'qinfo' (if any) is ephemeral
1274 * and can be different for different original queries (even if the
1275 * replaced query name is the same). So we need to make a deep copy
1276 * and store the copy for each reply info. */
1277 if(qinfo->local_alias) {
1278 struct packed_rrset_data* d;
1279 struct packed_rrset_data* dsrc;
1280 r->local_alias = regional_alloc_zero(s->s.region,
1281 sizeof(*qinfo->local_alias));
1284 r->local_alias->rrset = regional_alloc_init(s->s.region,
1285 qinfo->local_alias->rrset,
1286 sizeof(*qinfo->local_alias->rrset));
1287 if(!r->local_alias->rrset)
1289 dsrc = qinfo->local_alias->rrset->entry.data;
1291 /* In the current implementation, a local alias must be
1292 * a single CNAME RR (see worker_handle_request()). */
1293 log_assert(!qinfo->local_alias->next && dsrc->count == 1 &&
1294 qinfo->local_alias->rrset->rk.type ==
1295 htons(LDNS_RR_TYPE_CNAME));
1296 /* Technically, we should make a local copy for the owner
1297 * name of the RRset, but in the case of the first (and
1298 * currently only) local alias RRset, the owner name should
1299 * point to the qname of the corresponding query, which should
1300 * be valid throughout the lifetime of this mesh_reply. So
1301 * we can skip copying. */
1302 log_assert(qinfo->local_alias->rrset->rk.dname ==
1303 sldns_buffer_at(rep->c->buffer, LDNS_HEADER_SIZE));
1305 d = regional_alloc_init(s->s.region, dsrc,
1306 sizeof(struct packed_rrset_data)
1307 + sizeof(size_t) + sizeof(uint8_t*) + sizeof(time_t));
1310 r->local_alias->rrset->entry.data = d;
1311 d->rr_len = (size_t*)((uint8_t*)d +
1312 sizeof(struct packed_rrset_data));
1313 d->rr_data = (uint8_t**)&(d->rr_len[1]);
1314 d->rr_ttl = (time_t*)&(d->rr_data[1]);
1315 d->rr_len[0] = dsrc->rr_len[0];
1316 d->rr_ttl[0] = dsrc->rr_ttl[0];
1317 d->rr_data[0] = regional_alloc_init(s->s.region,
1318 dsrc->rr_data[0], d->rr_len[0]);
1322 r->local_alias = NULL;
1328 /* Extract the query info and flags from 'mstate' into '*qinfop' and '*qflags'.
1329 * Since this is only used for internal refetch of otherwise-expired answer,
1330 * we simply ignore the rare failure mode when memory allocation fails. */
1332 mesh_copy_qinfo(struct mesh_state* mstate, struct query_info** qinfop,
1335 struct regional* region = mstate->s.env->scratch;
1336 struct query_info* qinfo;
1338 qinfo = regional_alloc_init(region, &mstate->s.qinfo, sizeof(*qinfo));
1341 qinfo->qname = regional_alloc_init(region, qinfo->qname,
1346 *qflags = mstate->s.query_flags;
1350 * Continue processing the mesh state at another module.
1351 * Handles module to modules transfer of control.
1352 * Handles module finished.
1353 * @param mesh: the mesh area.
1354 * @param mstate: currently active mesh state.
1355 * Deleted if finished, calls _done and _supers to
1356 * send replies to clients and inform other mesh states.
1357 * This in turn may create additional runnable mesh states.
1358 * @param s: state at which the current module exited.
1359 * @param ev: the event sent to the module.
1360 * returned is the event to send to the next module.
1361 * @return true if continue processing at the new module.
1362 * false if not continued processing is needed.
1365 mesh_continue(struct mesh_area* mesh, struct mesh_state* mstate,
1366 enum module_ext_state s, enum module_ev* ev)
1368 mstate->num_activated++;
1369 if(mstate->num_activated > MESH_MAX_ACTIVATION) {
1370 /* module is looping. Stop it. */
1371 log_err("internal error: looping module (%s) stopped",
1372 mesh->mods.mod[mstate->s.curmod]->name);
1373 log_query_info(VERB_QUERY, "pass error for qstate",
1377 if(s == module_wait_module || s == module_restart_next) {
1378 /* start next module */
1380 if(mesh->mods.num == mstate->s.curmod) {
1381 log_err("Cannot pass to next module; at last module");
1382 log_query_info(VERB_QUERY, "pass error for qstate",
1385 return mesh_continue(mesh, mstate, module_error, ev);
1387 if(s == module_restart_next) {
1388 int curmod = mstate->s.curmod;
1389 for(; mstate->s.curmod < mesh->mods.num;
1390 mstate->s.curmod++) {
1391 fptr_ok(fptr_whitelist_mod_clear(
1392 mesh->mods.mod[mstate->s.curmod]->clear));
1393 (*mesh->mods.mod[mstate->s.curmod]->clear)
1394 (&mstate->s, mstate->s.curmod);
1395 mstate->s.minfo[mstate->s.curmod] = NULL;
1397 mstate->s.curmod = curmod;
1399 *ev = module_event_pass;
1402 if(s == module_wait_subquery && mstate->sub_set.count == 0) {
1403 log_err("module cannot wait for subquery, subquery list empty");
1404 log_query_info(VERB_QUERY, "pass error for qstate",
1408 if(s == module_error && mstate->s.return_rcode == LDNS_RCODE_NOERROR) {
1409 /* error is bad, handle pass back up below */
1410 mstate->s.return_rcode = LDNS_RCODE_SERVFAIL;
1412 if(s == module_error) {
1413 mesh_query_done(mstate);
1414 mesh_walk_supers(mesh, mstate);
1415 mesh_state_delete(&mstate->s);
1418 if(s == module_finished) {
1419 if(mstate->s.curmod == 0) {
1420 struct query_info* qinfo = NULL;
1423 mesh_query_done(mstate);
1424 mesh_walk_supers(mesh, mstate);
1426 /* If the answer to the query needs to be refetched
1427 * from an external DNS server, we'll need to schedule
1428 * a prefetch after removing the current state, so
1429 * we need to make a copy of the query info here. */
1430 if(mstate->s.need_refetch)
1431 mesh_copy_qinfo(mstate, &qinfo, &qflags);
1433 mesh_state_delete(&mstate->s);
1435 mesh_schedule_prefetch(mesh, qinfo, qflags,
1440 /* pass along the locus of control */
1441 mstate->s.curmod --;
1442 *ev = module_event_moddone;
1448 void mesh_run(struct mesh_area* mesh, struct mesh_state* mstate,
1449 enum module_ev ev, struct outbound_entry* e)
1451 enum module_ext_state s;
1452 verbose(VERB_ALGO, "mesh_run: start");
1454 /* run the module */
1455 fptr_ok(fptr_whitelist_mod_operate(
1456 mesh->mods.mod[mstate->s.curmod]->operate));
1457 (*mesh->mods.mod[mstate->s.curmod]->operate)
1458 (&mstate->s, ev, mstate->s.curmod, e);
1460 /* examine results */
1461 mstate->s.reply = NULL;
1462 regional_free_all(mstate->s.env->scratch);
1463 s = mstate->s.ext_state[mstate->s.curmod];
1464 verbose(VERB_ALGO, "mesh_run: %s module exit state is %s",
1465 mesh->mods.mod[mstate->s.curmod]->name, strextstate(s));
1467 if(mesh_continue(mesh, mstate, s, &ev))
1470 /* run more modules */
1471 ev = module_event_pass;
1472 if(mesh->run.count > 0) {
1473 /* pop random element off the runnable tree */
1474 mstate = (struct mesh_state*)mesh->run.root->key;
1475 (void)rbtree_delete(&mesh->run, mstate);
1476 } else mstate = NULL;
1478 if(verbosity >= VERB_ALGO) {
1479 mesh_stats(mesh, "mesh_run: end");
1480 mesh_log_list(mesh);
1485 mesh_log_list(struct mesh_area* mesh)
1488 struct mesh_state* m;
1490 RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
1491 snprintf(buf, sizeof(buf), "%d%s%s%s%s%s%s mod%d %s%s",
1492 num++, (m->s.is_priming)?"p":"", /* prime */
1493 (m->s.is_valrec)?"v":"", /* prime */
1494 (m->s.query_flags&BIT_RD)?"RD":"",
1495 (m->s.query_flags&BIT_CD)?"CD":"",
1496 (m->super_set.count==0)?"d":"", /* detached */
1497 (m->sub_set.count!=0)?"c":"", /* children */
1498 m->s.curmod, (m->reply_list)?"rep":"", /*hasreply*/
1499 (m->cb_list)?"cb":"" /* callbacks */
1501 log_query_info(VERB_ALGO, buf, &m->s.qinfo);
1506 mesh_stats(struct mesh_area* mesh, const char* str)
1508 verbose(VERB_DETAIL, "%s %u recursion states (%u with reply, "
1509 "%u detached), %u waiting replies, %u recursion replies "
1510 "sent, %d replies dropped, %d states jostled out",
1511 str, (unsigned)mesh->all.count,
1512 (unsigned)mesh->num_reply_states,
1513 (unsigned)mesh->num_detached_states,
1514 (unsigned)mesh->num_reply_addrs,
1515 (unsigned)mesh->replies_sent,
1516 (unsigned)mesh->stats_dropped,
1517 (unsigned)mesh->stats_jostled);
1518 if(mesh->replies_sent > 0) {
1520 timeval_divide(&avg, &mesh->replies_sum_wait,
1521 mesh->replies_sent);
1522 log_info("average recursion processing time "
1523 ARG_LL "d.%6.6d sec",
1524 (long long)avg.tv_sec, (int)avg.tv_usec);
1525 log_info("histogram of recursion processing times");
1526 timehist_log(mesh->histogram, "recursions");
1531 mesh_stats_clear(struct mesh_area* mesh)
1535 mesh->replies_sent = 0;
1536 mesh->replies_sum_wait.tv_sec = 0;
1537 mesh->replies_sum_wait.tv_usec = 0;
1538 mesh->stats_jostled = 0;
1539 mesh->stats_dropped = 0;
1540 timehist_clear(mesh->histogram);
1541 mesh->ans_secure = 0;
1542 mesh->ans_bogus = 0;
1543 memset(&mesh->ans_rcode[0], 0, sizeof(size_t)*16);
1544 mesh->ans_nodata = 0;
1548 mesh_get_mem(struct mesh_area* mesh)
1550 struct mesh_state* m;
1551 size_t s = sizeof(*mesh) + sizeof(struct timehist) +
1552 sizeof(struct th_buck)*mesh->histogram->num +
1553 sizeof(sldns_buffer) + sldns_buffer_capacity(mesh->qbuf_bak);
1554 RBTREE_FOR(m, struct mesh_state*, &mesh->all) {
1555 /* all, including m itself allocated in qstate region */
1556 s += regional_get_mem(m->s.region);
1562 mesh_detect_cycle(struct module_qstate* qstate, struct query_info* qinfo,
1563 uint16_t flags, int prime, int valrec)
1565 struct mesh_area* mesh = qstate->env->mesh;
1566 struct mesh_state* dep_m = NULL;
1567 if(!mesh_state_is_unique(qstate->mesh_info))
1568 dep_m = mesh_area_find(mesh, NULL, qinfo, flags, prime, valrec);
1569 return mesh_detect_cycle_found(qstate, dep_m);
1572 void mesh_list_insert(struct mesh_state* m, struct mesh_state** fp,
1573 struct mesh_state** lp)
1575 /* insert as last element */
1584 void mesh_list_remove(struct mesh_state* m, struct mesh_state** fp,
1585 struct mesh_state** lp)
1588 m->next->prev = m->prev;
1591 m->prev->next = m->next;