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