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