12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * Spanning tree protocol; timer-related code
41da177e4SLinus Torvalds * Linux ethernet bridge
51da177e4SLinus Torvalds *
61da177e4SLinus Torvalds * Authors:
71da177e4SLinus Torvalds * Lennert Buytenhek <buytenh@gnu.org>
81da177e4SLinus Torvalds */
91da177e4SLinus Torvalds
101da177e4SLinus Torvalds #include <linux/kernel.h>
111da177e4SLinus Torvalds #include <linux/times.h>
121da177e4SLinus Torvalds
131da177e4SLinus Torvalds #include "br_private.h"
141da177e4SLinus Torvalds #include "br_private_stp.h"
151da177e4SLinus Torvalds
161da177e4SLinus Torvalds /* called under bridge lock */
br_is_designated_for_some_port(const struct net_bridge * br)171da177e4SLinus Torvalds static int br_is_designated_for_some_port(const struct net_bridge *br)
181da177e4SLinus Torvalds {
191da177e4SLinus Torvalds struct net_bridge_port *p;
201da177e4SLinus Torvalds
211da177e4SLinus Torvalds list_for_each_entry(p, &br->port_list, list) {
221da177e4SLinus Torvalds if (p->state != BR_STATE_DISABLED &&
231da177e4SLinus Torvalds !memcmp(&p->designated_bridge, &br->bridge_id, 8))
241da177e4SLinus Torvalds return 1;
251da177e4SLinus Torvalds }
261da177e4SLinus Torvalds
271da177e4SLinus Torvalds return 0;
281da177e4SLinus Torvalds }
291da177e4SLinus Torvalds
br_hello_timer_expired(struct timer_list * t)301a3deb11SAllen Pais static void br_hello_timer_expired(struct timer_list *t)
311da177e4SLinus Torvalds {
321a3deb11SAllen Pais struct net_bridge *br = from_timer(br, t, hello_timer);
331da177e4SLinus Torvalds
3428a16c97Sstephen hemminger br_debug(br, "hello timer expired\n");
35e3efe08eSStephen Hemminger spin_lock(&br->lock);
361da177e4SLinus Torvalds if (br->dev->flags & IFF_UP) {
371da177e4SLinus Torvalds br_config_bpdu_generation(br);
381da177e4SLinus Torvalds
396d18c732SXin Long if (br->stp_enabled == BR_KERNEL_STP)
4076b91c32SNikolay Aleksandrov mod_timer(&br->hello_timer,
4176b91c32SNikolay Aleksandrov round_jiffies(jiffies + br->hello_time));
421da177e4SLinus Torvalds }
43e3efe08eSStephen Hemminger spin_unlock(&br->lock);
441da177e4SLinus Torvalds }
451da177e4SLinus Torvalds
br_message_age_timer_expired(struct timer_list * t)461a3deb11SAllen Pais static void br_message_age_timer_expired(struct timer_list *t)
471da177e4SLinus Torvalds {
481a3deb11SAllen Pais struct net_bridge_port *p = from_timer(p, t, message_age_timer);
491da177e4SLinus Torvalds struct net_bridge *br = p->br;
501da177e4SLinus Torvalds const bridge_id *id = &p->designated_bridge;
511da177e4SLinus Torvalds int was_root;
521da177e4SLinus Torvalds
531da177e4SLinus Torvalds if (p->state == BR_STATE_DISABLED)
541da177e4SLinus Torvalds return;
551da177e4SLinus Torvalds
5628a16c97Sstephen hemminger br_info(br, "port %u(%s) neighbor %.2x%.2x.%pM lost\n",
5795c96174SEric Dumazet (unsigned int) p->port_no, p->dev->name,
5828a16c97Sstephen hemminger id->prio[0], id->prio[1], &id->addr);
591da177e4SLinus Torvalds
601da177e4SLinus Torvalds /*
611da177e4SLinus Torvalds * According to the spec, the message age timer cannot be
621da177e4SLinus Torvalds * running when we are the root bridge. So.. this was_root
631da177e4SLinus Torvalds * check is redundant. I'm leaving it in for now, though.
641da177e4SLinus Torvalds */
65e3efe08eSStephen Hemminger spin_lock(&br->lock);
661da177e4SLinus Torvalds if (p->state == BR_STATE_DISABLED)
671da177e4SLinus Torvalds goto unlock;
681da177e4SLinus Torvalds was_root = br_is_root_bridge(br);
691da177e4SLinus Torvalds
701da177e4SLinus Torvalds br_become_designated_port(p);
711da177e4SLinus Torvalds br_configuration_update(br);
721da177e4SLinus Torvalds br_port_state_selection(br);
731da177e4SLinus Torvalds if (br_is_root_bridge(br) && !was_root)
741da177e4SLinus Torvalds br_become_root_bridge(br);
751da177e4SLinus Torvalds unlock:
76e3efe08eSStephen Hemminger spin_unlock(&br->lock);
771da177e4SLinus Torvalds }
781da177e4SLinus Torvalds
br_forward_delay_timer_expired(struct timer_list * t)791a3deb11SAllen Pais static void br_forward_delay_timer_expired(struct timer_list *t)
801da177e4SLinus Torvalds {
811a3deb11SAllen Pais struct net_bridge_port *p = from_timer(p, t, forward_delay_timer);
821da177e4SLinus Torvalds struct net_bridge *br = p->br;
831da177e4SLinus Torvalds
8428a16c97Sstephen hemminger br_debug(br, "port %u(%s) forward delay timer\n",
8595c96174SEric Dumazet (unsigned int) p->port_no, p->dev->name);
86e3efe08eSStephen Hemminger spin_lock(&br->lock);
871da177e4SLinus Torvalds if (p->state == BR_STATE_LISTENING) {
88775dd692SFlorian Fainelli br_set_state(p, BR_STATE_LEARNING);
891da177e4SLinus Torvalds mod_timer(&p->forward_delay_timer,
901da177e4SLinus Torvalds jiffies + br->forward_delay);
911da177e4SLinus Torvalds } else if (p->state == BR_STATE_LEARNING) {
92775dd692SFlorian Fainelli br_set_state(p, BR_STATE_FORWARDING);
931da177e4SLinus Torvalds if (br_is_designated_for_some_port(br))
941da177e4SLinus Torvalds br_topology_change_detection(br);
951faa4356Sstephen hemminger netif_carrier_on(br->dev);
961da177e4SLinus Torvalds }
9793a33a58SEric Dumazet rcu_read_lock();
9892899063SNikolay Aleksandrov br_ifinfo_notify(RTM_NEWLINK, NULL, p);
9993a33a58SEric Dumazet rcu_read_unlock();
100e3efe08eSStephen Hemminger spin_unlock(&br->lock);
1011da177e4SLinus Torvalds }
1021da177e4SLinus Torvalds
br_tcn_timer_expired(struct timer_list * t)1031a3deb11SAllen Pais static void br_tcn_timer_expired(struct timer_list *t)
1041da177e4SLinus Torvalds {
1051a3deb11SAllen Pais struct net_bridge *br = from_timer(br, t, tcn_timer);
1061da177e4SLinus Torvalds
10728a16c97Sstephen hemminger br_debug(br, "tcn timer expired\n");
108e3efe08eSStephen Hemminger spin_lock(&br->lock);
10983401eb4Sstephen hemminger if (!br_is_root_bridge(br) && (br->dev->flags & IFF_UP)) {
1101da177e4SLinus Torvalds br_transmit_tcn(br);
1111da177e4SLinus Torvalds
1121da177e4SLinus Torvalds mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time);
1131da177e4SLinus Torvalds }
114e3efe08eSStephen Hemminger spin_unlock(&br->lock);
1151da177e4SLinus Torvalds }
1161da177e4SLinus Torvalds
br_topology_change_timer_expired(struct timer_list * t)1171a3deb11SAllen Pais static void br_topology_change_timer_expired(struct timer_list *t)
1181da177e4SLinus Torvalds {
1191a3deb11SAllen Pais struct net_bridge *br = from_timer(br, t, topology_change_timer);
1201da177e4SLinus Torvalds
12128a16c97Sstephen hemminger br_debug(br, "topo change timer expired\n");
122e3efe08eSStephen Hemminger spin_lock(&br->lock);
1231da177e4SLinus Torvalds br->topology_change_detected = 0;
1248384b5f5SVivien Didelot __br_set_topology_change(br, 0);
125e3efe08eSStephen Hemminger spin_unlock(&br->lock);
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds
br_hold_timer_expired(struct timer_list * t)1281a3deb11SAllen Pais static void br_hold_timer_expired(struct timer_list *t)
1291da177e4SLinus Torvalds {
1301a3deb11SAllen Pais struct net_bridge_port *p = from_timer(p, t, hold_timer);
1311da177e4SLinus Torvalds
13228a16c97Sstephen hemminger br_debug(p->br, "port %u(%s) hold timer expired\n",
13395c96174SEric Dumazet (unsigned int) p->port_no, p->dev->name);
1341da177e4SLinus Torvalds
135e3efe08eSStephen Hemminger spin_lock(&p->br->lock);
1361da177e4SLinus Torvalds if (p->config_pending)
1371da177e4SLinus Torvalds br_transmit_config(p);
138e3efe08eSStephen Hemminger spin_unlock(&p->br->lock);
1391da177e4SLinus Torvalds }
1401da177e4SLinus Torvalds
br_stp_timer_init(struct net_bridge * br)1411da177e4SLinus Torvalds void br_stp_timer_init(struct net_bridge *br)
1421da177e4SLinus Torvalds {
1431a3deb11SAllen Pais timer_setup(&br->hello_timer, br_hello_timer_expired, 0);
1441a3deb11SAllen Pais timer_setup(&br->tcn_timer, br_tcn_timer_expired, 0);
1451a3deb11SAllen Pais timer_setup(&br->topology_change_timer,
1461a3deb11SAllen Pais br_topology_change_timer_expired, 0);
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds
br_stp_port_timer_init(struct net_bridge_port * p)1491da177e4SLinus Torvalds void br_stp_port_timer_init(struct net_bridge_port *p)
1501da177e4SLinus Torvalds {
1511a3deb11SAllen Pais timer_setup(&p->message_age_timer, br_message_age_timer_expired, 0);
1521a3deb11SAllen Pais timer_setup(&p->forward_delay_timer, br_forward_delay_timer_expired, 0);
1531a3deb11SAllen Pais timer_setup(&p->hold_timer, br_hold_timer_expired, 0);
1541da177e4SLinus Torvalds }
1551da177e4SLinus Torvalds
1561da177e4SLinus Torvalds /* Report ticks left (in USER_HZ) used for API */
br_timer_value(const struct timer_list * timer)1571da177e4SLinus Torvalds unsigned long br_timer_value(const struct timer_list *timer)
1581da177e4SLinus Torvalds {
1591da177e4SLinus Torvalds return timer_pending(timer)
160a399a805SEric Dumazet ? jiffies_delta_to_clock_t(timer->expires - jiffies) : 0;
1611da177e4SLinus Torvalds }
162