Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Spanning tree protocol; timer-related code |
| 3 | * Linux ethernet bridge |
| 4 | * |
| 5 | * Authors: |
| 6 | * Lennert Buytenhek <buytenh@gnu.org> |
| 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 14 | #include <linux/kernel.h> |
| 15 | #include <linux/times.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | |
| 17 | #include "br_private.h" |
| 18 | #include "br_private_stp.h" |
| 19 | |
| 20 | /* called under bridge lock */ |
| 21 | static int br_is_designated_for_some_port(const struct net_bridge *br) |
| 22 | { |
| 23 | struct net_bridge_port *p; |
| 24 | |
| 25 | list_for_each_entry(p, &br->port_list, list) { |
| 26 | if (p->state != BR_STATE_DISABLED && |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 27 | !memcmp(&p->designated_bridge, &br->bridge_id, 8)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | return 1; |
| 29 | } |
| 30 | |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | static void br_hello_timer_expired(unsigned long arg) |
| 35 | { |
| 36 | struct net_bridge *br = (struct net_bridge *)arg; |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 37 | |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 38 | br_debug(br, "hello timer expired\n"); |
Stephen Hemminger | e3efe08 | 2006-03-20 22:56:25 -0800 | [diff] [blame] | 39 | spin_lock(&br->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | if (br->dev->flags & IFF_UP) { |
| 41 | br_config_bpdu_generation(br); |
| 42 | |
Nikolay Aleksandrov | 76b91c3 | 2015-07-23 11:01:05 -0700 | [diff] [blame] | 43 | if (br->stp_enabled != BR_USER_STP) |
| 44 | mod_timer(&br->hello_timer, |
| 45 | round_jiffies(jiffies + br->hello_time)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | } |
Stephen Hemminger | e3efe08 | 2006-03-20 22:56:25 -0800 | [diff] [blame] | 47 | spin_unlock(&br->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | static void br_message_age_timer_expired(unsigned long arg) |
| 51 | { |
| 52 | struct net_bridge_port *p = (struct net_bridge_port *) arg; |
| 53 | struct net_bridge *br = p->br; |
| 54 | const bridge_id *id = &p->designated_bridge; |
| 55 | int was_root; |
| 56 | |
| 57 | if (p->state == BR_STATE_DISABLED) |
| 58 | return; |
| 59 | |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 60 | br_info(br, "port %u(%s) neighbor %.2x%.2x.%pM lost\n", |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 61 | (unsigned int) p->port_no, p->dev->name, |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 62 | id->prio[0], id->prio[1], &id->addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
| 64 | /* |
| 65 | * According to the spec, the message age timer cannot be |
| 66 | * running when we are the root bridge. So.. this was_root |
| 67 | * check is redundant. I'm leaving it in for now, though. |
| 68 | */ |
Stephen Hemminger | e3efe08 | 2006-03-20 22:56:25 -0800 | [diff] [blame] | 69 | spin_lock(&br->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | if (p->state == BR_STATE_DISABLED) |
| 71 | goto unlock; |
| 72 | was_root = br_is_root_bridge(br); |
| 73 | |
| 74 | br_become_designated_port(p); |
| 75 | br_configuration_update(br); |
| 76 | br_port_state_selection(br); |
| 77 | if (br_is_root_bridge(br) && !was_root) |
| 78 | br_become_root_bridge(br); |
| 79 | unlock: |
Stephen Hemminger | e3efe08 | 2006-03-20 22:56:25 -0800 | [diff] [blame] | 80 | spin_unlock(&br->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | static void br_forward_delay_timer_expired(unsigned long arg) |
| 84 | { |
| 85 | struct net_bridge_port *p = (struct net_bridge_port *) arg; |
| 86 | struct net_bridge *br = p->br; |
| 87 | |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 88 | br_debug(br, "port %u(%s) forward delay timer\n", |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 89 | (unsigned int) p->port_no, p->dev->name); |
Stephen Hemminger | e3efe08 | 2006-03-20 22:56:25 -0800 | [diff] [blame] | 90 | spin_lock(&br->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | if (p->state == BR_STATE_LISTENING) { |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 92 | br_set_state(p, BR_STATE_LEARNING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | mod_timer(&p->forward_delay_timer, |
| 94 | jiffies + br->forward_delay); |
| 95 | } else if (p->state == BR_STATE_LEARNING) { |
Florian Fainelli | 775dd69 | 2014-09-30 16:13:19 -0700 | [diff] [blame] | 96 | br_set_state(p, BR_STATE_FORWARDING); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | if (br_is_designated_for_some_port(br)) |
| 98 | br_topology_change_detection(br); |
stephen hemminger | 1faa435 | 2011-03-07 08:34:06 +0000 | [diff] [blame] | 99 | netif_carrier_on(br->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
Eric Dumazet | 93a33a5 | 2015-05-21 13:28:29 -0700 | [diff] [blame] | 101 | rcu_read_lock(); |
stephen hemminger | 4ecb961 | 2011-07-22 07:47:09 +0000 | [diff] [blame] | 102 | br_ifinfo_notify(RTM_NEWLINK, p); |
Eric Dumazet | 93a33a5 | 2015-05-21 13:28:29 -0700 | [diff] [blame] | 103 | rcu_read_unlock(); |
Stephen Hemminger | e3efe08 | 2006-03-20 22:56:25 -0800 | [diff] [blame] | 104 | spin_unlock(&br->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | static void br_tcn_timer_expired(unsigned long arg) |
| 108 | { |
| 109 | struct net_bridge *br = (struct net_bridge *) arg; |
| 110 | |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 111 | br_debug(br, "tcn timer expired\n"); |
Stephen Hemminger | e3efe08 | 2006-03-20 22:56:25 -0800 | [diff] [blame] | 112 | spin_lock(&br->lock); |
stephen hemminger | 83401eb | 2013-05-02 14:23:28 +0000 | [diff] [blame] | 113 | if (!br_is_root_bridge(br) && (br->dev->flags & IFF_UP)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | br_transmit_tcn(br); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 115 | |
tanxiaojun | 31a5b83 | 2013-12-19 13:28:12 +0800 | [diff] [blame] | 116 | mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | } |
Stephen Hemminger | e3efe08 | 2006-03-20 22:56:25 -0800 | [diff] [blame] | 118 | spin_unlock(&br->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void br_topology_change_timer_expired(unsigned long arg) |
| 122 | { |
| 123 | struct net_bridge *br = (struct net_bridge *) arg; |
| 124 | |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 125 | br_debug(br, "topo change timer expired\n"); |
Stephen Hemminger | e3efe08 | 2006-03-20 22:56:25 -0800 | [diff] [blame] | 126 | spin_lock(&br->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 127 | br->topology_change_detected = 0; |
Vivien Didelot | 8384b5f | 2016-12-10 13:44:28 -0500 | [diff] [blame] | 128 | __br_set_topology_change(br, 0); |
Stephen Hemminger | e3efe08 | 2006-03-20 22:56:25 -0800 | [diff] [blame] | 129 | spin_unlock(&br->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | static void br_hold_timer_expired(unsigned long arg) |
| 133 | { |
| 134 | struct net_bridge_port *p = (struct net_bridge_port *) arg; |
| 135 | |
stephen hemminger | 28a16c9 | 2010-05-10 09:31:09 +0000 | [diff] [blame] | 136 | br_debug(p->br, "port %u(%s) hold timer expired\n", |
Eric Dumazet | 95c9617 | 2012-04-15 05:58:06 +0000 | [diff] [blame] | 137 | (unsigned int) p->port_no, p->dev->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | |
Stephen Hemminger | e3efe08 | 2006-03-20 22:56:25 -0800 | [diff] [blame] | 139 | spin_lock(&p->br->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | if (p->config_pending) |
| 141 | br_transmit_config(p); |
Stephen Hemminger | e3efe08 | 2006-03-20 22:56:25 -0800 | [diff] [blame] | 142 | spin_unlock(&p->br->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | void br_stp_timer_init(struct net_bridge *br) |
| 146 | { |
Stephen Hemminger | a95fcac | 2006-03-20 22:56:38 -0800 | [diff] [blame] | 147 | setup_timer(&br->hello_timer, br_hello_timer_expired, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 148 | (unsigned long) br); |
| 149 | |
Stephen Hemminger | a95fcac | 2006-03-20 22:56:38 -0800 | [diff] [blame] | 150 | setup_timer(&br->tcn_timer, br_tcn_timer_expired, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | (unsigned long) br); |
| 152 | |
Stephen Hemminger | a95fcac | 2006-03-20 22:56:38 -0800 | [diff] [blame] | 153 | setup_timer(&br->topology_change_timer, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | br_topology_change_timer_expired, |
| 155 | (unsigned long) br); |
| 156 | |
Stephen Hemminger | a95fcac | 2006-03-20 22:56:38 -0800 | [diff] [blame] | 157 | setup_timer(&br->gc_timer, br_fdb_cleanup, (unsigned long) br); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void br_stp_port_timer_init(struct net_bridge_port *p) |
| 161 | { |
Stephen Hemminger | a95fcac | 2006-03-20 22:56:38 -0800 | [diff] [blame] | 162 | setup_timer(&p->message_age_timer, br_message_age_timer_expired, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | (unsigned long) p); |
| 164 | |
Stephen Hemminger | a95fcac | 2006-03-20 22:56:38 -0800 | [diff] [blame] | 165 | setup_timer(&p->forward_delay_timer, br_forward_delay_timer_expired, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | (unsigned long) p); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 167 | |
Stephen Hemminger | a95fcac | 2006-03-20 22:56:38 -0800 | [diff] [blame] | 168 | setup_timer(&p->hold_timer, br_hold_timer_expired, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | (unsigned long) p); |
YOSHIFUJI Hideaki | 9d6f229 | 2007-02-09 23:24:35 +0900 | [diff] [blame] | 170 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
| 172 | /* Report ticks left (in USER_HZ) used for API */ |
| 173 | unsigned long br_timer_value(const struct timer_list *timer) |
| 174 | { |
| 175 | return timer_pending(timer) |
Eric Dumazet | a399a80 | 2012-08-08 21:13:53 +0000 | [diff] [blame] | 176 | ? jiffies_delta_to_clock_t(timer->expires - jiffies) : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | } |