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