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 static struct ovs_mutex mutex;
52 static struct list all_rstps__ = LIST_INITIALIZER(&all_rstps__);
53 static struct list *const all_rstps OVS_GUARDED_BY(mutex) = &all_rstps__;
55 /* Internal use only */
56 static void set_port_id__(struct rstp_port *);
57 static void update_port_enabled__(struct rstp_port *);
58 static void set_bridge_priority__(struct rstp *);
59 static void reinitialize_rstp__(struct rstp *);
60 static bool is_port_number_taken__(struct rstp *, int, struct rstp_port *);
61 static uint16_t rstp_first_free_number__(struct rstp *, struct rstp_port *);
62 static void rstp_initialize_port__(struct rstp_port *);
65 rstp_state_name(enum rstp_state state)
82 rstp_port_role_name(enum rstp_port_role role)
100 /* Caller has to hold a reference to prevent 'rstp' from being deleted
101 * while we are taking a new reference. */
103 rstp_ref(struct rstp *rstp)
106 ovs_refcount_ref(&rstp->ref_cnt);
111 /* Frees RSTP struct */
113 rstp_unref(struct rstp *rstp)
115 if (rstp && ovs_refcount_unref(&rstp->ref_cnt) == 1) {
116 ovs_mutex_lock(&mutex);
117 if (rstp->ports_count > 0) {
120 LIST_FOR_EACH (p, node, &rstp->ports) {
124 list_remove(&rstp->node);
125 ovs_mutex_unlock(&mutex);
131 /* Returns the port number. Mutex is needed to guard against
132 * concurrent reinitialization (which can temporarily clear the
135 rstp_port_number(const struct rstp_port *p)
139 ovs_mutex_lock(&mutex);
140 number = p->port_number;
141 ovs_mutex_unlock(&mutex);
145 static void rstp_unixctl_tcn(struct unixctl_conn *, int argc,
146 const char *argv[], void *aux);
148 /* Decrements the State Machines' timers. */
150 rstp_tick_timers(struct rstp *rstp)
152 ovs_mutex_lock(&mutex);
153 decrease_rstp_port_timers(rstp);
154 ovs_mutex_unlock(&mutex);
157 /* Processes an incoming BPDU. */
159 rstp_received_bpdu(struct rstp_port *p, const void *bpdu, size_t bpdu_size)
161 ovs_mutex_lock(&mutex);
162 process_received_bpdu(p, bpdu, bpdu_size);
163 ovs_mutex_unlock(&mutex);
169 unixctl_command_register("rstp/tcn", "[bridge]", 0, 1, rstp_unixctl_tcn,
173 /* Creates and returns a new RSTP instance that initially has no ports. */
175 rstp_create(const char *name, rstp_identifier bridge_address,
176 void (*send_bpdu)(struct ofpbuf *bpdu, int port_no, void *aux),
179 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
182 VLOG_DBG("Creating RSTP instance");
183 if (ovsthread_once_start(&once)) {
184 ovs_mutex_init_recursive(&mutex);
185 ovsthread_once_done(&once);
188 rstp = xzalloc(sizeof *rstp);
189 rstp->name = xstrdup(name);
190 /* Set bridge address. */
191 rstp_set_bridge_address(rstp, bridge_address);
192 /* Set default parameters values. */
193 rstp_set_bridge_priority(rstp, RSTP_DEFAULT_PRIORITY);
194 rstp_set_bridge_ageing_time(rstp, RSTP_DEFAULT_AGEING_TIME);
195 rstp_set_bridge_force_protocol_version(rstp, FPV_DEFAULT);
196 rstp_set_bridge_forward_delay(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
197 rstp_set_bridge_hello_time(rstp);
198 rstp_set_bridge_max_age(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
199 rstp_set_bridge_migrate_time(rstp);
200 rstp_set_bridge_transmit_hold_count(rstp,
201 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
202 rstp_set_bridge_times(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
203 RSTP_BRIDGE_HELLO_TIME, RSTP_DEFAULT_BRIDGE_MAX_AGE,
205 rstp->send_bpdu = send_bpdu;
207 rstp->changes = false;
210 /* Initialize the ports list. */
211 list_init(&rstp->ports);
212 ovs_refcount_init(&rstp->ref_cnt);
214 ovs_mutex_lock(&mutex);
215 list_push_back(all_rstps, &rstp->node);
216 ovs_mutex_unlock(&mutex);
218 VLOG_DBG("RSTP instance creation done");
222 /* Called by rstp_set_bridge_address() and rstp_set_bridge_priority(),
223 * it updates the bridge priority vector according to the values passed by
227 set_bridge_priority__(struct rstp *rstp)
229 rstp->bridge_priority.root_bridge_id = rstp->bridge_identifier;
230 rstp->bridge_priority.designated_bridge_id = rstp->bridge_identifier;
231 VLOG_DBG("%s: new bridge identifier: "RSTP_ID_FMT"", rstp->name,
232 RSTP_ID_ARGS(rstp->bridge_identifier));
235 /* Sets the bridge address. */
237 rstp_set_bridge_address(struct rstp *rstp, rstp_identifier bridge_address)
241 VLOG_DBG("%s: set bridge address to: "RSTP_ID_FMT"", rstp->name,
242 RSTP_ID_ARGS(bridge_address));
244 ovs_mutex_lock(&mutex);
245 rstp->address = bridge_address;
246 rstp->bridge_identifier = bridge_address;
247 set_bridge_priority__(rstp);
249 /* [17.13] When the bridge address changes, recalculates all priority
252 if (rstp->ports_count > 0) {
253 LIST_FOR_EACH (p, node, &rstp->ports) {
258 rstp->changes = true;
259 updt_roles_tree(rstp);
260 ovs_mutex_unlock(&mutex);
264 rstp_get_name(const struct rstp *rstp)
268 ovs_mutex_lock(&mutex);
270 ovs_mutex_unlock(&mutex);
275 rstp_get_bridge_id(const struct rstp *rstp)
277 rstp_identifier bridge_id;
279 ovs_mutex_lock(&mutex);
280 bridge_id = rstp->bridge_identifier;
281 ovs_mutex_unlock(&mutex);
285 /* Sets the bridge priority. */
287 rstp_set_bridge_priority(struct rstp *rstp, int new_priority)
291 if (new_priority >= RSTP_MIN_PRIORITY &&
292 new_priority <= RSTP_MAX_PRIORITY) {
293 VLOG_DBG("%s: set bridge priority to %d", rstp->name,
294 (new_priority / 4096) * 4096);
295 ovs_mutex_lock(&mutex);
296 rstp->priority = (new_priority / 4096) * 4096;
297 rstp->bridge_identifier &= 0x0000ffffffffffffULL;
298 rstp->bridge_identifier |=
299 (uint64_t) ((new_priority / 4096) * 4096) << 48;
300 set_bridge_priority__(rstp);
303 if (rstp->ports_count > 0){
304 LIST_FOR_EACH (p, node, &rstp->ports) {
309 rstp->changes = true;
310 updt_roles_tree(rstp);
311 ovs_mutex_unlock(&mutex);
315 /* Sets the bridge ageing time. */
317 rstp_set_bridge_ageing_time(struct rstp *rstp, int new_ageing_time)
319 if (new_ageing_time >= RSTP_MIN_AGEING_TIME
320 && new_ageing_time <= RSTP_MAX_AGEING_TIME) {
321 VLOG_DBG("%s: set ageing time to %d", rstp->name, new_ageing_time);
323 ovs_mutex_lock(&mutex);
324 rstp->ageing_time = new_ageing_time;
325 ovs_mutex_unlock(&mutex);
329 /* Reinitializes RSTP when switching from RSTP mode to STP mode
333 reinitialize_rstp__(struct rstp *rstp)
337 static struct list ports;
339 /* Copy rstp in temp */
343 /* stop and clear rstp */
344 memset(rstp, 0, sizeof(struct rstp));
346 /* Initialize rstp. */
347 rstp->name = temp.name;
348 /* Set bridge address. */
349 rstp_set_bridge_address(rstp, temp.address);
350 /* Set default parameters values. */
351 rstp_set_bridge_priority(rstp, RSTP_DEFAULT_PRIORITY);
352 rstp_set_bridge_ageing_time(rstp, RSTP_DEFAULT_AGEING_TIME);
353 rstp_set_bridge_forward_delay(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
354 rstp_set_bridge_hello_time(rstp);
355 rstp_set_bridge_max_age(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
356 rstp_set_bridge_migrate_time(rstp);
357 rstp_set_bridge_transmit_hold_count(rstp,
358 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
359 rstp_set_bridge_times(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
360 RSTP_BRIDGE_HELLO_TIME, RSTP_DEFAULT_BRIDGE_MAX_AGE,
363 rstp->send_bpdu = temp.send_bpdu;
364 rstp->aux = temp.aux;
365 rstp->node = temp.node;
366 rstp->changes = false;
369 rstp->ports_count = temp.ports_count;
371 if (rstp->ports_count > 0) {
372 struct rstp_port *p, temp_port;
374 LIST_FOR_EACH (p, node, &rstp->ports) {
376 memset(p, 0, sizeof(struct rstp_port));
378 p->node = temp_port.node;
379 p->aux = temp_port.aux;
380 p->port_number = temp_port.port_number;
381 p->port_priority = temp_port.port_priority;
382 p->port_id = temp_port.port_id;
383 p->rstp_state = RSTP_DISCARDING;
385 rstp_port_set_administrative_bridge_port(p,
386 RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED);
387 rstp_port_set_oper_point_to_point_mac(p, 1);
388 rstp_port_set_path_cost(p, RSTP_DEFAULT_PORT_PATH_COST);
389 rstp_port_set_auto_edge(p, true);
390 /* Initialize state machines. */
391 p->port_receive_sm_state = PORT_RECEIVE_SM_INIT;
392 p->port_protocol_migration_sm_state =
393 PORT_PROTOCOL_MIGRATION_SM_INIT;
394 p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_INIT;
395 p->port_transmit_sm_state = PORT_TRANSMIT_SM_INIT;
396 p->port_information_sm_state = PORT_INFORMATION_SM_INIT;
397 p->port_role_transition_sm_state = PORT_ROLE_TRANSITION_SM_INIT;
398 p->port_state_transition_sm_state = PORT_STATE_TRANSITION_SM_INIT;
399 p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INIT;
403 rstp->ref_cnt = temp.ref_cnt;
406 /* Sets the force protocol version parameter. */
408 rstp_set_bridge_force_protocol_version(struct rstp *rstp,
409 enum rstp_force_protocol_version new_force_protocol_version)
411 if (new_force_protocol_version != rstp->force_protocol_version &&
412 (new_force_protocol_version == FPV_STP_COMPATIBILITY ||
413 new_force_protocol_version == FPV_DEFAULT)) {
414 VLOG_DBG("%s: set bridge Force Protocol Version to %d", rstp->name,
415 new_force_protocol_version);
416 ovs_mutex_lock(&mutex);
417 /* [17.13] The Spanning Tree Protocol Entity shall be reinitialized,
418 * as specified by the assertion of BEGIN (17.18.1) in the state
419 * machine specification.
421 reinitialize_rstp__(rstp);
422 rstp->force_protocol_version = new_force_protocol_version;
423 if (rstp->force_protocol_version < 2) {
424 rstp->stp_version = true;
425 rstp->rstp_version = false;
427 rstp->stp_version = false;
428 rstp->rstp_version = true;
430 rstp->changes = true;
432 ovs_mutex_unlock(&mutex);
436 /* Sets the bridge Hello Time parameter. */
438 rstp_set_bridge_hello_time(struct rstp *rstp)
440 VLOG_DBG("%s: set RSTP Hello Time to %d", rstp->name,
441 RSTP_BRIDGE_HELLO_TIME);
442 /* 2 is the only acceptable value. */
443 ovs_mutex_lock(&mutex);
444 rstp->bridge_hello_time = RSTP_BRIDGE_HELLO_TIME;
445 ovs_mutex_unlock(&mutex);
448 /* Sets the bridge max age parameter. */
450 rstp_set_bridge_max_age(struct rstp *rstp, int new_max_age)
452 if (new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE &&
453 new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) {
455 if ((2 * (rstp->bridge_forward_delay - 1) >= new_max_age)
456 && (new_max_age >= 2 * rstp->bridge_hello_time)) {
457 VLOG_DBG("%s: set RSTP bridge Max Age to %d", rstp->name,
459 ovs_mutex_lock(&mutex);
460 rstp->bridge_max_age = new_max_age;
461 rstp->bridge_times.max_age = new_max_age;
462 ovs_mutex_unlock(&mutex);
467 /* Sets the bridge forward delay parameter. */
469 rstp_set_bridge_forward_delay(struct rstp *rstp, int new_forward_delay)
471 if (new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY
472 && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) {
473 if (2 * (new_forward_delay - 1) >= rstp->bridge_max_age) {
474 VLOG_DBG("%s: set RSTP Forward Delay to %d", rstp->name,
476 ovs_mutex_lock(&mutex);
477 rstp->bridge_forward_delay = new_forward_delay;
478 rstp->bridge_times.forward_delay = new_forward_delay;
479 ovs_mutex_unlock(&mutex);
484 /* Sets the bridge transmit hold count parameter. */
486 rstp_set_bridge_transmit_hold_count(struct rstp *rstp,
487 int new_transmit_hold_count)
491 if (new_transmit_hold_count >= RSTP_MIN_TRANSMIT_HOLD_COUNT
492 && new_transmit_hold_count <= RSTP_MAX_TRANSMIT_HOLD_COUNT) {
493 VLOG_DBG("%s: set RSTP Transmit Hold Count to %d", rstp->name,
494 new_transmit_hold_count);
495 /* Resetting txCount on all ports [17.13]. */
496 ovs_mutex_lock(&mutex);
497 rstp->transmit_hold_count = new_transmit_hold_count;
498 if (rstp->ports_count > 0) {
499 LIST_FOR_EACH (p, node, &rstp->ports) {
503 ovs_mutex_unlock(&mutex);
507 /* Sets the bridge migrate time parameter. */
509 rstp_set_bridge_migrate_time(struct rstp *rstp)
511 VLOG_DBG("%s: set RSTP Migrate Time to %d", rstp->name,
513 /* 3 is the only acceptable value */
514 ovs_mutex_lock(&mutex);
515 rstp->migrate_time = RSTP_MIGRATE_TIME;
516 ovs_mutex_unlock(&mutex);
519 /* Sets the bridge times. */
521 rstp_set_bridge_times(struct rstp *rstp, int new_forward_delay,
522 int new_hello_time, int new_max_age,
525 VLOG_DBG("%s: set RSTP times to (%d, %d, %d, %d)", rstp->name,
526 new_forward_delay, new_hello_time, new_max_age, new_message_age);
527 if (new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY
528 && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) {
529 rstp->bridge_times.forward_delay = new_forward_delay;
531 if (new_hello_time == RSTP_BRIDGE_HELLO_TIME) {
532 rstp->bridge_times.hello_time = new_hello_time;
534 if (new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE
535 && new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) {
536 rstp->bridge_times.max_age = new_max_age;
538 rstp->bridge_times.message_age = new_message_age;
541 /* Sets the port id, it is called by rstp_port_set_port_number() or
542 * rstp_port_set_priority().
545 set_port_id__(struct rstp_port *p)
550 /* [9.2.7] Port identifier. */
551 p->port_id = p->port_number | (p->priority << 8);
552 VLOG_DBG("%s: new RSTP port id "RSTP_PORT_ID_FMT"", rstp->name,
556 /* Sets the port priority. */
558 rstp_port_set_priority(struct rstp_port *rstp_port, int new_port_priority)
562 rstp = rstp_port->rstp;
563 if (new_port_priority >= RSTP_MIN_PORT_PRIORITY
564 && new_port_priority <= RSTP_MAX_PORT_PRIORITY) {
565 VLOG_DBG("%s, port %u: set RSTP port priority to %d", rstp->name,
566 rstp_port->port_number, new_port_priority);
567 ovs_mutex_lock(&mutex);
568 new_port_priority -= new_port_priority % RSTP_STEP_PORT_PRIORITY;
569 rstp_port->priority = new_port_priority;
570 set_port_id__(rstp_port);
571 rstp_port->selected = false;
572 rstp_port->reselect = true;
573 ovs_mutex_unlock(&mutex);
577 /* Checks if a port number is already taken by an active port. */
579 is_port_number_taken__(struct rstp *rstp, int n, struct rstp_port *rstp_port)
583 if (rstp->ports_count > 0){
584 LIST_FOR_EACH (p, node, &rstp->ports) {
585 if (p->port_number == n && rstp_port != rstp_get_port(rstp, n)) {
586 VLOG_DBG("%s: port number %d not available", rstp->name, n);
591 VLOG_DBG("%s: port number %d is available", rstp->name, n);
596 rstp_first_free_number__(struct rstp *rstp, struct rstp_port *rstp_port) {
600 ovs_mutex_lock(&mutex);
601 while (free_number <= RSTP_MAX_PORTS) {
602 if (!is_port_number_taken__(rstp, free_number, rstp_port)) {
603 ovs_mutex_unlock(&mutex);
608 ovs_mutex_unlock(&mutex);
609 VLOG_DBG("%s, No free port number available.", rstp->name);
613 /* Sets the port number. */
615 rstp_port_set_port_number(struct rstp_port *rstp_port,
616 uint16_t new_port_number)
620 rstp = rstp_port->rstp;
621 ovs_mutex_lock(&mutex);
622 /* If new_port_number is inside bounds and available, use it.
623 * If new_port_number is 0 or it is already taken, use the first free
624 * available port number.
626 if ((new_port_number >= 1 && new_port_number <= RSTP_MAX_PORTS) &&
627 (!is_port_number_taken__(rstp_port->rstp, new_port_number, rstp_port)))
629 rstp_port->port_number = new_port_number;
631 else if (new_port_number == 0 ||
632 is_port_number_taken__(rstp_port->rstp, new_port_number,
634 rstp_port->port_number = rstp_first_free_number__(rstp, rstp_port);
637 set_port_id__(rstp_port);
638 /* [17.13] is not clear. I suppose that a port number change
639 * should trigger reselection like a port priority change. */
640 rstp_port->selected = false;
641 rstp_port->reselect = true;
642 ovs_mutex_unlock(&mutex);
643 VLOG_DBG("%s: set new RSTP port number %d", rstp->name,
644 rstp_port->port_number);
647 /* Converts the link speed to a port path cost [Table 17-3]. */
649 rstp_convert_speed_to_cost(unsigned int speed)
653 value = speed >= 10000000 ? 2 /* 10 Tb/s. */
654 : speed >= 1000000 ? 20 /* 1 Tb/s. */
655 : speed >= 100000 ? 200 /* 100 Gb/s. */
656 : speed >= 10000 ? 2000 /* 10 Gb/s. */
657 : speed >= 1000 ? 20000 /* 1 Gb/s. */
658 : speed >= 100 ? 200000 /* 100 Mb/s. */
659 : speed >= 10 ? 2000000 /* 10 Mb/s. */
660 : speed >= 1 ? 20000000 /* 1 Mb/s. */
661 : RSTP_DEFAULT_PORT_PATH_COST; /* 100 Mb/s. */
666 /* Sets the port path cost. */
668 rstp_port_set_path_cost(struct rstp_port *rstp_port,
669 uint32_t new_port_path_cost)
671 if (new_port_path_cost >= RSTP_MIN_PORT_PATH_COST &&
672 new_port_path_cost <= RSTP_MAX_PORT_PATH_COST) {
675 ovs_mutex_lock(&mutex);
676 rstp = rstp_port->rstp;
677 VLOG_DBG("%s, port %u, set RSTP port path cost to %d", rstp->name,
678 rstp_port->port_number, new_port_path_cost);
679 rstp_port->port_path_cost = new_port_path_cost;
680 rstp_port->selected = false;
681 rstp_port->reselect = true;
682 ovs_mutex_unlock(&mutex);
686 /* Gets the root path cost. */
688 rstp_get_root_path_cost(const struct rstp *rstp)
692 ovs_mutex_lock(&mutex);
693 cost = rstp->root_priority.root_path_cost;
694 ovs_mutex_unlock(&mutex);
698 /* Returns true if something has happened to 'rstp' which necessitates
699 * flushing the client's MAC learning table.
702 rstp_check_and_reset_fdb_flush(struct rstp *rstp)
709 ovs_mutex_lock(&mutex);
710 if (rstp->ports_count > 0){
711 LIST_FOR_EACH (p, node, &rstp->ports) {
714 /* fdb_flush should be reset by the filtering database
715 * once the entries are removed if rstp_version is TRUE, and
716 * immediately if stp_version is TRUE.*/
717 p->fdb_flush = false;
721 ovs_mutex_unlock(&mutex);
725 /* Finds a port whose state has changed. If successful, stores the port whose
726 * state changed in '*portp' and returns true. If no port has changed, stores
727 * NULL in '*portp' and returns false.
729 * XXX: This function is only called by the main thread, which is also the one
730 * that creates and deletes ports. Otherwise this function is not thread safe,
731 * as the returned '*portp' could become stale before it is referenced by the
734 rstp_get_changed_port(struct rstp *rstp, struct rstp_port **portp)
736 bool changed = false;
738 ovs_mutex_lock(&mutex);
739 if (rstp->ports_count > 0) {
742 LIST_FOR_EACH (p, node, &rstp->ports) {
743 if (p->state_changed) {
744 p->state_changed = false;
747 ovs_mutex_unlock(&mutex);
753 ovs_mutex_unlock(&mutex);
757 /* Returns the port in 'rstp' with number 'port_number'. */
759 rstp_get_port(struct rstp *rstp, int port_number)
761 struct rstp_port *port;
763 ovs_mutex_lock(&mutex);
764 if (rstp->ports_count > 0){
765 LIST_FOR_EACH (port, node, &rstp->ports) {
766 if (port->port_number == port_number) {
767 ovs_mutex_unlock(&mutex);
772 ovs_mutex_unlock(&mutex);
776 /* Updates the port_enabled parameter. */
778 update_port_enabled__(struct rstp_port *p)
780 if (p->mac_operational && p->is_administrative_bridge_port ==
781 RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) {
782 p->port_enabled = true;
784 p->port_enabled = false;
788 /* Sets the port MAC_Operational parameter [6.4.2]. */
790 rstp_port_set_mac_operational(struct rstp_port *p, bool new_mac_operational)
794 ovs_mutex_lock(&mutex);
796 p->mac_operational = new_mac_operational;
797 update_port_enabled__(p);
798 rstp->changes = true;
800 ovs_mutex_unlock(&mutex);
803 /* Gets the port MAC_Operational parameter [6.4.2]. */
805 rstp_port_get_mac_operational(struct rstp_port *p)
809 ovs_mutex_lock(&mutex);
810 value = p->mac_operational;
811 ovs_mutex_unlock(&mutex);
815 /* Sets the port Administrative Bridge Port parameter. */
817 rstp_port_set_administrative_bridge_port(struct rstp_port *p,
818 uint8_t new_admin_port_state)
820 if (new_admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_DISABLED ||
821 new_admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) {
822 p->is_administrative_bridge_port = new_admin_port_state;
823 update_port_enabled__(p);
827 /* Sets the port oper_point_to_point_mac parameter. */
829 rstp_port_set_oper_point_to_point_mac(struct rstp_port *p,
830 uint8_t new_oper_p2p_mac)
832 if (new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_DISABLED ||
833 new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_ENABLED) {
834 p->oper_point_to_point_mac = new_oper_p2p_mac;
835 update_port_enabled__(p);
839 /* Initializes a port with the defaults values for its parameters. */
841 rstp_initialize_port__(struct rstp_port *p)
847 rstp_port_set_administrative_bridge_port(p,
848 RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED);
849 rstp_port_set_oper_point_to_point_mac(p, 1);
850 rstp_port_set_priority(p, RSTP_DEFAULT_PORT_PRIORITY);
851 rstp_port_set_port_number(p, 0);
852 rstp_port_set_path_cost(p, RSTP_DEFAULT_PORT_PATH_COST);
853 rstp_port_set_auto_edge(p, true);
855 p->port_receive_sm_state = PORT_RECEIVE_SM_INIT;
856 p->port_protocol_migration_sm_state = PORT_PROTOCOL_MIGRATION_SM_INIT;
857 p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_INIT;
858 p->port_transmit_sm_state = PORT_TRANSMIT_SM_INIT;
859 p->port_information_sm_state = PORT_INFORMATION_SM_INIT;
860 p->port_role_transition_sm_state = PORT_ROLE_TRANSITION_SM_INIT;
861 p->port_state_transition_sm_state = PORT_STATE_TRANSITION_SM_INIT;
862 p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INIT;
866 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" initialized.", rstp->name,
870 /* Reinitialization function used in tests. */
872 reinitialize_port(struct rstp_port *p)
874 struct rstp_port temp_port;
879 memset(p, 0, sizeof(struct rstp_port));
881 p->node = temp_port.node;
882 p->aux = temp_port.aux;
883 p->port_number = temp_port.port_number;
884 p->port_priority = temp_port.port_priority;
885 p->port_id = temp_port.port_id;
886 p->rstp_state = RSTP_DISCARDING;
888 rstp_port_set_administrative_bridge_port(p,
889 RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED);
890 rstp_port_set_oper_point_to_point_mac(p, 1);
891 rstp_port_set_path_cost(p, RSTP_DEFAULT_PORT_PATH_COST);
892 rstp_port_set_auto_edge(p, true);
893 /* Initialize state machines. */
894 p->port_receive_sm_state = PORT_RECEIVE_SM_INIT;
895 p->port_protocol_migration_sm_state =
896 PORT_PROTOCOL_MIGRATION_SM_INIT;
897 p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_INIT;
898 p->port_transmit_sm_state = PORT_TRANSMIT_SM_INIT;
899 p->port_information_sm_state = PORT_INFORMATION_SM_INIT;
900 p->port_role_transition_sm_state = PORT_ROLE_TRANSITION_SM_INIT;
901 p->port_state_transition_sm_state = PORT_STATE_TRANSITION_SM_INIT;
902 p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INIT;
905 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" reinitialized.", rstp->name,
909 /* Sets the port state. */
911 rstp_port_set_state(struct rstp_port *p, enum rstp_state state)
917 VLOG_DBG("%s, port %u: set RSTP port state %s -> %s", rstp->name,
919 rstp_state_name(p->rstp_state), rstp_state_name(state));
921 if (state != p->rstp_state && !p->state_changed) {
922 p->state_changed = true;
923 seq_change(connectivity_seq_get());
925 p->rstp_state = state;
928 /* Adds a RSTP port. */
930 rstp_add_port(struct rstp *rstp) {
931 struct rstp_port *p = xzalloc(sizeof *p);
933 ovs_mutex_lock(&mutex);
935 rstp_initialize_port__(p);
936 rstp_port_set_state(p, RSTP_DISCARDING);
937 list_push_back(&rstp->ports, &p->node);
939 rstp->changes = true;
941 ovs_mutex_unlock(&mutex);
942 VLOG_DBG("%s: added port "RSTP_PORT_ID_FMT"", rstp->name, p->port_id);
946 /* Deletes a RSTP port. */
948 rstp_delete_port(struct rstp_port *p) {
951 ovs_mutex_lock(&mutex);
953 rstp_port_set_state(p, RSTP_DISABLED);
954 list_remove(&p->node);
956 VLOG_DBG("%s: removed port "RSTP_PORT_ID_FMT"", rstp->name, p->port_id);
958 ovs_mutex_unlock(&mutex);
961 /* Sets the port Admin Edge parameter. */
963 rstp_port_set_admin_edge(struct rstp_port *rstp_port, bool new_admin_edge)
967 rstp = rstp_port->rstp;
968 if (rstp_port->admin_edge != new_admin_edge) {
969 VLOG_DBG("%s, port %u: set RSTP Admin Edge to %d", rstp->name,
970 rstp_port->port_number, new_admin_edge);
971 ovs_mutex_lock(&mutex);
972 rstp_port->admin_edge = new_admin_edge;
973 ovs_mutex_unlock(&mutex);
977 /* Sets the port Auto Edge parameter. */
979 rstp_port_set_auto_edge(struct rstp_port *rstp_port, bool new_auto_edge)
983 rstp = rstp_port->rstp;
984 if (rstp_port->auto_edge != new_auto_edge) {
985 VLOG_DBG("%s, port %u: set RSTP Auto Edge to %d", rstp->name,
986 rstp_port->port_number, new_auto_edge);
987 ovs_mutex_lock(&mutex);
988 rstp_port->auto_edge = new_auto_edge;
989 ovs_mutex_unlock(&mutex);
993 /* Sets the port mcheck parameter.
994 * [17.19.13] May be set by management to force the Port Protocol Migration
995 * state machine to transmit RST BPDUs for a MigrateTime (17.13.9) period, to
996 * test whether all STP Bridges (17.4) on the attached LAN have been removed
997 * and the Port can continue to transmit RSTP BPDUs. Setting mcheck has no
998 * effect if stpVersion (17.20.12) is TRUE, i.e., the Bridge is operating in
999 * STP Compatibility. mode.
1002 rstp_port_set_mcheck(struct rstp_port *rstp_port, bool new_mcheck)
1006 ovs_mutex_lock(&mutex);
1007 rstp = rstp_port->rstp;
1008 if (new_mcheck == true && rstp_port->rstp->force_protocol_version >= 2) {
1009 rstp_port->mcheck = true;
1011 ovs_mutex_unlock(&mutex);
1012 VLOG_DBG("%s, port %u: set RSTP mcheck to %d", rstp->name,
1013 rstp_port->port_number, new_mcheck);
1016 /* Returns the designated bridge id. */
1018 rstp_get_designated_id(const struct rstp *rstp)
1020 rstp_identifier designated_id;
1022 ovs_mutex_lock(&mutex);
1023 designated_id = rstp->root_priority.designated_bridge_id;
1024 ovs_mutex_unlock(&mutex);
1025 return designated_id;
1028 /* Returns the root bridge id. */
1030 rstp_get_root_id(const struct rstp *rstp)
1032 rstp_identifier root_id;
1034 ovs_mutex_lock(&mutex);
1035 root_id = rstp->root_priority.root_bridge_id;
1036 ovs_mutex_unlock(&mutex);
1040 /* Returns the designated port id. */
1042 rstp_get_designated_port_id(const struct rstp *rstp)
1044 uint16_t designated_port_id;
1046 ovs_mutex_lock(&mutex);
1047 designated_port_id = rstp->root_priority.designated_port_id;
1048 ovs_mutex_unlock(&mutex);
1049 return designated_port_id;
1052 /* Return the bridge port id. */
1054 rstp_get_bridge_port_id(const struct rstp *rstp)
1056 uint16_t bridge_port_id;
1058 ovs_mutex_lock(&mutex);
1059 bridge_port_id = rstp->root_priority.bridge_port_id;
1060 ovs_mutex_unlock(&mutex);
1061 return bridge_port_id;
1064 /* Returns true if the bridge believes to the be root of the spanning tree,
1068 rstp_is_root_bridge(const struct rstp *rstp)
1072 ovs_mutex_lock(&mutex);
1073 is_root = rstp->bridge_identifier ==
1074 rstp->root_priority.designated_bridge_id;
1075 ovs_mutex_unlock(&mutex);
1079 /* Returns the bridge ID of the bridge currently believed to be the root. */
1081 rstp_get_designated_root(const struct rstp *rstp)
1083 rstp_identifier designated_root;
1085 ovs_mutex_lock(&mutex);
1086 designated_root = rstp->root_priority.designated_bridge_id;
1087 ovs_mutex_unlock(&mutex);
1088 return designated_root;
1091 /* Returns the port connecting 'rstp' to the root bridge, or a null pointer if
1092 * there is no such port.
1095 rstp_get_root_port(struct rstp *rstp)
1097 struct rstp_port *p;
1099 ovs_mutex_lock(&mutex);
1100 if (rstp->ports_count > 0){
1101 LIST_FOR_EACH (p, node, &rstp->ports) {
1102 if (p->port_id == rstp->root_port_id) {
1103 ovs_mutex_unlock(&mutex);
1108 ovs_mutex_unlock(&mutex);
1112 /* Returns the port ID for 'p'. */
1114 rstp_port_get_id(const struct rstp_port *p)
1118 ovs_mutex_lock(&mutex);
1119 port_id = p->port_id;
1120 ovs_mutex_unlock(&mutex);
1124 /* Returns the state of port 'p'. */
1126 rstp_port_get_state(const struct rstp_port *p)
1128 enum rstp_state state;
1130 ovs_mutex_lock(&mutex);
1131 state = p->rstp_state;
1132 ovs_mutex_unlock(&mutex);
1136 /* Returns the role of port 'p'. */
1138 rstp_port_get_role(const struct rstp_port *p)
1140 enum rstp_port_role role;
1142 ovs_mutex_lock(&mutex);
1144 ovs_mutex_unlock(&mutex);
1148 /* Retrieves BPDU transmit and receive counts for 'p'. */
1150 rstp_port_get_counts(const struct rstp_port *p,
1151 int *tx_count, int *rx_count, int *error_count, int *uptime)
1153 ovs_mutex_lock(&mutex);
1154 *tx_count = p->tx_count;
1155 *rx_count = p->rx_rstp_bpdu_cnt;
1156 *error_count = p->error_count;
1157 *uptime = p->uptime;
1158 ovs_mutex_unlock(&mutex);
1162 rstp_port_set_aux(struct rstp_port *p, void *aux)
1164 ovs_mutex_lock(&mutex);
1166 ovs_mutex_unlock(&mutex);
1170 rstp_port_get_aux(struct rstp_port *p)
1174 ovs_mutex_lock(&mutex);
1176 ovs_mutex_unlock(&mutex);
1180 /* Returns true if 'state' is one in which BPDU packets should be received
1181 * and transmitted on a port, false otherwise.
1184 rstp_should_manage_bpdu(enum rstp_state state)
1186 return (state == RSTP_DISCARDING || state == RSTP_LEARNING ||
1187 state == RSTP_FORWARDING);
1190 /* Returns true if 'state' is one in which packets received on a port should
1191 * be forwarded, false otherwise.
1193 * Returns true if 'state' is RSTP_DISABLED, since presumably in that case the
1194 * port should still work, just not have RSTP applied to it.
1197 rstp_forward_in_state(enum rstp_state state)
1199 return (state == RSTP_DISABLED || state == RSTP_FORWARDING);
1202 /* Returns true if 'state' is one in which MAC learning should be done on
1203 * packets received on a port, false otherwise.
1205 * Returns true if 'state' is RSTP_DISABLED, since presumably in that case the
1206 * port should still work, just not have RSTP applied to it. */
1208 rstp_learn_in_state(enum rstp_state state)
1210 return (state == RSTP_DISABLED || state == RSTP_LEARNING ||
1211 state == RSTP_FORWARDING);
1215 static struct rstp *
1216 rstp_find(const char *name) OVS_REQUIRES(mutex)
1220 LIST_FOR_EACH (rstp, node, all_rstps) {
1221 if (!strcmp(rstp->name, name)) {
1229 rstp_unixctl_tcn(struct unixctl_conn *conn, int argc,
1230 const char *argv[], void *aux OVS_UNUSED)
1232 ovs_mutex_lock(&mutex);
1234 struct rstp *rstp = rstp_find(argv[1]);
1236 unixctl_command_reply_error(conn, "No such RSTP object");
1239 rstp->changes = true;
1243 LIST_FOR_EACH (rstp, node, all_rstps) {
1244 rstp->changes = true;
1248 unixctl_command_reply(conn, "OK");
1251 ovs_mutex_unlock(&mutex);