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