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