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 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stddef.h>
  19. #include <gpxe/timer.h>
  20. #include <gpxe/list.h>
  21. #include <gpxe/process.h>
  22. #include <gpxe/init.h>
  23. #include <gpxe/retry.h>
  24. /** @file
  25. *
  26. * Retry timers
  27. *
  28. * A retry timer is a binary exponential backoff timer. It can be
  29. * used to build automatic retransmission into network protocols.
  30. *
  31. * This implementation of the timer is designed to satisfy RFC 2988
  32. * and therefore be usable as a TCP retransmission timer.
  33. *
  34. *
  35. */
  36. /* The theoretical minimum that the algorithm in stop_timer() can
  37. * adjust the timeout back down to is seven ticks, so set the minimum
  38. * timeout to at least that value for the sake of consistency.
  39. */
  40. #define MIN_TIMEOUT 7
  41. /** List of running timers */
  42. static LIST_HEAD ( timers );
  43. /**
  44. * Start timer
  45. *
  46. * @v timer Retry timer
  47. *
  48. * This starts the timer running with the current timeout value. If
  49. * stop_timer() is not called before the timer expires, the timer will
  50. * be stopped and the timer's callback function will be called.
  51. */
  52. void start_timer ( struct retry_timer *timer ) {
  53. if ( ! timer->running )
  54. list_add ( &timer->list, &timers );
  55. timer->start = currticks();
  56. timer->running = 1;
  57. /* 0 means "use default timeout" */
  58. if ( timer->min_timeout == 0 )
  59. timer->min_timeout = DEFAULT_MIN_TIMEOUT;
  60. /* We must never be less than MIN_TIMEOUT under any circumstances */
  61. if ( timer->min_timeout < MIN_TIMEOUT )
  62. timer->min_timeout = MIN_TIMEOUT;
  63. /* Honor user-specified minimum timeout */
  64. if ( timer->timeout < timer->min_timeout )
  65. timer->timeout = timer->min_timeout;
  66. DBG2 ( "Timer %p started at time %ld (expires at %ld)\n",
  67. timer, timer->start, ( timer->start + timer->timeout ) );
  68. }
  69. /**
  70. * Start timer with a specified fixed timeout
  71. *
  72. * @v timer Retry timer
  73. * @v timeout Timeout, in ticks
  74. */
  75. void start_timer_fixed ( struct retry_timer *timer, unsigned long timeout ) {
  76. start_timer ( timer );
  77. timer->timeout = timeout;
  78. DBG2 ( "Timer %p expiry time changed to %ld\n",
  79. timer, ( timer->start + timer->timeout ) );
  80. }
  81. /**
  82. * Stop timer
  83. *
  84. * @v timer Retry timer
  85. *
  86. * This stops the timer and updates the timer's timeout value.
  87. */
  88. void stop_timer ( struct retry_timer *timer ) {
  89. unsigned long old_timeout = timer->timeout;
  90. unsigned long now = currticks();
  91. unsigned long runtime;
  92. /* If timer was already stopped, do nothing */
  93. if ( ! timer->running )
  94. return;
  95. list_del ( &timer->list );
  96. runtime = ( now - timer->start );
  97. timer->running = 0;
  98. DBG2 ( "Timer %p stopped at time %ld (ran for %ld)\n",
  99. timer, now, runtime );
  100. /* Update timer. Variables are:
  101. *
  102. * r = round-trip time estimate (i.e. runtime)
  103. * t = timeout value (i.e. timer->timeout)
  104. * s = smoothed round-trip time
  105. *
  106. * By choice, we set t = 4s, i.e. allow for four times the
  107. * normal round-trip time to pass before retransmitting.
  108. *
  109. * We want to smooth according to s := ( 7 s + r ) / 8
  110. *
  111. * Since we don't actually store s, this reduces to
  112. * t := ( 7 t / 8 ) + ( r / 2 )
  113. *
  114. */
  115. if ( timer->count ) {
  116. timer->count--;
  117. } else {
  118. timer->timeout -= ( timer->timeout >> 3 );
  119. timer->timeout += ( runtime >> 1 );
  120. if ( timer->timeout != old_timeout ) {
  121. DBG ( "Timer %p timeout updated to %ld\n",
  122. timer, timer->timeout );
  123. }
  124. }
  125. }
  126. /**
  127. * Handle expired timer
  128. *
  129. * @v timer Retry timer
  130. */
  131. static void timer_expired ( struct retry_timer *timer ) {
  132. int fail;
  133. /* Stop timer without performing RTT calculations */
  134. DBG2 ( "Timer %p stopped at time %ld on expiry\n",
  135. timer, currticks() );
  136. assert ( timer->running );
  137. list_del ( &timer->list );
  138. timer->running = 0;
  139. timer->count++;
  140. /* Back off the timeout value */
  141. timer->timeout <<= 1;
  142. if ( timer->max_timeout == 0 ) /* 0 means "use default timeout" */
  143. timer->max_timeout = DEFAULT_MAX_TIMEOUT;
  144. if ( ( fail = ( timer->timeout > timer->max_timeout ) ) )
  145. timer->timeout = timer->max_timeout;
  146. DBG ( "Timer %p timeout backed off to %ld\n",
  147. timer, timer->timeout );
  148. /* Call expiry callback */
  149. timer->expired ( timer, fail );
  150. }
  151. /**
  152. * Single-step the retry timer list
  153. *
  154. * @v process Retry timer process
  155. */
  156. static void retry_step ( struct process *process __unused ) {
  157. struct retry_timer *timer;
  158. struct retry_timer *tmp;
  159. unsigned long now = currticks();
  160. unsigned long used;
  161. list_for_each_entry_safe ( timer, tmp, &timers, list ) {
  162. used = ( now - timer->start );
  163. if ( used >= timer->timeout )
  164. timer_expired ( timer );
  165. }
  166. }
  167. /** Retry timer process */
  168. struct process retry_process __permanent_process = {
  169. .step = retry_step,
  170. };