br_stp_timer.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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) 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) p->port_no, p->dev->name);
  73. spin_lock(&br->lock);
  74. if (p->state == BR_STATE_LISTENING) {
  75. p->state = BR_STATE_LEARNING;
  76. mod_timer(&p->forward_delay_timer,
  77. jiffies + br->forward_delay);
  78. } else if (p->state == BR_STATE_LEARNING) {
  79. p->state = 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. spin_unlock(&br->lock);
  86. }
  87. static void br_tcn_timer_expired(unsigned long arg)
  88. {
  89. struct net_bridge *br = (struct net_bridge *) arg;
  90. br_debug(br, "tcn timer expired\n");
  91. spin_lock(&br->lock);
  92. if (br->dev->flags & IFF_UP) {
  93. br_transmit_tcn(br);
  94. mod_timer(&br->tcn_timer,jiffies + br->bridge_hello_time);
  95. }
  96. spin_unlock(&br->lock);
  97. }
  98. static void br_topology_change_timer_expired(unsigned long arg)
  99. {
  100. struct net_bridge *br = (struct net_bridge *) arg;
  101. br_debug(br, "topo change timer expired\n");
  102. spin_lock(&br->lock);
  103. br->topology_change_detected = 0;
  104. br->topology_change = 0;
  105. spin_unlock(&br->lock);
  106. }
  107. static void br_hold_timer_expired(unsigned long arg)
  108. {
  109. struct net_bridge_port *p = (struct net_bridge_port *) arg;
  110. br_debug(p->br, "port %u(%s) hold timer expired\n",
  111. (unsigned) p->port_no, p->dev->name);
  112. spin_lock(&p->br->lock);
  113. if (p->config_pending)
  114. br_transmit_config(p);
  115. spin_unlock(&p->br->lock);
  116. }
  117. void br_stp_timer_init(struct net_bridge *br)
  118. {
  119. setup_timer(&br->hello_timer, br_hello_timer_expired,
  120. (unsigned long) br);
  121. setup_timer(&br->tcn_timer, br_tcn_timer_expired,
  122. (unsigned long) br);
  123. setup_timer(&br->topology_change_timer,
  124. br_topology_change_timer_expired,
  125. (unsigned long) br);
  126. setup_timer(&br->gc_timer, br_fdb_cleanup, (unsigned long) br);
  127. }
  128. void br_stp_port_timer_init(struct net_bridge_port *p)
  129. {
  130. setup_timer(&p->message_age_timer, br_message_age_timer_expired,
  131. (unsigned long) p);
  132. setup_timer(&p->forward_delay_timer, br_forward_delay_timer_expired,
  133. (unsigned long) p);
  134. setup_timer(&p->hold_timer, br_hold_timer_expired,
  135. (unsigned long) p);
  136. }
  137. /* Report ticks left (in USER_HZ) used for API */
  138. unsigned long br_timer_value(const struct timer_list *timer)
  139. {
  140. return timer_pending(timer)
  141. ? jiffies_to_clock_t(timer->expires - jiffies) : 0;
  142. }