br_stp_timer.c 4.6 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 *	Spanning tree protocol; timer-related code
 *	Linux ethernet bridge
 *
 *	Authors:
 *	Lennert Buytenhek		<buytenh@gnu.org>
 *
 *	This program is free software; you can redistribute it and/or
 *	modify it under the terms of the GNU General Public License
 *	as published by the Free Software Foundation; either version
 *	2 of the License, or (at your option) any later version.
 */

#include <linux/kernel.h>
#include <linux/times.h>

#include "br_private.h"
#include "br_private_stp.h"

/* called under bridge lock */
static int br_is_designated_for_some_port(const struct net_bridge *br)
{
	struct net_bridge_port *p;

	list_for_each_entry(p, &br->port_list, list) {
		if (p->state != BR_STATE_DISABLED &&
27
		    !memcmp(&p->designated_bridge, &br->bridge_id, 8))
L
Linus Torvalds 已提交
28 29 30 31 32 33 34 35 36
			return 1;
	}

	return 0;
}

static void br_hello_timer_expired(unsigned long arg)
{
	struct net_bridge *br = (struct net_bridge *)arg;
37

38
	br_debug(br, "hello timer expired\n");
39
	spin_lock(&br->lock);
L
Linus Torvalds 已提交
40 41 42
	if (br->dev->flags & IFF_UP) {
		br_config_bpdu_generation(br);

43
		mod_timer(&br->hello_timer, round_jiffies(jiffies + br->hello_time));
L
Linus Torvalds 已提交
44
	}
45
	spin_unlock(&br->lock);
L
Linus Torvalds 已提交
46 47 48 49 50 51 52 53 54 55 56 57
}

static void br_message_age_timer_expired(unsigned long arg)
{
	struct net_bridge_port *p = (struct net_bridge_port *) arg;
	struct net_bridge *br = p->br;
	const bridge_id *id = &p->designated_bridge;
	int was_root;

	if (p->state == BR_STATE_DISABLED)
		return;

58
	br_info(br, "port %u(%s) neighbor %.2x%.2x.%pM lost\n",
59
		(unsigned int) p->port_no, p->dev->name,
60
		id->prio[0], id->prio[1], &id->addr);
L
Linus Torvalds 已提交
61 62 63 64 65 66

	/*
	 * According to the spec, the message age timer cannot be
	 * running when we are the root bridge. So..  this was_root
	 * check is redundant. I'm leaving it in for now, though.
	 */
67
	spin_lock(&br->lock);
L
Linus Torvalds 已提交
68 69 70 71 72 73 74 75 76 77
	if (p->state == BR_STATE_DISABLED)
		goto unlock;
	was_root = br_is_root_bridge(br);

	br_become_designated_port(p);
	br_configuration_update(br);
	br_port_state_selection(br);
	if (br_is_root_bridge(br) && !was_root)
		br_become_root_bridge(br);
 unlock:
78
	spin_unlock(&br->lock);
L
Linus Torvalds 已提交
79 80 81 82 83 84 85
}

static void br_forward_delay_timer_expired(unsigned long arg)
{
	struct net_bridge_port *p = (struct net_bridge_port *) arg;
	struct net_bridge *br = p->br;

86
	br_debug(br, "port %u(%s) forward delay timer\n",
87
		 (unsigned int) p->port_no, p->dev->name);
88
	spin_lock(&br->lock);
L
Linus Torvalds 已提交
89
	if (p->state == BR_STATE_LISTENING) {
90
		br_set_state(p, BR_STATE_LEARNING);
L
Linus Torvalds 已提交
91 92 93
		mod_timer(&p->forward_delay_timer,
			  jiffies + br->forward_delay);
	} else if (p->state == BR_STATE_LEARNING) {
94
		br_set_state(p, BR_STATE_FORWARDING);
L
Linus Torvalds 已提交
95 96
		if (br_is_designated_for_some_port(br))
			br_topology_change_detection(br);
97
		netif_carrier_on(br->dev);
L
Linus Torvalds 已提交
98 99
	}
	br_log_state(p);
E
Eric Dumazet 已提交
100
	rcu_read_lock();
101
	br_ifinfo_notify(RTM_NEWLINK, p);
E
Eric Dumazet 已提交
102
	rcu_read_unlock();
103
	spin_unlock(&br->lock);
L
Linus Torvalds 已提交
104 105 106 107 108 109
}

static void br_tcn_timer_expired(unsigned long arg)
{
	struct net_bridge *br = (struct net_bridge *) arg;

110
	br_debug(br, "tcn timer expired\n");
111
	spin_lock(&br->lock);
112
	if (!br_is_root_bridge(br) && (br->dev->flags & IFF_UP)) {
L
Linus Torvalds 已提交
113
		br_transmit_tcn(br);
114

115
		mod_timer(&br->tcn_timer, jiffies + br->bridge_hello_time);
L
Linus Torvalds 已提交
116
	}
117
	spin_unlock(&br->lock);
L
Linus Torvalds 已提交
118 119 120 121 122 123
}

static void br_topology_change_timer_expired(unsigned long arg)
{
	struct net_bridge *br = (struct net_bridge *) arg;

124
	br_debug(br, "topo change timer expired\n");
125
	spin_lock(&br->lock);
L
Linus Torvalds 已提交
126 127
	br->topology_change_detected = 0;
	br->topology_change = 0;
128
	spin_unlock(&br->lock);
L
Linus Torvalds 已提交
129 130 131 132 133 134
}

static void br_hold_timer_expired(unsigned long arg)
{
	struct net_bridge_port *p = (struct net_bridge_port *) arg;

135
	br_debug(p->br, "port %u(%s) hold timer expired\n",
136
		 (unsigned int) p->port_no, p->dev->name);
L
Linus Torvalds 已提交
137

138
	spin_lock(&p->br->lock);
L
Linus Torvalds 已提交
139 140
	if (p->config_pending)
		br_transmit_config(p);
141
	spin_unlock(&p->br->lock);
L
Linus Torvalds 已提交
142 143 144 145
}

void br_stp_timer_init(struct net_bridge *br)
{
S
Stephen Hemminger 已提交
146
	setup_timer(&br->hello_timer, br_hello_timer_expired,
L
Linus Torvalds 已提交
147 148
		      (unsigned long) br);

S
Stephen Hemminger 已提交
149
	setup_timer(&br->tcn_timer, br_tcn_timer_expired,
L
Linus Torvalds 已提交
150 151
		      (unsigned long) br);

S
Stephen Hemminger 已提交
152
	setup_timer(&br->topology_change_timer,
L
Linus Torvalds 已提交
153 154 155
		      br_topology_change_timer_expired,
		      (unsigned long) br);

S
Stephen Hemminger 已提交
156
	setup_timer(&br->gc_timer, br_fdb_cleanup, (unsigned long) br);
L
Linus Torvalds 已提交
157 158 159 160
}

void br_stp_port_timer_init(struct net_bridge_port *p)
{
S
Stephen Hemminger 已提交
161
	setup_timer(&p->message_age_timer, br_message_age_timer_expired,
L
Linus Torvalds 已提交
162 163
		      (unsigned long) p);

S
Stephen Hemminger 已提交
164
	setup_timer(&p->forward_delay_timer, br_forward_delay_timer_expired,
L
Linus Torvalds 已提交
165
		      (unsigned long) p);
166

S
Stephen Hemminger 已提交
167
	setup_timer(&p->hold_timer, br_hold_timer_expired,
L
Linus Torvalds 已提交
168
		      (unsigned long) p);
169
}
L
Linus Torvalds 已提交
170 171 172 173 174

/* Report ticks left (in USER_HZ) used for API */
unsigned long br_timer_value(const struct timer_list *timer)
{
	return timer_pending(timer)
175
		? jiffies_delta_to_clock_t(timer->expires - jiffies) : 0;
L
Linus Torvalds 已提交
176
}