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