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 = OVS_MUTEX_INITIALIZER;
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_relaxed(&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(hmap_is_empty(&rstp->ports));
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 unixctl_command_register("rstp/tcn", "[bridge]", 0, 1, rstp_unixctl_tcn,
239 /* Creates and returns a new RSTP instance that initially has no ports. */
241 rstp_create(const char *name, rstp_identifier bridge_address,
242 void (*send_bpdu)(struct ofpbuf *bpdu, void *port_aux,
245 OVS_EXCLUDED(rstp_mutex)
249 VLOG_DBG("Creating RSTP instance");
251 rstp = xzalloc(sizeof *rstp);
252 rstp->name = xstrdup(name);
254 /* Initialize the ports map before calling any setters,
255 * so that the state machines will see an empty ports map. */
256 hmap_init(&rstp->ports);
258 ovs_mutex_lock(&rstp_mutex);
259 /* Set bridge address. */
260 rstp_set_bridge_address__(rstp, bridge_address);
261 /* Set default parameters values. */
262 rstp_set_bridge_priority__(rstp, RSTP_DEFAULT_PRIORITY);
263 rstp_set_bridge_ageing_time__(rstp, RSTP_DEFAULT_AGEING_TIME);
264 rstp_set_bridge_force_protocol_version__(rstp, FPV_DEFAULT);
265 rstp_set_bridge_forward_delay__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
266 rstp_set_bridge_hello_time__(rstp);
267 rstp_set_bridge_max_age__(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
268 rstp_set_bridge_migrate_time__(rstp);
269 rstp_set_bridge_transmit_hold_count__(rstp,
270 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
271 rstp_set_bridge_times__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
272 RSTP_BRIDGE_HELLO_TIME,
273 RSTP_DEFAULT_BRIDGE_MAX_AGE, 0);
274 rstp->send_bpdu = send_bpdu;
276 rstp->changes = false;
279 ovs_refcount_init(&rstp->ref_cnt);
281 list_push_back(all_rstps, &rstp->node);
282 ovs_mutex_unlock(&rstp_mutex);
284 VLOG_DBG("RSTP instance creation done");
288 /* Called by rstp_set_bridge_address() and rstp_set_bridge_priority(),
289 * it updates the bridge priority vector according to the values passed by
293 set_bridge_priority__(struct rstp *rstp)
294 OVS_REQUIRES(rstp_mutex)
298 rstp->bridge_priority.root_bridge_id = rstp->bridge_identifier;
299 rstp->bridge_priority.designated_bridge_id = rstp->bridge_identifier;
300 VLOG_DBG("%s: new bridge identifier: "RSTP_ID_FMT"", rstp->name,
301 RSTP_ID_ARGS(rstp->bridge_identifier));
303 /* [17.13] When the bridge address changes, recalculates all priority
306 HMAP_FOR_EACH (p, node, &rstp->ports) {
310 rstp->changes = true;
311 updt_roles_tree__(rstp);
314 /* Sets the bridge address. */
316 rstp_set_bridge_address__(struct rstp *rstp, rstp_identifier bridge_address)
317 OVS_REQUIRES(rstp_mutex)
319 VLOG_DBG("%s: set bridge address to: "RSTP_ID_FMT"", rstp->name,
320 RSTP_ID_ARGS(bridge_address));
322 rstp->address = bridge_address;
323 rstp->bridge_identifier = bridge_address;
324 set_bridge_priority__(rstp);
327 /* Sets the bridge address. */
329 rstp_set_bridge_address(struct rstp *rstp, rstp_identifier bridge_address)
330 OVS_EXCLUDED(rstp_mutex)
332 ovs_mutex_lock(&rstp_mutex);
333 rstp_set_bridge_address__(rstp, bridge_address);
334 ovs_mutex_unlock(&rstp_mutex);
338 rstp_get_name(const struct rstp *rstp)
339 OVS_EXCLUDED(rstp_mutex)
343 ovs_mutex_lock(&rstp_mutex);
345 ovs_mutex_unlock(&rstp_mutex);
350 rstp_get_bridge_id(const struct rstp *rstp)
351 OVS_EXCLUDED(rstp_mutex)
353 rstp_identifier bridge_id;
355 ovs_mutex_lock(&rstp_mutex);
356 bridge_id = rstp->bridge_identifier;
357 ovs_mutex_unlock(&rstp_mutex);
362 /* Sets the bridge priority. */
364 rstp_set_bridge_priority__(struct rstp *rstp, int new_priority)
365 OVS_REQUIRES(rstp_mutex)
367 new_priority = ROUND_DOWN(new_priority, RSTP_PRIORITY_STEP);
369 if (new_priority >= RSTP_MIN_PRIORITY
370 && new_priority <= RSTP_MAX_PRIORITY) {
371 VLOG_DBG("%s: set bridge priority to %d", rstp->name, new_priority);
373 rstp->priority = new_priority;
374 rstp->bridge_identifier &= 0x0000ffffffffffffULL;
375 rstp->bridge_identifier |= (uint64_t)new_priority << 48;
376 set_bridge_priority__(rstp);
381 rstp_set_bridge_priority(struct rstp *rstp, int new_priority)
382 OVS_EXCLUDED(rstp_mutex)
384 ovs_mutex_lock(&rstp_mutex);
385 rstp_set_bridge_priority__(rstp, new_priority);
386 ovs_mutex_unlock(&rstp_mutex);
389 /* Sets the bridge ageing time. */
391 rstp_set_bridge_ageing_time__(struct rstp *rstp, int new_ageing_time)
392 OVS_REQUIRES(rstp_mutex)
394 if (new_ageing_time >= RSTP_MIN_AGEING_TIME
395 && new_ageing_time <= RSTP_MAX_AGEING_TIME) {
396 VLOG_DBG("%s: set ageing time to %d", rstp->name, new_ageing_time);
398 rstp->ageing_time = new_ageing_time;
403 rstp_set_bridge_ageing_time(struct rstp *rstp, int new_ageing_time)
404 OVS_EXCLUDED(rstp_mutex)
406 ovs_mutex_lock(&rstp_mutex);
407 rstp_set_bridge_ageing_time__(rstp, new_ageing_time);
408 ovs_mutex_unlock(&rstp_mutex);
411 /* Reinitializes RSTP when switching from RSTP mode to STP mode
415 reinitialize_rstp__(struct rstp *rstp)
416 OVS_REQUIRES(rstp_mutex)
419 static struct hmap 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;
432 /* Initialize the ports hmap before calling any setters,
433 * so that the state machines will see an empty ports list. */
434 hmap_init(&rstp->ports);
436 /* Set bridge address. */
437 rstp_set_bridge_address__(rstp, temp.address);
438 /* Set default parameters values. */
439 rstp_set_bridge_priority__(rstp, RSTP_DEFAULT_PRIORITY);
440 rstp_set_bridge_ageing_time__(rstp, RSTP_DEFAULT_AGEING_TIME);
441 rstp_set_bridge_forward_delay__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
442 rstp_set_bridge_hello_time__(rstp);
443 rstp_set_bridge_max_age__(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
444 rstp_set_bridge_migrate_time__(rstp);
445 rstp_set_bridge_transmit_hold_count__(rstp,
446 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
447 rstp_set_bridge_times__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
448 RSTP_BRIDGE_HELLO_TIME,
449 RSTP_DEFAULT_BRIDGE_MAX_AGE, 0);
451 rstp->send_bpdu = temp.send_bpdu;
452 rstp->aux = temp.aux;
453 rstp->node = temp.node;
454 rstp->changes = false;
460 HMAP_FOR_EACH (p, node, &rstp->ports) {
461 reinitialize_port__(p);
464 rstp->ref_cnt = temp.ref_cnt;
467 /* Sets the force protocol version parameter. */
469 rstp_set_bridge_force_protocol_version__(struct rstp *rstp,
470 enum rstp_force_protocol_version new_force_protocol_version)
471 OVS_REQUIRES(rstp_mutex)
473 if (new_force_protocol_version != rstp->force_protocol_version &&
474 (new_force_protocol_version == FPV_STP_COMPATIBILITY ||
475 new_force_protocol_version == FPV_DEFAULT)) {
476 VLOG_DBG("%s: set bridge Force Protocol Version to %d", rstp->name,
477 new_force_protocol_version);
479 /* [17.13] The Spanning Tree Protocol Entity shall be reinitialized,
480 * as specified by the assertion of BEGIN (17.18.1) in the state
481 * machine specification.
483 reinitialize_rstp__(rstp);
484 rstp->force_protocol_version = new_force_protocol_version;
485 if (rstp->force_protocol_version < 2) {
486 rstp->stp_version = true;
487 rstp->rstp_version = false;
489 rstp->stp_version = false;
490 rstp->rstp_version = true;
492 rstp->changes = true;
498 rstp_set_bridge_force_protocol_version(struct rstp *rstp,
499 enum rstp_force_protocol_version new_force_protocol_version)
500 OVS_EXCLUDED(rstp_mutex)
502 ovs_mutex_lock(&rstp_mutex);
503 rstp_set_bridge_force_protocol_version__(rstp, new_force_protocol_version);
504 ovs_mutex_unlock(&rstp_mutex);
507 /* Sets the bridge Hello Time parameter. */
509 rstp_set_bridge_hello_time__(struct rstp *rstp)
510 OVS_REQUIRES(rstp_mutex)
512 VLOG_DBG("%s: set RSTP Hello Time to %d", rstp->name,
513 RSTP_BRIDGE_HELLO_TIME);
514 /* 2 is the only acceptable value. */
515 rstp->bridge_hello_time = RSTP_BRIDGE_HELLO_TIME;
518 /* Sets the bridge max age parameter. */
520 rstp_set_bridge_max_age__(struct rstp *rstp, int new_max_age)
521 OVS_REQUIRES(rstp_mutex)
523 if (new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE &&
524 new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) {
526 if ((2 * (rstp->bridge_forward_delay - 1) >= new_max_age)
527 && (new_max_age >= 2 * rstp->bridge_hello_time)) {
528 VLOG_DBG("%s: set RSTP bridge Max Age to %d", rstp->name,
531 rstp->bridge_max_age = new_max_age;
532 rstp->bridge_times.max_age = new_max_age;
538 rstp_set_bridge_max_age(struct rstp *rstp, int new_max_age)
539 OVS_EXCLUDED(rstp_mutex)
541 ovs_mutex_lock(&rstp_mutex);
542 rstp_set_bridge_max_age__(rstp, new_max_age);
543 ovs_mutex_unlock(&rstp_mutex);
546 /* Sets the bridge forward delay parameter. */
548 rstp_set_bridge_forward_delay__(struct rstp *rstp, int new_forward_delay)
549 OVS_REQUIRES(rstp_mutex)
551 if (new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY
552 && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) {
553 if (2 * (new_forward_delay - 1) >= rstp->bridge_max_age) {
554 VLOG_DBG("%s: set RSTP Forward Delay to %d", rstp->name,
556 rstp->bridge_forward_delay = new_forward_delay;
557 rstp->bridge_times.forward_delay = new_forward_delay;
563 rstp_set_bridge_forward_delay(struct rstp *rstp, int new_forward_delay)
564 OVS_EXCLUDED(rstp_mutex)
566 ovs_mutex_lock(&rstp_mutex);
567 rstp_set_bridge_forward_delay__(rstp, new_forward_delay);
568 ovs_mutex_unlock(&rstp_mutex);
571 /* Sets the bridge transmit hold count parameter. */
573 rstp_set_bridge_transmit_hold_count__(struct rstp *rstp,
574 int new_transmit_hold_count)
575 OVS_REQUIRES(rstp_mutex)
577 if (new_transmit_hold_count >= RSTP_MIN_TRANSMIT_HOLD_COUNT
578 && new_transmit_hold_count <= RSTP_MAX_TRANSMIT_HOLD_COUNT) {
581 VLOG_DBG("%s: set RSTP Transmit Hold Count to %d", rstp->name,
582 new_transmit_hold_count);
583 /* Resetting txCount on all ports [17.13]. */
585 rstp->transmit_hold_count = new_transmit_hold_count;
586 HMAP_FOR_EACH (p, node, &rstp->ports) {
593 rstp_set_bridge_transmit_hold_count(struct rstp *rstp,
594 int new_transmit_hold_count)
595 OVS_EXCLUDED(rstp_mutex)
597 ovs_mutex_lock(&rstp_mutex);
598 rstp_set_bridge_transmit_hold_count__(rstp, new_transmit_hold_count);
599 ovs_mutex_unlock(&rstp_mutex);
602 /* Sets the bridge migrate time parameter. */
604 rstp_set_bridge_migrate_time__(struct rstp *rstp)
605 OVS_REQUIRES(rstp_mutex)
607 VLOG_DBG("%s: set RSTP Migrate Time to %d", rstp->name,
609 /* 3 is the only acceptable value */
610 rstp->migrate_time = RSTP_MIGRATE_TIME;
613 /* Sets the bridge times. */
615 rstp_set_bridge_times__(struct rstp *rstp, int new_forward_delay,
616 int new_hello_time, int new_max_age,
618 OVS_REQUIRES(rstp_mutex)
620 VLOG_DBG("%s: set RSTP times to (%d, %d, %d, %d)", rstp->name,
621 new_forward_delay, new_hello_time, new_max_age, new_message_age);
622 if (new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY
623 && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) {
624 rstp->bridge_times.forward_delay = new_forward_delay;
626 if (new_hello_time == RSTP_BRIDGE_HELLO_TIME) {
627 rstp->bridge_times.hello_time = new_hello_time;
629 if (new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE
630 && new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) {
631 rstp->bridge_times.max_age = new_max_age;
633 rstp->bridge_times.message_age = new_message_age;
636 /* Sets the port id, it is called by rstp_port_set_port_number__() or
637 * rstp_port_set_priority__().
640 set_port_id__(struct rstp_port *p)
641 OVS_REQUIRES(rstp_mutex)
646 /* [9.2.7] Port identifier. */
647 p->port_id = p->port_number | (p->priority << 8);
648 VLOG_DBG("%s: new RSTP port id "RSTP_PORT_ID_FMT"", rstp->name,
652 /* Sets the port priority. */
654 rstp_port_set_priority__(struct rstp_port *port, int priority)
655 OVS_REQUIRES(rstp_mutex)
657 if (priority >= RSTP_MIN_PORT_PRIORITY
658 && priority <= RSTP_MAX_PORT_PRIORITY) {
659 VLOG_DBG("%s, port %u: set RSTP port priority to %d", port->rstp->name,
660 port->port_number, priority);
662 priority -= priority % RSTP_STEP_PORT_PRIORITY;
663 port->priority = priority;
665 port->selected = false;
666 port->reselect = true;
670 /* Checks if a port number is available. */
672 is_port_number_available__(struct rstp *rstp, int n, struct rstp_port *port)
673 OVS_REQUIRES(rstp_mutex)
675 if (n >= 1 && n <= RSTP_MAX_PORTS) {
676 struct rstp_port *p = rstp_get_port__(rstp, n);
678 return p == NULL || p == port;
684 rstp_first_free_number__(struct rstp *rstp, struct rstp_port *rstp_port)
685 OVS_REQUIRES(rstp_mutex)
689 while (free_number <= RSTP_MAX_PORTS) {
690 if (is_port_number_available__(rstp, free_number, rstp_port)) {
695 VLOG_DBG("%s, No free port number available.", rstp->name);
699 /* Sets the port number. */
701 rstp_port_set_port_number__(struct rstp_port *port, uint16_t port_number)
702 OVS_REQUIRES(rstp_mutex)
704 /* If new_port_number is available, use it, otherwise use the first free
705 * available port number. */
707 is_port_number_available__(port->rstp, port_number, port)
709 : rstp_first_free_number__(port->rstp, port);
712 /* [17.13] is not clear. I suppose that a port number change
713 * should trigger reselection like a port priority change. */
714 port->selected = false;
715 port->reselect = true;
717 VLOG_DBG("%s: set new RSTP port number %d", port->rstp->name,
721 /* Converts the link speed to a port path cost [Table 17-3]. */
723 rstp_convert_speed_to_cost(unsigned int speed)
727 value = speed >= 10000000 ? 2 /* 10 Tb/s. */
728 : speed >= 1000000 ? 20 /* 1 Tb/s. */
729 : speed >= 100000 ? 200 /* 100 Gb/s. */
730 : speed >= 10000 ? 2000 /* 10 Gb/s. */
731 : speed >= 1000 ? 20000 /* 1 Gb/s. */
732 : speed >= 100 ? 200000 /* 100 Mb/s. */
733 : speed >= 10 ? 2000000 /* 10 Mb/s. */
734 : speed >= 1 ? 20000000 /* 1 Mb/s. */
735 : RSTP_DEFAULT_PORT_PATH_COST; /* 100 Mb/s. */
740 /* Sets the port path cost. */
742 rstp_port_set_path_cost__(struct rstp_port *port, uint32_t path_cost)
743 OVS_REQUIRES(rstp_mutex)
745 if (path_cost >= RSTP_MIN_PORT_PATH_COST
746 && path_cost <= RSTP_MAX_PORT_PATH_COST) {
747 VLOG_DBG("%s, port %u, set RSTP port path cost to %d",
748 port->rstp->name, port->port_number, path_cost);
750 port->port_path_cost = path_cost;
751 port->selected = false;
752 port->reselect = true;
756 /* Gets the root path cost. */
758 rstp_get_root_path_cost(const struct rstp *rstp)
759 OVS_EXCLUDED(rstp_mutex)
763 ovs_mutex_lock(&rstp_mutex);
764 cost = rstp->root_priority.root_path_cost;
765 ovs_mutex_unlock(&rstp_mutex);
769 /* Returns true if something has happened to 'rstp' which necessitates
770 * flushing the client's MAC learning table.
773 rstp_check_and_reset_fdb_flush(struct rstp *rstp)
774 OVS_EXCLUDED(rstp_mutex)
781 ovs_mutex_lock(&rstp_mutex);
782 HMAP_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;
791 ovs_mutex_unlock(&rstp_mutex);
795 /* Finds a port whose state has changed, and returns the aux pointer set for
796 * the port. A NULL pointer is returned when no changed port is found. On
797 * return '*portp' contains the pointer to the rstp port that changed, or NULL
798 * if no changed port can be found.
800 * If '*portp' is passed as non-NULL, it must be the value set by the last
801 * invocation of this function.
803 * This function may only be called by the thread that creates and deletes
804 * ports. Otherwise this function is not thread safe, as the returned
805 * '*portp' could become stale before it is used in the next invocation. */
807 rstp_get_next_changed_port_aux(struct rstp *rstp, struct rstp_port **portp)
811 ovs_mutex_lock(&rstp_mutex);
812 if (*portp == NULL) {
815 HMAP_FOR_EACH (p, node, &rstp->ports) {
816 if (p->state_changed) {
817 p->state_changed = false;
823 } else { /* continue */
824 struct rstp_port *p = *portp;
826 HMAP_FOR_EACH_CONTINUE (p, node, &rstp->ports) {
827 if (p->state_changed) {
828 p->state_changed = false;
835 /* No changed port found. */
838 ovs_mutex_unlock(&rstp_mutex);
842 /* Returns the port in 'rstp' with number 'port_number'.
844 * XXX: May only be called while concurrent deletion of ports is excluded. */
845 static struct rstp_port *
846 rstp_get_port__(struct rstp *rstp, uint16_t port_number)
847 OVS_REQUIRES(rstp_mutex)
849 struct rstp_port *port;
851 ovs_assert(rstp && port_number > 0 && port_number <= RSTP_MAX_PORTS);
853 HMAP_FOR_EACH_WITH_HASH (port, node, hash_int(port_number, 0),
855 if (port->port_number == port_number) {
863 rstp_get_port(struct rstp *rstp, uint16_t port_number)
864 OVS_EXCLUDED(rstp_mutex)
868 ovs_mutex_lock(&rstp_mutex);
869 p = rstp_get_port__(rstp, port_number);
870 ovs_mutex_unlock(&rstp_mutex);
875 rstp_get_port_aux(struct rstp *rstp, uint16_t port_number)
876 OVS_EXCLUDED(rstp_mutex)
881 ovs_mutex_lock(&rstp_mutex);
882 p = rstp_get_port__(rstp, port_number);
884 ovs_mutex_unlock(&rstp_mutex);
888 /* Updates the port_enabled parameter. */
890 update_port_enabled__(struct rstp_port *p)
891 OVS_REQUIRES(rstp_mutex)
893 if (p->mac_operational && p->is_administrative_bridge_port
894 == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) {
895 p->port_enabled = true;
897 p->port_enabled = false;
901 /* Sets the port MAC_Operational parameter [6.4.2]. */
903 rstp_port_set_mac_operational(struct rstp_port *p, bool new_mac_operational)
904 OVS_EXCLUDED(rstp_mutex)
908 ovs_mutex_lock(&rstp_mutex);
910 if (p->mac_operational != new_mac_operational) {
911 p->mac_operational = new_mac_operational;
912 update_port_enabled__(p);
913 rstp->changes = true;
916 ovs_mutex_unlock(&rstp_mutex);
919 /* Sets the port Administrative Bridge Port parameter. */
921 rstp_port_set_administrative_bridge_port__(struct rstp_port *p,
922 uint8_t admin_port_state)
923 OVS_REQUIRES(rstp_mutex)
925 if (admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_DISABLED
926 || admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) {
928 p->is_administrative_bridge_port = admin_port_state;
929 update_port_enabled__(p);
933 /* Sets the port oper_point_to_point_mac parameter. */
935 rstp_port_set_oper_point_to_point_mac__(struct rstp_port *p,
936 uint8_t new_oper_p2p_mac)
937 OVS_REQUIRES(rstp_mutex)
939 if (new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_DISABLED
940 || new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_ENABLED) {
942 p->oper_point_to_point_mac = new_oper_p2p_mac;
943 update_port_enabled__(p);
947 /* Initializes a port with the defaults values for its parameters. */
949 rstp_initialize_port_defaults__(struct rstp_port *p)
950 OVS_REQUIRES(rstp_mutex)
952 rstp_port_set_administrative_bridge_port__(p,
953 RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED);
954 rstp_port_set_oper_point_to_point_mac__(p,
955 RSTP_OPER_P2P_MAC_STATE_ENABLED);
956 rstp_port_set_path_cost__(p, RSTP_DEFAULT_PORT_PATH_COST);
957 rstp_port_set_admin_edge__(p, false);
958 rstp_port_set_auto_edge__(p, true);
959 rstp_port_set_mcheck__(p, false);
961 /* Initialize state machines. */
962 p->port_receive_sm_state = PORT_RECEIVE_SM_INIT;
963 p->port_protocol_migration_sm_state = PORT_PROTOCOL_MIGRATION_SM_INIT;
964 p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_INIT;
965 p->port_transmit_sm_state = PORT_TRANSMIT_SM_INIT;
966 p->port_information_sm_state = PORT_INFORMATION_SM_INIT;
967 p->port_role_transition_sm_state = PORT_ROLE_TRANSITION_SM_INIT;
968 p->port_state_transition_sm_state = PORT_STATE_TRANSITION_SM_INIT;
969 p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INIT;
975 reinitialize_port__(struct rstp_port *p)
976 OVS_REQUIRES(rstp_mutex)
978 struct rstp_port temp_port;
983 memset(p, 0, sizeof(struct rstp_port));
985 p->ref_cnt = temp_port.ref_cnt;
987 p->node = temp_port.node;
988 p->aux = temp_port.aux;
989 p->port_number = temp_port.port_number;
990 p->port_priority = temp_port.port_priority;
991 p->port_id = temp_port.port_id;
992 p->rstp_state = RSTP_DISCARDING;
994 rstp_initialize_port_defaults__(p);
996 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" reinitialized.", rstp->name,
1001 reinitialize_port(struct rstp_port *p)
1002 OVS_EXCLUDED(rstp_mutex)
1004 ovs_mutex_lock(&rstp_mutex);
1005 reinitialize_port__(p);
1006 ovs_mutex_unlock(&rstp_mutex);
1009 /* Sets the port state. */
1011 rstp_port_set_state__(struct rstp_port *p, enum rstp_state state)
1012 OVS_REQUIRES(rstp_mutex)
1017 VLOG_DBG("%s, port %u: set RSTP port state %s -> %s", rstp->name,
1019 rstp_state_name(p->rstp_state), rstp_state_name(state));
1021 if (state != p->rstp_state && !p->state_changed) {
1022 p->state_changed = true;
1023 seq_change(connectivity_seq_get());
1025 p->rstp_state = state;
1029 rstp_port_set_state(struct rstp_port *p, enum rstp_state state)
1030 OVS_EXCLUDED(rstp_mutex)
1032 ovs_mutex_lock(&rstp_mutex);
1033 rstp_port_set_state__(p, state);
1034 ovs_mutex_unlock(&rstp_mutex);
1037 /* Adds a RSTP port. */
1039 rstp_add_port(struct rstp *rstp)
1040 OVS_EXCLUDED(rstp_mutex)
1042 struct rstp_port *p = xzalloc(sizeof *p);
1044 ovs_refcount_init(&p->ref_cnt);
1046 ovs_mutex_lock(&rstp_mutex);
1048 rstp_port_set_priority__(p, RSTP_DEFAULT_PORT_PRIORITY);
1049 rstp_port_set_port_number__(p, 0);
1051 rstp_initialize_port_defaults__(p);
1052 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" initialized.", rstp->name,
1055 rstp_port_set_state__(p, RSTP_DISCARDING);
1056 hmap_insert(&rstp->ports, &p->node, hash_int(p->port_number, 0));
1057 rstp->changes = true;
1059 VLOG_DBG("%s: added port "RSTP_PORT_ID_FMT"", rstp->name, p->port_id);
1060 ovs_mutex_unlock(&rstp_mutex);
1064 /* Caller has to hold a reference to prevent 'rstp_port' from being deleted
1065 * while taking a new reference. */
1067 rstp_port_ref(const struct rstp_port *rp_)
1068 OVS_EXCLUDED(rstp_mutex)
1070 struct rstp_port *rp = CONST_CAST(struct rstp_port *, rp_);
1073 ovs_refcount_ref(&rp->ref_cnt);
1078 /* Frees RSTP struct. This can be caller by any thread. */
1080 rstp_port_unref(struct rstp_port *rp)
1081 OVS_EXCLUDED(rstp_mutex)
1083 if (rp && ovs_refcount_unref_relaxed(&rp->ref_cnt) == 1) {
1086 ovs_mutex_lock(&rstp_mutex);
1088 rstp_port_set_state__(rp, RSTP_DISABLED);
1089 hmap_remove(&rstp->ports, &rp->node);
1090 VLOG_DBG("%s: removed port "RSTP_PORT_ID_FMT"", rstp->name,
1092 ovs_mutex_unlock(&rstp_mutex);
1097 /* Sets the port Admin Edge parameter. */
1099 rstp_port_set_admin_edge__(struct rstp_port *port, bool admin_edge)
1100 OVS_REQUIRES(rstp_mutex)
1102 if (port->admin_edge != admin_edge) {
1103 VLOG_DBG("%s, port %u: set RSTP Admin Edge to %d", port->rstp->name,
1104 port->port_number, admin_edge);
1106 port->admin_edge = admin_edge;
1110 /* Sets the port Auto Edge parameter. */
1112 rstp_port_set_auto_edge__(struct rstp_port *port, bool auto_edge)
1113 OVS_REQUIRES(rstp_mutex)
1115 if (port->auto_edge != auto_edge) {
1116 VLOG_DBG("%s, port %u: set RSTP Auto Edge to %d", port->rstp->name,
1117 port->port_number, auto_edge);
1119 port->auto_edge = auto_edge;
1123 /* Sets the port mcheck parameter.
1124 * [17.19.13] May be set by management to force the Port Protocol Migration
1125 * state machine to transmit RST BPDUs for a MigrateTime (17.13.9) period, to
1126 * test whether all STP Bridges (17.4) on the attached LAN have been removed
1127 * and the Port can continue to transmit RSTP BPDUs. Setting mcheck has no
1128 * effect if stpVersion (17.20.12) is TRUE, i.e., the Bridge is operating in
1129 * STP Compatibility mode.
1132 rstp_port_set_mcheck__(struct rstp_port *port, bool mcheck)
1133 OVS_REQUIRES(rstp_mutex)
1135 if (mcheck == true && port->rstp->force_protocol_version >= 2) {
1136 port->mcheck = true;
1138 VLOG_DBG("%s, port %u: set RSTP mcheck to %d", port->rstp->name,
1139 port->port_number, mcheck);
1143 /* Returns the designated bridge id. */
1145 rstp_get_designated_id(const struct rstp *rstp)
1146 OVS_EXCLUDED(rstp_mutex)
1148 rstp_identifier designated_id;
1150 ovs_mutex_lock(&rstp_mutex);
1151 designated_id = rstp->root_priority.designated_bridge_id;
1152 ovs_mutex_unlock(&rstp_mutex);
1154 return designated_id;
1157 /* Returns the root bridge id. */
1159 rstp_get_root_id(const struct rstp *rstp)
1160 OVS_EXCLUDED(rstp_mutex)
1162 rstp_identifier root_id;
1164 ovs_mutex_lock(&rstp_mutex);
1165 root_id = rstp->root_priority.root_bridge_id;
1166 ovs_mutex_unlock(&rstp_mutex);
1171 /* Returns the designated port id. */
1173 rstp_get_designated_port_id(const struct rstp *rstp)
1174 OVS_EXCLUDED(rstp_mutex)
1176 uint16_t designated_port_id;
1178 ovs_mutex_lock(&rstp_mutex);
1179 designated_port_id = rstp->root_priority.designated_port_id;
1180 ovs_mutex_unlock(&rstp_mutex);
1182 return designated_port_id;
1185 /* Return the bridge port id. */
1187 rstp_get_bridge_port_id(const struct rstp *rstp)
1188 OVS_EXCLUDED(rstp_mutex)
1190 uint16_t bridge_port_id;
1192 ovs_mutex_lock(&rstp_mutex);
1193 bridge_port_id = rstp->root_priority.bridge_port_id;
1194 ovs_mutex_unlock(&rstp_mutex);
1196 return bridge_port_id;
1199 /* Returns true if the bridge believes to the be root of the spanning tree,
1203 rstp_is_root_bridge(const struct rstp *rstp)
1204 OVS_EXCLUDED(rstp_mutex)
1208 ovs_mutex_lock(&rstp_mutex);
1209 is_root = rstp->bridge_identifier ==
1210 rstp->root_priority.designated_bridge_id;
1211 ovs_mutex_unlock(&rstp_mutex);
1216 /* Returns the bridge ID of the bridge currently believed to be the root. */
1218 rstp_get_designated_root(const struct rstp *rstp)
1219 OVS_EXCLUDED(rstp_mutex)
1221 rstp_identifier designated_root;
1223 ovs_mutex_lock(&rstp_mutex);
1224 designated_root = rstp->root_priority.designated_bridge_id;
1225 ovs_mutex_unlock(&rstp_mutex);
1227 return designated_root;
1230 /* Returns the port connecting 'rstp' to the root bridge, or a null pointer if
1231 * there is no such port.
1234 rstp_get_root_port(struct rstp *rstp)
1235 OVS_EXCLUDED(rstp_mutex)
1237 struct rstp_port *p;
1239 ovs_mutex_lock(&rstp_mutex);
1240 HMAP_FOR_EACH (p, node, &rstp->ports) {
1241 if (p->port_id == rstp->root_port_id) {
1242 ovs_mutex_unlock(&rstp_mutex);
1246 ovs_mutex_unlock(&rstp_mutex);
1250 /* Returns the state of port 'p'. */
1252 rstp_port_get_state(const struct rstp_port *p)
1254 enum rstp_state state;
1256 ovs_mutex_lock(&rstp_mutex);
1257 state = p->rstp_state;
1258 ovs_mutex_unlock(&rstp_mutex);
1263 /* Retrieves port status. */
1265 rstp_port_get_status(const struct rstp_port *p, uint16_t *id,
1266 enum rstp_state *state, enum rstp_port_role *role,
1267 int *tx_count, int *rx_count, int *error_count,
1269 OVS_EXCLUDED(rstp_mutex)
1271 ovs_mutex_lock(&rstp_mutex);
1273 *state = p->rstp_state;
1276 *tx_count = p->tx_count;
1277 *rx_count = p->rx_rstp_bpdu_cnt;
1278 *error_count = p->error_count;
1279 *uptime = p->uptime;
1280 ovs_mutex_unlock(&rstp_mutex);
1284 rstp_port_set(struct rstp_port *port, uint16_t port_num, int priority,
1285 uint32_t path_cost, bool is_admin_edge, bool is_auto_edge,
1286 bool do_mcheck, void *aux)
1287 OVS_EXCLUDED(rstp_mutex)
1289 ovs_mutex_lock(&rstp_mutex);
1291 rstp_port_set_priority__(port, priority);
1292 rstp_port_set_port_number__(port, port_num);
1293 rstp_port_set_path_cost__(port, path_cost);
1294 rstp_port_set_admin_edge__(port, is_admin_edge);
1295 rstp_port_set_auto_edge__(port, is_auto_edge);
1296 rstp_port_set_mcheck__(port, do_mcheck);
1297 ovs_mutex_unlock(&rstp_mutex);
1300 /* Individual setters only used by test-rstp.c. */
1302 rstp_port_set_priority(struct rstp_port *port, int priority)
1303 OVS_EXCLUDED(rstp_mutex)
1305 ovs_mutex_lock(&rstp_mutex);
1306 rstp_port_set_priority__(port, priority);
1307 ovs_mutex_unlock(&rstp_mutex);
1311 rstp_port_set_path_cost(struct rstp_port *port, uint32_t path_cost)
1312 OVS_EXCLUDED(rstp_mutex)
1314 ovs_mutex_lock(&rstp_mutex);
1315 rstp_port_set_path_cost__(port, path_cost);
1316 ovs_mutex_unlock(&rstp_mutex);
1320 rstp_port_set_aux(struct rstp_port *port, void *aux)
1321 OVS_EXCLUDED(rstp_mutex)
1323 ovs_mutex_lock(&rstp_mutex);
1325 ovs_mutex_unlock(&rstp_mutex);
1329 static struct rstp *
1330 rstp_find(const char *name)
1331 OVS_REQUIRES(rstp_mutex)
1335 LIST_FOR_EACH (rstp, node, all_rstps) {
1336 if (!strcmp(rstp->name, name)) {
1344 rstp_unixctl_tcn(struct unixctl_conn *conn, int argc,
1345 const char *argv[], void *aux OVS_UNUSED)
1346 OVS_EXCLUDED(rstp_mutex)
1348 ovs_mutex_lock(&rstp_mutex);
1350 struct rstp *rstp = rstp_find(argv[1]);
1352 unixctl_command_reply_error(conn, "No such RSTP object");
1355 rstp->changes = true;
1359 LIST_FOR_EACH (rstp, node, all_rstps) {
1360 rstp->changes = true;
1364 unixctl_command_reply(conn, "OK");
1367 ovs_mutex_unlock(&rstp_mutex);