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 *const 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 br_info(p
->br
, "port %u(%s) entering %s state\n",
35 (unsigned) p
->port_no
, p
->dev
->name
,
36 br_port_state_names
[p
->state
]);
39 /* called under bridge lock */
40 struct net_bridge_port
*br_get_port(struct net_bridge
*br
, u16 port_no
)
42 struct net_bridge_port
*p
;
44 list_for_each_entry_rcu(p
, &br
->port_list
, list
) {
45 if (p
->port_no
== port_no
)
52 /* called under bridge lock */
53 static int br_should_become_root_port(const struct net_bridge_port
*p
,
56 struct net_bridge
*br
;
57 struct net_bridge_port
*rp
;
61 if (p
->state
== BR_STATE_DISABLED
||
62 br_is_designated_port(p
))
65 if (memcmp(&br
->bridge_id
, &p
->designated_root
, 8) <= 0)
71 rp
= br_get_port(br
, root_port
);
73 t
= memcmp(&p
->designated_root
, &rp
->designated_root
, 8);
79 if (p
->designated_cost
+ p
->path_cost
<
80 rp
->designated_cost
+ rp
->path_cost
)
82 else if (p
->designated_cost
+ p
->path_cost
>
83 rp
->designated_cost
+ rp
->path_cost
)
86 t
= memcmp(&p
->designated_bridge
, &rp
->designated_bridge
, 8);
92 if (p
->designated_port
< rp
->designated_port
)
94 else if (p
->designated_port
> rp
->designated_port
)
97 if (p
->port_id
< rp
->port_id
)
103 /* called under bridge lock */
104 static void br_root_selection(struct net_bridge
*br
)
106 struct net_bridge_port
*p
;
109 list_for_each_entry(p
, &br
->port_list
, list
) {
110 if (br_should_become_root_port(p
, root_port
))
111 root_port
= p
->port_no
;
115 br
->root_port
= root_port
;
118 br
->designated_root
= br
->bridge_id
;
119 br
->root_path_cost
= 0;
121 p
= br_get_port(br
, root_port
);
122 br
->designated_root
= p
->designated_root
;
123 br
->root_path_cost
= p
->designated_cost
+ p
->path_cost
;
127 /* called under bridge lock */
128 void br_become_root_bridge(struct net_bridge
*br
)
130 br
->max_age
= br
->bridge_max_age
;
131 br
->hello_time
= br
->bridge_hello_time
;
132 br
->forward_delay
= br
->bridge_forward_delay
;
133 br_topology_change_detection(br
);
134 del_timer(&br
->tcn_timer
);
136 if (br
->dev
->flags
& IFF_UP
) {
137 br_config_bpdu_generation(br
);
138 mod_timer(&br
->hello_timer
, jiffies
+ br
->hello_time
);
142 /* called under bridge lock */
143 void br_transmit_config(struct net_bridge_port
*p
)
145 struct br_config_bpdu bpdu
;
146 struct net_bridge
*br
;
149 if (timer_pending(&p
->hold_timer
)) {
150 p
->config_pending
= 1;
156 bpdu
.topology_change
= br
->topology_change
;
157 bpdu
.topology_change_ack
= p
->topology_change_ack
;
158 bpdu
.root
= br
->designated_root
;
159 bpdu
.root_path_cost
= br
->root_path_cost
;
160 bpdu
.bridge_id
= br
->bridge_id
;
161 bpdu
.port_id
= p
->port_id
;
162 if (br_is_root_bridge(br
))
163 bpdu
.message_age
= 0;
165 struct net_bridge_port
*root
166 = br_get_port(br
, br
->root_port
);
167 bpdu
.message_age
= br
->max_age
168 - (root
->message_age_timer
.expires
- jiffies
)
171 bpdu
.max_age
= br
->max_age
;
172 bpdu
.hello_time
= br
->hello_time
;
173 bpdu
.forward_delay
= br
->forward_delay
;
175 if (bpdu
.message_age
< br
->max_age
) {
176 br_send_config_bpdu(p
, &bpdu
);
177 p
->topology_change_ack
= 0;
178 p
->config_pending
= 0;
179 mod_timer(&p
->hold_timer
,
180 round_jiffies(jiffies
+ BR_HOLD_TIME
));
184 /* called under bridge lock */
185 static inline void br_record_config_information(struct net_bridge_port
*p
,
186 const struct br_config_bpdu
*bpdu
)
188 p
->designated_root
= bpdu
->root
;
189 p
->designated_cost
= bpdu
->root_path_cost
;
190 p
->designated_bridge
= bpdu
->bridge_id
;
191 p
->designated_port
= bpdu
->port_id
;
193 mod_timer(&p
->message_age_timer
, jiffies
194 + (p
->br
->max_age
- bpdu
->message_age
));
197 /* called under bridge lock */
198 static inline void br_record_config_timeout_values(struct net_bridge
*br
,
199 const struct br_config_bpdu
*bpdu
)
201 br
->max_age
= bpdu
->max_age
;
202 br
->hello_time
= bpdu
->hello_time
;
203 br
->forward_delay
= bpdu
->forward_delay
;
204 br
->topology_change
= bpdu
->topology_change
;
207 /* called under bridge lock */
208 void br_transmit_tcn(struct net_bridge
*br
)
210 br_send_tcn_bpdu(br_get_port(br
, br
->root_port
));
213 /* called under bridge lock */
214 static int br_should_become_designated_port(const struct net_bridge_port
*p
)
216 struct net_bridge
*br
;
220 if (br_is_designated_port(p
))
223 if (memcmp(&p
->designated_root
, &br
->designated_root
, 8))
226 if (br
->root_path_cost
< p
->designated_cost
)
228 else if (br
->root_path_cost
> p
->designated_cost
)
231 t
= memcmp(&br
->bridge_id
, &p
->designated_bridge
, 8);
237 if (p
->port_id
< p
->designated_port
)
243 /* called under bridge lock */
244 static void br_designated_port_selection(struct net_bridge
*br
)
246 struct net_bridge_port
*p
;
248 list_for_each_entry(p
, &br
->port_list
, list
) {
249 if (p
->state
!= BR_STATE_DISABLED
&&
250 br_should_become_designated_port(p
))
251 br_become_designated_port(p
);
256 /* called under bridge lock */
257 static int br_supersedes_port_info(struct net_bridge_port
*p
, struct br_config_bpdu
*bpdu
)
261 t
= memcmp(&bpdu
->root
, &p
->designated_root
, 8);
267 if (bpdu
->root_path_cost
< p
->designated_cost
)
269 else if (bpdu
->root_path_cost
> p
->designated_cost
)
272 t
= memcmp(&bpdu
->bridge_id
, &p
->designated_bridge
, 8);
278 if (memcmp(&bpdu
->bridge_id
, &p
->br
->bridge_id
, 8))
281 if (bpdu
->port_id
<= p
->designated_port
)
287 /* called under bridge lock */
288 static inline void br_topology_change_acknowledged(struct net_bridge
*br
)
290 br
->topology_change_detected
= 0;
291 del_timer(&br
->tcn_timer
);
294 /* called under bridge lock */
295 void br_topology_change_detection(struct net_bridge
*br
)
297 int isroot
= br_is_root_bridge(br
);
299 if (br
->stp_enabled
!= BR_KERNEL_STP
)
302 br_info(br
, "topology change detected, %s\n",
303 isroot
? "propagating" : "sending tcn bpdu");
306 br
->topology_change
= 1;
307 mod_timer(&br
->topology_change_timer
, jiffies
308 + br
->bridge_forward_delay
+ br
->bridge_max_age
);
309 } else if (!br
->topology_change_detected
) {
311 mod_timer(&br
->tcn_timer
, jiffies
+ br
->bridge_hello_time
);
314 br
->topology_change_detected
= 1;
317 /* called under bridge lock */
318 void br_config_bpdu_generation(struct net_bridge
*br
)
320 struct net_bridge_port
*p
;
322 list_for_each_entry(p
, &br
->port_list
, list
) {
323 if (p
->state
!= BR_STATE_DISABLED
&&
324 br_is_designated_port(p
))
325 br_transmit_config(p
);
329 /* called under bridge lock */
330 static inline void br_reply(struct net_bridge_port
*p
)
332 br_transmit_config(p
);
335 /* called under bridge lock */
336 void br_configuration_update(struct net_bridge
*br
)
338 br_root_selection(br
);
339 br_designated_port_selection(br
);
342 /* called under bridge lock */
343 void br_become_designated_port(struct net_bridge_port
*p
)
345 struct net_bridge
*br
;
348 p
->designated_root
= br
->designated_root
;
349 p
->designated_cost
= br
->root_path_cost
;
350 p
->designated_bridge
= br
->bridge_id
;
351 p
->designated_port
= p
->port_id
;
355 /* called under bridge lock */
356 static void br_make_blocking(struct net_bridge_port
*p
)
358 if (p
->state
!= BR_STATE_DISABLED
&&
359 p
->state
!= BR_STATE_BLOCKING
) {
360 if (p
->state
== BR_STATE_FORWARDING
||
361 p
->state
== BR_STATE_LEARNING
)
362 br_topology_change_detection(p
->br
);
364 p
->state
= BR_STATE_BLOCKING
;
366 del_timer(&p
->forward_delay_timer
);
370 /* called under bridge lock */
371 static void br_make_forwarding(struct net_bridge_port
*p
)
373 struct net_bridge
*br
= p
->br
;
375 if (p
->state
!= BR_STATE_BLOCKING
)
378 if (br
->stp_enabled
== BR_NO_STP
|| br
->forward_delay
== 0) {
379 p
->state
= BR_STATE_FORWARDING
;
380 br_topology_change_detection(br
);
381 del_timer(&p
->forward_delay_timer
);
383 else if (br
->stp_enabled
== BR_KERNEL_STP
)
384 p
->state
= BR_STATE_LISTENING
;
386 p
->state
= BR_STATE_LEARNING
;
388 br_multicast_enable_port(p
);
392 if (br
->forward_delay
!= 0)
393 mod_timer(&p
->forward_delay_timer
, jiffies
+ br
->forward_delay
);
396 /* called under bridge lock */
397 void br_port_state_selection(struct net_bridge
*br
)
399 struct net_bridge_port
*p
;
400 unsigned int liveports
= 0;
402 /* Don't change port states if userspace is handling STP */
403 if (br
->stp_enabled
== BR_USER_STP
)
406 list_for_each_entry(p
, &br
->port_list
, list
) {
407 if (p
->state
== BR_STATE_DISABLED
)
410 if (p
->port_no
== br
->root_port
) {
411 p
->config_pending
= 0;
412 p
->topology_change_ack
= 0;
413 br_make_forwarding(p
);
414 } else if (br_is_designated_port(p
)) {
415 del_timer(&p
->message_age_timer
);
416 br_make_forwarding(p
);
418 p
->config_pending
= 0;
419 p
->topology_change_ack
= 0;
423 if (p
->state
== BR_STATE_FORWARDING
)
428 netif_carrier_off(br
->dev
);
430 netif_carrier_on(br
->dev
);
433 /* called under bridge lock */
434 static inline void br_topology_change_acknowledge(struct net_bridge_port
*p
)
436 p
->topology_change_ack
= 1;
437 br_transmit_config(p
);
440 /* called under bridge lock */
441 void br_received_config_bpdu(struct net_bridge_port
*p
, struct br_config_bpdu
*bpdu
)
443 struct net_bridge
*br
;
447 was_root
= br_is_root_bridge(br
);
449 if (br_supersedes_port_info(p
, bpdu
)) {
450 br_record_config_information(p
, bpdu
);
451 br_configuration_update(br
);
452 br_port_state_selection(br
);
454 if (!br_is_root_bridge(br
) && was_root
) {
455 del_timer(&br
->hello_timer
);
456 if (br
->topology_change_detected
) {
457 del_timer(&br
->topology_change_timer
);
460 mod_timer(&br
->tcn_timer
,
461 jiffies
+ br
->bridge_hello_time
);
465 if (p
->port_no
== br
->root_port
) {
466 br_record_config_timeout_values(br
, bpdu
);
467 br_config_bpdu_generation(br
);
468 if (bpdu
->topology_change_ack
)
469 br_topology_change_acknowledged(br
);
471 } else if (br_is_designated_port(p
)) {
476 /* called under bridge lock */
477 void br_received_tcn_bpdu(struct net_bridge_port
*p
)
479 if (br_is_designated_port(p
)) {
480 br_info(p
->br
, "port %u(%s) received tcn bpdu\n",
481 (unsigned) p
->port_no
, p
->dev
->name
);
483 br_topology_change_detection(p
->br
);
484 br_topology_change_acknowledge(p
);