]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
temp region kept for use during query processing.
authorWouter Wijngaards <wouter@nlnetlabs.nl>
Fri, 4 May 2007 12:35:01 +0000 (12:35 +0000)
committerWouter Wijngaards <wouter@nlnetlabs.nl>
Fri, 4 May 2007 12:35:01 +0000 (12:35 +0000)
git-svn-id: file:///svn/unbound/trunk@282 be551aaa-1e26-0410-a405-d3ace91eadb9

daemon/worker.c
daemon/worker.h
doc/Changelog
testcode/unitmsgparse.c
util/data/msgreply.c
util/data/msgreply.h

index a5cde258a9bd18d6c6749664e8347bfcc639940d..09fe3b9452a0371e7dd4fc2b9ea25c14ec01de10 100644 (file)
@@ -47,6 +47,7 @@
 #include "daemon/daemon.h"
 #include "util/netevent.h"
 #include "util/config_file.h"
+#include "util/region-allocator.h"
 #include "util/storage/slabhash.h"
 #include "services/listen_dnsport.h"
 #include "services/outside_network.h"
@@ -188,7 +189,7 @@ worker_store_rrsets(struct worker* worker, struct reply_info* rep)
                slabhash_insert(worker->daemon->rrset_cache, 
                        rep->rrsets[i]->entry.hash, &rep->rrsets[i]->entry,
                        rep->rrsets[i]->entry.data, &worker->alloc);
-               /* TODO store correct key and id */
+               if(e) rep->rrsets[i] = rep->ref[i].key;
        }
 }
 
@@ -217,21 +218,25 @@ worker_handle_reply(struct comm_point* c, void* arg, int error,
        if(LDNS_QDCOUNT(ldns_buffer_begin(c->buffer)) > 1)
                return 0; /* too much in the query section */
        /* woohoo a reply! */
-       if((r=reply_info_parse(c->buffer, &w->worker->alloc, &qinf, &rep))!=0) {
+       if((r=reply_info_parse(c->buffer, &w->worker->alloc, &qinf, &rep,
+               w->worker->scratchpad))!=0) {
                if(r == LDNS_RCODE_SERVFAIL)
                        log_err("reply_info_parse: out of memory");
                /* formerr on my parse gives servfail to my client */
                replyerror(LDNS_RCODE_SERVFAIL, w);
+               region_free_all(w->worker->scratchpad);
                return 0;
        }
        if(!reply_info_answer_encode(&qinf, rep, w->query_id, w->query_flags,
-               w->query_reply.c->buffer, 0, 0)) {
+               w->query_reply.c->buffer, 0, 0, w->worker->scratchpad)) {
                replyerror(LDNS_RCODE_SERVFAIL, w);
                query_info_clear(&qinf);
                reply_info_parsedelete(rep, &w->worker->alloc);
+               region_free_all(w->worker->scratchpad);
                return 0;
        }
        comm_point_send_reply(&w->query_reply);
+       region_free_all(w->worker->scratchpad);
        req_release(w);
        query_info_clear(&w->qinfo);
        if(rep->ttl == 0) {
@@ -339,7 +344,7 @@ worker_handle_control_cmd(struct comm_point* c, void* arg, int error,
 
 /** answer query from the cache */
 static int
-answer_from_cache(struct lruhash_entry* e, uint16_t id,
+answer_from_cache(struct worker* worker, struct lruhash_entry* e, uint16_t id,
        uint16_t flags, struct comm_reply* repinfo)
 {
        struct msgreply_entry* mrentry = (struct msgreply_entry*)e->key;
@@ -366,13 +371,14 @@ answer_from_cache(struct lruhash_entry* e, uint16_t id,
        }
        /* locked and ids and ttls are OK. */
        if(!reply_info_answer_encode(&mrentry->key, rep, id, flags, 
-               repinfo->c->buffer, timenow, 1)) {
+               repinfo->c->buffer, timenow, 1, worker->scratchpad)) {
                replyerror_fillbuf(LDNS_RCODE_SERVFAIL, repinfo, id,
                        flags, &mrentry->key);
        }
        /* unlock */
        for(i=0; i<rep->rrset_count; i++)
                lock_rw_unlock(&rep->ref[i].key->entry.lock);
+       region_free_all(worker->scratchpad);
        /* go and return this buffer to the client */
        return 1;
 }
@@ -415,7 +421,7 @@ worker_handle_request(struct comm_point* c, void* arg, int error,
        if((e=slabhash_lookup(worker->daemon->msg_cache, h, &qinfo, 0))) {
                /* answer from cache - we have acquired a readlock on it */
                log_info("answer from the cache");
-               if(answer_from_cache(e, 
+               if(answer_from_cache(worker, e, 
                        *(uint16_t*)ldns_buffer_begin(c->buffer), 
                        ldns_buffer_read_u16_at(c->buffer, 2), repinfo)) {
                        lock_rw_unlock(&e->lock);
@@ -652,6 +658,8 @@ worker_init(struct worker* worker, struct config_file *cfg,
        server_stats_init(&worker->stats);
        alloc_init(&worker->alloc, &worker->daemon->superalloc, 
                worker->thread_num);
+       worker->scratchpad = region_create_custom(malloc, free, 
+               65536, 8192, 32, 1);
        return 1;
 }
 
@@ -684,6 +692,7 @@ worker_delete(struct worker* worker)
                close(worker->cmd_recv_fd);
        worker->cmd_recv_fd = -1;
        alloc_clear(&worker->alloc);
+       region_destroy(worker->scratchpad);
        free(worker);
 }
 
index 210889564315d58d31800d4af6389ebd065fda35..50e9722d461287380ddb29375823f32243bc6059 100644 (file)
@@ -55,6 +55,7 @@ struct config_file;
 struct daemon;
 struct listen_port;
 struct ub_randstate;
+struct region;
 
 /** size of table used for random numbers. large to be more secure. */
 #define RND_STATE_SIZE 256
@@ -133,6 +134,8 @@ struct worker {
        struct alloc_cache alloc;
        /** per thread statistics */
        struct server_stats stats;
+       /** thread scratch region */
+       struct region* scratchpad;
 };
 
 /**
index faa53bebb267d0c21668990398c2fa3f3e5eaac9..5aec95598093f8bf3ce608e168944be35643130b 100644 (file)
@@ -5,6 +5,7 @@
          alloc cache to store released keys.
        - alloc cache special_release() locks if necessary.
        - rrset trustworthiness type added.
+       - thread keeps a scratchpad region for handling messages.
 
 3 May 2007: Wouter
        - fill refs. Use new parse and encode to answer queries.
index 14e943b8d78986dcda54aaf920404a0e91493a95..fe5221204040df30542e42782a31c281bf106aec 100644 (file)
@@ -258,7 +258,7 @@ testpkt(ldns_buffer* pkt, struct alloc_cache* alloc, ldns_buffer* out,
                flags = 0;
        else    memmove(&flags, ldns_buffer_at(pkt, 2), sizeof(flags));
        flags = ntohs(flags);
-       ret = reply_info_parse(pkt, alloc, &qi, &rep);
+       ret = reply_info_parse(pkt, alloc, &qi, &rep, region);
        if(ret != 0) {
                if(vbmp) printf("parse code %d: %s\n", ret, 
                        ldns_lookup_by_id(ldns_rcodes, ret)->name);
index 8dc0687cde23a8485ac1978e7c595628bc14c294..727bdd65344fd34448c19fa64a76344d707905be 100644 (file)
@@ -343,26 +343,21 @@ parse_create_msg(ldns_buffer* pkt, struct msg_parse* msg,
 }
 
 int reply_info_parse(ldns_buffer* pkt, struct alloc_cache* alloc,
-        struct query_info* qinf, struct reply_info** rep)
+        struct query_info* qinf, struct reply_info** rep, struct region* region)
 {
        /* use scratch pad region-allocator during parsing. */
-       region_type* region = region_create(malloc, free);
        struct msg_parse* msg;
        int ret;
        
        qinf->qname = NULL;
        *rep = NULL;
        if(!(msg = region_alloc(region, sizeof(*msg)))) {
-               region_free_all(region);
-               region_destroy(region);
                return LDNS_RCODE_SERVFAIL;
        }
        memset(msg, 0, sizeof(*msg));
        
        log_assert(ldns_buffer_position(pkt) == 0);
        if((ret = parse_packet(pkt, msg, region)) != 0) {
-               region_free_all(region);
-               region_destroy(region);
                return ret;
        }
 
@@ -372,14 +367,8 @@ int reply_info_parse(ldns_buffer* pkt, struct alloc_cache* alloc,
                query_info_clear(qinf);
                reply_info_parsedelete(*rep, alloc);
                *rep = NULL;
-               region_free_all(region);
-               region_destroy(region);
                return ret;
        }
-
-       /* exit and cleanup */
-       region_free_all(region);
-       region_destroy(region);
        return 0;
 }
 
@@ -1075,10 +1064,9 @@ int reply_info_encode(struct query_info* qinfo, struct reply_info* rep,
 int 
 reply_info_answer_encode(struct query_info* qinf, struct reply_info* rep, 
        uint16_t id, uint16_t qflags, ldns_buffer* pkt, uint32_t timenow,
-       int cached)
+       int cached, struct region* region)
 {
        uint16_t flags;
-       region_type* region = region_create(malloc, free);
 
        if(!cached) {
                /* original flags, copy RD bit from query. */
@@ -1093,7 +1081,6 @@ reply_info_answer_encode(struct query_info* qinf, struct reply_info* rep,
                log_err("reply encode: out of memory");
                return 0;
        }
-       region_destroy(region);
        return 1;
 }
 
index e356d0d1fc8c2c7dd4293b44e79a4baab8950d19..857733ffe05c3efe275f93084890eb16e86d10e3 100644 (file)
@@ -184,12 +184,14 @@ int query_info_parse(struct query_info* m, ldns_buffer* query);
  * @param alloc: creates packed rrset key structures.
  * @param rep: allocated reply_info is returned (only on no error).
  * @param qinf: query_info is returned (only on no error).
+ * @param region: where to store temporary data (for parsing).
  * @return: zero is OK, or DNS error code in case of error
  *     o FORMERR for parse errors.
  *     o SERVFAIL for memory allocation errors.
  */
 int reply_info_parse(ldns_buffer* pkt, struct alloc_cache* alloc,
-       struct query_info* qinf, struct reply_info** rep);
+       struct query_info* qinf, struct reply_info** rep, 
+       struct region* region);
 
 /**
  * Sorts the ref array.
@@ -253,11 +255,12 @@ hashvalue_t query_info_hash(struct query_info *q);
  * @param timenow: time to subtract.
  * @param cached: set true if a cached reply (so no AA bit).
  *     set false for the first reply.
+ * @param region: where to allocate temp variables (for compression).
  * @return: 0 on error (server failure).
  */
 int reply_info_answer_encode(struct query_info* qinf, struct reply_info* rep, 
        uint16_t id, uint16_t qflags, ldns_buffer* dest, uint32_t timenow,
-       int cached);
+       int cached, struct region* region);
 
 /**
  * Regenerate the wireformat from the stored msg reply.