2 * Copyright (c) 2011-2014 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>
24 * References to IEEE 802.1D-2004 standard are enclosed in square brackets.
25 * E.g. [17.3], [Table 17-1], etc.
32 #include "rstp-common.h"
33 #include "rstp-state-machines.h"
34 #include <arpa/inet.h>
36 #include <netinet/in.h>
38 #include <sys/types.h>
39 #include "byte-order.h"
40 #include "connectivity.h"
42 #include "ofproto/ofproto.h"
49 VLOG_DEFINE_THIS_MODULE(rstp);
51 struct ovs_mutex rstp_mutex;
53 static struct list all_rstps__ = LIST_INITIALIZER(&all_rstps__);
54 static struct list *const all_rstps OVS_GUARDED_BY(rstp_mutex) = &all_rstps__;
56 /* Internal use only. */
57 static void rstp_set_bridge_address__(struct rstp *, rstp_identifier)
58 OVS_REQUIRES(rstp_mutex);
59 static void rstp_set_bridge_priority__(struct rstp *, int new_priority)
60 OVS_REQUIRES(rstp_mutex);
61 static void rstp_set_bridge_ageing_time__(struct rstp *, int new_ageing_time)
62 OVS_REQUIRES(rstp_mutex);
63 static void rstp_set_bridge_force_protocol_version__(struct rstp *,
64 enum rstp_force_protocol_version)
65 OVS_REQUIRES(rstp_mutex);
66 static void rstp_set_bridge_hello_time__(struct rstp *)
67 OVS_REQUIRES(rstp_mutex);
68 static void rstp_set_bridge_max_age__(struct rstp *, int new_max_age)
69 OVS_REQUIRES(rstp_mutex);
70 static void rstp_set_bridge_forward_delay__(struct rstp *, int new_forward_delay)
71 OVS_REQUIRES(rstp_mutex);
72 static void rstp_set_bridge_transmit_hold_count__(struct rstp *,
73 int new_transmit_hold_count)
74 OVS_REQUIRES(rstp_mutex);
75 static void rstp_set_bridge_migrate_time__(struct rstp *)
76 OVS_REQUIRES(rstp_mutex);
77 static void rstp_set_bridge_times__(struct rstp *, int new_forward_delay,
78 int new_hello_time, int new_max_age,
80 OVS_REQUIRES(rstp_mutex);
82 static struct rstp_port *rstp_get_port__(struct rstp *rstp,
84 OVS_REQUIRES(rstp_mutex);
85 static void set_port_id__(struct rstp_port *)
86 OVS_REQUIRES(rstp_mutex);
87 static void update_port_enabled__(struct rstp_port *)
88 OVS_REQUIRES(rstp_mutex);
89 static void set_bridge_priority__(struct rstp *)
90 OVS_REQUIRES(rstp_mutex);
91 static void reinitialize_rstp__(struct rstp *)
92 OVS_REQUIRES(rstp_mutex);
93 static bool is_port_number_available__(struct rstp *, int, struct rstp_port *)
94 OVS_REQUIRES(rstp_mutex);
95 static uint16_t rstp_first_free_number__(struct rstp *, struct rstp_port *)
96 OVS_REQUIRES(rstp_mutex);
97 static void rstp_initialize_port_defaults__(struct rstp_port *)
98 OVS_REQUIRES(rstp_mutex);
99 static void rstp_port_set_priority__(struct rstp_port *, int priority)
100 OVS_REQUIRES(rstp_mutex);
101 static void rstp_port_set_port_number__(struct rstp_port *,
102 uint16_t port_number)
103 OVS_REQUIRES(rstp_mutex);
104 static void rstp_port_set_path_cost__(struct rstp_port *, uint32_t path_cost)
105 OVS_REQUIRES(rstp_mutex);
106 static void rstp_port_set_administrative_bridge_port__(struct rstp_port *,
107 uint8_t admin_port_state)
108 OVS_REQUIRES(rstp_mutex);
109 static void rstp_port_set_admin_edge__(struct rstp_port *, bool admin_edge)
110 OVS_REQUIRES(rstp_mutex);
111 static void rstp_port_set_auto_edge__(struct rstp_port *, bool auto_edge)
112 OVS_REQUIRES(rstp_mutex);
113 static void rstp_port_set_mcheck__(struct rstp_port *, bool mcheck)
114 OVS_REQUIRES(rstp_mutex);
115 static void reinitialize_port__(struct rstp_port *p)
116 OVS_REQUIRES(rstp_mutex);
119 rstp_state_name(enum rstp_state state)
126 case RSTP_FORWARDING:
128 case RSTP_DISCARDING:
136 rstp_port_role_name(enum rstp_port_role role)
141 case ROLE_DESIGNATED:
154 /* Caller has to hold a reference to prevent 'rstp' from being deleted
155 * while taking a new reference. */
157 rstp_ref(struct rstp *rstp)
158 OVS_EXCLUDED(rstp_mutex)
161 ovs_refcount_ref(&rstp->ref_cnt);
166 /* Frees RSTP struct when reference count reaches zero. */
168 rstp_unref(struct rstp *rstp)
169 OVS_EXCLUDED(rstp_mutex)
171 if (rstp && ovs_refcount_unref(&rstp->ref_cnt) == 1) {
172 ovs_mutex_lock(&rstp_mutex);
174 /* Each RSTP port points back to struct rstp without holding a
175 * reference for that pointer. This is OK as we never move
176 * ports from one bridge to another, and holders always
177 * release their ports before releasing the bridge. This
178 * means that there should be not ports at this time. */
179 ovs_assert(rstp->ports_count == 0);
181 list_remove(&rstp->node);
182 ovs_mutex_unlock(&rstp_mutex);
188 /* Returns the port number. Mutex is needed to guard against
189 * concurrent reinitialization (which can temporarily clear the
192 rstp_port_get_number(const struct rstp_port *p)
193 OVS_EXCLUDED(rstp_mutex)
197 ovs_mutex_lock(&rstp_mutex);
198 number = p->port_number;
199 ovs_mutex_unlock(&rstp_mutex);
204 static void rstp_unixctl_tcn(struct unixctl_conn *, int argc,
205 const char *argv[], void *aux);
207 /* Decrements the State Machines' timers. */
209 rstp_tick_timers(struct rstp *rstp)
210 OVS_EXCLUDED(rstp_mutex)
212 ovs_mutex_lock(&rstp_mutex);
213 decrease_rstp_port_timers__(rstp);
214 ovs_mutex_unlock(&rstp_mutex);
217 /* Processes an incoming BPDU. */
219 rstp_port_received_bpdu(struct rstp_port *rp, const void *bpdu,
221 OVS_EXCLUDED(rstp_mutex)
223 ovs_mutex_lock(&rstp_mutex);
224 /* Only process packets on ports that have RSTP enabled. */
225 if (rp && rp->rstp_state != RSTP_DISABLED) {
226 process_received_bpdu__(rp, bpdu, bpdu_size);
228 ovs_mutex_unlock(&rstp_mutex);
233 OVS_EXCLUDED(rstp_mutex)
235 ovs_mutex_init_recursive(&rstp_mutex);
237 unixctl_command_register("rstp/tcn", "[bridge]", 0, 1, rstp_unixctl_tcn,
241 /* Creates and returns a new RSTP instance that initially has no ports. */
243 rstp_create(const char *name, rstp_identifier bridge_address,
244 void (*send_bpdu)(struct ofpbuf *bpdu, int port_no, void *aux),
246 OVS_EXCLUDED(rstp_mutex)
250 VLOG_DBG("Creating RSTP instance");
252 rstp = xzalloc(sizeof *rstp);
253 rstp->name = xstrdup(name);
255 ovs_mutex_lock(&rstp_mutex);
256 /* Set bridge address. */
257 rstp_set_bridge_address__(rstp, bridge_address);
258 /* Set default parameters values. */
259 rstp_set_bridge_priority__(rstp, RSTP_DEFAULT_PRIORITY);
260 rstp_set_bridge_ageing_time__(rstp, RSTP_DEFAULT_AGEING_TIME);
261 rstp_set_bridge_force_protocol_version__(rstp, FPV_DEFAULT);
262 rstp_set_bridge_forward_delay__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
263 rstp_set_bridge_hello_time__(rstp);
264 rstp_set_bridge_max_age__(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
265 rstp_set_bridge_migrate_time__(rstp);
266 rstp_set_bridge_transmit_hold_count__(rstp,
267 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
268 rstp_set_bridge_times__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
269 RSTP_BRIDGE_HELLO_TIME,
270 RSTP_DEFAULT_BRIDGE_MAX_AGE, 0);
271 rstp->send_bpdu = send_bpdu;
273 rstp->changes = false;
276 /* Initialize the ports list. */
277 list_init(&rstp->ports);
278 ovs_refcount_init(&rstp->ref_cnt);
280 list_push_back(all_rstps, &rstp->node);
281 ovs_mutex_unlock(&rstp_mutex);
283 VLOG_DBG("RSTP instance creation done");
287 /* Called by rstp_set_bridge_address() and rstp_set_bridge_priority(),
288 * it updates the bridge priority vector according to the values passed by
292 set_bridge_priority__(struct rstp *rstp)
293 OVS_REQUIRES(rstp_mutex)
295 rstp->bridge_priority.root_bridge_id = rstp->bridge_identifier;
296 rstp->bridge_priority.designated_bridge_id = rstp->bridge_identifier;
297 VLOG_DBG("%s: new bridge identifier: "RSTP_ID_FMT"", rstp->name,
298 RSTP_ID_ARGS(rstp->bridge_identifier));
300 /* [17.13] When the bridge address changes, recalculates all priority
303 if (rstp->ports_count > 0) {
306 LIST_FOR_EACH (p, node, &rstp->ports) {
311 rstp->changes = true;
312 updt_roles_tree__(rstp);
315 /* Sets the bridge address. */
317 rstp_set_bridge_address__(struct rstp *rstp, rstp_identifier bridge_address)
318 OVS_REQUIRES(rstp_mutex)
320 VLOG_DBG("%s: set bridge address to: "RSTP_ID_FMT"", rstp->name,
321 RSTP_ID_ARGS(bridge_address));
323 rstp->address = bridge_address;
324 rstp->bridge_identifier = bridge_address;
325 set_bridge_priority__(rstp);
328 /* Sets the bridge address. */
330 rstp_set_bridge_address(struct rstp *rstp, rstp_identifier bridge_address)
331 OVS_EXCLUDED(rstp_mutex)
333 ovs_mutex_lock(&rstp_mutex);
334 rstp_set_bridge_address__(rstp, bridge_address);
335 ovs_mutex_unlock(&rstp_mutex);
339 rstp_get_name(const struct rstp *rstp)
340 OVS_EXCLUDED(rstp_mutex)
344 ovs_mutex_lock(&rstp_mutex);
346 ovs_mutex_unlock(&rstp_mutex);
351 rstp_get_bridge_id(const struct rstp *rstp)
352 OVS_EXCLUDED(rstp_mutex)
354 rstp_identifier bridge_id;
356 ovs_mutex_lock(&rstp_mutex);
357 bridge_id = rstp->bridge_identifier;
358 ovs_mutex_unlock(&rstp_mutex);
363 /* Sets the bridge priority. */
365 rstp_set_bridge_priority__(struct rstp *rstp, int new_priority)
366 OVS_REQUIRES(rstp_mutex)
368 new_priority = ROUND_DOWN(new_priority, RSTP_PRIORITY_STEP);
370 if (new_priority >= RSTP_MIN_PRIORITY
371 && new_priority <= RSTP_MAX_PRIORITY) {
372 VLOG_DBG("%s: set bridge priority to %d", rstp->name, new_priority);
374 rstp->priority = new_priority;
375 rstp->bridge_identifier &= 0x0000ffffffffffffULL;
376 rstp->bridge_identifier |= (uint64_t)new_priority << 48;
377 set_bridge_priority__(rstp);
382 rstp_set_bridge_priority(struct rstp *rstp, int new_priority)
383 OVS_EXCLUDED(rstp_mutex)
385 ovs_mutex_lock(&rstp_mutex);
386 rstp_set_bridge_priority__(rstp, new_priority);
387 ovs_mutex_unlock(&rstp_mutex);
390 /* Sets the bridge ageing time. */
392 rstp_set_bridge_ageing_time__(struct rstp *rstp, int new_ageing_time)
393 OVS_REQUIRES(rstp_mutex)
395 if (new_ageing_time >= RSTP_MIN_AGEING_TIME
396 && new_ageing_time <= RSTP_MAX_AGEING_TIME) {
397 VLOG_DBG("%s: set ageing time to %d", rstp->name, new_ageing_time);
399 rstp->ageing_time = new_ageing_time;
404 rstp_set_bridge_ageing_time(struct rstp *rstp, int new_ageing_time)
405 OVS_EXCLUDED(rstp_mutex)
407 ovs_mutex_lock(&rstp_mutex);
408 rstp_set_bridge_ageing_time__(rstp, new_ageing_time);
409 ovs_mutex_unlock(&rstp_mutex);
412 /* Reinitializes RSTP when switching from RSTP mode to STP mode
416 reinitialize_rstp__(struct rstp *rstp)
417 OVS_REQUIRES(rstp_mutex)
420 static struct list ports;
422 /* Copy rstp in temp */
426 /* stop and clear rstp */
427 memset(rstp, 0, sizeof(struct rstp));
429 /* Initialize rstp. */
430 rstp->name = temp.name;
431 /* Set bridge address. */
432 rstp_set_bridge_address__(rstp, temp.address);
433 /* Set default parameters values. */
434 rstp_set_bridge_priority__(rstp, RSTP_DEFAULT_PRIORITY);
435 rstp_set_bridge_ageing_time__(rstp, RSTP_DEFAULT_AGEING_TIME);
436 rstp_set_bridge_forward_delay__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
437 rstp_set_bridge_hello_time__(rstp);
438 rstp_set_bridge_max_age__(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
439 rstp_set_bridge_migrate_time__(rstp);
440 rstp_set_bridge_transmit_hold_count__(rstp,
441 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
442 rstp_set_bridge_times__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
443 RSTP_BRIDGE_HELLO_TIME,
444 RSTP_DEFAULT_BRIDGE_MAX_AGE, 0);
446 rstp->send_bpdu = temp.send_bpdu;
447 rstp->aux = temp.aux;
448 rstp->node = temp.node;
449 rstp->changes = false;
452 rstp->ports_count = temp.ports_count;
454 if (rstp->ports_count > 0) {
457 LIST_FOR_EACH (p, node, &rstp->ports) {
458 reinitialize_port__(p);
461 rstp->ref_cnt = temp.ref_cnt;
464 /* Sets the force protocol version parameter. */
466 rstp_set_bridge_force_protocol_version__(struct rstp *rstp,
467 enum rstp_force_protocol_version new_force_protocol_version)
468 OVS_REQUIRES(rstp_mutex)
470 if (new_force_protocol_version != rstp->force_protocol_version &&
471 (new_force_protocol_version == FPV_STP_COMPATIBILITY ||
472 new_force_protocol_version == FPV_DEFAULT)) {
473 VLOG_DBG("%s: set bridge Force Protocol Version to %d", rstp->name,
474 new_force_protocol_version);
476 /* [17.13] The Spanning Tree Protocol Entity shall be reinitialized,
477 * as specified by the assertion of BEGIN (17.18.1) in the state
478 * machine specification.
480 reinitialize_rstp__(rstp);
481 rstp->force_protocol_version = new_force_protocol_version;
482 if (rstp->force_protocol_version < 2) {
483 rstp->stp_version = true;
484 rstp->rstp_version = false;
486 rstp->stp_version = false;
487 rstp->rstp_version = true;
489 rstp->changes = true;
495 rstp_set_bridge_force_protocol_version(struct rstp *rstp,
496 enum rstp_force_protocol_version new_force_protocol_version)
497 OVS_EXCLUDED(rstp_mutex)
499 ovs_mutex_lock(&rstp_mutex);
500 rstp_set_bridge_force_protocol_version__(rstp, new_force_protocol_version);
501 ovs_mutex_unlock(&rstp_mutex);
504 /* Sets the bridge Hello Time parameter. */
506 rstp_set_bridge_hello_time__(struct rstp *rstp)
507 OVS_REQUIRES(rstp_mutex)
509 VLOG_DBG("%s: set RSTP Hello Time to %d", rstp->name,
510 RSTP_BRIDGE_HELLO_TIME);
511 /* 2 is the only acceptable value. */
512 rstp->bridge_hello_time = RSTP_BRIDGE_HELLO_TIME;
515 /* Sets the bridge max age parameter. */
517 rstp_set_bridge_max_age__(struct rstp *rstp, int new_max_age)
518 OVS_REQUIRES(rstp_mutex)
520 if (new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE &&
521 new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) {
523 if ((2 * (rstp->bridge_forward_delay - 1) >= new_max_age)
524 && (new_max_age >= 2 * rstp->bridge_hello_time)) {
525 VLOG_DBG("%s: set RSTP bridge Max Age to %d", rstp->name,
528 rstp->bridge_max_age = new_max_age;
529 rstp->bridge_times.max_age = new_max_age;
535 rstp_set_bridge_max_age(struct rstp *rstp, int new_max_age)
536 OVS_EXCLUDED(rstp_mutex)
538 ovs_mutex_lock(&rstp_mutex);
539 rstp_set_bridge_max_age__(rstp, new_max_age);
540 ovs_mutex_unlock(&rstp_mutex);
543 /* Sets the bridge forward delay parameter. */
545 rstp_set_bridge_forward_delay__(struct rstp *rstp, int new_forward_delay)
546 OVS_REQUIRES(rstp_mutex)
548 if (new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY
549 && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) {
550 if (2 * (new_forward_delay - 1) >= rstp->bridge_max_age) {
551 VLOG_DBG("%s: set RSTP Forward Delay to %d", rstp->name,
553 rstp->bridge_forward_delay = new_forward_delay;
554 rstp->bridge_times.forward_delay = new_forward_delay;
560 rstp_set_bridge_forward_delay(struct rstp *rstp, int new_forward_delay)
561 OVS_EXCLUDED(rstp_mutex)
563 ovs_mutex_lock(&rstp_mutex);
564 rstp_set_bridge_forward_delay__(rstp, new_forward_delay);
565 ovs_mutex_unlock(&rstp_mutex);
568 /* Sets the bridge transmit hold count parameter. */
570 rstp_set_bridge_transmit_hold_count__(struct rstp *rstp,
571 int new_transmit_hold_count)
572 OVS_REQUIRES(rstp_mutex)
576 if (new_transmit_hold_count >= RSTP_MIN_TRANSMIT_HOLD_COUNT
577 && new_transmit_hold_count <= RSTP_MAX_TRANSMIT_HOLD_COUNT) {
578 VLOG_DBG("%s: set RSTP Transmit Hold Count to %d", rstp->name,
579 new_transmit_hold_count);
580 /* Resetting txCount on all ports [17.13]. */
582 rstp->transmit_hold_count = new_transmit_hold_count;
583 if (rstp->ports_count > 0) {
584 LIST_FOR_EACH (p, node, &rstp->ports) {
592 rstp_set_bridge_transmit_hold_count(struct rstp *rstp,
593 int new_transmit_hold_count)
594 OVS_EXCLUDED(rstp_mutex)
596 ovs_mutex_lock(&rstp_mutex);
597 rstp_set_bridge_transmit_hold_count__(rstp, new_transmit_hold_count);
598 ovs_mutex_unlock(&rstp_mutex);
601 /* Sets the bridge migrate time parameter. */
603 rstp_set_bridge_migrate_time__(struct rstp *rstp)
604 OVS_REQUIRES(rstp_mutex)
606 VLOG_DBG("%s: set RSTP Migrate Time to %d", rstp->name,
608 /* 3 is the only acceptable value */
609 rstp->migrate_time = RSTP_MIGRATE_TIME;
612 /* Sets the bridge times. */
614 rstp_set_bridge_times__(struct rstp *rstp, int new_forward_delay,
615 int new_hello_time, int new_max_age,
617 OVS_REQUIRES(rstp_mutex)
619 VLOG_DBG("%s: set RSTP times to (%d, %d, %d, %d)", rstp->name,
620 new_forward_delay, new_hello_time, new_max_age, new_message_age);
621 if (new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY
622 && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) {
623 rstp->bridge_times.forward_delay = new_forward_delay;
625 if (new_hello_time == RSTP_BRIDGE_HELLO_TIME) {
626 rstp->bridge_times.hello_time = new_hello_time;
628 if (new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE
629 && new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) {
630 rstp->bridge_times.max_age = new_max_age;
632 rstp->bridge_times.message_age = new_message_age;
635 /* Sets the port id, it is called by rstp_port_set_port_number__() or
636 * rstp_port_set_priority__().
639 set_port_id__(struct rstp_port *p)
640 OVS_REQUIRES(rstp_mutex)
645 /* [9.2.7] Port identifier. */
646 p->port_id = p->port_number | (p->priority << 8);
647 VLOG_DBG("%s: new RSTP port id "RSTP_PORT_ID_FMT"", rstp->name,
651 /* Sets the port priority. */
653 rstp_port_set_priority__(struct rstp_port *port, int priority)
654 OVS_REQUIRES(rstp_mutex)
656 if (priority >= RSTP_MIN_PORT_PRIORITY
657 && priority <= RSTP_MAX_PORT_PRIORITY) {
658 VLOG_DBG("%s, port %u: set RSTP port priority to %d", port->rstp->name,
659 port->port_number, priority);
661 priority -= priority % RSTP_STEP_PORT_PRIORITY;
662 port->priority = priority;
664 port->selected = false;
665 port->reselect = true;
669 /* Checks if a port number is available. */
671 is_port_number_available__(struct rstp *rstp, int n, struct rstp_port *port)
672 OVS_REQUIRES(rstp_mutex)
674 if (n >= 1 && n <= RSTP_MAX_PORTS) {
675 struct rstp_port *p = rstp_get_port__(rstp, n);
677 return p == NULL || p == port;
683 rstp_first_free_number__(struct rstp *rstp, struct rstp_port *rstp_port)
684 OVS_REQUIRES(rstp_mutex)
688 while (free_number <= RSTP_MAX_PORTS) {
689 if (is_port_number_available__(rstp, free_number, rstp_port)) {
694 VLOG_DBG("%s, No free port number available.", rstp->name);
698 /* Sets the port number. */
700 rstp_port_set_port_number__(struct rstp_port *port, uint16_t port_number)
701 OVS_REQUIRES(rstp_mutex)
703 /* If new_port_number is available, use it, otherwise use the first free
704 * available port number. */
706 is_port_number_available__(port->rstp, port_number, port)
708 : rstp_first_free_number__(port->rstp, port);
711 /* [17.13] is not clear. I suppose that a port number change
712 * should trigger reselection like a port priority change. */
713 port->selected = false;
714 port->reselect = true;
716 VLOG_DBG("%s: set new RSTP port number %d", port->rstp->name,
720 /* Converts the link speed to a port path cost [Table 17-3]. */
722 rstp_convert_speed_to_cost(unsigned int speed)
726 value = speed >= 10000000 ? 2 /* 10 Tb/s. */
727 : speed >= 1000000 ? 20 /* 1 Tb/s. */
728 : speed >= 100000 ? 200 /* 100 Gb/s. */
729 : speed >= 10000 ? 2000 /* 10 Gb/s. */
730 : speed >= 1000 ? 20000 /* 1 Gb/s. */
731 : speed >= 100 ? 200000 /* 100 Mb/s. */
732 : speed >= 10 ? 2000000 /* 10 Mb/s. */
733 : speed >= 1 ? 20000000 /* 1 Mb/s. */
734 : RSTP_DEFAULT_PORT_PATH_COST; /* 100 Mb/s. */
739 /* Sets the port path cost. */
741 rstp_port_set_path_cost__(struct rstp_port *port, uint32_t path_cost)
742 OVS_REQUIRES(rstp_mutex)
744 if (path_cost >= RSTP_MIN_PORT_PATH_COST
745 && path_cost <= RSTP_MAX_PORT_PATH_COST) {
746 VLOG_DBG("%s, port %u, set RSTP port path cost to %d",
747 port->rstp->name, port->port_number, path_cost);
749 port->port_path_cost = path_cost;
750 port->selected = false;
751 port->reselect = true;
755 /* Gets the root path cost. */
757 rstp_get_root_path_cost(const struct rstp *rstp)
758 OVS_EXCLUDED(rstp_mutex)
762 ovs_mutex_lock(&rstp_mutex);
763 cost = rstp->root_priority.root_path_cost;
764 ovs_mutex_unlock(&rstp_mutex);
768 /* Returns true if something has happened to 'rstp' which necessitates
769 * flushing the client's MAC learning table.
772 rstp_check_and_reset_fdb_flush(struct rstp *rstp)
773 OVS_EXCLUDED(rstp_mutex)
780 ovs_mutex_lock(&rstp_mutex);
781 if (rstp->ports_count > 0){
782 LIST_FOR_EACH (p, node, &rstp->ports) {
785 /* fdb_flush should be reset by the filtering database
786 * once the entries are removed if rstp_version is TRUE, and
787 * immediately if stp_version is TRUE.*/
788 p->fdb_flush = false;
792 ovs_mutex_unlock(&rstp_mutex);
796 /* Finds a port whose state has changed, and returns the aux pointer set for
797 * the port. A NULL pointer is returned when no changed port is found. On
798 * return '*portp' contains the pointer to the rstp port that changed, or NULL
799 * if no changed port can be found.
801 * If '*portp' is passed as non-NULL, it must be the value set by the last
802 * invocation of this function.
804 * This function may only be called by the thread that creates and deletes
805 * ports. Otherwise this function is not thread safe, as the returned
806 * '*portp' could become stale before it is used in the next invocation. */
808 rstp_get_next_changed_port_aux(struct rstp *rstp, struct rstp_port **portp)
812 ovs_mutex_lock(&rstp_mutex);
813 if (*portp == NULL) {
816 LIST_FOR_EACH (p, node, &rstp->ports) {
817 if (p->state_changed) {
818 p->state_changed = false;
824 } else { /* continue */
825 struct rstp_port *p = *portp;
827 LIST_FOR_EACH_CONTINUE (p, node, &rstp->ports) {
828 if (p->state_changed) {
829 p->state_changed = false;
836 /* No changed port found. */
839 ovs_mutex_unlock(&rstp_mutex);
843 /* Returns the port in 'rstp' with number 'port_number'.
845 * XXX: May only be called while concurrent deletion of ports is excluded. */
846 static struct rstp_port *
847 rstp_get_port__(struct rstp *rstp, uint16_t port_number)
848 OVS_REQUIRES(rstp_mutex)
850 struct rstp_port *port;
852 ovs_assert(rstp && port_number > 0 && port_number <= RSTP_MAX_PORTS);
854 if (rstp->ports_count > 0) {
855 LIST_FOR_EACH (port, node, &rstp->ports) {
856 if (port->port_number == port_number) {
865 rstp_get_port(struct rstp *rstp, uint16_t port_number)
866 OVS_EXCLUDED(rstp_mutex)
870 ovs_mutex_lock(&rstp_mutex);
871 p = rstp_get_port__(rstp, port_number);
872 ovs_mutex_unlock(&rstp_mutex);
877 rstp_get_port_aux(struct rstp *rstp, uint16_t port_number)
878 OVS_EXCLUDED(rstp_mutex)
883 ovs_mutex_lock(&rstp_mutex);
884 p = rstp_get_port__(rstp, port_number);
886 ovs_mutex_unlock(&rstp_mutex);
890 /* Updates the port_enabled parameter. */
892 update_port_enabled__(struct rstp_port *p)
893 OVS_REQUIRES(rstp_mutex)
895 if (p->mac_operational && p->is_administrative_bridge_port
896 == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) {
897 p->port_enabled = true;
899 p->port_enabled = false;
903 /* Sets the port MAC_Operational parameter [6.4.2]. */
905 rstp_port_set_mac_operational(struct rstp_port *p, bool new_mac_operational)
906 OVS_EXCLUDED(rstp_mutex)
910 ovs_mutex_lock(&rstp_mutex);
912 if (p->mac_operational != new_mac_operational) {
913 p->mac_operational = new_mac_operational;
914 update_port_enabled__(p);
915 rstp->changes = true;
918 ovs_mutex_unlock(&rstp_mutex);
921 /* Sets the port Administrative Bridge Port parameter. */
923 rstp_port_set_administrative_bridge_port__(struct rstp_port *p,
924 uint8_t admin_port_state)
925 OVS_REQUIRES(rstp_mutex)
927 if (admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_DISABLED
928 || admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) {
930 p->is_administrative_bridge_port = admin_port_state;
931 update_port_enabled__(p);
935 /* Sets the port oper_point_to_point_mac parameter. */
937 rstp_port_set_oper_point_to_point_mac__(struct rstp_port *p,
938 uint8_t new_oper_p2p_mac)
939 OVS_REQUIRES(rstp_mutex)
941 if (new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_DISABLED
942 || new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_ENABLED) {
944 p->oper_point_to_point_mac = new_oper_p2p_mac;
945 update_port_enabled__(p);
949 /* Initializes a port with the defaults values for its parameters. */
951 rstp_initialize_port_defaults__(struct rstp_port *p)
952 OVS_REQUIRES(rstp_mutex)
954 rstp_port_set_administrative_bridge_port__(p,
955 RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED);
956 rstp_port_set_oper_point_to_point_mac__(p, 1);
957 rstp_port_set_path_cost__(p, RSTP_DEFAULT_PORT_PATH_COST);
958 rstp_port_set_admin_edge__(p, false);
959 rstp_port_set_auto_edge__(p, true);
960 rstp_port_set_mcheck__(p, false);
962 /* Initialize state machines. */
963 p->port_receive_sm_state = PORT_RECEIVE_SM_INIT;
964 p->port_protocol_migration_sm_state = PORT_PROTOCOL_MIGRATION_SM_INIT;
965 p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_INIT;
966 p->port_transmit_sm_state = PORT_TRANSMIT_SM_INIT;
967 p->port_information_sm_state = PORT_INFORMATION_SM_INIT;
968 p->port_role_transition_sm_state = PORT_ROLE_TRANSITION_SM_INIT;
969 p->port_state_transition_sm_state = PORT_STATE_TRANSITION_SM_INIT;
970 p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INIT;
976 reinitialize_port__(struct rstp_port *p)
977 OVS_REQUIRES(rstp_mutex)
979 struct rstp_port temp_port;
984 memset(p, 0, sizeof(struct rstp_port));
986 p->ref_cnt = temp_port.ref_cnt;
988 p->node = temp_port.node;
989 p->aux = temp_port.aux;
990 p->port_number = temp_port.port_number;
991 p->port_priority = temp_port.port_priority;
992 p->port_id = temp_port.port_id;
993 p->rstp_state = RSTP_DISCARDING;
995 rstp_initialize_port_defaults__(p);
997 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" reinitialized.", rstp->name,
1002 reinitialize_port(struct rstp_port *p)
1003 OVS_EXCLUDED(rstp_mutex)
1005 ovs_mutex_lock(&rstp_mutex);
1006 reinitialize_port__(p);
1007 ovs_mutex_unlock(&rstp_mutex);
1010 /* Sets the port state. */
1012 rstp_port_set_state__(struct rstp_port *p, enum rstp_state state)
1013 OVS_REQUIRES(rstp_mutex)
1018 VLOG_DBG("%s, port %u: set RSTP port state %s -> %s", rstp->name,
1020 rstp_state_name(p->rstp_state), rstp_state_name(state));
1022 if (state != p->rstp_state && !p->state_changed) {
1023 p->state_changed = true;
1024 seq_change(connectivity_seq_get());
1026 p->rstp_state = state;
1030 rstp_port_set_state(struct rstp_port *p, enum rstp_state state)
1031 OVS_EXCLUDED(rstp_mutex)
1033 ovs_mutex_lock(&rstp_mutex);
1034 rstp_port_set_state__(p, state);
1035 ovs_mutex_unlock(&rstp_mutex);
1038 /* Adds a RSTP port. */
1040 rstp_add_port(struct rstp *rstp)
1041 OVS_EXCLUDED(rstp_mutex)
1043 struct rstp_port *p = xzalloc(sizeof *p);
1045 ovs_refcount_init(&p->ref_cnt);
1047 ovs_mutex_lock(&rstp_mutex);
1049 rstp_port_set_priority__(p, RSTP_DEFAULT_PORT_PRIORITY);
1050 rstp_port_set_port_number__(p, 0);
1052 rstp_initialize_port_defaults__(p);
1053 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" initialized.", rstp->name,
1056 rstp_port_set_state__(p, RSTP_DISCARDING);
1057 list_push_back(&rstp->ports, &p->node);
1058 rstp->ports_count++;
1059 rstp->changes = true;
1061 VLOG_DBG("%s: added port "RSTP_PORT_ID_FMT"", rstp->name, p->port_id);
1062 ovs_mutex_unlock(&rstp_mutex);
1066 /* Caller has to hold a reference to prevent 'rstp_port' from being deleted
1067 * while taking a new reference. */
1069 rstp_port_ref(const struct rstp_port *rp_)
1070 OVS_EXCLUDED(rstp_mutex)
1072 struct rstp_port *rp = CONST_CAST(struct rstp_port *, rp_);
1075 ovs_refcount_ref(&rp->ref_cnt);
1080 /* Frees RSTP struct. This can be caller by any thread. */
1082 rstp_port_unref(struct rstp_port *rp)
1083 OVS_EXCLUDED(rstp_mutex)
1085 if (rp && ovs_refcount_unref(&rp->ref_cnt) == 1) {
1088 ovs_mutex_lock(&rstp_mutex);
1090 rstp_port_set_state__(rp, RSTP_DISABLED);
1091 list_remove(&rp->node);
1092 rstp->ports_count--;
1093 VLOG_DBG("%s: removed port "RSTP_PORT_ID_FMT"", rstp->name, rp->port_id);
1094 ovs_mutex_unlock(&rstp_mutex);
1099 /* Sets the port Admin Edge parameter. */
1101 rstp_port_set_admin_edge__(struct rstp_port *port, bool admin_edge)
1102 OVS_REQUIRES(rstp_mutex)
1104 if (port->admin_edge != admin_edge) {
1105 VLOG_DBG("%s, port %u: set RSTP Admin Edge to %d", port->rstp->name,
1106 port->port_number, admin_edge);
1108 port->admin_edge = admin_edge;
1112 /* Sets the port Auto Edge parameter. */
1114 rstp_port_set_auto_edge__(struct rstp_port *port, bool auto_edge)
1115 OVS_REQUIRES(rstp_mutex)
1117 if (port->auto_edge != auto_edge) {
1118 VLOG_DBG("%s, port %u: set RSTP Auto Edge to %d", port->rstp->name,
1119 port->port_number, auto_edge);
1121 port->auto_edge = auto_edge;
1125 /* Sets the port mcheck parameter.
1126 * [17.19.13] May be set by management to force the Port Protocol Migration
1127 * state machine to transmit RST BPDUs for a MigrateTime (17.13.9) period, to
1128 * test whether all STP Bridges (17.4) on the attached LAN have been removed
1129 * and the Port can continue to transmit RSTP BPDUs. Setting mcheck has no
1130 * effect if stpVersion (17.20.12) is TRUE, i.e., the Bridge is operating in
1131 * STP Compatibility mode.
1134 rstp_port_set_mcheck__(struct rstp_port *port, bool mcheck)
1135 OVS_REQUIRES(rstp_mutex)
1137 if (mcheck == true && port->rstp->force_protocol_version >= 2) {
1138 port->mcheck = true;
1140 VLOG_DBG("%s, port %u: set RSTP mcheck to %d", port->rstp->name,
1141 port->port_number, mcheck);
1145 /* Returns the designated bridge id. */
1147 rstp_get_designated_id(const struct rstp *rstp)
1148 OVS_EXCLUDED(rstp_mutex)
1150 rstp_identifier designated_id;
1152 ovs_mutex_lock(&rstp_mutex);
1153 designated_id = rstp->root_priority.designated_bridge_id;
1154 ovs_mutex_unlock(&rstp_mutex);
1156 return designated_id;
1159 /* Returns the root bridge id. */
1161 rstp_get_root_id(const struct rstp *rstp)
1162 OVS_EXCLUDED(rstp_mutex)
1164 rstp_identifier root_id;
1166 ovs_mutex_lock(&rstp_mutex);
1167 root_id = rstp->root_priority.root_bridge_id;
1168 ovs_mutex_unlock(&rstp_mutex);
1173 /* Returns the designated port id. */
1175 rstp_get_designated_port_id(const struct rstp *rstp)
1176 OVS_EXCLUDED(rstp_mutex)
1178 uint16_t designated_port_id;
1180 ovs_mutex_lock(&rstp_mutex);
1181 designated_port_id = rstp->root_priority.designated_port_id;
1182 ovs_mutex_unlock(&rstp_mutex);
1184 return designated_port_id;
1187 /* Return the bridge port id. */
1189 rstp_get_bridge_port_id(const struct rstp *rstp)
1190 OVS_EXCLUDED(rstp_mutex)
1192 uint16_t bridge_port_id;
1194 ovs_mutex_lock(&rstp_mutex);
1195 bridge_port_id = rstp->root_priority.bridge_port_id;
1196 ovs_mutex_unlock(&rstp_mutex);
1198 return bridge_port_id;
1201 /* Returns true if the bridge believes to the be root of the spanning tree,
1205 rstp_is_root_bridge(const struct rstp *rstp)
1206 OVS_EXCLUDED(rstp_mutex)
1210 ovs_mutex_lock(&rstp_mutex);
1211 is_root = rstp->bridge_identifier ==
1212 rstp->root_priority.designated_bridge_id;
1213 ovs_mutex_unlock(&rstp_mutex);
1218 /* Returns the bridge ID of the bridge currently believed to be the root. */
1220 rstp_get_designated_root(const struct rstp *rstp)
1221 OVS_EXCLUDED(rstp_mutex)
1223 rstp_identifier designated_root;
1225 ovs_mutex_lock(&rstp_mutex);
1226 designated_root = rstp->root_priority.designated_bridge_id;
1227 ovs_mutex_unlock(&rstp_mutex);
1229 return designated_root;
1232 /* Returns the port connecting 'rstp' to the root bridge, or a null pointer if
1233 * there is no such port.
1236 rstp_get_root_port(struct rstp *rstp)
1237 OVS_EXCLUDED(rstp_mutex)
1239 struct rstp_port *p;
1241 ovs_mutex_lock(&rstp_mutex);
1242 if (rstp->ports_count > 0){
1243 LIST_FOR_EACH (p, node, &rstp->ports) {
1244 if (p->port_id == rstp->root_port_id) {
1245 ovs_mutex_unlock(&rstp_mutex);
1250 ovs_mutex_unlock(&rstp_mutex);
1254 /* Returns the state of port 'p'. */
1256 rstp_port_get_state(const struct rstp_port *p)
1258 enum rstp_state state;
1260 ovs_mutex_lock(&rstp_mutex);
1261 state = p->rstp_state;
1262 ovs_mutex_unlock(&rstp_mutex);
1267 /* Retrieves port status. */
1269 rstp_port_get_status(const struct rstp_port *p, uint16_t *id,
1270 enum rstp_state *state, enum rstp_port_role *role,
1271 int *tx_count, int *rx_count, int *error_count,
1273 OVS_EXCLUDED(rstp_mutex)
1275 ovs_mutex_lock(&rstp_mutex);
1277 *state = p->rstp_state;
1280 *tx_count = p->tx_count;
1281 *rx_count = p->rx_rstp_bpdu_cnt;
1282 *error_count = p->error_count;
1283 *uptime = p->uptime;
1284 ovs_mutex_unlock(&rstp_mutex);
1288 rstp_port_set(struct rstp_port *port, uint16_t port_num, int priority,
1289 uint32_t path_cost, bool is_admin_edge, bool is_auto_edge,
1290 bool do_mcheck, void *aux)
1291 OVS_EXCLUDED(rstp_mutex)
1293 ovs_mutex_lock(&rstp_mutex);
1295 rstp_port_set_priority__(port, priority);
1296 rstp_port_set_port_number__(port, port_num);
1297 rstp_port_set_path_cost__(port, path_cost);
1298 rstp_port_set_admin_edge__(port, is_admin_edge);
1299 rstp_port_set_auto_edge__(port, is_auto_edge);
1300 rstp_port_set_mcheck__(port, do_mcheck);
1301 ovs_mutex_unlock(&rstp_mutex);
1304 /* Individual setters only used by test-rstp.c. */
1306 rstp_port_set_priority(struct rstp_port *port, int priority)
1307 OVS_EXCLUDED(rstp_mutex)
1309 ovs_mutex_lock(&rstp_mutex);
1310 rstp_port_set_priority__(port, priority);
1311 ovs_mutex_unlock(&rstp_mutex);
1315 rstp_port_set_path_cost(struct rstp_port *port, uint32_t path_cost)
1316 OVS_EXCLUDED(rstp_mutex)
1318 ovs_mutex_lock(&rstp_mutex);
1319 rstp_port_set_path_cost__(port, path_cost);
1320 ovs_mutex_unlock(&rstp_mutex);
1324 rstp_port_set_aux(struct rstp_port *port, void *aux)
1325 OVS_EXCLUDED(rstp_mutex)
1327 ovs_mutex_lock(&rstp_mutex);
1329 ovs_mutex_unlock(&rstp_mutex);
1333 static struct rstp *
1334 rstp_find(const char *name)
1335 OVS_REQUIRES(rstp_mutex)
1339 LIST_FOR_EACH (rstp, node, all_rstps) {
1340 if (!strcmp(rstp->name, name)) {
1348 rstp_unixctl_tcn(struct unixctl_conn *conn, int argc,
1349 const char *argv[], void *aux OVS_UNUSED)
1350 OVS_EXCLUDED(rstp_mutex)
1352 ovs_mutex_lock(&rstp_mutex);
1354 struct rstp *rstp = rstp_find(argv[1]);
1356 unixctl_command_reply_error(conn, "No such RSTP object");
1359 rstp->changes = true;
1363 LIST_FOR_EACH (rstp, node, all_rstps) {
1364 rstp->changes = true;
1368 unixctl_command_reply(conn, "OK");
1371 ovs_mutex_unlock(&rstp_mutex);