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

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