val_kcache.c revision 1.1.1.1.2.2 1 1.1.1.1.2.2 pgoyette /*
2 1.1.1.1.2.2 pgoyette * validator/val_kcache.c - validator key shared cache with validated keys
3 1.1.1.1.2.2 pgoyette *
4 1.1.1.1.2.2 pgoyette * Copyright (c) 2007, NLnet Labs. All rights reserved.
5 1.1.1.1.2.2 pgoyette *
6 1.1.1.1.2.2 pgoyette * This software is open source.
7 1.1.1.1.2.2 pgoyette *
8 1.1.1.1.2.2 pgoyette * Redistribution and use in source and binary forms, with or without
9 1.1.1.1.2.2 pgoyette * modification, are permitted provided that the following conditions
10 1.1.1.1.2.2 pgoyette * are met:
11 1.1.1.1.2.2 pgoyette *
12 1.1.1.1.2.2 pgoyette * Redistributions of source code must retain the above copyright notice,
13 1.1.1.1.2.2 pgoyette * this list of conditions and the following disclaimer.
14 1.1.1.1.2.2 pgoyette *
15 1.1.1.1.2.2 pgoyette * Redistributions in binary form must reproduce the above copyright notice,
16 1.1.1.1.2.2 pgoyette * this list of conditions and the following disclaimer in the documentation
17 1.1.1.1.2.2 pgoyette * and/or other materials provided with the distribution.
18 1.1.1.1.2.2 pgoyette *
19 1.1.1.1.2.2 pgoyette * Neither the name of the NLNET LABS nor the names of its contributors may
20 1.1.1.1.2.2 pgoyette * be used to endorse or promote products derived from this software without
21 1.1.1.1.2.2 pgoyette * specific prior written permission.
22 1.1.1.1.2.2 pgoyette *
23 1.1.1.1.2.2 pgoyette * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 1.1.1.1.2.2 pgoyette * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 1.1.1.1.2.2 pgoyette * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 1.1.1.1.2.2 pgoyette * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 1.1.1.1.2.2 pgoyette * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 1.1.1.1.2.2 pgoyette * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 1.1.1.1.2.2 pgoyette * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 1.1.1.1.2.2 pgoyette * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 1.1.1.1.2.2 pgoyette * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 1.1.1.1.2.2 pgoyette * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 1.1.1.1.2.2 pgoyette * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1.1.1.2.2 pgoyette */
35 1.1.1.1.2.2 pgoyette
36 1.1.1.1.2.2 pgoyette /**
37 1.1.1.1.2.2 pgoyette * \file
38 1.1.1.1.2.2 pgoyette *
39 1.1.1.1.2.2 pgoyette * This file contains functions for dealing with the validator key cache.
40 1.1.1.1.2.2 pgoyette */
41 1.1.1.1.2.2 pgoyette #include "config.h"
42 1.1.1.1.2.2 pgoyette #include "validator/val_kcache.h"
43 1.1.1.1.2.2 pgoyette #include "validator/val_kentry.h"
44 1.1.1.1.2.2 pgoyette #include "util/log.h"
45 1.1.1.1.2.2 pgoyette #include "util/config_file.h"
46 1.1.1.1.2.2 pgoyette #include "util/data/dname.h"
47 1.1.1.1.2.2 pgoyette #include "util/module.h"
48 1.1.1.1.2.2 pgoyette
49 1.1.1.1.2.2 pgoyette struct key_cache*
50 1.1.1.1.2.2 pgoyette key_cache_create(struct config_file* cfg)
51 1.1.1.1.2.2 pgoyette {
52 1.1.1.1.2.2 pgoyette struct key_cache* kcache = (struct key_cache*)calloc(1,
53 1.1.1.1.2.2 pgoyette sizeof(*kcache));
54 1.1.1.1.2.2 pgoyette size_t numtables, start_size, maxmem;
55 1.1.1.1.2.2 pgoyette if(!kcache) {
56 1.1.1.1.2.2 pgoyette log_err("malloc failure");
57 1.1.1.1.2.2 pgoyette return NULL;
58 1.1.1.1.2.2 pgoyette }
59 1.1.1.1.2.2 pgoyette numtables = cfg->key_cache_slabs;
60 1.1.1.1.2.2 pgoyette start_size = HASH_DEFAULT_STARTARRAY;
61 1.1.1.1.2.2 pgoyette maxmem = cfg->key_cache_size;
62 1.1.1.1.2.2 pgoyette kcache->slab = slabhash_create(numtables, start_size, maxmem,
63 1.1.1.1.2.2 pgoyette &key_entry_sizefunc, &key_entry_compfunc,
64 1.1.1.1.2.2 pgoyette &key_entry_delkeyfunc, &key_entry_deldatafunc, NULL);
65 1.1.1.1.2.2 pgoyette if(!kcache->slab) {
66 1.1.1.1.2.2 pgoyette log_err("malloc failure");
67 1.1.1.1.2.2 pgoyette free(kcache);
68 1.1.1.1.2.2 pgoyette return NULL;
69 1.1.1.1.2.2 pgoyette }
70 1.1.1.1.2.2 pgoyette return kcache;
71 1.1.1.1.2.2 pgoyette }
72 1.1.1.1.2.2 pgoyette
73 1.1.1.1.2.2 pgoyette void
74 1.1.1.1.2.2 pgoyette key_cache_delete(struct key_cache* kcache)
75 1.1.1.1.2.2 pgoyette {
76 1.1.1.1.2.2 pgoyette if(!kcache)
77 1.1.1.1.2.2 pgoyette return;
78 1.1.1.1.2.2 pgoyette slabhash_delete(kcache->slab);
79 1.1.1.1.2.2 pgoyette free(kcache);
80 1.1.1.1.2.2 pgoyette }
81 1.1.1.1.2.2 pgoyette
82 1.1.1.1.2.2 pgoyette void
83 1.1.1.1.2.2 pgoyette key_cache_insert(struct key_cache* kcache, struct key_entry_key* kkey,
84 1.1.1.1.2.2 pgoyette struct module_qstate* qstate)
85 1.1.1.1.2.2 pgoyette {
86 1.1.1.1.2.2 pgoyette struct key_entry_key* k = key_entry_copy(kkey);
87 1.1.1.1.2.2 pgoyette if(!k)
88 1.1.1.1.2.2 pgoyette return;
89 1.1.1.1.2.2 pgoyette if(key_entry_isbad(k) && qstate->errinf &&
90 1.1.1.1.2.2 pgoyette qstate->env->cfg->val_log_level >= 2) {
91 1.1.1.1.2.2 pgoyette /* on malloc failure there is simply no reason string */
92 1.1.1.1.2.2 pgoyette key_entry_set_reason(k, errinf_to_str(qstate));
93 1.1.1.1.2.2 pgoyette }
94 1.1.1.1.2.2 pgoyette key_entry_hash(k);
95 1.1.1.1.2.2 pgoyette slabhash_insert(kcache->slab, k->entry.hash, &k->entry,
96 1.1.1.1.2.2 pgoyette k->entry.data, NULL);
97 1.1.1.1.2.2 pgoyette }
98 1.1.1.1.2.2 pgoyette
99 1.1.1.1.2.2 pgoyette /**
100 1.1.1.1.2.2 pgoyette * Lookup exactly in the key cache. Returns pointer to locked entry.
101 1.1.1.1.2.2 pgoyette * Caller must unlock it after use.
102 1.1.1.1.2.2 pgoyette * @param kcache: the key cache.
103 1.1.1.1.2.2 pgoyette * @param name: for what name to look; uncompressed wireformat
104 1.1.1.1.2.2 pgoyette * @param namelen: length of the name.
105 1.1.1.1.2.2 pgoyette * @param key_class: class of the key.
106 1.1.1.1.2.2 pgoyette * @param wr: set true to get a writelock.
107 1.1.1.1.2.2 pgoyette * @return key entry, locked, or NULL if not found. No TTL checking is
108 1.1.1.1.2.2 pgoyette * performed.
109 1.1.1.1.2.2 pgoyette */
110 1.1.1.1.2.2 pgoyette static struct key_entry_key*
111 1.1.1.1.2.2 pgoyette key_cache_search(struct key_cache* kcache, uint8_t* name, size_t namelen,
112 1.1.1.1.2.2 pgoyette uint16_t key_class, int wr)
113 1.1.1.1.2.2 pgoyette {
114 1.1.1.1.2.2 pgoyette struct lruhash_entry* e;
115 1.1.1.1.2.2 pgoyette struct key_entry_key lookfor;
116 1.1.1.1.2.2 pgoyette lookfor.entry.key = &lookfor;
117 1.1.1.1.2.2 pgoyette lookfor.name = name;
118 1.1.1.1.2.2 pgoyette lookfor.namelen = namelen;
119 1.1.1.1.2.2 pgoyette lookfor.key_class = key_class;
120 1.1.1.1.2.2 pgoyette key_entry_hash(&lookfor);
121 1.1.1.1.2.2 pgoyette e = slabhash_lookup(kcache->slab, lookfor.entry.hash, &lookfor, wr);
122 1.1.1.1.2.2 pgoyette if(!e)
123 1.1.1.1.2.2 pgoyette return NULL;
124 1.1.1.1.2.2 pgoyette return (struct key_entry_key*)e->key;
125 1.1.1.1.2.2 pgoyette }
126 1.1.1.1.2.2 pgoyette
127 1.1.1.1.2.2 pgoyette struct key_entry_key*
128 1.1.1.1.2.2 pgoyette key_cache_obtain(struct key_cache* kcache, uint8_t* name, size_t namelen,
129 1.1.1.1.2.2 pgoyette uint16_t key_class, struct regional* region, time_t now)
130 1.1.1.1.2.2 pgoyette {
131 1.1.1.1.2.2 pgoyette /* keep looking until we find a nonexpired entry */
132 1.1.1.1.2.2 pgoyette while(1) {
133 1.1.1.1.2.2 pgoyette struct key_entry_key* k = key_cache_search(kcache, name,
134 1.1.1.1.2.2 pgoyette namelen, key_class, 0);
135 1.1.1.1.2.2 pgoyette if(k) {
136 1.1.1.1.2.2 pgoyette /* see if TTL is OK */
137 1.1.1.1.2.2 pgoyette struct key_entry_data* d = (struct key_entry_data*)
138 1.1.1.1.2.2 pgoyette k->entry.data;
139 1.1.1.1.2.2 pgoyette if(now <= d->ttl) {
140 1.1.1.1.2.2 pgoyette /* copy and return it */
141 1.1.1.1.2.2 pgoyette struct key_entry_key* retkey =
142 1.1.1.1.2.2 pgoyette key_entry_copy_toregion(k, region);
143 1.1.1.1.2.2 pgoyette lock_rw_unlock(&k->entry.lock);
144 1.1.1.1.2.2 pgoyette return retkey;
145 1.1.1.1.2.2 pgoyette }
146 1.1.1.1.2.2 pgoyette lock_rw_unlock(&k->entry.lock);
147 1.1.1.1.2.2 pgoyette }
148 1.1.1.1.2.2 pgoyette /* snip off first label to continue */
149 1.1.1.1.2.2 pgoyette if(dname_is_root(name))
150 1.1.1.1.2.2 pgoyette break;
151 1.1.1.1.2.2 pgoyette dname_remove_label(&name, &namelen);
152 1.1.1.1.2.2 pgoyette }
153 1.1.1.1.2.2 pgoyette return NULL;
154 1.1.1.1.2.2 pgoyette }
155 1.1.1.1.2.2 pgoyette
156 1.1.1.1.2.2 pgoyette size_t
157 1.1.1.1.2.2 pgoyette key_cache_get_mem(struct key_cache* kcache)
158 1.1.1.1.2.2 pgoyette {
159 1.1.1.1.2.2 pgoyette return sizeof(*kcache) + slabhash_get_mem(kcache->slab);
160 1.1.1.1.2.2 pgoyette }
161 1.1.1.1.2.2 pgoyette
162 1.1.1.1.2.2 pgoyette void key_cache_remove(struct key_cache* kcache,
163 1.1.1.1.2.2 pgoyette uint8_t* name, size_t namelen, uint16_t key_class)
164 1.1.1.1.2.2 pgoyette {
165 1.1.1.1.2.2 pgoyette struct key_entry_key lookfor;
166 1.1.1.1.2.2 pgoyette lookfor.entry.key = &lookfor;
167 1.1.1.1.2.2 pgoyette lookfor.name = name;
168 1.1.1.1.2.2 pgoyette lookfor.namelen = namelen;
169 1.1.1.1.2.2 pgoyette lookfor.key_class = key_class;
170 1.1.1.1.2.2 pgoyette key_entry_hash(&lookfor);
171 1.1.1.1.2.2 pgoyette slabhash_remove(kcache->slab, lookfor.entry.hash, &lookfor);
172 1.1.1.1.2.2 pgoyette }
173