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_admin_point_to_point_mac__(struct rstp_port *,
114 enum rstp_admin_point_to_point_mac_state admin_p2p_mac_state)
115 OVS_REQUIRES(rstp_mutex);
116 static void rstp_port_set_mcheck__(struct rstp_port *, bool mcheck)
117 OVS_REQUIRES(rstp_mutex);
118 static void reinitialize_port__(struct rstp_port *p)
119 OVS_REQUIRES(rstp_mutex);
122 rstp_state_name(enum rstp_state state)
129 case RSTP_FORWARDING:
131 case RSTP_DISCARDING:
139 rstp_port_role_name(enum rstp_port_role role)
144 case ROLE_DESIGNATED:
157 /* Caller has to hold a reference to prevent 'rstp' from being deleted
158 * while taking a new reference. */
160 rstp_ref(struct rstp *rstp)
161 OVS_EXCLUDED(rstp_mutex)
164 ovs_refcount_ref(&rstp->ref_cnt);
169 /* Frees RSTP struct when reference count reaches zero. */
171 rstp_unref(struct rstp *rstp)
172 OVS_EXCLUDED(rstp_mutex)
174 if (rstp && ovs_refcount_unref_relaxed(&rstp->ref_cnt) == 1) {
175 ovs_mutex_lock(&rstp_mutex);
177 /* Each RSTP port points back to struct rstp without holding a
178 * reference for that pointer. This is OK as we never move
179 * ports from one bridge to another, and holders always
180 * release their ports before releasing the bridge. This
181 * means that there should be not ports at this time. */
182 ovs_assert(hmap_is_empty(&rstp->ports));
184 list_remove(&rstp->node);
185 ovs_mutex_unlock(&rstp_mutex);
191 /* Returns the port number. Mutex is needed to guard against
192 * concurrent reinitialization (which can temporarily clear the
195 rstp_port_get_number(const struct rstp_port *p)
196 OVS_EXCLUDED(rstp_mutex)
200 ovs_mutex_lock(&rstp_mutex);
201 number = p->port_number;
202 ovs_mutex_unlock(&rstp_mutex);
207 static void rstp_unixctl_tcn(struct unixctl_conn *, int argc,
208 const char *argv[], void *aux);
210 /* Decrements the State Machines' timers. */
212 rstp_tick_timers(struct rstp *rstp)
213 OVS_EXCLUDED(rstp_mutex)
215 ovs_mutex_lock(&rstp_mutex);
216 decrease_rstp_port_timers__(rstp);
217 ovs_mutex_unlock(&rstp_mutex);
220 /* Processes an incoming BPDU. */
222 rstp_port_received_bpdu(struct rstp_port *rp, const void *bpdu,
224 OVS_EXCLUDED(rstp_mutex)
226 ovs_mutex_lock(&rstp_mutex);
227 /* Only process packets on ports that have RSTP enabled. */
228 if (rp && rp->rstp_state != RSTP_DISABLED) {
229 process_received_bpdu__(rp, bpdu, bpdu_size);
231 ovs_mutex_unlock(&rstp_mutex);
236 OVS_EXCLUDED(rstp_mutex)
238 unixctl_command_register("rstp/tcn", "[bridge]", 0, 1, rstp_unixctl_tcn,
242 /* Creates and returns a new RSTP instance that initially has no ports. */
244 rstp_create(const char *name, rstp_identifier bridge_address,
245 void (*send_bpdu)(struct ofpbuf *bpdu, void *port_aux,
248 OVS_EXCLUDED(rstp_mutex)
252 VLOG_DBG("Creating RSTP instance");
254 rstp = xzalloc(sizeof *rstp);
255 rstp->name = xstrdup(name);
257 /* Initialize the ports map before calling any setters,
258 * so that the state machines will see an empty ports map. */
259 hmap_init(&rstp->ports);
261 ovs_mutex_lock(&rstp_mutex);
262 /* Set bridge address. */
263 rstp_set_bridge_address__(rstp, bridge_address);
264 /* Set default parameters values. */
265 rstp_set_bridge_priority__(rstp, RSTP_DEFAULT_PRIORITY);
266 rstp_set_bridge_ageing_time__(rstp, RSTP_DEFAULT_AGEING_TIME);
267 rstp_set_bridge_force_protocol_version__(rstp, FPV_DEFAULT);
268 rstp_set_bridge_forward_delay__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
269 rstp_set_bridge_hello_time__(rstp);
270 rstp_set_bridge_max_age__(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
271 rstp_set_bridge_migrate_time__(rstp);
272 rstp_set_bridge_transmit_hold_count__(rstp,
273 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
274 rstp_set_bridge_times__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
275 RSTP_BRIDGE_HELLO_TIME,
276 RSTP_DEFAULT_BRIDGE_MAX_AGE, 0);
277 rstp->send_bpdu = send_bpdu;
279 rstp->changes = false;
282 ovs_refcount_init(&rstp->ref_cnt);
284 list_push_back(all_rstps, &rstp->node);
285 ovs_mutex_unlock(&rstp_mutex);
287 VLOG_DBG("RSTP instance creation done");
291 /* Called by rstp_set_bridge_address() and rstp_set_bridge_priority(),
292 * it updates the bridge priority vector according to the values passed by
296 set_bridge_priority__(struct rstp *rstp)
297 OVS_REQUIRES(rstp_mutex)
301 rstp->bridge_priority.root_bridge_id = rstp->bridge_identifier;
302 rstp->bridge_priority.designated_bridge_id = rstp->bridge_identifier;
303 VLOG_DBG("%s: new bridge identifier: "RSTP_ID_FMT"", rstp->name,
304 RSTP_ID_ARGS(rstp->bridge_identifier));
306 /* [17.13] When the bridge address changes, recalculates all priority
309 HMAP_FOR_EACH (p, node, &rstp->ports) {
313 rstp->changes = true;
314 updt_roles_tree__(rstp);
317 /* Sets the bridge address. */
319 rstp_set_bridge_address__(struct rstp *rstp, rstp_identifier bridge_address)
320 OVS_REQUIRES(rstp_mutex)
322 VLOG_DBG("%s: set bridge address to: "RSTP_ID_FMT"", rstp->name,
323 RSTP_ID_ARGS(bridge_address));
325 rstp->address = bridge_address;
326 rstp->bridge_identifier = bridge_address;
327 set_bridge_priority__(rstp);
330 /* Sets the bridge address. */
332 rstp_set_bridge_address(struct rstp *rstp, rstp_identifier bridge_address)
333 OVS_EXCLUDED(rstp_mutex)
335 ovs_mutex_lock(&rstp_mutex);
336 rstp_set_bridge_address__(rstp, bridge_address);
337 ovs_mutex_unlock(&rstp_mutex);
341 rstp_get_name(const struct rstp *rstp)
342 OVS_EXCLUDED(rstp_mutex)
346 ovs_mutex_lock(&rstp_mutex);
348 ovs_mutex_unlock(&rstp_mutex);
353 rstp_get_bridge_id(const struct rstp *rstp)
354 OVS_EXCLUDED(rstp_mutex)
356 rstp_identifier bridge_id;
358 ovs_mutex_lock(&rstp_mutex);
359 bridge_id = rstp->bridge_identifier;
360 ovs_mutex_unlock(&rstp_mutex);
365 /* Sets the bridge priority. */
367 rstp_set_bridge_priority__(struct rstp *rstp, int new_priority)
368 OVS_REQUIRES(rstp_mutex)
370 new_priority = ROUND_DOWN(new_priority, RSTP_PRIORITY_STEP);
372 if (new_priority >= RSTP_MIN_PRIORITY
373 && new_priority <= RSTP_MAX_PRIORITY) {
374 VLOG_DBG("%s: set bridge priority to %d", rstp->name, new_priority);
376 rstp->priority = new_priority;
377 rstp->bridge_identifier &= 0x0000ffffffffffffULL;
378 rstp->bridge_identifier |= (uint64_t)new_priority << 48;
379 set_bridge_priority__(rstp);
384 rstp_set_bridge_priority(struct rstp *rstp, int new_priority)
385 OVS_EXCLUDED(rstp_mutex)
387 ovs_mutex_lock(&rstp_mutex);
388 rstp_set_bridge_priority__(rstp, new_priority);
389 ovs_mutex_unlock(&rstp_mutex);
392 /* Sets the bridge ageing time. */
394 rstp_set_bridge_ageing_time__(struct rstp *rstp, int new_ageing_time)
395 OVS_REQUIRES(rstp_mutex)
397 if (new_ageing_time >= RSTP_MIN_AGEING_TIME
398 && new_ageing_time <= RSTP_MAX_AGEING_TIME) {
399 VLOG_DBG("%s: set ageing time to %d", rstp->name, new_ageing_time);
401 rstp->ageing_time = new_ageing_time;
406 rstp_set_bridge_ageing_time(struct rstp *rstp, int new_ageing_time)
407 OVS_EXCLUDED(rstp_mutex)
409 ovs_mutex_lock(&rstp_mutex);
410 rstp_set_bridge_ageing_time__(rstp, new_ageing_time);
411 ovs_mutex_unlock(&rstp_mutex);
414 /* Reinitializes RSTP when switching from RSTP mode to STP mode
418 reinitialize_rstp__(struct rstp *rstp)
419 OVS_REQUIRES(rstp_mutex)
422 static struct hmap ports;
425 /* Copy rstp in temp */
429 /* stop and clear rstp */
430 memset(rstp, 0, sizeof(struct rstp));
432 /* Initialize rstp. */
433 rstp->name = temp.name;
435 /* Initialize the ports hmap before calling any setters,
436 * so that the state machines will see an empty ports list. */
437 hmap_init(&rstp->ports);
439 /* Set bridge address. */
440 rstp_set_bridge_address__(rstp, temp.address);
441 /* Set default parameters values. */
442 rstp_set_bridge_priority__(rstp, RSTP_DEFAULT_PRIORITY);
443 rstp_set_bridge_ageing_time__(rstp, RSTP_DEFAULT_AGEING_TIME);
444 rstp_set_bridge_forward_delay__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
445 rstp_set_bridge_hello_time__(rstp);
446 rstp_set_bridge_max_age__(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
447 rstp_set_bridge_migrate_time__(rstp);
448 rstp_set_bridge_transmit_hold_count__(rstp,
449 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
450 rstp_set_bridge_times__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
451 RSTP_BRIDGE_HELLO_TIME,
452 RSTP_DEFAULT_BRIDGE_MAX_AGE, 0);
454 rstp->send_bpdu = temp.send_bpdu;
455 rstp->aux = temp.aux;
456 rstp->node = temp.node;
457 rstp->changes = false;
463 HMAP_FOR_EACH (p, node, &rstp->ports) {
464 reinitialize_port__(p);
467 rstp->ref_cnt = temp.ref_cnt;
470 /* Sets the force protocol version parameter. */
472 rstp_set_bridge_force_protocol_version__(struct rstp *rstp,
473 enum rstp_force_protocol_version new_force_protocol_version)
474 OVS_REQUIRES(rstp_mutex)
476 if (new_force_protocol_version != rstp->force_protocol_version &&
477 (new_force_protocol_version == FPV_STP_COMPATIBILITY ||
478 new_force_protocol_version == FPV_DEFAULT)) {
479 VLOG_DBG("%s: set bridge Force Protocol Version to %d", rstp->name,
480 new_force_protocol_version);
482 /* [17.13] The Spanning Tree Protocol Entity shall be reinitialized,
483 * as specified by the assertion of BEGIN (17.18.1) in the state
484 * machine specification.
486 reinitialize_rstp__(rstp);
487 rstp->force_protocol_version = new_force_protocol_version;
488 if (rstp->force_protocol_version < 2) {
489 rstp->stp_version = true;
490 rstp->rstp_version = false;
492 rstp->stp_version = false;
493 rstp->rstp_version = true;
495 rstp->changes = true;
501 rstp_set_bridge_force_protocol_version(struct rstp *rstp,
502 enum rstp_force_protocol_version new_force_protocol_version)
503 OVS_EXCLUDED(rstp_mutex)
505 ovs_mutex_lock(&rstp_mutex);
506 rstp_set_bridge_force_protocol_version__(rstp, new_force_protocol_version);
507 ovs_mutex_unlock(&rstp_mutex);
510 /* Sets the bridge Hello Time parameter. */
512 rstp_set_bridge_hello_time__(struct rstp *rstp)
513 OVS_REQUIRES(rstp_mutex)
515 VLOG_DBG("%s: set RSTP Hello Time to %d", rstp->name,
516 RSTP_BRIDGE_HELLO_TIME);
517 /* 2 is the only acceptable value. */
518 rstp->bridge_hello_time = RSTP_BRIDGE_HELLO_TIME;
521 /* Sets the bridge max age parameter. */
523 rstp_set_bridge_max_age__(struct rstp *rstp, int new_max_age)
524 OVS_REQUIRES(rstp_mutex)
526 if (rstp->bridge_max_age != new_max_age
527 && new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE
528 && new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) {
530 if ((2 * (rstp->bridge_forward_delay - 1) >= new_max_age)
531 && (new_max_age >= 2 * rstp->bridge_hello_time)) {
532 VLOG_DBG("%s: set RSTP bridge Max Age to %d", rstp->name,
535 rstp->bridge_max_age = new_max_age;
536 rstp->bridge_times.max_age = new_max_age;
537 rstp->changes = true;
538 updt_roles_tree__(rstp);
544 rstp_set_bridge_max_age(struct rstp *rstp, int new_max_age)
545 OVS_EXCLUDED(rstp_mutex)
547 ovs_mutex_lock(&rstp_mutex);
548 rstp_set_bridge_max_age__(rstp, new_max_age);
549 ovs_mutex_unlock(&rstp_mutex);
552 /* Sets the bridge forward delay parameter. */
554 rstp_set_bridge_forward_delay__(struct rstp *rstp, int new_forward_delay)
555 OVS_REQUIRES(rstp_mutex)
557 if (rstp->bridge_forward_delay != new_forward_delay
558 && new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY
559 && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) {
560 if (2 * (new_forward_delay - 1) >= rstp->bridge_max_age) {
561 VLOG_DBG("%s: set RSTP Forward Delay to %d", rstp->name,
563 rstp->bridge_forward_delay = new_forward_delay;
564 rstp->bridge_times.forward_delay = new_forward_delay;
565 rstp->changes = true;
566 updt_roles_tree__(rstp);
572 rstp_set_bridge_forward_delay(struct rstp *rstp, int new_forward_delay)
573 OVS_EXCLUDED(rstp_mutex)
575 ovs_mutex_lock(&rstp_mutex);
576 rstp_set_bridge_forward_delay__(rstp, new_forward_delay);
577 ovs_mutex_unlock(&rstp_mutex);
580 /* Sets the bridge transmit hold count parameter. */
582 rstp_set_bridge_transmit_hold_count__(struct rstp *rstp,
583 int new_transmit_hold_count)
584 OVS_REQUIRES(rstp_mutex)
586 if (new_transmit_hold_count >= RSTP_MIN_TRANSMIT_HOLD_COUNT
587 && new_transmit_hold_count <= RSTP_MAX_TRANSMIT_HOLD_COUNT) {
590 VLOG_DBG("%s: set RSTP Transmit Hold Count to %d", rstp->name,
591 new_transmit_hold_count);
592 /* Resetting txCount on all ports [17.13]. */
594 rstp->transmit_hold_count = new_transmit_hold_count;
595 HMAP_FOR_EACH (p, node, &rstp->ports) {
602 rstp_set_bridge_transmit_hold_count(struct rstp *rstp,
603 int new_transmit_hold_count)
604 OVS_EXCLUDED(rstp_mutex)
606 ovs_mutex_lock(&rstp_mutex);
607 rstp_set_bridge_transmit_hold_count__(rstp, new_transmit_hold_count);
608 ovs_mutex_unlock(&rstp_mutex);
611 /* Sets the bridge migrate time parameter. */
613 rstp_set_bridge_migrate_time__(struct rstp *rstp)
614 OVS_REQUIRES(rstp_mutex)
616 VLOG_DBG("%s: set RSTP Migrate Time to %d", rstp->name,
618 /* 3 is the only acceptable value */
619 rstp->migrate_time = RSTP_MIGRATE_TIME;
622 /* Sets the bridge times. */
624 rstp_set_bridge_times__(struct rstp *rstp, int new_forward_delay,
625 int new_hello_time, int new_max_age,
627 OVS_REQUIRES(rstp_mutex)
629 VLOG_DBG("%s: set RSTP times to (%d, %d, %d, %d)", rstp->name,
630 new_forward_delay, new_hello_time, new_max_age, new_message_age);
631 if (new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY
632 && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) {
633 rstp->bridge_times.forward_delay = new_forward_delay;
635 if (new_hello_time == RSTP_BRIDGE_HELLO_TIME) {
636 rstp->bridge_times.hello_time = new_hello_time;
638 if (new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE
639 && new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) {
640 rstp->bridge_times.max_age = new_max_age;
642 rstp->bridge_times.message_age = new_message_age;
645 /* Sets the port id, it is called by rstp_port_set_port_number__() or
646 * rstp_port_set_priority__().
649 set_port_id__(struct rstp_port *p)
650 OVS_REQUIRES(rstp_mutex)
655 /* [9.2.7] Port identifier. */
656 p->port_id = p->port_number | (p->priority << 8);
657 VLOG_DBG("%s: new RSTP port id "RSTP_PORT_ID_FMT"", rstp->name,
661 /* Sets the port priority. */
663 rstp_port_set_priority__(struct rstp_port *port, int priority)
664 OVS_REQUIRES(rstp_mutex)
666 if (priority >= RSTP_MIN_PORT_PRIORITY
667 && priority <= RSTP_MAX_PORT_PRIORITY) {
668 VLOG_DBG("%s, port %u: set RSTP port priority to %d", port->rstp->name,
669 port->port_number, priority);
671 priority -= priority % RSTP_STEP_PORT_PRIORITY;
672 port->priority = priority;
674 port->selected = false;
675 port->reselect = true;
679 /* Checks if a port number is available. */
681 is_port_number_available__(struct rstp *rstp, int n, struct rstp_port *port)
682 OVS_REQUIRES(rstp_mutex)
684 if (n >= 1 && n <= RSTP_MAX_PORTS) {
685 struct rstp_port *p = rstp_get_port__(rstp, n);
687 return p == NULL || p == port;
693 rstp_first_free_number__(struct rstp *rstp, struct rstp_port *rstp_port)
694 OVS_REQUIRES(rstp_mutex)
698 while (free_number <= RSTP_MAX_PORTS) {
699 if (is_port_number_available__(rstp, free_number, rstp_port)) {
704 VLOG_DBG("%s, No free port number available.", rstp->name);
708 /* Sets the port number. */
710 rstp_port_set_port_number__(struct rstp_port *port, uint16_t port_number)
711 OVS_REQUIRES(rstp_mutex)
713 /* If new_port_number is available, use it, otherwise use the first free
714 * available port number. */
716 is_port_number_available__(port->rstp, port_number, port)
718 : rstp_first_free_number__(port->rstp, port);
721 /* [17.13] is not clear. I suppose that a port number change
722 * should trigger reselection like a port priority change. */
723 port->selected = false;
724 port->reselect = true;
726 VLOG_DBG("%s: set new RSTP port number %d", port->rstp->name,
730 /* Converts the link speed to a port path cost [Table 17-3]. */
732 rstp_convert_speed_to_cost(unsigned int speed)
736 value = speed >= 10000000 ? 2 /* 10 Tb/s. */
737 : speed >= 1000000 ? 20 /* 1 Tb/s. */
738 : speed >= 100000 ? 200 /* 100 Gb/s. */
739 : speed >= 10000 ? 2000 /* 10 Gb/s. */
740 : speed >= 1000 ? 20000 /* 1 Gb/s. */
741 : speed >= 100 ? 200000 /* 100 Mb/s. */
742 : speed >= 10 ? 2000000 /* 10 Mb/s. */
743 : speed >= 1 ? 20000000 /* 1 Mb/s. */
744 : RSTP_DEFAULT_PORT_PATH_COST; /* 100 Mb/s. */
749 /* Sets the port path cost. */
751 rstp_port_set_path_cost__(struct rstp_port *port, uint32_t path_cost)
752 OVS_REQUIRES(rstp_mutex)
754 if (path_cost >= RSTP_MIN_PORT_PATH_COST
755 && path_cost <= RSTP_MAX_PORT_PATH_COST) {
756 VLOG_DBG("%s, port %u, set RSTP port path cost to %d",
757 port->rstp->name, port->port_number, path_cost);
759 port->port_path_cost = path_cost;
760 port->selected = false;
761 port->reselect = true;
765 /* Gets the root path cost. */
767 rstp_get_root_path_cost(const struct rstp *rstp)
768 OVS_EXCLUDED(rstp_mutex)
772 ovs_mutex_lock(&rstp_mutex);
773 cost = rstp->root_priority.root_path_cost;
774 ovs_mutex_unlock(&rstp_mutex);
778 /* Finds a port which needs to flush its own MAC learning table. A NULL
779 * pointer is returned if no port needs to flush its MAC learning table.
780 * '*port' needs to be NULL in the first call to start the iteration. If
781 * '*port' is passed as non-NULL, it must be the value set by the last
782 * invocation of this function. */
784 rstp_check_and_reset_fdb_flush(struct rstp *rstp, struct rstp_port **port)
785 OVS_EXCLUDED(rstp_mutex)
789 ovs_mutex_lock(&rstp_mutex);
793 HMAP_FOR_EACH (p, node, &rstp->ports) {
800 } else { /* continue */
801 struct rstp_port *p = *port;
803 HMAP_FOR_EACH_CONTINUE (p, node, &rstp->ports) {
811 /* No port needs flushing. */
814 /* fdb_flush should be reset by the filtering database
815 * once the entries are removed if rstp_version is TRUE, and
816 * immediately if stp_version is TRUE.*/
818 (*port)->fdb_flush = false;
820 ovs_mutex_unlock(&rstp_mutex);
824 /* Finds a port whose state has changed, and returns the aux pointer set for
825 * the port. A NULL pointer is returned when no changed port is found. On
826 * return '*portp' contains the pointer to the rstp port that changed, or NULL
827 * if no changed port can be found.
829 * If '*portp' is passed as non-NULL, it must be the value set by the last
830 * invocation of this function.
832 * This function may only be called by the thread that creates and deletes
833 * ports. Otherwise this function is not thread safe, as the returned
834 * '*portp' could become stale before it is used in the next invocation. */
836 rstp_get_next_changed_port_aux(struct rstp *rstp, struct rstp_port **portp)
840 ovs_mutex_lock(&rstp_mutex);
841 if (*portp == NULL) {
844 HMAP_FOR_EACH (p, node, &rstp->ports) {
845 if (p->state_changed) {
846 p->state_changed = false;
852 } else { /* continue */
853 struct rstp_port *p = *portp;
855 HMAP_FOR_EACH_CONTINUE (p, node, &rstp->ports) {
856 if (p->state_changed) {
857 p->state_changed = false;
864 /* No changed port found. */
867 ovs_mutex_unlock(&rstp_mutex);
871 /* Returns the port in 'rstp' with number 'port_number'.
873 * XXX: May only be called while concurrent deletion of ports is excluded. */
874 static struct rstp_port *
875 rstp_get_port__(struct rstp *rstp, uint16_t port_number)
876 OVS_REQUIRES(rstp_mutex)
878 struct rstp_port *port;
880 ovs_assert(rstp && port_number > 0 && port_number <= RSTP_MAX_PORTS);
882 HMAP_FOR_EACH_WITH_HASH (port, node, hash_int(port_number, 0),
884 if (port->port_number == port_number) {
892 rstp_get_port(struct rstp *rstp, uint16_t port_number)
893 OVS_EXCLUDED(rstp_mutex)
897 ovs_mutex_lock(&rstp_mutex);
898 p = rstp_get_port__(rstp, port_number);
899 ovs_mutex_unlock(&rstp_mutex);
904 rstp_get_port_aux(struct rstp *rstp, uint16_t port_number)
905 OVS_EXCLUDED(rstp_mutex)
910 ovs_mutex_lock(&rstp_mutex);
911 p = rstp_get_port__(rstp, port_number);
913 ovs_mutex_unlock(&rstp_mutex);
917 /* Updates the port_enabled parameter. */
919 update_port_enabled__(struct rstp_port *p)
920 OVS_REQUIRES(rstp_mutex)
922 if (p->mac_operational && p->is_administrative_bridge_port
923 == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) {
924 p->port_enabled = true;
926 p->port_enabled = false;
930 /* Sets the port MAC_Operational parameter [6.4.2]. */
932 rstp_port_set_mac_operational(struct rstp_port *p, bool new_mac_operational)
933 OVS_EXCLUDED(rstp_mutex)
937 ovs_mutex_lock(&rstp_mutex);
939 if (p->mac_operational != new_mac_operational) {
940 p->mac_operational = new_mac_operational;
941 update_port_enabled__(p);
942 rstp->changes = true;
945 ovs_mutex_unlock(&rstp_mutex);
948 /* Sets the port Administrative Bridge Port parameter. */
950 rstp_port_set_administrative_bridge_port__(struct rstp_port *p,
951 uint8_t admin_port_state)
952 OVS_REQUIRES(rstp_mutex)
954 VLOG_DBG("%s, port %u: set RSTP port admin-port-state to %d",
955 p->rstp->name, p->port_number, admin_port_state);
957 if (admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_DISABLED
958 || admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) {
960 p->is_administrative_bridge_port = admin_port_state;
961 update_port_enabled__(p);
965 /* Sets the port oper_point_to_point_mac parameter. */
967 rstp_port_set_oper_point_to_point_mac__(struct rstp_port *p,
968 uint8_t new_oper_p2p_mac)
969 OVS_REQUIRES(rstp_mutex)
971 if (new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_DISABLED
972 || new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_ENABLED) {
974 p->oper_point_to_point_mac = new_oper_p2p_mac;
975 update_port_enabled__(p);
979 /* Initializes a port with the defaults values for its parameters. */
981 rstp_initialize_port_defaults__(struct rstp_port *p)
982 OVS_REQUIRES(rstp_mutex)
984 rstp_port_set_administrative_bridge_port__(p,
985 RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED);
986 rstp_port_set_oper_point_to_point_mac__(p,
987 RSTP_OPER_P2P_MAC_STATE_ENABLED);
988 rstp_port_set_path_cost__(p, RSTP_DEFAULT_PORT_PATH_COST);
989 rstp_port_set_admin_edge__(p, false);
990 rstp_port_set_auto_edge__(p, true);
991 rstp_port_set_mcheck__(p, false);
993 /* Initialize state machines. */
994 p->port_receive_sm_state = PORT_RECEIVE_SM_INIT;
995 p->port_protocol_migration_sm_state = PORT_PROTOCOL_MIGRATION_SM_INIT;
996 p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_INIT;
997 p->port_transmit_sm_state = PORT_TRANSMIT_SM_INIT;
998 p->port_information_sm_state = PORT_INFORMATION_SM_INIT;
999 p->port_role_transition_sm_state = PORT_ROLE_TRANSITION_SM_INIT;
1000 p->port_state_transition_sm_state = PORT_STATE_TRANSITION_SM_INIT;
1001 p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INIT;
1007 reinitialize_port__(struct rstp_port *p)
1008 OVS_REQUIRES(rstp_mutex)
1010 struct rstp_port temp_port;
1015 memset(p, 0, sizeof(struct rstp_port));
1017 p->ref_cnt = temp_port.ref_cnt;
1019 p->node = temp_port.node;
1020 p->aux = temp_port.aux;
1021 p->port_number = temp_port.port_number;
1022 p->port_priority = temp_port.port_priority;
1023 p->port_id = temp_port.port_id;
1024 p->rstp_state = RSTP_DISCARDING;
1026 rstp_initialize_port_defaults__(p);
1028 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" reinitialized.", rstp->name,
1033 reinitialize_port(struct rstp_port *p)
1034 OVS_EXCLUDED(rstp_mutex)
1036 ovs_mutex_lock(&rstp_mutex);
1037 reinitialize_port__(p);
1038 ovs_mutex_unlock(&rstp_mutex);
1041 /* Sets the port state. */
1043 rstp_port_set_state__(struct rstp_port *p, enum rstp_state state)
1044 OVS_REQUIRES(rstp_mutex)
1049 VLOG_DBG("%s, port %u: set RSTP port state %s -> %s", rstp->name,
1051 rstp_state_name(p->rstp_state), rstp_state_name(state));
1053 if (state != p->rstp_state && !p->state_changed) {
1054 p->state_changed = true;
1055 seq_change(connectivity_seq_get());
1057 p->rstp_state = state;
1061 rstp_port_set_state(struct rstp_port *p, enum rstp_state state)
1062 OVS_EXCLUDED(rstp_mutex)
1064 ovs_mutex_lock(&rstp_mutex);
1065 rstp_port_set_state__(p, state);
1066 ovs_mutex_unlock(&rstp_mutex);
1069 /* Adds a RSTP port. */
1071 rstp_add_port(struct rstp *rstp)
1072 OVS_EXCLUDED(rstp_mutex)
1074 struct rstp_port *p = xzalloc(sizeof *p);
1076 ovs_refcount_init(&p->ref_cnt);
1078 ovs_mutex_lock(&rstp_mutex);
1080 rstp_port_set_priority__(p, RSTP_DEFAULT_PORT_PRIORITY);
1081 rstp_port_set_port_number__(p, 0);
1083 rstp_initialize_port_defaults__(p);
1084 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" initialized.", rstp->name,
1087 rstp_port_set_state__(p, RSTP_DISCARDING);
1088 hmap_insert(&rstp->ports, &p->node, hash_int(p->port_number, 0));
1089 rstp->changes = true;
1091 VLOG_DBG("%s: added port "RSTP_PORT_ID_FMT"", rstp->name, p->port_id);
1092 ovs_mutex_unlock(&rstp_mutex);
1096 /* Caller has to hold a reference to prevent 'rstp_port' from being deleted
1097 * while taking a new reference. */
1099 rstp_port_ref(const struct rstp_port *rp_)
1100 OVS_EXCLUDED(rstp_mutex)
1102 struct rstp_port *rp = CONST_CAST(struct rstp_port *, rp_);
1105 ovs_refcount_ref(&rp->ref_cnt);
1110 /* Frees RSTP struct. This can be caller by any thread. */
1112 rstp_port_unref(struct rstp_port *rp)
1113 OVS_EXCLUDED(rstp_mutex)
1115 if (rp && ovs_refcount_unref_relaxed(&rp->ref_cnt) == 1) {
1118 ovs_mutex_lock(&rstp_mutex);
1120 rstp_port_set_state__(rp, RSTP_DISABLED);
1121 hmap_remove(&rstp->ports, &rp->node);
1122 VLOG_DBG("%s: removed port "RSTP_PORT_ID_FMT"", rstp->name,
1124 ovs_mutex_unlock(&rstp_mutex);
1129 /* Sets the port Admin Edge parameter. */
1131 rstp_port_set_admin_edge__(struct rstp_port *port, bool admin_edge)
1132 OVS_REQUIRES(rstp_mutex)
1134 if (port->admin_edge != admin_edge) {
1135 VLOG_DBG("%s, port %u: set RSTP Admin Edge to %d", port->rstp->name,
1136 port->port_number, admin_edge);
1138 port->admin_edge = admin_edge;
1142 /* Sets the port Auto Edge parameter. */
1144 rstp_port_set_auto_edge__(struct rstp_port *port, bool auto_edge)
1145 OVS_REQUIRES(rstp_mutex)
1147 if (port->auto_edge != auto_edge) {
1148 VLOG_DBG("%s, port %u: set RSTP Auto Edge to %d", port->rstp->name,
1149 port->port_number, auto_edge);
1151 port->auto_edge = auto_edge;
1155 /* Sets the port admin_point_to_point_mac parameter. */
1156 static void rstp_port_set_admin_point_to_point_mac__(struct rstp_port *port,
1157 enum rstp_admin_point_to_point_mac_state admin_p2p_mac_state)
1158 OVS_REQUIRES(rstp_mutex)
1160 VLOG_DBG("%s, port %u: set RSTP port admin-point-to-point-mac to %d",
1161 port->rstp->name, port->port_number, admin_p2p_mac_state);
1163 if (admin_p2p_mac_state == RSTP_ADMIN_P2P_MAC_FORCE_TRUE) {
1164 port->admin_point_to_point_mac = admin_p2p_mac_state;
1165 rstp_port_set_oper_point_to_point_mac__(port,
1166 RSTP_OPER_P2P_MAC_STATE_ENABLED);
1167 } else if (admin_p2p_mac_state == RSTP_ADMIN_P2P_MAC_FORCE_FALSE) {
1168 port->admin_point_to_point_mac = admin_p2p_mac_state;
1169 rstp_port_set_oper_point_to_point_mac__(port,
1170 RSTP_OPER_P2P_MAC_STATE_DISABLED);
1171 } else if (admin_p2p_mac_state == RSTP_ADMIN_P2P_MAC_AUTO) {
1172 /* If adminPointToPointMAC is set to Auto, then the value of
1173 * operPointToPointMAC is determined in accordance with the
1174 * specific procedures defined for the MAC entity concerned, as
1175 * defined in 6.5. If these procedures determine that the MAC
1176 * entity is connected to a point-to-point LAN, then
1177 * operPointToPointMAC is set TRUE; otherwise it is set FALSE.
1178 * In the absence of a specific definition of how to determine
1179 * whether the MAC is connected to a point-to-point LAN or not,
1180 * the value of operPointToPointMAC shall be FALSE. */
1181 port->admin_point_to_point_mac = admin_p2p_mac_state;
1182 rstp_port_set_oper_point_to_point_mac__(
1183 port, RSTP_OPER_P2P_MAC_STATE_DISABLED);
1187 /* Sets the port mcheck parameter.
1188 * [17.19.13] May be set by management to force the Port Protocol Migration
1189 * state machine to transmit RST BPDUs for a MigrateTime (17.13.9) period, to
1190 * test whether all STP Bridges (17.4) on the attached LAN have been removed
1191 * and the Port can continue to transmit RSTP BPDUs. Setting mcheck has no
1192 * effect if stpVersion (17.20.12) is TRUE, i.e., the Bridge is operating in
1193 * STP Compatibility mode.
1196 rstp_port_set_mcheck__(struct rstp_port *port, bool mcheck)
1197 OVS_REQUIRES(rstp_mutex)
1199 if (mcheck == true && port->rstp->force_protocol_version >= 2) {
1200 port->mcheck = true;
1202 VLOG_DBG("%s, port %u: set RSTP mcheck to %d", port->rstp->name,
1203 port->port_number, mcheck);
1207 /* Returns the designated bridge id. */
1209 rstp_get_designated_id(const struct rstp *rstp)
1210 OVS_EXCLUDED(rstp_mutex)
1212 rstp_identifier designated_id;
1214 ovs_mutex_lock(&rstp_mutex);
1215 designated_id = rstp->root_priority.designated_bridge_id;
1216 ovs_mutex_unlock(&rstp_mutex);
1218 return designated_id;
1221 /* Returns the root bridge id. */
1223 rstp_get_root_id(const struct rstp *rstp)
1224 OVS_EXCLUDED(rstp_mutex)
1226 rstp_identifier root_id;
1228 ovs_mutex_lock(&rstp_mutex);
1229 root_id = rstp->root_priority.root_bridge_id;
1230 ovs_mutex_unlock(&rstp_mutex);
1235 /* Returns the designated port id. */
1237 rstp_get_designated_port_id(const struct rstp *rstp)
1238 OVS_EXCLUDED(rstp_mutex)
1240 uint16_t designated_port_id;
1242 ovs_mutex_lock(&rstp_mutex);
1243 designated_port_id = rstp->root_priority.designated_port_id;
1244 ovs_mutex_unlock(&rstp_mutex);
1246 return designated_port_id;
1249 /* Return the bridge port id. */
1251 rstp_get_bridge_port_id(const struct rstp *rstp)
1252 OVS_EXCLUDED(rstp_mutex)
1254 uint16_t bridge_port_id;
1256 ovs_mutex_lock(&rstp_mutex);
1257 bridge_port_id = rstp->root_priority.bridge_port_id;
1258 ovs_mutex_unlock(&rstp_mutex);
1260 return bridge_port_id;
1263 /* Returns true if the bridge believes to the be root of the spanning tree,
1267 rstp_is_root_bridge(const struct rstp *rstp)
1268 OVS_EXCLUDED(rstp_mutex)
1272 ovs_mutex_lock(&rstp_mutex);
1273 is_root = rstp->bridge_identifier ==
1274 rstp->root_priority.designated_bridge_id;
1275 ovs_mutex_unlock(&rstp_mutex);
1280 /* Returns the bridge ID of the bridge currently believed to be the root. */
1282 rstp_get_designated_root(const struct rstp *rstp)
1283 OVS_EXCLUDED(rstp_mutex)
1285 rstp_identifier designated_root;
1287 ovs_mutex_lock(&rstp_mutex);
1288 designated_root = rstp->root_priority.designated_bridge_id;
1289 ovs_mutex_unlock(&rstp_mutex);
1291 return designated_root;
1294 /* Returns the port connecting 'rstp' to the root bridge, or a null pointer if
1295 * there is no such port.
1298 rstp_get_root_port(struct rstp *rstp)
1299 OVS_EXCLUDED(rstp_mutex)
1301 struct rstp_port *p;
1303 ovs_mutex_lock(&rstp_mutex);
1304 HMAP_FOR_EACH (p, node, &rstp->ports) {
1305 if (p->port_id == rstp->root_port_id) {
1306 ovs_mutex_unlock(&rstp_mutex);
1310 ovs_mutex_unlock(&rstp_mutex);
1314 /* Returns the state of port 'p'. */
1316 rstp_port_get_state(const struct rstp_port *p)
1318 enum rstp_state state;
1320 ovs_mutex_lock(&rstp_mutex);
1321 state = p->rstp_state;
1322 ovs_mutex_unlock(&rstp_mutex);
1327 /* Retrieves port status. */
1329 rstp_port_get_status(const struct rstp_port *p, uint16_t *id,
1330 enum rstp_state *state, enum rstp_port_role *role,
1331 rstp_identifier *designated_bridge_id,
1332 uint16_t *designated_port_id,
1333 uint32_t *designated_path_cost, int *tx_count,
1334 int *rx_count, int *error_count, int *uptime)
1335 OVS_EXCLUDED(rstp_mutex)
1337 ovs_mutex_lock(&rstp_mutex);
1339 *state = p->rstp_state;
1342 *designated_bridge_id = p->port_priority.designated_bridge_id;
1343 *designated_port_id = p->port_priority.designated_port_id;
1344 *designated_path_cost = p->port_priority.root_path_cost;
1346 *tx_count = p->tx_count;
1347 *rx_count = p->rx_rstp_bpdu_cnt;
1348 *error_count = p->error_count;
1349 *uptime = p->uptime;
1350 ovs_mutex_unlock(&rstp_mutex);
1354 rstp_port_set(struct rstp_port *port, uint16_t port_num, int priority,
1355 uint32_t path_cost, bool is_admin_edge, bool is_auto_edge,
1356 enum rstp_admin_point_to_point_mac_state admin_p2p_mac_state,
1357 bool admin_port_state, bool do_mcheck, void *aux)
1358 OVS_EXCLUDED(rstp_mutex)
1360 ovs_mutex_lock(&rstp_mutex);
1362 rstp_port_set_priority__(port, priority);
1363 rstp_port_set_port_number__(port, port_num);
1364 rstp_port_set_path_cost__(port, path_cost);
1365 rstp_port_set_admin_edge__(port, is_admin_edge);
1366 rstp_port_set_auto_edge__(port, is_auto_edge);
1367 rstp_port_set_admin_point_to_point_mac__(port, admin_p2p_mac_state);
1368 rstp_port_set_administrative_bridge_port__(port, admin_port_state);
1369 rstp_port_set_mcheck__(port, do_mcheck);
1370 ovs_mutex_unlock(&rstp_mutex);
1373 /* Individual setters only used by test-rstp.c. */
1375 rstp_port_set_priority(struct rstp_port *port, int priority)
1376 OVS_EXCLUDED(rstp_mutex)
1378 ovs_mutex_lock(&rstp_mutex);
1379 rstp_port_set_priority__(port, priority);
1380 ovs_mutex_unlock(&rstp_mutex);
1384 rstp_port_set_path_cost(struct rstp_port *port, uint32_t path_cost)
1385 OVS_EXCLUDED(rstp_mutex)
1387 ovs_mutex_lock(&rstp_mutex);
1388 rstp_port_set_path_cost__(port, path_cost);
1389 ovs_mutex_unlock(&rstp_mutex);
1393 rstp_port_set_aux(struct rstp_port *port, void *aux)
1394 OVS_EXCLUDED(rstp_mutex)
1396 ovs_mutex_lock(&rstp_mutex);
1398 ovs_mutex_unlock(&rstp_mutex);
1402 static struct rstp *
1403 rstp_find(const char *name)
1404 OVS_REQUIRES(rstp_mutex)
1408 LIST_FOR_EACH (rstp, node, all_rstps) {
1409 if (!strcmp(rstp->name, name)) {
1417 rstp_unixctl_tcn(struct unixctl_conn *conn, int argc,
1418 const char *argv[], void *aux OVS_UNUSED)
1419 OVS_EXCLUDED(rstp_mutex)
1421 ovs_mutex_lock(&rstp_mutex);
1423 struct rstp *rstp = rstp_find(argv[1]);
1425 unixctl_command_reply_error(conn, "No such RSTP object");
1428 rstp->changes = true;
1432 LIST_FOR_EACH (rstp, node, all_rstps) {
1433 rstp->changes = true;
1437 unixctl_command_reply(conn, "OK");
1440 ovs_mutex_unlock(&rstp_mutex);