blob: 77f5255e691569a48766cce3ce109a4aec17c0df [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018
19#include "br_private.h"
20#include "br_private_stp.h"
21
22/* called under bridge lock */
23static int br_is_designated_for_some_port(const struct net_bridge *br)
24{
25 struct net_bridge_port *p;
26
27 list_for_each_entry(p, &br->port_list, list) {
28 if (p->state != BR_STATE_DISABLED &&
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090029 !memcmp(&p->designated_bridge, &br->bridge_id, 8))
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 return 1;
31 }
32
33 return 0;
34}
35
36static void br_hello_timer_expired(unsigned long arg)
37{
38 struct net_bridge *br = (struct net_bridge *)arg;
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090039
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 pr_debug("%s: hello timer expired\n", br->dev->name);
Stephen Hemmingere3efe082006-03-20 22:56:25 -080041 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 if (br->dev->flags & IFF_UP) {
43 br_config_bpdu_generation(br);
44
Stephen Hemminger9a834b82007-05-31 01:21:39 -070045 mod_timer(&br->hello_timer, 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
50static void br_message_age_timer_expired(unsigned long arg)
51{
52 struct net_bridge_port *p = (struct net_bridge_port *) arg;
53 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
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +090060
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 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 +090062 br->dev->name,
63 id->prio[0], id->prio[1],
64 id->addr[0], id->addr[1], id->addr[2],
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 id->addr[3], id->addr[4], id->addr[5],
66 p->port_no, p->dev->name);
67
68 /*
69 * According to the spec, the message age timer cannot be
70 * running when we are the root bridge. So.. this was_root
71 * check is redundant. I'm leaving it in for now, though.
72 */
Stephen Hemmingere3efe082006-03-20 22:56:25 -080073 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 if (p->state == BR_STATE_DISABLED)
75 goto unlock;
76 was_root = br_is_root_bridge(br);
77
78 br_become_designated_port(p);
79 br_configuration_update(br);
80 br_port_state_selection(br);
81 if (br_is_root_bridge(br) && !was_root)
82 br_become_root_bridge(br);
83 unlock:
Stephen Hemmingere3efe082006-03-20 22:56:25 -080084 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87static void br_forward_delay_timer_expired(unsigned long arg)
88{
89 struct net_bridge_port *p = (struct net_bridge_port *) arg;
90 struct net_bridge *br = p->br;
91
92 pr_debug("%s: %d(%s) forward delay timer\n",
93 br->dev->name, p->port_no, p->dev->name);
Stephen Hemmingere3efe082006-03-20 22:56:25 -080094 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 if (p->state == BR_STATE_LISTENING) {
96 p->state = BR_STATE_LEARNING;
97 mod_timer(&p->forward_delay_timer,
98 jiffies + br->forward_delay);
99 } else if (p->state == BR_STATE_LEARNING) {
100 p->state = BR_STATE_FORWARDING;
101 if (br_is_designated_for_some_port(br))
102 br_topology_change_detection(br);
103 }
104 br_log_state(p);
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800105 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106}
107
108static void br_tcn_timer_expired(unsigned long arg)
109{
110 struct net_bridge *br = (struct net_bridge *) arg;
111
112 pr_debug("%s: tcn timer expired\n", br->dev->name);
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800113 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (br->dev->flags & IFF_UP) {
115 br_transmit_tcn(br);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 mod_timer(&br->tcn_timer,jiffies + br->bridge_hello_time);
118 }
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800119 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122static void br_topology_change_timer_expired(unsigned long arg)
123{
124 struct net_bridge *br = (struct net_bridge *) arg;
125
126 pr_debug("%s: topo change timer expired\n", br->dev->name);
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800127 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 br->topology_change_detected = 0;
129 br->topology_change = 0;
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800130 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131}
132
133static void br_hold_timer_expired(unsigned long arg)
134{
135 struct net_bridge_port *p = (struct net_bridge_port *) arg;
136
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900137 pr_debug("%s: %d(%s) hold timer expired\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 p->br->dev->name, p->port_no, p->dev->name);
139
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800140 spin_lock(&p->br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (p->config_pending)
142 br_transmit_config(p);
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800143 spin_unlock(&p->br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146void br_stp_timer_init(struct net_bridge *br)
147{
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800148 setup_timer(&br->hello_timer, br_hello_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 (unsigned long) br);
150
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800151 setup_timer(&br->tcn_timer, br_tcn_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 (unsigned long) br);
153
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800154 setup_timer(&br->topology_change_timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 br_topology_change_timer_expired,
156 (unsigned long) br);
157
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800158 setup_timer(&br->gc_timer, br_fdb_cleanup, (unsigned long) br);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161void br_stp_port_timer_init(struct net_bridge_port *p)
162{
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800163 setup_timer(&p->message_age_timer, br_message_age_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 (unsigned long) p);
165
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800166 setup_timer(&p->forward_delay_timer, br_forward_delay_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 (unsigned long) p);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900168
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800169 setup_timer(&p->hold_timer, br_hold_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 (unsigned long) p);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900171}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/* Report ticks left (in USER_HZ) used for API */
174unsigned long br_timer_value(const struct timer_list *timer)
175{
176 return timer_pending(timer)
177 ? jiffies_to_clock_t(timer->expires - jiffies) : 0;
178}