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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. /** Default timeout value */
  32. #define MIN_TIMEOUT ( TICKS_PER_SEC / 4 )
  33. /** Limit after which the timeout will be deemed permanent */
  34. #define MAX_TIMEOUT ( 10 * TICKS_PER_SEC )
  35. /* The theoretical minimum that the algorithm in stop_timer() can
  36. * adjust the timeout back down to is seven ticks, so set the minimum
  37. * timeout to at least that value for the sake of consistency.
  38. */
  39. #if MIN_TIMEOUT < 7
  40. #undef MIN_TIMEOUT
  41. #define MIN_TIMEOUT 7
  42. #endif
  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. list_add ( &timer->list, &timers );
  56. timer->start = currticks();
  57. if ( timer->timeout < MIN_TIMEOUT )
  58. timer->timeout = MIN_TIMEOUT;
  59. }
  60. /**
  61. * Stop timer
  62. *
  63. * @v timer Retry timer
  64. *
  65. * This stops the timer and updates the timer's timeout value.
  66. */
  67. void stop_timer ( struct retry_timer *timer ) {
  68. unsigned long old_timeout = timer->timeout;
  69. unsigned long runtime;
  70. list_del ( &timer->list );
  71. runtime = currticks() - timer->start;
  72. /* Update timer. Variables are:
  73. *
  74. * r = round-trip time estimate (i.e. runtime)
  75. * t = timeout value (i.e. timer->timeout)
  76. * s = smoothed round-trip time
  77. *
  78. * By choice, we set t = 4s, i.e. allow for four times the
  79. * normal round-trip time to pass before retransmitting.
  80. *
  81. * We want to smooth according to s := ( 7 s + r ) / 8
  82. *
  83. * Since we don't actually store s, this reduces to
  84. * t := ( 7 t / 8 ) + ( r / 2 )
  85. *
  86. */
  87. timer->timeout -= ( timer->timeout >> 3 );
  88. timer->timeout += ( runtime >> 1 );
  89. if ( timer->timeout != old_timeout ) {
  90. DBG ( "Timer updated to %dms\n",
  91. ( ( 1000 * timer->timeout ) / TICKS_PER_SEC ) );
  92. }
  93. }
  94. /**
  95. * Single-step the retry timer list
  96. *
  97. * @v process Retry timer process
  98. */
  99. static void retry_step ( struct process *process ) {
  100. struct retry_timer *timer;
  101. struct retry_timer *tmp;
  102. unsigned long now = currticks();
  103. unsigned long used;
  104. int fail;
  105. list_for_each_entry_safe ( timer, tmp, &timers, list ) {
  106. used = ( now - timer->start );
  107. if ( used >= timer->timeout ) {
  108. /* Stop timer without performing RTT calculations */
  109. list_del ( &timer->list );
  110. /* Back off the timeout value */
  111. timer->timeout <<= 1;
  112. if ( ( fail = ( timer->timeout > MAX_TIMEOUT ) ) )
  113. timer->timeout = MAX_TIMEOUT;
  114. DBG ( "Timer backed off to %dms\n",
  115. ( ( 1000 * timer->timeout ) / TICKS_PER_SEC ) );
  116. /* Call expiry callback */
  117. timer->expired ( timer, fail );
  118. }
  119. }
  120. schedule ( process );
  121. }
  122. /** Retry timer process */
  123. static struct process retry_process = {
  124. .step = retry_step,
  125. };
  126. /** Initialise the retry timer module */
  127. static void init_retry ( void ) {
  128. schedule ( &retry_process );
  129. }
  130. INIT_FN ( INIT_PROCESS, init_retry, NULL, NULL );