br_stp_timer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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(struct timer_list *t)
  29. {
  30. struct net_bridge *br = from_timer(br, t, hello_timer);
  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. if (br->stp_enabled == BR_KERNEL_STP)
  36. mod_timer(&br->hello_timer,
  37. round_jiffies(jiffies + br->hello_time));
  38. }
  39. spin_unlock(&br->lock);
  40. }
  41. static void br_message_age_timer_expired(struct timer_list *t)
  42. {
  43. struct net_bridge_port *p = from_timer(p, t, message_age_timer);
  44. struct net_bridge *br = p->br;
  45. const bridge_id *id = &p->designated_bridge;
  46. int was_root;
  47. if (p->state == BR_STATE_DISABLED)
  48. return;
  49. br_info(br, "port %u(%s) neighbor %.2x%.2x.%pM lost\n",
  50. (unsigned int) p->port_no, p->dev->name,
  51. id->prio[0], id->prio[1], &id->addr);
  52. /*
  53. * According to the spec, the message age timer cannot be
  54. * running when we are the root bridge. So.. this was_root
  55. * check is redundant. I'm leaving it in for now, though.
  56. */
  57. spin_lock(&br->lock);
  58. if (p->state == BR_STATE_DISABLED)
  59. goto unlock;
  60. was_root = br_is_root_bridge(br);
  61. br_become_designated_port(p);
  62. br_configuration_update(br);
  63. br_port_state_selection(br);
  64. if (br_is_root_bridge(br) && !was_root)
  65. br_become_root_bridge(br);
  66. unlock:
  67. spin_unlock(&br->lock);
  68. }
  69. static void br_forward_delay_timer_expired(struct timer_list *t)
  70. {
  71. struct net_bridge_port *p = from_timer(p, t, forward_delay_timer);
  72. struct net_bridge *br = p->br;
  73. br_debug(br, "port %u(%s) forward delay timer\n",
  74. (unsigned int) p->port_no, p->dev->name);
  75. spin_lock(&br->lock);
  76. if (p->state == BR_STATE_LISTENING) {
  77. br_set_state(p, BR_STATE_LEARNING);
  78. mod_timer(&p->forward_delay_timer,
  79. jiffies + br->forward_delay);
  80. } else if (p->state == BR_STATE_LEARNING) {
  81. br_set_state(p, BR_STATE_FORWARDING);
  82. if (br_is_designated_for_some_port(br))
  83. br_topology_change_detection(br);
  84. netif_carrier_on(br->dev);
  85. }
  86. rcu_read_lock();
  87. br_ifinfo_notify(RTM_NEWLINK, NULL, p);
  88. rcu_read_unlock();
  89. spin_unlock(&br->lock);
  90. }
  91. static void br_tcn_timer_expired(struct timer_list *t)
  92. {
  93. struct net_bridge *br = from_timer(br, t, tcn_timer);
  94. br_debug(br, "tcn timer expired\n");
  95. spin_lock(&br->lock);
  96. if (!br_is_root_bridge(br) && (br->dev->flags & IFF_UP)) {
  97. br_transmit_tcn(br);
  98. mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time);
  99. }
  100. spin_unlock(&br->lock);
  101. }
  102. static void br_topology_change_timer_expired(struct timer_list *t)
  103. {
  104. struct net_bridge *br = from_timer(br, t, topology_change_timer);
  105. br_debug(br, "topo change timer expired\n");
  106. spin_lock(&br->lock);
  107. br->topology_change_detected = 0;
  108. __br_set_topology_change(br, 0);
  109. spin_unlock(&br->lock);
  110. }
  111. static void br_hold_timer_expired(struct timer_list *t)
  112. {
  113. struct net_bridge_port *p = from_timer(p, t, hold_timer);
  114. br_debug(p->br, "port %u(%s) hold timer expired\n",
  115. (unsigned int) p->port_no, p->dev->name);
  116. spin_lock(&p->br->lock);
  117. if (p->config_pending)
  118. br_transmit_config(p);
  119. spin_unlock(&p->br->lock);
  120. }
  121. void br_stp_timer_init(struct net_bridge *br)
  122. {
  123. timer_setup(&br->hello_timer, br_hello_timer_expired, 0);
  124. timer_setup(&br->tcn_timer, br_tcn_timer_expired, 0);
  125. timer_setup(&br->topology_change_timer,
  126. br_topology_change_timer_expired, 0);
  127. }
  128. void br_stp_port_timer_init(struct net_bridge_port *p)
  129. {
  130. timer_setup(&p->message_age_timer, br_message_age_timer_expired, 0);
  131. timer_setup(&p->forward_delay_timer, br_forward_delay_timer_expired, 0);
  132. timer_setup(&p->hold_timer, br_hold_timer_expired, 0);
  133. }
  134. /* Report ticks left (in USER_HZ) used for API */
  135. unsigned long br_timer_value(const struct timer_list *timer)
  136. {
  137. return timer_pending(timer)
  138. ? jiffies_delta_to_clock_t(timer->expires - jiffies) : 0;
  139. }