br_stp_timer.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. 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(unsigned long arg)
  42. {
  43. struct net_bridge_port *p = (struct net_bridge_port *) arg;
  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(unsigned long arg)
  70. {
  71. struct net_bridge_port *p = (struct net_bridge_port *) arg;
  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, p);
  88. rcu_read_unlock();
  89. spin_unlock(&br->lock);
  90. }
  91. static void br_tcn_timer_expired(unsigned long arg)
  92. {
  93. struct net_bridge *br = (struct net_bridge *) arg;
  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(unsigned long arg)
  103. {
  104. struct net_bridge *br = (struct net_bridge *) arg;
  105. br_debug(br, "topo change timer expired\n");
  106. spin_lock(&br->lock);
  107. br->topology_change_detected = 0;
  108. br->topology_change = 0;
  109. spin_unlock(&br->lock);
  110. }
  111. static void br_hold_timer_expired(unsigned long arg)
  112. {
  113. struct net_bridge_port *p = (struct net_bridge_port *) arg;
  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. setup_timer(&br->hello_timer, br_hello_timer_expired,
  124. (unsigned long) br);
  125. setup_timer(&br->tcn_timer, br_tcn_timer_expired,
  126. (unsigned long) br);
  127. setup_timer(&br->topology_change_timer,
  128. br_topology_change_timer_expired,
  129. (unsigned long) br);
  130. setup_timer(&br->gc_timer, br_fdb_cleanup, (unsigned long) br);
  131. }
  132. void br_stp_port_timer_init(struct net_bridge_port *p)
  133. {
  134. setup_timer(&p->message_age_timer, br_message_age_timer_expired,
  135. (unsigned long) p);
  136. setup_timer(&p->forward_delay_timer, br_forward_delay_timer_expired,
  137. (unsigned long) p);
  138. setup_timer(&p->hold_timer, br_hold_timer_expired,
  139. (unsigned long) p);
  140. }
  141. /* Report ticks left (in USER_HZ) used for API */
  142. unsigned long br_timer_value(const struct timer_list *timer)
  143. {
  144. return timer_pending(timer)
  145. ? jiffies_delta_to_clock_t(timer->expires - jiffies) : 0;
  146. }