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_set_state(struct net_bridge_port
*p
, unsigned int state
)
35 struct switchdev_attr attr
= {
37 .id
= SWITCHDEV_ATTR_ID_PORT_STP_STATE
,
38 .flags
= SWITCHDEV_F_DEFER
,
44 err
= switchdev_port_attr_set(p
->dev
, &attr
);
45 if (err
&& err
!= -EOPNOTSUPP
)
46 br_warn(p
->br
, "error setting offload STP state on port %u(%s)\n",
47 (unsigned int) p
->port_no
, p
->dev
->name
);
49 br_info(p
->br
, "port %u(%s) entered %s state\n",
50 (unsigned int) p
->port_no
, p
->dev
->name
,
51 br_port_state_names
[p
->state
]);
54 /* called under bridge lock */
55 struct net_bridge_port
*br_get_port(struct net_bridge
*br
, u16 port_no
)
57 struct net_bridge_port
*p
;
59 list_for_each_entry_rcu(p
, &br
->port_list
, list
) {
60 if (p
->port_no
== port_no
)
67 /* called under bridge lock */
68 static int br_should_become_root_port(const struct net_bridge_port
*p
,
71 struct net_bridge
*br
;
72 struct net_bridge_port
*rp
;
76 if (p
->state
== BR_STATE_DISABLED
||
77 br_is_designated_port(p
))
80 if (memcmp(&br
->bridge_id
, &p
->designated_root
, 8) <= 0)
86 rp
= br_get_port(br
, root_port
);
88 t
= memcmp(&p
->designated_root
, &rp
->designated_root
, 8);
94 if (p
->designated_cost
+ p
->path_cost
<
95 rp
->designated_cost
+ rp
->path_cost
)
97 else if (p
->designated_cost
+ p
->path_cost
>
98 rp
->designated_cost
+ rp
->path_cost
)
101 t
= memcmp(&p
->designated_bridge
, &rp
->designated_bridge
, 8);
107 if (p
->designated_port
< rp
->designated_port
)
109 else if (p
->designated_port
> rp
->designated_port
)
112 if (p
->port_id
< rp
->port_id
)
118 static void br_root_port_block(const struct net_bridge
*br
,
119 struct net_bridge_port
*p
)
122 br_notice(br
, "port %u(%s) tried to become root port (blocked)",
123 (unsigned int) p
->port_no
, p
->dev
->name
);
125 br_set_state(p
, BR_STATE_LISTENING
);
126 br_ifinfo_notify(RTM_NEWLINK
, NULL
, p
);
128 if (br
->forward_delay
> 0)
129 mod_timer(&p
->forward_delay_timer
, jiffies
+ br
->forward_delay
);
132 /* called under bridge lock */
133 static void br_root_selection(struct net_bridge
*br
)
135 struct net_bridge_port
*p
;
138 list_for_each_entry(p
, &br
->port_list
, list
) {
139 if (!br_should_become_root_port(p
, root_port
))
142 if (p
->flags
& BR_ROOT_BLOCK
)
143 br_root_port_block(br
, p
);
145 root_port
= p
->port_no
;
148 br
->root_port
= root_port
;
151 br
->designated_root
= br
->bridge_id
;
152 br
->root_path_cost
= 0;
154 p
= br_get_port(br
, root_port
);
155 br
->designated_root
= p
->designated_root
;
156 br
->root_path_cost
= p
->designated_cost
+ p
->path_cost
;
160 /* called under bridge lock */
161 void br_become_root_bridge(struct net_bridge
*br
)
163 br
->max_age
= br
->bridge_max_age
;
164 br
->hello_time
= br
->bridge_hello_time
;
165 br
->forward_delay
= br
->bridge_forward_delay
;
166 br_topology_change_detection(br
);
167 del_timer(&br
->tcn_timer
);
169 if (br
->dev
->flags
& IFF_UP
) {
170 br_config_bpdu_generation(br
);
171 mod_timer(&br
->hello_timer
, jiffies
+ br
->hello_time
);
175 /* called under bridge lock */
176 void br_transmit_config(struct net_bridge_port
*p
)
178 struct br_config_bpdu bpdu
;
179 struct net_bridge
*br
;
181 if (timer_pending(&p
->hold_timer
)) {
182 p
->config_pending
= 1;
188 bpdu
.topology_change
= br
->topology_change
;
189 bpdu
.topology_change_ack
= p
->topology_change_ack
;
190 bpdu
.root
= br
->designated_root
;
191 bpdu
.root_path_cost
= br
->root_path_cost
;
192 bpdu
.bridge_id
= br
->bridge_id
;
193 bpdu
.port_id
= p
->port_id
;
194 if (br_is_root_bridge(br
))
195 bpdu
.message_age
= 0;
197 struct net_bridge_port
*root
198 = br_get_port(br
, br
->root_port
);
199 bpdu
.message_age
= (jiffies
- root
->designated_age
)
202 bpdu
.max_age
= br
->max_age
;
203 bpdu
.hello_time
= br
->hello_time
;
204 bpdu
.forward_delay
= br
->forward_delay
;
206 if (bpdu
.message_age
< br
->max_age
) {
207 br_send_config_bpdu(p
, &bpdu
);
208 p
->topology_change_ack
= 0;
209 p
->config_pending
= 0;
210 if (p
->br
->stp_enabled
== BR_KERNEL_STP
)
211 mod_timer(&p
->hold_timer
,
212 round_jiffies(jiffies
+ BR_HOLD_TIME
));
216 /* called under bridge lock */
217 static void br_record_config_information(struct net_bridge_port
*p
,
218 const struct br_config_bpdu
*bpdu
)
220 p
->designated_root
= bpdu
->root
;
221 p
->designated_cost
= bpdu
->root_path_cost
;
222 p
->designated_bridge
= bpdu
->bridge_id
;
223 p
->designated_port
= bpdu
->port_id
;
224 p
->designated_age
= jiffies
- bpdu
->message_age
;
226 mod_timer(&p
->message_age_timer
, jiffies
227 + (bpdu
->max_age
- bpdu
->message_age
));
230 /* called under bridge lock */
231 static void br_record_config_timeout_values(struct net_bridge
*br
,
232 const struct br_config_bpdu
*bpdu
)
234 br
->max_age
= bpdu
->max_age
;
235 br
->hello_time
= bpdu
->hello_time
;
236 br
->forward_delay
= bpdu
->forward_delay
;
237 __br_set_topology_change(br
, bpdu
->topology_change
);
240 /* called under bridge lock */
241 void br_transmit_tcn(struct net_bridge
*br
)
243 struct net_bridge_port
*p
;
245 p
= br_get_port(br
, br
->root_port
);
249 br_notice(br
, "root port %u not found for topology notice\n",
253 /* called under bridge lock */
254 static int br_should_become_designated_port(const struct net_bridge_port
*p
)
256 struct net_bridge
*br
;
260 if (br_is_designated_port(p
))
263 if (memcmp(&p
->designated_root
, &br
->designated_root
, 8))
266 if (br
->root_path_cost
< p
->designated_cost
)
268 else if (br
->root_path_cost
> p
->designated_cost
)
271 t
= memcmp(&br
->bridge_id
, &p
->designated_bridge
, 8);
277 if (p
->port_id
< p
->designated_port
)
283 /* called under bridge lock */
284 static void br_designated_port_selection(struct net_bridge
*br
)
286 struct net_bridge_port
*p
;
288 list_for_each_entry(p
, &br
->port_list
, list
) {
289 if (p
->state
!= BR_STATE_DISABLED
&&
290 br_should_become_designated_port(p
))
291 br_become_designated_port(p
);
296 /* called under bridge lock */
297 static int br_supersedes_port_info(const struct net_bridge_port
*p
,
298 const struct br_config_bpdu
*bpdu
)
302 t
= memcmp(&bpdu
->root
, &p
->designated_root
, 8);
308 if (bpdu
->root_path_cost
< p
->designated_cost
)
310 else if (bpdu
->root_path_cost
> p
->designated_cost
)
313 t
= memcmp(&bpdu
->bridge_id
, &p
->designated_bridge
, 8);
319 if (memcmp(&bpdu
->bridge_id
, &p
->br
->bridge_id
, 8))
322 if (bpdu
->port_id
<= p
->designated_port
)
328 /* called under bridge lock */
329 static void br_topology_change_acknowledged(struct net_bridge
*br
)
331 br
->topology_change_detected
= 0;
332 del_timer(&br
->tcn_timer
);
335 /* called under bridge lock */
336 void br_topology_change_detection(struct net_bridge
*br
)
338 int isroot
= br_is_root_bridge(br
);
340 if (br
->stp_enabled
!= BR_KERNEL_STP
)
343 br_info(br
, "topology change detected, %s\n",
344 isroot
? "propagating" : "sending tcn bpdu");
347 __br_set_topology_change(br
, 1);
348 mod_timer(&br
->topology_change_timer
, jiffies
349 + br
->bridge_forward_delay
+ br
->bridge_max_age
);
350 } else if (!br
->topology_change_detected
) {
352 mod_timer(&br
->tcn_timer
, jiffies
+ br
->bridge_hello_time
);
355 br
->topology_change_detected
= 1;
358 /* called under bridge lock */
359 void br_config_bpdu_generation(struct net_bridge
*br
)
361 struct net_bridge_port
*p
;
363 list_for_each_entry(p
, &br
->port_list
, list
) {
364 if (p
->state
!= BR_STATE_DISABLED
&&
365 br_is_designated_port(p
))
366 br_transmit_config(p
);
370 /* called under bridge lock */
371 static void br_reply(struct net_bridge_port
*p
)
373 br_transmit_config(p
);
376 /* called under bridge lock */
377 void br_configuration_update(struct net_bridge
*br
)
379 br_root_selection(br
);
380 br_designated_port_selection(br
);
383 /* called under bridge lock */
384 void br_become_designated_port(struct net_bridge_port
*p
)
386 struct net_bridge
*br
;
389 p
->designated_root
= br
->designated_root
;
390 p
->designated_cost
= br
->root_path_cost
;
391 p
->designated_bridge
= br
->bridge_id
;
392 p
->designated_port
= p
->port_id
;
396 /* called under bridge lock */
397 static void br_make_blocking(struct net_bridge_port
*p
)
399 if (p
->state
!= BR_STATE_DISABLED
&&
400 p
->state
!= BR_STATE_BLOCKING
) {
401 if (p
->state
== BR_STATE_FORWARDING
||
402 p
->state
== BR_STATE_LEARNING
)
403 br_topology_change_detection(p
->br
);
405 br_set_state(p
, BR_STATE_BLOCKING
);
406 br_ifinfo_notify(RTM_NEWLINK
, NULL
, p
);
408 del_timer(&p
->forward_delay_timer
);
412 /* called under bridge lock */
413 static void br_make_forwarding(struct net_bridge_port
*p
)
415 struct net_bridge
*br
= p
->br
;
417 if (p
->state
!= BR_STATE_BLOCKING
)
420 if (br
->stp_enabled
== BR_NO_STP
|| br
->forward_delay
== 0) {
421 br_set_state(p
, BR_STATE_FORWARDING
);
422 br_topology_change_detection(br
);
423 del_timer(&p
->forward_delay_timer
);
424 } else if (br
->stp_enabled
== BR_KERNEL_STP
)
425 br_set_state(p
, BR_STATE_LISTENING
);
427 br_set_state(p
, BR_STATE_LEARNING
);
429 br_ifinfo_notify(RTM_NEWLINK
, NULL
, p
);
431 if (br
->forward_delay
!= 0)
432 mod_timer(&p
->forward_delay_timer
, jiffies
+ br
->forward_delay
);
435 /* called under bridge lock */
436 void br_port_state_selection(struct net_bridge
*br
)
438 struct net_bridge_port
*p
;
439 unsigned int liveports
= 0;
441 list_for_each_entry(p
, &br
->port_list
, list
) {
442 if (p
->state
== BR_STATE_DISABLED
)
445 /* Don't change port states if userspace is handling STP */
446 if (br
->stp_enabled
!= BR_USER_STP
) {
447 if (p
->port_no
== br
->root_port
) {
448 p
->config_pending
= 0;
449 p
->topology_change_ack
= 0;
450 br_make_forwarding(p
);
451 } else if (br_is_designated_port(p
)) {
452 del_timer(&p
->message_age_timer
);
453 br_make_forwarding(p
);
455 p
->config_pending
= 0;
456 p
->topology_change_ack
= 0;
461 if (p
->state
!= BR_STATE_BLOCKING
)
462 br_multicast_enable_port(p
);
463 /* Multicast is not disabled for the port when it goes in
464 * blocking state because the timers will expire and stop by
465 * themselves without sending more queries.
467 if (p
->state
== BR_STATE_FORWARDING
)
472 netif_carrier_off(br
->dev
);
474 netif_carrier_on(br
->dev
);
477 /* called under bridge lock */
478 static void br_topology_change_acknowledge(struct net_bridge_port
*p
)
480 p
->topology_change_ack
= 1;
481 br_transmit_config(p
);
484 /* called under bridge lock */
485 void br_received_config_bpdu(struct net_bridge_port
*p
,
486 const struct br_config_bpdu
*bpdu
)
488 struct net_bridge
*br
;
492 was_root
= br_is_root_bridge(br
);
494 if (br_supersedes_port_info(p
, bpdu
)) {
495 br_record_config_information(p
, bpdu
);
496 br_configuration_update(br
);
497 br_port_state_selection(br
);
499 if (!br_is_root_bridge(br
) && was_root
) {
500 del_timer(&br
->hello_timer
);
501 if (br
->topology_change_detected
) {
502 del_timer(&br
->topology_change_timer
);
505 mod_timer(&br
->tcn_timer
,
506 jiffies
+ br
->bridge_hello_time
);
510 if (p
->port_no
== br
->root_port
) {
511 br_record_config_timeout_values(br
, bpdu
);
512 br_config_bpdu_generation(br
);
513 if (bpdu
->topology_change_ack
)
514 br_topology_change_acknowledged(br
);
516 } else if (br_is_designated_port(p
)) {
521 /* called under bridge lock */
522 void br_received_tcn_bpdu(struct net_bridge_port
*p
)
524 if (br_is_designated_port(p
)) {
525 br_info(p
->br
, "port %u(%s) received tcn bpdu\n",
526 (unsigned int) p
->port_no
, p
->dev
->name
);
528 br_topology_change_detection(p
->br
);
529 br_topology_change_acknowledge(p
);
533 /* Change bridge STP parameter */
534 int br_set_hello_time(struct net_bridge
*br
, unsigned long val
)
536 unsigned long t
= clock_t_to_jiffies(val
);
538 if (t
< BR_MIN_HELLO_TIME
|| t
> BR_MAX_HELLO_TIME
)
541 spin_lock_bh(&br
->lock
);
542 br
->bridge_hello_time
= t
;
543 if (br_is_root_bridge(br
))
544 br
->hello_time
= br
->bridge_hello_time
;
545 spin_unlock_bh(&br
->lock
);
549 int br_set_max_age(struct net_bridge
*br
, unsigned long val
)
551 unsigned long t
= clock_t_to_jiffies(val
);
553 if (t
< BR_MIN_MAX_AGE
|| t
> BR_MAX_MAX_AGE
)
556 spin_lock_bh(&br
->lock
);
557 br
->bridge_max_age
= t
;
558 if (br_is_root_bridge(br
))
559 br
->max_age
= br
->bridge_max_age
;
560 spin_unlock_bh(&br
->lock
);
565 /* called under bridge lock */
566 int __set_ageing_time(struct net_device
*dev
, unsigned long t
)
568 struct switchdev_attr attr
= {
570 .id
= SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME
,
571 .flags
= SWITCHDEV_F_SKIP_EOPNOTSUPP
| SWITCHDEV_F_DEFER
,
572 .u
.ageing_time
= jiffies_to_clock_t(t
),
576 err
= switchdev_port_attr_set(dev
, &attr
);
577 if (err
&& err
!= -EOPNOTSUPP
)
583 /* Set time interval that dynamic forwarding entries live
584 * For pure software bridge, allow values outside the 802.1
585 * standard specification for special cases:
586 * 0 - entry never ages (all permanant)
587 * 1 - entry disappears (no persistance)
589 * Offloaded switch entries maybe more restrictive
591 int br_set_ageing_time(struct net_bridge
*br
, clock_t ageing_time
)
593 unsigned long t
= clock_t_to_jiffies(ageing_time
);
596 err
= __set_ageing_time(br
->dev
, t
);
600 spin_lock_bh(&br
->lock
);
601 br
->bridge_ageing_time
= t
;
603 spin_unlock_bh(&br
->lock
);
605 mod_delayed_work(system_long_wq
, &br
->gc_work
, 0);
610 /* called under bridge lock */
611 void __br_set_topology_change(struct net_bridge
*br
, unsigned char val
)
616 if (br
->stp_enabled
== BR_KERNEL_STP
&& br
->topology_change
!= val
) {
617 /* On topology change, set the bridge ageing time to twice the
618 * forward delay. Otherwise, restore its default ageing time.
622 t
= 2 * br
->forward_delay
;
623 br_debug(br
, "decreasing ageing time to %lu\n", t
);
625 t
= br
->bridge_ageing_time
;
626 br_debug(br
, "restoring ageing time to %lu\n", t
);
629 err
= __set_ageing_time(br
->dev
, t
);
631 br_warn(br
, "error offloading ageing time\n");
636 br
->topology_change
= val
;
639 void __br_set_forward_delay(struct net_bridge
*br
, unsigned long t
)
641 br
->bridge_forward_delay
= t
;
642 if (br_is_root_bridge(br
))
643 br
->forward_delay
= br
->bridge_forward_delay
;
646 int br_set_forward_delay(struct net_bridge
*br
, unsigned long val
)
648 unsigned long t
= clock_t_to_jiffies(val
);
651 spin_lock_bh(&br
->lock
);
652 if (br
->stp_enabled
!= BR_NO_STP
&&
653 (t
< BR_MIN_FORWARD_DELAY
|| t
> BR_MAX_FORWARD_DELAY
))
656 __br_set_forward_delay(br
, t
);
660 spin_unlock_bh(&br
->lock
);