br_stp_timer.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Spanning tree protocol; timer-related code
  4. * Linux ethernet bridge
  5. *
  6. * Authors:
  7. * Lennert Buytenhek <buytenh@gnu.org>
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/times.h>
  11. #include "br_private.h"
  12. #include "br_private_stp.h"
  13. /* called under bridge lock */
  14. static int br_is_designated_for_some_port(const struct net_bridge *br)
  15. {
  16. struct net_bridge_port *p;
  17. list_for_each_entry(p, &br->port_list, list) {
  18. if (p->state != BR_STATE_DISABLED &&
  19. !memcmp(&p->designated_bridge, &br->bridge_id, 8))
  20. return 1;
  21. }
  22. return 0;
  23. }
  24. static void br_hello_timer_expired(struct timer_list *t)
  25. {
  26. struct net_bridge *br = timer_container_of(br, t, hello_timer);
  27. br_debug(br, "hello timer expired\n");
  28. spin_lock(&br->lock);
  29. if (br->dev->flags & IFF_UP) {
  30. br_config_bpdu_generation(br);
  31. if (br->stp_enabled == BR_KERNEL_STP)
  32. mod_timer(&br->hello_timer,
  33. round_jiffies(jiffies + br->hello_time));
  34. }
  35. spin_unlock(&br->lock);
  36. }
  37. static void br_message_age_timer_expired(struct timer_list *t)
  38. {
  39. struct net_bridge_port *p = timer_container_of(p, t,
  40. message_age_timer);
  41. struct net_bridge *br = p->br;
  42. const bridge_id *id = &p->designated_bridge;
  43. int was_root;
  44. if (p->state == BR_STATE_DISABLED)
  45. return;
  46. br_info(br, "port %u(%s) neighbor %.2x%.2x.%pM lost\n",
  47. (unsigned int) p->port_no, p->dev->name,
  48. id->prio[0], id->prio[1], &id->addr);
  49. /*
  50. * According to the spec, the message age timer cannot be
  51. * running when we are the root bridge. So.. this was_root
  52. * check is redundant. I'm leaving it in for now, though.
  53. */
  54. spin_lock(&br->lock);
  55. if (p->state == BR_STATE_DISABLED)
  56. goto unlock;
  57. was_root = br_is_root_bridge(br);
  58. br_become_designated_port(p);
  59. br_configuration_update(br);
  60. br_port_state_selection(br);
  61. if (br_is_root_bridge(br) && !was_root)
  62. br_become_root_bridge(br);
  63. unlock:
  64. spin_unlock(&br->lock);
  65. }
  66. static void br_forward_delay_timer_expired(struct timer_list *t)
  67. {
  68. struct net_bridge_port *p = timer_container_of(p, t,
  69. forward_delay_timer);
  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. rcu_read_lock();
  85. br_ifinfo_notify(RTM_NEWLINK, NULL, p);
  86. rcu_read_unlock();
  87. spin_unlock(&br->lock);
  88. }
  89. static void br_tcn_timer_expired(struct timer_list *t)
  90. {
  91. struct net_bridge *br = timer_container_of(br, t, tcn_timer);
  92. br_debug(br, "tcn timer expired\n");
  93. spin_lock(&br->lock);
  94. if (!br_is_root_bridge(br) && (br->dev->flags & IFF_UP)) {
  95. br_transmit_tcn(br);
  96. mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time);
  97. }
  98. spin_unlock(&br->lock);
  99. }
  100. static void br_topology_change_timer_expired(struct timer_list *t)
  101. {
  102. struct net_bridge *br = timer_container_of(br, t,
  103. topology_change_timer);
  104. br_debug(br, "topo change timer expired\n");
  105. spin_lock(&br->lock);
  106. br->topology_change_detected = 0;
  107. __br_set_topology_change(br, 0);
  108. spin_unlock(&br->lock);
  109. }
  110. static void br_hold_timer_expired(struct timer_list *t)
  111. {
  112. struct net_bridge_port *p = timer_container_of(p, t, hold_timer);
  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. timer_setup(&br->hello_timer, br_hello_timer_expired, 0);
  123. timer_setup(&br->tcn_timer, br_tcn_timer_expired, 0);
  124. timer_setup(&br->topology_change_timer,
  125. br_topology_change_timer_expired, 0);
  126. }
  127. void br_stp_port_timer_init(struct net_bridge_port *p)
  128. {
  129. timer_setup(&p->message_age_timer, br_message_age_timer_expired, 0);
  130. timer_setup(&p->forward_delay_timer, br_forward_delay_timer_expired, 0);
  131. timer_setup(&p->hold_timer, br_hold_timer_expired, 0);
  132. }
  133. /* Report ticks left (in USER_HZ) used for API */
  134. unsigned long br_timer_value(const struct timer_list *timer)
  135. {
  136. return timer_pending(timer)
  137. ? jiffies_delta_to_clock_t(timer->expires - jiffies) : 0;
  138. }