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