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