xref: /freebsd/contrib/unbound/services/cache/rrset.h (revision b7579f77d18196a58ff700756c84dc9a302a7f67)
1*b7579f77SDag-Erling Smørgrav /*
2*b7579f77SDag-Erling Smørgrav  * services/cache/rrset.h - Resource record set cache.
3*b7579f77SDag-Erling Smørgrav  *
4*b7579f77SDag-Erling Smørgrav  * Copyright (c) 2007, NLnet Labs. All rights reserved.
5*b7579f77SDag-Erling Smørgrav  *
6*b7579f77SDag-Erling Smørgrav  * This software is open source.
7*b7579f77SDag-Erling Smørgrav  *
8*b7579f77SDag-Erling Smørgrav  * Redistribution and use in source and binary forms, with or without
9*b7579f77SDag-Erling Smørgrav  * modification, are permitted provided that the following conditions
10*b7579f77SDag-Erling Smørgrav  * are met:
11*b7579f77SDag-Erling Smørgrav  *
12*b7579f77SDag-Erling Smørgrav  * Redistributions of source code must retain the above copyright notice,
13*b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer.
14*b7579f77SDag-Erling Smørgrav  *
15*b7579f77SDag-Erling Smørgrav  * Redistributions in binary form must reproduce the above copyright notice,
16*b7579f77SDag-Erling Smørgrav  * this list of conditions and the following disclaimer in the documentation
17*b7579f77SDag-Erling Smørgrav  * and/or other materials provided with the distribution.
18*b7579f77SDag-Erling Smørgrav  *
19*b7579f77SDag-Erling Smørgrav  * Neither the name of the NLNET LABS nor the names of its contributors may
20*b7579f77SDag-Erling Smørgrav  * be used to endorse or promote products derived from this software without
21*b7579f77SDag-Erling Smørgrav  * specific prior written permission.
22*b7579f77SDag-Erling Smørgrav  *
23*b7579f77SDag-Erling Smørgrav  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24*b7579f77SDag-Erling Smørgrav  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25*b7579f77SDag-Erling Smørgrav  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26*b7579f77SDag-Erling Smørgrav  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
27*b7579f77SDag-Erling Smørgrav  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28*b7579f77SDag-Erling Smørgrav  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29*b7579f77SDag-Erling Smørgrav  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30*b7579f77SDag-Erling Smørgrav  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31*b7579f77SDag-Erling Smørgrav  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32*b7579f77SDag-Erling Smørgrav  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33*b7579f77SDag-Erling Smørgrav  * POSSIBILITY OF SUCH DAMAGE.
34*b7579f77SDag-Erling Smørgrav  */
35*b7579f77SDag-Erling Smørgrav 
36*b7579f77SDag-Erling Smørgrav /**
37*b7579f77SDag-Erling Smørgrav  * \file
38*b7579f77SDag-Erling Smørgrav  *
39*b7579f77SDag-Erling Smørgrav  * This file contains the rrset cache.
40*b7579f77SDag-Erling Smørgrav  */
41*b7579f77SDag-Erling Smørgrav 
42*b7579f77SDag-Erling Smørgrav #ifndef SERVICES_CACHE_RRSET_H
43*b7579f77SDag-Erling Smørgrav #define SERVICES_CACHE_RRSET_H
44*b7579f77SDag-Erling Smørgrav #include "util/storage/lruhash.h"
45*b7579f77SDag-Erling Smørgrav #include "util/storage/slabhash.h"
46*b7579f77SDag-Erling Smørgrav #include "util/data/packed_rrset.h"
47*b7579f77SDag-Erling Smørgrav struct config_file;
48*b7579f77SDag-Erling Smørgrav struct alloc_cache;
49*b7579f77SDag-Erling Smørgrav struct rrset_ref;
50*b7579f77SDag-Erling Smørgrav struct regional;
51*b7579f77SDag-Erling Smørgrav 
52*b7579f77SDag-Erling Smørgrav /**
53*b7579f77SDag-Erling Smørgrav  * The rrset cache
54*b7579f77SDag-Erling Smørgrav  * Thin wrapper around hashtable, like a typedef.
55*b7579f77SDag-Erling Smørgrav  */
56*b7579f77SDag-Erling Smørgrav struct rrset_cache {
57*b7579f77SDag-Erling Smørgrav 	/** uses partitioned hash table */
58*b7579f77SDag-Erling Smørgrav 	struct slabhash table;
59*b7579f77SDag-Erling Smørgrav };
60*b7579f77SDag-Erling Smørgrav 
61*b7579f77SDag-Erling Smørgrav /**
62*b7579f77SDag-Erling Smørgrav  * Create rrset cache
63*b7579f77SDag-Erling Smørgrav  * @param cfg: config settings or NULL for defaults.
64*b7579f77SDag-Erling Smørgrav  * @param alloc: initial default rrset key allocation.
65*b7579f77SDag-Erling Smørgrav  * @return: NULL on error.
66*b7579f77SDag-Erling Smørgrav  */
67*b7579f77SDag-Erling Smørgrav struct rrset_cache* rrset_cache_create(struct config_file* cfg,
68*b7579f77SDag-Erling Smørgrav 	struct alloc_cache* alloc);
69*b7579f77SDag-Erling Smørgrav 
70*b7579f77SDag-Erling Smørgrav /**
71*b7579f77SDag-Erling Smørgrav  * Delete rrset cache
72*b7579f77SDag-Erling Smørgrav  * @param r: rrset cache to delete.
73*b7579f77SDag-Erling Smørgrav  */
74*b7579f77SDag-Erling Smørgrav void rrset_cache_delete(struct rrset_cache* r);
75*b7579f77SDag-Erling Smørgrav 
76*b7579f77SDag-Erling Smørgrav /**
77*b7579f77SDag-Erling Smørgrav  * Adjust settings of the cache to settings from the config file.
78*b7579f77SDag-Erling Smørgrav  * May purge the cache. May recreate the cache.
79*b7579f77SDag-Erling Smørgrav  * There may be no threading or use by other threads.
80*b7579f77SDag-Erling Smørgrav  * @param r: rrset cache to adjust (like realloc).
81*b7579f77SDag-Erling Smørgrav  * @param cfg: config settings or NULL for defaults.
82*b7579f77SDag-Erling Smørgrav  * @param alloc: initial default rrset key allocation.
83*b7579f77SDag-Erling Smørgrav  * @return 0 on error, or new rrset cache pointer on success.
84*b7579f77SDag-Erling Smørgrav  */
85*b7579f77SDag-Erling Smørgrav struct rrset_cache* rrset_cache_adjust(struct rrset_cache* r,
86*b7579f77SDag-Erling Smørgrav 	struct config_file* cfg, struct alloc_cache* alloc);
87*b7579f77SDag-Erling Smørgrav 
88*b7579f77SDag-Erling Smørgrav /**
89*b7579f77SDag-Erling Smørgrav  * Touch rrset, with given pointer and id.
90*b7579f77SDag-Erling Smørgrav  * Caller may not hold a lock on ANY rrset, this could give deadlock.
91*b7579f77SDag-Erling Smørgrav  *
92*b7579f77SDag-Erling Smørgrav  * This routine is faster than a hashtable lookup:
93*b7579f77SDag-Erling Smørgrav  *	o no bin_lock is acquired.
94*b7579f77SDag-Erling Smørgrav  *	o no walk through the bin-overflow-list.
95*b7579f77SDag-Erling Smørgrav  *	o no comparison of the entry key to find it.
96*b7579f77SDag-Erling Smørgrav  *
97*b7579f77SDag-Erling Smørgrav  * @param r: rrset cache.
98*b7579f77SDag-Erling Smørgrav  * @param key: rrset key. Marked recently used (if it was not deleted
99*b7579f77SDag-Erling Smørgrav  *	before the lock is acquired, in that case nothing happens).
100*b7579f77SDag-Erling Smørgrav  * @param hash: hash value of the item. Please read it from the key when
101*b7579f77SDag-Erling Smørgrav  *	you have it locked. Used to find slab from slabhash.
102*b7579f77SDag-Erling Smørgrav  * @param id: used to check that the item is unchanged and not deleted.
103*b7579f77SDag-Erling Smørgrav  */
104*b7579f77SDag-Erling Smørgrav void rrset_cache_touch(struct rrset_cache* r, struct ub_packed_rrset_key* key,
105*b7579f77SDag-Erling Smørgrav 	hashvalue_t hash, rrset_id_t id);
106*b7579f77SDag-Erling Smørgrav 
107*b7579f77SDag-Erling Smørgrav /**
108*b7579f77SDag-Erling Smørgrav  * Update an rrset in the rrset cache. Stores the information for later use.
109*b7579f77SDag-Erling Smørgrav  * Will lookup if the rrset is in the cache and perform an update if necessary.
110*b7579f77SDag-Erling Smørgrav  * If the item was present, and superior, references are returned to that.
111*b7579f77SDag-Erling Smørgrav  * The passed item is then deallocated with rrset_parsedelete.
112*b7579f77SDag-Erling Smørgrav  *
113*b7579f77SDag-Erling Smørgrav  * A superior rrset is:
114*b7579f77SDag-Erling Smørgrav  *	o rrset with better trust value.
115*b7579f77SDag-Erling Smørgrav  *	o same trust value, different rdata, newly passed rrset is inserted.
116*b7579f77SDag-Erling Smørgrav  * If rdata is the same, TTL in the cache is updated.
117*b7579f77SDag-Erling Smørgrav  *
118*b7579f77SDag-Erling Smørgrav  * @param r: the rrset cache.
119*b7579f77SDag-Erling Smørgrav  * @param ref: reference (ptr and id) to the rrset. Pass reference setup for
120*b7579f77SDag-Erling Smørgrav  *	the new rrset. The reference may be changed if the cached rrset is
121*b7579f77SDag-Erling Smørgrav  *	superior.
122*b7579f77SDag-Erling Smørgrav  *	Before calling the rrset is presumed newly allocated and changeable.
123*b7579f77SDag-Erling Smørgrav  *	Afer calling you do not hold a lock, and the rrset is inserted in
124*b7579f77SDag-Erling Smørgrav  *	the hashtable so you need a lock to change it.
125*b7579f77SDag-Erling Smørgrav  * @param alloc: how to allocate (and deallocate) the special rrset key.
126*b7579f77SDag-Erling Smørgrav  * @param timenow: current time (to see if ttl in cache is expired).
127*b7579f77SDag-Erling Smørgrav  * @return: true if the passed reference is updated, false if it is unchanged.
128*b7579f77SDag-Erling Smørgrav  * 	0: reference unchanged, inserted in cache.
129*b7579f77SDag-Erling Smørgrav  * 	1: reference updated, item is inserted in cache.
130*b7579f77SDag-Erling Smørgrav  * 	2: reference updated, item in cache is considered superior.
131*b7579f77SDag-Erling Smørgrav  *	   also the rdata is equal (but other parameters in cache are superior).
132*b7579f77SDag-Erling Smørgrav  */
133*b7579f77SDag-Erling Smørgrav int rrset_cache_update(struct rrset_cache* r, struct rrset_ref* ref,
134*b7579f77SDag-Erling Smørgrav 	struct alloc_cache* alloc, uint32_t timenow);
135*b7579f77SDag-Erling Smørgrav 
136*b7579f77SDag-Erling Smørgrav /**
137*b7579f77SDag-Erling Smørgrav  * Lookup rrset. You obtain read/write lock. You must unlock before lookup
138*b7579f77SDag-Erling Smørgrav  * anything of else.
139*b7579f77SDag-Erling Smørgrav  * @param r: the rrset cache.
140*b7579f77SDag-Erling Smørgrav  * @param qname: name of rrset to lookup.
141*b7579f77SDag-Erling Smørgrav  * @param qnamelen: length of name of rrset to lookup.
142*b7579f77SDag-Erling Smørgrav  * @param qtype: type of rrset to lookup (host order).
143*b7579f77SDag-Erling Smørgrav  * @param qclass: class of rrset to lookup (host order).
144*b7579f77SDag-Erling Smørgrav  * @param flags: rrset flags, or 0.
145*b7579f77SDag-Erling Smørgrav  * @param timenow: used to compare with TTL.
146*b7579f77SDag-Erling Smørgrav  * @param wr: set true to get writelock.
147*b7579f77SDag-Erling Smørgrav  * @return packed rrset key pointer. Remember to unlock the key.entry.lock.
148*b7579f77SDag-Erling Smørgrav  * 	or NULL if could not be found or it was timed out.
149*b7579f77SDag-Erling Smørgrav  */
150*b7579f77SDag-Erling Smørgrav struct ub_packed_rrset_key* rrset_cache_lookup(struct rrset_cache* r,
151*b7579f77SDag-Erling Smørgrav 	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
152*b7579f77SDag-Erling Smørgrav 	uint32_t flags, uint32_t timenow, int wr);
153*b7579f77SDag-Erling Smørgrav 
154*b7579f77SDag-Erling Smørgrav /**
155*b7579f77SDag-Erling Smørgrav  * Obtain readlock on a (sorted) list of rrset references.
156*b7579f77SDag-Erling Smørgrav  * Checks TTLs and IDs of the rrsets and rollbacks locking if not Ok.
157*b7579f77SDag-Erling Smørgrav  * @param ref: array of rrset references (key pointer and ID value).
158*b7579f77SDag-Erling Smørgrav  *	duplicate references are allowed and handled.
159*b7579f77SDag-Erling Smørgrav  * @param count: size of array.
160*b7579f77SDag-Erling Smørgrav  * @param timenow: used to compare with TTL.
161*b7579f77SDag-Erling Smørgrav  * @return true on success, false on a failure, which can be that some
162*b7579f77SDag-Erling Smørgrav  * 	RRsets have timed out, or that they do not exist any more, the
163*b7579f77SDag-Erling Smørgrav  *	RRsets have been purged from the cache.
164*b7579f77SDag-Erling Smørgrav  *	If true, you hold readlocks on all the ref items.
165*b7579f77SDag-Erling Smørgrav  */
166*b7579f77SDag-Erling Smørgrav int rrset_array_lock(struct rrset_ref* ref, size_t count, uint32_t timenow);
167*b7579f77SDag-Erling Smørgrav 
168*b7579f77SDag-Erling Smørgrav /**
169*b7579f77SDag-Erling Smørgrav  * Unlock array (sorted) of rrset references.
170*b7579f77SDag-Erling Smørgrav  * @param ref: array of rrset references (key pointer and ID value).
171*b7579f77SDag-Erling Smørgrav  *	duplicate references are allowed and handled.
172*b7579f77SDag-Erling Smørgrav  * @param count: size of array.
173*b7579f77SDag-Erling Smørgrav  */
174*b7579f77SDag-Erling Smørgrav void rrset_array_unlock(struct rrset_ref* ref, size_t count);
175*b7579f77SDag-Erling Smørgrav 
176*b7579f77SDag-Erling Smørgrav /**
177*b7579f77SDag-Erling Smørgrav  * Unlock array (sorted) of rrset references and at the same time
178*b7579f77SDag-Erling Smørgrav  * touch LRU on the rrsets. It needs the scratch region for temporary
179*b7579f77SDag-Erling Smørgrav  * storage as it uses the initial locks to obtain hash values.
180*b7579f77SDag-Erling Smørgrav  * @param r: the rrset cache. In this cache LRU is updated.
181*b7579f77SDag-Erling Smørgrav  * @param scratch: region for temporary storage of hash values.
182*b7579f77SDag-Erling Smørgrav  *	if memory allocation fails, the lru touch fails silently,
183*b7579f77SDag-Erling Smørgrav  *	but locks are released. memory errors are logged.
184*b7579f77SDag-Erling Smørgrav  * @param ref: array of rrset references (key pointer and ID value).
185*b7579f77SDag-Erling Smørgrav  *	duplicate references are allowed and handled.
186*b7579f77SDag-Erling Smørgrav  * @param count: size of array.
187*b7579f77SDag-Erling Smørgrav  */
188*b7579f77SDag-Erling Smørgrav void rrset_array_unlock_touch(struct rrset_cache* r, struct regional* scratch,
189*b7579f77SDag-Erling Smørgrav 	struct rrset_ref* ref, size_t count);
190*b7579f77SDag-Erling Smørgrav 
191*b7579f77SDag-Erling Smørgrav /**
192*b7579f77SDag-Erling Smørgrav  * Update security status of an rrset. Looks up the rrset.
193*b7579f77SDag-Erling Smørgrav  * If found, checks if rdata is equal.
194*b7579f77SDag-Erling Smørgrav  * If so, it will update the security, trust and rrset-ttl values.
195*b7579f77SDag-Erling Smørgrav  * The values are only updated if security is increased (towards secure).
196*b7579f77SDag-Erling Smørgrav  * @param r: the rrset cache.
197*b7579f77SDag-Erling Smørgrav  * @param rrset: which rrset to attempt to update. This rrset is left
198*b7579f77SDag-Erling Smørgrav  * 	untouched. The rrset in the cache is updated in-place.
199*b7579f77SDag-Erling Smørgrav  * @param now: current time.
200*b7579f77SDag-Erling Smørgrav  */
201*b7579f77SDag-Erling Smørgrav void rrset_update_sec_status(struct rrset_cache* r,
202*b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key* rrset, uint32_t now);
203*b7579f77SDag-Erling Smørgrav 
204*b7579f77SDag-Erling Smørgrav /**
205*b7579f77SDag-Erling Smørgrav  * Looks up security status of an rrset. Looks up the rrset.
206*b7579f77SDag-Erling Smørgrav  * If found, checks if rdata is equal, and entry did not expire.
207*b7579f77SDag-Erling Smørgrav  * If so, it will update the security, trust and rrset-ttl values.
208*b7579f77SDag-Erling Smørgrav  * @param r: the rrset cache.
209*b7579f77SDag-Erling Smørgrav  * @param rrset: This rrset may change security status due to the cache.
210*b7579f77SDag-Erling Smørgrav  * 	But its status will only improve, towards secure.
211*b7579f77SDag-Erling Smørgrav  * @param now: current time.
212*b7579f77SDag-Erling Smørgrav  */
213*b7579f77SDag-Erling Smørgrav void rrset_check_sec_status(struct rrset_cache* r,
214*b7579f77SDag-Erling Smørgrav 	struct ub_packed_rrset_key* rrset, uint32_t now);
215*b7579f77SDag-Erling Smørgrav 
216*b7579f77SDag-Erling Smørgrav /**
217*b7579f77SDag-Erling Smørgrav  * Remove an rrset from the cache, by name and type and flags
218*b7579f77SDag-Erling Smørgrav  * @param r: rrset cache
219*b7579f77SDag-Erling Smørgrav  * @param nm: name of rrset
220*b7579f77SDag-Erling Smørgrav  * @param nmlen: length of name
221*b7579f77SDag-Erling Smørgrav  * @param type: type of rrset
222*b7579f77SDag-Erling Smørgrav  * @param dclass: class of rrset, host order
223*b7579f77SDag-Erling Smørgrav  * @param flags: flags of rrset, host order
224*b7579f77SDag-Erling Smørgrav  */
225*b7579f77SDag-Erling Smørgrav void rrset_cache_remove(struct rrset_cache* r, uint8_t* nm, size_t nmlen,
226*b7579f77SDag-Erling Smørgrav 	uint16_t type, uint16_t dclass, uint32_t flags);
227*b7579f77SDag-Erling Smørgrav 
228*b7579f77SDag-Erling Smørgrav /** mark rrset to be deleted, set id=0 */
229*b7579f77SDag-Erling Smørgrav void rrset_markdel(void* key);
230*b7579f77SDag-Erling Smørgrav 
231*b7579f77SDag-Erling Smørgrav #endif /* SERVICES_CACHE_RRSET_H */
232