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