br_stp_timer.c 4.6 KB

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