You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

retry.c 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. *
  19. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stddef.h>
  25. #include <ipxe/timer.h>
  26. #include <ipxe/list.h>
  27. #include <ipxe/process.h>
  28. #include <ipxe/init.h>
  29. #include <ipxe/retry.h>
  30. /** @file
  31. *
  32. * Retry timers
  33. *
  34. * A retry timer is a binary exponential backoff timer. It can be
  35. * used to build automatic retransmission into network protocols.
  36. *
  37. * This implementation of the timer is designed to satisfy RFC 2988
  38. * and therefore be usable as a TCP retransmission timer.
  39. *
  40. */
  41. /* The theoretical minimum that the algorithm in stop_timer() can
  42. * adjust the timeout back down to is seven ticks, so set the minimum
  43. * timeout to at least that value for the sake of consistency.
  44. */
  45. #define MIN_TIMEOUT 7
  46. /** List of running timers */
  47. static LIST_HEAD ( timers );
  48. /**
  49. * Start timer with a specified timeout
  50. *
  51. * @v timer Retry timer
  52. * @v timeout Timeout, in ticks
  53. *
  54. * This starts the timer running with the specified timeout value. If
  55. * stop_timer() is not called before the timer expires, the timer will
  56. * be stopped and the timer's callback function will be called.
  57. */
  58. void start_timer_fixed ( struct retry_timer *timer, unsigned long timeout ) {
  59. /* Add to list of running timers (if applicable) */
  60. if ( ! timer->running ) {
  61. list_add ( &timer->list, &timers );
  62. ref_get ( timer->refcnt );
  63. timer->running = 1;
  64. }
  65. /* Record start time */
  66. timer->start = currticks();
  67. /* Record timeout */
  68. timer->timeout = timeout;
  69. DBGC2 ( timer, "Timer %p started at time %ld (expires at %ld)\n",
  70. timer, timer->start, ( timer->start + timer->timeout ) );
  71. }
  72. /**
  73. * Start timer
  74. *
  75. * @v timer Retry timer
  76. *
  77. * This starts the timer running with the current timeout value
  78. * (rounded up to the minimum timeout value). If stop_timer() is not
  79. * called before the timer expires, the timer will be stopped and the
  80. * timer's callback function will be called.
  81. */
  82. void start_timer ( struct retry_timer *timer ) {
  83. unsigned long timeout = timer->timeout;
  84. unsigned long min;
  85. /* Calculate minimum timeout */
  86. min = ( timer->min ? timer->min : DEFAULT_MIN_TIMEOUT );
  87. if ( min < MIN_TIMEOUT )
  88. min = MIN_TIMEOUT;
  89. /* Ensure timeout is at least the minimum */
  90. if ( timeout < min )
  91. timeout = min;
  92. /* Start timer with this timeout */
  93. start_timer_fixed ( timer, timeout );
  94. }
  95. /**
  96. * Stop timer
  97. *
  98. * @v timer Retry timer
  99. *
  100. * This stops the timer and updates the timer's timeout value.
  101. */
  102. void stop_timer ( struct retry_timer *timer ) {
  103. unsigned long old_timeout = timer->timeout;
  104. unsigned long now = currticks();
  105. unsigned long runtime;
  106. /* If timer was already stopped, do nothing */
  107. if ( ! timer->running )
  108. return;
  109. list_del ( &timer->list );
  110. runtime = ( now - timer->start );
  111. timer->running = 0;
  112. DBGC2 ( timer, "Timer %p stopped at time %ld (ran for %ld)\n",
  113. timer, now, runtime );
  114. /* Update timer. Variables are:
  115. *
  116. * r = round-trip time estimate (i.e. runtime)
  117. * t = timeout value (i.e. timer->timeout)
  118. * s = smoothed round-trip time
  119. *
  120. * By choice, we set t = 4s, i.e. allow for four times the
  121. * normal round-trip time to pass before retransmitting.
  122. *
  123. * We want to smooth according to s := ( 7 s + r ) / 8
  124. *
  125. * Since we don't actually store s, this reduces to
  126. * t := ( 7 t / 8 ) + ( r / 2 )
  127. *
  128. */
  129. if ( timer->count ) {
  130. timer->count--;
  131. } else {
  132. timer->timeout -= ( timer->timeout >> 3 );
  133. timer->timeout += ( runtime >> 1 );
  134. if ( timer->timeout != old_timeout ) {
  135. DBGC ( timer, "Timer %p timeout updated to %ld\n",
  136. timer, timer->timeout );
  137. }
  138. }
  139. ref_put ( timer->refcnt );
  140. }
  141. /**
  142. * Handle expired timer
  143. *
  144. * @v timer Retry timer
  145. */
  146. static void timer_expired ( struct retry_timer *timer ) {
  147. struct refcnt *refcnt = timer->refcnt;
  148. unsigned long max = ( timer->max ? timer->max : DEFAULT_MAX_TIMEOUT );
  149. int fail;
  150. /* Stop timer without performing RTT calculations */
  151. DBGC2 ( timer, "Timer %p stopped at time %ld on expiry\n",
  152. timer, currticks() );
  153. assert ( timer->running );
  154. list_del ( &timer->list );
  155. timer->running = 0;
  156. timer->count++;
  157. /* Back off the timeout value */
  158. timer->timeout <<= 1;
  159. if ( ( fail = ( timer->timeout > max ) ) )
  160. timer->timeout = max;
  161. DBGC ( timer, "Timer %p timeout backed off to %ld\n",
  162. timer, timer->timeout );
  163. /* Call expiry callback */
  164. timer->expired ( timer, fail );
  165. /* If refcnt is NULL, then timer may already have been freed */
  166. ref_put ( refcnt );
  167. }
  168. /**
  169. * Poll the retry timer list
  170. *
  171. */
  172. void retry_poll ( void ) {
  173. struct retry_timer *timer;
  174. unsigned long now = currticks();
  175. unsigned long used;
  176. /* Process at most one timer expiry. We cannot process
  177. * multiple expiries in one pass, because one timer expiring
  178. * may end up triggering another timer's deletion from the
  179. * list.
  180. */
  181. list_for_each_entry ( timer, &timers, list ) {
  182. used = ( now - timer->start );
  183. if ( used >= timer->timeout ) {
  184. timer_expired ( timer );
  185. break;
  186. }
  187. }
  188. }
  189. /**
  190. * Single-step the retry timer list
  191. *
  192. * @v process Retry timer process
  193. */
  194. static void retry_step ( struct process *process __unused ) {
  195. retry_poll();
  196. }
  197. /** Retry timer process */
  198. PERMANENT_PROCESS ( retry_process, retry_step );