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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 <latch.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. /** Default timeout value */
  37. #define MIN_TIMEOUT ( TICKS_PER_SEC / 4 )
  38. /** Limit after which the timeout will be deemed permanent */
  39. #define MAX_TIMEOUT ( 10 * TICKS_PER_SEC )
  40. /* The theoretical minimum that the algorithm in stop_timer() can
  41. * adjust the timeout back down to is seven ticks, so set the minimum
  42. * timeout to at least that value for the sake of consistency.
  43. */
  44. #if MIN_TIMEOUT < 7
  45. #undef MIN_TIMEOUT
  46. #define MIN_TIMEOUT 7
  47. #endif
  48. /** List of running timers */
  49. static LIST_HEAD ( timers );
  50. /**
  51. * Start timer
  52. *
  53. * @v timer Retry timer
  54. *
  55. * This starts the timer running with the current timeout value. If
  56. * stop_timer() is not called before the timer expires, the timer will
  57. * be stopped and the timer's callback function will be called.
  58. */
  59. void start_timer ( struct retry_timer *timer ) {
  60. if ( ! timer_running ( timer ) )
  61. list_add ( &timer->list, &timers );
  62. timer->start = currticks();
  63. if ( timer->timeout < MIN_TIMEOUT )
  64. timer->timeout = MIN_TIMEOUT;
  65. DBG2 ( "Timer %p started at time %ld (expires at %ld)\n",
  66. timer, timer->start, ( timer->start + timer->timeout ) );
  67. }
  68. /**
  69. * Start timer with no delay
  70. *
  71. * @v timer Retry timer
  72. *
  73. * This starts the timer running with a zero timeout value.
  74. */
  75. void start_timer_nodelay ( struct retry_timer *timer ) {
  76. start_timer ( timer );
  77. timer->timeout = 0;
  78. }
  79. /**
  80. * Stop timer
  81. *
  82. * @v timer Retry timer
  83. *
  84. * This stops the timer and updates the timer's timeout value.
  85. */
  86. void stop_timer ( struct retry_timer *timer ) {
  87. unsigned long old_timeout = timer->timeout;
  88. unsigned long now = currticks();
  89. unsigned long runtime;
  90. /* If timer was already stopped, do nothing */
  91. if ( ! timer_running ( timer ) )
  92. return;
  93. list_del ( &timer->list );
  94. runtime = ( now - timer->start );
  95. timer->start = 0;
  96. DBG2 ( "Timer %p stopped at time %ld (ran for %ld)\n",
  97. timer, now, runtime );
  98. /* Update timer. Variables are:
  99. *
  100. * r = round-trip time estimate (i.e. runtime)
  101. * t = timeout value (i.e. timer->timeout)
  102. * s = smoothed round-trip time
  103. *
  104. * By choice, we set t = 4s, i.e. allow for four times the
  105. * normal round-trip time to pass before retransmitting.
  106. *
  107. * We want to smooth according to s := ( 7 s + r ) / 8
  108. *
  109. * Since we don't actually store s, this reduces to
  110. * t := ( 7 t / 8 ) + ( r / 2 )
  111. *
  112. */
  113. if ( timer->count ) {
  114. timer->count--;
  115. } else {
  116. timer->timeout -= ( timer->timeout >> 3 );
  117. timer->timeout += ( runtime >> 1 );
  118. if ( timer->timeout != old_timeout ) {
  119. DBG ( "Timer %p timeout updated to %ld\n",
  120. timer, timer->timeout );
  121. }
  122. }
  123. }
  124. /**
  125. * Handle expired timer
  126. *
  127. * @v timer Retry timer
  128. */
  129. static void timer_expired ( struct retry_timer *timer ) {
  130. int fail;
  131. /* Stop timer without performing RTT calculations */
  132. DBG2 ( "Timer %p stopped at time %ld on expiry\n",
  133. timer, currticks() );
  134. list_del ( &timer->list );
  135. timer->start = 0;
  136. timer->count++;
  137. /* Back off the timeout value */
  138. timer->timeout <<= 1;
  139. if ( ( fail = ( timer->timeout > MAX_TIMEOUT ) ) )
  140. timer->timeout = MAX_TIMEOUT;
  141. DBG ( "Timer %p timeout backed off to %ld\n",
  142. timer, timer->timeout );
  143. /* Call expiry callback */
  144. timer->expired ( timer, fail );
  145. }
  146. /**
  147. * Single-step the retry timer list
  148. *
  149. * @v process Retry timer process
  150. */
  151. static void retry_step ( struct process *process __unused ) {
  152. struct retry_timer *timer;
  153. struct retry_timer *tmp;
  154. unsigned long now = currticks();
  155. unsigned long used;
  156. list_for_each_entry_safe ( timer, tmp, &timers, list ) {
  157. used = ( now - timer->start );
  158. if ( used >= timer->timeout )
  159. timer_expired ( timer );
  160. }
  161. }
  162. /** Retry timer process */
  163. struct process retry_process __permanent_process = {
  164. .step = retry_step,
  165. };