blob: 7b22456023c5615642b3c1a786bc135065aeb990 [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
stephen hemminger28a16c92010-05-10 09:31:09 +000038 br_debug(br, "hello timer expired\n");
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
stephen hemminger28a16c92010-05-10 09:31:09 +000058 br_info(br, "port %u(%s) neighbor %.2x%.2x.%pM lost\n",
59 (unsigned) p->port_no, p->dev->name,
60 id->prio[0], id->prio[1], &id->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
62 /*
63 * According to the spec, the message age timer cannot be
64 * running when we are the root bridge. So.. this was_root
65 * check is redundant. I'm leaving it in for now, though.
66 */
Stephen Hemmingere3efe082006-03-20 22:56:25 -080067 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (p->state == BR_STATE_DISABLED)
69 goto unlock;
70 was_root = br_is_root_bridge(br);
71
72 br_become_designated_port(p);
73 br_configuration_update(br);
74 br_port_state_selection(br);
75 if (br_is_root_bridge(br) && !was_root)
76 br_become_root_bridge(br);
77 unlock:
Stephen Hemmingere3efe082006-03-20 22:56:25 -080078 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
81static void br_forward_delay_timer_expired(unsigned long arg)
82{
83 struct net_bridge_port *p = (struct net_bridge_port *) arg;
84 struct net_bridge *br = p->br;
85
stephen hemminger28a16c92010-05-10 09:31:09 +000086 br_debug(br, "port %u(%s) forward delay timer\n",
87 (unsigned) p->port_no, p->dev->name);
Stephen Hemmingere3efe082006-03-20 22:56:25 -080088 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (p->state == BR_STATE_LISTENING) {
90 p->state = BR_STATE_LEARNING;
91 mod_timer(&p->forward_delay_timer,
92 jiffies + br->forward_delay);
93 } else if (p->state == BR_STATE_LEARNING) {
94 p->state = BR_STATE_FORWARDING;
95 if (br_is_designated_for_some_port(br))
96 br_topology_change_detection(br);
97 }
98 br_log_state(p);
Stephen Hemmingere3efe082006-03-20 22:56:25 -080099 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102static void br_tcn_timer_expired(unsigned long arg)
103{
104 struct net_bridge *br = (struct net_bridge *) arg;
105
stephen hemminger28a16c92010-05-10 09:31:09 +0000106 br_debug(br, "tcn timer expired\n");
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800107 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (br->dev->flags & IFF_UP) {
109 br_transmit_tcn(br);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 mod_timer(&br->tcn_timer,jiffies + br->bridge_hello_time);
112 }
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800113 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
116static void br_topology_change_timer_expired(unsigned long arg)
117{
118 struct net_bridge *br = (struct net_bridge *) arg;
119
stephen hemminger28a16c92010-05-10 09:31:09 +0000120 br_debug(br, "topo change timer expired\n");
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800121 spin_lock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 br->topology_change_detected = 0;
123 br->topology_change = 0;
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800124 spin_unlock(&br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
126
127static void br_hold_timer_expired(unsigned long arg)
128{
129 struct net_bridge_port *p = (struct net_bridge_port *) arg;
130
stephen hemminger28a16c92010-05-10 09:31:09 +0000131 br_debug(p->br, "port %u(%s) hold timer expired\n",
132 (unsigned) p->port_no, p->dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800134 spin_lock(&p->br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (p->config_pending)
136 br_transmit_config(p);
Stephen Hemmingere3efe082006-03-20 22:56:25 -0800137 spin_unlock(&p->br->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140void br_stp_timer_init(struct net_bridge *br)
141{
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800142 setup_timer(&br->hello_timer, br_hello_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 (unsigned long) br);
144
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800145 setup_timer(&br->tcn_timer, br_tcn_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 (unsigned long) br);
147
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800148 setup_timer(&br->topology_change_timer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 br_topology_change_timer_expired,
150 (unsigned long) br);
151
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800152 setup_timer(&br->gc_timer, br_fdb_cleanup, (unsigned long) br);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155void br_stp_port_timer_init(struct net_bridge_port *p)
156{
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800157 setup_timer(&p->message_age_timer, br_message_age_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 (unsigned long) p);
159
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800160 setup_timer(&p->forward_delay_timer, br_forward_delay_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 (unsigned long) p);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900162
Stephen Hemmingera95fcac2006-03-20 22:56:38 -0800163 setup_timer(&p->hold_timer, br_hold_timer_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 (unsigned long) p);
YOSHIFUJI Hideaki9d6f2292007-02-09 23:24:35 +0900165}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167/* Report ticks left (in USER_HZ) used for API */
168unsigned long br_timer_value(const struct timer_list *timer)
169{
170 return timer_pending(timer)
171 ? jiffies_to_clock_t(timer->expires - jiffies) : 0;
172}