![]() |
The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
|
Binary IO abstractions for deduping packets. More...
#include <freeradius-devel/bio/bio_priv.h>#include <freeradius-devel/bio/null.h>#include <freeradius-devel/bio/buf.h>#include <freeradius-devel/bio/dedup.h>
Include dependency graph for dedup.c:Go to the source code of this file.
Data Structures | |
| struct | fr_bio_dedup_entry_s |
| union | fr_bio_dedup_entry_s.__unnamed19__ |
| struct | fr_bio_dedup_entry_s.__unnamed19__.__unnamed21__ |
| struct | fr_bio_dedup_s |
Macros | |
| #define | _BIO_DEDUP_PRIVATE |
Typedefs | |
| typedef struct fr_bio_dedup_list_s | fr_bio_dedup_list_t |
| typedef struct fr_bio_dedup_s | fr_bio_dedup_t |
Functions | |
| static fr_cmp_ret_t | _entry_cmp (void const *one, void const *two) |
| 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_blocked (fr_bio_dedup_t *my, fr_bio_dedup_entry_t *item, ssize_t rcode) |
| The write is blocked. | |
| 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". | |
| 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. | |
| static ssize_t | fr_bio_dedup_buffer_write (fr_bio_dedup_t *my) |
| Write data from our local buffer to the next bio. | |
| int | fr_bio_dedup_conflict (fr_bio_t *bio, fr_bio_dedup_entry_t *item) |
| Mark an entry as conflicting with a newer request. | |
| void | fr_bio_dedup_entry_cancel (fr_bio_t *bio, fr_bio_dedup_entry_t *item) |
| Cancel one item. | |
| 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. | |
| 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. | |
| static ssize_t | fr_bio_dedup_flush_pending (fr_bio_dedup_t *my) |
| Flush any packets in the pending queue. | |
| 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". | |
| 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 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_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 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. | |
| 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. | |
| static void | fr_bio_dedup_timer (UNUSED fr_timer_list_t *tl, fr_time_t now, void *uctx) |
| Release every entry which expires within 10ms of now. | |
| static int | fr_bio_dedup_timer_reset (fr_bio_dedup_t *my) |
| Reset the timer after changing the rb tree. | |
| static void | fr_bio_dedup_timer_reset_item (fr_bio_dedup_t *my, fr_bio_dedup_entry_t *item) |
| 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. | |
| 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. | |
| 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. | |
| static int | fr_bio_dedup_write_resume (fr_bio_t *bio) |
| Resume writes. | |
Binary IO abstractions for deduping packets.
The dedup BIO receives packets from the network, and allows for deduplication of requests, so that duplicate requests are only processed once. In addition, if a reply is available, a duplicate request will result in a duplicate reply.
The application creates the dedup tree, and sets the compare function. The compare function has to take two fr_bio_dedup_entry_t, and then checks packet_ctx. Each protocol has its own requirements for dedup :( The application searches the dedup tree, and inserts new entries in the fr_bio_dedup_receive_t callback. Only the dedup bio removes entries from the dedup tree, or when the application calls fr_bio_dedup_conflict().
When packets are read() from the next bio, the fr_bio_dedup_receive_t callback is run. That callback tells the dedup BIO whether or not the packet should be received, and whether or not the packet should be returned to the caller. The receive callback is also passed a fr_bio_dedup_entry_t pointer, where the packet_ctx, packet, and size are already filled out. This entry is used to correlate requests and replies.
When packets are write() to the network, the fr_bio_dedup_get_item_t callback is called to get the previously cached fr_bio_dedup_entry_t pointer. This is because there is no generic way to get an additional context to this BIO via the write() routine. i.e. the packet_ctx for write() may include things like src/dst ip/port, and therefore can't always be an fr_bio_dedup_entry_t. The caller should associate the fr_bio_dedup_entry_t with the packet_ctx for the reply. The get_item() routine can then return the entry.
For simplicity, the next bio should be a memory one. That way the read() can read more than one packet if necessary. And the write() can cache a partial packet if it blocks.
The entry needs to be cached in order to maintain the internal tracking used by the dedup BIO.
On client retransmit, the fr_bio_dedup_receive_t callback is run, just as if it is a new packet. The dedup BIO does not know if the received data is a new packet until the fr_bio_dedup_receive_t callback says so. On duplicate client request, the fr_bio_dedup_receive_t callback can call fr_bio_dedup_respond() to send a duplicate reply. That call bypasses the normal dedup stack, and writes directly to the next bio.
The calling application can also call fr_bio_dedup_respond() as soon as it has a reply. i.e. skip the BIO write() call. That works, and is safe.
The dedup BIO tracks a number of lists / trees internally. Packets which are received but which have no reply are in an "active" list. Packets which have a reply are in an "expired" RB tree, where a timer is set to expire packets. If a write() call results in a partial write, that packet is put into a "partially written" state. If multiple calls to write() are done when writing is blocked, the replies are put into a "pending" state. A pending reply is added to the expiry tree, so that it's cleaned up if it expires before the socket becomes writable again.
The calling application can always call fr_bio_dedup_cancel() to cancel or expire a packet. This call is safe, and can be made at any time, no matter what state the packet is in.
In some cases, the application doesn't reply to the client. These are:
Definition in file dedup.c.
| struct fr_bio_dedup_entry_s |
Collaboration diagram for fr_bio_dedup_entry_s:| Data Fields | ||
|---|---|---|
| union fr_bio_dedup_entry_s.__unnamed19__ | __unnamed__ | |
| fr_rb_node_t | dedup | node in fr_bio_dedup_config_t.dedup |
| uint8_t * | packet |
cached packet data for finding duplicates cached packet data. |
| void * | packet_ctx | packet_ctx for dedup purposes |
| size_t | packet_size | size of the cached packet data |
| uint8_t * | reply | reply cached by the application |
| void * | reply_ctx | reply ctx |
| size_t | reply_size | size of the cached reply |
| void * | uctx | user-writable context |
| union fr_bio_dedup_entry_s.__unnamed19__ |
| Data Fields | ||
|---|---|---|
| struct fr_bio_dedup_entry_s.__unnamed19__.__unnamed21__ | __unnamed__ | |
| struct fr_bio_dedup_entry_s.__unnamed19__.__unnamed21__ |
| Data Fields | ||
|---|---|---|
| fr_rb_node_t | node | for the expiry timers |
| struct fr_bio_dedup_s |
Collaboration diagram for fr_bio_dedup_s:| Data Fields | ||
|---|---|---|
| fr_bio_buf_t | buffer | |
| fr_bio_dedup_config_t | config | |
| fr_rb_tree_t * | dedup | the application's dedup tree, from fr_bio_dedup_config_t |
| fr_event_list_t * | el | |
| fr_timer_t * | ev | |
| fr_bio_dedup_entry_t * | first | |
| FR_BIO_COMMON | ||
| fr_bio_dedup_get_item_t | get_item | turn a packet_ctx into a fr_bio_dedup_entry_t |
| fr_bio_dedup_entry_t * | partial | |
| fr_rb_tree_t | rb | expire list |
| fr_bio_dedup_receive_t | receive | called when we receive a potentially new packet |
| fr_bio_dedup_release_t | release | called to release a packet |
| typedef struct fr_bio_dedup_list_s fr_bio_dedup_list_t |
| typedef struct fr_bio_dedup_s fr_bio_dedup_t |
| enum fr_bio_dedup_state_t : uint8_t |
|
static |
| 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.
Definition at line 1499 of file dedup.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
The write is blocked.
We couldn't write out the entire packet, the bio is blocked. Don't write anything else until we become unblocked!
Do NOT free the timer. We can still expire old entries. The partially written entry is not in the expiry tree, because expiring a partially written entry would nuke it's packet. And then write only a partial packet to the socket.
Definition at line 860 of file dedup.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
The write is blocked, but we don't have "item".
We couldn't write out the entire packet, the bio is blocked. Don't write anything else until we become unblocked!
Do NOT free the timer. We can still expire old entries.
Definition at line 958 of file dedup.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
|
static |
| int fr_bio_dedup_conflict | ( | fr_bio_t * | bio, |
| fr_bio_dedup_entry_t * | item | ||
| ) |
Mark an entry as conflicting with a newer request.
The application calls this function from the fr_bio_dedup_receive_t callback, when a newer request is found to conflict with an older request which the application is still processing. The old entry stays in the active list, but is marked as FR_BIO_DEDUP_STATE_CLEANUP. The entry is then released when the application finishes processing the older request, and calls fr_bio_dedup_respond().
If the application has already finished processing the older request, the entry is released now, with reason FR_BIO_DEDUP_CANCELLED.
Any other state is an application error. Because the dedup bio has already removed a cancelled, cleanup, or free entry from the dedup tree, the application cannot find the entry to pass to this function.
| bio | the binary IO handler |
| item | the older entry, which conflicts with the request passed to fr_bio_dedup_receive_t |
Definition at line 1397 of file dedup.c.
Here is the call graph for this function:
Here is the caller graph for this function:| void fr_bio_dedup_entry_cancel | ( | fr_bio_t * | bio, |
| fr_bio_dedup_entry_t * | item | ||
| ) |
| 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.
| bio | the binary IO handler |
| item | the dedup context from #fr_bio_dedup_respond_t |
| expires | the new expiry time |
Definition at line 1263 of file dedup.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
|
static |
| 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".
The application calls this function while it is processing the request, and then later calls fr_bio_dedup_respond() once, when it has finished processing the request. fr_bio_dedup_respond() does not write a reply, and moves the entry to the expiry tree. The entry is cleaned up when it expires, with reason FR_BIO_DEDUP_NO_REPLY. Calling fr_bio_dedup_respond() a second time is an error.
The entry must still be active, i.e. received, but not replied. Or, it must be in the FR_BIO_DEDUP_STATE_CLEANUP, as we can mark a request as conflicting before the application has finished processing it.
Any other state is an application error.
| bio | the binary IO handler |
| item | the dedup context from fr_bio_dedup_receive_t |
Definition at line 1336 of file dedup.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
inlinestatic |
Return true if an entry in state FR_BIO_DEDUP_STATE_NO_REPLY is in the expiry tree.
We need to distinguish "no reply, and still being processed" (in the active list) from "no reply, and waiting to expire" (in the expiry tree). We do that by checking if the expiry time is zero.
Definition at line 204 of file dedup.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
Add an entry to the pending list, because writes are blocked.
A pending entry is also in the expiry tree. If the next bio stays blocked until the entry expires, the expiry timer releases the entry, and the reply is never written.
Definition at line 315 of file dedup.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
|
static |
Add an entry to the expiry tree once the reply has been written, or once a "no reply" entry is done.
The expiry time is set from now, i.e. from when the reply was written (or for a "no reply" entry, from when the application called fr_bio_dedup_respond()), and not from when the reply was queued.
Expiration for an entry is the maximum of now + expiry, or now + lifetime. i.e. The continued reception of duplicate requests indicates that we should extend the expiry time.
Also updates the timers, because otherwise all callers have to do that.
Definition at line 234 of file dedup.c.
Here is the call graph for this function:
Here is the caller graph for this function:| 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.
This function should be called by the respond() callback to re-send a duplicate reply.
It can also be called by the application when it first has a response to the request.
| bio | the binary IO handler |
| item | the dedup context from #fr_bio_dedup_sent_t |
Definition at line 351 of file dedup.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
|
static |
Release every entry which expires within 10ms of now.
Because a pending entry is in the expiry tree, the timer also releases pending entries. Pending entries which expire mean that they were never written, so we can just clean them up.
Definition at line 1020 of file dedup.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
|
inlinestatic |
|
static |
Write raw data to the bio.
Due to the BIO API, this function can be passed a NULL buffer (for flushing the BIOs), and it can't be passed a fr_bio_dedup_entry_t. Instead, it has to be passed a "void *packet_ctx", and it calls the fr_bio_dedup_get_item_t callback to find the entry.
When there is an entry, the reply in the entry must be the data which is being written, and this function calls fr_bio_dedup_respond(). The one difference is that for an entry marked with fr_bio_dedup_no_reply() or fr_bio_dedup_conflict(), this function returns the full size instead of 0. When there is no entry, the data is written as un-tracked data.
The application can call fr_bio_dedup_respond() directly instead of calling this function.
Definition at line 1060 of file dedup.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
There's a partial packet written.
Write all of that one first, before writing another packet.
The packet can either be cancelled, or IO blocked. In either case, we must write this packet before we can write another one.
Definition at line 791 of file dedup.c.
Here is the call graph for this function:
Here is the caller graph for this function:
|
static |
Resume writes.
Write the rest of any partly written reply or data, and then write the replies on the pending list.
Definition at line 987 of file dedup.c.
Here is the call graph for this function:
Here is the caller graph for this function:
1.9.8