The FreeRADIUS server $Id: 15bac2a4c627c01d1aa2047687b3418955ac7f00 $
Loading...
Searching...
No Matches
retry.c
Go to the documentation of this file.
1/*
2 * This program is 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: f77ffb6ecb11ac6828df1e30a3324b11a2f603b5 $
19 * @file lib/bio/retry.c
20 * @brief Binary IO abstractions for retrying packets.
21 *
22 * The retry BIO provides a mechanism for the application to send one packet, and then delegate
23 * retransmissions to the retry bio.
24 *
25 * This BIO will monitor writes, and run callbacks when a packet is sent, received, and released. The
26 * application should cache the request and response until the release callback has been run. The BIO will
27 * call the application on retries, or when the retransmissions have stopped.
28 *
29 * The retry BIO also deals with partially written packets. The BIO takes responsibility for not writing
30 * partial packets, which means that requests can be rleeased even if the data has been partially written.
31 * The application can also cancel an ongoing retryt entrty at any time.
32 *
33 * If something blocks IO, the application should call the blocked / resume functions for this BIO to inform
34 * it of IO changes. Otherwise, the only time this BIO blocks is when it runs out of retransmission slots.
35 *
36 * There are provisions for application-layer watchdogs, where the application can reserve a retry entry. It
37 * can then call the fr_bio_retry_rewrite() function instead of fr_bio_write() to write the watchdog packet.
38 * Any retransmission timers for the application-layer watchdog must be handled by the application. The BIO
39 * will not retry reserved watchdog requests.
40 *
41 * In general, the next BIO after this one should be the memory bio, so that this bio receives only complete
42 * packets.
43 *
44 * @copyright 2024 Network RADIUS SAS (legal@networkradius.com)
45 */
46
47#include <freeradius-devel/bio/bio_priv.h>
48#include <freeradius-devel/bio/null.h>
49#include <freeradius-devel/bio/buf.h>
50#include <freeradius-devel/util/rb.h>
51#include <freeradius-devel/util/dlist.h>
52
53#define _BIO_RETRY_PRIVATE
54#include <freeradius-devel/bio/retry.h>
55
56typedef struct fr_bio_retry_list_s fr_bio_retry_list_t;
58
59/*
60 * Define type-safe wrappers for head and entry definitions.
61 */
62FR_DLIST_TYPES(fr_bio_retry_list)
63
65 void *uctx;
67 fr_bio_retry_rewrite_t rewrite; //!< per-packet rewrite callback
68 void *rewrite_ctx; //!< context specifically for rewriting this packet
69
70 fr_retry_t retry; //!< retry timers and counters
71
72 union {
73 fr_rb_node_t next_retry_node; //!< for retries
74 FR_DLIST_ENTRY(fr_bio_retry_list) entry; //!< for the free list
75 };
76 fr_rb_node_t expiry_node; //!< for expiries
77
78 fr_bio_retry_t *my; //!< so we can get to it from the event timer callback
79
80 uint8_t const *buffer; //!< cached copy of the packet to send
81 size_t size; //!< size of the cached packet
82
83 bool cancelled; //!< was this item cancelled?
84 bool reserved; //!< for application-layer watchdog
85};
86
87FR_DLIST_FUNCS(fr_bio_retry_list, fr_bio_retry_entry_t, entry)
88
91
92 fr_timer_list_t *next_tl; //!< when packets are retried next
93 fr_timer_list_t *expiry_tl; //!< when packets expire, so that we expire packets when the socket is blocked.
94
96
98
100 bool all_used; //!< blocked due to no free entries
101
102 /*
103 * Cache a partial write when IO is blocked. Partial
104 * packets are left in the timer tree so that they can be expired.
105 */
106 fr_bio_retry_entry_t *partial; //!< for partial writes
107
108 fr_bio_retry_sent_t sent; //!< callback for when we successfully sent a packet
109 fr_bio_retry_rewrite_t rewrite; //!< optional callback which can change a packet on retry
110 fr_bio_retry_response_t response; //!< callback to see if we got a valid response
111 fr_bio_retry_release_t release; //!< callback to release a request / response pair
112
113 fr_bio_buf_t buffer; //!< to store partial packets
114
115 FR_DLIST_HEAD(fr_bio_retry_list) free; //!< free lists are better than memory fragmentation
116};
117
118static ssize_t fr_bio_retry_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size);
120
121/** Release an entry back to the free list.
122 *
123 */
125{
126 item->cancelled = true;
127
128 /*
129 * Remove the item from all timer lists before calling the application "release" function.
130 *
131 * reserved items (e.g. application-layer watchdogs like Status-Server) are run by the
132 * application, and aren't inserted into any tree.
133 */
134 if (!item->reserved) {
135 (void) fr_timer_uctx_remove(my->next_tl, item);
136 (void) fr_timer_uctx_remove(my->expiry_tl, item);
137 }
138
139 /*
140 * Tell the caller that we've released it before doing anything else. That way we can safely
141 * modify anything we want.
142 */
143 my->release((fr_bio_t *) my, item, reason);
144
145 /*
146 * We've partially written this item. Don't bother changing it's position in any of the lists,
147 * as it's in progress.
148 */
149 if (my->partial == item) return;
150
151 /*
152 * This item is reserved. The application has cached a pointer to it, so it never gets returned
153 * to the free list.
154 */
155 if (item->reserved) return;
156
157 /*
158 * If we were blocked due to having no free entries, then we can resume writes, since we now have
159 * a free entry.
160 */
161 if (my->all_used) {
162 fr_assert(fr_bio_retry_list_num_elements(&my->free) == 0);
163
164 /*
165 * The application MUST call fr_bio_retry_write_resume(), which will check if IO is
166 * actually blocked.
167 *
168 * @todo - make this function return a failure, OR update the ctx with a failure? OR
169 * call a bio error function on failure? That way we can just call write_resume() from here.
170 */
171 my->all_used = false;
172
173 if (!my->info.write_blocked && my->cb.write_resume) (void) my->cb.write_resume(&my->bio);
174 }
175
176 item->packet_ctx = NULL;
177
178 fr_bio_retry_list_insert_head(&my->free, item);
179}
180
181/** Writes are blocked.
182 *
183 */
185{
186 fr_bio_retry_t *my = talloc_get_type_abort(bio, fr_bio_retry_t);
187
188 if (my->info.write_blocked) {
189 return 1;
190 }
191
192 /*
193 * Disarm the retry timer, and enable the expiry timer.
194 *
195 * i.e. we won't retry packets, but we will expire them when their timer runs out.
196 */
197 if (fr_timer_list_disarm(my->next_tl) < 0) return fr_bio_error(GENERIC);
198
199 if (fr_timer_list_arm(my->expiry_tl) < 0) return fr_bio_error(GENERIC);
200
201 my->info.write_blocked = true;
202
203 return 1;
204}
205
206
207/** Write one item.
208 *
209 * @return
210 * - <0 on error
211 * - 0 for "can't write any more"
212 * - 1 for "wrote a packet"
213 */
215{
216 ssize_t rcode;
217 fr_retry_state_t state;
218
219 fr_assert(!my->partial);
220 fr_assert(!item->reserved);
221
222 /*
223 * Are we there yet?
224 *
225 * Release it, indicating whether or not we successfully got a reply.
226 */
227 state = fr_retry_next(&item->retry, now);
228 if (state != FR_RETRY_CONTINUE) {
230 return 1;
231 }
232
233 /*
234 * Track when we last sent a NEW packet. Also track when we first sent a packet after becoming
235 * writeable again.
236 */
237 if ((item->retry.count == 1) && fr_time_lt(my->info.last_sent, now)) {
238 my->info.last_sent = now;
239
240 if (fr_time_lteq(my->info.first_sent, my->info.last_idle)) my->info.first_sent = now;
241 }
242
243 fr_assert(fr_time_gt(item->retry.next, now));
244
245 /*
246 * We rewrote the "next" timer. Remove the item from the timer tree, which doesn't call the cmp
247 * function and therefore doesn't care that the time has changed. Then re-insert it, which does
248 * call the cmp function.
249 */
250 (void) fr_timer_uctx_remove(my->next_tl, item);
251 (void) fr_timer_uctx_insert(my->next_tl, item);
252
253 /*
254 * Write out the packet. On failure release this item.
255 *
256 * If there's an error, we hope that the next "real" write will find the error, and do any
257 * necessary cleanups. Note that we can't call bio shutdown here, as the bio is controlled by the
258 * application, and not by us.
259 */
260 if (item->rewrite) {
261 rcode = item->rewrite(&my->bio, item, item->buffer, item->size);
262 } else {
263 rcode = my->rewrite(&my->bio, item, item->buffer, item->size);
264 }
265 if (rcode < 0) {
266 if (rcode == fr_bio_error(IO_WOULD_BLOCK)) return rcode;
267
269 return rcode;
270 }
271
272 /*
273 * We didn't write the whole packet, we're blocked.
274 */
275 if ((size_t) rcode < item->size) {
276 if (fr_bio_retry_save_write(my, item, rcode) < 0) return fr_bio_error(OOM);
277
278 return 0;
279 }
280
281 return 1;
282}
283
284/** Resume writes.
285 *
286 * On resume, we try to flush any pending packets which should have been sent.
287 */
289{
290 fr_bio_retry_t *my = talloc_get_type_abort(bio, fr_bio_retry_t);
291
292 if (!my->info.write_blocked) return 1;
293
294 my->info.write_blocked = false;
295
296 /*
297 * Disarm the expiry list, and rearm the next retry list.
298 *
299 * Rearming the next retry list will cause all pending events to be run. Which means calling the
300 * write routine for each item. If the write ends up blocking, it will disarm the next retry
301 * timer, re-arm the expiry timer, and then set the write_blocked flag.
302 */
303 (void) fr_timer_list_disarm(my->expiry_tl);
304 (void) fr_timer_list_arm(my->next_tl);
305
306 return !my->info.write_blocked; /* return 0 for "can't resume" and 1 for "can resume" */
307}
308
309
310/** There's a partial packet written. Write all of that one first, before writing another packet.
311 *
312 * The packet can either be cancelled, or IO blocked. In either case, we must write the full packet before
313 * going on to the next one, OR retrying another packet.
314 */
315static ssize_t fr_bio_retry_write_partial(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size)
316{
317 size_t used;
318 ssize_t rcode;
319 fr_bio_retry_t *my = talloc_get_type_abort(bio, fr_bio_retry_t);
320 fr_bio_t *next;
321 fr_bio_retry_entry_t *item = my->partial;
322
323 fr_assert(my->partial != NULL);
324 fr_assert(my->buffer.start);
325
326 used = fr_bio_buf_used(&my->buffer);
327 fr_assert(used > 0);
328
329 /*
330 * There must be a next bio.
331 */
332 next = fr_bio_next(&my->bio);
333 fr_assert(next != NULL);
334
335 rcode = next->write(next, NULL, my->buffer.read, used);
336 if (rcode <= 0) return rcode;
337
338 my->buffer.read += rcode;
339
340 /*
341 * Still data in the buffer. We can't send more packets until we finished writing this one.
342 */
343 if (fr_bio_buf_used(&my->buffer) > 0) return 0;
344
345 /*
346 * We're done. Reset the buffer and clean up our cached partial packet.
347 */
348 fr_bio_buf_reset(&my->buffer);
349 my->partial = NULL;
350
351 /*
352 * The item was cancelled, which means it's no longer in the timer tree.
353 *
354 * If it's not cancelled, then we leave it in the tree, and run its timers s normal.
355 */
356 if (item->cancelled) {
357 item->packet_ctx = NULL;
358
359 fr_bio_retry_list_insert_head(&my->free, item);
360 }
361
362 /*
363 * Update the write function to allow writes before calling the resume function. The resume
364 * function may flush a partial write.
365 */
366 my->bio.write = fr_bio_retry_write;
367
368 rcode = fr_bio_retry_write_resume(&my->bio);
369 if (rcode <= 0) return rcode;
370
371 /*
372 * Try to write the packet which we were given.
373 */
374 return fr_bio_retry_write(bio, packet_ctx, buffer, size);
375}
376
377/** Save a partial packet when the write becomes blocked.
378 */
380{
381 fr_assert(!my->partial);
382 fr_assert(rcode > 0);
383 fr_assert((size_t) rcode < item->size);
384
385 /*
386 * (re)-alloc the buffer for partial writes.
387 */
388 if (!my->buffer.start ||
389 (item->size > fr_bio_buf_size(&my->buffer))) {
390 if (fr_bio_buf_alloc(my, &my->buffer, item->size)) return fr_bio_error(OOM);
391 }
392
393 fr_assert(fr_bio_buf_used(&my->buffer) == 0);
394 fr_assert(my->buffer.read == my->buffer.start);
395
396 fr_bio_buf_write(&my->buffer, item->buffer + rcode, item->size - rcode);
397
398 my->partial = item;
399
400 /*
401 * If the "next" BIO blocked, then the call to fr_bio_write_blocked() will have already called
402 * this function.
403 */
404 if (fr_bio_retry_write_blocked(&my->bio) < 0) return fr_bio_error(GENERIC);
405
406 my->bio.write = fr_bio_retry_write_partial;
407
408 /*
409 * We leave the entry in the timer tree so that the expiry timer will get hit.
410 *
411 * And then return the size of the partial data we wrote.
412 */
413 return rcode;
414}
415
416
417/** Resend a packet.
418 *
419 * This function should be called by the rewrite() callback, after (possibly) re-encoding the packet.
420 *
421 * @param bio the binary IO handler
422 * @param item the retry context from #fr_bio_retry_sent_t
423 * @param buffer raw data for the packet. May be NULL, in which case the previous packet is retried
424 * @param size size of the raw data
425 * @return
426 * - <0 on error
427 * - 0 for "wrote no data"
428 * - >0 for "wrote data".
429 */
431{
432 ssize_t rcode;
433 fr_bio_retry_t *my = talloc_get_type_abort(bio, fr_bio_retry_t);
434 fr_bio_t *next;
435
436 /*
437 * The caller may (accidentally or intentionally) call this function when there's a partial
438 * packet. The intention for rewrite() is that it is only called from timers, and those only run
439 * when the socket isn't blocked. But the caller might not pay attention to those issues.
440 */
441 if (my->partial) return 0;
442
443 /*
444 * There must be a next bio.
445 */
446 next = fr_bio_next(&my->bio);
447 fr_assert(next != NULL);
448
449 /*
450 * The caller should pass NULL for "use the previous packet".
451 */
452 if (buffer) {
453 item->buffer = buffer;
454 item->size = size;
455 }
456
457 /*
458 * Write out the packet, if everything is OK, return.
459 *
460 * Note that we don't update any timers if the write succeeded. That is handled by the caller.
461 */
462 rcode = next->write(next, item->packet_ctx, item->buffer, item->size);
463 if ((size_t) rcode == item->size) return rcode;
464
465 /*
466 * Can't write anything, be sad.
467 */
468 if (rcode == 0) return 0;
469
470 /*
471 * There's an error writing the packet. Release it, and move the item to the free list.
472 *
473 * Note that we don't bother resetting the timer. There's no point in changing the timer when
474 * the bio is likely dead.
475 */
476 if (rcode < 0) {
477 if (rcode == fr_bio_error(IO_WOULD_BLOCK)) return rcode;
478
480 return rcode;
481 }
482
483 /*
484 * We had previously written the packet, so save the re-sent one, too.
485 */
486 return fr_bio_retry_save_write(my, item, rcode);
487}
488
489/** A previous timer write had a fatal error, so we forbid further writes.
490 *
491 */
492static ssize_t fr_bio_retry_write_fatal(fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void const *buffer, UNUSED size_t size)
493{
494 fr_bio_retry_t *my = talloc_get_type_abort(bio, fr_bio_retry_t);
495 ssize_t rcode = my->error;
496
497 my->error = 0;
498 my->bio.write = fr_bio_null_write;
499
500 return rcode;
501}
502
503/** Run an expiry timer event.
504 *
505 */
507{
508 fr_bio_retry_entry_t *item = talloc_get_type_abort(uctx, fr_bio_retry_entry_t);
509 fr_bio_retry_t *my = item->my;
510
511 /*
512 * We only expire entries if writing is blocked.
513 */
514 fr_assert(my->info.write_blocked);
515
516 /*
517 * An item is DONE if it received a reply, then waited for another reply, and then the socket
518 * became blocked.
519 */
521}
522
523/** Run a timer event. Usually to write out another packet.
524 *
525 */
527{
528 ssize_t rcode;
529 fr_bio_retry_entry_t *item = talloc_get_type_abort(uctx, fr_bio_retry_entry_t);
530 fr_bio_retry_t *my = item->my;
531
532 fr_assert(my->partial == NULL);
533 fr_assert(!my->info.write_blocked);
534
535 /*
536 * Retry one item.
537 */
538 rcode = fr_bio_retry_write_item(my, item, now);
539 if (rcode < 0) {
540 if (rcode == fr_bio_error(IO_WOULD_BLOCK)) return;
541
542 my->error = rcode;
543 my->bio.write = fr_bio_retry_write_fatal;
544 return;
545 }
546}
547
548/** Write a request, and see if we have a reply.
549 *
550 */
551static ssize_t fr_bio_retry_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
552{
553 ssize_t rcode;
555 fr_bio_retry_t *my = talloc_get_type_abort(bio, fr_bio_retry_t);
556 fr_bio_t *next;
557
558 fr_assert(!my->partial);
559
560 /*
561 * There must be a next bio.
562 */
563 next = fr_bio_next(&my->bio);
564 fr_assert(next != NULL);
565
566 /*
567 * The caller is trying to flush partial data. But we don't have any partial data, so just call
568 * the next bio to flush it.
569 */
570 if (!buffer) {
571 return next->write(next, packet_ctx, NULL, size);
572 }
573
574 /*
575 * Catch the corner case where the max number of saved packets is exceeded.
576 */
577 if (fr_bio_retry_list_num_elements(&my->free) == 0) {
578 /*
579 * Grab the first item which can be expired.
580 */
581 item = fr_timer_uctx_peek(my->expiry_tl);
582
583 /*
584 * If the item has no replies, we can't cancel it. Otherwise, try to cancel it, which
585 * will give us a free slot. If we can't cancel it, tell the application that we're
586 * blocked.
587 *
588 * Note that we do NOT call fr_bio_retry_write_blocked(), as that assumes the IO is
589 * blocked, and will stop all of the timers. Instead, the IO is fine, but we have no way
590 * to send more packets.
591 */
592 if (!item || !item->retry.replies || (fr_bio_retry_entry_cancel(bio, item) < 0)) {
593 /*
594 * Note that we're blocked BEFORE running the callback, so that calls to
595 * fr_bio_retry_write_blocked() doesn't delete timers and stop retrying packets.
596 */
597 my->info.write_blocked = true;
598 my->all_used = true;
599
600 /*
601 * Previous BIOs are blocked, but we still try to write retries.
602 */
603 rcode = fr_bio_write_blocked(bio);
604 if (rcode < 0) return rcode;
605
606 return fr_bio_error(IO_WOULD_BLOCK);
607 }
608
609 /*
610 * We now have a free item, so we can use it.
611 */
612 fr_assert(fr_bio_retry_list_num_elements(&my->free) > 0);
613 }
614
615 /*
616 * Write out the packet. If there's an error, OR we wrote nothing, return.
617 *
618 * Note that we don't mark the socket as blocked if the next bio didn't write anything. We want
619 * the caller to know that the write didn't succeed, and the caller takes care of managing the
620 * current packet. So there's no need for us to do that.
621 */
622 rcode = next->write(next, packet_ctx, buffer, size);
623 if (rcode <= 0) return rcode;
624
625 /*
626 * Initialize the retry timers after writing the packet.
627 */
628 item = fr_bio_retry_list_pop_head(&my->free);
629 fr_assert(item != NULL);
630
631 fr_assert(item->my == my);
633 .my = my,
634 .retry.start = fr_time(),
635 .packet_ctx = packet_ctx,
636 .buffer = buffer,
637 .size = size,
638 };
639
640 /*
641 * Always initialize the retry timer. That way the sent() callback doesn't have to call
642 * fr_time().
643 *
644 * The application can call fr_bio_retry_entry_init() to re-initialize it, but that's fine.
645 */
646 fr_retry_init(&item->retry, item->retry.start, &my->retry_config);
647
648 /*
649 * Tell the application that we've saved the packet. The "item" pointer allows the application
650 * to cancel this packet if necessary.
651 */
652 my->sent(bio, packet_ctx, buffer, size, item);
653
654 /*
655 * This should never fail.
656 */
657 (void) fr_timer_uctx_insert(my->next_tl, item);
658 (void) fr_timer_uctx_insert(my->expiry_tl, item);
659
660 /*
661 * We only wrote part of the packet, remember to write the rest of it.
662 */
663 if ((size_t) rcode < size) {
664 return fr_bio_retry_save_write(my, item, rcode);
665 }
666
667 return size;
668}
669
670static ssize_t fr_bio_retry_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
671{
672 ssize_t rcode;
674 fr_bio_retry_t *my = talloc_get_type_abort(bio, fr_bio_retry_t);
675 fr_bio_t *next;
676
677 /*
678 * There must be a next bio.
679 */
680 next = fr_bio_next(&my->bio);
681 fr_assert(next != NULL);
682
683 /*
684 * Read the packet. If error or nothing, return immediately.
685 */
686 rcode = next->read(next, packet_ctx, buffer, size);
687 if (rcode <= 0) return rcode;
688
689 /*
690 * Not a valid response to a request, OR a duplicate response to a request: don't return it to
691 * the caller.
692 *
693 * But if it is a duplicate response, update the counters and do cleanups as necessary.
694 */
695 item = NULL;
696 if (!my->response(bio, &item, packet_ctx, buffer, size)) {
697 if (!item) return 0;
698
699 item->retry.replies++;
700
701 /*
702 * We have enough replies. Release it.
703 */
704 if ((item->retry.replies >= item->retry.count) || !fr_time_delta_ispos(my->retry_config.mrd)) {
706 }
707
708 return 0;
709 }
710
711 fr_assert(item != NULL);
712 fr_assert(item->retry.replies == 0);
713 fr_assert(item != my->partial);
714
715 /*
716 * Track when the "most recently sent" packet has a reply. This metric is better than most
717 * others for judging the liveliness of the destination.
718 */
719 if (fr_time_lt(my->info.mrs_time, item->retry.start)) my->info.mrs_time = item->retry.start;
720
721 /*
722 * We have a new reply, remember when that happened. Note that we don't update this timer for
723 * duplicate replies, but perhaps we should?
724 */
725 my->info.last_reply = fr_time();
726
727 /*
728 * We have a new reply. If we've received all of the replies (i.e. one), OR we don't have a
729 * maximum lifetime for this request, then release it immediately.
730 */
731 item->retry.replies++;
732
733 /*
734 * We don't retry application-layer watchdog packets. And we don't run timers for them. The
735 * application is responsible for managing those timers itself.
736 */
737 if (item->reserved) return rcode;
738
739 /*
740 * There are no more packets to send, so this connection is idle.
741 *
742 * Note that partial packets aren't tracked in the timer tree. We can't do retransmits until the
743 * socket is writable.
744 */
745 if (fr_bio_retry_outstanding((fr_bio_t *) my) == 1) my->info.last_idle = my->info.last_reply;
746
747 /*
748 * We have enough replies. Release it.
749 */
750 if ((item->retry.replies >= item->retry.count) || !fr_time_delta_ispos(my->retry_config.mrd)) {
752 return rcode;
753 }
754
755 /*
756 * There are more replies pending. Wait passively for more replies, and clean up the item
757 * when the timer has expired.
758 */
759 item->retry.next = fr_time_add_time_delta(item->retry.start, my->retry_config.mrd);
760
761 (void) fr_timer_uctx_remove(my->next_tl, item);
762 (void) fr_timer_uctx_insert(my->next_tl, item);
763
764 return rcode;
765}
766
767/*
768 * Order the retries by what we have to do next.
769 *
770 * Note that "retry.next" here is capped at "retry.end". So if we need to expire an entry, it will
771 * happen at the "next" retry.
772 */
773static int8_t _next_retry_cmp(void const *one, void const *two)
774{
775 fr_bio_retry_entry_t const *a = one;
776 fr_bio_retry_entry_t const *b = two;
777
778 fr_assert(a->buffer);
779 fr_assert(b->buffer);
780
781 return fr_time_cmp(a->retry.next, b->retry.next);
782}
783
784/*
785 * Order entries by when they expire, when we're not retrying.
786 *
787 * i.e. the socket is blocked, so all retries are paused.
788 */
789static int8_t _expiry_cmp(void const *one, void const *two)
790{
791 fr_bio_retry_entry_t const *a = one;
792 fr_bio_retry_entry_t const *b = two;
793
794 fr_assert(a->buffer);
795 fr_assert(b->buffer);
796
797 return fr_time_cmp(a->retry.end, b->retry.end);
798}
799
800/** Cancel one item.
801 *
802 * If "item" is NULL, the last entry in the timer tree is cancelled.
803 *
804 * @param bio the binary IO handler
805 * @param item the retry context from #fr_bio_retry_sent_t
806 * @return
807 * - <0 error
808 * - 0 - didn't cancel
809 * - 1 - did cancel
810 */
812{
813 fr_bio_retry_t *my = talloc_get_type_abort(bio, fr_bio_retry_t);
814
815 /*
816 * No item passed, try to cancel the first one to expire.
817 */
818 if (!item) {
819 item = fr_timer_uctx_peek(my->expiry_tl);
820 if (!item) return 0;
821
822 /*
823 * This item hasn't had a response, we can't cancel it.
824 */
825 if (!item->retry.replies) return 0;
826 }
827
828 /*
829 * If the caller has cached a previously finished item, then that's a fatal error.
830 */
831 fr_assert(item->buffer != NULL);
832
834
835 return 1;
836}
837
838/** Set a per-packet retry config
839 *
840 * This function should be called from the #fr_bio_retry_sent_t callback to set a unique retry timer for this
841 * packet. If no retry configuration is set, then the main one from the alloc() function is used.
842 */
844{
845 fr_assert(item->buffer != NULL);
846
847 if (item->retry.config) return 0;
848
850
851 fr_retry_init(&item->retry, item->retry.start, cfg);
852
853 return 0;
854}
855
856/** Allow the callbacks / application to know when things are being retried.
857 *
858 * This is not initialized util _after_ fr_bio_retry_entry_start() has been called.
859 */
861{
862 fr_assert(item->buffer != NULL);
863
864 if (!item->retry.config) return NULL;
865
866 return &item->retry;
867}
868
869/** Orderly shutdown.
870 *
871 */
873{
874 fr_bio_retry_t *my = talloc_get_type_abort(bio, fr_bio_retry_t);
876
877 fr_timer_list_disarm(my->next_tl);
878 fr_timer_list_disarm(my->expiry_tl);
879
880 /*
881 * Cancel all outgoing packets. Don't bother updating the tree or the free list, as all of the
882 * entries will be deleted when the memory is freed.
883 */
884 while ((item = fr_timer_uctx_peek(my->next_tl)) != NULL) {
885 (void) fr_timer_uctx_remove(my->next_tl, item);
886 my->release((fr_bio_t *) my, item, FR_BIO_RETRY_CANCELLED);
887 }
888
889 return 0;
890}
891
892/** Allocate a #fr_bio_retry_t
893 *
894 */
895fr_bio_t *fr_bio_retry_alloc(TALLOC_CTX *ctx, size_t max_saved,
900 fr_bio_retry_config_t const *cfg,
901 fr_bio_t *next)
902{
903 size_t i;
906
907 fr_assert(cfg->el);
908
909 /*
910 * Limit to reasonable values.
911 */
912 if (!max_saved) return NULL;
913 if (max_saved > 65536) return NULL;
914
915 my = talloc_zero(ctx, fr_bio_retry_t);
916 if (!my) return NULL;
917
918 /*
919 * Allocate everything up front, to get better locality of reference, less memory fragmentation,
920 * and better reuse of data structures.
921 */
922 items = talloc_array(my, fr_bio_retry_entry_t, max_saved);
923 if (!items) {
924 error:
926 return NULL;
927 }
928
929 /*
930 * Insert the entries into the free list in order.
931 */
932 fr_bio_retry_list_init(&my->free);
933 for (i = 0; i < max_saved; i++) {
934 items[i].my = my;
935 fr_bio_retry_list_insert_tail(&my->free, &items[i]);
936 }
937
939 offsetof(fr_bio_retry_entry_t, next_retry_node),
940 offsetof(fr_bio_retry_entry_t, retry.next));
941 if (!my->next_tl) goto error;
942
944 offsetof(fr_bio_retry_entry_t, expiry_node),
945 offsetof(fr_bio_retry_entry_t, retry.end));
946 if (!my->expiry_tl) goto error;
947
948 /*
949 * The expiry list is run only when writes are blocked. We cannot have both lists active at the
950 * same time.
951 */
952 (void) fr_timer_list_disarm(my->expiry_tl);
953
954 my->sent = sent;
955 if (!rewrite) {
956 my->rewrite = fr_bio_retry_rewrite;
957 } else {
958 my->rewrite = rewrite;
959 }
960 my->response = response;
961 my->release = release;
962
963 my->info.last_idle = fr_time();
964 my->info.el = cfg->el;
965 my->info.cfg = cfg;
966
968
969 my->bio.write = fr_bio_retry_write;
970 my->bio.read = fr_bio_retry_read;
971
972 my->priv_cb.write_blocked = fr_bio_retry_write_blocked;
973 my->priv_cb.write_resume = fr_bio_retry_write_resume;
974 my->priv_cb.shutdown = fr_bio_retry_shutdown;
975
976 fr_bio_chain(&my->bio, next);
977
978 talloc_set_destructor((fr_bio_t *) my, fr_bio_destructor); /* always use a common destructor */
979 return (fr_bio_t *) my;
980}
981
983{
984 fr_bio_retry_t *my = talloc_get_type_abort(bio, fr_bio_retry_t);
985
986 return &my->info;
987}
988
990{
991 fr_bio_retry_t *my = talloc_get_type_abort(bio, fr_bio_retry_t);
992 size_t num;
993
994 num = fr_timer_list_num_events(my->next_tl);
995
996 if (!my->partial) return num;
997
998 /*
999 * Only count partially written items if they haven't been cancelled.
1000 */
1001 return num + !my->partial->cancelled;
1002}
1003
1004/** Reserve an entry for later use with fr_bio_retry_rewrite()
1005 *
1006 * So that application-layer watchdogs can bypass the normal write / retry routines.
1007 */
1009{
1010 fr_bio_retry_t *my = talloc_get_type_abort(bio, fr_bio_retry_t);
1012
1013 item = fr_bio_retry_list_pop_head(&my->free);
1014 if (!item) return NULL;
1015
1016 fr_assert(item->my == my);
1018 .my = my,
1019 .reserved = true,
1020 };
1021
1022 return item;
1023}
static int const char char buffer[256]
Definition acutest.h:578
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 ssize_t fr_bio_retry_read(fr_bio_t *bio, void *packet_ctx, void *buffer, size_t size)
Definition retry.c:670
static int8_t _expiry_cmp(void const *one, void const *two)
Definition retry.c:789
fr_bio_retry_release_t release
callback to release a request / response pair
Definition retry.c:111
int fr_bio_retry_entry_init(UNUSED fr_bio_t *bio, fr_bio_retry_entry_t *item, fr_retry_config_t const *cfg)
Set a per-packet retry config.
Definition retry.c:843
fr_bio_t * fr_bio_retry_alloc(TALLOC_CTX *ctx, size_t max_saved, fr_bio_retry_sent_t sent, fr_bio_retry_response_t response, fr_bio_retry_rewrite_t rewrite, fr_bio_retry_release_t release, fr_bio_retry_config_t const *cfg, fr_bio_t *next)
Allocate a fr_bio_retry_t.
Definition retry.c:895
static ssize_t fr_bio_retry_save_write(fr_bio_retry_t *my, fr_bio_retry_entry_t *item, ssize_t rcode)
Save a partial packet when the write becomes blocked.
Definition retry.c:379
fr_bio_buf_t buffer
to store partial packets
Definition retry.c:113
static void fr_bio_retry_release(fr_bio_retry_t *my, fr_bio_retry_entry_t *item, fr_bio_retry_release_reason_t reason)
Release an entry back to the free list.
Definition retry.c:124
size_t fr_bio_retry_outstanding(fr_bio_t *bio)
Definition retry.c:989
fr_timer_list_t * next_tl
when packets are retried next
Definition retry.c:92
static void fr_bio_retry_next_timer(UNUSED fr_timer_list_t *tl, fr_time_t now, void *uctx)
Run a timer event.
Definition retry.c:526
int fr_bio_retry_entry_cancel(fr_bio_t *bio, fr_bio_retry_entry_t *item)
Cancel one item.
Definition retry.c:811
fr_timer_list_t * expiry_tl
when packets expire, so that we expire packets when the socket is blocked.
Definition retry.c:93
struct fr_bio_retry_list_s fr_bio_retry_list_t
Definition retry.c:56
ssize_t fr_bio_retry_rewrite(fr_bio_t *bio, fr_bio_retry_entry_t *item, const void *buffer, size_t size)
Resend a packet.
Definition retry.c:430
static int8_t _next_retry_cmp(void const *one, void const *two)
Definition retry.c:773
static int fr_bio_retry_write_item(fr_bio_retry_t *my, fr_bio_retry_entry_t *item, fr_time_t now)
Write one item.
Definition retry.c:214
static int fr_bio_retry_shutdown(fr_bio_t *bio)
Orderly shutdown.
Definition retry.c:872
fr_retry_config_t retry_config
Definition retry.c:97
static void fr_bio_retry_expiry_timer(UNUSED fr_timer_list_t *tl, UNUSED fr_time_t now, void *uctx)
Run an expiry timer event.
Definition retry.c:506
fr_bio_retry_entry_t * partial
for partial writes
Definition retry.c:106
const fr_retry_t * fr_bio_retry_entry_info(UNUSED fr_bio_t *bio, fr_bio_retry_entry_t *item)
Allow the callbacks / application to know when things are being retried.
Definition retry.c:860
fr_bio_retry_sent_t sent
callback for when we successfully sent a packet
Definition retry.c:108
fr_bio_retry_response_t response
callback to see if we got a valid response
Definition retry.c:110
static ssize_t fr_bio_retry_write(fr_bio_t *bio, void *packet_ctx, void const *buffer, size_t size)
Write a request, and see if we have a reply.
Definition retry.c:551
ssize_t error
Definition retry.c:99
static ssize_t fr_bio_retry_write_fatal(fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void const *buffer, UNUSED size_t size)
A previous timer write had a fatal error, so we forbid further writes.
Definition retry.c:492
bool all_used
blocked due to no free entries
Definition retry.c:100
static int fr_bio_retry_write_blocked(fr_bio_t *bio)
Writes are blocked.
Definition retry.c:184
static ssize_t fr_bio_retry_write_partial(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size)
There's a partial packet written.
Definition retry.c:315
fr_bio_retry_info_t info
Definition retry.c:95
fr_bio_retry_rewrite_t rewrite
optional callback which can change a packet on retry
Definition retry.c:109
fr_bio_retry_entry_t * fr_bio_retry_item_reserve(fr_bio_t *bio)
Reserve an entry for later use with fr_bio_retry_rewrite()
Definition retry.c:1008
fr_bio_retry_info_t const * fr_bio_retry_info(fr_bio_t *bio)
Definition retry.c:982
static int fr_bio_retry_write_resume(fr_bio_t *bio)
Resume writes.
Definition retry.c:288
void * rewrite_ctx
context specifically for rewriting this packet
Definition retry.c:68
fr_retry_config_t retry_config
base retry config
Definition retry.h:47
void(* fr_bio_retry_release_t)(fr_bio_t *bio, fr_bio_retry_entry_t *retry_ctx, fr_bio_retry_release_reason_t reason)
Callback on release the packet (timeout or have all replies)
Definition retry.h:136
fr_retry_t retry
retry timers and counters
Definition retry.c:70
fr_bio_retry_release_reason_t
Definition retry.h:79
@ FR_BIO_RETRY_WRITE_ERROR
Definition retry.h:83
@ FR_BIO_RETRY_CANCELLED
Definition retry.h:82
@ FR_BIO_RETRY_DONE
Definition retry.h:80
@ FR_BIO_RETRY_NO_REPLY
Definition retry.h:81
struct fr_bio_retry_entry_s fr_bio_retry_entry_t
Definition retry.h:64
uint8_t const * buffer
cached copy of the packet to send
Definition retry.c:80
size_t size
size of the cached packet
Definition retry.c:81
bool reserved
for application-layer watchdog
Definition retry.c:84
void(* fr_bio_retry_sent_t)(fr_bio_t *bio, void *packet_ctx, const void *buffer, size_t size, fr_bio_retry_entry_t *retry_ctx)
Callback for when a packet is sent.
Definition retry.h:98
ssize_t(* fr_bio_retry_rewrite_t)(fr_bio_t *bio, fr_bio_retry_entry_t *retry_ctx, const void *buffer, size_t size)
Definition retry.h:66
bool(* fr_bio_retry_response_t)(fr_bio_t *bio, fr_bio_retry_entry_t **item_p, void *packet_ctx, const void *buffer, size_t size)
Callback on read to see if a packet is a response.
Definition retry.h:123
fr_event_list_t * el
event list
Definition retry.h:45
void * packet_ctx
packet_ctx from the write() call
Definition retry.c:66
fr_rb_node_t expiry_node
for expiries
Definition retry.c:76
bool cancelled
was this item cancelled?
Definition retry.c:83
fr_bio_retry_t * my
so we can get to it from the event timer callback
Definition retry.c:78
fr_bio_retry_rewrite_t rewrite
per-packet rewrite callback
Definition retry.c:67
void * uctx
user-writable context
Definition retry.c:65
Definition retry.c:64
static void fr_bio_chain(fr_bio_t *first, fr_bio_t *second)
Chain one bio after another.
Definition bio_priv.h:84
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
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
static size_t fr_bio_buf_size(fr_bio_buf_t const *bio_buf)
Definition buf.h:151
#define UNUSED
Definition build.h:317
#define FR_DLIST_TYPES(_name)
Define type specific wrapper structs for dlists.
Definition dlist.h:1111
#define FR_DLIST_ENTRY(_name)
Expands to the type name used for the entry wrapper structure.
Definition dlist.h:1097
#define FR_DLIST_FUNCS(_name, _element_type, _element_entry)
Define type specific wrapper functions for dlists.
Definition dlist.h:1134
#define FR_DLIST_HEAD(_name)
Expands to the type name used for the head wrapper structure.
Definition dlist.h:1104
void fr_bio_shutdown & my
Definition fd_errno.h:70
free(array)
talloc_free(hp)
int fr_bio_write_blocked(fr_bio_t *bio)
Internal BIO function to tell all BIOs that it's blocked.
Definition base.c:261
int fr_bio_destructor(fr_bio_t *bio)
Free this bio.
Definition base.c:35
static void * item(fr_lst_t const *lst, fr_lst_index_t idx)
Definition lst.c:122
long int ssize_t
unsigned char uint8_t
static size_t used
ssize_t fr_bio_null_write(UNUSED fr_bio_t *bio, UNUSED void *packet_ctx, UNUSED void const *buffer, UNUSED size_t size)
Always return 0 on write.
Definition null.c:39
#define fr_assert(_expr)
Definition rad_assert.h:38
#define fr_time()
Allow us to arbitrarily manipulate time.
Definition state_test.c:8
static fr_time_t fr_time_add_time_delta(fr_time_t a, fr_time_delta_t b)
Definition time.h:173
static int64_t fr_time_delta_unwrap(fr_time_delta_t time)
Definition time.h:154
#define fr_time_lteq(_a, _b)
Definition time.h:240
#define fr_time_delta_ispos(_a)
Definition time.h:290
#define fr_time_gt(_a, _b)
Definition time.h:237
#define fr_time_lt(_a, _b)
Definition time.h:239
static int8_t fr_time_cmp(fr_time_t a, fr_time_t b)
Compare two fr_time_t values.
Definition time.h:916
"server local" time.
Definition time.h:69
int fr_timer_list_disarm(fr_timer_list_t *tl)
Disarm a timer list.
Definition timer.c:1101
uint64_t fr_timer_list_num_events(fr_timer_list_t *tl)
Return number of pending events.
Definition timer.c:1149
int fr_timer_uctx_insert(fr_timer_list_t *tl, void *uctx)
Insert a uctx into a shared timer, and update the timer.
Definition timer.c:1348
fr_timer_list_t * fr_timer_list_shared_alloc(TALLOC_CTX *ctx, fr_timer_list_t *parent, fr_cmp_t cmp, fr_timer_cb_t callback, size_t node_offset, size_t time_offset)
Allocate a new shared event timer list.
Definition timer.c:1312
int fr_timer_uctx_remove(fr_timer_list_t *tl, void *uctx)
Remove a uctx from a shared timer.
Definition timer.c:1371
void * fr_timer_uctx_peek(fr_timer_list_t *tl)
Definition timer.c:1381
int fr_timer_list_arm(fr_timer_list_t *tl)
Arm (or re-arm) a timer list.
Definition timer.c:1122
An event timer list.
Definition timer.c:50
fr_retry_state_t fr_retry_next(fr_retry_t *r, fr_time_t now)
Initialize a retransmission counter.
Definition retry.c:110
void fr_retry_init(fr_retry_t *r, fr_time_t now, fr_retry_config_t const *config)
Initialize a retransmission counter.
Definition retry.c:36
fr_time_delta_t irt
Initial transmission time.
Definition retry.h:33
fr_retry_state_t
Definition retry.h:45
@ FR_RETRY_CONTINUE
Definition retry.h:46
fr_time_t end
when we will end the retransmissions
Definition retry.h:54
fr_time_t next
when the next timer should be set
Definition retry.h:55