The FreeRADIUS server $Id: a4e7f8c94e8038b7627bcf52356b7ed15955c1d7 $
Loading...
Searching...
No Matches
dedup.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: 598608888e1452acb0a5ec3cc835f554682f39fe $
19 * @file lib/bio/dedup.c
20 * @brief Binary IO abstractions for deduping packets.
21 *
22 * The dedup BIO receives packets from the network, and allows for deduplication of requests, so that
23 * duplicate requests are only processed once. In addition, if a reply is available, a duplicate request will
24 * result in a duplicate reply.
25 *
26 * The application creates the dedup tree, and sets the compare function. The compare function has to take
27 * two #fr_bio_dedup_entry_t, and then checks `packet_ctx`. Each protocol has its own requirements for dedup
28 * :( The application searches the dedup tree, and inserts new entries in the #fr_bio_dedup_receive_t
29 * callback. Only the dedup bio removes entries from the dedup tree, or when the application calls
30 * fr_bio_dedup_conflict().
31 *
32 * When packets are read() from the next bio, the #fr_bio_dedup_receive_t callback is run. That callback
33 * tells the dedup BIO whether or not the packet should be received, and whether or not the packet should be
34 * returned to the caller. The receive callback is also passed a #fr_bio_dedup_entry_t pointer, where the
35 * packet_ctx, packet, and size are already filled out. This entry is used to correlate requests and replies.
36 *
37 * When packets are write() to the network, the #fr_bio_dedup_get_item_t callback is called to get the
38 * previously cached #fr_bio_dedup_entry_t pointer. This is because there is no generic way to get an
39 * additional context to this BIO via the write() routine. i.e. the packet_ctx for write() may include things
40 * like src/dst ip/port, and therefore can't always be an #fr_bio_dedup_entry_t. The caller should associate
41 * the #fr_bio_dedup_entry_t with the packet_ctx for the reply. The get_item() routine can then return the entry.
42 *
43 * For simplicity, the next bio should be a memory one. That way the read() can read more than one packet if
44 * necessary. And the write() can cache a partial packet if it blocks.
45 *
46 * The entry needs to be cached in order to maintain the internal tracking used by the dedup BIO.
47 *
48 * On client retransmit, the #fr_bio_dedup_receive_t callback is run, just as if it is a new packet. The
49 * dedup BIO does not know if the received data is a new packet until the #fr_bio_dedup_receive_t callback
50 * says so. On duplicate client request, the #fr_bio_dedup_receive_t callback can call fr_bio_dedup_respond()
51 * to send a duplicate reply. That call bypasses the normal dedup stack, and writes directly to the next bio.
52 *
53 * The calling application can also call fr_bio_dedup_respond() as soon as it has a reply. i.e. skip the BIO
54 * write() call. That works, and is safe.
55 *
56 * The dedup BIO tracks a number of lists / trees internally. Packets which are received but which have no
57 * reply are in an "active" list. Packets which have a reply are in an "expired" RB tree, where a timer is
58 * set to expire packets. If a write() call results in a partial write, that packet is put into a "partially
59 * written" state. If multiple calls to write() are done when writing is blocked, the replies are put into a
60 * "pending" state. A pending reply is added to the expiry tree, so that it's cleaned up if it expires before
61 * the socket becomes writable again.
62 *
63 * The calling application can always call fr_bio_dedup_cancel() to cancel or expire a packet. This call is
64 * safe, and can be made at any time, no matter what state the packet is in.
65 *
66 * In some cases, the application doesn't reply to the client. These are:
67 *
68 * 1. The application decided that it doesn't need to reply. It calls fr_bio_dedup_no_reply(), and the
69 * entry stays in the dedup BIO until it expires. Retransmitted requests see the cached entry, and don't
70 * send a reply.
71 *
72 * 2. A newer request conflicts with an older request. The application calls fr_bio_dedup_conflict() for the
73 * old request, but is likely to still be running the old request. When the old request is done, the
74 * application calls fr_bio_dedup_respond() which notices that the entry is done, and releases it.
75 *
76 * @copyright 2026 Network RADIUS SAS (legal@networkradius.com)
77 */
78
79#include <freeradius-devel/bio/bio_priv.h>
80#include <freeradius-devel/bio/null.h>
81#include <freeradius-devel/bio/buf.h>
82
83#define _BIO_DEDUP_PRIVATE
84#include <freeradius-devel/bio/dedup.h>
85
86typedef struct fr_bio_dedup_list_s fr_bio_dedup_list_t;
88
89/*
90 * There is substantial similarity between this code and the
91 * "retry" bio. Any fixes which are done here should be checked
92 * there, and vice versa.
93 */
94
95/*
96 * Define type-safe wrappers for head and entry definitions.
97 */
98FR_DLIST_TYPES(fr_bio_dedup_list)
99FR_DLIST_TYPES(fr_bio_dedup_pending_list)
100
101typedef enum : uint8_t {
103 FR_BIO_DEDUP_STATE_ACTIVE, //!< Received, but not replied.
104 FR_BIO_DEDUP_STATE_PENDING, //!< Have a reply, but we're can't (yet) write it out. The entry
105 ///< is in the expiry tree. and the pending list.
106 FR_BIO_DEDUP_STATE_REPLIED, //!< Replied, and waiting for it to expire.
107 FR_BIO_DEDUP_STATE_PARTIAL, //!< Partially written
108 FR_BIO_DEDUP_STATE_CANCELLED, //!< Partially written, and then cancelled.
109 FR_BIO_DEDUP_STATE_NO_REPLY, //!< Active, or waiting to expire. Don't reply.
110 FR_BIO_DEDUP_STATE_CLEANUP, //!< Active, but conflicting request. Don't reply, and clean
111 ///< up immediately when responding.
113
115 void *uctx;
117 uint8_t *packet; //!< cached packet data for finding duplicates
118 size_t packet_size; //!< size of the cached packet data
119 void *reply_ctx; //!< reply ctx
120 uint8_t *reply; //!< reply cached by the application
121 size_t reply_size; //!< size of the cached reply
122
123 fr_rb_node_t dedup; //!< node in #fr_bio_dedup_config_t.dedup
124
125 union {
126 struct {
127 fr_rb_node_t node; //!< for the expiry timers
128 };
129 FR_DLIST_ENTRY(fr_bio_dedup_list) entry; //!< for the free list
130 };
131
132 /*
133 * A pending entry is in the expiry tree through `node`, and in the pending list. Because `entry`
134 * shares memory with `node`, the pending list needs a separate link.
135 */
136 FR_DLIST_ENTRY(fr_bio_dedup_pending_list) pending_entry; //!< for the pending list
137
138 fr_bio_dedup_t *my; //!< so we can get to it from the event timer callback
139
140 fr_time_t expires; //!< when this entry expires
141 fr_bio_dedup_state_t state; //!< which tree or list this item is in
142};
143
144FR_DLIST_FUNCS(fr_bio_dedup_list, fr_bio_dedup_entry_t, entry)
145FR_DLIST_FUNCS(fr_bio_dedup_pending_list, fr_bio_dedup_entry_t, pending_entry)
146
149
151
152 fr_rb_tree_t *dedup; //!< the application's dedup tree, from #fr_bio_dedup_config_t
153
154 fr_rb_tree_t rb; //!< expire list
155
157
159
160 /*
161 * The "first" entry is cached here so that we can detect when it changes. The insert / delete
162 * code can just do its work without worrying about timers. And then when the tree manipulation
163 * is done, call the fr_bio_dedup_timer_reset() function to reset (or not) the timer.
164 *
165 * The timer is set for is when the first packet expires.
166 */
168
169 /*
170 * Cache a partial write when IO is blocked.
171 *
172 * When the IO is blocked, we can still expire old entries, unlike the "retry" BIOs. This is
173 * because we're not resending packets, we're just cleaning up *sent* packets when they expire.
174 */
176
177 fr_bio_dedup_receive_t receive; //!< called when we receive a potentially new packet
178 fr_bio_dedup_release_t release; //!< called to release a packet
179
180 fr_bio_dedup_get_item_t get_item; //!< turn a packet_ctx into a #fr_bio_dedup_entry_t
181
183
184 FR_DLIST_HEAD(fr_bio_dedup_list) active; //!< received but not yet replied
185 FR_DLIST_HEAD(fr_bio_dedup_pending_list) pending; //!< trying to write when the socket is blocked.
186 FR_DLIST_HEAD(fr_bio_dedup_list) free; //!< free list, at most #fr_bio_dedup_config_t.max_free entries
187
188 size_t num_used; //!< number of entries in use, i.e. not in the free list
189 bool read_paused; //!< reads are paused because #fr_bio_dedup_config_t.max_active entries are in use
190};
191
192static void fr_bio_dedup_timer(UNUSED fr_timer_list_t *el, fr_time_t now, void *uctx);
193static ssize_t fr_bio_dedup_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size);
198
199/** Return true if an entry in state #FR_BIO_DEDUP_STATE_NO_REPLY is in the expiry tree
200 *
201 * We need to distinguish "no reply, and still being processed" (in the active list) from "no reply, and
202 * waiting to expire" (in the expiry tree). We do that by checking if the expiry time is zero.
203 */
205{
206 bool expiring;
207
209
210 expiring = !fr_time_eq(item->expires, fr_time_wrap(0)); /* compares uint64_t */
211 fr_assert(fr_rb_node_inline_in_tree(&item->node) == expiring); /* checks many pointers */
212
213 return expiring;
214}
215
217{
218 if (my->first != item) return;
219
220 my->first = NULL;
221 (void) fr_bio_dedup_timer_reset(my);
222}
223
224/** Add an entry to the expiry tree once the reply has been written, or once a "no reply" entry is done
225 *
226 * The expiry time is set from now, i.e. from when the reply was written (or for a "no reply" entry, from
227 * when the application called fr_bio_dedup_respond()), and not from when the reply was queued.
228 *
229 * Expiration for an entry is the maximum of now + expiry, or now + lifetime. i.e. The continued reception
230 * of duplicate requests indicates that we should extend the expiry time.
231 *
232 * Also updates the timers, because otherwise all callers have to do that.
233 */
235{
236 fr_time_t expires;
237
238 if (!item) return;
239
240 switch (item->state) {
243 fr_assert(0); /* fr_bio_dedup_respond() called twice */
244 return;
245 }
246
247 (void) fr_bio_dedup_list_remove(&my->active, item);
248 goto extend;
249
251 (void) fr_bio_dedup_list_remove(&my->active, item);
253 goto extend;
254
255 /*
256 * The partial reply has now been fully written. fr_bio_dedup_write_partial() has
257 * already cleared my->partial. The entry is not in any list or tree.
258 */
261 goto keep_later;
262
263 /*
264 * The partial reply has now been fully written. fr_bio_dedup_flush_pending() has
265 * already removed the entry from the pending list, but the entry is still in the expiry
266 * tree. We remove and re-insert the entry, because we extend its expiry time.
267 */
271
272 /*
273 * We wrote a duplicate reply. If the entry is the first one in the tree, then the timer
274 * has to be moved, even if the entry is still the first one after the change.
275 */
277 (void) fr_rb_remove_by_inline_node(&my->rb, &item->node);
278 if (my->first == item) my->first = NULL;
279
280 keep_later:
282 if (fr_time_gt(expires, item->expires)) item->expires = expires;
283 goto insert;
284
285 extend:
286 /*
287 * Now that we have a reply, set the default expiry time. The caller can always call
288 * fr_bio_dedup_entry_extend() to change the expiry time.
289 */
291
292 insert:
293 /*
294 * This should never fail.
295 */
296 (void) fr_rb_insert(&my->rb, item);
297 (void) fr_bio_dedup_timer_reset(my);
298 break;
299
300 default:
301 fr_assert(0);
302 return;
303 }
304}
305
306/** Add an entry to the pending list, because writes are blocked.
307 *
308 * A pending entry is also in the expiry tree. If the next bio stays blocked until the entry expires, the
309 * expiry timer releases the entry, and the reply is never written.
310 *
311 * - An active entry moves from the active list to the expiry tree, with an expiry time of now plus the
312 * configured lifetime.
313 * - A replied entry is already in the expiry tree, and keeps the expiry time which the entry has.
314 */
316{
317 switch (item->state) {
319 (void) fr_bio_dedup_list_remove(&my->active, item);
320
322 (void) fr_rb_insert(&my->rb, item);
323 (void) fr_bio_dedup_timer_reset(my);
324 break;
325
327 break;
328
329 default:
330 fr_assert(0);
331 return;
332 }
333
334 fr_bio_dedup_pending_list_insert_tail(&my->pending, item);
336}
337
338/** Resend a reply when we receive a duplicate request.
339 *
340 * This function should be called by the respond() callback to re-send a duplicate reply.
341 *
342 * It can also be called by the application when it first has a response to the request.
343 *
344 * @param bio the binary IO handler
345 * @param item the dedup context from #fr_bio_dedup_sent_t
346 * @return
347 * - <0 on error
348 * - 0 for "wrote no data"
349 * - >0 for "wrote data".
350 */
352{
353 ssize_t rcode;
354 fr_bio_dedup_t *my = talloc_get_type_abort(bio, fr_bio_dedup_t);
355 fr_bio_t *next;
356
357 switch (item->state) {
358 /*
359 * Send a first reply if we can.
360 */
362 fr_assert(item->reply && item->reply_size);
363 if (!item->reply || !item->reply_size) return fr_bio_error(GENERIC);
364
365 /*
366 * If we're not writing to the socket, just insert the packet into the pending list.
367 */
368 if (my->bio.write != fr_bio_dedup_write) {
370 return item->reply_size;
371 }
372
373 /*
374 * The socket is writable, go do that.
375 */
376 break;
377
378 /*
379 * Send a duplicate reply.
380 */
382 fr_assert(item->reply && item->reply_size);
383 if (!item->reply || !item->reply_size) return fr_bio_error(GENERIC);
384
385 /*
386 * We can write it, do so.
387 */
388 if (my->bio.write == fr_bio_dedup_write) break;
389
390 /*
391 * The socket is blocked. Save the packet in the pending queue.
392 */
394 return item->reply_size;
395
396 /*
397 * The application had previously said that we shouldn't respond. The application has
398 * now finished processing the request, which means that we skip writing the reply, and
399 * instead just add it to the expiry tree.
400 */
403 return 0;
404
405 /*
406 * We had a previous conflicting request, so we can't reply to this (old) request. Just
407 * clean the old one up now.
408 */
411 return 0;
412
413 /*
414 * We're already trying to write this entry, don't do anything else.
415 */
418 return fr_bio_error(IO_WOULD_BLOCK);
419
420 /*
421 * The entry was cancelled, e.g. by fr_bio_dedup_conflict(), while the reply was partially
422 * written. fr_bio_dedup_write_partial() writes the rest of the reply on the next write or
423 * write_resume, so we tell the caller that the whole reply has been written.
424 */
426 return item->reply_size;
427
429 fr_assert(0);
430 return fr_bio_error(GENERIC);
431 }
432
433 /*
434 * There must be a next bio.
435 */
436 next = fr_bio_next(&my->bio);
437 fr_assert(next != NULL);
438
439 /*
440 * Write out the packet, if everything is OK, return.
441 */
442 rcode = next->write(next, item->reply_ctx, item->reply, item->reply_size);
443 if ((size_t) rcode == item->reply_size) {
445 return rcode;
446 }
447
448 /*
449 * Can't write anything, be sad.
450 */
451 if ((rcode == 0) || (rcode == fr_bio_error(IO_WOULD_BLOCK))) {
453
455 return item->reply_size;
456 }
457
458 /*
459 * There's an error writing the packet. Release it, and move the item to the free list.
460 *
461 * Note that we don't bother resetting the timer. There's no point in changing the timer when
462 * the bio is likely dead.
463 */
464 if (rcode < 0) {
466 return rcode;
467 }
468
469 /*
470 * We are writing item->reply, and that's blocked. Save the partial packet for later.
471 */
472 return fr_bio_dedup_blocked(my, item, rcode);
473}
474
475/** Reset the timer after changing the rb tree.
476 *
477 */
479{
481
482 /*
483 * Nothing to do, don't set any timers.
484 */
485 first = fr_rb_first(&my->rb);
486 if (!first) {
488 my->ev = NULL;
489 my->first = NULL;
490 return 0;
491 }
492
493 /*
494 * We don't care about partially written packets. The timer remains set even when we have a
495 * partial outgoing packet, because we can expire entries which aren't partially written.
496 *
497 * However, the partially written packet MUST NOT be in the expiry tree.
498 *
499 * A pending entry is in the expiry tree, and can be the first entry.
500 */
501 fr_assert(first != my->partial);
502
503 /*
504 * The timer is already set correctly, we're done.
505 */
506 if (first == my->first) return 0;
507
508 /*
509 * Update the timer. This should never fail.
510 */
511 if (fr_timer_at(my, my->el->tl, &my->ev, first->expires, false, fr_bio_dedup_timer, my) < 0) return -1;
512
513 my->first = first;
514 return 0;
515}
516
517/** Release an entry back to the free list.
518 *
519 */
521{
522 /*
523 * Remove the entry from the application's dedup tree by node, and not by key. After
524 * fr_bio_dedup_conflict(), a newer entry with the same key may be in the dedup tree.
525 */
526 (void) fr_rb_remove_by_inline_node(my->dedup, &item->dedup);
527
528 my->release((fr_bio_t *) my, item, reason);
529
530 switch (item->state) {
531 /*
532 * Cancel an active item, just nuke it.
533 */
536 fr_bio_dedup_list_remove(&my->active, item);
537 break;
538
539 /*
540 * The entry is in the active list until the application calls fr_bio_dedup_respond().
541 * It then can get added to the expiry tree, or cleaned up.
542 */
545 fr_bio_dedup_list_remove(&my->active, item);
546 break;
547 }
549
550 /*
551 * We already replied, remove it from the expiry tree.
552 *
553 * When reason is #FR_BIO_DEDUP_EXPIRED, we do not reset the timer. Instead,
554 * fr_bio_dedup_timer() and fr_bio_dedup_flush_pending() will release expired entries in
555 * a loop, and then reset the timer after the loop has finished. Clearing my->first
556 * ensures that they will reset the timer after the loop.
557 */
559 expiry_tree:
560 (void) fr_rb_remove_by_inline_node(&my->rb, &item->node);
561
562 if (reason != FR_BIO_DEDUP_EXPIRED) {
564
565 } else if (my->first == item) {
566 my->first = NULL;
567 }
568 break;
569
570 /*
571 * It was pending another write, so we just discard the write. The entry is also in the
572 * expiry tree. When fr_bio_dedup_flush_pending() has already popped the entry from the
573 * pending list, fr_bio_dedup_pending_list_remove() does nothing.
574 */
576 (void) fr_bio_dedup_pending_list_remove(&my->pending, item);
577 goto expiry_tree;
578
579 /*
580 * Don't free it. Just set its state to cancelled.
581 */
583 fr_assert(my->partial == item);
585 return;
586
589 fr_assert(0);
590 return;
591 }
592
593 fr_assert(my->first != item);
595}
596
597/** Put an entry which is no longer in use into the free list, or free the entry if the free list is full.
598 *
599 * The caller must not use the entry after this function returns.
600 */
602{
603 fr_assert(my->num_used > 0);
604 my->num_used--;
605
606 if (fr_bio_dedup_list_num_elements(&my->free) >= my->config.max_free) {
608
609 } else {
610#ifndef NDEBUG
611 item->packet = NULL;
612#endif
613 item->uctx = NULL;
614 item->packet_ctx = NULL;
615
617 fr_bio_dedup_list_insert_head(&my->free, item);
618 }
619
620 /*
621 * If reads are paused, then resume reads after freeing an entry.
622 */
623 if (my->read_paused) {
624 fr_assert(my->num_used < my->config.max_active);
625
626 my->read_paused = false;
627
628 if (my->cb.read_resume) (void) my->cb.read_resume(&my->bio);
629 }
630}
631
632/** Flush any packets in the pending queue.
633 *
634 */
636{
637 ssize_t rcode, out_rcode;
639 fr_bio_t *next;
640 fr_time_t now;
641
642 /*
643 * We can only flush the pending list when any previous partial packet has been written.
644 */
645 fr_assert(!my->partial);
646
647 /*
648 * Nothing in the list, we're done.
649 */
650 if (fr_bio_dedup_pending_list_num_elements(&my->pending) == 0) return 0;
651
652 /*
653 * There must be a next bio.
654 */
655 next = fr_bio_next(&my->bio);
656 fr_assert(next != NULL);
657
658 now = fr_time();
659 out_rcode = 0;
660
661 /*
662 * Write out any pending packets.
663 */
664 while ((item = fr_bio_dedup_pending_list_pop_head(&my->pending)) != NULL) {
666
667 /*
668 * It's already expired, don't bother replying.
669 */
670 if (fr_time_lteq(item->expires, now)) {
672 continue;
673 }
674
675 /*
676 * Write the entry to the next bio.
677 */
678 rcode = next->write(next, item->reply_ctx, item->reply, item->reply_size);
679
680 /*
681 * The next bio is blocked. Put the entry back at the head of the pending list, so that we write
682 * the entry first when writes resume.
683 */
684 if ((rcode == 0) || (rcode == fr_bio_error(IO_WOULD_BLOCK))) {
685 fr_bio_dedup_pending_list_insert_head(&my->pending, item);
686 out_rcode = fr_bio_error(IO_WOULD_BLOCK);
687 break;
688 }
689
690 /*
691 * The next bio returned an error other than IO_WOULD_BLOCK. Release the entry, and return the
692 * error. The next bio has already decided whether the error is fatal, and has shut the chain
693 * down if the error is fatal.
694 */
695 if (rcode < 0) {
697 out_rcode = rcode;
698 break;
699 }
700
701 /*
702 * We've written the entire packet, move it to the expiry list.
703 */
704 if ((size_t) rcode == item->reply_size) {
706 continue;
707 }
708
709 fr_bio_dedup_blocked(my, item, rcode);
710
711 out_rcode = fr_bio_error(IO_WOULD_BLOCK);
712 break;
713 }
714
715 /*
716 * We may need to update the timer if we've removed the first entry from the tree, or added a new
717 * first entry.
718 */
719 if (!my->first || (my->first != fr_rb_first(&my->rb))) {
720 my->first = NULL;
721 (void) fr_bio_dedup_timer_reset(my);
722 }
723
724 return out_rcode;
725}
726
727/** Save partially written data to our local buffer.
728 *
729 */
730static int fr_bio_dedup_buffer_save(fr_bio_dedup_t *my, uint8_t const *buffer, size_t size, ssize_t rcode)
731{
732 /*
733 * (re)-alloc the buffer for partial writes.
734 */
735 if (!my->buffer.start ||
736 (size > fr_bio_buf_size(&my->buffer))) {
737 if (fr_bio_buf_alloc(my, &my->buffer, size) < 0) return -1;
738 }
739
740 fr_assert(fr_bio_buf_used(&my->buffer) == 0);
741 fr_assert(my->buffer.read == my->buffer.start);
742
743 fr_bio_buf_write(&my->buffer, buffer + rcode, size - rcode);
744
745 return 0;
746}
747
748/** Write data from our local buffer to the next bio.
749 *
750 */
752{
753 size_t used;
754 ssize_t rcode;
755 fr_bio_t *next;
756
758
760 fr_assert(used > 0);
761
762 /*
763 * There must be a next bio.
764 */
765 next = fr_bio_next(&my->bio);
766 fr_assert(next != NULL);
767
768 rcode = next->write(next, NULL, my->buffer.read, used);
769 if (rcode <= 0) return rcode;
770
771 my->buffer.read += rcode;
772
773 /*
774 * Still data in the buffer. We can't send more packets until we finish writing this one.
775 */
776 if (fr_bio_buf_used(&my->buffer) > 0) return 0;
777
778 /*
779 * We're done. Reset the buffer and clean up our cached partial packet.
780 */
782
783 return rcode;
784}
785
786/** There's a partial packet written. Write all of that one first, before writing another packet.
787 *
788 * The packet can either be cancelled, or IO blocked. In either case, we must write this packet before
789 * we can write another one.
790 */
791static ssize_t fr_bio_dedup_write_partial(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size)
792{
793 ssize_t rcode;
794 fr_bio_dedup_t *my = talloc_get_type_abort(bio, fr_bio_dedup_t);
796
797 fr_assert(my->partial != NULL);
799
802
803 rcode = fr_bio_dedup_buffer_write(my);
804 if (rcode <= 0) return rcode;
805
806 my->partial = NULL;
807
808 /*
809 * Partial writes are removed from the expiry tree until they're fully written. When they're
810 * written, either add it back to the tree if it's still operational, or add it to the free list
811 * if it has been cancelled.
812 */
813 if (item->state == FR_BIO_DEDUP_STATE_PARTIAL) {
814 /*
815 * The whole reply has now been written. Move the entry to the expiry tree, with an
816 * expiry time set from now.
817 */
819
820 } else {
821 /*
822 * The item was cancelled, add it to the free list.
823 */
825 }
826
827 /*
828 * We have written all of the partial entry, so we restore fr_bio_dedup_write() as the write
829 * function. If the next bio writes only part of a pending reply, fr_bio_dedup_blocked() installs
830 * fr_bio_dedup_write_partial() again.
831 */
832 my->bio.write = fr_bio_dedup_write;
833
834 /*
835 * Flush any packets which were pending during the blocking period.
836 */
837 rcode = fr_bio_dedup_flush_pending(my);
838 if (rcode < 0) return rcode;
839
840 /*
841 * Unlike the retry BIO, we don't retry writes for items in the RB tree. Those packets have already
842 * been written.
843 */
844
845 /*
846 * Try to write the packet which we were given.
847 */
848 return fr_bio_dedup_write(bio, packet_ctx, buffer, size);
849}
850
851/** The write is blocked.
852 *
853 * We couldn't write out the entire packet, the bio is blocked. Don't write anything else until we become
854 * unblocked!
855 *
856 * Do NOT free the timer. We can still expire old entries. The partially written entry is not in the expiry
857 * tree, because expiring a partially written entry would nuke it's packet. And then write only a partial
858 * packet to the socket.
859 */
861{
862 fr_assert(!my->partial);
863 fr_assert(rcode > 0);
864 fr_assert((size_t) rcode < item->reply_size);
865
866 if (fr_bio_dedup_buffer_save(my, item->reply, item->reply_size, rcode) < 0) return fr_bio_error(OOM);
867
868 switch (item->state) {
870 (void) fr_bio_dedup_list_remove(&my->active, item);
871 break;
872
873 /*
874 * We cannot expire this entry, so remove it from the expiration tree. That step lets us
875 * expire other entries.
876 */
878 (void) fr_rb_remove_by_inline_node(&my->rb, &item->node);
880 break;
881
882 /*
883 * The next bio wrote only part of a pending reply. fr_bio_dedup_flush_pending() has already
884 * removed the entry from the pending list. Because a partially written entry must not be
885 * in the expiry tree (see fr_bio_dedup_timer_reset()), we remove the entry from the expiry
886 * tree.
887 */
889 (void) fr_rb_remove_by_inline_node(&my->rb, &item->node);
891 break;
892
893 /*
894 * None of these states should be possible.
895 */
901 fr_assert(0);
902 return fr_bio_error(GENERIC);
903 }
904
905 my->partial = item;
907
908 /*
909 * Reset the write routine, so that if the application tries any more writes, the partial entry
910 * gets written first.
911 */
912 my->bio.write = fr_bio_dedup_write_partial;
913 return rcode;
914}
915
916/** There's a partial block of data written. Write all of that data first, before writing another packet.
917 */
918static ssize_t fr_bio_dedup_write_data(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size)
919{
920 ssize_t rcode;
921 fr_bio_dedup_t *my = talloc_get_type_abort(bio, fr_bio_dedup_t);
922
923 fr_assert(!my->partial);
924
925 /*
926 * Flush out any partly written data.
927 */
928 rcode = fr_bio_dedup_buffer_write(my);
929 if (rcode <= 0) return rcode;
930
931 /*
932 * We have written all of the partial data, so we restore fr_bio_dedup_write() as the write
933 * function. If the next bio writes only part of a pending reply, fr_bio_dedup_blocked() installs
934 * fr_bio_dedup_write_partial().
935 */
936 my->bio.write = fr_bio_dedup_write;
937
938 /*
939 * Flush any packets which were pending during the blocking period.
940 */
941 rcode = fr_bio_dedup_flush_pending(my);
942 if (rcode < 0) return rcode;
943
944 /*
945 * Try to write the packet which we were given.
946 */
947 return fr_bio_dedup_write(bio, packet_ctx, buffer, size);
948}
949
950
951/** The write is blocked, but we don't have "item".
952 *
953 * We couldn't write out the entire packet, the bio is blocked. Don't write anything else until we become
954 * unblocked!
955 *
956 * Do NOT free the timer. We can still expire old entries.
957 */
959{
960 fr_assert(!my->partial);
961 fr_assert(rcode > 0);
962 fr_assert((size_t) rcode < size);
963
964 if (fr_bio_dedup_buffer_save(my, buffer, size, rcode) < 0) return fr_bio_error(OOM);
965
966 /*
967 * Reset the write routine, so that if the application tries any more writes, the data
968 * gets written first.
969 */
970 my->bio.write = fr_bio_dedup_write_data;
971 return rcode;
972}
973
974/*
975 * There is no fr_bio_dedup_rewrite(), packets are never re-written by this bio.
976 */
977
978/** Resume writes.
979 *
980 * Write the rest of any partly written reply or data, and then write the replies on the pending list.
981 *
982 * @return
983 * - <0 on error
984 * - 0 for "still blocked"
985 * - 1 for "can resume"
986 */
988{
989 ssize_t rcode;
990 fr_bio_dedup_t *my = talloc_get_type_abort(bio, fr_bio_dedup_t);
991
992 if (my->bio.write == fr_bio_dedup_write) {
993 rcode = fr_bio_dedup_flush_pending(my);
994
995 } else {
996 /*
997 * fr_bio_dedup_write_partial() or fr_bio_dedup_write_data() writes the rest of the partial
998 * data, and then writes the replies on the pending list. The NULL buffer tells
999 * fr_bio_dedup_write() to flush the next bio.
1000 */
1001 rcode = my->bio.write(bio, NULL, NULL, SIZE_MAX);
1002 }
1003
1004 if ((rcode < 0) && (rcode != fr_bio_error(IO_WOULD_BLOCK))) return (int)rcode; /* fr_bio_error() codes fit in an int */
1005
1006 /*
1007 * We are still blocked if there is partial data, or a pending reply. A flush of the next bio may
1008 * also return IO_WOULD_BLOCK, but the write_resume callback of the next bio resumes the next bio.
1009 */
1010 if (my->bio.write != fr_bio_dedup_write) return 0;
1011
1012 return (fr_bio_dedup_pending_list_num_elements(&my->pending) == 0);
1013}
1014
1015/** Release every entry which expires within 10ms of now
1016 *
1017 * Because a pending entry is in the expiry tree, the timer also releases pending entries. Pending entries
1018 * which expire mean that they were never written, so we can just clean them up.
1019 */
1020static void fr_bio_dedup_timer(UNUSED fr_timer_list_t *tl, fr_time_t now, void *uctx)
1021{
1022 fr_bio_dedup_t *my = talloc_get_type_abort(uctx, fr_bio_dedup_t);
1024 fr_time_t expires;
1025
1026 fr_assert(my->first != NULL);
1027 fr_assert(fr_rb_first(&my->rb) == my->first);
1028
1029 my->first = NULL;
1030
1031 /*
1032 * Expire all entries which are within 10ms of "now". That way we don't reset the event many
1033 * times in short succession.
1034 */
1035 expires = fr_time_add(now, fr_time_delta_from_msec(10));
1036
1037 while ((item = fr_rb_first(&my->rb)) != NULL) {
1038 if (fr_time_gt(item->expires, expires)) break;
1039
1042 }
1043
1044 (void) fr_bio_dedup_timer_reset(my);
1045}
1046
1047/** Write raw data to the bio.
1048 *
1049 * Due to the BIO API, this function can be passed a NULL buffer (for flushing the BIOs), and it can't be
1050 * passed a #fr_bio_dedup_entry_t. Instead, it has to be passed a "void *packet_ctx", and it calls the
1051 * #fr_bio_dedup_get_item_t callback to find the entry.
1052 *
1053 * When there is an entry, the reply in the entry must be the data which is being written, and this
1054 * function calls fr_bio_dedup_respond(). The one difference is that for an entry marked with
1055 * fr_bio_dedup_no_reply() or fr_bio_dedup_conflict(), this function returns the full size instead of 0.
1056 * When there is no entry, the data is written as un-tracked data.
1057 *
1058 * The application can call fr_bio_dedup_respond() directly instead of calling this function.
1059 */
1060static ssize_t fr_bio_dedup_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
1061{
1062 ssize_t rcode;
1064 fr_bio_dedup_t *my = talloc_get_type_abort(bio, fr_bio_dedup_t);
1065 fr_bio_t *next;
1066
1067 fr_assert(!my->partial);
1068
1069 /*
1070 * There must be a next bio.
1071 */
1072 next = fr_bio_next(&my->bio);
1073 fr_assert(next != NULL);
1074
1075 /*
1076 * The caller is trying to flush partial data. But we don't have any partial data, so just call
1077 * the next bio to flush it.
1078 */
1079 if (!buffer) {
1080 return next->write(next, packet_ctx, NULL, size);
1081 }
1082
1083 /*
1084 * We need the item pointer to mark this entry as blocked. If that doesn't exist, then we try
1085 * really hard to write out the un-tracked data.
1086 */
1087 item = NULL;
1088 if (my->get_item) item = my->get_item(bio, packet_ctx);
1089
1090 if (item) {
1091 switch (item->state) {
1092 /*
1093 * We're not replying. Just say "we responded". We return the full size of the
1094 * data to tell the application that everything is OK.
1095 */
1098 rcode = fr_bio_dedup_respond(bio, item);
1099 if (rcode < 0) return rcode;
1100 return size;
1101
1102 /*
1103 * Other cases are handled by fr_bio_dedup_respond(). The data being written
1104 * here must match the cached entry.
1105 */
1112 fr_assert(item->reply_ctx == packet_ctx);
1113 fr_assert(item->reply == buffer);
1114 fr_assert(item->reply_size == size);
1115 break;
1116 }
1117
1118 return fr_bio_dedup_respond(bio, item);
1119 }
1120
1121 /*
1122 * Write out the un-tracked data. If there's an error, OR we wrote nothing, return.
1123 *
1124 * Note that we don't mark the socket as blocked if the next bio didn't write anything. We want
1125 * the caller to know that the write didn't succeed, and the caller takes care of managing the
1126 * current packet. So there's no need for us to do that.
1127 */
1128 rcode = next->write(next, packet_ctx, buffer, size);
1129 if (rcode <= 0) return rcode;
1130
1131 if ((size_t) rcode == size) return rcode;
1132
1133 return fr_bio_dedup_blocked_data(my, buffer, size, rcode);
1134}
1135
1136static ssize_t fr_bio_dedup_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
1137{
1138 ssize_t rcode;
1140 fr_bio_dedup_t *my = talloc_get_type_abort(bio, fr_bio_dedup_t);
1141 fr_bio_t *next;
1142
1143 /*
1144 * Be safe if reads are paused. The application shouldn't call us.
1145 */
1146 if (my->read_paused) return fr_bio_error(IO_WOULD_BLOCK);
1147
1148 fr_assert(my->num_used < my->config.max_active);
1149
1150 /*
1151 * There must be a next bio.
1152 */
1153 next = fr_bio_next(&my->bio);
1154 fr_assert(next != NULL);
1155
1156 /*
1157 * Read the packet. If error or nothing, return immediately.
1158 */
1159 rcode = next->read(next, packet_ctx, buffer, size);
1160 if (rcode <= 0) return rcode;
1161
1162 /*
1163 * Get an entry from the free list, or allocate a new entry if the free list is empty.
1164 */
1165 item = fr_bio_dedup_list_pop_head(&my->free);
1166 if (!item) {
1167 item = talloc_zero(my, fr_bio_dedup_entry_t);
1168 if (!item) return fr_bio_error(OOM);
1169 }
1170 my->num_used++;
1171
1173 .my = my,
1174 .packet_ctx = packet_ctx,
1175 .packet = buffer,
1176 .packet_size = (size_t) rcode,
1178 };
1179
1180 /*
1181 * See if we want to receive this packet. If this isn't something we need to receive, then we
1182 * just discard it.
1183 *
1184 * The "receive" function updates `packet_ctx`, and searches the dedup tree for a previous
1185 * request. For a new request, the "receive" function inserts the entry into the dedup tree.
1186 * For a duplicate request, the "receive" function needs to call fr_bio_dedup_respond() to send a
1187 * duplicate reply, and then returns `false`, so that the dedup bio stops processing the packet.
1188 *
1189 * The application can also call fr_bio_dedup_entry_extend() in order to extend the lifetime of a
1190 * packet which has a cached response.
1191 *
1192 * If we're NOT going to process this packet, then the entry goes back into the free list, or is
1193 * freed if the free list is full.
1194 *
1195 * The caller should mark any older conflicting packets by calling fr_bio_dedup_conflict(). Note
1196 * that for sanity, we don't re-use the previous #fr_bio_dedup_entry_t.
1197 */
1198 if (!my->receive(bio, item, packet_ctx)) {
1201 return 0;
1202 }
1203
1204 fr_bio_dedup_list_insert_tail(&my->active, item);
1205
1206 /*
1207 * We've hit our limit of packets to process. Pause reads until an entry is freed.
1208 *
1209 * fr_bio_dedup_entry_free() then resumes reads.
1210 */
1211 if (my->num_used >= my->config.max_active) {
1212 my->read_paused = true;
1213
1214 if (my->cb.read_blocked) (void) my->cb.read_blocked(&my->bio);
1215 }
1216
1217 return rcode;
1218}
1219
1220static fr_cmp_ret_t _entry_cmp(void const *one, void const *two)
1221{
1222 fr_bio_dedup_entry_t const *a = one;
1223 fr_bio_dedup_entry_t const *b = two;
1224 int ret;
1225
1226 fr_assert(a->packet);
1227 fr_assert(b->packet);
1228
1229 ret = fr_time_cmp(a->expires, b->expires);
1230 if (ret != 0) return ret;
1231
1232 /*
1233 * Technically two timestamps can be equal, even at nanosecond resolution. We pick something
1234 * else to make the entries unique. All we care is that (a) the entries are ordered, and (b)
1235 * that they are unique.
1236 */
1237 return CMP(a, b);
1238}
1239
1240/** Cancel one item.
1241 *
1242 * @param bio the binary IO handler
1243 * @param item the dedup context from #fr_bio_dedup_respond_t
1244 */
1246{
1247 fr_bio_dedup_t *my = talloc_get_type_abort(bio, fr_bio_dedup_t);
1248
1250
1252}
1253
1254/** Extend the expiry time for an entry
1255 *
1256 * @param bio the binary IO handler
1257 * @param item the dedup context from #fr_bio_dedup_respond_t
1258 * @param expires the new expiry time
1259 * @return
1260 * - <0 error
1261 * - 0 success
1262 */
1264{
1265 fr_bio_dedup_t *my = talloc_get_type_abort(bio, fr_bio_dedup_t);
1266
1267 switch (item->state) {
1270 return 0;
1271
1272 /*
1273 * Change the expiry time only if the entry is in the expiry tree.
1274 */
1276 if (!fr_bio_dedup_no_reply_expiring(item)) return 0;
1277 break;
1278
1281 break;
1282
1283 /*
1284 * A partially written reply is not in the expiry tree, so we can change the expiry time
1285 * without moving the entry. When the write finishes, it's added to the expiry tree.
1286 */
1288 item->expires = expires;
1289 return 0;
1290
1293 fr_assert(0);
1294 return fr_bio_error(GENERIC);
1295 }
1296
1297 /*
1298 * Change places in the tree.
1299 */
1300 item->expires = expires;
1301 (void) fr_rb_remove_by_inline_node(&my->rb, &item->node);
1302 (void) fr_rb_insert(&my->rb, item);
1303
1304 /*
1305 * If we're not changing the first item, we don't need to change the timers.
1306 *
1307 * Otherwise we clear the "first" flag, so that the reset timer function will change the timer
1308 * value.
1309 */
1310 if (my->first != item) return 0;
1311
1312 my->first = NULL;
1313
1314 return fr_bio_dedup_timer_reset(my);
1315}
1316
1317/** Mark an entry as "we intend to never reply"
1318 *
1319 * The application calls this function while it is processing the request, and then later calls
1320 * fr_bio_dedup_respond() once, when it has finished processing the request. fr_bio_dedup_respond() does
1321 * not write a reply, and moves the entry to the expiry tree. The entry is cleaned up when it expires,
1322 * with reason #FR_BIO_DEDUP_NO_REPLY. Calling fr_bio_dedup_respond() a second time is an error.
1323 *
1324 * The entry must still be active, i.e. received, but not replied. Or, it must be in the
1325 * #FR_BIO_DEDUP_STATE_CLEANUP, as we can mark a request as conflicting before the application has finished
1326 * processing it.
1327 *
1328 * Any other state is an application error.
1329 *
1330 * @param bio the binary IO handler
1331 * @param item the dedup context from #fr_bio_dedup_receive_t
1332 * @return
1333 * - <0 error, the entry is unchanged
1334 * - 0 success
1335 */
1337{
1338 switch (item->state) {
1340 fr_assert(fr_time_eq(item->expires, fr_time_wrap(0)));
1342 return 0;
1343
1344 /*
1345 * A newer request conflicted with this request while the application was processing this
1346 * request. We don't release the entry here, as the application then calls
1347 * fr_bio_dedup_respond(), which releases the entry.
1348 *
1349 * We could require that the application call _either_ fr_bio_dedup_no_reply(), or
1350 * fr_bio_dedup_respond(). But doing that means that every call to
1351 * fr_bio_dedup_respond() has to check for "we don't reply". Doing it this way means
1352 * that the application can call fr_bio_dedup_no_reply() at any point. Which means that
1353 * the normal "request / reply" state machine doesn't have to change.
1354 */
1356 return 0;
1357
1364 break;
1365 }
1366
1367 fr_assert(0);
1368 return fr_bio_error(GENERIC);
1369}
1370
1371/** Mark an entry as conflicting with a newer request
1372 *
1373 * The application calls this function from the #fr_bio_dedup_receive_t callback, when a newer request is
1374 * found to conflict with an older request which the application is still processing. The old entry stays in
1375 * the active list, but is marked as #FR_BIO_DEDUP_STATE_CLEANUP. The entry is then released when the
1376 * application finishes processing the older request, and calls fr_bio_dedup_respond().
1377 *
1378 * If the application has already finished processing the older request, the entry is released now, with
1379 * reason #FR_BIO_DEDUP_CANCELLED.
1380 *
1381 * - Replied, or "no reply" and waiting to expire. The entry is released.
1382 * - Pending reply. The reply is removed from the write queue (so we don't write it), and the entry is released.
1383 * - Reply partially written. The rest of the reply is still written, as the next bio has already
1384 * started writing the reply. The entry is marked #FR_BIO_DEDUP_STATE_CANCELLED, and goes back to the
1385 * free list once the write finishes.
1386 *
1387 * Any other state is an application error. Because the dedup bio has already removed a cancelled,
1388 * cleanup, or free entry from the dedup tree, the application cannot find the entry to pass to this
1389 * function.
1390 *
1391 * @param bio the binary IO handler
1392 * @param item the older entry, which conflicts with the request passed to #fr_bio_dedup_receive_t
1393 * @return
1394 * - <0 error, the entry is unchanged
1395 * - 0 success
1396 */
1398{
1399 fr_bio_dedup_t *my = talloc_get_type_abort(bio, fr_bio_dedup_t);
1400
1401 /*
1402 * Remove the older entry from the dedup tree, so that the application can insert the newer
1403 * entry, which has the same key. An entry marked #FR_BIO_DEDUP_STATE_CLEANUP stays in the
1404 * active list, but is no longer in the dedup tree.
1405 */
1406 (void) fr_rb_remove_by_inline_node(my->dedup, &item->dedup);
1407
1408 switch (item->state) {
1410 if (fr_bio_dedup_no_reply_expiring(item)) goto release;
1412
1415 return 0;
1416
1417 /*
1418 * fr_bio_dedup_release() removes the entry from the expiry tree, removes a pending entry
1419 * from the pending list, and puts the entry on the free list. For a partially written
1420 * entry, fr_bio_dedup_release() only sets the state to #FR_BIO_DEDUP_STATE_CANCELLED.
1421 * fr_bio_dedup_write_partial() then puts the entry on the free list when the write finishes.
1422 */
1426 release:
1428 return 0;
1429
1433 break;
1434 }
1435
1436 fr_assert(0);
1437 return fr_bio_error(GENERIC);
1438}
1439
1440
1441/** Remove the dedup cache
1442 *
1443 */
1445{
1448 fr_bio_dedup_t *my = talloc_get_type_abort(bio, fr_bio_dedup_t);
1449
1450 talloc_const_free(my->ev);
1451
1452 /*
1453 * Cancel all outgoing packets. Don't bother updating the tree or the free list, as all of the
1454 * entries will be deleted when the memory is freed.
1455 *
1456 * Because every pending entry is also in the expiry tree, the loop below removes all pending
1457 * entries.
1458 */
1459 while ((item = fr_rb_iter_init_inorder(&my->rb, &iter)) != NULL) {
1460 fr_rb_iter_delete_inorder(&my->rb, &iter);
1461 if (item->state == FR_BIO_DEDUP_STATE_PENDING) {
1462 (void) fr_bio_dedup_pending_list_remove(&my->pending, item);
1463 }
1464 (void) fr_rb_remove_by_inline_node(my->dedup, &item->dedup);
1466 }
1467
1468 fr_assert(fr_bio_dedup_pending_list_num_elements(&my->pending) == 0);
1469
1470 while ((item = fr_bio_dedup_list_pop_head(&my->active)) != NULL) {
1471 (void) fr_rb_remove_by_inline_node(my->dedup, &item->dedup);
1473 }
1474
1475 /*
1476 * A partially written entry isn't in any tree or list.. For an entry in
1477 * #FR_BIO_DEDUP_STATE_CANCELLED, fr_bio_dedup_release() has already called the release callback,
1478 * so we only call the release callback for an entry in #FR_BIO_DEDUP_STATE_PARTIAL.
1479 */
1480 if (my->partial) {
1481 if (my->partial->state == FR_BIO_DEDUP_STATE_PARTIAL) {
1484 }
1485 my->partial = NULL;
1486 }
1487
1488#ifndef NDEBUG
1489 my->ev = NULL;
1490 my->first = NULL;
1491#endif
1492
1493 return 0;
1494}
1495
1496/** Allocate a #fr_bio_dedup_t
1497 *
1498 */
1500 fr_bio_dedup_receive_t receive,
1501 fr_bio_dedup_release_t release,
1502 fr_bio_dedup_get_item_t get_item,
1503 fr_bio_dedup_config_t const *cfg,
1504 fr_bio_t *next)
1505{
1506 fr_bio_dedup_t *my;
1507
1508 fr_assert(cfg->el);
1509 fr_assert(cfg->dedup);
1510 if (!cfg->dedup) return NULL;
1511
1512 my = talloc_zero(ctx, fr_bio_dedup_t);
1513 if (!my) return NULL;
1514
1515 /*
1516 * The dedup bio starts with no entries. fr_bio_dedup_read() allocates each entry with
1517 * talloc_zero() when the free list is empty.
1518 */
1519 fr_bio_dedup_list_init(&my->free);
1520
1521 fr_bio_dedup_list_init(&my->active);
1522 fr_bio_dedup_pending_list_init(&my->pending);
1523
1524 (void) fr_rb_inline_init(&my->rb, fr_bio_dedup_entry_t, node, _entry_cmp, NULL);
1525
1526 my->receive = receive;
1527 my->release = release;
1528 my->get_item = get_item;
1529
1530 my->el = cfg->el;
1531 my->dedup = cfg->dedup;
1532 my->config = *cfg;
1533
1534 /*
1535 * Limit to reasonable values.
1536 */
1537 if (!my->config.max_active) my->config.max_active = 1024;
1538 if (my->config.max_active > (1 << 18)) my->config.max_active = (1 << 18);
1539
1540 if (!my->config.max_free) my->config.max_free = 1024;
1541 if (my->config.max_free > (1 << 18)) my->config.max_free = (1 << 18);
1542
1543 my->bio.write = fr_bio_dedup_write;
1544 my->bio.read = fr_bio_dedup_read;
1545
1546 fr_bio_chain(&my->bio, next);
1547
1548 my->priv_cb.shutdown = fr_bio_dedup_shutdown;
1549 my->priv_cb.write_resume = fr_bio_dedup_write_resume;
1550
1551 talloc_set_destructor((fr_bio_t *) my, fr_bio_destructor); /* always use a common destructor */
1552
1553 return (fr_bio_t *) my;
1554}
static int const char char buffer[256]
Definition acutest.h:641
fr_bio_write_t _CONST write
write to the underlying bio
Definition base.h:117
fr_bio_read_t _CONST read
read from the underlying bio
Definition base.h:116
static fr_bio_t * fr_bio_next(fr_bio_t *bio)
Definition base.h:131
#define fr_bio_error(_x)
Definition base.h:200
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.
Definition bio_priv.h:86
int fr_bio_buf_alloc(TALLOC_CTX *ctx, fr_bio_buf_t *bio_buf, size_t size)
Definition buf.c:117
ssize_t fr_bio_buf_write(fr_bio_buf_t *bio_buf, const void *buffer, size_t size)
Definition buf.c:84
uint8_t * start
start of the buffer
Definition buf.h:30
static size_t fr_bio_buf_used(fr_bio_buf_t const *bio_buf)
Definition buf.h:73
static void fr_bio_buf_reset(fr_bio_buf_t *bio_buf)
Definition buf.h:61
uint8_t * read
where in the buffer reads are taken from
Definition buf.h:33
static size_t fr_bio_buf_size(fr_bio_buf_t const *bio_buf)
Definition buf.h:151
#define FALL_THROUGH
clang 10 doesn't recognised the FALL-THROUGH comment anymore
Definition build.h:422
#define CMP(_a, _b)
Same as CMP_PREFER_SMALLER use when you don't really care about ordering, you just want an ordering.
Definition build.h:130
#define UNUSED
Definition build.h:415
fr_cmp_ret_t
Result of an ordering comparison.
Definition build.h:113
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:1397
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:351
static int fr_bio_dedup_shutdown(fr_bio_t *bio)
Remove the dedup cache.
Definition dedup.c:1444
fr_bio_dedup_release_t release
called to release a packet
Definition dedup.c:178
static void fr_bio_dedup_timer_reset_item(fr_bio_dedup_t *my, fr_bio_dedup_entry_t *item)
Definition dedup.c:216
static int fr_bio_dedup_write_resume(fr_bio_t *bio)
Resume writes.
Definition dedup.c:987
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.
Definition dedup.c:730
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:1263
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".
Definition dedup.c:1336
fr_rb_tree_t * dedup
the application's dedup tree, from fr_bio_dedup_config_t
Definition dedup.c:152
fr_bio_dedup_config_t config
Definition dedup.c:156
fr_bio_dedup_get_item_t get_item
turn a packet_ctx into a fr_bio_dedup_entry_t
Definition dedup.c:180
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.
Definition dedup.c:791
void fr_bio_dedup_entry_cancel(fr_bio_t *bio, fr_bio_dedup_entry_t *item)
Cancel one item.
Definition dedup.c:1245
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.
Definition dedup.c:204
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.
Definition dedup.c:918
fr_bio_dedup_entry_t * partial
Definition dedup.c:175
fr_event_list_t * el
Definition dedup.c:150
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.
Definition dedup.c:315
static ssize_t fr_bio_dedup_flush_pending(fr_bio_dedup_t *my)
Flush any packets in the pending queue.
Definition dedup.c:635
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.
Definition dedup.c:860
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.
Definition dedup.c:1020
fr_rb_tree_t rb
expire list
Definition dedup.c:154
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.
Definition dedup.c:234
fr_bio_dedup_receive_t receive
called when we receive a potentially new packet
Definition dedup.c:177
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".
Definition dedup.c:958
fr_bio_dedup_state_t
Definition dedup.c:101
@ FR_BIO_DEDUP_STATE_NO_REPLY
Active, or waiting to expire. Don't reply.
Definition dedup.c:109
@ FR_BIO_DEDUP_STATE_CLEANUP
Active, but conflicting request.
Definition dedup.c:110
@ FR_BIO_DEDUP_STATE_PARTIAL
Partially written.
Definition dedup.c:107
@ FR_BIO_DEDUP_STATE_FREE
Definition dedup.c:102
@ FR_BIO_DEDUP_STATE_ACTIVE
Received, but not replied.
Definition dedup.c:103
@ FR_BIO_DEDUP_STATE_CANCELLED
Partially written, and then cancelled.
Definition dedup.c:108
@ FR_BIO_DEDUP_STATE_PENDING
Have a reply, but we're can't (yet) write it out.
Definition dedup.c:104
@ FR_BIO_DEDUP_STATE_REPLIED
Replied, and waiting for it to expire.
Definition dedup.c:106
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 dedup.c:1499
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.
Definition dedup.c:1060
fr_bio_dedup_entry_t * first
Definition dedup.c:167
static ssize_t fr_bio_dedup_buffer_write(fr_bio_dedup_t *my)
Write data from our local buffer to the next bio.
Definition dedup.c:751
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...
Definition dedup.c:601
fr_bio_buf_t buffer
Definition dedup.c:182
struct fr_bio_dedup_list_s fr_bio_dedup_list_t
Definition dedup.c:86
static int fr_bio_dedup_timer_reset(fr_bio_dedup_t *my)
Reset the timer after changing the rb tree.
Definition dedup.c:478
static ssize_t fr_bio_dedup_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Definition dedup.c:1136
fr_timer_t * ev
Definition dedup.c:158
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.
Definition dedup.c:520
static fr_cmp_ret_t _entry_cmp(void const *one, void const *two)
Definition dedup.c:1220
void * uctx
user-writable context
Definition dedup.c:115
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)
Definition dedup.h:116
fr_event_list_t * el
event list
Definition dedup.h:47
fr_rb_tree_t * dedup
application-supplied dedup tree, using the inline fr_bio_dedup_entry_t.dedup node.
Definition dedup.h:51
uint32_t max_active
maximum number of entries in use.
Definition dedup.h:55
fr_time_delta_t lifetime
default lifetime of dedup entry
Definition dedup.h:49
size_t packet_size
size of the cached packet data
Definition dedup.c:118
struct fr_bio_dedup_entry_s fr_bio_dedup_entry_t
Definition dedup.h:63
void * packet_ctx
packet_ctx for dedup purposes
Definition dedup.c:116
uint8_t * reply
reply cached by the application
Definition dedup.c:120
size_t reply_size
size of the cached reply
Definition dedup.c:121
void * reply_ctx
reply ctx
Definition dedup.c:119
uint8_t * packet
cached packet data for finding duplicates
Definition dedup.c:117
fr_rb_node_t dedup
node in fr_bio_dedup_config_t.dedup
Definition dedup.c:123
uint32_t max_free
maximum number of entries in the free list.
Definition dedup.h:58
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.
Definition dedup.h:100
fr_bio_dedup_entry_t *(* fr_bio_dedup_get_item_t)(fr_bio_t *bio, void *packet_ctx)
Definition dedup.h:118
fr_bio_dedup_release_reason_t
Definition dedup.h:79
@ FR_BIO_DEDUP_WRITE_ERROR
IO error.
Definition dedup.h:82
@ FR_BIO_DEDUP_CANCELLED
it was cancelled
Definition dedup.h:81
@ FR_BIO_DEDUP_EXPIRED
it's expired, and we have to delete it
Definition dedup.h:80
@ FR_BIO_DEDUP_NO_REPLY
We're not replying to this item.
Definition dedup.h:84
Definition dedup.c:114
#define FR_DLIST_TYPES(_name)
Define type specific wrapper structs for dlists.
Definition dlist.h:1150
#define FR_DLIST_ENTRY(_name)
Expands to the type name used for the entry wrapper structure.
Definition dlist.h:1136
#define FR_DLIST_FUNCS(_name, _element_type, _element_entry)
Define type specific wrapper functions for dlists.
Definition dlist.h:1173
#define FR_DLIST_HEAD(_name)
Expands to the type name used for the head wrapper structure.
Definition dlist.h:1143
free(array)
talloc_free(hp)
int fr_bio_destructor(fr_bio_t *bio)
Free this bio.
Definition base.c:35
#define fr_time()
Definition event.c:61
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
long int ssize_t
unsigned char uint8_t
unsigned long int size_t
static size_t used
#define fr_assert(_expr)
Definition rad_assert.h:37
void * fr_rb_iter_init_inorder(fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Initialise an in-order iterator.
Definition rb.c:850
void fr_rb_iter_delete_inorder(fr_rb_tree_t *tree, fr_rb_iter_inorder_t *iter)
Remove the current node from the tree.
Definition rb.c:925
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.
Definition rb.c:748
void * fr_rb_first(fr_rb_tree_t *tree)
Definition rb.c:812
int fr_rb_insert(fr_rb_tree_t *tree, void const *data)
Insert data into a tree.
Definition rb.c:637
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.
Definition rb.h:312
#define fr_rb_inline_init(_tree, _type, _field, _data_cmp, _data_free)
Initialises a red black tree.
Definition rb.h:178
Iterator structure for in-order traversal of an rbtree.
Definition rb.h:319
The main red black tree structure.
Definition rb.h:71
static int talloc_const_free(void const *ptr)
Free const'd memory.
Definition talloc.h:289
static fr_time_delta_t fr_time_delta_from_msec(int64_t msec)
Definition time.h:584
static fr_time_t fr_time_add_time_delta(fr_time_t a, fr_time_delta_t b)
Definition time.h:173
#define fr_time_wrap(_time)
Definition time.h:145
#define fr_time_lteq(_a, _b)
Definition time.h:240
#define fr_time_eq(_a, _b)
Definition time.h:241
static fr_cmp_ret_t fr_time_cmp(fr_time_t a, fr_time_t b)
Compare two fr_time_t values.
Definition time.h:934
#define fr_time_add(_a, _b)
Add a time/time delta together.
Definition time.h:196
#define fr_time_gt(_a, _b)
Definition time.h:237
"server local" time.
Definition time.h:69
An event timer list.
Definition timer.c:65
A timer event.
Definition timer.c:100
#define fr_timer_at(...)
Definition timer.h:81
static fr_event_list_t * el