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,
109 OVS_REQUIRES(rstp_mutex);
110 static void rstp_port_set_admin_edge__(struct rstp_port *, bool admin_edge)
111 OVS_REQUIRES(rstp_mutex);
112 static void rstp_port_set_auto_edge__(struct rstp_port *, bool auto_edge)
113 OVS_REQUIRES(rstp_mutex);
114 static void rstp_port_set_admin_point_to_point_mac__(struct rstp_port *,
115 enum rstp_admin_point_to_point_mac_state admin_p2p_mac_state)
116 OVS_REQUIRES(rstp_mutex);
117 static void rstp_port_set_mcheck__(struct rstp_port *, bool mcheck)
118 OVS_REQUIRES(rstp_mutex);
119 static void reinitialize_port__(struct rstp_port *p)
120 OVS_REQUIRES(rstp_mutex);
123 rstp_state_name(enum rstp_state state)
130 case RSTP_FORWARDING:
132 case RSTP_DISCARDING:
140 rstp_port_role_name(enum rstp_port_role role)
145 case ROLE_DESIGNATED:
158 /* Caller has to hold a reference to prevent 'rstp' from being deleted
159 * while taking a new reference. */
161 rstp_ref(struct rstp *rstp)
162 OVS_EXCLUDED(rstp_mutex)
165 ovs_refcount_ref(&rstp->ref_cnt);
170 /* Frees RSTP struct when reference count reaches zero. */
172 rstp_unref(struct rstp *rstp)
173 OVS_EXCLUDED(rstp_mutex)
175 if (rstp && ovs_refcount_unref_relaxed(&rstp->ref_cnt) == 1) {
176 ovs_mutex_lock(&rstp_mutex);
178 /* Each RSTP port points back to struct rstp without holding a
179 * reference for that pointer. This is OK as we never move
180 * ports from one bridge to another, and holders always
181 * release their ports before releasing the bridge. This
182 * means that there should be not ports at this time. */
183 ovs_assert(hmap_is_empty(&rstp->ports));
185 list_remove(&rstp->node);
186 ovs_mutex_unlock(&rstp_mutex);
192 /* Returns the port number. Mutex is needed to guard against
193 * concurrent reinitialization (which can temporarily clear the
196 rstp_port_get_number(const struct rstp_port *p)
197 OVS_EXCLUDED(rstp_mutex)
201 ovs_mutex_lock(&rstp_mutex);
202 number = p->port_number;
203 ovs_mutex_unlock(&rstp_mutex);
208 static void rstp_unixctl_tcn(struct unixctl_conn *, int argc,
209 const char *argv[], void *aux);
211 /* Decrements the State Machines' timers. */
213 rstp_tick_timers(struct rstp *rstp)
214 OVS_EXCLUDED(rstp_mutex)
216 ovs_mutex_lock(&rstp_mutex);
217 decrease_rstp_port_timers__(rstp);
218 ovs_mutex_unlock(&rstp_mutex);
221 /* Processes an incoming BPDU. */
223 rstp_port_received_bpdu(struct rstp_port *rp, const void *bpdu,
225 OVS_EXCLUDED(rstp_mutex)
227 ovs_mutex_lock(&rstp_mutex);
228 /* Only process packets on ports that have RSTP enabled. */
229 if (rp && rp->rstp_state != RSTP_DISABLED) {
230 process_received_bpdu__(rp, bpdu, bpdu_size);
232 ovs_mutex_unlock(&rstp_mutex);
237 OVS_EXCLUDED(rstp_mutex)
239 unixctl_command_register("rstp/tcn", "[bridge]", 0, 1, rstp_unixctl_tcn,
243 /* Creates and returns a new RSTP instance that initially has no ports. */
245 rstp_create(const char *name, rstp_identifier bridge_address,
246 void (*send_bpdu)(struct ofpbuf *bpdu, void *port_aux,
249 OVS_EXCLUDED(rstp_mutex)
253 VLOG_DBG("Creating RSTP instance");
255 rstp = xzalloc(sizeof *rstp);
256 rstp->name = xstrdup(name);
258 /* Initialize the ports map before calling any setters,
259 * so that the state machines will see an empty ports map. */
260 hmap_init(&rstp->ports);
262 ovs_mutex_lock(&rstp_mutex);
263 /* Set bridge address. */
264 rstp_set_bridge_address__(rstp, bridge_address);
265 /* Set default parameters values. */
266 rstp_set_bridge_priority__(rstp, RSTP_DEFAULT_PRIORITY);
267 rstp_set_bridge_ageing_time__(rstp, RSTP_DEFAULT_AGEING_TIME);
268 rstp_set_bridge_force_protocol_version__(rstp, FPV_DEFAULT);
269 rstp_set_bridge_forward_delay__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
270 rstp_set_bridge_hello_time__(rstp);
271 rstp_set_bridge_max_age__(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
272 rstp_set_bridge_migrate_time__(rstp);
273 rstp_set_bridge_transmit_hold_count__(rstp,
274 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
275 rstp_set_bridge_times__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
276 RSTP_BRIDGE_HELLO_TIME,
277 RSTP_DEFAULT_BRIDGE_MAX_AGE, 0);
278 rstp->send_bpdu = send_bpdu;
280 rstp->changes = false;
283 ovs_refcount_init(&rstp->ref_cnt);
285 list_push_back(all_rstps, &rstp->node);
286 ovs_mutex_unlock(&rstp_mutex);
288 VLOG_DBG("RSTP instance creation done");
292 /* Called by rstp_set_bridge_address() and rstp_set_bridge_priority(),
293 * it updates the bridge priority vector according to the values passed by
297 set_bridge_priority__(struct rstp *rstp)
298 OVS_REQUIRES(rstp_mutex)
302 rstp->bridge_priority.root_bridge_id = rstp->bridge_identifier;
303 rstp->bridge_priority.designated_bridge_id = rstp->bridge_identifier;
304 VLOG_DBG("%s: new bridge identifier: "RSTP_ID_FMT"", rstp->name,
305 RSTP_ID_ARGS(rstp->bridge_identifier));
307 /* [17.13] When the bridge address changes, recalculates all priority
310 HMAP_FOR_EACH (p, node, &rstp->ports) {
314 rstp->changes = true;
315 updt_roles_tree__(rstp);
318 /* Sets the bridge address. */
320 rstp_set_bridge_address__(struct rstp *rstp, rstp_identifier bridge_address)
321 OVS_REQUIRES(rstp_mutex)
323 VLOG_DBG("%s: set bridge address to: "RSTP_ID_FMT"", rstp->name,
324 RSTP_ID_ARGS(bridge_address));
326 rstp->address = bridge_address;
327 rstp->bridge_identifier = bridge_address;
328 set_bridge_priority__(rstp);
331 /* Sets the bridge address. */
333 rstp_set_bridge_address(struct rstp *rstp, rstp_identifier bridge_address)
334 OVS_EXCLUDED(rstp_mutex)
336 ovs_mutex_lock(&rstp_mutex);
337 rstp_set_bridge_address__(rstp, bridge_address);
338 ovs_mutex_unlock(&rstp_mutex);
342 rstp_get_name(const struct rstp *rstp)
343 OVS_EXCLUDED(rstp_mutex)
347 ovs_mutex_lock(&rstp_mutex);
349 ovs_mutex_unlock(&rstp_mutex);
354 rstp_get_bridge_id(const struct rstp *rstp)
355 OVS_EXCLUDED(rstp_mutex)
357 rstp_identifier bridge_id;
359 ovs_mutex_lock(&rstp_mutex);
360 bridge_id = rstp->bridge_identifier;
361 ovs_mutex_unlock(&rstp_mutex);
366 /* Sets the bridge priority. */
368 rstp_set_bridge_priority__(struct rstp *rstp, int new_priority)
369 OVS_REQUIRES(rstp_mutex)
371 new_priority = ROUND_DOWN(new_priority, RSTP_PRIORITY_STEP);
373 if (new_priority >= RSTP_MIN_PRIORITY
374 && new_priority <= RSTP_MAX_PRIORITY) {
375 VLOG_DBG("%s: set bridge priority to %d", rstp->name, new_priority);
377 rstp->priority = new_priority;
378 rstp->bridge_identifier &= 0x0000ffffffffffffULL;
379 rstp->bridge_identifier |= (uint64_t)new_priority << 48;
380 set_bridge_priority__(rstp);
385 rstp_set_bridge_priority(struct rstp *rstp, int new_priority)
386 OVS_EXCLUDED(rstp_mutex)
388 ovs_mutex_lock(&rstp_mutex);
389 rstp_set_bridge_priority__(rstp, new_priority);
390 ovs_mutex_unlock(&rstp_mutex);
393 /* Sets the bridge ageing time. */
395 rstp_set_bridge_ageing_time__(struct rstp *rstp, int new_ageing_time)
396 OVS_REQUIRES(rstp_mutex)
398 if (new_ageing_time >= RSTP_MIN_AGEING_TIME
399 && new_ageing_time <= RSTP_MAX_AGEING_TIME) {
400 VLOG_DBG("%s: set ageing time to %d", rstp->name, new_ageing_time);
402 rstp->ageing_time = new_ageing_time;
407 rstp_set_bridge_ageing_time(struct rstp *rstp, int new_ageing_time)
408 OVS_EXCLUDED(rstp_mutex)
410 ovs_mutex_lock(&rstp_mutex);
411 rstp_set_bridge_ageing_time__(rstp, new_ageing_time);
412 ovs_mutex_unlock(&rstp_mutex);
415 /* Reinitializes RSTP when switching from RSTP mode to STP mode
419 reinitialize_rstp__(struct rstp *rstp)
420 OVS_REQUIRES(rstp_mutex)
423 static struct hmap ports;
426 /* Copy rstp in temp */
430 /* stop and clear rstp */
431 memset(rstp, 0, sizeof(struct rstp));
433 /* Initialize rstp. */
434 rstp->name = temp.name;
436 /* Initialize the ports hmap before calling any setters,
437 * so that the state machines will see an empty ports list. */
438 hmap_init(&rstp->ports);
440 /* Set bridge address. */
441 rstp_set_bridge_address__(rstp, temp.address);
442 /* Set default parameters values. */
443 rstp_set_bridge_priority__(rstp, RSTP_DEFAULT_PRIORITY);
444 rstp_set_bridge_ageing_time__(rstp, RSTP_DEFAULT_AGEING_TIME);
445 rstp_set_bridge_forward_delay__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
446 rstp_set_bridge_hello_time__(rstp);
447 rstp_set_bridge_max_age__(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
448 rstp_set_bridge_migrate_time__(rstp);
449 rstp_set_bridge_transmit_hold_count__(rstp,
450 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
451 rstp_set_bridge_times__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
452 RSTP_BRIDGE_HELLO_TIME,
453 RSTP_DEFAULT_BRIDGE_MAX_AGE, 0);
455 rstp->send_bpdu = temp.send_bpdu;
456 rstp->aux = temp.aux;
457 rstp->node = temp.node;
458 rstp->changes = false;
464 HMAP_FOR_EACH (p, node, &rstp->ports) {
465 reinitialize_port__(p);
468 rstp->ref_cnt = temp.ref_cnt;
471 /* Sets the force protocol version parameter. */
473 rstp_set_bridge_force_protocol_version__(struct rstp *rstp,
474 enum rstp_force_protocol_version new_force_protocol_version)
475 OVS_REQUIRES(rstp_mutex)
477 if (new_force_protocol_version != rstp->force_protocol_version &&
478 (new_force_protocol_version == FPV_STP_COMPATIBILITY ||
479 new_force_protocol_version == FPV_DEFAULT)) {
480 VLOG_DBG("%s: set bridge Force Protocol Version to %d", rstp->name,
481 new_force_protocol_version);
483 /* [17.13] The Spanning Tree Protocol Entity shall be reinitialized,
484 * as specified by the assertion of BEGIN (17.18.1) in the state
485 * machine specification.
487 reinitialize_rstp__(rstp);
488 rstp->force_protocol_version = new_force_protocol_version;
489 if (rstp->force_protocol_version < 2) {
490 rstp->stp_version = true;
491 rstp->rstp_version = false;
493 rstp->stp_version = false;
494 rstp->rstp_version = true;
496 rstp->changes = true;
502 rstp_set_bridge_force_protocol_version(struct rstp *rstp,
503 enum rstp_force_protocol_version new_force_protocol_version)
504 OVS_EXCLUDED(rstp_mutex)
506 ovs_mutex_lock(&rstp_mutex);
507 rstp_set_bridge_force_protocol_version__(rstp, new_force_protocol_version);
508 ovs_mutex_unlock(&rstp_mutex);
511 /* Sets the bridge Hello Time parameter. */
513 rstp_set_bridge_hello_time__(struct rstp *rstp)
514 OVS_REQUIRES(rstp_mutex)
516 VLOG_DBG("%s: set RSTP Hello Time to %d", rstp->name,
517 RSTP_BRIDGE_HELLO_TIME);
518 /* 2 is the only acceptable value. */
519 rstp->bridge_hello_time = RSTP_BRIDGE_HELLO_TIME;
522 /* Sets the bridge max age parameter. */
524 rstp_set_bridge_max_age__(struct rstp *rstp, int new_max_age)
525 OVS_REQUIRES(rstp_mutex)
527 if (rstp->bridge_max_age != new_max_age
528 && new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE
529 && new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) {
531 if ((2 * (rstp->bridge_forward_delay - 1) >= new_max_age)
532 && (new_max_age >= 2 * rstp->bridge_hello_time)) {
533 VLOG_DBG("%s: set RSTP bridge Max Age to %d", rstp->name,
536 rstp->bridge_max_age = new_max_age;
537 rstp->bridge_times.max_age = new_max_age;
538 rstp->changes = true;
539 updt_roles_tree__(rstp);
545 rstp_set_bridge_max_age(struct rstp *rstp, int new_max_age)
546 OVS_EXCLUDED(rstp_mutex)
548 ovs_mutex_lock(&rstp_mutex);
549 rstp_set_bridge_max_age__(rstp, new_max_age);
550 ovs_mutex_unlock(&rstp_mutex);
553 /* Sets the bridge forward delay parameter. */
555 rstp_set_bridge_forward_delay__(struct rstp *rstp, int new_forward_delay)
556 OVS_REQUIRES(rstp_mutex)
558 if (rstp->bridge_forward_delay != new_forward_delay
559 && new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY
560 && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) {
561 if (2 * (new_forward_delay - 1) >= rstp->bridge_max_age) {
562 VLOG_DBG("%s: set RSTP Forward Delay to %d", rstp->name,
564 rstp->bridge_forward_delay = new_forward_delay;
565 rstp->bridge_times.forward_delay = new_forward_delay;
566 rstp->changes = true;
567 updt_roles_tree__(rstp);
573 rstp_set_bridge_forward_delay(struct rstp *rstp, int new_forward_delay)
574 OVS_EXCLUDED(rstp_mutex)
576 ovs_mutex_lock(&rstp_mutex);
577 rstp_set_bridge_forward_delay__(rstp, new_forward_delay);
578 ovs_mutex_unlock(&rstp_mutex);
581 /* Sets the bridge transmit hold count parameter. */
583 rstp_set_bridge_transmit_hold_count__(struct rstp *rstp,
584 int new_transmit_hold_count)
585 OVS_REQUIRES(rstp_mutex)
587 if (new_transmit_hold_count >= RSTP_MIN_TRANSMIT_HOLD_COUNT
588 && new_transmit_hold_count <= RSTP_MAX_TRANSMIT_HOLD_COUNT) {
591 VLOG_DBG("%s: set RSTP Transmit Hold Count to %d", rstp->name,
592 new_transmit_hold_count);
593 /* Resetting txCount on all ports [17.13]. */
595 rstp->transmit_hold_count = new_transmit_hold_count;
596 HMAP_FOR_EACH (p, node, &rstp->ports) {
603 rstp_set_bridge_transmit_hold_count(struct rstp *rstp,
604 int new_transmit_hold_count)
605 OVS_EXCLUDED(rstp_mutex)
607 ovs_mutex_lock(&rstp_mutex);
608 rstp_set_bridge_transmit_hold_count__(rstp, new_transmit_hold_count);
609 ovs_mutex_unlock(&rstp_mutex);
612 /* Sets the bridge migrate time parameter. */
614 rstp_set_bridge_migrate_time__(struct rstp *rstp)
615 OVS_REQUIRES(rstp_mutex)
617 VLOG_DBG("%s: set RSTP Migrate Time to %d", rstp->name,
619 /* 3 is the only acceptable value */
620 rstp->migrate_time = RSTP_MIGRATE_TIME;
623 /* Sets the bridge times. */
625 rstp_set_bridge_times__(struct rstp *rstp, int new_forward_delay,
626 int new_hello_time, int new_max_age,
628 OVS_REQUIRES(rstp_mutex)
630 VLOG_DBG("%s: set RSTP times to (%d, %d, %d, %d)", rstp->name,
631 new_forward_delay, new_hello_time, new_max_age, new_message_age);
632 if (new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY
633 && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) {
634 rstp->bridge_times.forward_delay = new_forward_delay;
636 if (new_hello_time == RSTP_BRIDGE_HELLO_TIME) {
637 rstp->bridge_times.hello_time = new_hello_time;
639 if (new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE
640 && new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) {
641 rstp->bridge_times.max_age = new_max_age;
643 rstp->bridge_times.message_age = new_message_age;
646 /* Sets the port id, it is called by rstp_port_set_port_number__() or
647 * rstp_port_set_priority__().
650 set_port_id__(struct rstp_port *p)
651 OVS_REQUIRES(rstp_mutex)
656 /* [9.2.7] Port identifier. */
657 p->port_id = p->port_number | (p->priority << 8);
658 VLOG_DBG("%s: new RSTP port id "RSTP_PORT_ID_FMT"", rstp->name,
662 /* Sets the port priority. */
664 rstp_port_set_priority__(struct rstp_port *port, int priority)
665 OVS_REQUIRES(rstp_mutex)
667 if (priority >= RSTP_MIN_PORT_PRIORITY
668 && priority <= RSTP_MAX_PORT_PRIORITY) {
669 VLOG_DBG("%s, port %u: set RSTP port priority to %d", port->rstp->name,
670 port->port_number, priority);
672 priority -= priority % RSTP_STEP_PORT_PRIORITY;
673 port->priority = priority;
675 port->selected = false;
676 port->reselect = true;
680 /* Checks if a port number is available. */
682 is_port_number_available__(struct rstp *rstp, int n, struct rstp_port *port)
683 OVS_REQUIRES(rstp_mutex)
685 if (n >= 1 && n <= RSTP_MAX_PORTS) {
686 struct rstp_port *p = rstp_get_port__(rstp, n);
688 return p == NULL || p == port;
694 rstp_first_free_number__(struct rstp *rstp, struct rstp_port *rstp_port)
695 OVS_REQUIRES(rstp_mutex)
699 while (free_number <= RSTP_MAX_PORTS) {
700 if (is_port_number_available__(rstp, free_number, rstp_port)) {
705 VLOG_DBG("%s, No free port number available.", rstp->name);
709 /* Sets the port number. */
711 rstp_port_set_port_number__(struct rstp_port *port, uint16_t port_number)
712 OVS_REQUIRES(rstp_mutex)
714 /* If new_port_number is available, use it, otherwise use the first free
715 * available port number. */
717 is_port_number_available__(port->rstp, port_number, port)
719 : rstp_first_free_number__(port->rstp, port);
722 /* [17.13] is not clear. I suppose that a port number change
723 * should trigger reselection like a port priority change. */
724 port->selected = false;
725 port->reselect = true;
727 VLOG_DBG("%s: set new RSTP port number %d", port->rstp->name,
731 /* Converts the link speed to a port path cost [Table 17-3]. */
733 rstp_convert_speed_to_cost(unsigned int speed)
737 value = speed >= 10000000 ? 2 /* 10 Tb/s. */
738 : speed >= 1000000 ? 20 /* 1 Tb/s. */
739 : speed >= 100000 ? 200 /* 100 Gb/s. */
740 : speed >= 10000 ? 2000 /* 10 Gb/s. */
741 : speed >= 1000 ? 20000 /* 1 Gb/s. */
742 : speed >= 100 ? 200000 /* 100 Mb/s. */
743 : speed >= 10 ? 2000000 /* 10 Mb/s. */
744 : speed >= 1 ? 20000000 /* 1 Mb/s. */
745 : RSTP_DEFAULT_PORT_PATH_COST; /* 100 Mb/s. */
750 /* Sets the port path cost. */
752 rstp_port_set_path_cost__(struct rstp_port *port, uint32_t path_cost)
753 OVS_REQUIRES(rstp_mutex)
755 if (path_cost >= RSTP_MIN_PORT_PATH_COST
756 && path_cost <= RSTP_MAX_PORT_PATH_COST) {
757 VLOG_DBG("%s, port %u, set RSTP port path cost to %d",
758 port->rstp->name, port->port_number, path_cost);
760 port->port_path_cost = path_cost;
761 port->selected = false;
762 port->reselect = true;
766 /* Gets the root path cost. */
768 rstp_get_root_path_cost(const struct rstp *rstp)
769 OVS_EXCLUDED(rstp_mutex)
773 ovs_mutex_lock(&rstp_mutex);
774 cost = rstp->root_priority.root_path_cost;
775 ovs_mutex_unlock(&rstp_mutex);
779 /* Finds a port which needs to flush its own MAC learning table. A NULL
780 * pointer is returned if no port needs to flush its MAC learning table.
781 * '*port' needs to be NULL in the first call to start the iteration. If
782 * '*port' is passed as non-NULL, it must be the value set by the last
783 * invocation of this function. */
785 rstp_check_and_reset_fdb_flush(struct rstp *rstp, struct rstp_port **port)
786 OVS_EXCLUDED(rstp_mutex)
790 ovs_mutex_lock(&rstp_mutex);
794 HMAP_FOR_EACH (p, node, &rstp->ports) {
801 } else { /* continue */
802 struct rstp_port *p = *port;
804 HMAP_FOR_EACH_CONTINUE (p, node, &rstp->ports) {
812 /* No port needs flushing. */
815 /* fdb_flush should be reset by the filtering database
816 * once the entries are removed if rstp_version is TRUE, and
817 * immediately if stp_version is TRUE.*/
819 (*port)->fdb_flush = false;
821 ovs_mutex_unlock(&rstp_mutex);
825 /* Finds a port whose state has changed, and returns the aux pointer set for
826 * the port. A NULL pointer is returned when no changed port is found. On
827 * return '*portp' contains the pointer to the rstp port that changed, or NULL
828 * if no changed port can be found.
830 * If '*portp' is passed as non-NULL, it must be the value set by the last
831 * invocation of this function.
833 * This function may only be called by the thread that creates and deletes
834 * ports. Otherwise this function is not thread safe, as the returned
835 * '*portp' could become stale before it is used in the next invocation. */
837 rstp_get_next_changed_port_aux(struct rstp *rstp, struct rstp_port **portp)
841 ovs_mutex_lock(&rstp_mutex);
842 if (*portp == NULL) {
845 HMAP_FOR_EACH (p, node, &rstp->ports) {
846 if (p->state_changed) {
847 p->state_changed = false;
853 } else { /* continue */
854 struct rstp_port *p = *portp;
856 HMAP_FOR_EACH_CONTINUE (p, node, &rstp->ports) {
857 if (p->state_changed) {
858 p->state_changed = false;
865 /* No changed port found. */
868 ovs_mutex_unlock(&rstp_mutex);
872 /* Returns the port in 'rstp' with number 'port_number'.
874 * XXX: May only be called while concurrent deletion of ports is excluded. */
875 static struct rstp_port *
876 rstp_get_port__(struct rstp *rstp, uint16_t port_number)
877 OVS_REQUIRES(rstp_mutex)
879 struct rstp_port *port;
881 ovs_assert(rstp && port_number > 0 && port_number <= RSTP_MAX_PORTS);
883 HMAP_FOR_EACH_WITH_HASH (port, node, hash_int(port_number, 0),
885 if (port->port_number == port_number) {
893 rstp_get_port(struct rstp *rstp, uint16_t port_number)
894 OVS_EXCLUDED(rstp_mutex)
898 ovs_mutex_lock(&rstp_mutex);
899 p = rstp_get_port__(rstp, port_number);
900 ovs_mutex_unlock(&rstp_mutex);
905 rstp_get_port_aux(struct rstp *rstp, uint16_t port_number)
906 OVS_EXCLUDED(rstp_mutex)
911 ovs_mutex_lock(&rstp_mutex);
912 p = rstp_get_port__(rstp, port_number);
914 ovs_mutex_unlock(&rstp_mutex);
918 /* Updates the port_enabled parameter. */
920 update_port_enabled__(struct rstp_port *p)
921 OVS_REQUIRES(rstp_mutex)
923 if (p->mac_operational && p->is_administrative_bridge_port
924 == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) {
925 p->port_enabled = true;
927 p->port_enabled = false;
931 /* Sets the port MAC_Operational parameter [6.4.2]. */
933 rstp_port_set_mac_operational(struct rstp_port *p, bool new_mac_operational)
934 OVS_EXCLUDED(rstp_mutex)
938 ovs_mutex_lock(&rstp_mutex);
940 if (p->mac_operational != new_mac_operational) {
941 p->mac_operational = new_mac_operational;
942 update_port_enabled__(p);
943 rstp->changes = true;
946 ovs_mutex_unlock(&rstp_mutex);
949 /* Sets the port Administrative Bridge Port parameter. */
951 rstp_port_set_administrative_bridge_port__(struct rstp_port *p,
952 uint8_t admin_port_state,
954 OVS_REQUIRES(rstp_mutex)
956 VLOG_DBG("%s, port %u: set RSTP port admin-port-state to %d",
957 p->rstp->name, p->port_number, admin_port_state);
959 if (admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_DISABLED
960 || admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) {
962 p->is_administrative_bridge_port = admin_port_state;
963 update_port_enabled__(p);
966 struct rstp *rstp = p->rstp;
968 rstp->changes = true;
974 /* Sets the port oper_point_to_point_mac parameter. */
976 rstp_port_set_oper_point_to_point_mac__(struct rstp_port *p,
977 uint8_t new_oper_p2p_mac)
978 OVS_REQUIRES(rstp_mutex)
980 if (new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_DISABLED
981 || new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_ENABLED) {
983 p->oper_point_to_point_mac = new_oper_p2p_mac;
984 update_port_enabled__(p);
988 /* Initializes a port with the defaults values for its parameters. */
990 rstp_initialize_port_defaults__(struct rstp_port *p)
991 OVS_REQUIRES(rstp_mutex)
993 rstp_port_set_administrative_bridge_port__(p,
994 RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED,
996 rstp_port_set_oper_point_to_point_mac__(p,
997 RSTP_OPER_P2P_MAC_STATE_ENABLED);
998 rstp_port_set_path_cost__(p, RSTP_DEFAULT_PORT_PATH_COST);
999 rstp_port_set_admin_edge__(p, false);
1000 rstp_port_set_auto_edge__(p, true);
1001 rstp_port_set_mcheck__(p, false);
1003 /* Initialize state machines. */
1004 p->port_receive_sm_state = PORT_RECEIVE_SM_INIT;
1005 p->port_protocol_migration_sm_state = PORT_PROTOCOL_MIGRATION_SM_INIT;
1006 p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_INIT;
1007 p->port_transmit_sm_state = PORT_TRANSMIT_SM_INIT;
1008 p->port_information_sm_state = PORT_INFORMATION_SM_INIT;
1009 p->port_role_transition_sm_state = PORT_ROLE_TRANSITION_SM_INIT;
1010 p->port_state_transition_sm_state = PORT_STATE_TRANSITION_SM_INIT;
1011 p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INIT;
1017 reinitialize_port__(struct rstp_port *p)
1018 OVS_REQUIRES(rstp_mutex)
1020 struct rstp_port temp_port;
1025 memset(p, 0, sizeof(struct rstp_port));
1027 p->ref_cnt = temp_port.ref_cnt;
1029 p->node = temp_port.node;
1030 p->aux = temp_port.aux;
1031 p->port_number = temp_port.port_number;
1032 p->port_priority = temp_port.port_priority;
1033 p->port_id = temp_port.port_id;
1034 p->rstp_state = RSTP_DISCARDING;
1036 rstp_initialize_port_defaults__(p);
1038 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" reinitialized.", rstp->name,
1043 reinitialize_port(struct rstp_port *p)
1044 OVS_EXCLUDED(rstp_mutex)
1046 ovs_mutex_lock(&rstp_mutex);
1047 reinitialize_port__(p);
1048 ovs_mutex_unlock(&rstp_mutex);
1051 /* Sets the port state. */
1053 rstp_port_set_state__(struct rstp_port *p, enum rstp_state state)
1054 OVS_REQUIRES(rstp_mutex)
1059 VLOG_DBG("%s, port %u: set RSTP port state %s -> %s", rstp->name,
1061 rstp_state_name(p->rstp_state), rstp_state_name(state));
1063 if (state != p->rstp_state && !p->state_changed) {
1064 p->state_changed = true;
1065 seq_change(connectivity_seq_get());
1067 p->rstp_state = state;
1071 rstp_port_set_state(struct rstp_port *p, enum rstp_state state)
1072 OVS_EXCLUDED(rstp_mutex)
1074 ovs_mutex_lock(&rstp_mutex);
1075 rstp_port_set_state__(p, state);
1076 ovs_mutex_unlock(&rstp_mutex);
1079 /* Adds a RSTP port. */
1081 rstp_add_port(struct rstp *rstp)
1082 OVS_EXCLUDED(rstp_mutex)
1084 struct rstp_port *p = xzalloc(sizeof *p);
1086 ovs_refcount_init(&p->ref_cnt);
1088 ovs_mutex_lock(&rstp_mutex);
1090 rstp_port_set_priority__(p, RSTP_DEFAULT_PORT_PRIORITY);
1091 rstp_port_set_port_number__(p, 0);
1093 rstp_initialize_port_defaults__(p);
1094 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" initialized.", rstp->name,
1097 rstp_port_set_state__(p, RSTP_DISCARDING);
1098 hmap_insert(&rstp->ports, &p->node, hash_int(p->port_number, 0));
1099 rstp->changes = true;
1101 VLOG_DBG("%s: added port "RSTP_PORT_ID_FMT"", rstp->name, p->port_id);
1102 ovs_mutex_unlock(&rstp_mutex);
1106 /* Caller has to hold a reference to prevent 'rstp_port' from being deleted
1107 * while taking a new reference. */
1109 rstp_port_ref(const struct rstp_port *rp_)
1110 OVS_EXCLUDED(rstp_mutex)
1112 struct rstp_port *rp = CONST_CAST(struct rstp_port *, rp_);
1115 ovs_refcount_ref(&rp->ref_cnt);
1120 /* Frees RSTP struct. This can be caller by any thread. */
1122 rstp_port_unref(struct rstp_port *rp)
1123 OVS_EXCLUDED(rstp_mutex)
1125 if (rp && ovs_refcount_unref_relaxed(&rp->ref_cnt) == 1) {
1128 ovs_mutex_lock(&rstp_mutex);
1130 rstp_port_set_state__(rp, RSTP_DISABLED);
1131 hmap_remove(&rstp->ports, &rp->node);
1132 VLOG_DBG("%s: removed port "RSTP_PORT_ID_FMT"", rstp->name,
1134 ovs_mutex_unlock(&rstp_mutex);
1139 /* Sets the port Admin Edge parameter. */
1141 rstp_port_set_admin_edge__(struct rstp_port *port, bool admin_edge)
1142 OVS_REQUIRES(rstp_mutex)
1144 if (port->admin_edge != admin_edge) {
1145 VLOG_DBG("%s, port %u: set RSTP Admin Edge to %d", port->rstp->name,
1146 port->port_number, admin_edge);
1148 port->admin_edge = admin_edge;
1152 /* Sets the port Auto Edge parameter. */
1154 rstp_port_set_auto_edge__(struct rstp_port *port, bool auto_edge)
1155 OVS_REQUIRES(rstp_mutex)
1157 if (port->auto_edge != auto_edge) {
1158 VLOG_DBG("%s, port %u: set RSTP Auto Edge to %d", port->rstp->name,
1159 port->port_number, auto_edge);
1161 port->auto_edge = auto_edge;
1165 /* Sets the port admin_point_to_point_mac parameter. */
1166 static void rstp_port_set_admin_point_to_point_mac__(struct rstp_port *port,
1167 enum rstp_admin_point_to_point_mac_state admin_p2p_mac_state)
1168 OVS_REQUIRES(rstp_mutex)
1170 VLOG_DBG("%s, port %u: set RSTP port admin-point-to-point-mac to %d",
1171 port->rstp->name, port->port_number, admin_p2p_mac_state);
1173 if (admin_p2p_mac_state == RSTP_ADMIN_P2P_MAC_FORCE_TRUE) {
1174 port->admin_point_to_point_mac = admin_p2p_mac_state;
1175 rstp_port_set_oper_point_to_point_mac__(port,
1176 RSTP_OPER_P2P_MAC_STATE_ENABLED);
1177 } else if (admin_p2p_mac_state == RSTP_ADMIN_P2P_MAC_FORCE_FALSE) {
1178 port->admin_point_to_point_mac = admin_p2p_mac_state;
1179 rstp_port_set_oper_point_to_point_mac__(port,
1180 RSTP_OPER_P2P_MAC_STATE_DISABLED);
1181 } else if (admin_p2p_mac_state == RSTP_ADMIN_P2P_MAC_AUTO) {
1182 /* If adminPointToPointMAC is set to Auto, then the value of
1183 * operPointToPointMAC is determined in accordance with the
1184 * specific procedures defined for the MAC entity concerned, as
1185 * defined in 6.5. If these procedures determine that the MAC
1186 * entity is connected to a point-to-point LAN, then
1187 * operPointToPointMAC is set TRUE; otherwise it is set FALSE.
1188 * In the absence of a specific definition of how to determine
1189 * whether the MAC is connected to a point-to-point LAN or not,
1190 * the value of operPointToPointMAC shall be FALSE. */
1191 port->admin_point_to_point_mac = admin_p2p_mac_state;
1192 rstp_port_set_oper_point_to_point_mac__(
1193 port, RSTP_OPER_P2P_MAC_STATE_DISABLED);
1197 /* Sets the port mcheck parameter.
1198 * [17.19.13] May be set by management to force the Port Protocol Migration
1199 * state machine to transmit RST BPDUs for a MigrateTime (17.13.9) period, to
1200 * test whether all STP Bridges (17.4) on the attached LAN have been removed
1201 * and the Port can continue to transmit RSTP BPDUs. Setting mcheck has no
1202 * effect if stpVersion (17.20.12) is TRUE, i.e., the Bridge is operating in
1203 * STP Compatibility mode.
1206 rstp_port_set_mcheck__(struct rstp_port *port, bool mcheck)
1207 OVS_REQUIRES(rstp_mutex)
1209 if (mcheck == true && port->rstp->force_protocol_version >= 2) {
1210 port->mcheck = true;
1212 VLOG_DBG("%s, port %u: set RSTP mcheck to %d", port->rstp->name,
1213 port->port_number, mcheck);
1217 /* Returns the designated bridge id. */
1219 rstp_get_designated_id(const struct rstp *rstp)
1220 OVS_EXCLUDED(rstp_mutex)
1222 rstp_identifier designated_id;
1224 ovs_mutex_lock(&rstp_mutex);
1225 designated_id = rstp->root_priority.designated_bridge_id;
1226 ovs_mutex_unlock(&rstp_mutex);
1228 return designated_id;
1231 /* Returns the root bridge id. */
1233 rstp_get_root_id(const struct rstp *rstp)
1234 OVS_EXCLUDED(rstp_mutex)
1236 rstp_identifier root_id;
1238 ovs_mutex_lock(&rstp_mutex);
1239 root_id = rstp->root_priority.root_bridge_id;
1240 ovs_mutex_unlock(&rstp_mutex);
1245 /* Returns the designated port id. */
1247 rstp_get_designated_port_id(const struct rstp *rstp)
1248 OVS_EXCLUDED(rstp_mutex)
1250 uint16_t designated_port_id;
1252 ovs_mutex_lock(&rstp_mutex);
1253 designated_port_id = rstp->root_priority.designated_port_id;
1254 ovs_mutex_unlock(&rstp_mutex);
1256 return designated_port_id;
1259 /* Return the bridge port id. */
1261 rstp_get_bridge_port_id(const struct rstp *rstp)
1262 OVS_EXCLUDED(rstp_mutex)
1264 uint16_t bridge_port_id;
1266 ovs_mutex_lock(&rstp_mutex);
1267 bridge_port_id = rstp->root_priority.bridge_port_id;
1268 ovs_mutex_unlock(&rstp_mutex);
1270 return bridge_port_id;
1273 /* Returns true if the bridge believes to the be root of the spanning tree,
1277 rstp_is_root_bridge(const struct rstp *rstp)
1278 OVS_EXCLUDED(rstp_mutex)
1282 ovs_mutex_lock(&rstp_mutex);
1283 is_root = rstp->bridge_identifier ==
1284 rstp->root_priority.designated_bridge_id;
1285 ovs_mutex_unlock(&rstp_mutex);
1290 /* Returns the bridge ID of the bridge currently believed to be the root. */
1292 rstp_get_designated_root(const struct rstp *rstp)
1293 OVS_EXCLUDED(rstp_mutex)
1295 rstp_identifier designated_root;
1297 ovs_mutex_lock(&rstp_mutex);
1298 designated_root = rstp->root_priority.designated_bridge_id;
1299 ovs_mutex_unlock(&rstp_mutex);
1301 return designated_root;
1304 /* Returns the port connecting 'rstp' to the root bridge, or a null pointer if
1305 * there is no such port.
1308 rstp_get_root_port(struct rstp *rstp)
1309 OVS_EXCLUDED(rstp_mutex)
1311 struct rstp_port *p;
1313 ovs_mutex_lock(&rstp_mutex);
1314 HMAP_FOR_EACH (p, node, &rstp->ports) {
1315 if (p->port_id == rstp->root_port_id) {
1316 ovs_mutex_unlock(&rstp_mutex);
1320 ovs_mutex_unlock(&rstp_mutex);
1324 /* Returns the state of port 'p'. */
1326 rstp_port_get_state(const struct rstp_port *p)
1328 enum rstp_state state;
1330 ovs_mutex_lock(&rstp_mutex);
1331 state = p->rstp_state;
1332 ovs_mutex_unlock(&rstp_mutex);
1337 /* Retrieves port status. */
1339 rstp_port_get_status(const struct rstp_port *p, uint16_t *id,
1340 enum rstp_state *state, enum rstp_port_role *role,
1341 rstp_identifier *designated_bridge_id,
1342 uint16_t *designated_port_id,
1343 uint32_t *designated_path_cost, int *tx_count,
1344 int *rx_count, int *error_count, int *uptime)
1345 OVS_EXCLUDED(rstp_mutex)
1347 ovs_mutex_lock(&rstp_mutex);
1349 *state = p->rstp_state;
1352 *designated_bridge_id = p->port_priority.designated_bridge_id;
1353 *designated_port_id = p->port_priority.designated_port_id;
1354 *designated_path_cost = p->port_priority.root_path_cost;
1356 *tx_count = p->tx_count;
1357 *rx_count = p->rx_rstp_bpdu_cnt;
1358 *error_count = p->error_count;
1359 *uptime = p->uptime;
1360 ovs_mutex_unlock(&rstp_mutex);
1364 rstp_port_set(struct rstp_port *port, uint16_t port_num, int priority,
1365 uint32_t path_cost, bool is_admin_edge, bool is_auto_edge,
1366 enum rstp_admin_point_to_point_mac_state admin_p2p_mac_state,
1367 bool admin_port_state, bool do_mcheck, void *aux)
1368 OVS_EXCLUDED(rstp_mutex)
1370 ovs_mutex_lock(&rstp_mutex);
1372 rstp_port_set_priority__(port, priority);
1373 rstp_port_set_port_number__(port, port_num);
1374 rstp_port_set_path_cost__(port, path_cost);
1375 rstp_port_set_admin_edge__(port, is_admin_edge);
1376 rstp_port_set_auto_edge__(port, is_auto_edge);
1377 rstp_port_set_admin_point_to_point_mac__(port, admin_p2p_mac_state);
1378 rstp_port_set_administrative_bridge_port__(port, admin_port_state, false);
1379 rstp_port_set_mcheck__(port, do_mcheck);
1380 ovs_mutex_unlock(&rstp_mutex);
1383 /* Individual setters only used by test-rstp.c. */
1385 rstp_port_set_priority(struct rstp_port *port, int priority)
1386 OVS_EXCLUDED(rstp_mutex)
1388 ovs_mutex_lock(&rstp_mutex);
1389 rstp_port_set_priority__(port, priority);
1390 ovs_mutex_unlock(&rstp_mutex);
1394 rstp_port_set_path_cost(struct rstp_port *port, uint32_t path_cost)
1395 OVS_EXCLUDED(rstp_mutex)
1397 ovs_mutex_lock(&rstp_mutex);
1398 rstp_port_set_path_cost__(port, path_cost);
1399 ovs_mutex_unlock(&rstp_mutex);
1403 rstp_port_set_aux(struct rstp_port *port, void *aux)
1404 OVS_EXCLUDED(rstp_mutex)
1406 ovs_mutex_lock(&rstp_mutex);
1408 ovs_mutex_unlock(&rstp_mutex);
1412 static struct rstp *
1413 rstp_find(const char *name)
1414 OVS_REQUIRES(rstp_mutex)
1418 LIST_FOR_EACH (rstp, node, all_rstps) {
1419 if (!strcmp(rstp->name, name)) {
1427 rstp_unixctl_tcn(struct unixctl_conn *conn, int argc,
1428 const char *argv[], void *aux OVS_UNUSED)
1429 OVS_EXCLUDED(rstp_mutex)
1431 ovs_mutex_lock(&rstp_mutex);
1433 struct rstp *rstp = rstp_find(argv[1]);
1435 unixctl_command_reply_error(conn, "No such RSTP object");
1438 rstp->changes = true;
1442 LIST_FOR_EACH (rstp, node, all_rstps) {
1443 rstp->changes = true;
1447 unixctl_command_reply(conn, "OK");
1450 ovs_mutex_unlock(&rstp_mutex);