25 #include <freeradius-devel/server/base.h>
26 #include <freeradius-devel/util/heap.h>
27 #include <freeradius-devel/util/debug.h>
28 #include <freeradius-devel/util/value.h>
29 #include <freeradius-devel/util/htrie.h>
30 #include "../../rlm_cache.h"
111 cf_log_err(ci,
"Key type is unspecified. Add a cast to set a specific type");
119 inst->ktype = our_ktype;
124 cf_log_err(ci,
"Incompatible key types '%s' and '%s', cast to a more broadly compatible "
132 old_ktype =
inst->ktype;
141 if (!
inst->htrie_auto || (old_ktype ==
inst->ktype))
return 0;
148 cf_log_err(ci,
"Invalid data type '%s' for htrie key. "
153 cf_log_info(ci,
"Automatically setting htrie type to '%s' based on key type '%s'",
156 *(
void **)
out = key_tmpl;
184 RERROR(
"Failed allocating cache entry");
287 RERROR(
"Failed adding entry");
295 RERROR(
"Failed adding entry to expiry heap");
320 RERROR(
"Entry not in heap");
326 RERROR(
"Failed updating entry TTL. Entry was forcefully expired");
359 pthread_mutex_lock(&driver->
mutex);
379 pthread_mutex_unlock(&driver->
mutex);
403 pthread_mutex_destroy(&driver->
mutex);
427 if (!driver->
cache) {
428 PERROR(
"Failed to create cache");
437 ERROR(
"Failed to create heap for the cache");
441 if ((ret = pthread_mutex_init(&driver->
mutex, NULL)) < 0) {
453 .name =
"cache_htrie",
458 .inst_type =
"rlm_cache_htrie_t",
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
int call_env_parse_pair(TALLOC_CTX *ctx, void *out, tmpl_rules_t const *t_rules, CONF_ITEM *ci, UNUSED call_env_ctx_t const *cec, UNUSED call_env_parser_t const *rule)
Standard function we use for parsing call env pairs.
int cf_table_parse_int(UNUSED TALLOC_CTX *ctx, void *out, UNUSED void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Generic function for parsing conf pair values as int.
API to parse internal format configuration items into native C types.
#define CONF_PARSER_TERMINATOR
#define FR_CONF_OFFSET(_name, _struct, _field)
conf_parser_t which parses a single CONF_PAIR, writing the result to a field in a struct
Defines a CONF_PAIR to C data type mapping.
Common header for all CONF_* types.
#define cf_log_err(_cf, _fmt,...)
#define cf_log_info(_cf, _fmt,...)
#define fr_cond_assert(_x)
Calls panic_action ifndef NDEBUG, else logs error and evaluates to value of _x.
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
uint32_t(* fr_hash_t)(void const *)
int fr_heap_insert(fr_heap_t **hp, void *data)
Insert a new element into the heap.
int fr_heap_extract(fr_heap_t **hp, void *data)
Remove a node from the heap.
unsigned int fr_heap_index_t
static void * fr_heap_peek(fr_heap_t *h)
Return the item from the top of the heap but don't pop it.
#define fr_heap_talloc_alloc(_ctx, _cmp, _talloc_type, _field, _init)
Creates a heap that verifies elements are of a specific talloc type.
size_t fr_htrie_type_table_len
fr_htrie_t * fr_htrie_alloc(TALLOC_CTX *ctx, fr_htrie_type_t type, fr_hash_t hash_data, fr_cmp_t cmp_data, fr_trie_key_t get_key, fr_free_t free_data)
An abstraction over our internal hashes, rb trees, and prefix tries.
fr_table_num_sorted_t const fr_htrie_type_table[]
@ FR_HTRIE_AUTO
Automatically choose the best type.
static fr_htrie_type_t fr_htrie_hint(fr_type_t type)
static bool fr_htrie_insert(fr_htrie_t *ht, void const *data)
Insert data into a htrie.
static int fr_htrie_num_elements(fr_htrie_t *ht)
Return the number of elements in the htrie.
static bool fr_htrie_delete(fr_htrie_t *ht, void const *data)
Delete data from a htrie, freeing it if free_data cb was passed to fr_htrie_alloc.
static void * fr_htrie_find(fr_htrie_t *ht, void const *data)
Find data in a htrie.
static char const * fr_htrie_type_to_str(fr_htrie_type_t type)
Return a static string containing the type name.
A hash/rb/prefix trie abstraction.
int8_t(* fr_cmp_t)(void const *a, void const *b)
module_instance_t * mi
Module instance to detach.
module_instance_t * mi
Instance of the module being instantiated.
Temporary structure to hold arguments for detach calls.
Temporary structure to hold arguments for instantiation calls.
static const conf_parser_t config[]
void fr_rb_iter_delete_inorder(fr_rb_iter_inorder_t *iter)
Remove the current node from the tree.
void * fr_rb_iter_next_inorder(fr_rb_iter_inorder_t *iter)
Return the next node.
void * fr_rb_iter_init_inorder(fr_rb_iter_inorder_t *iter, fr_rb_tree_t *tree)
Initialise an in-order iterator.
Iterator structure for in-order traversal of an rbtree.
fr_value_box_t key
Key used to identify entry.
module_t common
Common fields for all loadable modules.
@ CACHE_ERROR
Fatal error.
@ CACHE_OK
Cache entry found/updated.
@ CACHE_MISS
Cache entry notfound.
fr_unix_time_t expires
When the entry expires.
Configuration for the rlm_cache module.
static int mod_detach(module_detach_ctx_t const *mctx)
Cleanup a cache_htrie instance.
bool htrie_auto
Whether the user wanted to automatically configure the htrie.
static int cf_htrie_type_parse(TALLOC_CTX *ctx, void *out, void *parent, CONF_ITEM *ci, conf_parser_t const *rule)
Custom htrie type parsing function.
static int cf_htrie_key_parse(TALLOC_CTX *ctx, void *out, tmpl_rules_t const *t_rules, CONF_ITEM *ci, void const *data, UNUSED call_env_parser_t const *rule)
Custom key parsing function for checking compatibility of key types.
rlm_cache_entry_t fields
Entry data.
static conf_parser_t driver_config[]
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 htrie.
fr_htrie_t * cache
Tree for looking up cache keys.
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.
rlm_cache_driver_t rlm_cache_htrie
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.
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.
pthread_mutex_t mutex
Protect the tree from multiple readers/writers.
fr_heap_index_t heap_id
Offset used for expiry heap.
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_htrie 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.
fr_heap_t * heap
For managing entry expiry.
fr_htrie_type_t htype
The htrie type we'll be using.
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.
fr_type_t ktype
When htrie is "auto", we use this type to decide what type of tree to use.
void * data
Module's instance data.
Structures and prototypes for templates.
fr_type_t tmpl_expanded_type(tmpl_t const *vpt)
Return the native data type of the expression.
Optional arguments passed to vp_tmpl functions.
eap_aka_sim_process_conf_t * inst
char const * fr_syserror(int num)
Guaranteed to be thread-safe version of strerror.
#define talloc_get_type_abort_const
static int8_t fr_unix_time_cmp(fr_unix_time_t a, fr_unix_time_t b)
Compare two fr_unix_time_t values.
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)
#define fr_unix_time_lt(_a, _b)
int(* fr_trie_key_t)(uint8_t **out, size_t *outlen, void const *data)
bool fr_type_cast(fr_type_t dst, fr_type_t src)
Return if we're allowed to cast the types.
fr_type_t fr_type_promote(fr_type_t a, fr_type_t b)
Return the promoted type.
Types of values contained within an fr_value_box_t.
#define fr_type_is_void(_x)
static char const * fr_type_to_str(fr_type_t type)
Return a static string containing the type name.
uint32_t fr_value_box_hash(fr_value_box_t const *vb)
Hash the contents of a value box.
int8_t fr_value_box_cmp(fr_value_box_t const *a, fr_value_box_t const *b)
Compare two values.
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.
int fr_value_box_to_key(uint8_t **out, size_t *outlen, fr_value_box_t const *value)
Get a key from a value box.
static size_t char ** out