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