aboutsummaryrefslogtreecommitdiff
path: root/src/net/retry.c
diff options
context:
space:
mode:
authorMichael Brown <mcb30@etherboot.org>2006-05-29 14:55:07 +0000
committerMichael Brown <mcb30@etherboot.org>2006-05-29 14:55:07 +0000
commit1db1a6dad3209fa03221bb420665f04e8b6a8418 (patch)
tree1e05f9825d53088494e3df4af85a5ac5e2b2ac9b /src/net/retry.c
parent654133889709631cb7aa9390134cada1b7067c86 (diff)
downloadipxe-1db1a6dad3209fa03221bb420665f04e8b6a8418.zip
ipxe-1db1a6dad3209fa03221bb420665f04e8b6a8418.tar.gz
ipxe-1db1a6dad3209fa03221bb420665f04e8b6a8418.tar.bz2
Added first sketch of a generic retry timer mechanism. The idea is to use
these timer objects in AoE and UDP protocols (where there is no underlying retransmission mechanism) without requiring each protocol to implement its own individual retry logic. Eventually, we should be able to use the same timer code for TCP retransmissions as well.
Diffstat (limited to 'src/net/retry.c')
-rw-r--r--src/net/retry.c130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/net/retry.c b/src/net/retry.c
new file mode 100644
index 0000000..f00c178
--- /dev/null
+++ b/src/net/retry.c
@@ -0,0 +1,130 @@
+/*
+ * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
+#include <stddef.h>
+#include <latch.h>
+#include <gpxe/list.h>
+#include <gpxe/process.h>
+#include <gpxe/init.h>
+#include <gpxe/retry.h>
+
+/** @file
+ *
+ * Retry timers
+ *
+ * A retry timer is a truncated binary exponential backoff timer. It
+ * can be used to build automatic retransmission into network
+ * protocols.
+ */
+
+/** List of running timers */
+static LIST_HEAD ( timers );
+
+/**
+ * Reload timer
+ *
+ * @v timer Retry timer
+ *
+ * This reloads the timer with a new expiry time. The expiry time
+ * will be the timer's base timeout value, shifted left by the number
+ * of retries (i.e. the number of timer expiries since the last timer
+ * reset).
+ */
+static void reload_timer ( struct retry_timer *timer ) {
+ unsigned int exp;
+
+ exp = timer->retries;
+ if ( exp > BACKOFF_LIMIT )
+ exp = BACKOFF_LIMIT;
+ timer->expiry = currticks() + ( timer->base << exp );
+}
+
+/**
+ * Reset timer
+ *
+ * @v timer Retry timer
+ *
+ * This resets the timer, i.e. clears its retry count and starts it
+ * running with its base timeout value.
+ *
+ * Note that it is explicitly permitted to call reset_timer() on an
+ * inactive timer.
+ */
+void reset_timer ( struct retry_timer *timer ) {
+ timer->retries = 0;
+ reload_timer ( timer );
+}
+
+/**
+ * Start timer
+ *
+ * @v timer Retry timer
+ *
+ * This resets the timer and starts it running (i.e. adds it to the
+ * list of running timers). The retry_timer::base and
+ * retry_timer::callback fields must have been filled in.
+ */
+void start_timer ( struct retry_timer *timer ) {
+ list_add ( &timer->list, &timers );
+ reset_timer ( timer );
+}
+
+/**
+ * Stop timer
+ *
+ * @v timer Retry timer
+ *
+ * This stops the timer (i.e. removes it from the list of running
+ * timers).
+ */
+void stop_timer ( struct retry_timer *timer ) {
+ list_del ( &timer->list );
+}
+
+/**
+ * Single-step the retry timer list
+ *
+ * @v process Retry timer process
+ */
+static void retry_step ( struct process *process ) {
+ struct retry_timer *timer;
+ struct retry_timer *tmp;
+ unsigned long now = currticks();
+
+ list_for_each_entry_safe ( timer, tmp, &timers, list ) {
+ if ( timer->expiry >= now ) {
+ timer->retries++;
+ reload_timer ( timer );
+ timer->expired ( timer );
+ }
+ }
+
+ schedule ( process );
+}
+
+/** Retry timer process */
+static struct process retry_process = {
+ .step = retry_step,
+};
+
+/** Initialise the retry timer module */
+static void init_retry ( void ) {
+ schedule ( &retry_process );
+}
+
+INIT_FN ( INIT_PROCESS, init_retry, NULL, NULL );