Skip to content

Commit

Permalink
Switch rlm_cache to new submodule API
Browse files Browse the repository at this point in the history
  • Loading branch information
arr2036 committed Apr 21, 2019
1 parent 70b5073 commit 8d57357
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 51 deletions.
92 changes: 43 additions & 49 deletions src/modules/rlm_cache/rlm_cache.c
Expand Up @@ -84,7 +84,7 @@ static int cache_acquire(rlm_cache_handle_t **out, rlm_cache_t const *inst, REQU
return 0;
}

return inst->driver->acquire(out, &inst->config, inst->driver_inst->data, request);
return inst->driver->acquire(out, &inst->config, inst->driver_inst->dl_inst->data, request);
}

/** Release a handle we previously acquired
Expand All @@ -95,7 +95,7 @@ static void cache_release(rlm_cache_t const *inst, REQUEST *request, rlm_cache_h
if (!inst->driver->release) return;
if (!handle || !*handle) return;

inst->driver->release(&inst->config, inst->driver_inst->data, request, *handle);
inst->driver->release(&inst->config, inst->driver_inst->dl_inst->data, request, *handle);
*handle = NULL;
}

Expand All @@ -106,7 +106,7 @@ static int cache_reconnect(rlm_cache_handle_t **handle, rlm_cache_t const *inst,
{
rad_assert(inst->driver->reconnect);

return inst->driver->reconnect(handle, &inst->config, inst->driver_inst->data, request);
return inst->driver->reconnect(handle, &inst->config, inst->driver_inst->dl_inst->data, request);
}

/** Allocate a cache entry
Expand All @@ -119,7 +119,7 @@ static int cache_reconnect(rlm_cache_handle_t **handle, rlm_cache_t const *inst,
*/
static rlm_cache_entry_t *cache_alloc(rlm_cache_t const *inst, REQUEST *request)
{
if (inst->driver->alloc) return inst->driver->alloc(&inst->config, inst->driver_inst->data, request);
if (inst->driver->alloc) return inst->driver->alloc(&inst->config, inst->driver_inst->dl_inst->data, request);

return talloc_zero(NULL, rlm_cache_entry_t);
}
Expand Down Expand Up @@ -209,7 +209,7 @@ static rlm_rcode_t cache_find(rlm_cache_entry_t **out, rlm_cache_t const *inst,
*out = NULL;

for (;;) {
ret = inst->driver->find(&c, &inst->config, inst->driver_inst->data, request, *handle, key, key_len);
ret = inst->driver->find(&c, &inst->config, inst->driver_inst->dl_inst->data, request, *handle, key, key_len);
switch (ret) {
case CACHE_RECONNECT:
RDEBUG2("Reconnecting...");
Expand Down Expand Up @@ -241,7 +241,7 @@ static rlm_rcode_t cache_find(rlm_cache_entry_t **out, rlm_cache_t const *inst,
fr_box_strvalue_len((char const *)key, key_len),
request->packet->timestamp.tv_sec - c->expires);

inst->driver->expire(&inst->config, inst->driver_inst->data, request, handle, c->key, c->key_len);
inst->driver->expire(&inst->config, inst->driver_inst->dl_inst->data, request, handle, c->key, c->key_len);
cache_free(inst, &c);
return RLM_MODULE_NOTFOUND; /* Couldn't find a non-expired entry */
}
Expand All @@ -265,7 +265,7 @@ static rlm_rcode_t cache_expire(rlm_cache_t const *inst, REQUEST *request,
rlm_cache_handle_t **handle, uint8_t const *key, size_t key_len)
{
RDEBUG2("Expiring cache entry");
for (;;) switch (inst->driver->expire(&inst->config, inst->driver_inst->data, request,
for (;;) switch (inst->driver->expire(&inst->config, inst->driver_inst->dl_inst->data, request,
*handle, key, key_len)) {
case CACHE_RECONNECT:
if (cache_reconnect(handle, inst, request) == 0) continue;
Expand Down Expand Up @@ -303,7 +303,7 @@ static rlm_rcode_t cache_insert(rlm_cache_t const *inst, REQUEST *request, rlm_c
TALLOC_CTX *pool;

if ((inst->config.max_entries > 0) && inst->driver->count &&
(inst->driver->count(&inst->config, inst->driver_inst->data, request, handle) > inst->config.max_entries)) {
(inst->driver->count(&inst->config, inst->driver_inst->dl_inst->data, request, handle) > inst->config.max_entries)) {
RWDEBUG("Cache is full: %d entries", inst->config.max_entries);
return RLM_MODULE_FAIL;
}
Expand Down Expand Up @@ -450,7 +450,7 @@ static rlm_rcode_t cache_insert(rlm_cache_t const *inst, REQUEST *request, rlm_c
for (;;) {
cache_status_t ret;

ret = inst->driver->insert(&inst->config, inst->driver_inst->data, request, *handle, c);
ret = inst->driver->insert(&inst->config, inst->driver_inst->dl_inst->data, request, *handle, c);
switch (ret) {
case CACHE_RECONNECT:
if (cache_reconnect(handle, inst, request) == 0) continue;
Expand Down Expand Up @@ -484,7 +484,7 @@ static rlm_rcode_t cache_set_ttl(rlm_cache_t const *inst, REQUEST *request,
if (!inst->driver->set_ttl) for (;;) {
cache_status_t ret;

ret = inst->driver->insert(&inst->config, inst->driver_inst->data, request, *handle, c);
ret = inst->driver->insert(&inst->config, inst->driver_inst->dl_inst->data, request, *handle, c);
switch (ret) {
case CACHE_RECONNECT:
if (cache_reconnect(handle, inst, request) == 0) continue;
Expand All @@ -506,7 +506,7 @@ static rlm_rcode_t cache_set_ttl(rlm_cache_t const *inst, REQUEST *request,
for (;;) {
cache_status_t ret;

ret = inst->driver->set_ttl(&inst->config, inst->driver_inst->data, request, *handle, c);
ret = inst->driver->set_ttl(&inst->config, inst->driver_inst->dl_inst->data, request, *handle, c);
switch (ret) {
case CACHE_RECONNECT:
if (cache_reconnect(handle, inst, request) == 0) continue;
Expand Down Expand Up @@ -898,42 +898,14 @@ static int mod_detach(void *instance)
*/
static int mod_bootstrap(void *instance, CONF_SECTION *conf)
{
rlm_cache_t *inst = instance;

inst->cs = conf;

inst->config.name = cf_section_name2(conf);
if (!inst->config.name) inst->config.name = cf_section_name1(conf);

/*
* Register the cache xlat function
*/
xlat_register(inst, inst->config.name, cache_xlat, NULL, NULL, 0, 0, true);

return 0;
}

/** Create a new rlm_cache_instance
*
*/
static int mod_instantiate(void *instance, CONF_SECTION *conf)
{
rlm_cache_t *inst = instance;
CONF_SECTION *update;
rlm_cache_t *inst = instance;
CONF_SECTION *driver_cs;
char const *name;

inst->cs = conf;

rad_assert(inst->config.key);

/*
* Sanity check for crazy people.
*/
if (strncmp(inst->config.driver_name, "rlm_cache_", 8) != 0) {
cf_log_err(conf, "\"%s\" is NOT an Cache driver!", inst->config.driver_name);
return -1;
}
inst->config.name = cf_section_name2(conf);
if (!inst->config.name) inst->config.name = cf_section_name1(conf);

name = strrchr(inst->config.driver_name, '_');
if (!name) {
Expand All @@ -951,11 +923,20 @@ static int mod_instantiate(void *instance, CONF_SECTION *conf)
/*
* Load the appropriate driver for our backend
*/
if (dl_instance(inst, &inst->driver_inst, driver_cs, dl_instance_find(inst), name, DL_TYPE_SUBMODULE) < 0) {
inst->driver_inst = module_bootstrap(module_by_data(inst), driver_cs);
if (!inst->driver_inst) {
cf_log_err(driver_cs, "Failed loading driver");
return -1;
}
inst->driver = (cache_driver_t const *)inst->driver_inst->module->common;
inst->driver = (cache_driver_t const *)inst->driver_inst->dl_inst->module->common;

/*
* Sanity check for crazy people.
*/
if (strncmp(inst->config.driver_name, "rlm_cache_", 8) != 0) {
cf_log_err(conf, "\"%s\" is NOT an Cache driver!", inst->config.driver_name);
return -1;
}

/*
* Non optional fields and callbacks
Expand All @@ -965,12 +946,25 @@ static int mod_instantiate(void *instance, CONF_SECTION *conf)
rad_assert(inst->driver->insert);
rad_assert(inst->driver->expire);

if (inst->driver->instantiate &&
(inst->driver->instantiate(&inst->config, inst->driver_inst->data, driver_cs) < 0)) return -1;
/*
* Register the cache xlat function
*/
xlat_register(inst, inst->config.name, cache_xlat, NULL, NULL, 0, 0, true);

#ifndef NDEBUG
module_instance_read_only(inst->driver_inst->data, inst->driver->name);
#endif
return 0;
}

/** Create a new rlm_cache_instance
*
*/
static int mod_instantiate(void *instance, CONF_SECTION *conf)
{
rlm_cache_t *inst = instance;
CONF_SECTION *update;

inst->cs = conf;

rad_assert(inst->config.key);

if (inst->config.ttl == 0) {
cf_log_err(conf, "Must set 'ttl' to non-zero");
Expand Down
5 changes: 3 additions & 2 deletions src/modules/rlm_cache/rlm_cache.h
Expand Up @@ -66,7 +66,7 @@ typedef struct {
typedef struct {
rlm_cache_config_t config; //!< Must come first because of icky hacks.

dl_instance_t *driver_inst; //!< Driver's instance data.
module_instance_t *driver_inst; //!< Driver's instance data.
cache_driver_t const *driver; //!< Driver's exported interface.

vp_map_t *maps; //!< Attribute map applied to users.
Expand Down Expand Up @@ -279,8 +279,9 @@ typedef int (*cache_reconnect_t)(rlm_cache_handle_t **handle, rlm_cache_config_

struct cache_driver {
DL_MODULE_COMMON; //!< Common fields for all loadable modules.
FR_MODULE_COMMON; //!< Common fields for all instantiated modules.
FR_MODULE_THREADED_COMMON; //!< Common fields for threaded modules.

cache_instantiate_t instantiate; //!< (optional) Instantiate a driver.
cache_entry_alloc_t alloc; //!< (optional) Allocate a new entry.
cache_entry_free_t free; //!< (optional) Free memory used by an entry.

Expand Down

0 comments on commit 8d57357

Please sign in to comment.