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