xref: /freebsd/contrib/unbound/services/cache/rrset.h (revision 335c7cda12138f2aefa41fb739707612cc12a9be)
1b7579f77SDag-Erling Smørgrav /*
2b7579f77SDag-Erling Smørgrav  * services/cache/rrset.h - Resource record set cache.
3b7579f77SDag-Erling Smørgrav  *
4b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5b7579f77SDag-Erling Smørgrav  *
6b7579f77SDag-Erling Smørgrav  * This software is open source.
7b7579f77SDag-Erling Smørgrav  *
8b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10b7579f77SDag-Erling Smørgrav  * are met:
11b7579f77SDag-Erling Smørgrav  *
12b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14b7579f77SDag-Erling Smørgrav  *
15b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18b7579f77SDag-Erling Smørgrav  *
19b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22b7579f77SDag-Erling Smørgrav  *
23b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2417d15b25SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2517d15b25SDag-Erling Smørgrav  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2617d15b25SDag-Erling Smørgrav  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2717d15b25SDag-Erling Smørgrav  * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2817d15b25SDag-Erling Smørgrav  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
2917d15b25SDag-Erling Smørgrav  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
3017d15b25SDag-Erling Smørgrav  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
3117d15b25SDag-Erling Smørgrav  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
3217d15b25SDag-Erling Smørgrav  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3317d15b25SDag-Erling Smørgrav  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34b7579f77SDag-Erling Smørgrav  */
35b7579f77SDag-Erling Smørgrav 
36b7579f77SDag-Erling Smørgrav /**
37b7579f77SDag-Erling Smørgrav  * \file
38b7579f77SDag-Erling Smørgrav  *
39b7579f77SDag-Erling Smørgrav  * This file contains the rrset cache.
40b7579f77SDag-Erling Smørgrav  */
41b7579f77SDag-Erling Smørgrav 
42b7579f77SDag-Erling Smørgrav #ifndef SERVICES_CACHE_RRSET_H
43b7579f77SDag-Erling Smørgrav #define SERVICES_CACHE_RRSET_H
44b7579f77SDag-Erling Smørgrav #include "util/storage/lruhash.h"
45b7579f77SDag-Erling Smørgrav #include "util/storage/slabhash.h"
46b7579f77SDag-Erling Smørgrav #include "util/data/packed_rrset.h"
47b7579f77SDag-Erling Smørgrav struct config_file;
48b7579f77SDag-Erling Smørgrav struct alloc_cache;
49b7579f77SDag-Erling Smørgrav struct rrset_ref;
50b7579f77SDag-Erling Smørgrav struct regional;
51b7579f77SDag-Erling Smørgrav 
52b7579f77SDag-Erling Smørgrav /**
53b7579f77SDag-Erling Smørgrav  * The rrset cache
54b7579f77SDag-Erling Smørgrav  * Thin wrapper around hashtable, like a typedef.
55b7579f77SDag-Erling Smørgrav  */
56b7579f77SDag-Erling Smørgrav struct rrset_cache {
57b7579f77SDag-Erling Smørgrav 	/** uses partitioned hash table */
58b7579f77SDag-Erling Smørgrav 	struct slabhash table;
59b7579f77SDag-Erling Smørgrav };
60b7579f77SDag-Erling Smørgrav 
61b7579f77SDag-Erling Smørgrav /**
62b7579f77SDag-Erling Smørgrav  * Create rrset cache
63b7579f77SDag-Erling Smørgrav  * @param cfg: config settings or NULL for defaults.
64b7579f77SDag-Erling Smørgrav  * @param alloc: initial default rrset key allocation.
65b7579f77SDag-Erling Smørgrav  * @return: NULL on error.
66b7579f77SDag-Erling Smørgrav  */
67b7579f77SDag-Erling Smørgrav struct rrset_cache* rrset_cache_create(struct config_file* cfg,
68b7579f77SDag-Erling Smørgrav 	struct alloc_cache* alloc);
69b7579f77SDag-Erling Smørgrav 
70b7579f77SDag-Erling Smørgrav /**
71b7579f77SDag-Erling Smørgrav  * Delete rrset cache
72b7579f77SDag-Erling Smørgrav  * @param r: rrset cache to delete.
73b7579f77SDag-Erling Smørgrav  */
74b7579f77SDag-Erling Smørgrav void rrset_cache_delete(struct rrset_cache* r);
75b7579f77SDag-Erling Smørgrav 
76b7579f77SDag-Erling Smørgrav /**
77b7579f77SDag-Erling Smørgrav  * Adjust settings of the cache to settings from the config file.
78b7579f77SDag-Erling Smørgrav  * May purge the cache. May recreate the cache.
79b7579f77SDag-Erling Smørgrav  * There may be no threading or use by other threads.
80b7579f77SDag-Erling Smørgrav  * @param r: rrset cache to adjust (like realloc).
81b7579f77SDag-Erling Smørgrav  * @param cfg: config settings or NULL for defaults.
82b7579f77SDag-Erling Smørgrav  * @param alloc: initial default rrset key allocation.
83b7579f77SDag-Erling Smørgrav  * @return 0 on error, or new rrset cache pointer on success.
84b7579f77SDag-Erling Smørgrav  */
85b7579f77SDag-Erling Smørgrav struct rrset_cache* rrset_cache_adjust(struct rrset_cache* r,
86b7579f77SDag-Erling Smørgrav 	struct config_file* cfg, struct alloc_cache* alloc);
87b7579f77SDag-Erling Smørgrav 
88b7579f77SDag-Erling Smørgrav /**
89b7579f77SDag-Erling Smørgrav  * Touch rrset, with given pointer and id.
90b7579f77SDag-Erling Smørgrav  * Caller may not hold a lock on ANY rrset, this could give deadlock.
91b7579f77SDag-Erling Smørgrav  *
92b7579f77SDag-Erling Smørgrav  * This routine is faster than a hashtable lookup:
93b7579f77SDag-Erling Smørgrav  *	o no bin_lock is acquired.
94b7579f77SDag-Erling Smørgrav  *	o no walk through the bin-overflow-list.
95b7579f77SDag-Erling Smørgrav  *	o no comparison of the entry key to find it.
96b7579f77SDag-Erling Smørgrav  *
97b7579f77SDag-Erling Smørgrav  * @param r: rrset cache.
98b7579f77SDag-Erling Smørgrav  * @param key: rrset key. Marked recently used (if it was not deleted
99b7579f77SDag-Erling Smørgrav  *	before the lock is acquired, in that case nothing happens).
100b7579f77SDag-Erling Smørgrav  * @param hash: hash value of the item. Please read it from the key when
101b7579f77SDag-Erling Smørgrav  *	you have it locked. Used to find slab from slabhash.
102b7579f77SDag-Erling Smørgrav  * @param id: used to check that the item is unchanged and not deleted.
103b7579f77SDag-Erling Smørgrav  */
104b7579f77SDag-Erling Smørgrav void rrset_cache_touch(struct rrset_cache* r, struct ub_packed_rrset_key* key,
1053005e0a3SDag-Erling Smørgrav 	hashvalue_type hash, rrset_id_type id);
106b7579f77SDag-Erling Smørgrav 
107b7579f77SDag-Erling Smørgrav /**
108b7579f77SDag-Erling Smørgrav  * Update an rrset in the rrset cache. Stores the information for later use.
109b7579f77SDag-Erling Smørgrav  * Will lookup if the rrset is in the cache and perform an update if necessary.
110b7579f77SDag-Erling Smørgrav  * If the item was present, and superior, references are returned to that.
111b7579f77SDag-Erling Smørgrav  * The passed item is then deallocated with rrset_parsedelete.
112b7579f77SDag-Erling Smørgrav  *
113b7579f77SDag-Erling Smørgrav  * A superior rrset is:
114b7579f77SDag-Erling Smørgrav  *	o rrset with better trust value.
115b7579f77SDag-Erling Smørgrav  *	o same trust value, different rdata, newly passed rrset is inserted.
116b7579f77SDag-Erling Smørgrav  * If rdata is the same, TTL in the cache is updated.
117b7579f77SDag-Erling Smørgrav  *
118b7579f77SDag-Erling Smørgrav  * @param r: the rrset cache.
119b7579f77SDag-Erling Smørgrav  * @param ref: reference (ptr and id) to the rrset. Pass reference setup for
120b7579f77SDag-Erling Smørgrav  *	the new rrset. The reference may be changed if the cached rrset is
121b7579f77SDag-Erling Smørgrav  *	superior.
122b7579f77SDag-Erling Smørgrav  *	Before calling the rrset is presumed newly allocated and changeable.
12324e36522SCy Schubert  *	After calling you do not hold a lock, and the rrset is inserted in
124b7579f77SDag-Erling Smørgrav  *	the hashtable so you need a lock to change it.
125b7579f77SDag-Erling Smørgrav  * @param alloc: how to allocate (and deallocate) the special rrset key.
126b7579f77SDag-Erling Smørgrav  * @param timenow: current time (to see if ttl in cache is expired).
127b7579f77SDag-Erling Smørgrav  * @return: true if the passed reference is updated, false if it is unchanged.
128b7579f77SDag-Erling Smørgrav  * 	0: reference unchanged, inserted in cache.
129b7579f77SDag-Erling Smørgrav  * 	1: reference updated, item is inserted in cache.
130b7579f77SDag-Erling Smørgrav  * 	2: reference updated, item in cache is considered superior.
131b7579f77SDag-Erling Smørgrav  *	   also the rdata is equal (but other parameters in cache are superior).
132b7579f77SDag-Erling Smørgrav  */
133b7579f77SDag-Erling Smørgrav int rrset_cache_update(struct rrset_cache* r, struct rrset_ref* ref,
13417d15b25SDag-Erling Smørgrav 	struct alloc_cache* alloc, time_t timenow);
135b7579f77SDag-Erling Smørgrav 
136b7579f77SDag-Erling Smørgrav /**
13757bddd21SDag-Erling Smørgrav  * Update or add an rrset in the rrset cache using a wildcard dname.
13857bddd21SDag-Erling Smørgrav  * Generates wildcard dname by prepending the wildcard label to the closest
13957bddd21SDag-Erling Smørgrav  * encloser. Will lookup if the rrset is in the cache and perform an update if
14057bddd21SDag-Erling Smørgrav  * necessary.
14157bddd21SDag-Erling Smørgrav  *
14257bddd21SDag-Erling Smørgrav  * @param rrset_cache: the rrset cache.
14357bddd21SDag-Erling Smørgrav  * @param rrset: which rrset to cache as wildcard. This rrset is left
14457bddd21SDag-Erling Smørgrav  * 	untouched.
14557bddd21SDag-Erling Smørgrav  * @param ce: the closest encloser, will be uses to generate the wildcard dname.
14624e36522SCy Schubert  * @param ce_len: the closest encloser length.
14757bddd21SDag-Erling Smørgrav  * @param alloc: how to allocate (and deallocate) the special rrset key.
14857bddd21SDag-Erling Smørgrav  * @param timenow: current time (to see if ttl in cache is expired).
14957bddd21SDag-Erling Smørgrav  */
15057bddd21SDag-Erling Smørgrav void rrset_cache_update_wildcard(struct rrset_cache* rrset_cache,
15157bddd21SDag-Erling Smørgrav 	struct ub_packed_rrset_key* rrset, uint8_t* ce, size_t ce_len,
15257bddd21SDag-Erling Smørgrav 	struct alloc_cache* alloc, time_t timenow);
15357bddd21SDag-Erling Smørgrav 
15457bddd21SDag-Erling Smørgrav /**
155b7579f77SDag-Erling Smørgrav  * Lookup rrset. You obtain read/write lock. You must unlock before lookup
156b7579f77SDag-Erling Smørgrav  * anything of else.
157b7579f77SDag-Erling Smørgrav  * @param r: the rrset cache.
158b7579f77SDag-Erling Smørgrav  * @param qname: name of rrset to lookup.
159b7579f77SDag-Erling Smørgrav  * @param qnamelen: length of name of rrset to lookup.
160b7579f77SDag-Erling Smørgrav  * @param qtype: type of rrset to lookup (host order).
161b7579f77SDag-Erling Smørgrav  * @param qclass: class of rrset to lookup (host order).
162b7579f77SDag-Erling Smørgrav  * @param flags: rrset flags, or 0.
163b7579f77SDag-Erling Smørgrav  * @param timenow: used to compare with TTL.
164b7579f77SDag-Erling Smørgrav  * @param wr: set true to get writelock.
165b7579f77SDag-Erling Smørgrav  * @return packed rrset key pointer. Remember to unlock the key.entry.lock.
166b7579f77SDag-Erling Smørgrav  * 	or NULL if could not be found or it was timed out.
167b7579f77SDag-Erling Smørgrav  */
168b7579f77SDag-Erling Smørgrav struct ub_packed_rrset_key* rrset_cache_lookup(struct rrset_cache* r,
169b7579f77SDag-Erling Smørgrav 	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
17017d15b25SDag-Erling Smørgrav 	uint32_t flags, time_t timenow, int wr);
171b7579f77SDag-Erling Smørgrav 
172b7579f77SDag-Erling Smørgrav /**
173b7579f77SDag-Erling Smørgrav  * Obtain readlock on a (sorted) list of rrset references.
174b7579f77SDag-Erling Smørgrav  * Checks TTLs and IDs of the rrsets and rollbacks locking if not Ok.
175b7579f77SDag-Erling Smørgrav  * @param ref: array of rrset references (key pointer and ID value).
176b7579f77SDag-Erling Smørgrav  *	duplicate references are allowed and handled.
177b7579f77SDag-Erling Smørgrav  * @param count: size of array.
178b7579f77SDag-Erling Smørgrav  * @param timenow: used to compare with TTL.
179b7579f77SDag-Erling Smørgrav  * @return true on success, false on a failure, which can be that some
180b7579f77SDag-Erling Smørgrav  * 	RRsets have timed out, or that they do not exist any more, the
181b7579f77SDag-Erling Smørgrav  *	RRsets have been purged from the cache.
182b7579f77SDag-Erling Smørgrav  *	If true, you hold readlocks on all the ref items.
183b7579f77SDag-Erling Smørgrav  */
18417d15b25SDag-Erling Smørgrav int rrset_array_lock(struct rrset_ref* ref, size_t count, time_t timenow);
185b7579f77SDag-Erling Smørgrav 
186b7579f77SDag-Erling Smørgrav /**
187b7579f77SDag-Erling Smørgrav  * Unlock array (sorted) of rrset references.
188b7579f77SDag-Erling Smørgrav  * @param ref: array of rrset references (key pointer and ID value).
189b7579f77SDag-Erling Smørgrav  *	duplicate references are allowed and handled.
190b7579f77SDag-Erling Smørgrav  * @param count: size of array.
191b7579f77SDag-Erling Smørgrav  */
192b7579f77SDag-Erling Smørgrav void rrset_array_unlock(struct rrset_ref* ref, size_t count);
193b7579f77SDag-Erling Smørgrav 
194b7579f77SDag-Erling Smørgrav /**
195b7579f77SDag-Erling Smørgrav  * Unlock array (sorted) of rrset references and at the same time
196b7579f77SDag-Erling Smørgrav  * touch LRU on the rrsets. It needs the scratch region for temporary
197b7579f77SDag-Erling Smørgrav  * storage as it uses the initial locks to obtain hash values.
198b7579f77SDag-Erling Smørgrav  * @param r: the rrset cache. In this cache LRU is updated.
199b7579f77SDag-Erling Smørgrav  * @param scratch: region for temporary storage of hash values.
200b7579f77SDag-Erling Smørgrav  *	if memory allocation fails, the lru touch fails silently,
201b7579f77SDag-Erling Smørgrav  *	but locks are released. memory errors are logged.
202b7579f77SDag-Erling Smørgrav  * @param ref: array of rrset references (key pointer and ID value).
203b7579f77SDag-Erling Smørgrav  *	duplicate references are allowed and handled.
204b7579f77SDag-Erling Smørgrav  * @param count: size of array.
205b7579f77SDag-Erling Smørgrav  */
206b7579f77SDag-Erling Smørgrav void rrset_array_unlock_touch(struct rrset_cache* r, struct regional* scratch,
207b7579f77SDag-Erling Smørgrav 	struct rrset_ref* ref, size_t count);
208b7579f77SDag-Erling Smørgrav 
209b7579f77SDag-Erling Smørgrav /**
210b7579f77SDag-Erling Smørgrav  * Update security status of an rrset. Looks up the rrset.
211b7579f77SDag-Erling Smørgrav  * If found, checks if rdata is equal.
212b7579f77SDag-Erling Smørgrav  * If so, it will update the security, trust and rrset-ttl values.
213b7579f77SDag-Erling Smørgrav  * The values are only updated if security is increased (towards secure).
214b7579f77SDag-Erling Smørgrav  * @param r: the rrset cache.
215b7579f77SDag-Erling Smørgrav  * @param rrset: which rrset to attempt to update. This rrset is left
216b7579f77SDag-Erling Smørgrav  * 	untouched. The rrset in the cache is updated in-place.
217b7579f77SDag-Erling Smørgrav  * @param now: current time.
218b7579f77SDag-Erling Smørgrav  */
219b7579f77SDag-Erling Smørgrav void rrset_update_sec_status(struct rrset_cache* r,
22017d15b25SDag-Erling Smørgrav 	struct ub_packed_rrset_key* rrset, time_t now);
221b7579f77SDag-Erling Smørgrav 
222b7579f77SDag-Erling Smørgrav /**
223b7579f77SDag-Erling Smørgrav  * Looks up security status of an rrset. Looks up the rrset.
224b7579f77SDag-Erling Smørgrav  * If found, checks if rdata is equal, and entry did not expire.
225b7579f77SDag-Erling Smørgrav  * If so, it will update the security, trust and rrset-ttl values.
226b7579f77SDag-Erling Smørgrav  * @param r: the rrset cache.
227b7579f77SDag-Erling Smørgrav  * @param rrset: This rrset may change security status due to the cache.
228b7579f77SDag-Erling Smørgrav  * 	But its status will only improve, towards secure.
229b7579f77SDag-Erling Smørgrav  * @param now: current time.
230b7579f77SDag-Erling Smørgrav  */
231b7579f77SDag-Erling Smørgrav void rrset_check_sec_status(struct rrset_cache* r,
23217d15b25SDag-Erling Smørgrav 	struct ub_packed_rrset_key* rrset, time_t now);
233b7579f77SDag-Erling Smørgrav 
234b7579f77SDag-Erling Smørgrav /**
235*335c7cdaSCy Schubert  * Removes rrsets above the qname, returns upper qname.
236*335c7cdaSCy Schubert  * @param r: the rrset cache.
237*335c7cdaSCy Schubert  * @param qname: the start qname, also used as the output.
238*335c7cdaSCy Schubert  * @param qnamelen: length of qname, updated when it returns.
239*335c7cdaSCy Schubert  * @param searchtype: qtype to search for.
240*335c7cdaSCy Schubert  * @param qclass: qclass to search for.
241*335c7cdaSCy Schubert  * @param now: current time.
242*335c7cdaSCy Schubert  * @param qnametop: the top qname to stop removal (it is not removed).
243*335c7cdaSCy Schubert  * @param qnametoplen: length of qnametop.
244*335c7cdaSCy Schubert  */
245*335c7cdaSCy Schubert void rrset_cache_remove_above(struct rrset_cache* r, uint8_t** qname,
246*335c7cdaSCy Schubert 	size_t* qnamelen, uint16_t searchtype, uint16_t qclass, time_t now,
247*335c7cdaSCy Schubert 	uint8_t* qnametop, size_t qnametoplen);
248*335c7cdaSCy Schubert 
249*335c7cdaSCy Schubert /**
250*335c7cdaSCy Schubert  * Sees if an rrset is expired above the qname, returns upper qname.
251*335c7cdaSCy Schubert  * @param r: the rrset cache.
252*335c7cdaSCy Schubert  * @param qname: the start qname, also used as the output.
253*335c7cdaSCy Schubert  * @param qnamelen: length of qname, updated when it returns.
254*335c7cdaSCy Schubert  * @param searchtype: qtype to search for.
255*335c7cdaSCy Schubert  * @param qclass: qclass to search for.
256*335c7cdaSCy Schubert  * @param now: current time.
257*335c7cdaSCy Schubert  * @param qnametop: the top qname, don't look farther than that.
258*335c7cdaSCy Schubert  * @param qnametoplen: length of qnametop.
259*335c7cdaSCy Schubert  * @return true if there is an expired rrset above, false otherwise.
260*335c7cdaSCy Schubert  */
261*335c7cdaSCy Schubert int rrset_cache_expired_above(struct rrset_cache* r, uint8_t** qname,
262*335c7cdaSCy Schubert 	size_t* qnamelen, uint16_t searchtype, uint16_t qclass, time_t now,
263*335c7cdaSCy Schubert 	uint8_t* qnametop, size_t qnametoplen);
264*335c7cdaSCy Schubert 
265*335c7cdaSCy Schubert /**
266b7579f77SDag-Erling Smørgrav  * Remove an rrset from the cache, by name and type and flags
267b7579f77SDag-Erling Smørgrav  * @param r: rrset cache
268b7579f77SDag-Erling Smørgrav  * @param nm: name of rrset
269b7579f77SDag-Erling Smørgrav  * @param nmlen: length of name
270b7579f77SDag-Erling Smørgrav  * @param type: type of rrset
271b7579f77SDag-Erling Smørgrav  * @param dclass: class of rrset, host order
272b7579f77SDag-Erling Smørgrav  * @param flags: flags of rrset, host order
273b7579f77SDag-Erling Smørgrav  */
274b7579f77SDag-Erling Smørgrav void rrset_cache_remove(struct rrset_cache* r, uint8_t* nm, size_t nmlen,
275b7579f77SDag-Erling Smørgrav 	uint16_t type, uint16_t dclass, uint32_t flags);
276b7579f77SDag-Erling Smørgrav 
277b7579f77SDag-Erling Smørgrav /** mark rrset to be deleted, set id=0 */
278b7579f77SDag-Erling Smørgrav void rrset_markdel(void* key);
279b7579f77SDag-Erling Smørgrav 
280b7579f77SDag-Erling Smørgrav #endif /* SERVICES_CACHE_RRSET_H */
281