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