2 * Copyright (c) 2011-2014 M3S, Srl - Italy
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * Rapid Spanning Tree Protocol (IEEE 802.1D-2004) public interface.
21 * Martino Fornasa <mf@fornasa.it>
22 * Daniele Venturino <daniele.venturino@m3s.it>
24 * References to IEEE 802.1D-2004 standard are enclosed in square brackets.
25 * E.g. [17.3], [Table 17-1], etc.
32 #include "rstp-common.h"
33 #include "rstp-state-machines.h"
34 #include <arpa/inet.h>
36 #include <netinet/in.h>
38 #include <sys/types.h>
39 #include "byte-order.h"
40 #include "connectivity.h"
42 #include "ofproto/ofproto.h"
49 VLOG_DEFINE_THIS_MODULE(rstp);
51 struct ovs_mutex rstp_mutex = OVS_MUTEX_INITIALIZER;
53 static struct list all_rstps__ = LIST_INITIALIZER(&all_rstps__);
54 static struct list *const all_rstps OVS_GUARDED_BY(rstp_mutex) = &all_rstps__;
56 /* Internal use only. */
57 static void rstp_set_bridge_address__(struct rstp *, rstp_identifier)
58 OVS_REQUIRES(rstp_mutex);
59 static void rstp_set_bridge_priority__(struct rstp *, int new_priority)
60 OVS_REQUIRES(rstp_mutex);
61 static void rstp_set_bridge_ageing_time__(struct rstp *, int new_ageing_time)
62 OVS_REQUIRES(rstp_mutex);
63 static void rstp_set_bridge_force_protocol_version__(struct rstp *,
64 enum rstp_force_protocol_version)
65 OVS_REQUIRES(rstp_mutex);
66 static void rstp_set_bridge_hello_time__(struct rstp *)
67 OVS_REQUIRES(rstp_mutex);
68 static void rstp_set_bridge_max_age__(struct rstp *, int new_max_age)
69 OVS_REQUIRES(rstp_mutex);
70 static void rstp_set_bridge_forward_delay__(struct rstp *, int new_forward_delay)
71 OVS_REQUIRES(rstp_mutex);
72 static void rstp_set_bridge_transmit_hold_count__(struct rstp *,
73 int new_transmit_hold_count)
74 OVS_REQUIRES(rstp_mutex);
75 static void rstp_set_bridge_migrate_time__(struct rstp *)
76 OVS_REQUIRES(rstp_mutex);
77 static void rstp_set_bridge_times__(struct rstp *, int new_forward_delay,
78 int new_hello_time, int new_max_age,
80 OVS_REQUIRES(rstp_mutex);
82 static struct rstp_port *rstp_get_port__(struct rstp *rstp,
84 OVS_REQUIRES(rstp_mutex);
85 static void set_port_id__(struct rstp_port *)
86 OVS_REQUIRES(rstp_mutex);
87 static void update_port_enabled__(struct rstp_port *)
88 OVS_REQUIRES(rstp_mutex);
89 static void set_bridge_priority__(struct rstp *)
90 OVS_REQUIRES(rstp_mutex);
91 static void reinitialize_rstp__(struct rstp *)
92 OVS_REQUIRES(rstp_mutex);
93 static bool is_port_number_available__(struct rstp *, int, struct rstp_port *)
94 OVS_REQUIRES(rstp_mutex);
95 static uint16_t rstp_first_free_number__(struct rstp *, struct rstp_port *)
96 OVS_REQUIRES(rstp_mutex);
97 static void rstp_initialize_port_defaults__(struct rstp_port *)
98 OVS_REQUIRES(rstp_mutex);
99 static void rstp_port_set_priority__(struct rstp_port *, int priority)
100 OVS_REQUIRES(rstp_mutex);
101 static void rstp_port_set_port_number__(struct rstp_port *,
102 uint16_t port_number)
103 OVS_REQUIRES(rstp_mutex);
104 static void rstp_port_set_path_cost__(struct rstp_port *, uint32_t path_cost)
105 OVS_REQUIRES(rstp_mutex);
106 static void rstp_port_set_administrative_bridge_port__(struct rstp_port *,
107 uint8_t admin_port_state)
108 OVS_REQUIRES(rstp_mutex);
109 static void rstp_port_set_admin_edge__(struct rstp_port *, bool admin_edge)
110 OVS_REQUIRES(rstp_mutex);
111 static void rstp_port_set_auto_edge__(struct rstp_port *, bool auto_edge)
112 OVS_REQUIRES(rstp_mutex);
113 static void rstp_port_set_mcheck__(struct rstp_port *, bool mcheck)
114 OVS_REQUIRES(rstp_mutex);
115 static void reinitialize_port__(struct rstp_port *p)
116 OVS_REQUIRES(rstp_mutex);
119 rstp_state_name(enum rstp_state state)
126 case RSTP_FORWARDING:
128 case RSTP_DISCARDING:
136 rstp_port_role_name(enum rstp_port_role role)
141 case ROLE_DESIGNATED:
154 /* Caller has to hold a reference to prevent 'rstp' from being deleted
155 * while taking a new reference. */
157 rstp_ref(struct rstp *rstp)
158 OVS_EXCLUDED(rstp_mutex)
161 ovs_refcount_ref(&rstp->ref_cnt);
166 /* Frees RSTP struct when reference count reaches zero. */
168 rstp_unref(struct rstp *rstp)
169 OVS_EXCLUDED(rstp_mutex)
171 if (rstp && ovs_refcount_unref(&rstp->ref_cnt) == 1) {
172 ovs_mutex_lock(&rstp_mutex);
174 /* Each RSTP port points back to struct rstp without holding a
175 * reference for that pointer. This is OK as we never move
176 * ports from one bridge to another, and holders always
177 * release their ports before releasing the bridge. This
178 * means that there should be not ports at this time. */
179 ovs_assert(rstp->ports_count == 0);
181 list_remove(&rstp->node);
182 ovs_mutex_unlock(&rstp_mutex);
188 /* Returns the port number. Mutex is needed to guard against
189 * concurrent reinitialization (which can temporarily clear the
192 rstp_port_get_number(const struct rstp_port *p)
193 OVS_EXCLUDED(rstp_mutex)
197 ovs_mutex_lock(&rstp_mutex);
198 number = p->port_number;
199 ovs_mutex_unlock(&rstp_mutex);
204 static void rstp_unixctl_tcn(struct unixctl_conn *, int argc,
205 const char *argv[], void *aux);
207 /* Decrements the State Machines' timers. */
209 rstp_tick_timers(struct rstp *rstp)
210 OVS_EXCLUDED(rstp_mutex)
212 ovs_mutex_lock(&rstp_mutex);
213 decrease_rstp_port_timers__(rstp);
214 ovs_mutex_unlock(&rstp_mutex);
217 /* Processes an incoming BPDU. */
219 rstp_port_received_bpdu(struct rstp_port *rp, const void *bpdu,
221 OVS_EXCLUDED(rstp_mutex)
223 ovs_mutex_lock(&rstp_mutex);
224 /* Only process packets on ports that have RSTP enabled. */
225 if (rp && rp->rstp_state != RSTP_DISABLED) {
226 process_received_bpdu__(rp, bpdu, bpdu_size);
228 ovs_mutex_unlock(&rstp_mutex);
233 OVS_EXCLUDED(rstp_mutex)
235 unixctl_command_register("rstp/tcn", "[bridge]", 0, 1, rstp_unixctl_tcn,
239 /* Creates and returns a new RSTP instance that initially has no ports. */
241 rstp_create(const char *name, rstp_identifier bridge_address,
242 void (*send_bpdu)(struct ofpbuf *bpdu, void *port_aux,
245 OVS_EXCLUDED(rstp_mutex)
249 VLOG_DBG("Creating RSTP instance");
251 rstp = xzalloc(sizeof *rstp);
252 rstp->name = xstrdup(name);
254 ovs_mutex_lock(&rstp_mutex);
255 /* Set bridge address. */
256 rstp_set_bridge_address__(rstp, bridge_address);
257 /* Set default parameters values. */
258 rstp_set_bridge_priority__(rstp, RSTP_DEFAULT_PRIORITY);
259 rstp_set_bridge_ageing_time__(rstp, RSTP_DEFAULT_AGEING_TIME);
260 rstp_set_bridge_force_protocol_version__(rstp, FPV_DEFAULT);
261 rstp_set_bridge_forward_delay__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
262 rstp_set_bridge_hello_time__(rstp);
263 rstp_set_bridge_max_age__(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
264 rstp_set_bridge_migrate_time__(rstp);
265 rstp_set_bridge_transmit_hold_count__(rstp,
266 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
267 rstp_set_bridge_times__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
268 RSTP_BRIDGE_HELLO_TIME,
269 RSTP_DEFAULT_BRIDGE_MAX_AGE, 0);
270 rstp->send_bpdu = send_bpdu;
272 rstp->changes = false;
275 /* Initialize the ports list. */
276 list_init(&rstp->ports);
277 ovs_refcount_init(&rstp->ref_cnt);
279 list_push_back(all_rstps, &rstp->node);
280 ovs_mutex_unlock(&rstp_mutex);
282 VLOG_DBG("RSTP instance creation done");
286 /* Called by rstp_set_bridge_address() and rstp_set_bridge_priority(),
287 * it updates the bridge priority vector according to the values passed by
291 set_bridge_priority__(struct rstp *rstp)
292 OVS_REQUIRES(rstp_mutex)
294 rstp->bridge_priority.root_bridge_id = rstp->bridge_identifier;
295 rstp->bridge_priority.designated_bridge_id = rstp->bridge_identifier;
296 VLOG_DBG("%s: new bridge identifier: "RSTP_ID_FMT"", rstp->name,
297 RSTP_ID_ARGS(rstp->bridge_identifier));
299 /* [17.13] When the bridge address changes, recalculates all priority
302 if (rstp->ports_count > 0) {
305 LIST_FOR_EACH (p, node, &rstp->ports) {
310 rstp->changes = true;
311 updt_roles_tree__(rstp);
314 /* Sets the bridge address. */
316 rstp_set_bridge_address__(struct rstp *rstp, rstp_identifier bridge_address)
317 OVS_REQUIRES(rstp_mutex)
319 VLOG_DBG("%s: set bridge address to: "RSTP_ID_FMT"", rstp->name,
320 RSTP_ID_ARGS(bridge_address));
322 rstp->address = bridge_address;
323 rstp->bridge_identifier = bridge_address;
324 set_bridge_priority__(rstp);
327 /* Sets the bridge address. */
329 rstp_set_bridge_address(struct rstp *rstp, rstp_identifier bridge_address)
330 OVS_EXCLUDED(rstp_mutex)
332 ovs_mutex_lock(&rstp_mutex);
333 rstp_set_bridge_address__(rstp, bridge_address);
334 ovs_mutex_unlock(&rstp_mutex);
338 rstp_get_name(const struct rstp *rstp)
339 OVS_EXCLUDED(rstp_mutex)
343 ovs_mutex_lock(&rstp_mutex);
345 ovs_mutex_unlock(&rstp_mutex);
350 rstp_get_bridge_id(const struct rstp *rstp)
351 OVS_EXCLUDED(rstp_mutex)
353 rstp_identifier bridge_id;
355 ovs_mutex_lock(&rstp_mutex);
356 bridge_id = rstp->bridge_identifier;
357 ovs_mutex_unlock(&rstp_mutex);
362 /* Sets the bridge priority. */
364 rstp_set_bridge_priority__(struct rstp *rstp, int new_priority)
365 OVS_REQUIRES(rstp_mutex)
367 new_priority = ROUND_DOWN(new_priority, RSTP_PRIORITY_STEP);
369 if (new_priority >= RSTP_MIN_PRIORITY
370 && new_priority <= RSTP_MAX_PRIORITY) {
371 VLOG_DBG("%s: set bridge priority to %d", rstp->name, new_priority);
373 rstp->priority = new_priority;
374 rstp->bridge_identifier &= 0x0000ffffffffffffULL;
375 rstp->bridge_identifier |= (uint64_t)new_priority << 48;
376 set_bridge_priority__(rstp);
381 rstp_set_bridge_priority(struct rstp *rstp, int new_priority)
382 OVS_EXCLUDED(rstp_mutex)
384 ovs_mutex_lock(&rstp_mutex);
385 rstp_set_bridge_priority__(rstp, new_priority);
386 ovs_mutex_unlock(&rstp_mutex);
389 /* Sets the bridge ageing time. */
391 rstp_set_bridge_ageing_time__(struct rstp *rstp, int new_ageing_time)
392 OVS_REQUIRES(rstp_mutex)
394 if (new_ageing_time >= RSTP_MIN_AGEING_TIME
395 && new_ageing_time <= RSTP_MAX_AGEING_TIME) {
396 VLOG_DBG("%s: set ageing time to %d", rstp->name, new_ageing_time);
398 rstp->ageing_time = new_ageing_time;
403 rstp_set_bridge_ageing_time(struct rstp *rstp, int new_ageing_time)
404 OVS_EXCLUDED(rstp_mutex)
406 ovs_mutex_lock(&rstp_mutex);
407 rstp_set_bridge_ageing_time__(rstp, new_ageing_time);
408 ovs_mutex_unlock(&rstp_mutex);
411 /* Reinitializes RSTP when switching from RSTP mode to STP mode
415 reinitialize_rstp__(struct rstp *rstp)
416 OVS_REQUIRES(rstp_mutex)
419 static struct list ports;
421 /* Copy rstp in temp */
425 /* stop and clear rstp */
426 memset(rstp, 0, sizeof(struct rstp));
428 /* Initialize rstp. */
429 rstp->name = temp.name;
430 /* Set bridge address. */
431 rstp_set_bridge_address__(rstp, temp.address);
432 /* Set default parameters values. */
433 rstp_set_bridge_priority__(rstp, RSTP_DEFAULT_PRIORITY);
434 rstp_set_bridge_ageing_time__(rstp, RSTP_DEFAULT_AGEING_TIME);
435 rstp_set_bridge_forward_delay__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
436 rstp_set_bridge_hello_time__(rstp);
437 rstp_set_bridge_max_age__(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
438 rstp_set_bridge_migrate_time__(rstp);
439 rstp_set_bridge_transmit_hold_count__(rstp,
440 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
441 rstp_set_bridge_times__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
442 RSTP_BRIDGE_HELLO_TIME,
443 RSTP_DEFAULT_BRIDGE_MAX_AGE, 0);
445 rstp->send_bpdu = temp.send_bpdu;
446 rstp->aux = temp.aux;
447 rstp->node = temp.node;
448 rstp->changes = false;
451 rstp->ports_count = temp.ports_count;
453 if (rstp->ports_count > 0) {
456 LIST_FOR_EACH (p, node, &rstp->ports) {
457 reinitialize_port__(p);
460 rstp->ref_cnt = temp.ref_cnt;
463 /* Sets the force protocol version parameter. */
465 rstp_set_bridge_force_protocol_version__(struct rstp *rstp,
466 enum rstp_force_protocol_version new_force_protocol_version)
467 OVS_REQUIRES(rstp_mutex)
469 if (new_force_protocol_version != rstp->force_protocol_version &&
470 (new_force_protocol_version == FPV_STP_COMPATIBILITY ||
471 new_force_protocol_version == FPV_DEFAULT)) {
472 VLOG_DBG("%s: set bridge Force Protocol Version to %d", rstp->name,
473 new_force_protocol_version);
475 /* [17.13] The Spanning Tree Protocol Entity shall be reinitialized,
476 * as specified by the assertion of BEGIN (17.18.1) in the state
477 * machine specification.
479 reinitialize_rstp__(rstp);
480 rstp->force_protocol_version = new_force_protocol_version;
481 if (rstp->force_protocol_version < 2) {
482 rstp->stp_version = true;
483 rstp->rstp_version = false;
485 rstp->stp_version = false;
486 rstp->rstp_version = true;
488 rstp->changes = true;
494 rstp_set_bridge_force_protocol_version(struct rstp *rstp,
495 enum rstp_force_protocol_version new_force_protocol_version)
496 OVS_EXCLUDED(rstp_mutex)
498 ovs_mutex_lock(&rstp_mutex);
499 rstp_set_bridge_force_protocol_version__(rstp, new_force_protocol_version);
500 ovs_mutex_unlock(&rstp_mutex);
503 /* Sets the bridge Hello Time parameter. */
505 rstp_set_bridge_hello_time__(struct rstp *rstp)
506 OVS_REQUIRES(rstp_mutex)
508 VLOG_DBG("%s: set RSTP Hello Time to %d", rstp->name,
509 RSTP_BRIDGE_HELLO_TIME);
510 /* 2 is the only acceptable value. */
511 rstp->bridge_hello_time = RSTP_BRIDGE_HELLO_TIME;
514 /* Sets the bridge max age parameter. */
516 rstp_set_bridge_max_age__(struct rstp *rstp, int new_max_age)
517 OVS_REQUIRES(rstp_mutex)
519 if (new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE &&
520 new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) {
522 if ((2 * (rstp->bridge_forward_delay - 1) >= new_max_age)
523 && (new_max_age >= 2 * rstp->bridge_hello_time)) {
524 VLOG_DBG("%s: set RSTP bridge Max Age to %d", rstp->name,
527 rstp->bridge_max_age = new_max_age;
528 rstp->bridge_times.max_age = new_max_age;
534 rstp_set_bridge_max_age(struct rstp *rstp, int new_max_age)
535 OVS_EXCLUDED(rstp_mutex)
537 ovs_mutex_lock(&rstp_mutex);
538 rstp_set_bridge_max_age__(rstp, new_max_age);
539 ovs_mutex_unlock(&rstp_mutex);
542 /* Sets the bridge forward delay parameter. */
544 rstp_set_bridge_forward_delay__(struct rstp *rstp, int new_forward_delay)
545 OVS_REQUIRES(rstp_mutex)
547 if (new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY
548 && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) {
549 if (2 * (new_forward_delay - 1) >= rstp->bridge_max_age) {
550 VLOG_DBG("%s: set RSTP Forward Delay to %d", rstp->name,
552 rstp->bridge_forward_delay = new_forward_delay;
553 rstp->bridge_times.forward_delay = new_forward_delay;
559 rstp_set_bridge_forward_delay(struct rstp *rstp, int new_forward_delay)
560 OVS_EXCLUDED(rstp_mutex)
562 ovs_mutex_lock(&rstp_mutex);
563 rstp_set_bridge_forward_delay__(rstp, new_forward_delay);
564 ovs_mutex_unlock(&rstp_mutex);
567 /* Sets the bridge transmit hold count parameter. */
569 rstp_set_bridge_transmit_hold_count__(struct rstp *rstp,
570 int new_transmit_hold_count)
571 OVS_REQUIRES(rstp_mutex)
575 if (new_transmit_hold_count >= RSTP_MIN_TRANSMIT_HOLD_COUNT
576 && new_transmit_hold_count <= RSTP_MAX_TRANSMIT_HOLD_COUNT) {
577 VLOG_DBG("%s: set RSTP Transmit Hold Count to %d", rstp->name,
578 new_transmit_hold_count);
579 /* Resetting txCount on all ports [17.13]. */
581 rstp->transmit_hold_count = new_transmit_hold_count;
582 if (rstp->ports_count > 0) {
583 LIST_FOR_EACH (p, node, &rstp->ports) {
591 rstp_set_bridge_transmit_hold_count(struct rstp *rstp,
592 int new_transmit_hold_count)
593 OVS_EXCLUDED(rstp_mutex)
595 ovs_mutex_lock(&rstp_mutex);
596 rstp_set_bridge_transmit_hold_count__(rstp, new_transmit_hold_count);
597 ovs_mutex_unlock(&rstp_mutex);
600 /* Sets the bridge migrate time parameter. */
602 rstp_set_bridge_migrate_time__(struct rstp *rstp)
603 OVS_REQUIRES(rstp_mutex)
605 VLOG_DBG("%s: set RSTP Migrate Time to %d", rstp->name,
607 /* 3 is the only acceptable value */
608 rstp->migrate_time = RSTP_MIGRATE_TIME;
611 /* Sets the bridge times. */
613 rstp_set_bridge_times__(struct rstp *rstp, int new_forward_delay,
614 int new_hello_time, int new_max_age,
616 OVS_REQUIRES(rstp_mutex)
618 VLOG_DBG("%s: set RSTP times to (%d, %d, %d, %d)", rstp->name,
619 new_forward_delay, new_hello_time, new_max_age, new_message_age);
620 if (new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY
621 && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) {
622 rstp->bridge_times.forward_delay = new_forward_delay;
624 if (new_hello_time == RSTP_BRIDGE_HELLO_TIME) {
625 rstp->bridge_times.hello_time = new_hello_time;
627 if (new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE
628 && new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) {
629 rstp->bridge_times.max_age = new_max_age;
631 rstp->bridge_times.message_age = new_message_age;
634 /* Sets the port id, it is called by rstp_port_set_port_number__() or
635 * rstp_port_set_priority__().
638 set_port_id__(struct rstp_port *p)
639 OVS_REQUIRES(rstp_mutex)
644 /* [9.2.7] Port identifier. */
645 p->port_id = p->port_number | (p->priority << 8);
646 VLOG_DBG("%s: new RSTP port id "RSTP_PORT_ID_FMT"", rstp->name,
650 /* Sets the port priority. */
652 rstp_port_set_priority__(struct rstp_port *port, int priority)
653 OVS_REQUIRES(rstp_mutex)
655 if (priority >= RSTP_MIN_PORT_PRIORITY
656 && priority <= RSTP_MAX_PORT_PRIORITY) {
657 VLOG_DBG("%s, port %u: set RSTP port priority to %d", port->rstp->name,
658 port->port_number, priority);
660 priority -= priority % RSTP_STEP_PORT_PRIORITY;
661 port->priority = priority;
663 port->selected = false;
664 port->reselect = true;
668 /* Checks if a port number is available. */
670 is_port_number_available__(struct rstp *rstp, int n, struct rstp_port *port)
671 OVS_REQUIRES(rstp_mutex)
673 if (n >= 1 && n <= RSTP_MAX_PORTS) {
674 struct rstp_port *p = rstp_get_port__(rstp, n);
676 return p == NULL || p == port;
682 rstp_first_free_number__(struct rstp *rstp, struct rstp_port *rstp_port)
683 OVS_REQUIRES(rstp_mutex)
687 while (free_number <= RSTP_MAX_PORTS) {
688 if (is_port_number_available__(rstp, free_number, rstp_port)) {
693 VLOG_DBG("%s, No free port number available.", rstp->name);
697 /* Sets the port number. */
699 rstp_port_set_port_number__(struct rstp_port *port, uint16_t port_number)
700 OVS_REQUIRES(rstp_mutex)
702 /* If new_port_number is available, use it, otherwise use the first free
703 * available port number. */
705 is_port_number_available__(port->rstp, port_number, port)
707 : rstp_first_free_number__(port->rstp, port);
710 /* [17.13] is not clear. I suppose that a port number change
711 * should trigger reselection like a port priority change. */
712 port->selected = false;
713 port->reselect = true;
715 VLOG_DBG("%s: set new RSTP port number %d", port->rstp->name,
719 /* Converts the link speed to a port path cost [Table 17-3]. */
721 rstp_convert_speed_to_cost(unsigned int speed)
725 value = speed >= 10000000 ? 2 /* 10 Tb/s. */
726 : speed >= 1000000 ? 20 /* 1 Tb/s. */
727 : speed >= 100000 ? 200 /* 100 Gb/s. */
728 : speed >= 10000 ? 2000 /* 10 Gb/s. */
729 : speed >= 1000 ? 20000 /* 1 Gb/s. */
730 : speed >= 100 ? 200000 /* 100 Mb/s. */
731 : speed >= 10 ? 2000000 /* 10 Mb/s. */
732 : speed >= 1 ? 20000000 /* 1 Mb/s. */
733 : RSTP_DEFAULT_PORT_PATH_COST; /* 100 Mb/s. */
738 /* Sets the port path cost. */
740 rstp_port_set_path_cost__(struct rstp_port *port, uint32_t path_cost)
741 OVS_REQUIRES(rstp_mutex)
743 if (path_cost >= RSTP_MIN_PORT_PATH_COST
744 && path_cost <= RSTP_MAX_PORT_PATH_COST) {
745 VLOG_DBG("%s, port %u, set RSTP port path cost to %d",
746 port->rstp->name, port->port_number, path_cost);
748 port->port_path_cost = path_cost;
749 port->selected = false;
750 port->reselect = true;
754 /* Gets the root path cost. */
756 rstp_get_root_path_cost(const struct rstp *rstp)
757 OVS_EXCLUDED(rstp_mutex)
761 ovs_mutex_lock(&rstp_mutex);
762 cost = rstp->root_priority.root_path_cost;
763 ovs_mutex_unlock(&rstp_mutex);
767 /* Returns true if something has happened to 'rstp' which necessitates
768 * flushing the client's MAC learning table.
771 rstp_check_and_reset_fdb_flush(struct rstp *rstp)
772 OVS_EXCLUDED(rstp_mutex)
779 ovs_mutex_lock(&rstp_mutex);
780 if (rstp->ports_count > 0){
781 LIST_FOR_EACH (p, node, &rstp->ports) {
784 /* fdb_flush should be reset by the filtering database
785 * once the entries are removed if rstp_version is TRUE, and
786 * immediately if stp_version is TRUE.*/
787 p->fdb_flush = false;
791 ovs_mutex_unlock(&rstp_mutex);
795 /* Finds a port whose state has changed, and returns the aux pointer set for
796 * the port. A NULL pointer is returned when no changed port is found. On
797 * return '*portp' contains the pointer to the rstp port that changed, or NULL
798 * if no changed port can be found.
800 * If '*portp' is passed as non-NULL, it must be the value set by the last
801 * invocation of this function.
803 * This function may only be called by the thread that creates and deletes
804 * ports. Otherwise this function is not thread safe, as the returned
805 * '*portp' could become stale before it is used in the next invocation. */
807 rstp_get_next_changed_port_aux(struct rstp *rstp, struct rstp_port **portp)
811 ovs_mutex_lock(&rstp_mutex);
812 if (*portp == NULL) {
815 LIST_FOR_EACH (p, node, &rstp->ports) {
816 if (p->state_changed) {
817 p->state_changed = false;
823 } else { /* continue */
824 struct rstp_port *p = *portp;
826 LIST_FOR_EACH_CONTINUE (p, node, &rstp->ports) {
827 if (p->state_changed) {
828 p->state_changed = false;
835 /* No changed port found. */
838 ovs_mutex_unlock(&rstp_mutex);
842 /* Returns the port in 'rstp' with number 'port_number'.
844 * XXX: May only be called while concurrent deletion of ports is excluded. */
845 static struct rstp_port *
846 rstp_get_port__(struct rstp *rstp, uint16_t port_number)
847 OVS_REQUIRES(rstp_mutex)
849 struct rstp_port *port;
851 ovs_assert(rstp && port_number > 0 && port_number <= RSTP_MAX_PORTS);
853 if (rstp->ports_count > 0) {
854 LIST_FOR_EACH (port, node, &rstp->ports) {
855 if (port->port_number == port_number) {
864 rstp_get_port(struct rstp *rstp, uint16_t port_number)
865 OVS_EXCLUDED(rstp_mutex)
869 ovs_mutex_lock(&rstp_mutex);
870 p = rstp_get_port__(rstp, port_number);
871 ovs_mutex_unlock(&rstp_mutex);
876 rstp_get_port_aux(struct rstp *rstp, uint16_t port_number)
877 OVS_EXCLUDED(rstp_mutex)
882 ovs_mutex_lock(&rstp_mutex);
883 p = rstp_get_port__(rstp, port_number);
885 ovs_mutex_unlock(&rstp_mutex);
889 /* Updates the port_enabled parameter. */
891 update_port_enabled__(struct rstp_port *p)
892 OVS_REQUIRES(rstp_mutex)
894 if (p->mac_operational && p->is_administrative_bridge_port
895 == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) {
896 p->port_enabled = true;
898 p->port_enabled = false;
902 /* Sets the port MAC_Operational parameter [6.4.2]. */
904 rstp_port_set_mac_operational(struct rstp_port *p, bool new_mac_operational)
905 OVS_EXCLUDED(rstp_mutex)
909 ovs_mutex_lock(&rstp_mutex);
911 if (p->mac_operational != new_mac_operational) {
912 p->mac_operational = new_mac_operational;
913 update_port_enabled__(p);
914 rstp->changes = true;
917 ovs_mutex_unlock(&rstp_mutex);
920 /* Sets the port Administrative Bridge Port parameter. */
922 rstp_port_set_administrative_bridge_port__(struct rstp_port *p,
923 uint8_t admin_port_state)
924 OVS_REQUIRES(rstp_mutex)
926 if (admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_DISABLED
927 || admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) {
929 p->is_administrative_bridge_port = admin_port_state;
930 update_port_enabled__(p);
934 /* Sets the port oper_point_to_point_mac parameter. */
936 rstp_port_set_oper_point_to_point_mac__(struct rstp_port *p,
937 uint8_t new_oper_p2p_mac)
938 OVS_REQUIRES(rstp_mutex)
940 if (new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_DISABLED
941 || new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_ENABLED) {
943 p->oper_point_to_point_mac = new_oper_p2p_mac;
944 update_port_enabled__(p);
948 /* Initializes a port with the defaults values for its parameters. */
950 rstp_initialize_port_defaults__(struct rstp_port *p)
951 OVS_REQUIRES(rstp_mutex)
953 rstp_port_set_administrative_bridge_port__(p,
954 RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED);
955 rstp_port_set_oper_point_to_point_mac__(p, 1);
956 rstp_port_set_path_cost__(p, RSTP_DEFAULT_PORT_PATH_COST);
957 rstp_port_set_admin_edge__(p, false);
958 rstp_port_set_auto_edge__(p, true);
959 rstp_port_set_mcheck__(p, false);
961 /* Initialize state machines. */
962 p->port_receive_sm_state = PORT_RECEIVE_SM_INIT;
963 p->port_protocol_migration_sm_state = PORT_PROTOCOL_MIGRATION_SM_INIT;
964 p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_INIT;
965 p->port_transmit_sm_state = PORT_TRANSMIT_SM_INIT;
966 p->port_information_sm_state = PORT_INFORMATION_SM_INIT;
967 p->port_role_transition_sm_state = PORT_ROLE_TRANSITION_SM_INIT;
968 p->port_state_transition_sm_state = PORT_STATE_TRANSITION_SM_INIT;
969 p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INIT;
975 reinitialize_port__(struct rstp_port *p)
976 OVS_REQUIRES(rstp_mutex)
978 struct rstp_port temp_port;
983 memset(p, 0, sizeof(struct rstp_port));
985 p->ref_cnt = temp_port.ref_cnt;
987 p->node = temp_port.node;
988 p->aux = temp_port.aux;
989 p->port_number = temp_port.port_number;
990 p->port_priority = temp_port.port_priority;
991 p->port_id = temp_port.port_id;
992 p->rstp_state = RSTP_DISCARDING;
994 rstp_initialize_port_defaults__(p);
996 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" reinitialized.", rstp->name,
1001 reinitialize_port(struct rstp_port *p)
1002 OVS_EXCLUDED(rstp_mutex)
1004 ovs_mutex_lock(&rstp_mutex);
1005 reinitialize_port__(p);
1006 ovs_mutex_unlock(&rstp_mutex);
1009 /* Sets the port state. */
1011 rstp_port_set_state__(struct rstp_port *p, enum rstp_state state)
1012 OVS_REQUIRES(rstp_mutex)
1017 VLOG_DBG("%s, port %u: set RSTP port state %s -> %s", rstp->name,
1019 rstp_state_name(p->rstp_state), rstp_state_name(state));
1021 if (state != p->rstp_state && !p->state_changed) {
1022 p->state_changed = true;
1023 seq_change(connectivity_seq_get());
1025 p->rstp_state = state;
1029 rstp_port_set_state(struct rstp_port *p, enum rstp_state state)
1030 OVS_EXCLUDED(rstp_mutex)
1032 ovs_mutex_lock(&rstp_mutex);
1033 rstp_port_set_state__(p, state);
1034 ovs_mutex_unlock(&rstp_mutex);
1037 /* Adds a RSTP port. */
1039 rstp_add_port(struct rstp *rstp)
1040 OVS_EXCLUDED(rstp_mutex)
1042 struct rstp_port *p = xzalloc(sizeof *p);
1044 ovs_refcount_init(&p->ref_cnt);
1046 ovs_mutex_lock(&rstp_mutex);
1048 rstp_port_set_priority__(p, RSTP_DEFAULT_PORT_PRIORITY);
1049 rstp_port_set_port_number__(p, 0);
1051 rstp_initialize_port_defaults__(p);
1052 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" initialized.", rstp->name,
1055 rstp_port_set_state__(p, RSTP_DISCARDING);
1056 list_push_back(&rstp->ports, &p->node);
1057 rstp->ports_count++;
1058 rstp->changes = true;
1060 VLOG_DBG("%s: added port "RSTP_PORT_ID_FMT"", rstp->name, p->port_id);
1061 ovs_mutex_unlock(&rstp_mutex);
1065 /* Caller has to hold a reference to prevent 'rstp_port' from being deleted
1066 * while taking a new reference. */
1068 rstp_port_ref(const struct rstp_port *rp_)
1069 OVS_EXCLUDED(rstp_mutex)
1071 struct rstp_port *rp = CONST_CAST(struct rstp_port *, rp_);
1074 ovs_refcount_ref(&rp->ref_cnt);
1079 /* Frees RSTP struct. This can be caller by any thread. */
1081 rstp_port_unref(struct rstp_port *rp)
1082 OVS_EXCLUDED(rstp_mutex)
1084 if (rp && ovs_refcount_unref(&rp->ref_cnt) == 1) {
1087 ovs_mutex_lock(&rstp_mutex);
1089 rstp_port_set_state__(rp, RSTP_DISABLED);
1090 list_remove(&rp->node);
1091 rstp->ports_count--;
1092 VLOG_DBG("%s: removed port "RSTP_PORT_ID_FMT"", rstp->name, rp->port_id);
1093 ovs_mutex_unlock(&rstp_mutex);
1098 /* Sets the port Admin Edge parameter. */
1100 rstp_port_set_admin_edge__(struct rstp_port *port, bool admin_edge)
1101 OVS_REQUIRES(rstp_mutex)
1103 if (port->admin_edge != admin_edge) {
1104 VLOG_DBG("%s, port %u: set RSTP Admin Edge to %d", port->rstp->name,
1105 port->port_number, admin_edge);
1107 port->admin_edge = admin_edge;
1111 /* Sets the port Auto Edge parameter. */
1113 rstp_port_set_auto_edge__(struct rstp_port *port, bool auto_edge)
1114 OVS_REQUIRES(rstp_mutex)
1116 if (port->auto_edge != auto_edge) {
1117 VLOG_DBG("%s, port %u: set RSTP Auto Edge to %d", port->rstp->name,
1118 port->port_number, auto_edge);
1120 port->auto_edge = auto_edge;
1124 /* Sets the port mcheck parameter.
1125 * [17.19.13] May be set by management to force the Port Protocol Migration
1126 * state machine to transmit RST BPDUs for a MigrateTime (17.13.9) period, to
1127 * test whether all STP Bridges (17.4) on the attached LAN have been removed
1128 * and the Port can continue to transmit RSTP BPDUs. Setting mcheck has no
1129 * effect if stpVersion (17.20.12) is TRUE, i.e., the Bridge is operating in
1130 * STP Compatibility mode.
1133 rstp_port_set_mcheck__(struct rstp_port *port, bool mcheck)
1134 OVS_REQUIRES(rstp_mutex)
1136 if (mcheck == true && port->rstp->force_protocol_version >= 2) {
1137 port->mcheck = true;
1139 VLOG_DBG("%s, port %u: set RSTP mcheck to %d", port->rstp->name,
1140 port->port_number, mcheck);
1144 /* Returns the designated bridge id. */
1146 rstp_get_designated_id(const struct rstp *rstp)
1147 OVS_EXCLUDED(rstp_mutex)
1149 rstp_identifier designated_id;
1151 ovs_mutex_lock(&rstp_mutex);
1152 designated_id = rstp->root_priority.designated_bridge_id;
1153 ovs_mutex_unlock(&rstp_mutex);
1155 return designated_id;
1158 /* Returns the root bridge id. */
1160 rstp_get_root_id(const struct rstp *rstp)
1161 OVS_EXCLUDED(rstp_mutex)
1163 rstp_identifier root_id;
1165 ovs_mutex_lock(&rstp_mutex);
1166 root_id = rstp->root_priority.root_bridge_id;
1167 ovs_mutex_unlock(&rstp_mutex);
1172 /* Returns the designated port id. */
1174 rstp_get_designated_port_id(const struct rstp *rstp)
1175 OVS_EXCLUDED(rstp_mutex)
1177 uint16_t designated_port_id;
1179 ovs_mutex_lock(&rstp_mutex);
1180 designated_port_id = rstp->root_priority.designated_port_id;
1181 ovs_mutex_unlock(&rstp_mutex);
1183 return designated_port_id;
1186 /* Return the bridge port id. */
1188 rstp_get_bridge_port_id(const struct rstp *rstp)
1189 OVS_EXCLUDED(rstp_mutex)
1191 uint16_t bridge_port_id;
1193 ovs_mutex_lock(&rstp_mutex);
1194 bridge_port_id = rstp->root_priority.bridge_port_id;
1195 ovs_mutex_unlock(&rstp_mutex);
1197 return bridge_port_id;
1200 /* Returns true if the bridge believes to the be root of the spanning tree,
1204 rstp_is_root_bridge(const struct rstp *rstp)
1205 OVS_EXCLUDED(rstp_mutex)
1209 ovs_mutex_lock(&rstp_mutex);
1210 is_root = rstp->bridge_identifier ==
1211 rstp->root_priority.designated_bridge_id;
1212 ovs_mutex_unlock(&rstp_mutex);
1217 /* Returns the bridge ID of the bridge currently believed to be the root. */
1219 rstp_get_designated_root(const struct rstp *rstp)
1220 OVS_EXCLUDED(rstp_mutex)
1222 rstp_identifier designated_root;
1224 ovs_mutex_lock(&rstp_mutex);
1225 designated_root = rstp->root_priority.designated_bridge_id;
1226 ovs_mutex_unlock(&rstp_mutex);
1228 return designated_root;
1231 /* Returns the port connecting 'rstp' to the root bridge, or a null pointer if
1232 * there is no such port.
1235 rstp_get_root_port(struct rstp *rstp)
1236 OVS_EXCLUDED(rstp_mutex)
1238 struct rstp_port *p;
1240 ovs_mutex_lock(&rstp_mutex);
1241 if (rstp->ports_count > 0){
1242 LIST_FOR_EACH (p, node, &rstp->ports) {
1243 if (p->port_id == rstp->root_port_id) {
1244 ovs_mutex_unlock(&rstp_mutex);
1249 ovs_mutex_unlock(&rstp_mutex);
1253 /* Returns the state of port 'p'. */
1255 rstp_port_get_state(const struct rstp_port *p)
1257 enum rstp_state state;
1259 ovs_mutex_lock(&rstp_mutex);
1260 state = p->rstp_state;
1261 ovs_mutex_unlock(&rstp_mutex);
1266 /* Retrieves port status. */
1268 rstp_port_get_status(const struct rstp_port *p, uint16_t *id,
1269 enum rstp_state *state, enum rstp_port_role *role,
1270 int *tx_count, int *rx_count, int *error_count,
1272 OVS_EXCLUDED(rstp_mutex)
1274 ovs_mutex_lock(&rstp_mutex);
1276 *state = p->rstp_state;
1279 *tx_count = p->tx_count;
1280 *rx_count = p->rx_rstp_bpdu_cnt;
1281 *error_count = p->error_count;
1282 *uptime = p->uptime;
1283 ovs_mutex_unlock(&rstp_mutex);
1287 rstp_port_set(struct rstp_port *port, uint16_t port_num, int priority,
1288 uint32_t path_cost, bool is_admin_edge, bool is_auto_edge,
1289 bool do_mcheck, void *aux)
1290 OVS_EXCLUDED(rstp_mutex)
1292 ovs_mutex_lock(&rstp_mutex);
1294 rstp_port_set_priority__(port, priority);
1295 rstp_port_set_port_number__(port, port_num);
1296 rstp_port_set_path_cost__(port, path_cost);
1297 rstp_port_set_admin_edge__(port, is_admin_edge);
1298 rstp_port_set_auto_edge__(port, is_auto_edge);
1299 rstp_port_set_mcheck__(port, do_mcheck);
1300 ovs_mutex_unlock(&rstp_mutex);
1303 /* Individual setters only used by test-rstp.c. */
1305 rstp_port_set_priority(struct rstp_port *port, int priority)
1306 OVS_EXCLUDED(rstp_mutex)
1308 ovs_mutex_lock(&rstp_mutex);
1309 rstp_port_set_priority__(port, priority);
1310 ovs_mutex_unlock(&rstp_mutex);
1314 rstp_port_set_path_cost(struct rstp_port *port, uint32_t path_cost)
1315 OVS_EXCLUDED(rstp_mutex)
1317 ovs_mutex_lock(&rstp_mutex);
1318 rstp_port_set_path_cost__(port, path_cost);
1319 ovs_mutex_unlock(&rstp_mutex);
1323 rstp_port_set_aux(struct rstp_port *port, void *aux)
1324 OVS_EXCLUDED(rstp_mutex)
1326 ovs_mutex_lock(&rstp_mutex);
1328 ovs_mutex_unlock(&rstp_mutex);
1332 static struct rstp *
1333 rstp_find(const char *name)
1334 OVS_REQUIRES(rstp_mutex)
1338 LIST_FOR_EACH (rstp, node, all_rstps) {
1339 if (!strcmp(rstp->name, name)) {
1347 rstp_unixctl_tcn(struct unixctl_conn *conn, int argc,
1348 const char *argv[], void *aux OVS_UNUSED)
1349 OVS_EXCLUDED(rstp_mutex)
1351 ovs_mutex_lock(&rstp_mutex);
1353 struct rstp *rstp = rstp_find(argv[1]);
1355 unixctl_command_reply_error(conn, "No such RSTP object");
1358 rstp->changes = true;
1362 LIST_FOR_EACH (rstp, node, all_rstps) {
1363 rstp->changes = true;
1367 unixctl_command_reply(conn, "OK");
1370 ovs_mutex_unlock(&rstp_mutex);