1238106Sdes/*
2238106Sdes * services/cache/rrset.h - Resource record set cache.
3238106Sdes *
4238106Sdes * Copyright (c) 2007, NLnet Labs. All rights reserved.
5238106Sdes *
6238106Sdes * This software is open source.
7238106Sdes *
8238106Sdes * Redistribution and use in source and binary forms, with or without
9238106Sdes * modification, are permitted provided that the following conditions
10238106Sdes * are met:
11238106Sdes *
12238106Sdes * Redistributions of source code must retain the above copyright notice,
13238106Sdes * this list of conditions and the following disclaimer.
14238106Sdes *
15238106Sdes * Redistributions in binary form must reproduce the above copyright notice,
16238106Sdes * this list of conditions and the following disclaimer in the documentation
17238106Sdes * and/or other materials provided with the distribution.
18238106Sdes *
19238106Sdes * Neither the name of the NLNET LABS nor the names of its contributors may
20238106Sdes * be used to endorse or promote products derived from this software without
21238106Sdes * specific prior written permission.
22238106Sdes *
23238106Sdes * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24269257Sdes * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25269257Sdes * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26269257Sdes * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27269257Sdes * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28269257Sdes * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29269257Sdes * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30269257Sdes * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31269257Sdes * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32269257Sdes * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33269257Sdes * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34238106Sdes */
35238106Sdes
36238106Sdes/**
37238106Sdes * \file
38238106Sdes *
39238106Sdes * This file contains the rrset cache.
40238106Sdes */
41238106Sdes
42238106Sdes#ifndef SERVICES_CACHE_RRSET_H
43238106Sdes#define SERVICES_CACHE_RRSET_H
44238106Sdes#include "util/storage/lruhash.h"
45238106Sdes#include "util/storage/slabhash.h"
46238106Sdes#include "util/data/packed_rrset.h"
47238106Sdesstruct config_file;
48238106Sdesstruct alloc_cache;
49238106Sdesstruct rrset_ref;
50238106Sdesstruct regional;
51238106Sdes
52238106Sdes/**
53238106Sdes * The rrset cache
54238106Sdes * Thin wrapper around hashtable, like a typedef.
55238106Sdes */
56238106Sdesstruct rrset_cache {
57238106Sdes	/** uses partitioned hash table */
58238106Sdes	struct slabhash table;
59238106Sdes};
60238106Sdes
61238106Sdes/**
62238106Sdes * Create rrset cache
63238106Sdes * @param cfg: config settings or NULL for defaults.
64238106Sdes * @param alloc: initial default rrset key allocation.
65238106Sdes * @return: NULL on error.
66238106Sdes */
67238106Sdesstruct rrset_cache* rrset_cache_create(struct config_file* cfg,
68238106Sdes	struct alloc_cache* alloc);
69238106Sdes
70238106Sdes/**
71238106Sdes * Delete rrset cache
72238106Sdes * @param r: rrset cache to delete.
73238106Sdes */
74238106Sdesvoid rrset_cache_delete(struct rrset_cache* r);
75238106Sdes
76238106Sdes/**
77238106Sdes * Adjust settings of the cache to settings from the config file.
78238106Sdes * May purge the cache. May recreate the cache.
79238106Sdes * There may be no threading or use by other threads.
80238106Sdes * @param r: rrset cache to adjust (like realloc).
81238106Sdes * @param cfg: config settings or NULL for defaults.
82238106Sdes * @param alloc: initial default rrset key allocation.
83238106Sdes * @return 0 on error, or new rrset cache pointer on success.
84238106Sdes */
85238106Sdesstruct rrset_cache* rrset_cache_adjust(struct rrset_cache* r,
86238106Sdes	struct config_file* cfg, struct alloc_cache* alloc);
87238106Sdes
88238106Sdes/**
89238106Sdes * Touch rrset, with given pointer and id.
90238106Sdes * Caller may not hold a lock on ANY rrset, this could give deadlock.
91238106Sdes *
92238106Sdes * This routine is faster than a hashtable lookup:
93238106Sdes *	o no bin_lock is acquired.
94238106Sdes *	o no walk through the bin-overflow-list.
95238106Sdes *	o no comparison of the entry key to find it.
96238106Sdes *
97238106Sdes * @param r: rrset cache.
98238106Sdes * @param key: rrset key. Marked recently used (if it was not deleted
99238106Sdes *	before the lock is acquired, in that case nothing happens).
100238106Sdes * @param hash: hash value of the item. Please read it from the key when
101238106Sdes *	you have it locked. Used to find slab from slabhash.
102238106Sdes * @param id: used to check that the item is unchanged and not deleted.
103238106Sdes */
104238106Sdesvoid rrset_cache_touch(struct rrset_cache* r, struct ub_packed_rrset_key* key,
105238106Sdes	hashvalue_t hash, rrset_id_t id);
106238106Sdes
107238106Sdes/**
108238106Sdes * Update an rrset in the rrset cache. Stores the information for later use.
109238106Sdes * Will lookup if the rrset is in the cache and perform an update if necessary.
110238106Sdes * If the item was present, and superior, references are returned to that.
111238106Sdes * The passed item is then deallocated with rrset_parsedelete.
112238106Sdes *
113238106Sdes * A superior rrset is:
114238106Sdes *	o rrset with better trust value.
115238106Sdes *	o same trust value, different rdata, newly passed rrset is inserted.
116238106Sdes * If rdata is the same, TTL in the cache is updated.
117238106Sdes *
118238106Sdes * @param r: the rrset cache.
119238106Sdes * @param ref: reference (ptr and id) to the rrset. Pass reference setup for
120238106Sdes *	the new rrset. The reference may be changed if the cached rrset is
121238106Sdes *	superior.
122238106Sdes *	Before calling the rrset is presumed newly allocated and changeable.
123238106Sdes *	Afer calling you do not hold a lock, and the rrset is inserted in
124238106Sdes *	the hashtable so you need a lock to change it.
125238106Sdes * @param alloc: how to allocate (and deallocate) the special rrset key.
126238106Sdes * @param timenow: current time (to see if ttl in cache is expired).
127238106Sdes * @return: true if the passed reference is updated, false if it is unchanged.
128238106Sdes * 	0: reference unchanged, inserted in cache.
129238106Sdes * 	1: reference updated, item is inserted in cache.
130238106Sdes * 	2: reference updated, item in cache is considered superior.
131238106Sdes *	   also the rdata is equal (but other parameters in cache are superior).
132238106Sdes */
133238106Sdesint rrset_cache_update(struct rrset_cache* r, struct rrset_ref* ref,
134269257Sdes	struct alloc_cache* alloc, time_t timenow);
135238106Sdes
136238106Sdes/**
137238106Sdes * Lookup rrset. You obtain read/write lock. You must unlock before lookup
138238106Sdes * anything of else.
139238106Sdes * @param r: the rrset cache.
140238106Sdes * @param qname: name of rrset to lookup.
141238106Sdes * @param qnamelen: length of name of rrset to lookup.
142238106Sdes * @param qtype: type of rrset to lookup (host order).
143238106Sdes * @param qclass: class of rrset to lookup (host order).
144238106Sdes * @param flags: rrset flags, or 0.
145238106Sdes * @param timenow: used to compare with TTL.
146238106Sdes * @param wr: set true to get writelock.
147238106Sdes * @return packed rrset key pointer. Remember to unlock the key.entry.lock.
148238106Sdes * 	or NULL if could not be found or it was timed out.
149238106Sdes */
150238106Sdesstruct ub_packed_rrset_key* rrset_cache_lookup(struct rrset_cache* r,
151238106Sdes	uint8_t* qname, size_t qnamelen, uint16_t qtype, uint16_t qclass,
152269257Sdes	uint32_t flags, time_t timenow, int wr);
153238106Sdes
154238106Sdes/**
155238106Sdes * Obtain readlock on a (sorted) list of rrset references.
156238106Sdes * Checks TTLs and IDs of the rrsets and rollbacks locking if not Ok.
157238106Sdes * @param ref: array of rrset references (key pointer and ID value).
158238106Sdes *	duplicate references are allowed and handled.
159238106Sdes * @param count: size of array.
160238106Sdes * @param timenow: used to compare with TTL.
161238106Sdes * @return true on success, false on a failure, which can be that some
162238106Sdes * 	RRsets have timed out, or that they do not exist any more, the
163238106Sdes *	RRsets have been purged from the cache.
164238106Sdes *	If true, you hold readlocks on all the ref items.
165238106Sdes */
166269257Sdesint rrset_array_lock(struct rrset_ref* ref, size_t count, time_t timenow);
167238106Sdes
168238106Sdes/**
169238106Sdes * Unlock array (sorted) of rrset references.
170238106Sdes * @param ref: array of rrset references (key pointer and ID value).
171238106Sdes *	duplicate references are allowed and handled.
172238106Sdes * @param count: size of array.
173238106Sdes */
174238106Sdesvoid rrset_array_unlock(struct rrset_ref* ref, size_t count);
175238106Sdes
176238106Sdes/**
177238106Sdes * Unlock array (sorted) of rrset references and at the same time
178238106Sdes * touch LRU on the rrsets. It needs the scratch region for temporary
179238106Sdes * storage as it uses the initial locks to obtain hash values.
180238106Sdes * @param r: the rrset cache. In this cache LRU is updated.
181238106Sdes * @param scratch: region for temporary storage of hash values.
182238106Sdes *	if memory allocation fails, the lru touch fails silently,
183238106Sdes *	but locks are released. memory errors are logged.
184238106Sdes * @param ref: array of rrset references (key pointer and ID value).
185238106Sdes *	duplicate references are allowed and handled.
186238106Sdes * @param count: size of array.
187238106Sdes */
188238106Sdesvoid rrset_array_unlock_touch(struct rrset_cache* r, struct regional* scratch,
189238106Sdes	struct rrset_ref* ref, size_t count);
190238106Sdes
191238106Sdes/**
192238106Sdes * Update security status of an rrset. Looks up the rrset.
193238106Sdes * If found, checks if rdata is equal.
194238106Sdes * If so, it will update the security, trust and rrset-ttl values.
195238106Sdes * The values are only updated if security is increased (towards secure).
196238106Sdes * @param r: the rrset cache.
197238106Sdes * @param rrset: which rrset to attempt to update. This rrset is left
198238106Sdes * 	untouched. The rrset in the cache is updated in-place.
199238106Sdes * @param now: current time.
200238106Sdes */
201238106Sdesvoid rrset_update_sec_status(struct rrset_cache* r,
202269257Sdes	struct ub_packed_rrset_key* rrset, time_t now);
203238106Sdes
204238106Sdes/**
205238106Sdes * Looks up security status of an rrset. Looks up the rrset.
206238106Sdes * If found, checks if rdata is equal, and entry did not expire.
207238106Sdes * If so, it will update the security, trust and rrset-ttl values.
208238106Sdes * @param r: the rrset cache.
209238106Sdes * @param rrset: This rrset may change security status due to the cache.
210238106Sdes * 	But its status will only improve, towards secure.
211238106Sdes * @param now: current time.
212238106Sdes */
213238106Sdesvoid rrset_check_sec_status(struct rrset_cache* r,
214269257Sdes	struct ub_packed_rrset_key* rrset, time_t now);
215238106Sdes
216238106Sdes/**
217238106Sdes * Remove an rrset from the cache, by name and type and flags
218238106Sdes * @param r: rrset cache
219238106Sdes * @param nm: name of rrset
220238106Sdes * @param nmlen: length of name
221238106Sdes * @param type: type of rrset
222238106Sdes * @param dclass: class of rrset, host order
223238106Sdes * @param flags: flags of rrset, host order
224238106Sdes */
225238106Sdesvoid rrset_cache_remove(struct rrset_cache* r, uint8_t* nm, size_t nmlen,
226238106Sdes	uint16_t type, uint16_t dclass, uint32_t flags);
227238106Sdes
228238106Sdes/** mark rrset to be deleted, set id=0 */
229238106Sdesvoid rrset_markdel(void* key);
230238106Sdes
231238106Sdes#endif /* SERVICES_CACHE_RRSET_H */
232