rrset.c revision 1.1 1 1.1 christos /*
2 1.1 christos * services/cache/rrset.c - Resource record set cache.
3 1.1 christos *
4 1.1 christos * Copyright (c) 2007, NLnet Labs. All rights reserved.
5 1.1 christos *
6 1.1 christos * This software is open source.
7 1.1 christos *
8 1.1 christos * Redistribution and use in source and binary forms, with or without
9 1.1 christos * modification, are permitted provided that the following conditions
10 1.1 christos * are met:
11 1.1 christos *
12 1.1 christos * Redistributions of source code must retain the above copyright notice,
13 1.1 christos * this list of conditions and the following disclaimer.
14 1.1 christos *
15 1.1 christos * Redistributions in binary form must reproduce the above copyright notice,
16 1.1 christos * this list of conditions and the following disclaimer in the documentation
17 1.1 christos * and/or other materials provided with the distribution.
18 1.1 christos *
19 1.1 christos * Neither the name of the NLNET LABS nor the names of its contributors may
20 1.1 christos * be used to endorse or promote products derived from this software without
21 1.1 christos * specific prior written permission.
22 1.1 christos *
23 1.1 christos * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 1.1 christos * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 1.1 christos * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 1.1 christos * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 1.1 christos * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 1.1 christos * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 1.1 christos * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 1.1 christos * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 1.1 christos * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 1.1 christos * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 1.1 christos * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 1.1 christos */
35 1.1 christos
36 1.1 christos /**
37 1.1 christos * \file
38 1.1 christos *
39 1.1 christos * This file contains the rrset cache.
40 1.1 christos */
41 1.1 christos #include "config.h"
42 1.1 christos #include "services/cache/rrset.h"
43 1.1 christos #include "sldns/rrdef.h"
44 1.1 christos #include "util/storage/slabhash.h"
45 1.1 christos #include "util/config_file.h"
46 1.1 christos #include "util/data/packed_rrset.h"
47 1.1 christos #include "util/data/msgreply.h"
48 1.1 christos #include "util/regional.h"
49 1.1 christos #include "util/alloc.h"
50 1.1 christos
51 1.1 christos void
52 1.1 christos rrset_markdel(void* key)
53 1.1 christos {
54 1.1 christos struct ub_packed_rrset_key* r = (struct ub_packed_rrset_key*)key;
55 1.1 christos r->id = 0;
56 1.1 christos }
57 1.1 christos
58 1.1 christos struct rrset_cache* rrset_cache_create(struct config_file* cfg,
59 1.1 christos struct alloc_cache* alloc)
60 1.1 christos {
61 1.1 christos size_t slabs = (cfg?cfg->rrset_cache_slabs:HASH_DEFAULT_SLABS);
62 1.1 christos size_t startarray = HASH_DEFAULT_STARTARRAY;
63 1.1 christos size_t maxmem = (cfg?cfg->rrset_cache_size:HASH_DEFAULT_MAXMEM);
64 1.1 christos
65 1.1 christos struct rrset_cache *r = (struct rrset_cache*)slabhash_create(slabs,
66 1.1 christos startarray, maxmem, ub_rrset_sizefunc, ub_rrset_compare,
67 1.1 christos ub_rrset_key_delete, rrset_data_delete, alloc);
68 1.1 christos slabhash_setmarkdel(&r->table, &rrset_markdel);
69 1.1 christos return r;
70 1.1 christos }
71 1.1 christos
72 1.1 christos void rrset_cache_delete(struct rrset_cache* r)
73 1.1 christos {
74 1.1 christos if(!r)
75 1.1 christos return;
76 1.1 christos slabhash_delete(&r->table);
77 1.1 christos /* slabhash delete also does free(r), since table is first in struct*/
78 1.1 christos }
79 1.1 christos
80 1.1 christos struct rrset_cache* rrset_cache_adjust(struct rrset_cache *r,
81 1.1 christos struct config_file* cfg, struct alloc_cache* alloc)
82 1.1 christos {
83 1.1 christos if(!r || !cfg || cfg->rrset_cache_slabs != r->table.size ||
84 1.1 christos cfg->rrset_cache_size != slabhash_get_size(&r->table))
85 1.1 christos {
86 1.1 christos rrset_cache_delete(r);
87 1.1 christos r = rrset_cache_create(cfg, alloc);
88 1.1 christos }
89 1.1 christos return r;
90 1.1 christos }
91 1.1 christos
92 1.1 christos void
93 1.1 christos rrset_cache_touch(struct rrset_cache* r, struct ub_packed_rrset_key* key,
94 1.1 christos hashvalue_t hash, rrset_id_t id)
95 1.1 christos {
96 1.1 christos struct lruhash* table = slabhash_gettable(&r->table, hash);
97 1.1 christos /*
98 1.1 christos * This leads to locking problems, deadlocks, if the caller is
99 1.1 christos * holding any other rrset lock.
100 1.1 christos * Because a lookup through the hashtable does:
101 1.1 christos * tablelock -> entrylock (for that entry caller holds)
102 1.1 christos * And this would do
103 1.1 christos * entrylock(already held) -> tablelock
104 1.1 christos * And if two threads do this, it results in deadlock.
105 1.1 christos * So, the caller must not hold entrylock.
106 1.1 christos */
107 1.1 christos lock_quick_lock(&table->lock);
108 1.1 christos /* we have locked the hash table, the item can still be deleted.
109 1.1 christos * because it could already have been reclaimed, but not yet set id=0.
110 1.1 christos * This is because some lruhash routines have lazy deletion.
111 1.1 christos * so, we must acquire a lock on the item to verify the id != 0.
112 1.1 christos * also, with hash not changed, we are using the right slab.
113 1.1 christos */
114 1.1 christos lock_rw_rdlock(&key->entry.lock);
115 1.1 christos if(key->id == id && key->entry.hash == hash) {
116 1.1 christos lru_touch(table, &key->entry);
117 1.1 christos }
118 1.1 christos lock_rw_unlock(&key->entry.lock);
119 1.1 christos lock_quick_unlock(&table->lock);
120 1.1 christos }
121 1.1 christos
122 1.1 christos /** see if rrset needs to be updated in the cache */
123 1.1 christos static int
124 1.1 christos need_to_update_rrset(void* nd, void* cd, time_t timenow, int equal, int ns)
125 1.1 christos {
126 1.1 christos struct packed_rrset_data* newd = (struct packed_rrset_data*)nd;
127 1.1 christos struct packed_rrset_data* cached = (struct packed_rrset_data*)cd;
128 1.1 christos /* o store if rrset has been validated
129 1.1 christos * everything better than bogus data
130 1.1 christos * secure is preferred */
131 1.1 christos if( newd->security == sec_status_secure &&
132 1.1 christos cached->security != sec_status_secure)
133 1.1 christos return 1;
134 1.1 christos if( cached->security == sec_status_bogus &&
135 1.1 christos newd->security != sec_status_bogus && !equal)
136 1.1 christos return 1;
137 1.1 christos /* o if current RRset is more trustworthy - insert it */
138 1.1 christos if( newd->trust > cached->trust ) {
139 1.1 christos /* if the cached rrset is bogus, and this one equal,
140 1.1 christos * do not update the TTL - let it expire. */
141 1.1 christos if(equal && cached->ttl >= timenow &&
142 1.1 christos cached->security == sec_status_bogus)
143 1.1 christos return 0;
144 1.1 christos return 1;
145 1.1 christos }
146 1.1 christos /* o item in cache has expired */
147 1.1 christos if( cached->ttl < timenow )
148 1.1 christos return 1;
149 1.1 christos /* o same trust, but different in data - insert it */
150 1.1 christos if( newd->trust == cached->trust && !equal ) {
151 1.1 christos /* if this is type NS, do not 'stick' to owner that changes
152 1.1 christos * the NS RRset, but use the old TTL for the new data, and
153 1.1 christos * update to fetch the latest data. ttl is not expired, because
154 1.1 christos * that check was before this one. */
155 1.1 christos if(ns) {
156 1.1 christos size_t i;
157 1.1 christos newd->ttl = cached->ttl;
158 1.1 christos for(i=0; i<(newd->count+newd->rrsig_count); i++)
159 1.1 christos if(newd->rr_ttl[i] > newd->ttl)
160 1.1 christos newd->rr_ttl[i] = newd->ttl;
161 1.1 christos }
162 1.1 christos return 1;
163 1.1 christos }
164 1.1 christos return 0;
165 1.1 christos }
166 1.1 christos
167 1.1 christos /** Update RRSet special key ID */
168 1.1 christos static void
169 1.1 christos rrset_update_id(struct rrset_ref* ref, struct alloc_cache* alloc)
170 1.1 christos {
171 1.1 christos /* this may clear the cache and invalidate lock below */
172 1.1 christos uint64_t newid = alloc_get_id(alloc);
173 1.1 christos /* obtain writelock */
174 1.1 christos lock_rw_wrlock(&ref->key->entry.lock);
175 1.1 christos /* check if it was deleted in the meantime, if so, skip update */
176 1.1 christos if(ref->key->id == ref->id) {
177 1.1 christos ref->key->id = newid;
178 1.1 christos ref->id = newid;
179 1.1 christos }
180 1.1 christos lock_rw_unlock(&ref->key->entry.lock);
181 1.1 christos }
182 1.1 christos
183 1.1 christos int
184 1.1 christos rrset_cache_update(struct rrset_cache* r, struct rrset_ref* ref,
185 1.1 christos struct alloc_cache* alloc, time_t timenow)
186 1.1 christos {
187 1.1 christos struct lruhash_entry* e;
188 1.1 christos struct ub_packed_rrset_key* k = ref->key;
189 1.1 christos hashvalue_t h = k->entry.hash;
190 1.1 christos uint16_t rrset_type = ntohs(k->rk.type);
191 1.1 christos int equal = 0;
192 1.1 christos log_assert(ref->id != 0 && k->id != 0);
193 1.1 christos log_assert(k->rk.dname != NULL);
194 1.1 christos /* looks up item with a readlock - no editing! */
195 1.1 christos if((e=slabhash_lookup(&r->table, h, k, 0)) != 0) {
196 1.1 christos /* return id and key as they will be used in the cache
197 1.1 christos * since the lruhash_insert, if item already exists, deallocs
198 1.1 christos * the passed key in favor of the already stored key.
199 1.1 christos * because of the small gap (see below) this key ptr and id
200 1.1 christos * may prove later to be already deleted, which is no problem
201 1.1 christos * as it only makes a cache miss.
202 1.1 christos */
203 1.1 christos ref->key = (struct ub_packed_rrset_key*)e->key;
204 1.1 christos ref->id = ref->key->id;
205 1.1 christos equal = rrsetdata_equal((struct packed_rrset_data*)k->entry.
206 1.1 christos data, (struct packed_rrset_data*)e->data);
207 1.1 christos if(!need_to_update_rrset(k->entry.data, e->data, timenow,
208 1.1 christos equal, (rrset_type==LDNS_RR_TYPE_NS))) {
209 1.1 christos /* cache is superior, return that value */
210 1.1 christos lock_rw_unlock(&e->lock);
211 1.1 christos ub_packed_rrset_parsedelete(k, alloc);
212 1.1 christos if(equal) return 2;
213 1.1 christos return 1;
214 1.1 christos }
215 1.1 christos lock_rw_unlock(&e->lock);
216 1.1 christos /* Go on and insert the passed item.
217 1.1 christos * small gap here, where entry is not locked.
218 1.1 christos * possibly entry is updated with something else.
219 1.1 christos * we then overwrite that with our data.
220 1.1 christos * this is just too bad, its cache anyway. */
221 1.1 christos /* use insert to update entry to manage lruhash
222 1.1 christos * cache size values nicely. */
223 1.1 christos }
224 1.1 christos log_assert(ref->key->id != 0);
225 1.1 christos slabhash_insert(&r->table, h, &k->entry, k->entry.data, alloc);
226 1.1 christos if(e) {
227 1.1 christos /* For NSEC, NSEC3, DNAME, when rdata is updated, update
228 1.1 christos * the ID number so that proofs in message cache are
229 1.1 christos * invalidated */
230 1.1 christos if((rrset_type == LDNS_RR_TYPE_NSEC
231 1.1 christos || rrset_type == LDNS_RR_TYPE_NSEC3
232 1.1 christos || rrset_type == LDNS_RR_TYPE_DNAME) && !equal) {
233 1.1 christos rrset_update_id(ref, alloc);
234 1.1 christos }
235 1.1 christos return 1;
236 1.1 christos }
237 1.1 christos return 0;
238 1.1 christos }
239 1.1 christos
240 1.1 christos struct ub_packed_rrset_key*
241 1.1 christos rrset_cache_lookup(struct rrset_cache* r, uint8_t* qname, size_t qnamelen,
242 1.1 christos uint16_t qtype, uint16_t qclass, uint32_t flags, time_t timenow,
243 1.1 christos int wr)
244 1.1 christos {
245 1.1 christos struct lruhash_entry* e;
246 1.1 christos struct ub_packed_rrset_key key;
247 1.1 christos
248 1.1 christos key.entry.key = &key;
249 1.1 christos key.entry.data = NULL;
250 1.1 christos key.rk.dname = qname;
251 1.1 christos key.rk.dname_len = qnamelen;
252 1.1 christos key.rk.type = htons(qtype);
253 1.1 christos key.rk.rrset_class = htons(qclass);
254 1.1 christos key.rk.flags = flags;
255 1.1 christos
256 1.1 christos key.entry.hash = rrset_key_hash(&key.rk);
257 1.1 christos
258 1.1 christos if((e = slabhash_lookup(&r->table, key.entry.hash, &key, wr))) {
259 1.1 christos /* check TTL */
260 1.1 christos struct packed_rrset_data* data =
261 1.1 christos (struct packed_rrset_data*)e->data;
262 1.1 christos if(timenow > data->ttl) {
263 1.1 christos lock_rw_unlock(&e->lock);
264 1.1 christos return NULL;
265 1.1 christos }
266 1.1 christos /* we're done */
267 1.1 christos return (struct ub_packed_rrset_key*)e->key;
268 1.1 christos }
269 1.1 christos return NULL;
270 1.1 christos }
271 1.1 christos
272 1.1 christos int
273 1.1 christos rrset_array_lock(struct rrset_ref* ref, size_t count, time_t timenow)
274 1.1 christos {
275 1.1 christos size_t i;
276 1.1 christos for(i=0; i<count; i++) {
277 1.1 christos if(i>0 && ref[i].key == ref[i-1].key)
278 1.1 christos continue; /* only lock items once */
279 1.1 christos lock_rw_rdlock(&ref[i].key->entry.lock);
280 1.1 christos if(ref[i].id != ref[i].key->id || timenow >
281 1.1 christos ((struct packed_rrset_data*)(ref[i].key->entry.data))
282 1.1 christos ->ttl) {
283 1.1 christos /* failure! rollback our readlocks */
284 1.1 christos rrset_array_unlock(ref, i+1);
285 1.1 christos return 0;
286 1.1 christos }
287 1.1 christos }
288 1.1 christos return 1;
289 1.1 christos }
290 1.1 christos
291 1.1 christos void
292 1.1 christos rrset_array_unlock(struct rrset_ref* ref, size_t count)
293 1.1 christos {
294 1.1 christos size_t i;
295 1.1 christos for(i=0; i<count; i++) {
296 1.1 christos if(i>0 && ref[i].key == ref[i-1].key)
297 1.1 christos continue; /* only unlock items once */
298 1.1 christos lock_rw_unlock(&ref[i].key->entry.lock);
299 1.1 christos }
300 1.1 christos }
301 1.1 christos
302 1.1 christos void
303 1.1 christos rrset_array_unlock_touch(struct rrset_cache* r, struct regional* scratch,
304 1.1 christos struct rrset_ref* ref, size_t count)
305 1.1 christos {
306 1.1 christos hashvalue_t* h;
307 1.1 christos size_t i;
308 1.1 christos if(count > RR_COUNT_MAX || !(h = (hashvalue_t*)regional_alloc(scratch,
309 1.1 christos sizeof(hashvalue_t)*count))) {
310 1.1 christos log_warn("rrset LRU: memory allocation failed");
311 1.1 christos h = NULL;
312 1.1 christos } else /* store hash values */
313 1.1 christos for(i=0; i<count; i++)
314 1.1 christos h[i] = ref[i].key->entry.hash;
315 1.1 christos /* unlock */
316 1.1 christos for(i=0; i<count; i++) {
317 1.1 christos if(i>0 && ref[i].key == ref[i-1].key)
318 1.1 christos continue; /* only unlock items once */
319 1.1 christos lock_rw_unlock(&ref[i].key->entry.lock);
320 1.1 christos }
321 1.1 christos if(h) {
322 1.1 christos /* LRU touch, with no rrset locks held */
323 1.1 christos for(i=0; i<count; i++) {
324 1.1 christos if(i>0 && ref[i].key == ref[i-1].key)
325 1.1 christos continue; /* only touch items once */
326 1.1 christos rrset_cache_touch(r, ref[i].key, h[i], ref[i].id);
327 1.1 christos }
328 1.1 christos }
329 1.1 christos }
330 1.1 christos
331 1.1 christos void
332 1.1 christos rrset_update_sec_status(struct rrset_cache* r,
333 1.1 christos struct ub_packed_rrset_key* rrset, time_t now)
334 1.1 christos {
335 1.1 christos struct packed_rrset_data* updata =
336 1.1 christos (struct packed_rrset_data*)rrset->entry.data;
337 1.1 christos struct lruhash_entry* e;
338 1.1 christos struct packed_rrset_data* cachedata;
339 1.1 christos
340 1.1 christos /* hash it again to make sure it has a hash */
341 1.1 christos rrset->entry.hash = rrset_key_hash(&rrset->rk);
342 1.1 christos
343 1.1 christos e = slabhash_lookup(&r->table, rrset->entry.hash, rrset, 1);
344 1.1 christos if(!e)
345 1.1 christos return; /* not in the cache anymore */
346 1.1 christos cachedata = (struct packed_rrset_data*)e->data;
347 1.1 christos if(!rrsetdata_equal(updata, cachedata)) {
348 1.1 christos lock_rw_unlock(&e->lock);
349 1.1 christos return; /* rrset has changed in the meantime */
350 1.1 christos }
351 1.1 christos /* update the cached rrset */
352 1.1 christos if(updata->security > cachedata->security) {
353 1.1 christos size_t i;
354 1.1 christos if(updata->trust > cachedata->trust)
355 1.1 christos cachedata->trust = updata->trust;
356 1.1 christos cachedata->security = updata->security;
357 1.1 christos /* for NS records only shorter TTLs, other types: update it */
358 1.1 christos if(ntohs(rrset->rk.type) != LDNS_RR_TYPE_NS ||
359 1.1 christos updata->ttl+now < cachedata->ttl ||
360 1.1 christos cachedata->ttl < now ||
361 1.1 christos updata->security == sec_status_bogus) {
362 1.1 christos cachedata->ttl = updata->ttl + now;
363 1.1 christos for(i=0; i<cachedata->count+cachedata->rrsig_count; i++)
364 1.1 christos cachedata->rr_ttl[i] = updata->rr_ttl[i]+now;
365 1.1 christos }
366 1.1 christos }
367 1.1 christos lock_rw_unlock(&e->lock);
368 1.1 christos }
369 1.1 christos
370 1.1 christos void
371 1.1 christos rrset_check_sec_status(struct rrset_cache* r,
372 1.1 christos struct ub_packed_rrset_key* rrset, time_t now)
373 1.1 christos {
374 1.1 christos struct packed_rrset_data* updata =
375 1.1 christos (struct packed_rrset_data*)rrset->entry.data;
376 1.1 christos struct lruhash_entry* e;
377 1.1 christos struct packed_rrset_data* cachedata;
378 1.1 christos
379 1.1 christos /* hash it again to make sure it has a hash */
380 1.1 christos rrset->entry.hash = rrset_key_hash(&rrset->rk);
381 1.1 christos
382 1.1 christos e = slabhash_lookup(&r->table, rrset->entry.hash, rrset, 0);
383 1.1 christos if(!e)
384 1.1 christos return; /* not in the cache anymore */
385 1.1 christos cachedata = (struct packed_rrset_data*)e->data;
386 1.1 christos if(now > cachedata->ttl || !rrsetdata_equal(updata, cachedata)) {
387 1.1 christos lock_rw_unlock(&e->lock);
388 1.1 christos return; /* expired, or rrset has changed in the meantime */
389 1.1 christos }
390 1.1 christos if(cachedata->security > updata->security) {
391 1.1 christos updata->security = cachedata->security;
392 1.1 christos if(cachedata->security == sec_status_bogus) {
393 1.1 christos size_t i;
394 1.1 christos updata->ttl = cachedata->ttl - now;
395 1.1 christos for(i=0; i<cachedata->count+cachedata->rrsig_count; i++)
396 1.1 christos if(cachedata->rr_ttl[i] < now)
397 1.1 christos updata->rr_ttl[i] = 0;
398 1.1 christos else updata->rr_ttl[i] =
399 1.1 christos cachedata->rr_ttl[i]-now;
400 1.1 christos }
401 1.1 christos if(cachedata->trust > updata->trust)
402 1.1 christos updata->trust = cachedata->trust;
403 1.1 christos }
404 1.1 christos lock_rw_unlock(&e->lock);
405 1.1 christos }
406 1.1 christos
407 1.1 christos void rrset_cache_remove(struct rrset_cache* r, uint8_t* nm, size_t nmlen,
408 1.1 christos uint16_t type, uint16_t dclass, uint32_t flags)
409 1.1 christos {
410 1.1 christos struct ub_packed_rrset_key key;
411 1.1 christos key.entry.key = &key;
412 1.1 christos key.rk.dname = nm;
413 1.1 christos key.rk.dname_len = nmlen;
414 1.1 christos key.rk.rrset_class = htons(dclass);
415 1.1 christos key.rk.type = htons(type);
416 1.1 christos key.rk.flags = flags;
417 1.1 christos key.entry.hash = rrset_key_hash(&key.rk);
418 1.1 christos slabhash_remove(&r->table, key.entry.hash, &key);
419 1.1 christos }
420