Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 LT |
2 | /* |
3 | * Spanning tree protocol; timer-related code | |
4 | * Linux ethernet bridge | |
5 | * | |
6 | * Authors: | |
7 | * Lennert Buytenhek <buytenh@gnu.org> | |
1da177e4 LT |
8 | */ |
9 | ||
10 | #include <linux/kernel.h> | |
11 | #include <linux/times.h> | |
1da177e4 LT |
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 && | |
9d6f229f | 23 | !memcmp(&p->designated_bridge, &br->bridge_id, 8)) |
1da177e4 LT |
24 | return 1; |
25 | } | |
26 | ||
27 | return 0; | |
28 | } | |
29 | ||
1a3deb11 | 30 | static void br_hello_timer_expired(struct timer_list *t) |
1da177e4 | 31 | { |
1a3deb11 | 32 | struct net_bridge *br = from_timer(br, t, hello_timer); |
9d6f229f | 33 | |
28a16c97 | 34 | br_debug(br, "hello timer expired\n"); |
e3efe08e | 35 | spin_lock(&br->lock); |
1da177e4 LT |
36 | if (br->dev->flags & IFF_UP) { |
37 | br_config_bpdu_generation(br); | |
38 | ||
6d18c732 | 39 | if (br->stp_enabled == BR_KERNEL_STP) |
76b91c32 NA |
40 | mod_timer(&br->hello_timer, |
41 | round_jiffies(jiffies + br->hello_time)); | |
1da177e4 | 42 | } |
e3efe08e | 43 | spin_unlock(&br->lock); |
1da177e4 LT |
44 | } |
45 | ||
1a3deb11 | 46 | static void br_message_age_timer_expired(struct timer_list *t) |
1da177e4 | 47 | { |
1a3deb11 | 48 | struct net_bridge_port *p = from_timer(p, t, message_age_timer); |
1da177e4 LT |
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 | ||
28a16c97 | 56 | br_info(br, "port %u(%s) neighbor %.2x%.2x.%pM lost\n", |
95c96174 | 57 | (unsigned int) p->port_no, p->dev->name, |
28a16c97 | 58 | id->prio[0], id->prio[1], &id->addr); |
1da177e4 LT |
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 | */ | |
e3efe08e | 65 | spin_lock(&br->lock); |
1da177e4 LT |
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: | |
e3efe08e | 76 | spin_unlock(&br->lock); |
1da177e4 LT |
77 | } |
78 | ||
1a3deb11 | 79 | static void br_forward_delay_timer_expired(struct timer_list *t) |
1da177e4 | 80 | { |
1a3deb11 | 81 | struct net_bridge_port *p = from_timer(p, t, forward_delay_timer); |
1da177e4 LT |
82 | struct net_bridge *br = p->br; |
83 | ||
28a16c97 | 84 | br_debug(br, "port %u(%s) forward delay timer\n", |
95c96174 | 85 | (unsigned int) p->port_no, p->dev->name); |
e3efe08e | 86 | spin_lock(&br->lock); |
1da177e4 | 87 | if (p->state == BR_STATE_LISTENING) { |
775dd692 | 88 | br_set_state(p, BR_STATE_LEARNING); |
1da177e4 LT |
89 | mod_timer(&p->forward_delay_timer, |
90 | jiffies + br->forward_delay); | |
91 | } else if (p->state == BR_STATE_LEARNING) { | |
775dd692 | 92 | br_set_state(p, BR_STATE_FORWARDING); |
1da177e4 LT |
93 | if (br_is_designated_for_some_port(br)) |
94 | br_topology_change_detection(br); | |
1faa4356 | 95 | netif_carrier_on(br->dev); |
1da177e4 | 96 | } |
93a33a58 | 97 | rcu_read_lock(); |
92899063 | 98 | br_ifinfo_notify(RTM_NEWLINK, NULL, p); |
93a33a58 | 99 | rcu_read_unlock(); |
e3efe08e | 100 | spin_unlock(&br->lock); |
1da177e4 LT |
101 | } |
102 | ||
1a3deb11 | 103 | static void br_tcn_timer_expired(struct timer_list *t) |
1da177e4 | 104 | { |
1a3deb11 | 105 | struct net_bridge *br = from_timer(br, t, tcn_timer); |
1da177e4 | 106 | |
28a16c97 | 107 | br_debug(br, "tcn timer expired\n"); |
e3efe08e | 108 | spin_lock(&br->lock); |
83401eb4 | 109 | if (!br_is_root_bridge(br) && (br->dev->flags & IFF_UP)) { |
1da177e4 | 110 | br_transmit_tcn(br); |
9d6f229f | 111 | |
31a5b837 | 112 | mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time); |
1da177e4 | 113 | } |
e3efe08e | 114 | spin_unlock(&br->lock); |
1da177e4 LT |
115 | } |
116 | ||
1a3deb11 | 117 | static void br_topology_change_timer_expired(struct timer_list *t) |
1da177e4 | 118 | { |
1a3deb11 | 119 | struct net_bridge *br = from_timer(br, t, topology_change_timer); |
1da177e4 | 120 | |
28a16c97 | 121 | br_debug(br, "topo change timer expired\n"); |
e3efe08e | 122 | spin_lock(&br->lock); |
1da177e4 | 123 | br->topology_change_detected = 0; |
8384b5f5 | 124 | __br_set_topology_change(br, 0); |
e3efe08e | 125 | spin_unlock(&br->lock); |
1da177e4 LT |
126 | } |
127 | ||
1a3deb11 | 128 | static void br_hold_timer_expired(struct timer_list *t) |
1da177e4 | 129 | { |
1a3deb11 | 130 | struct net_bridge_port *p = from_timer(p, t, hold_timer); |
1da177e4 | 131 | |
28a16c97 | 132 | br_debug(p->br, "port %u(%s) hold timer expired\n", |
95c96174 | 133 | (unsigned int) p->port_no, p->dev->name); |
1da177e4 | 134 | |
e3efe08e | 135 | spin_lock(&p->br->lock); |
1da177e4 LT |
136 | if (p->config_pending) |
137 | br_transmit_config(p); | |
e3efe08e | 138 | spin_unlock(&p->br->lock); |
1da177e4 LT |
139 | } |
140 | ||
1da177e4 LT |
141 | void br_stp_timer_init(struct net_bridge *br) |
142 | { | |
1a3deb11 AP |
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); | |
1da177e4 LT |
147 | } |
148 | ||
149 | void br_stp_port_timer_init(struct net_bridge_port *p) | |
150 | { | |
1a3deb11 AP |
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); | |
9d6f229f | 154 | } |
1da177e4 LT |
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) | |
a399a805 | 160 | ? jiffies_delta_to_clock_t(timer->expires - jiffies) : 0; |
1da177e4 | 161 | } |