2 * Spanning tree protocol; generic parts
3 * Linux ethernet bridge
6 * Lennert Buytenhek <buytenh@gnu.org>
8 * 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.
13 #include <linux/kernel.h>
14 #include <linux/rculist.h>
16 #include "br_private.h"
17 #include "br_private_stp.h"
19 /* since time values in bpdu are in jiffies and then scaled (1/256)
20 * before sending, make sure that is at least one.
22 #define MESSAGE_AGE_INCR ((HZ < 256) ? 1 : (HZ/256))
24 static const char *br_port_state_names
[] = {
25 [BR_STATE_DISABLED
] = "disabled",
26 [BR_STATE_LISTENING
] = "listening",
27 [BR_STATE_LEARNING
] = "learning",
28 [BR_STATE_FORWARDING
] = "forwarding",
29 [BR_STATE_BLOCKING
] = "blocking",
32 void br_log_state(const struct net_bridge_port
*p
)
34 pr_info("%s: port %d(%s) entering %s state\n",
35 p
->br
->dev
->name
, p
->port_no
, p
->dev
->name
,
36 br_port_state_names
[p
->state
]);
40 /* called under bridge lock */
41 struct net_bridge_port
*br_get_port(struct net_bridge
*br
, u16 port_no
)
43 struct net_bridge_port
*p
;
45 list_for_each_entry_rcu(p
, &br
->port_list
, list
) {
46 if (p
->port_no
== port_no
)
53 /* called under bridge lock */
54 static int br_should_become_root_port(const struct net_bridge_port
*p
,
57 struct net_bridge
*br
;
58 struct net_bridge_port
*rp
;
62 if (p
->state
== BR_STATE_DISABLED
||
63 br_is_designated_port(p
))
66 if (memcmp(&br
->bridge_id
, &p
->designated_root
, 8) <= 0)
72 rp
= br_get_port(br
, root_port
);
74 t
= memcmp(&p
->designated_root
, &rp
->designated_root
, 8);
80 if (p
->designated_cost
+ p
->path_cost
<
81 rp
->designated_cost
+ rp
->path_cost
)
83 else if (p
->designated_cost
+ p
->path_cost
>
84 rp
->designated_cost
+ rp
->path_cost
)
87 t
= memcmp(&p
->designated_bridge
, &rp
->designated_bridge
, 8);
93 if (p
->designated_port
< rp
->designated_port
)
95 else if (p
->designated_port
> rp
->designated_port
)
98 if (p
->port_id
< rp
->port_id
)
104 /* called under bridge lock */
105 static void br_root_selection(struct net_bridge
*br
)
107 struct net_bridge_port
*p
;
110 list_for_each_entry(p
, &br
->port_list
, list
) {
111 if (br_should_become_root_port(p
, root_port
))
112 root_port
= p
->port_no
;
116 br
->root_port
= root_port
;
119 br
->designated_root
= br
->bridge_id
;
120 br
->root_path_cost
= 0;
122 p
= br_get_port(br
, root_port
);
123 br
->designated_root
= p
->designated_root
;
124 br
->root_path_cost
= p
->designated_cost
+ p
->path_cost
;
128 /* called under bridge lock */
129 void br_become_root_bridge(struct net_bridge
*br
)
131 br
->max_age
= br
->bridge_max_age
;
132 br
->hello_time
= br
->bridge_hello_time
;
133 br
->forward_delay
= br
->bridge_forward_delay
;
134 br_topology_change_detection(br
);
135 del_timer(&br
->tcn_timer
);
137 if (br
->dev
->flags
& IFF_UP
) {
138 br_config_bpdu_generation(br
);
139 mod_timer(&br
->hello_timer
, jiffies
+ br
->hello_time
);
143 /* called under bridge lock */
144 void br_transmit_config(struct net_bridge_port
*p
)
146 struct br_config_bpdu bpdu
;
147 struct net_bridge
*br
;
150 if (timer_pending(&p
->hold_timer
)) {
151 p
->config_pending
= 1;
157 bpdu
.topology_change
= br
->topology_change
;
158 bpdu
.topology_change_ack
= p
->topology_change_ack
;
159 bpdu
.root
= br
->designated_root
;
160 bpdu
.root_path_cost
= br
->root_path_cost
;
161 bpdu
.bridge_id
= br
->bridge_id
;
162 bpdu
.port_id
= p
->port_id
;
163 if (br_is_root_bridge(br
))
164 bpdu
.message_age
= 0;
166 struct net_bridge_port
*root
167 = br_get_port(br
, br
->root_port
);
168 bpdu
.message_age
= br
->max_age
169 - (root
->message_age_timer
.expires
- jiffies
)
172 bpdu
.max_age
= br
->max_age
;
173 bpdu
.hello_time
= br
->hello_time
;
174 bpdu
.forward_delay
= br
->forward_delay
;
176 if (bpdu
.message_age
< br
->max_age
) {
177 br_send_config_bpdu(p
, &bpdu
);
178 p
->topology_change_ack
= 0;
179 p
->config_pending
= 0;
180 mod_timer(&p
->hold_timer
,
181 round_jiffies(jiffies
+ BR_HOLD_TIME
));
185 /* called under bridge lock */
186 static inline void br_record_config_information(struct net_bridge_port
*p
,
187 const struct br_config_bpdu
*bpdu
)
189 p
->designated_root
= bpdu
->root
;
190 p
->designated_cost
= bpdu
->root_path_cost
;
191 p
->designated_bridge
= bpdu
->bridge_id
;
192 p
->designated_port
= bpdu
->port_id
;
194 mod_timer(&p
->message_age_timer
, jiffies
195 + (p
->br
->max_age
- bpdu
->message_age
));
198 /* called under bridge lock */
199 static inline void br_record_config_timeout_values(struct net_bridge
*br
,
200 const struct br_config_bpdu
*bpdu
)
202 br
->max_age
= bpdu
->max_age
;
203 br
->hello_time
= bpdu
->hello_time
;
204 br
->forward_delay
= bpdu
->forward_delay
;
205 br
->topology_change
= bpdu
->topology_change
;
208 /* called under bridge lock */
209 void br_transmit_tcn(struct net_bridge
*br
)
211 br_send_tcn_bpdu(br_get_port(br
, br
->root_port
));
214 /* called under bridge lock */
215 static int br_should_become_designated_port(const struct net_bridge_port
*p
)
217 struct net_bridge
*br
;
221 if (br_is_designated_port(p
))
224 if (memcmp(&p
->designated_root
, &br
->designated_root
, 8))
227 if (br
->root_path_cost
< p
->designated_cost
)
229 else if (br
->root_path_cost
> p
->designated_cost
)
232 t
= memcmp(&br
->bridge_id
, &p
->designated_bridge
, 8);
238 if (p
->port_id
< p
->designated_port
)
244 /* called under bridge lock */
245 static void br_designated_port_selection(struct net_bridge
*br
)
247 struct net_bridge_port
*p
;
249 list_for_each_entry(p
, &br
->port_list
, list
) {
250 if (p
->state
!= BR_STATE_DISABLED
&&
251 br_should_become_designated_port(p
))
252 br_become_designated_port(p
);
257 /* called under bridge lock */
258 static int br_supersedes_port_info(struct net_bridge_port
*p
, struct br_config_bpdu
*bpdu
)
262 t
= memcmp(&bpdu
->root
, &p
->designated_root
, 8);
268 if (bpdu
->root_path_cost
< p
->designated_cost
)
270 else if (bpdu
->root_path_cost
> p
->designated_cost
)
273 t
= memcmp(&bpdu
->bridge_id
, &p
->designated_bridge
, 8);
279 if (memcmp(&bpdu
->bridge_id
, &p
->br
->bridge_id
, 8))
282 if (bpdu
->port_id
<= p
->designated_port
)
288 /* called under bridge lock */
289 static inline void br_topology_change_acknowledged(struct net_bridge
*br
)
291 br
->topology_change_detected
= 0;
292 del_timer(&br
->tcn_timer
);
295 /* called under bridge lock */
296 void br_topology_change_detection(struct net_bridge
*br
)
298 int isroot
= br_is_root_bridge(br
);
300 if (br
->stp_enabled
!= BR_KERNEL_STP
)
303 pr_info("%s: topology change detected, %s\n", br
->dev
->name
,
304 isroot
? "propagating" : "sending tcn bpdu");
307 br
->topology_change
= 1;
308 mod_timer(&br
->topology_change_timer
, jiffies
309 + br
->bridge_forward_delay
+ br
->bridge_max_age
);
310 } else if (!br
->topology_change_detected
) {
312 mod_timer(&br
->tcn_timer
, jiffies
+ br
->bridge_hello_time
);
315 br
->topology_change_detected
= 1;
318 /* called under bridge lock */
319 void br_config_bpdu_generation(struct net_bridge
*br
)
321 struct net_bridge_port
*p
;
323 list_for_each_entry(p
, &br
->port_list
, list
) {
324 if (p
->state
!= BR_STATE_DISABLED
&&
325 br_is_designated_port(p
))
326 br_transmit_config(p
);
330 /* called under bridge lock */
331 static inline void br_reply(struct net_bridge_port
*p
)
333 br_transmit_config(p
);
336 /* called under bridge lock */
337 void br_configuration_update(struct net_bridge
*br
)
339 br_root_selection(br
);
340 br_designated_port_selection(br
);
343 /* called under bridge lock */
344 void br_become_designated_port(struct net_bridge_port
*p
)
346 struct net_bridge
*br
;
349 p
->designated_root
= br
->designated_root
;
350 p
->designated_cost
= br
->root_path_cost
;
351 p
->designated_bridge
= br
->bridge_id
;
352 p
->designated_port
= p
->port_id
;
356 /* called under bridge lock */
357 static void br_make_blocking(struct net_bridge_port
*p
)
359 if (p
->state
!= BR_STATE_DISABLED
&&
360 p
->state
!= BR_STATE_BLOCKING
) {
361 if (p
->state
== BR_STATE_FORWARDING
||
362 p
->state
== BR_STATE_LEARNING
)
363 br_topology_change_detection(p
->br
);
365 p
->state
= BR_STATE_BLOCKING
;
367 del_timer(&p
->forward_delay_timer
);
371 /* called under bridge lock */
372 static void br_make_forwarding(struct net_bridge_port
*p
)
374 struct net_bridge
*br
= p
->br
;
376 if (p
->state
!= BR_STATE_BLOCKING
)
379 if (br
->forward_delay
== 0) {
380 p
->state
= BR_STATE_FORWARDING
;
381 br_topology_change_detection(br
);
382 del_timer(&p
->forward_delay_timer
);
384 else if (p
->br
->stp_enabled
== BR_KERNEL_STP
)
385 p
->state
= BR_STATE_LISTENING
;
387 p
->state
= BR_STATE_LEARNING
;
391 if (br
->forward_delay
!= 0)
392 mod_timer(&p
->forward_delay_timer
, jiffies
+ br
->forward_delay
);
395 /* called under bridge lock */
396 void br_port_state_selection(struct net_bridge
*br
)
398 struct net_bridge_port
*p
;
400 /* Don't change port states if userspace is handling STP */
401 if (br
->stp_enabled
== BR_USER_STP
)
404 list_for_each_entry(p
, &br
->port_list
, list
) {
405 if (p
->state
!= BR_STATE_DISABLED
) {
406 if (p
->port_no
== br
->root_port
) {
407 p
->config_pending
= 0;
408 p
->topology_change_ack
= 0;
409 br_make_forwarding(p
);
410 } else if (br_is_designated_port(p
)) {
411 del_timer(&p
->message_age_timer
);
412 br_make_forwarding(p
);
414 p
->config_pending
= 0;
415 p
->topology_change_ack
= 0;
423 /* called under bridge lock */
424 static inline void br_topology_change_acknowledge(struct net_bridge_port
*p
)
426 p
->topology_change_ack
= 1;
427 br_transmit_config(p
);
430 /* called under bridge lock */
431 void br_received_config_bpdu(struct net_bridge_port
*p
, struct br_config_bpdu
*bpdu
)
433 struct net_bridge
*br
;
437 was_root
= br_is_root_bridge(br
);
439 if (br_supersedes_port_info(p
, bpdu
)) {
440 br_record_config_information(p
, bpdu
);
441 br_configuration_update(br
);
442 br_port_state_selection(br
);
444 if (!br_is_root_bridge(br
) && was_root
) {
445 del_timer(&br
->hello_timer
);
446 if (br
->topology_change_detected
) {
447 del_timer(&br
->topology_change_timer
);
450 mod_timer(&br
->tcn_timer
,
451 jiffies
+ br
->bridge_hello_time
);
455 if (p
->port_no
== br
->root_port
) {
456 br_record_config_timeout_values(br
, bpdu
);
457 br_config_bpdu_generation(br
);
458 if (bpdu
->topology_change_ack
)
459 br_topology_change_acknowledged(br
);
461 } else if (br_is_designated_port(p
)) {
466 /* called under bridge lock */
467 void br_received_tcn_bpdu(struct net_bridge_port
*p
)
469 if (br_is_designated_port(p
)) {
470 pr_info("%s: received tcn bpdu on port %i(%s)\n",
471 p
->br
->dev
->name
, p
->port_no
, p
->dev
->name
);
473 br_topology_change_detection(p
->br
);
474 br_topology_change_acknowledge(p
);