]> git.proxmox.com Git - mirror_ovs.git/blame - lib/rstp.c
cirrus: Use FreeBSD 12.2.
[mirror_ovs.git] / lib / rstp.c
CommitLineData
9efd308e 1/*
eb1746e6 2 * Copyright (c) 2011-2015 M3S, Srl - Italy
9efd308e
DV
3 *
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:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17/*
18 * Rapid Spanning Tree Protocol (IEEE 802.1D-2004) public interface.
19 *
20 * Authors:
21 * Martino Fornasa <mf@fornasa.it>
22 * Daniele Venturino <daniele.venturino@m3s.it>
eb1746e6 23 * Carlo Andreotti <c.andreotti@m3s.it>
9efd308e
DV
24 *
25 * References to IEEE 802.1D-2004 standard are enclosed in square brackets.
26 * E.g. [17.3], [Table 17-1], etc.
27 *
28 */
29
30#include <config.h>
4b30ba05 31
9efd308e
DV
32#include "rstp.h"
33#include "rstp-common.h"
34#include "rstp-state-machines.h"
b2befd5b
BP
35#include <sys/types.h>
36#include <netinet/in.h>
9efd308e
DV
37#include <arpa/inet.h>
38#include <inttypes.h>
9efd308e 39#include <stdlib.h>
9efd308e
DV
40#include "byte-order.h"
41#include "connectivity.h"
64c96779 42#include "openvswitch/ofpbuf.h"
9efd308e 43#include "ofproto/ofproto.h"
cf62fa4c 44#include "dp-packet.h"
9efd308e
DV
45#include "packets.h"
46#include "seq.h"
47#include "unixctl.h"
48#include "util.h"
e6211adc 49#include "openvswitch/vlog.h"
9efd308e
DV
50
51VLOG_DEFINE_THIS_MODULE(rstp);
52
6b90bc57 53struct ovs_mutex rstp_mutex = OVS_MUTEX_INITIALIZER;
f025bcb7 54
55951e15 55static struct ovs_list all_rstps__ = OVS_LIST_INITIALIZER(&all_rstps__);
ca6ba700 56static struct ovs_list *const all_rstps OVS_GUARDED_BY(rstp_mutex) = &all_rstps__;
f025bcb7
JR
57
58/* Internal use only. */
59static void rstp_set_bridge_address__(struct rstp *, rstp_identifier)
60 OVS_REQUIRES(rstp_mutex);
61static void rstp_set_bridge_priority__(struct rstp *, int new_priority)
62 OVS_REQUIRES(rstp_mutex);
63static void rstp_set_bridge_ageing_time__(struct rstp *, int new_ageing_time)
64 OVS_REQUIRES(rstp_mutex);
65static void rstp_set_bridge_force_protocol_version__(struct rstp *,
66 enum rstp_force_protocol_version)
67 OVS_REQUIRES(rstp_mutex);
68static void rstp_set_bridge_hello_time__(struct rstp *)
69 OVS_REQUIRES(rstp_mutex);
70static void rstp_set_bridge_max_age__(struct rstp *, int new_max_age)
71 OVS_REQUIRES(rstp_mutex);
72static void rstp_set_bridge_forward_delay__(struct rstp *, int new_forward_delay)
73 OVS_REQUIRES(rstp_mutex);
74static void rstp_set_bridge_transmit_hold_count__(struct rstp *,
75 int new_transmit_hold_count)
76 OVS_REQUIRES(rstp_mutex);
77static void rstp_set_bridge_migrate_time__(struct rstp *)
78 OVS_REQUIRES(rstp_mutex);
79static void rstp_set_bridge_times__(struct rstp *, int new_forward_delay,
80 int new_hello_time, int new_max_age,
81 int new_message_age)
82 OVS_REQUIRES(rstp_mutex);
83
84static struct rstp_port *rstp_get_port__(struct rstp *rstp,
85 uint16_t port_number)
86 OVS_REQUIRES(rstp_mutex);
87static void set_port_id__(struct rstp_port *)
88 OVS_REQUIRES(rstp_mutex);
89static void update_port_enabled__(struct rstp_port *)
90 OVS_REQUIRES(rstp_mutex);
91static void set_bridge_priority__(struct rstp *)
92 OVS_REQUIRES(rstp_mutex);
93static void reinitialize_rstp__(struct rstp *)
94 OVS_REQUIRES(rstp_mutex);
95static bool is_port_number_available__(struct rstp *, int, struct rstp_port *)
96 OVS_REQUIRES(rstp_mutex);
97static uint16_t rstp_first_free_number__(struct rstp *, struct rstp_port *)
98 OVS_REQUIRES(rstp_mutex);
99static void rstp_initialize_port_defaults__(struct rstp_port *)
100 OVS_REQUIRES(rstp_mutex);
101static void rstp_port_set_priority__(struct rstp_port *, int priority)
102 OVS_REQUIRES(rstp_mutex);
103static void rstp_port_set_port_number__(struct rstp_port *,
104 uint16_t port_number)
105 OVS_REQUIRES(rstp_mutex);
106static void rstp_port_set_path_cost__(struct rstp_port *, uint32_t path_cost)
107 OVS_REQUIRES(rstp_mutex);
108static void rstp_port_set_administrative_bridge_port__(struct rstp_port *,
37a4efd1
JR
109 uint8_t admin_port_state,
110 bool initializing)
f025bcb7
JR
111 OVS_REQUIRES(rstp_mutex);
112static void rstp_port_set_admin_edge__(struct rstp_port *, bool admin_edge)
113 OVS_REQUIRES(rstp_mutex);
114static void rstp_port_set_auto_edge__(struct rstp_port *, bool auto_edge)
115 OVS_REQUIRES(rstp_mutex);
67e8c1ac
JR
116static void rstp_port_set_admin_point_to_point_mac__(struct rstp_port *,
117 enum rstp_admin_point_to_point_mac_state admin_p2p_mac_state)
118 OVS_REQUIRES(rstp_mutex);
f025bcb7
JR
119static void rstp_port_set_mcheck__(struct rstp_port *, bool mcheck)
120 OVS_REQUIRES(rstp_mutex);
121static void reinitialize_port__(struct rstp_port *p)
122 OVS_REQUIRES(rstp_mutex);
066f0ab4 123static bool rstp_is_root_bridge__(const struct rstp *rstp)
124 OVS_REQUIRES(rstp_mutex);
125static uint32_t rstp_get_root_path_cost__(const struct rstp *rstp)
126 OVS_REQUIRES(rstp_mutex);
127static struct rstp_port *rstp_get_root_port__(const struct rstp *rstp)
128 OVS_REQUIRES(rstp_mutex);
129static rstp_identifier rstp_get_root_id__(const struct rstp *rstp)
130 OVS_REQUIRES(rstp_mutex);
131static void rstp_unixctl_tcn(struct unixctl_conn *, int argc,
132 const char *argv[], void *aux);
cc3a32f3 133static void rstp_unixctl_show(struct unixctl_conn *, int argc,
134 const char *argv[], void *aux);
9efd308e
DV
135
136const char *
137rstp_state_name(enum rstp_state state)
138{
139 switch (state) {
140 case RSTP_DISABLED:
141 return "Disabled";
142 case RSTP_LEARNING:
143 return "Learning";
144 case RSTP_FORWARDING:
145 return "Forwarding";
146 case RSTP_DISCARDING:
147 return "Discarding";
148 default:
149 return "Unknown";
150 }
151}
152
153const char *
154rstp_port_role_name(enum rstp_port_role role)
155{
156 switch (role) {
157 case ROLE_ROOT:
158 return "Root";
159 case ROLE_DESIGNATED:
160 return "Designated";
161 case ROLE_ALTERNATE:
162 return "Alternate";
163 case ROLE_BACKUP:
164 return "Backup";
165 case ROLE_DISABLED:
166 return "Disabled";
167 default:
168 return "Unknown";
169 }
170}
171
4b30ba05 172/* Caller has to hold a reference to prevent 'rstp' from being deleted
f025bcb7 173 * while taking a new reference. */
9efd308e 174struct rstp *
4b30ba05 175rstp_ref(struct rstp *rstp)
f025bcb7 176 OVS_EXCLUDED(rstp_mutex)
9efd308e 177{
9efd308e
DV
178 if (rstp) {
179 ovs_refcount_ref(&rstp->ref_cnt);
180 }
181 return rstp;
182}
183
f025bcb7 184/* Frees RSTP struct when reference count reaches zero. */
9efd308e
DV
185void
186rstp_unref(struct rstp *rstp)
f025bcb7 187 OVS_EXCLUDED(rstp_mutex)
9efd308e 188{
f5920067 189 if (rstp && ovs_refcount_unref_relaxed(&rstp->ref_cnt) == 1) {
f025bcb7
JR
190 ovs_mutex_lock(&rstp_mutex);
191
192 /* Each RSTP port points back to struct rstp without holding a
193 * reference for that pointer. This is OK as we never move
194 * ports from one bridge to another, and holders always
195 * release their ports before releasing the bridge. This
196 * means that there should be not ports at this time. */
d5f31dc8 197 ovs_assert(hmap_is_empty(&rstp->ports));
4b30ba05 198
417e7e66 199 ovs_list_remove(&rstp->node);
f025bcb7 200 ovs_mutex_unlock(&rstp_mutex);
d36de1d1 201 hmap_destroy(&rstp->ports);
9efd308e
DV
202 free(rstp->name);
203 free(rstp);
204 }
205}
206
4b30ba05
JR
207/* Returns the port number. Mutex is needed to guard against
208 * concurrent reinitialization (which can temporarily clear the
209 * port_number). */
9efd308e 210int
f025bcb7
JR
211rstp_port_get_number(const struct rstp_port *p)
212 OVS_EXCLUDED(rstp_mutex)
9efd308e
DV
213{
214 int number;
215
f025bcb7 216 ovs_mutex_lock(&rstp_mutex);
9efd308e 217 number = p->port_number;
f025bcb7
JR
218 ovs_mutex_unlock(&rstp_mutex);
219
9efd308e
DV
220 return number;
221}
222
9efd308e
DV
223/* Decrements the State Machines' timers. */
224void
225rstp_tick_timers(struct rstp *rstp)
f025bcb7 226 OVS_EXCLUDED(rstp_mutex)
9efd308e 227{
f025bcb7
JR
228 ovs_mutex_lock(&rstp_mutex);
229 decrease_rstp_port_timers__(rstp);
230 ovs_mutex_unlock(&rstp_mutex);
9efd308e
DV
231}
232
233/* Processes an incoming BPDU. */
234void
f025bcb7
JR
235rstp_port_received_bpdu(struct rstp_port *rp, const void *bpdu,
236 size_t bpdu_size)
237 OVS_EXCLUDED(rstp_mutex)
9efd308e 238{
f025bcb7
JR
239 ovs_mutex_lock(&rstp_mutex);
240 /* Only process packets on ports that have RSTP enabled. */
241 if (rp && rp->rstp_state != RSTP_DISABLED) {
242 process_received_bpdu__(rp, bpdu, bpdu_size);
243 }
244 ovs_mutex_unlock(&rstp_mutex);
9efd308e
DV
245}
246
247void
248rstp_init(void)
f025bcb7 249 OVS_EXCLUDED(rstp_mutex)
9efd308e 250{
4b30ba05
JR
251 unixctl_command_register("rstp/tcn", "[bridge]", 0, 1, rstp_unixctl_tcn,
252 NULL);
cc3a32f3 253 unixctl_command_register("rstp/show", "[bridge]", 0, 1, rstp_unixctl_show,
254 NULL);
9efd308e
DV
255}
256
257/* Creates and returns a new RSTP instance that initially has no ports. */
258struct rstp *
259rstp_create(const char *name, rstp_identifier bridge_address,
cf62fa4c 260 void (*send_bpdu)(struct dp_packet *bpdu, void *port_aux,
6b90bc57 261 void *rstp_aux),
4b30ba05 262 void *aux)
f025bcb7 263 OVS_EXCLUDED(rstp_mutex)
9efd308e 264{
9efd308e
DV
265 struct rstp *rstp;
266
267 VLOG_DBG("Creating RSTP instance");
9efd308e
DV
268
269 rstp = xzalloc(sizeof *rstp);
270 rstp->name = xstrdup(name);
f025bcb7 271
d5f31dc8
JR
272 /* Initialize the ports map before calling any setters,
273 * so that the state machines will see an empty ports map. */
274 hmap_init(&rstp->ports);
37db915e 275
f025bcb7 276 ovs_mutex_lock(&rstp_mutex);
9efd308e 277 /* Set bridge address. */
f025bcb7 278 rstp_set_bridge_address__(rstp, bridge_address);
9efd308e 279 /* Set default parameters values. */
f025bcb7
JR
280 rstp_set_bridge_priority__(rstp, RSTP_DEFAULT_PRIORITY);
281 rstp_set_bridge_ageing_time__(rstp, RSTP_DEFAULT_AGEING_TIME);
282 rstp_set_bridge_force_protocol_version__(rstp, FPV_DEFAULT);
283 rstp_set_bridge_forward_delay__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
284 rstp_set_bridge_hello_time__(rstp);
285 rstp_set_bridge_max_age__(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
286 rstp_set_bridge_migrate_time__(rstp);
287 rstp_set_bridge_transmit_hold_count__(rstp,
288 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
289 rstp_set_bridge_times__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
290 RSTP_BRIDGE_HELLO_TIME,
291 RSTP_DEFAULT_BRIDGE_MAX_AGE, 0);
9efd308e
DV
292 rstp->send_bpdu = send_bpdu;
293 rstp->aux = aux;
294 rstp->changes = false;
295 rstp->begin = true;
c7e4f0b2
DV
296 rstp->old_root_aux = NULL;
297 rstp->new_root_aux = NULL;
9efd308e 298
9efd308e 299 ovs_refcount_init(&rstp->ref_cnt);
4b30ba05 300
417e7e66 301 ovs_list_push_back(all_rstps, &rstp->node);
f025bcb7 302 ovs_mutex_unlock(&rstp_mutex);
4b30ba05 303
9efd308e
DV
304 VLOG_DBG("RSTP instance creation done");
305 return rstp;
306}
307
308/* Called by rstp_set_bridge_address() and rstp_set_bridge_priority(),
309 * it updates the bridge priority vector according to the values passed by
310 * those setters.
311 */
312static void
313set_bridge_priority__(struct rstp *rstp)
f025bcb7 314 OVS_REQUIRES(rstp_mutex)
9efd308e 315{
37db915e
JR
316 struct rstp_port *p;
317
9efd308e
DV
318 rstp->bridge_priority.root_bridge_id = rstp->bridge_identifier;
319 rstp->bridge_priority.designated_bridge_id = rstp->bridge_identifier;
320 VLOG_DBG("%s: new bridge identifier: "RSTP_ID_FMT"", rstp->name,
321 RSTP_ID_ARGS(rstp->bridge_identifier));
aaab616a
JR
322
323 /* [17.13] When the bridge address changes, recalculates all priority
324 * vectors.
325 */
d5f31dc8 326 HMAP_FOR_EACH (p, node, &rstp->ports) {
37db915e
JR
327 p->selected = false;
328 p->reselect = true;
aaab616a
JR
329 }
330 rstp->changes = true;
f025bcb7 331 updt_roles_tree__(rstp);
9efd308e
DV
332}
333
334/* Sets the bridge address. */
f025bcb7
JR
335static void
336rstp_set_bridge_address__(struct rstp *rstp, rstp_identifier bridge_address)
337 OVS_REQUIRES(rstp_mutex)
9efd308e 338{
9efd308e
DV
339 VLOG_DBG("%s: set bridge address to: "RSTP_ID_FMT"", rstp->name,
340 RSTP_ID_ARGS(bridge_address));
036dc965
DV
341 if (rstp->address != bridge_address) {
342 rstp->address = bridge_address;
343 rstp->bridge_identifier &= 0xffff000000000000ULL;
344 rstp->bridge_identifier |= bridge_address;
345 set_bridge_priority__(rstp);
346 }
f025bcb7
JR
347}
348
349/* Sets the bridge address. */
350void
351rstp_set_bridge_address(struct rstp *rstp, rstp_identifier bridge_address)
352 OVS_EXCLUDED(rstp_mutex)
353{
354 ovs_mutex_lock(&rstp_mutex);
355 rstp_set_bridge_address__(rstp, bridge_address);
356 ovs_mutex_unlock(&rstp_mutex);
9efd308e
DV
357}
358
359const char *
360rstp_get_name(const struct rstp *rstp)
f025bcb7 361 OVS_EXCLUDED(rstp_mutex)
9efd308e
DV
362{
363 char *name;
364
f025bcb7 365 ovs_mutex_lock(&rstp_mutex);
9efd308e 366 name = rstp->name;
f025bcb7 367 ovs_mutex_unlock(&rstp_mutex);
9efd308e
DV
368 return name;
369}
370
371rstp_identifier
372rstp_get_bridge_id(const struct rstp *rstp)
f025bcb7 373 OVS_EXCLUDED(rstp_mutex)
9efd308e
DV
374{
375 rstp_identifier bridge_id;
376
f025bcb7 377 ovs_mutex_lock(&rstp_mutex);
9efd308e 378 bridge_id = rstp->bridge_identifier;
f025bcb7
JR
379 ovs_mutex_unlock(&rstp_mutex);
380
9efd308e
DV
381 return bridge_id;
382}
383
384/* Sets the bridge priority. */
f025bcb7
JR
385static void
386rstp_set_bridge_priority__(struct rstp *rstp, int new_priority)
387 OVS_REQUIRES(rstp_mutex)
9efd308e 388{
aaab616a
JR
389 new_priority = ROUND_DOWN(new_priority, RSTP_PRIORITY_STEP);
390
036dc965
DV
391 if (rstp->priority != new_priority
392 && new_priority >= RSTP_MIN_PRIORITY
aaab616a
JR
393 && new_priority <= RSTP_MAX_PRIORITY) {
394 VLOG_DBG("%s: set bridge priority to %d", rstp->name, new_priority);
9efd308e 395
aaab616a 396 rstp->priority = new_priority;
4b30ba05 397 rstp->bridge_identifier &= 0x0000ffffffffffffULL;
aaab616a 398 rstp->bridge_identifier |= (uint64_t)new_priority << 48;
9efd308e 399 set_bridge_priority__(rstp);
9efd308e
DV
400 }
401}
402
9efd308e 403void
f025bcb7
JR
404rstp_set_bridge_priority(struct rstp *rstp, int new_priority)
405 OVS_EXCLUDED(rstp_mutex)
406{
407 ovs_mutex_lock(&rstp_mutex);
408 rstp_set_bridge_priority__(rstp, new_priority);
409 ovs_mutex_unlock(&rstp_mutex);
410}
411
412/* Sets the bridge ageing time. */
413static void
414rstp_set_bridge_ageing_time__(struct rstp *rstp, int new_ageing_time)
415 OVS_REQUIRES(rstp_mutex)
9efd308e 416{
4b30ba05
JR
417 if (new_ageing_time >= RSTP_MIN_AGEING_TIME
418 && new_ageing_time <= RSTP_MAX_AGEING_TIME) {
9efd308e 419 VLOG_DBG("%s: set ageing time to %d", rstp->name, new_ageing_time);
4b30ba05 420
9efd308e 421 rstp->ageing_time = new_ageing_time;
9efd308e
DV
422 }
423}
424
f025bcb7
JR
425void
426rstp_set_bridge_ageing_time(struct rstp *rstp, int new_ageing_time)
427 OVS_EXCLUDED(rstp_mutex)
428{
429 ovs_mutex_lock(&rstp_mutex);
430 rstp_set_bridge_ageing_time__(rstp, new_ageing_time);
431 ovs_mutex_unlock(&rstp_mutex);
432}
433
9efd308e
DV
434/* Reinitializes RSTP when switching from RSTP mode to STP mode
435 * or vice versa.
436 */
437static void
438reinitialize_rstp__(struct rstp *rstp)
f025bcb7 439 OVS_REQUIRES(rstp_mutex)
9efd308e
DV
440{
441 struct rstp temp;
d5f31dc8 442 static struct hmap ports;
37db915e 443 struct rstp_port *p;
9efd308e
DV
444
445 /* Copy rstp in temp */
446 temp = *rstp;
447 ports = rstp->ports;
4b30ba05 448
9efd308e
DV
449 /* stop and clear rstp */
450 memset(rstp, 0, sizeof(struct rstp));
451
452 /* Initialize rstp. */
453 rstp->name = temp.name;
37db915e 454
d5f31dc8 455 /* Initialize the ports hmap before calling any setters,
37db915e 456 * so that the state machines will see an empty ports list. */
d5f31dc8 457 hmap_init(&rstp->ports);
37db915e 458
9efd308e 459 /* Set bridge address. */
f025bcb7 460 rstp_set_bridge_address__(rstp, temp.address);
9efd308e 461 /* Set default parameters values. */
f025bcb7
JR
462 rstp_set_bridge_priority__(rstp, RSTP_DEFAULT_PRIORITY);
463 rstp_set_bridge_ageing_time__(rstp, RSTP_DEFAULT_AGEING_TIME);
464 rstp_set_bridge_forward_delay__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY);
465 rstp_set_bridge_hello_time__(rstp);
466 rstp_set_bridge_max_age__(rstp, RSTP_DEFAULT_BRIDGE_MAX_AGE);
467 rstp_set_bridge_migrate_time__(rstp);
468 rstp_set_bridge_transmit_hold_count__(rstp,
469 RSTP_DEFAULT_TRANSMIT_HOLD_COUNT);
470 rstp_set_bridge_times__(rstp, RSTP_DEFAULT_BRIDGE_FORWARD_DELAY,
471 RSTP_BRIDGE_HELLO_TIME,
472 RSTP_DEFAULT_BRIDGE_MAX_AGE, 0);
9efd308e
DV
473
474 rstp->send_bpdu = temp.send_bpdu;
475 rstp->aux = temp.aux;
476 rstp->node = temp.node;
477 rstp->changes = false;
478 rstp->begin = true;
4b30ba05 479
37db915e
JR
480 /* Restore ports. */
481 rstp->ports = ports;
4b30ba05 482
d5f31dc8 483 HMAP_FOR_EACH (p, node, &rstp->ports) {
37db915e 484 reinitialize_port__(p);
9efd308e 485 }
37db915e 486
9efd308e
DV
487 rstp->ref_cnt = temp.ref_cnt;
488}
489
490/* Sets the force protocol version parameter. */
f025bcb7
JR
491static void
492rstp_set_bridge_force_protocol_version__(struct rstp *rstp,
9efd308e 493 enum rstp_force_protocol_version new_force_protocol_version)
f025bcb7 494 OVS_REQUIRES(rstp_mutex)
9efd308e
DV
495{
496 if (new_force_protocol_version != rstp->force_protocol_version &&
497 (new_force_protocol_version == FPV_STP_COMPATIBILITY ||
498 new_force_protocol_version == FPV_DEFAULT)) {
499 VLOG_DBG("%s: set bridge Force Protocol Version to %d", rstp->name,
500 new_force_protocol_version);
f025bcb7 501
9efd308e
DV
502 /* [17.13] The Spanning Tree Protocol Entity shall be reinitialized,
503 * as specified by the assertion of BEGIN (17.18.1) in the state
504 * machine specification.
505 */
506 reinitialize_rstp__(rstp);
507 rstp->force_protocol_version = new_force_protocol_version;
508 if (rstp->force_protocol_version < 2) {
509 rstp->stp_version = true;
510 rstp->rstp_version = false;
511 } else {
512 rstp->stp_version = false;
513 rstp->rstp_version = true;
514 }
515 rstp->changes = true;
f025bcb7 516 move_rstp__(rstp);
9efd308e
DV
517 }
518}
519
9efd308e 520void
f025bcb7
JR
521rstp_set_bridge_force_protocol_version(struct rstp *rstp,
522 enum rstp_force_protocol_version new_force_protocol_version)
523 OVS_EXCLUDED(rstp_mutex)
524{
525 ovs_mutex_lock(&rstp_mutex);
526 rstp_set_bridge_force_protocol_version__(rstp, new_force_protocol_version);
527 ovs_mutex_unlock(&rstp_mutex);
528}
529
530/* Sets the bridge Hello Time parameter. */
531static void
532rstp_set_bridge_hello_time__(struct rstp *rstp)
533 OVS_REQUIRES(rstp_mutex)
9efd308e
DV
534{
535 VLOG_DBG("%s: set RSTP Hello Time to %d", rstp->name,
536 RSTP_BRIDGE_HELLO_TIME);
537 /* 2 is the only acceptable value. */
9efd308e 538 rstp->bridge_hello_time = RSTP_BRIDGE_HELLO_TIME;
9efd308e
DV
539}
540
541/* Sets the bridge max age parameter. */
f025bcb7
JR
542static void
543rstp_set_bridge_max_age__(struct rstp *rstp, int new_max_age)
544 OVS_REQUIRES(rstp_mutex)
9efd308e 545{
53a68641
JR
546 if (rstp->bridge_max_age != new_max_age
547 && new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE
548 && new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) {
9efd308e 549 /* [17.13] */
4b30ba05
JR
550 if ((2 * (rstp->bridge_forward_delay - 1) >= new_max_age)
551 && (new_max_age >= 2 * rstp->bridge_hello_time)) {
9efd308e
DV
552 VLOG_DBG("%s: set RSTP bridge Max Age to %d", rstp->name,
553 new_max_age);
f025bcb7 554
9efd308e
DV
555 rstp->bridge_max_age = new_max_age;
556 rstp->bridge_times.max_age = new_max_age;
53a68641
JR
557 rstp->changes = true;
558 updt_roles_tree__(rstp);
9efd308e
DV
559 }
560 }
561}
562
9efd308e 563void
f025bcb7
JR
564rstp_set_bridge_max_age(struct rstp *rstp, int new_max_age)
565 OVS_EXCLUDED(rstp_mutex)
566{
567 ovs_mutex_lock(&rstp_mutex);
568 rstp_set_bridge_max_age__(rstp, new_max_age);
569 ovs_mutex_unlock(&rstp_mutex);
570}
571
572/* Sets the bridge forward delay parameter. */
573static void
574rstp_set_bridge_forward_delay__(struct rstp *rstp, int new_forward_delay)
575 OVS_REQUIRES(rstp_mutex)
9efd308e 576{
53a68641
JR
577 if (rstp->bridge_forward_delay != new_forward_delay
578 && new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY
579 && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) {
9efd308e
DV
580 if (2 * (new_forward_delay - 1) >= rstp->bridge_max_age) {
581 VLOG_DBG("%s: set RSTP Forward Delay to %d", rstp->name,
582 new_forward_delay);
9efd308e
DV
583 rstp->bridge_forward_delay = new_forward_delay;
584 rstp->bridge_times.forward_delay = new_forward_delay;
53a68641
JR
585 rstp->changes = true;
586 updt_roles_tree__(rstp);
9efd308e
DV
587 }
588 }
589}
590
9efd308e 591void
f025bcb7
JR
592rstp_set_bridge_forward_delay(struct rstp *rstp, int new_forward_delay)
593 OVS_EXCLUDED(rstp_mutex)
594{
595 ovs_mutex_lock(&rstp_mutex);
596 rstp_set_bridge_forward_delay__(rstp, new_forward_delay);
597 ovs_mutex_unlock(&rstp_mutex);
598}
599
600/* Sets the bridge transmit hold count parameter. */
601static void
602rstp_set_bridge_transmit_hold_count__(struct rstp *rstp,
603 int new_transmit_hold_count)
604 OVS_REQUIRES(rstp_mutex)
9efd308e 605{
036dc965
DV
606 if (rstp->transmit_hold_count != new_transmit_hold_count
607 && new_transmit_hold_count >= RSTP_MIN_TRANSMIT_HOLD_COUNT
4b30ba05 608 && new_transmit_hold_count <= RSTP_MAX_TRANSMIT_HOLD_COUNT) {
37db915e
JR
609 struct rstp_port *p;
610
9efd308e
DV
611 VLOG_DBG("%s: set RSTP Transmit Hold Count to %d", rstp->name,
612 new_transmit_hold_count);
613 /* Resetting txCount on all ports [17.13]. */
f025bcb7 614
9efd308e 615 rstp->transmit_hold_count = new_transmit_hold_count;
d5f31dc8 616 HMAP_FOR_EACH (p, node, &rstp->ports) {
37db915e 617 p->tx_count = 0;
9efd308e 618 }
9efd308e
DV
619 }
620}
621
9efd308e 622void
f025bcb7
JR
623rstp_set_bridge_transmit_hold_count(struct rstp *rstp,
624 int new_transmit_hold_count)
625 OVS_EXCLUDED(rstp_mutex)
626{
627 ovs_mutex_lock(&rstp_mutex);
628 rstp_set_bridge_transmit_hold_count__(rstp, new_transmit_hold_count);
629 ovs_mutex_unlock(&rstp_mutex);
630}
631
632/* Sets the bridge migrate time parameter. */
633static void
634rstp_set_bridge_migrate_time__(struct rstp *rstp)
635 OVS_REQUIRES(rstp_mutex)
9efd308e
DV
636{
637 VLOG_DBG("%s: set RSTP Migrate Time to %d", rstp->name,
638 RSTP_MIGRATE_TIME);
639 /* 3 is the only acceptable value */
9efd308e 640 rstp->migrate_time = RSTP_MIGRATE_TIME;
9efd308e
DV
641}
642
643/* Sets the bridge times. */
f025bcb7
JR
644static void
645rstp_set_bridge_times__(struct rstp *rstp, int new_forward_delay,
646 int new_hello_time, int new_max_age,
647 int new_message_age)
648 OVS_REQUIRES(rstp_mutex)
9efd308e
DV
649{
650 VLOG_DBG("%s: set RSTP times to (%d, %d, %d, %d)", rstp->name,
651 new_forward_delay, new_hello_time, new_max_age, new_message_age);
4b30ba05
JR
652 if (new_forward_delay >= RSTP_MIN_BRIDGE_FORWARD_DELAY
653 && new_forward_delay <= RSTP_MAX_BRIDGE_FORWARD_DELAY) {
9efd308e 654 rstp->bridge_times.forward_delay = new_forward_delay;
4b30ba05
JR
655 }
656 if (new_hello_time == RSTP_BRIDGE_HELLO_TIME) {
9efd308e 657 rstp->bridge_times.hello_time = new_hello_time;
4b30ba05
JR
658 }
659 if (new_max_age >= RSTP_MIN_BRIDGE_MAX_AGE
660 && new_max_age <= RSTP_MAX_BRIDGE_MAX_AGE) {
9efd308e 661 rstp->bridge_times.max_age = new_max_age;
4b30ba05 662 }
9efd308e
DV
663 rstp->bridge_times.message_age = new_message_age;
664}
665
f025bcb7
JR
666/* Sets the port id, it is called by rstp_port_set_port_number__() or
667 * rstp_port_set_priority__().
9efd308e
DV
668 */
669static void
670set_port_id__(struct rstp_port *p)
f025bcb7 671 OVS_REQUIRES(rstp_mutex)
9efd308e
DV
672{
673 struct rstp *rstp;
674
675 rstp = p->rstp;
676 /* [9.2.7] Port identifier. */
677 p->port_id = p->port_number | (p->priority << 8);
678 VLOG_DBG("%s: new RSTP port id "RSTP_PORT_ID_FMT"", rstp->name,
679 p->port_id);
680}
681
682/* Sets the port priority. */
f025bcb7
JR
683static void
684rstp_port_set_priority__(struct rstp_port *port, int priority)
685 OVS_REQUIRES(rstp_mutex)
9efd308e 686{
036dc965
DV
687 if (port->priority != priority
688 && priority >= RSTP_MIN_PORT_PRIORITY
f025bcb7
JR
689 && priority <= RSTP_MAX_PORT_PRIORITY) {
690 VLOG_DBG("%s, port %u: set RSTP port priority to %d", port->rstp->name,
691 port->port_number, priority);
692
693 priority -= priority % RSTP_STEP_PORT_PRIORITY;
694 port->priority = priority;
695 set_port_id__(port);
696 port->selected = false;
697 port->reselect = true;
9efd308e
DV
698 }
699}
700
801171c1 701/* Checks if a port number is available. */
9efd308e 702static bool
801171c1 703is_port_number_available__(struct rstp *rstp, int n, struct rstp_port *port)
f025bcb7 704 OVS_REQUIRES(rstp_mutex)
9efd308e 705{
801171c1 706 if (n >= 1 && n <= RSTP_MAX_PORTS) {
f025bcb7 707 struct rstp_port *p = rstp_get_port__(rstp, n);
9efd308e 708
801171c1 709 return p == NULL || p == port;
9efd308e 710 }
9efd308e
DV
711 return false;
712}
713
714static uint16_t
801171c1 715rstp_first_free_number__(struct rstp *rstp, struct rstp_port *rstp_port)
f025bcb7 716 OVS_REQUIRES(rstp_mutex)
801171c1
JR
717{
718 int free_number = 1;
9efd308e 719
9efd308e 720 while (free_number <= RSTP_MAX_PORTS) {
801171c1 721 if (is_port_number_available__(rstp, free_number, rstp_port)) {
9efd308e
DV
722 return free_number;
723 }
724 free_number++;
725 }
9efd308e
DV
726 VLOG_DBG("%s, No free port number available.", rstp->name);
727 return 0;
728}
729
730/* Sets the port number. */
f025bcb7
JR
731static void
732rstp_port_set_port_number__(struct rstp_port *port, uint16_t port_number)
733 OVS_REQUIRES(rstp_mutex)
9efd308e 734{
2372c146
JR
735 int old_port_number = port->port_number;
736
801171c1
JR
737 /* If new_port_number is available, use it, otherwise use the first free
738 * available port number. */
036dc965
DV
739 if (port->port_number != port_number || port_number == 0) {
740 port->port_number =
741 is_port_number_available__(port->rstp, port_number, port)
742 ? port_number
743 : rstp_first_free_number__(port->rstp, port);
744
745 if (port->port_number != old_port_number) {
746 set_port_id__(port);
747 /* [17.13] is not clear. I suppose that a port number change
748 * should trigger reselection like a port priority change. */
749 port->selected = false;
750 port->reselect = true;
751
752 /* Adjust the ports hmap. */
753 if (!hmap_node_is_null(&port->node)) {
754 hmap_remove(&port->rstp->ports, &port->node);
755 }
756 hmap_insert(&port->rstp->ports, &port->node,
757 hash_int(port->port_number, 0));
2372c146 758
036dc965
DV
759 VLOG_DBG("%s: set new RSTP port number %d", port->rstp->name,
760 port->port_number);
2372c146 761 }
2372c146 762 }
9efd308e
DV
763}
764
8d2f8375 765static void
766rstp_port_set_port_name__(struct rstp_port *port, const char *name)
767 OVS_REQUIRES(rstp_mutex)
768{
769 free(port->port_name);
770 port->port_name = xstrdup(name);
771}
772
9efd308e
DV
773/* Converts the link speed to a port path cost [Table 17-3]. */
774uint32_t
775rstp_convert_speed_to_cost(unsigned int speed)
776{
777 uint32_t value;
778
779 value = speed >= 10000000 ? 2 /* 10 Tb/s. */
780 : speed >= 1000000 ? 20 /* 1 Tb/s. */
781 : speed >= 100000 ? 200 /* 100 Gb/s. */
782 : speed >= 10000 ? 2000 /* 10 Gb/s. */
783 : speed >= 1000 ? 20000 /* 1 Gb/s. */
784 : speed >= 100 ? 200000 /* 100 Mb/s. */
785 : speed >= 10 ? 2000000 /* 10 Mb/s. */
786 : speed >= 1 ? 20000000 /* 1 Mb/s. */
787 : RSTP_DEFAULT_PORT_PATH_COST; /* 100 Mb/s. */
788
789 return value;
790}
791
792/* Sets the port path cost. */
f025bcb7
JR
793static void
794rstp_port_set_path_cost__(struct rstp_port *port, uint32_t path_cost)
795 OVS_REQUIRES(rstp_mutex)
9efd308e 796{
036dc965
DV
797 if (port->port_path_cost != path_cost
798 && path_cost >= RSTP_MIN_PORT_PATH_COST
f025bcb7
JR
799 && path_cost <= RSTP_MAX_PORT_PATH_COST) {
800 VLOG_DBG("%s, port %u, set RSTP port path cost to %d",
801 port->rstp->name, port->port_number, path_cost);
802
803 port->port_path_cost = path_cost;
804 port->selected = false;
805 port->reselect = true;
9efd308e
DV
806 }
807}
808
809/* Gets the root path cost. */
066f0ab4 810static uint32_t
811rstp_get_root_path_cost__(const struct rstp *rstp)
812 OVS_REQUIRES(rstp_mutex)
813{
814 return rstp->root_priority.root_path_cost;
815}
816
9efd308e
DV
817uint32_t
818rstp_get_root_path_cost(const struct rstp *rstp)
f025bcb7 819 OVS_EXCLUDED(rstp_mutex)
9efd308e
DV
820{
821 uint32_t cost;
822
f025bcb7 823 ovs_mutex_lock(&rstp_mutex);
066f0ab4 824 cost = rstp_get_root_path_cost__(rstp);
f025bcb7 825 ovs_mutex_unlock(&rstp_mutex);
9efd308e
DV
826 return cost;
827}
828
66ff280d
JR
829/* Finds a port which needs to flush its own MAC learning table. A NULL
830 * pointer is returned if no port needs to flush its MAC learning table.
831 * '*port' needs to be NULL in the first call to start the iteration. If
832 * '*port' is passed as non-NULL, it must be the value set by the last
2372c146
JR
833 * invocation of this function.
834 *
835 * This function may only be called by the thread that creates and deletes
836 * ports. Otherwise this function is not thread safe, as the returned
837 * '*port' could become stale before it is used in the next invocation. */
66ff280d
JR
838void *
839rstp_check_and_reset_fdb_flush(struct rstp *rstp, struct rstp_port **port)
f025bcb7 840 OVS_EXCLUDED(rstp_mutex)
9efd308e 841{
66ff280d 842 void *aux = NULL;
9efd308e 843
f025bcb7 844 ovs_mutex_lock(&rstp_mutex);
66ff280d
JR
845 if (*port == NULL) {
846 struct rstp_port *p;
847
848 HMAP_FOR_EACH (p, node, &rstp->ports) {
849 if (p->fdb_flush) {
850 aux = p->aux;
851 *port = p;
852 goto out;
853 }
854 }
855 } else { /* continue */
856 struct rstp_port *p = *port;
857
858 HMAP_FOR_EACH_CONTINUE (p, node, &rstp->ports) {
859 if (p->fdb_flush) {
860 aux = p->aux;
861 *port = p;
862 goto out;
863 }
9efd308e
DV
864 }
865 }
66ff280d
JR
866 /* No port needs flushing. */
867 *port = NULL;
868out:
869 /* fdb_flush should be reset by the filtering database
870 * once the entries are removed if rstp_version is TRUE, and
871 * immediately if stp_version is TRUE.*/
872 if (*port != NULL) {
873 (*port)->fdb_flush = false;
874 }
f025bcb7 875 ovs_mutex_unlock(&rstp_mutex);
2372c146 876
66ff280d 877 return aux;
2372c146 878}
9efd308e 879
f025bcb7
JR
880/* Finds a port whose state has changed, and returns the aux pointer set for
881 * the port. A NULL pointer is returned when no changed port is found. On
882 * return '*portp' contains the pointer to the rstp port that changed, or NULL
883 * if no changed port can be found.
4b30ba05 884 *
f025bcb7
JR
885 * If '*portp' is passed as non-NULL, it must be the value set by the last
886 * invocation of this function.
887 *
888 * This function may only be called by the thread that creates and deletes
889 * ports. Otherwise this function is not thread safe, as the returned
890 * '*portp' could become stale before it is used in the next invocation. */
891void *
892rstp_get_next_changed_port_aux(struct rstp *rstp, struct rstp_port **portp)
9efd308e 893{
f025bcb7 894 void *aux = NULL;
9efd308e 895
f025bcb7
JR
896 ovs_mutex_lock(&rstp_mutex);
897 if (*portp == NULL) {
4b30ba05
JR
898 struct rstp_port *p;
899
d5f31dc8 900 HMAP_FOR_EACH (p, node, &rstp->ports) {
9efd308e
DV
901 if (p->state_changed) {
902 p->state_changed = false;
f025bcb7
JR
903 aux = p->aux;
904 *portp = p;
905 goto out;
906 }
907 }
908 } else { /* continue */
909 struct rstp_port *p = *portp;
910
d5f31dc8 911 HMAP_FOR_EACH_CONTINUE (p, node, &rstp->ports) {
f025bcb7
JR
912 if (p->state_changed) {
913 p->state_changed = false;
914 aux = p->aux;
9efd308e 915 *portp = p;
f025bcb7 916 goto out;
9efd308e
DV
917 }
918 }
919 }
f025bcb7 920 /* No changed port found. */
9efd308e 921 *portp = NULL;
f025bcb7
JR
922out:
923 ovs_mutex_unlock(&rstp_mutex);
2372c146 924
f025bcb7 925 return aux;
9efd308e
DV
926}
927
2372c146
JR
928bool
929rstp_shift_root_learned_address(struct rstp *rstp)
930{
931 bool ret;
932
933 ovs_mutex_lock(&rstp_mutex);
934 ret = rstp->root_changed;
935 ovs_mutex_unlock(&rstp_mutex);
936
937 return ret;
938}
939
940void *
941rstp_get_old_root_aux(struct rstp *rstp)
942{
943 void *aux;
944
945 ovs_mutex_lock(&rstp_mutex);
946 aux = rstp->old_root_aux;
947 ovs_mutex_unlock(&rstp_mutex);
948
949 return aux;
950}
951
952void *
953rstp_get_new_root_aux(struct rstp *rstp)
954{
955 void *aux;
956
957 ovs_mutex_lock(&rstp_mutex);
958 aux = rstp->new_root_aux;
959 ovs_mutex_unlock(&rstp_mutex);
960
961 return aux;
962}
963
964void
965rstp_reset_root_changed(struct rstp *rstp)
966{
967 ovs_mutex_lock(&rstp_mutex);
968 rstp->root_changed = false;
969 ovs_mutex_unlock(&rstp_mutex);
970}
971
54d3863b
JR
972/* Returns the port in 'rstp' with number 'port_number'.
973 *
974 * XXX: May only be called while concurrent deletion of ports is excluded. */
f025bcb7
JR
975static struct rstp_port *
976rstp_get_port__(struct rstp *rstp, uint16_t port_number)
977 OVS_REQUIRES(rstp_mutex)
9efd308e
DV
978{
979 struct rstp_port *port;
980
f025bcb7
JR
981 ovs_assert(rstp && port_number > 0 && port_number <= RSTP_MAX_PORTS);
982
d5f31dc8
JR
983 HMAP_FOR_EACH_WITH_HASH (port, node, hash_int(port_number, 0),
984 &rstp->ports) {
37db915e
JR
985 if (port->port_number == port_number) {
986 return port;
9efd308e
DV
987 }
988 }
9efd308e
DV
989 return NULL;
990}
991
f025bcb7
JR
992struct rstp_port *
993rstp_get_port(struct rstp *rstp, uint16_t port_number)
994 OVS_EXCLUDED(rstp_mutex)
995{
996 struct rstp_port *p;
997
998 ovs_mutex_lock(&rstp_mutex);
999 p = rstp_get_port__(rstp, port_number);
1000 ovs_mutex_unlock(&rstp_mutex);
1001 return p;
1002}
1003
1004void *
2372c146
JR
1005rstp_get_port_aux__(struct rstp *rstp, uint16_t port_number)
1006 OVS_REQUIRES(rstp_mutex)
f025bcb7
JR
1007{
1008 struct rstp_port *p;
f025bcb7 1009 p = rstp_get_port__(rstp, port_number);
2372c146
JR
1010 if (p) {
1011 return p->aux;
1012 }
1013 return NULL;
f025bcb7
JR
1014}
1015
9efd308e
DV
1016/* Updates the port_enabled parameter. */
1017static void
1018update_port_enabled__(struct rstp_port *p)
f025bcb7 1019 OVS_REQUIRES(rstp_mutex)
9efd308e 1020{
54d3863b
JR
1021 if (p->mac_operational && p->is_administrative_bridge_port
1022 == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED) {
9efd308e
DV
1023 p->port_enabled = true;
1024 } else {
1025 p->port_enabled = false;
1026 }
1027}
1028
1029/* Sets the port MAC_Operational parameter [6.4.2]. */
1030void
1031rstp_port_set_mac_operational(struct rstp_port *p, bool new_mac_operational)
f025bcb7 1032 OVS_EXCLUDED(rstp_mutex)
9efd308e
DV
1033{
1034 struct rstp *rstp;
1035
f025bcb7 1036 ovs_mutex_lock(&rstp_mutex);
9efd308e 1037 rstp = p->rstp;
f025bcb7
JR
1038 if (p->mac_operational != new_mac_operational) {
1039 p->mac_operational = new_mac_operational;
1040 update_port_enabled__(p);
1041 rstp->changes = true;
1042 move_rstp__(rstp);
1043 }
1044 ovs_mutex_unlock(&rstp_mutex);
9efd308e
DV
1045}
1046
1047/* Sets the port Administrative Bridge Port parameter. */
f025bcb7
JR
1048static void
1049rstp_port_set_administrative_bridge_port__(struct rstp_port *p,
37a4efd1
JR
1050 uint8_t admin_port_state,
1051 bool initializing)
f025bcb7 1052 OVS_REQUIRES(rstp_mutex)
9efd308e 1053{
67e8c1ac
JR
1054 VLOG_DBG("%s, port %u: set RSTP port admin-port-state to %d",
1055 p->rstp->name, p->port_number, admin_port_state);
1056
036dc965
DV
1057 if (p->is_administrative_bridge_port != admin_port_state
1058 && (admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_DISABLED
1059 || admin_port_state == RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED)) {
f025bcb7 1060 p->is_administrative_bridge_port = admin_port_state;
9efd308e 1061 update_port_enabled__(p);
37a4efd1
JR
1062
1063 if (!initializing) {
1064 struct rstp *rstp = p->rstp;
1065
1066 rstp->changes = true;
1067 move_rstp__(rstp);
1068 }
9efd308e
DV
1069 }
1070}
1071
1072/* Sets the port oper_point_to_point_mac parameter. */
f025bcb7
JR
1073static void
1074rstp_port_set_oper_point_to_point_mac__(struct rstp_port *p,
1075 uint8_t new_oper_p2p_mac)
1076 OVS_REQUIRES(rstp_mutex)
9efd308e 1077{
036dc965
DV
1078 if (p->oper_point_to_point_mac != new_oper_p2p_mac
1079 && (new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_DISABLED
1080 || new_oper_p2p_mac == RSTP_OPER_P2P_MAC_STATE_ENABLED)) {
f025bcb7 1081
9efd308e
DV
1082 p->oper_point_to_point_mac = new_oper_p2p_mac;
1083 update_port_enabled__(p);
1084 }
1085}
1086
1087/* Initializes a port with the defaults values for its parameters. */
1088static void
54d3863b 1089rstp_initialize_port_defaults__(struct rstp_port *p)
f025bcb7 1090 OVS_REQUIRES(rstp_mutex)
9efd308e 1091{
f025bcb7 1092 rstp_port_set_administrative_bridge_port__(p,
37a4efd1
JR
1093 RSTP_ADMIN_BRIDGE_PORT_STATE_ENABLED,
1094 true);
7ac97d76
DV
1095 rstp_port_set_oper_point_to_point_mac__(p,
1096 RSTP_OPER_P2P_MAC_STATE_ENABLED);
f025bcb7
JR
1097 rstp_port_set_path_cost__(p, RSTP_DEFAULT_PORT_PATH_COST);
1098 rstp_port_set_admin_edge__(p, false);
1099 rstp_port_set_auto_edge__(p, true);
1100 rstp_port_set_mcheck__(p, false);
9efd308e 1101
54d3863b 1102 /* Initialize state machines. */
9efd308e
DV
1103 p->port_receive_sm_state = PORT_RECEIVE_SM_INIT;
1104 p->port_protocol_migration_sm_state = PORT_PROTOCOL_MIGRATION_SM_INIT;
1105 p->bridge_detection_sm_state = BRIDGE_DETECTION_SM_INIT;
1106 p->port_transmit_sm_state = PORT_TRANSMIT_SM_INIT;
1107 p->port_information_sm_state = PORT_INFORMATION_SM_INIT;
1108 p->port_role_transition_sm_state = PORT_ROLE_TRANSITION_SM_INIT;
1109 p->port_state_transition_sm_state = PORT_STATE_TRANSITION_SM_INIT;
1110 p->topology_change_sm_state = TOPOLOGY_CHANGE_SM_INIT;
9efd308e
DV
1111 p->uptime = 0;
1112
9efd308e
DV
1113}
1114
54d3863b
JR
1115static void
1116reinitialize_port__(struct rstp_port *p)
f025bcb7 1117 OVS_REQUIRES(rstp_mutex)
9efd308e
DV
1118{
1119 struct rstp_port temp_port;
1120 struct rstp *rstp;
1121
1122 rstp = p->rstp;
1123 temp_port = *p;
1124 memset(p, 0, sizeof(struct rstp_port));
f025bcb7
JR
1125
1126 p->ref_cnt = temp_port.ref_cnt;
9efd308e
DV
1127 p->rstp = rstp;
1128 p->node = temp_port.node;
1129 p->aux = temp_port.aux;
1130 p->port_number = temp_port.port_number;
1131 p->port_priority = temp_port.port_priority;
1132 p->port_id = temp_port.port_id;
1133 p->rstp_state = RSTP_DISCARDING;
1134
54d3863b 1135 rstp_initialize_port_defaults__(p);
9efd308e
DV
1136
1137 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" reinitialized.", rstp->name,
54d3863b
JR
1138 p->port_id);
1139}
1140
1141void
1142reinitialize_port(struct rstp_port *p)
f025bcb7 1143 OVS_EXCLUDED(rstp_mutex)
54d3863b 1144{
f025bcb7 1145 ovs_mutex_lock(&rstp_mutex);
54d3863b 1146 reinitialize_port__(p);
f025bcb7 1147 ovs_mutex_unlock(&rstp_mutex);
9efd308e
DV
1148}
1149
1150/* Sets the port state. */
1151void
f025bcb7
JR
1152rstp_port_set_state__(struct rstp_port *p, enum rstp_state state)
1153 OVS_REQUIRES(rstp_mutex)
9efd308e
DV
1154{
1155 struct rstp *rstp;
1156
1157 rstp = p->rstp;
1158 VLOG_DBG("%s, port %u: set RSTP port state %s -> %s", rstp->name,
1159 p->port_number,
1160 rstp_state_name(p->rstp_state), rstp_state_name(state));
1161
1162 if (state != p->rstp_state && !p->state_changed) {
1163 p->state_changed = true;
1164 seq_change(connectivity_seq_get());
1165 }
1166 p->rstp_state = state;
1167}
1168
f025bcb7
JR
1169void
1170rstp_port_set_state(struct rstp_port *p, enum rstp_state state)
1171 OVS_EXCLUDED(rstp_mutex)
1172{
1173 ovs_mutex_lock(&rstp_mutex);
1174 rstp_port_set_state__(p, state);
1175 ovs_mutex_unlock(&rstp_mutex);
1176}
1177
9efd308e
DV
1178/* Adds a RSTP port. */
1179struct rstp_port *
cc33c223 1180rstp_add_port(struct rstp *rstp)
f025bcb7 1181 OVS_EXCLUDED(rstp_mutex)
cc33c223 1182{
9efd308e
DV
1183 struct rstp_port *p = xzalloc(sizeof *p);
1184
f025bcb7 1185 ovs_refcount_init(&p->ref_cnt);
2372c146 1186 hmap_node_nullify(&p->node);
f025bcb7
JR
1187
1188 ovs_mutex_lock(&rstp_mutex);
9efd308e 1189 p->rstp = rstp;
f025bcb7
JR
1190 rstp_port_set_priority__(p, RSTP_DEFAULT_PORT_PRIORITY);
1191 rstp_port_set_port_number__(p, 0);
54d3863b 1192 p->aux = NULL;
8d2f8375 1193 p->port_name = NULL;
54d3863b
JR
1194 rstp_initialize_port_defaults__(p);
1195 VLOG_DBG("%s: RSTP port "RSTP_PORT_ID_FMT" initialized.", rstp->name,
1196 p->port_id);
1197
f025bcb7 1198 rstp_port_set_state__(p, RSTP_DISCARDING);
9efd308e 1199 rstp->changes = true;
f025bcb7 1200 move_rstp__(rstp);
9efd308e 1201 VLOG_DBG("%s: added port "RSTP_PORT_ID_FMT"", rstp->name, p->port_id);
f025bcb7 1202 ovs_mutex_unlock(&rstp_mutex);
9efd308e
DV
1203 return p;
1204}
1205
f025bcb7
JR
1206/* Caller has to hold a reference to prevent 'rstp_port' from being deleted
1207 * while taking a new reference. */
1208struct rstp_port *
1209rstp_port_ref(const struct rstp_port *rp_)
1210 OVS_EXCLUDED(rstp_mutex)
cc33c223 1211{
f025bcb7 1212 struct rstp_port *rp = CONST_CAST(struct rstp_port *, rp_);
9efd308e 1213
f025bcb7
JR
1214 if (rp) {
1215 ovs_refcount_ref(&rp->ref_cnt);
1216 }
1217 return rp;
9efd308e
DV
1218}
1219
f025bcb7 1220/* Frees RSTP struct. This can be caller by any thread. */
9efd308e 1221void
f025bcb7
JR
1222rstp_port_unref(struct rstp_port *rp)
1223 OVS_EXCLUDED(rstp_mutex)
9efd308e 1224{
f5920067 1225 if (rp && ovs_refcount_unref_relaxed(&rp->ref_cnt) == 1) {
cc33c223
JR
1226 struct rstp *rstp;
1227
f025bcb7
JR
1228 ovs_mutex_lock(&rstp_mutex);
1229 rstp = rp->rstp;
1230 rstp_port_set_state__(rp, RSTP_DISABLED);
8d2f8375 1231 free(rp->port_name);
d5f31dc8 1232 hmap_remove(&rstp->ports, &rp->node);
37db915e
JR
1233 VLOG_DBG("%s: removed port "RSTP_PORT_ID_FMT"", rstp->name,
1234 rp->port_id);
f025bcb7
JR
1235 ovs_mutex_unlock(&rstp_mutex);
1236 free(rp);
1237 }
1238}
1239
1240/* Sets the port Admin Edge parameter. */
1241static void
1242rstp_port_set_admin_edge__(struct rstp_port *port, bool admin_edge)
1243 OVS_REQUIRES(rstp_mutex)
1244{
1245 if (port->admin_edge != admin_edge) {
1246 VLOG_DBG("%s, port %u: set RSTP Admin Edge to %d", port->rstp->name,
1247 port->port_number, admin_edge);
1248
1249 port->admin_edge = admin_edge;
9efd308e
DV
1250 }
1251}
1252
1253/* Sets the port Auto Edge parameter. */
f025bcb7
JR
1254static void
1255rstp_port_set_auto_edge__(struct rstp_port *port, bool auto_edge)
1256 OVS_REQUIRES(rstp_mutex)
9efd308e 1257{
f025bcb7
JR
1258 if (port->auto_edge != auto_edge) {
1259 VLOG_DBG("%s, port %u: set RSTP Auto Edge to %d", port->rstp->name,
1260 port->port_number, auto_edge);
cc33c223 1261
f025bcb7 1262 port->auto_edge = auto_edge;
9efd308e
DV
1263 }
1264}
1265
67e8c1ac
JR
1266/* Sets the port admin_point_to_point_mac parameter. */
1267static void rstp_port_set_admin_point_to_point_mac__(struct rstp_port *port,
1268 enum rstp_admin_point_to_point_mac_state admin_p2p_mac_state)
1269 OVS_REQUIRES(rstp_mutex)
1270{
1271 VLOG_DBG("%s, port %u: set RSTP port admin-point-to-point-mac to %d",
1272 port->rstp->name, port->port_number, admin_p2p_mac_state);
036dc965
DV
1273 if (port->admin_point_to_point_mac != admin_p2p_mac_state) {
1274 if (admin_p2p_mac_state == RSTP_ADMIN_P2P_MAC_FORCE_TRUE) {
1275 port->admin_point_to_point_mac = admin_p2p_mac_state;
1276 rstp_port_set_oper_point_to_point_mac__(
1277 port, RSTP_OPER_P2P_MAC_STATE_ENABLED);
1278 } else if (admin_p2p_mac_state == RSTP_ADMIN_P2P_MAC_FORCE_FALSE) {
1279 port->admin_point_to_point_mac = admin_p2p_mac_state;
1280 rstp_port_set_oper_point_to_point_mac__(
1281 port, RSTP_OPER_P2P_MAC_STATE_DISABLED);
1282 } else if (admin_p2p_mac_state == RSTP_ADMIN_P2P_MAC_AUTO) {
1283 /* If adminPointToPointMAC is set to Auto, then the value of
1284 * operPointToPointMAC is determined in accordance with the
1285 * specific procedures defined for the MAC entity concerned, as
1286 * defined in 6.5. If these procedures determine that the MAC
1287 * entity is connected to a point-to-point LAN, then
1288 * operPointToPointMAC is set TRUE; otherwise it is set FALSE.
1289 * In the absence of a specific definition of how to determine
1290 * whether the MAC is connected to a point-to-point LAN or not,
1291 * the value of operPointToPointMAC shall be FALSE. */
1292 port->admin_point_to_point_mac = admin_p2p_mac_state;
1293 rstp_port_set_oper_point_to_point_mac__(
1294 port, RSTP_OPER_P2P_MAC_STATE_DISABLED);
1295 }
67e8c1ac
JR
1296 }
1297}
1298
9efd308e
DV
1299/* Sets the port mcheck parameter.
1300 * [17.19.13] May be set by management to force the Port Protocol Migration
1301 * state machine to transmit RST BPDUs for a MigrateTime (17.13.9) period, to
1302 * test whether all STP Bridges (17.4) on the attached LAN have been removed
1303 * and the Port can continue to transmit RSTP BPDUs. Setting mcheck has no
1304 * effect if stpVersion (17.20.12) is TRUE, i.e., the Bridge is operating in
f025bcb7 1305 * STP Compatibility mode.
9efd308e 1306 */
f025bcb7
JR
1307static void
1308rstp_port_set_mcheck__(struct rstp_port *port, bool mcheck)
1309 OVS_REQUIRES(rstp_mutex)
9efd308e 1310{
f025bcb7
JR
1311 if (mcheck == true && port->rstp->force_protocol_version >= 2) {
1312 port->mcheck = true;
9efd308e 1313
f025bcb7
JR
1314 VLOG_DBG("%s, port %u: set RSTP mcheck to %d", port->rstp->name,
1315 port->port_number, mcheck);
9efd308e 1316 }
9efd308e
DV
1317}
1318
1319/* Returns the designated bridge id. */
1320rstp_identifier
1321rstp_get_designated_id(const struct rstp *rstp)
f025bcb7 1322 OVS_EXCLUDED(rstp_mutex)
9efd308e
DV
1323{
1324 rstp_identifier designated_id;
1325
f025bcb7 1326 ovs_mutex_lock(&rstp_mutex);
9efd308e 1327 designated_id = rstp->root_priority.designated_bridge_id;
f025bcb7 1328 ovs_mutex_unlock(&rstp_mutex);
cc33c223 1329
9efd308e
DV
1330 return designated_id;
1331}
1332
1333/* Returns the root bridge id. */
066f0ab4 1334static rstp_identifier
1335rstp_get_root_id__(const struct rstp *rstp)
1336 OVS_REQUIRES(rstp_mutex)
1337{
1338 return rstp->root_priority.root_bridge_id;
1339}
1340
9efd308e
DV
1341rstp_identifier
1342rstp_get_root_id(const struct rstp *rstp)
f025bcb7 1343 OVS_EXCLUDED(rstp_mutex)
9efd308e
DV
1344{
1345 rstp_identifier root_id;
1346
f025bcb7 1347 ovs_mutex_lock(&rstp_mutex);
066f0ab4 1348 root_id = rstp_get_root_id__(rstp);
f025bcb7 1349 ovs_mutex_unlock(&rstp_mutex);
cc33c223 1350
9efd308e
DV
1351 return root_id;
1352}
1353
1354/* Returns the designated port id. */
1355uint16_t
1356rstp_get_designated_port_id(const struct rstp *rstp)
f025bcb7 1357 OVS_EXCLUDED(rstp_mutex)
9efd308e
DV
1358{
1359 uint16_t designated_port_id;
1360
f025bcb7 1361 ovs_mutex_lock(&rstp_mutex);
9efd308e 1362 designated_port_id = rstp->root_priority.designated_port_id;
f025bcb7 1363 ovs_mutex_unlock(&rstp_mutex);
cc33c223 1364
9efd308e
DV
1365 return designated_port_id;
1366}
1367
1368/* Return the bridge port id. */
1369uint16_t
1370rstp_get_bridge_port_id(const struct rstp *rstp)
f025bcb7 1371 OVS_EXCLUDED(rstp_mutex)
9efd308e
DV
1372{
1373 uint16_t bridge_port_id;
1374
f025bcb7 1375 ovs_mutex_lock(&rstp_mutex);
9efd308e 1376 bridge_port_id = rstp->root_priority.bridge_port_id;
f025bcb7 1377 ovs_mutex_unlock(&rstp_mutex);
cc33c223 1378
9efd308e
DV
1379 return bridge_port_id;
1380}
1381
1382/* Returns true if the bridge believes to the be root of the spanning tree,
1383 * false otherwise.
1384 */
066f0ab4 1385static bool
1386rstp_is_root_bridge__(const struct rstp *rstp)
1387 OVS_REQUIRES(rstp_mutex)
1388{
1389 return rstp->bridge_identifier ==
1390 rstp->root_priority.designated_bridge_id;
1391}
1392
9efd308e
DV
1393bool
1394rstp_is_root_bridge(const struct rstp *rstp)
f025bcb7 1395 OVS_EXCLUDED(rstp_mutex)
9efd308e
DV
1396{
1397 bool is_root;
1398
f025bcb7 1399 ovs_mutex_lock(&rstp_mutex);
066f0ab4 1400 is_root = rstp_is_root_bridge__(rstp);
f025bcb7 1401 ovs_mutex_unlock(&rstp_mutex);
cc33c223 1402
9efd308e
DV
1403 return is_root;
1404}
1405
1406/* Returns the bridge ID of the bridge currently believed to be the root. */
1407rstp_identifier
1408rstp_get_designated_root(const struct rstp *rstp)
f025bcb7 1409 OVS_EXCLUDED(rstp_mutex)
9efd308e
DV
1410{
1411 rstp_identifier designated_root;
1412
f025bcb7 1413 ovs_mutex_lock(&rstp_mutex);
9efd308e 1414 designated_root = rstp->root_priority.designated_bridge_id;
f025bcb7 1415 ovs_mutex_unlock(&rstp_mutex);
cc33c223 1416
9efd308e
DV
1417 return designated_root;
1418}
1419
1420/* Returns the port connecting 'rstp' to the root bridge, or a null pointer if
1421 * there is no such port.
1422 */
066f0ab4 1423static struct rstp_port *
1424rstp_get_root_port__(const struct rstp *rstp)
1425 OVS_REQUIRES(rstp_mutex)
9efd308e
DV
1426{
1427 struct rstp_port *p;
1428
d5f31dc8 1429 HMAP_FOR_EACH (p, node, &rstp->ports) {
37db915e 1430 if (p->port_id == rstp->root_port_id) {
37db915e 1431 return p;
9efd308e
DV
1432 }
1433 }
9efd308e
DV
1434 return NULL;
1435}
1436
066f0ab4 1437struct rstp_port *
1438rstp_get_root_port(const struct rstp *rstp)
1439 OVS_EXCLUDED(rstp_mutex)
1440{
1441 ovs_mutex_lock(&rstp_mutex);
1442 struct rstp_port *p = rstp_get_root_port__(rstp);
1443 ovs_mutex_unlock(&rstp_mutex);
1444 return p;
1445}
1446
9efd308e
DV
1447/* Returns the state of port 'p'. */
1448enum rstp_state
1449rstp_port_get_state(const struct rstp_port *p)
2372c146 1450 OVS_EXCLUDED(rstp_mutex)
9efd308e
DV
1451{
1452 enum rstp_state state;
1453
f025bcb7 1454 ovs_mutex_lock(&rstp_mutex);
9efd308e 1455 state = p->rstp_state;
f025bcb7 1456 ovs_mutex_unlock(&rstp_mutex);
cc33c223 1457
9efd308e
DV
1458 return state;
1459}
1460
f025bcb7 1461/* Retrieves port status. */
9efd308e 1462void
f025bcb7
JR
1463rstp_port_get_status(const struct rstp_port *p, uint16_t *id,
1464 enum rstp_state *state, enum rstp_port_role *role,
9c64e6b8
JR
1465 rstp_identifier *designated_bridge_id,
1466 uint16_t *designated_port_id,
1467 uint32_t *designated_path_cost, int *tx_count,
1468 int *rx_count, int *error_count, int *uptime)
f025bcb7 1469 OVS_EXCLUDED(rstp_mutex)
9efd308e 1470{
f025bcb7
JR
1471 ovs_mutex_lock(&rstp_mutex);
1472 *id = p->port_id;
1473 *state = p->rstp_state;
1474 *role = p->role;
1475
9c64e6b8
JR
1476 *designated_bridge_id = p->port_priority.designated_bridge_id;
1477 *designated_port_id = p->port_priority.designated_port_id;
1478 *designated_path_cost = p->port_priority.root_path_cost;
1479
9efd308e
DV
1480 *tx_count = p->tx_count;
1481 *rx_count = p->rx_rstp_bpdu_cnt;
1482 *error_count = p->error_count;
1483 *uptime = p->uptime;
f025bcb7 1484 ovs_mutex_unlock(&rstp_mutex);
9efd308e
DV
1485}
1486
1487void
f025bcb7
JR
1488rstp_port_set(struct rstp_port *port, uint16_t port_num, int priority,
1489 uint32_t path_cost, bool is_admin_edge, bool is_auto_edge,
67e8c1ac 1490 enum rstp_admin_point_to_point_mac_state admin_p2p_mac_state,
8d2f8375 1491 bool admin_port_state, bool do_mcheck, void *aux,
1492 const char *name)
f025bcb7 1493 OVS_EXCLUDED(rstp_mutex)
9efd308e 1494{
f025bcb7
JR
1495 ovs_mutex_lock(&rstp_mutex);
1496 port->aux = aux;
1497 rstp_port_set_priority__(port, priority);
1498 rstp_port_set_port_number__(port, port_num);
8d2f8375 1499 rstp_port_set_port_name__(port, name);
f025bcb7
JR
1500 rstp_port_set_path_cost__(port, path_cost);
1501 rstp_port_set_admin_edge__(port, is_admin_edge);
1502 rstp_port_set_auto_edge__(port, is_auto_edge);
67e8c1ac 1503 rstp_port_set_admin_point_to_point_mac__(port, admin_p2p_mac_state);
37a4efd1 1504 rstp_port_set_administrative_bridge_port__(port, admin_port_state, false);
f025bcb7
JR
1505 rstp_port_set_mcheck__(port, do_mcheck);
1506 ovs_mutex_unlock(&rstp_mutex);
9efd308e
DV
1507}
1508
f025bcb7
JR
1509/* Individual setters only used by test-rstp.c. */
1510void
1511rstp_port_set_priority(struct rstp_port *port, int priority)
1512 OVS_EXCLUDED(rstp_mutex)
9efd308e 1513{
f025bcb7
JR
1514 ovs_mutex_lock(&rstp_mutex);
1515 rstp_port_set_priority__(port, priority);
1516 ovs_mutex_unlock(&rstp_mutex);
1517}
9efd308e 1518
f025bcb7
JR
1519void
1520rstp_port_set_path_cost(struct rstp_port *port, uint32_t path_cost)
1521 OVS_EXCLUDED(rstp_mutex)
1522{
1523 ovs_mutex_lock(&rstp_mutex);
1524 rstp_port_set_path_cost__(port, path_cost);
1525 ovs_mutex_unlock(&rstp_mutex);
1526}
cc33c223 1527
f025bcb7
JR
1528void
1529rstp_port_set_aux(struct rstp_port *port, void *aux)
1530 OVS_EXCLUDED(rstp_mutex)
1531{
1532 ovs_mutex_lock(&rstp_mutex);
1533 port->aux = aux;
1534 ovs_mutex_unlock(&rstp_mutex);
9efd308e
DV
1535}
1536
9efd308e
DV
1537/* Unixctl. */
1538static struct rstp *
cc33c223 1539rstp_find(const char *name)
f025bcb7 1540 OVS_REQUIRES(rstp_mutex)
9efd308e
DV
1541{
1542 struct rstp *rstp;
1543
1544 LIST_FOR_EACH (rstp, node, all_rstps) {
1545 if (!strcmp(rstp->name, name)) {
1546 return rstp;
1547 }
1548 }
1549 return NULL;
1550}
1551
1552static void
1553rstp_unixctl_tcn(struct unixctl_conn *conn, int argc,
1554 const char *argv[], void *aux OVS_UNUSED)
f025bcb7 1555 OVS_EXCLUDED(rstp_mutex)
9efd308e 1556{
f025bcb7 1557 ovs_mutex_lock(&rstp_mutex);
9efd308e
DV
1558 if (argc > 1) {
1559 struct rstp *rstp = rstp_find(argv[1]);
1560 if (!rstp) {
1561 unixctl_command_reply_error(conn, "No such RSTP object");
1562 goto out;
1563 }
1564 rstp->changes = true;
f025bcb7 1565 move_rstp__(rstp);
9efd308e
DV
1566 } else {
1567 struct rstp *rstp;
1568 LIST_FOR_EACH (rstp, node, all_rstps) {
1569 rstp->changes = true;
f025bcb7 1570 move_rstp__(rstp);
9efd308e
DV
1571 }
1572 }
1573 unixctl_command_reply(conn, "OK");
1574
1575out:
f025bcb7 1576 ovs_mutex_unlock(&rstp_mutex);
9efd308e 1577}
cc3a32f3 1578
1579static void
1580rstp_bridge_id_details(struct ds *ds, const rstp_identifier bridge_id,
1581 uint16_t hello_time, uint16_t max_age,
1582 uint16_t forward_delay)
1583 OVS_REQUIRES(rstp_mutex)
1584{
1585 uint16_t priority = bridge_id >> 48;
aa141053 1586 ds_put_format(ds, " stp-priority %"PRIu16"\n", priority);
cc3a32f3 1587
1588 struct eth_addr mac;
1589 const uint64_t mac_bits = (UINT64_C(1) << 48) - 1;
1590 eth_addr_from_uint64(bridge_id & mac_bits, &mac);
aa141053
BP
1591 ds_put_format(ds, " stp-system-id "ETH_ADDR_FMT"\n", ETH_ADDR_ARGS(mac));
1592 ds_put_format(ds, " stp-hello-time %"PRIu16"s\n", hello_time);
1593 ds_put_format(ds, " stp-max-age %"PRIu16"s\n", max_age);
1594 ds_put_format(ds, " stp-fwd-delay %"PRIu16"s\n", forward_delay);
cc3a32f3 1595}
1596
1597static void
1598rstp_print_details(struct ds *ds, const struct rstp *rstp)
1599 OVS_REQUIRES(rstp_mutex)
1600{
1601 ds_put_format(ds, "---- %s ----\n", rstp->name);
1602
cc3a32f3 1603 ds_put_cstr(ds, "Root ID:\n");
c88f3556
BP
1604 if (rstp_is_root_bridge__(rstp)) {
1605 rstp_bridge_id_details(ds, rstp->bridge_identifier,
1606 rstp->bridge_hello_time,
1607 rstp->bridge_max_age,
1608 rstp->bridge_forward_delay);
aa141053 1609 ds_put_cstr(ds, " This bridge is the root\n");
cc3a32f3 1610 } else {
c88f3556
BP
1611 struct rstp_port *root_port = rstp_get_root_port__(rstp);
1612 if (!root_port) {
1613 ds_put_cstr(ds, "unknown root port\n");
1614 return;
1615 }
1616
1617 rstp_bridge_id_details(ds, rstp_get_root_id__(rstp),
1618 root_port->designated_times.hello_time,
1619 root_port->designated_times.max_age,
1620 root_port->designated_times.forward_delay);
aa141053
BP
1621 ds_put_format(ds, " root-port %s\n", root_port->port_name);
1622 ds_put_format(ds, " root-path-cost %u\n",
cc3a32f3 1623 rstp_get_root_path_cost__(rstp));
1624 }
1625 ds_put_cstr(ds, "\n");
1626
1627 ds_put_cstr(ds, "Bridge ID:\n");
1628 rstp_bridge_id_details(ds, rstp->bridge_identifier,
1629 rstp->bridge_hello_time,
1630 rstp->bridge_max_age,
1631 rstp->bridge_forward_delay);
1632 ds_put_cstr(ds, "\n");
1633
aa141053 1634 ds_put_format(ds, " %-11.10s%-11.10s%-11.10s%-9.8s%-8.7s\n",
cc3a32f3 1635 "Interface", "Role", "State", "Cost", "Pri.Nbr");
aa141053 1636 ds_put_cstr(ds, " ---------- ---------- ---------- -------- -------\n");
c88f3556
BP
1637
1638 struct rstp_port *p;
cc3a32f3 1639 HMAP_FOR_EACH (p, node, &rstp->ports) {
1640 if (p->rstp_state != RSTP_DISABLED) {
aa141053 1641 ds_put_format(ds, " %-11.10s",
cc3a32f3 1642 p->port_name ? p->port_name : "null");
1643 ds_put_format(ds, "%-11.10s", rstp_port_role_name(p->role));
1644 ds_put_format(ds, "%-11.10s", rstp_state_name(p->rstp_state));
1645 ds_put_format(ds, "%-9d", p->port_path_cost);
1646 ds_put_format(ds, "%d.%d\n", p->priority, p->port_number);
1647 }
1648 }
1649
1650 ds_put_cstr(ds, "\n");
1651}
1652
1653static void
1654rstp_unixctl_show(struct unixctl_conn *conn, int argc,
1655 const char *argv[], void *aux OVS_UNUSED)
1656 OVS_EXCLUDED(rstp_mutex)
1657{
1658 struct ds ds = DS_EMPTY_INITIALIZER;
1659
1660 ovs_mutex_lock(&rstp_mutex);
1661 if (argc > 1) {
1662 struct rstp *rstp = rstp_find(argv[1]);
1663
1664 if (!rstp) {
1665 unixctl_command_reply_error(conn, "No such RSTP object");
1666 goto out;
1667 }
1668
1669 rstp_print_details(&ds, rstp);
1670 } else {
1671 struct rstp *rstp;
1672
1673 LIST_FOR_EACH (rstp, node, all_rstps) {
1674 rstp_print_details(&ds, rstp);
1675 }
1676 }
1677
1678 unixctl_command_reply(conn, ds_cstr(&ds));
1679 ds_destroy(&ds);
1680
1681out:
1682 ovs_mutex_unlock(&rstp_mutex);
1683}