79#include <freeradius-devel/bio/bio_priv.h>
80#include <freeradius-devel/bio/null.h>
81#include <freeradius-devel/bio/buf.h>
83#define _BIO_DEDUP_PRIVATE
84#include <freeradius-devel/bio/dedup.h>
240 switch (
item->state) {
247 (void) fr_bio_dedup_list_remove(&my->active,
item);
251 (void) fr_bio_dedup_list_remove(&my->active,
item);
317 switch (
item->state) {
319 (void) fr_bio_dedup_list_remove(&my->active,
item);
334 fr_bio_dedup_pending_list_insert_tail(&my->pending,
item);
357 switch (
item->state) {
370 return item->reply_size;
394 return item->reply_size;
426 return item->reply_size;
443 if ((
size_t) rcode ==
item->reply_size) {
451 if ((rcode == 0) || (rcode ==
fr_bio_error(IO_WOULD_BLOCK))) {
455 return item->reply_size;
506 if (first == my->
first)
return 0;
530 switch (
item->state) {
536 fr_bio_dedup_list_remove(&my->active,
item);
545 fr_bio_dedup_list_remove(&my->active,
item);
576 (void) fr_bio_dedup_pending_list_remove(&my->pending,
item);
606 if (fr_bio_dedup_list_num_elements(&my->free) >= my->
config.
max_free) {
614 item->packet_ctx = NULL;
617 fr_bio_dedup_list_insert_head(&my->free,
item);
623 if (my->read_paused) {
626 my->read_paused =
false;
628 if (my->cb.read_resume) (void) my->cb.read_resume(&my->bio);
650 if (fr_bio_dedup_pending_list_num_elements(&my->pending) == 0)
return 0;
664 while ((
item = fr_bio_dedup_pending_list_pop_head(&my->pending)) != NULL) {
684 if ((rcode == 0) || (rcode ==
fr_bio_error(IO_WOULD_BLOCK))) {
685 fr_bio_dedup_pending_list_insert_head(&my->pending,
item);
704 if ((
size_t) rcode ==
item->reply_size) {
769 if (rcode <= 0)
return rcode;
804 if (rcode <= 0)
return rcode;
838 if (rcode < 0)
return rcode;
864 fr_assert((
size_t) rcode < item->reply_size);
868 switch (
item->state) {
870 (void) fr_bio_dedup_list_remove(&my->active,
item);
929 if (rcode <= 0)
return rcode;
942 if (rcode < 0)
return rcode;
1001 rcode = my->bio.write(bio, NULL, NULL, SIZE_MAX);
1004 if ((rcode < 0) && (rcode !=
fr_bio_error(IO_WOULD_BLOCK)))
return (
int)rcode;
1012 return (fr_bio_dedup_pending_list_num_elements(&my->pending) == 0);
1091 switch (
item->state) {
1099 if (rcode < 0)
return rcode;
1129 if (rcode <= 0)
return rcode;
1131 if ((
size_t) rcode == size)
return rcode;
1146 if (my->read_paused)
return fr_bio_error(IO_WOULD_BLOCK);
1160 if (rcode <= 0)
return rcode;
1165 item = fr_bio_dedup_list_pop_head(&my->free);
1176 .packet_size = (
size_t) rcode,
1204 fr_bio_dedup_list_insert_tail(&my->active,
item);
1212 my->read_paused =
true;
1214 if (my->cb.read_blocked) (void) my->cb.read_blocked(&my->bio);
1230 if (ret != 0)
return ret;
1267 switch (
item->state) {
1288 item->expires = expires;
1300 item->expires = expires;
1338 switch (
item->state) {
1408 switch (
item->state) {
1462 (void) fr_bio_dedup_pending_list_remove(&my->pending,
item);
1468 fr_assert(fr_bio_dedup_pending_list_num_elements(&my->pending) == 0);
1470 while ((
item = fr_bio_dedup_list_pop_head(&my->active)) != NULL) {
1510 if (!cfg->
dedup)
return NULL;
1513 if (!my)
return NULL;
1519 fr_bio_dedup_list_init(&my->free);
1521 fr_bio_dedup_list_init(&my->active);
1522 fr_bio_dedup_pending_list_init(&my->pending);
static int const char char buffer[256]
fr_bio_write_t _CONST write
write to the underlying bio
fr_bio_read_t _CONST read
read from the underlying bio
static fr_bio_t * fr_bio_next(fr_bio_t *bio)
static int packet_ctx
the tests pass only the address of packet_ctx
static void fr_bio_chain(fr_bio_t *first, fr_bio_t *second)
Chain one bio after another.
int fr_bio_buf_alloc(TALLOC_CTX *ctx, fr_bio_buf_t *bio_buf, size_t size)
ssize_t fr_bio_buf_write(fr_bio_buf_t *bio_buf, const void *buffer, size_t size)
uint8_t * start
start of the buffer
static size_t fr_bio_buf_used(fr_bio_buf_t const *bio_buf)
static void fr_bio_buf_reset(fr_bio_buf_t *bio_buf)
uint8_t * read
where in the buffer reads are taken from
static size_t fr_bio_buf_size(fr_bio_buf_t const *bio_buf)
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
fr_cmp_ret_t
Result of an ordering comparison.
int fr_bio_dedup_conflict(fr_bio_t *bio, fr_bio_dedup_entry_t *item)
Mark an entry as conflicting with a newer request.
ssize_t fr_bio_dedup_respond(fr_bio_t *bio, fr_bio_dedup_entry_t *item)
Resend a reply when we receive a duplicate request.
static int fr_bio_dedup_shutdown(fr_bio_t *bio)
Remove the dedup cache.
fr_bio_dedup_release_t release
called to release a packet
static void fr_bio_dedup_timer_reset_item(fr_bio_dedup_t *my, fr_bio_dedup_entry_t *item)
static int fr_bio_dedup_write_resume(fr_bio_t *bio)
Resume writes.
static int fr_bio_dedup_buffer_save(fr_bio_dedup_t *my, uint8_t const *buffer, size_t size, ssize_t rcode)
Save partially written data to our local buffer.
int fr_bio_dedup_entry_extend(fr_bio_t *bio, fr_bio_dedup_entry_t *item, fr_time_t expires)
Extend the expiry time for an entry.
int fr_bio_dedup_no_reply(UNUSED fr_bio_t *bio, fr_bio_dedup_entry_t *item)
Mark an entry as "we intend to never reply".
fr_rb_tree_t * dedup
the application's dedup tree, from fr_bio_dedup_config_t
fr_bio_dedup_config_t config
fr_bio_dedup_get_item_t get_item
turn a packet_ctx into a fr_bio_dedup_entry_t
static ssize_t fr_bio_dedup_write_partial(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size)
There's a partial packet written.
void fr_bio_dedup_entry_cancel(fr_bio_t *bio, fr_bio_dedup_entry_t *item)
Cancel one item.
static bool fr_bio_dedup_no_reply_expiring(fr_bio_dedup_entry_t const *item)
Return true if an entry in state FR_BIO_DEDUP_STATE_NO_REPLY is in the expiry tree.
static ssize_t fr_bio_dedup_write_data(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size)
There's a partial block of data written.
fr_bio_dedup_entry_t * partial
static void fr_bio_dedup_pending_add(fr_bio_dedup_t *my, fr_bio_dedup_entry_t *item)
Add an entry to the pending list, because writes are blocked.
static ssize_t fr_bio_dedup_flush_pending(fr_bio_dedup_t *my)
Flush any packets in the pending queue.
static ssize_t fr_bio_dedup_blocked(fr_bio_dedup_t *my, fr_bio_dedup_entry_t *item, ssize_t rcode)
The write is blocked.
static void fr_bio_dedup_timer(UNUSED fr_timer_list_t *el, fr_time_t now, void *uctx)
Release every entry which expires within 10ms of now.
fr_rb_tree_t rb
expire list
static void fr_bio_dedup_replied(fr_bio_dedup_t *my, fr_bio_dedup_entry_t *item)
Add an entry to the expiry tree once the reply has been written, or once a "no reply" entry is done.
fr_bio_dedup_receive_t receive
called when we receive a potentially new packet
static ssize_t fr_bio_dedup_blocked_data(fr_bio_dedup_t *my, uint8_t const *buffer, size_t size, ssize_t rcode)
The write is blocked, but we don't have "item".
@ FR_BIO_DEDUP_STATE_NO_REPLY
Active, or waiting to expire. Don't reply.
@ FR_BIO_DEDUP_STATE_CLEANUP
Active, but conflicting request.
@ FR_BIO_DEDUP_STATE_PARTIAL
Partially written.
@ FR_BIO_DEDUP_STATE_FREE
@ FR_BIO_DEDUP_STATE_ACTIVE
Received, but not replied.
@ FR_BIO_DEDUP_STATE_CANCELLED
Partially written, and then cancelled.
@ FR_BIO_DEDUP_STATE_PENDING
Have a reply, but we're can't (yet) write it out.
@ FR_BIO_DEDUP_STATE_REPLIED
Replied, and waiting for it to expire.
fr_bio_t * fr_bio_dedup_alloc(TALLOC_CTX *ctx, fr_bio_dedup_receive_t receive, fr_bio_dedup_release_t release, fr_bio_dedup_get_item_t get_item, fr_bio_dedup_config_t const *cfg, fr_bio_t *next)
Allocate a fr_bio_dedup_t.
static ssize_t fr_bio_dedup_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
Write raw data to the bio.
fr_bio_dedup_entry_t * first
static ssize_t fr_bio_dedup_buffer_write(fr_bio_dedup_t *my)
Write data from our local buffer to the next bio.
static void fr_bio_dedup_entry_free(fr_bio_dedup_t *my, fr_bio_dedup_entry_t *item)
Put an entry which is no longer in use into the free list, or free the entry if the free list is full...
struct fr_bio_dedup_list_s fr_bio_dedup_list_t
static int fr_bio_dedup_timer_reset(fr_bio_dedup_t *my)
Reset the timer after changing the rb tree.
static ssize_t fr_bio_dedup_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
static void fr_bio_dedup_release(fr_bio_dedup_t *my, fr_bio_dedup_entry_t *item, fr_bio_dedup_release_reason_t reason)
Release an entry back to the free list.
static fr_cmp_ret_t _entry_cmp(void const *one, void const *two)
void * uctx
user-writable context
void(* fr_bio_dedup_release_t)(fr_bio_t *bio, fr_bio_dedup_entry_t *dedup_ctx, fr_bio_dedup_release_reason_t reason)
Callback on release the packet (timeout, or cancelled by the application)
fr_event_list_t * el
event list
fr_rb_tree_t * dedup
application-supplied dedup tree, using the inline fr_bio_dedup_entry_t.dedup node.
uint32_t max_active
maximum number of entries in use.
fr_time_delta_t lifetime
default lifetime of dedup entry
size_t packet_size
size of the cached packet data
struct fr_bio_dedup_entry_s fr_bio_dedup_entry_t
void * packet_ctx
packet_ctx for dedup purposes
uint8_t * reply
reply cached by the application
size_t reply_size
size of the cached reply
void * reply_ctx
reply ctx
uint8_t * packet
cached packet data for finding duplicates
fr_rb_node_t dedup
node in fr_bio_dedup_config_t.dedup
uint32_t max_free
maximum number of entries in the free list.
bool(* fr_bio_dedup_receive_t)(fr_bio_t *bio, fr_bio_dedup_entry_t *dedup_ctx, void *packet_ctx)
Callback on read to see if we should receive the packet.
fr_bio_dedup_entry_t *(* fr_bio_dedup_get_item_t)(fr_bio_t *bio, void *packet_ctx)
fr_bio_dedup_release_reason_t
@ FR_BIO_DEDUP_WRITE_ERROR
IO error.
@ FR_BIO_DEDUP_CANCELLED
it was cancelled
@ FR_BIO_DEDUP_EXPIRED
it's expired, and we have to delete it
@ FR_BIO_DEDUP_NO_REPLY
We're not replying to this item.
#define FR_DLIST_TYPES(_name)
Define type specific wrapper structs for dlists.
#define FR_DLIST_ENTRY(_name)
Expands to the type name used for the entry wrapper structure.
#define FR_DLIST_FUNCS(_name, _element_type, _element_entry)
Define type specific wrapper functions for dlists.
#define FR_DLIST_HEAD(_name)
Expands to the type name used for the head wrapper structure.
int fr_bio_destructor(fr_bio_t *bio)
Free this bio.
Stores all information relating to an event list.
static void * item(fr_lst_t const *lst, fr_lst_index_t idx)
void * fr_rb_iter_init_inorder(fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Initialise an in-order iterator.
void fr_rb_iter_delete_inorder(fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Remove the current node from the tree.
void * fr_rb_remove_by_inline_node(fr_rb_tree_t *tree, fr_rb_node_t *node)
Remove an entry from the tree, using the node structure, without freeing the data.
void * fr_rb_first(fr_rb_tree_t *tree)
int fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
static bool fr_rb_node_inline_in_tree(fr_rb_node_t const *node)
Check to see if an item is in a tree by examining its inline fr_rb_node_t.
#define fr_rb_inline_init(_tree, _type, _field, _data_cmp, _data_free)
Initialises a red black tree.
Iterator structure for in-order traversal of an rbtree.
The main red black tree structure.
static int talloc_const_free(void const *ptr)
Free const'd memory.
static fr_time_delta_t fr_time_delta_from_msec(int64_t msec)
static fr_time_t fr_time_add_time_delta(fr_time_t a, fr_time_delta_t b)
#define fr_time_wrap(_time)
#define fr_time_lteq(_a, _b)
#define fr_time_eq(_a, _b)
static fr_cmp_ret_t fr_time_cmp(fr_time_t a, fr_time_t b)
Compare two fr_time_t values.
#define fr_time_add(_a, _b)
Add a time/time delta together.
#define fr_time_gt(_a, _b)
static fr_event_list_t * el