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.
31 #include "rstp-common.h"
32 #include "rstp-state-machines.h"
33 #include <arpa/inet.h>
35 #include <netinet/in.h>
37 #include <sys/types.h>
38 #include "byte-order.h"
39 #include "connectivity.h"
41 #include "ofproto/ofproto.h"
48 VLOG_DEFINE_THIS_MODULE(rstp);
50 static struct ovs_mutex mutex;
51 static struct list all_rstps__ = LIST_INITIALIZER(&all_rstps__);
52 static struct list *const all_rstps OVS_GUARDED_BY(mutex) = &all_rstps__;
54 /* Internal use only */
55 static void set_port_id__(struct rstp_port *);
56 static void update_port_enabled__(struct rstp_port *);
57 static void set_bridge_priority__(struct rstp *);
58 static void reinitialize_rstp__(struct rstp *);
59 static bool is_port_number_taken__(struct rstp *, int, struct rstp_port *);
60 static uint16_t rstp_first_free_number__(struct rstp *, struct rstp_port *);
61 static void rstp_initialize_port(struct rstp_port *p);
64 rstp_state_name(enum rstp_state state)
81 rstp_port_role_name(enum rstp_port_role role)
100 rstp_ref(struct rstp *rstp_)
106 ovs_refcount_ref(&rstp->ref_cnt);
111 /* Frees RSTP struct */
113 rstp_unref(struct rstp *rstp)
117 if (rstp && ovs_refcount_unref(&rstp->ref_cnt) == 1) {
118 ovs_mutex_lock(&mutex);
119 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. */
133 rstp_port_number(const struct rstp_port *p)
137 ovs_mutex_lock(&mutex);
138 number = p->port_number;
139 ovs_mutex_unlock(&mutex);
143 static void rstp_unixctl_tcn(struct unixctl_conn *, int argc,
144 const char *argv[], void *aux);
146 /* Decrements the State Machines' timers. */
148 rstp_tick_timers(struct rstp *rstp)
150 ovs_mutex_lock(&mutex);
151 decrease_rstp_port_timers(rstp);
152 ovs_mutex_unlock(&mutex);
155 /* Processes an incoming BPDU. */
157 rstp_received_bpdu(struct rstp_port *p, const void *bpdu, size_t bpdu_size)
159 ovs_mutex_lock(&mutex);
160 process_received_bpdu(p, bpdu, bpdu_size);
161 ovs_mutex_unlock(&mutex);
167 unixctl_command_register("rstp/tcn", "[bridge]", 0, 1, rstp_unixctl_tcn,
171 /* Creates and returns a new RSTP instance that initially has no ports. */
173 rstp_create(const char *name, rstp_identifier bridge_address,
174 void (*send_bpdu)(struct ofpbuf *bpdu, int port_no, void *aux),
177 static struct ovsthread_once once = OVSTHREAD_ONCE_INITIALIZER;
180 VLOG_DBG("Creating RSTP instance");
181 if (ovsthread_once_start(&once)) {
182 ovs_mutex_init_recursive(&mutex);
183 ovsthread_once_done(&once);
186 rstp = xzalloc(sizeof *rstp);
187 rstp->name = xstrdup(name);
188 /* Set bridge address. */
189 rstp_set_bridge_address(rstp, bridge_address);
190 /* Set default parameters values. */
191 rstp_set_bridge_priority(rstp, RSTP_DEFAULT_PRIORITY);
192 rstp_set_bridge_ageing_time(rstp, RSTP_DEFAULT_AGEING_TIME);
193 rstp_set_bridge_force_protocol_version(rstp, FPV_DEFAULT);
194 rstp_set_bridge_forward_delay(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
195 rstp_set_bridge_hello_time(rstp);
196 rstp_set_bridge_max_age(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
197 rstp_set_bridge_migrate_time(rstp);
198 rstp_set_bridge_transmit_hold_count(rstp,
199 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
200 rstp_set_bridge_times(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
201 RSTP_BRIDGE_HELLO_TIME, RSTP_DEFAULT_BRIDGE_MAX_AGE,
203 rstp->send_bpdu = send_bpdu;
205 rstp->changes = false;
208 ovs_mutex_lock(&mutex);
209 /* Initialize the ports list. */
210 list_init(&rstp->ports);
211 ovs_refcount_init(&rstp->ref_cnt);
212 list_push_back(all_rstps, &rstp->node);
213 ovs_mutex_unlock(&mutex);
214 VLOG_DBG("RSTP instance creation done");
218 /* Called by rstp_set_bridge_address() and rstp_set_bridge_priority(),
219 * it updates the bridge priority vector according to the values passed by
223 set_bridge_priority__(struct rstp *rstp)
225 rstp->bridge_priority.root_bridge_id = rstp->bridge_identifier;
226 rstp->bridge_priority.designated_bridge_id = rstp->bridge_identifier;
227 VLOG_DBG("%s: new bridge identifier: "RSTP_ID_FMT"", rstp->name,
228 RSTP_ID_ARGS(rstp->bridge_identifier));
231 /* Sets the bridge address. */
233 rstp_set_bridge_address(struct rstp *rstp, rstp_identifier bridge_address)
237 VLOG_DBG("%s: set bridge address to: "RSTP_ID_FMT"", rstp->name,
238 RSTP_ID_ARGS(bridge_address));
239 ovs_mutex_lock(&mutex);
240 rstp->address = bridge_address;
241 rstp->bridge_identifier = bridge_address;
242 set_bridge_priority__(rstp);
244 /* [17.13] When the bridge address changes, recalculates all priority
247 if (rstp->ports_count > 0) {
248 LIST_FOR_EACH (p, node, &rstp->ports) {
253 rstp->changes = true;
254 updt_roles_tree(rstp);
255 ovs_mutex_unlock(&mutex);
259 rstp_get_name(const struct rstp *rstp)
263 ovs_mutex_lock(&mutex);
265 ovs_mutex_unlock(&mutex);
270 rstp_get_bridge_id(const struct rstp *rstp)
272 rstp_identifier bridge_id;
274 ovs_mutex_lock(&mutex);
275 bridge_id = rstp->bridge_identifier;
276 ovs_mutex_unlock(&mutex);
280 /* Sets the bridge priority. */
282 rstp_set_bridge_priority(struct rstp *rstp, int new_priority)
286 if (new_priority >= RSTP_MIN_PRIORITY &&
287 new_priority <= RSTP_MAX_PRIORITY) {
288 VLOG_DBG("%s: set bridge priority to %d", rstp->name,
289 (new_priority / 4096) * 4096);
290 ovs_mutex_lock(&mutex);
291 rstp->priority = (new_priority / 4096) * 4096;
292 rstp->bridge_identifier &= 0xffffffffffffULL;
293 rstp->bridge_identifier |=
294 (uint64_t) ((new_priority / 4096) * 4096) << 48;
295 set_bridge_priority__(rstp);
298 if (rstp->ports_count > 0){
299 LIST_FOR_EACH (p, node, &rstp->ports) {
304 rstp->changes = true;
305 updt_roles_tree(rstp);
306 ovs_mutex_unlock(&mutex);
310 /* Sets the bridge ageing time. */
312 rstp_set_bridge_ageing_time(struct rstp *rstp, int new_ageing_time)
314 if (new_ageing_time >= RSTP_MIN_AGEING_TIME &&
315 new_ageing_time <= RSTP_MAX_AGEING_TIME) {
316 VLOG_DBG("%s: set ageing time to %d", rstp->name, new_ageing_time);
317 ovs_mutex_lock(&mutex);
318 rstp->ageing_time = new_ageing_time;
319 ovs_mutex_unlock(&mutex);
323 /* Reinitializes RSTP when switching from RSTP mode to STP mode
327 reinitialize_rstp__(struct rstp *rstp)
330 struct rstp_port *p, temp_port;
331 static struct list ports;
333 /* Copy rstp in temp */
336 /* stop and clear rstp */
337 memset(rstp, 0, sizeof(struct rstp));
339 /* Initialize rstp. */
340 rstp->name = temp.name;
341 /* Set bridge address. */
342 rstp_set_bridge_address(rstp, temp.address);
343 /* Set default parameters values. */
344 rstp_set_bridge_priority(rstp, RSTP_DEFAULT_PRIORITY);
345 rstp_set_bridge_ageing_time(rstp, RSTP_DEFAULT_AGEING_TIME);
346 rstp_set_bridge_forward_delay(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
347 rstp_set_bridge_hello_time(rstp);
348 rstp_set_bridge_max_age(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
349 rstp_set_bridge_migrate_time(rstp);
350 rstp_set_bridge_transmit_hold_count(rstp,
351 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
352 rstp_set_bridge_times(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
353 RSTP_BRIDGE_HELLO_TIME, RSTP_DEFAULT_BRIDGE_MAX_AGE,
356 rstp->send_bpdu = temp.send_bpdu;
357 rstp->aux = temp.aux;
358 rstp->node = temp.node;
359 rstp->changes = false;
362 rstp->ports_count = temp.ports_count;
363 if (rstp->ports_count > 0){
364 LIST_FOR_EACH (p, node, &rstp->ports) {
366 memset(p, 0, sizeof(struct rstp_port));
368 p->node = temp_port.node;
369 p->aux = temp_port.aux;
370 p->port_number = temp_port.port_number;
371 p->port_priority = temp_port.port_priority;
372 p->port_id = temp_port.port_id;
373 p->rstp_state = RSTP_DISCARDING;
375 rstp_port_set_administrative_bridge_port(p,
376 RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED);
377 rstp_port_set_oper_point_to_point_mac(p, 1);
378 rstp_port_set_path_cost(p, RSTP_DEFAULT_PORT_PATH_COST);
379 rstp_port_set_auto_edge(p, true);
380 /* Initialize state machines. */
381 p->port_receive_sm_state = PORT_RECEIVE_SM_INIT;
382 p->port_protocol_migration_sm_state =
383 PORT_PROTOCOL_MIGRATION_SM_INIT;
384 p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_INIT;
385 p->port_transmit_sm_state = PORT_TRANSMIT_SM_INIT;
386 p->port_information_sm_state = PORT_INFORMATION_SM_INIT;
387 p->port_role_transition_sm_state = PORT_ROLE_TRANSITION_SM_INIT;
388 p->port_state_transition_sm_state = PORT_STATE_TRANSITION_SM_INIT;
389 p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INIT;
393 rstp->ref_cnt = temp.ref_cnt;
396 /* Sets the force protocol version parameter. */
398 rstp_set_bridge_force_protocol_version(struct rstp *rstp,
399 enum rstp_force_protocol_version new_force_protocol_version)
401 if (new_force_protocol_version != rstp->force_protocol_version &&
402 (new_force_protocol_version == FPV_STP_COMPATIBILITY ||
403 new_force_protocol_version == FPV_DEFAULT)) {
404 VLOG_DBG("%s: set bridge Force Protocol Version to %d", rstp->name,
405 new_force_protocol_version);
406 ovs_mutex_lock(&mutex);
407 /* [17.13] The Spanning Tree Protocol Entity shall be reinitialized,
408 * as specified by the assertion of BEGIN (17.18.1) in the state
409 * machine specification.
411 reinitialize_rstp__(rstp);
412 rstp->force_protocol_version = new_force_protocol_version;
413 if (rstp->force_protocol_version < 2) {
414 rstp->stp_version = true;
415 rstp->rstp_version = false;
417 rstp->stp_version = false;
418 rstp->rstp_version = true;
420 rstp->changes = true;
422 ovs_mutex_unlock(&mutex);
426 /* Sets the bridge Hello Time parameter. */
428 rstp_set_bridge_hello_time(struct rstp *rstp)
430 VLOG_DBG("%s: set RSTP Hello Time to %d", rstp->name,
431 RSTP_BRIDGE_HELLO_TIME);
432 /* 2 is the only acceptable value. */
433 ovs_mutex_lock(&mutex);
434 rstp->bridge_hello_time = RSTP_BRIDGE_HELLO_TIME;
435 ovs_mutex_unlock(&mutex);
438 /* Sets the bridge max age parameter. */
440 rstp_set_bridge_max_age(struct rstp *rstp, int new_max_age)
442 if (new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE &&
443 new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) {
445 if ((2*(rstp->bridge_forward_delay - 1) >= new_max_age) &&
446 (new_max_age >= 2*rstp->bridge_hello_time)) {
447 VLOG_DBG("%s: set RSTP bridge Max Age to %d", rstp->name,
449 ovs_mutex_lock(&mutex);
450 rstp->bridge_max_age = new_max_age;
451 rstp->bridge_times.max_age = new_max_age;
452 ovs_mutex_unlock(&mutex);
457 /* Sets the bridge forward delay parameter. */
459 rstp_set_bridge_forward_delay(struct rstp *rstp, int new_forward_delay)
461 if (new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY &&
462 new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) {
463 if (2 * (new_forward_delay - 1) >= rstp->bridge_max_age) {
464 VLOG_DBG("%s: set RSTP Forward Delay to %d", rstp->name,
466 ovs_mutex_lock(&mutex);
467 rstp->bridge_forward_delay = new_forward_delay;
468 rstp->bridge_times.forward_delay = new_forward_delay;
469 ovs_mutex_unlock(&mutex);
474 /* Sets the bridge transmit hold count parameter. */
476 rstp_set_bridge_transmit_hold_count(struct rstp *rstp,
477 int new_transmit_hold_count)
481 if (new_transmit_hold_count >= RSTP_MIN_TRANSMIT_HOLD_COUNT &&
482 new_transmit_hold_count <= RSTP_MAX_TRANSMIT_HOLD_COUNT) {
483 VLOG_DBG("%s: set RSTP Transmit Hold Count to %d", rstp->name,
484 new_transmit_hold_count);
485 /* Resetting txCount on all ports [17.13]. */
486 ovs_mutex_lock(&mutex);
487 rstp->transmit_hold_count = new_transmit_hold_count;
488 if (rstp->ports_count > 0){
489 LIST_FOR_EACH (p, node, &rstp->ports) {
493 ovs_mutex_unlock(&mutex);
497 /* Sets the bridge migrate time parameter. */
499 rstp_set_bridge_migrate_time(struct rstp *rstp)
501 VLOG_DBG("%s: set RSTP Migrate Time to %d", rstp->name,
503 /* 3 is the only acceptable value */
504 ovs_mutex_lock(&mutex);
505 rstp->migrate_time = RSTP_MIGRATE_TIME;
506 ovs_mutex_unlock(&mutex);
509 /* Sets the bridge times. */
511 rstp_set_bridge_times(struct rstp *rstp, int new_forward_delay,
512 int new_hello_time, int new_max_age,
515 VLOG_DBG("%s: set RSTP times to (%d, %d, %d, %d)", rstp->name,
516 new_forward_delay, new_hello_time, new_max_age, new_message_age);
517 if (new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY &&
518 new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY)
519 rstp->bridge_times.forward_delay = new_forward_delay;
520 if (new_hello_time == RSTP_BRIDGE_HELLO_TIME)
521 rstp->bridge_times.hello_time = new_hello_time;
522 if (new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE &&
523 new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE)
524 rstp->bridge_times.max_age = new_max_age;
525 rstp->bridge_times.message_age = new_message_age;
528 /* Sets the port id, it is called by rstp_port_set_port_number() or
529 * rstp_port_set_priority().
532 set_port_id__(struct rstp_port *p)
537 /* [9.2.7] Port identifier. */
538 p->port_id = p->port_number | (p->priority << 8);
539 VLOG_DBG("%s: new RSTP port id "RSTP_PORT_ID_FMT"", rstp->name,
543 /* Sets the port priority. */
545 rstp_port_set_priority(struct rstp_port *rstp_port, int new_port_priority)
549 rstp = rstp_port->rstp;
550 if (new_port_priority >= RSTP_MIN_PORT_PRIORITY &&
551 new_port_priority <= RSTP_MAX_PORT_PRIORITY) {
552 VLOG_DBG("%s, port %u: set RSTP port priority to %d", rstp->name,
553 rstp_port->port_number, new_port_priority);
554 ovs_mutex_lock(&mutex);
555 new_port_priority -= new_port_priority % RSTP_STEP_PORT_PRIORITY;
556 rstp_port->priority = new_port_priority;
557 set_port_id__(rstp_port);
558 rstp_port->selected = 0;
559 rstp_port->reselect = 1;
560 ovs_mutex_unlock(&mutex);
564 /* Checks if a port number is already taken by an active port. */
566 is_port_number_taken__(struct rstp *rstp, int n, struct rstp_port *rstp_port)
570 if (rstp->ports_count > 0){
571 LIST_FOR_EACH (p, node, &rstp->ports) {
572 if (p->port_number == n && rstp_port != rstp_get_port(rstp, n)) {
573 VLOG_DBG("%s: port number %d not available", rstp->name, n);
578 VLOG_DBG("%s: port number %d is available", rstp->name, n);
583 rstp_first_free_number__(struct rstp *rstp, struct rstp_port *rstp_port) {
587 ovs_mutex_lock(&mutex);
588 while (free_number <= RSTP_MAX_PORTS) {
589 if (!is_port_number_taken__(rstp, free_number, rstp_port)) {
590 ovs_mutex_unlock(&mutex);
595 ovs_mutex_unlock(&mutex);
596 VLOG_DBG("%s, No free port number available.", rstp->name);
600 /* Sets the port number. */
602 rstp_port_set_port_number(struct rstp_port *rstp_port,
603 uint16_t new_port_number)
607 rstp = rstp_port->rstp;
608 ovs_mutex_lock(&mutex);
609 /* If new_port_number is inside bounds and available, use it.
610 * If new_port_number is 0 or it is already taken, use the first free
611 * available port number.
613 if ((new_port_number >= 1 && new_port_number <= RSTP_MAX_PORTS) &&
614 (!is_port_number_taken__(rstp_port->rstp, new_port_number, rstp_port)))
616 rstp_port->port_number = new_port_number;
618 else if (new_port_number == 0 ||
619 is_port_number_taken__(rstp_port->rstp, new_port_number,
621 rstp_port->port_number = rstp_first_free_number__(rstp, rstp_port);
624 set_port_id__(rstp_port);
625 /* [17.13] is not clear. I suppose that a port number change
626 * should trigger reselection like a port priority change.
628 rstp_port->selected = 0;
629 rstp_port->reselect = 1;
630 ovs_mutex_unlock(&mutex);
631 VLOG_DBG("%s: set new RSTP port number %d", rstp->name,
632 rstp_port->port_number);
635 /* Converts the link speed to a port path cost [Table 17-3]. */
637 rstp_convert_speed_to_cost(unsigned int speed)
641 value = speed >= 10000000 ? 2 /* 10 Tb/s. */
642 : speed >= 1000000 ? 20 /* 1 Tb/s. */
643 : speed >= 100000 ? 200 /* 100 Gb/s. */
644 : speed >= 10000 ? 2000 /* 10 Gb/s. */
645 : speed >= 1000 ? 20000 /* 1 Gb/s. */
646 : speed >= 100 ? 200000 /* 100 Mb/s. */
647 : speed >= 10 ? 2000000 /* 10 Mb/s. */
648 : speed >= 1 ? 20000000 /* 1 Mb/s. */
649 : RSTP_DEFAULT_PORT_PATH_COST; /* 100 Mb/s. */
654 /* Sets the port path cost. */
656 rstp_port_set_path_cost(struct rstp_port *rstp_port,
657 uint32_t new_port_path_cost)
661 rstp = rstp_port->rstp;
662 if (new_port_path_cost >= RSTP_MIN_PORT_PATH_COST &&
663 new_port_path_cost <= RSTP_MAX_PORT_PATH_COST) {
664 VLOG_DBG("%s, port %u, set RSTP port path cost to %d", rstp->name,
665 rstp_port->port_number, new_port_path_cost);
666 ovs_mutex_lock(&mutex);
667 rstp_port->port_path_cost = new_port_path_cost;
668 rstp_port->selected = 0;
669 rstp_port->reselect = 1;
670 ovs_mutex_unlock(&mutex);
674 /* Gets the root path cost. */
676 rstp_get_root_path_cost(const struct rstp *rstp)
680 ovs_mutex_lock(&mutex);
681 cost = rstp->root_priority.root_path_cost;
682 ovs_mutex_unlock(&mutex);
686 /* Returns true if something has happened to 'rstp' which necessitates
687 * flushing the client's MAC learning table.
690 rstp_check_and_reset_fdb_flush(struct rstp *rstp)
697 ovs_mutex_lock(&mutex);
698 if (rstp->ports_count > 0){
699 LIST_FOR_EACH (p, node, &rstp->ports) {
702 /* fdb_flush should be reset by the filtering database
703 * once the entries are removed if rstp_version is TRUE, and
704 * immediately if stp_version is TRUE.*/
705 p->fdb_flush = false;
709 ovs_mutex_unlock(&mutex);
713 /* Finds a port whose state has changed. If successful, stores the port whose
714 * state changed in '*portp' and returns true. If no port has changed, stores
715 * NULL in '*portp' and returns false. */
717 rstp_get_changed_port(struct rstp *rstp, struct rstp_port **portp)
724 ovs_mutex_lock(&mutex);
725 if (rstp->ports_count > 0){
726 LIST_FOR_EACH (p, node, &rstp->ports) {
727 if (p->state_changed) {
728 p->state_changed = false;
731 ovs_mutex_unlock(&mutex);
737 ovs_mutex_unlock(&mutex);
741 /* Returns the port in 'rstp' with number 'port_number'. */
743 rstp_get_port(struct rstp *rstp, int port_number)
745 struct rstp_port *port;
747 ovs_mutex_lock(&mutex);
748 if (rstp->ports_count > 0){
749 LIST_FOR_EACH (port, node, &rstp->ports) {
750 if (port->port_number == port_number) {
751 ovs_mutex_unlock(&mutex);
756 ovs_mutex_unlock(&mutex);
760 /* Updates the port_enabled parameter. */
762 update_port_enabled__(struct rstp_port *p)
764 if (p->mac_operational && p->is_administrative_bridge_port ==
765 RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) {
766 p->port_enabled = true;
768 p->port_enabled = false;
772 /* Sets the port MAC_Operational parameter [6.4.2]. */
774 rstp_port_set_mac_operational(struct rstp_port *p, bool new_mac_operational)
778 ovs_mutex_lock(&mutex);
780 p->mac_operational = new_mac_operational;
781 update_port_enabled__(p);
782 rstp->changes = true;
784 ovs_mutex_unlock(&mutex);
787 /* Gets the port MAC_Operational parameter [6.4.2]. */
789 rstp_port_get_mac_operational(struct rstp_port *p)
793 ovs_mutex_lock(&mutex);
794 value = p->mac_operational;
795 ovs_mutex_unlock(&mutex);
799 /* Sets the port Administrative Bridge Port parameter. */
801 rstp_port_set_administrative_bridge_port(struct rstp_port *p,
802 uint8_t new_admin_port_state)
804 if (new_admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_DISABLED ||
805 new_admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) {
806 p->is_administrative_bridge_port = new_admin_port_state;
807 update_port_enabled__(p);
811 /* Sets the port oper_point_to_point_mac parameter. */
813 rstp_port_set_oper_point_to_point_mac(struct rstp_port *p,
814 uint8_t new_oper_p2p_mac)
816 if (new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_DISABLED ||
817 new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_ENABLED) {
818 p->oper_point_to_point_mac = new_oper_p2p_mac;
819 update_port_enabled__(p);
823 /* Initializes a port with the defaults values for its parameters. */
825 rstp_initialize_port(struct rstp_port *p)
831 rstp_port_set_administrative_bridge_port(p,
832 RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED);
833 rstp_port_set_oper_point_to_point_mac(p, 1);
834 rstp_port_set_priority(p, RSTP_DEFAULT_PORT_PRIORITY);
835 rstp_port_set_port_number(p, 0);
836 rstp_port_set_path_cost(p, RSTP_DEFAULT_PORT_PATH_COST);
837 rstp_port_set_auto_edge(p, true);
839 p->port_receive_sm_state = PORT_RECEIVE_SM_INIT;
840 p->port_protocol_migration_sm_state = PORT_PROTOCOL_MIGRATION_SM_INIT;
841 p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_INIT;
842 p->port_transmit_sm_state = PORT_TRANSMIT_SM_INIT;
843 p->port_information_sm_state = PORT_INFORMATION_SM_INIT;
844 p->port_role_transition_sm_state = PORT_ROLE_TRANSITION_SM_INIT;
845 p->port_state_transition_sm_state = PORT_STATE_TRANSITION_SM_INIT;
846 p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INIT;
850 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" initialized.", rstp->name,
854 /* Reinitialization function used in tests. */
856 reinitialize_port(struct rstp_port *p)
858 struct rstp_port temp_port;
863 memset(p, 0, sizeof(struct rstp_port));
865 p->node = temp_port.node;
866 p->aux = temp_port.aux;
867 p->port_number = temp_port.port_number;
868 p->port_priority = temp_port.port_priority;
869 p->port_id = temp_port.port_id;
870 p->rstp_state = RSTP_DISCARDING;
872 rstp_port_set_administrative_bridge_port(p,
873 RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED);
874 rstp_port_set_oper_point_to_point_mac(p, 1);
875 rstp_port_set_path_cost(p, RSTP_DEFAULT_PORT_PATH_COST);
876 rstp_port_set_auto_edge(p, true);
877 /* Initialize state machines. */
878 p->port_receive_sm_state = PORT_RECEIVE_SM_INIT;
879 p->port_protocol_migration_sm_state =
880 PORT_PROTOCOL_MIGRATION_SM_INIT;
881 p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_INIT;
882 p->port_transmit_sm_state = PORT_TRANSMIT_SM_INIT;
883 p->port_information_sm_state = PORT_INFORMATION_SM_INIT;
884 p->port_role_transition_sm_state = PORT_ROLE_TRANSITION_SM_INIT;
885 p->port_state_transition_sm_state = PORT_STATE_TRANSITION_SM_INIT;
886 p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INIT;
889 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" reinitialized.", rstp->name,
893 /* Sets the port state. */
895 rstp_port_set_state(struct rstp_port *p, enum rstp_state state)
901 VLOG_DBG("%s, port %u: set RSTP port state %s -> %s", rstp->name,
903 rstp_state_name(p->rstp_state), rstp_state_name(state));
905 if (state != p->rstp_state && !p->state_changed) {
906 p->state_changed = true;
907 seq_change(connectivity_seq_get());
909 p->rstp_state = state;
912 /* Adds a RSTP port. */
914 rstp_add_port(struct rstp *rstp) {
915 struct rstp_port *p = xzalloc(sizeof *p);
917 ovs_mutex_lock(&mutex);
919 rstp_initialize_port(p);
920 rstp_port_set_state(p, RSTP_DISCARDING);
921 list_push_back(&rstp->ports, &p->node);
923 rstp->changes = true;
925 ovs_mutex_unlock(&mutex);
926 VLOG_DBG("%s: added port "RSTP_PORT_ID_FMT"", rstp->name, p->port_id);
930 /* Deletes a RSTP port. */
932 rstp_delete_port(struct rstp_port *p) {
935 ovs_mutex_lock(&mutex);
937 rstp_port_set_state(p, RSTP_DISABLED);
938 list_remove(&p->node);
940 VLOG_DBG("%s: removed port "RSTP_PORT_ID_FMT"", rstp->name, p->port_id);
942 ovs_mutex_unlock(&mutex);
945 /* Sets the port Admin Edge parameter. */
947 rstp_port_set_admin_edge(struct rstp_port *rstp_port, bool new_admin_edge)
951 rstp = rstp_port->rstp;
952 if (rstp_port->admin_edge != new_admin_edge) {
953 VLOG_DBG("%s, port %u: set RSTP Admin Edge to %d", rstp->name,
954 rstp_port->port_number, new_admin_edge);
955 ovs_mutex_lock(&mutex);
956 rstp_port->admin_edge = new_admin_edge;
957 ovs_mutex_unlock(&mutex);
961 /* Sets the port Auto Edge parameter. */
963 rstp_port_set_auto_edge(struct rstp_port *rstp_port, bool new_auto_edge)
967 rstp = rstp_port->rstp;
968 if (rstp_port->auto_edge != new_auto_edge) {
969 VLOG_DBG("%s, port %u: set RSTP Auto Edge to %d", rstp->name,
970 rstp_port->port_number, new_auto_edge);
971 ovs_mutex_lock(&mutex);
972 rstp_port->auto_edge = new_auto_edge;
973 ovs_mutex_unlock(&mutex);
977 /* Sets the port mcheck parameter.
978 * [17.19.13] May be set by management to force the Port Protocol Migration
979 * state machine to transmit RST BPDUs for a MigrateTime (17.13.9) period, to
980 * test whether all STP Bridges (17.4) on the attached LAN have been removed
981 * and the Port can continue to transmit RSTP BPDUs. Setting mcheck has no
982 * effect if stpVersion (17.20.12) is TRUE, i.e., the Bridge is operating in
983 * STP Compatibility. mode.
986 rstp_port_set_mcheck(struct rstp_port *rstp_port, bool new_mcheck)
990 ovs_mutex_lock(&mutex);
991 rstp = rstp_port->rstp;
992 if (new_mcheck == true && rstp_port->rstp->force_protocol_version >= 2) {
993 rstp_port->mcheck = true;
995 ovs_mutex_unlock(&mutex);
996 VLOG_DBG("%s, port %u: set RSTP mcheck to %d", rstp->name,
997 rstp_port->port_number, new_mcheck);
1000 /* Returns the designated bridge id. */
1002 rstp_get_designated_id(const struct rstp *rstp)
1004 rstp_identifier designated_id;
1006 ovs_mutex_lock(&mutex);
1007 designated_id = rstp->root_priority.designated_bridge_id;
1008 ovs_mutex_unlock(&mutex);
1009 return designated_id;
1012 /* Returns the root bridge id. */
1014 rstp_get_root_id(const struct rstp *rstp)
1016 rstp_identifier root_id;
1018 ovs_mutex_lock(&mutex);
1019 root_id = rstp->root_priority.root_bridge_id;
1020 ovs_mutex_unlock(&mutex);
1024 /* Returns the designated port id. */
1026 rstp_get_designated_port_id(const struct rstp *rstp)
1028 uint16_t designated_port_id;
1030 ovs_mutex_lock(&mutex);
1031 designated_port_id = rstp->root_priority.designated_port_id;
1032 ovs_mutex_unlock(&mutex);
1033 return designated_port_id;
1036 /* Return the bridge port id. */
1038 rstp_get_bridge_port_id(const struct rstp *rstp)
1040 uint16_t bridge_port_id;
1042 ovs_mutex_lock(&mutex);
1043 bridge_port_id = rstp->root_priority.bridge_port_id;
1044 ovs_mutex_unlock(&mutex);
1045 return bridge_port_id;
1048 /* Returns true if the bridge believes to the be root of the spanning tree,
1052 rstp_is_root_bridge(const struct rstp *rstp)
1056 ovs_mutex_lock(&mutex);
1057 is_root = rstp->bridge_identifier ==
1058 rstp->root_priority.designated_bridge_id;
1059 ovs_mutex_unlock(&mutex);
1063 /* Returns the bridge ID of the bridge currently believed to be the root. */
1065 rstp_get_designated_root(const struct rstp *rstp)
1067 rstp_identifier designated_root;
1069 ovs_mutex_lock(&mutex);
1070 designated_root = rstp->root_priority.designated_bridge_id;
1071 ovs_mutex_unlock(&mutex);
1072 return designated_root;
1075 /* Returns the port connecting 'rstp' to the root bridge, or a null pointer if
1076 * there is no such port.
1079 rstp_get_root_port(struct rstp *rstp)
1081 struct rstp_port *p;
1083 ovs_mutex_lock(&mutex);
1084 if (rstp->ports_count > 0){
1085 LIST_FOR_EACH (p, node, &rstp->ports) {
1086 if (p->port_id == rstp->root_port_id) {
1087 ovs_mutex_unlock(&mutex);
1092 ovs_mutex_unlock(&mutex);
1096 /* Returns the port ID for 'p'. */
1098 rstp_port_get_id(const struct rstp_port *p)
1102 ovs_mutex_lock(&mutex);
1103 port_id = p->port_id;
1104 ovs_mutex_unlock(&mutex);
1108 /* Returns the state of port 'p'. */
1110 rstp_port_get_state(const struct rstp_port *p)
1112 enum rstp_state state;
1114 ovs_mutex_lock(&mutex);
1115 state = p->rstp_state;
1116 ovs_mutex_unlock(&mutex);
1120 /* Returns the role of port 'p'. */
1122 rstp_port_get_role(const struct rstp_port *p)
1124 enum rstp_port_role role;
1126 ovs_mutex_lock(&mutex);
1128 ovs_mutex_unlock(&mutex);
1132 /* Retrieves BPDU transmit and receive counts for 'p'. */
1134 rstp_port_get_counts(const struct rstp_port *p,
1135 int *tx_count, int *rx_count, int *error_count, int *uptime)
1137 ovs_mutex_lock(&mutex);
1138 *tx_count = p->tx_count;
1139 *rx_count = p->rx_rstp_bpdu_cnt;
1140 *error_count = p->error_count;
1141 *uptime = p->uptime;
1142 ovs_mutex_unlock(&mutex);
1146 rstp_port_set_aux(struct rstp_port *p, void *aux)
1148 ovs_mutex_lock(&mutex);
1150 ovs_mutex_unlock(&mutex);
1154 rstp_port_get_aux(struct rstp_port *p)
1158 ovs_mutex_lock(&mutex);
1160 ovs_mutex_unlock(&mutex);
1164 /* Returns true if 'state' is one in which BPDU packets should be received
1165 * and transmitted on a port, false otherwise.
1168 rstp_should_manage_bpdu(enum rstp_state state)
1170 return (state == RSTP_DISCARDING || state == RSTP_LEARNING ||
1171 state == RSTP_FORWARDING);
1174 /* Returns true if 'state' is one in which packets received on a port should
1175 * be forwarded, false otherwise.
1177 * Returns true if 'state' is RSTP_DISABLED, since presumably in that case the
1178 * port should still work, just not have RSTP applied to it.
1181 rstp_forward_in_state(enum rstp_state state)
1183 return (state == RSTP_DISABLED || state == RSTP_FORWARDING);
1186 /* Returns true if 'state' is one in which MAC learning should be done on
1187 * packets received on a port, false otherwise.
1189 * Returns true if 'state' is RSTP_DISABLED, since presumably in that case the
1190 * port should still work, just not have RSTP applied to it. */
1192 rstp_learn_in_state(enum rstp_state state)
1194 return (state == RSTP_DISABLED || state == RSTP_LEARNING ||
1195 state == RSTP_FORWARDING);
1199 static struct rstp *
1200 rstp_find(const char *name) OVS_REQUIRES(mutex)
1204 LIST_FOR_EACH (rstp, node, all_rstps) {
1205 if (!strcmp(rstp->name, name)) {
1213 rstp_unixctl_tcn(struct unixctl_conn *conn, int argc,
1214 const char *argv[], void *aux OVS_UNUSED)
1216 ovs_mutex_lock(&mutex);
1218 struct rstp *rstp = rstp_find(argv[1]);
1220 unixctl_command_reply_error(conn, "No such RSTP object");
1223 rstp->changes = true;
1227 LIST_FOR_EACH (rstp, node, all_rstps) {
1228 rstp->changes = true;
1232 unixctl_command_reply(conn, "OK");
1235 ovs_mutex_unlock(&mutex);