2 * Copyright (c) 2011-2015 M3S, Srl - Italy
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Rapid Spanning Tree Protocol (IEEE 802.1D-2004) public interface.
21 * Martino Fornasa <mf@fornasa.it>
22 * Daniele Venturino <daniele.venturino@m3s.it>
23 * Carlo Andreotti <c.andreotti@m3s.it>
25 * References to IEEE 802.1D-2004 standard are enclosed in square brackets.
26 * E.g. [17.3], [Table 17-1], etc.
33 #include "rstp-common.h"
34 #include "rstp-state-machines.h"
35 #include <sys/types.h>
36 #include <netinet/in.h>
37 #include <arpa/inet.h>
40 #include "byte-order.h"
41 #include "connectivity.h"
42 #include "openvswitch/ofpbuf.h"
43 #include "ofproto/ofproto.h"
44 #include "dp-packet.h"
49 #include "openvswitch/vlog.h"
51 VLOG_DEFINE_THIS_MODULE(rstp
);
53 struct ovs_mutex rstp_mutex
= OVS_MUTEX_INITIALIZER
;
55 static struct ovs_list all_rstps__
= OVS_LIST_INITIALIZER(&all_rstps__
);
56 static struct ovs_list
*const all_rstps
OVS_GUARDED_BY(rstp_mutex
) = &all_rstps__
;
58 /* Internal use only. */
59 static void rstp_set_bridge_address__(struct rstp
*, rstp_identifier
)
60 OVS_REQUIRES(rstp_mutex
);
61 static void rstp_set_bridge_priority__(struct rstp
*, int new_priority
)
62 OVS_REQUIRES(rstp_mutex
);
63 static void rstp_set_bridge_ageing_time__(struct rstp
*, int new_ageing_time
)
64 OVS_REQUIRES(rstp_mutex
);
65 static void rstp_set_bridge_force_protocol_version__(struct rstp
*,
66 enum rstp_force_protocol_version
)
67 OVS_REQUIRES(rstp_mutex
);
68 static void rstp_set_bridge_hello_time__(struct rstp
*)
69 OVS_REQUIRES(rstp_mutex
);
70 static void rstp_set_bridge_max_age__(struct rstp
*, int new_max_age
)
71 OVS_REQUIRES(rstp_mutex
);
72 static void rstp_set_bridge_forward_delay__(struct rstp
*, int new_forward_delay
)
73 OVS_REQUIRES(rstp_mutex
);
74 static void rstp_set_bridge_transmit_hold_count__(struct rstp
*,
75 int new_transmit_hold_count
)
76 OVS_REQUIRES(rstp_mutex
);
77 static void rstp_set_bridge_migrate_time__(struct rstp
*)
78 OVS_REQUIRES(rstp_mutex
);
79 static void rstp_set_bridge_times__(struct rstp
*, int new_forward_delay
,
80 int new_hello_time
, int new_max_age
,
82 OVS_REQUIRES(rstp_mutex
);
84 static struct rstp_port
*rstp_get_port__(struct rstp
*rstp
,
86 OVS_REQUIRES(rstp_mutex
);
87 static void set_port_id__(struct rstp_port
*)
88 OVS_REQUIRES(rstp_mutex
);
89 static void update_port_enabled__(struct rstp_port
*)
90 OVS_REQUIRES(rstp_mutex
);
91 static void set_bridge_priority__(struct rstp
*)
92 OVS_REQUIRES(rstp_mutex
);
93 static void reinitialize_rstp__(struct rstp
*)
94 OVS_REQUIRES(rstp_mutex
);
95 static bool is_port_number_available__(struct rstp
*, int, struct rstp_port
*)
96 OVS_REQUIRES(rstp_mutex
);
97 static uint16_t rstp_first_free_number__(struct rstp
*, struct rstp_port
*)
98 OVS_REQUIRES(rstp_mutex
);
99 static void rstp_initialize_port_defaults__(struct rstp_port
*)
100 OVS_REQUIRES(rstp_mutex
);
101 static void rstp_port_set_priority__(struct rstp_port
*, int priority
)
102 OVS_REQUIRES(rstp_mutex
);
103 static void rstp_port_set_port_number__(struct rstp_port
*,
104 uint16_t port_number
)
105 OVS_REQUIRES(rstp_mutex
);
106 static void rstp_port_set_path_cost__(struct rstp_port
*, uint32_t path_cost
)
107 OVS_REQUIRES(rstp_mutex
);
108 static void rstp_port_set_administrative_bridge_port__(struct rstp_port
*,
109 uint8_t admin_port_state
,
111 OVS_REQUIRES(rstp_mutex
);
112 static void rstp_port_set_admin_edge__(struct rstp_port
*, bool admin_edge
)
113 OVS_REQUIRES(rstp_mutex
);
114 static void rstp_port_set_auto_edge__(struct rstp_port
*, bool auto_edge
)
115 OVS_REQUIRES(rstp_mutex
);
116 static void rstp_port_set_admin_point_to_point_mac__(struct rstp_port
*,
117 enum rstp_admin_point_to_point_mac_state admin_p2p_mac_state
)
118 OVS_REQUIRES(rstp_mutex
);
119 static void rstp_port_set_mcheck__(struct rstp_port
*, bool mcheck
)
120 OVS_REQUIRES(rstp_mutex
);
121 static void reinitialize_port__(struct rstp_port
*p
)
122 OVS_REQUIRES(rstp_mutex
);
123 static bool rstp_is_root_bridge__(const struct rstp
*rstp
)
124 OVS_REQUIRES(rstp_mutex
);
125 static uint32_t rstp_get_root_path_cost__(const struct rstp
*rstp
)
126 OVS_REQUIRES(rstp_mutex
);
127 static struct rstp_port
*rstp_get_root_port__(const struct rstp
*rstp
)
128 OVS_REQUIRES(rstp_mutex
);
129 static rstp_identifier
rstp_get_root_id__(const struct rstp
*rstp
)
130 OVS_REQUIRES(rstp_mutex
);
131 static void rstp_unixctl_tcn(struct unixctl_conn
*, int argc
,
132 const char *argv
[], void *aux
);
133 static void rstp_unixctl_show(struct unixctl_conn
*, int argc
,
134 const char *argv
[], void *aux
);
137 rstp_state_name(enum rstp_state state
)
144 case RSTP_FORWARDING
:
146 case RSTP_DISCARDING
:
154 rstp_port_role_name(enum rstp_port_role role
)
159 case ROLE_DESIGNATED
:
172 /* Caller has to hold a reference to prevent 'rstp' from being deleted
173 * while taking a new reference. */
175 rstp_ref(struct rstp
*rstp
)
176 OVS_EXCLUDED(rstp_mutex
)
179 ovs_refcount_ref(&rstp
->ref_cnt
);
184 /* Frees RSTP struct when reference count reaches zero. */
186 rstp_unref(struct rstp
*rstp
)
187 OVS_EXCLUDED(rstp_mutex
)
189 if (rstp
&& ovs_refcount_unref_relaxed(&rstp
->ref_cnt
) == 1) {
190 ovs_mutex_lock(&rstp_mutex
);
192 /* Each RSTP port points back to struct rstp without holding a
193 * reference for that pointer. This is OK as we never move
194 * ports from one bridge to another, and holders always
195 * release their ports before releasing the bridge. This
196 * means that there should be not ports at this time. */
197 ovs_assert(hmap_is_empty(&rstp
->ports
));
199 ovs_list_remove(&rstp
->node
);
200 ovs_mutex_unlock(&rstp_mutex
);
201 hmap_destroy(&rstp
->ports
);
207 /* Returns the port number. Mutex is needed to guard against
208 * concurrent reinitialization (which can temporarily clear the
211 rstp_port_get_number(const struct rstp_port
*p
)
212 OVS_EXCLUDED(rstp_mutex
)
216 ovs_mutex_lock(&rstp_mutex
);
217 number
= p
->port_number
;
218 ovs_mutex_unlock(&rstp_mutex
);
223 /* Decrements the State Machines' timers. */
225 rstp_tick_timers(struct rstp
*rstp
)
226 OVS_EXCLUDED(rstp_mutex
)
228 ovs_mutex_lock(&rstp_mutex
);
229 decrease_rstp_port_timers__(rstp
);
230 ovs_mutex_unlock(&rstp_mutex
);
233 /* Processes an incoming BPDU. */
235 rstp_port_received_bpdu(struct rstp_port
*rp
, const void *bpdu
,
237 OVS_EXCLUDED(rstp_mutex
)
239 ovs_mutex_lock(&rstp_mutex
);
240 /* Only process packets on ports that have RSTP enabled. */
241 if (rp
&& rp
->rstp_state
!= RSTP_DISABLED
) {
242 process_received_bpdu__(rp
, bpdu
, bpdu_size
);
244 ovs_mutex_unlock(&rstp_mutex
);
249 OVS_EXCLUDED(rstp_mutex
)
251 unixctl_command_register("rstp/tcn", "[bridge]", 0, 1, rstp_unixctl_tcn
,
253 unixctl_command_register("rstp/show", "[bridge]", 0, 1, rstp_unixctl_show
,
257 /* Creates and returns a new RSTP instance that initially has no ports. */
259 rstp_create(const char *name
, rstp_identifier bridge_address
,
260 void (*send_bpdu
)(struct dp_packet
*bpdu
, void *port_aux
,
263 OVS_EXCLUDED(rstp_mutex
)
267 VLOG_DBG("Creating RSTP instance");
269 rstp
= xzalloc(sizeof *rstp
);
270 rstp
->name
= xstrdup(name
);
272 /* Initialize the ports map before calling any setters,
273 * so that the state machines will see an empty ports map. */
274 hmap_init(&rstp
->ports
);
276 ovs_mutex_lock(&rstp_mutex
);
277 /* Set bridge address. */
278 rstp_set_bridge_address__(rstp
, bridge_address
);
279 /* Set default parameters values. */
280 rstp_set_bridge_priority__(rstp
, RSTP_DEFAULT_PRIORITY
);
281 rstp_set_bridge_ageing_time__(rstp
, RSTP_DEFAULT_AGEING_TIME
);
282 rstp_set_bridge_force_protocol_version__(rstp
, FPV_DEFAULT
);
283 rstp_set_bridge_forward_delay__(rstp
, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY
);
284 rstp_set_bridge_hello_time__(rstp
);
285 rstp_set_bridge_max_age__(rstp
, RSTP_DEFAULT_BRIDGE_MAX_AGE
);
286 rstp_set_bridge_migrate_time__(rstp
);
287 rstp_set_bridge_transmit_hold_count__(rstp
,
288 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT
);
289 rstp_set_bridge_times__(rstp
, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY
,
290 RSTP_BRIDGE_HELLO_TIME
,
291 RSTP_DEFAULT_BRIDGE_MAX_AGE
, 0);
292 rstp
->send_bpdu
= send_bpdu
;
294 rstp
->changes
= false;
296 rstp
->old_root_aux
= NULL
;
297 rstp
->new_root_aux
= NULL
;
299 ovs_refcount_init(&rstp
->ref_cnt
);
301 ovs_list_push_back(all_rstps
, &rstp
->node
);
302 ovs_mutex_unlock(&rstp_mutex
);
304 VLOG_DBG("RSTP instance creation done");
308 /* Called by rstp_set_bridge_address() and rstp_set_bridge_priority(),
309 * it updates the bridge priority vector according to the values passed by
313 set_bridge_priority__(struct rstp
*rstp
)
314 OVS_REQUIRES(rstp_mutex
)
318 rstp
->bridge_priority
.root_bridge_id
= rstp
->bridge_identifier
;
319 rstp
->bridge_priority
.designated_bridge_id
= rstp
->bridge_identifier
;
320 VLOG_DBG("%s: new bridge identifier: "RSTP_ID_FMT
"", rstp
->name
,
321 RSTP_ID_ARGS(rstp
->bridge_identifier
));
323 /* [17.13] When the bridge address changes, recalculates all priority
326 HMAP_FOR_EACH (p
, node
, &rstp
->ports
) {
330 rstp
->changes
= true;
331 updt_roles_tree__(rstp
);
334 /* Sets the bridge address. */
336 rstp_set_bridge_address__(struct rstp
*rstp
, rstp_identifier bridge_address
)
337 OVS_REQUIRES(rstp_mutex
)
339 VLOG_DBG("%s: set bridge address to: "RSTP_ID_FMT
"", rstp
->name
,
340 RSTP_ID_ARGS(bridge_address
));
341 if (rstp
->address
!= bridge_address
) {
342 rstp
->address
= bridge_address
;
343 rstp
->bridge_identifier
&= 0xffff000000000000ULL
;
344 rstp
->bridge_identifier
|= bridge_address
;
345 set_bridge_priority__(rstp
);
349 /* Sets the bridge address. */
351 rstp_set_bridge_address(struct rstp
*rstp
, rstp_identifier bridge_address
)
352 OVS_EXCLUDED(rstp_mutex
)
354 ovs_mutex_lock(&rstp_mutex
);
355 rstp_set_bridge_address__(rstp
, bridge_address
);
356 ovs_mutex_unlock(&rstp_mutex
);
360 rstp_get_name(const struct rstp
*rstp
)
361 OVS_EXCLUDED(rstp_mutex
)
365 ovs_mutex_lock(&rstp_mutex
);
367 ovs_mutex_unlock(&rstp_mutex
);
372 rstp_get_bridge_id(const struct rstp
*rstp
)
373 OVS_EXCLUDED(rstp_mutex
)
375 rstp_identifier bridge_id
;
377 ovs_mutex_lock(&rstp_mutex
);
378 bridge_id
= rstp
->bridge_identifier
;
379 ovs_mutex_unlock(&rstp_mutex
);
384 /* Sets the bridge priority. */
386 rstp_set_bridge_priority__(struct rstp
*rstp
, int new_priority
)
387 OVS_REQUIRES(rstp_mutex
)
389 new_priority
= ROUND_DOWN(new_priority
, RSTP_PRIORITY_STEP
);
391 if (rstp
->priority
!= new_priority
392 && new_priority
>= RSTP_MIN_PRIORITY
393 && new_priority
<= RSTP_MAX_PRIORITY
) {
394 VLOG_DBG("%s: set bridge priority to %d", rstp
->name
, new_priority
);
396 rstp
->priority
= new_priority
;
397 rstp
->bridge_identifier
&= 0x0000ffffffffffffULL
;
398 rstp
->bridge_identifier
|= (uint64_t)new_priority
<< 48;
399 set_bridge_priority__(rstp
);
404 rstp_set_bridge_priority(struct rstp
*rstp
, int new_priority
)
405 OVS_EXCLUDED(rstp_mutex
)
407 ovs_mutex_lock(&rstp_mutex
);
408 rstp_set_bridge_priority__(rstp
, new_priority
);
409 ovs_mutex_unlock(&rstp_mutex
);
412 /* Sets the bridge ageing time. */
414 rstp_set_bridge_ageing_time__(struct rstp
*rstp
, int new_ageing_time
)
415 OVS_REQUIRES(rstp_mutex
)
417 if (new_ageing_time
>= RSTP_MIN_AGEING_TIME
418 && new_ageing_time
<= RSTP_MAX_AGEING_TIME
) {
419 VLOG_DBG("%s: set ageing time to %d", rstp
->name
, new_ageing_time
);
421 rstp
->ageing_time
= new_ageing_time
;
426 rstp_set_bridge_ageing_time(struct rstp
*rstp
, int new_ageing_time
)
427 OVS_EXCLUDED(rstp_mutex
)
429 ovs_mutex_lock(&rstp_mutex
);
430 rstp_set_bridge_ageing_time__(rstp
, new_ageing_time
);
431 ovs_mutex_unlock(&rstp_mutex
);
434 /* Reinitializes RSTP when switching from RSTP mode to STP mode
438 reinitialize_rstp__(struct rstp
*rstp
)
439 OVS_REQUIRES(rstp_mutex
)
442 static struct hmap ports
;
445 /* Copy rstp in temp */
449 /* stop and clear rstp */
450 memset(rstp
, 0, sizeof(struct rstp
));
452 /* Initialize rstp. */
453 rstp
->name
= temp
.name
;
455 /* Initialize the ports hmap before calling any setters,
456 * so that the state machines will see an empty ports list. */
457 hmap_init(&rstp
->ports
);
459 /* Set bridge address. */
460 rstp_set_bridge_address__(rstp
, temp
.address
);
461 /* Set default parameters values. */
462 rstp_set_bridge_priority__(rstp
, RSTP_DEFAULT_PRIORITY
);
463 rstp_set_bridge_ageing_time__(rstp
, RSTP_DEFAULT_AGEING_TIME
);
464 rstp_set_bridge_forward_delay__(rstp
, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY
);
465 rstp_set_bridge_hello_time__(rstp
);
466 rstp_set_bridge_max_age__(rstp
, RSTP_DEFAULT_BRIDGE_MAX_AGE
);
467 rstp_set_bridge_migrate_time__(rstp
);
468 rstp_set_bridge_transmit_hold_count__(rstp
,
469 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT
);
470 rstp_set_bridge_times__(rstp
, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY
,
471 RSTP_BRIDGE_HELLO_TIME
,
472 RSTP_DEFAULT_BRIDGE_MAX_AGE
, 0);
474 rstp
->send_bpdu
= temp
.send_bpdu
;
475 rstp
->aux
= temp
.aux
;
476 rstp
->node
= temp
.node
;
477 rstp
->changes
= false;
483 HMAP_FOR_EACH (p
, node
, &rstp
->ports
) {
484 reinitialize_port__(p
);
487 rstp
->ref_cnt
= temp
.ref_cnt
;
490 /* Sets the force protocol version parameter. */
492 rstp_set_bridge_force_protocol_version__(struct rstp
*rstp
,
493 enum rstp_force_protocol_version new_force_protocol_version
)
494 OVS_REQUIRES(rstp_mutex
)
496 if (new_force_protocol_version
!= rstp
->force_protocol_version
&&
497 (new_force_protocol_version
== FPV_STP_COMPATIBILITY
||
498 new_force_protocol_version
== FPV_DEFAULT
)) {
499 VLOG_DBG("%s: set bridge Force Protocol Version to %d", rstp
->name
,
500 new_force_protocol_version
);
502 /* [17.13] The Spanning Tree Protocol Entity shall be reinitialized,
503 * as specified by the assertion of BEGIN (17.18.1) in the state
504 * machine specification.
506 reinitialize_rstp__(rstp
);
507 rstp
->force_protocol_version
= new_force_protocol_version
;
508 if (rstp
->force_protocol_version
< 2) {
509 rstp
->stp_version
= true;
510 rstp
->rstp_version
= false;
512 rstp
->stp_version
= false;
513 rstp
->rstp_version
= true;
515 rstp
->changes
= true;
521 rstp_set_bridge_force_protocol_version(struct rstp
*rstp
,
522 enum rstp_force_protocol_version new_force_protocol_version
)
523 OVS_EXCLUDED(rstp_mutex
)
525 ovs_mutex_lock(&rstp_mutex
);
526 rstp_set_bridge_force_protocol_version__(rstp
, new_force_protocol_version
);
527 ovs_mutex_unlock(&rstp_mutex
);
530 /* Sets the bridge Hello Time parameter. */
532 rstp_set_bridge_hello_time__(struct rstp
*rstp
)
533 OVS_REQUIRES(rstp_mutex
)
535 VLOG_DBG("%s: set RSTP Hello Time to %d", rstp
->name
,
536 RSTP_BRIDGE_HELLO_TIME
);
537 /* 2 is the only acceptable value. */
538 rstp
->bridge_hello_time
= RSTP_BRIDGE_HELLO_TIME
;
541 /* Sets the bridge max age parameter. */
543 rstp_set_bridge_max_age__(struct rstp
*rstp
, int new_max_age
)
544 OVS_REQUIRES(rstp_mutex
)
546 if (rstp
->bridge_max_age
!= new_max_age
547 && new_max_age
>= RSTP_MIN_BRIDGE_MAX_AGE
548 && new_max_age
<= RSTP_MAX_BRIDGE_MAX_AGE
) {
550 if ((2 * (rstp
->bridge_forward_delay
- 1) >= new_max_age
)
551 && (new_max_age
>= 2 * rstp
->bridge_hello_time
)) {
552 VLOG_DBG("%s: set RSTP bridge Max Age to %d", rstp
->name
,
555 rstp
->bridge_max_age
= new_max_age
;
556 rstp
->bridge_times
.max_age
= new_max_age
;
557 rstp
->changes
= true;
558 updt_roles_tree__(rstp
);
564 rstp_set_bridge_max_age(struct rstp
*rstp
, int new_max_age
)
565 OVS_EXCLUDED(rstp_mutex
)
567 ovs_mutex_lock(&rstp_mutex
);
568 rstp_set_bridge_max_age__(rstp
, new_max_age
);
569 ovs_mutex_unlock(&rstp_mutex
);
572 /* Sets the bridge forward delay parameter. */
574 rstp_set_bridge_forward_delay__(struct rstp
*rstp
, int new_forward_delay
)
575 OVS_REQUIRES(rstp_mutex
)
577 if (rstp
->bridge_forward_delay
!= new_forward_delay
578 && new_forward_delay
>= RSTP_MIN_BRIDGE_FORWARD_DELAY
579 && new_forward_delay
<= RSTP_MAX_BRIDGE_FORWARD_DELAY
) {
580 if (2 * (new_forward_delay
- 1) >= rstp
->bridge_max_age
) {
581 VLOG_DBG("%s: set RSTP Forward Delay to %d", rstp
->name
,
583 rstp
->bridge_forward_delay
= new_forward_delay
;
584 rstp
->bridge_times
.forward_delay
= new_forward_delay
;
585 rstp
->changes
= true;
586 updt_roles_tree__(rstp
);
592 rstp_set_bridge_forward_delay(struct rstp
*rstp
, int new_forward_delay
)
593 OVS_EXCLUDED(rstp_mutex
)
595 ovs_mutex_lock(&rstp_mutex
);
596 rstp_set_bridge_forward_delay__(rstp
, new_forward_delay
);
597 ovs_mutex_unlock(&rstp_mutex
);
600 /* Sets the bridge transmit hold count parameter. */
602 rstp_set_bridge_transmit_hold_count__(struct rstp
*rstp
,
603 int new_transmit_hold_count
)
604 OVS_REQUIRES(rstp_mutex
)
606 if (rstp
->transmit_hold_count
!= new_transmit_hold_count
607 && new_transmit_hold_count
>= RSTP_MIN_TRANSMIT_HOLD_COUNT
608 && new_transmit_hold_count
<= RSTP_MAX_TRANSMIT_HOLD_COUNT
) {
611 VLOG_DBG("%s: set RSTP Transmit Hold Count to %d", rstp
->name
,
612 new_transmit_hold_count
);
613 /* Resetting txCount on all ports [17.13]. */
615 rstp
->transmit_hold_count
= new_transmit_hold_count
;
616 HMAP_FOR_EACH (p
, node
, &rstp
->ports
) {
623 rstp_set_bridge_transmit_hold_count(struct rstp
*rstp
,
624 int new_transmit_hold_count
)
625 OVS_EXCLUDED(rstp_mutex
)
627 ovs_mutex_lock(&rstp_mutex
);
628 rstp_set_bridge_transmit_hold_count__(rstp
, new_transmit_hold_count
);
629 ovs_mutex_unlock(&rstp_mutex
);
632 /* Sets the bridge migrate time parameter. */
634 rstp_set_bridge_migrate_time__(struct rstp
*rstp
)
635 OVS_REQUIRES(rstp_mutex
)
637 VLOG_DBG("%s: set RSTP Migrate Time to %d", rstp
->name
,
639 /* 3 is the only acceptable value */
640 rstp
->migrate_time
= RSTP_MIGRATE_TIME
;
643 /* Sets the bridge times. */
645 rstp_set_bridge_times__(struct rstp
*rstp
, int new_forward_delay
,
646 int new_hello_time
, int new_max_age
,
648 OVS_REQUIRES(rstp_mutex
)
650 VLOG_DBG("%s: set RSTP times to (%d, %d, %d, %d)", rstp
->name
,
651 new_forward_delay
, new_hello_time
, new_max_age
, new_message_age
);
652 if (new_forward_delay
>= RSTP_MIN_BRIDGE_FORWARD_DELAY
653 && new_forward_delay
<= RSTP_MAX_BRIDGE_FORWARD_DELAY
) {
654 rstp
->bridge_times
.forward_delay
= new_forward_delay
;
656 if (new_hello_time
== RSTP_BRIDGE_HELLO_TIME
) {
657 rstp
->bridge_times
.hello_time
= new_hello_time
;
659 if (new_max_age
>= RSTP_MIN_BRIDGE_MAX_AGE
660 && new_max_age
<= RSTP_MAX_BRIDGE_MAX_AGE
) {
661 rstp
->bridge_times
.max_age
= new_max_age
;
663 rstp
->bridge_times
.message_age
= new_message_age
;
666 /* Sets the port id, it is called by rstp_port_set_port_number__() or
667 * rstp_port_set_priority__().
670 set_port_id__(struct rstp_port
*p
)
671 OVS_REQUIRES(rstp_mutex
)
676 /* [9.2.7] Port identifier. */
677 p
->port_id
= p
->port_number
| (p
->priority
<< 8);
678 VLOG_DBG("%s: new RSTP port id "RSTP_PORT_ID_FMT
"", rstp
->name
,
682 /* Sets the port priority. */
684 rstp_port_set_priority__(struct rstp_port
*port
, int priority
)
685 OVS_REQUIRES(rstp_mutex
)
687 if (port
->priority
!= priority
688 && priority
>= RSTP_MIN_PORT_PRIORITY
689 && priority
<= RSTP_MAX_PORT_PRIORITY
) {
690 VLOG_DBG("%s, port %u: set RSTP port priority to %d", port
->rstp
->name
,
691 port
->port_number
, priority
);
693 priority
-= priority
% RSTP_STEP_PORT_PRIORITY
;
694 port
->priority
= priority
;
696 port
->selected
= false;
697 port
->reselect
= true;
701 /* Checks if a port number is available. */
703 is_port_number_available__(struct rstp
*rstp
, int n
, struct rstp_port
*port
)
704 OVS_REQUIRES(rstp_mutex
)
706 if (n
>= 1 && n
<= RSTP_MAX_PORTS
) {
707 struct rstp_port
*p
= rstp_get_port__(rstp
, n
);
709 return p
== NULL
|| p
== port
;
715 rstp_first_free_number__(struct rstp
*rstp
, struct rstp_port
*rstp_port
)
716 OVS_REQUIRES(rstp_mutex
)
720 while (free_number
<= RSTP_MAX_PORTS
) {
721 if (is_port_number_available__(rstp
, free_number
, rstp_port
)) {
726 VLOG_DBG("%s, No free port number available.", rstp
->name
);
730 /* Sets the port number. */
732 rstp_port_set_port_number__(struct rstp_port
*port
, uint16_t port_number
)
733 OVS_REQUIRES(rstp_mutex
)
735 int old_port_number
= port
->port_number
;
737 /* If new_port_number is available, use it, otherwise use the first free
738 * available port number. */
739 if (port
->port_number
!= port_number
|| port_number
== 0) {
741 is_port_number_available__(port
->rstp
, port_number
, port
)
743 : rstp_first_free_number__(port
->rstp
, port
);
745 if (port
->port_number
!= old_port_number
) {
747 /* [17.13] is not clear. I suppose that a port number change
748 * should trigger reselection like a port priority change. */
749 port
->selected
= false;
750 port
->reselect
= true;
752 /* Adjust the ports hmap. */
753 if (!hmap_node_is_null(&port
->node
)) {
754 hmap_remove(&port
->rstp
->ports
, &port
->node
);
756 hmap_insert(&port
->rstp
->ports
, &port
->node
,
757 hash_int(port
->port_number
, 0));
759 VLOG_DBG("%s: set new RSTP port number %d", port
->rstp
->name
,
766 rstp_port_set_port_name__(struct rstp_port
*port
, const char *name
)
767 OVS_REQUIRES(rstp_mutex
)
769 free(port
->port_name
);
770 port
->port_name
= xstrdup(name
);
773 /* Converts the link speed to a port path cost [Table 17-3]. */
775 rstp_convert_speed_to_cost(unsigned int speed
)
779 value
= speed
>= 10000000 ? 2 /* 10 Tb/s. */
780 : speed
>= 1000000 ? 20 /* 1 Tb/s. */
781 : speed
>= 100000 ? 200 /* 100 Gb/s. */
782 : speed
>= 10000 ? 2000 /* 10 Gb/s. */
783 : speed
>= 1000 ? 20000 /* 1 Gb/s. */
784 : speed
>= 100 ? 200000 /* 100 Mb/s. */
785 : speed
>= 10 ? 2000000 /* 10 Mb/s. */
786 : speed
>= 1 ? 20000000 /* 1 Mb/s. */
787 : RSTP_DEFAULT_PORT_PATH_COST
; /* 100 Mb/s. */
792 /* Sets the port path cost. */
794 rstp_port_set_path_cost__(struct rstp_port
*port
, uint32_t path_cost
)
795 OVS_REQUIRES(rstp_mutex
)
797 if (port
->port_path_cost
!= path_cost
798 && path_cost
>= RSTP_MIN_PORT_PATH_COST
799 && path_cost
<= RSTP_MAX_PORT_PATH_COST
) {
800 VLOG_DBG("%s, port %u, set RSTP port path cost to %d",
801 port
->rstp
->name
, port
->port_number
, path_cost
);
803 port
->port_path_cost
= path_cost
;
804 port
->selected
= false;
805 port
->reselect
= true;
809 /* Gets the root path cost. */
811 rstp_get_root_path_cost__(const struct rstp
*rstp
)
812 OVS_REQUIRES(rstp_mutex
)
814 return rstp
->root_priority
.root_path_cost
;
818 rstp_get_root_path_cost(const struct rstp
*rstp
)
819 OVS_EXCLUDED(rstp_mutex
)
823 ovs_mutex_lock(&rstp_mutex
);
824 cost
= rstp_get_root_path_cost__(rstp
);
825 ovs_mutex_unlock(&rstp_mutex
);
829 /* Finds a port which needs to flush its own MAC learning table. A NULL
830 * pointer is returned if no port needs to flush its MAC learning table.
831 * '*port' needs to be NULL in the first call to start the iteration. If
832 * '*port' is passed as non-NULL, it must be the value set by the last
833 * invocation of this function.
835 * This function may only be called by the thread that creates and deletes
836 * ports. Otherwise this function is not thread safe, as the returned
837 * '*port' could become stale before it is used in the next invocation. */
839 rstp_check_and_reset_fdb_flush(struct rstp
*rstp
, struct rstp_port
**port
)
840 OVS_EXCLUDED(rstp_mutex
)
844 ovs_mutex_lock(&rstp_mutex
);
848 HMAP_FOR_EACH (p
, node
, &rstp
->ports
) {
855 } else { /* continue */
856 struct rstp_port
*p
= *port
;
858 HMAP_FOR_EACH_CONTINUE (p
, node
, &rstp
->ports
) {
866 /* No port needs flushing. */
869 /* fdb_flush should be reset by the filtering database
870 * once the entries are removed if rstp_version is TRUE, and
871 * immediately if stp_version is TRUE.*/
873 (*port
)->fdb_flush
= false;
875 ovs_mutex_unlock(&rstp_mutex
);
880 /* Finds a port whose state has changed, and returns the aux pointer set for
881 * the port. A NULL pointer is returned when no changed port is found. On
882 * return '*portp' contains the pointer to the rstp port that changed, or NULL
883 * if no changed port can be found.
885 * If '*portp' is passed as non-NULL, it must be the value set by the last
886 * invocation of this function.
888 * This function may only be called by the thread that creates and deletes
889 * ports. Otherwise this function is not thread safe, as the returned
890 * '*portp' could become stale before it is used in the next invocation. */
892 rstp_get_next_changed_port_aux(struct rstp
*rstp
, struct rstp_port
**portp
)
896 ovs_mutex_lock(&rstp_mutex
);
897 if (*portp
== NULL
) {
900 HMAP_FOR_EACH (p
, node
, &rstp
->ports
) {
901 if (p
->state_changed
) {
902 p
->state_changed
= false;
908 } else { /* continue */
909 struct rstp_port
*p
= *portp
;
911 HMAP_FOR_EACH_CONTINUE (p
, node
, &rstp
->ports
) {
912 if (p
->state_changed
) {
913 p
->state_changed
= false;
920 /* No changed port found. */
923 ovs_mutex_unlock(&rstp_mutex
);
929 rstp_shift_root_learned_address(struct rstp
*rstp
)
933 ovs_mutex_lock(&rstp_mutex
);
934 ret
= rstp
->root_changed
;
935 ovs_mutex_unlock(&rstp_mutex
);
941 rstp_get_old_root_aux(struct rstp
*rstp
)
945 ovs_mutex_lock(&rstp_mutex
);
946 aux
= rstp
->old_root_aux
;
947 ovs_mutex_unlock(&rstp_mutex
);
953 rstp_get_new_root_aux(struct rstp
*rstp
)
957 ovs_mutex_lock(&rstp_mutex
);
958 aux
= rstp
->new_root_aux
;
959 ovs_mutex_unlock(&rstp_mutex
);
965 rstp_reset_root_changed(struct rstp
*rstp
)
967 ovs_mutex_lock(&rstp_mutex
);
968 rstp
->root_changed
= false;
969 ovs_mutex_unlock(&rstp_mutex
);
972 /* Returns the port in 'rstp' with number 'port_number'.
974 * XXX: May only be called while concurrent deletion of ports is excluded. */
975 static struct rstp_port
*
976 rstp_get_port__(struct rstp
*rstp
, uint16_t port_number
)
977 OVS_REQUIRES(rstp_mutex
)
979 struct rstp_port
*port
;
981 ovs_assert(rstp
&& port_number
> 0 && port_number
<= RSTP_MAX_PORTS
);
983 HMAP_FOR_EACH_WITH_HASH (port
, node
, hash_int(port_number
, 0),
985 if (port
->port_number
== port_number
) {
993 rstp_get_port(struct rstp
*rstp
, uint16_t port_number
)
994 OVS_EXCLUDED(rstp_mutex
)
998 ovs_mutex_lock(&rstp_mutex
);
999 p
= rstp_get_port__(rstp
, port_number
);
1000 ovs_mutex_unlock(&rstp_mutex
);
1005 rstp_get_port_aux__(struct rstp
*rstp
, uint16_t port_number
)
1006 OVS_REQUIRES(rstp_mutex
)
1008 struct rstp_port
*p
;
1009 p
= rstp_get_port__(rstp
, port_number
);
1016 /* Updates the port_enabled parameter. */
1018 update_port_enabled__(struct rstp_port
*p
)
1019 OVS_REQUIRES(rstp_mutex
)
1021 if (p
->mac_operational
&& p
->is_administrative_bridge_port
1022 == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED
) {
1023 p
->port_enabled
= true;
1025 p
->port_enabled
= false;
1029 /* Sets the port MAC_Operational parameter [6.4.2]. */
1031 rstp_port_set_mac_operational(struct rstp_port
*p
, bool new_mac_operational
)
1032 OVS_EXCLUDED(rstp_mutex
)
1036 ovs_mutex_lock(&rstp_mutex
);
1038 if (p
->mac_operational
!= new_mac_operational
) {
1039 p
->mac_operational
= new_mac_operational
;
1040 update_port_enabled__(p
);
1041 rstp
->changes
= true;
1044 ovs_mutex_unlock(&rstp_mutex
);
1047 /* Sets the port Administrative Bridge Port parameter. */
1049 rstp_port_set_administrative_bridge_port__(struct rstp_port
*p
,
1050 uint8_t admin_port_state
,
1052 OVS_REQUIRES(rstp_mutex
)
1054 VLOG_DBG("%s, port %u: set RSTP port admin-port-state to %d",
1055 p
->rstp
->name
, p
->port_number
, admin_port_state
);
1057 if (p
->is_administrative_bridge_port
!= admin_port_state
1058 && (admin_port_state
== RSTP_ADMIN_BRIDGE_PORT_STATE_DISABLED
1059 || admin_port_state
== RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED
)) {
1060 p
->is_administrative_bridge_port
= admin_port_state
;
1061 update_port_enabled__(p
);
1063 if (!initializing
) {
1064 struct rstp
*rstp
= p
->rstp
;
1066 rstp
->changes
= true;
1072 /* Sets the port oper_point_to_point_mac parameter. */
1074 rstp_port_set_oper_point_to_point_mac__(struct rstp_port
*p
,
1075 uint8_t new_oper_p2p_mac
)
1076 OVS_REQUIRES(rstp_mutex
)
1078 if (p
->oper_point_to_point_mac
!= new_oper_p2p_mac
1079 && (new_oper_p2p_mac
== RSTP_OPER_P2P_MAC_STATE_DISABLED
1080 || new_oper_p2p_mac
== RSTP_OPER_P2P_MAC_STATE_ENABLED
)) {
1082 p
->oper_point_to_point_mac
= new_oper_p2p_mac
;
1083 update_port_enabled__(p
);
1087 /* Initializes a port with the defaults values for its parameters. */
1089 rstp_initialize_port_defaults__(struct rstp_port
*p
)
1090 OVS_REQUIRES(rstp_mutex
)
1092 rstp_port_set_administrative_bridge_port__(p
,
1093 RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED
,
1095 rstp_port_set_oper_point_to_point_mac__(p
,
1096 RSTP_OPER_P2P_MAC_STATE_ENABLED
);
1097 rstp_port_set_path_cost__(p
, RSTP_DEFAULT_PORT_PATH_COST
);
1098 rstp_port_set_admin_edge__(p
, false);
1099 rstp_port_set_auto_edge__(p
, true);
1100 rstp_port_set_mcheck__(p
, false);
1102 /* Initialize state machines. */
1103 p
->port_receive_sm_state
= PORT_RECEIVE_SM_INIT
;
1104 p
->port_protocol_migration_sm_state
= PORT_PROTOCOL_MIGRATION_SM_INIT
;
1105 p
->bridge_detection_sm_state
= BRIDGE_DETECTION_SM_INIT
;
1106 p
->port_transmit_sm_state
= PORT_TRANSMIT_SM_INIT
;
1107 p
->port_information_sm_state
= PORT_INFORMATION_SM_INIT
;
1108 p
->port_role_transition_sm_state
= PORT_ROLE_TRANSITION_SM_INIT
;
1109 p
->port_state_transition_sm_state
= PORT_STATE_TRANSITION_SM_INIT
;
1110 p
->topology_change_sm_state
= TOPOLOGY_CHANGE_SM_INIT
;
1116 reinitialize_port__(struct rstp_port
*p
)
1117 OVS_REQUIRES(rstp_mutex
)
1119 struct rstp_port temp_port
;
1124 memset(p
, 0, sizeof(struct rstp_port
));
1126 p
->ref_cnt
= temp_port
.ref_cnt
;
1128 p
->node
= temp_port
.node
;
1129 p
->aux
= temp_port
.aux
;
1130 p
->port_number
= temp_port
.port_number
;
1131 p
->port_priority
= temp_port
.port_priority
;
1132 p
->port_id
= temp_port
.port_id
;
1133 p
->rstp_state
= RSTP_DISCARDING
;
1135 rstp_initialize_port_defaults__(p
);
1137 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT
" reinitialized.", rstp
->name
,
1142 reinitialize_port(struct rstp_port
*p
)
1143 OVS_EXCLUDED(rstp_mutex
)
1145 ovs_mutex_lock(&rstp_mutex
);
1146 reinitialize_port__(p
);
1147 ovs_mutex_unlock(&rstp_mutex
);
1150 /* Sets the port state. */
1152 rstp_port_set_state__(struct rstp_port
*p
, enum rstp_state state
)
1153 OVS_REQUIRES(rstp_mutex
)
1158 VLOG_DBG("%s, port %u: set RSTP port state %s -> %s", rstp
->name
,
1160 rstp_state_name(p
->rstp_state
), rstp_state_name(state
));
1162 if (state
!= p
->rstp_state
&& !p
->state_changed
) {
1163 p
->state_changed
= true;
1164 seq_change(connectivity_seq_get());
1166 p
->rstp_state
= state
;
1170 rstp_port_set_state(struct rstp_port
*p
, enum rstp_state state
)
1171 OVS_EXCLUDED(rstp_mutex
)
1173 ovs_mutex_lock(&rstp_mutex
);
1174 rstp_port_set_state__(p
, state
);
1175 ovs_mutex_unlock(&rstp_mutex
);
1178 /* Adds a RSTP port. */
1180 rstp_add_port(struct rstp
*rstp
)
1181 OVS_EXCLUDED(rstp_mutex
)
1183 struct rstp_port
*p
= xzalloc(sizeof *p
);
1185 ovs_refcount_init(&p
->ref_cnt
);
1186 hmap_node_nullify(&p
->node
);
1188 ovs_mutex_lock(&rstp_mutex
);
1190 rstp_port_set_priority__(p
, RSTP_DEFAULT_PORT_PRIORITY
);
1191 rstp_port_set_port_number__(p
, 0);
1193 p
->port_name
= NULL
;
1194 rstp_initialize_port_defaults__(p
);
1195 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT
" initialized.", rstp
->name
,
1198 rstp_port_set_state__(p
, RSTP_DISCARDING
);
1199 rstp
->changes
= true;
1201 VLOG_DBG("%s: added port "RSTP_PORT_ID_FMT
"", rstp
->name
, p
->port_id
);
1202 ovs_mutex_unlock(&rstp_mutex
);
1206 /* Caller has to hold a reference to prevent 'rstp_port' from being deleted
1207 * while taking a new reference. */
1209 rstp_port_ref(const struct rstp_port
*rp_
)
1210 OVS_EXCLUDED(rstp_mutex
)
1212 struct rstp_port
*rp
= CONST_CAST(struct rstp_port
*, rp_
);
1215 ovs_refcount_ref(&rp
->ref_cnt
);
1220 /* Frees RSTP struct. This can be caller by any thread. */
1222 rstp_port_unref(struct rstp_port
*rp
)
1223 OVS_EXCLUDED(rstp_mutex
)
1225 if (rp
&& ovs_refcount_unref_relaxed(&rp
->ref_cnt
) == 1) {
1228 ovs_mutex_lock(&rstp_mutex
);
1230 rstp_port_set_state__(rp
, RSTP_DISABLED
);
1231 free(rp
->port_name
);
1232 hmap_remove(&rstp
->ports
, &rp
->node
);
1233 VLOG_DBG("%s: removed port "RSTP_PORT_ID_FMT
"", rstp
->name
,
1235 ovs_mutex_unlock(&rstp_mutex
);
1240 /* Sets the port Admin Edge parameter. */
1242 rstp_port_set_admin_edge__(struct rstp_port
*port
, bool admin_edge
)
1243 OVS_REQUIRES(rstp_mutex
)
1245 if (port
->admin_edge
!= admin_edge
) {
1246 VLOG_DBG("%s, port %u: set RSTP Admin Edge to %d", port
->rstp
->name
,
1247 port
->port_number
, admin_edge
);
1249 port
->admin_edge
= admin_edge
;
1253 /* Sets the port Auto Edge parameter. */
1255 rstp_port_set_auto_edge__(struct rstp_port
*port
, bool auto_edge
)
1256 OVS_REQUIRES(rstp_mutex
)
1258 if (port
->auto_edge
!= auto_edge
) {
1259 VLOG_DBG("%s, port %u: set RSTP Auto Edge to %d", port
->rstp
->name
,
1260 port
->port_number
, auto_edge
);
1262 port
->auto_edge
= auto_edge
;
1266 /* Sets the port admin_point_to_point_mac parameter. */
1267 static void rstp_port_set_admin_point_to_point_mac__(struct rstp_port
*port
,
1268 enum rstp_admin_point_to_point_mac_state admin_p2p_mac_state
)
1269 OVS_REQUIRES(rstp_mutex
)
1271 VLOG_DBG("%s, port %u: set RSTP port admin-point-to-point-mac to %d",
1272 port
->rstp
->name
, port
->port_number
, admin_p2p_mac_state
);
1273 if (port
->admin_point_to_point_mac
!= admin_p2p_mac_state
) {
1274 if (admin_p2p_mac_state
== RSTP_ADMIN_P2P_MAC_FORCE_TRUE
) {
1275 port
->admin_point_to_point_mac
= admin_p2p_mac_state
;
1276 rstp_port_set_oper_point_to_point_mac__(
1277 port
, RSTP_OPER_P2P_MAC_STATE_ENABLED
);
1278 } else if (admin_p2p_mac_state
== RSTP_ADMIN_P2P_MAC_FORCE_FALSE
) {
1279 port
->admin_point_to_point_mac
= admin_p2p_mac_state
;
1280 rstp_port_set_oper_point_to_point_mac__(
1281 port
, RSTP_OPER_P2P_MAC_STATE_DISABLED
);
1282 } else if (admin_p2p_mac_state
== RSTP_ADMIN_P2P_MAC_AUTO
) {
1283 /* If adminPointToPointMAC is set to Auto, then the value of
1284 * operPointToPointMAC is determined in accordance with the
1285 * specific procedures defined for the MAC entity concerned, as
1286 * defined in 6.5. If these procedures determine that the MAC
1287 * entity is connected to a point-to-point LAN, then
1288 * operPointToPointMAC is set TRUE; otherwise it is set FALSE.
1289 * In the absence of a specific definition of how to determine
1290 * whether the MAC is connected to a point-to-point LAN or not,
1291 * the value of operPointToPointMAC shall be FALSE. */
1292 port
->admin_point_to_point_mac
= admin_p2p_mac_state
;
1293 rstp_port_set_oper_point_to_point_mac__(
1294 port
, RSTP_OPER_P2P_MAC_STATE_DISABLED
);
1299 /* Sets the port mcheck parameter.
1300 * [17.19.13] May be set by management to force the Port Protocol Migration
1301 * state machine to transmit RST BPDUs for a MigrateTime (17.13.9) period, to
1302 * test whether all STP Bridges (17.4) on the attached LAN have been removed
1303 * and the Port can continue to transmit RSTP BPDUs. Setting mcheck has no
1304 * effect if stpVersion (17.20.12) is TRUE, i.e., the Bridge is operating in
1305 * STP Compatibility mode.
1308 rstp_port_set_mcheck__(struct rstp_port
*port
, bool mcheck
)
1309 OVS_REQUIRES(rstp_mutex
)
1311 if (mcheck
== true && port
->rstp
->force_protocol_version
>= 2) {
1312 port
->mcheck
= true;
1314 VLOG_DBG("%s, port %u: set RSTP mcheck to %d", port
->rstp
->name
,
1315 port
->port_number
, mcheck
);
1319 /* Returns the designated bridge id. */
1321 rstp_get_designated_id(const struct rstp
*rstp
)
1322 OVS_EXCLUDED(rstp_mutex
)
1324 rstp_identifier designated_id
;
1326 ovs_mutex_lock(&rstp_mutex
);
1327 designated_id
= rstp
->root_priority
.designated_bridge_id
;
1328 ovs_mutex_unlock(&rstp_mutex
);
1330 return designated_id
;
1333 /* Returns the root bridge id. */
1334 static rstp_identifier
1335 rstp_get_root_id__(const struct rstp
*rstp
)
1336 OVS_REQUIRES(rstp_mutex
)
1338 return rstp
->root_priority
.root_bridge_id
;
1342 rstp_get_root_id(const struct rstp
*rstp
)
1343 OVS_EXCLUDED(rstp_mutex
)
1345 rstp_identifier root_id
;
1347 ovs_mutex_lock(&rstp_mutex
);
1348 root_id
= rstp_get_root_id__(rstp
);
1349 ovs_mutex_unlock(&rstp_mutex
);
1354 /* Returns the designated port id. */
1356 rstp_get_designated_port_id(const struct rstp
*rstp
)
1357 OVS_EXCLUDED(rstp_mutex
)
1359 uint16_t designated_port_id
;
1361 ovs_mutex_lock(&rstp_mutex
);
1362 designated_port_id
= rstp
->root_priority
.designated_port_id
;
1363 ovs_mutex_unlock(&rstp_mutex
);
1365 return designated_port_id
;
1368 /* Return the bridge port id. */
1370 rstp_get_bridge_port_id(const struct rstp
*rstp
)
1371 OVS_EXCLUDED(rstp_mutex
)
1373 uint16_t bridge_port_id
;
1375 ovs_mutex_lock(&rstp_mutex
);
1376 bridge_port_id
= rstp
->root_priority
.bridge_port_id
;
1377 ovs_mutex_unlock(&rstp_mutex
);
1379 return bridge_port_id
;
1382 /* Returns true if the bridge believes to the be root of the spanning tree,
1386 rstp_is_root_bridge__(const struct rstp
*rstp
)
1387 OVS_REQUIRES(rstp_mutex
)
1389 return rstp
->bridge_identifier
==
1390 rstp
->root_priority
.designated_bridge_id
;
1394 rstp_is_root_bridge(const struct rstp
*rstp
)
1395 OVS_EXCLUDED(rstp_mutex
)
1399 ovs_mutex_lock(&rstp_mutex
);
1400 is_root
= rstp_is_root_bridge__(rstp
);
1401 ovs_mutex_unlock(&rstp_mutex
);
1406 /* Returns the bridge ID of the bridge currently believed to be the root. */
1408 rstp_get_designated_root(const struct rstp
*rstp
)
1409 OVS_EXCLUDED(rstp_mutex
)
1411 rstp_identifier designated_root
;
1413 ovs_mutex_lock(&rstp_mutex
);
1414 designated_root
= rstp
->root_priority
.designated_bridge_id
;
1415 ovs_mutex_unlock(&rstp_mutex
);
1417 return designated_root
;
1420 /* Returns the port connecting 'rstp' to the root bridge, or a null pointer if
1421 * there is no such port.
1423 static struct rstp_port
*
1424 rstp_get_root_port__(const struct rstp
*rstp
)
1425 OVS_REQUIRES(rstp_mutex
)
1427 struct rstp_port
*p
;
1429 HMAP_FOR_EACH (p
, node
, &rstp
->ports
) {
1430 if (p
->port_id
== rstp
->root_port_id
) {
1438 rstp_get_root_port(const struct rstp
*rstp
)
1439 OVS_EXCLUDED(rstp_mutex
)
1441 ovs_mutex_lock(&rstp_mutex
);
1442 struct rstp_port
*p
= rstp_get_root_port__(rstp
);
1443 ovs_mutex_unlock(&rstp_mutex
);
1447 /* Returns the state of port 'p'. */
1449 rstp_port_get_state(const struct rstp_port
*p
)
1450 OVS_EXCLUDED(rstp_mutex
)
1452 enum rstp_state state
;
1454 ovs_mutex_lock(&rstp_mutex
);
1455 state
= p
->rstp_state
;
1456 ovs_mutex_unlock(&rstp_mutex
);
1461 /* Retrieves port status. */
1463 rstp_port_get_status(const struct rstp_port
*p
, uint16_t *id
,
1464 enum rstp_state
*state
, enum rstp_port_role
*role
,
1465 rstp_identifier
*designated_bridge_id
,
1466 uint16_t *designated_port_id
,
1467 uint32_t *designated_path_cost
, int *tx_count
,
1468 int *rx_count
, int *error_count
, int *uptime
)
1469 OVS_EXCLUDED(rstp_mutex
)
1471 ovs_mutex_lock(&rstp_mutex
);
1473 *state
= p
->rstp_state
;
1476 *designated_bridge_id
= p
->port_priority
.designated_bridge_id
;
1477 *designated_port_id
= p
->port_priority
.designated_port_id
;
1478 *designated_path_cost
= p
->port_priority
.root_path_cost
;
1480 *tx_count
= p
->tx_count
;
1481 *rx_count
= p
->rx_rstp_bpdu_cnt
;
1482 *error_count
= p
->error_count
;
1483 *uptime
= p
->uptime
;
1484 ovs_mutex_unlock(&rstp_mutex
);
1488 rstp_port_set(struct rstp_port
*port
, uint16_t port_num
, int priority
,
1489 uint32_t path_cost
, bool is_admin_edge
, bool is_auto_edge
,
1490 enum rstp_admin_point_to_point_mac_state admin_p2p_mac_state
,
1491 bool admin_port_state
, bool do_mcheck
, void *aux
,
1493 OVS_EXCLUDED(rstp_mutex
)
1495 ovs_mutex_lock(&rstp_mutex
);
1497 rstp_port_set_priority__(port
, priority
);
1498 rstp_port_set_port_number__(port
, port_num
);
1499 rstp_port_set_port_name__(port
, name
);
1500 rstp_port_set_path_cost__(port
, path_cost
);
1501 rstp_port_set_admin_edge__(port
, is_admin_edge
);
1502 rstp_port_set_auto_edge__(port
, is_auto_edge
);
1503 rstp_port_set_admin_point_to_point_mac__(port
, admin_p2p_mac_state
);
1504 rstp_port_set_administrative_bridge_port__(port
, admin_port_state
, false);
1505 rstp_port_set_mcheck__(port
, do_mcheck
);
1506 ovs_mutex_unlock(&rstp_mutex
);
1509 /* Individual setters only used by test-rstp.c. */
1511 rstp_port_set_priority(struct rstp_port
*port
, int priority
)
1512 OVS_EXCLUDED(rstp_mutex
)
1514 ovs_mutex_lock(&rstp_mutex
);
1515 rstp_port_set_priority__(port
, priority
);
1516 ovs_mutex_unlock(&rstp_mutex
);
1520 rstp_port_set_path_cost(struct rstp_port
*port
, uint32_t path_cost
)
1521 OVS_EXCLUDED(rstp_mutex
)
1523 ovs_mutex_lock(&rstp_mutex
);
1524 rstp_port_set_path_cost__(port
, path_cost
);
1525 ovs_mutex_unlock(&rstp_mutex
);
1529 rstp_port_set_aux(struct rstp_port
*port
, void *aux
)
1530 OVS_EXCLUDED(rstp_mutex
)
1532 ovs_mutex_lock(&rstp_mutex
);
1534 ovs_mutex_unlock(&rstp_mutex
);
1538 static struct rstp
*
1539 rstp_find(const char *name
)
1540 OVS_REQUIRES(rstp_mutex
)
1544 LIST_FOR_EACH (rstp
, node
, all_rstps
) {
1545 if (!strcmp(rstp
->name
, name
)) {
1553 rstp_unixctl_tcn(struct unixctl_conn
*conn
, int argc
,
1554 const char *argv
[], void *aux OVS_UNUSED
)
1555 OVS_EXCLUDED(rstp_mutex
)
1557 ovs_mutex_lock(&rstp_mutex
);
1559 struct rstp
*rstp
= rstp_find(argv
[1]);
1561 unixctl_command_reply_error(conn
, "No such RSTP object");
1564 rstp
->changes
= true;
1568 LIST_FOR_EACH (rstp
, node
, all_rstps
) {
1569 rstp
->changes
= true;
1573 unixctl_command_reply(conn
, "OK");
1576 ovs_mutex_unlock(&rstp_mutex
);
1580 rstp_bridge_id_details(struct ds
*ds
, const rstp_identifier bridge_id
,
1581 uint16_t hello_time
, uint16_t max_age
,
1582 uint16_t forward_delay
)
1583 OVS_REQUIRES(rstp_mutex
)
1585 uint16_t priority
= bridge_id
>> 48;
1586 ds_put_format(ds
, " stp-priority %"PRIu16
"\n", priority
);
1588 struct eth_addr mac
;
1589 const uint64_t mac_bits
= (UINT64_C(1) << 48) - 1;
1590 eth_addr_from_uint64(bridge_id
& mac_bits
, &mac
);
1591 ds_put_format(ds
, " stp-system-id "ETH_ADDR_FMT
"\n", ETH_ADDR_ARGS(mac
));
1592 ds_put_format(ds
, " stp-hello-time %"PRIu16
"s\n", hello_time
);
1593 ds_put_format(ds
, " stp-max-age %"PRIu16
"s\n", max_age
);
1594 ds_put_format(ds
, " stp-fwd-delay %"PRIu16
"s\n", forward_delay
);
1598 rstp_print_details(struct ds
*ds
, const struct rstp
*rstp
)
1599 OVS_REQUIRES(rstp_mutex
)
1601 ds_put_format(ds
, "---- %s ----\n", rstp
->name
);
1603 ds_put_cstr(ds
, "Root ID:\n");
1604 if (rstp_is_root_bridge__(rstp
)) {
1605 rstp_bridge_id_details(ds
, rstp
->bridge_identifier
,
1606 rstp
->bridge_hello_time
,
1607 rstp
->bridge_max_age
,
1608 rstp
->bridge_forward_delay
);
1609 ds_put_cstr(ds
, " This bridge is the root\n");
1611 struct rstp_port
*root_port
= rstp_get_root_port__(rstp
);
1613 ds_put_cstr(ds
, "unknown root port\n");
1617 rstp_bridge_id_details(ds
, rstp_get_root_id__(rstp
),
1618 root_port
->designated_times
.hello_time
,
1619 root_port
->designated_times
.max_age
,
1620 root_port
->designated_times
.forward_delay
);
1621 ds_put_format(ds
, " root-port %s\n", root_port
->port_name
);
1622 ds_put_format(ds
, " root-path-cost %u\n",
1623 rstp_get_root_path_cost__(rstp
));
1625 ds_put_cstr(ds
, "\n");
1627 ds_put_cstr(ds
, "Bridge ID:\n");
1628 rstp_bridge_id_details(ds
, rstp
->bridge_identifier
,
1629 rstp
->bridge_hello_time
,
1630 rstp
->bridge_max_age
,
1631 rstp
->bridge_forward_delay
);
1632 ds_put_cstr(ds
, "\n");
1634 ds_put_format(ds
, " %-11.10s%-11.10s%-11.10s%-9.8s%-8.7s\n",
1635 "Interface", "Role", "State", "Cost", "Pri.Nbr");
1636 ds_put_cstr(ds
, " ---------- ---------- ---------- -------- -------\n");
1638 struct rstp_port
*p
;
1639 HMAP_FOR_EACH (p
, node
, &rstp
->ports
) {
1640 if (p
->rstp_state
!= RSTP_DISABLED
) {
1641 ds_put_format(ds
, " %-11.10s",
1642 p
->port_name
? p
->port_name
: "null");
1643 ds_put_format(ds
, "%-11.10s", rstp_port_role_name(p
->role
));
1644 ds_put_format(ds
, "%-11.10s", rstp_state_name(p
->rstp_state
));
1645 ds_put_format(ds
, "%-9d", p
->port_path_cost
);
1646 ds_put_format(ds
, "%d.%d\n", p
->priority
, p
->port_number
);
1650 ds_put_cstr(ds
, "\n");
1654 rstp_unixctl_show(struct unixctl_conn
*conn
, int argc
,
1655 const char *argv
[], void *aux OVS_UNUSED
)
1656 OVS_EXCLUDED(rstp_mutex
)
1658 struct ds ds
= DS_EMPTY_INITIALIZER
;
1660 ovs_mutex_lock(&rstp_mutex
);
1662 struct rstp
*rstp
= rstp_find(argv
[1]);
1665 unixctl_command_reply_error(conn
, "No such RSTP object");
1669 rstp_print_details(&ds
, rstp
);
1673 LIST_FOR_EACH (rstp
, node
, all_rstps
) {
1674 rstp_print_details(&ds
, rstp
);
1678 unixctl_command_reply(conn
, ds_cstr(&ds
));
1682 ovs_mutex_unlock(&rstp_mutex
);