br_stp_timer.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Spanning tree protocol; timer-related code
  3. * Linux ethernet bridge
  4. *
  5. * Authors:
  6. * Lennert Buytenhek <buytenh@gnu.org>
  7. *
  8. * $Id: br_stp_timer.c,v 1.3 2000/05/05 02:17:17 davem Exp $
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU General Public License
  12. * as published by the Free Software Foundation; either version
  13. * 2 of the License, or (at your option) any later version.
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/times.h>
  17. #include <linux/smp_lock.h>
  18. #include "br_private.h"
  19. #include "br_private_stp.h"
  20. /* called under bridge lock */
  21. static int br_is_designated_for_some_port(const struct net_bridge *br)
  22. {
  23. struct net_bridge_port *p;
  24. list_for_each_entry(p, &br->port_list, list) {
  25. if (p->state != BR_STATE_DISABLED &&
  26. !memcmp(&p->designated_bridge, &br->bridge_id, 8))
  27. return 1;
  28. }
  29. return 0;
  30. }
  31. static void br_hello_timer_expired(unsigned long arg)
  32. {
  33. struct net_bridge *br = (struct net_bridge *)arg;
  34. pr_debug("%s: hello timer expired\n", br->dev->name);
  35. spin_lock_bh(&br->lock);
  36. if (br->dev->flags & IFF_UP) {
  37. br_config_bpdu_generation(br);
  38. mod_timer(&br->hello_timer, jiffies + br->hello_time);
  39. }
  40. spin_unlock_bh(&br->lock);
  41. }
  42. static void br_message_age_timer_expired(unsigned long arg)
  43. {
  44. struct net_bridge_port *p = (struct net_bridge_port *) arg;
  45. struct net_bridge *br = p->br;
  46. const bridge_id *id = &p->designated_bridge;
  47. int was_root;
  48. if (p->state == BR_STATE_DISABLED)
  49. return;
  50. pr_info("%s: neighbor %.2x%.2x.%.2x:%.2x:%.2x:%.2x:%.2x:%.2x lost on port %d(%s)\n",
  51. br->dev->name,
  52. id->prio[0], id->prio[1],
  53. id->addr[0], id->addr[1], id->addr[2],
  54. id->addr[3], id->addr[4], id->addr[5],
  55. p->port_no, p->dev->name);
  56. /*
  57. * According to the spec, the message age timer cannot be
  58. * running when we are the root bridge. So.. this was_root
  59. * check is redundant. I'm leaving it in for now, though.
  60. */
  61. spin_lock_bh(&br->lock);
  62. if (p->state == BR_STATE_DISABLED)
  63. goto unlock;
  64. was_root = br_is_root_bridge(br);
  65. br_become_designated_port(p);
  66. br_configuration_update(br);
  67. br_port_state_selection(br);
  68. if (br_is_root_bridge(br) && !was_root)
  69. br_become_root_bridge(br);
  70. unlock:
  71. spin_unlock_bh(&br->lock);
  72. }
  73. static void br_forward_delay_timer_expired(unsigned long arg)
  74. {
  75. struct net_bridge_port *p = (struct net_bridge_port *) arg;
  76. struct net_bridge *br = p->br;
  77. pr_debug("%s: %d(%s) forward delay timer\n",
  78. br->dev->name, p->port_no, p->dev->name);
  79. spin_lock_bh(&br->lock);
  80. if (p->state == BR_STATE_LISTENING) {
  81. p->state = BR_STATE_LEARNING;
  82. mod_timer(&p->forward_delay_timer,
  83. jiffies + br->forward_delay);
  84. } else if (p->state == BR_STATE_LEARNING) {
  85. p->state = BR_STATE_FORWARDING;
  86. if (br_is_designated_for_some_port(br))
  87. br_topology_change_detection(br);
  88. }
  89. br_log_state(p);
  90. spin_unlock_bh(&br->lock);
  91. }
  92. static void br_tcn_timer_expired(unsigned long arg)
  93. {
  94. struct net_bridge *br = (struct net_bridge *) arg;
  95. pr_debug("%s: tcn timer expired\n", br->dev->name);
  96. spin_lock_bh(&br->lock);
  97. if (br->dev->flags & IFF_UP) {
  98. br_transmit_tcn(br);
  99. mod_timer(&br->tcn_timer,jiffies + br->bridge_hello_time);
  100. }
  101. spin_unlock_bh(&br->lock);
  102. }
  103. static void br_topology_change_timer_expired(unsigned long arg)
  104. {
  105. struct net_bridge *br = (struct net_bridge *) arg;
  106. pr_debug("%s: topo change timer expired\n", br->dev->name);
  107. spin_lock_bh(&br->lock);
  108. br->topology_change_detected = 0;
  109. br->topology_change = 0;
  110. spin_unlock_bh(&br->lock);
  111. }
  112. static void br_hold_timer_expired(unsigned long arg)
  113. {
  114. struct net_bridge_port *p = (struct net_bridge_port *) arg;
  115. pr_debug("%s: %d(%s) hold timer expired\n",
  116. p->br->dev->name, p->port_no, p->dev->name);
  117. spin_lock_bh(&p->br->lock);
  118. if (p->config_pending)
  119. br_transmit_config(p);
  120. spin_unlock_bh(&p->br->lock);
  121. }
  122. static inline void br_timer_init(struct timer_list *timer,
  123. void (*_function)(unsigned long),
  124. unsigned long _data)
  125. {
  126. init_timer(timer);
  127. timer->function = _function;
  128. timer->data = _data;
  129. }
  130. void br_stp_timer_init(struct net_bridge *br)
  131. {
  132. br_timer_init(&br->hello_timer, br_hello_timer_expired,
  133. (unsigned long) br);
  134. br_timer_init(&br->tcn_timer, br_tcn_timer_expired,
  135. (unsigned long) br);
  136. br_timer_init(&br->topology_change_timer,
  137. br_topology_change_timer_expired,
  138. (unsigned long) br);
  139. br_timer_init(&br->gc_timer, br_fdb_cleanup, (unsigned long) br);
  140. }
  141. void br_stp_port_timer_init(struct net_bridge_port *p)
  142. {
  143. br_timer_init(&p->message_age_timer, br_message_age_timer_expired,
  144. (unsigned long) p);
  145. br_timer_init(&p->forward_delay_timer, br_forward_delay_timer_expired,
  146. (unsigned long) p);
  147. br_timer_init(&p->hold_timer, br_hold_timer_expired,
  148. (unsigned long) p);
  149. }
  150. /* Report ticks left (in USER_HZ) used for API */
  151. unsigned long br_timer_value(const struct timer_list *timer)
  152. {
  153. return timer_pending(timer)
  154. ? jiffies_to_clock_t(timer->expires - jiffies) : 0;
  155. }