blob: 772a140bfdf0f8edc2f9692bf7d001d5dbf97954 [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
34static void br_hello_timer_expired(unsigned long arg)
35{
36 struct net_bridge *br = (struct net_bridge *)arg;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090037
Linus Torvalds1da177e2005-04-16 15:20:36 -070038 pr_debug("%s: hello timer expired\n", br->dev->name);
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
Stephen Hemminger9a834b82007-05-31 01:21:39 -070043 mod_timer(&br->hello_timer, round_jiffies(jiffies + br->hello_time));
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 }
Stephen Hemmingere3efe082006-03-20 22:56:25 -080045 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070046}
47
48static void br_message_age_timer_expired(unsigned long arg)
49{
50 struct net_bridge_port *p = (struct net_bridge_port *) arg;
51 struct net_bridge *br = p->br;
52 const bridge_id *id = &p->designated_bridge;
53 int was_root;
54
55 if (p->state == BR_STATE_DISABLED)
56 return;
57
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090058
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 pr_info("%s: neighbor %.2x%.2x.%.2x:%.2x:%.2x:%.2x:%.2x:%.2x lost on port %d(%s)\n",
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090060 br->dev->name,
61 id->prio[0], id->prio[1],
62 id->addr[0], id->addr[1], id->addr[2],
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 id->addr[3], id->addr[4], id->addr[5],
64 p->port_no, p->dev->name);
65
66 /*
67 * According to the spec, the message age timer cannot be
68 * running when we are the root bridge. So.. this was_root
69 * check is redundant. I'm leaving it in for now, though.
70 */
Stephen Hemmingere3efe082006-03-20 22:56:25 -080071 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (p->state == BR_STATE_DISABLED)
73 goto unlock;
74 was_root = br_is_root_bridge(br);
75
76 br_become_designated_port(p);
77 br_configuration_update(br);
78 br_port_state_selection(br);
79 if (br_is_root_bridge(br) && !was_root)
80 br_become_root_bridge(br);
81 unlock:
Stephen Hemmingere3efe082006-03-20 22:56:25 -080082 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85static void br_forward_delay_timer_expired(unsigned long arg)
86{
87 struct net_bridge_port *p = (struct net_bridge_port *) arg;
88 struct net_bridge *br = p->br;
89
90 pr_debug("%s: %d(%s) forward delay timer\n",
91 br->dev->name, p->port_no, p->dev->name);
Stephen Hemmingere3efe082006-03-20 22:56:25 -080092 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 if (p->state == BR_STATE_LISTENING) {
94 p->state = BR_STATE_LEARNING;
95 mod_timer(&p->forward_delay_timer,
96 jiffies + br->forward_delay);
97 } else if (p->state == BR_STATE_LEARNING) {
98 p->state = BR_STATE_FORWARDING;
99 if (br_is_designated_for_some_port(br))
100 br_topology_change_detection(br);
101 }
102 br_log_state(p);
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800103 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106static void br_tcn_timer_expired(unsigned long arg)
107{
108 struct net_bridge *br = (struct net_bridge *) arg;
109
110 pr_debug("%s: tcn timer expired\n", br->dev->name);
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800111 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 if (br->dev->flags & IFF_UP) {
113 br_transmit_tcn(br);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900114
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 mod_timer(&br->tcn_timer,jiffies + br->bridge_hello_time);
116 }
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800117 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
120static void br_topology_change_timer_expired(unsigned long arg)
121{
122 struct net_bridge *br = (struct net_bridge *) arg;
123
124 pr_debug("%s: topo change timer expired\n", br->dev->name);
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800125 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 br->topology_change_detected = 0;
127 br->topology_change = 0;
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800128 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
131static void br_hold_timer_expired(unsigned long arg)
132{
133 struct net_bridge_port *p = (struct net_bridge_port *) arg;
134
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900135 pr_debug("%s: %d(%s) hold timer expired\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 p->br->dev->name, p->port_no, p->dev->name);
137
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800138 spin_lock(&p->br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (p->config_pending)
140 br_transmit_config(p);
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800141 spin_unlock(&p->br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142}
143
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144void br_stp_timer_init(struct net_bridge *br)
145{
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800146 setup_timer(&br->hello_timer, br_hello_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 (unsigned long) br);
148
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800149 setup_timer(&br->tcn_timer, br_tcn_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 (unsigned long) br);
151
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800152 setup_timer(&br->topology_change_timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 br_topology_change_timer_expired,
154 (unsigned long) br);
155
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800156 setup_timer(&br->gc_timer, br_fdb_cleanup, (unsigned long) br);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
159void br_stp_port_timer_init(struct net_bridge_port *p)
160{
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800161 setup_timer(&p->message_age_timer, br_message_age_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 (unsigned long) p);
163
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800164 setup_timer(&p->forward_delay_timer, br_forward_delay_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 (unsigned long) p);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900166
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800167 setup_timer(&p->hold_timer, br_hold_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 (unsigned long) p);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900169}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171/* Report ticks left (in USER_HZ) used for API */
172unsigned long br_timer_value(const struct timer_list *timer)
173{
174 return timer_pending(timer)
175 ? jiffies_to_clock_t(timer->expires - jiffies) : 0;
176}