The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
rlm_cache_rbtree.c
Go to the documentation of this file.
1/*
2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17/**
18 * $Id: bb6fa3eb41f7f54926f74807f05be7ea3a4a51f2 $
19 * @file rlm_cache_rbtree.c
20 * @brief Simple rbtree based cache.
21 *
22 * @copyright 2014 The FreeRADIUS server project
23 */
24#include <freeradius-devel/server/base.h>
25#include <freeradius-devel/util/debug.h>
26#include "../../rlm_cache.h"
27
28typedef struct {
29 fr_rb_tree_t *cache; //!< Tree for looking up cache keys.
30 fr_heap_t *heap; //!< For managing entry expiry.
31
32 pthread_mutex_t mutex; //!< Protect the tree from multiple readers/writers.
34
35typedef struct {
36 rlm_cache_rbtree_mutable_t *mutable; //!< Mutable instance data.
38
39typedef struct {
40 rlm_cache_entry_t fields; //!< Entry data.
41
42 fr_rb_node_t node; //!< Entry used for lookups.
43 fr_heap_index_t heap_id; //!< Offset used for expiry heap.
45
46/** Compare two entries by key
47 *
48 * There may only be one entry with the same key.
49 */
50static int8_t cache_entry_cmp(void const *one, void const *two)
51{
52 rlm_cache_entry_t const *a = one, *b = two;
53
54 return MEMCMP_FIELDS(a, b, key.vb_strvalue, key.vb_length);
55}
56
57/** Compare two entries by expiry time
58 *
59 * There may be multiple entries with the same expiry time.
60 */
61static int8_t cache_heap_cmp(void const *one, void const *two)
62{
63 rlm_cache_entry_t const *a = one, *b = two;
64
65 return fr_unix_time_cmp(a->expires, b->expires);
66}
67
68/** Custom allocation function for the driver
69 *
70 * Allows allocation of cache entry structures with additional fields.
71 *
72 * @copydetails cache_entry_alloc_t
73 */
75 request_t *request)
76{
78
79 c = talloc_zero(NULL, rlm_cache_rb_entry_t);
80 if (!c) {
81 RERROR("Failed allocating cache entry");
82 return NULL;
83 }
84
85 return (rlm_cache_entry_t *)c;
86}
87
88/** Locate a cache entry
89 *
90 * @note handle not used except for sanity checks.
91 *
92 * @copydetails cache_entry_find_t
93 */
95 UNUSED rlm_cache_config_t const *config, void *instance,
96 request_t *request, UNUSED void *handle, fr_value_box_t const *key)
97{
98 rlm_cache_rbtree_t *driver = talloc_get_type_abort(instance, rlm_cache_rbtree_t);
99 rlm_cache_rbtree_mutable_t *mutable = driver->mutable;
100 rlm_cache_entry_t find = {};
101 int i;
103
104 fr_assert(mutable->cache);
105
106 /*
107 * Clear out old entries
108 */
109 for (i = 0; i < 3; i++) {
110 c = fr_heap_peek(mutable->heap);
111 if (!c) break;
112
113 if (fr_unix_time_gt(c->expires, fr_time_to_unix_time(request->packet->timestamp))) break;
114
115 fr_heap_extract(&mutable->heap, c);
116 fr_rb_delete(mutable->cache, c);
117 talloc_free(c);
118 }
119
120 fr_value_box_copy_shallow(NULL, &find.key, key);
121
122 /*
123 * Is there an entry for this key?
124 */
125 c = fr_rb_find(mutable->cache, &find);
126 if (!c) {
127 *out = NULL;
128 return CACHE_MISS;
129 }
130 *out = c;
131
132 return CACHE_OK;
133}
134
135/** Free an entry and remove it from the data store
136 *
137 * @note handle not used except for sanity checks.
138 *
139 * @copydetails cache_entry_expire_t
140 */
142 request_t *request, UNUSED void *handle,
143 fr_value_box_t const *key)
144{
145 rlm_cache_rbtree_t *driver = talloc_get_type_abort(instance, rlm_cache_rbtree_t);
146 rlm_cache_entry_t find = {};
148
149 if (!request) return CACHE_ERROR;
150
151 fr_value_box_copy_shallow(NULL, &find.key, key);
152
153 c = fr_rb_find(driver->mutable->cache, &find);
154 if (!c) return CACHE_MISS;
155
156 fr_heap_extract(&driver->mutable->heap, c);
157 fr_rb_delete(driver->mutable->cache, c);
158 talloc_free(c);
159
160 return CACHE_OK;
161}
162
163/** Insert a new entry into the data store
164 *
165 * @note handle not used except for sanity checks.
166 *
167 * @copydetails cache_entry_insert_t
168 */
170 request_t *request, void *handle,
171 rlm_cache_entry_t const *c)
172{
173 cache_status_t status;
174
175 rlm_cache_rbtree_t *driver = talloc_get_type_abort(instance, rlm_cache_rbtree_t);
176
177 fr_assert(handle == request);
178
179 if (!request) return CACHE_ERROR;
180
181 /*
182 * Allow overwriting
183 */
184 if (!fr_rb_insert(driver->mutable->cache, c)) {
185 status = cache_entry_expire(config, instance, request, handle, &c->key);
186 if ((status != CACHE_OK) && !fr_cond_assert(0)) return CACHE_ERROR;
187
188 if (!fr_rb_insert(driver->mutable->cache, c)) {
189 RERROR("Failed adding entry");
190
191 return CACHE_ERROR;
192 }
193 }
194
195 if (fr_heap_insert(&driver->mutable->heap, UNCONST(rlm_cache_entry_t *, c)) < 0) {
196 fr_rb_delete(driver->mutable->cache, c);
197 RERROR("Failed adding entry to expiry heap");
198
199 return CACHE_ERROR;
200 }
201
202 return CACHE_OK;
203}
204
205/** Update the TTL of an entry
206 *
207 * @note handle not used except for sanity checks.
208 *
209 * @copydetails cache_entry_set_ttl_t
210 */
212 request_t *request, UNUSED void *handle,
214{
215 rlm_cache_rbtree_t *driver = talloc_get_type_abort(instance, rlm_cache_rbtree_t);
216
217#ifdef NDEBUG
218 if (!request) return CACHE_ERROR;
219#endif
220
221 if (!fr_cond_assert(fr_heap_extract(&driver->mutable->heap, c) == 0)) {
222 RERROR("Entry not in heap");
223 return CACHE_ERROR;
224 }
225
226 if (fr_heap_insert(&driver->mutable->heap, c) < 0) {
227 fr_rb_delete(driver->mutable->cache, c); /* make sure we don't leak entries... */
228 RERROR("Failed updating entry TTL. Entry was forcefully expired");
229 return CACHE_ERROR;
230 }
231 return CACHE_OK;
232}
233
234/** Return the number of entries in the cache
235 *
236 * @note handle not used except for sanity checks.
237 *
238 * @copydetails cache_entry_count_t
239 */
240static uint64_t cache_entry_count(UNUSED rlm_cache_config_t const *config, void *instance,
241 request_t *request, UNUSED void *handle)
242{
243 rlm_cache_rbtree_t *driver = talloc_get_type_abort(instance, rlm_cache_rbtree_t);
244
245 if (!request) return 0; /* can't return an error due to signed */
246
247 return fr_rb_num_elements(driver->mutable->cache);
248}
249
250/** Lock the rbtree
251 *
252 * @note handle not used except for sanity checks.
253 *
254 * @copydetails cache_acquire_t
255 */
256static int cache_acquire(void **handle, UNUSED rlm_cache_config_t const *config, void *instance,
257 request_t *request)
258{
259 rlm_cache_rbtree_t *driver = talloc_get_type_abort(instance, rlm_cache_rbtree_t);
260
261 pthread_mutex_lock(&driver->mutable->mutex);
262
263 *handle = request; /* handle is unused, this is just for sanity checking */
264
265 RDEBUG3("Mutex acquired");
266
267 return 0;
268}
269
270/** Release an entry unlocking any mutexes
271 *
272 * @note handle not used except for sanity checks.
273 *
274 * @copydetails cache_release_t
275 */
276static void cache_release(UNUSED rlm_cache_config_t const *config, void *instance, request_t *request,
278{
279 rlm_cache_rbtree_t *driver = talloc_get_type_abort(instance, rlm_cache_rbtree_t);
280
281 pthread_mutex_unlock(&driver->mutable->mutex);
282
283 RDEBUG3("Mutex released");
284}
285
286/** Cleanup a cache_rbtree instance
287 *
288 */
289static int mod_detach(module_detach_ctx_t const *mctx)
290{
291 rlm_cache_rbtree_t *driver = talloc_get_type_abort(mctx->mi->data, rlm_cache_rbtree_t);
292 rlm_cache_rbtree_mutable_t *mutable = driver->mutable;
293
294 if (mutable->cache) {
296 void *data;
297
298 for (data = fr_rb_iter_init_inorder(mutable->cache, &iter);
299 data;
300 data = fr_rb_iter_next_inorder(mutable->cache, &iter)) {
301 fr_rb_iter_delete_inorder(mutable->cache, &iter);
303 }
304 }
305
306 pthread_mutex_destroy(&mutable->mutex);
307
308 TALLOC_FREE(driver->mutable);
309
310 return 0;
311}
312
313/** Create a new cache_rbtree instance
314 *
315 * @param[in] mctx Data required for instantiation.
316 * @return
317 * - 0 on success.
318 * - -1 on failure.
319 */
320static int mod_instantiate(module_inst_ctx_t const *mctx)
321{
322 rlm_cache_rbtree_t *driver = talloc_get_type_abort(mctx->mi->data, rlm_cache_rbtree_t);
324 int ret;
325
326 MEM(mutable = talloc_zero(NULL, rlm_cache_rbtree_mutable_t));
327
328 /*
329 * The cache.
330 */
331 mutable->cache = fr_rb_inline_talloc_alloc(mutable, rlm_cache_rb_entry_t, node, cache_entry_cmp, NULL);
332 if (!mutable->cache) {
333 ERROR("Failed to create cache");
334 error:
335 talloc_free(mutable);
336 return -1;
337 }
338
339 /*
340 * The heap of entries to expire.
341 */
342 mutable->heap = fr_heap_talloc_alloc(mutable, cache_heap_cmp, rlm_cache_rb_entry_t, heap_id, 0);
343 if (!mutable->heap) {
344 ERROR("Failed to create heap for the cache");
345 goto error;
346 }
347
348 if ((ret = pthread_mutex_init(&mutable->mutex, NULL)) < 0) {
349 ERROR("Failed initializing mutex: %s", fr_syserror(ret));
350 goto error;
351 }
352
353 driver->mutable = mutable;
354
355 return 0;
356}
357
360 .common = {
361 .magic = MODULE_MAGIC_INIT,
362 .name = "cache_rbtree",
364 .detach = mod_detach,
365 .inst_size = sizeof(rlm_cache_rbtree_t),
366 .inst_type = "rlm_cache_rbtree_t",
367 },
368 .alloc = cache_entry_alloc,
369
370 .find = cache_entry_find,
371 .insert = cache_entry_insert,
372 .expire = cache_entry_expire,
373 .set_ttl = cache_entry_set_ttl,
374 .count = cache_entry_count,
375
376 .acquire = cache_acquire,
377 .release = cache_release,
378};
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:186
#define UNUSED
Definition build.h:336
#define MEMCMP_FIELDS(_a, _b, _field, _len_field)
Return the comparison of two opaque fields of a structure.
Definition build.h:178
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
Definition debug.h:141
#define MEM(x)
Definition debug.h:46
#define ERROR(fmt,...)
Definition dhcpclient.c:40
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
int fr_heap_insert(fr_heap_t **hp, void *data)
Insert a new element into the heap.
Definition heap.c:146
int fr_heap_extract(fr_heap_t **hp, void *data)
Remove a node from the heap.
Definition heap.c:239
unsigned int fr_heap_index_t
Definition heap.h:80
static void * fr_heap_peek(fr_heap_t *h)
Return the item from the top of the heap but don't pop it.
Definition heap.h:136
#define fr_heap_talloc_alloc(_ctx, _cmp, _talloc_type, _field, _init)
Creates a heap that verifies elements are of a specific talloc type.
Definition heap.h:115
The main heap structure.
Definition heap.h:66
talloc_free(hp)
#define RDEBUG3(fmt,...)
Definition log.h:355
#define RERROR(fmt,...)
Definition log.h:310
module_instance_t * mi
Module instance to detach.
Definition module_ctx.h:57
module_instance_t * mi
Instance of the module being instantiated.
Definition module_ctx.h:51
Temporary structure to hold arguments for detach calls.
Definition module_ctx.h:56
Temporary structure to hold arguments for instantiation calls.
Definition module_ctx.h:50
static const conf_parser_t config[]
Definition base.c:163
#define fr_assert(_expr)
Definition rad_assert.h:37
uint32_t fr_rb_num_elements(fr_rb_tree_t *tree)
Return how many nodes there are in a tree.
Definition rb.c:781
void * fr_rb_iter_init_inorder(fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Initialise an in-order iterator.
Definition rb.c:824
void fr_rb_iter_delete_inorder(fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Remove the current node from the tree.
Definition rb.c:899
void * fr_rb_find(fr_rb_tree_t const *tree, void const *data)
Find an element in the tree, returning the data, not the node.
Definition rb.c:577
bool fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:626
bool fr_rb_delete(fr_rb_tree_t *tree, void const *data)
Remove node and free data (if a free function was specified)
Definition rb.c:741
void * fr_rb_iter_next_inorder(UNUSED fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Return the next node.
Definition rb.c:850
#define fr_rb_inline_talloc_alloc(_ctx, _type, _field, _data_cmp, _data_free)
Allocs a red black that verifies elements are of a specific talloc type.
Definition rb.h:244
Iterator structure for in-order traversal of an rbtree.
Definition rb.h:319
The main red black tree structure.
Definition rb.h:71
fr_value_box_t key
Key used to identify entry.
Definition rlm_cache.h:73
module_t common
Common fields for all loadable modules.
Definition rlm_cache.h:258
cache_status_t
Definition rlm_cache.h:39
@ CACHE_ERROR
Fatal error.
Definition rlm_cache.h:41
@ CACHE_OK
Cache entry found/updated.
Definition rlm_cache.h:42
@ CACHE_MISS
Cache entry notfound.
Definition rlm_cache.h:43
void rlm_cache_handle_t
Definition rlm_cache.h:35
fr_unix_time_t expires
When the entry expires.
Definition rlm_cache.h:76
Configuration for the rlm_cache module.
Definition rlm_cache.h:51
Definition rlm_cache.h:72
static int mod_detach(module_detach_ctx_t const *mctx)
Cleanup a cache_rbtree instance.
static rlm_cache_entry_t * cache_entry_alloc(UNUSED rlm_cache_config_t const *config, UNUSED void *instance, request_t *request)
Custom allocation function for the driver.
rlm_cache_driver_t rlm_cache_rbtree
rlm_cache_rbtree_mutable_t * mutable
Mutable instance data.
fr_rb_tree_t * cache
Tree for looking up cache keys.
pthread_mutex_t mutex
Protect the tree from multiple readers/writers.
static uint64_t cache_entry_count(UNUSED rlm_cache_config_t const *config, void *instance, request_t *request, UNUSED void *handle)
Return the number of entries in the cache.
static int cache_acquire(void **handle, UNUSED rlm_cache_config_t const *config, void *instance, request_t *request)
Lock the rbtree.
fr_heap_index_t heap_id
Offset used for expiry heap.
static void cache_release(UNUSED rlm_cache_config_t const *config, void *instance, request_t *request, UNUSED rlm_cache_handle_t *handle)
Release an entry unlocking any mutexes.
static int8_t cache_heap_cmp(void const *one, void const *two)
Compare two entries by expiry time.
static cache_status_t cache_entry_expire(UNUSED rlm_cache_config_t const *config, void *instance, request_t *request, UNUSED void *handle, fr_value_box_t const *key)
Free an entry and remove it from the data store.
fr_rb_node_t node
Entry used for lookups.
static cache_status_t cache_entry_find(rlm_cache_entry_t **out, UNUSED rlm_cache_config_t const *config, void *instance, request_t *request, UNUSED void *handle, fr_value_box_t const *key)
Locate a cache entry.
rlm_cache_entry_t fields
Entry data.
fr_heap_t * heap
For managing entry expiry.
static int8_t cache_entry_cmp(void const *one, void const *two)
Compare two entries by key.
static cache_status_t cache_entry_set_ttl(UNUSED rlm_cache_config_t const *config, void *instance, request_t *request, UNUSED void *handle, rlm_cache_entry_t *c)
Update the TTL of an entry.
static int mod_instantiate(module_inst_ctx_t const *mctx)
Create a new cache_rbtree instance.
static cache_status_t cache_entry_insert(rlm_cache_config_t const *config, void *instance, request_t *request, void *handle, rlm_cache_entry_t const *c)
Insert a new entry into the data store.
void * data
Module's instance data.
Definition module.h:293
module_instantiate_t instantiate
Callback to allow the module to register any per-instance resources like sockets and file handles.
Definition module.h:227
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
Definition syserror.c:243
#define fr_unix_time_gt(_a, _b)
Definition time.h:365
static int8_t fr_unix_time_cmp(fr_unix_time_t a, fr_unix_time_t b)
Compare two fr_unix_time_t values.
Definition time.h:944
static fr_unix_time_t fr_time_to_unix_time(fr_time_t when)
Convert an fr_time_t (internal time) to our version of unix time (wallclock time)
Definition time.h:688
void fr_value_box_copy_shallow(TALLOC_CTX *ctx, fr_value_box_t *dst, fr_value_box_t const *src)
Perform a shallow copy of a value_box.
Definition value.c:4503
static fr_slen_t data
Definition value.h:1340
static size_t char ** out
Definition value.h:1030