Linux 2.6.13-rc3
[linux-2.6/next.git] / net / bridge / br_stp_if.c
blob0da11ff05fa3a9a9d9c218f9a394735b137fac7f
1 /*
2 * Spanning tree protocol; interface code
3 * Linux ethernet bridge
5 * Authors:
6 * Lennert Buytenhek <buytenh@gnu.org>
8 * $Id: br_stp_if.c,v 1.4 2001/04/14 21:14:39 davem Exp $
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.
16 #include <linux/kernel.h>
17 #include <linux/smp_lock.h>
19 #include "br_private.h"
20 #include "br_private_stp.h"
23 /* Port id is composed of priority and port number.
24 * NB: least significant bits of priority are dropped to
25 * make room for more ports.
27 static inline port_id br_make_port_id(__u8 priority, __u16 port_no)
29 return ((u16)priority << BR_PORT_BITS)
30 | (port_no & ((1<<BR_PORT_BITS)-1));
33 /* called under bridge lock */
34 void br_init_port(struct net_bridge_port *p)
36 p->port_id = br_make_port_id(p->priority, p->port_no);
37 br_become_designated_port(p);
38 p->state = BR_STATE_BLOCKING;
39 p->topology_change_ack = 0;
40 p->config_pending = 0;
42 br_stp_port_timer_init(p);
45 /* called under bridge lock */
46 void br_stp_enable_bridge(struct net_bridge *br)
48 struct net_bridge_port *p;
50 spin_lock_bh(&br->lock);
51 mod_timer(&br->hello_timer, jiffies + br->hello_time);
52 mod_timer(&br->gc_timer, jiffies + HZ/10);
54 br_config_bpdu_generation(br);
56 list_for_each_entry(p, &br->port_list, list) {
57 if ((p->dev->flags & IFF_UP) && netif_carrier_ok(p->dev))
58 br_stp_enable_port(p);
61 spin_unlock_bh(&br->lock);
64 /* NO locks held */
65 void br_stp_disable_bridge(struct net_bridge *br)
67 struct net_bridge_port *p;
69 spin_lock(&br->lock);
70 list_for_each_entry(p, &br->port_list, list) {
71 if (p->state != BR_STATE_DISABLED)
72 br_stp_disable_port(p);
76 br->topology_change = 0;
77 br->topology_change_detected = 0;
78 spin_unlock(&br->lock);
80 del_timer_sync(&br->hello_timer);
81 del_timer_sync(&br->topology_change_timer);
82 del_timer_sync(&br->tcn_timer);
83 del_timer_sync(&br->gc_timer);
86 /* called under bridge lock */
87 void br_stp_enable_port(struct net_bridge_port *p)
89 br_init_port(p);
90 br_port_state_selection(p->br);
93 /* called under bridge lock */
94 void br_stp_disable_port(struct net_bridge_port *p)
96 struct net_bridge *br;
97 int wasroot;
99 br = p->br;
100 printk(KERN_INFO "%s: port %i(%s) entering %s state\n",
101 br->dev->name, p->port_no, p->dev->name, "disabled");
103 wasroot = br_is_root_bridge(br);
104 br_become_designated_port(p);
105 p->state = BR_STATE_DISABLED;
106 p->topology_change_ack = 0;
107 p->config_pending = 0;
109 del_timer(&p->message_age_timer);
110 del_timer(&p->forward_delay_timer);
111 del_timer(&p->hold_timer);
113 br_configuration_update(br);
115 br_port_state_selection(br);
117 if (br_is_root_bridge(br) && !wasroot)
118 br_become_root_bridge(br);
121 /* called under bridge lock */
122 static void br_stp_change_bridge_id(struct net_bridge *br,
123 const unsigned char *addr)
125 unsigned char oldaddr[6];
126 struct net_bridge_port *p;
127 int wasroot;
129 wasroot = br_is_root_bridge(br);
131 memcpy(oldaddr, br->bridge_id.addr, ETH_ALEN);
132 memcpy(br->bridge_id.addr, addr, ETH_ALEN);
133 memcpy(br->dev->dev_addr, addr, ETH_ALEN);
135 list_for_each_entry(p, &br->port_list, list) {
136 if (!memcmp(p->designated_bridge.addr, oldaddr, ETH_ALEN))
137 memcpy(p->designated_bridge.addr, addr, ETH_ALEN);
139 if (!memcmp(p->designated_root.addr, oldaddr, ETH_ALEN))
140 memcpy(p->designated_root.addr, addr, ETH_ALEN);
144 br_configuration_update(br);
145 br_port_state_selection(br);
146 if (br_is_root_bridge(br) && !wasroot)
147 br_become_root_bridge(br);
150 static const unsigned char br_mac_zero[6];
152 /* called under bridge lock */
153 void br_stp_recalculate_bridge_id(struct net_bridge *br)
155 const unsigned char *addr = br_mac_zero;
156 struct net_bridge_port *p;
158 list_for_each_entry(p, &br->port_list, list) {
159 if (addr == br_mac_zero ||
160 memcmp(p->dev->dev_addr, addr, ETH_ALEN) < 0)
161 addr = p->dev->dev_addr;
165 if (memcmp(br->bridge_id.addr, addr, ETH_ALEN))
166 br_stp_change_bridge_id(br, addr);
169 /* called under bridge lock */
170 void br_stp_set_bridge_priority(struct net_bridge *br, u16 newprio)
172 struct net_bridge_port *p;
173 int wasroot;
175 wasroot = br_is_root_bridge(br);
177 list_for_each_entry(p, &br->port_list, list) {
178 if (p->state != BR_STATE_DISABLED &&
179 br_is_designated_port(p)) {
180 p->designated_bridge.prio[0] = (newprio >> 8) & 0xFF;
181 p->designated_bridge.prio[1] = newprio & 0xFF;
186 br->bridge_id.prio[0] = (newprio >> 8) & 0xFF;
187 br->bridge_id.prio[1] = newprio & 0xFF;
188 br_configuration_update(br);
189 br_port_state_selection(br);
190 if (br_is_root_bridge(br) && !wasroot)
191 br_become_root_bridge(br);
194 /* called under bridge lock */
195 void br_stp_set_port_priority(struct net_bridge_port *p, u8 newprio)
197 port_id new_port_id = br_make_port_id(newprio, p->port_no);
199 if (br_is_designated_port(p))
200 p->designated_port = new_port_id;
202 p->port_id = new_port_id;
203 p->priority = newprio;
204 if (!memcmp(&p->br->bridge_id, &p->designated_bridge, 8) &&
205 p->port_id < p->designated_port) {
206 br_become_designated_port(p);
207 br_port_state_selection(p->br);
211 /* called under bridge lock */
212 void br_stp_set_path_cost(struct net_bridge_port *p, u32 path_cost)
214 p->path_cost = path_cost;
215 br_configuration_update(p->br);
216 br_port_state_selection(p->br);
219 ssize_t br_show_bridge_id(char *buf, const struct bridge_id *id)
221 return sprintf(buf, "%.2x%.2x.%.2x%.2x%.2x%.2x%.2x%.2x\n",
222 id->prio[0], id->prio[1],
223 id->addr[0], id->addr[1], id->addr[2],
224 id->addr[3], id->addr[4], id->addr[5]);