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