The FreeRADIUS server $Id: f3670dba8951ca10eb4948feb3dc3db9423a334f $
Loading...
Searching...
No Matches
rlm_cache_redis.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: 8fc8d6bc836ac48a2eb846b1b65f9c20a01718a5 $
19 * @file rlm_cache_redis.c
20 * @brief redis based cache.
21 *
22 * @copyright 2015 Arran Cudbard-Bell (a.cudbardb@freeradius.org)
23 */
24#define LOG_PREFIX "cache - redis"
25
26#include <freeradius-devel/server/base.h>
27#include <freeradius-devel/util/debug.h>
28
29#include "../../rlm_cache.h"
30#include <freeradius-devel/redis/base.h>
31#include <freeradius-devel/redis/cluster_async.h>
32#include <freeradius-devel/io/coord_pair.h>
33
38
39typedef struct {
40 fr_redis_conf_t conf; //!< Connection parameters for the Redis server.
41 //!< Must be first field in this struct.
42
43
44 tmpl_t *created_attr; //!< LHS of the Cache-Created map.
45 tmpl_t *expires_attr; //!< LHS of the Cache-Expires map.
46
47 module_instance_t const *mi; //!< Module instance.
48
49 fr_coord_reg_t *coord_reg; //!< Coordinator registration.
50 fr_coord_pair_reg_t *coord_pair_reg; //!< Coord pair registration.
52
53typedef struct {
54 rlm_cache_redis_t const *inst; //!< Module instance.
55 fr_redis_ct_t *rtcluster; //!< Per thread Redis cluster.
56 fr_coord_worker_t *cw; //!< Coord-worker for fetching cluster map.
58
67
69static fr_dict_t const *dict_redis;
70
73 { .out = &dict_freeradius, .proto = "freeradius" },
74 { .out = &dict_redis, .proto = "redis" },
76};
77
80
83 { .out = &attr_cache_created, .name = "Cache-Created", .type = FR_TYPE_DATE, .dict = &dict_freeradius },
84 { .out = &attr_cache_expires, .name = "Cache-Expires", .type = FR_TYPE_DATE, .dict = &dict_freeradius },
86};
87
89
90/** Create a new rlm_cache_redis instance
91 *
92 * @param[in] mctx Data required for instantiation.
93 * @return
94 * - 0 on success.
95 * - -1 on failure.
96 */
97static int mod_instantiate(module_inst_ctx_t const *mctx)
98{
99 rlm_cache_redis_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_cache_redis_t);
100 char buffer[256];
101
102 snprintf(buffer, sizeof(buffer), "rlm_cache (%s)", mctx->mi->parent->name);
103
104 inst->mi = mctx->mi;
105 inst->conf.log_prefix = talloc_asprintf(inst, "rlm_cache (%s)", mctx->mi->parent->name);
106 inst->conf.module_name = mctx->mi->parent->module->name;
107 inst->conf.inst_name = mctx->mi->parent->name;
108
109#ifdef HAVE_REDIS_SSL
110 if (inst->conf.use_tls && !inst->conf.tls) {
111 cf_log_err(mctx->mi->conf, "Missing tls section");
112 return -1;
113 }
114#endif
115
116 /*
117 * These never change, so do it once on instantiation
118 */
119 if (tmpl_afrom_attr_str(inst, NULL, &inst->created_attr, "Cache-Created", NULL) <= 0) {
120 ERROR("Cache-Created attribute not defined");
121 return -1;
122 }
123
124 if (tmpl_afrom_attr_str(inst, NULL, &inst->expires_attr, "Cache-Expires", NULL) <= 0) {
125 ERROR("Cache-Expires attribute not defined");
126 return -1;
127 }
128
129 if (!inst->conf.use_cluster_map) return 0;
130
131 if (inst->conf.database) {
132 cf_log_err(mctx->mi->conf, "Cannot set Redis database number when cluster in use");
133 return -1;
134 }
135
137 .name = mctx->mi->name,
138 .worker_cb = worker_pair_callbacks,
139 .cb_id = REDIS_COORD_PAIR_CALLBACK_ID,
140 .root = fr_dict_root(dict_redis),
141 .cs = mctx->mi->conf,
142 }
143 );
144 if (!inst->coord_pair_reg) return -1;
145
146 FR_COORD_PAIR_CB_CTX_SET(coord_callbacks, worker_callbacks, inst->coord_pair_reg);
147
149 .name = mctx->mi->name,
150 .coord_cb = coord_callbacks,
151 .worker_cb = worker_callbacks,
152 .mi = mctx->mi
153 });
154
155 if (!inst->coord_reg) return -1;
156
157 return 0;
158}
159
161{
162 rlm_cache_redis_thread_t *t = talloc_get_type_abort(mctx->thread, rlm_cache_redis_thread_t);
163 rlm_cache_redis_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_cache_redis_t);
164
165 t->rtcluster = fr_redis_ct_alloc(t, mctx->el, &inst->conf, NULL, NULL, false);
166 if (!t->rtcluster) return -1;
167 t->inst = inst;
168
169 return 0;
170}
171
173{
174 rlm_cache_redis_thread_t *t = talloc_get_type_abort(mctx->thread, rlm_cache_redis_thread_t);
175 rlm_cache_redis_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_cache_redis_t);
176
177 if (!inst->conf.use_cluster_map) return 0;
178
179 t->cw = fr_coord_attach(t, mctx->el, inst->coord_reg);
180
181 if (!t->cw) {
182 ERROR("Failed to attach to coordinator");
183 return -1;
184 }
185
186 if ((inst->conf.trunk_conf.start == 0) || (fr_schedule_worker_id() != 0)) return 0;
187
188 return fr_redis_ct_map_bootstrap(t->rtcluster, t->cw, inst->coord_pair_reg);
189}
190
192{
193 rlm_cache_redis_thread_t *t = talloc_get_type_abort(mctx->thread, rlm_cache_redis_thread_t);
194
195 if (!t->cw) return 0;
196
197 fr_coord_detach(t->cw, true);
198 t->cw = NULL;
199 return 0;
200}
201
202static int mod_detach(module_detach_ctx_t const *mctx)
203{
204 rlm_cache_redis_t *inst = talloc_get_type_abort(mctx->mi->data, rlm_cache_redis_t);
205
206 if (!inst->conf.use_cluster_map) return 0;
207
208 fr_coord_deregister(inst->coord_reg);
209 talloc_free(inst->coord_pair_reg);
210 return 0;
211}
212
213static int mod_load(void)
214{
216 return redis_dict_init();
217}
218
220{
221 talloc_free(c);
222}
223
225{
226 size_t i;
227 for (i = 0; i < talloc_array_length(rctx->cmd_str); i++) {
228 if (!rctx->cmd_str[i]) continue;
229 redisFreeCommand(rctx->cmd_str[i]);
230 }
231 return 0;
232}
233
234/** Process the results of Redis cache commands
235 *
236 * Initiating a cluster remap and query redirection if needed.
237 *
238 */
241{
242 switch (fr_redis_command_set_rcode(cmds)) {
244 return rcode;
245
247 {
248 rlm_cache_redis_thread_t *thread = talloc_get_type_abort(module_thread(inst->mi)->data,
250
251 if (inst->conf.use_cluster_map) fr_redis_ct_map_get(thread->rtcluster, thread->cw,
252 inst->coord_pair_reg, false);
253 }
255
258 return CACHE_YIELD;
259
262 return CACHE_YIELD;
263
265 RPERROR("Server returned error");
266 return CACHE_ERROR;
267
268 default:
269 return CACHE_ERROR;
270 }
271}
272
274 UNUSED void *handle, void *rctx)
275{
276 rlm_cache_redis_rctx_t *cache_rctx = talloc_get_type_abort(rctx, rlm_cache_redis_rctx_t);
277
278 RDEBUG2("Forcibly cancelling pending redis cache request");
279 fr_redis_async_cmd_cancel(cache_rctx->cmd);
280}
281
283 void *instance, UNUSED request_t *request,
284 UNUSED void *handle, void *rctx)
285{
286 rlm_cache_redis_t *driver = instance;
287 rlm_cache_redis_rctx_t *cache_rctx = talloc_get_type_abort(rctx, rlm_cache_redis_rctx_t);
288
289 *out = cache_rctx->entry;
290 return cache_redis_results(request, driver, cache_rctx->cmds, cache_rctx->cmd, cache_rctx->rcode);
291}
292
293static void cache_entry_find_results(request_t *request, UNUSED fr_redis_command_t *cmd, redisReply *reply, void *rctx)
294{
295 rlm_cache_redis_rctx_t *cache_rctx = talloc_get_type_abort(rctx, rlm_cache_redis_rctx_t);
296 size_t i;
297#ifdef HAVE_TALLOC_ZERO_POOLED_OBJECT
298 size_t pool_size = 0;
299#endif
300 map_list_t head;
302
303 if (reply->type != REDIS_REPLY_ARRAY) {
304 REDEBUG("Bad result type, expected array, got %s",
305 fr_table_str_by_value(redis_reply_types, reply->type, "<UNKNOWN>"));
306 error:
307 cache_rctx->rcode = CACHE_ERROR;
308 return;
309 }
310
311 RDEBUG3("Entry contains %zu elements", reply->elements);
312
313 if (reply->elements == 0) {
314 cache_rctx->rcode = CACHE_MISS;
315 return;
316 }
317
318 if (reply->elements % 3) {
319 REDEBUG("Invalid number of reply elements (%zu). "
320 "Reply must contain triplets of keys operators and values",
321 reply->elements);
322 goto error;
323 }
324
325 map_list_init(&head);
326
327#ifdef HAVE_TALLOC_ZERO_POOLED_OBJECT
328 /*
329 * We can get a pretty good idea of the required size of the pool
330 */
331 for (i = 0; i < reply->elements; i += 3) {
332 pool_size += sizeof(map_t) + (sizeof(tmpl_t) * 2);
333 if (reply->element[i]->type == REDIS_REPLY_STRING) pool_size += reply->element[i]->len + 1;
334 }
335
336 /*
337 * reply->elements gives us the number of chunks, as the maps are triplets, and there
338 * are three chunks per map
339 */
340
341 MEM(c = talloc_zero_pooled_object(NULL, rlm_cache_entry_t, reply->elements, pool_size));
342#else
343 MEM(c = talloc_zero(NULL, rlm_cache_entry_t));
344#endif
345 map_list_init(&c->maps);
346 /*
347 * Convert the key/value pairs back into maps
348 */
349 for (i = 0; i < reply->elements; i += 3) {
351 reply->element[i], reply->element[i + 1], reply->element[i + 2]) < 0) {
352 talloc_free(c);
353 cache_rctx->rcode = CACHE_ERROR;
354 return;
355 }
356 }
357
358 /*
359 * Pull out the cache created date
360 */
361 if (tmpl_attr_tail_da(map_list_head(&head)->lhs) == attr_cache_created) {
362 map_t *map;
363
364 c->created = tmpl_value(map_list_head(&head)->rhs)->vb_date;
365
366 map = map_list_pop_head(&head);
367 talloc_free(map);
368 }
369
370 /*
371 * Pull out the cache expires date
372 */
373 if (tmpl_attr_tail_da(map_list_head(&head)->lhs) == attr_cache_expires) {
374 map_t *map;
375
376 c->expires = tmpl_value(map_list_head(&head)->rhs)->vb_date;
377
378 map = map_list_pop_head(&head);
379 talloc_free(map);
380 }
381
382 if (unlikely(fr_value_box_copy(c, &c->key, cache_rctx->key) < 0)) goto error;
383
384 map_list_move(&c->maps, &head);
385 cache_rctx->entry = c;
386}
387
388/** Locate a cache entry in redis
389 *
390 * @copydetails cache_entry_find_t
391 */
393 UNUSED rlm_cache_config_t const *config, void *instance,
394 request_t *request, UNUSED void *handle, fr_value_box_t const *key)
395{
396 rlm_cache_redis_t *driver = instance;
397 rlm_cache_redis_thread_t *thread = talloc_get_type_abort(module_thread(driver->mi)->data, rlm_cache_redis_thread_t);
400 int cmd_len;
402
404 MEM(cmds = fr_redis_command_set_alloc(rctx, request, NULL, NULL, NULL, false));
405 rctx->cmds = cmds;
406 rctx->key = key;
407 rctx->cmd_str = talloc_zero_array(rctx, char *, 1);
408 talloc_set_destructor(rctx, cache_redis_rctx_free);
409
410 RDEBUG3("LRANGE %pV 0 -1", key);
411 cmd_len = redisFormatCommand(&rctx->cmd_str[0], "LRANGE %b 0 -1", key->vb_strvalue, key->vb_length);
412 if (cmd_len < 0) {
413 RERROR("Failed formatting redis command");
414 error:
415 talloc_free(rctx);
416 return CACHE_ERROR;
417 }
419 rctx) != FR_REDIS_PIPELINE_OK) goto error;
420
421 rctx->cmd = fr_redis_async_cmd_start(rctx, request, &ret, thread->rtcluster, (uint8_t const *)key->vb_strvalue,
422 key->vb_length, cmds, false, NULL);
423 REDIS_ASYNC_START_RCODE_PROCESS(ret, thread->rtcluster, thread->cw, thread->inst->coord_pair_reg,
424 "Failed to enqueue Redis cache find commands", CACHE_ERROR)
425
426 *rctx_out = rctx;
427 return CACHE_YIELD;
428}
429
431 void *instance, UNUSED request_t *request,
432 UNUSED void *handle, void *rctx)
433{
434 rlm_cache_redis_t *driver = instance;
435 rlm_cache_redis_rctx_t *cache_rctx = talloc_get_type_abort(rctx, rlm_cache_redis_rctx_t);
436
437 *out = cache_rctx->entry;
438
439 return cache_redis_results(request, driver, cache_rctx->cmds, cache_rctx->cmd, cache_rctx->rcode);
440}
441
442static void cache_entry_insert_results(request_t *request, UNUSED fr_redis_command_t *cmd, redisReply *reply, void *rctx)
443{
444 rlm_cache_redis_rctx_t *cache_rctx = talloc_get_type_abort(rctx, rlm_cache_redis_rctx_t);
445
446 RDEBUG3("Command results");
447 RINDENT();
449 REXDENT();
450
451 cache_rctx->rcode = CACHE_OK;
452}
453
454/** Insert a new entry into the data store
455 *
456 * @copydetails cache_entry_insert_t
457 */
458static cache_status_t cache_entry_insert(UNUSED void **rctx_out, UNUSED rlm_cache_config_t const *config, void *instance,
459 request_t *request, UNUSED void *handle, const rlm_cache_entry_t *c)
460{
461 rlm_cache_redis_t *driver = instance;
462 rlm_cache_redis_thread_t *thread = talloc_get_type_abort(module_thread(driver->mi)->data, rlm_cache_redis_thread_t);
465 int cmd_len;
467
468 TALLOC_CTX *pool;
469
470 map_t *map = NULL;
471
472 static char const command[] = "RPUSH";
473 char const **argv;
474 size_t *argv_len;
475 char const **argv_p;
476 size_t *argv_len_p;
477 size_t i;
478
479 int cnt;
480
481 tmpl_t expires_value;
482 map_t expires = {
483 .op = T_OP_SET,
484 .lhs = driver->expires_attr,
485 .rhs = &expires_value,
486 };
487
488 tmpl_t created_value;
489 map_t created = {
490 .op = T_OP_SET,
491 .lhs = driver->created_attr,
492 .rhs = &created_value,
493 };
494
496 rctx->entry = UNCONST(rlm_cache_entry_t *, c);
497
498 /*
499 * Encode the entry created date
500 */
501 tmpl_init_shallow(&created_value, TMPL_TYPE_DATA, T_BARE_WORD, "<TEMP>", 6, NULL);
502 fr_value_box_init(&created_value.data.literal, FR_TYPE_DATE, NULL, true);
503 tmpl_value(&created_value)->vb_date = c->created;
504
505 /*
506 * Encode the entry expiry time
507 *
508 * Although Redis objects expire on their own, we still need this
509 * to ignore entries that were created before the last epoch.
510 */
511 tmpl_init_shallow(&expires_value, TMPL_TYPE_DATA, T_BARE_WORD, "<TEMP>", 6, NULL);
512 fr_value_box_init(&expires_value.data.literal, FR_TYPE_DATE, NULL, true);
513 tmpl_value(&expires_value)->vb_date = c->expires;
514
515 cnt = map_list_num_elements(&c->maps) + 2;
516
517 /*
518 * The majority of serialized entries should be under 1k.
519 *
520 * @todo We should really calculate this using some sort of moving average.
521 */
522 pool = talloc_pool(rctx, 1024);
523 if (!pool) return CACHE_ERROR;
524
525 argv_p = argv = talloc_array(pool, char const *, (cnt * 3) + 2); /* pair = 3 + cmd + key */
526 argv_len_p = argv_len = talloc_array(pool, size_t, (cnt * 3) + 2); /* pair = 3 + cmd + key */
527
528 *argv_p++ = command;
529 *argv_len_p++ = sizeof(command) - 1;
530
531 *argv_p++ = (char const *)c->key.vb_strvalue;
532 *argv_len_p++ = c->key.vb_length;
533
534 /*
535 * Add the maps to the command string in reverse order
536 */
537 if (fr_redis_tuple_from_map(pool, argv_p, argv_len_p, &created) < 0) {
538 REDEBUG("Failed encoding map as Redis K/V pair");
539 talloc_free(rctx);
540 return CACHE_ERROR;
541 }
542 argv_p += 3;
543 argv_len_p += 3;
544 if (fr_redis_tuple_from_map(pool, argv_p, argv_len_p, &expires) < 0) {
545 REDEBUG("Failed encoding map as Redis K/V pair");
546 talloc_free(rctx);
547 return CACHE_ERROR;
548 }
549 argv_p += 3;
550 argv_len_p += 3;
551 while ((map = map_list_next(&c->maps, map))) {
552 if (fr_redis_tuple_from_map(pool, argv_p, argv_len_p, map) < 0) {
553 REDEBUG("Failed encoding map as Redis K/V pair");
554 talloc_free(rctx);
555 return CACHE_ERROR;
556 }
557 argv_p += 3;
558 argv_len_p += 3;
559 }
560
561 MEM(cmds = fr_redis_command_set_alloc(rctx, request, NULL, NULL, NULL, false));
562 rctx->cmds = cmds;
563 rctx->key = &c->key;
564 MEM(rctx->cmd_str = talloc_zero_array(rctx, char *, 3));
565 talloc_set_destructor(rctx, cache_redis_rctx_free);
566
567 RDEBUG3("Pipelining commands");
568
569 if (fr_unix_time_ispos(c->expires)) {
570 RDEBUG3("MULTI");
571 if (fr_redis_command_literal_add(cmds, "MULTI", NULL, NULL) != FR_REDIS_PIPELINE_OK) {
572 error:
573 talloc_free(rctx);
574 return CACHE_ERROR;
575 };
576 }
577
578 RDEBUG3("DEL \"%pV\"", &c->key);
579 cmd_len = redisFormatCommand(&rctx->cmd_str[0], "DEL %b", (uint8_t const *)c->key.vb_strvalue, c->key.vb_length);
580 if (cmd_len < 0) {
581 format_error:
582 RERROR("Failed formatting redis command");
583 goto error;
584 }
585 if (fr_redis_command_preformatted_add(cmds, rctx->cmd_str[0], cmd_len, NULL,
586 NULL) != FR_REDIS_PIPELINE_OK) goto error;
587
588 if (RDEBUG_ENABLED3) {
589 RDEBUG3("argv command");
590 RINDENT();
591 for (i = 0; i < talloc_array_length(argv); i++) {
592 RDEBUG3("%pV", fr_box_strvalue_len(argv[i], argv_len[i]));
593 }
594 REXDENT();
595 }
596 cmd_len = redisFormatCommandArgv(&rctx->cmd_str[1], talloc_array_length(argv), argv, argv_len);
597 if (cmd_len < 0) goto format_error;
598
599 if (fr_unix_time_ispos(c->expires)) {
600 if (fr_redis_command_preformatted_add(cmds, rctx->cmd_str[1], cmd_len, NULL,
601 NULL) != FR_REDIS_PIPELINE_OK) goto error;
602
603 RDEBUG3("EXPIREAT \"%pV\" %" PRIu64, &c->key, fr_unix_time_to_sec(c->expires));
604 cmd_len = redisFormatCommand(&rctx->cmd_str[2], "EXPIREAT %b %" PRIu64,
605 (uint8_t const *)c->key.vb_strvalue, (size_t)c->key.vb_length,
607 if (cmd_len < 0) goto format_error;
608 if (fr_redis_command_preformatted_add(cmds, rctx->cmd_str[2], cmd_len, NULL,
609 NULL) != FR_REDIS_PIPELINE_OK) goto error;
610
611 RDEBUG3("EXEC");
613 rctx) != FR_REDIS_PIPELINE_OK) goto error;
614 } else {
616 rctx) != FR_REDIS_PIPELINE_OK) goto error;
617 }
618
619 rctx->cmd = fr_redis_async_cmd_start(rctx, request, &ret, thread->rtcluster, (uint8_t const *)c->key.vb_strvalue,
620 c->key.vb_length, cmds, false, NULL);
621 REDIS_ASYNC_START_RCODE_PROCESS(ret, thread->rtcluster, thread->cw, thread->inst->coord_pair_reg,
622 "Failed to enqueue Redis cache insert commands", CACHE_ERROR)
623
624 *rctx_out = rctx;
625 return CACHE_YIELD;
626}
627
629 UNUSED request_t *request, UNUSED void *handle, void *rctx)
630{
631 rlm_cache_redis_t *driver = instance;
632 rlm_cache_redis_rctx_t *cache_rctx = talloc_get_type_abort(rctx, rlm_cache_redis_rctx_t);
633
634 return cache_redis_results(request, driver, cache_rctx->cmds, cache_rctx->cmd, cache_rctx->rcode);
635}
636
637static void cache_entry_expire_results(request_t *request, UNUSED fr_redis_command_t *cmd, redisReply *reply, void *rctx)
638{
639 rlm_cache_redis_rctx_t *cache_rctx = talloc_get_type_abort(rctx, rlm_cache_redis_rctx_t);
640
641 if (reply->type == REDIS_REPLY_INTEGER) {
642 cache_rctx->rcode = (reply->integer) ? CACHE_OK : CACHE_MISS;
643 return;
644 }
645
646 REDEBUG("Bad result type, expected integer, got %s",
647 fr_table_str_by_value(redis_reply_types, reply->type, "<UNKNOWN>"));
648
649 cache_rctx->rcode = CACHE_ERROR;
650}
651
652/** Call delete the cache entry from redis
653 *
654 * @copydetails cache_entry_expire_t
655 */
656static cache_status_t cache_entry_expire(UNUSED void **rctx_out, UNUSED rlm_cache_config_t const *config, void *instance,
657 request_t *request, UNUSED void *handle, fr_value_box_t const *key)
658{
659 rlm_cache_redis_t *driver = instance;
660 rlm_cache_redis_thread_t *thread = talloc_get_type_abort(module_thread(driver->mi)->data, rlm_cache_redis_thread_t);
663 int cmd_len;
665
667 MEM(cmds = fr_redis_command_set_alloc(rctx, request, NULL, NULL, NULL, false));
668 rctx->cmds = cmds;
669 rctx->key = key;
670 rctx->cmd_str = talloc_zero_array(rctx, char *, 1);
671 talloc_set_destructor(rctx, cache_redis_rctx_free);
672
673 cmd_len = redisFormatCommand(&rctx->cmd_str[0], "DEL %b", (uint8_t const *)key->vb_strvalue, key->vb_length);
674 if (cmd_len < 0) {
675 RERROR("Failed formatting redis command");
676 error:
677 talloc_free(rctx);
678 return CACHE_ERROR;
679 }
681 rctx) != FR_REDIS_PIPELINE_OK) goto error;
682
683 rctx->cmd = fr_redis_async_cmd_start(rctx, request, &ret, thread->rtcluster, (uint8_t const *)key->vb_strvalue,
684 key->vb_length, cmds, false, NULL);
685 REDIS_ASYNC_START_RCODE_PROCESS(ret, thread->rtcluster, thread->cw, thread->inst->coord_pair_reg,
686 "Failed to enqueue Redis cache expire command", CACHE_ERROR)
687
688 *rctx_out = rctx;
689 return CACHE_YIELD;
690}
691
694 .common = {
695 .magic = MODULE_MAGIC_INIT,
696 .name = "cache_redis",
697 .onload = mod_load,
699 .coord_attach = mod_coord_attach,
700 .coord_detach = mod_coord_detach,
701 .detach = mod_detach,
702 .inst_size = sizeof(rlm_cache_redis_t),
704 .thread_instantiate = mod_thread_instantiate,
705 .config = driver_config,
706 },
707 .free = cache_entry_free,
708 .find = cache_entry_find,
709 .find_resume = cache_entry_find_resume,
710 .find_cancel = cache_redis_cancel,
711 .insert = cache_entry_insert,
712 .insert_resume = cache_entry_insert_resume,
713 .insert_cancel = cache_redis_cancel,
714 .expire = cache_entry_expire,
715 .expire_resume = cache_entry_expire_resume,
716 .expire_cancel = cache_redis_cancel,
717};
static int const char char buffer[256]
Definition acutest.h:635
static uint8_t request[4]
static uint8_t reply[4]
#define UNCONST(_type, _ptr)
Remove const qualification from a pointer.
Definition build.h:186
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:391
#define unlikely(_x)
Definition build.h:455
#define UNUSED
Definition build.h:384
#define CONF_PARSER_TERMINATOR
Definition cf_parse.h:696
Defines a CONF_PAIR to C data type mapping.
Definition cf_parse.h:633
#define cf_log_err(_cf, _fmt,...)
Definition cf_util.h:340
int fr_redis_ct_map_bootstrap(fr_redis_ct_t *rtcluster, fr_coord_worker_t *cw, fr_coord_pair_reg_t *coord_pair_reg)
Initiate bootstrapping of the cluster map.
fr_redis_ct_t * fr_redis_ct_alloc(TALLOC_CTX *ctx, fr_event_list_t *el, fr_redis_conf_t *conf, fr_redis_trunk_active_t active, void *active_uctx, bool active_oneshot)
Allocate per-thread, per-cluster instance.
fr_redis_async_cmd_t * fr_redis_async_cmd_start(TALLOC_CTX *ctx, request_t *request, fr_redis_async_rcode_t *rcode, fr_redis_ct_t *rtcluster, uint8_t const *key, size_t key_len, fr_redis_command_set_t *cmds, bool read_only, fr_redis_ct_node_t *node)
Start running a command set on an async redis cluster.
fr_redis_async_rcode_t fr_redis_ct_map_get(fr_redis_ct_t *rtcluster, fr_coord_worker_t *cw, fr_coord_pair_reg_t *coord_pair_reg, bool force)
Initiate updating of the cluster map.
void fr_redis_async_cmd_cancel(fr_redis_async_cmd_t *cmd)
Cancel a Redis async command.
fr_redis_async_rcode_t fr_redis_async_cmd_resend(fr_redis_async_cmd_t *cmd)
Re-submit a redis async command set.
fr_redis_async_rcode_t fr_redis_async_cmd_redirect(fr_redis_async_cmd_t *cmd)
Re-submit a redis async command set on a different node.
Structure for holding the state of an async redis command set.
Thread local state for a cluster.
#define REDIS_ASYNC_COORD_CALLBACKS(_thread_type)
#define REDIS_ASYNC_START_RCODE_PROCESS(_rcode, _cluster, _cw, _coord_pair_reg, _error_msg, _error_ret)
Convenience macro to reduce boilerplate.
fr_coord_reg_t * fr_coord_register(fr_coord_reg_ctx_t *reg_ctx)
Register a coordinator.
Definition coord.c:137
fr_coord_worker_t * fr_coord_attach(TALLOC_CTX *ctx, fr_event_list_t *el, fr_coord_reg_t *coord_reg)
Attach a worker to a coordinator.
Definition coord.c:691
void fr_coord_deregister(fr_coord_reg_t *coord_reg)
De-register a coordinator.
Definition coord.c:172
int fr_coord_detach(fr_coord_worker_t *cw, bool exiting)
Signal a coordinator that a worker wants to detach.
Definition coord.c:652
A coordinator registration.
Definition coord.c:85
The worker end of worker <-> coordinator communication.
Definition coord.c:73
fr_coord_pair_reg_t * fr_coord_pair_register(fr_coord_pair_reg_ctx_t *reg_ctx)
Register a set of callbacks for pair list based coordinator messages.
Definition coord_pair.c:113
struct fr_coord_pair_reg_s fr_coord_pair_reg_t
Definition coord_pair.h:32
#define FR_COORD_PAIR_CB_CTX_SET(_in_cb, _out_cb, _reg)
Set up ctx on pair list callbacks.
Definition coord_pair.h:93
#define MEM(x)
Definition debug.h:38
#define ERROR(fmt,...)
Definition dhcpclient.c:40
fr_dict_attr_t const * fr_dict_root(fr_dict_t const *dict)
Return the root attribute of a dictionary.
Definition dict_util.c:2720
fr_dict_attr_t const ** out
Where to write a pointer to the resolved fr_dict_attr_t.
Definition dict.h:316
fr_dict_t const ** out
Where to write a pointer to the loaded/resolved fr_dict_t.
Definition dict.h:329
#define DICT_AUTOLOAD_TERMINATOR
Definition dict.h:335
Specifies an attribute which must be present for the module to function.
Definition dict.h:315
Specifies a dictionary which must be loaded/loadable for the module to function.
Definition dict.h:328
#define MODULE_MAGIC_INIT
Stop people using different module/library/server versions together.
Definition dl_module.h:63
talloc_free(hp)
TALLOC_CTX * unlang_interpret_frame_talloc_ctx(request_t *request)
Get a talloc_ctx which is valid only for this frame.
Definition interpret.c:2053
#define REXDENT()
Exdent (unindent) R* messages by one level.
Definition log.h:460
#define RDEBUG_ENABLED3
True if request debug level 1-3 messages are enabled.
Definition log.h:352
#define RDEBUG3(fmt,...)
Definition log.h:360
#define RERROR(fmt,...)
Definition log.h:315
#define RPERROR(fmt,...)
Definition log.h:319
#define RINDENT()
Indent R* messages by one level.
Definition log.h:447
@ L_DBG_LVL_3
3rd highest priority debug messages (-xxx | -Xx).
Definition log.h:69
@ FR_TYPE_DATE
Unix time stamp, always has value >2^31.
unsigned char uint8_t
fr_event_list_t * el
Event list to register any IO handlers and timers against.
Definition module_ctx.h:68
module_instance_t * mi
Module instance to detach.
Definition module_ctx.h:57
void * thread
Thread instance data.
Definition module_ctx.h:67
module_instance_t const * mi
Instance of the module being instantiated.
Definition module_ctx.h:64
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
Temporary structure to hold arguments for thread_instantiation calls.
Definition module_ctx.h:63
fr_redis_async_rcode_t fr_redis_command_set_rcode(fr_redis_command_set_t *cmds)
Extract the rcode from a command set.
Definition pipeline.c:1040
fr_redis_pipeline_status_t fr_redis_command_preformatted_add(fr_redis_command_set_t *cmds, char const *cmd_str, size_t cmd_len, fr_redis_command_complete_t complete, void *rctx)
Add an preformatted command to the command set as formatted by redisCommandFormat or it's variants.
Definition pipeline.c:482
fr_redis_command_set_t * fr_redis_command_set_alloc(TALLOC_CTX *ctx, request_t *request, fr_redis_command_set_complete_t complete, fr_redis_command_set_fail_t fail, void *rctx, bool autofree)
Allocate a new command set.
Definition pipeline.c:252
fr_redis_pipeline_status_t fr_redis_command_literal_add(fr_redis_command_set_t *cmds, char const *cmd_str, fr_redis_command_complete_t complete, void *rctx)
Add a literal command to the command set.
Definition pipeline.c:404
Represents a single command.
Definition pipeline.c:62
Represents a collection of pipelined commands.
Definition pipeline.c:94
@ FR_REDIS_PIPELINE_OK
No failure.
Definition pipeline.h:44
static const conf_parser_t config[]
Definition base.c:162
#define REDEBUG(fmt,...)
#define RDEBUG2(fmt,...)
void fr_redis_reply_print(fr_log_lvl_t lvl, redisReply *reply, request_t *request, int idx, fr_redis_rcode_t status)
Print the response data in a useful treelike form.
Definition redis.c:153
int redis_dict_init(void)
Load the Redis dictionaries.
Definition redis.c:136
#define REDIS_COMMON_CONFIG
Definition base.h:171
fr_redis_async_rcode_t
Definition base.h:90
@ REDIS_ASYNC_RCODE_MOVE
Attempt operation on an alternative node with remap.
Definition base.h:98
@ REDIS_ASYNC_RCODE_ERROR
Unrecoverable error.
Definition base.h:92
@ REDIS_ASYNC_RCODE_ASK
Attempt operation on an alternative node.
Definition base.h:97
@ REDIS_ASYNC_RCODE_TRY_AGAIN
Try the operation again.
Definition base.h:96
@ REDIS_ASYNC_RCODE_SUCCESS
Operation was successful.
Definition base.h:91
int fr_redis_tuple_from_map(TALLOC_CTX *pool, char const *out[], size_t out_len[], map_t *map)
Add a single map pair to an existing command string as three elements.
Definition redis.c:474
void fr_redis_version_print(void)
Print the version of libhiredis the server was built against.
Definition redis.c:107
fr_table_num_sorted_t const redis_reply_types[]
Definition redis.c:31
@ REDIS_RCODE_SUCCESS
Operation was successful.
Definition base.h:80
int fr_redis_reply_to_map(TALLOC_CTX *ctx, map_list_t *out, request_t *request, redisReply *key, redisReply *op, redisReply *value)
Convert a pair of redis reply objects to a map.
Definition redis.c:381
Configuration parameters for a redis connection.
Definition base.h:124
fr_value_box_t key
Key used to identify entry.
Definition rlm_cache.h:74
map_list_t maps
Head of the maps list.
Definition rlm_cache.h:79
fr_unix_time_t created
When the entry was created.
Definition rlm_cache.h:76
module_t common
Common fields for all loadable modules.
Definition rlm_cache.h:341
cache_status_t
Definition rlm_cache.h:39
@ CACHE_ERROR
Fatal error.
Definition rlm_cache.h:41
@ CACHE_YIELD
The driver has pushed an async.
Definition rlm_cache.h:44
@ CACHE_OK
Cache entry found/updated.
Definition rlm_cache.h:42
@ CACHE_MISS
Cache entry notfound.
Definition rlm_cache.h:43
fr_unix_time_t expires
When the entry expires.
Definition rlm_cache.h:77
Configuration for the rlm_cache module.
Definition rlm_cache.h:52
Definition rlm_cache.h:73
static cache_status_t cache_entry_find_resume(rlm_cache_entry_t **out, UNUSED rlm_cache_config_t const *config, void *instance, UNUSED request_t *request, UNUSED void *handle, void *rctx)
static int mod_detach(module_detach_ctx_t const *mctx)
static int mod_load(void)
fr_coord_worker_t * cw
Coord-worker for fetching cluster map.
static cache_status_t cache_entry_expire_resume(UNUSED rlm_cache_config_t const *config, void *instance, UNUSED request_t *request, UNUSED void *handle, void *rctx)
fr_redis_conf_t conf
Connection parameters for the Redis server.
static fr_dict_attr_t const * attr_cache_created
static int mod_coord_attach(module_thread_inst_ctx_t const *mctx)
static cache_status_t cache_entry_expire(UNUSED void **rctx_out, UNUSED rlm_cache_config_t const *config, void *instance, request_t *request, UNUSED void *handle, fr_value_box_t const *key)
Call delete the cache entry from redis.
static void cache_entry_free(rlm_cache_entry_t *c)
fr_dict_attr_autoload_t rlm_cache_redis_dict_attr[]
rlm_cache_driver_t rlm_cache_redis
rlm_cache_entry_t * entry
static void cache_entry_insert_results(request_t *request, UNUSED fr_redis_command_t *cmd, redisReply *reply, void *rctx)
tmpl_t * expires_attr
LHS of the Cache-Expires map.
static fr_dict_t const * dict_freeradius
static conf_parser_t driver_config[]
fr_value_box_t const * key
static void cache_entry_find_results(request_t *request, UNUSED fr_redis_command_t *cmd, redisReply *reply, void *rctx)
static void cache_entry_expire_results(request_t *request, UNUSED fr_redis_command_t *cmd, redisReply *reply, void *rctx)
static cache_status_t cache_entry_insert_resume(rlm_cache_entry_t **out, UNUSED rlm_cache_config_t const *config, void *instance, UNUSED request_t *request, UNUSED void *handle, void *rctx)
rlm_cache_redis_t const * inst
Module instance.
static fr_dict_t const * dict_redis
static fr_dict_attr_t const * attr_cache_expires
static int mod_thread_instantiate(module_thread_inst_ctx_t const *mctx)
module_instance_t const * mi
Module instance.
static int mod_coord_detach(module_thread_inst_ctx_t const *mctx)
fr_redis_ct_t * rtcluster
Per thread Redis cluster.
static int cache_redis_rctx_free(rlm_cache_redis_rctx_t *rctx)
static void cache_redis_cancel(UNUSED rlm_cache_config_t const *config, UNUSED void *instance, request_t *request, UNUSED void *handle, void *rctx)
fr_redis_async_cmd_t * cmd
fr_dict_autoload_t rlm_cache_redis_dict[]
static cache_status_t cache_redis_results(request_t *request, rlm_cache_redis_t *inst, fr_redis_command_set_t *cmds, fr_redis_async_cmd_t *cmd, cache_status_t rcode)
Process the results of Redis cache commands.
tmpl_t * created_attr
LHS of the Cache-Created map.
static cache_status_t cache_entry_find(UNUSED rlm_cache_entry_t **out, void **rctx_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 in redis.
static cache_status_t cache_entry_insert(UNUSED void **rctx_out, UNUSED rlm_cache_config_t const *config, void *instance, request_t *request, UNUSED void *handle, const rlm_cache_entry_t *c)
Insert a new entry into the data store.
static int mod_instantiate(module_inst_ctx_t const *mctx)
Create a new rlm_cache_redis instance.
fr_coord_pair_reg_t * coord_pair_reg
Coord pair registration.
fr_redis_command_set_t * cmds
fr_coord_reg_t * coord_reg
Coordinator registration.
int fr_schedule_worker_id(void)
Return the worker id for the current thread.
Definition schedule.c:111
struct map_s map_t
Definition map.h:33
#define MODULE_THREAD_INST(_ctype)
Definition module.h:260
char const * name
Instance name e.g. user_database.
Definition module.h:359
CONF_SECTION * conf
Module's instance configuration.
Definition module.h:353
void * data
Module's instance data.
Definition module.h:295
module_instance_t const * parent
Parent module's instance (if any).
Definition module.h:361
module_instantiate_t instantiate
Callback to allow the module to register any per-instance resources like sockets and file handles.
Definition module.h:227
void * data
Thread specific instance data.
Definition module.h:376
static module_thread_instance_t * module_thread(module_instance_t const *mi)
Retrieve module/thread specific instance for a module.
Definition module.h:515
Module instance data.
Definition module.h:289
#define tmpl_value(_tmpl)
Definition tmpl.h:960
@ TMPL_TYPE_DATA
Value in native boxed format.
Definition tmpl.h:145
static fr_dict_attr_t const * tmpl_attr_tail_da(tmpl_t const *vpt)
Return the last attribute reference da.
Definition tmpl.h:824
fr_slen_t tmpl_afrom_attr_str(TALLOC_CTX *ctx, tmpl_attr_error_t *err, tmpl_t **out, char const *name, tmpl_rules_t const *rules))
Parse a string into a TMPL_TYPE_ATTR_* type tmpl_t.
tmpl_t * tmpl_init_shallow(tmpl_t *vpt, tmpl_type_t type, fr_token_t quote, char const *name, ssize_t len, tmpl_rules_t const *t_rules))
Initialise a tmpl without copying the input name string.
PUBLIC int snprintf(char *string, size_t length, char *format, va_alist)
Definition snprintf.c:689
eap_aka_sim_process_conf_t * inst
Value pair map.
Definition map.h:77
fr_token_t op
The operator that controls insertion of the dst attribute.
Definition map.h:82
#define fr_table_str_by_value(_table, _number, _def)
Convert an integer to a string.
Definition table.h:804
#define talloc_zero_pooled_object(_ctx, _type, _num_subobjects, _total_subobjects_size)
Definition talloc.h:209
#define talloc_asprintf
Definition talloc.h:151
#define fr_unix_time_ispos(_a)
Definition time.h:372
static int64_t fr_unix_time_to_sec(fr_unix_time_t delta)
Definition time.h:506
@ T_BARE_WORD
Definition token.h:118
@ T_OP_SET
Definition token.h:82
static fr_slen_t head
Definition xlat.h:410
int fr_value_box_copy(TALLOC_CTX *ctx, fr_value_box_t *dst, const fr_value_box_t *src)
Copy value data verbatim duplicating any buffers.
Definition value.c:4434
#define fr_box_strvalue_len(_val, _len)
Definition value.h:340
static fr_sbuff_err_t char ** out
Definition value.h:1061
#define fr_value_box_init(_vb, _type, _enumv, _tainted)
Initialise a fr_value_box_t.
Definition value.h:634