xref: /freebsd/contrib/unbound/validator/val_kcache.c (revision e5b786625f7f82a1fa91e41823332459ea5550f9)
1 /*
2  * validator/val_kcache.c - validator key shared cache with validated keys
3  *
4  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5  *
6  * This software is open source.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
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.
18  *
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.
22  *
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.
34  */
35 
36 /**
37  * \file
38  *
39  * This file contains functions for dealing with the validator key cache.
40  */
41 #include "config.h"
42 #include "validator/val_kcache.h"
43 #include "validator/val_kentry.h"
44 #include "util/log.h"
45 #include "util/config_file.h"
46 #include "util/data/dname.h"
47 #include "util/module.h"
48 
49 struct key_cache*
50 key_cache_create(struct config_file* cfg)
51 {
52 	struct key_cache* kcache = (struct key_cache*)calloc(1,
53 		sizeof(*kcache));
54 	size_t numtables, start_size, maxmem;
55 	if(!kcache) {
56 		log_err("malloc failure");
57 		return NULL;
58 	}
59 	numtables = cfg->key_cache_slabs;
60 	start_size = HASH_DEFAULT_STARTARRAY;
61 	maxmem = cfg->key_cache_size;
62 	kcache->slab = slabhash_create(numtables, start_size, maxmem,
63 		&key_entry_sizefunc, &key_entry_compfunc,
64 		&key_entry_delkeyfunc, &key_entry_deldatafunc, NULL);
65 	if(!kcache->slab) {
66 		log_err("malloc failure");
67 		free(kcache);
68 		return NULL;
69 	}
70 	return kcache;
71 }
72 
73 void
74 key_cache_delete(struct key_cache* kcache)
75 {
76 	if(!kcache)
77 		return;
78 	slabhash_delete(kcache->slab);
79 	free(kcache);
80 }
81 
82 void
83 key_cache_insert(struct key_cache* kcache, struct key_entry_key* kkey,
84 	int copy_reason)
85 {
86 	struct key_entry_key* k = key_entry_copy(kkey, copy_reason);
87 	if(!k)
88 		return;
89 	key_entry_hash(k);
90 	slabhash_insert(kcache->slab, k->entry.hash, &k->entry,
91 		k->entry.data, NULL);
92 }
93 
94 /**
95  * Lookup exactly in the key cache. Returns pointer to locked entry.
96  * Caller must unlock it after use.
97  * @param kcache: the key cache.
98  * @param name: for what name to look; uncompressed wireformat
99  * @param namelen: length of the name.
100  * @param key_class: class of the key.
101  * @param wr: set true to get a writelock.
102  * @return key entry, locked, or NULL if not found. No TTL checking is
103  * 	performed.
104  */
105 static struct key_entry_key*
106 key_cache_search(struct key_cache* kcache, uint8_t* name, size_t namelen,
107 	uint16_t key_class, int wr)
108 {
109 	struct lruhash_entry* e;
110 	struct key_entry_key lookfor;
111 	lookfor.entry.key = &lookfor;
112 	lookfor.name = name;
113 	lookfor.namelen = namelen;
114 	lookfor.key_class = key_class;
115 	key_entry_hash(&lookfor);
116 	e = slabhash_lookup(kcache->slab, lookfor.entry.hash, &lookfor, wr);
117 	if(!e)
118 		return NULL;
119 	return (struct key_entry_key*)e->key;
120 }
121 
122 struct key_entry_key*
123 key_cache_obtain(struct key_cache* kcache, uint8_t* name, size_t namelen,
124 	uint16_t key_class, struct regional* region, time_t now)
125 {
126 	/* keep looking until we find a nonexpired entry */
127 	while(1) {
128 		struct key_entry_key* k = key_cache_search(kcache, name,
129 			namelen, key_class, 0);
130 		if(k) {
131 			/* see if TTL is OK */
132 			struct key_entry_data* d = (struct key_entry_data*)
133 				k->entry.data;
134 			if(now <= d->ttl) {
135 				/* copy and return it */
136 				struct key_entry_key* retkey =
137 					key_entry_copy_toregion(k, region);
138 				lock_rw_unlock(&k->entry.lock);
139 				return retkey;
140 			}
141 			lock_rw_unlock(&k->entry.lock);
142 		}
143 		/* snip off first label to continue */
144 		if(dname_is_root(name))
145 			break;
146 		dname_remove_label(&name, &namelen);
147 	}
148 	return NULL;
149 }
150 
151 size_t
152 key_cache_get_mem(struct key_cache* kcache)
153 {
154 	return sizeof(*kcache) + slabhash_get_mem(kcache->slab);
155 }
156 
157 void key_cache_remove(struct key_cache* kcache,
158 	uint8_t* name, size_t namelen, uint16_t key_class)
159 {
160 	struct key_entry_key lookfor;
161 	lookfor.entry.key = &lookfor;
162 	lookfor.name = name;
163 	lookfor.namelen = namelen;
164 	lookfor.key_class = key_class;
165 	key_entry_hash(&lookfor);
166 	slabhash_remove(kcache->slab, lookfor.entry.hash, &lookfor);
167 }
168