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