summaryrefslogtreecommitdiffstats
path: root/src/net/retry.c
blob: fd7042663975a9a4fc9bc778fe5506a2915d1c3f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
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 );