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>
15 #include <net/switchdev.h>
17 #include "br_private.h"
18 #include "br_private_stp.h"
20 /* since time values in bpdu are in jiffies and then scaled (1/256)
21 * before sending, make sure that is at least one STP tick.
23 #define MESSAGE_AGE_INCR ((HZ / 256) + 1)
25 static const char *const br_port_state_names
[] = {
26 [BR_STATE_DISABLED
] = "disabled",
27 [BR_STATE_LISTENING
] = "listening",
28 [BR_STATE_LEARNING
] = "learning",
29 [BR_STATE_FORWARDING
] = "forwarding",
30 [BR_STATE_BLOCKING
] = "blocking",
33 void br_log_state(const struct net_bridge_port
*p
)
35 br_info(p
->br
, "port %u(%s) entered %s state\n",
36 (unsigned int) p
->port_no
, p
->dev
->name
,
37 br_port_state_names
[p
->state
]);
40 void br_set_state(struct net_bridge_port
*p
, unsigned int state
)
42 struct switchdev_attr attr
= {
43 .id
= SWITCHDEV_ATTR_PORT_STP_STATE
,
49 err
= switchdev_port_attr_set(p
->dev
, &attr
);
50 if (err
&& err
!= -EOPNOTSUPP
)
51 br_warn(p
->br
, "error setting offload STP state on port %u(%s)\n",
52 (unsigned int) p
->port_no
, p
->dev
->name
);
55 /* called under bridge lock */
56 struct net_bridge_port
*br_get_port(struct net_bridge
*br
, u16 port_no
)
58 struct net_bridge_port
*p
;
60 list_for_each_entry_rcu(p
, &br
->port_list
, list
) {
61 if (p
->port_no
== port_no
)
68 /* called under bridge lock */
69 static int br_should_become_root_port(const struct net_bridge_port
*p
,
72 struct net_bridge
*br
;
73 struct net_bridge_port
*rp
;
77 if (p
->state
== BR_STATE_DISABLED
||
78 br_is_designated_port(p
))
81 if (memcmp(&br
->bridge_id
, &p
->designated_root
, 8) <= 0)
87 rp
= br_get_port(br
, root_port
);
89 t
= memcmp(&p
->designated_root
, &rp
->designated_root
, 8);
95 if (p
->designated_cost
+ p
->path_cost
<
96 rp
->designated_cost
+ rp
->path_cost
)
98 else if (p
->designated_cost
+ p
->path_cost
>
99 rp
->designated_cost
+ rp
->path_cost
)
102 t
= memcmp(&p
->designated_bridge
, &rp
->designated_bridge
, 8);
108 if (p
->designated_port
< rp
->designated_port
)
110 else if (p
->designated_port
> rp
->designated_port
)
113 if (p
->port_id
< rp
->port_id
)
119 static void br_root_port_block(const struct net_bridge
*br
,
120 struct net_bridge_port
*p
)
123 br_notice(br
, "port %u(%s) tried to become root port (blocked)",
124 (unsigned int) p
->port_no
, p
->dev
->name
);
126 br_set_state(p
, BR_STATE_LISTENING
);
128 br_ifinfo_notify(RTM_NEWLINK
, p
);
130 if (br
->forward_delay
> 0)
131 mod_timer(&p
->forward_delay_timer
, jiffies
+ br
->forward_delay
);
134 /* called under bridge lock */
135 static void br_root_selection(struct net_bridge
*br
)
137 struct net_bridge_port
*p
;
140 list_for_each_entry(p
, &br
->port_list
, list
) {
141 if (!br_should_become_root_port(p
, root_port
))
144 if (p
->flags
& BR_ROOT_BLOCK
)
145 br_root_port_block(br
, p
);
147 root_port
= p
->port_no
;
150 br
->root_port
= root_port
;
153 br
->designated_root
= br
->bridge_id
;
154 br
->root_path_cost
= 0;
156 p
= br_get_port(br
, root_port
);
157 br
->designated_root
= p
->designated_root
;
158 br
->root_path_cost
= p
->designated_cost
+ p
->path_cost
;
162 /* called under bridge lock */
163 void br_become_root_bridge(struct net_bridge
*br
)
165 br
->max_age
= br
->bridge_max_age
;
166 br
->hello_time
= br
->bridge_hello_time
;
167 br
->forward_delay
= br
->bridge_forward_delay
;
168 br_topology_change_detection(br
);
169 del_timer(&br
->tcn_timer
);
171 if (br
->dev
->flags
& IFF_UP
) {
172 br_config_bpdu_generation(br
);
173 mod_timer(&br
->hello_timer
, jiffies
+ br
->hello_time
);
177 /* called under bridge lock */
178 void br_transmit_config(struct net_bridge_port
*p
)
180 struct br_config_bpdu bpdu
;
181 struct net_bridge
*br
;
183 if (timer_pending(&p
->hold_timer
)) {
184 p
->config_pending
= 1;
190 bpdu
.topology_change
= br
->topology_change
;
191 bpdu
.topology_change_ack
= p
->topology_change_ack
;
192 bpdu
.root
= br
->designated_root
;
193 bpdu
.root_path_cost
= br
->root_path_cost
;
194 bpdu
.bridge_id
= br
->bridge_id
;
195 bpdu
.port_id
= p
->port_id
;
196 if (br_is_root_bridge(br
))
197 bpdu
.message_age
= 0;
199 struct net_bridge_port
*root
200 = br_get_port(br
, br
->root_port
);
201 bpdu
.message_age
= (jiffies
- root
->designated_age
)
204 bpdu
.max_age
= br
->max_age
;
205 bpdu
.hello_time
= br
->hello_time
;
206 bpdu
.forward_delay
= br
->forward_delay
;
208 if (bpdu
.message_age
< br
->max_age
) {
209 br_send_config_bpdu(p
, &bpdu
);
210 p
->topology_change_ack
= 0;
211 p
->config_pending
= 0;
212 if (p
->br
->stp_enabled
== BR_KERNEL_STP
)
213 mod_timer(&p
->hold_timer
,
214 round_jiffies(jiffies
+ BR_HOLD_TIME
));
218 /* called under bridge lock */
219 static void br_record_config_information(struct net_bridge_port
*p
,
220 const struct br_config_bpdu
*bpdu
)
222 p
->designated_root
= bpdu
->root
;
223 p
->designated_cost
= bpdu
->root_path_cost
;
224 p
->designated_bridge
= bpdu
->bridge_id
;
225 p
->designated_port
= bpdu
->port_id
;
226 p
->designated_age
= jiffies
- bpdu
->message_age
;
228 mod_timer(&p
->message_age_timer
, jiffies
229 + (bpdu
->max_age
- bpdu
->message_age
));
232 /* called under bridge lock */
233 static void br_record_config_timeout_values(struct net_bridge
*br
,
234 const struct br_config_bpdu
*bpdu
)
236 br
->max_age
= bpdu
->max_age
;
237 br
->hello_time
= bpdu
->hello_time
;
238 br
->forward_delay
= bpdu
->forward_delay
;
239 br
->topology_change
= bpdu
->topology_change
;
242 /* called under bridge lock */
243 void br_transmit_tcn(struct net_bridge
*br
)
245 struct net_bridge_port
*p
;
247 p
= br_get_port(br
, br
->root_port
);
251 br_notice(br
, "root port %u not found for topology notice\n",
255 /* called under bridge lock */
256 static int br_should_become_designated_port(const struct net_bridge_port
*p
)
258 struct net_bridge
*br
;
262 if (br_is_designated_port(p
))
265 if (memcmp(&p
->designated_root
, &br
->designated_root
, 8))
268 if (br
->root_path_cost
< p
->designated_cost
)
270 else if (br
->root_path_cost
> p
->designated_cost
)
273 t
= memcmp(&br
->bridge_id
, &p
->designated_bridge
, 8);
279 if (p
->port_id
< p
->designated_port
)
285 /* called under bridge lock */
286 static void br_designated_port_selection(struct net_bridge
*br
)
288 struct net_bridge_port
*p
;
290 list_for_each_entry(p
, &br
->port_list
, list
) {
291 if (p
->state
!= BR_STATE_DISABLED
&&
292 br_should_become_designated_port(p
))
293 br_become_designated_port(p
);
298 /* called under bridge lock */
299 static int br_supersedes_port_info(const struct net_bridge_port
*p
,
300 const struct br_config_bpdu
*bpdu
)
304 t
= memcmp(&bpdu
->root
, &p
->designated_root
, 8);
310 if (bpdu
->root_path_cost
< p
->designated_cost
)
312 else if (bpdu
->root_path_cost
> p
->designated_cost
)
315 t
= memcmp(&bpdu
->bridge_id
, &p
->designated_bridge
, 8);
321 if (memcmp(&bpdu
->bridge_id
, &p
->br
->bridge_id
, 8))
324 if (bpdu
->port_id
<= p
->designated_port
)
330 /* called under bridge lock */
331 static void br_topology_change_acknowledged(struct net_bridge
*br
)
333 br
->topology_change_detected
= 0;
334 del_timer(&br
->tcn_timer
);
337 /* called under bridge lock */
338 void br_topology_change_detection(struct net_bridge
*br
)
340 int isroot
= br_is_root_bridge(br
);
342 if (br
->stp_enabled
!= BR_KERNEL_STP
)
345 br_info(br
, "topology change detected, %s\n",
346 isroot
? "propagating" : "sending tcn bpdu");
349 br
->topology_change
= 1;
350 mod_timer(&br
->topology_change_timer
, jiffies
351 + br
->bridge_forward_delay
+ br
->bridge_max_age
);
352 } else if (!br
->topology_change_detected
) {
354 mod_timer(&br
->tcn_timer
, jiffies
+ br
->bridge_hello_time
);
357 br
->topology_change_detected
= 1;
360 /* called under bridge lock */
361 void br_config_bpdu_generation(struct net_bridge
*br
)
363 struct net_bridge_port
*p
;
365 list_for_each_entry(p
, &br
->port_list
, list
) {
366 if (p
->state
!= BR_STATE_DISABLED
&&
367 br_is_designated_port(p
))
368 br_transmit_config(p
);
372 /* called under bridge lock */
373 static void br_reply(struct net_bridge_port
*p
)
375 br_transmit_config(p
);
378 /* called under bridge lock */
379 void br_configuration_update(struct net_bridge
*br
)
381 br_root_selection(br
);
382 br_designated_port_selection(br
);
385 /* called under bridge lock */
386 void br_become_designated_port(struct net_bridge_port
*p
)
388 struct net_bridge
*br
;
391 p
->designated_root
= br
->designated_root
;
392 p
->designated_cost
= br
->root_path_cost
;
393 p
->designated_bridge
= br
->bridge_id
;
394 p
->designated_port
= p
->port_id
;
398 /* called under bridge lock */
399 static void br_make_blocking(struct net_bridge_port
*p
)
401 if (p
->state
!= BR_STATE_DISABLED
&&
402 p
->state
!= BR_STATE_BLOCKING
) {
403 if (p
->state
== BR_STATE_FORWARDING
||
404 p
->state
== BR_STATE_LEARNING
)
405 br_topology_change_detection(p
->br
);
407 br_set_state(p
, BR_STATE_BLOCKING
);
409 br_ifinfo_notify(RTM_NEWLINK
, p
);
411 del_timer(&p
->forward_delay_timer
);
415 /* called under bridge lock */
416 static void br_make_forwarding(struct net_bridge_port
*p
)
418 struct net_bridge
*br
= p
->br
;
420 if (p
->state
!= BR_STATE_BLOCKING
)
423 if (br
->stp_enabled
== BR_NO_STP
|| br
->forward_delay
== 0) {
424 br_set_state(p
, BR_STATE_FORWARDING
);
425 br_topology_change_detection(br
);
426 del_timer(&p
->forward_delay_timer
);
427 } else if (br
->stp_enabled
== BR_KERNEL_STP
)
428 br_set_state(p
, BR_STATE_LISTENING
);
430 br_set_state(p
, BR_STATE_LEARNING
);
433 br_ifinfo_notify(RTM_NEWLINK
, p
);
435 if (br
->forward_delay
!= 0)
436 mod_timer(&p
->forward_delay_timer
, jiffies
+ br
->forward_delay
);
439 /* called under bridge lock */
440 void br_port_state_selection(struct net_bridge
*br
)
442 struct net_bridge_port
*p
;
443 unsigned int liveports
= 0;
445 list_for_each_entry(p
, &br
->port_list
, list
) {
446 if (p
->state
== BR_STATE_DISABLED
)
449 /* Don't change port states if userspace is handling STP */
450 if (br
->stp_enabled
!= BR_USER_STP
) {
451 if (p
->port_no
== br
->root_port
) {
452 p
->config_pending
= 0;
453 p
->topology_change_ack
= 0;
454 br_make_forwarding(p
);
455 } else if (br_is_designated_port(p
)) {
456 del_timer(&p
->message_age_timer
);
457 br_make_forwarding(p
);
459 p
->config_pending
= 0;
460 p
->topology_change_ack
= 0;
465 if (p
->state
!= BR_STATE_BLOCKING
)
466 br_multicast_enable_port(p
);
467 /* Multicast is not disabled for the port when it goes in
468 * blocking state because the timers will expire and stop by
469 * themselves without sending more queries.
471 if (p
->state
== BR_STATE_FORWARDING
)
476 netif_carrier_off(br
->dev
);
478 netif_carrier_on(br
->dev
);
481 /* called under bridge lock */
482 static void br_topology_change_acknowledge(struct net_bridge_port
*p
)
484 p
->topology_change_ack
= 1;
485 br_transmit_config(p
);
488 /* called under bridge lock */
489 void br_received_config_bpdu(struct net_bridge_port
*p
,
490 const struct br_config_bpdu
*bpdu
)
492 struct net_bridge
*br
;
496 was_root
= br_is_root_bridge(br
);
498 if (br_supersedes_port_info(p
, bpdu
)) {
499 br_record_config_information(p
, bpdu
);
500 br_configuration_update(br
);
501 br_port_state_selection(br
);
503 if (!br_is_root_bridge(br
) && was_root
) {
504 del_timer(&br
->hello_timer
);
505 if (br
->topology_change_detected
) {
506 del_timer(&br
->topology_change_timer
);
509 mod_timer(&br
->tcn_timer
,
510 jiffies
+ br
->bridge_hello_time
);
514 if (p
->port_no
== br
->root_port
) {
515 br_record_config_timeout_values(br
, bpdu
);
516 br_config_bpdu_generation(br
);
517 if (bpdu
->topology_change_ack
)
518 br_topology_change_acknowledged(br
);
520 } else if (br_is_designated_port(p
)) {
525 /* called under bridge lock */
526 void br_received_tcn_bpdu(struct net_bridge_port
*p
)
528 if (br_is_designated_port(p
)) {
529 br_info(p
->br
, "port %u(%s) received tcn bpdu\n",
530 (unsigned int) p
->port_no
, p
->dev
->name
);
532 br_topology_change_detection(p
->br
);
533 br_topology_change_acknowledge(p
);
537 /* Change bridge STP parameter */
538 int br_set_hello_time(struct net_bridge
*br
, unsigned long val
)
540 unsigned long t
= clock_t_to_jiffies(val
);
542 if (t
< BR_MIN_HELLO_TIME
|| t
> BR_MAX_HELLO_TIME
)
545 spin_lock_bh(&br
->lock
);
546 br
->bridge_hello_time
= t
;
547 if (br_is_root_bridge(br
))
548 br
->hello_time
= br
->bridge_hello_time
;
549 spin_unlock_bh(&br
->lock
);
553 int br_set_max_age(struct net_bridge
*br
, unsigned long val
)
555 unsigned long t
= clock_t_to_jiffies(val
);
557 if (t
< BR_MIN_MAX_AGE
|| t
> BR_MAX_MAX_AGE
)
560 spin_lock_bh(&br
->lock
);
561 br
->bridge_max_age
= t
;
562 if (br_is_root_bridge(br
))
563 br
->max_age
= br
->bridge_max_age
;
564 spin_unlock_bh(&br
->lock
);
569 void __br_set_forward_delay(struct net_bridge
*br
, unsigned long t
)
571 br
->bridge_forward_delay
= t
;
572 if (br_is_root_bridge(br
))
573 br
->forward_delay
= br
->bridge_forward_delay
;
576 int br_set_forward_delay(struct net_bridge
*br
, unsigned long val
)
578 unsigned long t
= clock_t_to_jiffies(val
);
581 spin_lock_bh(&br
->lock
);
582 if (br
->stp_enabled
!= BR_NO_STP
&&
583 (t
< BR_MIN_FORWARD_DELAY
|| t
> BR_MAX_FORWARD_DELAY
))
586 __br_set_forward_delay(br
, t
);
590 spin_unlock_bh(&br
->lock
);