blob: e7739de5f0e1092037d878d989dcc5c61c372b9d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Spanning tree protocol; timer-related code
3 * Linux ethernet bridge
4 *
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 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 Torvalds1da177e2005-04-16 15:20:36 -070016
17#include "br_private.h"
18#include "br_private_stp.h"
19
20/* called under bridge lock */
21static 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 Hideaki9d6f2292007-02-09 23:24:35 +090027 !memcmp(&p->designated_bridge, &br->bridge_id, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 return 1;
29 }
30
31 return 0;
32}
33
Allen Pais1a3deb12017-11-03 11:51:11 +053034static void br_hello_timer_expired(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070035{
Allen Pais1a3deb12017-11-03 11:51:11 +053036 struct net_bridge *br = from_timer(br, t, hello_timer);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090037
stephen hemminger28a16c92010-05-10 09:31:09 +000038 br_debug(br, "hello timer expired\n");
Stephen Hemmingere3efe082006-03-20 22:56:25 -080039 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 if (br->dev->flags & IFF_UP) {
41 br_config_bpdu_generation(br);
42
Xin Long6d18c732017-05-19 22:20:29 +080043 if (br->stp_enabled == BR_KERNEL_STP)
Nikolay Aleksandrov76b91c32015-07-23 11:01:05 -070044 mod_timer(&br->hello_timer,
45 round_jiffies(jiffies + br->hello_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 }
Stephen Hemmingere3efe082006-03-20 22:56:25 -080047 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048}
49
Allen Pais1a3deb12017-11-03 11:51:11 +053050static void br_message_age_timer_expired(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070051{
Allen Pais1a3deb12017-11-03 11:51:11 +053052 struct net_bridge_port *p = from_timer(p, t, message_age_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 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 hemminger28a16c92010-05-10 09:31:09 +000060 br_info(br, "port %u(%s) neighbor %.2x%.2x.%pM lost\n",
Eric Dumazet95c96172012-04-15 05:58:06 +000061 (unsigned int) p->port_no, p->dev->name,
stephen hemminger28a16c92010-05-10 09:31:09 +000062 id->prio[0], id->prio[1], &id->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
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 Hemmingere3efe082006-03-20 22:56:25 -080069 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 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 Hemmingere3efe082006-03-20 22:56:25 -080080 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Allen Pais1a3deb12017-11-03 11:51:11 +053083static void br_forward_delay_timer_expired(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Allen Pais1a3deb12017-11-03 11:51:11 +053085 struct net_bridge_port *p = from_timer(p, t, forward_delay_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 struct net_bridge *br = p->br;
87
stephen hemminger28a16c92010-05-10 09:31:09 +000088 br_debug(br, "port %u(%s) forward delay timer\n",
Eric Dumazet95c96172012-04-15 05:58:06 +000089 (unsigned int) p->port_no, p->dev->name);
Stephen Hemmingere3efe082006-03-20 22:56:25 -080090 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 if (p->state == BR_STATE_LISTENING) {
Florian Fainelli775dd692014-09-30 16:13:19 -070092 br_set_state(p, BR_STATE_LEARNING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 mod_timer(&p->forward_delay_timer,
94 jiffies + br->forward_delay);
95 } else if (p->state == BR_STATE_LEARNING) {
Florian Fainelli775dd692014-09-30 16:13:19 -070096 br_set_state(p, BR_STATE_FORWARDING);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 if (br_is_designated_for_some_port(br))
98 br_topology_change_detection(br);
stephen hemminger1faa4352011-03-07 08:34:06 +000099 netif_carrier_on(br->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
Eric Dumazet93a33a52015-05-21 13:28:29 -0700101 rcu_read_lock();
Nikolay Aleksandrov92899062017-11-01 12:18:13 +0200102 br_ifinfo_notify(RTM_NEWLINK, NULL, p);
Eric Dumazet93a33a52015-05-21 13:28:29 -0700103 rcu_read_unlock();
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800104 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
106
Allen Pais1a3deb12017-11-03 11:51:11 +0530107static void br_tcn_timer_expired(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Allen Pais1a3deb12017-11-03 11:51:11 +0530109 struct net_bridge *br = from_timer(br, t, tcn_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
stephen hemminger28a16c92010-05-10 09:31:09 +0000111 br_debug(br, "tcn timer expired\n");
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800112 spin_lock(&br->lock);
stephen hemminger83401eb2013-05-02 14:23:28 +0000113 if (!br_is_root_bridge(br) && (br->dev->flags & IFF_UP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 br_transmit_tcn(br);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900115
tanxiaojun31a5b832013-12-19 13:28:12 +0800116 mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 }
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800118 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Allen Pais1a3deb12017-11-03 11:51:11 +0530121static void br_topology_change_timer_expired(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122{
Allen Pais1a3deb12017-11-03 11:51:11 +0530123 struct net_bridge *br = from_timer(br, t, topology_change_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
stephen hemminger28a16c92010-05-10 09:31:09 +0000125 br_debug(br, "topo change timer expired\n");
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800126 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 br->topology_change_detected = 0;
Vivien Didelot8384b5f2016-12-10 13:44:28 -0500128 __br_set_topology_change(br, 0);
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800129 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Allen Pais1a3deb12017-11-03 11:51:11 +0530132static void br_hold_timer_expired(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Allen Pais1a3deb12017-11-03 11:51:11 +0530134 struct net_bridge_port *p = from_timer(p, t, hold_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
stephen hemminger28a16c92010-05-10 09:31:09 +0000136 br_debug(p->br, "port %u(%s) hold timer expired\n",
Eric Dumazet95c96172012-04-15 05:58:06 +0000137 (unsigned int) p->port_no, p->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800139 spin_lock(&p->br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (p->config_pending)
141 br_transmit_config(p);
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800142 spin_unlock(&p->br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145void br_stp_timer_init(struct net_bridge *br)
146{
Allen Pais1a3deb12017-11-03 11:51:11 +0530147 timer_setup(&br->hello_timer, br_hello_timer_expired, 0);
148 timer_setup(&br->tcn_timer, br_tcn_timer_expired, 0);
149 timer_setup(&br->topology_change_timer,
150 br_topology_change_timer_expired, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
153void br_stp_port_timer_init(struct net_bridge_port *p)
154{
Allen Pais1a3deb12017-11-03 11:51:11 +0530155 timer_setup(&p->message_age_timer, br_message_age_timer_expired, 0);
156 timer_setup(&p->forward_delay_timer, br_forward_delay_timer_expired, 0);
157 timer_setup(&p->hold_timer, br_hold_timer_expired, 0);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900158}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159
160/* Report ticks left (in USER_HZ) used for API */
161unsigned long br_timer_value(const struct timer_list *timer)
162{
163 return timer_pending(timer)
Eric Dumazeta399a802012-08-08 21:13:53 +0000164 ? jiffies_delta_to_clock_t(timer->expires - jiffies) : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}