blob: 030aa798fea70bd5b9ee4a6fc814529c569e460b [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 *
8 * $Id: br_stp_timer.c,v 1.3 2000/05/05 02:17:17 davem Exp $
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version
13 * 2 of the License, or (at your option) any later version.
14 */
15
16#include <linux/kernel.h>
17#include <linux/times.h>
18#include <linux/smp_lock.h>
19
20#include "br_private.h"
21#include "br_private_stp.h"
22
23/* called under bridge lock */
24static int br_is_designated_for_some_port(const struct net_bridge *br)
25{
26 struct net_bridge_port *p;
27
28 list_for_each_entry(p, &br->port_list, list) {
29 if (p->state != BR_STATE_DISABLED &&
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090030 !memcmp(&p->designated_bridge, &br->bridge_id, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 return 1;
32 }
33
34 return 0;
35}
36
37static void br_hello_timer_expired(unsigned long arg)
38{
39 struct net_bridge *br = (struct net_bridge *)arg;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090040
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 pr_debug("%s: hello timer expired\n", br->dev->name);
Stephen Hemmingere3efe082006-03-20 22:56:25 -080042 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 if (br->dev->flags & IFF_UP) {
44 br_config_bpdu_generation(br);
45
46 mod_timer(&br->hello_timer, jiffies + br->hello_time);
47 }
Stephen Hemmingere3efe082006-03-20 22:56:25 -080048 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070049}
50
51static void br_message_age_timer_expired(unsigned long arg)
52{
53 struct net_bridge_port *p = (struct net_bridge_port *) arg;
54 struct net_bridge *br = p->br;
55 const bridge_id *id = &p->designated_bridge;
56 int was_root;
57
58 if (p->state == BR_STATE_DISABLED)
59 return;
60
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 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 +090063 br->dev->name,
64 id->prio[0], id->prio[1],
65 id->addr[0], id->addr[1], id->addr[2],
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 id->addr[3], id->addr[4], id->addr[5],
67 p->port_no, p->dev->name);
68
69 /*
70 * According to the spec, the message age timer cannot be
71 * running when we are the root bridge. So.. this was_root
72 * check is redundant. I'm leaving it in for now, though.
73 */
Stephen Hemmingere3efe082006-03-20 22:56:25 -080074 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 if (p->state == BR_STATE_DISABLED)
76 goto unlock;
77 was_root = br_is_root_bridge(br);
78
79 br_become_designated_port(p);
80 br_configuration_update(br);
81 br_port_state_selection(br);
82 if (br_is_root_bridge(br) && !was_root)
83 br_become_root_bridge(br);
84 unlock:
Stephen Hemmingere3efe082006-03-20 22:56:25 -080085 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
88static void br_forward_delay_timer_expired(unsigned long arg)
89{
90 struct net_bridge_port *p = (struct net_bridge_port *) arg;
91 struct net_bridge *br = p->br;
92
93 pr_debug("%s: %d(%s) forward delay timer\n",
94 br->dev->name, p->port_no, p->dev->name);
Stephen Hemmingere3efe082006-03-20 22:56:25 -080095 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 if (p->state == BR_STATE_LISTENING) {
97 p->state = BR_STATE_LEARNING;
98 mod_timer(&p->forward_delay_timer,
99 jiffies + br->forward_delay);
100 } else if (p->state == BR_STATE_LEARNING) {
101 p->state = BR_STATE_FORWARDING;
102 if (br_is_designated_for_some_port(br))
103 br_topology_change_detection(br);
104 }
105 br_log_state(p);
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800106 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
109static void br_tcn_timer_expired(unsigned long arg)
110{
111 struct net_bridge *br = (struct net_bridge *) arg;
112
113 pr_debug("%s: tcn timer expired\n", br->dev->name);
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800114 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 if (br->dev->flags & IFF_UP) {
116 br_transmit_tcn(br);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900117
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 mod_timer(&br->tcn_timer,jiffies + br->bridge_hello_time);
119 }
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800120 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123static void br_topology_change_timer_expired(unsigned long arg)
124{
125 struct net_bridge *br = (struct net_bridge *) arg;
126
127 pr_debug("%s: topo change timer expired\n", br->dev->name);
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800128 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 br->topology_change_detected = 0;
130 br->topology_change = 0;
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800131 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134static void br_hold_timer_expired(unsigned long arg)
135{
136 struct net_bridge_port *p = (struct net_bridge_port *) arg;
137
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900138 pr_debug("%s: %d(%s) hold timer expired\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 p->br->dev->name, p->port_no, p->dev->name);
140
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800141 spin_lock(&p->br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 if (p->config_pending)
143 br_transmit_config(p);
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800144 spin_unlock(&p->br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147void br_stp_timer_init(struct net_bridge *br)
148{
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800149 setup_timer(&br->hello_timer, br_hello_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->tcn_timer, br_tcn_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 (unsigned long) br);
154
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800155 setup_timer(&br->topology_change_timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 br_topology_change_timer_expired,
157 (unsigned long) br);
158
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800159 setup_timer(&br->gc_timer, br_fdb_cleanup, (unsigned long) br);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162void br_stp_port_timer_init(struct net_bridge_port *p)
163{
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800164 setup_timer(&p->message_age_timer, br_message_age_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 (unsigned long) p);
166
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800167 setup_timer(&p->forward_delay_timer, br_forward_delay_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 (unsigned long) p);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900169
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800170 setup_timer(&p->hold_timer, br_hold_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 (unsigned long) p);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900172}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174/* Report ticks left (in USER_HZ) used for API */
175unsigned long br_timer_value(const struct timer_list *timer)
176{
177 return timer_pending(timer)
178 ? jiffies_to_clock_t(timer->expires - jiffies) : 0;
179}