2006-05-29 14:55:07 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
2006-06-01 14:33:52 +00:00
|
|
|
* A retry timer is a binary exponential backoff timer. It can be
|
|
|
|
* used to build automatic retransmission into network protocols.
|
2006-08-09 15:54:17 +00:00
|
|
|
*
|
|
|
|
* This implementation of the timer is designed to satisfy RFC 2988
|
|
|
|
* and therefore be usable as a TCP retransmission timer.
|
|
|
|
*
|
|
|
|
*
|
2006-05-29 14:55:07 +00:00
|
|
|
*/
|
|
|
|
|
2006-06-01 14:33:52 +00:00
|
|
|
/** Default timeout value */
|
|
|
|
#define MIN_TIMEOUT ( TICKS_PER_SEC / 4 )
|
2006-05-29 14:55:07 +00:00
|
|
|
|
2006-06-01 14:33:52 +00:00
|
|
|
/** Limit after which the timeout will be deemed permanent */
|
|
|
|
#define MAX_TIMEOUT ( 10 * TICKS_PER_SEC )
|
2006-05-29 14:55:07 +00:00
|
|
|
|
2006-06-01 14:33:52 +00:00
|
|
|
/* The theoretical minimum that the algorithm in stop_timer() can
|
|
|
|
* adjust the timeout back down to is seven ticks, so set the minimum
|
|
|
|
* timeout to at least that value for the sake of consistency.
|
2006-05-29 14:55:07 +00:00
|
|
|
*/
|
2006-06-01 14:33:52 +00:00
|
|
|
#if MIN_TIMEOUT < 7
|
|
|
|
#undef MIN_TIMEOUT
|
|
|
|
#define MIN_TIMEOUT 7
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/** List of running timers */
|
|
|
|
static LIST_HEAD ( timers );
|
2006-05-29 14:55:07 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start timer
|
|
|
|
*
|
|
|
|
* @v timer Retry timer
|
|
|
|
*
|
2006-06-01 14:33:52 +00:00
|
|
|
* This starts the timer running with the current timeout value. If
|
|
|
|
* stop_timer() is not called before the timer expires, the timer will
|
|
|
|
* be stopped and the timer's callback function will be called.
|
2006-05-29 14:55:07 +00:00
|
|
|
*/
|
|
|
|
void start_timer ( struct retry_timer *timer ) {
|
2006-12-22 01:35:21 +00:00
|
|
|
if ( ! timer->start )
|
|
|
|
list_add ( &timer->list, &timers );
|
2006-06-01 14:33:52 +00:00
|
|
|
timer->start = currticks();
|
|
|
|
if ( timer->timeout < MIN_TIMEOUT )
|
|
|
|
timer->timeout = MIN_TIMEOUT;
|
2006-12-27 22:56:39 +00:00
|
|
|
DBG ( "Timer %p started at time %ld (expires at %ld)\n",
|
|
|
|
timer, timer->start, ( timer->start + timer->timeout ) );
|
2006-05-29 14:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Stop timer
|
|
|
|
*
|
|
|
|
* @v timer Retry timer
|
|
|
|
*
|
2006-06-01 14:33:52 +00:00
|
|
|
* This stops the timer and updates the timer's timeout value.
|
2006-05-29 14:55:07 +00:00
|
|
|
*/
|
|
|
|
void stop_timer ( struct retry_timer *timer ) {
|
2006-06-01 14:33:52 +00:00
|
|
|
unsigned long old_timeout = timer->timeout;
|
2006-12-27 22:56:39 +00:00
|
|
|
unsigned long now = currticks();
|
2006-06-01 14:33:52 +00:00
|
|
|
unsigned long runtime;
|
|
|
|
|
2006-12-22 01:35:21 +00:00
|
|
|
/* If timer was already stopped, do nothing */
|
|
|
|
if ( ! timer->start )
|
|
|
|
return;
|
|
|
|
|
2006-05-29 14:55:07 +00:00
|
|
|
list_del ( &timer->list );
|
2006-12-27 22:56:39 +00:00
|
|
|
runtime = ( now - timer->start );
|
2006-12-22 01:35:21 +00:00
|
|
|
timer->start = 0;
|
2006-12-27 22:56:39 +00:00
|
|
|
DBG ( "Timer %p stopped at time %ld (ran for %ld)\n",
|
|
|
|
timer, now, runtime );
|
2006-06-01 14:33:52 +00:00
|
|
|
|
|
|
|
/* Update timer. Variables are:
|
|
|
|
*
|
|
|
|
* r = round-trip time estimate (i.e. runtime)
|
|
|
|
* t = timeout value (i.e. timer->timeout)
|
|
|
|
* s = smoothed round-trip time
|
|
|
|
*
|
|
|
|
* By choice, we set t = 4s, i.e. allow for four times the
|
|
|
|
* normal round-trip time to pass before retransmitting.
|
|
|
|
*
|
|
|
|
* We want to smooth according to s := ( 7 s + r ) / 8
|
|
|
|
*
|
|
|
|
* Since we don't actually store s, this reduces to
|
|
|
|
* t := ( 7 t / 8 ) + ( r / 2 )
|
|
|
|
*
|
|
|
|
*/
|
2006-08-09 15:54:17 +00:00
|
|
|
if ( timer->count ) {
|
|
|
|
timer->count--;
|
|
|
|
} else {
|
|
|
|
timer->timeout -= ( timer->timeout >> 3 );
|
|
|
|
timer->timeout += ( runtime >> 1 );
|
|
|
|
if ( timer->timeout != old_timeout ) {
|
2006-12-27 22:56:39 +00:00
|
|
|
DBG ( "Timer %p timeout updated to %ld\n",
|
|
|
|
timer, timer->timeout );
|
2006-08-09 15:54:17 +00:00
|
|
|
}
|
2006-06-01 14:33:52 +00:00
|
|
|
}
|
2006-05-29 14:55:07 +00:00
|
|
|
}
|
|
|
|
|
2006-08-09 15:54:17 +00:00
|
|
|
/**
|
|
|
|
* Handle expired timer
|
|
|
|
*
|
|
|
|
* @v timer Retry timer
|
|
|
|
*/
|
|
|
|
static void timer_expired ( struct retry_timer *timer ) {
|
|
|
|
int fail;
|
|
|
|
|
|
|
|
/* Stop timer without performing RTT calculations */
|
2006-12-27 22:56:39 +00:00
|
|
|
DBG ( "Timer %p stopped at time %ld on expiry\n",
|
|
|
|
timer, currticks() );
|
2006-08-09 15:54:17 +00:00
|
|
|
list_del ( &timer->list );
|
2006-12-22 01:35:21 +00:00
|
|
|
timer->start = 0;
|
2006-08-09 15:54:17 +00:00
|
|
|
timer->count++;
|
|
|
|
|
|
|
|
/* Back off the timeout value */
|
|
|
|
timer->timeout <<= 1;
|
|
|
|
if ( ( fail = ( timer->timeout > MAX_TIMEOUT ) ) )
|
|
|
|
timer->timeout = MAX_TIMEOUT;
|
2006-12-27 22:56:39 +00:00
|
|
|
DBG ( "Timer %p timeout backed off to %ld\n",
|
|
|
|
timer, timer->timeout );
|
2006-08-09 15:54:17 +00:00
|
|
|
|
|
|
|
/* Call expiry callback */
|
|
|
|
timer->expired ( timer, fail );
|
|
|
|
}
|
|
|
|
|
2006-05-29 14:55:07 +00:00
|
|
|
/**
|
|
|
|
* 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();
|
2006-06-01 14:33:52 +00:00
|
|
|
unsigned long used;
|
2006-05-29 14:55:07 +00:00
|
|
|
|
|
|
|
list_for_each_entry_safe ( timer, tmp, &timers, list ) {
|
2006-06-01 14:33:52 +00:00
|
|
|
used = ( now - timer->start );
|
2006-08-09 15:54:17 +00:00
|
|
|
if ( used >= timer->timeout )
|
|
|
|
timer_expired ( timer );
|
2006-05-29 14:55:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 );
|