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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*
 *	Spanning tree protocol; timer-related code
 *	Linux ethernet bridge
 *
 *	Authors:
 *	Lennert Buytenhek		<buytenh@gnu.org>
 *
 *	$Id: br_stp_timer.c,v 1.3 2000/05/05 02:17:17 davem Exp $
 *
 *	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 <linux/smp_lock.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 &&
		    !memcmp(&p->designated_bridge, &br->bridge_id, 8)) 
			return 1;
	}

	return 0;
}

static void br_hello_timer_expired(unsigned long arg)
{
	struct net_bridge *br = (struct net_bridge *)arg;
	
	pr_debug("%s: hello timer expired\n", br->dev->name);
42
	spin_lock(&br->lock);
L
Linus Torvalds 已提交
43 44 45 46 47
	if (br->dev->flags & IFF_UP) {
		br_config_bpdu_generation(br);

		mod_timer(&br->hello_timer, jiffies + br->hello_time);
	}
48
	spin_unlock(&br->lock);
L
Linus Torvalds 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
}

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;

	
	pr_info("%s: neighbor %.2x%.2x.%.2x:%.2x:%.2x:%.2x:%.2x:%.2x lost on port %d(%s)\n",
		br->dev->name, 
		id->prio[0], id->prio[1], 
		id->addr[0], id->addr[1], id->addr[2], 
		id->addr[3], id->addr[4], id->addr[5],
		p->port_no, p->dev->name);

	/*
	 * 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.
	 */
74
	spin_lock(&br->lock);
L
Linus Torvalds 已提交
75 76 77 78 79 80 81 82 83 84
	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:
85
	spin_unlock(&br->lock);
L
Linus Torvalds 已提交
86 87 88 89 90 91 92 93 94
}

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;

	pr_debug("%s: %d(%s) forward delay timer\n",
		 br->dev->name, p->port_no, p->dev->name);
95
	spin_lock(&br->lock);
L
Linus Torvalds 已提交
96 97 98 99 100 101 102 103 104 105
	if (p->state == BR_STATE_LISTENING) {
		p->state = BR_STATE_LEARNING;
		mod_timer(&p->forward_delay_timer,
			  jiffies + br->forward_delay);
	} else if (p->state == BR_STATE_LEARNING) {
		p->state = BR_STATE_FORWARDING;
		if (br_is_designated_for_some_port(br))
			br_topology_change_detection(br);
	}
	br_log_state(p);
106
	spin_unlock(&br->lock);
L
Linus Torvalds 已提交
107 108 109 110 111 112 113
}

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

	pr_debug("%s: tcn timer expired\n", br->dev->name);
114
	spin_lock(&br->lock);
L
Linus Torvalds 已提交
115 116 117 118 119
	if (br->dev->flags & IFF_UP) {
		br_transmit_tcn(br);
	
		mod_timer(&br->tcn_timer,jiffies + br->bridge_hello_time);
	}
120
	spin_unlock(&br->lock);
L
Linus Torvalds 已提交
121 122 123 124 125 126 127
}

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

	pr_debug("%s: topo change timer expired\n", br->dev->name);
128
	spin_lock(&br->lock);
L
Linus Torvalds 已提交
129 130
	br->topology_change_detected = 0;
	br->topology_change = 0;
131
	spin_unlock(&br->lock);
L
Linus Torvalds 已提交
132 133 134 135 136 137 138 139 140
}

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

	pr_debug("%s: %d(%s) hold timer expired\n", 
		 p->br->dev->name,  p->port_no, p->dev->name);

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

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

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

S
Stephen Hemminger 已提交
155
	setup_timer(&br->topology_change_timer,
L
Linus Torvalds 已提交
156 157 158
		      br_topology_change_timer_expired,
		      (unsigned long) br);

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

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

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

/* Report ticks left (in USER_HZ) used for API */
unsigned long br_timer_value(const struct timer_list *timer)
{
	return timer_pending(timer)
		? jiffies_to_clock_t(timer->expires - jiffies) : 0;
}