Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

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