The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
dedup.h
Go to the documentation of this file.
1#pragma once
2/*
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
16 */
17
18/**
19 * $Id: 7a78269699d3ce70968a517351184de1e6052081 $
20 * @file lib/bio/dedup.h
21 * @brief Binary IO abstractions for deduping of raw packets
22 *
23 * Read packets from a dedup bio. Once a packet is read, it is checked
24 * against a dedup tree. Duplicate packets are suppressed.
25 *
26 * The dedup bio creates the dedup tree. The application sets the `dedup_cmp` function, which compares the
27 * `dedup_ctx` of two #fr_bio_dedup_entry_t. Each protocol has its own requirements for dedup. For an
28 * unconnected socket, the dedup bio compares the addresses of the two entries before calling `dedup_cmp`.
29 *
30 * The dedup bio creates the dedup context of each packet with the `dedup_ctx` callback, searches the dedup
31 * tree, and inserts new entries. Only the dedup bio inserts entries into the dedup tree, or removes
32 * entries from the dedup tree.
33 *
34 * When `max_active` entries are in use, the dedup bio pauses reads, and calls the `read_blocked` callback.
35 * While reads are paused, fr_bio_read() on the dedup bio returns IO_WOULD_BLOCK, and any unread packet
36 * stays in the next bio. When an entry stops being in use, the dedup bio resumes reads, and calls the
37 * `read_resume` callback. The application sets the callbacks on the dedup bio with fr_bio_cb_set().
38 * Because reads stay paused until the `read_resume` callback returns, a read from inside the
39 * `read_resume` callback returns IO_WOULD_BLOCK. The `read_resume` callback should only mark the file
40 * descriptor (FD) as readable, and should not read from the dedup bio.
41 *
42 * @copyright 2024 Network RADIUS SAS (legal@networkradius.com)
43 */
44RCSIDH(lib_bio_dedup_h, "$Id: 7a78269699d3ce70968a517351184de1e6052081 $")
45
46#include <freeradius-devel/util/event.h>
47#include <freeradius-devel/util/rb.h>
48#include <freeradius-devel/bio/protocol.h>
49
51
52#ifndef _BIO_DEDUP_PRIVATE
54 void *uctx; //!< user-writable context
55 void *dedup_ctx; //!< context for dedup purposes
56 uint8_t *packet; //!< cached packet data.
57 size_t packet_size; //!< size of the cached packet data
58 void *reply_ctx; //!< reply ctx
59 uint8_t *reply; //!< reply cached by the application
60 size_t reply_size; //!< size of the cached reply
61
62 fr_rb_node_t dedup; //!< node in the dedup tree of the dedup bio
63};
64#endif
65
66typedef enum : uint8_t {
67 FR_BIO_DEDUP_EXPIRED = 0, //!< it's expired, and we have to delete it
68 FR_BIO_DEDUP_CANCELLED, //!< it was cancelled
70 FR_BIO_DEDUP_INTERNAL_ERROR, //!< something something, error.
71 FR_BIO_DEDUP_NO_REPLY, //!< We're not replying to this item.
73
74/** Tell the application that the packet of the current read has the entry `item`
75 *
76 * The dedup bio calls `receive` for a new packet only, and after the dedup bio has put `item` into the dedup
77 * tree and the active list. The application should store `item` in a field of `packet_ctx`, so that it can
78 * find the entry later. The callback MUST NOT call any other dedup function.
79 *
80 * @param bio the binary IO handler
81 * @param item the entry for the packet
82 * @param packet_ctx the `packet_ctx` passed to fr_bio_read()
83 */
85
86/** Callback on release the packet (timeout, or cancelled by the application)
87 *
88 * The dedup bio has already removed the entry from the dedup tree. The callback must not remove the
89 * entry from the dedup tree.
90 *
91 * The callback function should clean up any additonal resources associated with the packet. The resources
92 * MUST NOT be released until the data is either "released" or "cancelled".
93 *
94 * The packet will be cancelled after this call returns. The cancellation callback will NOT be run.
95 *
96 * @param bio the binary IO handler
97 * @param item the item to release
98 * @param reason why this packet is being released
99 */
101
102typedef fr_bio_dedup_entry_t *(*fr_bio_dedup_get_item_t)(fr_bio_t *bio, void *packet_ctx);
103
104typedef struct {
105 fr_event_list_t *el; //!< event list
106
107 fr_time_delta_t lifetime; //!< default lifetime of dedup entry
108
109 fr_protocol_dedup_ctx_t dedup_ctx; //!< create the dedup context of a packet. It MUST be set.
110 fr_cmp_t dedup_cmp; //!< compare the dedup contexts of two #fr_bio_dedup_entry_t.
111 ///< It MUST be set.
112 fr_cmp_t duplicate_cmp; //!< returns 0 when two entries with the same key hold the same
113 ///< packet, i.e. the newer packet is a duplicate. Non-zero
114 ///< means that the newer packet conflicts with the older
115 ///< packet. It MUST be set.
116
117 fr_bio_dedup_receive_t receive; //!< tell the application which entry the dedup bio created
118 ///< for a new packet. It MUST be set.
119 fr_bio_dedup_release_t release; //!< called to release a packet. It MUST be set.
120 fr_bio_dedup_get_item_t get_item; //!< turn a packet_ctx into a #fr_bio_dedup_entry_t.
121 ///< May be NULL.
122
123 bool connected; //!< this BIO is for a connected socket, where all packets
124 ///< have the same address.
125
126 size_t offset; //!< where #fr_bio_fd_packet_ctx_t is stored in `packet_ctx`.
127 ///< MUST be the `offset` passed to fr_bio_fd_alloc(). Only used
128 ///< when `connected` is false. Only used for unconnected sockets,
129 ///< and even then, only for AF_INET and AF_INET6.
130
131 uint32_t max_active; //!< maximum number of entries in use. At the limit, the dedup
132 ///< bio pauses reads. Zero means 1024. The maximum is (1 << 18).
133
134 uint32_t max_free; //!< maximum number of entries in the free list. The dedup bio
135 ///< frees any entry over the limit. Zero means 1024. The
136 ///< maximum is (1 << 18).
138
139fr_bio_t *fr_bio_dedup_alloc(TALLOC_CTX *ctx, fr_bio_dedup_config_t const *cfg, fr_bio_t *next) CC_HINT(nonnull);
140
142
144
146
148
static int packet_ctx
the tests pass only the address of packet_ctx
#define RCSIDH(h, id)
Definition build.h:610
int fr_bio_dedup_conflict(fr_bio_t *bio, fr_bio_dedup_entry_t *item)
Mark an entry as conflicting with a newer request.
Definition dedup.c:1496
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.
Definition dedup.c:368
fr_cmp_t duplicate_cmp
returns 0 when two entries with the same key hold the same packet, i.e.
Definition dedup.h:112
fr_protocol_dedup_ctx_t dedup_ctx
create the dedup context of a packet. It MUST be set.
Definition dedup.h:109
void * uctx
user-writable context
Definition dedup.c:131
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.
Definition dedup.c:1362
fr_cmp_t dedup_cmp
compare the dedup contexts of two fr_bio_dedup_entry_t.
Definition dedup.h:110
bool connected
this BIO is for a connected socket, where all packets have the same address.
Definition dedup.h:123
fr_bio_dedup_receive_t receive
tell the application which entry the dedup bio created for a new packet.
Definition dedup.h:117
fr_event_list_t * el
event list
Definition dedup.h:105
fr_bio_dedup_get_item_t get_item
turn a packet_ctx into a fr_bio_dedup_entry_t.
Definition dedup.h:120
void fr_bio_dedup_entry_cancel(fr_bio_t *bio, fr_bio_dedup_entry_t *item)
Cancel one item.
Definition dedup.c:1344
int fr_bio_dedup_no_reply(fr_bio_t *bio, fr_bio_dedup_entry_t *item)
uint32_t max_active
maximum number of entries in use.
Definition dedup.h:131
fr_time_delta_t lifetime
default lifetime of dedup entry
Definition dedup.h:107
fr_bio_t * fr_bio_dedup_alloc(TALLOC_CTX *ctx, fr_bio_dedup_config_t const *cfg, fr_bio_t *next)
Allocate a fr_bio_dedup_t.
Definition dedup.c:1641
size_t packet_size
size of the cached packet data
Definition dedup.c:134
void(* fr_bio_dedup_receive_t)(fr_bio_t *bio, fr_bio_dedup_entry_t *item, void *packet_ctx)
Tell the application that the packet of the current read has the entry item
Definition dedup.h:84
void * dedup_ctx
context for dedup purposes
Definition dedup.c:132
uint8_t * reply
reply cached by the application
Definition dedup.c:136
size_t reply_size
size of the cached reply
Definition dedup.c:137
void * reply_ctx
reply ctx
Definition dedup.c:135
size_t offset
where fr_bio_fd_packet_ctx_t is stored in packet_ctx.
Definition dedup.h:126
uint8_t * packet
cached packet data for finding duplicates
Definition dedup.c:133
fr_rb_node_t dedup
node in fr_bio_dedup_t.dedup
Definition dedup.c:139
uint32_t max_free
maximum number of entries in the free list.
Definition dedup.h:134
fr_bio_dedup_entry_t *(* fr_bio_dedup_get_item_t)(fr_bio_t *bio, void *packet_ctx)
Definition dedup.h:102
fr_bio_dedup_release_reason_t
Definition dedup.h:66
@ FR_BIO_DEDUP_WRITE_ERROR
IO error.
Definition dedup.h:69
@ FR_BIO_DEDUP_INTERNAL_ERROR
something something, error.
Definition dedup.h:70
@ FR_BIO_DEDUP_CANCELLED
it was cancelled
Definition dedup.h:68
@ FR_BIO_DEDUP_EXPIRED
it's expired, and we have to delete it
Definition dedup.h:67
@ FR_BIO_DEDUP_NO_REPLY
We're not replying to this item.
Definition dedup.h:71
fr_bio_dedup_release_t release
called to release a packet. It MUST be set.
Definition dedup.h:119
void(* fr_bio_dedup_release_t)(fr_bio_t *bio, fr_bio_dedup_entry_t *item, fr_bio_dedup_release_reason_t reason)
Callback on release the packet (timeout, or cancelled by the application)
Definition dedup.h:100
Definition dedup.c:130
Stores all information relating to an event list.
Definition event.c:415
static void * item(fr_lst_t const *lst, fr_lst_index_t idx)
Definition lst.c:121
unsigned int uint32_t
long int ssize_t
unsigned char uint8_t
fr_cmp_ret_t(* fr_cmp_t)(void const *a, void const *b)
Definition misc.h:40
void *(* fr_protocol_dedup_ctx_t)(TALLOC_CTX *ctx, uint8_t const *packet, size_t packet_len, void *uctx)
Create the dedup context of a packet.
Definition protocol.h:65
A time delta, a difference in time measured in nanoseconds.
Definition time.h:80
"server local" time.
Definition time.h:69
int nonnull(2, 5))