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