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