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_ID_PORT_STP_STATE
,
44 .flags
= SWITCHDEV_F_DEFER
,
50 err
= switchdev_port_attr_set(p
->dev
, &attr
);
51 if (err
&& err
!= -EOPNOTSUPP
)
52 br_warn(p
->br
, "error setting offload STP state on port %u(%s)\n",
53 (unsigned int) p
->port_no
, p
->dev
->name
);
56 /* called under bridge lock */
57 struct net_bridge_port
*br_get_port(struct net_bridge
*br
, u16 port_no
)
59 struct net_bridge_port
*p
;
61 list_for_each_entry_rcu(p
, &br
->port_list
, list
) {
62 if (p
->port_no
== port_no
)
69 /* called under bridge lock */
70 static int br_should_become_root_port(const struct net_bridge_port
*p
,
73 struct net_bridge
*br
;
74 struct net_bridge_port
*rp
;
78 if (p
->state
== BR_STATE_DISABLED
||
79 br_is_designated_port(p
))
82 if (memcmp(&br
->bridge_id
, &p
->designated_root
, 8) <= 0)
88 rp
= br_get_port(br
, root_port
);
90 t
= memcmp(&p
->designated_root
, &rp
->designated_root
, 8);
96 if (p
->designated_cost
+ p
->path_cost
<
97 rp
->designated_cost
+ rp
->path_cost
)
99 else if (p
->designated_cost
+ p
->path_cost
>
100 rp
->designated_cost
+ rp
->path_cost
)
103 t
= memcmp(&p
->designated_bridge
, &rp
->designated_bridge
, 8);
109 if (p
->designated_port
< rp
->designated_port
)
111 else if (p
->designated_port
> rp
->designated_port
)
114 if (p
->port_id
< rp
->port_id
)
120 static void br_root_port_block(const struct net_bridge
*br
,
121 struct net_bridge_port
*p
)
124 br_notice(br
, "port %u(%s) tried to become root port (blocked)",
125 (unsigned int) p
->port_no
, p
->dev
->name
);
127 br_set_state(p
, BR_STATE_LISTENING
);
129 br_ifinfo_notify(RTM_NEWLINK
, p
);
131 if (br
->forward_delay
> 0)
132 mod_timer(&p
->forward_delay_timer
, jiffies
+ br
->forward_delay
);
135 /* called under bridge lock */
136 static void br_root_selection(struct net_bridge
*br
)
138 struct net_bridge_port
*p
;
141 list_for_each_entry(p
, &br
->port_list
, list
) {
142 if (!br_should_become_root_port(p
, root_port
))
145 if (p
->flags
& BR_ROOT_BLOCK
)
146 br_root_port_block(br
, p
);
148 root_port
= p
->port_no
;
151 br
->root_port
= root_port
;
154 br
->designated_root
= br
->bridge_id
;
155 br
->root_path_cost
= 0;
157 p
= br_get_port(br
, root_port
);
158 br
->designated_root
= p
->designated_root
;
159 br
->root_path_cost
= p
->designated_cost
+ p
->path_cost
;
163 /* called under bridge lock */
164 void br_become_root_bridge(struct net_bridge
*br
)
166 br
->max_age
= br
->bridge_max_age
;
167 br
->hello_time
= br
->bridge_hello_time
;
168 br
->forward_delay
= br
->bridge_forward_delay
;
169 br_topology_change_detection(br
);
170 del_timer(&br
->tcn_timer
);
172 if (br
->dev
->flags
& IFF_UP
) {
173 br_config_bpdu_generation(br
);
174 mod_timer(&br
->hello_timer
, jiffies
+ br
->hello_time
);
178 /* called under bridge lock */
179 void br_transmit_config(struct net_bridge_port
*p
)
181 struct br_config_bpdu bpdu
;
182 struct net_bridge
*br
;
184 if (timer_pending(&p
->hold_timer
)) {
185 p
->config_pending
= 1;
191 bpdu
.topology_change
= br
->topology_change
;
192 bpdu
.topology_change_ack
= p
->topology_change_ack
;
193 bpdu
.root
= br
->designated_root
;
194 bpdu
.root_path_cost
= br
->root_path_cost
;
195 bpdu
.bridge_id
= br
->bridge_id
;
196 bpdu
.port_id
= p
->port_id
;
197 if (br_is_root_bridge(br
))
198 bpdu
.message_age
= 0;
200 struct net_bridge_port
*root
201 = br_get_port(br
, br
->root_port
);
202 bpdu
.message_age
= (jiffies
- root
->designated_age
)
205 bpdu
.max_age
= br
->max_age
;
206 bpdu
.hello_time
= br
->hello_time
;
207 bpdu
.forward_delay
= br
->forward_delay
;
209 if (bpdu
.message_age
< br
->max_age
) {
210 br_send_config_bpdu(p
, &bpdu
);
211 p
->topology_change_ack
= 0;
212 p
->config_pending
= 0;
213 if (p
->br
->stp_enabled
== BR_KERNEL_STP
)
214 mod_timer(&p
->hold_timer
,
215 round_jiffies(jiffies
+ BR_HOLD_TIME
));
219 /* called under bridge lock */
220 static void br_record_config_information(struct net_bridge_port
*p
,
221 const struct br_config_bpdu
*bpdu
)
223 p
->designated_root
= bpdu
->root
;
224 p
->designated_cost
= bpdu
->root_path_cost
;
225 p
->designated_bridge
= bpdu
->bridge_id
;
226 p
->designated_port
= bpdu
->port_id
;
227 p
->designated_age
= jiffies
- bpdu
->message_age
;
229 mod_timer(&p
->message_age_timer
, jiffies
230 + (bpdu
->max_age
- bpdu
->message_age
));
233 /* called under bridge lock */
234 static void br_record_config_timeout_values(struct net_bridge
*br
,
235 const struct br_config_bpdu
*bpdu
)
237 br
->max_age
= bpdu
->max_age
;
238 br
->hello_time
= bpdu
->hello_time
;
239 br
->forward_delay
= bpdu
->forward_delay
;
240 br
->topology_change
= bpdu
->topology_change
;
243 /* called under bridge lock */
244 void br_transmit_tcn(struct net_bridge
*br
)
246 struct net_bridge_port
*p
;
248 p
= br_get_port(br
, br
->root_port
);
252 br_notice(br
, "root port %u not found for topology notice\n",
256 /* called under bridge lock */
257 static int br_should_become_designated_port(const struct net_bridge_port
*p
)
259 struct net_bridge
*br
;
263 if (br_is_designated_port(p
))
266 if (memcmp(&p
->designated_root
, &br
->designated_root
, 8))
269 if (br
->root_path_cost
< p
->designated_cost
)
271 else if (br
->root_path_cost
> p
->designated_cost
)
274 t
= memcmp(&br
->bridge_id
, &p
->designated_bridge
, 8);
280 if (p
->port_id
< p
->designated_port
)
286 /* called under bridge lock */
287 static void br_designated_port_selection(struct net_bridge
*br
)
289 struct net_bridge_port
*p
;
291 list_for_each_entry(p
, &br
->port_list
, list
) {
292 if (p
->state
!= BR_STATE_DISABLED
&&
293 br_should_become_designated_port(p
))
294 br_become_designated_port(p
);
299 /* called under bridge lock */
300 static int br_supersedes_port_info(const struct net_bridge_port
*p
,
301 const struct br_config_bpdu
*bpdu
)
305 t
= memcmp(&bpdu
->root
, &p
->designated_root
, 8);
311 if (bpdu
->root_path_cost
< p
->designated_cost
)
313 else if (bpdu
->root_path_cost
> p
->designated_cost
)
316 t
= memcmp(&bpdu
->bridge_id
, &p
->designated_bridge
, 8);
322 if (memcmp(&bpdu
->bridge_id
, &p
->br
->bridge_id
, 8))
325 if (bpdu
->port_id
<= p
->designated_port
)
331 /* called under bridge lock */
332 static void br_topology_change_acknowledged(struct net_bridge
*br
)
334 br
->topology_change_detected
= 0;
335 del_timer(&br
->tcn_timer
);
338 /* called under bridge lock */
339 void br_topology_change_detection(struct net_bridge
*br
)
341 int isroot
= br_is_root_bridge(br
);
343 if (br
->stp_enabled
!= BR_KERNEL_STP
)
346 br_info(br
, "topology change detected, %s\n",
347 isroot
? "propagating" : "sending tcn bpdu");
350 br
->topology_change
= 1;
351 mod_timer(&br
->topology_change_timer
, jiffies
352 + br
->bridge_forward_delay
+ br
->bridge_max_age
);
353 } else if (!br
->topology_change_detected
) {
355 mod_timer(&br
->tcn_timer
, jiffies
+ br
->bridge_hello_time
);
358 br
->topology_change_detected
= 1;
361 /* called under bridge lock */
362 void br_config_bpdu_generation(struct net_bridge
*br
)
364 struct net_bridge_port
*p
;
366 list_for_each_entry(p
, &br
->port_list
, list
) {
367 if (p
->state
!= BR_STATE_DISABLED
&&
368 br_is_designated_port(p
))
369 br_transmit_config(p
);
373 /* called under bridge lock */
374 static void br_reply(struct net_bridge_port
*p
)
376 br_transmit_config(p
);
379 /* called under bridge lock */
380 void br_configuration_update(struct net_bridge
*br
)
382 br_root_selection(br
);
383 br_designated_port_selection(br
);
386 /* called under bridge lock */
387 void br_become_designated_port(struct net_bridge_port
*p
)
389 struct net_bridge
*br
;
392 p
->designated_root
= br
->designated_root
;
393 p
->designated_cost
= br
->root_path_cost
;
394 p
->designated_bridge
= br
->bridge_id
;
395 p
->designated_port
= p
->port_id
;
399 /* called under bridge lock */
400 static void br_make_blocking(struct net_bridge_port
*p
)
402 if (p
->state
!= BR_STATE_DISABLED
&&
403 p
->state
!= BR_STATE_BLOCKING
) {
404 if (p
->state
== BR_STATE_FORWARDING
||
405 p
->state
== BR_STATE_LEARNING
)
406 br_topology_change_detection(p
->br
);
408 br_set_state(p
, BR_STATE_BLOCKING
);
410 br_ifinfo_notify(RTM_NEWLINK
, p
);
412 del_timer(&p
->forward_delay_timer
);
416 /* called under bridge lock */
417 static void br_make_forwarding(struct net_bridge_port
*p
)
419 struct net_bridge
*br
= p
->br
;
421 if (p
->state
!= BR_STATE_BLOCKING
)
424 if (br
->stp_enabled
== BR_NO_STP
|| br
->forward_delay
== 0) {
425 br_set_state(p
, BR_STATE_FORWARDING
);
426 br_topology_change_detection(br
);
427 del_timer(&p
->forward_delay_timer
);
428 } else if (br
->stp_enabled
== BR_KERNEL_STP
)
429 br_set_state(p
, BR_STATE_LISTENING
);
431 br_set_state(p
, BR_STATE_LEARNING
);
434 br_ifinfo_notify(RTM_NEWLINK
, p
);
436 if (br
->forward_delay
!= 0)
437 mod_timer(&p
->forward_delay_timer
, jiffies
+ br
->forward_delay
);
440 /* called under bridge lock */
441 void br_port_state_selection(struct net_bridge
*br
)
443 struct net_bridge_port
*p
;
444 unsigned int liveports
= 0;
446 list_for_each_entry(p
, &br
->port_list
, list
) {
447 if (p
->state
== BR_STATE_DISABLED
)
450 /* Don't change port states if userspace is handling STP */
451 if (br
->stp_enabled
!= BR_USER_STP
) {
452 if (p
->port_no
== br
->root_port
) {
453 p
->config_pending
= 0;
454 p
->topology_change_ack
= 0;
455 br_make_forwarding(p
);
456 } else if (br_is_designated_port(p
)) {
457 del_timer(&p
->message_age_timer
);
458 br_make_forwarding(p
);
460 p
->config_pending
= 0;
461 p
->topology_change_ack
= 0;
466 if (p
->state
!= BR_STATE_BLOCKING
)
467 br_multicast_enable_port(p
);
468 /* Multicast is not disabled for the port when it goes in
469 * blocking state because the timers will expire and stop by
470 * themselves without sending more queries.
472 if (p
->state
== BR_STATE_FORWARDING
)
477 netif_carrier_off(br
->dev
);
479 netif_carrier_on(br
->dev
);
482 /* called under bridge lock */
483 static void br_topology_change_acknowledge(struct net_bridge_port
*p
)
485 p
->topology_change_ack
= 1;
486 br_transmit_config(p
);
489 /* called under bridge lock */
490 void br_received_config_bpdu(struct net_bridge_port
*p
,
491 const struct br_config_bpdu
*bpdu
)
493 struct net_bridge
*br
;
497 was_root
= br_is_root_bridge(br
);
499 if (br_supersedes_port_info(p
, bpdu
)) {
500 br_record_config_information(p
, bpdu
);
501 br_configuration_update(br
);
502 br_port_state_selection(br
);
504 if (!br_is_root_bridge(br
) && was_root
) {
505 del_timer(&br
->hello_timer
);
506 if (br
->topology_change_detected
) {
507 del_timer(&br
->topology_change_timer
);
510 mod_timer(&br
->tcn_timer
,
511 jiffies
+ br
->bridge_hello_time
);
515 if (p
->port_no
== br
->root_port
) {
516 br_record_config_timeout_values(br
, bpdu
);
517 br_config_bpdu_generation(br
);
518 if (bpdu
->topology_change_ack
)
519 br_topology_change_acknowledged(br
);
521 } else if (br_is_designated_port(p
)) {
526 /* called under bridge lock */
527 void br_received_tcn_bpdu(struct net_bridge_port
*p
)
529 if (br_is_designated_port(p
)) {
530 br_info(p
->br
, "port %u(%s) received tcn bpdu\n",
531 (unsigned int) p
->port_no
, p
->dev
->name
);
533 br_topology_change_detection(p
->br
);
534 br_topology_change_acknowledge(p
);
538 /* Change bridge STP parameter */
539 int br_set_hello_time(struct net_bridge
*br
, unsigned long val
)
541 unsigned long t
= clock_t_to_jiffies(val
);
543 if (t
< BR_MIN_HELLO_TIME
|| t
> BR_MAX_HELLO_TIME
)
546 spin_lock_bh(&br
->lock
);
547 br
->bridge_hello_time
= t
;
548 if (br_is_root_bridge(br
))
549 br
->hello_time
= br
->bridge_hello_time
;
550 spin_unlock_bh(&br
->lock
);
554 int br_set_max_age(struct net_bridge
*br
, unsigned long val
)
556 unsigned long t
= clock_t_to_jiffies(val
);
558 if (t
< BR_MIN_MAX_AGE
|| t
> BR_MAX_MAX_AGE
)
561 spin_lock_bh(&br
->lock
);
562 br
->bridge_max_age
= t
;
563 if (br_is_root_bridge(br
))
564 br
->max_age
= br
->bridge_max_age
;
565 spin_unlock_bh(&br
->lock
);
570 int br_set_ageing_time(struct net_bridge
*br
, u32 ageing_time
)
572 struct switchdev_attr attr
= {
573 .id
= SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME
,
574 .flags
= SWITCHDEV_F_SKIP_EOPNOTSUPP
,
575 .u
.ageing_time
= ageing_time
,
577 unsigned long t
= clock_t_to_jiffies(ageing_time
);
580 if (t
< BR_MIN_AGEING_TIME
|| t
> BR_MAX_AGEING_TIME
)
583 err
= switchdev_port_attr_set(br
->dev
, &attr
);
588 mod_timer(&br
->gc_timer
, jiffies
);
593 void __br_set_forward_delay(struct net_bridge
*br
, unsigned long t
)
595 br
->bridge_forward_delay
= t
;
596 if (br_is_root_bridge(br
))
597 br
->forward_delay
= br
->bridge_forward_delay
;
600 int br_set_forward_delay(struct net_bridge
*br
, unsigned long val
)
602 unsigned long t
= clock_t_to_jiffies(val
);
605 spin_lock_bh(&br
->lock
);
606 if (br
->stp_enabled
!= BR_NO_STP
&&
607 (t
< BR_MIN_FORWARD_DELAY
|| t
> BR_MAX_FORWARD_DELAY
))
610 __br_set_forward_delay(br
, t
);
614 spin_unlock_bh(&br
->lock
);